user_webserver.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790
  1. /******************************************************************************
  2. * Copyright 2013-2014 Espressif Systems (Wuxi)
  3. *
  4. * FileName: user_webserver.c
  5. *
  6. * Description: The web server mode configration.
  7. * Check your hardware connection with the host while use this mode.
  8. * Modification history:
  9. * 2014/3/12, v1.0 create this file.
  10. *******************************************************************************/
  11. #include "ets_sys.h"
  12. #include "os_type.h"
  13. #include "osapi.h"
  14. #include "mem.h"
  15. #include "user_interface.h"
  16. #include "user_iot_version.h"
  17. #include "espconn.h"
  18. #include "user_json.h"
  19. #include "user_webserver.h"
  20. #include "upgrade.h"
  21. #if ESP_PLATFORM
  22. #include "user_esp_platform.h"
  23. #endif
  24. #ifdef SERVER_SSL_ENABLE
  25. #include "ssl/cert.h"
  26. #include "ssl/private_key.h"
  27. #endif
  28. #if LIGHT_DEVICE
  29. #include "user_light.h"
  30. #endif
  31. LOCAL struct station_config *sta_conf;
  32. LOCAL struct softap_config *ap_conf;
  33. //LOCAL struct secrty_server_info *sec_server;
  34. //LOCAL struct upgrade_server_info *server;
  35. //struct lewei_login_info *login_info;
  36. LOCAL scaninfo *pscaninfo;
  37. struct bss_info *bss;
  38. struct bss_info *bss_temp;
  39. struct bss_info *bss_head;
  40. extern u16 scannum;
  41. LOCAL uint32 PostCmdNeeRsp = 1;
  42. uint8 upgrade_lock = 0;
  43. LOCAL os_timer_t app_upgrade_10s;
  44. LOCAL os_timer_t upgrade_check_timer;
  45. /******************************************************************************
  46. * FunctionName : device_get
  47. * Description : set up the device information parmer as a JSON format
  48. * Parameters : js_ctx -- A pointer to a JSON set up
  49. * Returns : result
  50. *******************************************************************************/
  51. LOCAL int ICACHE_FLASH_ATTR
  52. device_get(struct jsontree_context *js_ctx)
  53. {
  54. const char *path = jsontree_path_name(js_ctx, js_ctx->depth - 1);
  55. if (os_strncmp(path, "manufacture", 11) == 0) {
  56. jsontree_write_string(js_ctx, "Espressif Systems");
  57. } else if (os_strncmp(path, "product", 7) == 0) {
  58. #if SENSOR_DEVICE
  59. #if HUMITURE_SUB_DEVICE
  60. jsontree_write_string(js_ctx, "Humiture");
  61. #elif FLAMMABLE_GAS_SUB_DEVICE
  62. jsontree_write_string(js_ctx, "Flammable Gas");
  63. #endif
  64. #endif
  65. #if PLUG_DEVICE
  66. jsontree_write_string(js_ctx, "Plug");
  67. #endif
  68. #if LIGHT_DEVICE
  69. jsontree_write_string(js_ctx, "Light");
  70. #endif
  71. }
  72. return 0;
  73. }
  74. LOCAL struct jsontree_callback device_callback =
  75. JSONTREE_CALLBACK(device_get, NULL);
  76. /******************************************************************************
  77. * FunctionName : userbin_get
  78. * Description : get up the user bin paramer as a JSON format
  79. * Parameters : js_ctx -- A pointer to a JSON set up
  80. * Returns : result
  81. *******************************************************************************/
  82. LOCAL int ICACHE_FLASH_ATTR
  83. userbin_get(struct jsontree_context *js_ctx)
  84. {
  85. const char *path = jsontree_path_name(js_ctx, js_ctx->depth - 1);
  86. char string[32];
  87. if (os_strncmp(path, "status", 8) == 0) {
  88. os_sprintf(string, "200");
  89. } else if (os_strncmp(path, "user_bin", 8) == 0) {
  90. if (system_upgrade_userbin_check() == 0x00) {
  91. os_sprintf(string, "user1.bin");
  92. } else if (system_upgrade_userbin_check() == 0x01) {
  93. os_sprintf(string, "user2.bin");
  94. } else{
  95. return 0;
  96. }
  97. }
  98. jsontree_write_string(js_ctx, string);
  99. return 0;
  100. }
  101. LOCAL struct jsontree_callback userbin_callback =
  102. JSONTREE_CALLBACK(userbin_get, NULL);
  103. JSONTREE_OBJECT(userbin_tree,
  104. JSONTREE_PAIR("status", &userbin_callback),
  105. JSONTREE_PAIR("user_bin", &userbin_callback));
  106. JSONTREE_OBJECT(userinfo_tree,JSONTREE_PAIR("user_info",&userbin_tree));
  107. /******************************************************************************
  108. * FunctionName : version_get
  109. * Description : set up the device version paramer as a JSON format
  110. * Parameters : js_ctx -- A pointer to a JSON set up
  111. * Returns : result
  112. *******************************************************************************/
  113. LOCAL int ICACHE_FLASH_ATTR
  114. version_get(struct jsontree_context *js_ctx)
  115. {
  116. const char *path = jsontree_path_name(js_ctx, js_ctx->depth - 1);
  117. char string[32];
  118. if (os_strncmp(path, "hardware", 8) == 0) {
  119. #if SENSOR_DEVICE
  120. os_sprintf(string, "0.3");
  121. #else
  122. os_sprintf(string, "0.1");
  123. #endif
  124. } else if (os_strncmp(path, "sdk_version", 11) == 0) {
  125. os_sprintf(string, "%s", system_get_sdk_version());
  126. } else if (os_strncmp(path, "iot_version", 11) == 0) {
  127. os_sprintf(string,"%s%d.%d.%dt%d(%s)",VERSION_TYPE,IOT_VERSION_MAJOR,\
  128. IOT_VERSION_MINOR,IOT_VERSION_REVISION,device_type,UPGRADE_FALG);
  129. }
  130. jsontree_write_string(js_ctx, string);
  131. return 0;
  132. }
  133. LOCAL struct jsontree_callback version_callback =
  134. JSONTREE_CALLBACK(version_get, NULL);
  135. JSONTREE_OBJECT(device_tree,
  136. JSONTREE_PAIR("product", &device_callback),
  137. JSONTREE_PAIR("manufacturer", &device_callback));
  138. JSONTREE_OBJECT(version_tree,
  139. JSONTREE_PAIR("hardware", &version_callback),
  140. JSONTREE_PAIR("sdk_version", &version_callback),
  141. JSONTREE_PAIR("iot_version", &version_callback),
  142. );
  143. JSONTREE_OBJECT(info_tree,
  144. JSONTREE_PAIR("Version", &version_tree),
  145. JSONTREE_PAIR("Device", &device_tree));
  146. JSONTREE_OBJECT(INFOTree,
  147. JSONTREE_PAIR("info", &info_tree));
  148. LOCAL int ICACHE_FLASH_ATTR
  149. connect_status_get(struct jsontree_context *js_ctx)
  150. {
  151. const char *path = jsontree_path_name(js_ctx, js_ctx->depth - 1);
  152. if (os_strncmp(path, "status", 8) == 0) {
  153. jsontree_write_int(js_ctx, user_esp_platform_get_connect_status());
  154. }
  155. return 0;
  156. }
  157. LOCAL struct jsontree_callback connect_status_callback =
  158. JSONTREE_CALLBACK(connect_status_get, NULL);
  159. JSONTREE_OBJECT(status_sub_tree,
  160. JSONTREE_PAIR("status", &connect_status_callback));
  161. JSONTREE_OBJECT(connect_status_tree,
  162. JSONTREE_PAIR("Status", &status_sub_tree));
  163. JSONTREE_OBJECT(con_status_tree,
  164. JSONTREE_PAIR("info", &connect_status_tree));
  165. #if PLUG_DEVICE
  166. /******************************************************************************
  167. * FunctionName : status_get
  168. * Description : set up the device status as a JSON format
  169. * Parameters : js_ctx -- A pointer to a JSON set up
  170. * Returns : result
  171. *******************************************************************************/
  172. LOCAL int ICACHE_FLASH_ATTR
  173. status_get(struct jsontree_context *js_ctx)
  174. {
  175. if (user_plug_get_status() == 1) {
  176. jsontree_write_int(js_ctx, 1);
  177. } else {
  178. jsontree_write_int(js_ctx, 0);
  179. }
  180. return 0;
  181. }
  182. /******************************************************************************
  183. * FunctionName : status_set
  184. * Description : parse the device status parmer as a JSON format
  185. * Parameters : js_ctx -- A pointer to a JSON set up
  186. * parser -- A pointer to a JSON parser state
  187. * Returns : result
  188. *******************************************************************************/
  189. LOCAL int ICACHE_FLASH_ATTR
  190. status_set(struct jsontree_context *js_ctx, struct jsonparse_state *parser)
  191. {
  192. int type;
  193. while ((type = jsonparse_next(parser)) != 0) {
  194. if (type == JSON_TYPE_PAIR_NAME) {
  195. if (jsonparse_strcmp_value(parser, "status") == 0) {
  196. uint8 status;
  197. jsonparse_next(parser);
  198. jsonparse_next(parser);
  199. status = jsonparse_get_value_as_int(parser);
  200. user_plug_set_status(status);
  201. }
  202. }
  203. }
  204. return 0;
  205. }
  206. LOCAL struct jsontree_callback status_callback =
  207. JSONTREE_CALLBACK(status_get, status_set);
  208. JSONTREE_OBJECT(status_tree,
  209. JSONTREE_PAIR("status", &status_callback));
  210. JSONTREE_OBJECT(response_tree,
  211. JSONTREE_PAIR("Response", &status_tree));
  212. JSONTREE_OBJECT(StatusTree,
  213. JSONTREE_PAIR("switch", &response_tree));
  214. #endif
  215. #if LIGHT_DEVICE
  216. LOCAL int ICACHE_FLASH_ATTR
  217. light_status_get(struct jsontree_context *js_ctx)
  218. {
  219. const char *path = jsontree_path_name(js_ctx, js_ctx->depth - 1);
  220. if (os_strncmp(path, "red", 3) == 0) {
  221. jsontree_write_int(js_ctx, user_light_get_duty(LIGHT_RED));
  222. } else if (os_strncmp(path, "green", 5) == 0) {
  223. jsontree_write_int(js_ctx, user_light_get_duty(LIGHT_GREEN));
  224. } else if (os_strncmp(path, "blue", 4) == 0) {
  225. jsontree_write_int(js_ctx, user_light_get_duty(LIGHT_BLUE));
  226. } else if (os_strncmp(path, "wwhite", 6) == 0) {
  227. if(PWM_CHANNEL>LIGHT_WARM_WHITE){
  228. jsontree_write_int(js_ctx, user_light_get_duty(LIGHT_WARM_WHITE));
  229. }else{
  230. jsontree_write_int(js_ctx, 0);
  231. }
  232. } else if (os_strncmp(path, "cwhite", 6) == 0) {
  233. if(PWM_CHANNEL>LIGHT_COLD_WHITE){
  234. jsontree_write_int(js_ctx, user_light_get_duty(LIGHT_COLD_WHITE));
  235. }else{
  236. jsontree_write_int(js_ctx, 0);
  237. }
  238. } else if (os_strncmp(path, "period", 6) == 0) {
  239. jsontree_write_int(js_ctx, user_light_get_period());
  240. }
  241. return 0;
  242. }
  243. LOCAL int ICACHE_FLASH_ATTR
  244. light_status_set(struct jsontree_context *js_ctx, struct jsonparse_state *parser)
  245. {
  246. int type;
  247. static uint32 r,g,b,cw,ww,period;
  248. period = 1000;
  249. cw=0;
  250. ww=0;
  251. extern uint8 light_sleep_flg;
  252. while ((type = jsonparse_next(parser)) != 0) {
  253. if (type == JSON_TYPE_PAIR_NAME) {
  254. if (jsonparse_strcmp_value(parser, "red") == 0) {
  255. uint32 status;
  256. jsonparse_next(parser);
  257. jsonparse_next(parser);
  258. status = jsonparse_get_value_as_int(parser);
  259. r=status;
  260. os_printf("R: %d \n",status);
  261. //user_light_set_duty(status, LIGHT_RED);
  262. //light_set_aim_r( r);
  263. } else if (jsonparse_strcmp_value(parser, "green") == 0) {
  264. uint32 status;
  265. jsonparse_next(parser);
  266. jsonparse_next(parser);
  267. status = jsonparse_get_value_as_int(parser);
  268. g=status;
  269. os_printf("G: %d \n",status);
  270. //user_light_set_duty(status, LIGHT_GREEN);
  271. //light_set_aim_g( g);
  272. } else if (jsonparse_strcmp_value(parser, "blue") == 0) {
  273. uint32 status;
  274. jsonparse_next(parser);
  275. jsonparse_next(parser);
  276. status = jsonparse_get_value_as_int(parser);
  277. b=status;
  278. os_printf("B: %d \n",status);
  279. //user_light_set_duty(status, LIGHT_BLUE);
  280. //set_aim_b( b);
  281. } else if (jsonparse_strcmp_value(parser, "cwhite") == 0) {
  282. uint32 status;
  283. jsonparse_next(parser);
  284. jsonparse_next(parser);
  285. status = jsonparse_get_value_as_int(parser);
  286. cw=status;
  287. os_printf("CW: %d \n",status);
  288. //user_light_set_duty(status, LIGHT_BLUE);
  289. //set_aim_b( b);
  290. } else if (jsonparse_strcmp_value(parser, "wwhite") == 0) {
  291. uint32 status;
  292. jsonparse_next(parser);
  293. jsonparse_next(parser);
  294. status = jsonparse_get_value_as_int(parser);
  295. ww=status;
  296. os_printf("WW: %d \n",status);
  297. //user_light_set_duty(status, LIGHT_BLUE);
  298. //set_aim_b( b);
  299. } else if (jsonparse_strcmp_value(parser, "period") == 0) {
  300. uint32 status;
  301. jsonparse_next(parser);
  302. jsonparse_next(parser);
  303. status = jsonparse_get_value_as_int(parser);
  304. os_printf("PERIOD: %d \n",status);
  305. period=status;
  306. //user_light_set_period(status);
  307. }else if (jsonparse_strcmp_value(parser, "response") == 0) {
  308. uint32 status;
  309. jsonparse_next(parser);
  310. jsonparse_next(parser);
  311. status = jsonparse_get_value_as_int(parser);
  312. os_printf("rspneed: %d \n",status);
  313. PostCmdNeeRsp = status;
  314. }
  315. }
  316. }
  317. if((r|g|b|ww|cw) == 0){
  318. if(light_sleep_flg==0){
  319. }
  320. }else{
  321. if(light_sleep_flg==1){
  322. os_printf("modem sleep en\r\n");
  323. wifi_set_sleep_type(MODEM_SLEEP_T);
  324. light_sleep_flg =0;
  325. }
  326. }
  327. light_set_aim(r,g,b,cw,ww,period);
  328. return 0;
  329. }
  330. LOCAL struct jsontree_callback light_callback =
  331. JSONTREE_CALLBACK(light_status_get, light_status_set);
  332. JSONTREE_OBJECT(rgb_tree,
  333. JSONTREE_PAIR("red", &light_callback),
  334. JSONTREE_PAIR("green", &light_callback),
  335. JSONTREE_PAIR("blue", &light_callback),
  336. JSONTREE_PAIR("cwhite", &light_callback),
  337. JSONTREE_PAIR("wwhite", &light_callback),
  338. );
  339. JSONTREE_OBJECT(sta_tree,
  340. JSONTREE_PAIR("period", &light_callback),
  341. JSONTREE_PAIR("rgb", &rgb_tree));
  342. JSONTREE_OBJECT(PwmTree,
  343. JSONTREE_PAIR("light", &sta_tree));
  344. #endif
  345. /******************************************************************************
  346. * FunctionName : wifi_station_get
  347. * Description : set up the station paramer as a JSON format
  348. * Parameters : js_ctx -- A pointer to a JSON set up
  349. * Returns : result
  350. *******************************************************************************/
  351. LOCAL int ICACHE_FLASH_ATTR
  352. wifi_station_get(struct jsontree_context *js_ctx)
  353. {
  354. const char *path = jsontree_path_name(js_ctx, js_ctx->depth - 1);
  355. struct ip_info ipconfig;
  356. uint8 buf[20];
  357. os_bzero(buf, sizeof(buf));
  358. wifi_station_get_config(sta_conf);
  359. wifi_get_ip_info(STATION_IF, &ipconfig);
  360. if (os_strncmp(path, "ssid", 4) == 0) {
  361. jsontree_write_string(js_ctx, sta_conf->ssid);
  362. } else if (os_strncmp(path, "password", 8) == 0) {
  363. jsontree_write_string(js_ctx, sta_conf->password);
  364. } else if (os_strncmp(path, "ip", 2) == 0) {
  365. os_sprintf(buf, IPSTR, IP2STR(&ipconfig.ip));
  366. jsontree_write_string(js_ctx, buf);
  367. } else if (os_strncmp(path, "mask", 4) == 0) {
  368. os_sprintf(buf, IPSTR, IP2STR(&ipconfig.netmask));
  369. jsontree_write_string(js_ctx, buf);
  370. } else if (os_strncmp(path, "gw", 2) == 0) {
  371. os_sprintf(buf, IPSTR, IP2STR(&ipconfig.gw));
  372. jsontree_write_string(js_ctx, buf);
  373. }
  374. return 0;
  375. }
  376. /******************************************************************************
  377. * FunctionName : wifi_station_set
  378. * Description : parse the station parmer as a JSON format
  379. * Parameters : js_ctx -- A pointer to a JSON set up
  380. * parser -- A pointer to a JSON parser state
  381. * Returns : result
  382. *******************************************************************************/
  383. LOCAL int ICACHE_FLASH_ATTR
  384. wifi_station_set(struct jsontree_context *js_ctx, struct jsonparse_state *parser)
  385. {
  386. int type;
  387. uint8 station_tree;
  388. while ((type = jsonparse_next(parser)) != 0) {
  389. if (type == JSON_TYPE_PAIR_NAME) {
  390. char buffer[64];
  391. os_bzero(buffer, 64);
  392. if (jsonparse_strcmp_value(parser, "Station") == 0) {
  393. station_tree = 1;
  394. } else if (jsonparse_strcmp_value(parser, "Softap") == 0) {
  395. station_tree = 0;
  396. }
  397. if (station_tree) {
  398. if (jsonparse_strcmp_value(parser, "ssid") == 0) {
  399. jsonparse_next(parser);
  400. jsonparse_next(parser);
  401. jsonparse_copy_value(parser, buffer, sizeof(buffer));
  402. os_memcpy(sta_conf->ssid, buffer, os_strlen(buffer));
  403. } else if (jsonparse_strcmp_value(parser, "password") == 0) {
  404. jsonparse_next(parser);
  405. jsonparse_next(parser);
  406. jsonparse_copy_value(parser, buffer, sizeof(buffer));
  407. os_memcpy(sta_conf->password, buffer, os_strlen(buffer));
  408. }
  409. #if ESP_PLATFORM
  410. else if (jsonparse_strcmp_value(parser, "token") == 0) {
  411. jsonparse_next(parser);
  412. jsonparse_next(parser);
  413. jsonparse_copy_value(parser, buffer, sizeof(buffer));
  414. user_esp_platform_set_token(buffer);
  415. }
  416. #endif
  417. }
  418. }
  419. }
  420. return 0;
  421. }
  422. LOCAL struct jsontree_callback wifi_station_callback =
  423. JSONTREE_CALLBACK(wifi_station_get, wifi_station_set);
  424. JSONTREE_OBJECT(get_station_config_tree,
  425. JSONTREE_PAIR("ssid", &wifi_station_callback),
  426. JSONTREE_PAIR("password", &wifi_station_callback));
  427. JSONTREE_OBJECT(set_station_config_tree,
  428. JSONTREE_PAIR("ssid", &wifi_station_callback),
  429. JSONTREE_PAIR("password", &wifi_station_callback),
  430. JSONTREE_PAIR("token", &wifi_station_callback));
  431. JSONTREE_OBJECT(ip_tree,
  432. JSONTREE_PAIR("ip", &wifi_station_callback),
  433. JSONTREE_PAIR("mask", &wifi_station_callback),
  434. JSONTREE_PAIR("gw", &wifi_station_callback));
  435. JSONTREE_OBJECT(get_station_tree,
  436. JSONTREE_PAIR("Connect_Station", &get_station_config_tree),
  437. JSONTREE_PAIR("Ipinfo_Station", &ip_tree));
  438. JSONTREE_OBJECT(set_station_tree,
  439. JSONTREE_PAIR("Connect_Station", &set_station_config_tree));
  440. //JSONTREE_OBJECT(get_wifi_station_info_tree,
  441. // JSONTREE_PAIR("Station", &get_station_tree));
  442. //JSONTREE_OBJECT(set_wifi_station_info_tree,
  443. // JSONTREE_PAIR("station", &set_station_tree));
  444. /******************************************************************************
  445. * FunctionName : wifi_softap_get
  446. * Description : set up the softap paramer as a JSON format
  447. * Parameters : js_ctx -- A pointer to a JSON set up
  448. * Returns : result
  449. *******************************************************************************/
  450. LOCAL int ICACHE_FLASH_ATTR
  451. wifi_softap_get(struct jsontree_context *js_ctx)
  452. {
  453. const char *path = jsontree_path_name(js_ctx, js_ctx->depth - 1);
  454. struct ip_info ipconfig;
  455. uint8 buf[20];
  456. os_bzero(buf, sizeof(buf));
  457. wifi_softap_get_config(ap_conf);
  458. wifi_get_ip_info(SOFTAP_IF, &ipconfig);
  459. if (os_strncmp(path, "ssid", 4) == 0) {
  460. jsontree_write_string(js_ctx, ap_conf->ssid);
  461. } else if (os_strncmp(path, "password", 8) == 0) {
  462. jsontree_write_string(js_ctx, ap_conf->password);
  463. } else if (os_strncmp(path, "channel", 7) == 0) {
  464. jsontree_write_int(js_ctx, ap_conf->channel);
  465. } else if (os_strncmp(path, "authmode", 8) == 0) {
  466. switch (ap_conf->authmode) {
  467. case AUTH_OPEN:
  468. jsontree_write_string(js_ctx, "OPEN");
  469. break;
  470. case AUTH_WEP:
  471. jsontree_write_string(js_ctx, "WEP");
  472. break;
  473. case AUTH_WPA_PSK:
  474. jsontree_write_string(js_ctx, "WPAPSK");
  475. break;
  476. case AUTH_WPA2_PSK:
  477. jsontree_write_string(js_ctx, "WPA2PSK");
  478. break;
  479. case AUTH_WPA_WPA2_PSK:
  480. jsontree_write_string(js_ctx, "WPAPSK/WPA2PSK");
  481. break;
  482. default :
  483. jsontree_write_int(js_ctx, ap_conf->authmode);
  484. break;
  485. }
  486. } else if (os_strncmp(path, "ip", 2) == 0) {
  487. os_sprintf(buf, IPSTR, IP2STR(&ipconfig.ip));
  488. jsontree_write_string(js_ctx, buf);
  489. } else if (os_strncmp(path, "mask", 4) == 0) {
  490. os_sprintf(buf, IPSTR, IP2STR(&ipconfig.netmask));
  491. jsontree_write_string(js_ctx, buf);
  492. } else if (os_strncmp(path, "gw", 2) == 0) {
  493. os_sprintf(buf, IPSTR, IP2STR(&ipconfig.gw));
  494. jsontree_write_string(js_ctx, buf);
  495. }
  496. return 0;
  497. }
  498. /******************************************************************************
  499. * FunctionName : wifi_softap_set
  500. * Description : parse the softap parmer as a JSON format
  501. * Parameters : js_ctx -- A pointer to a JSON set up
  502. * parser -- A pointer to a JSON parser state
  503. * Returns : result
  504. *******************************************************************************/
  505. LOCAL int ICACHE_FLASH_ATTR
  506. wifi_softap_set(struct jsontree_context *js_ctx, struct jsonparse_state *parser)
  507. {
  508. int type;
  509. uint8 softap_tree;
  510. while ((type = jsonparse_next(parser)) != 0) {
  511. if (type == JSON_TYPE_PAIR_NAME) {
  512. char buffer[64];
  513. os_bzero(buffer, 64);
  514. if (jsonparse_strcmp_value(parser, "Station") == 0) {
  515. softap_tree = 0;
  516. } else if (jsonparse_strcmp_value(parser, "Softap") == 0) {
  517. softap_tree = 1;
  518. }
  519. if (softap_tree) {
  520. if (jsonparse_strcmp_value(parser, "authmode") == 0) {
  521. jsonparse_next(parser);
  522. jsonparse_next(parser);
  523. jsonparse_copy_value(parser, buffer, sizeof(buffer));
  524. // other mode will be supported later...
  525. if (os_strcmp(buffer, "OPEN") == 0) {
  526. ap_conf->authmode = AUTH_OPEN;
  527. } else if (os_strcmp(buffer, "WPAPSK") == 0) {
  528. ap_conf->authmode = AUTH_WPA_PSK;
  529. os_printf("%d %s\n", ap_conf->authmode, buffer);
  530. } else if (os_strcmp(buffer, "WPA2PSK") == 0) {
  531. ap_conf->authmode = AUTH_WPA2_PSK;
  532. } else if (os_strcmp(buffer, "WPAPSK/WPA2PSK") == 0) {
  533. ap_conf->authmode = AUTH_WPA_WPA2_PSK;
  534. } else {
  535. ap_conf->authmode = AUTH_OPEN;
  536. return 0;
  537. }
  538. }
  539. if (jsonparse_strcmp_value(parser, "channel") == 0) {
  540. jsonparse_next(parser);
  541. jsonparse_next(parser);
  542. ap_conf->channel = jsonparse_get_value_as_int(parser);
  543. } else if (jsonparse_strcmp_value(parser, "ssid") == 0) {
  544. jsonparse_next(parser);
  545. jsonparse_next(parser);
  546. jsonparse_copy_value(parser, buffer, sizeof(buffer));
  547. os_memcpy(ap_conf->ssid, buffer, os_strlen(buffer));
  548. } else if (jsonparse_strcmp_value(parser, "password") == 0) {
  549. jsonparse_next(parser);
  550. jsonparse_next(parser);
  551. jsonparse_copy_value(parser, buffer, sizeof(buffer));
  552. os_memcpy(ap_conf->password, buffer, os_strlen(buffer));
  553. }
  554. }
  555. }
  556. }
  557. return 0;
  558. }
  559. LOCAL struct jsontree_callback wifi_softap_callback =
  560. JSONTREE_CALLBACK(wifi_softap_get, wifi_softap_set);
  561. JSONTREE_OBJECT(softap_config_tree,
  562. JSONTREE_PAIR("authmode", &wifi_softap_callback),
  563. JSONTREE_PAIR("channel", &wifi_softap_callback),
  564. JSONTREE_PAIR("ssid", &wifi_softap_callback),
  565. JSONTREE_PAIR("password", &wifi_softap_callback));
  566. JSONTREE_OBJECT(softap_ip_tree,
  567. JSONTREE_PAIR("ip", &wifi_softap_callback),
  568. JSONTREE_PAIR("mask", &wifi_softap_callback),
  569. JSONTREE_PAIR("gw", &wifi_softap_callback));
  570. JSONTREE_OBJECT(get_softap_tree,
  571. JSONTREE_PAIR("Connect_Softap", &softap_config_tree),
  572. JSONTREE_PAIR("Ipinfo_Softap", &softap_ip_tree));
  573. JSONTREE_OBJECT(set_softap_tree,
  574. JSONTREE_PAIR("Ipinfo_Softap", &softap_config_tree));
  575. JSONTREE_OBJECT(get_wifi_tree,
  576. JSONTREE_PAIR("Station", &get_station_tree),
  577. JSONTREE_PAIR("Softap", &get_softap_tree));
  578. JSONTREE_OBJECT(set_wifi_tree,
  579. JSONTREE_PAIR("Station", &set_station_tree),
  580. JSONTREE_PAIR("Softap", &set_softap_tree));
  581. JSONTREE_OBJECT(wifi_response_tree,
  582. JSONTREE_PAIR("Response", &get_wifi_tree));
  583. JSONTREE_OBJECT(wifi_request_tree,
  584. JSONTREE_PAIR("Request", &set_wifi_tree));
  585. JSONTREE_OBJECT(wifi_info_tree,
  586. JSONTREE_PAIR("wifi", &wifi_response_tree));
  587. JSONTREE_OBJECT(wifi_req_tree,
  588. JSONTREE_PAIR("wifi", &wifi_request_tree));
  589. /******************************************************************************
  590. * FunctionName : scan_get
  591. * Description : set up the scan data as a JSON format
  592. * Parameters : js_ctx -- A pointer to a JSON set up
  593. * Returns : result
  594. *******************************************************************************/
  595. LOCAL int ICACHE_FLASH_ATTR
  596. scan_get(struct jsontree_context *js_ctx)
  597. {
  598. const char *path = jsontree_path_name(js_ctx, js_ctx->depth - 1);
  599. // STAILQ_HEAD(, bss_info) *pbss = scanarg;
  600. // LOCAL struct bss_info *bss;
  601. if (os_strncmp(path, "TotalPage", 9) == 0) {
  602. jsontree_write_int(js_ctx, pscaninfo->totalpage);
  603. } else if (os_strncmp(path, "PageNum", 7) == 0) {
  604. jsontree_write_int(js_ctx, pscaninfo->pagenum);
  605. } else if (os_strncmp(path, "bssid", 5) == 0) {
  606. if( bss == NULL )
  607. bss = bss_head;
  608. u8 buffer[32];
  609. //if (bss != NULL){
  610. os_memset(buffer, 0, sizeof(buffer));
  611. os_sprintf(buffer, MACSTR, MAC2STR(bss->bssid));
  612. jsontree_write_string(js_ctx, buffer);
  613. //}
  614. } else if (os_strncmp(path, "ssid", 4) == 0) {
  615. //if (bss != NULL)
  616. jsontree_write_string(js_ctx, bss->ssid);
  617. } else if (os_strncmp(path, "rssi", 4) == 0) {
  618. //if (bss != NULL)
  619. jsontree_write_int(js_ctx, -(bss->rssi));
  620. } else if (os_strncmp(path, "channel", 7) == 0) {
  621. //if (bss != NULL)
  622. jsontree_write_int(js_ctx, bss->channel);
  623. } else if (os_strncmp(path, "authmode", 8) == 0) {
  624. //if (bss != NULL){
  625. switch (bss->authmode) {
  626. case AUTH_OPEN:
  627. jsontree_write_string(js_ctx, "OPEN");
  628. break;
  629. case AUTH_WEP:
  630. jsontree_write_string(js_ctx, "WEP");
  631. break;
  632. case AUTH_WPA_PSK:
  633. jsontree_write_string(js_ctx, "WPAPSK");
  634. break;
  635. case AUTH_WPA2_PSK:
  636. jsontree_write_string(js_ctx, "WPA2PSK");
  637. break;
  638. case AUTH_WPA_WPA2_PSK:
  639. jsontree_write_string(js_ctx, "WPAPSK/WPA2PSK");
  640. break;
  641. default :
  642. jsontree_write_int(js_ctx, bss->authmode);
  643. break;
  644. }
  645. bss = STAILQ_NEXT(bss, next);
  646. // os_free(bss);
  647. //}
  648. }
  649. return 0;
  650. }
  651. LOCAL struct jsontree_callback scan_callback =
  652. JSONTREE_CALLBACK(scan_get, NULL);
  653. JSONTREE_OBJECT(scaninfo_tree,
  654. JSONTREE_PAIR("bssid", &scan_callback),
  655. JSONTREE_PAIR("ssid", &scan_callback),
  656. JSONTREE_PAIR("rssi", &scan_callback),
  657. JSONTREE_PAIR("channel", &scan_callback),
  658. JSONTREE_PAIR("authmode", &scan_callback));
  659. JSONTREE_ARRAY(scanrslt_tree,
  660. JSONTREE_PAIR_ARRAY(&scaninfo_tree),
  661. JSONTREE_PAIR_ARRAY(&scaninfo_tree),
  662. JSONTREE_PAIR_ARRAY(&scaninfo_tree),
  663. JSONTREE_PAIR_ARRAY(&scaninfo_tree),
  664. JSONTREE_PAIR_ARRAY(&scaninfo_tree),
  665. JSONTREE_PAIR_ARRAY(&scaninfo_tree),
  666. JSONTREE_PAIR_ARRAY(&scaninfo_tree),
  667. JSONTREE_PAIR_ARRAY(&scaninfo_tree));
  668. JSONTREE_OBJECT(scantree,
  669. JSONTREE_PAIR("TotalPage", &scan_callback),
  670. JSONTREE_PAIR("PageNum", &scan_callback),
  671. JSONTREE_PAIR("ScanResult", &scanrslt_tree));
  672. JSONTREE_OBJECT(scanres_tree,
  673. JSONTREE_PAIR("Response", &scantree));
  674. JSONTREE_OBJECT(scan_tree,
  675. JSONTREE_PAIR("scan", &scanres_tree));
  676. /******************************************************************************
  677. * FunctionName : parse_url
  678. * Description : parse the received data from the server
  679. * Parameters : precv -- the received data
  680. * purl_frame -- the result of parsing the url
  681. * Returns : none
  682. *******************************************************************************/
  683. LOCAL void ICACHE_FLASH_ATTR
  684. parse_url(char *precv, URL_Frame *purl_frame)
  685. {
  686. char *str = NULL;
  687. uint8 length = 0;
  688. char *pbuffer = NULL;
  689. char *pbufer = NULL;
  690. if (purl_frame == NULL || precv == NULL) {
  691. return;
  692. }
  693. pbuffer = (char *)os_strstr(precv, "Host:");
  694. if (pbuffer != NULL) {
  695. length = pbuffer - precv;
  696. pbufer = (char *)os_zalloc(length + 1);
  697. pbuffer = pbufer;
  698. os_memcpy(pbuffer, precv, length);
  699. os_memset(purl_frame->pSelect, 0, URLSize);
  700. os_memset(purl_frame->pCommand, 0, URLSize);
  701. os_memset(purl_frame->pFilename, 0, URLSize);
  702. if (os_strncmp(pbuffer, "GET ", 4) == 0) {
  703. purl_frame->Type = GET;
  704. pbuffer += 4;
  705. } else if (os_strncmp(pbuffer, "POST ", 5) == 0) {
  706. purl_frame->Type = POST;
  707. pbuffer += 5;
  708. }
  709. pbuffer ++;
  710. str = (char *)os_strstr(pbuffer, "?");
  711. if (str != NULL) {
  712. length = str - pbuffer;
  713. os_memcpy(purl_frame->pSelect, pbuffer, length);
  714. str ++;
  715. pbuffer = (char *)os_strstr(str, "=");
  716. if (pbuffer != NULL) {
  717. length = pbuffer - str;
  718. os_memcpy(purl_frame->pCommand, str, length);
  719. pbuffer ++;
  720. str = (char *)os_strstr(pbuffer, "&");
  721. if (str != NULL) {
  722. length = str - pbuffer;
  723. os_memcpy(purl_frame->pFilename, pbuffer, length);
  724. } else {
  725. str = (char *)os_strstr(pbuffer, " HTTP");
  726. if (str != NULL) {
  727. length = str - pbuffer;
  728. os_memcpy(purl_frame->pFilename, pbuffer, length);
  729. }
  730. }
  731. }
  732. }
  733. os_free(pbufer);
  734. } else {
  735. return;
  736. }
  737. }
  738. LOCAL char *precvbuffer;
  739. static uint32 dat_sumlength = 0;
  740. LOCAL bool ICACHE_FLASH_ATTR
  741. save_data(char *precv, uint16 length)
  742. {
  743. bool flag = false;
  744. char length_buf[10] = {0};
  745. char *ptemp = NULL;
  746. char *pdata = NULL;
  747. uint16 headlength = 0;
  748. static uint32 totallength = 0;
  749. ptemp = (char *)os_strstr(precv, "\r\n\r\n");
  750. if (ptemp != NULL) {
  751. length -= ptemp - precv;
  752. length -= 4;
  753. totallength += length;
  754. headlength = ptemp - precv + 4;
  755. pdata = (char *)os_strstr(precv, "Content-Length: ");
  756. if (pdata != NULL) {
  757. pdata += 16;
  758. precvbuffer = (char *)os_strstr(pdata, "\r\n");
  759. if (precvbuffer != NULL) {
  760. os_memcpy(length_buf, pdata, precvbuffer - pdata);
  761. dat_sumlength = atoi(length_buf);
  762. }
  763. } else {
  764. if (totallength != 0x00){
  765. totallength = 0;
  766. dat_sumlength = 0;
  767. return false;
  768. }
  769. }
  770. if ((dat_sumlength + headlength) >= 1024) {
  771. precvbuffer = (char *)os_zalloc(headlength + 1);
  772. os_memcpy(precvbuffer, precv, headlength + 1);
  773. } else {
  774. precvbuffer = (char *)os_zalloc(dat_sumlength + headlength + 1);
  775. os_memcpy(precvbuffer, precv, os_strlen(precv));
  776. }
  777. } else {
  778. if (precvbuffer != NULL) {
  779. totallength += length;
  780. os_memcpy(precvbuffer + os_strlen(precvbuffer), precv, length);
  781. } else {
  782. totallength = 0;
  783. dat_sumlength = 0;
  784. return false;
  785. }
  786. }
  787. if (totallength == dat_sumlength) {
  788. totallength = 0;
  789. dat_sumlength = 0;
  790. return true;
  791. } else {
  792. return false;
  793. }
  794. }
  795. LOCAL bool ICACHE_FLASH_ATTR
  796. check_data(char *precv, uint16 length)
  797. {
  798. //bool flag = true;
  799. char length_buf[10] = {0};
  800. char *ptemp = NULL;
  801. char *pdata = NULL;
  802. char *tmp_precvbuffer;
  803. uint16 tmp_length = length;
  804. uint32 tmp_totallength = 0;
  805. ptemp = (char *)os_strstr(precv, "\r\n\r\n");
  806. if (ptemp != NULL) {
  807. tmp_length -= ptemp - precv;
  808. tmp_length -= 4;
  809. tmp_totallength += tmp_length;
  810. pdata = (char *)os_strstr(precv, "Content-Length: ");
  811. if (pdata != NULL){
  812. pdata += 16;
  813. tmp_precvbuffer = (char *)os_strstr(pdata, "\r\n");
  814. if (tmp_precvbuffer != NULL){
  815. os_memcpy(length_buf, pdata, tmp_precvbuffer - pdata);
  816. dat_sumlength = atoi(length_buf);
  817. os_printf("A_dat:%u,tot:%u,lenght:%u\n",dat_sumlength,tmp_totallength,tmp_length);
  818. if(dat_sumlength != tmp_totallength){
  819. return false;
  820. }
  821. }
  822. }
  823. }
  824. return true;
  825. }
  826. LOCAL os_timer_t *restart_10ms;
  827. LOCAL rst_parm *rstparm;
  828. /******************************************************************************
  829. * FunctionName : restart_10ms_cb
  830. * Description : system restart or wifi reconnected after a certain time.
  831. * Parameters : arg -- Additional argument to pass to the function
  832. * Returns : none
  833. *******************************************************************************/
  834. LOCAL void ICACHE_FLASH_ATTR
  835. restart_10ms_cb(void *arg)
  836. {
  837. if (rstparm != NULL && rstparm->pespconn != NULL) {
  838. switch (rstparm->parmtype) {
  839. case WIFI:
  840. //if (rstparm->pespconn->state == ESPCONN_CLOSE) {
  841. if (sta_conf->ssid[0] != 0x00) {
  842. wifi_station_set_config(sta_conf);
  843. wifi_station_disconnect();
  844. wifi_station_connect();
  845. user_esp_platform_check_ip(1);
  846. }
  847. if (ap_conf->ssid[0] != 0x00) {
  848. wifi_softap_set_config(ap_conf);
  849. system_restart();
  850. }
  851. os_free(ap_conf);
  852. ap_conf = NULL;
  853. os_free(sta_conf);
  854. sta_conf = NULL;
  855. os_free(rstparm);
  856. rstparm = NULL;
  857. os_free(restart_10ms);
  858. restart_10ms = NULL;
  859. //} else {
  860. // os_timer_arm(restart_10ms, 10, 0);
  861. //}
  862. break;
  863. case DEEP_SLEEP:
  864. case REBOOT:
  865. if (rstparm->pespconn->state == ESPCONN_CLOSE) {
  866. wifi_set_opmode(STATION_MODE);
  867. if (rstparm->parmtype == DEEP_SLEEP) {
  868. #if SENSOR_DEVICE
  869. system_deep_sleep(SENSOR_DEEP_SLEEP_TIME);
  870. #endif
  871. }
  872. } else {
  873. os_timer_arm(restart_10ms, 10, 0);
  874. }
  875. break;
  876. default:
  877. break;
  878. }
  879. }
  880. }
  881. /******************************************************************************
  882. * FunctionName : data_send
  883. * Description : processing the data as http format and send to the client or server
  884. * Parameters : arg -- argument to set for client or server
  885. * responseOK -- true or false
  886. * psend -- The send data
  887. * Returns :
  888. *******************************************************************************/
  889. LOCAL void ICACHE_FLASH_ATTR
  890. data_send(void *arg, bool responseOK, char *psend)
  891. {
  892. uint16 length = 0;
  893. char *pbuf = NULL;
  894. char httphead[256];
  895. struct espconn *ptrespconn = arg;
  896. os_memset(httphead, 0, 256);
  897. if (responseOK) {
  898. os_sprintf(httphead,
  899. "HTTP/1.0 200 OK\r\nContent-Length: %d\r\nServer: lwIP/1.4.0\r\n",
  900. psend ? os_strlen(psend) : 0);
  901. if (psend) {
  902. os_sprintf(httphead + os_strlen(httphead),
  903. "Content-type: application/json\r\nExpires: Fri, 10 Apr 2008 14:00:00 GMT\r\nPragma: no-cache\r\n\r\n");
  904. length = os_strlen(httphead) + os_strlen(psend);
  905. pbuf = (char *)os_zalloc(length + 1);
  906. os_memcpy(pbuf, httphead, os_strlen(httphead));
  907. os_memcpy(pbuf + os_strlen(httphead), psend, os_strlen(psend));
  908. } else {
  909. os_sprintf(httphead + os_strlen(httphead), "\n");
  910. length = os_strlen(httphead);
  911. }
  912. } else {
  913. os_sprintf(httphead, "HTTP/1.0 400 BadRequest\r\n\
  914. Content-Length: 0\r\nServer: lwIP/1.4.0\r\n\n");
  915. length = os_strlen(httphead);
  916. }
  917. if (psend) {
  918. #ifdef SERVER_SSL_ENABLE
  919. espconn_secure_sent(ptrespconn, pbuf, length);
  920. #else
  921. espconn_sent(ptrespconn, pbuf, length);
  922. #endif
  923. } else {
  924. #ifdef SERVER_SSL_ENABLE
  925. espconn_secure_sent(ptrespconn, httphead, length);
  926. #else
  927. espconn_sent(ptrespconn, httphead, length);
  928. #endif
  929. }
  930. if (pbuf) {
  931. os_free(pbuf);
  932. pbuf = NULL;
  933. }
  934. }
  935. /******************************************************************************
  936. * FunctionName : json_send
  937. * Description : processing the data as json format and send to the client or server
  938. * Parameters : arg -- argument to set for client or server
  939. * ParmType -- json format type
  940. * Returns : none
  941. *******************************************************************************/
  942. LOCAL void ICACHE_FLASH_ATTR
  943. json_send(void *arg, ParmType ParmType)
  944. {
  945. char *pbuf = NULL;
  946. pbuf = (char *)os_zalloc(jsonSize);
  947. struct espconn *ptrespconn = arg;
  948. switch (ParmType) {
  949. #if LIGHT_DEVICE
  950. case LIGHT_STATUS:
  951. json_ws_send((struct jsontree_value *)&PwmTree, "light", pbuf);
  952. break;
  953. #endif
  954. #if PLUG_DEVICE
  955. case SWITCH_STATUS:
  956. json_ws_send((struct jsontree_value *)&StatusTree, "switch", pbuf);
  957. break;
  958. #endif
  959. case INFOMATION:
  960. json_ws_send((struct jsontree_value *)&INFOTree, "info", pbuf);
  961. break;
  962. case WIFI:
  963. json_ws_send((struct jsontree_value *)&wifi_info_tree, "wifi", pbuf);
  964. break;
  965. case CONNECT_STATUS:
  966. json_ws_send((struct jsontree_value *)&con_status_tree, "info", pbuf);
  967. break;
  968. case USER_BIN:
  969. json_ws_send((struct jsontree_value *)&userinfo_tree, "user_info", pbuf);
  970. break;
  971. case SCAN: {
  972. u8 i = 0;
  973. u8 scancount = 0;
  974. struct bss_info *bss = NULL;
  975. // bss = STAILQ_FIRST(pscaninfo->pbss);
  976. bss = bss_head;
  977. if (bss == NULL) {
  978. os_free(pscaninfo);
  979. pscaninfo = NULL;
  980. os_sprintf(pbuf, "{\n\"successful\": false,\n\"data\": null\n}");
  981. } else {
  982. do {
  983. if (pscaninfo->page_sn == pscaninfo->pagenum) {
  984. pscaninfo->page_sn = 0;
  985. os_sprintf(pbuf, "{\n\"successful\": false,\n\"meessage\": \"repeated page\"\n}");
  986. break;
  987. }
  988. scancount = scannum - (pscaninfo->pagenum - 1) * 8;
  989. if (scancount >= 8) {
  990. pscaninfo->data_cnt += 8;
  991. pscaninfo->page_sn = pscaninfo->pagenum;
  992. if (pscaninfo->data_cnt > scannum) {
  993. pscaninfo->data_cnt -= 8;
  994. os_sprintf(pbuf, "{\n\"successful\": false,\n\"meessage\": \"error page\"\n}");
  995. break;
  996. }
  997. json_ws_send((struct jsontree_value *)&scan_tree, "scan", pbuf);
  998. } else {
  999. pscaninfo->data_cnt += scancount;
  1000. pscaninfo->page_sn = pscaninfo->pagenum;
  1001. if (pscaninfo->data_cnt > scannum) {
  1002. pscaninfo->data_cnt -= scancount;
  1003. os_sprintf(pbuf, "{\n\"successful\": false,\n\"meessage\": \"error page\"\n}");
  1004. break;
  1005. }
  1006. char *ptrscanbuf = (char *)os_zalloc(jsonSize);
  1007. char *pscanbuf = ptrscanbuf;
  1008. os_sprintf(pscanbuf, ",\n\"ScanResult\": [\n");
  1009. pscanbuf += os_strlen(pscanbuf);
  1010. for (i = 0; i < scancount; i ++) {
  1011. JSONTREE_OBJECT(page_tree,
  1012. JSONTREE_PAIR("page", &scaninfo_tree));
  1013. json_ws_send((struct jsontree_value *)&page_tree, "page", pscanbuf);
  1014. os_sprintf(pscanbuf + os_strlen(pscanbuf), ",\n");
  1015. pscanbuf += os_strlen(pscanbuf);
  1016. }
  1017. os_sprintf(pscanbuf - 2, "]\n");
  1018. JSONTREE_OBJECT(scantree,
  1019. JSONTREE_PAIR("TotalPage", &scan_callback),
  1020. JSONTREE_PAIR("PageNum", &scan_callback));
  1021. JSONTREE_OBJECT(scanres_tree,
  1022. JSONTREE_PAIR("Response", &scantree));
  1023. JSONTREE_OBJECT(scan_tree,
  1024. JSONTREE_PAIR("scan", &scanres_tree));
  1025. json_ws_send((struct jsontree_value *)&scan_tree, "scan", pbuf);
  1026. os_memcpy(pbuf + os_strlen(pbuf) - 4, ptrscanbuf, os_strlen(ptrscanbuf));
  1027. os_sprintf(pbuf + os_strlen(pbuf), "}\n}");
  1028. os_free(ptrscanbuf);
  1029. }
  1030. } while (0);
  1031. }
  1032. break;
  1033. }
  1034. default :
  1035. break;
  1036. }
  1037. data_send(ptrespconn, true, pbuf);
  1038. os_free(pbuf);
  1039. pbuf = NULL;
  1040. }
  1041. /******************************************************************************
  1042. * FunctionName : response_send
  1043. * Description : processing the send result
  1044. * Parameters : arg -- argument to set for client or server
  1045. * responseOK -- true or false
  1046. * Returns : none
  1047. *******************************************************************************/
  1048. LOCAL void ICACHE_FLASH_ATTR
  1049. response_send(void *arg, bool responseOK)
  1050. {
  1051. struct espconn *ptrespconn = arg;
  1052. data_send(ptrespconn, responseOK, NULL);
  1053. }
  1054. /******************************************************************************
  1055. * FunctionName : json_scan_cb
  1056. * Description : processing the scan result
  1057. * Parameters : arg -- Additional argument to pass to the callback function
  1058. * status -- scan status
  1059. * Returns : none
  1060. *******************************************************************************/
  1061. LOCAL void ICACHE_FLASH_ATTR json_scan_cb(void *arg, STATUS status)
  1062. {
  1063. pscaninfo->pbss = arg;
  1064. if (scannum % 8 == 0) {
  1065. pscaninfo->totalpage = scannum / 8;
  1066. } else {
  1067. pscaninfo->totalpage = scannum / 8 + 1;
  1068. }
  1069. JSONTREE_OBJECT(totaltree,
  1070. JSONTREE_PAIR("TotalPage", &scan_callback));
  1071. JSONTREE_OBJECT(totalres_tree,
  1072. JSONTREE_PAIR("Response", &totaltree));
  1073. JSONTREE_OBJECT(total_tree,
  1074. JSONTREE_PAIR("total", &totalres_tree));
  1075. bss_temp = bss_head;
  1076. while(bss_temp !=NULL) {
  1077. bss_head = bss_temp->next.stqe_next;
  1078. os_free(bss_temp);
  1079. bss_temp = bss_head;
  1080. }
  1081. bss_head = NULL;
  1082. bss_temp = NULL;
  1083. bss = STAILQ_FIRST(pscaninfo->pbss);
  1084. while(bss != NULL) {
  1085. if(bss_temp == NULL){
  1086. bss_temp = (struct bss_info *)os_zalloc(sizeof(struct bss_info));
  1087. bss_head = bss_temp;
  1088. } else {
  1089. bss_temp->next.stqe_next = (struct bss_info *)os_zalloc(sizeof(struct bss_info));
  1090. bss_temp = bss_temp->next.stqe_next;
  1091. }
  1092. if(bss_temp == NULL) {
  1093. os_printf("malloc scan info failed\n");
  1094. break;
  1095. } else{
  1096. os_memcpy(bss_temp->bssid,bss->bssid,sizeof(bss->bssid));
  1097. os_memcpy(bss_temp->ssid,bss->ssid,sizeof(bss->ssid));
  1098. bss_temp->authmode = bss->authmode;
  1099. bss_temp->rssi = bss->rssi;
  1100. bss_temp->channel = bss->channel;
  1101. }
  1102. bss = STAILQ_NEXT(bss,next);
  1103. }
  1104. char *pbuf = NULL;
  1105. pbuf = (char *)os_zalloc(jsonSize);
  1106. json_ws_send((struct jsontree_value *)&total_tree, "total", pbuf);
  1107. data_send(pscaninfo->pespconn, true, pbuf);
  1108. os_free(pbuf);
  1109. }
  1110. void ICACHE_FLASH_ATTR
  1111. upgrade_check_func(void *arg)
  1112. {
  1113. struct espconn *ptrespconn = arg;
  1114. os_timer_disarm(&upgrade_check_timer);
  1115. if(system_upgrade_flag_check() == UPGRADE_FLAG_START) {
  1116. response_send(ptrespconn, false);
  1117. system_upgrade_deinit();
  1118. system_upgrade_flag_set(UPGRADE_FLAG_IDLE);
  1119. upgrade_lock = 0;
  1120. os_printf("local upgrade failed\n");
  1121. } else if( system_upgrade_flag_check() == UPGRADE_FLAG_FINISH ) {
  1122. os_printf("local upgrade success\n");
  1123. response_send(ptrespconn, true);
  1124. upgrade_lock = 0;
  1125. } else {
  1126. }
  1127. }
  1128. /******************************************************************************
  1129. * FunctionName : upgrade_deinit
  1130. * Description : disconnect the connection with the host
  1131. * Parameters : bin -- server number
  1132. * Returns : none
  1133. *******************************************************************************/
  1134. void ICACHE_FLASH_ATTR
  1135. LOCAL local_upgrade_deinit(void)
  1136. {
  1137. if (system_upgrade_flag_check() != UPGRADE_FLAG_START) {
  1138. os_printf("system upgrade deinit\n");
  1139. system_upgrade_deinit();
  1140. }
  1141. }
  1142. /******************************************************************************
  1143. * FunctionName : upgrade_download
  1144. * Description : Processing the upgrade data from the host
  1145. * Parameters : bin -- server number
  1146. * pusrdata -- The upgrade data (or NULL when the connection has been closed!)
  1147. * length -- The length of upgrade data
  1148. * Returns : none
  1149. *******************************************************************************/
  1150. LOCAL void ICACHE_FLASH_ATTR
  1151. local_upgrade_download(void * arg,char *pusrdata, unsigned short length)
  1152. {
  1153. char *ptr = NULL;
  1154. char *ptmp2 = NULL;
  1155. char lengthbuffer[32];
  1156. static uint32 totallength = 0;
  1157. static uint32 sumlength = 0;
  1158. static uint32 erase_length = 0;
  1159. char A_buf[2] = {0xE9 ,0x03}; char B_buf[2] = {0xEA,0x04};
  1160. struct espconn *pespconn = arg;
  1161. if (totallength == 0 && (ptr = (char *)os_strstr(pusrdata, "\r\n\r\n")) != NULL &&
  1162. (ptr = (char *)os_strstr(pusrdata, "Content-Length")) != NULL) {
  1163. ptr = (char *)os_strstr(pusrdata, "Content-Length: ");
  1164. if (ptr != NULL) {
  1165. ptr += 16;
  1166. ptmp2 = (char *)os_strstr(ptr, "\r\n");
  1167. if (ptmp2 != NULL) {
  1168. os_memset(lengthbuffer, 0, sizeof(lengthbuffer));
  1169. os_memcpy(lengthbuffer, ptr, ptmp2 - ptr);
  1170. sumlength = atoi(lengthbuffer);
  1171. if (sumlength == 0) {
  1172. os_timer_disarm(&upgrade_check_timer);
  1173. os_timer_setfn(&upgrade_check_timer, (os_timer_func_t *)upgrade_check_func, pespconn);
  1174. os_timer_arm(&upgrade_check_timer, 10, 0);
  1175. return;
  1176. }
  1177. } else {
  1178. os_printf("sumlength failed\n");
  1179. }
  1180. } else {
  1181. os_printf("Content-Length: failed\n");
  1182. }
  1183. if (sumlength != 0) {
  1184. if (sumlength >= LIMIT_ERASE_SIZE){
  1185. system_upgrade_erase_flash(0xFFFF);
  1186. erase_length = sumlength - LIMIT_ERASE_SIZE;
  1187. } else {
  1188. system_upgrade_erase_flash(sumlength);
  1189. erase_length = 0;
  1190. }
  1191. }
  1192. ptr = (char *)os_strstr(pusrdata, "\r\n\r\n");
  1193. length -= ptr - pusrdata;
  1194. length -= 4;
  1195. totallength += length;
  1196. os_printf("upgrade file download start.\n");
  1197. system_upgrade(ptr + 4, length);
  1198. } else {
  1199. totallength += length;
  1200. if (erase_length >= LIMIT_ERASE_SIZE){
  1201. system_upgrade_erase_flash(0xFFFF);
  1202. erase_length -= LIMIT_ERASE_SIZE;
  1203. } else {
  1204. system_upgrade_erase_flash(erase_length);
  1205. erase_length = 0;
  1206. }
  1207. system_upgrade(pusrdata, length);
  1208. }
  1209. if (totallength == sumlength) {
  1210. os_printf("upgrade file download finished.\n");
  1211. system_upgrade_flag_set(UPGRADE_FLAG_FINISH);
  1212. totallength = 0;
  1213. sumlength = 0;
  1214. upgrade_check_func(pespconn);
  1215. os_timer_disarm(&app_upgrade_10s);
  1216. os_timer_setfn(&app_upgrade_10s, (os_timer_func_t *)local_upgrade_deinit, NULL);
  1217. os_timer_arm(&app_upgrade_10s, 10, 0);
  1218. }
  1219. }
  1220. /******************************************************************************
  1221. * FunctionName : webserver_recv
  1222. * Description : Processing the received data from the server
  1223. * Parameters : arg -- Additional argument to pass to the callback function
  1224. * pusrdata -- The received data (or NULL when the connection has been closed!)
  1225. * length -- The length of received data
  1226. * Returns : none
  1227. *******************************************************************************/
  1228. LOCAL void ICACHE_FLASH_ATTR
  1229. webserver_recv(void *arg, char *pusrdata, unsigned short length)
  1230. {
  1231. URL_Frame *pURL_Frame = NULL;
  1232. char *pParseBuffer = NULL;
  1233. bool parse_flag = false;
  1234. struct espconn *ptrespconn = arg;
  1235. if(upgrade_lock == 0){
  1236. os_printf("len:%u\n",length);
  1237. if(check_data(pusrdata, length) == false)
  1238. {
  1239. os_printf("goto\n");
  1240. goto _temp_exit;
  1241. }
  1242. parse_flag = save_data(pusrdata, length);
  1243. if (parse_flag == false) {
  1244. response_send(ptrespconn, false);
  1245. }
  1246. // os_printf(precvbuffer);
  1247. pURL_Frame = (URL_Frame *)os_zalloc(sizeof(URL_Frame));
  1248. parse_url(precvbuffer, pURL_Frame);
  1249. switch (pURL_Frame->Type) {
  1250. case GET:
  1251. os_printf("We have a GET request.\n");
  1252. if (os_strcmp(pURL_Frame->pSelect, "client") == 0 &&
  1253. os_strcmp(pURL_Frame->pCommand, "command") == 0) {
  1254. if (os_strcmp(pURL_Frame->pFilename, "info") == 0) {
  1255. json_send(ptrespconn, INFOMATION);
  1256. }
  1257. if (os_strcmp(pURL_Frame->pFilename, "status") == 0) {
  1258. json_send(ptrespconn, CONNECT_STATUS);
  1259. } else if (os_strcmp(pURL_Frame->pFilename, "scan") == 0) {
  1260. char *strstr = NULL;
  1261. strstr = (char *)os_strstr(pusrdata, "&");
  1262. if (strstr == NULL) {
  1263. if (pscaninfo == NULL) {
  1264. pscaninfo = (scaninfo *)os_zalloc(sizeof(scaninfo));
  1265. }
  1266. pscaninfo->pespconn = ptrespconn;
  1267. pscaninfo->pagenum = 0;
  1268. pscaninfo->page_sn = 0;
  1269. pscaninfo->data_cnt = 0;
  1270. wifi_station_scan(NULL, json_scan_cb);
  1271. } else {
  1272. strstr ++;
  1273. if (os_strncmp(strstr, "page", 4) == 0) {
  1274. if (pscaninfo != NULL) {
  1275. pscaninfo->pagenum = *(strstr + 5);
  1276. pscaninfo->pagenum -= 0x30;
  1277. if (pscaninfo->pagenum > pscaninfo->totalpage || pscaninfo->pagenum == 0) {
  1278. response_send(ptrespconn, false);
  1279. } else {
  1280. json_send(ptrespconn, SCAN);
  1281. }
  1282. } else {
  1283. response_send(ptrespconn, false);
  1284. }
  1285. } else if(os_strncmp(strstr, "finish", 6) == 0){
  1286. bss_temp = bss_head;
  1287. while(bss_temp != NULL) {
  1288. bss_head = bss_temp->next.stqe_next;
  1289. os_free(bss_temp);
  1290. bss_temp = bss_head;
  1291. }
  1292. bss_head = NULL;
  1293. bss_temp = NULL;
  1294. response_send(ptrespconn, true);
  1295. } else {
  1296. response_send(ptrespconn, false);
  1297. }
  1298. }
  1299. } else {
  1300. response_send(ptrespconn, false);
  1301. }
  1302. } else if (os_strcmp(pURL_Frame->pSelect, "config") == 0 &&
  1303. os_strcmp(pURL_Frame->pCommand, "command") == 0) {
  1304. if (os_strcmp(pURL_Frame->pFilename, "wifi") == 0) {
  1305. ap_conf = (struct softap_config *)os_zalloc(sizeof(struct softap_config));
  1306. sta_conf = (struct station_config *)os_zalloc(sizeof(struct station_config));
  1307. json_send(ptrespconn, WIFI);
  1308. os_free(sta_conf);
  1309. os_free(ap_conf);
  1310. sta_conf = NULL;
  1311. ap_conf = NULL;
  1312. }
  1313. #if PLUG_DEVICE
  1314. else if (os_strcmp(pURL_Frame->pFilename, "switch") == 0) {
  1315. json_send(ptrespconn, SWITCH_STATUS);
  1316. }
  1317. #endif
  1318. #if LIGHT_DEVICE
  1319. else if (os_strcmp(pURL_Frame->pFilename, "light") == 0) {
  1320. json_send(ptrespconn, LIGHT_STATUS);
  1321. }
  1322. #endif
  1323. else if (os_strcmp(pURL_Frame->pFilename, "reboot") == 0) {
  1324. json_send(ptrespconn, REBOOT);
  1325. } else {
  1326. response_send(ptrespconn, false);
  1327. }
  1328. } else if (os_strcmp(pURL_Frame->pSelect, "upgrade") == 0 &&
  1329. os_strcmp(pURL_Frame->pCommand, "command") == 0) {
  1330. if (os_strcmp(pURL_Frame->pFilename, "getuser") == 0) {
  1331. json_send(ptrespconn , USER_BIN);
  1332. }
  1333. } else {
  1334. response_send(ptrespconn, false);
  1335. }
  1336. break;
  1337. case POST:
  1338. os_printf("We have a POST request.\n");
  1339. pParseBuffer = (char *)os_strstr(precvbuffer, "\r\n\r\n");
  1340. if (pParseBuffer == NULL) {
  1341. break;
  1342. }
  1343. pParseBuffer += 4;
  1344. if (os_strcmp(pURL_Frame->pSelect, "config") == 0 &&
  1345. os_strcmp(pURL_Frame->pCommand, "command") == 0) {
  1346. #if SENSOR_DEVICE
  1347. if (os_strcmp(pURL_Frame->pFilename, "sleep") == 0) {
  1348. #else
  1349. if (os_strcmp(pURL_Frame->pFilename, "reboot") == 0) {
  1350. #endif
  1351. if (pParseBuffer != NULL) {
  1352. if (restart_10ms != NULL) {
  1353. os_timer_disarm(restart_10ms);
  1354. }
  1355. if (rstparm == NULL) {
  1356. rstparm = (rst_parm *)os_zalloc(sizeof(rst_parm));
  1357. }
  1358. rstparm->pespconn = ptrespconn;
  1359. #if SENSOR_DEVICE
  1360. rstparm->parmtype = DEEP_SLEEP;
  1361. #else
  1362. rstparm->parmtype = REBOOT;
  1363. #endif
  1364. if (restart_10ms == NULL) {
  1365. restart_10ms = (os_timer_t *)os_malloc(sizeof(os_timer_t));
  1366. }
  1367. os_timer_setfn(restart_10ms, (os_timer_func_t *)restart_10ms_cb, NULL);
  1368. os_timer_arm(restart_10ms, 10, 0); // delay 10ms, then do
  1369. response_send(ptrespconn, true);
  1370. } else {
  1371. response_send(ptrespconn, false);
  1372. }
  1373. } else if (os_strcmp(pURL_Frame->pFilename, "wifi") == 0) {
  1374. if (pParseBuffer != NULL) {
  1375. struct jsontree_context js;
  1376. user_esp_platform_set_connect_status(DEVICE_CONNECTING);
  1377. if (restart_10ms != NULL) {
  1378. os_timer_disarm(restart_10ms);
  1379. }
  1380. if (ap_conf == NULL) {
  1381. ap_conf = (struct softap_config *)os_zalloc(sizeof(struct softap_config));
  1382. }
  1383. if (sta_conf == NULL) {
  1384. sta_conf = (struct station_config *)os_zalloc(sizeof(struct station_config));
  1385. }
  1386. jsontree_setup(&js, (struct jsontree_value *)&wifi_req_tree, json_putchar);
  1387. json_parse(&js, pParseBuffer);
  1388. if (rstparm == NULL) {
  1389. rstparm = (rst_parm *)os_zalloc(sizeof(rst_parm));
  1390. }
  1391. rstparm->pespconn = ptrespconn;
  1392. rstparm->parmtype = WIFI;
  1393. if (sta_conf->ssid[0] != 0x00 || ap_conf->ssid[0] != 0x00) {
  1394. ap_conf->ssid_hidden = 0;
  1395. ap_conf->max_connection = 4;
  1396. if (restart_10ms == NULL) {
  1397. restart_10ms = (os_timer_t *)os_malloc(sizeof(os_timer_t));
  1398. }
  1399. os_timer_disarm(restart_10ms);
  1400. os_timer_setfn(restart_10ms, (os_timer_func_t *)restart_10ms_cb, NULL);
  1401. os_timer_arm(restart_10ms, 10, 0); // delay 10ms, then do
  1402. } else {
  1403. os_free(ap_conf);
  1404. os_free(sta_conf);
  1405. os_free(rstparm);
  1406. sta_conf = NULL;
  1407. ap_conf = NULL;
  1408. rstparm =NULL;
  1409. }
  1410. response_send(ptrespconn, true);
  1411. } else {
  1412. response_send(ptrespconn, false);
  1413. }
  1414. }
  1415. #if PLUG_DEVICE
  1416. else if (os_strcmp(pURL_Frame->pFilename, "switch") == 0) {
  1417. if (pParseBuffer != NULL) {
  1418. struct jsontree_context js;
  1419. jsontree_setup(&js, (struct jsontree_value *)&StatusTree, json_putchar);
  1420. json_parse(&js, pParseBuffer);
  1421. response_send(ptrespconn, true);
  1422. } else {
  1423. response_send(ptrespconn, false);
  1424. }
  1425. }
  1426. #endif
  1427. #if LIGHT_DEVICE
  1428. else if (os_strcmp(pURL_Frame->pFilename, "light") == 0) {
  1429. if (pParseBuffer != NULL) {
  1430. struct jsontree_context js;
  1431. jsontree_setup(&js, (struct jsontree_value *)&PwmTree, json_putchar);
  1432. json_parse(&js, pParseBuffer);
  1433. os_printf("rsp1:%u\n",PostCmdNeeRsp);
  1434. if(PostCmdNeeRsp == 0)
  1435. PostCmdNeeRsp = 1;
  1436. else
  1437. response_send(ptrespconn, true);
  1438. } else {
  1439. response_send(ptrespconn, false);
  1440. }
  1441. }
  1442. else if (os_strcmp(pURL_Frame->pFilename, "reset") == 0) {
  1443. response_send(ptrespconn, true);
  1444. extern struct esp_platform_saved_param esp_param;
  1445. esp_param.activeflag = 0;
  1446. system_param_save_with_protect(ESP_PARAM_START_SEC, &esp_param, sizeof(esp_param));
  1447. system_restore();
  1448. system_restart();
  1449. }
  1450. #endif
  1451. else {
  1452. response_send(ptrespconn, false);
  1453. }
  1454. }
  1455. else if(os_strcmp(pURL_Frame->pSelect, "upgrade") == 0 &&
  1456. os_strcmp(pURL_Frame->pCommand, "command") == 0){
  1457. if (os_strcmp(pURL_Frame->pFilename, "start") == 0){
  1458. response_send(ptrespconn, true);
  1459. os_printf("local upgrade start\n");
  1460. upgrade_lock = 1;
  1461. system_upgrade_init();
  1462. system_upgrade_flag_set(UPGRADE_FLAG_START);
  1463. os_timer_disarm(&upgrade_check_timer);
  1464. os_timer_setfn(&upgrade_check_timer, (os_timer_func_t *)upgrade_check_func, NULL);
  1465. os_timer_arm(&upgrade_check_timer, 120000, 0);
  1466. } else if (os_strcmp(pURL_Frame->pFilename, "reset") == 0) {
  1467. response_send(ptrespconn, true);
  1468. os_printf("local upgrade restart\n");
  1469. system_upgrade_reboot();
  1470. } else {
  1471. response_send(ptrespconn, false);
  1472. }
  1473. }else {
  1474. response_send(ptrespconn, false);
  1475. }
  1476. break;
  1477. }
  1478. if (precvbuffer != NULL){
  1479. os_free(precvbuffer);
  1480. precvbuffer = NULL;
  1481. }
  1482. os_free(pURL_Frame);
  1483. pURL_Frame = NULL;
  1484. _temp_exit:
  1485. ;
  1486. }
  1487. else if(upgrade_lock == 1){
  1488. local_upgrade_download(ptrespconn,pusrdata, length);
  1489. if (precvbuffer != NULL){
  1490. os_free(precvbuffer);
  1491. precvbuffer = NULL;
  1492. }
  1493. os_free(pURL_Frame);
  1494. pURL_Frame = NULL;
  1495. }
  1496. }
  1497. /******************************************************************************
  1498. * FunctionName : webserver_recon
  1499. * Description : the connection has been err, reconnection
  1500. * Parameters : arg -- Additional argument to pass to the callback function
  1501. * Returns : none
  1502. *******************************************************************************/
  1503. LOCAL ICACHE_FLASH_ATTR
  1504. void webserver_recon(void *arg, sint8 err)
  1505. {
  1506. struct espconn *pesp_conn = arg;
  1507. os_printf("webserver's %d.%d.%d.%d:%d err %d reconnect\n", pesp_conn->proto.tcp->remote_ip[0],
  1508. pesp_conn->proto.tcp->remote_ip[1],pesp_conn->proto.tcp->remote_ip[2],
  1509. pesp_conn->proto.tcp->remote_ip[3],pesp_conn->proto.tcp->remote_port, err);
  1510. }
  1511. /******************************************************************************
  1512. * FunctionName : webserver_recon
  1513. * Description : the connection has been err, reconnection
  1514. * Parameters : arg -- Additional argument to pass to the callback function
  1515. * Returns : none
  1516. *******************************************************************************/
  1517. LOCAL ICACHE_FLASH_ATTR
  1518. void webserver_discon(void *arg)
  1519. {
  1520. struct espconn *pesp_conn = arg;
  1521. os_printf("webserver's %d.%d.%d.%d:%d disconnect\n", pesp_conn->proto.tcp->remote_ip[0],
  1522. pesp_conn->proto.tcp->remote_ip[1],pesp_conn->proto.tcp->remote_ip[2],
  1523. pesp_conn->proto.tcp->remote_ip[3],pesp_conn->proto.tcp->remote_port);
  1524. }
  1525. /******************************************************************************
  1526. * FunctionName : user_accept_listen
  1527. * Description : server listened a connection successfully
  1528. * Parameters : arg -- Additional argument to pass to the callback function
  1529. * Returns : none
  1530. *******************************************************************************/
  1531. LOCAL void ICACHE_FLASH_ATTR
  1532. webserver_listen(void *arg)
  1533. {
  1534. struct espconn *pesp_conn = arg;
  1535. espconn_regist_recvcb(pesp_conn, webserver_recv);
  1536. espconn_regist_reconcb(pesp_conn, webserver_recon);
  1537. espconn_regist_disconcb(pesp_conn, webserver_discon);
  1538. }
  1539. /******************************************************************************
  1540. * FunctionName : user_webserver_init
  1541. * Description : parameter initialize as a server
  1542. * Parameters : port -- server port
  1543. * Returns : none
  1544. *******************************************************************************/
  1545. void ICACHE_FLASH_ATTR
  1546. user_webserver_init(uint32 port)
  1547. {
  1548. LOCAL struct espconn esp_conn;
  1549. LOCAL esp_tcp esptcp;
  1550. esp_conn.type = ESPCONN_TCP;
  1551. esp_conn.state = ESPCONN_NONE;
  1552. esp_conn.proto.tcp = &esptcp;
  1553. esp_conn.proto.tcp->local_port = port;
  1554. espconn_regist_connectcb(&esp_conn, webserver_listen);
  1555. #ifdef SERVER_SSL_ENABLE
  1556. espconn_secure_set_default_certificate(default_certificate, default_certificate_len);
  1557. espconn_secure_set_default_private_key(default_private_key, default_private_key_len);
  1558. espconn_secure_accept(&esp_conn);
  1559. #else
  1560. espconn_accept(&esp_conn);
  1561. #endif
  1562. }