web.c 14 KB

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