web.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 "cfg.h"
  8. #include <stdio.h>
  9. #include "datadef.h"
  10. #define TIMER_INTERVAL 1000
  11. #define ONCE_SEND_CH_CNT 10
  12. //#define USE_MG716
  13. typedef struct mg_mgr mg_mgr_t;
  14. typedef struct mg_connection mg_conn_t;
  15. #ifdef USE_MG716
  16. typedef struct mg_tls_opts mg_tls_t;
  17. static void timer_fn(void *arg)
  18. {
  19. int r;
  20. web_handle_t *h=(web_handle_t*)arg;
  21. list_node_t *ln=NULL;
  22. mg_conn_t* c;
  23. r = list_take_node(h->list, &ln, 0);
  24. if(r==0) {
  25. node_t *n=&ln->data;
  26. for (c = h->mgr.conns; c != NULL; c = c->next) {
  27. ws_send_pkt(h, c, n->tp, n->buf, n->dlen, 1);
  28. }
  29. list_back_node(h->list, ln);
  30. }
  31. }
  32. #else
  33. typedef struct mg_serve_http_opts mg_opts_t;
  34. #define MG_PATH_MAX 128
  35. static void set_timer(struct mg_connection *c, int ms)
  36. {
  37. mg_set_timer(c, mg_time()+ms/1000.0f);
  38. }
  39. #endif
  40. typedef struct {
  41. struct mg_connection *c;
  42. int ev;
  43. void *ev_data;
  44. }mg_stream_t;
  45. typedef struct {
  46. pthread_t tid;
  47. mg_mgr_t mgr;
  48. #ifdef USE_MG716
  49. mg_tls_t tls;
  50. #else
  51. mg_opts_t opts;
  52. #endif
  53. handle_t list;
  54. char rootDir[MG_PATH_MAX];
  55. char *buf;
  56. int buflen;
  57. }web_handle_t;
  58. static web_handle_t webHandle={0};
  59. static int is_ws(mg_conn_t *c)
  60. {
  61. int r=0;
  62. #ifdef USE_MG716
  63. r = c->is_websocket?1:0;
  64. #else
  65. r = (c->flags & MG_F_IS_WEBSOCKET)?1:0;
  66. #endif
  67. return r;
  68. }
  69. static int ws_send(mg_conn_t *c, void *data, int len, int isbin)
  70. {
  71. if(!c || !data || len<=0) {
  72. return -1;
  73. }
  74. LOGD("____ws_send, len: %d\n", len);
  75. #ifdef USE_MG716
  76. mg_ws_send(c, data, len, isbin?WEBSOCKET_OP_BINARY:WEBSOCKET_OP_TEXT);
  77. #else
  78. mg_send_websocket_frame(c, isbin?WEBSOCKET_OP_BINARY:WEBSOCKET_OP_TEXT, data, len);
  79. #endif
  80. return 0;
  81. }
  82. static int ws_send_pkt(web_handle_t *h, mg_conn_t *c, uint8_t type, void *data, int len, int isbin)
  83. {
  84. pkt_hdr_t *hdr=NULL;
  85. if(!h->buf || !len || h->buflen<sizeof(pkt_hdr_t)+len) {
  86. LOGE("____ws_send, para are invalid\n");
  87. return -1;
  88. }
  89. hdr = (pkt_hdr_t*)h->buf;
  90. hdr->magic = PKT_MAGIC;
  91. hdr->type = type;
  92. hdr->nack = 0;
  93. hdr->dlen = len;
  94. memcpy(hdr->data, data, len);
  95. ws_send(c, hdr, len+sizeof(pkt_hdr_t), isbin);
  96. return 0;
  97. }
  98. static void send_powerc(web_handle_t *h, mg_conn_t *c)
  99. {
  100. int i,j,xlen,offset,times,left;
  101. pkt_hdr_t *hdr=NULL;
  102. power_data_t pd=power_data_get();
  103. if(pd.chs==0 || h->buf==NULL ) {
  104. return;
  105. }
  106. hdr = (pkt_hdr_t*)h->buf;
  107. hdr->magic = PKT_MAGIC;
  108. hdr->type = PKT_TYPE_POWER_CH;
  109. hdr->nack = 0;
  110. times = pd.chs/ONCE_SEND_CH_CNT;
  111. for(i=0; i<times; i++) {
  112. for(j=0; j<ONCE_SEND_CH_CNT; j++) {
  113. offset = j*sizeof(power_ch_t);
  114. memcpy(hdr->data+offset, &pd.pch[i*ONCE_SEND_CH_CNT+j], sizeof(power_ch_t));
  115. }
  116. xlen = ONCE_SEND_CH_CNT*sizeof(power_ch_t)+sizeof(pkt_hdr_t);
  117. hdr->dlen = xlen-sizeof(pkt_hdr_t);
  118. ws_send(c, h->buf, xlen, 1);
  119. }
  120. left = pd.chs%ONCE_SEND_CH_CNT;
  121. for(i=0; i<left; i++) {
  122. offset = i*sizeof(power_ch_t);
  123. memcpy(hdr->data+offset, &pd.pch[pd.chs-left+i], sizeof(power_ch_t));
  124. }
  125. xlen = (pd.chs%ONCE_SEND_CH_CNT)*sizeof(power_ch_t)+sizeof(pkt_hdr_t);
  126. hdr->dlen = xlen-sizeof(pkt_hdr_t);
  127. ws_send(c, h->buf, xlen, 1);
  128. }
  129. static void send_powert(web_handle_t *h, mg_conn_t *c)
  130. {
  131. int i,r;
  132. power_data_t pd=power_data_get();
  133. ws_send_pkt(h, c, PKT_TYPE_POWER_TOTAL, &pd.ttl, sizeof(power_total_t), 1);
  134. }
  135. static int ws_handle(web_handle_t *h, pkt_hdr_t *hdr, mg_conn_t *c)
  136. {
  137. int r=0;
  138. if (!is_ws(c)) {
  139. return -1;
  140. }
  141. if(hdr->magic!=PKT_MAGIC) {
  142. LOGE("___ magic: 0x%x is wrong!, correct is: 0x%x\n", hdr->magic, PKT_MAGIC);
  143. return -1;
  144. }
  145. //LOGD("_____ ws handle, type: %d, flag: %d\n", hdr->type, hdr->flag);
  146. switch(hdr->type) {
  147. case PKT_TYPE_PARAS:
  148. {
  149. //setup
  150. }
  151. break;
  152. case PKT_TYPE_POWER_CH:
  153. {
  154. if(hdr->dlen>0) {
  155. power_ch_t *pch=(power_ch_t*)hdr->data;
  156. switch(hdr->subtype) {
  157. case SUB_TYPE_POWER_SWITCH:
  158. {
  159. r = power_set_ch_sw(pch->info.ch, pch->power[0].status);
  160. }
  161. break;
  162. case SUB_TYPE_POWER_ALARM:
  163. {
  164. r = power_set_alarm(pch);
  165. }
  166. break;
  167. case SUB_TYPE_POWER_THRESHOLD:
  168. {
  169. r = power_set_threshold(pch);
  170. }
  171. break;
  172. case SUB_TYPE_POWER_START_DELAY:
  173. {
  174. r = power_set_start_delay(pch);
  175. }
  176. break;
  177. case SUB_TYPE_POWER_STOP_DELAY:
  178. {
  179. r = power_set_stop_delay(pch);
  180. }
  181. break;
  182. }
  183. }
  184. else {
  185. LOGD("_____ send_powerc\n");
  186. send_powerc(h, c);
  187. }
  188. }
  189. break;
  190. case PKT_TYPE_POWER_TOTAL:
  191. {
  192. LOGD("_____ send_powert\n");
  193. send_powert(h, c);
  194. }
  195. break;
  196. case PKT_TYPE_CH_ALL:
  197. {
  198. power_set_all_sw(hdr->flag);
  199. }
  200. break;
  201. case PKT_TYPE_POWER_RESET:
  202. {
  203. r = power_reset();
  204. }
  205. break;
  206. case PKT_TYPE_SCAN:
  207. {
  208. r = power_scan();
  209. }
  210. break;
  211. case PKT_TYPE_FACTORY:
  212. {
  213. //r = power_scan();
  214. }
  215. break;
  216. case PKT_TYPE_SENSOR:
  217. {
  218. }
  219. break;
  220. case PKT_TYPE_BREAKER:
  221. {
  222. }
  223. break;
  224. case PKT_TYPE_FILE:
  225. {
  226. }
  227. break;
  228. }
  229. }
  230. static void http_fn(mg_conn_t *c, int ev, void *ev_data)
  231. {
  232. int r,flag=0;
  233. web_handle_t *h=&webHandle;
  234. #ifdef USE_MG716
  235. if (ev == MG_EV_HTTP_MSG) {
  236. struct mg_http_message *hm = (struct mg_http_message *) ev_data;
  237. if (mg_match(hm->uri, mg_str("/ws"), NULL)) {
  238. mg_ws_upgrade(c, hm, NULL);
  239. } else if (mg_match(hm->uri, mg_str("/rest"), NULL)) {
  240. // Serve REST response
  241. mg_http_reply(c, 200, "", "{\"result\": %d}\n", 123);
  242. } else {
  243. struct mg_http_serve_opts opts = {.root_dir = h->rootDir};
  244. mg_http_serve_dir(c, hm, &opts);
  245. }
  246. }
  247. else if(ev == MG_EV_ACCEPT) {
  248. if (c->fn_data) {
  249. mg_tls_init(c, &h->tls);
  250. }
  251. }
  252. else if(ev == MG_EV_WS_MSG) {
  253. struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
  254. pkt_hdr_t *hdr=(pkt_hdr_t*)wm->data.buf;
  255. ws_handle(h, hdr, c);
  256. }
  257. #else
  258. if (ev == MG_EV_HTTP_REQUEST) {
  259. mg_serve_http(c, (struct http_message *)ev_data, h->opts);
  260. }
  261. else if(ev == MG_EV_WEBSOCKET_HANDSHAKE_DONE) {
  262. paras_data_t *p=paras_get();
  263. //ws_send(h, c, PKT_TYPE_PARAS, p, sizeof(paras_data_t), 1);
  264. set_timer(c, TIMER_INTERVAL);
  265. }
  266. else if(ev == MG_EV_WEBSOCKET_FRAME) {
  267. struct websocket_message *wm = (struct websocket_message *) ev_data;
  268. pkt_hdr_t *hdr=(pkt_hdr_t*)wm->data;
  269. ws_handle(h, hdr, c);
  270. }
  271. else if(ev==MG_EV_TIMER) {
  272. list_node_t *ln=NULL;
  273. r = list_take_node(h->list, &ln, 0);
  274. if(r==0) {
  275. node_t *n=&ln->data;
  276. ws_send_pkt(h, c, n->tp, n->buf, n->dlen, 1);
  277. list_back_node(h->list, ln);
  278. }
  279. }
  280. #endif
  281. }
  282. static void web_thread(void *arg)
  283. {
  284. mg_conn_t *nc;
  285. const char *http_addr="http://[::]:80";
  286. const char *https_addr="https://[::]:443";
  287. thread_handle_t *th=(thread_handle_t*)arg;
  288. web_handle_t *h=(web_handle_t*)th->attr->arg;
  289. strcpy(h->rootDir, "/web");
  290. #ifdef USE_MG716
  291. mg_log_set(MG_LL_VERBOSE);
  292. mg_mgr_init(&h->mgr);
  293. mg_timer_add(&h->mgr, TIMER_INTERVAL, MG_TIMER_REPEAT, timer_fn, h);
  294. if (mg_http_listen(&h->mgr, http_addr, http_fn, NULL) == NULL) {
  295. MG_ERROR(("cannot listen on %s\n", http_addr));
  296. }
  297. //if (mg_http_listen(&h->mgr, https_addr, http_fn, (void*)1) == NULL) {
  298. // MG_ERROR(("cannot listen on %s\n", https_addr));
  299. //}
  300. //mh->tls.cert = mg_str(file_load2("/root/run/app/cert/server.crt",0));
  301. //mh->tls.key = mg_str(file_load2("/root/run/app/cert/server.key",0));
  302. //mh->tls.name = mg_url_host(https_addr);
  303. #else
  304. mg_mgr_init(&h->mgr, NULL);
  305. nc = mg_bind(&h->mgr, "80", http_fn);
  306. strcpy(h->rootDir, "/web");
  307. mg_set_protocol_http_websocket(nc);
  308. h->opts.document_root = h->rootDir;
  309. h->opts.enable_directory_listing = "yes";
  310. #endif
  311. while(th->quit==0) {
  312. mg_mgr_poll(&h->mgr, 300);
  313. }
  314. mg_mgr_free(&h->mgr);
  315. }
  316. int web_init(void)
  317. {
  318. list_cfg_t lc={0,0,10};
  319. web_handle_t *h=&webHandle;
  320. memset(h, 0, sizeof(web_handle_t));
  321. h->list = list_init(&lc);
  322. h->buflen = sizeof(pkt_hdr_t)+sizeof(power_ch_t)*ONCE_SEND_CH_CNT+8;
  323. h->buf = (char*)malloc(h->buflen);
  324. thread_start(THREAD_ID_WEB, web_thread, h);
  325. return 0;
  326. }
  327. int web_post(int type, void *data, int len)
  328. {
  329. struct mg_connection *c;
  330. web_handle_t *h=&webHandle;
  331. return list_append(h->list, type, data, len);
  332. }