user_webserver.c 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284
  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_WEBSERVER
  22. #if ESP_PLATFORM
  23. #include "user_esp_platform.h"
  24. #endif
  25. #if LIGHT_DEVICE
  26. #include "user_light.h"
  27. #endif
  28. #if CH_AIRCON_DEVICE
  29. #include "ch_aircon.h"
  30. // #include "queue.h"
  31. // os_event_t aircon_RespondTaskQueue[aircon_RespondTaskQueueLen];
  32. #endif
  33. LOCAL struct station_config *sta_conf;
  34. LOCAL struct softap_config *ap_conf;
  35. //LOCAL struct secrty_server_info *sec_server;
  36. //LOCAL struct upgrade_server_info *server;
  37. //struct lewei_login_info *login_info;
  38. LOCAL scaninfo *pscaninfo;
  39. struct bss_info *bss;
  40. struct bss_info *bss_temp;
  41. struct bss_info *bss_head;
  42. extern u16 scannum;
  43. LOCAL uint32 PostCmdNeeRsp = 1;
  44. LOCAL struct espconn esp_webserv_conn;
  45. LOCAL esp_tcp esp_webserv_tcp;
  46. uint8 upgrade_lock = 0;
  47. LOCAL os_timer_t app_upgrade_10s;
  48. LOCAL os_timer_t upgrade_check_timer;
  49. LOCAL void response_send(void *arg, bool responseOK);
  50. #if CH_AIRCON_DEVICE
  51. LOCAL void ch_aircon_add_user_ip(uint8 *remoteip, uint16 remoteport);
  52. LOCAL uint8 current_user_ip[4];
  53. LOCAL uint16 current_user_port;
  54. LOCAL uint8 current_cmd_type = 0;
  55. #endif
  56. #if EP_PRJ_LIGHT_PWM
  57. extern void pwldev_change_light_config();
  58. #endif
  59. /******************************************************************************
  60. * FunctionName : device_get
  61. * Description : set up the device information parmer as a JSON format
  62. * Parameters : js_ctx -- A pointer to a JSON set up
  63. * Returns : result
  64. *******************************************************************************/
  65. LOCAL int ICACHE_FLASH_ATTR
  66. device_get(struct jsontree_context *js_ctx)
  67. {
  68. const char *path = jsontree_path_name(js_ctx, js_ctx->depth - 1);
  69. if (os_strncmp(path, "manufacture", 11) == 0) {
  70. jsontree_write_string(js_ctx, "Espressif Systems");
  71. } else if (os_strncmp(path, "product", 7) == 0) {
  72. #if SENSOR_DEVICE
  73. #if HUMITURE_SUB_DEVICE
  74. jsontree_write_string(js_ctx, "Humiture");
  75. #elif FLAMMABLE_GAS_SUB_DEVICE
  76. jsontree_write_string(js_ctx, "Flammable Gas");
  77. #endif
  78. #endif
  79. #if PLUG_DEVICE
  80. jsontree_write_string(js_ctx, "Plug");
  81. #endif
  82. #if LIGHT_DEVICE
  83. jsontree_write_string(js_ctx, "Light");
  84. #endif
  85. #if CH_AIRCON_DEVICE
  86. // jsontree_write_string(js_ctx, "Aircon");
  87. jsontree_write_string(js_ctx, "Plug");
  88. #endif
  89. #if EP_PRJ_LIGHT_PWM
  90. jsontree_write_string(js_ctx, "Light");
  91. #endif
  92. }
  93. return 0;
  94. }
  95. LOCAL struct jsontree_callback device_callback =
  96. JSONTREE_CALLBACK(device_get, NULL);
  97. /******************************************************************************
  98. * FunctionName : userbin_get
  99. * Description : get up the user bin paramer as a JSON format
  100. * Parameters : js_ctx -- A pointer to a JSON set up
  101. * Returns : result
  102. *******************************************************************************/
  103. LOCAL int ICACHE_FLASH_ATTR
  104. userbin_get(struct jsontree_context *js_ctx)
  105. {
  106. const char *path = jsontree_path_name(js_ctx, js_ctx->depth - 1);
  107. char string[32];
  108. if (os_strncmp(path, "status", 8) == 0) {
  109. os_sprintf(string, "200");
  110. } else if (os_strncmp(path, "user_bin", 8) == 0) {
  111. if (system_upgrade_userbin_check() == 0x00) {
  112. os_sprintf(string, "user1.bin");
  113. } else if (system_upgrade_userbin_check() == 0x01) {
  114. os_sprintf(string, "user2.bin");
  115. } else{
  116. return 0;
  117. }
  118. }
  119. jsontree_write_string(js_ctx, string);
  120. return 0;
  121. }
  122. LOCAL struct jsontree_callback userbin_callback =
  123. JSONTREE_CALLBACK(userbin_get, NULL);
  124. JSONTREE_OBJECT(userbin_tree,
  125. JSONTREE_PAIR("status", &userbin_callback),
  126. JSONTREE_PAIR("user_bin", &userbin_callback));
  127. JSONTREE_OBJECT(userinfo_tree,JSONTREE_PAIR("user_info",&userbin_tree));
  128. /******************************************************************************
  129. * FunctionName : version_get
  130. * Description : set up the device version paramer as a JSON format
  131. * Parameters : js_ctx -- A pointer to a JSON set up
  132. * Returns : result
  133. *******************************************************************************/
  134. LOCAL int ICACHE_FLASH_ATTR
  135. version_get(struct jsontree_context *js_ctx)
  136. {
  137. const char *path = jsontree_path_name(js_ctx, js_ctx->depth - 1);
  138. char string[32];
  139. if (os_strncmp(path, "hardware", 8) == 0) {
  140. #if SENSOR_DEVICE
  141. os_sprintf(string, "0.3");
  142. #else
  143. os_sprintf(string, "0.1");
  144. #endif
  145. } else if (os_strncmp(path, "sdk_version", 11) == 0) {
  146. os_sprintf(string, "%s", system_get_sdk_version());
  147. #if (ESP_PLATFORM) /* added macro ESP_PLATFORM */
  148. } else if (os_strncmp(path, "iot_version", 11) == 0) {
  149. os_sprintf(string,"%s%d.%d.%dt%d(%s)",VERSION_TYPE,IOT_VERSION_MAJOR,\
  150. IOT_VERSION_MINOR,IOT_VERSION_REVISION,device_type,UPGRADE_FALG);
  151. #endif /* ESP_PLATFORM */
  152. }
  153. jsontree_write_string(js_ctx, string);
  154. return 0;
  155. }
  156. LOCAL struct jsontree_callback version_callback =
  157. JSONTREE_CALLBACK(version_get, NULL);
  158. JSONTREE_OBJECT(device_tree,
  159. JSONTREE_PAIR("product", &device_callback),
  160. JSONTREE_PAIR("manufacturer", &device_callback));
  161. JSONTREE_OBJECT(version_tree,
  162. JSONTREE_PAIR("hardware", &version_callback),
  163. JSONTREE_PAIR("sdk_version", &version_callback),
  164. JSONTREE_PAIR("iot_version", &version_callback),
  165. );
  166. JSONTREE_OBJECT(info_tree,
  167. JSONTREE_PAIR("Version", &version_tree),
  168. JSONTREE_PAIR("Device", &device_tree));
  169. JSONTREE_OBJECT(INFOTree,
  170. JSONTREE_PAIR("info", &info_tree));
  171. #if ESP_PLATFORM
  172. LOCAL int ICACHE_FLASH_ATTR
  173. connect_status_get(struct jsontree_context *js_ctx)
  174. {
  175. const char *path = jsontree_path_name(js_ctx, js_ctx->depth - 1);
  176. if (os_strncmp(path, "status", 8) == 0) {
  177. jsontree_write_int(js_ctx, user_esp_platform_get_connect_status());
  178. }
  179. return 0;
  180. }
  181. LOCAL struct jsontree_callback connect_status_callback =
  182. JSONTREE_CALLBACK(connect_status_get, NULL);
  183. JSONTREE_OBJECT(status_sub_tree,
  184. JSONTREE_PAIR("status", &connect_status_callback));
  185. JSONTREE_OBJECT(connect_status_tree,
  186. JSONTREE_PAIR("Status", &status_sub_tree));
  187. JSONTREE_OBJECT(con_status_tree,
  188. JSONTREE_PAIR("info", &connect_status_tree));
  189. #endif
  190. #if PLUG_DEVICE
  191. /******************************************************************************
  192. * FunctionName : status_get
  193. * Description : set up the device status as a JSON format
  194. * Parameters : js_ctx -- A pointer to a JSON set up
  195. * Returns : result
  196. *******************************************************************************/
  197. LOCAL int ICACHE_FLASH_ATTR
  198. status_get(struct jsontree_context *js_ctx)
  199. {
  200. if (user_plug_get_status() == 1) {
  201. jsontree_write_int(js_ctx, 1);
  202. } else {
  203. jsontree_write_int(js_ctx, 0);
  204. }
  205. return 0;
  206. }
  207. /******************************************************************************
  208. * FunctionName : status_set
  209. * Description : parse the device status parmer as a JSON format
  210. * Parameters : js_ctx -- A pointer to a JSON set up
  211. * parser -- A pointer to a JSON parser state
  212. * Returns : result
  213. *******************************************************************************/
  214. LOCAL int ICACHE_FLASH_ATTR
  215. status_set(struct jsontree_context *js_ctx, struct jsonparse_state *parser)
  216. {
  217. int type;
  218. while ((type = jsonparse_next(parser)) != 0) {
  219. if (type == JSON_TYPE_PAIR_NAME) {
  220. if (jsonparse_strcmp_value(parser, "status") == 0) {
  221. uint8 status;
  222. jsonparse_next(parser);
  223. jsonparse_next(parser);
  224. status = jsonparse_get_value_as_int(parser);
  225. user_plug_set_status(status);
  226. }
  227. }
  228. }
  229. return 0;
  230. }
  231. LOCAL struct jsontree_callback status_callback =
  232. JSONTREE_CALLBACK(status_get, status_set);
  233. JSONTREE_OBJECT(status_tree,
  234. JSONTREE_PAIR("status", &status_callback));
  235. JSONTREE_OBJECT(response_tree,
  236. JSONTREE_PAIR("Response", &status_tree));
  237. JSONTREE_OBJECT(StatusTree,
  238. JSONTREE_PAIR("switch", &response_tree));
  239. #endif
  240. #if LIGHT_DEVICE
  241. LOCAL int ICACHE_FLASH_ATTR
  242. light_status_get(struct jsontree_context *js_ctx)
  243. {
  244. const char *path = jsontree_path_name(js_ctx, js_ctx->depth - 1);
  245. if (os_strncmp(path, "red", 3) == 0) {
  246. jsontree_write_int(js_ctx, user_light_get_duty(LIGHT_RED));
  247. } else if (os_strncmp(path, "green", 5) == 0) {
  248. jsontree_write_int(js_ctx, user_light_get_duty(LIGHT_GREEN));
  249. } else if (os_strncmp(path, "blue", 4) == 0) {
  250. jsontree_write_int(js_ctx, user_light_get_duty(LIGHT_BLUE));
  251. } else if (os_strncmp(path, "wwhite", 6) == 0) {
  252. if(PWM_CHANNEL>LIGHT_WARM_WHITE){
  253. jsontree_write_int(js_ctx, user_light_get_duty(LIGHT_WARM_WHITE));
  254. }else{
  255. jsontree_write_int(js_ctx, 0);
  256. }
  257. } else if (os_strncmp(path, "cwhite", 6) == 0) {
  258. if(PWM_CHANNEL>LIGHT_COLD_WHITE){
  259. jsontree_write_int(js_ctx, user_light_get_duty(LIGHT_COLD_WHITE));
  260. }else{
  261. jsontree_write_int(js_ctx, 0);
  262. }
  263. } else if (os_strncmp(path, "period", 6) == 0) {
  264. jsontree_write_int(js_ctx, user_light_get_period());
  265. }
  266. return 0;
  267. }
  268. LOCAL int ICACHE_FLASH_ATTR
  269. light_status_set(struct jsontree_context *js_ctx, struct jsonparse_state *parser)
  270. {
  271. int type;
  272. static uint32 r,g,b,cw,ww,period;
  273. period = 1000;
  274. cw=0;
  275. ww=0;
  276. extern uint8 light_sleep_flg;
  277. while ((type = jsonparse_next(parser)) != 0) {
  278. if (type == JSON_TYPE_PAIR_NAME) {
  279. if (jsonparse_strcmp_value(parser, "red") == 0) {
  280. uint32 status;
  281. jsonparse_next(parser);
  282. jsonparse_next(parser);
  283. status = jsonparse_get_value_as_int(parser);
  284. r=status;
  285. os_printf("R: %d \n",status);
  286. //user_light_set_duty(status, LIGHT_RED);
  287. //light_set_aim_r( r);
  288. } else if (jsonparse_strcmp_value(parser, "green") == 0) {
  289. uint32 status;
  290. jsonparse_next(parser);
  291. jsonparse_next(parser);
  292. status = jsonparse_get_value_as_int(parser);
  293. g=status;
  294. os_printf("G: %d \n",status);
  295. //user_light_set_duty(status, LIGHT_GREEN);
  296. //light_set_aim_g( g);
  297. } else if (jsonparse_strcmp_value(parser, "blue") == 0) {
  298. uint32 status;
  299. jsonparse_next(parser);
  300. jsonparse_next(parser);
  301. status = jsonparse_get_value_as_int(parser);
  302. b=status;
  303. os_printf("B: %d \n",status);
  304. //user_light_set_duty(status, LIGHT_BLUE);
  305. //set_aim_b( b);
  306. } else if (jsonparse_strcmp_value(parser, "cwhite") == 0) {
  307. uint32 status;
  308. jsonparse_next(parser);
  309. jsonparse_next(parser);
  310. status = jsonparse_get_value_as_int(parser);
  311. cw=status;
  312. os_printf("CW: %d \n",status);
  313. //user_light_set_duty(status, LIGHT_BLUE);
  314. //set_aim_b( b);
  315. } else if (jsonparse_strcmp_value(parser, "wwhite") == 0) {
  316. uint32 status;
  317. jsonparse_next(parser);
  318. jsonparse_next(parser);
  319. status = jsonparse_get_value_as_int(parser);
  320. ww=status;
  321. os_printf("WW: %d \n",status);
  322. //user_light_set_duty(status, LIGHT_BLUE);
  323. //set_aim_b( b);
  324. } else if (jsonparse_strcmp_value(parser, "period") == 0) {
  325. uint32 status;
  326. jsonparse_next(parser);
  327. jsonparse_next(parser);
  328. status = jsonparse_get_value_as_int(parser);
  329. os_printf("PERIOD: %d \n",status);
  330. period=status;
  331. //user_light_set_period(status);
  332. }else if (jsonparse_strcmp_value(parser, "response") == 0) {
  333. uint32 status;
  334. jsonparse_next(parser);
  335. jsonparse_next(parser);
  336. status = jsonparse_get_value_as_int(parser);
  337. os_printf("rspneed: %d \n",status);
  338. PostCmdNeeRsp = status;
  339. }
  340. }
  341. }
  342. if((r|g|b|ww|cw) == 0){
  343. if(light_sleep_flg==0){
  344. }
  345. }else{
  346. if(light_sleep_flg==1){
  347. os_printf("modem sleep en\r\n");
  348. wifi_set_sleep_type(MODEM_SLEEP_T);
  349. light_sleep_flg =0;
  350. }
  351. }
  352. light_set_aim(r,g,b,cw,ww,period);
  353. return 0;
  354. }
  355. LOCAL struct jsontree_callback light_callback =
  356. JSONTREE_CALLBACK(light_status_get, light_status_set);
  357. JSONTREE_OBJECT(rgb_tree,
  358. JSONTREE_PAIR("red", &light_callback),
  359. JSONTREE_PAIR("green", &light_callback),
  360. JSONTREE_PAIR("blue", &light_callback),
  361. JSONTREE_PAIR("cwhite", &light_callback),
  362. JSONTREE_PAIR("wwhite", &light_callback),
  363. );
  364. JSONTREE_OBJECT(sta_tree,
  365. JSONTREE_PAIR("period", &light_callback),
  366. JSONTREE_PAIR("rgb", &rgb_tree));
  367. JSONTREE_OBJECT(PwmTree,
  368. JSONTREE_PAIR("light", &sta_tree));
  369. #endif
  370. #if CH_AIRCON_DEVICE
  371. LOCAL void ICACHE_FLASH_ATTR
  372. ch_aircon_add_user_ip(uint8 *remoteip, uint16 remoteport)
  373. {
  374. os_memcpy(current_user_ip, remoteip, 4);
  375. current_user_port = remoteport;
  376. }
  377. LOCAL int ICACHE_FLASH_ATTR
  378. aircon_status_get(struct jsontree_context *js_ctx)
  379. {
  380. const char *path = jsontree_path_name(js_ctx, js_ctx->depth - 1);
  381. if (os_strncmp(path, "onoff", 5) == 0) {
  382. jsontree_write_int(js_ctx, ch_aircon_get_cfg_onoff());
  383. } else if (os_strncmp(path, "fanspeed", 8) == 0) {
  384. jsontree_write_int(js_ctx, ch_aircon_get_cfg_fanspeed());
  385. } else if (os_strncmp(path, "temperature", 11) == 0) {
  386. jsontree_write_int(js_ctx, ch_aircon_get_cfg_temperature());
  387. } else if (os_strncmp(path, "light", 5) == 0) {
  388. jsontree_write_int(js_ctx, ch_aircon_get_cfg_light());
  389. }
  390. return 0;
  391. }
  392. LOCAL int ICACHE_FLASH_ATTR
  393. aircon_status_set(struct jsontree_context *js_ctx, struct jsonparse_state *parser)
  394. {
  395. int type;
  396. uint32 status;
  397. while ((type = jsonparse_next(parser)) != 0) {
  398. if (type == JSON_TYPE_PAIR_NAME) {
  399. if (jsonparse_strcmp_value(parser, "onoff") == 0) {
  400. jsonparse_next(parser);
  401. jsonparse_next(parser);
  402. status = jsonparse_get_value_as_int(parser);
  403. ch_aircon_set_cfg_onoff(status);
  404. os_printf("on: %d \n",status);
  405. } else if (jsonparse_strcmp_value(parser, "fanspeed") == 0) {
  406. jsonparse_next(parser);
  407. jsonparse_next(parser);
  408. status = jsonparse_get_value_as_int(parser);
  409. ch_aircon_set_cfg_fanspeed(status);
  410. os_printf("fs: %d \n",status);
  411. } else if (jsonparse_strcmp_value(parser, "temperature") == 0) {
  412. jsonparse_next(parser);
  413. jsonparse_next(parser);
  414. status = jsonparse_get_value_as_int(parser);
  415. ch_aircon_set_cfg_temperature(status);
  416. os_printf("tm: %d \n",status);
  417. } else if (jsonparse_strcmp_value(parser, "light") == 0) {
  418. jsonparse_next(parser);
  419. jsonparse_next(parser);
  420. status = jsonparse_get_value_as_int(parser);
  421. ch_aircon_set_cfg_light(status);
  422. os_printf("li: %d \n",status);
  423. }
  424. }
  425. }
  426. // ch_aircon_set_config(chac_on, chac_fanspeed, chac_thigh, chac_tlow, chac_light);
  427. return 0;
  428. }
  429. LOCAL struct jsontree_callback aircon_callback =
  430. JSONTREE_CALLBACK(aircon_status_get, aircon_status_set);
  431. JSONTREE_OBJECT(acon_cfg_set,
  432. JSONTREE_PAIR("onoff", &aircon_callback),
  433. JSONTREE_PAIR("fanspeed", &aircon_callback),
  434. JSONTREE_PAIR("temperature", &aircon_callback),
  435. JSONTREE_PAIR("light", &aircon_callback)
  436. );
  437. JSONTREE_OBJECT(acon_stat_get,
  438. JSONTREE_PAIR("onoff", &aircon_callback),
  439. JSONTREE_PAIR("fanspeed", &aircon_callback),
  440. JSONTREE_PAIR("temperature", &aircon_callback),
  441. JSONTREE_PAIR("light", &aircon_callback)
  442. );
  443. JSONTREE_OBJECT(get_aircon_tree,
  444. JSONTREE_PAIR("aircon", &acon_stat_get));
  445. JSONTREE_OBJECT(set_aircon_tree,
  446. JSONTREE_PAIR("aircon", &acon_cfg_set));
  447. #endif
  448. #if EP_PRJ_LIGHT_PWM
  449. LOCAL int ICACHE_FLASH_ATTR
  450. pwl_status_get(struct jsontree_context *js_ctx)
  451. {
  452. const char *path = jsontree_path_name(js_ctx, js_ctx->depth - 1);
  453. if (os_strncmp(path, "onoff", 5) == 0) {
  454. jsontree_write_int(js_ctx, pwldev_get_cfg_onoff());
  455. } else if (os_strncmp(path, "bright", 6) == 0) {
  456. jsontree_write_int(js_ctx, pwldev_get_cfg_light());
  457. } else if (os_strncmp(path, "fbstat", 6) == 0) {
  458. jsontree_write_int(js_ctx, pwldev_get_cfg_fb());
  459. } else if (os_strncmp(path, "stoptick", 8) == 0) {
  460. jsontree_write_int(js_ctx, pwldev_get_cfg_stoptick());
  461. }
  462. return 0;
  463. }
  464. LOCAL int ICACHE_FLASH_ATTR
  465. pwl_status_set(struct jsontree_context *js_ctx, struct jsonparse_state *parser)
  466. {
  467. int type;
  468. uint32 status;
  469. while ((type = jsonparse_next(parser)) != 0) {
  470. if (type == JSON_TYPE_PAIR_NAME) {
  471. if (jsonparse_strcmp_value(parser, "onoff") == 0) {
  472. jsonparse_next(parser);
  473. jsonparse_next(parser);
  474. status = jsonparse_get_value_as_int(parser);
  475. pwldev_set_cfg_onoff(status);
  476. os_printf("on: %d \n",status);
  477. } else if (jsonparse_strcmp_value(parser, "bright") == 0) {
  478. jsonparse_next(parser);
  479. jsonparse_next(parser);
  480. status = jsonparse_get_value_as_int(parser);
  481. pwldev_set_cfg_light(status);
  482. os_printf("li: %d \n",status);
  483. } else if (jsonparse_strcmp_value(parser, "reset") == 0) {
  484. jsonparse_next(parser);
  485. jsonparse_next(parser);
  486. status = jsonparse_get_value_as_int(parser);
  487. pwldev_set_cfg_reset(status);
  488. os_printf("rst: %d \n",status);
  489. } else if (jsonparse_strcmp_value(parser, "stoptick") == 0) {
  490. jsonparse_next(parser);
  491. jsonparse_next(parser);
  492. status = jsonparse_get_value_as_int(parser);
  493. pwldev_set_cfg_stoptick(status);
  494. os_printf("rst: %d \n",status);
  495. }
  496. }
  497. }
  498. pwldev_change_light_config();
  499. return 0;
  500. }
  501. LOCAL struct jsontree_callback pwl_callback =
  502. JSONTREE_CALLBACK(pwl_status_get, pwl_status_set);
  503. JSONTREE_OBJECT(pwl_cfg_set,
  504. JSONTREE_PAIR("reset", &pwl_callback),
  505. JSONTREE_PAIR("stoptick", &pwl_callback),
  506. JSONTREE_PAIR("onoff", &pwl_callback),
  507. JSONTREE_PAIR("bright", &pwl_callback)
  508. );
  509. JSONTREE_OBJECT(pwl_stat_get,
  510. JSONTREE_PAIR("onoff", &pwl_callback),
  511. JSONTREE_PAIR("fbstat", &pwl_callback),
  512. JSONTREE_PAIR("bright", &pwl_callback)
  513. );
  514. JSONTREE_OBJECT(get_pwl_tree,
  515. JSONTREE_PAIR("light", &pwl_stat_get));
  516. JSONTREE_OBJECT(set_pwl_tree,
  517. JSONTREE_PAIR("light", &pwl_cfg_set));
  518. #endif
  519. /******************************************************************************
  520. * FunctionName : wifi_station_get
  521. * Description : set up the station paramer as a JSON format
  522. * Parameters : js_ctx -- A pointer to a JSON set up
  523. * Returns : result
  524. *******************************************************************************/
  525. LOCAL int ICACHE_FLASH_ATTR
  526. wifi_station_get(struct jsontree_context *js_ctx)
  527. {
  528. const char *path = jsontree_path_name(js_ctx, js_ctx->depth - 1);
  529. struct ip_info ipconfig;
  530. uint8 buf[20];
  531. os_bzero(buf, sizeof(buf));
  532. wifi_station_get_config(sta_conf);
  533. wifi_get_ip_info(STATION_IF, &ipconfig);
  534. if (os_strncmp(path, "ssid", 4) == 0) {
  535. jsontree_write_string(js_ctx, sta_conf->ssid);
  536. } else if (os_strncmp(path, "password", 8) == 0) {
  537. jsontree_write_string(js_ctx, sta_conf->password);
  538. } else if (os_strncmp(path, "ip", 2) == 0) {
  539. os_sprintf(buf, IPSTR, IP2STR(&ipconfig.ip));
  540. jsontree_write_string(js_ctx, buf);
  541. } else if (os_strncmp(path, "mask", 4) == 0) {
  542. os_sprintf(buf, IPSTR, IP2STR(&ipconfig.netmask));
  543. jsontree_write_string(js_ctx, buf);
  544. } else if (os_strncmp(path, "gw", 2) == 0) {
  545. os_sprintf(buf, IPSTR, IP2STR(&ipconfig.gw));
  546. jsontree_write_string(js_ctx, buf);
  547. }
  548. return 0;
  549. }
  550. /******************************************************************************
  551. * FunctionName : wifi_station_set
  552. * Description : parse the station parmer as a JSON format
  553. * Parameters : js_ctx -- A pointer to a JSON set up
  554. * parser -- A pointer to a JSON parser state
  555. * Returns : result
  556. *******************************************************************************/
  557. LOCAL int ICACHE_FLASH_ATTR
  558. wifi_station_set(struct jsontree_context *js_ctx, struct jsonparse_state *parser)
  559. {
  560. int type;
  561. uint8 station_tree;
  562. while ((type = jsonparse_next(parser)) != 0) {
  563. if (type == JSON_TYPE_PAIR_NAME) {
  564. char buffer[64];
  565. os_bzero(buffer, 64);
  566. if (jsonparse_strcmp_value(parser, "Station") == 0) {
  567. station_tree = 1;
  568. } else if (jsonparse_strcmp_value(parser, "Softap") == 0) {
  569. station_tree = 0;
  570. }
  571. if (station_tree) {
  572. if (jsonparse_strcmp_value(parser, "ssid") == 0) {
  573. jsonparse_next(parser);
  574. jsonparse_next(parser);
  575. jsonparse_copy_value(parser, buffer, sizeof(buffer));
  576. os_memcpy(sta_conf->ssid, buffer, os_strlen(buffer));
  577. } else if (jsonparse_strcmp_value(parser, "password") == 0) {
  578. jsonparse_next(parser);
  579. jsonparse_next(parser);
  580. jsonparse_copy_value(parser, buffer, sizeof(buffer));
  581. os_memcpy(sta_conf->password, buffer, os_strlen(buffer));
  582. }
  583. #if ESP_PLATFORM
  584. else if (jsonparse_strcmp_value(parser, "token") == 0) {
  585. jsonparse_next(parser);
  586. jsonparse_next(parser);
  587. jsonparse_copy_value(parser, buffer, sizeof(buffer));
  588. user_esp_platform_set_token(buffer);
  589. }
  590. #endif
  591. }
  592. }
  593. }
  594. return 0;
  595. }
  596. LOCAL struct jsontree_callback wifi_station_callback =
  597. JSONTREE_CALLBACK(wifi_station_get, wifi_station_set);
  598. JSONTREE_OBJECT(get_station_config_tree,
  599. JSONTREE_PAIR("ssid", &wifi_station_callback),
  600. JSONTREE_PAIR("password", &wifi_station_callback));
  601. JSONTREE_OBJECT(set_station_config_tree,
  602. JSONTREE_PAIR("ssid", &wifi_station_callback),
  603. JSONTREE_PAIR("password", &wifi_station_callback),
  604. JSONTREE_PAIR("token", &wifi_station_callback));
  605. JSONTREE_OBJECT(ip_tree,
  606. JSONTREE_PAIR("ip", &wifi_station_callback),
  607. JSONTREE_PAIR("mask", &wifi_station_callback),
  608. JSONTREE_PAIR("gw", &wifi_station_callback));
  609. JSONTREE_OBJECT(get_station_tree,
  610. JSONTREE_PAIR("Connect_Station", &get_station_config_tree),
  611. JSONTREE_PAIR("Ipinfo_Station", &ip_tree));
  612. JSONTREE_OBJECT(set_station_tree,
  613. JSONTREE_PAIR("Connect_Station", &set_station_config_tree));
  614. //JSONTREE_OBJECT(get_wifi_station_info_tree,
  615. // JSONTREE_PAIR("Station", &get_station_tree));
  616. //JSONTREE_OBJECT(set_wifi_station_info_tree,
  617. // JSONTREE_PAIR("station", &set_station_tree));
  618. /******************************************************************************
  619. * FunctionName : wifi_softap_get
  620. * Description : set up the softap paramer as a JSON format
  621. * Parameters : js_ctx -- A pointer to a JSON set up
  622. * Returns : result
  623. *******************************************************************************/
  624. LOCAL int ICACHE_FLASH_ATTR
  625. wifi_softap_get(struct jsontree_context *js_ctx)
  626. {
  627. const char *path = jsontree_path_name(js_ctx, js_ctx->depth - 1);
  628. struct ip_info ipconfig;
  629. uint8 buf[20];
  630. os_bzero(buf, sizeof(buf));
  631. wifi_softap_get_config(ap_conf);
  632. wifi_get_ip_info(SOFTAP_IF, &ipconfig);
  633. if (os_strncmp(path, "ssid", 4) == 0) {
  634. jsontree_write_string(js_ctx, ap_conf->ssid);
  635. } else if (os_strncmp(path, "password", 8) == 0) {
  636. jsontree_write_string(js_ctx, ap_conf->password);
  637. } else if (os_strncmp(path, "channel", 7) == 0) {
  638. jsontree_write_int(js_ctx, ap_conf->channel);
  639. } else if (os_strncmp(path, "authmode", 8) == 0) {
  640. switch (ap_conf->authmode) {
  641. case AUTH_OPEN:
  642. jsontree_write_string(js_ctx, "OPEN");
  643. break;
  644. case AUTH_WEP:
  645. jsontree_write_string(js_ctx, "WEP");
  646. break;
  647. case AUTH_WPA_PSK:
  648. jsontree_write_string(js_ctx, "WPAPSK");
  649. break;
  650. case AUTH_WPA2_PSK:
  651. jsontree_write_string(js_ctx, "WPA2PSK");
  652. break;
  653. case AUTH_WPA_WPA2_PSK:
  654. jsontree_write_string(js_ctx, "WPAPSK/WPA2PSK");
  655. break;
  656. default :
  657. jsontree_write_int(js_ctx, ap_conf->authmode);
  658. break;
  659. }
  660. } else if (os_strncmp(path, "ip", 2) == 0) {
  661. os_sprintf(buf, IPSTR, IP2STR(&ipconfig.ip));
  662. jsontree_write_string(js_ctx, buf);
  663. } else if (os_strncmp(path, "mask", 4) == 0) {
  664. os_sprintf(buf, IPSTR, IP2STR(&ipconfig.netmask));
  665. jsontree_write_string(js_ctx, buf);
  666. } else if (os_strncmp(path, "gw", 2) == 0) {
  667. os_sprintf(buf, IPSTR, IP2STR(&ipconfig.gw));
  668. jsontree_write_string(js_ctx, buf);
  669. }
  670. return 0;
  671. }
  672. /******************************************************************************
  673. * FunctionName : wifi_softap_set
  674. * Description : parse the softap parmer as a JSON format
  675. * Parameters : js_ctx -- A pointer to a JSON set up
  676. * parser -- A pointer to a JSON parser state
  677. * Returns : result
  678. *******************************************************************************/
  679. LOCAL int ICACHE_FLASH_ATTR
  680. wifi_softap_set(struct jsontree_context *js_ctx, struct jsonparse_state *parser)
  681. {
  682. int type;
  683. uint8 softap_tree;
  684. while ((type = jsonparse_next(parser)) != 0) {
  685. if (type == JSON_TYPE_PAIR_NAME) {
  686. char buffer[64];
  687. os_bzero(buffer, 64);
  688. if (jsonparse_strcmp_value(parser, "Station") == 0) {
  689. softap_tree = 0;
  690. } else if (jsonparse_strcmp_value(parser, "Softap") == 0) {
  691. softap_tree = 1;
  692. }
  693. if (softap_tree) {
  694. if (jsonparse_strcmp_value(parser, "authmode") == 0) {
  695. jsonparse_next(parser);
  696. jsonparse_next(parser);
  697. jsonparse_copy_value(parser, buffer, sizeof(buffer));
  698. // other mode will be supported later...
  699. if (os_strcmp(buffer, "OPEN") == 0) {
  700. ap_conf->authmode = AUTH_OPEN;
  701. } else if (os_strcmp(buffer, "WPAPSK") == 0) {
  702. ap_conf->authmode = AUTH_WPA_PSK;
  703. os_printf("%d %s\n", ap_conf->authmode, buffer);
  704. } else if (os_strcmp(buffer, "WPA2PSK") == 0) {
  705. ap_conf->authmode = AUTH_WPA2_PSK;
  706. } else if (os_strcmp(buffer, "WPAPSK/WPA2PSK") == 0) {
  707. ap_conf->authmode = AUTH_WPA_WPA2_PSK;
  708. } else {
  709. ap_conf->authmode = AUTH_OPEN;
  710. return 0;
  711. }
  712. }
  713. if (jsonparse_strcmp_value(parser, "channel") == 0) {
  714. jsonparse_next(parser);
  715. jsonparse_next(parser);
  716. ap_conf->channel = jsonparse_get_value_as_int(parser);
  717. } else if (jsonparse_strcmp_value(parser, "ssid") == 0) {
  718. jsonparse_next(parser);
  719. jsonparse_next(parser);
  720. jsonparse_copy_value(parser, buffer, sizeof(buffer));
  721. os_memcpy(ap_conf->ssid, buffer, os_strlen(buffer));
  722. } else if (jsonparse_strcmp_value(parser, "password") == 0) {
  723. jsonparse_next(parser);
  724. jsonparse_next(parser);
  725. jsonparse_copy_value(parser, buffer, sizeof(buffer));
  726. os_memcpy(ap_conf->password, buffer, os_strlen(buffer));
  727. }
  728. }
  729. }
  730. }
  731. return 0;
  732. }
  733. LOCAL struct jsontree_callback wifi_softap_callback =
  734. JSONTREE_CALLBACK(wifi_softap_get, wifi_softap_set);
  735. JSONTREE_OBJECT(softap_config_tree,
  736. JSONTREE_PAIR("authmode", &wifi_softap_callback),
  737. JSONTREE_PAIR("channel", &wifi_softap_callback),
  738. JSONTREE_PAIR("ssid", &wifi_softap_callback),
  739. JSONTREE_PAIR("password", &wifi_softap_callback));
  740. JSONTREE_OBJECT(softap_ip_tree,
  741. JSONTREE_PAIR("ip", &wifi_softap_callback),
  742. JSONTREE_PAIR("mask", &wifi_softap_callback),
  743. JSONTREE_PAIR("gw", &wifi_softap_callback));
  744. JSONTREE_OBJECT(get_softap_tree,
  745. JSONTREE_PAIR("Connect_Softap", &softap_config_tree),
  746. JSONTREE_PAIR("Ipinfo_Softap", &softap_ip_tree));
  747. JSONTREE_OBJECT(set_softap_tree,
  748. JSONTREE_PAIR("Ipinfo_Softap", &softap_config_tree));
  749. JSONTREE_OBJECT(get_wifi_tree,
  750. JSONTREE_PAIR("Station", &get_station_tree),
  751. JSONTREE_PAIR("Softap", &get_softap_tree));
  752. JSONTREE_OBJECT(set_wifi_tree,
  753. JSONTREE_PAIR("Station", &set_station_tree),
  754. JSONTREE_PAIR("Softap", &set_softap_tree));
  755. JSONTREE_OBJECT(wifi_response_tree,
  756. JSONTREE_PAIR("Response", &get_wifi_tree));
  757. JSONTREE_OBJECT(wifi_request_tree,
  758. JSONTREE_PAIR("Request", &set_wifi_tree));
  759. JSONTREE_OBJECT(wifi_info_tree,
  760. JSONTREE_PAIR("wifi", &wifi_response_tree));
  761. JSONTREE_OBJECT(wifi_req_tree,
  762. JSONTREE_PAIR("wifi", &wifi_request_tree));
  763. /******************************************************************************
  764. * FunctionName : scan_get
  765. * Description : set up the scan data as a JSON format
  766. * Parameters : js_ctx -- A pointer to a JSON set up
  767. * Returns : result
  768. *******************************************************************************/
  769. LOCAL int ICACHE_FLASH_ATTR
  770. scan_get(struct jsontree_context *js_ctx)
  771. {
  772. const char *path = jsontree_path_name(js_ctx, js_ctx->depth - 1);
  773. // STAILQ_HEAD(, bss_info) *pbss = scanarg;
  774. // LOCAL struct bss_info *bss;
  775. if (os_strncmp(path, "TotalPage", 9) == 0) {
  776. jsontree_write_int(js_ctx, pscaninfo->totalpage);
  777. } else if (os_strncmp(path, "PageNum", 7) == 0) {
  778. jsontree_write_int(js_ctx, pscaninfo->pagenum);
  779. } else if (os_strncmp(path, "bssid", 5) == 0) {
  780. if( bss == NULL )
  781. bss = bss_head;
  782. u8 buffer[32];
  783. //if (bss != NULL){
  784. os_memset(buffer, 0, sizeof(buffer));
  785. os_sprintf(buffer, MACSTR, MAC2STR(bss->bssid));
  786. jsontree_write_string(js_ctx, buffer);
  787. //}
  788. } else if (os_strncmp(path, "ssid", 4) == 0) {
  789. //if (bss != NULL)
  790. jsontree_write_string(js_ctx, bss->ssid);
  791. } else if (os_strncmp(path, "rssi", 4) == 0) {
  792. //if (bss != NULL)
  793. jsontree_write_int(js_ctx, -(bss->rssi));
  794. } else if (os_strncmp(path, "channel", 7) == 0) {
  795. //if (bss != NULL)
  796. jsontree_write_int(js_ctx, bss->channel);
  797. } else if (os_strncmp(path, "authmode", 8) == 0) {
  798. //if (bss != NULL){
  799. switch (bss->authmode) {
  800. case AUTH_OPEN:
  801. jsontree_write_string(js_ctx, "OPEN");
  802. break;
  803. case AUTH_WEP:
  804. jsontree_write_string(js_ctx, "WEP");
  805. break;
  806. case AUTH_WPA_PSK:
  807. jsontree_write_string(js_ctx, "WPAPSK");
  808. break;
  809. case AUTH_WPA2_PSK:
  810. jsontree_write_string(js_ctx, "WPA2PSK");
  811. break;
  812. case AUTH_WPA_WPA2_PSK:
  813. jsontree_write_string(js_ctx, "WPAPSK/WPA2PSK");
  814. break;
  815. default :
  816. jsontree_write_int(js_ctx, bss->authmode);
  817. break;
  818. }
  819. bss = STAILQ_NEXT(bss, next);
  820. // os_free(bss);
  821. //}
  822. }
  823. return 0;
  824. }
  825. LOCAL struct jsontree_callback scan_callback =
  826. JSONTREE_CALLBACK(scan_get, NULL);
  827. JSONTREE_OBJECT(scaninfo_tree,
  828. JSONTREE_PAIR("bssid", &scan_callback),
  829. JSONTREE_PAIR("ssid", &scan_callback),
  830. JSONTREE_PAIR("rssi", &scan_callback),
  831. JSONTREE_PAIR("channel", &scan_callback),
  832. JSONTREE_PAIR("authmode", &scan_callback));
  833. JSONTREE_ARRAY(scanrslt_tree,
  834. JSONTREE_PAIR_ARRAY(&scaninfo_tree),
  835. JSONTREE_PAIR_ARRAY(&scaninfo_tree),
  836. JSONTREE_PAIR_ARRAY(&scaninfo_tree),
  837. JSONTREE_PAIR_ARRAY(&scaninfo_tree),
  838. JSONTREE_PAIR_ARRAY(&scaninfo_tree),
  839. JSONTREE_PAIR_ARRAY(&scaninfo_tree),
  840. JSONTREE_PAIR_ARRAY(&scaninfo_tree),
  841. JSONTREE_PAIR_ARRAY(&scaninfo_tree));
  842. JSONTREE_OBJECT(scantree,
  843. JSONTREE_PAIR("TotalPage", &scan_callback),
  844. JSONTREE_PAIR("PageNum", &scan_callback),
  845. JSONTREE_PAIR("ScanResult", &scanrslt_tree));
  846. JSONTREE_OBJECT(scanres_tree,
  847. JSONTREE_PAIR("Response", &scantree));
  848. JSONTREE_OBJECT(scan_tree,
  849. JSONTREE_PAIR("scan", &scanres_tree));
  850. /******************************************************************************
  851. * FunctionName : parse_url
  852. * Description : parse the received data from the server
  853. * Parameters : precv -- the received data
  854. * purl_frame -- the result of parsing the url
  855. * Returns : none
  856. *******************************************************************************/
  857. LOCAL void ICACHE_FLASH_ATTR
  858. parse_url(char *precv, URL_Frame *purl_frame)
  859. {
  860. char *str = NULL;
  861. uint8 length = 0;
  862. char *pbuffer = NULL;
  863. char *pbufer = NULL;
  864. if (purl_frame == NULL || precv == NULL) {
  865. return;
  866. }
  867. pbuffer = (char *)os_strstr(precv, "Host:");
  868. if (pbuffer != NULL) {
  869. length = pbuffer - precv;
  870. pbufer = (char *)os_zalloc(length + 1);
  871. pbuffer = pbufer;
  872. os_memcpy(pbuffer, precv, length);
  873. os_memset(purl_frame->pSelect, 0, URLSize);
  874. os_memset(purl_frame->pCommand, 0, URLSize);
  875. os_memset(purl_frame->pFilename, 0, URLSize);
  876. if (os_strncmp(pbuffer, "GET ", 4) == 0) {
  877. purl_frame->Type = GET;
  878. pbuffer += 4;
  879. } else if (os_strncmp(pbuffer, "POST ", 5) == 0) {
  880. purl_frame->Type = POST;
  881. pbuffer += 5;
  882. }
  883. pbuffer ++;
  884. str = (char *)os_strstr(pbuffer, "?");
  885. if (str != NULL) {
  886. length = str - pbuffer;
  887. os_memcpy(purl_frame->pSelect, pbuffer, length);
  888. str ++;
  889. pbuffer = (char *)os_strstr(str, "=");
  890. if (pbuffer != NULL) {
  891. length = pbuffer - str;
  892. os_memcpy(purl_frame->pCommand, str, length);
  893. pbuffer ++;
  894. str = (char *)os_strstr(pbuffer, "&");
  895. if (str != NULL) {
  896. length = str - pbuffer;
  897. os_memcpy(purl_frame->pFilename, pbuffer, length);
  898. } else {
  899. str = (char *)os_strstr(pbuffer, " HTTP");
  900. if (str != NULL) {
  901. length = str - pbuffer;
  902. os_memcpy(purl_frame->pFilename, pbuffer, length);
  903. }
  904. }
  905. }
  906. }
  907. os_free(pbufer);
  908. } else {
  909. return;
  910. }
  911. }
  912. LOCAL char *precvbuffer;
  913. static uint32 dat_sumlength = 0;
  914. LOCAL bool ICACHE_FLASH_ATTR
  915. save_data(char *precv, uint16 length)
  916. {
  917. // bool flag = false;
  918. char length_buf[10] = {0};
  919. char *ptemp = NULL;
  920. char *pdata = NULL;
  921. uint16 headlength = 0;
  922. static uint32 totallength = 0;
  923. ptemp = (char *)os_strstr(precv, "\r\n\r\n");
  924. if (ptemp != NULL) {
  925. length -= ptemp - precv;
  926. length -= 4;
  927. totallength += length;
  928. headlength = ptemp - precv + 4;
  929. pdata = (char *)os_strstr(precv, "Content-Length: ");
  930. if (pdata != NULL) {
  931. pdata += 16;
  932. precvbuffer = (char *)os_strstr(pdata, "\r\n");
  933. if (precvbuffer != NULL) {
  934. os_memcpy(length_buf, pdata, precvbuffer - pdata);
  935. dat_sumlength = atoi(length_buf);
  936. }
  937. } else {
  938. if (totallength != 0x00){
  939. totallength = 0;
  940. dat_sumlength = 0;
  941. return false;
  942. }
  943. }
  944. if ((dat_sumlength + headlength) >= 1024) {
  945. precvbuffer = (char *)os_zalloc(headlength + 1);
  946. os_memcpy(precvbuffer, precv, headlength + 1);
  947. } else {
  948. precvbuffer = (char *)os_zalloc(dat_sumlength + headlength + 1);
  949. os_memcpy(precvbuffer, precv, os_strlen(precv));
  950. }
  951. } else {
  952. if (precvbuffer != NULL) {
  953. totallength += length;
  954. os_memcpy(precvbuffer + os_strlen(precvbuffer), precv, length);
  955. } else {
  956. totallength = 0;
  957. dat_sumlength = 0;
  958. return false;
  959. }
  960. }
  961. if (totallength == dat_sumlength) {
  962. totallength = 0;
  963. dat_sumlength = 0;
  964. return true;
  965. } else {
  966. return false;
  967. }
  968. }
  969. LOCAL bool ICACHE_FLASH_ATTR
  970. check_data(char *precv, uint16 length)
  971. {
  972. //bool flag = true;
  973. char length_buf[10] = {0};
  974. char *ptemp = NULL;
  975. char *pdata = NULL;
  976. char *tmp_precvbuffer;
  977. uint16 tmp_length = length;
  978. uint32 tmp_totallength = 0;
  979. ptemp = (char *)os_strstr(precv, "\r\n\r\n");
  980. if (ptemp != NULL) {
  981. tmp_length -= ptemp - precv;
  982. tmp_length -= 4;
  983. tmp_totallength += tmp_length;
  984. pdata = (char *)os_strstr(precv, "Content-Length: ");
  985. if (pdata != NULL){
  986. pdata += 16;
  987. tmp_precvbuffer = (char *)os_strstr(pdata, "\r\n");
  988. if (tmp_precvbuffer != NULL){
  989. os_memcpy(length_buf, pdata, tmp_precvbuffer - pdata);
  990. dat_sumlength = atoi(length_buf);
  991. os_printf("A_dat:%u,tot:%u,lenght:%u\n",dat_sumlength,tmp_totallength,tmp_length);
  992. if(dat_sumlength != tmp_totallength){
  993. return false;
  994. }
  995. }
  996. }
  997. }
  998. return true;
  999. }
  1000. LOCAL os_timer_t *restart_10ms;
  1001. LOCAL rst_parm *rstparm;
  1002. /******************************************************************************
  1003. * FunctionName : restart_10ms_cb
  1004. * Description : system restart or wifi reconnected after a certain time.
  1005. * Parameters : arg -- Additional argument to pass to the function
  1006. * Returns : none
  1007. *******************************************************************************/
  1008. LOCAL void ICACHE_FLASH_ATTR
  1009. restart_10ms_cb(void *arg)
  1010. {
  1011. if (rstparm != NULL && rstparm->pespconn != NULL) {
  1012. switch (rstparm->parmtype) {
  1013. case WIFI:
  1014. //if (rstparm->pespconn->state == ESPCONN_CLOSE) {
  1015. if (sta_conf->ssid[0] != 0x00) {
  1016. wifi_station_set_config(sta_conf);
  1017. wifi_station_disconnect();
  1018. wifi_station_connect();
  1019. #if ESP_PLATFORM
  1020. user_esp_platform_check_ip(1);
  1021. #endif
  1022. }
  1023. if (ap_conf->ssid[0] != 0x00) {
  1024. wifi_softap_set_config(ap_conf);
  1025. system_restart();
  1026. }
  1027. os_free(ap_conf);
  1028. ap_conf = NULL;
  1029. os_free(sta_conf);
  1030. sta_conf = NULL;
  1031. os_free(rstparm);
  1032. rstparm = NULL;
  1033. os_free(restart_10ms);
  1034. restart_10ms = NULL;
  1035. //} else {
  1036. // os_timer_arm(restart_10ms, 10, 0);
  1037. //}
  1038. break;
  1039. case DEEP_SLEEP:
  1040. case REBOOT:
  1041. if (rstparm->pespconn->state == ESPCONN_CLOSE) {
  1042. wifi_set_opmode(STATION_MODE);
  1043. if (rstparm->parmtype == DEEP_SLEEP) {
  1044. #if SENSOR_DEVICE
  1045. system_deep_sleep(SENSOR_DEEP_SLEEP_TIME);
  1046. #endif
  1047. }
  1048. } else {
  1049. os_timer_arm(restart_10ms, 10, 0);
  1050. }
  1051. break;
  1052. default:
  1053. break;
  1054. }
  1055. }
  1056. }
  1057. /******************************************************************************
  1058. * FunctionName : data_send
  1059. * Description : processing the data as http format and send to the client or server
  1060. * Parameters : arg -- argument to set for client or server
  1061. * responseOK -- true or false
  1062. * psend -- The send data
  1063. * Returns :
  1064. *******************************************************************************/
  1065. LOCAL void ICACHE_FLASH_ATTR
  1066. data_send(void *arg, bool responseOK, char *psend)
  1067. {
  1068. uint16 length = 0;
  1069. char *pbuf = NULL;
  1070. char httphead[256];
  1071. struct espconn *ptrespconn = arg;
  1072. os_memset(httphead, 0, 256);
  1073. if (responseOK) {
  1074. os_sprintf(httphead,
  1075. "HTTP/1.0 200 OK\r\nContent-Length: %d\r\nServer: lwIP/1.4.0\r\n",
  1076. psend ? os_strlen(psend) : 0);
  1077. if (psend) {
  1078. os_sprintf(httphead + os_strlen(httphead),
  1079. "Content-type: application/json\r\nExpires: Fri, 10 Apr 2008 14:00:00 GMT\r\nPragma: no-cache\r\n\r\n");
  1080. length = os_strlen(httphead) + os_strlen(psend);
  1081. pbuf = (char *)os_zalloc(length + 1);
  1082. os_memcpy(pbuf, httphead, os_strlen(httphead));
  1083. os_memcpy(pbuf + os_strlen(httphead), psend, os_strlen(psend));
  1084. } else {
  1085. os_sprintf(httphead + os_strlen(httphead), "\n");
  1086. length = os_strlen(httphead);
  1087. }
  1088. } else {
  1089. os_sprintf(httphead, "HTTP/1.0 400 BadRequest\r\n\
  1090. Content-Length: 0\r\nServer: lwIP/1.4.0\r\n\n");
  1091. length = os_strlen(httphead);
  1092. }
  1093. if (psend) {
  1094. #ifdef SERVER_SSL_ENABLE
  1095. espconn_secure_sent(ptrespconn, pbuf, length);
  1096. #else
  1097. espconn_sent(ptrespconn, pbuf, length);
  1098. #endif
  1099. } else {
  1100. #ifdef SERVER_SSL_ENABLE
  1101. espconn_secure_sent(ptrespconn, httphead, length);
  1102. #else
  1103. espconn_sent(ptrespconn, httphead, length);
  1104. #endif
  1105. }
  1106. if (pbuf) {
  1107. os_free(pbuf);
  1108. pbuf = NULL;
  1109. }
  1110. }
  1111. /******************************************************************************
  1112. * FunctionName : json_send
  1113. * Description : processing the data as json format and send to the client or server
  1114. * Parameters : arg -- argument to set for client or server
  1115. * ParmType -- json format type
  1116. * Returns : none
  1117. *******************************************************************************/
  1118. LOCAL void ICACHE_FLASH_ATTR
  1119. json_send(void *arg, ParmType ParmType)
  1120. {
  1121. char *pbuf = NULL;
  1122. pbuf = (char *)os_zalloc(jsonSize);
  1123. struct espconn *ptrespconn = arg;
  1124. switch (ParmType) {
  1125. #if LIGHT_DEVICE
  1126. case LIGHT_STATUS:
  1127. json_ws_send((struct jsontree_value *)&PwmTree, "light", pbuf);
  1128. break;
  1129. #endif
  1130. #if CH_AIRCON_DEVICE
  1131. case AIRCON_STATUS:
  1132. json_ws_send((struct jsontree_value *)&get_aircon_tree, "aircon", pbuf);
  1133. break;
  1134. #endif
  1135. #if EP_PRJ_LIGHT_PWM
  1136. case LIGHT_STATUS:
  1137. json_ws_send((struct jsontree_value *)&get_pwl_tree, "light", pbuf);
  1138. break;
  1139. #endif
  1140. #if PLUG_DEVICE
  1141. case SWITCH_STATUS:
  1142. json_ws_send((struct jsontree_value *)&StatusTree, "switch", pbuf);
  1143. break;
  1144. #endif
  1145. case INFOMATION:
  1146. json_ws_send((struct jsontree_value *)&INFOTree, "info", pbuf);
  1147. break;
  1148. case WIFI:
  1149. json_ws_send((struct jsontree_value *)&wifi_info_tree, "wifi", pbuf);
  1150. break;
  1151. #if ESP_PLATFORM
  1152. case CONNECT_STATUS:
  1153. json_ws_send((struct jsontree_value *)&con_status_tree, "info", pbuf);
  1154. break;
  1155. #endif
  1156. case USER_BIN:
  1157. json_ws_send((struct jsontree_value *)&userinfo_tree, "user_info", pbuf);
  1158. break;
  1159. case SCAN: {
  1160. u8 i = 0;
  1161. u8 scancount = 0;
  1162. struct bss_info *bss = NULL;
  1163. // bss = STAILQ_FIRST(pscaninfo->pbss);
  1164. bss = bss_head;
  1165. if (bss == NULL) {
  1166. os_free(pscaninfo);
  1167. pscaninfo = NULL;
  1168. os_sprintf(pbuf, "{\n\"successful\": false,\n\"data\": null\n}");
  1169. } else {
  1170. do {
  1171. if (pscaninfo->page_sn == pscaninfo->pagenum) {
  1172. pscaninfo->page_sn = 0;
  1173. os_sprintf(pbuf, "{\n\"successful\": false,\n\"meessage\": \"repeated page\"\n}");
  1174. break;
  1175. }
  1176. scancount = scannum - (pscaninfo->pagenum - 1) * 8;
  1177. if (scancount >= 8) {
  1178. pscaninfo->data_cnt += 8;
  1179. pscaninfo->page_sn = pscaninfo->pagenum;
  1180. if (pscaninfo->data_cnt > scannum) {
  1181. pscaninfo->data_cnt -= 8;
  1182. os_sprintf(pbuf, "{\n\"successful\": false,\n\"meessage\": \"error page\"\n}");
  1183. break;
  1184. }
  1185. json_ws_send((struct jsontree_value *)&scan_tree, "scan", pbuf);
  1186. } else {
  1187. pscaninfo->data_cnt += scancount;
  1188. pscaninfo->page_sn = pscaninfo->pagenum;
  1189. if (pscaninfo->data_cnt > scannum) {
  1190. pscaninfo->data_cnt -= scancount;
  1191. os_sprintf(pbuf, "{\n\"successful\": false,\n\"meessage\": \"error page\"\n}");
  1192. break;
  1193. }
  1194. char *ptrscanbuf = (char *)os_zalloc(jsonSize);
  1195. char *pscanbuf = ptrscanbuf;
  1196. os_sprintf(pscanbuf, ",\n\"ScanResult\": [\n");
  1197. pscanbuf += os_strlen(pscanbuf);
  1198. for (i = 0; i < scancount; i ++) {
  1199. JSONTREE_OBJECT(page_tree,
  1200. JSONTREE_PAIR("page", &scaninfo_tree));
  1201. json_ws_send((struct jsontree_value *)&page_tree, "page", pscanbuf);
  1202. os_sprintf(pscanbuf + os_strlen(pscanbuf), ",\n");
  1203. pscanbuf += os_strlen(pscanbuf);
  1204. }
  1205. os_sprintf(pscanbuf - 2, "]\n");
  1206. JSONTREE_OBJECT(scantree,
  1207. JSONTREE_PAIR("TotalPage", &scan_callback),
  1208. JSONTREE_PAIR("PageNum", &scan_callback));
  1209. JSONTREE_OBJECT(scanres_tree,
  1210. JSONTREE_PAIR("Response", &scantree));
  1211. JSONTREE_OBJECT(scan_tree,
  1212. JSONTREE_PAIR("scan", &scanres_tree));
  1213. json_ws_send((struct jsontree_value *)&scan_tree, "scan", pbuf);
  1214. os_memcpy(pbuf + os_strlen(pbuf) - 4, ptrscanbuf, os_strlen(ptrscanbuf));
  1215. os_sprintf(pbuf + os_strlen(pbuf), "}\n}");
  1216. os_free(ptrscanbuf);
  1217. }
  1218. } while (0);
  1219. }
  1220. break;
  1221. }
  1222. default :
  1223. break;
  1224. }
  1225. data_send(ptrespconn, true, pbuf);
  1226. os_free(pbuf);
  1227. pbuf = NULL;
  1228. }
  1229. /******************************************************************************
  1230. * FunctionName : response_send
  1231. * Description : processing the send result
  1232. * Parameters : arg -- argument to set for client or server
  1233. * responseOK -- true or false
  1234. * Returns : none
  1235. *******************************************************************************/
  1236. LOCAL void ICACHE_FLASH_ATTR
  1237. response_send(void *arg, bool responseOK)
  1238. {
  1239. struct espconn *ptrespconn = arg;
  1240. data_send(ptrespconn, responseOK, NULL);
  1241. }
  1242. /******************************************************************************
  1243. * FunctionName : json_scan_cb
  1244. * Description : processing the scan result
  1245. * Parameters : arg -- Additional argument to pass to the callback function
  1246. * status -- scan status
  1247. * Returns : none
  1248. *******************************************************************************/
  1249. LOCAL void ICACHE_FLASH_ATTR json_scan_cb(void *arg, STATUS status)
  1250. {
  1251. pscaninfo->pbss = arg;
  1252. if (scannum % 8 == 0) {
  1253. pscaninfo->totalpage = scannum / 8;
  1254. } else {
  1255. pscaninfo->totalpage = scannum / 8 + 1;
  1256. }
  1257. JSONTREE_OBJECT(totaltree,
  1258. JSONTREE_PAIR("TotalPage", &scan_callback));
  1259. JSONTREE_OBJECT(totalres_tree,
  1260. JSONTREE_PAIR("Response", &totaltree));
  1261. JSONTREE_OBJECT(total_tree,
  1262. JSONTREE_PAIR("total", &totalres_tree));
  1263. bss_temp = bss_head;
  1264. while(bss_temp !=NULL) {
  1265. bss_head = bss_temp->next.stqe_next;
  1266. os_free(bss_temp);
  1267. bss_temp = bss_head;
  1268. }
  1269. bss_head = NULL;
  1270. bss_temp = NULL;
  1271. bss = STAILQ_FIRST(pscaninfo->pbss);
  1272. while(bss != NULL) {
  1273. if(bss_temp == NULL){
  1274. bss_temp = (struct bss_info *)os_zalloc(sizeof(struct bss_info));
  1275. bss_head = bss_temp;
  1276. } else {
  1277. bss_temp->next.stqe_next = (struct bss_info *)os_zalloc(sizeof(struct bss_info));
  1278. bss_temp = bss_temp->next.stqe_next;
  1279. }
  1280. if(bss_temp == NULL) {
  1281. os_printf("malloc scan info failed\n");
  1282. break;
  1283. } else{
  1284. os_memcpy(bss_temp->bssid,bss->bssid,sizeof(bss->bssid));
  1285. os_memcpy(bss_temp->ssid,bss->ssid,sizeof(bss->ssid));
  1286. bss_temp->authmode = bss->authmode;
  1287. bss_temp->rssi = bss->rssi;
  1288. bss_temp->channel = bss->channel;
  1289. }
  1290. bss = STAILQ_NEXT(bss,next);
  1291. }
  1292. char *pbuf = NULL;
  1293. pbuf = (char *)os_zalloc(jsonSize);
  1294. json_ws_send((struct jsontree_value *)&total_tree, "total", pbuf);
  1295. data_send(pscaninfo->pespconn, true, pbuf);
  1296. os_free(pbuf);
  1297. }
  1298. void ICACHE_FLASH_ATTR
  1299. upgrade_check_func(void *arg)
  1300. {
  1301. struct espconn *ptrespconn = arg;
  1302. os_timer_disarm(&upgrade_check_timer);
  1303. if(system_upgrade_flag_check() == UPGRADE_FLAG_START) {
  1304. response_send(ptrespconn, false);
  1305. system_upgrade_deinit();
  1306. system_upgrade_flag_set(UPGRADE_FLAG_IDLE);
  1307. upgrade_lock = 0;
  1308. os_printf("local upgrade failed\n");
  1309. } else if( system_upgrade_flag_check() == UPGRADE_FLAG_FINISH ) {
  1310. os_printf("local upgrade success\n");
  1311. response_send(ptrespconn, true);
  1312. upgrade_lock = 0;
  1313. } else {
  1314. }
  1315. }
  1316. /******************************************************************************
  1317. * FunctionName : upgrade_deinit
  1318. * Description : disconnect the connection with the host
  1319. * Parameters : bin -- server number
  1320. * Returns : none
  1321. *******************************************************************************/
  1322. void ICACHE_FLASH_ATTR
  1323. LOCAL local_upgrade_deinit(void)
  1324. {
  1325. if (system_upgrade_flag_check() != UPGRADE_FLAG_START) {
  1326. os_printf("system upgrade deinit\n");
  1327. system_upgrade_deinit();
  1328. }
  1329. }
  1330. /******************************************************************************
  1331. * FunctionName : upgrade_download
  1332. * Description : Processing the upgrade data from the host
  1333. * Parameters : bin -- server number
  1334. * pusrdata -- The upgrade data (or NULL when the connection has been closed!)
  1335. * length -- The length of upgrade data
  1336. * Returns : none
  1337. *******************************************************************************/
  1338. LOCAL void ICACHE_FLASH_ATTR
  1339. local_upgrade_download(void * arg,char *pusrdata, unsigned short length)
  1340. {
  1341. char *ptr = NULL;
  1342. char *ptmp2 = NULL;
  1343. char lengthbuffer[32];
  1344. static uint32 totallength = 0;
  1345. static uint32 sumlength = 0;
  1346. static uint32 erase_length = 0;
  1347. // char A_buf[2] = {0xE9 ,0x03};
  1348. // char B_buf[2] = {0xEA,0x04};
  1349. struct espconn *pespconn = arg;
  1350. if (totallength == 0 && (ptr = (char *)os_strstr(pusrdata, "\r\n\r\n")) != NULL &&
  1351. (ptr = (char *)os_strstr(pusrdata, "Content-Length")) != NULL) {
  1352. ptr = (char *)os_strstr(pusrdata, "Content-Length: ");
  1353. if (ptr != NULL) {
  1354. ptr += 16;
  1355. ptmp2 = (char *)os_strstr(ptr, "\r\n");
  1356. if (ptmp2 != NULL) {
  1357. os_memset(lengthbuffer, 0, sizeof(lengthbuffer));
  1358. os_memcpy(lengthbuffer, ptr, ptmp2 - ptr);
  1359. sumlength = atoi(lengthbuffer);
  1360. if (sumlength == 0) {
  1361. os_timer_disarm(&upgrade_check_timer);
  1362. os_timer_setfn(&upgrade_check_timer, (os_timer_func_t *)upgrade_check_func, pespconn);
  1363. os_timer_arm(&upgrade_check_timer, 10, 0);
  1364. return;
  1365. }
  1366. } else {
  1367. os_printf("sumlength failed\n");
  1368. }
  1369. } else {
  1370. os_printf("Content-Length: failed\n");
  1371. }
  1372. if (sumlength != 0) {
  1373. if (sumlength >= LIMIT_ERASE_SIZE){
  1374. system_upgrade_erase_flash(0xFFFF);
  1375. erase_length = sumlength - LIMIT_ERASE_SIZE;
  1376. } else {
  1377. system_upgrade_erase_flash(sumlength);
  1378. erase_length = 0;
  1379. }
  1380. }
  1381. ptr = (char *)os_strstr(pusrdata, "\r\n\r\n");
  1382. length -= ptr - pusrdata;
  1383. length -= 4;
  1384. totallength += length;
  1385. os_printf("upgrade file download start.\n");
  1386. system_upgrade(ptr + 4, length);
  1387. } else {
  1388. totallength += length;
  1389. if (erase_length >= LIMIT_ERASE_SIZE){
  1390. system_upgrade_erase_flash(0xFFFF);
  1391. erase_length -= LIMIT_ERASE_SIZE;
  1392. } else {
  1393. system_upgrade_erase_flash(erase_length);
  1394. erase_length = 0;
  1395. }
  1396. system_upgrade(pusrdata, length);
  1397. }
  1398. if (totallength == sumlength) {
  1399. os_printf("upgrade file download finished.\n");
  1400. system_upgrade_flag_set(UPGRADE_FLAG_FINISH);
  1401. totallength = 0;
  1402. sumlength = 0;
  1403. upgrade_check_func(pespconn);
  1404. os_timer_disarm(&app_upgrade_10s);
  1405. os_timer_setfn(&app_upgrade_10s, (os_timer_func_t *)local_upgrade_deinit, NULL);
  1406. os_timer_arm(&app_upgrade_10s, 10, 0);
  1407. }
  1408. }
  1409. /******************************************************************************
  1410. * FunctionName : webserver_recv
  1411. * Description : Processing the received data from the server
  1412. * Parameters : arg -- Additional argument to pass to the callback function
  1413. * pusrdata -- The received data (or NULL when the connection has been closed!)
  1414. * length -- The length of received data
  1415. * Returns : none
  1416. *******************************************************************************/
  1417. LOCAL void ICACHE_FLASH_ATTR
  1418. webserver_recv(void *arg, char *pusrdata, unsigned short length)
  1419. {
  1420. URL_Frame *pURL_Frame = NULL;
  1421. char *pParseBuffer = NULL;
  1422. bool parse_flag = false;
  1423. struct espconn *ptrespconn = arg;
  1424. os_printf("pconn:%x,len:%u @t %d\n%s\n", ptrespconn, length, system_get_time(), pusrdata);
  1425. if(upgrade_lock == 0){
  1426. os_printf("len:%u\n",length);
  1427. if(check_data(pusrdata, length) == false)
  1428. {
  1429. os_printf("goto\n");
  1430. goto _temp_exit;
  1431. }
  1432. parse_flag = save_data(pusrdata, length);
  1433. if (parse_flag == false) {
  1434. response_send(ptrespconn, false);
  1435. }
  1436. // os_printf(precvbuffer);
  1437. pURL_Frame = (URL_Frame *)os_zalloc(sizeof(URL_Frame));
  1438. parse_url(precvbuffer, pURL_Frame);
  1439. os_printf("sel:%s\ncmd%s\nfil:%s\n", pURL_Frame->pSelect,
  1440. pURL_Frame->pCommand, pURL_Frame->pFilename);
  1441. switch (pURL_Frame->Type) {
  1442. case GET:
  1443. os_printf("We have a GET request.\n");
  1444. if (os_strcmp(pURL_Frame->pSelect, "client") == 0 &&
  1445. os_strcmp(pURL_Frame->pCommand, "command") == 0) {
  1446. if (os_strcmp(pURL_Frame->pFilename, "info") == 0) {
  1447. json_send(ptrespconn, INFOMATION);
  1448. }
  1449. if (os_strcmp(pURL_Frame->pFilename, "status") == 0) {
  1450. json_send(ptrespconn, CONNECT_STATUS);
  1451. } else if (os_strcmp(pURL_Frame->pFilename, "scan") == 0) {
  1452. char *strstr = NULL;
  1453. strstr = (char *)os_strstr(pusrdata, "&");
  1454. if (strstr == NULL) {
  1455. if (pscaninfo == NULL) {
  1456. pscaninfo = (scaninfo *)os_zalloc(sizeof(scaninfo));
  1457. }
  1458. pscaninfo->pespconn = ptrespconn;
  1459. pscaninfo->pagenum = 0;
  1460. pscaninfo->page_sn = 0;
  1461. pscaninfo->data_cnt = 0;
  1462. wifi_station_scan(NULL, json_scan_cb);
  1463. } else {
  1464. strstr ++;
  1465. if (os_strncmp(strstr, "page", 4) == 0) {
  1466. if (pscaninfo != NULL) {
  1467. pscaninfo->pagenum = *(strstr + 5);
  1468. pscaninfo->pagenum -= 0x30;
  1469. if (pscaninfo->pagenum > pscaninfo->totalpage || pscaninfo->pagenum == 0) {
  1470. response_send(ptrespconn, false);
  1471. } else {
  1472. json_send(ptrespconn, SCAN);
  1473. }
  1474. } else {
  1475. response_send(ptrespconn, false);
  1476. }
  1477. } else if(os_strncmp(strstr, "finish", 6) == 0){
  1478. bss_temp = bss_head;
  1479. while(bss_temp != NULL) {
  1480. bss_head = bss_temp->next.stqe_next;
  1481. os_free(bss_temp);
  1482. bss_temp = bss_head;
  1483. }
  1484. bss_head = NULL;
  1485. bss_temp = NULL;
  1486. response_send(ptrespconn, true);
  1487. } else {
  1488. response_send(ptrespconn, false);
  1489. }
  1490. }
  1491. } else {
  1492. response_send(ptrespconn, false);
  1493. }
  1494. } else if (os_strcmp(pURL_Frame->pSelect, "config") == 0 &&
  1495. os_strcmp(pURL_Frame->pCommand, "command") == 0) {
  1496. if (os_strcmp(pURL_Frame->pFilename, "wifi") == 0) {
  1497. ap_conf = (struct softap_config *)os_zalloc(sizeof(struct softap_config));
  1498. sta_conf = (struct station_config *)os_zalloc(sizeof(struct station_config));
  1499. json_send(ptrespconn, WIFI);
  1500. os_free(sta_conf);
  1501. os_free(ap_conf);
  1502. sta_conf = NULL;
  1503. ap_conf = NULL;
  1504. }
  1505. #if PLUG_DEVICE
  1506. else if (os_strcmp(pURL_Frame->pFilename, "switch") == 0) {
  1507. json_send(ptrespconn, SWITCH_STATUS);
  1508. }
  1509. #endif
  1510. #if LIGHT_DEVICE || EP_PRJ_LIGHT_PWM
  1511. else if (os_strcmp(pURL_Frame->pFilename, "light") == 0) {
  1512. json_send(ptrespconn, LIGHT_STATUS);
  1513. }
  1514. #endif
  1515. #if CH_AIRCON_DEVICE
  1516. else if (os_strcmp(pURL_Frame->pFilename, "aircon") == 0) {
  1517. ch_aircon_add_user_ip(ptrespconn->proto.tcp->remote_ip,
  1518. ptrespconn->proto.tcp->remote_port);
  1519. ch_aircon_query_status();
  1520. // json_send(ptrespconn, AIRCON_STATUS);
  1521. }
  1522. #endif
  1523. else if (os_strcmp(pURL_Frame->pFilename, "reboot") == 0) {
  1524. json_send(ptrespconn, REBOOT);
  1525. } else {
  1526. response_send(ptrespconn, false);
  1527. }
  1528. } else if (os_strcmp(pURL_Frame->pSelect, "upgrade") == 0 &&
  1529. os_strcmp(pURL_Frame->pCommand, "command") == 0) {
  1530. if (os_strcmp(pURL_Frame->pFilename, "getuser") == 0) {
  1531. json_send(ptrespconn , USER_BIN);
  1532. }
  1533. } else {
  1534. response_send(ptrespconn, false);
  1535. }
  1536. break;
  1537. case POST:
  1538. os_printf("We have a POST request.\n");
  1539. pParseBuffer = (char *)os_strstr(precvbuffer, "\r\n\r\n");
  1540. if (pParseBuffer == NULL) {
  1541. break;
  1542. }
  1543. pParseBuffer += 4;
  1544. if (os_strcmp(pURL_Frame->pSelect, "config") == 0 &&
  1545. os_strcmp(pURL_Frame->pCommand, "command") == 0) {
  1546. #if SENSOR_DEVICE
  1547. if (os_strcmp(pURL_Frame->pFilename, "sleep") == 0) {
  1548. #else
  1549. if (os_strcmp(pURL_Frame->pFilename, "reboot") == 0) {
  1550. #endif
  1551. if (pParseBuffer != NULL) {
  1552. if (restart_10ms != NULL) {
  1553. os_timer_disarm(restart_10ms);
  1554. }
  1555. if (rstparm == NULL) {
  1556. rstparm = (rst_parm *)os_zalloc(sizeof(rst_parm));
  1557. }
  1558. rstparm->pespconn = ptrespconn;
  1559. #if SENSOR_DEVICE
  1560. rstparm->parmtype = DEEP_SLEEP;
  1561. #else
  1562. rstparm->parmtype = REBOOT;
  1563. #endif
  1564. if (restart_10ms == NULL) {
  1565. restart_10ms = (os_timer_t *)os_malloc(sizeof(os_timer_t));
  1566. }
  1567. os_timer_setfn(restart_10ms, (os_timer_func_t *)restart_10ms_cb, NULL);
  1568. os_timer_arm(restart_10ms, 10, 0); // delay 10ms, then do
  1569. response_send(ptrespconn, true);
  1570. } else {
  1571. response_send(ptrespconn, false);
  1572. }
  1573. } else if (os_strcmp(pURL_Frame->pFilename, "wifi") == 0) {
  1574. if (pParseBuffer != NULL) {
  1575. struct jsontree_context js;
  1576. #if ESP_PLATFORM
  1577. user_esp_platform_set_connect_status(DEVICE_CONNECTING);
  1578. #endif
  1579. if (restart_10ms != NULL) {
  1580. os_timer_disarm(restart_10ms);
  1581. }
  1582. if (ap_conf == NULL) {
  1583. ap_conf = (struct softap_config *)os_zalloc(sizeof(struct softap_config));
  1584. }
  1585. if (sta_conf == NULL) {
  1586. sta_conf = (struct station_config *)os_zalloc(sizeof(struct station_config));
  1587. }
  1588. jsontree_setup(&js, (struct jsontree_value *)&wifi_req_tree, json_putchar);
  1589. json_parse(&js, pParseBuffer);
  1590. if (rstparm == NULL) {
  1591. rstparm = (rst_parm *)os_zalloc(sizeof(rst_parm));
  1592. }
  1593. rstparm->pespconn = ptrespconn;
  1594. rstparm->parmtype = WIFI;
  1595. if (sta_conf->ssid[0] != 0x00 || ap_conf->ssid[0] != 0x00) {
  1596. ap_conf->ssid_hidden = 0;
  1597. ap_conf->max_connection = 4;
  1598. if (restart_10ms == NULL) {
  1599. restart_10ms = (os_timer_t *)os_malloc(sizeof(os_timer_t));
  1600. }
  1601. os_timer_disarm(restart_10ms);
  1602. os_timer_setfn(restart_10ms, (os_timer_func_t *)restart_10ms_cb, NULL);
  1603. os_timer_arm(restart_10ms, 10, 0); // delay 10ms, then do
  1604. } else {
  1605. os_free(ap_conf);
  1606. os_free(sta_conf);
  1607. os_free(rstparm);
  1608. sta_conf = NULL;
  1609. ap_conf = NULL;
  1610. rstparm =NULL;
  1611. }
  1612. response_send(ptrespconn, true);
  1613. } else {
  1614. response_send(ptrespconn, false);
  1615. }
  1616. }
  1617. #if PLUG_DEVICE
  1618. else if (os_strcmp(pURL_Frame->pFilename, "switch") == 0) {
  1619. if (pParseBuffer != NULL) {
  1620. struct jsontree_context js;
  1621. jsontree_setup(&js, (struct jsontree_value *)&StatusTree, json_putchar);
  1622. json_parse(&js, pParseBuffer);
  1623. response_send(ptrespconn, true);
  1624. } else {
  1625. response_send(ptrespconn, false);
  1626. }
  1627. }
  1628. #endif
  1629. #if CH_AIRCON_DEVICE
  1630. else if (os_strcmp(pURL_Frame->pFilename, "aircon") == 0) {
  1631. if (pParseBuffer != NULL) {
  1632. ch_aircon_add_user_ip(ptrespconn->proto.tcp->remote_ip,
  1633. ptrespconn->proto.tcp->remote_port);
  1634. current_cmd_type = 2;
  1635. ch_aircon_query_status();
  1636. struct jsontree_context js;
  1637. jsontree_setup(&js, (struct jsontree_value *)&set_aircon_tree, json_putchar);
  1638. json_parse(&js, pParseBuffer);
  1639. // response_send(ptrespconn, true);
  1640. } else {
  1641. response_send(ptrespconn, false);
  1642. }
  1643. }
  1644. #endif
  1645. #if EP_PRJ_LIGHT_PWM
  1646. else if (os_strcmp(pURL_Frame->pFilename, "light") == 0) {
  1647. if (pParseBuffer != NULL) {
  1648. struct jsontree_context js;
  1649. jsontree_setup(&js, (struct jsontree_value *)&set_pwl_tree, json_putchar);
  1650. json_parse(&js, pParseBuffer);
  1651. json_send(ptrespconn, LIGHT_STATUS);
  1652. } else {
  1653. response_send(ptrespconn, false);
  1654. }
  1655. }
  1656. #endif
  1657. #if LIGHT_DEVICE
  1658. else if (os_strcmp(pURL_Frame->pFilename, "light") == 0) {
  1659. if (pParseBuffer != NULL) {
  1660. struct jsontree_context js;
  1661. jsontree_setup(&js, (struct jsontree_value *)&PwmTree, json_putchar);
  1662. json_parse(&js, pParseBuffer);
  1663. os_printf("rsp1:%u\n",PostCmdNeeRsp);
  1664. if(PostCmdNeeRsp == 0)
  1665. PostCmdNeeRsp = 1;
  1666. else
  1667. response_send(ptrespconn, true);
  1668. } else {
  1669. response_send(ptrespconn, false);
  1670. }
  1671. }
  1672. #endif
  1673. else if (os_strcmp(pURL_Frame->pFilename, "reset") == 0) {
  1674. response_send(ptrespconn, true);
  1675. #if ESP_PLATFORM
  1676. extern struct esp_platform_saved_param esp_param;
  1677. esp_param.activeflag = 0;
  1678. system_param_save_with_protect(ESP_PARAM_START_SEC, &esp_param, sizeof(esp_param));
  1679. #endif
  1680. system_restore();
  1681. system_restart();
  1682. }
  1683. else {
  1684. response_send(ptrespconn, false);
  1685. }
  1686. }
  1687. else if(os_strcmp(pURL_Frame->pSelect, "upgrade") == 0 &&
  1688. os_strcmp(pURL_Frame->pCommand, "command") == 0){
  1689. if (os_strcmp(pURL_Frame->pFilename, "start") == 0){
  1690. response_send(ptrespconn, true);
  1691. os_printf("local upgrade start\n");
  1692. upgrade_lock = 1;
  1693. system_upgrade_init();
  1694. system_upgrade_flag_set(UPGRADE_FLAG_START);
  1695. os_timer_disarm(&upgrade_check_timer);
  1696. os_timer_setfn(&upgrade_check_timer, (os_timer_func_t *)upgrade_check_func, NULL);
  1697. os_timer_arm(&upgrade_check_timer, 120000, 0);
  1698. } else if (os_strcmp(pURL_Frame->pFilename, "reset") == 0) {
  1699. response_send(ptrespconn, true);
  1700. os_printf("local upgrade restart\n");
  1701. system_upgrade_reboot();
  1702. } else {
  1703. response_send(ptrespconn, false);
  1704. }
  1705. }else {
  1706. response_send(ptrespconn, false);
  1707. }
  1708. break;
  1709. }
  1710. if (precvbuffer != NULL){
  1711. os_free(precvbuffer);
  1712. precvbuffer = NULL;
  1713. }
  1714. os_free(pURL_Frame);
  1715. pURL_Frame = NULL;
  1716. _temp_exit:
  1717. ;
  1718. }
  1719. else if(upgrade_lock == 1){
  1720. local_upgrade_download(ptrespconn,pusrdata, length);
  1721. if (precvbuffer != NULL){
  1722. os_free(precvbuffer);
  1723. precvbuffer = NULL;
  1724. }
  1725. os_free(pURL_Frame);
  1726. pURL_Frame = NULL;
  1727. }
  1728. }
  1729. #if CH_AIRCON_DEVICE
  1730. // LOCAL void ICACHE_FLASH_ATTR
  1731. // aircon_RespondTask(os_event_t *events)
  1732. // {
  1733. // struct espconn *ptrespconn = &esp_webserv_conn;
  1734. // os_printf("[---aircon_RespondTask---] stat %d\n", ptrespconn->state );
  1735. // json_send(ptrespconn, AIRCON_STATUS);
  1736. // }
  1737. #if 0//defined(AJFJFKF) /* added macro AJFJFKF */
  1738. struct MqttClientDataType {
  1739. uint8 remote_ip[4];
  1740. int remote_port;
  1741. uint8 *usrdata;
  1742. int datalen;
  1743. };
  1744. struct MqttdevClientDataQueueType {
  1745. STAILQ_ENTRY(MqttdevClientDataQueueType) next;
  1746. struct MqttClientDataType local_client_msg;
  1747. };
  1748. LOCAL STAILQ_HEAD(, MqttdevClientDataQueueType) mqttdev_localserver_client_data_list;
  1749. LOCAL uint8_t localserver_sending_semaphore = 0;
  1750. LOCAL os_timer_t mqttdev_localserver_send_timer;
  1751. LOCAL void ICACHE_FLASH_ATTR
  1752. mqttdev_localserver_clear_queue_all()
  1753. {
  1754. struct MqttdevClientDataQueueType *client_msg = NULL;
  1755. struct MqttdevClientDataQueueType *dumpmsg = NULL;
  1756. EPIT_INFO("%s " "\n", __FUNCTION__);
  1757. STAILQ_FOREACH(client_msg, &mqttdev_localserver_client_data_list, next)
  1758. {
  1759. EPIT_DBG("clientmsg %x len %d, " IPPORTSTR "\n", client_msg, client_msg->local_client_msg.datalen,
  1760. IP2STR(client_msg->local_client_msg.remote_ip), client_msg->local_client_msg.remote_port);
  1761. }
  1762. while((dumpmsg = STAILQ_FIRST(&mqttdev_localserver_client_data_list)) != NULL){
  1763. STAILQ_REMOVE_HEAD(&mqttdev_localserver_client_data_list, next);
  1764. --localerver_sending_blockcount;
  1765. EPIT_DBG("free clientmsg %x len %d\n", dumpmsg, dumpmsg->local_client_msg.datalen);
  1766. os_free(dumpmsg->local_client_msg.usrdata);
  1767. os_free(dumpmsg);
  1768. }
  1769. // if(STAILQ_EMPTY(&mqttdev_localserver_client_data_list))
  1770. // {
  1771. // localserver_sending_semaphore = 0;
  1772. // }
  1773. }
  1774. LOCAL void ICACHE_FLASH_ATTR
  1775. mqttdev_localserver_clear_queue_by_ip(uint8 *remote_ip)
  1776. {
  1777. struct MqttdevClientDataQueueType *client_msg = NULL;
  1778. struct MqttdevClientDataQueueType *dumpmsg = NULL;
  1779. EPIT_INFO("%s " IPSTR "\n", __FUNCTION__, IP2STR(remote_ip));
  1780. STAILQ_FOREACH(client_msg, &mqttdev_localserver_client_data_list, next)
  1781. {
  1782. EPIT_DBG("clientmsg %x len %d, " IPPORTSTR "\n", client_msg, client_msg->local_client_msg.datalen,
  1783. IP2STR(client_msg->local_client_msg.remote_ip), client_msg->local_client_msg.remote_port);
  1784. }
  1785. STAILQ_FOREACH(client_msg, &mqttdev_localserver_client_data_list, next)
  1786. {
  1787. if(dumpmsg)
  1788. {
  1789. EPIT_DBG("free clientmsg %x len %d\n", dumpmsg, dumpmsg->local_client_msg.datalen);
  1790. os_free(dumpmsg->local_client_msg.usrdata);
  1791. os_free(dumpmsg);
  1792. dumpmsg = NULL;
  1793. }
  1794. if(os_memcmp(client_msg->local_client_msg.remote_ip, remote_ip, 4) == 0)
  1795. {
  1796. dumpmsg = client_msg;
  1797. STAILQ_REMOVE(&mqttdev_localserver_client_data_list, dumpmsg, MqttdevClientDataQueueType, next);
  1798. --localerver_sending_blockcount;
  1799. }
  1800. }
  1801. if(dumpmsg)
  1802. {
  1803. EPIT_DBG("free clientmsg %x len %d\n", dumpmsg, dumpmsg->local_client_msg.datalen);
  1804. os_free(dumpmsg->local_client_msg.usrdata);
  1805. os_free(dumpmsg);
  1806. dumpmsg = NULL;
  1807. }
  1808. }
  1809. LOCAL void ICACHE_FLASH_ATTR
  1810. mqttdev_localserver_clear_queue_by_socket(uint8 *remote_ip, int remote_port)
  1811. {
  1812. struct espconn *pespconn = &esp_webserv_conn;
  1813. remot_info *premot = NULL;
  1814. if (espconn_get_connection_info(pespconn,&premot,0) == ESPCONN_OK){
  1815. if(pespconn->link_cnt > 0)
  1816. {
  1817. mqttdev_localserver_clear_queue_by_ip(remote_ip);
  1818. }
  1819. else
  1820. mqttdev_localserver_clear_queue_all();
  1821. }
  1822. else
  1823. {
  1824. mqttdev_localserver_clear_queue_all();
  1825. }
  1826. if(STAILQ_EMPTY(&mqttdev_localserver_client_data_list))
  1827. {
  1828. localserver_sending_semaphore = 0;
  1829. }
  1830. }
  1831. LOCAL void ICACHE_FLASH_ATTR
  1832. mqttdev_localserver_send_timer_cb(void)
  1833. {
  1834. struct espconn *pespconn = &localserver_conn;
  1835. EPIT_DBG("%s, localsem=%d, localcnt=%d\n", __FUNCTION__, localserver_sending_semaphore,localerver_sending_blockcount);
  1836. struct MqttdevClientDataQueueType *mqttdev_client;
  1837. os_timer_disarm(&mqttdev_localserver_send_timer);
  1838. localserver_sending_semaphore = 0;
  1839. if((mqttdev_client = STAILQ_FIRST(&mqttdev_localserver_client_data_list)) != NULL)
  1840. {
  1841. STAILQ_REMOVE_HEAD(&mqttdev_localserver_client_data_list, next);
  1842. --localerver_sending_blockcount;
  1843. mqttdev_localserver_send_packet(pespconn,
  1844. mqttdev_client->local_client_msg.remote_ip, mqttdev_client->local_client_msg.remote_port,
  1845. mqttdev_client->local_client_msg.usrdata, mqttdev_client->local_client_msg.datalen);
  1846. os_free(mqttdev_client);
  1847. }
  1848. }
  1849. LOCAL void ICACHE_FLASH_ATTR
  1850. mqttdev_localserver_send_packet(struct espconn *pespconn,
  1851. uint8 *remote_ip, int remote_port, uint8_t * databuf, int length)
  1852. {
  1853. EPIT_DBG("%s, localsem=%d, localcnt=%d\n", __FUNCTION__, localserver_sending_semaphore,localerver_sending_blockcount);
  1854. EPIT_DBG("server %x, useconn %x, \n", &localserver_conn, pespconn
  1855. );
  1856. EPIT_ERR("localserver send to " IPPORTSTR "\n",
  1857. IP2STR(remote_ip), remote_port);
  1858. if(localserver_sending_semaphore)
  1859. {
  1860. struct MqttdevClientDataQueueType *mqttdev_client = (struct MqttdevClientDataQueueType *)os_zalloc(sizeof(struct MqttdevClientDataQueueType));
  1861. if(mqttdev_client)
  1862. {
  1863. os_memcpy(mqttdev_client->local_client_msg.remote_ip, remote_ip, 4);
  1864. mqttdev_client->local_client_msg.remote_port = remote_port;
  1865. mqttdev_client->local_client_msg.datalen = length;
  1866. mqttdev_client->local_client_msg.usrdata = databuf;
  1867. STAILQ_INSERT_TAIL(&mqttdev_localserver_client_data_list, mqttdev_client, next);
  1868. ++localerver_sending_blockcount;
  1869. EPIT_DBG("add clientmsg %x len %d\n", mqttdev_client, mqttdev_client->local_client_msg.datalen);
  1870. EPIT_DBG("%s add queue localcnt=%d\n", __FUNCTION__, localerver_sending_blockcount);
  1871. }
  1872. else
  1873. {
  1874. os_free(databuf);
  1875. EPIT_ERR("localserver tcpqueue alloc fail\r\n");
  1876. }
  1877. }
  1878. else
  1879. {
  1880. pespconn = &esp_webserv_conn;
  1881. pespconn->proto.tcp->remote_port = remote_port;
  1882. pespconn->proto.tcp->remote_ip[0] = remote_ip[0];
  1883. pespconn->proto.tcp->remote_ip[1] = remote_ip[1];
  1884. pespconn->proto.tcp->remote_ip[2] = remote_ip[2];
  1885. pespconn->proto.tcp->remote_ip[3] = remote_ip[3];
  1886. localserver_sending_semaphore = 1;
  1887. sint8 ret = espconn_send(pespconn, databuf, length);
  1888. os_timer_disarm(&mqttdev_localserver_send_timer);
  1889. os_timer_setfn(&mqttdev_localserver_send_timer, (os_timer_func_t *)mqttdev_localserver_send_timer_cb, NULL);
  1890. os_timer_arm(&mqttdev_localserver_send_timer, 2000, 0);
  1891. if(ret)
  1892. EPIT_ERR("localserver send to " IPPORTSTR " fail %d\n",
  1893. IP2STR(remote_ip), remote_port,
  1894. ret);
  1895. user_printbuf(databuf, length<10?length:10, "localserverout");
  1896. os_free(databuf);
  1897. }
  1898. }
  1899. #endif /* AJFJFKF */
  1900. void ICACHE_FLASH_ATTR
  1901. ch_aircon_reply_to_user(bool result)
  1902. {
  1903. struct espconn *ptrespconn = &esp_webserv_conn;
  1904. os_printf("[---aircon_RespondTask---] %x stat %d\n", ptrespconn, ptrespconn->state );
  1905. if(current_cmd_type == 2)
  1906. {
  1907. current_cmd_type = 1;
  1908. ch_aircon_set_config();
  1909. return;
  1910. }
  1911. if(current_user_port)
  1912. {
  1913. ptrespconn->proto.tcp->remote_port = current_user_port;
  1914. ptrespconn->proto.tcp->remote_ip[0] = current_user_ip[0];
  1915. ptrespconn->proto.tcp->remote_ip[1] = current_user_ip[1];
  1916. ptrespconn->proto.tcp->remote_ip[2] = current_user_ip[2];
  1917. ptrespconn->proto.tcp->remote_ip[3] = current_user_ip[3];
  1918. if(result)
  1919. json_send(ptrespconn, AIRCON_STATUS);
  1920. else
  1921. response_send(ptrespconn, false);
  1922. current_user_port = 0;
  1923. current_cmd_type = 0;
  1924. }
  1925. }
  1926. #endif
  1927. LOCAL void ICACHE_FLASH_ATTR
  1928. mqttdev_localserver_sent_cb(void *arg)
  1929. {
  1930. struct espconn *pespconn = arg;
  1931. #if 0//(CH_AIRCON_DEVICE) /* added macro CH_AIRCON_DEVICE */
  1932. EPIT_DBG("%s, localsem=%d, localcnt=%d\n", __FUNCTION__, localserver_sending_semaphore,localerver_sending_blockcount);
  1933. struct MqttdevClientDataQueueType *mqttdev_client;
  1934. os_timer_disarm(&mqttdev_localserver_send_timer);
  1935. localserver_sending_semaphore = 0;
  1936. if((mqttdev_client = STAILQ_FIRST(&mqttdev_localserver_client_data_list)) != NULL)
  1937. {
  1938. STAILQ_REMOVE_HEAD(&mqttdev_localserver_client_data_list, next);
  1939. // os_printf("send udp to " IPSTR "\n",
  1940. // IP2STR((struct ip_addr *)(mqttdev_client->client_ip)));
  1941. --localerver_sending_blockcount;
  1942. mqttdev_localserver_send_packet(pespconn,
  1943. mqttdev_client->local_client_msg.remote_ip, mqttdev_client->local_client_msg.remote_port,
  1944. mqttdev_client->local_client_msg.usrdata, mqttdev_client->local_client_msg.datalen);
  1945. os_free(mqttdev_client);
  1946. }
  1947. #endif /* CH_AIRCON_DEVICE */
  1948. }
  1949. /******************************************************************************
  1950. * FunctionName : webserver_recon
  1951. * Description : the connection has been err, reconnection
  1952. * Parameters : arg -- Additional argument to pass to the callback function
  1953. * Returns : none
  1954. *******************************************************************************/
  1955. LOCAL ICACHE_FLASH_ATTR
  1956. void webserver_recon(void *arg, sint8 err)
  1957. {
  1958. struct espconn *pesp_conn = arg;
  1959. os_printf("webserver's %d.%d.%d.%d:%d err %d reconnect\n", pesp_conn->proto.tcp->remote_ip[0],
  1960. pesp_conn->proto.tcp->remote_ip[1],pesp_conn->proto.tcp->remote_ip[2],
  1961. pesp_conn->proto.tcp->remote_ip[3],pesp_conn->proto.tcp->remote_port, err);
  1962. }
  1963. /******************************************************************************
  1964. * FunctionName : webserver_recon
  1965. * Description : the connection has been err, reconnection
  1966. * Parameters : arg -- Additional argument to pass to the callback function
  1967. * Returns : none
  1968. *******************************************************************************/
  1969. LOCAL ICACHE_FLASH_ATTR
  1970. void webserver_discon(void *arg)
  1971. {
  1972. struct espconn *pesp_conn = arg;
  1973. os_printf("webserver's %d.%d.%d.%d:%d disconnect @t %d\n", pesp_conn->proto.tcp->remote_ip[0],
  1974. pesp_conn->proto.tcp->remote_ip[1],pesp_conn->proto.tcp->remote_ip[2],
  1975. pesp_conn->proto.tcp->remote_ip[3],pesp_conn->proto.tcp->remote_port,system_get_time());
  1976. // mqttdev_localserver_clear_queue_by_socket(pesp_conn->proto.tcp->remote_ip,
  1977. // pesp_conn->proto.tcp->remote_port);
  1978. #if (CH_AIRCON_DEVICE) /* added macro CH_AIRCON_DEVICE */
  1979. if(os_memcmp(pesp_conn->proto.tcp->remote_ip, current_user_ip, 4) == 0)
  1980. {
  1981. os_printf("clear user\n");
  1982. current_user_port = 0;
  1983. os_bzero(current_user_ip, 4);
  1984. }
  1985. #endif /* CH_AIRCON_DEVICE */
  1986. }
  1987. /******************************************************************************
  1988. * FunctionName : user_accept_listen
  1989. * Description : server listened a connection successfully
  1990. * Parameters : arg -- Additional argument to pass to the callback function
  1991. * Returns : none
  1992. *******************************************************************************/
  1993. LOCAL void ICACHE_FLASH_ATTR
  1994. webserver_listen(void *arg)
  1995. {
  1996. struct espconn *pesp_conn = arg;
  1997. espconn_regist_recvcb(pesp_conn, webserver_recv);
  1998. espconn_regist_reconcb(pesp_conn, webserver_recon);
  1999. espconn_regist_disconcb(pesp_conn, webserver_discon);
  2000. espconn_regist_sentcb(pesp_conn, mqttdev_localserver_sent_cb);
  2001. }
  2002. /******************************************************************************
  2003. * FunctionName : user_webserver_init
  2004. * Description : parameter initialize as a server
  2005. * Parameters : port -- server port
  2006. * Returns : none
  2007. *******************************************************************************/
  2008. void ICACHE_FLASH_ATTR
  2009. user_webserver_init(uint32 port)
  2010. {
  2011. esp_webserv_conn.type = ESPCONN_TCP;
  2012. esp_webserv_conn.state = ESPCONN_NONE;
  2013. esp_webserv_conn.proto.tcp = &esp_webserv_tcp;
  2014. esp_webserv_conn.proto.tcp->local_port = port;
  2015. espconn_regist_connectcb(&esp_webserv_conn, webserver_listen);
  2016. #if (CH_AIRCON_DEVICE)
  2017. // localserver_sending_semaphore = 0;
  2018. // STAILQ_INIT(&mqttdev_localserver_client_data_list);
  2019. #endif
  2020. #ifdef SERVER_SSL_ENABLE
  2021. espconn_secure_accept(&esp_webserv_conn);
  2022. #else
  2023. espconn_accept(&esp_webserv_conn);
  2024. #endif
  2025. }
  2026. #endif