web.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. #include "web.h"
  2. #include "mongoose.h"
  3. #include "thread.h"
  4. #include "list.h"
  5. #include "power.h"
  6. #include "paras.h"
  7. #include "cascade.h"
  8. #include "cfg.h"
  9. #include "lcd.h"
  10. #include <stdio.h>
  11. #include "datadef.h"
  12. #include "cascade.h"
  13. #define TIMER_INTERVAL 1000
  14. #define ONCE_SEND_CH_CNT 10
  15. //#define USE_MG716
  16. typedef struct mg_mgr mg_mgr_t;
  17. typedef struct mg_connection mg_conn_t;
  18. #ifdef USE_MG716
  19. typedef struct mg_tls_opts mg_tls_t;
  20. static void timer_fn(void *arg)
  21. {
  22. int r;
  23. web_handle_t *h=(web_handle_t*)arg;
  24. list_node_t *ln=NULL;
  25. mg_conn_t* c;
  26. r = list_take_node(h->list, &ln, 0);
  27. if(r==0) {
  28. node_t *n=&ln->data;
  29. for (c = h->mgr.conns; c != NULL; c = c->next) {
  30. ws_send_pkt(h, c, n->tp, n->buf, n->dlen, 1);
  31. }
  32. list_back_node(h->list, ln);
  33. }
  34. }
  35. #else
  36. typedef struct mg_serve_http_opts mg_opts_t;
  37. #define MG_PATH_MAX 128
  38. static void set_timer(struct mg_connection *c, int ms)
  39. {
  40. mg_set_timer(c, mg_time()+ms/1000.0f);
  41. }
  42. #endif
  43. typedef struct {
  44. struct mg_connection *c;
  45. int ev;
  46. void *ev_data;
  47. }mg_stream_t;
  48. typedef struct {
  49. pthread_t tid;
  50. mg_mgr_t mgr;
  51. #ifdef USE_MG716
  52. mg_tls_t tls;
  53. #else
  54. mg_opts_t opts;
  55. #endif
  56. handle_t list;
  57. char rootDir[MG_PATH_MAX];
  58. char *buf;
  59. int buflen;
  60. uint8_t cur_addr;
  61. }web_handle_t;
  62. static web_handle_t webHandle={0};
  63. static int is_ws(mg_conn_t *c)
  64. {
  65. int r=0;
  66. #ifdef USE_MG716
  67. r = c->is_websocket?1:0;
  68. #else
  69. r = (c->flags & MG_F_IS_WEBSOCKET)?1:0;
  70. #endif
  71. return r;
  72. }
  73. static int ws_send(mg_conn_t *c, void *data, int len, int isbin)
  74. {
  75. if(!c || !data || len<=0) {
  76. return -1;
  77. }
  78. //LOGD("____ws_send, len: %d\n", len);
  79. #ifdef USE_MG716
  80. mg_ws_send(c, data, len, isbin?WEBSOCKET_OP_BINARY:WEBSOCKET_OP_TEXT);
  81. #else
  82. mg_send_websocket_frame(c, isbin?WEBSOCKET_OP_BINARY:WEBSOCKET_OP_TEXT, data, len);
  83. #endif
  84. return 0;
  85. }
  86. static int ws_send_pkt(web_handle_t *h, mg_conn_t *c, uint8_t addr, uint8_t type, void *data, int len, int isbin)
  87. {
  88. pkt_hdr_t *hdr=NULL;
  89. if(!h->buf || !len || h->buflen<sizeof(pkt_hdr_t)+len) {
  90. LOGE("____ws_send, para are invalid\n");
  91. return -1;
  92. }
  93. hdr = (pkt_hdr_t*)h->buf;
  94. hdr->magic = PKT_MAGIC;
  95. hdr->addr = addr;
  96. hdr->type = type;
  97. hdr->nack = 0;
  98. hdr->dlen = len;
  99. memcpy(hdr->data, data, len);
  100. ws_send(c, hdr, len+sizeof(pkt_hdr_t), isbin);
  101. return 0;
  102. }
  103. static void send_powerc(web_handle_t *h, mg_conn_t *c, uint8_t addr)
  104. {
  105. int i,j,xlen,offset,times,left;
  106. pkt_hdr_t *hdr=NULL;
  107. power_all_t *p=(addr==0)?power_data_get():cascade_power_get();
  108. if(p->chs==0 || h->buf==NULL ) {
  109. return;
  110. }
  111. hdr = (pkt_hdr_t*)h->buf;
  112. hdr->magic = PKT_MAGIC;
  113. hdr->addr = addr;
  114. hdr->type = PKT_TYPE_POWER_CH;
  115. hdr->nack = 0;
  116. times = p->chs/ONCE_SEND_CH_CNT;
  117. for(i=0; i<times; i++) {
  118. for(j=0; j<ONCE_SEND_CH_CNT; j++) {
  119. offset = j*sizeof(power_ch_t);
  120. memcpy(hdr->data+offset, &p->pch[i*ONCE_SEND_CH_CNT+j], sizeof(power_ch_t));
  121. }
  122. xlen = ONCE_SEND_CH_CNT*sizeof(power_ch_t)+sizeof(pkt_hdr_t);
  123. hdr->dlen = xlen-sizeof(pkt_hdr_t);
  124. ws_send(c, h->buf, xlen, 1);
  125. }
  126. left = p->chs%ONCE_SEND_CH_CNT;
  127. for(i=0; i<left; i++) {
  128. offset = i*sizeof(power_ch_t);
  129. memcpy(hdr->data+offset, &p->pch[p->chs-left+i], sizeof(power_ch_t));
  130. }
  131. xlen = (p->chs%ONCE_SEND_CH_CNT)*sizeof(power_ch_t)+sizeof(pkt_hdr_t);
  132. hdr->dlen = xlen-sizeof(pkt_hdr_t);
  133. ws_send(c, h->buf, xlen, 1);
  134. }
  135. static void send_powert(web_handle_t *h, mg_conn_t *c, uint8_t addr)
  136. {
  137. int i,r;
  138. power_all_t *p=(addr==0)?power_data_get():cascade_power_get();
  139. ws_send_pkt(h, c, addr, PKT_TYPE_POWER_TOTAL, &p->ttl, sizeof(power_total_t), 1);
  140. }
  141. static void send_dlist(web_handle_t *h, mg_conn_t *c)
  142. {
  143. int i,j=0,r;
  144. dev_list_t dl;
  145. slave_list_t sl;
  146. r = cascade_get_dlist(&dl);
  147. if(r==0) {
  148. sl.cnt = dl.cnt-1;
  149. if(sl.cnt>0) {
  150. for(i=1; i<dl.cnt; i++) {
  151. sl.addr[j++] = dl.slave[i].addr;
  152. }
  153. ws_send_pkt(h, c, 0, PKT_TYPE_POWER_TOTAL, &sl, 1+sl.cnt, 1);
  154. }
  155. cascade_free_dlist(&dl);
  156. }
  157. }
  158. static int ws_handle(web_handle_t *h, pkt_hdr_t *hdr, mg_conn_t *c)
  159. {
  160. int r=0;
  161. if (!is_ws(c)) {
  162. return -1;
  163. }
  164. if(hdr->magic!=PKT_MAGIC) {
  165. LOGE("___ magic: 0x%x is wrong!, correct is: 0x%x\n", hdr->magic, PKT_MAGIC);
  166. return -1;
  167. }
  168. if(hdr->addr>0 && h->cur_addr!=hdr->addr) {
  169. //paras_data_t *p=paras_get();
  170. //cascade_reinit();
  171. }
  172. //LOGD("_____ ws handle, type: %d, flag: %d\n", hdr->type, hdr->flag);
  173. switch(hdr->type) {
  174. case PKT_TYPE_PARAS:
  175. {
  176. paras_data_t* para=paras_get();
  177. switch(hdr->subtype) {
  178. case SUB_TYPE_PARAS_LANG:
  179. {
  180. if(hdr->flag<LANG_ID_MAX) {
  181. para->lang = hdr->flag;
  182. paras_save();
  183. }
  184. }
  185. break;
  186. case SUB_TYPE_PARAS_MODBUS:
  187. {
  188. modbus_data_t *p=(modbus_data_t*)hdr->data;
  189. cascade_reinit(p->mode, p->addr, p->baud);
  190. para->cas = *p;
  191. paras_save();
  192. }
  193. break;
  194. case SUB_TYPE_PARAS_MISC:
  195. {
  196. misc_data_t *p=(misc_data_t*)hdr->data;
  197. if(p->rotate!=para->misc.rotate) {
  198. lcd_rotate(p->rotate);
  199. }
  200. para->misc = *p;
  201. paras_save();
  202. }
  203. break;
  204. case SUB_TYPE_PARAS_PROD:
  205. {
  206. product_data_t *p=(product_data_t*)hdr->data;
  207. }
  208. break;
  209. case SUB_TYPE_PARAS_SERV:
  210. {
  211. service_data_t *p=(service_data_t*)hdr->data;
  212. }
  213. break;
  214. case SUB_TYPE_PARAS_NETWORK:
  215. {
  216. network_data_t *p=(network_data_t*)hdr->data;
  217. extern void set_dns(uint8_t dns_num, char* dns_server);
  218. extern void set_if(char* netif_name, char* ip_addr, char* gw_addr, char* nm_addr);
  219. set_if("e0", p->ipaddr, p->gateway, p->mask);
  220. set_dns(0, p->dns1); set_dns(1, p->dns2);
  221. }
  222. break;
  223. }
  224. }
  225. break;
  226. case PKT_TYPE_POWER_CH:
  227. {
  228. if(hdr->dlen>0) {
  229. power_ch_t *pch=(power_ch_t*)hdr->data;
  230. switch(hdr->subtype) {
  231. case SUB_TYPE_POWER_SWITCH:
  232. {
  233. if(hdr->addr==0) {
  234. r = power_set_ch_sw(pch->info.ch, pch->power[0].status);
  235. }
  236. else {
  237. }
  238. }
  239. break;
  240. case SUB_TYPE_POWER_ALARM:
  241. {
  242. if(hdr->addr==0) {
  243. r = power_set_alarm(pch);
  244. }
  245. else {
  246. }
  247. }
  248. break;
  249. case SUB_TYPE_POWER_THRESHOLD:
  250. {
  251. if(hdr->addr==0) {
  252. r = power_set_threshold(pch);
  253. }
  254. }
  255. break;
  256. case SUB_TYPE_POWER_START_DELAY:
  257. {
  258. if(hdr->addr==0) {
  259. r = power_set_start_delay(pch);
  260. }
  261. else {
  262. }
  263. }
  264. break;
  265. case SUB_TYPE_POWER_STOP_DELAY:
  266. {
  267. if(hdr->addr==0) {
  268. r = power_set_stop_delay(pch);
  269. }
  270. else {
  271. }
  272. }
  273. break;
  274. }
  275. }
  276. else {
  277. send_powerc(h, c, hdr->addr);
  278. }
  279. }
  280. break;
  281. case PKT_TYPE_POWER_TOTAL:
  282. {
  283. LOGD("_____ send_powert\n");
  284. send_powert(h, c, hdr->addr);
  285. }
  286. break;
  287. case PKT_TYPE_CH_ALL:
  288. {
  289. if(hdr->addr==0) {
  290. power_set_all_sw(hdr->flag);
  291. }
  292. else {
  293. }
  294. }
  295. break;
  296. case PKT_TYPE_POWER_RESET:
  297. {
  298. if(hdr->addr==0) {
  299. r = power_reset();
  300. }
  301. else {
  302. }
  303. }
  304. break;
  305. case PKT_TYPE_SCAN:
  306. {
  307. if(hdr->addr==0) {
  308. r = power_scan();
  309. }
  310. else {
  311. }
  312. }
  313. break;
  314. case PKT_TYPE_FACTORY:
  315. {
  316. if(hdr->addr==0) {
  317. //r = power_scan();
  318. }
  319. else {
  320. }
  321. }
  322. break;
  323. case PKT_TYPE_SENSOR:
  324. {
  325. }
  326. break;
  327. case PKT_TYPE_BREAKER:
  328. {
  329. }
  330. break;
  331. case PKT_TYPE_FILE:
  332. {
  333. }
  334. break;
  335. case PKT_TYPE_SLAVE_LIST:
  336. {
  337. send_dlist(h, c);
  338. }
  339. break;
  340. }
  341. }
  342. static void http_fn(mg_conn_t *c, int ev, void *ev_data)
  343. {
  344. int r,flag=0;
  345. web_handle_t *h=&webHandle;
  346. #ifdef USE_MG716
  347. if (ev == MG_EV_HTTP_MSG) {
  348. struct mg_http_message *hm = (struct mg_http_message *) ev_data;
  349. if (mg_match(hm->uri, mg_str("/ws"), NULL)) {
  350. mg_ws_upgrade(c, hm, NULL);
  351. } else if (mg_match(hm->uri, mg_str("/rest"), NULL)) {
  352. // Serve REST response
  353. mg_http_reply(c, 200, "", "{\"result\": %d}\n", 123);
  354. } else {
  355. struct mg_http_serve_opts opts = {.root_dir = h->rootDir};
  356. mg_http_serve_dir(c, hm, &opts);
  357. }
  358. }
  359. else if(ev == MG_EV_ACCEPT) {
  360. if (c->fn_data) {
  361. mg_tls_init(c, &h->tls);
  362. }
  363. paras_data_t *p=paras_get();
  364. ws_send_pkt(h, c, 0, PKT_TYPE_PARAS, p, sizeof(paras_data_t), 1);
  365. }
  366. else if(ev == MG_EV_WS_MSG) {
  367. struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
  368. pkt_hdr_t *hdr=(pkt_hdr_t*)wm->data.buf;
  369. ws_handle(h, hdr, c);
  370. }
  371. #else
  372. if (ev == MG_EV_HTTP_REQUEST) {
  373. mg_serve_http(c, (struct http_message *)ev_data, h->opts);
  374. }
  375. else if(ev == MG_EV_WEBSOCKET_HANDSHAKE_DONE) {
  376. paras_data_t *p=paras_get();
  377. ws_send_pkt(h, c, 0, PKT_TYPE_PARAS, p, sizeof(paras_data_t), 1);
  378. set_timer(c, TIMER_INTERVAL);
  379. }
  380. else if(ev == MG_EV_WEBSOCKET_FRAME) {
  381. struct websocket_message *wm = (struct websocket_message *) ev_data;
  382. pkt_hdr_t *hdr=(pkt_hdr_t*)wm->data;
  383. ws_handle(h, hdr, c);
  384. }
  385. else if(ev==MG_EV_TIMER) {
  386. list_node_t *ln=NULL;
  387. r = list_take_node(h->list, &ln, 0);
  388. if(r==0) {
  389. node_t *n=&ln->data;
  390. ws_send_pkt(h, c, 0, n->tp, n->buf, n->dlen, 1);
  391. list_back_node(h->list, ln);
  392. }
  393. }
  394. #endif
  395. }
  396. static void web_thread(void *arg)
  397. {
  398. mg_conn_t *nc;
  399. const char *http_addr="http://[::]:80";
  400. const char *https_addr="https://[::]:443";
  401. thread_handle_t *th=(thread_handle_t*)arg;
  402. web_handle_t *h=(web_handle_t*)th->attr->arg;
  403. strcpy(h->rootDir, "/web");
  404. #ifdef USE_MG716
  405. mg_log_set(MG_LL_VERBOSE);
  406. mg_mgr_init(&h->mgr);
  407. mg_timer_add(&h->mgr, TIMER_INTERVAL, MG_TIMER_REPEAT, timer_fn, h);
  408. if (mg_http_listen(&h->mgr, http_addr, http_fn, NULL) == NULL) {
  409. MG_ERROR(("cannot listen on %s\n", http_addr));
  410. }
  411. //if (mg_http_listen(&h->mgr, https_addr, http_fn, (void*)1) == NULL) {
  412. // MG_ERROR(("cannot listen on %s\n", https_addr));
  413. //}
  414. //mh->tls.cert = mg_str(file_load2("/root/run/app/cert/server.crt",0));
  415. //mh->tls.key = mg_str(file_load2("/root/run/app/cert/server.key",0));
  416. //mh->tls.name = mg_url_host(https_addr);
  417. #else
  418. mg_mgr_init(&h->mgr, NULL);
  419. nc = mg_bind(&h->mgr, "80", http_fn);
  420. strcpy(h->rootDir, "/web");
  421. mg_set_protocol_http_websocket(nc);
  422. h->opts.document_root = h->rootDir;
  423. h->opts.enable_directory_listing = "yes";
  424. #endif
  425. while(th->quit==0) {
  426. mg_mgr_poll(&h->mgr, 300);
  427. }
  428. mg_mgr_free(&h->mgr);
  429. }
  430. int web_init(void)
  431. {
  432. list_cfg_t lc={0,0,10};
  433. web_handle_t *h=&webHandle;
  434. memset(h, 0, sizeof(web_handle_t));
  435. h->list = list_init(&lc);
  436. h->buflen = sizeof(pkt_hdr_t)+sizeof(power_ch_t)*ONCE_SEND_CH_CNT+8;
  437. h->buf = (char*)malloc(h->buflen);
  438. thread_start(THREAD_ID_WEB, web_thread, h);
  439. return 0;
  440. }
  441. int web_post(int type, void *data, int len)
  442. {
  443. struct mg_connection *c;
  444. web_handle_t *h=&webHandle;
  445. return list_append(h->list, type, data, len);
  446. }