WiredLanTask.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. /********************************************************************************
  2. * File Name: WiredLanTask
  3. * Function Describe:The Wired Lan Task
  4. * Relate Module:
  5. * Explain:
  6. * Writer: slw
  7. * Date:
  8. *******************************************************************************/
  9. #define THIS_FILE_ID 11
  10. //---------------------------------------------------------------------------------
  11. #include "includes.h"
  12. #include "spi.h"
  13. #include "socket.h"
  14. #include "Internet/TcpEchoServer.h"
  15. #include "Internet/dhcp.h"
  16. #include "Internet/dns.h"
  17. /* Private typedef -----------------------------------------------------------*/
  18. /* Private define ------------------------------------------------------------*/
  19. //SOCKET 分配
  20. #define SOCK_UDPS_DHCP 0 //DHCP 用于动态分配本地IP
  21. #define SOCK_UDPS_DNS 1 //DNS Clinet
  22. #define SOCK_TCPS_APP1 2 //Clinet
  23. #define SOCK_TCPS_LOOPBACK 7 //回环测试
  24. //uint8_t DHCP_MSG_BUF[548];
  25. #define DATA_BUF_SIZE 1050
  26. uint8_t gDATABUF[DATA_BUF_SIZE];
  27. SUT_WL_STATUS sutWLanStatus;
  28. /* Private macro -------------------------------------------------------------*/
  29. OS_TID idWLanTask=0;
  30. U64 stkWLANTask[WIRE_LAN_TASK_STK_SIZE];
  31. // Default Network Configuration
  32. wiz_NetInfo gWIZNETINFO = { .mac = {0x00, 0x08, 0xdc,0x00, 0xab, 0xcd},
  33. .ip = {0, 0, 0, 0},
  34. .sn = {255,255,255,0},
  35. .gw = {0, 0, 0, 0},
  36. .dns = {0,0,0,0},
  37. .dhcp = NETINFO_DHCP };//dhcp:=NETINFO_STATIC or NETINFO_DHCP
  38. /* Private variables ---------------------------------------------------------*/
  39. /* Private function prototypes -----------------------------------------------*/
  40. /* Private functions ---------------------------------------------------------*/
  41. void network_init(void); // Initialize Network information and display it
  42. void cb_ip_assign(void); //callback func when IP is assigned from DHCP server first
  43. void cb_ip_update(void); // callback func when IP is changed
  44. void cb_ip_conflict(void); // callback func when the assigned IP is conflict with others.
  45. uint8_t ClientInit(void);
  46. void ClientRun(void);
  47. int32_t WiredSendTcpData(uint8_t *data,uint16_t len);
  48. void SetIoMode(uint8_t iomode);//SOCK_IO_NONBLOCK;//SOCK_IO_BLOCK;
  49. int SendLogin2Server(void);
  50. int SendCurPosition2Server(void);
  51. /* ************************************************************************
  52. W5500 PMODE配置
  53. 111: ALL CAPABLE,Auto-Negotiation enable
  54. * ************************************************************************/
  55. void W5500_PMODE_CONFIG(void)
  56. {
  57. GPIO_InitTypeDef GPIO_InitStruct;
  58. //PB3->PMODE0,PB4->PMODE1,PB5->PMODE2
  59. GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4|GPIO_Pin_5;
  60. GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  61. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
  62. GPIO_Init(GPIOB, &GPIO_InitStruct);
  63. GPIO_SetBits(GPIOB,(GPIO_Pin_3 | GPIO_Pin_4|GPIO_Pin_5));
  64. }
  65. /* ************************************************************************
  66. W5500 RESET 配置
  67. (Active low) RESET should be held low at least 500 us for W550
  68. * ************************************************************************/
  69. void W5500_RESET_CONFIG(void)
  70. {
  71. GPIO_InitTypeDef GPIO_InitStruct;
  72. //PB2->RESET
  73. GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
  74. GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  75. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
  76. GPIO_Init(GPIOB, &GPIO_InitStruct);
  77. GPIO_ResetBits(GPIOB,GPIO_Pin_2);
  78. os_dly_wait(1);
  79. GPIO_SetBits(GPIOB,GPIO_Pin_2);
  80. }
  81. /*
  82. 链接服务器
  83. type=0 app服务器
  84. type=1 server服务器
  85. 成功 返回1
  86. 失败 返回0
  87. */
  88. int8_t Connect2Server(int type)
  89. {
  90. uint8_t mode;
  91. char tracebuf[22];
  92. int8_t r;
  93. if(type){
  94. SlwTrace(DEBUG,"[WL]Connect2FoTaServer\r\n");
  95. if(sutServerIp.FotaServerIP[0]==0){
  96. SlwTrace(DEBUG,"SOCKERR_IPINVALID\r\n");
  97. os_dly_wait(500);
  98. return 0;
  99. }
  100. r=connect(SOCK_TCPS_APP1,sutServerIp.FotaServerIP,sutProductPara.FotaPort);
  101. }else{
  102. SlwTrace(DEBUG,"[WL]Connect2AppServer\r\n");
  103. if(sutServerIp.AppServerIP[0]==0){
  104. SlwTrace(DEBUG,"SOCKERR_IPINVALID\r\n");
  105. os_dly_wait(500);
  106. return 0;
  107. }
  108. r=connect(SOCK_TCPS_APP1,sutServerIp.AppServerIP,sutProductPara.AppPort);
  109. }
  110. if(r==SOCK_OK){
  111. SlwTrace(DEBUG,"Connect OK!\r\n");
  112. return 1;
  113. }else if(r==SOCKERR_SOCKINIT || r==SOCKERR_SOCKMODE){//socket未初始化,初始化之
  114. SlwTrace(DEBUG,"socket init...\r\n");
  115. mode=SOCK_IO_NONBLOCK;
  116. socket(SOCK_TCPS_APP1,Sn_MR_TCP,6000,SF_TCP_NODELAY);
  117. ctlsocket(SOCK_TCPS_APP1,CS_SET_IOMODE,&mode);
  118. }else if(r==SOCKERR_TIMEOUT){
  119. SlwTrace(DEBUG,"Connect timeout!\r\n");
  120. //os_dly_wait(1000);
  121. }else {
  122. sprintf(tracebuf,"connect err=%d\r\n",r);
  123. SlwTrace(DEBUG,tracebuf);
  124. }
  125. return 0;
  126. }
  127. /*
  128. */
  129. void Disconnect2Server(void)
  130. {
  131. disconnect(SOCK_TCPS_APP1);
  132. SlwTrace(DEBUG,"[WL]CloseSocket!\r\n");
  133. }
  134. //int32_t SendReg2Server(void)
  135. //{
  136. // int r,len;
  137. // SlwTrace(DEBUG,">>SendReg>>\r\n");
  138. // sutModemStatus.TcpSendTimeCt++;
  139. // sutTestData.NetMode=2;//LAN模式上传
  140. // len=TcpPackingReg(g_ucTcpSendBuf);
  141. // if(len==0)return -1;
  142. // Debugsend(g_ucTcpSendBuf, len);//打印发送的数据
  143. // r = WiredSendTcpData(g_ucTcpSendBuf,len);
  144. // return r;
  145. //}
  146. //int32_t SendGetFileInfo2Server(void)
  147. //{
  148. // int r,len;
  149. // SlwTrace(DEBUG,">>SendGetFileInfo>>\r\n");
  150. // len=PacketGetFileInfo(g_ucTcpSendBuf);
  151. // r = WiredSendTcpData(g_ucTcpSendBuf,len);
  152. // return r;
  153. //}
  154. //uint8_t TcpData[400];
  155. //int32_t RecvDataFromServer()
  156. //{
  157. // char tracebuf[20];
  158. // int32_t r;
  159. // r=recv(SOCK_TCPS_APP1,gDATABUF, sizeof(gDATABUF));
  160. // if(r>0){
  161. // snprintf(tracebuf,sizeof(tracebuf),"<Recv[%d]<\r\n",r);
  162. // SlwTrace(DEBUG,tracebuf);
  163. // gDATABUF[r]=0;
  164. // SlwTrace(DEBUG,(char *)gDATABUF);
  165. // TcpRecvData(gDATABUF,r);
  166. // }
  167. // return r;
  168. //}
  169. //int32_t SendTest2Server(void)
  170. //{
  171. // int16_t len;
  172. // SlwTrace(DEBUG,">>SendUpload>>\r\n");
  173. // sutTestData.CSQ=0;
  174. // sutTestData.NetMode=2;
  175. // len=TcpPackingSendTest(g_ucTcpSendBuf);
  176. // return WiredSendTcpData(g_ucTcpSendBuf,len);
  177. //}
  178. void WiredLanRecv(unsigned char *pData,unsigned short len)
  179. {
  180. unsigned short UploadInterval,SamplingInterval;
  181. unsigned short AckID,AckNum;
  182. unsigned char Result;
  183. unsigned short NewVer;
  184. int cmd;
  185. int i;
  186. char buf[30];
  187. sutWLanStatus.ServerOfflineCt=0;
  188. sprintf(buf,"[WL]Recv<<%d\r\n",len);
  189. SlwTrace(DEBUG,buf);
  190. //FoTa升级接收数据处理
  191. if(sutFotaPara.fotaStatus!=FOTA_END){
  192. FoTaRecvData(pData,len);
  193. return;
  194. }
  195. //正常应用接收数据处理
  196. cmd=CheckPacket(pData,len);
  197. if(cmd<0){
  198. sprintf(buf,"[WL]CheckPacket Err=%d\r\n",cmd);
  199. SlwTrace(DEBUG,buf);
  200. return;
  201. }
  202. switch(cmd)
  203. {
  204. case 0x8200://上传应答
  205. //pData=8001 0005 000000000000 0000 01 02 03 04 86
  206. // ID 属性 设备编号 流水号 结果 红外使能 摄像头使能 复位使能 校验
  207. SlwTrace(DEBUG,"[WL]<<UploadAck\r\n");
  208. AckNum=((unsigned short)pData[10]<<8) | pData[11];
  209. sutWLanStatus.RecvCmdNum=AckNum;
  210. if(pData[12]!=0){//结果 0--成功
  211. return;
  212. };
  213. sutTestData.InfraredEn=pData[13];
  214. sutTestData.CameraEn=pData[14];
  215. sutTestData.ResetEn=pData[15];
  216. //sprintf(buf,"[WL]IEn=%02X CEn=%02X REn=%02X\r\n",sutTestData.InfraredEn,sutTestData.CameraEn,sutTestData.ResetEn);
  217. //SlwTrace(DEBUG,buf);
  218. break;
  219. case 0x8929://登陆应答
  220. //pData=8929 000E 000190210001 0018 190217142738 01 00 0000 0500 0500 01
  221. // ID Len 编码 流水号 时间 验证 升级 新版本 采样间隔 上传间隔 校验
  222. SlwTrace(DEBUG,"[WL]<<LoginAck\r\n");
  223. //TraceData(pData,25);
  224. AckNum=((unsigned short)pData[10]<<8)|pData[11];
  225. sutWLanStatus.RecvCmdNum=AckNum;
  226. // sprintf(buf,"RFlowId=%d\r\n",sutWLanStatus.RecvCmdNum);
  227. // SlwTrace(DEBUG,buf);
  228. RTC_Set(&pData[12]);
  229. //鉴权结果 0--成功 >0 未成功需要继续等N分钟再重新登陆
  230. sutWLanStatus.UnauthorizedCt=(unsigned short)pData[18];
  231. if(sutWLanStatus.UnauthorizedCt>0){
  232. sutWLanStatus.UnauthorizedCt*=60;
  233. SlwTrace(DEBUG,"Unauthorized!\r\n");
  234. sprintf(buf,"UCt=%d\r\n",sutWLanStatus.UnauthorizedCt);
  235. SlwTrace(DEBUG,buf);
  236. }
  237. if(pData[19]==1){//使能升级
  238. NewVer=((unsigned short)pData[20]<<8)|pData[21];
  239. if(NewVer!=0 && NewVer!=sutProductPara.SoftwareVer && sutModemStatus.FoTaGetFileInfoCt<5){
  240. sprintf(buf,"[WL]CurVer=%d NewVer=%d\r\n",sutProductPara.SoftwareVer,NewVer );
  241. SlwTrace(DEBUG,buf);
  242. SlwTrace(DEBUG,"[WL]Upgrade now!\r\n");
  243. sutFotaPara.targetVersion=NewVer;
  244. sutFotaPara.fotaStatus=FOTA_GET_FILEINFO;
  245. Disconnect2Server();
  246. sutWLanStatus.Step=WL_STEP_CONNETING;
  247. return;
  248. }else{
  249. sutWLanStatus.FoTaGetFileInfoCt=0;
  250. }
  251. }else sutWLanStatus.FoTaGetFileInfoCt=0;
  252. UploadInterval=((unsigned short)pData[22]<<8)|pData[23];
  253. SamplingInterval=((unsigned short)pData[24]<<8)|pData[25];
  254. if((sutDeviceConfig.UploadInterval!=UploadInterval&& UploadInterval>=5) ||
  255. (sutDeviceConfig.SamplingInterval!=SamplingInterval && SamplingInterval>=5)){
  256. sutDeviceConfig.UploadInterval=UploadInterval;
  257. sutDeviceConfig.SamplingInterval=SamplingInterval;
  258. SaveDeviceConfigToFlash();
  259. }
  260. break;
  261. }
  262. }
  263. void WireLanInit(void)
  264. {
  265. uint8_t memsize[2][8] = {{2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2}};
  266. W5500_PMODE_CONFIG();
  267. W5500_RESET_CONFIG();
  268. SPI_Configuration();//Config SPI
  269. reg_wizchip_cris_cbfunc(SPI_CrisEnter, SPI_CrisExit); //注册临界区函数
  270. #if _WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_SPI_VDM_
  271. reg_wizchip_cs_cbfunc(SPI_CS_Select, SPI_CS_Deselect);//注册SPI片选信号函数
  272. #elif _WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_SPI_FDM_
  273. reg_wizchip_cs_cbfunc(SPI_CS_Select, SPI_CS_Deselect); // CS must be tried with LOW.
  274. #else
  275. #if (_WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_SIP_) != _WIZCHIP_IO_MODE_SIP_
  276. #error "Unknown _WIZCHIP_IO_MODE_"
  277. #else
  278. reg_wizchip_cs_cbfunc(wizchip_select, wizchip_deselect);
  279. #endif
  280. #endif
  281. /* SPI Read & Write callback function */
  282. reg_wizchip_spi_cbfunc(SPI_ReadByte, SPI_WriteByte); //注册读写函数
  283. /* WIZCHIP SOCKET Buffer initialize */
  284. if(ctlwizchip(CW_INIT_WIZCHIP,(void*)memsize) == -1){
  285. SlwTrace(DEBUG,"WIZCHIP Initialized fail.\r\n");
  286. GPIO_ResetBits(GPIOB,GPIO_Pin_2);//set W5500 Reset
  287. os_dly_wait(6000);
  288. }
  289. }
  290. void WLNextStep(WLAN_STEP step)
  291. {
  292. sutWLanStatus.StepCt=0;
  293. sutWLanStatus.Step=step;
  294. }
  295. int WLanRecvProcess(void)
  296. {
  297. int8_t flag=1;
  298. int ret;
  299. ret=recv(SOCK_TCPS_APP1,gDATABUF, sizeof(gDATABUF));
  300. if(ret>0)WiredLanRecv(gDATABUF,ret);
  301. return ret;
  302. }
  303. /*******************************************************************
  304. *WiredLanTask
  305. *有线网络任务
  306. ********************************************************************/
  307. __task void WiredLanTask(void)
  308. {
  309. static unsigned long lastTick=100;
  310. unsigned long curTick;
  311. static unsigned short susSendUploadCt=0;
  312. static unsigned char sucSendTickCt=0;
  313. static unsigned char sucSendWaitAckCt=0;//发送后等待ACK计数器
  314. char tracebuf[50];
  315. static uint8_t TraceCt=0;
  316. uint8_t tmp;
  317. int r;
  318. // int32_t ret = 0;
  319. // uint8_t flag=1;
  320. SlwTrace(DEBUG,"WiredLanTask Start...\r\n");
  321. memset(&sutWLanStatus,0,sizeof(SUT_WL_STATUS));
  322. while(1){
  323. if(++TraceCt>2)TraceCt=0;
  324. switch(sutWLanStatus.Step)
  325. {
  326. case WL_STEP_INIT:
  327. if(TraceCt==1)SlwTrace(DEBUG,"[WL_INIT]\r\n");
  328. WireLanInit();
  329. WLNextStep(WL_STEP_GET_PHYLINK);
  330. break;
  331. case WL_STEP_GET_PHYLINK://检查IC是否工作(启动后需要插入网线才工作)
  332. if(TraceCt==1)SlwTrace(DEBUG,"[WL_PHYLINK]\r\n");
  333. os_dly_wait(100);
  334. if(ctlwizchip(CW_GET_PHYLINK, (void*)&tmp) == -1){
  335. SlwTrace(DEBUG,"Unknown PHY Link stauts.\r\n");
  336. if(++sutWLanStatus.StepCt>6000)WLNextStep(WL_STEP_INIT);
  337. }
  338. if(PHY_LINK_ON==tmp){
  339. network_init();
  340. WLNextStep(WL_STEP_CK);
  341. }else if(++sutWLanStatus.StepCt>30000)WLNextStep(WL_STEP_INIT);
  342. break;
  343. case WL_STEP_CK://持续检查网线是否插入
  344. if(TraceCt==1)SlwTrace(DEBUG,"[WL_CK]\r\n");
  345. os_dly_wait(100);
  346. tmp=getPHYCFGR();
  347. if((tmp&PHYCFGR_LNK_ON)==PHYCFGR_LNK_ON){
  348. gWIZNETINFO.ip[0]=0;gWIZNETINFO.ip[1]=0;gWIZNETINFO.ip[2]=0;gWIZNETINFO.ip[3]=0;
  349. //DHCP client initialization
  350. DHCP_init(SOCK_UDPS_DHCP,gDATABUF);//DHCP_init(SOCK_UDPS_DHCP,DHCP_MSG_BUF);
  351. //Register DHCP call back function
  352. reg_dhcp_cbfunc(cb_ip_assign,cb_ip_update,cb_ip_conflict);
  353. WLNextStep(WL_STEP_DHCP);
  354. }
  355. else if(++sutWLanStatus.StepCt>30000)WLNextStep(WL_STEP_GET_PHYLINK);
  356. break;
  357. case WL_STEP_DHCP:
  358. os_dly_wait(1);
  359. DHCP_run();
  360. //控制一秒执行一次
  361. curTick=os_time_get();
  362. if(curTick>lastTick){
  363. lastTick=curTick+100;
  364. }else continue;
  365. if(TraceCt==1)SlwTrace(DEBUG,"[WL_DHCP]\r\n");
  366. DHCP_time_handler();
  367. if(gWIZNETINFO.ip[0]!=0)WLNextStep(WL_STEP_IDEL);
  368. else if(++sutWLanStatus.StepCt>3000)WLNextStep(WL_STEP_IDEL);
  369. break;
  370. case WL_STEP_IDEL:
  371. if(TraceCt==1)SlwTrace(DEBUG,"[WL_IDEL]\r\n");
  372. os_dly_wait(100);
  373. //IP为0 不登陆,需要等4G获取域名解析到IP才连接服务器
  374. if(sutServerIp.AppServerIP[0]==0){
  375. continue;
  376. }
  377. //4G离线时间超过125秒才登陆服务器
  378. if(sutModemStatus.ServerOfflineCt>WLAN_CONNECT_AFTER_4G_TIME){
  379. WLNextStep(WL_STEP_CONNETING);
  380. }
  381. break;
  382. case WL_STEP_CONNETING:
  383. if(TraceCt==1)SlwTrace(DEBUG,"[WL_CONNETING]\r\n");
  384. os_dly_wait(100);
  385. //连接服务器
  386. if(sutFotaPara.fotaStatus!=FOTA_END){//远程升级
  387. r=Connect2Server(1);
  388. }else{//正常
  389. r=Connect2Server(0);
  390. }
  391. if(r){
  392. sutWLanStatus.CmdNum=sutWLanStatus.RecvCmdNum+5;//保证不相等
  393. sutModemStatus.CtrlReSendCt=5;//使进入WL_STEP_LOGIN后马上发送
  394. WLNextStep(WL_STEP_LOGIN);
  395. }else{
  396. //5秒一次,连接10次不成功,
  397. if(++sutWLanStatus.StepCt>10)WLNextStep(WL_STEP_CK);
  398. else os_dly_wait(500);
  399. }
  400. break;
  401. case WL_STEP_LOGIN:
  402. os_dly_wait(1);
  403. r=WLanRecvProcess();
  404. if(r<0){
  405. sprintf(tracebuf,"[WL]Recv Err=%d\r\n",r);
  406. SlwTrace(DEBUG,tracebuf);
  407. Disconnect2Server();
  408. WLNextStep(WL_STEP_CONNETING);
  409. continue;
  410. }else if(r>0)continue;
  411. //控制一秒执行一次
  412. curTick=os_time_get();
  413. if(curTick>lastTick){
  414. lastTick=curTick+100;
  415. }else continue;
  416. SlwTrace(DEBUG,"[WL_LOGIN]\r\n");
  417. //远程升级
  418. if(sutFotaPara.fotaStatus==FOTA_GET_FILEINFO){
  419. if(FoTaGetFileInfo(1)<0){
  420. Disconnect2Server();
  421. WLNextStep(WL_STEP_CONNETING);
  422. continue;
  423. }
  424. if(++sutWLanStatus.CtrlReSendCt>5){//连续发几次都收不到服务器应答,退出升级
  425. sutWLanStatus.CtrlReSendCt=0;
  426. sutFotaPara.fotaStatus=FOTA_END;
  427. Disconnect2Server();
  428. WLNextStep(WL_STEP_CONNETING);
  429. continue;
  430. }
  431. }else if(sutFotaPara.fotaStatus==FOTA_GET_FILEDATA){
  432. sutFotaPara.getFileInfoCt=0;
  433. if(++sutFotaPara.reSendCt>FOTA_RE_SEND_CT_MAX){
  434. sutFotaPara.reSendCt=0;
  435. if(FoTaGetFileData(1)<0){
  436. Disconnect2Server();
  437. WLNextStep(WL_STEP_CONNETING);
  438. continue;
  439. }
  440. }
  441. }else{//sutFotaPara.fotaStatus==FOTA_END
  442. //----正常工作
  443. if(sutWLanStatus.CmdNum!=sutWLanStatus.RecvCmdNum){
  444. if(++sutWLanStatus.CtrlReSendCt>5){
  445. sutWLanStatus.CtrlReSendCt=0;
  446. r=SendLogin2Server();
  447. if(r<=0){
  448. Disconnect2Server();
  449. WLNextStep(WL_STEP_CONNETING);
  450. continue;
  451. }
  452. }
  453. }else{
  454. if(sutWLanStatus.UnauthorizedCt>0){
  455. sutWLanStatus.UnauthorizedCt--;
  456. if(sutWLanStatus.UnauthorizedCt==0){
  457. sutWLanStatus.CmdNum=sutWLanStatus.RecvCmdNum+1;
  458. }
  459. }else{
  460. sutWLanStatus.Step=WL_STEP_UPLOAD;
  461. sutWLanStatus.CtrlIntervalSendCt=sutDeviceConfig.UploadInterval;
  462. }
  463. }
  464. }
  465. break;
  466. case WL_STEP_UPLOAD:
  467. os_dly_wait(1);
  468. r=WLanRecvProcess();
  469. if(r<0){
  470. sprintf(tracebuf,"[WL]Recv Err=%d\r\n",r);
  471. SlwTrace(DEBUG,tracebuf);
  472. Disconnect2Server();
  473. WLNextStep(WL_STEP_CONNETING);
  474. continue;
  475. }else if(r>0)continue;
  476. //控制一秒执行一次
  477. curTick=os_time_get();
  478. if(curTick>lastTick){
  479. lastTick=curTick+100;
  480. }else continue;
  481. if(TraceCt==1)SlwTrace(DEBUG,"[WL_UPLOAD]\r\n");
  482. if(++sutWLanStatus.CtrlIntervalSendCt>=sutDeviceConfig.UploadInterval || sutTestData.TcUpdated){
  483. sutWLanStatus.CtrlIntervalSendCt=0;
  484. sutWLanStatus.CmdNum++;
  485. sutTestData.TcUpdated=0;
  486. sutWLanStatus.CtrlReSendCt=3;
  487. }
  488. if(sutWLanStatus.CmdNum!=sutWLanStatus.RecvCmdNum){
  489. if(++sutWLanStatus.CtrlReSendCt>3){
  490. sutWLanStatus.CtrlReSendCt=0;
  491. SendCurPosition2Server();
  492. }
  493. }
  494. if(sutFotaPara.fotaStatus==FOTA_END && sutModemStatus.ServerOfflineCt<WLAN_CONNECT_AFTER_4G_TIME){
  495. Disconnect2Server();
  496. WLNextStep(WL_STEP_IDEL);
  497. }
  498. break;
  499. }
  500. }
  501. }
  502. void DhcpDisplayIP(void)
  503. {
  504. #if 0
  505. uint8_t ip[4];
  506. char tracebuf[30];
  507. getIPfromDHCP(ip);
  508. sprintf(tracebuf,"LC IP=%d.%d.%d.%d\r\n",ip[0],ip[1],ip[2],ip[3]);
  509. SlwTrace(DEBUG,tracebuf);
  510. getGWfromDHCP(ip);
  511. sprintf(tracebuf,"GW IP=%d.%d.%d.%d\r\n",ip[0],ip[1],ip[2],ip[3]);
  512. SlwTrace(DEBUG,tracebuf);
  513. getSNfromDHCP(ip);
  514. sprintf(tracebuf,"SN IP=%d.%d.%d.%d\r\n",ip[0],ip[1],ip[2],ip[3]);
  515. SlwTrace(DEBUG,tracebuf);
  516. getDNSfromDHCP(ip);
  517. sprintf(tracebuf,"DNS IP=%d.%d.%d.%d\r\n",ip[0],ip[1],ip[2],ip[3]);
  518. SlwTrace(DEBUG,tracebuf);
  519. #else
  520. char tracebuf[30];
  521. sprintf(tracebuf,"LC IP=%d.%d.%d.%d\r\n",gWIZNETINFO.ip[0],gWIZNETINFO.ip[1],gWIZNETINFO.ip[2],gWIZNETINFO.ip[3]);
  522. SlwTrace(DEBUG,tracebuf);
  523. sprintf(tracebuf,"GW IP=%d.%d.%d.%d\r\n",gWIZNETINFO.gw[0],gWIZNETINFO.gw[1],gWIZNETINFO.gw[2],gWIZNETINFO.gw[3]);
  524. SlwTrace(DEBUG,tracebuf);
  525. sprintf(tracebuf,"SN IP=%d.%d.%d.%d\r\n",gWIZNETINFO.sn[0],gWIZNETINFO.sn[1],gWIZNETINFO.sn[2],gWIZNETINFO.sn[3]);
  526. SlwTrace(DEBUG,tracebuf);
  527. sprintf(tracebuf,"DNS IP=%d.%d.%d.%d\r\n",gWIZNETINFO.dns[0],gWIZNETINFO.dns[1],gWIZNETINFO.dns[2],gWIZNETINFO.dns[3]);
  528. SlwTrace(DEBUG,tracebuf);
  529. #endif
  530. }
  531. /**************************************************************************************
  532. cb_ip_assign
  533. callback func when IP is assigned from DHCP server first
  534. ***************************************************************************************/
  535. void cb_ip_assign(void)
  536. {
  537. default_ip_assign();
  538. SlwTrace(DEBUG,"IP is assigned from DHCP!\r\n");
  539. ctlnetwork(CN_GET_NETINFO, (void*)&gWIZNETINFO);
  540. DhcpDisplayIP();
  541. }
  542. /**************************************************************************************
  543. cb_ip_update
  544. callback func when IP is changed
  545. ***************************************************************************************/
  546. void cb_ip_update(void)
  547. {
  548. default_ip_update();
  549. SlwTrace(DEBUG,"IP is update from DHCP!\r\n");
  550. ctlnetwork(CN_GET_NETINFO, (void*)&gWIZNETINFO);
  551. DhcpDisplayIP();
  552. }
  553. /**************************************************************************************
  554. cb_ip_conflict
  555. callback func when the assigned IP is conflict with others.
  556. ***************************************************************************************/
  557. void cb_ip_conflict(void)
  558. {
  559. SlwTrace(DEBUG,"IP Conflict!\r\n");
  560. default_ip_conflict();
  561. }
  562. /**
  563. * @brief Intialize the network information to be used in WIZCHIP
  564. * @retval None
  565. */
  566. void network_init(void)
  567. {
  568. char tacebuf[100];
  569. uint8_t tmpstr[6];
  570. //MAC
  571. memcpy(gWIZNETINFO.mac,sutProductPara.MAC,6);
  572. ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO);
  573. ctlnetwork(CN_GET_NETINFO, (void*)&gWIZNETINFO);
  574. if(0!=memcmp(gWIZNETINFO.mac,sutProductPara.MAC,6)){
  575. W5500_RESET_CONFIG();
  576. os_dly_wait(20);
  577. memcpy(gWIZNETINFO.mac,sutProductPara.MAC,6);
  578. ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO);
  579. ctlnetwork(CN_GET_NETINFO, (void*)&gWIZNETINFO);
  580. if(0!=memcmp(gWIZNETINFO.mac,sutProductPara.MAC,6)){
  581. SlwTrace(DEBUG,"W5500 Init fail!\r\nWiredLan Stop!\r\n");
  582. while(1){
  583. os_dly_wait(10000);
  584. }
  585. }
  586. }
  587. // Display Network Information
  588. ctlwizchip(CW_GET_ID,(void*)tmpstr);
  589. snprintf(tacebuf,sizeof(tacebuf),"\r\n=== %s NET CONF ===\r\n",(char*)tmpstr);
  590. SlwTrace(DEBUG,tacebuf);
  591. snprintf(tacebuf,sizeof(tacebuf),"MAC: %02X:%02X:%02X:%02X:%02X:%02X\r\n",gWIZNETINFO.mac[0],gWIZNETINFO.mac[1],gWIZNETINFO.mac[2],
  592. gWIZNETINFO.mac[3],gWIZNETINFO.mac[4],gWIZNETINFO.mac[5]);
  593. SlwTrace(DEBUG,tacebuf);
  594. snprintf(tacebuf,sizeof(tacebuf),"SIP: %d.%d.%d.%d\r\n", gWIZNETINFO.ip[0],gWIZNETINFO.ip[1],gWIZNETINFO.ip[2],gWIZNETINFO.ip[3]);
  595. SlwTrace(DEBUG,tacebuf);
  596. snprintf(tacebuf,sizeof(tacebuf),"GAR: %d.%d.%d.%d\r\n", gWIZNETINFO.gw[0],gWIZNETINFO.gw[1],gWIZNETINFO.gw[2],gWIZNETINFO.gw[3]);
  597. SlwTrace(DEBUG,tacebuf);
  598. snprintf(tacebuf,sizeof(tacebuf),"SUB: %d.%d.%d.%d\r\n", gWIZNETINFO.sn[0],gWIZNETINFO.sn[1],gWIZNETINFO.sn[2],gWIZNETINFO.sn[3]);
  599. SlwTrace(DEBUG,tacebuf);
  600. snprintf(tacebuf,sizeof(tacebuf),"DNS: %d.%d.%d.%d\r\n", gWIZNETINFO.dns[0],gWIZNETINFO.dns[1],gWIZNETINFO.dns[2],gWIZNETINFO.dns[3]);
  601. SlwTrace(DEBUG,tacebuf);
  602. SlwTrace(DEBUG,"======================\r\n");
  603. }
  604. /*
  605. SOCK_IO_NONBLOCK;//SOCK_IO_BLOCK;
  606. */
  607. void SetIoMode(uint8_t iomode)
  608. {
  609. static uint8_t mode;
  610. mode=iomode;
  611. ctlsocket(SOCK_TCPS_APP1,CS_SET_IOMODE,&mode);
  612. }
  613. /*
  614. 通过有线通道发送TCP数据
  615. */
  616. int32_t WiredSendTcpData(uint8_t *data,uint16_t len)
  617. {
  618. char buf[30];
  619. int i,flag;
  620. int32_t r;
  621. sprintf(buf,">WD Send %d>\r\n",len);
  622. SlwTrace(DEBUG,buf);
  623. //-------------------------------
  624. //sutModemStatus.TcpSendTimeCt++;
  625. r=send(SOCK_TCPS_APP1,data,len);
  626. switch(r){
  627. case SOCKERR_TIMEOUT://- Timeout occurred \n
  628. SlwTrace(DEBUG,"err:SOCKERR_TIMEOUT\r\n");
  629. break;
  630. case SOCKERR_SOCKMODE://Invalid operation in the socket \n
  631. SlwTrace(DEBUG,"err:SOCKERR_SOCKMODE\r\n");
  632. break;
  633. case SOCKERR_SOCKNUM://Invalid socket number \n
  634. SlwTrace(DEBUG,"err:SOCKERR_SOCKNUM\r\n");
  635. break;
  636. case SOCKERR_DATALEN://zero data length \n
  637. SlwTrace(DEBUG,"err:SOCKERR_DATALEN\r\n");
  638. break;
  639. case SOCK_BUSY://Socket is busy.
  640. SlwTrace(DEBUG,"err:SOCK_BUSY\r\n");
  641. break;
  642. }
  643. if(sutDeviceConfig.DebugPrintEn != 0 && r>0){
  644. snprintf(buf,sizeof(buf),"Send[%d/%d] OK!\r\n",r,len);
  645. SlwTrace(DEBUG,buf);
  646. }
  647. return r;
  648. }
  649. /*
  650. 发登陆包
  651. 成功 >0
  652. 失败 <=0
  653. */
  654. int SendLogin2Server(void)
  655. {
  656. char buf[20];
  657. unsigned short len;
  658. len=PacketLogin(sutWLanStatus.CmdNum);
  659. sprintf(buf,"[WL]SLogin>>%d\r\n",len);
  660. SlwTrace(DEBUG,buf);
  661. if(len==0)return -20;
  662. return WiredSendTcpData(g_ucPropacketBuf,len);
  663. }
  664. /*
  665. 发当前点位置包
  666. 成功 >0
  667. 失败 <=0
  668. */
  669. int SendCurPosition2Server(void)
  670. {
  671. char buf[20];
  672. unsigned short i,len;
  673. SUT_POSITION sutPosition;
  674. memset(&sutPosition,0,sizeof(SUT_POSITION));
  675. if(sutGpsInfo.isGpsValid)sutPosition.Status.GNSS=1;
  676. else sutPosition.Status.GNSS=0;
  677. sutPosition.Status.ACC=1;
  678. sutPosition.Status.BD=sutGpsInfo.isBDWork;
  679. sutPosition.Status.GPS=sutGpsInfo.isGpsWork;
  680. sutPosition.Status.West=sutGpsInfo.West;
  681. sutPosition.Status.South=sutGpsInfo.South;
  682. sutPosition.Latitude=sutGpsInfo.Latitude;//22620926;//114035268; //114.035268
  683. sutPosition.Longitude=sutGpsInfo.Longitude; //114035268;//22620926; //22.620926
  684. sutPosition.Speed=sutGpsInfo.Speed;//0;
  685. sutPosition.Aspect=sutGpsInfo.Aspect;//123;
  686. sutPosition.Altitude=sutGpsInfo.Altitude;
  687. RTC_Get(sutPosition.Time);
  688. sutPosition.ExtraMsgId=0x3A;//RT231固定3A
  689. sutPosition.NetMode=2;//WL
  690. sutPosition.CSQ =0;
  691. sutPosition.Infrared=sutTestData.Infrared;
  692. sutPosition.Camera=sutTestData.Camera;
  693. for(i=0;i<sutTestData.TcLen;i++){
  694. sutPosition.TcData[i]=sutTestData.TcData[i];
  695. }
  696. sutPosition.TcLen=sutTestData.TcLen;
  697. sutPosition.ExtraMsgLen=sutPosition.TcLen+4;//
  698. len=PacketPosition(sutWLanStatus.CmdNum,sutPosition);
  699. if(len==0)return -20;
  700. sprintf(buf,"[WL]STest>>%d\r\n",len);
  701. SlwTrace(DEBUG,buf);
  702. return WiredSendTcpData(g_ucPropacketBuf,len);
  703. }