colony.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. #include "includes.h"
  2. COLONY_TYPEDEF sutColony;
  3. unsigned char CCID[21];
  4. void ConolyInit(void)
  5. {
  6. memset((unsigned char *)&sutColony, 0, sizeof(COLONY_TYPEDEF));
  7. ColonyFeature(1);//默认开机处理一次业务
  8. }
  9. /**********************************************************************************************************************
  10. 格式:AT+ICCID
  11. +GETICCID:0x89860315402521836592
  12. +SCID:98680036904030021872
  13. ***********************************************************************************************************************/
  14. short ModemGetCCID(void)
  15. {
  16. short t;
  17. short i,len,ver;
  18. char buf[80],bufTemp[3];
  19. SUT_MODEM_AT_MSG *pMsg=&sutAtmPro;
  20. ver=0;
  21. for(i=0;i<5;i++){
  22. t=100;
  23. ModemSendAT("AT+ICCID\r\n");
  24. while(t--)
  25. {//^SCID: 89860316452001903429
  26. if(g_ucQuitModemInitNow==1) return -1;
  27. if((pMsg->MsgLen = MsgQueueAccept(pMsg->MsgData, sizeof(pMsg->MsgData))))
  28. {
  29. if(0==ModemStrCmp((char *)pMsg->MsgData,"^SCID:"))
  30. {
  31. memcpy(CCID, &pMsg->MsgData[7], sizeof(CCID));
  32. CCID[20]=0;
  33. snprintf(buf, sizeof(buf), "[Minit]ICCID:%s[", CCID);
  34. bufTemp[0] = CCID[4];
  35. bufTemp[1] = CCID[5];
  36. bufTemp[2] = 0;
  37. switch(atoi(bufTemp))
  38. {
  39. case 1:
  40. case 9:thisYunYingShang = CHINA_UNICOM;
  41. strcat(buf, "CHINA_UNICOM]");
  42. break;
  43. case 0:
  44. case 2:
  45. case 4:
  46. case 7:thisYunYingShang = CHINA_MOBILE;
  47. strcat(buf, "CHINA_MOBILE]");
  48. break;
  49. case 3:
  50. case 6:
  51. case 8:
  52. case 11:thisYunYingShang = CHINA_TELECOM;
  53. strcat(buf, "CHINA_TELECOM]");
  54. break;
  55. default:strcat(buf, "UNKNOW-ICCID]");
  56. break;
  57. }
  58. SlwTrace(INF, buf,1);
  59. return 1;
  60. }
  61. }else os_dly_wait(1);
  62. }
  63. }
  64. SlwTrace(INF, "[Minit]Get CCID failed",1);
  65. return 0;
  66. }
  67. void ColonyTcpStatus(STATUE ipStatus)
  68. {
  69. char ipFlag;
  70. if(ipStatus == OPEN) ipFlag=1;
  71. else ipFlag=0;
  72. if(ipFlag != sutColony.targetConnectOk) //1
  73. {
  74. if(ipFlag==1)//连接上了
  75. sutColony.targetDataSendTime=0;
  76. else//突然断开了
  77. ColonyParaReset();
  78. sutColony.targetConnectOk=ipFlag;
  79. }
  80. }
  81. static void SendHgsData(void);
  82. void ColonyTick(void)
  83. {
  84. char buf[50];
  85. static char step=0;
  86. if(sutColony.colonyCtl ==0) return;
  87. if(sutGpsStatus.PPPStatus != OPEN) return;
  88. switch(step)
  89. {
  90. case 0://获取域名
  91. if(sutColony.domainGetOk==1) break; //获取成功返回
  92. SlwTrace(INF, "Colony step1",1);
  93. if(sutColony.domainSendTick==0){
  94. memset(buf,0,sizeof(buf));
  95. snprintf(buf,sizeof(buf),"AT+DOMAIN=%s\r\n",sutProductPara.domain);
  96. SlwTrace(INF,buf,1);
  97. ModemSendAT(buf);
  98. }
  99. if(++sutColony.domainSendTick>=3) sutColony.domainSendTick=0;
  100. break;
  101. case 1://发起连接
  102. if(sutColony.domainGetOk==1)
  103. {
  104. if(sutColony.targetConnectOk==0){ //连接不成功
  105. if(++sutColony.targetTryTime >= 3){
  106. ColonyParaReset();
  107. sutColony.colonyCtl=0;
  108. SlwTrace(INF, "Timeout,Quit Colony",1);
  109. break;
  110. }
  111. SlwTrace(INF, "Colony step2",1);
  112. snprintf(buf, sizeof(buf),"AT+TCPOPEN=%d,%s:%d\r\n",COLONY_SER_SOCKET,sutColony.targetIp,COLONY_SER_PORT);
  113. ModemSendAT(buf);
  114. }
  115. }
  116. break;
  117. case 2://检测目标IP是否连接成功
  118. if(sutColony.domainGetOk==0) break;
  119. SlwTrace(INF, "Colony step3",1);
  120. snprintf(buf, sizeof(buf), "AT+TCPSTATUS=%d\r\n", COLONY_SER_SOCKET);
  121. ModemSendAT(buf);
  122. break;
  123. case 3://发数据
  124. SlwTrace(INF, "Colony step4",1);
  125. if(sutColony.targetConnectOk==1){
  126. SendHgsData();
  127. SlwTrace(INF,"Colony step5",1);
  128. }
  129. break;
  130. }
  131. if(++step>3) step=0;
  132. }
  133. void Colony_RecvHandle(char *data, unsigned short len)
  134. {
  135. unsigned short i;
  136. unsigned char sum,type,zerocnt;
  137. unsigned short thisLen;
  138. unsigned char newIP[4];
  139. unsigned char newGPSIP[4];
  140. char buf[60];
  141. char buf1[120];
  142. char buf2[40];
  143. char *p=data;
  144. unsigned char status;
  145. unsigned char Right,AckStatus;
  146. unsigned char newCip[4],newPip[4],newGip[4];
  147. unsigned short newCPort,newPPort,newGPort;
  148. SUTDS ds;
  149. unsigned char zeroNum;
  150. char needSave=0;
  151. status=0;
  152. if(sutColony.colonyCtl==0) return;
  153. //check load data
  154. SlwTrace(INF,"show data...........",1);
  155. PrintBufferData((unsigned char*)data,len);
  156. type = *p++;
  157. thisLen = *p++;
  158. thisLen <<= 8;
  159. thisLen &= 0xff00;
  160. thisLen |= *p++;
  161. if((1+2+thisLen) != len)
  162. {
  163. snprintf(buf1, sizeof(buf1), "Len err");
  164. status=1;
  165. goto EXIT;
  166. }
  167. sum = 0;
  168. for(i=0;i<thisLen+2;i++) sum += data[i];
  169. if(sum != data[len -1])
  170. {
  171. snprintf(buf1, sizeof(buf1), "Sum err");
  172. status=2;
  173. goto EXIT;
  174. }
  175. if(type != 0x12 && type != 0x19)
  176. {
  177. snprintf(buf1, sizeof(buf1), "Type unSupport");
  178. status=3;
  179. goto EXIT;
  180. }
  181. //收到正常的应答包
  182. if(type == 0x12)
  183. {
  184. Right = *p++;
  185. if(0xEA != Right)
  186. {
  187. snprintf(buf1, sizeof(buf1), "Right err");
  188. status=4;
  189. goto EXIT;
  190. }
  191. memcpy(newIP, data+23, 4);//
  192. snprintf(buf, sizeof(buf), "NewIP:%d.%d.%d.%d",newIP[0],newIP[1],newIP[2],newIP[3]);
  193. SlwTrace(INF, buf,1);
  194. memcpy(newGPSIP,data+27,4);
  195. snprintf(buf2, sizeof(buf2), "NewGPSIP:%d.%d.%d.%d",newGPSIP[0],newGPSIP[1],newGPSIP[2],newGPSIP[3]);
  196. SlwTrace(INF, buf2,1);
  197. //?????0,???????IP,???????IP??
  198. zerocnt=0;
  199. for(i=0;i<4;i++)
  200. {
  201. if(newIP[i] == 0) zerocnt++;
  202. }
  203. if(zerocnt >= 2)
  204. {
  205. SlwTrace(INF, "err_IP",1);
  206. status=5;
  207. goto EXIT;
  208. }
  209. zerocnt=0;
  210. for(i=0;i<4;i++)
  211. {
  212. if(newGPSIP[i] == 0) zerocnt++;
  213. }
  214. if(zerocnt >= 2)
  215. {
  216. SlwTrace(INF, "err_GPSIP",1);
  217. status=6;
  218. goto EXIT;
  219. }
  220. snprintf(buf2, sizeof(buf2), "%d.%d.%d.%d", newGPSIP[0],newGPSIP[1],newGPSIP[2],newGPSIP[3]);
  221. if(strcmp(sutProductPara.PIP,buf) != 0){
  222. strcpy(sutProductPara.GIP,buf2);
  223. memset(buf2,0,sizeof(buf2));
  224. snprintf(buf, sizeof(buf), "AT+TCPCLOSE=%d\r\n", GPS_DATA_SOCKET);
  225. ModemSendAT(buf);
  226. }
  227. snprintf(buf, sizeof(buf), "%d.%d.%d.%d", newIP[0],newIP[1],newIP[2],newIP[3]);
  228. if(strcmp(sutProductPara.PIP,buf) != 0)
  229. {//????IP
  230. IWDG_ReloadCounter();
  231. //1?????
  232. snprintf(sutProductPara.PIP,sizeof(sutProductPara.PIP), "%d.%d.%d.%d", newIP[0],newIP[1],newIP[2],newIP[3]);
  233. SaveProductParaToFlash("HGS_IP");
  234. //2??????
  235. snprintf(buf, sizeof(buf),"id=%lu;ip=%s;pwd=%s;",sutProductPara.PSN,sutProductPara.PIP,sutProductPara.MEID);
  236. AscStrToHexStr(buf,buf1);
  237. ModemSendAT("AT+POC=010000");
  238. ModemSendAT(buf1);
  239. ModemSendAT("\r\n");
  240. //reset whole system
  241. SlwTrace(INF, "Reboot...",1);
  242. os_dly_wait(50);
  243. ModemSendAT("AT+POC=040000\r\n");
  244. // os_dly_wait(100);
  245. // while(1);
  246. }else
  247. {
  248. snprintf(buf1, sizeof(buf1),"Same_IP");
  249. status=0;
  250. goto EXIT;
  251. }
  252. EXIT:
  253. SlwTrace(INF, buf1,1);
  254. SlwTrace(INF,"conlony over.........",1);
  255. //成功拿到一包数据后,认为完成处理了
  256. ColonyFeature(0);
  257. }
  258. }
  259. void ColonyDomainGet(char *pMsg)
  260. {//128.4.2.1 后面不能有多的数据了
  261. unsigned short i,j;
  262. unsigned char dotNum;
  263. unsigned char findFlag;
  264. //1、找到第3个点
  265. i=0;dotNum=0;findFlag=0;j=0;
  266. do{
  267. if(findFlag==1){
  268. if(pMsg[i] < '0' || pMsg[i] > '9'){
  269. //有多的数据
  270. if(strlen(pMsg+i) > 2) return;//不正常的数据包
  271. if(pMsg[i] =='\r') pMsg[i]=0;
  272. }
  273. if(pMsg[i] == '\r')pMsg[i]=0;
  274. }
  275. if(pMsg[i] == '.'){
  276. if(++dotNum == 3) findFlag=1;
  277. }
  278. i++;
  279. }while(pMsg[i] !='\0');
  280. j=strlen(pMsg);
  281. if(j<=15){
  282. memset(sutColony.targetIp, 0, sizeof(sutColony.targetIp));
  283. strncpy(sutColony.targetIp, pMsg, j);
  284. sutColony.domainGetOk=1;
  285. sutColony.targetTryTime=0;
  286. printf("get targetip:%s\r\n",sutColony.targetIp);
  287. }
  288. }
  289. void ColonyParaReset(void)
  290. {
  291. sutColony.domainGetOk=0;
  292. sutColony.domainSendTick=0;
  293. sutColony.targetConnectOk=0;;
  294. sutColony.targetTryTime=0;
  295. sutColony.targetDataSendTime=0;
  296. }
  297. void ColonyFeature(char status)
  298. {
  299. if(status >1) return;
  300. sutColony.colonyCtl=status;
  301. ColonyParaReset();
  302. }
  303. /////////proto parts
  304. static unsigned short SendTcpDataFill(unsigned char *buffer, unsigned char cmd,unsigned char status,unsigned char value)
  305. {
  306. unsigned char *p = buffer;
  307. unsigned short i,j;
  308. unsigned char nowCIP[4],nowPIP[4],nowGIP[4];
  309. unsigned short nowCPort,nowPPort,nowGPort;
  310. SUTDL dl;
  311. SUTDS ds;
  312. i=0;
  313. //PSN
  314. dl.Data.ulData = sutProductPara.PSN; //4字节
  315. p[i++] = dl.Data.ucData.b4;
  316. p[i++] = dl.Data.ucData.b3;
  317. p[i++] = dl.Data.ucData.b2;
  318. p[i++] = dl.Data.ucData.b1;
  319. //PASS
  320. if(cmd == 0x12)
  321. {
  322. dl.Data.ulData = 0;
  323. p[i++] = dl.Data.ucData.b4;
  324. p[i++] = dl.Data.ucData.b3;
  325. p[i++] = dl.Data.ucData.b2;
  326. p[i++] = dl.Data.ucData.b1;
  327. }
  328. //PName
  329. for(j=0;j<8;j++) p[i++]= sutProductPara.ProductName[j];
  330. //CurVer
  331. ds.Data.usData = sutProductPara.ProductVersion;
  332. p[i++] = ds.Data.ucData.b2;
  333. p[i++] = ds.Data.ucData.b1;
  334. //CCID
  335. for(j=0;j<20;j++) p[i++]= CCID[j];
  336. return i;
  337. }
  338. static unsigned short SendTcpSeverPack(unsigned char *Buffer,unsigned char CMD,unsigned short Datalen)
  339. {
  340. unsigned short j,i=0;
  341. unsigned char *pBuf=Buffer,sum;
  342. SUTDS ds;
  343. pBuf[0]=CMD;
  344. ds.Data.usData = Datalen+1;
  345. pBuf[1]=ds.Data.ucData.b2;
  346. pBuf[2]=ds.Data.ucData.b1;
  347. sum=0;
  348. for(j=0;j<1+2+Datalen;j++) sum += pBuf[j];
  349. pBuf[3+Datalen]=sum;
  350. return (3+Datalen+1);
  351. }
  352. static void SendHgsData(void)
  353. {
  354. unsigned short len;
  355. unsigned char HgsBuf[60];
  356. len=SendTcpDataFill(&HgsBuf[3],0x12,0,0);
  357. len=SendTcpSeverPack(HgsBuf,0x12,len);
  358. M9507CSendTcpData(COLONY_SER_SOCKET, HgsBuf, len);
  359. PrintBufferData(HgsBuf,len);
  360. }
  361. //end of proto parts
  362. /*获取集群GPSIP与PIP*/
  363. void GetDomainIp(char *pMsg,char type)
  364. {//128.4.2.1 后面不能有多的数据了
  365. unsigned short i,j;
  366. unsigned char dotNum;
  367. unsigned char findFlag;
  368. //1、找到第3个点
  369. unsigned char buf[40];
  370. i=0;dotNum=0;findFlag=0;j=0;
  371. do{
  372. if(findFlag==1){
  373. if(pMsg[i] < '0' || pMsg[i] > '9'){
  374. //有多的数据
  375. if(strlen(pMsg+i) > 2) return;//不正常的数据包 // 2222
  376. if(pMsg[i] =='\r') pMsg[i]=0;
  377. }
  378. if(pMsg[i] == '\r')pMsg[i]=0;
  379. }
  380. if(pMsg[i] == '.'){
  381. if(++dotNum == 3) findFlag=1;
  382. }
  383. i++;
  384. }while(pMsg[i] !='\0');
  385. j=strlen(pMsg);
  386. if(j<=15)
  387. {
  388. if(type=='p'){
  389. if(strcmp(sutProductPara.PIP,pMsg) != 0){
  390. memset(sutProductPara.PIP, 0, sizeof(sutProductPara.PIP));
  391. strncpy(sutProductPara.PIP, pMsg, j);
  392. //strcat(buf,sutProductPara.PIP);
  393. snprintf(buf,sizeof(buf),"SET newPIP:%s",sutProductPara.PIP);
  394. SlwTrace(INF,buf,1);
  395. SaveProductParaToFlash("save new IP");
  396. //printf("SET newPIP:%s\r\n",sutProductPara.PIP);
  397. }
  398. }else if(type=='g'){
  399. if(strcmp(sutProductPara.GIP,pMsg) != 0)
  400. {
  401. memset(sutProductPara.GIP, 0, sizeof(sutProductPara.GIP));
  402. strncpy(sutProductPara.GIP, pMsg, j);
  403. snprintf(buf,sizeof(buf),"set new GIP:%s\r\n",sutProductPara.GIP);
  404. SlwTrace(INF,buf,1);
  405. snprintf(buf, sizeof(buf), "AT+TCPCLOSE=%d\r\n", GPS_DATA_SOCKET);
  406. ModemSendAT(buf);
  407. SaveProductParaToFlash("save new GIP");
  408. }
  409. }
  410. }
  411. }
  412. /* IP 查询,网络模式查询,及处于2-4G查询*/
  413. void AtQuery()
  414. {
  415. static unsigned char send_sucCt=0;
  416. switch(send_sucCt){
  417. case 0:
  418. ModemSendAT("AT+SYSCMD=cat /tmp/group-config\r\n");
  419. break;
  420. case 1:
  421. ModemSendAT("AT+MODODREX?\r\n");
  422. break;
  423. case 2:
  424. ModemSendAT("AT^SYSINFO\r\n");
  425. break;
  426. }
  427. if(++send_sucCt>2) send_sucCt=0;
  428. }