web.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. typedef void (*http_fn_t)(void *arg);
  12. #ifdef USE_MG714
  13. typedef struct mg_tls_opts mg_tls_t;
  14. #else
  15. typedef struct mg_serve_http_opts mg_opts_t;
  16. #define MG_PATH_MAX 128
  17. #endif
  18. typedef struct {
  19. struct mg_connection *c;
  20. int ev;
  21. void *ev_data;
  22. }mg_stream_t;
  23. typedef struct {
  24. pthread_t tid;
  25. struct mg_mgr mgr;
  26. #ifdef USE_MG714
  27. mg_tls_t tls;
  28. #else
  29. mg_opts_t opts;
  30. #endif
  31. power_data_t pdat;
  32. handle_t list;
  33. char rootDir[MG_PATH_MAX];
  34. char *buf;
  35. int buflen;
  36. }web_handle_t;
  37. static web_handle_t webHandle={0};
  38. static void set_timer(struct mg_connection *c, int ms)
  39. {
  40. mg_set_timer(c, mg_time()+ms/1000.0f);
  41. }
  42. static int ws_send(web_handle_t *h, struct mg_connection *c, uint8_t type, void *data, int len, int isbin)
  43. {
  44. pkt_hdr_t *hdr=NULL;
  45. if(!h->buf) {
  46. LOGE("____ h->buf is NULL\n");
  47. return -1;
  48. }
  49. if(h->buflen<sizeof(pkt_hdr_t)+len) {
  50. h->buf = realloc(h->buf, sizeof(pkt_hdr_t)+len);
  51. if(h->buf) {
  52. h->buflen = sizeof(pkt_hdr_t)+len;
  53. }
  54. else {
  55. h->buflen = 0;
  56. LOGE("____ realloc failed\n");
  57. return -1;
  58. }
  59. }
  60. hdr = (pkt_hdr_t*)h->buf;
  61. hdr->magic = PKT_MAGIC;
  62. hdr->type = type;
  63. hdr->nack = 0;
  64. hdr->dlen = len;
  65. memcpy(hdr->data, data, len);
  66. mg_send_websocket_frame(c, isbin?WEBSOCKET_OP_BINARY:WEBSOCKET_OP_TEXT, h, len+sizeof(pkt_hdr_t));
  67. free(h);
  68. return 0;
  69. }
  70. static int send_power(web_handle_t *h, struct mg_connection *c)
  71. {
  72. int i,r;
  73. r = power_data_clone(&h->pdat);
  74. if(r==0) {
  75. for(i=0; i<h->pdat.chs; i++) {
  76. ws_send(h, c, PKT_TYPE_POWER_CH, &h->pdat.pch[i], sizeof(power_ch_t), 1);
  77. }
  78. ws_send(h, c, PKT_TYPE_POWER_TOTAL, &h->pdat.ttl, sizeof(power_total_t), 1);
  79. }
  80. }
  81. static int ws_handle(web_handle_t *h, pkt_hdr_t *hdr)
  82. {
  83. int r=0;
  84. if(hdr->magic!=PKT_MAGIC) {
  85. LOGE("___ magic: 0x%x is wrong!, correct is: 0x%x\n", hdr->magic, PKT_MAGIC);
  86. return -1;
  87. }
  88. switch(hdr->type) {
  89. case PKT_TYPE_PARAS:
  90. {
  91. //setup
  92. }
  93. break;
  94. case PKT_TYPE_POWER_CH:
  95. {
  96. power_ch_t *pch=(power_ch_t*)hdr->data;
  97. switch(hdr->subtype) {
  98. case SUB_TYPE_POWER_SWITCH:
  99. {
  100. r = power_set_sw(pch);
  101. }
  102. break;
  103. case SUB_TYPE_POWER_ALARM:
  104. {
  105. r = power_set_alarm(pch);
  106. }
  107. break;
  108. case SUB_TYPE_POWER_THRESHOLD:
  109. {
  110. r = power_set_threshold(pch);
  111. }
  112. break;
  113. case SUB_TYPE_POWER_START_DELAY:
  114. {
  115. r = power_set_start_delay(pch);
  116. }
  117. break;
  118. case SUB_TYPE_POWER_STOP_DELAY:
  119. {
  120. r = power_set_stop_delay(pch);
  121. }
  122. break;
  123. }
  124. }
  125. break;
  126. case PKT_TYPE_CH_ALL:
  127. {
  128. power_ch_t *pch=(power_ch_t*)hdr->data;
  129. switch(hdr->subtype) {
  130. case SUB_TYPE_CONSUMP_RESET:
  131. {
  132. //r = power_reset_consump(addr);
  133. }
  134. break;
  135. case SUB_TYPE_BOARD_SCAN:
  136. {
  137. r = power_scan();
  138. }
  139. break;
  140. }
  141. }
  142. break;
  143. case PKT_TYPE_SENSOR:
  144. {
  145. }
  146. break;
  147. case PKT_TYPE_BREAKER:
  148. {
  149. }
  150. break;
  151. case PKT_TYPE_FILE:
  152. {
  153. }
  154. break;
  155. }
  156. }
  157. static void http_fn(struct mg_connection *c, int ev, void *ev_data)
  158. {
  159. int r,flag=0;
  160. web_handle_t *h=&webHandle;
  161. mg_stream_t s={c,ev,ev_data};
  162. #ifdef USE_MG714
  163. if (ev == MG_EV_HTTP_MSG) {
  164. struct mg_http_serve_opts opts = {.root_dir = h->rootDir};
  165. mg_http_serve_dir(c, ev_data, &opts);
  166. }
  167. else if(ev == MG_EV_ACCEPT) {
  168. if (c->fn_data) {
  169. mg_tls_init(c, &h->tls);
  170. }
  171. }
  172. #else
  173. if (ev == MG_EV_HTTP_REQUEST) {
  174. mg_serve_http(c, (struct http_message *)ev_data, h->opts);
  175. }
  176. else if(ev == MG_EV_WEBSOCKET_HANDSHAKE_DONE) {
  177. paras_data_t *p=paras_get();
  178. ws_send(h, c, PKT_TYPE_PARAS, p, sizeof(paras_data_t), 1);
  179. set_timer(c, TIMER_INTERVAL);
  180. }
  181. else if(ev == MG_EV_WEBSOCKET_FRAME) {
  182. struct websocket_message *wm = (struct websocket_message *) ev_data;
  183. pkt_hdr_t *pkt=(pkt_hdr_t*)wm->data;
  184. ws_handle(h, pkt);
  185. }
  186. else if(ev==MG_EV_TIMER) {
  187. list_node_t *ln=NULL;
  188. r = list_take_node(h->list, &ln, 0);
  189. if(r==0) {
  190. node_t *n=&ln->data;
  191. ws_send(h, c, n->tp, n->buf, n->dlen, 1);
  192. list_back_node(list_append, ln);
  193. }
  194. }
  195. #endif
  196. }
  197. static void *web_thread(void *arg)
  198. {
  199. struct mg_connection *nc;
  200. const char *http_addr="http://[::]:80";
  201. const char *https_addr="https://[::]:443";
  202. thread_handle_t *th=(thread_handle_t*)arg;
  203. web_handle_t *h=(web_handle_t*)th->arg;
  204. #ifdef USE_MG714
  205. //mg_log_set(MG_LL_DEBUG);
  206. mg_mgr_init(&mgr);
  207. //sys_get_path(mh->rootDir, WEB_ROOT);
  208. strcpy(h->rootDir, "/web");
  209. if (mg_http_listen(&mgr, http_addr, http_fn, NULL) == NULL) {
  210. MG_ERROR(("cannot listen on %s\n", http_addr));
  211. return NULL;
  212. }
  213. if (mg_http_listen(&mgr, https_addr, http_fn, (void*)1) == NULL) {
  214. MG_ERROR(("cannot listen on %s\n", https_addr));
  215. return NULL;
  216. }
  217. //mh->tls.cert = mg_str(file_load2("/root/run/app/cert/server.crt",0));
  218. //mh->tls.key = mg_str(file_load2("/root/run/app/cert/server.key",0));
  219. //mh->tls.name = mg_url_host(https_addr);
  220. #else
  221. mg_mgr_init(&h->mgr, NULL);
  222. nc = mg_bind(&h->mgr, "80", http_fn);
  223. strcpy(h->rootDir, "/web");
  224. mg_set_protocol_http_websocket(nc);
  225. h->opts.document_root = h->rootDir;
  226. h->opts.enable_directory_listing = "yes";
  227. #endif
  228. while(th->quit==0) {
  229. mg_mgr_poll(&h->mgr, 200);
  230. }
  231. mg_mgr_free(&h->mgr);
  232. pthread_exit(NULL);
  233. }
  234. int web_init(void)
  235. {
  236. list_cfg_t lc={0,0,10};
  237. web_handle_t *h=&webHandle;
  238. memset(h, 0, sizeof(web_handle_t));
  239. h->list = list_init(&lc);
  240. h->buflen = sizeof(pkt_hdr_t)+sizeof(power_ch_t)+32;
  241. h->buf = (char*)malloc(h->buflen);
  242. thread_start(THREAD_ID_WEB, web_thread, h);
  243. return 0;
  244. }
  245. int web_send_bin(int type, void *data, int len)
  246. {
  247. struct mg_connection *c;
  248. web_handle_t *h=&webHandle;
  249. for (c = mg_next(&h->mgr, NULL); c != NULL; c = mg_next(&h->mgr, c)) {
  250. ws_send(h, c, type, data, len, 1);
  251. }
  252. return 0;
  253. }
  254. int web_post_bin(int type, void *data, int len)
  255. {
  256. struct mg_connection *c;
  257. web_handle_t *h=&webHandle;
  258. return list_append(h->list, type, data, len);
  259. }