AppFota.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. #include "includes.h"
  2. #include "lsapi_sock.h"
  3. #include "lsapi_http.h"
  4. #include "lsapi_fota.h"
  5. #include "lsapi_os.h"
  6. NET_DEF netWork;
  7. DOMAIN_INFO_DEF domainInfo;
  8. char targetVersion[10]={0};
  9. char wgetPath[64];
  10. short serverAck=0;
  11. LSAPI_OSI_Thread_t *lteThread=NULL;
  12. HttpInfoSet httpinfo;
  13. TUPSEND_DEF tupsendPara;
  14. typedef enum{
  15. TUP_STATUS_TRUE,
  16. TUP_STATUS_FALSE,
  17. TUP_STATUS_WAIT,
  18. TUP_STATUS_ONLINE
  19. }TUP_CONNECT_ENUM;
  20. FOTA_INFO AppFotaInfo;
  21. typedef enum{
  22. LTE_UPDATE_BUSY,
  23. LTE_UPDATE_FAILED,
  24. LTE_UPDATE_DONE
  25. }LTE_UPDATE_ENUM;
  26. LTE_UPDATE_ENUM lteResult;
  27. LTE_UPDATE_ENUM getLteUpdateStatus(void){return lteResult;}
  28. void MS5700_Networking(unsigned int interval){
  29. unsigned char info[23];
  30. uint32_t nRet,id;
  31. static bool getIccidFlag=FALSE;
  32. static int nAttstate,nActstate;
  33. static int LastnAttstate=0,LastnActstate=0;
  34. bool netIf;
  35. static bool lastNetIf=FALSE;
  36. static unsigned short cnt=0;
  37. static unsigned short regCnt=0;
  38. static unsigned short tickcnt=0;
  39. if(++tickcnt<(1000/interval)) return;//1秒读一次ADC即可
  40. tickcnt=0;
  41. nRet = LSAPI_NET_GetGprsAttState(&nAttstate);
  42. if(nRet!=0){
  43. MSG_WARN(1,"Get REG status failed");
  44. return;
  45. }
  46. if(nAttstate!=1){
  47. if(LastnAttstate!=nAttstate){ MSG_WARN(1,"reg off");}
  48. netWork.reg=0;
  49. netWork.pdp=0;
  50. netWork.netReady=0;
  51. MSG_INFO(1,"wait reg");
  52. if(regCnt==60){
  53. MSG_WARN(1,"!!!CFUN=0");
  54. LSAPI_NET_CFUN(0);
  55. }else if(regCnt>=61) {
  56. regCnt=0;
  57. MSG_WARN(1,"!!!CFUN=1");
  58. LSAPI_NET_CFUN(1);
  59. }
  60. ++regCnt;
  61. }else{
  62. regCnt=0;
  63. if(LastnAttstate!=nAttstate){ MSG_WARN(1,"reg on");}
  64. netWork.reg=1;
  65. nRet = LSAPI_NET_GetGprsActState(&nActstate);
  66. if(nRet!=0){
  67. MSG_WARN(1,"LSAPI_NET_GetGprsActState get failed:%d",nRet);
  68. }else{
  69. if(nActstate!=1){
  70. if(LastnActstate != nActstate){ MSG_WARN(1,"pdp off");}
  71. netWork.pdp=0;
  72. netWork.netReady=0;
  73. // ls_PdpActive(); HYL
  74. }else{
  75. if(LastnActstate != nActstate){ MSG_WARN(1,"pdp on");}
  76. netWork.pdp=1;
  77. netIf=LSAPI_NET_GET_GprsNetIf();
  78. if(false==netIf){
  79. if(lastNetIf!=netIf){ MSG_WARN(1,"netif off");}
  80. netWork.netReady=0;
  81. MSG_INFO(1,"netif start create");
  82. LSAPI_NET_NetIf_Create();
  83. MSG_INFO(1,"netif create done");
  84. }else{
  85. if(lastNetIf!=netIf){ MSG_WARN(1,"netif on");}
  86. netWork.netReady=1;
  87. }
  88. lastNetIf=netIf;
  89. }
  90. LastnActstate=nActstate;
  91. }
  92. }
  93. LastnAttstate=nAttstate;
  94. }
  95. void userExitThread(const char *name){
  96. MSG_INFO("[DieThread:%x]%s", LSAPI_OSI_ThreadCurrent(),name);
  97. LSAPI_OSI_ThreadExit();
  98. }
  99. void threadPostEvent(LSAPI_OSI_Thread_t *threadId, unsigned int id){
  100. LSAPI_OSI_Event_t pEventSend;
  101. pEventSend.id=id;
  102. osiEventTrySend(threadId,&pEventSend,0);
  103. }
  104. void domainCallBack(void *param){
  105. LSAPI_OSI_Event_t *ev = (LSAPI_OSI_Event_t *)param;
  106. bool needtobeclose=FALSE;
  107. if (ev->id== LSAPI_SOCK_DNS_RESOLV_SUC_IND) {
  108. LSAPI_SOCK_IP_ADDR_t* nIpAddr = (LSAPI_SOCK_IP_ADDR_t *)ev->param1;
  109. snprintf(domainInfo.address, sizeof(domainInfo.address),"%s",LSAPI_SOCK_IPAddr_ntoa(nIpAddr));
  110. MSG_INFO(1,"LSAPI_SOCK_Gethostbyname callback: %s",domainInfo.address);
  111. domainInfo.status=2;
  112. needtobeclose=TRUE;
  113. }else if (ev->id== LSAPI_SOCK_RESOLV_ERR_IND) {
  114. MSG_WARN(1,"LSAPI_SOCK_Gethostbyname EV_CFW_DNS_RESOLV_ERR_IND FAIL");
  115. domainInfo.status=0;
  116. needtobeclose=TRUE;
  117. }
  118. LSAPI_OSI_Free(ev);
  119. if(TRUE==needtobeclose) threadPostEvent(LSAPI_OSI_ThreadCurrent(),USER_EVENT_EXIT);
  120. }
  121. void dnsEntry(void *param){
  122. LSAPI_DNS_RESULT_t dnsRet;
  123. LSAPI_SOCK_IP_ADDR_t pAddr;
  124. LSAPI_OSI_Event_t event = {};
  125. MSG_INFO(1,"[NewThread:dnsEntry]%x:%s",LSAPI_OSI_ThreadCurrent(),domainInfo.address);
  126. dnsRet=LSAPI_SOCK_Gethostbyname(FOTADOMAIN_ADDR, &pAddr, 1,0,domainCallBack,(void *)param);
  127. if(dnsRet==LSAPI_RESOLV_COMPLETE) {
  128. MSG_INFO(1,"domain LSAPI_RESOLV_COMPLETE");
  129. snprintf(domainInfo.address, sizeof(domainInfo.address),"%s",LSAPI_SOCK_IPAddr_ntoa(&pAddr));
  130. domainInfo.status=2;
  131. userExitThread("dnsEntry");
  132. }else if(dnsRet==LSAPI_RESOLV_QUERY_INVALID){
  133. MSG_WARN(1,"domain LSAPI_RESOLV_QUERY_INVALID");
  134. domainInfo.status=0;
  135. userExitThread("dnsEntry");
  136. }else if(dnsRet==LSAPI_RESOLV_QUERY_QUEUED){
  137. MSG_INFO(1,"domain LSAPI_RESOLV_QUERY_QUEUED");
  138. //domainInfo.status=0;
  139. }
  140. for(;;){
  141. LSAPI_OSI_EventWait(LSAPI_OSI_ThreadCurrent(), &event);
  142. if(USER_EVENT_EXIT==event.id){
  143. userExitThread("dnsEntry");
  144. }
  145. }
  146. }
  147. void dns_Thread(void)
  148. {
  149. domainInfo.status=1; //进行中
  150. if(0==LSAPI_OSI_ThreadCreate("dns", dnsEntry,NULL,LSAPI_OSI_PRIORITY_NORMAL,1024,4))MSG_INFO(1,"creat dnsEntry error");
  151. }
  152. void Domain_Process(unsigned int interval)
  153. {
  154. static unsigned short cnt=0;
  155. if(++cnt<(1000/interval)) return;//1秒读一次ADC即可
  156. cnt=0;
  157. if(domainInfo.status!=0)return;
  158. if(netWork.netReady==0)return;
  159. dns_Thread();
  160. }
  161. static unsigned short translateAfterRecv(unsigned char *src, unsigned char *in_src,unsigned short length){
  162. unsigned short i,retlen=0;
  163. unsigned char lastByte=0xff;
  164. unsigned char *savePtr=src;
  165. for(i=1;i<length-2;i++){
  166. if(lastByte==0x7D){
  167. if(in_src[i]==0x01){
  168. *savePtr++=0x7D;
  169. retlen++;
  170. }else if(in_src[i]==0x02){
  171. *savePtr++=0x7E;
  172. retlen++;
  173. }else{
  174. *savePtr++=in_src[i];
  175. retlen++;
  176. }
  177. }else{
  178. *savePtr++=in_src[i];
  179. retlen++;
  180. }
  181. lastByte=in_src[i];
  182. }
  183. return retlen;
  184. }
  185. void fillTargetVersion(char *filePath){
  186. //120.77.66.129/higos/fotaFiles/M5700_POC_ZT_2006.pack
  187. //-->120.77.66.129/higos/fotaFiles/M5700_POC_ZT_20052006.pack
  188. int i,len;
  189. char findDot=0;
  190. char info[30];
  191. len=strlen(filePath)-1;
  192. for(i=len;i>=0;i--){
  193. if(findDot==0){
  194. if(filePath[i]=='.'){
  195. filePath[i]=0;
  196. findDot=1;
  197. }
  198. }else if(filePath[i]=='_'){
  199. snprintf(targetVersion,sizeof(targetVersion),"%s",&filePath[i+1]);
  200. strcpy(filePath+i+1,OHPOC_VERSION);
  201. strcat(filePath, targetVersion);
  202. strcat(filePath, ".pack");
  203. return;
  204. }
  205. }
  206. }
  207. #define BUF_SIZE 150
  208. static signed char RecvDataHandle(unsigned char *src, unsigned short length){
  209. unsigned short i,j;
  210. unsigned int k;
  211. unsigned char num_7E=0,sum=0,reallen;
  212. unsigned short takenotelen=0,temp;
  213. unsigned char temBuf[BUF_SIZE*2];
  214. unsigned char *realPtr;
  215. unsigned char filePath[80];
  216. unsigned char gpsTime;
  217. static bool handleDataOk=FALSE;
  218. for(i=0;i<length;i++){
  219. takenotelen++;
  220. if(src[i]==0x7E){
  221. if(++num_7E>=2){
  222. break;
  223. }
  224. }
  225. }
  226. memcpy(temBuf, src,takenotelen);
  227. if(num_7E != 2 || takenotelen<=2) return 1;
  228. j=translateAfterRecv(temBuf,src,takenotelen);
  229. if(j<12) return 2;//header len err
  230. temp = temBuf[2];temp <<= 8;temp &= 0xFF00;
  231. temp |= temBuf[3];
  232. if(((temp >> 10)&0xF) != 0) return 4;//no support sub packet or encrypt
  233. temp &= 0x3FF;//sms length
  234. if((12+temp) != j) return 5;//packet length err
  235. for(i=0;i<j-0;i++) sum ^= temBuf[i];
  236. if(sum != temBuf[j+1]) return 6;//sum err
  237. realPtr = temBuf + 12;
  238. if(temBuf[0] == 0x89 && temBuf[1] == 0x29){
  239. if(handleDataOk==TRUE) return 8;
  240. realPtr += 6;
  241. //handle ack info
  242. /* 超时响应
  243. if(realPtr[0]==1){
  244. rightFlag=1;//have right
  245. updateLocalTime(realPtr-6);
  246. }
  247. else rightFlag=0;//no right
  248. tudptime2_3=realPtr[1];
  249. tudptime4=realPtr[2];
  250. */
  251. gpsTime=realPtr[3];
  252. realPtr += 4;//point to extend info
  253. temp -= 9;//extend info length
  254. k=temp;
  255. while(k>0){
  256. if(realPtr[0]==1){
  257. //take type 1 info, for poc update
  258. i=realPtr[1];//take length
  259. //decrypt
  260. realPtr += 2;
  261. for(j=0;j<i;j++){
  262. if(j % 2 !=0) filePath[j]=realPtr[j] ^ 0xB8;
  263. else filePath[j]=realPtr[j] ^ 0x56;
  264. }
  265. filePath[j]=0;
  266. MSG_INFO(1,"Get file path:%s",filePath);
  267. //backup file path
  268. strcpy(wgetPath,filePath);
  269. fillTargetVersion(wgetPath);
  270. MSG_INFO(1,"target:%s,file:%s",targetVersion,wgetPath);
  271. serverAck=1;
  272. handleDataOk=TRUE;
  273. return 0;
  274. }else k -= (1+1+realPtr[1]);
  275. }
  276. serverAck=2;
  277. return 7;
  278. }
  279. else return 3;//ID not fit
  280. }
  281. void tupParaSet(TUPSEND_DEF *para, unsigned short port, LSAPI_SOCK_Callback *callback,unsigned short stacksize){
  282. para->port=port;
  283. para->tupRecvCallBack=callback;
  284. para->stacksize=stacksize;
  285. }
  286. void authFotaRecv_cb(void *param){
  287. int len_ret;
  288. unsigned char buffer[BUF_SIZE];
  289. LSAPI_OSI_Event_t *pEvent = (LSAPI_OSI_Event_t *)param;
  290. TUPSEND_DEF *para=(TUPSEND_DEF *)pEvent->param3;
  291. bool needExitThread=FALSE;
  292. // showTupEventInfo("authFota",pEvent->id,para,0);
  293. switch(pEvent->id){
  294. case LSAPI_SOCK_TCPIP_SOCKET_CONNECT_RSP:
  295. // if(AF_TCP_WAIT==fotaStatus) fotaStatus=AF_SEND_WAIT_ACK;
  296. break;
  297. case LSAPI_SOCK_TCPIP_SOCKET_SEND_RSP:break;
  298. case LSAPI_SOCK_TCPIP_SOCKET_CLOSE_RSP:
  299. MSG_WARN(1,"authfota client close");
  300. serverAck=-1;
  301. needExitThread=TRUE;
  302. break;
  303. case LSAPI_SOCK_TCPIP_REV_DATA_IND:
  304. while(1){
  305. len_ret=LSAPI_SOCK_Recvfrom(para->fd, buffer, sizeof(buffer),0, &para->sockAddr);
  306. if(len_ret>=0){
  307. if(len_ret==0) break;
  308. len_ret=RecvDataHandle(buffer, len_ret);
  309. MSG_INFO(1,"RecvDataHandle return %d", len_ret);
  310. }else{
  311. MSG_WARN(1,"authFotaRecv_cb recv failed");
  312. break;
  313. }
  314. }
  315. break;
  316. case LSAPI_SOCK_TCPIP_CLOSE_IND:break;
  317. default: break;
  318. }
  319. LSAPI_OSI_Free(pEvent);
  320. if(TRUE==needExitThread) threadPostEvent(LSAPI_OSI_ThreadCurrent(),USER_EVENT_EXIT);
  321. }
  322. void userCloseSocket(int *fd){
  323. if(*fd<=0) return;
  324. MSG_INFO(1,"SockClose:%d",*fd);
  325. LSAPI_SOCK_Close(*fd);
  326. *fd=0;
  327. }
  328. void sockEntry(void *param){
  329. TUPSEND_DEF *para=(TUPSEND_DEF *)param;
  330. unsigned int sockfd;
  331. unsigned char type;
  332. int g_keepalive = 0;
  333. MSG_INFO(1,"[NewThread:sockEntry]%x",LSAPI_OSI_ThreadCurrent());
  334. if(TUP_TCP==para->tupType) type=LSAPI_SOCK_TCPIP_SOCK_STREAM;
  335. else type=LSAPI_SOCK_TCPIP_SOCK_DGRAM;
  336. sockfd = LSAPI_SOCK_Create(LSAPI_SOCK_TCPIP_AF_INET, type, 0, para->tupRecvCallBack,(uint32_t)para);
  337. if(sockfd<0){
  338. MSG_INFO(1,"sockfd create err=%d",LSAPI_SOCK_Error());
  339. para->errno=1;
  340. LSAPI_OSI_ThreadExit();
  341. }
  342. para->fd=sockfd;
  343. MSG_INFO(1,"tup info:%d,%s:%d", sockfd,para->saddr, para->port);
  344. LSAPI_SOCK_TcpipSocketAddr_t *serveraddr=&para->sockAddr;
  345. serveraddr->sin_family = LSAPI_SOCK_TCPIP_AF_INET;
  346. serveraddr->sin_port = LSAPI_SOCK_htons(para->port);
  347. serveraddr->sin_addr.s_addr = LSAPI_SOCK_inet_addr(para->saddr);
  348. LSAPI_SOCK_SetSockOpt(sockfd, LSAPI_SOCK_SOL_SOCKET, LSPAI_SOCK_KEEPALIVE, (void *)&g_keepalive, sizeof(g_keepalive));
  349. if(LSAPI_RET_OK != LSAPI_SOCK_Connect(sockfd, serveraddr)){
  350. MSG_ERR(1,"sockfd connect err");
  351. userCloseSocket(&sockfd);
  352. para->fd=0;
  353. para->errno=2;
  354. userExitThread("sockEntry");
  355. return;
  356. }
  357. /*
  358. para->ptimer_t=LSAPI_OSI_TimerCreate(LSAPI_OSI_ThreadCurrent(), sockTimerOutCallback, param);
  359. if(NULL != para->ptimer_t) LSAPI_OSI_TimerStart(para->ptimer_t, 10000);
  360. MSG_INFO(1,"open fd:%d",sockfd);
  361. */
  362. LSAPI_OSI_Event_t event={};
  363. for(;;){
  364. LSAPI_OSI_EventWait(LSAPI_OSI_ThreadCurrent(), &event);
  365. if(USER_EVENT_EXIT==event.id){
  366. userCloseSocket(&para->fd);
  367. userExitThread("sockEntry");
  368. }
  369. }
  370. }
  371. LSAPI_OSI_Thread_t *TcpConnectthreadid = NULL;
  372. TUP_CONNECT_ENUM tryConnectTup(char *seraddr, TUP_ENUM tup,TUPSEND_DEF *para){
  373. para->saddr=seraddr;
  374. para->tupType=tup;
  375. para->errno=0;
  376. para->fd=0;
  377. if(NULL==LSAPI_OSI_ThreadCreate("sockCreate", sockEntry,(void *)para,LSAPI_OSI_PRIORITY_NORMAL,para->stacksize,4)){
  378. MSG_ERR(1,"tryConnectTup thread create error for %s",seraddr);
  379. return TUP_STATUS_FALSE;
  380. }
  381. return TUP_STATUS_WAIT;
  382. }
  383. void FOTA_Process(unsigned char interval)
  384. {
  385. unsigned char ret;
  386. static unsigned short tickcnt=0;
  387. if(++tickcnt<(1000/interval)) return;//1秒读一次ADC即可
  388. tickcnt=0;
  389. if(AppFotaInfo.fota_statue==ENABLE){
  390. //try to update poc
  391. tryUpdateLte(wgetPath);
  392. /*
  393. do{
  394. ret=getLteUpdateStatus();
  395. PTTimerStart(ptPool, &ptTimer,10);//should be fast
  396. PT_WAIT_UNTIL(pt, PTTimerIsExpired(&ptTimer));
  397. }while(LTE_UPDATE_BUSY==ret);
  398. */
  399. ret=getLteUpdateStatus();
  400. if(LTE_UPDATE_BUSY==ret){
  401. AppFotaInfo.fota_statue=DISENABLE;
  402. goto PTHREAD_END;;
  403. }
  404. }
  405. if(1==serverAck){
  406. MSG_INFO(1,"get server ack");
  407. if(0==strcmp(OHPOC_VERSION,targetVersion)){
  408. if(fotaStep==0){
  409. fotaStep=6;
  410. MSG_INFO(1,"无可更新版本");
  411. }
  412. } else {
  413. if(fotaStep==0){
  414. fotaStep=9;
  415. }
  416. }
  417. }else if(2==serverAck){
  418. MSG_INFO(1,"no extend info, exit");//扩展
  419. goto PTHREAD_END;
  420. }else{
  421. // MSG_INFO(1,"ack data not fit, exit");
  422. goto PTHREAD_END;
  423. }
  424. PTHREAD_END:
  425. serverAck=-1;
  426. return;
  427. }
  428. static void lteEntry(void *param){
  429. MSG_INFO(1,"lteEntry:url=%s",httpinfo.url);
  430. if(LSAPI_HTTP_GET(&httpinfo)){
  431. MSG_WARN(1,"httpget err");
  432. goto FOTA_FAILED;
  433. }
  434. if(httpinfo.recvlen==0){
  435. MSG_WARN(1,"pack file len err");
  436. goto FOTA_FAILED;
  437. }
  438. MSG_WARN(1,"pack file len:%d",httpinfo.recvlen);
  439. showLoading();
  440. if(false==LSAPI_FOTA_UpdataSetReady(httpinfo.RecvData,httpinfo.recvlen)){
  441. MSG_WARN(1,"fota file update err");
  442. if(fotaStep!=4&&fotaStep!=5)fotaStep=7;
  443. goto FOTA_FAILED;
  444. }
  445. lteResult=LTE_UPDATE_DONE;
  446. fotaStep=8;
  447. LSAPI_HTTP_ParaClear(&httpinfo);
  448. MSG_WARN(1,"fota file done,and reboot");
  449. lteThread=NULL;
  450. MSG_INFO(1,"lteEntry End");
  451. LSAPI_OSI_ThreadExit();
  452. return;
  453. FOTA_FAILED:
  454. LSAPI_HTTP_ParaClear(&httpinfo);
  455. lteThread=NULL;
  456. lteResult=LTE_UPDATE_FAILED;
  457. MSG_INFO(1,"lteEntry End");
  458. LSAPI_OSI_ThreadExit();
  459. }
  460. void tryUpdateLte(char *httpfilePath){
  461. if(NULL != lteThread){
  462. MSG_WARN(1,"lte proc is busy");
  463. return;
  464. }
  465. lteResult=LTE_UPDATE_BUSY;
  466. memset(&httpinfo, 0, sizeof(HttpInfoSet));
  467. snprintf(httpinfo.url, sizeof(httpinfo.url), "http://%s",httpfilePath);
  468. httpinfo.cid=1;
  469. lteThread=LSAPI_OSI_ThreadCreate("lteEntry", lteEntry,NULL,LSAPI_OSI_PRIORITY_NORMAL,HTTP_THREAD_STACK,4);
  470. if(NULL==lteThread){
  471. MSG_ERR(1,"lteEntry thread error");
  472. lteResult=LTE_UPDATE_FAILED;
  473. }
  474. }
  475. static unsigned short translateBeforeSend(unsigned char *src, unsigned short length){
  476. //0x7E-->0X7D,0X02
  477. //0X7D-->0X7D,0X01
  478. unsigned char temp[BUF_SIZE*2];
  479. unsigned short i,retlen;
  480. retlen=0;
  481. for(i=0;i<length;i++){
  482. if(src[i]==0x7E){
  483. temp[retlen++]=0x7D;
  484. temp[retlen++]=0x02;
  485. }else if(src[i]==0x7D){
  486. temp[retlen++]=0x7D;
  487. temp[retlen++]=0x01;
  488. }else{
  489. temp[retlen++]=src[i];
  490. }
  491. }
  492. memcpy(src, temp, retlen);
  493. return retlen;
  494. }
  495. void trySendTup(unsigned char *data, unsigned short len, TUP_ENUM tup, TUPSEND_DEF *para){
  496. int ret;
  497. if(para->fd<=0) return;
  498. if(TUP_TCP==para->tupType) ret=LSAPI_SOCK_Send(para->fd,data,len,0);
  499. else ret=LSAPI_SOCK_Sendto(para->fd,data,len,0, &para->sockAddr, sizeof(LSAPI_SOCK_TcpipSocketAddr_t));
  500. if(ret<0) MSG_INFO(1,"tup[%d]:%d send failed", para->fd,len);
  501. else MSG_INFO(1,"tup[%d]:%d send ok",para->fd,len);
  502. }
  503. void infoPacketAndSend(
  504. unsigned int psn,
  505. char *imei,
  506. char *version,
  507. char *modemType,
  508. unsigned char udpvalue2_3,
  509. unsigned char udpvalue4){
  510. unsigned char sendbuf[BUF_SIZE];
  511. unsigned char *ptr=sendbuf;
  512. unsigned char sum=0,th,tl;
  513. unsigned short i,len;
  514. char temp[20];
  515. unsigned int ret;
  516. unsigned int tver=atoi(version);
  517. static short liushui=0;//hyl
  518. len=0;
  519. //make login info
  520. //start indicator
  521. ptr[len++]=0x7E;
  522. //header
  523. //sms type 2bytes
  524. ptr[len++]=0x09;
  525. ptr[len++]=0x29;
  526. //sms property 2bytes
  527. i=59;//only low 10bits are define as sms length
  528. ptr[len++]=i>>8;//no sub packets,no encrypt
  529. ptr[len++]=i;//sms length
  530. //device SN 6bytes
  531. snprintf(temp, sizeof(temp),"%012d",psn);
  532. for(i=0;i<6;i++){
  533. th=temp[2*i];
  534. tl=temp[2*i+1];
  535. th <<= 4;th &= 0xF0;
  536. tl &= 0x0F;
  537. ptr[len++]=th | tl;
  538. }
  539. //serial nuber 2bytes
  540. ptr[len++]=(liushui>>8)&0xFF;
  541. ptr[len++]=liushui&0xFF;
  542. liushui++;
  543. //sms info
  544. //poc modle:EC20TGA_POC 16bytes
  545. memset(&ptr[len], 0, 16);
  546. // snprintf(temp, sizeof(temp), "%s_%s",fotaGetModle(),fotaGetCt());
  547. /*
  548. #define OHPOC_MODLE "M5700" //浣滀负OPEN鏃讹紝姝ゅ€肩敤浜嶧OTA鍗囩骇鏍囪瘑浣跨敤
  549. #define OHPOC_CUST OHPOC_APP_NAME //浣滀负OPEN鏃讹紝姝ゅ€肩敤浜嶧OTA鍗囩骇鏍囪瘑浣跨敤
  550. */
  551. snprintf(temp, sizeof(temp), "%s_%s",OHPOC_MODLE,OHPOC_CUST);
  552. memcpy(&ptr[len],temp,16);
  553. len += 16;
  554. //poc tver 4bytes
  555. ptr[len++]=tver>>24;
  556. ptr[len++]=tver>>16;
  557. ptr[len++]=tver>>8;
  558. ptr[len++]=tver;
  559. //mcu modle 16bytes
  560. memset(&ptr[len], 0, 16);
  561. len += 16;
  562. //mcu version 4bytes
  563. memset(&ptr[len], 0, 4);
  564. len += 4;
  565. //NET CODE:IMEI 16:fill 15bytes
  566. memset(&ptr[len], 0, 16);
  567. memcpy(&ptr[len], imei, 15);
  568. len += 16;
  569. //current udp2_3 heart time 1byte
  570. ptr[len++]=udpvalue2_3;
  571. //current udp4 heart time 1byte
  572. ptr[len++]=udpvalue4;
  573. //current gps upload time 1byte
  574. ptr[len++]=0;
  575. //verify code
  576. for(i=1;i<len;i++) sum ^= sendbuf[i];
  577. ptr[len++]=sum;
  578. //translate
  579. len=translateBeforeSend(sendbuf+1,len-1)+1;
  580. sendbuf[len++]=0x7E;
  581. //printf("Len:%d\r\n",len);
  582. //for(i=0;i<len;i++) printf("%02x",sendbuf[i]);
  583. //printf("\r\n");
  584. //send now
  585. //wlog_info("udp send data len %d\n", len)
  586. trySendTup(sendbuf,len,TUP_UDP,&tupsendPara);
  587. }