at_upgrade.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /******************************************************************************
  2. * Copyright 2015-2018 Espressif Systems (Wuxi)
  3. *
  4. * FileName: user_main.c
  5. *
  6. * Description: entry file of user application
  7. *
  8. * Modification history:
  9. * 2015/3/06, v1.0 create this file.
  10. *******************************************************************************/
  11. #include "c_types.h"
  12. #include "user_interface.h"
  13. #include "espconn.h"
  14. #include "mem.h"
  15. #include "osapi.h"
  16. #include "upgrade.h"
  17. #ifdef AT_UPGRADE_SUPPORT
  18. #ifdef AT_CUSTOM_UPGRADE
  19. #define UPGRADE_FRAME "{\"path\": \"/v1/messages/\", \"method\": \"POST\", \"meta\": {\"Authorization\": \"token %s\"},\
  20. \"get\":{\"action\":\"%s\"},\"body\":{\"pre_rom_version\":\"%s\",\"rom_version\":\"%s\"}}\n"
  21. #define pheadbuffer "Connection: keep-alive\r\n\
  22. Cache-Control: no-cache\r\n\
  23. User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36 \r\n\
  24. Accept: */*\r\n\
  25. Accept-Encoding: gzip,deflate\r\n\
  26. Accept-Language: zh-CN,eb-US;q=0.8\r\n\r\n"
  27. /**/
  28. struct espconn *pespconn = NULL;
  29. struct upgrade_server_info *upServer = NULL;
  30. static os_timer_t at_delay_check;
  31. static struct espconn *pTcpServer = NULL;
  32. static ip_addr_t host_ip;
  33. /******************************************************************************
  34. * FunctionName : user_esp_platform_upgrade_cb
  35. * Description : Processing the downloaded data from the server
  36. * Parameters : pespconn -- the espconn used to connetion with the host
  37. * Returns : none
  38. *******************************************************************************/
  39. LOCAL void ICACHE_FLASH_ATTR
  40. at_upDate_rsp(void *arg)
  41. {
  42. struct upgrade_server_info *server = arg;
  43. if(server->upgrade_flag == true)
  44. {
  45. os_printf("device_upgrade_success\r\n");
  46. at_response_ok();
  47. system_upgrade_reboot();
  48. }
  49. else
  50. {
  51. os_printf("device_upgrade_failed\r\n");
  52. at_response_error();
  53. }
  54. os_free(server->url);
  55. server->url = NULL;
  56. os_free(server);
  57. server = NULL;
  58. }
  59. /**
  60. * @brief Tcp client disconnect success callback function.
  61. * @param arg: contain the ip link information
  62. * @retval None
  63. */
  64. static void ICACHE_FLASH_ATTR
  65. at_upDate_discon_cb(void *arg)
  66. {
  67. struct espconn *pespconn = (struct espconn *)arg;
  68. uint8_t idTemp = 0;
  69. if(pespconn->proto.tcp != NULL)
  70. {
  71. os_free(pespconn->proto.tcp);
  72. }
  73. if(pespconn != NULL)
  74. {
  75. os_free(pespconn);
  76. }
  77. os_printf("disconnect\r\n");
  78. if(system_upgrade_start(upServer) == false)
  79. {
  80. at_response_error();
  81. }
  82. else
  83. {
  84. at_port_print("+CIPUPDATE:4\r\n");
  85. }
  86. }
  87. /**
  88. * @brief Udp server receive data callback function.
  89. * @param arg: contain the ip link information
  90. * @retval None
  91. */
  92. LOCAL void ICACHE_FLASH_ATTR
  93. at_upDate_recv(void *arg, char *pusrdata, unsigned short len)
  94. {
  95. struct espconn *pespconn = (struct espconn *)arg;
  96. char temp[32] = {0};
  97. uint8_t user_bin[12] = {0};
  98. uint8_t i = 0;
  99. os_timer_disarm(&at_delay_check);
  100. at_port_print("+CIPUPDATE:3\r\n");
  101. upServer = (struct upgrade_server_info *)os_zalloc(sizeof(struct upgrade_server_info));
  102. upServer->upgrade_version[5] = '\0';
  103. upServer->pespconn = pespconn;
  104. os_memcpy(upServer->ip, pespconn->proto.tcp->remote_ip, 4);
  105. upServer->port = pespconn->proto.tcp->remote_port;
  106. upServer->check_cb = at_upDate_rsp;
  107. upServer->check_times = 60000;
  108. if(upServer->url == NULL)
  109. {
  110. upServer->url = (uint8 *) os_zalloc(1024);
  111. }
  112. if(system_upgrade_userbin_check() == UPGRADE_FW_BIN1)
  113. {
  114. os_memcpy(user_bin, "user2.bin", 10);
  115. }
  116. else if(system_upgrade_userbin_check() == UPGRADE_FW_BIN2)
  117. {
  118. os_memcpy(user_bin, "user1.bin", 10);
  119. }
  120. os_sprintf(upServer->url,
  121. "GET /%s HTTP/1.1\r\nHost: "IPSTR"\r\n"pheadbuffer"",
  122. user_bin, IP2STR(upServer->ip));
  123. }
  124. LOCAL void ICACHE_FLASH_ATTR
  125. at_upDate_wait(void *arg)
  126. {
  127. struct espconn *pespconn = arg;
  128. os_timer_disarm(&at_delay_check);
  129. if(pespconn != NULL)
  130. {
  131. espconn_disconnect(pespconn);
  132. }
  133. else
  134. {
  135. at_response_error();
  136. }
  137. }
  138. /******************************************************************************
  139. * FunctionName : user_esp_platform_sent_cb
  140. * Description : Data has been sent successfully and acknowledged by the remote host.
  141. * Parameters : arg -- Additional argument to pass to the callback function
  142. * Returns : none
  143. *******************************************************************************/
  144. LOCAL void ICACHE_FLASH_ATTR
  145. at_upDate_sent_cb(void *arg)
  146. {
  147. struct espconn *pespconn = arg;
  148. os_timer_disarm(&at_delay_check);
  149. os_timer_setfn(&at_delay_check, (os_timer_func_t *)at_upDate_wait, pespconn);
  150. os_timer_arm(&at_delay_check, 5000, 0);
  151. os_printf("at_upDate_sent_cb\r\n");
  152. }
  153. /**
  154. * @brief Tcp client connect success callback function.
  155. * @param arg: contain the ip link information
  156. * @retval None
  157. */
  158. static void ICACHE_FLASH_ATTR
  159. at_upDate_connect_cb(void *arg)
  160. {
  161. struct espconn *pespconn = (struct espconn *)arg;
  162. uint8_t user_bin[9] = {0};
  163. char *temp = NULL;
  164. at_port_print("+CIPUPDATE:2\r\n");
  165. espconn_regist_disconcb(pespconn, at_upDate_discon_cb);
  166. espconn_regist_recvcb(pespconn, at_upDate_recv);////////
  167. espconn_regist_sentcb(pespconn, at_upDate_sent_cb);
  168. temp = (uint8 *) os_zalloc(512);
  169. os_sprintf(temp,"GET /v1/device/rom/?is_format_simple=true HTTP/1.0\r\nHost: "IPSTR"\r\n"pheadbuffer"",
  170. IP2STR(pespconn->proto.tcp->remote_ip));
  171. espconn_sent(pespconn, temp, os_strlen(temp));
  172. os_free(temp);
  173. }
  174. /**
  175. * @brief Tcp client connect repeat callback function.
  176. * @param arg: contain the ip link information
  177. * @retval None
  178. */
  179. static void ICACHE_FLASH_ATTR
  180. at_upDate_recon_cb(void *arg, sint8 errType)
  181. {
  182. struct espconn *pespconn = (struct espconn *)arg;
  183. at_response_error();
  184. if(pespconn->proto.tcp != NULL)
  185. {
  186. os_free(pespconn->proto.tcp);
  187. }
  188. os_free(pespconn);
  189. os_printf("disconnect\r\n");
  190. if(upServer != NULL)
  191. {
  192. os_free(upServer);
  193. upServer = NULL;
  194. }
  195. at_response_error();
  196. }
  197. /******************************************************************************
  198. * FunctionName : upServer_dns_found
  199. * Description : dns found callback
  200. * Parameters : name -- pointer to the name that was looked up.
  201. * ipaddr -- pointer to an ip_addr_t containing the IP address of
  202. * the hostname, or NULL if the name could not be found (or on any
  203. * other error).
  204. * callback_arg -- a user-specified callback argument passed to
  205. * dns_gethostbyname
  206. * Returns : none
  207. *******************************************************************************/
  208. LOCAL void ICACHE_FLASH_ATTR
  209. upServer_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
  210. {
  211. struct espconn *pespconn = (struct espconn *) arg;
  212. // char temp[32];
  213. if(ipaddr == NULL)
  214. {
  215. at_response_error();
  216. return;
  217. }
  218. at_port_print("+CIPUPDATE:1\r\n");
  219. if(host_ip.addr == 0 && ipaddr->addr != 0)
  220. {
  221. if(pespconn->type == ESPCONN_TCP)
  222. {
  223. os_memcpy(pespconn->proto.tcp->remote_ip, &ipaddr->addr, 4);
  224. espconn_regist_connectcb(pespconn, at_upDate_connect_cb);
  225. espconn_regist_reconcb(pespconn, at_upDate_recon_cb);
  226. espconn_connect(pespconn);
  227. }
  228. }
  229. }
  230. void ICACHE_FLASH_ATTR
  231. at_exeCmdCiupdate(uint8_t id)
  232. {
  233. pespconn = (struct espconn *)os_zalloc(sizeof(struct espconn));
  234. pespconn->type = ESPCONN_TCP;
  235. pespconn->state = ESPCONN_NONE;
  236. pespconn->proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp));
  237. pespconn->proto.tcp->local_port = espconn_port();
  238. pespconn->proto.tcp->remote_port = 80;
  239. host_ip.addr = ipaddr_addr("192.168.10.9");
  240. at_port_print("+CIPUPDATE:1\r\n");
  241. os_memcpy(pespconn->proto.tcp->remote_ip, &host_ip.addr, 4);
  242. espconn_regist_connectcb(pespconn, at_upDate_connect_cb);
  243. espconn_regist_reconcb(pespconn, at_upDate_recon_cb);
  244. espconn_connect(pespconn);
  245. }
  246. #endif
  247. #endif