upgrade.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. #include "ets_sys.h"
  2. #include "osapi.h"
  3. #include "os_type.h"
  4. #include "lwip/err.h"
  5. #include "lwip/ip_addr.h"
  6. #include "lwip/mem.h"
  7. #include "lwip/app/espconn.h"
  8. #include "upgrade.h"
  9. #include "upgrade_lib.c"
  10. #define UPGRADE_DEBUG
  11. #ifdef UPGRADE_DEBUG
  12. #define UPGRADE_DBG os_printf
  13. #else
  14. #define UPGRADE_DBG
  15. #endif
  16. LOCAL struct espconn *upgrade_conn;
  17. LOCAL uint8 *pbuf;
  18. LOCAL os_timer_t upgrade_10s;
  19. LOCAL os_timer_t upgrade_timer;
  20. LOCAL uint32 totallength = 0;
  21. LOCAL uint32 sumlength = 0;
  22. /******************************************************************************
  23. * FunctionName : upgrade_disconcb
  24. * Description : The connection has been disconnected successfully.
  25. * Parameters : arg -- Additional argument to pass to the callback function
  26. * Returns : none
  27. *******************************************************************************/
  28. LOCAL void ICACHE_FLASH_ATTR
  29. upgrade_disconcb(void *arg)
  30. {
  31. struct espconn *pespconn = arg;
  32. if (pespconn == NULL) {
  33. return;
  34. }
  35. os_free(pespconn->proto.tcp);
  36. pespconn->proto.tcp = NULL;
  37. os_free(pespconn);
  38. pespconn = NULL;
  39. upgrade_conn = NULL;
  40. }
  41. /******************************************************************************
  42. * FunctionName : upgrade_datasent
  43. * Description : Data has been sent successfully,This means that more data can
  44. * be sent.
  45. * Parameters : arg -- Additional argument to pass to the callback function
  46. * Returns : none
  47. *******************************************************************************/
  48. LOCAL void ICACHE_FLASH_ATTR
  49. upgrade_datasent(void *arg)
  50. {
  51. struct espconn *pespconn = arg;
  52. if (pespconn ->state == ESPCONN_CONNECT) {
  53. }
  54. }
  55. /******************************************************************************
  56. * FunctionName : upgrade_deinit
  57. * Description : disconnect the connection with the host
  58. * Parameters : bin -- server number
  59. * Returns : none
  60. *******************************************************************************/
  61. void ICACHE_FLASH_ATTR
  62. LOCAL upgrade_deinit(void)
  63. {
  64. if (system_upgrade_flag_check() != UPGRADE_FLAG_START) {
  65. system_upgrade_deinit();
  66. }
  67. }
  68. /******************************************************************************
  69. * FunctionName : upgrade_10s_cb
  70. * Description : Processing the client when connected with host time out
  71. * Parameters : pespconn -- A point to the host
  72. * Returns : none
  73. *******************************************************************************/
  74. LOCAL void ICACHE_FLASH_ATTR upgrade_10s_cb(struct espconn *pespconn)
  75. {
  76. if (pespconn == NULL) {
  77. return;
  78. }
  79. system_upgrade_deinit();
  80. os_free(pespconn->proto.tcp);
  81. pespconn->proto.tcp = NULL;
  82. os_free(pespconn);
  83. pespconn = NULL;
  84. upgrade_conn = NULL;
  85. }
  86. /******************************************************************************
  87. * FunctionName : user_upgrade_check
  88. * Description : Processing the received data from the server
  89. * Parameters : arg -- Additional argument to pass to the callback function
  90. * pusrdata -- The received data (or NULL when the connection has been closed!)
  91. * length -- The length of received data
  92. * Returns : none
  93. *******************************************************************************/
  94. LOCAL void ICACHE_FLASH_ATTR
  95. upgrade_check(struct upgrade_server_info *server)
  96. {
  97. UPGRADE_DBG("upgrade_check\n");
  98. if (system_upgrade_flag_check() != UPGRADE_FLAG_FINISH) {
  99. totallength = 0;
  100. sumlength = 0;
  101. os_timer_disarm(&upgrade_timer);
  102. system_upgrade_flag_set(UPGRADE_FLAG_IDLE);
  103. upgrade_deinit();
  104. server->upgrade_flag = false;
  105. if (server->check_cb != NULL) {
  106. server->check_cb(server);
  107. }
  108. } else {
  109. os_timer_disarm(&upgrade_timer);
  110. upgrade_deinit();
  111. server->upgrade_flag = true;
  112. if (server->check_cb != NULL) {
  113. server->check_cb(server);
  114. }
  115. }
  116. #ifdef UPGRADE_SSL_ENABLE
  117. espconn_secure_disconnect(upgrade_conn);
  118. #else
  119. espconn_disconnect(upgrade_conn);
  120. #endif
  121. }
  122. /******************************************************************************
  123. * FunctionName : upgrade_download
  124. * Description : Processing the upgrade data from the host
  125. * Parameters : bin -- server number
  126. * pusrdata -- The upgrade data (or NULL when the connection has been closed!)
  127. * length -- The length of upgrade data
  128. * Returns : none
  129. *******************************************************************************/
  130. LOCAL void ICACHE_FLASH_ATTR
  131. upgrade_download(void *arg, char *pusrdata, unsigned short length)
  132. {
  133. char *ptr = NULL;
  134. char *ptmp2 = NULL;
  135. char lengthbuffer[32];
  136. if (totallength == 0 && (ptr = (char *)os_strstr(pusrdata, "\r\n\r\n")) != NULL &&
  137. (ptr = (char *)os_strstr(pusrdata, "Content-Length")) != NULL) {
  138. ptr = (char *)os_strstr(pusrdata, "\r\n\r\n");
  139. length -= ptr - pusrdata;
  140. length -= 4;
  141. totallength += length;
  142. UPGRADE_DBG("upgrade file download start.\n");
  143. system_upgrade(ptr + 4, length);
  144. ptr = (char *)os_strstr(pusrdata, "Content-Length: ");
  145. if (ptr != NULL) {
  146. ptr += 16;
  147. ptmp2 = (char *)os_strstr(ptr, "\r\n");
  148. if (ptmp2 != NULL) {
  149. os_memset(lengthbuffer, 0, sizeof(lengthbuffer));
  150. os_memcpy(lengthbuffer, ptr, ptmp2 - ptr);
  151. sumlength = atoi(lengthbuffer);
  152. } else {
  153. UPGRADE_DBG("sumlength failed\n");
  154. }
  155. } else {
  156. UPGRADE_DBG("Content-Length: failed\n");
  157. }
  158. } else {
  159. totallength += length;
  160. os_printf("totallen = %d\n",totallength);
  161. system_upgrade(pusrdata, length);
  162. }
  163. if (totallength == sumlength) {
  164. UPGRADE_DBG("upgrade file download finished.\n");
  165. system_upgrade_flag_set(UPGRADE_FLAG_FINISH);
  166. totallength = 0;
  167. sumlength = 0;
  168. upgrade_check(upgrade_conn->reverse);
  169. os_timer_disarm(&upgrade_10s);
  170. os_timer_setfn(&upgrade_10s, (os_timer_func_t *)upgrade_deinit, NULL);
  171. os_timer_arm(&upgrade_10s, 10, 0);
  172. } else {
  173. if (upgrade_conn->state != ESPCONN_READ) {
  174. totallength = 0;
  175. sumlength = 0;
  176. os_timer_disarm(&upgrade_10s);
  177. os_timer_setfn(&upgrade_10s, (os_timer_func_t *)upgrade_check, upgrade_conn->reverse);
  178. os_timer_arm(&upgrade_10s, 10, 0);
  179. }
  180. }
  181. }
  182. /******************************************************************************
  183. * FunctionName : upgrade_connect
  184. * Description : client connected with a host successfully
  185. * Parameters : arg -- Additional argument to pass to the callback function
  186. * Returns : none
  187. *******************************************************************************/
  188. LOCAL void ICACHE_FLASH_ATTR
  189. upgrade_connect_cb(void *arg)
  190. {
  191. struct espconn *pespconn = arg;
  192. UPGRADE_DBG("upgrade_connect_cb\n");
  193. os_timer_disarm(&upgrade_10s);
  194. espconn_regist_disconcb(pespconn, upgrade_disconcb);
  195. espconn_regist_sentcb(pespconn, upgrade_datasent);
  196. if (pbuf != NULL) {
  197. UPGRADE_DBG("%s\n", pbuf);
  198. #ifdef UPGRADE_SSL_ENABLE
  199. espconn_secure_sent(pespconn, pbuf, os_strlen(pbuf));
  200. #else
  201. espconn_sent(pespconn, pbuf, os_strlen(pbuf));
  202. #endif
  203. }
  204. }
  205. /******************************************************************************
  206. * FunctionName : upgrade_connection
  207. * Description : connect with a server
  208. * Parameters : bin -- server number
  209. * url -- the url whitch upgrade files saved
  210. * Returns : none
  211. *******************************************************************************/
  212. LOCAL void ICACHE_FLASH_ATTR
  213. upgrade_connect(struct upgrade_server_info *server)
  214. {
  215. UPGRADE_DBG("upgrade_connect\n");
  216. pbuf = server->url;
  217. espconn_regist_connectcb(upgrade_conn, upgrade_connect_cb);
  218. espconn_regist_recvcb(upgrade_conn, upgrade_download);
  219. system_upgrade_init();
  220. system_upgrade_flag_set(UPGRADE_FLAG_START);
  221. #ifdef UPGRADE_SSL_ENABLE
  222. espconn_secure_connect(upgrade_conn);
  223. #else
  224. espconn_connect(upgrade_conn);
  225. #endif
  226. os_timer_disarm(&upgrade_10s);
  227. os_timer_setfn(&upgrade_10s, (os_timer_func_t *)upgrade_10s_cb, upgrade_conn);
  228. os_timer_arm(&upgrade_10s, 10000, 0);
  229. }
  230. /******************************************************************************
  231. * FunctionName : user_upgrade_init
  232. * Description : parameter initialize as a client
  233. * Parameters : server -- A point to a server parmer which connected
  234. * Returns : none
  235. *******************************************************************************/
  236. bool ICACHE_FLASH_ATTR
  237. #ifdef UPGRADE_SSL_ENABLE
  238. system_upgrade_start_ssl(struct upgrade_server_info *server)
  239. #else
  240. system_upgrade_start(struct upgrade_server_info *server)
  241. #endif
  242. {
  243. if (system_upgrade_flag_check() == UPGRADE_FLAG_START) {
  244. return false;
  245. }
  246. if (server == NULL) {
  247. UPGRADE_DBG("server is NULL\n");
  248. return false;
  249. }
  250. if (upgrade_conn == NULL) {
  251. upgrade_conn = (struct espconn *)os_zalloc(sizeof(struct espconn));
  252. }
  253. if (upgrade_conn != NULL) {
  254. upgrade_conn->proto.tcp = NULL;
  255. upgrade_conn->type = ESPCONN_TCP;
  256. upgrade_conn->state = ESPCONN_NONE;
  257. upgrade_conn->reverse = server;
  258. if (upgrade_conn->proto.tcp == NULL) {
  259. upgrade_conn->proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp));
  260. }
  261. if (upgrade_conn->proto.tcp != NULL) {
  262. upgrade_conn->proto.tcp->local_port = espconn_port();
  263. upgrade_conn->proto.tcp->remote_port = server->port;
  264. os_memcpy(upgrade_conn->proto.tcp->remote_ip, server->ip, 4);
  265. UPGRADE_DBG("%s\n", __func__);
  266. upgrade_connect(server);
  267. if (server->check_cb != NULL) {
  268. os_timer_disarm(&upgrade_timer);
  269. os_timer_setfn(&upgrade_timer, (os_timer_func_t *)upgrade_check, server);
  270. os_timer_arm(&upgrade_timer, server->check_times, 0);
  271. }
  272. }
  273. }
  274. return true;
  275. }