HgsTcpProc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. /********************************************************************************
  2. * File Name: HgsTcpProc.c
  3. * Function Describe:The HgsTcpProc for the system
  4. * Relate Module:
  5. * Explain: Hardware version is
  6. * Writer: wuzhao
  7. * Date: 2016.12.8
  8. *******************************************************************************/
  9. //--------------------------------------------------------------------------------
  10. #include "includes.h"
  11. CONFIG_SER_DEF sutConfig;
  12. #define POC_TIME_OUT_1 180 //seconds
  13. #define POC_TIME_OUT_2 60 //TCP连接失败或发送登陆包超三次后重新按此时间计时
  14. //负载均衡思想
  15. //目的,后台管理会给终端分配所属交换服务器
  16. //终端处理方法:
  17. //1、每次开机都会访问管理服务器ManaIP,如果成功则获取到非0的IP,检测是否与本地原PIP一致,如果不一致,则重置模块参数,并保存新PIP到本地,然后重启整机系统
  18. //2、如果开机后登陆不上(判定A)交换IP,则启动登陆管理服务器获取新IP,如果与本地一致,不改变本地参数,继续登陆交换服务器,如果一致,则更新并复位整机系统
  19. //3、如果成功登陆过后,然后掉线后再也登陆不上(判定B)交换服务器,则启动登陆管理服务器获取新IP,如果与本地一致,不改变本地参数,继续登陆交换服务器,如果一致,则更新并复位整机系统
  20. //判定A:即未登陆状态,从配置完POC参数后开始计时(PocTimeoutCnt),如果登录成功则重置PocTimeoutCnt计数,且不再计数,如果掉线后则再次清PocTimeoutCnt且开始计时,超过3分钟(待定)则判定成功
  21. //判定B:与判定A后半部分类似,即从掉线后开始计时
  22. //注意,执行访问管理服务器时,我们就不处理GPS了。因为此模块查询TCP状态返回不带SOCKET,且代码模式如果更改会比较复杂
  23. unsigned char CCID[21];
  24. unsigned char HigosSendFlag;//发送login包的次数
  25. unsigned char HgsConnetCnt;//发起连接次数
  26. unsigned char HgsConnetStatus;//tcp连接状态
  27. unsigned char HgsProcessResult;//0未知道结果或没测试 1测试不成功, 2测试通过
  28. unsigned char PocStartCnt;
  29. unsigned short PocTimeoutCnt,newCOUNT;
  30. //load part
  31. unsigned char tcpControl;
  32. //根据不同的cmd往buffer填充相应的值
  33. //当前支持cmd = 0x12 0x19
  34. unsigned short SendTcpDataFill(unsigned char *buffer, unsigned char cmd,unsigned char status,unsigned char value)
  35. {
  36. unsigned char *p = buffer;
  37. unsigned short i,j;
  38. unsigned char nowCIP[4],nowPIP[4],nowGIP[4];
  39. unsigned short nowCPort,nowPPort,nowGPort;
  40. SUTDL dl;
  41. SUTDS ds;
  42. i=0;
  43. //PSN
  44. dl.Data.ulData = sutProductPara.PSN;
  45. p[i++] = dl.Data.ucData.b4;
  46. p[i++] = dl.Data.ucData.b3;
  47. p[i++] = dl.Data.ucData.b2;
  48. p[i++] = dl.Data.ucData.b1;
  49. //PASS
  50. if(cmd == 0x12)
  51. {
  52. dl.Data.ulData = 0;
  53. p[i++] = dl.Data.ucData.b4;
  54. p[i++] = dl.Data.ucData.b3;
  55. p[i++] = dl.Data.ucData.b2;
  56. p[i++] = dl.Data.ucData.b1;
  57. }
  58. //PName
  59. for(j=0;j<8;j++) p[i++]= sutProductPara.ProductName[j];
  60. //CurVer
  61. ds.Data.usData = sutProductPara.ProductVersion;
  62. p[i++] = ds.Data.ucData.b2;
  63. p[i++] = ds.Data.ucData.b1;
  64. //CCID
  65. for(j=0;j<20;j++) p[i++]= CCID[j];
  66. if(cmd == 0x19)
  67. {
  68. p[i++] = status;
  69. p[i++] = value;
  70. if(status == 1)
  71. {//上报详情
  72. memcpy(nowCIP, sutProductPara.ManagerIP, 4);
  73. nowCPort = sutProductPara.ManagerPort;
  74. if(GetStringIP2Bytes(nowPIP, sutProductPara.PIP)) SlwTrace(INF, "IP Para err1",1);
  75. nowPPort = 0xffff;
  76. if(GetStringIP2Bytes(nowGIP, sutProductPara.GIP)) SlwTrace(INF, "IP Para err2",1);
  77. nowGPort = sutProductPara.GpsPort;
  78. }else if(status == 2)
  79. {//上报结果不需要值
  80. memset(nowCIP, 0, 4);
  81. memset(nowPIP, 0, 4);
  82. memset(nowGIP, 0, 4);
  83. nowCPort=0;nowPPort=0;nowGPort=0;
  84. }else{
  85. }
  86. for(j=0;j<4;j++) p[i++] = nowCIP[j];
  87. ds.Data.usData = nowCPort;
  88. p[i++] = ds.Data.ucData.b2;
  89. p[i++] = ds.Data.ucData.b1;
  90. for(j=0;j<4;j++) p[i++] = nowPIP[j];
  91. ds.Data.usData = nowPPort;
  92. p[i++] = ds.Data.ucData.b2;
  93. p[i++] = ds.Data.ucData.b1;
  94. for(j=0;j<4;j++) p[i++] = nowGIP[j];
  95. ds.Data.usData = nowGPort;
  96. p[i++] = ds.Data.ucData.b2;
  97. p[i++] = ds.Data.ucData.b1;
  98. }
  99. return i;
  100. }
  101. /**********************************************************************************************************************
  102. Type:1字节,决定Data的内容
  103. length:2字节 从data到checkSum的字节数
  104. Data[n]:根据实际数据
  105. checkSum:1字节 从头开始按字节累加到data为止每次累加的进位去掉
  106. ***********************************************************************************************************************/
  107. unsigned short SendTcpSeverPack(unsigned char *Buffer,unsigned char CMD,unsigned short Datalen)
  108. {
  109. unsigned short j,i=0;
  110. unsigned char *pBuf=Buffer,sum;
  111. SUTDS ds;
  112. pBuf[0]=CMD;
  113. ds.Data.usData = Datalen+1;
  114. pBuf[1]=ds.Data.ucData.b2;
  115. pBuf[2]=ds.Data.ucData.b1;
  116. sum=0;
  117. for(j=0;j<1+2+Datalen;j++) sum += pBuf[j];
  118. pBuf[3+Datalen]=sum;
  119. return (3+Datalen+1);
  120. }
  121. /**********************************************************************************************************************
  122. 格式:AT+ICCID
  123. +GETICCID:0x89860315402521836592
  124. +SCID:98680036904030021872
  125. ***********************************************************************************************************************/
  126. int ModemGetCCID(void)
  127. {
  128. int t;
  129. int i,len,ver;
  130. char buf[80],bufTemp[3];
  131. SUT_MODEM_AT_MSG *pMsg=&sutAtmPro;
  132. ver=0;
  133. for(i=0;i<5;i++){
  134. t=100;
  135. ModemSendAT("AT+ICCID\r\n");
  136. while(t--)
  137. {//^SCID: 89860316452001903429
  138. if((pMsg->MsgLen = MsgQueueAccept(pMsg->MsgData, sizeof(pMsg->MsgData))))
  139. {
  140. if(0==ModemStrCmp((char *)pMsg->MsgData,"^SCID:"))
  141. {
  142. memcpy(CCID, &pMsg->MsgData[7], sizeof(CCID));
  143. CCID[20]=0;
  144. snprintf(buf, sizeof(buf), "[09]ICCID:%s[", CCID);
  145. bufTemp[0] = CCID[4];
  146. bufTemp[1] = CCID[5];
  147. bufTemp[2] = 0;
  148. switch(atoi(bufTemp))
  149. {
  150. case 1:
  151. case 9:thisYunYingShang = CHINA_UNICOM;
  152. strcat(buf, "CHINA_UNICOM]");
  153. break;
  154. case 0:
  155. case 2:
  156. case 7:thisYunYingShang = CHINA_MOBILE;
  157. strcat(buf, "CHINA_MOBILE]");
  158. break;
  159. case 3:
  160. case 6:
  161. case 8:
  162. case 11:thisYunYingShang = CHINA_TELECOM;
  163. strcat(buf, "CHINA_TELECOM]");
  164. break;
  165. default:strcat(buf, "UNKNOW-ICCID]");
  166. break;
  167. }
  168. SlwTrace(INF, buf,1);
  169. return 1;
  170. }
  171. }else os_dly_wait(1);
  172. }
  173. }
  174. SlwTrace(INF, "[09]Get CCID failed",1);
  175. return 0;
  176. }
  177. /**********************************************************************************************************************
  178. 开机发送CCID
  179. (unsigned char *Buffer,unsigned long PSN,unsigned char CMD,unsigned long PASS,unsigned char *PName,unsigned short CurVer,unsigned char *CCID);
  180. ***********************************************************************************************************************/
  181. void SendHgsData(void)
  182. {
  183. unsigned short len;
  184. unsigned char HgsBuf[60];
  185. HigosSendFlag +=1;
  186. len=SendTcpDataFill(&HgsBuf[3],0x12,0,0);//先填充数据段,后两参数没意义
  187. len=SendTcpSeverPack(HgsBuf,0x12,len);//再按协议格式打包
  188. M9507CSendTcpData(GPS_DATA_SOCKET, HgsBuf, len);
  189. PrintTcpData(HgsBuf,len);
  190. }
  191. /**********************************************************************************************************************
  192. 收到服务器返回后处理
  193. ***********************************************************************************************************************/
  194. void HigosInit(void)
  195. {
  196. #if(USE_HIGOS_PRO==1)
  197. tcpControl = 0;//先处理ManaTCP
  198. HigosSendFlag=0;
  199. HgsConnetCnt=0;
  200. HgsProcessResult=0;
  201. HgsConnetStatus=0;
  202. #else
  203. tcpControl=1;
  204. #endif
  205. }
  206. void HigosStartCnt(unsigned char long_short)
  207. {
  208. PocStartCnt=1;//打开计时
  209. PocTimeoutCnt=0;//从0开始计数
  210. if(long_short == 1)//long
  211. newCOUNT=POC_TIME_OUT_1;
  212. else
  213. newCOUNT=POC_TIME_OUT_2;
  214. HgsProcessResult=0;
  215. SlwTrace(INF, "\r\nStart cnt",1);
  216. }
  217. void HigosStopCnt(void)
  218. {
  219. #if(USE_HIGOS_PRO==1)
  220. PocStartCnt=0;//禁止计时
  221. PocTimeoutCnt=0;//从0开始计数
  222. SlwTrace(INF, "\r\nStop cnt",1);
  223. #endif
  224. }
  225. /************************************************
  226. 每10ms处理一次
  227. AT+ZIPSETUP=0,higos.f3322.net,12001
  228. +ZDNSGETIP:125.118.61.34
  229. **************************************************/
  230. #define TCP_RETRY_NUM 3
  231. void HigosTick(void)
  232. {
  233. static unsigned char sucSecCt=0;
  234. static unsigned char sucSteep=0;
  235. static unsigned char sucTcpCnt=TCP_RETRY_NUM;
  236. unsigned char ret;
  237. char buf[50];
  238. static STATUE psStatus = INVALID;
  239. #if(USE_HIGOS_PRO==0)
  240. return;
  241. #endif
  242. if(psStatus != sutGpsStatus.PPPStatus)
  243. {
  244. if(psStatus != OPEN &&
  245. sutGpsStatus.PPPStatus == OPEN)
  246. {//PPP从没打开变成打开了,此时开始认为所到网络条件都具备了
  247. if(sutPocStatus.Logined == 0)
  248. HigosStartCnt(1);
  249. }
  250. psStatus = sutGpsStatus.PPPStatus;
  251. }
  252. //===========以下控制每1.5秒执行一次============
  253. if(++sucSecCt>99)sucSecCt=0;
  254. else return;
  255. if(sutGpsStatus.PPPStatus != OPEN) return;
  256. /////////////判定计时/////////////////
  257. if(PocStartCnt)
  258. {
  259. if(++PocTimeoutCnt >= newCOUNT)
  260. {
  261. SlwTrace(INF, "POC Login timeout",1);
  262. HigosStopCnt();
  263. //开启管理服务器登陆
  264. //先断开GPS吧
  265. snprintf(buf, sizeof(buf), "AT+TCPCLOSE=%d\r\n", GPS_DATA_SOCKET);
  266. ModemSendAT(buf);
  267. //1分钟后继续尝试
  268. if(HgsProcessResult==1)
  269. HigosStartCnt(0);//因连接TCP失败而导致未登陆管理服务器,短时间后重试
  270. HigosInit();
  271. }
  272. }
  273. if(tcpControl != 0) return;
  274. //---以下控制---
  275. if(++sucSteep>3)sucSteep=0;
  276. //----
  277. if(sucSteep==0)
  278. {
  279. if(++sucTcpCnt >= TCP_RETRY_NUM)
  280. {
  281. sucTcpCnt=0;
  282. if(HgsConnetCnt<3 && HgsConnetStatus==0)
  283. {
  284. snprintf(buf, sizeof(buf),"AT+TCPOPEN=%d,%d.%d.%d.%d:%d\r\n",
  285. GPS_DATA_SOCKET,
  286. sutProductPara.ManagerIP[0],
  287. sutProductPara.ManagerIP[1],
  288. sutProductPara.ManagerIP[2],
  289. sutProductPara.ManagerIP[3],
  290. sutProductPara.ManagerPort);
  291. ModemSendAT(buf);
  292. ++HgsConnetCnt;
  293. SlwTrace(INF, buf,0);
  294. }
  295. }
  296. }else if(1==sucSteep)
  297. {
  298. if(HgsConnetStatus)//已连接成功
  299. SendHgsData();
  300. }else if(2==sucSteep)
  301. {
  302. if(HigosSendFlag >=3 || //已发了三次了
  303. HgsConnetCnt >= 3 || //三次连接失败了
  304. HgsProcessResult == 2)//处理完成
  305. {
  306. if(HgsProcessResult != 2)
  307. HgsProcessResult=1;
  308. snprintf(buf, sizeof(buf),"AT+TCPCLOSE=%d\r\n", GPS_DATA_SOCKET);
  309. ModemSendAT(buf);
  310. os_dly_wait(1);
  311. tcpControl=1;//ManaTCP完毕,允许执行GPS处理
  312. sutConfig.g_ubConfigFlag=0;
  313. sutConfig.g_usOptionCnt=0;
  314. HgsConnetStatus=0;
  315. }
  316. }
  317. }
  318. void Hgs_Data_RecvHandle(char *data, unsigned short len)
  319. {
  320. unsigned short i;
  321. unsigned char sum,type,zerocnt;
  322. unsigned short thisLen;
  323. unsigned char newIP[4];
  324. char buf[60];
  325. char buf1[120];
  326. char *p=data;
  327. unsigned char status;
  328. unsigned char Right,AckStatus;
  329. unsigned char newCip[4],newPip[4],newGip[4];
  330. unsigned short newCPort,newPPort,newGPort;
  331. SUTDS ds;
  332. unsigned char zeroNum;
  333. char needSave=0;
  334. status=0;
  335. if(tcpControl != 0 && tcpControl != 2) return;
  336. //check load data
  337. type = *p++;
  338. thisLen = *p++;
  339. thisLen <<= 8;
  340. thisLen &= 0xff00;
  341. thisLen |= *p++;
  342. if((1+2+thisLen) != len)
  343. {
  344. snprintf(buf1, sizeof(buf1), "Len err");
  345. status=1;
  346. goto EXIT;
  347. }
  348. sum = 0;
  349. for(i=0;i<thisLen+2;i++) sum += data[i];
  350. if(sum != data[len -1])
  351. {
  352. snprintf(buf1, sizeof(buf1), "Sum err");
  353. status=2;
  354. goto EXIT;
  355. }
  356. if(type != 0x12 && type != 0x19)
  357. {
  358. snprintf(buf1, sizeof(buf1), "Type unSupport");
  359. status=3;
  360. goto EXIT;
  361. }
  362. //收到正常的应答包
  363. if(type == 0x12)
  364. {
  365. Right = *p++;
  366. if(0xEA != Right)
  367. {
  368. snprintf(buf1, sizeof(buf1), "Right err");
  369. status=4;
  370. goto EXIT;
  371. }
  372. HigosStopCnt();
  373. memcpy(newIP, data+13, 4);
  374. snprintf(buf, sizeof(buf), "NewIP:%d.%d.%d.%d",newIP[0],newIP[1],newIP[2],newIP[3]);
  375. SlwTrace(INF, buf,1);
  376. //如果不全为0,也不匹配原来的IP,则更新并启动新IP连接
  377. zerocnt=0;
  378. for(i=0;i<4;i++)
  379. {
  380. if(newIP[i] == 0) zerocnt++;
  381. }
  382. if(zerocnt >= 2)
  383. {
  384. SlwTrace(INF, "err_IP",1);
  385. status=5;
  386. goto EXIT;
  387. }
  388. snprintf(buf, sizeof(buf), "%d.%d.%d.%d", newIP[0],newIP[1],newIP[2],newIP[3]);
  389. if(strcmp(sutProductPara.PIP,buf) != 0)
  390. {//有新交换IP
  391. IWDG_ReloadCounter();
  392. //1、保存起来
  393. snprintf(sutProductPara.PIP,sizeof(sutProductPara.PIP), "%d.%d.%d.%d", newIP[0],newIP[1],newIP[2],newIP[3]);
  394. SaveProductParaToFlash("HGS_IP");
  395. //2、写到模块去
  396. snprintf(buf, sizeof(buf),"id=%lu;ip=%s;pwd=%s;",sutProductPara.PSN,sutProductPara.PIP,sutProductPara.HARDID);
  397. AscStrToHexStr(buf,buf1);
  398. ModemSendAT("AT+POC=010000");
  399. ModemSendAT(buf1);
  400. ModemSendAT("\r\n");
  401. //reset whole system
  402. SlwTrace(INF, "Reboot...",1);
  403. os_dly_wait(100);
  404. while(1);
  405. }else
  406. {
  407. snprintf(buf1, sizeof(buf1),"Same_IP");
  408. status=0;
  409. goto EXIT;
  410. }
  411. EXIT:
  412. SlwTrace(INF, buf1,1);
  413. //成功拿到一包数据后,则认为完成一次处理
  414. if(sutPocStatus.Logined==0)
  415. HigosStartCnt(1);//如果未登陆要接着计数
  416. HgsProcessResult=2;
  417. }else if(type == 0x19)
  418. {
  419. #if(USE_CONFIG_FUN==1)
  420. AckStatus = *p++;
  421. sutConfig.g_ucConfigSerAckStatus=AckStatus;
  422. switch(AckStatus)
  423. {
  424. case 1://响应成功,可处理数据
  425. memcpy(newCip, p, 4);p+=4;
  426. ds.Data.ucData.b2 = *p++;
  427. ds.Data.ucData.b1 = *p++;
  428. newCPort=ds.Data.usData;
  429. memcpy(newPip, p, 4);p+=4;
  430. ds.Data.ucData.b2 = *p++;
  431. ds.Data.ucData.b1 = *p++;
  432. newPPort=ds.Data.usData;
  433. memcpy(newGip, p, 4);p+=4;
  434. ds.Data.ucData.b2 = *p++;
  435. ds.Data.ucData.b1 = *p++;
  436. newGPort=ds.Data.usData;
  437. snprintf(buf1, sizeof(buf1), "\r\nCIP:%d.%d.%d.%d,%d PIP:%d.%d.%d.%d,%d GIP:%d.%d.%d.%d,%d",
  438. newCip[0],newCip[1],newCip[2],newCip[3],newCPort,
  439. newPip[0],newPip[1],newPip[2],newPip[3],newPPort,
  440. newGip[0],newGip[1],newGip[2],newGip[3],newGPort);
  441. SlwTrace(INF, buf1, 1);
  442. //检测CIP是否为全0 IP
  443. zeroNum=0;
  444. for(i=0;i<4;i++) if(newCip[i] == 0) zeroNum++;
  445. if(zeroNum <=1 && newCPort)//只有1个或没有0,认为IP有效
  446. {
  447. if( 0!=memcmp(newCip, sutProductPara.ManagerIP, 4) ||
  448. newCPort != sutProductPara.ManagerPort)
  449. {
  450. needSave=1;
  451. memcpy(sutProductPara.ManagerIP, newCip, 4);
  452. sutProductPara.ManagerPort = newCPort;
  453. SlwTrace(INF, "CIP Update",1);
  454. }else SlwTrace(INF, "CIP No change",1);
  455. }
  456. //检测PIP是否为全0 IP
  457. zeroNum=0;
  458. for(i=0;i<4;i++) if(newPip[i] == 0) zeroNum++;
  459. if(zeroNum <=1)//只有1个或没有0,认为IP有效
  460. {
  461. snprintf(buf, sizeof(buf), "%d.%d.%d.%d",newPip[0],newPip[1],newPip[2],newPip[3]);
  462. if(0!=strcmp(buf, sutProductPara.PIP))
  463. {
  464. needSave=2;
  465. strcpy(sutProductPara.PIP, buf);
  466. SlwTrace(INF, "PIP Update",1);
  467. }else SlwTrace(INF, "PIP No change",1);
  468. }
  469. //检测GIP是否为全0 IP
  470. zeroNum=0;
  471. for(i=0;i<4;i++) if(newGip[i] == 0) zeroNum++;
  472. if(zeroNum <=1 && newGPort)//只有1个或没有0,认为IP有效
  473. {
  474. snprintf(buf, sizeof(buf), "%d.%d.%d.%d",newGip[0],newGip[1],newGip[2],newGip[3]);
  475. if(0!=strcmp(buf, sutProductPara.GIP) ||
  476. newGPort != sutProductPara.GpsPort)
  477. {
  478. needSave=3;
  479. strcpy(sutProductPara.GIP, buf);
  480. sutProductPara.GpsPort = newGPort;
  481. SlwTrace(INF, "GIP Update",1);
  482. }else SlwTrace(INF, "GIP No change",1);
  483. }
  484. if(needSave !=0) SaveProductParaToFlash("NCONF");
  485. sutConfig.g_ucConfigStatus=2;//上报更新结果
  486. sutConfig.g_ucConfigValue=1;//完成配置
  487. sutConfig.g_ubConfigFinished=1;
  488. break;
  489. case 2://服务器无此PSN
  490. SlwTrace(INF, "\r\nNo PSN in server",1);
  491. break;
  492. case 3://CCID不匹配
  493. SlwTrace(INF, "\r\nCCID no fitted",1);
  494. break;
  495. case 4://新旧IP一致
  496. SlwTrace(INF, "\r\nAll para no change",1);
  497. break;
  498. default://保留
  499. break;
  500. }
  501. #endif
  502. }
  503. }
  504. #if(USE_CONFIG_FUN==1)
  505. void SerConfigSend(void)
  506. {
  507. unsigned short len;
  508. unsigned char HgsBuf[80];
  509. len=SendTcpDataFill(&HgsBuf[3],0x19,sutConfig.g_ucConfigStatus,sutConfig.g_ucConfigValue);//先填充数据段,后两参数没意义
  510. len=SendTcpSeverPack(HgsBuf,0x19,len);//再按协议格式打包
  511. M9507CSendTcpData(CONFIG_SER_SOCKET, HgsBuf, len);
  512. PrintTcpData(HgsBuf,len);
  513. }
  514. void ConfigCtrHandle(void)
  515. {
  516. static unsigned char Cnt,thisStatus=0,timeout=0;
  517. char buf[50],needAt;
  518. if(sutConfig.g_ubConfigFlag==0) return;
  519. // if(sutConfig.g_ubConfigFinished) return;
  520. if(++Cnt < 99) return;
  521. if(timeout++ > 60)
  522. {
  523. CONFIG_EXIT:
  524. //////////////////////////////////////////
  525. GuiClearRect(0,65,LCD_WIDTH-1,81);
  526. GuiShowBmp(30,34,"logo.bmp");
  527. if(sutGpsStatus.PPPStatus != OPEN)
  528. {
  529. SlwTrace(INF, "PPP Failed",1);
  530. //sprintf(buf,"B2A6BAC5CAA7B0DC");
  531. GuiShowStr(42, 55, "拨号失败!",1);
  532. }else if(sutConfig.g_ubConfigTcpStatus==0)
  533. {
  534. SlwTrace(INF, "Tcp Failed",1);
  535. //sprintf(buf,"C1ACBDD3CAA7B0DC");
  536. GuiShowStr(42, 55, "连接失败!",1);
  537. }else if(sutConfig.g_ucTcpSendDateTime >=3)
  538. {
  539. SlwTrace(INF, "Tcp no fit data",1);
  540. //sprintf(buf,"CFECD3A6CEDED0A7");
  541. GuiShowStr(42, 55, "响应无效!",1);
  542. }else{
  543. SlwTrace(INF, "Access ok",1);
  544. //sprintf(buf,"B7C3CECACDEAB3C9");
  545. GuiShowStr(42, 55, "访问完成!",1);
  546. }
  547. //strcat(buf, "2CCDCBB3F6C5E4D6C3");
  548. GuiShowStr(42, 80, "退出配置!",1);
  549. //MeSpeak(ENCODE_GBK, buf, 1);
  550. SlwTrace(INF, "Quit config",1);
  551. //////////////////////////////////////////
  552. if(sutConfig.g_ubConfigTcpStatus)
  553. {
  554. os_dly_wait(100);
  555. snprintf(buf, sizeof(buf), "AT+TCPCLOSE=%d\r\n", CONFIG_SER_SOCKET);
  556. ModemSendAT(buf);
  557. }
  558. memset((unsigned char *)&sutConfig, 0, sizeof(CONFIG_SER_DEF));
  559. timeout=0;
  560. sutConfig.g_ubConfigFlag=0;
  561. return;
  562. }
  563. Cnt=0;
  564. needAt=0;
  565. if(++thisStatus > 2) thisStatus=0;
  566. switch(thisStatus)
  567. {
  568. case 0://检测ppp
  569. if(sutGpsStatus.PPPStatus == OPEN) snprintf(buf, sizeof(buf), "AT+TCPSTATUS=%d\r\n", CONFIG_SER_SOCKET);
  570. else snprintf(buf, sizeof(buf), "AT+POC_PPP\r\n");
  571. needAt=1;
  572. break;
  573. case 1://连接TCP
  574. if(sutGpsStatus.PPPStatus != OPEN) break;
  575. if(sutConfig.g_ucTcpRetryNum++ >= CONFIG_TCP_RETRY_CNT)
  576. {
  577. sutConfig.g_ucTcpRetryNum =0;
  578. if(sutConfig.g_ubConfigTcpStatus == 0)//未连接成功
  579. {//发起连接
  580. if(sutConfig.g_ucTcpConnectTime < CONFIG_TCP_MAX_CONNECT)
  581. {//连接次数未超标
  582. snprintf(buf, sizeof(buf), "AT+TCPOPEN=%d,%d.%d.%d.%d:%d\r\n",
  583. CONFIG_SER_SOCKET,
  584. sutProductPara.ConfigSerIP[0],
  585. sutProductPara.ConfigSerIP[1],
  586. sutProductPara.ConfigSerIP[2],
  587. sutProductPara.ConfigSerIP[3],
  588. sutProductPara.ConfigSerPort);
  589. needAt=1;
  590. sutConfig.g_ucTcpConnectTime++;
  591. }else goto CONFIG_EXIT;
  592. }
  593. }
  594. break;
  595. case 2://发送数据
  596. if(sutGpsStatus.PPPStatus != OPEN) break;
  597. if(sutConfig.g_ubConfigTcpStatus==0) break;
  598. if(sutConfig.g_ucTcpSendDateTime++ < 3 ||
  599. sutConfig.g_ubConfigFinished)//收到服务器应答并处理完成后,应答结果,完成交互
  600. {
  601. SerConfigSend();
  602. }else goto CONFIG_EXIT;
  603. break;
  604. }
  605. if(needAt)
  606. {
  607. SlwTrace(INF, buf,0);
  608. ModemSendAT(buf);
  609. }
  610. if(sutConfig.g_ubConfigFinished) goto CONFIG_EXIT;
  611. }
  612. #endif
  613. #define PRINTF_MAX_LEN 70
  614. void PrintTcpData(unsigned char *tcpData, unsigned short len)
  615. {
  616. char buf[PRINTF_MAX_LEN*2+10];
  617. char temp[2];
  618. unsigned char thisLen,i;
  619. thisLen = PRINTF_MAX_LEN;
  620. if(len > thisLen) len = thisLen;
  621. snprintf(buf, sizeof(buf), "TcpS[%d]:",len);
  622. #if 0
  623. for(i=0;i<len;i++)
  624. {
  625. snprintf(temp, sizeof(temp), "%02x",tcpData[i]);
  626. strcat(buf,temp);
  627. }
  628. #endif
  629. SlwTrace(INF, buf,1);
  630. }