http.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include "http.h"
  2. #include "log.h"
  3. #include "main.h"
  4. #include "lsapi_http.h"
  5. #include "lsapi_fota.h"
  6. #include "lsapi_os.h"
  7. #include "app.h"
  8. #include "domain.h"
  9. ///////////////////////////////////////FOTA LTE////////////////////////////////////////////
  10. #define FOTA_SERVER_ADDR "fota.rtuol.com"
  11. #define FOTA_FILE_FOLDER "RTL"
  12. HttpInfoSet fotaHttpInfo;
  13. LSAPI_OSI_Thread_t *fotaLteThread=NULL;
  14. LTE_UPDATE_ENUM fotaLteResult;
  15. char newVersion[30];
  16. LTE_UPDATE_ENUM getLteUpdateStatus(void){return fotaLteResult;}
  17. static void lteEntry(void *param);
  18. /*
  19. FOTA升级LTE接口API,实现从http服务器获取差分包
  20. 如果获取成功且差分包正确,则自动重启并完成LTE升级
  21. 如果失败则返回,不影响正常业务
  22. */
  23. void fotaLetPre(char *NewVer){
  24. int i;
  25. char ch;
  26. for(i=0;i<strlen(NewVer);i++){
  27. ch=NewVer[i];
  28. if(ch=='\r' || ch=='\n' || ch==0){
  29. NewVer[i]=0;
  30. break;
  31. }
  32. }
  33. wlog_info("CurVer:%s,NewVer:%s",APP_VERSION,NewVer);
  34. if(NewVer==NULL) return;
  35. if(0==strcmp(NewVer,APP_VERSION)) return;
  36. strcpy(newVersion,NewVer);
  37. if(NULL != fotaLteThread){
  38. wlog_warn("lte proc is busy:%s",NewVer);
  39. return;
  40. }
  41. fotaLteResult=LTE_UPDATE_BUSY;
  42. fotaLteThread=LSAPI_OSI_ThreadCreate("lteEntry", lteEntry,NULL,LSAPI_OSI_PRIORITY_NORMAL,HTTP_THREAD_STACK,4);
  43. if(NULL==fotaLteThread){
  44. wlog_error("lteEntry thread error");//因为是阻塞的,故需要开线程,且栈要比较大
  45. fotaLteResult=LTE_UPDATE_FAILED;
  46. }
  47. }
  48. static void lteEntry(void *param){
  49. T_UINT8 index,ret;
  50. T_INT8 ipAddr[20];
  51. wlog_info("lteEntry start");
  52. msgToInner("AT+LSHTTS=2,\"53735C0653477EA7\"\r\n");//即将升级
  53. //首先根据域名获取IP
  54. if(TRUE==setDomainForIp(FOTA_SERVER_ADDR, &index)){
  55. while(1){
  56. ret=getDomainForIp(index, ipAddr, sizeof(ipAddr));
  57. if(DOMAIN_ERR==ret){
  58. wlog_warn("fota addr domain failed");
  59. goto FOTA_FAILED;
  60. }else if(DOMAIN_OK==ret){
  61. wlog_info("fota domain done:%s->%s", FOTA_SERVER_ADDR,ipAddr);
  62. break;
  63. }else{//wait result
  64. LSAPI_OSI_ThreadSleep(20);
  65. }
  66. }
  67. }
  68. memset(&fotaHttpInfo, 0, sizeof(HttpInfoSet));
  69. snprintf(fotaHttpInfo.url, sizeof(fotaHttpInfo.url), "http://%s/%s/%s/%s_V%s%s.pack",
  70. ipAddr,
  71. FOTA_FILE_FOLDER,
  72. APP_NAME,
  73. APP_NAME,
  74. APP_VERSION,newVersion);//http://120.77.66.129/RTL/RTL271_V20022003.pack
  75. fotaHttpInfo.cid=1;
  76. wlog_info("lteEntry url:%s",fotaHttpInfo.url);
  77. if(LSAPI_HTTP_GET(&fotaHttpInfo)){//尝试获取差分包
  78. wlog_warn("httpget err");
  79. goto FOTA_FAILED;
  80. }
  81. if(fotaHttpInfo.recvlen==0){//差分包获取成功,但数据长度为0,FOTA失败
  82. wlog_warn("pack file len err");
  83. goto FOTA_FAILED;
  84. }
  85. wlog_warn("pack file len:%d",fotaHttpInfo.recvlen);
  86. LSAPI_OSI_ThreadSleep(1000);//为了让业务中的反馈包能发给服务器。
  87. if(false==LSAPI_FOTA_UpdataSetReady(fotaHttpInfo.RecvData,fotaHttpInfo.recvlen)){//差分包有数据,检测是否可以升级
  88. wlog_warn("LSAPI_FOTA_UpdataSetReady:FALSE");
  89. goto FOTA_FAILED;
  90. }
  91. fotaLteResult=LTE_UPDATE_DONE;
  92. //差分包正确且可以升级,清除缓存并执行重启,系统会自动完成LTE升级
  93. LSAPI_HTTP_ParaClear(&fotaHttpInfo);
  94. wlog_warn("fota file done,and reboot");
  95. #ifdef TAKE_NOTE_FOR_REBOOT
  96. saveRebootReason("fota done\r\n");
  97. #endif
  98. LSAPI_OSI_ThreadSleep(1000);
  99. msgToInner("AT+LSHTTS=0\r\n");//即将升级
  100. msgToInner("AT+LSHTTS=2,\"53735C0691CD542F\"\r\n");//即将重启
  101. LSAPI_OSI_ThreadSleep(1000);//仅为能看日志而加,去掉可能看不到日志
  102. LSAPI_SYS_reboot();
  103. return;
  104. FOTA_FAILED:
  105. LSAPI_OSI_ThreadSleep(1000);
  106. msgToInner("AT+LSHTTS=0\r\n");//即将升级
  107. msgToInner("AT+LSHTTS=2,\"900051FA53477EA7\"\r\n");//退出升级
  108. LSAPI_HTTP_ParaClear(&fotaHttpInfo);
  109. fotaLteThread=NULL;
  110. fotaLteResult=LTE_UPDATE_FAILED;
  111. wlog_info("lteEntry End");
  112. LSAPI_OSI_ThreadExit();
  113. }
  114. //////////////////////////////////////POST VOICE//////////////////////////////////////////
  115. #ifdef ENABLE_HTTP_POST
  116. HttpInfoSet postHttpInfo;
  117. LSAPI_OSI_Thread_t *postVoiceThread=NULL;
  118. POST_VOICE_ENUM postVoiceResult;
  119. unsigned char *postData=NULL;
  120. unsigned int postLen=0;
  121. T_BOOL postTimeFlag=FALSE;
  122. POST_VOICE_ENUM getLtePostStatus(void){return postVoiceResult;}
  123. static void postEntry(void *param);
  124. void tryPostVoiceData(unsigned char *pcm, unsigned int len, char *httpfilePath){
  125. if(NULL != postVoiceThread){
  126. wlog_warn("lte proc is busy");
  127. return;
  128. }
  129. if(len==0) return;
  130. postVoiceResult=POST_BUSY;
  131. memset(&postHttpInfo, 0, sizeof(HttpInfoSet));
  132. snprintf(postHttpInfo.url, sizeof(postHttpInfo.url), "http://%s",httpfilePath);
  133. postHttpInfo.cid=1;
  134. postData=pcm;
  135. postLen=len;
  136. postVoiceThread=LSAPI_OSI_ThreadCreate("postEntry", postEntry,NULL,LSAPI_OSI_PRIORITY_NORMAL,POST_THREAD_STACK,4);
  137. if(NULL==postVoiceThread){
  138. wlog_error("postEntry thread error");//因为是阻塞的,故需要开线程,且栈要比较大
  139. postVoiceResult=POST_FAILED;
  140. }
  141. }
  142. /*外部检测超时未成功时调用此API取消POST*/
  143. void postTimeOut(void){
  144. if(postVoiceResult!=POST_BUSY) return;
  145. postTimeFlag=TRUE;
  146. }
  147. static void postEntry(void *param){
  148. int i,index=0,sendlen;
  149. wlog_info("postEntry:url=%s,datalen=%d",postHttpInfo.url,postLen);
  150. i=postLen;
  151. do{
  152. if(i>sizeof(postHttpInfo.SendData)) sendlen=sizeof(postHttpInfo.SendData);
  153. else sendlen=i;
  154. memcpy(postHttpInfo.SendData, postData+index,sendlen);
  155. if(LSAPI_HTTP_POST(&postHttpInfo))
  156. wlog_warn("post failed, retry");
  157. else{
  158. i -= sendlen;
  159. index += sendlen;
  160. }
  161. LSAPI_OSI_ThreadSleep(10);
  162. if(postTimeFlag==TRUE){
  163. postTimeFlag=FALSE;
  164. wlog_info("post timeout cancle");
  165. break;
  166. }
  167. }while(i>0);
  168. if(i==0) wlog_info("post ok");
  169. LSAPI_HTTP_ParaClear(&postHttpInfo);
  170. postVoiceThread=NULL;
  171. postVoiceResult=POST_DONE;
  172. wlog_info("postEntry End");
  173. LSAPI_OSI_ThreadExit();
  174. }
  175. #endif