user_esp_platform.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  1. /******************************************************************************
  2. * Copyright 2013-2014 Espressif Systems (Wuxi)
  3. *
  4. * FileName: user_esp_platform.c
  5. *
  6. * Description: The client mode configration.
  7. * Check your hardware connection with the host while use this mode.
  8. *
  9. * Modification history:
  10. * 2014/5/09, v1.0 create this file.
  11. *******************************************************************************/
  12. #include "ets_sys.h"
  13. #include "os_type.h"
  14. #include "mem.h"
  15. #include "osapi.h"
  16. #include "user_interface.h"
  17. #include "espconn.h"
  18. #include "user_esp_platform.h"
  19. #include "user_iot_version.h"
  20. #include "upgrade.h"
  21. #if ESP_PLATFORM
  22. #define ESP_DEBUG
  23. #ifdef ESP_DEBUG
  24. #define ESP_DBG os_printf
  25. #else
  26. #define ESP_DBG
  27. #endif
  28. #define ACTIVE_FRAME "{\"nonce\": %d,\"path\": \"/v1/device/activate/\", \"method\": \"POST\", \"body\": {\"encrypt_method\": \"PLAIN\", \"token\": \"%s\", \"bssid\": \""MACSTR"\",\"rom_version\":\"%s\"}, \"meta\": {\"Authorization\": \"token %s\"}}\n"
  29. #if PLUG_DEVICE
  30. #include "user_plug.h"
  31. #define RESPONSE_FRAME "{\"status\": 200, \"datapoint\": {\"x\": %d}, \"nonce\": %d, \"deliver_to_device\": true}\n"
  32. #define FIRST_FRAME "{\"nonce\": %d, \"path\": \"/v1/device/identify\", \"method\": \"GET\",\"meta\": {\"Authorization\": \"token %s\"}}\n"
  33. #elif LIGHT_DEVICE
  34. #include "user_light.h"
  35. #define RESPONSE_FRAME "{\"status\": 200,\"nonce\": %d, \"datapoint\": {\"x\": %d,\"y\": %d,\"z\": %d,\"k\": %d,\"l\": %d},\"deliver_to_device\":true}\n"
  36. #define FIRST_FRAME "{\"nonce\": %d, \"path\": \"/v1/device/identify\", \"method\": \"GET\",\"meta\": {\"Authorization\": \"token %s\"}}\n"
  37. #elif SENSOR_DEVICE
  38. #include "user_sensor.h"
  39. #if HUMITURE_SUB_DEVICE
  40. #define UPLOAD_FRAME "{\"nonce\": %d, \"path\": \"/v1/datastreams/tem_hum/datapoint/\", \"method\": \"POST\", \
  41. \"body\": {\"datapoint\": {\"x\": %s%d.%02d,\"y\": %d.%02d}}, \"meta\": {\"Authorization\": \"token %s\"}}\n"
  42. #elif FLAMMABLE_GAS_SUB_DEVICE
  43. #define UPLOAD_FRAME "{\"nonce\": %d, \"path\": \"/v1/datastreams/flammable_gas/datapoint/\", \"method\": \"POST\", \
  44. \"body\": {\"datapoint\": {\"x\": %d.%03d}}, \"meta\": {\"Authorization\": \"token %s\"}}\n"
  45. #endif
  46. LOCAL uint32 count = 0;
  47. #endif
  48. #define UPGRADE_FRAME "{\"path\": \"/v1/messages/\", \"method\": \"POST\", \"meta\": {\"Authorization\": \"token %s\"},\
  49. \"get\":{\"action\":\"%s\"},\"body\":{\"pre_rom_version\":\"%s\",\"rom_version\":\"%s\"}}\n"
  50. #if PLUG_DEVICE || LIGHT_DEVICE
  51. #define BEACON_FRAME "{\"path\": \"/v1/ping/\", \"method\": \"POST\",\"meta\": {\"Authorization\": \"token %s\"}}\n"
  52. #define RPC_RESPONSE_FRAME "{\"status\": 200, \"nonce\": %d, \"deliver_to_device\": true}\n"
  53. #define TIMER_FRAME "{\"body\": {}, \"get\":{\"is_humanize_format_simple\":\"true\"},\"meta\": {\"Authorization\": \"Token %s\"},\"path\": \"/v1/device/timers/\",\"post\":{},\"method\": \"GET\"}\n"
  54. #define pheadbuffer "Connection: keep-alive\r\n\
  55. Cache-Control: no-cache\r\n\
  56. 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\
  57. Accept: */*\r\n\
  58. Authorization: token %s\r\n\
  59. Accept-Encoding: gzip,deflate,sdch\r\n\
  60. Accept-Language: zh-CN,zh;q=0.8\r\n\r\n"
  61. LOCAL uint8 ping_status;
  62. LOCAL os_timer_t beacon_timer;
  63. #endif
  64. #ifdef USE_DNS
  65. ip_addr_t esp_server_ip;
  66. #endif
  67. LOCAL struct espconn user_conn;
  68. LOCAL struct _esp_tcp user_tcp;
  69. LOCAL os_timer_t client_timer;
  70. struct esp_platform_saved_param esp_param;
  71. LOCAL uint8 device_status;
  72. LOCAL uint8 device_recon_count = 0;
  73. LOCAL uint32 active_nonce = 0;
  74. LOCAL uint8 iot_version[20] = {0};
  75. struct rst_info rtc_info;
  76. void user_esp_platform_check_ip(uint8 reset_flag);
  77. /******************************************************************************
  78. * FunctionName : user_esp_platform_get_token
  79. * Description : get the espressif's device token
  80. * Parameters : token -- the parame point which write the flash
  81. * Returns : none
  82. *******************************************************************************/
  83. void ICACHE_FLASH_ATTR
  84. user_esp_platform_get_token(uint8_t *token)
  85. {
  86. if (token == NULL) {
  87. return;
  88. }
  89. os_memcpy(token, esp_param.token, sizeof(esp_param.token));
  90. }
  91. /******************************************************************************
  92. * FunctionName : user_esp_platform_set_token
  93. * Description : save the token for the espressif's device
  94. * Parameters : token -- the parame point which write the flash
  95. * Returns : none
  96. *******************************************************************************/
  97. void ICACHE_FLASH_ATTR
  98. user_esp_platform_set_token(uint8_t *token)
  99. {
  100. if (token == NULL) {
  101. return;
  102. }
  103. esp_param.activeflag = 0;
  104. os_memcpy(esp_param.token, token, os_strlen(token));
  105. system_param_save_with_protect(ESP_PARAM_START_SEC, &esp_param, sizeof(esp_param));
  106. }
  107. /******************************************************************************
  108. * FunctionName : user_esp_platform_set_active
  109. * Description : set active flag
  110. * Parameters : activeflag -- 0 or 1
  111. * Returns : none
  112. *******************************************************************************/
  113. void ICACHE_FLASH_ATTR
  114. user_esp_platform_set_active(uint8 activeflag)
  115. {
  116. esp_param.activeflag = activeflag;
  117. system_param_save_with_protect(ESP_PARAM_START_SEC, &esp_param, sizeof(esp_param));
  118. }
  119. void ICACHE_FLASH_ATTR
  120. user_esp_platform_set_connect_status(uint8 status)
  121. {
  122. device_status = status;
  123. }
  124. /******************************************************************************
  125. * FunctionName : user_esp_platform_get_connect_status
  126. * Description : get each connection step's status
  127. * Parameters : none
  128. * Returns : status
  129. *******************************************************************************/
  130. uint8 ICACHE_FLASH_ATTR
  131. user_esp_platform_get_connect_status(void)
  132. {
  133. uint8 status = wifi_station_get_connect_status();
  134. if (status == STATION_GOT_IP) {
  135. status = (device_status == 0) ? DEVICE_CONNECTING : device_status;
  136. }
  137. ESP_DBG("status %d\n", status);
  138. return status;
  139. }
  140. /******************************************************************************
  141. * FunctionName : user_esp_platform_parse_nonce
  142. * Description : parse the device nonce
  143. * Parameters : pbuffer -- the recivce data point
  144. * Returns : the nonce
  145. *******************************************************************************/
  146. int ICACHE_FLASH_ATTR
  147. user_esp_platform_parse_nonce(char *pbuffer)
  148. {
  149. char *pstr = NULL;
  150. char *pparse = NULL;
  151. char noncestr[11] = {0};
  152. int nonce = 0;
  153. pstr = (char *)os_strstr(pbuffer, "\"nonce\": ");
  154. if (pstr != NULL) {
  155. pstr += 9;
  156. pparse = (char *)os_strstr(pstr, ",");
  157. if (pparse != NULL) {
  158. os_memcpy(noncestr, pstr, pparse - pstr);
  159. } else {
  160. pparse = (char *)os_strstr(pstr, "}");
  161. if (pparse != NULL) {
  162. os_memcpy(noncestr, pstr, pparse - pstr);
  163. } else {
  164. pparse = (char *)os_strstr(pstr, "]");
  165. if (pparse != NULL) {
  166. os_memcpy(noncestr, pstr, pparse - pstr);
  167. } else {
  168. return 0;
  169. }
  170. }
  171. }
  172. nonce = atoi(noncestr);
  173. }
  174. return nonce;
  175. }
  176. /******************************************************************************
  177. * FunctionName : user_esp_platform_get_info
  178. * Description : get and update the espressif's device status
  179. * Parameters : pespconn -- the espconn used to connect with host
  180. * pbuffer -- prossing the data point
  181. * Returns : none
  182. *******************************************************************************/
  183. void ICACHE_FLASH_ATTR
  184. user_esp_platform_get_info(struct espconn *pconn, uint8 *pbuffer)
  185. {
  186. char *pbuf = NULL;
  187. int nonce = 0;
  188. pbuf = (char *)os_zalloc(packet_size);
  189. nonce = user_esp_platform_parse_nonce(pbuffer);
  190. if (pbuf != NULL) {
  191. #if PLUG_DEVICE
  192. os_sprintf(pbuf, RESPONSE_FRAME, user_plug_get_status(), nonce);
  193. #elif LIGHT_DEVICE
  194. uint32 white_val;
  195. white_val = (PWM_CHANNEL>LIGHT_COLD_WHITE?user_light_get_duty(LIGHT_COLD_WHITE):0);
  196. os_sprintf(pbuf, RESPONSE_FRAME, nonce, user_light_get_period(),
  197. user_light_get_duty(LIGHT_RED), user_light_get_duty(LIGHT_GREEN),
  198. user_light_get_duty(LIGHT_BLUE),white_val );//50);
  199. #endif
  200. ESP_DBG("%s\n", pbuf);
  201. #ifdef CLIENT_SSL_ENABLE
  202. espconn_secure_sent(pconn, pbuf, os_strlen(pbuf));
  203. #else
  204. espconn_sent(pconn, pbuf, os_strlen(pbuf));
  205. #endif
  206. os_free(pbuf);
  207. pbuf = NULL;
  208. }
  209. }
  210. /******************************************************************************
  211. * FunctionName : user_esp_platform_set_info
  212. * Description : prossing the data and controling the espressif's device
  213. * Parameters : pespconn -- the espconn used to connect with host
  214. * pbuffer -- prossing the data point
  215. * Returns : none
  216. *******************************************************************************/
  217. void ICACHE_FLASH_ATTR
  218. user_esp_platform_set_info(struct espconn *pconn, uint8 *pbuffer)
  219. {
  220. #if PLUG_DEVICE
  221. char *pstr = NULL;
  222. pstr = (char *)os_strstr(pbuffer, "plug-status");
  223. if (pstr != NULL) {
  224. pstr = (char *)os_strstr(pbuffer, "body");
  225. if (pstr != NULL) {
  226. if (os_strncmp(pstr + 27, "1", 1) == 0) {
  227. user_plug_set_status(0x01);
  228. } else if (os_strncmp(pstr + 27, "0", 1) == 0) {
  229. user_plug_set_status(0x00);
  230. }
  231. }
  232. }
  233. #elif LIGHT_DEVICE
  234. char *pstr = NULL;
  235. char *pdata = NULL;
  236. char *pbuf = NULL;
  237. char recvbuf[10];
  238. uint16 length = 0;
  239. uint32 data = 0;
  240. static uint32 rr,gg,bb,cw,ww,period;
  241. ww=0;
  242. cw=0;
  243. extern uint8 light_sleep_flg;
  244. pstr = (char *)os_strstr(pbuffer, "\"path\": \"/v1/datastreams/light/datapoint/\"");
  245. if (pstr != NULL) {
  246. pstr = (char *)os_strstr(pbuffer, "{\"datapoint\": ");
  247. if (pstr != NULL) {
  248. pbuf = (char *)os_strstr(pbuffer, "}}");
  249. length = pbuf - pstr;
  250. length += 2;
  251. pdata = (char *)os_zalloc(length + 1);
  252. os_memcpy(pdata, pstr, length);
  253. pstr = (char *)os_strchr(pdata, 'x');
  254. if (pstr != NULL) {
  255. pstr += 4;
  256. pbuf = (char *)os_strchr(pstr, ',');
  257. if (pbuf != NULL) {
  258. length = pbuf - pstr;
  259. os_memset(recvbuf, 0, 10);
  260. os_memcpy(recvbuf, pstr, length);
  261. data = atoi(recvbuf);
  262. period = data;
  263. //user_light_set_period(data);
  264. }
  265. }
  266. pstr = (char *)os_strchr(pdata, 'y');
  267. if (pstr != NULL) {
  268. pstr += 4;
  269. pbuf = (char *)os_strchr(pstr, ',');
  270. if (pbuf != NULL) {
  271. length = pbuf - pstr;
  272. os_memset(recvbuf, 0, 10);
  273. os_memcpy(recvbuf, pstr, length);
  274. data = atoi(recvbuf);
  275. rr=data;
  276. os_printf("r: %d\r\n",rr);
  277. //user_light_set_duty(data, 0);
  278. }
  279. }
  280. pstr = (char *)os_strchr(pdata, 'z');
  281. if (pstr != NULL) {
  282. pstr += 4;
  283. pbuf = (char *)os_strchr(pstr, ',');
  284. if (pbuf != NULL) {
  285. length = pbuf - pstr;
  286. os_memset(recvbuf, 0, 10);
  287. os_memcpy(recvbuf, pstr, length);
  288. data = atoi(recvbuf);
  289. gg=data;
  290. os_printf("g: %d\r\n",gg);
  291. //user_light_set_duty(data, 1);
  292. }
  293. }
  294. pstr = (char *)os_strchr(pdata, 'k');
  295. if (pstr != NULL) {
  296. pstr += 4;;
  297. pbuf = (char *)os_strchr(pstr, ',');
  298. if (pbuf != NULL) {
  299. length = pbuf - pstr;
  300. os_memset(recvbuf, 0, 10);
  301. os_memcpy(recvbuf, pstr, length);
  302. data = atoi(recvbuf);
  303. bb=data;
  304. os_printf("b: %d\r\n",bb);
  305. //user_light_set_duty(data, 2);
  306. }
  307. }
  308. pstr = (char *)os_strchr(pdata, 'l');
  309. if (pstr != NULL) {
  310. pstr += 4;;
  311. pbuf = (char *)os_strchr(pstr, ',');
  312. if (pbuf != NULL) {
  313. length = pbuf - pstr;
  314. os_memset(recvbuf, 0, 10);
  315. os_memcpy(recvbuf, pstr, length);
  316. data = atoi(recvbuf);
  317. cw=data;
  318. ww=data;
  319. os_printf("cw: %d\r\n",cw);
  320. os_printf("ww:%d\r\n",ww); //chg
  321. //user_light_set_duty(data, 2);
  322. }
  323. }
  324. os_free(pdata);
  325. }
  326. }
  327. if((rr|gg|bb|cw|ww) == 0){
  328. if(light_sleep_flg==0){
  329. }
  330. }else{
  331. if(light_sleep_flg==1){
  332. os_printf("modem sleep en\r\n");
  333. wifi_set_sleep_type(MODEM_SLEEP_T);
  334. light_sleep_flg =0;
  335. }
  336. }
  337. light_set_aim(rr,gg,bb,cw,ww,period);
  338. //user_light_restart();
  339. #endif
  340. user_esp_platform_get_info(pconn, pbuffer);
  341. }
  342. /******************************************************************************
  343. * FunctionName : user_esp_platform_reconnect
  344. * Description : reconnect with host after get ip
  345. * Parameters : pespconn -- the espconn used to reconnect with host
  346. * Returns : none
  347. *******************************************************************************/
  348. LOCAL void ICACHE_FLASH_ATTR
  349. user_esp_platform_reconnect(struct espconn *pespconn)
  350. {
  351. ESP_DBG("user_esp_platform_reconnect\n");
  352. user_esp_platform_check_ip(0);
  353. }
  354. /******************************************************************************
  355. * FunctionName : user_esp_platform_discon_cb
  356. * Description : disconnect successfully with the host
  357. * Parameters : arg -- Additional argument to pass to the callback function
  358. * Returns : none
  359. *******************************************************************************/
  360. LOCAL void ICACHE_FLASH_ATTR
  361. user_esp_platform_discon_cb(void *arg)
  362. {
  363. struct espconn *pespconn = arg;
  364. struct ip_info ipconfig;
  365. struct dhcp_client_info dhcp_info;
  366. ESP_DBG("user_esp_platform_discon_cb\n");
  367. #if (PLUG_DEVICE || LIGHT_DEVICE)
  368. os_timer_disarm(&beacon_timer);
  369. #endif
  370. if (pespconn == NULL) {
  371. return;
  372. }
  373. pespconn->proto.tcp->local_port = espconn_port();
  374. #if (PLUG_DEVICE || SENSOR_DEVICE)
  375. user_link_led_output(1);
  376. #endif
  377. #if SENSOR_DEVICE
  378. #ifdef SENSOR_DEEP_SLEEP
  379. if (wifi_get_opmode() == STATION_MODE) {
  380. /***add by tzx for saving ip_info to avoid dhcp_client start****/
  381. wifi_get_ip_info(STATION_IF, &ipconfig);
  382. dhcp_info.ip_addr = ipconfig.ip;
  383. dhcp_info.netmask = ipconfig.netmask;
  384. dhcp_info.gw = ipconfig.gw ;
  385. dhcp_info.flag = 0x01;
  386. os_printf("dhcp_info.ip_addr = %d\n",dhcp_info.ip_addr);
  387. system_rtc_mem_write(64,&dhcp_info,sizeof(struct dhcp_client_info));
  388. user_sensor_deep_sleep_enter();
  389. } else {
  390. os_timer_disarm(&client_timer);
  391. os_timer_setfn(&client_timer, (os_timer_func_t *)user_esp_platform_reconnect, pespconn);
  392. os_timer_arm(&client_timer, SENSOR_DEEP_SLEEP_TIME / 1000, 0);
  393. }
  394. #else
  395. os_timer_disarm(&client_timer);
  396. os_timer_setfn(&client_timer, (os_timer_func_t *)user_esp_platform_reconnect, pespconn);
  397. os_timer_arm(&client_timer, 1000, 0);
  398. #endif
  399. #else
  400. user_esp_platform_reconnect(pespconn);
  401. #endif
  402. }
  403. /******************************************************************************
  404. * FunctionName : user_esp_platform_discon
  405. * Description : A new incoming connection has been disconnected.
  406. * Parameters : espconn -- the espconn used to disconnect with host
  407. * Returns : none
  408. *******************************************************************************/
  409. LOCAL void ICACHE_FLASH_ATTR
  410. user_esp_platform_discon(struct espconn *pespconn)
  411. {
  412. ESP_DBG("user_esp_platform_discon\n");
  413. #if (PLUG_DEVICE || SENSOR_DEVICE)
  414. user_link_led_output(1);
  415. #endif
  416. #ifdef CLIENT_SSL_ENABLE
  417. espconn_secure_disconnect(pespconn);
  418. #else
  419. espconn_disconnect(pespconn);
  420. #endif
  421. }
  422. /******************************************************************************
  423. * FunctionName : user_esp_platform_sent_cb
  424. * Description : Data has been sent successfully and acknowledged by the remote host.
  425. * Parameters : arg -- Additional argument to pass to the callback function
  426. * Returns : none
  427. *******************************************************************************/
  428. LOCAL void ICACHE_FLASH_ATTR
  429. user_esp_platform_sent_cb(void *arg)
  430. {
  431. struct espconn *pespconn = arg;
  432. ESP_DBG("user_esp_platform_sent_cb\n");
  433. }
  434. /******************************************************************************
  435. * FunctionName : user_esp_platform_sent
  436. * Description : Processing the application data and sending it to the host
  437. * Parameters : pespconn -- the espconn used to connetion with the host
  438. * Returns : none
  439. *******************************************************************************/
  440. LOCAL void ICACHE_FLASH_ATTR
  441. user_esp_platform_sent(struct espconn *pespconn)
  442. {
  443. uint8 devkey[token_size] = {0};
  444. uint32 nonce;
  445. char *pbuf = (char *)os_zalloc(packet_size);
  446. os_memcpy(devkey, esp_param.devkey, 40);
  447. if (esp_param.activeflag == 0xFF) {
  448. esp_param.activeflag = 0;
  449. }
  450. if (pbuf != NULL) {
  451. if (esp_param.activeflag == 0) {
  452. uint8 token[token_size] = {0};
  453. uint8 bssid[6];
  454. active_nonce = os_random() & 0x7FFFFFFF;
  455. os_memcpy(token, esp_param.token, 40);
  456. wifi_get_macaddr(STATION_IF, bssid);
  457. os_sprintf(pbuf, ACTIVE_FRAME, active_nonce, token, MAC2STR(bssid),iot_version, devkey);
  458. }
  459. #if SENSOR_DEVICE
  460. #if HUMITURE_SUB_DEVICE
  461. else {
  462. #if 0
  463. uint16 tp, rh;
  464. uint8 data[4];
  465. if (user_mvh3004_read_th(data)) {
  466. rh = data[0] << 8 | data[1];
  467. tp = data[2] << 8 | data[3];
  468. }
  469. #else
  470. uint16 tp, rh;
  471. uint8 *data;
  472. uint32 tp_t, rh_t;
  473. data = (uint8 *)user_mvh3004_get_poweron_th();
  474. rh = data[0] << 8 | data[1];
  475. tp = data[2] << 8 | data[3];
  476. #endif
  477. tp_t = (tp >> 2) * 165 * 100 / (16384 - 1);
  478. rh_t = (rh & 0x3fff) * 100 * 100 / (16384 - 1);
  479. if (tp_t >= 4000) {
  480. os_sprintf(pbuf, UPLOAD_FRAME, count, "", tp_t / 100 - 40, tp_t % 100, rh_t / 100, rh_t % 100, devkey);
  481. } else {
  482. tp_t = 4000 - tp_t;
  483. os_sprintf(pbuf, UPLOAD_FRAME, count, "-", tp_t / 100, tp_t % 100, rh_t / 100, rh_t % 100, devkey);
  484. }
  485. }
  486. #elif FLAMMABLE_GAS_SUB_DEVICE
  487. else {
  488. uint32 adc_value = system_adc_read();
  489. os_sprintf(pbuf, UPLOAD_FRAME, count, adc_value / 1024, adc_value * 1000 / 1024, devkey);
  490. }
  491. #endif
  492. #else
  493. else {
  494. nonce = os_random() & 0x7FFFFFFF;
  495. os_sprintf(pbuf, FIRST_FRAME, nonce , devkey);
  496. }
  497. #endif
  498. ESP_DBG("%s\n", pbuf);
  499. #ifdef CLIENT_SSL_ENABLE
  500. espconn_secure_sent(pespconn, pbuf, os_strlen(pbuf));
  501. #else
  502. espconn_sent(pespconn, pbuf, os_strlen(pbuf));
  503. #endif
  504. os_free(pbuf);
  505. }
  506. }
  507. #if PLUG_DEVICE || LIGHT_DEVICE
  508. /******************************************************************************
  509. * FunctionName : user_esp_platform_sent_beacon
  510. * Description : sent beacon frame for connection with the host is activate
  511. * Parameters : pespconn -- the espconn used to connetion with the host
  512. * Returns : none
  513. *******************************************************************************/
  514. LOCAL void ICACHE_FLASH_ATTR
  515. user_esp_platform_sent_beacon(struct espconn *pespconn)
  516. {
  517. if (pespconn == NULL) {
  518. return;
  519. }
  520. if (pespconn->state == ESPCONN_CONNECT) {
  521. if (esp_param.activeflag == 0) {
  522. ESP_DBG("please check device is activated.\n");
  523. user_esp_platform_sent(pespconn);
  524. } else {
  525. uint8 devkey[token_size] = {0};
  526. os_memcpy(devkey, esp_param.devkey, 40);
  527. ESP_DBG("user_esp_platform_sent_beacon %u\n", system_get_time());
  528. if (ping_status == 0) {
  529. ESP_DBG("user_esp_platform_sent_beacon sent fail!\n");
  530. user_esp_platform_discon(pespconn);
  531. } else {
  532. char *pbuf = (char *)os_zalloc(packet_size);
  533. if (pbuf != NULL) {
  534. os_sprintf(pbuf, BEACON_FRAME, devkey);
  535. #ifdef CLIENT_SSL_ENABLE
  536. espconn_secure_sent(pespconn, pbuf, os_strlen(pbuf));
  537. #else
  538. espconn_sent(pespconn, pbuf, os_strlen(pbuf));
  539. #endif
  540. ping_status = 0;
  541. os_timer_arm(&beacon_timer, BEACON_TIME, 0);
  542. os_free(pbuf);
  543. }
  544. }
  545. }
  546. } else {
  547. ESP_DBG("user_esp_platform_sent_beacon sent fail!\n");
  548. user_esp_platform_discon(pespconn);
  549. }
  550. }
  551. /******************************************************************************
  552. * FunctionName : user_platform_rpc_set_rsp
  553. * Description : response the message to server to show setting info is received
  554. * Parameters : pespconn -- the espconn used to connetion with the host
  555. * nonce -- mark the message received from server
  556. * Returns : none
  557. *******************************************************************************/
  558. LOCAL void ICACHE_FLASH_ATTR
  559. user_platform_rpc_set_rsp(struct espconn *pespconn, int nonce)
  560. {
  561. char *pbuf = (char *)os_zalloc(packet_size);
  562. if (pespconn == NULL) {
  563. return;
  564. }
  565. os_sprintf(pbuf, RPC_RESPONSE_FRAME, nonce);
  566. ESP_DBG("%s\n", pbuf);
  567. #ifdef CLIENT_SSL_ENABLE
  568. espconn_secure_sent(pespconn, pbuf, os_strlen(pbuf));
  569. #else
  570. espconn_sent(pespconn, pbuf, os_strlen(pbuf));
  571. #endif
  572. os_free(pbuf);
  573. }
  574. /******************************************************************************
  575. * FunctionName : user_platform_timer_get
  576. * Description : get the timers from server
  577. * Parameters : pespconn -- the espconn used to connetion with the host
  578. * Returns : none
  579. *******************************************************************************/
  580. LOCAL void ICACHE_FLASH_ATTR
  581. user_platform_timer_get(struct espconn *pespconn)
  582. {
  583. uint8 devkey[token_size] = {0};
  584. char *pbuf = (char *)os_zalloc(packet_size);
  585. os_memcpy(devkey, esp_param.devkey, 40);
  586. if (pespconn == NULL) {
  587. return;
  588. }
  589. os_sprintf(pbuf, TIMER_FRAME, devkey);
  590. ESP_DBG("%s\n", pbuf);
  591. #ifdef CLIENT_SSL_ENABLE
  592. espconn_secure_sent(pespconn, pbuf, os_strlen(pbuf));
  593. #else
  594. espconn_sent(pespconn, pbuf, os_strlen(pbuf));
  595. #endif
  596. os_free(pbuf);
  597. }
  598. /******************************************************************************
  599. * FunctionName : user_esp_platform_upgrade_cb
  600. * Description : Processing the downloaded data from the server
  601. * Parameters : pespconn -- the espconn used to connetion with the host
  602. * Returns : none
  603. *******************************************************************************/
  604. LOCAL void ICACHE_FLASH_ATTR
  605. user_esp_platform_upgrade_rsp(void *arg)
  606. {
  607. struct upgrade_server_info *server = arg;
  608. struct espconn *pespconn = server->pespconn;
  609. uint8 devkey[41] = {0};
  610. uint8 *pbuf = NULL;
  611. char *action = NULL;
  612. os_memcpy(devkey, esp_param.devkey, 40);
  613. pbuf = (char *)os_zalloc(packet_size);
  614. if (server->upgrade_flag == true) {
  615. ESP_DBG("user_esp_platform_upgarde_successfully\n");
  616. action = "device_upgrade_success";
  617. os_sprintf(pbuf, UPGRADE_FRAME, devkey, action, server->pre_version, server->upgrade_version);
  618. ESP_DBG("%s\n",pbuf);
  619. #ifdef CLIENT_SSL_ENABLE
  620. espconn_secure_sent(pespconn, pbuf, os_strlen(pbuf));
  621. #else
  622. espconn_sent(pespconn, pbuf, os_strlen(pbuf));
  623. #endif
  624. if (pbuf != NULL) {
  625. os_free(pbuf);
  626. pbuf = NULL;
  627. }
  628. } else {
  629. ESP_DBG("user_esp_platform_upgrade_failed\n");
  630. action = "device_upgrade_failed";
  631. os_sprintf(pbuf, UPGRADE_FRAME, devkey, action,server->pre_version, server->upgrade_version);
  632. ESP_DBG("%s\n",pbuf);
  633. #ifdef CLIENT_SSL_ENABLE
  634. espconn_secure_sent(pespconn, pbuf, os_strlen(pbuf));
  635. #else
  636. espconn_sent(pespconn, pbuf, os_strlen(pbuf));
  637. #endif
  638. if (pbuf != NULL) {
  639. os_free(pbuf);
  640. pbuf = NULL;
  641. }
  642. }
  643. os_free(server->url);
  644. server->url = NULL;
  645. os_free(server);
  646. server = NULL;
  647. }
  648. /******************************************************************************
  649. * FunctionName : user_esp_platform_upgrade_begin
  650. * Description : Processing the received data from the server
  651. * Parameters : pespconn -- the espconn used to connetion with the host
  652. * server -- upgrade param
  653. * Returns : none
  654. *******************************************************************************/
  655. LOCAL void ICACHE_FLASH_ATTR
  656. user_esp_platform_upgrade_begin(struct espconn *pespconn, struct upgrade_server_info *server)
  657. {
  658. uint8 user_bin[9] = {0};
  659. uint8 devkey[41] = {0};
  660. server->pespconn = pespconn;
  661. os_memcpy(devkey, esp_param.devkey, 40);
  662. os_memcpy(server->ip, pespconn->proto.tcp->remote_ip, 4);
  663. #ifdef UPGRADE_SSL_ENABLE
  664. server->port = 443;
  665. #else
  666. server->port = 80;
  667. #endif
  668. server->check_cb = user_esp_platform_upgrade_rsp;
  669. server->check_times = 120000;
  670. if (server->url == NULL) {
  671. server->url = (uint8 *)os_zalloc(512);
  672. }
  673. if (system_upgrade_userbin_check() == UPGRADE_FW_BIN1) {
  674. os_memcpy(user_bin, "user2.bin", 10);
  675. } else if (system_upgrade_userbin_check() == UPGRADE_FW_BIN2) {
  676. os_memcpy(user_bin, "user1.bin", 10);
  677. }
  678. os_sprintf(server->url, "GET /v1/device/rom/?action=download_rom&version=%s&filename=%s HTTP/1.0\r\nHost: "IPSTR":%d\r\n"pheadbuffer"",
  679. server->upgrade_version, user_bin, IP2STR(server->ip),
  680. server->port, devkey);
  681. ESP_DBG("%s\n",server->url);
  682. #ifdef UPGRADE_SSL_ENABLE
  683. if (system_upgrade_start_ssl(server) == false) {
  684. #else
  685. if (system_upgrade_start(server) == false) {
  686. #endif
  687. ESP_DBG("upgrade is already started\n");
  688. }
  689. }
  690. #endif
  691. /******************************************************************************
  692. * FunctionName : user_esp_platform_recv_cb
  693. * Description : Processing the received data from the server
  694. * Parameters : arg -- Additional argument to pass to the callback function
  695. * pusrdata -- The received data (or NULL when the connection has been closed!)
  696. * length -- The length of received data
  697. * Returns : none
  698. *******************************************************************************/
  699. LOCAL void ICACHE_FLASH_ATTR
  700. user_esp_platform_recv_cb(void *arg, char *pusrdata, unsigned short length)
  701. {
  702. char *pstr = NULL;
  703. LOCAL char pbuffer[1024 * 2] = {0};
  704. struct espconn *pespconn = arg;
  705. ESP_DBG("user_esp_platform_recv_cb %s\n", pusrdata);
  706. #if (PLUG_DEVICE || LIGHT_DEVICE)
  707. os_timer_disarm(&beacon_timer);
  708. #endif
  709. if (length == 1460) {
  710. os_memcpy(pbuffer, pusrdata, length);
  711. } else {
  712. struct espconn *pespconn = (struct espconn *)arg;
  713. os_memcpy(pbuffer + os_strlen(pbuffer), pusrdata, length);
  714. if ((pstr = (char *)os_strstr(pbuffer, "\"activate_status\": ")) != NULL &&
  715. user_esp_platform_parse_nonce(pbuffer) == active_nonce) {
  716. if (os_strncmp(pstr + 19, "1", 1) == 0) {
  717. ESP_DBG("device activates successful.\n");
  718. device_status = DEVICE_ACTIVE_DONE;
  719. esp_param.activeflag = 1;
  720. system_param_save_with_protect(ESP_PARAM_START_SEC, &esp_param, sizeof(esp_param));
  721. user_esp_platform_sent(pespconn);
  722. if(LIGHT_DEVICE){
  723. system_restart();
  724. }
  725. } else {
  726. ESP_DBG("device activates failed.\n");
  727. device_status = DEVICE_ACTIVE_FAIL;
  728. }
  729. }
  730. #if (PLUG_DEVICE || LIGHT_DEVICE)
  731. else if ((pstr = (char *)os_strstr(pbuffer, "\"action\": \"sys_upgrade\"")) != NULL) {
  732. if ((pstr = (char *)os_strstr(pbuffer, "\"version\":")) != NULL) {
  733. struct upgrade_server_info *server = NULL;
  734. int nonce = user_esp_platform_parse_nonce(pbuffer);
  735. user_platform_rpc_set_rsp(pespconn, nonce);
  736. server = (struct upgrade_server_info *)os_zalloc(sizeof(struct upgrade_server_info));
  737. os_memcpy(server->upgrade_version, pstr + 12, 16);
  738. server->upgrade_version[15] = '\0';
  739. os_sprintf(server->pre_version,"%s%d.%d.%dt%d(%s)",VERSION_TYPE,IOT_VERSION_MAJOR,\
  740. IOT_VERSION_MINOR,IOT_VERSION_REVISION,device_type,UPGRADE_FALG);
  741. user_esp_platform_upgrade_begin(pespconn, server);
  742. }
  743. } else if ((pstr = (char *)os_strstr(pbuffer, "\"action\": \"sys_reboot\"")) != NULL) {
  744. os_timer_disarm(&client_timer);
  745. os_timer_setfn(&client_timer, (os_timer_func_t *)system_upgrade_reboot, NULL);
  746. os_timer_arm(&client_timer, 1000, 0);
  747. } else if ((pstr = (char *)os_strstr(pbuffer, "/v1/device/timers/")) != NULL) {
  748. int nonce = user_esp_platform_parse_nonce(pbuffer);
  749. user_platform_rpc_set_rsp(pespconn, nonce);
  750. os_timer_disarm(&client_timer);
  751. os_timer_setfn(&client_timer, (os_timer_func_t *)user_platform_timer_get, pespconn);
  752. os_timer_arm(&client_timer, 2000, 0);
  753. } else if ((pstr = (char *)os_strstr(pbuffer, "\"method\": ")) != NULL) {
  754. if (os_strncmp(pstr + 11, "GET", 3) == 0) {
  755. user_esp_platform_get_info(pespconn, pbuffer);
  756. } else if (os_strncmp(pstr + 11, "POST", 4) == 0) {
  757. user_esp_platform_set_info(pespconn, pbuffer);
  758. }
  759. } else if ((pstr = (char *)os_strstr(pbuffer, "ping success")) != NULL) {
  760. ESP_DBG("ping success\n");
  761. ping_status = 1;
  762. } else if ((pstr = (char *)os_strstr(pbuffer, "send message success")) != NULL) {
  763. } else if ((pstr = (char *)os_strstr(pbuffer, "timers")) != NULL) {
  764. user_platform_timer_start(pusrdata , pespconn);
  765. }
  766. #elif SENSOR_DEVICE
  767. else if ((pstr = (char *)os_strstr(pbuffer, "\"status\":")) != NULL) {
  768. if (os_strncmp(pstr + 10, "200", 3) != 0) {
  769. ESP_DBG("message upload failed.\n");
  770. } else {
  771. count++;
  772. ESP_DBG("message upload sucessful.\n");
  773. }
  774. os_timer_disarm(&client_timer);
  775. os_timer_setfn(&client_timer, (os_timer_func_t *)user_esp_platform_discon, pespconn);
  776. os_timer_arm(&client_timer, 10, 0);
  777. }
  778. #endif
  779. else if ((pstr = (char *)os_strstr(pbuffer, "device")) != NULL) {
  780. #if PLUG_DEVICE || LIGHT_DEVICE
  781. user_platform_timer_get(pespconn);
  782. #elif SENSOR_DEVICE
  783. #endif
  784. }
  785. os_memset(pbuffer, 0, sizeof(pbuffer));
  786. }
  787. #if (PLUG_DEVICE || LIGHT_DEVICE)
  788. os_timer_arm(&beacon_timer, BEACON_TIME, 0);
  789. #endif
  790. }
  791. #if AP_CACHE
  792. /******************************************************************************
  793. * FunctionName : user_esp_platform_ap_change
  794. * Description : add the user interface for changing to next ap ID.
  795. * Parameters :
  796. * Returns : none
  797. *******************************************************************************/
  798. LOCAL void ICACHE_FLASH_ATTR
  799. user_esp_platform_ap_change(void)
  800. {
  801. uint8 current_id;
  802. uint8 i = 0;
  803. ESP_DBG("user_esp_platform_ap_is_changing\n");
  804. current_id = wifi_station_get_current_ap_id();
  805. ESP_DBG("current ap id =%d\n", current_id);
  806. if (current_id == AP_CACHE_NUMBER - 1) {
  807. i = 0;
  808. } else {
  809. i = current_id + 1;
  810. }
  811. while (wifi_station_ap_change(i) != true) {
  812. i++;
  813. if (i == AP_CACHE_NUMBER - 1) {
  814. i = 0;
  815. }
  816. }
  817. /* just need to re-check ip while change AP */
  818. device_recon_count = 0;
  819. os_timer_disarm(&client_timer);
  820. os_timer_setfn(&client_timer, (os_timer_func_t *)user_esp_platform_check_ip, NULL);
  821. os_timer_arm(&client_timer, 100, 0);
  822. }
  823. #endif
  824. LOCAL bool ICACHE_FLASH_ATTR
  825. user_esp_platform_reset_mode(void)
  826. {
  827. if (wifi_get_opmode() == STATION_MODE) {
  828. wifi_set_opmode(STATIONAP_MODE);
  829. }
  830. #if AP_CACHE
  831. /* delay 5s to change AP */
  832. os_timer_disarm(&client_timer);
  833. os_timer_setfn(&client_timer, (os_timer_func_t *)user_esp_platform_ap_change, NULL);
  834. os_timer_arm(&client_timer, 5000, 0);
  835. return true;
  836. #endif
  837. return false;
  838. }
  839. /******************************************************************************
  840. * FunctionName : user_esp_platform_recon_cb
  841. * Description : The connection had an error and is already deallocated.
  842. * Parameters : arg -- Additional argument to pass to the callback function
  843. * Returns : none
  844. *******************************************************************************/
  845. LOCAL void ICACHE_FLASH_ATTR
  846. user_esp_platform_recon_cb(void *arg, sint8 err)
  847. {
  848. struct espconn *pespconn = (struct espconn *)arg;
  849. ESP_DBG("user_esp_platform_recon_cb\n");
  850. #if (PLUG_DEVICE || LIGHT_DEVICE)
  851. os_timer_disarm(&beacon_timer);
  852. #endif
  853. #if (PLUG_DEVICE || SENSOR_DEVICE)
  854. user_link_led_output(1);
  855. #endif
  856. if (++device_recon_count == 5) {
  857. device_status = DEVICE_CONNECT_SERVER_FAIL;
  858. if (user_esp_platform_reset_mode()) {
  859. return;
  860. }
  861. }
  862. #if SENSOR_DEVICE
  863. #ifdef SENSOR_DEEP_SLEEP
  864. if (wifi_get_opmode() == STATION_MODE) {
  865. user_esp_platform_reset_mode();
  866. //user_sensor_deep_sleep_enter();
  867. } else {
  868. os_timer_disarm(&client_timer);
  869. os_timer_setfn(&client_timer, (os_timer_func_t *)user_esp_platform_reconnect, pespconn);
  870. os_timer_arm(&client_timer, 1000, 0);
  871. }
  872. #else
  873. os_timer_disarm(&client_timer);
  874. os_timer_setfn(&client_timer, (os_timer_func_t *)user_esp_platform_reconnect, pespconn);
  875. os_timer_arm(&client_timer, 1000, 0);
  876. #endif
  877. #else
  878. os_timer_disarm(&client_timer);
  879. os_timer_setfn(&client_timer, (os_timer_func_t *)user_esp_platform_reconnect, pespconn);
  880. os_timer_arm(&client_timer, 1000, 0);
  881. #endif
  882. }
  883. /******************************************************************************
  884. * FunctionName : user_esp_platform_connect_cb
  885. * Description : A new incoming connection has been connected.
  886. * Parameters : arg -- Additional argument to pass to the callback function
  887. * Returns : none
  888. *******************************************************************************/
  889. LOCAL void ICACHE_FLASH_ATTR
  890. user_esp_platform_connect_cb(void *arg)
  891. {
  892. struct espconn *pespconn = arg;
  893. ESP_DBG("user_esp_platform_connect_cb\n");
  894. if (wifi_get_opmode() == STATIONAP_MODE ) {
  895. wifi_set_opmode(STATION_MODE);
  896. }
  897. #if (PLUG_DEVICE || SENSOR_DEVICE)
  898. user_link_led_timer_done();
  899. #endif
  900. device_recon_count = 0;
  901. espconn_regist_recvcb(pespconn, user_esp_platform_recv_cb);
  902. espconn_regist_sentcb(pespconn, user_esp_platform_sent_cb);
  903. user_esp_platform_sent(pespconn);
  904. }
  905. /******************************************************************************
  906. * FunctionName : user_esp_platform_connect
  907. * Description : The function given as the connect with the host
  908. * Parameters : espconn -- the espconn used to connect the connection
  909. * Returns : none
  910. *******************************************************************************/
  911. LOCAL void ICACHE_FLASH_ATTR
  912. user_esp_platform_connect(struct espconn *pespconn)
  913. {
  914. ESP_DBG("user_esp_platform_connect\n");
  915. #ifdef CLIENT_SSL_ENABLE
  916. espconn_secure_connect(pespconn);
  917. #else
  918. espconn_connect(pespconn);
  919. #endif
  920. }
  921. #ifdef USE_DNS
  922. /******************************************************************************
  923. * FunctionName : user_esp_platform_dns_found
  924. * Description : dns found callback
  925. * Parameters : name -- pointer to the name that was looked up.
  926. * ipaddr -- pointer to an ip_addr_t containing the IP address of
  927. * the hostname, or NULL if the name could not be found (or on any
  928. * other error).
  929. * callback_arg -- a user-specified callback argument passed to
  930. * dns_gethostbyname
  931. * Returns : none
  932. *******************************************************************************/
  933. LOCAL void ICACHE_FLASH_ATTR
  934. user_esp_platform_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
  935. {
  936. struct espconn *pespconn = (struct espconn *)arg;
  937. if (ipaddr == NULL) {
  938. ESP_DBG("user_esp_platform_dns_found NULL\n");
  939. if (++device_recon_count == 5) {
  940. device_status = DEVICE_CONNECT_SERVER_FAIL;
  941. user_esp_platform_reset_mode();
  942. }
  943. return;
  944. }
  945. ESP_DBG("user_esp_platform_dns_found %d.%d.%d.%d\n",
  946. *((uint8 *)&ipaddr->addr), *((uint8 *)&ipaddr->addr + 1),
  947. *((uint8 *)&ipaddr->addr + 2), *((uint8 *)&ipaddr->addr + 3));
  948. if (esp_server_ip.addr == 0 && ipaddr->addr != 0) {
  949. os_timer_disarm(&client_timer);
  950. esp_server_ip.addr = ipaddr->addr;
  951. os_memcpy(pespconn->proto.tcp->remote_ip, &ipaddr->addr, 4);
  952. pespconn->proto.tcp->local_port = espconn_port();
  953. #ifdef CLIENT_SSL_ENABLE
  954. pespconn->proto.tcp->remote_port = 8443;
  955. #else
  956. pespconn->proto.tcp->remote_port = 8000;
  957. #endif
  958. #if (PLUG_DEVICE || LIGHT_DEVICE)
  959. ping_status = 1;
  960. #endif
  961. espconn_regist_connectcb(pespconn, user_esp_platform_connect_cb);
  962. espconn_regist_disconcb(pespconn, user_esp_platform_discon_cb);
  963. espconn_regist_reconcb(pespconn, user_esp_platform_recon_cb);
  964. user_esp_platform_connect(pespconn);
  965. }
  966. }
  967. /******************************************************************************
  968. * FunctionName : user_esp_platform_dns_check_cb
  969. * Description : 1s time callback to check dns found
  970. * Parameters : arg -- Additional argument to pass to the callback function
  971. * Returns : none
  972. *******************************************************************************/
  973. LOCAL void ICACHE_FLASH_ATTR
  974. user_esp_platform_dns_check_cb(void *arg)
  975. {
  976. struct espconn *pespconn = arg;
  977. ESP_DBG("user_esp_platform_dns_check_cb\n");
  978. espconn_gethostbyname(pespconn, ESP_DOMAIN, &esp_server_ip, user_esp_platform_dns_found);
  979. os_timer_arm(&client_timer, 1000, 0);
  980. }
  981. LOCAL void ICACHE_FLASH_ATTR
  982. user_esp_platform_start_dns(struct espconn *pespconn)
  983. {
  984. esp_server_ip.addr = 0;
  985. espconn_gethostbyname(pespconn, ESP_DOMAIN, &esp_server_ip, user_esp_platform_dns_found);
  986. os_timer_disarm(&client_timer);
  987. os_timer_setfn(&client_timer, (os_timer_func_t *)user_esp_platform_dns_check_cb, pespconn);
  988. os_timer_arm(&client_timer, 1000, 0);
  989. }
  990. #endif
  991. #if LIGHT_DEVICE
  992. void user_mdns_conf()
  993. {
  994. struct ip_info ipconfig;
  995. wifi_get_ip_info(STATION_IF, &ipconfig);
  996. struct mdns_info *info = (struct mdns_info *)os_zalloc(sizeof(struct mdns_info));
  997. info->host_name = "espressif_light_demo";
  998. info->ipAddr= ipconfig.ip.addr; //sation ip
  999. info->server_name = "espLight";
  1000. info->server_port = 80;
  1001. info->txt_data[0] = "version = 1.0.1";
  1002. espconn_mdns_init(info);
  1003. }
  1004. #endif
  1005. /******************************************************************************
  1006. * FunctionName : user_esp_platform_check_ip
  1007. * Description : espconn struct parame init when get ip addr
  1008. * Parameters : none
  1009. * Returns : none
  1010. *******************************************************************************/
  1011. void ICACHE_FLASH_ATTR
  1012. user_esp_platform_check_ip(uint8 reset_flag)
  1013. {
  1014. struct ip_info ipconfig;
  1015. os_timer_disarm(&client_timer);
  1016. wifi_get_ip_info(STATION_IF, &ipconfig);
  1017. if (wifi_station_get_connect_status() == STATION_GOT_IP && ipconfig.ip.addr != 0) {
  1018. #if (PLUG_DEVICE || SENSOR_DEVICE)
  1019. user_link_led_timer_init();
  1020. #endif
  1021. //***************************
  1022. #if LIGHT_DEVICE
  1023. user_mdns_conf();
  1024. #endif
  1025. //***************************
  1026. user_conn.proto.tcp = &user_tcp;
  1027. user_conn.type = ESPCONN_TCP;
  1028. user_conn.state = ESPCONN_NONE;
  1029. device_status = DEVICE_CONNECTING;
  1030. if (reset_flag) {
  1031. device_recon_count = 0;
  1032. }
  1033. #if (PLUG_DEVICE || LIGHT_DEVICE)
  1034. os_timer_disarm(&beacon_timer);
  1035. os_timer_setfn(&beacon_timer, (os_timer_func_t *)user_esp_platform_sent_beacon, &user_conn);
  1036. #endif
  1037. #ifdef USE_DNS
  1038. user_esp_platform_start_dns(&user_conn);
  1039. #else
  1040. const char esp_server_ip[4] = {114, 215, 177, 97};
  1041. os_memcpy(user_conn.proto.tcp->remote_ip, esp_server_ip, 4);
  1042. user_conn.proto.tcp->local_port = espconn_port();
  1043. #ifdef CLIENT_SSL_ENABLE
  1044. user_conn.proto.tcp->remote_port = 8443;
  1045. #else
  1046. user_conn.proto.tcp->remote_port = 8000;
  1047. #endif
  1048. espconn_regist_connectcb(&user_conn, user_esp_platform_connect_cb);
  1049. espconn_regist_reconcb(&user_conn, user_esp_platform_recon_cb);
  1050. user_esp_platform_connect(&user_conn);
  1051. #endif
  1052. } else {
  1053. /* if there are wrong while connecting to some AP, then reset mode */
  1054. if ((wifi_station_get_connect_status() == STATION_WRONG_PASSWORD ||
  1055. wifi_station_get_connect_status() == STATION_NO_AP_FOUND ||
  1056. wifi_station_get_connect_status() == STATION_CONNECT_FAIL)) {
  1057. user_esp_platform_reset_mode();
  1058. } else {
  1059. os_timer_setfn(&client_timer, (os_timer_func_t *)user_esp_platform_check_ip, NULL);
  1060. os_timer_arm(&client_timer, 100, 0);
  1061. }
  1062. }
  1063. }
  1064. /******************************************************************************
  1065. * FunctionName : user_esp_platform_init
  1066. * Description : device parame init based on espressif platform
  1067. * Parameters : none
  1068. * Returns : none
  1069. *******************************************************************************/
  1070. void ICACHE_FLASH_ATTR
  1071. user_esp_platform_init(void)
  1072. {
  1073. os_sprintf(iot_version,"%s%d.%d.%dt%d(%s)",VERSION_TYPE,IOT_VERSION_MAJOR,\
  1074. IOT_VERSION_MINOR,IOT_VERSION_REVISION,device_type,UPGRADE_FALG);
  1075. os_printf("IOT VERSION = %s\n",iot_version);
  1076. system_param_load(ESP_PARAM_START_SEC, 0, &esp_param, sizeof(esp_param));
  1077. struct rst_info *rtc_info = system_get_rst_info();
  1078. os_printf("reset reason: %x\n", rtc_info->reason);
  1079. if (rtc_info->reason == REASON_WDT_RST ||
  1080. rtc_info->reason == REASON_EXCEPTION_RST ||
  1081. rtc_info->reason == REASON_SOFT_WDT_RST) {
  1082. if (rtc_info->reason == REASON_EXCEPTION_RST) {
  1083. os_printf("Fatal exception (%d):\n", rtc_info->exccause);
  1084. }
  1085. os_printf("epc1=0x%08x, epc2=0x%08x, epc3=0x%08x, excvaddr=0x%08x, depc=0x%08x\n",
  1086. rtc_info->epc1, rtc_info->epc2, rtc_info->epc3, rtc_info->excvaddr, rtc_info->depc);
  1087. }
  1088. /***add by tzx for saving ip_info to avoid dhcp_client start****/
  1089. struct dhcp_client_info dhcp_info;
  1090. struct ip_info sta_info;
  1091. system_rtc_mem_read(64,&dhcp_info,sizeof(struct dhcp_client_info));
  1092. if(dhcp_info.flag == 0x01 ) {
  1093. if (true == wifi_station_dhcpc_status())
  1094. {
  1095. wifi_station_dhcpc_stop();
  1096. }
  1097. sta_info.ip = dhcp_info.ip_addr;
  1098. sta_info.gw = dhcp_info.gw;
  1099. sta_info.netmask = dhcp_info.netmask;
  1100. if ( true != wifi_set_ip_info(STATION_IF,&sta_info)) {
  1101. os_printf("set default ip wrong\n");
  1102. }
  1103. }
  1104. os_memset(&dhcp_info,0,sizeof(struct dhcp_client_info));
  1105. system_rtc_mem_write(64,&dhcp_info,sizeof(struct rst_info));
  1106. #if AP_CACHE
  1107. wifi_station_ap_number_set(AP_CACHE_NUMBER);
  1108. #endif
  1109. #if 0
  1110. {
  1111. char sofap_mac[6] = {0x16, 0x34, 0x56, 0x78, 0x90, 0xab};
  1112. char sta_mac[6] = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab};
  1113. struct ip_info info;
  1114. wifi_set_macaddr(SOFTAP_IF, sofap_mac);
  1115. wifi_set_macaddr(STATION_IF, sta_mac);
  1116. IP4_ADDR(&info.ip, 192, 168, 3, 200);
  1117. IP4_ADDR(&info.gw, 192, 168, 3, 1);
  1118. IP4_ADDR(&info.netmask, 255, 255, 255, 0);
  1119. wifi_set_ip_info(STATION_IF, &info);
  1120. IP4_ADDR(&info.ip, 10, 10, 10, 1);
  1121. IP4_ADDR(&info.gw, 10, 10, 10, 1);
  1122. IP4_ADDR(&info.netmask, 255, 255, 255, 0);
  1123. wifi_set_ip_info(SOFTAP_IF, &info);
  1124. }
  1125. #endif
  1126. if (esp_param.activeflag != 1) {
  1127. #ifdef SOFTAP_ENCRYPT
  1128. struct softap_config config;
  1129. char password[33];
  1130. char macaddr[6];
  1131. wifi_softap_get_config(&config);
  1132. wifi_get_macaddr(SOFTAP_IF, macaddr);
  1133. os_memset(config.password, 0, sizeof(config.password));
  1134. os_sprintf(password, MACSTR "_%s", MAC2STR(macaddr), PASSWORD);
  1135. os_memcpy(config.password, password, os_strlen(password));
  1136. config.authmode = AUTH_WPA_WPA2_PSK;
  1137. wifi_softap_set_config(&config);
  1138. #endif
  1139. wifi_set_opmode(STATIONAP_MODE);
  1140. }
  1141. #if PLUG_DEVICE
  1142. user_plug_init();
  1143. #elif LIGHT_DEVICE
  1144. user_light_init();
  1145. #elif SENSOR_DEVICE
  1146. user_sensor_init(esp_param.activeflag);
  1147. #endif
  1148. if (wifi_get_opmode() != SOFTAP_MODE) {
  1149. os_timer_disarm(&client_timer);
  1150. os_timer_setfn(&client_timer, (os_timer_func_t *)user_esp_platform_check_ip, 1);
  1151. os_timer_arm(&client_timer, 100, 0);
  1152. }
  1153. }
  1154. #endif