web.c 9.7 KB

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