web.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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, hdr, len+sizeof(pkt_hdr_t));
  67. return 0;
  68. }
  69. static void send_powerc(web_handle_t *h, struct mg_connection *c)
  70. {
  71. int i,r;
  72. r = power_data_copy(&h->pdat, 1);
  73. if(r==0) {
  74. for(i=0; i<h->pdat.chs; i++) {
  75. ws_send(h, c, PKT_TYPE_POWER_CH, &h->pdat.pch[i], sizeof(power_ch_t), 1);
  76. }
  77. }
  78. }
  79. static void send_powert(web_handle_t *h, struct mg_connection *c)
  80. {
  81. int i,r;
  82. r = power_data_copy(&h->pdat, 2);
  83. if(r==0) {
  84. ws_send(h, c, PKT_TYPE_POWER_TOTAL, &h->pdat.ttl, sizeof(power_total_t), 1);
  85. }
  86. }
  87. static int ws_handle(web_handle_t *h, pkt_hdr_t *hdr, struct mg_connection *c)
  88. {
  89. int r=0;
  90. if(hdr->magic!=PKT_MAGIC) {
  91. LOGE("___ magic: 0x%x is wrong!, correct is: 0x%x\n", hdr->magic, PKT_MAGIC);
  92. return -1;
  93. }
  94. LOGD("_____ ws handle, type: %d, flag: %d\n", hdr->type, hdr->flag);
  95. switch(hdr->type) {
  96. case PKT_TYPE_PARAS:
  97. {
  98. //setup
  99. }
  100. break;
  101. case PKT_TYPE_POWER_CH:
  102. {
  103. if(hdr->dlen>0) {
  104. power_ch_t *pch=(power_ch_t*)hdr->data;
  105. switch(hdr->subtype) {
  106. case SUB_TYPE_POWER_SWITCH:
  107. {
  108. r = power_set_sw(pch);
  109. }
  110. break;
  111. case SUB_TYPE_POWER_ALARM:
  112. {
  113. r = power_set_alarm(pch);
  114. }
  115. break;
  116. case SUB_TYPE_POWER_THRESHOLD:
  117. {
  118. r = power_set_threshold(pch);
  119. }
  120. break;
  121. case SUB_TYPE_POWER_START_DELAY:
  122. {
  123. r = power_set_start_delay(pch);
  124. }
  125. break;
  126. case SUB_TYPE_POWER_STOP_DELAY:
  127. {
  128. r = power_set_stop_delay(pch);
  129. }
  130. break;
  131. }
  132. }
  133. else {
  134. LOGD("_____ send_powerc\n");
  135. send_powerc(h, c);
  136. }
  137. }
  138. break;
  139. case PKT_TYPE_POWER_TOTAL:
  140. {
  141. send_powert(h, c);
  142. }
  143. break;
  144. case PKT_TYPE_CH_ALL:
  145. {
  146. //hdr->flag;
  147. }
  148. break;
  149. case PKT_TYPE_POWER_RESET:
  150. {
  151. //r = power_reset_consump(addr);
  152. }
  153. break;
  154. case PKT_TYPE_SCAN:
  155. {
  156. //r = power_scan();
  157. }
  158. break;
  159. case PKT_TYPE_FACTORY:
  160. {
  161. //r = power_scan();
  162. }
  163. break;
  164. case PKT_TYPE_SENSOR:
  165. {
  166. }
  167. break;
  168. case PKT_TYPE_BREAKER:
  169. {
  170. }
  171. break;
  172. case PKT_TYPE_FILE:
  173. {
  174. }
  175. break;
  176. }
  177. }
  178. static void http_fn(struct mg_connection *c, int ev, void *ev_data)
  179. {
  180. int r,flag=0;
  181. web_handle_t *h=&webHandle;
  182. mg_stream_t s={c,ev,ev_data};
  183. #ifdef USE_MG714
  184. if (ev == MG_EV_HTTP_MSG) {
  185. struct mg_http_serve_opts opts = {.root_dir = h->rootDir};
  186. mg_http_serve_dir(c, ev_data, &opts);
  187. }
  188. else if(ev == MG_EV_ACCEPT) {
  189. if (c->fn_data) {
  190. mg_tls_init(c, &h->tls);
  191. }
  192. }
  193. #else
  194. if (ev == MG_EV_HTTP_REQUEST) {
  195. mg_serve_http(c, (struct http_message *)ev_data, h->opts);
  196. }
  197. else if(ev == MG_EV_WEBSOCKET_HANDSHAKE_DONE) {
  198. paras_data_t *p=paras_get();
  199. //ws_send(h, c, PKT_TYPE_PARAS, p, sizeof(paras_data_t), 1);
  200. //set_timer(c, TIMER_INTERVAL);
  201. }
  202. else if(ev == MG_EV_WEBSOCKET_FRAME) {
  203. struct websocket_message *wm = (struct websocket_message *) ev_data;
  204. pkt_hdr_t *hdr=(pkt_hdr_t*)wm->data;
  205. ws_handle(h, hdr, c);
  206. }
  207. else if(ev==MG_EV_TIMER) {
  208. list_node_t *ln=NULL;
  209. r = list_take_node(h->list, &ln, 0);
  210. if(r==0) {
  211. node_t *n=&ln->data;
  212. ws_send(h, c, n->tp, n->buf, n->dlen, 1);
  213. list_back_node(list_append, ln);
  214. }
  215. }
  216. #endif
  217. }
  218. static void *web_thread(void *arg)
  219. {
  220. struct mg_connection *nc;
  221. const char *http_addr="http://[::]:80";
  222. const char *https_addr="https://[::]:443";
  223. thread_handle_t *th=(thread_handle_t*)arg;
  224. web_handle_t *h=(web_handle_t*)th->arg;
  225. #ifdef USE_MG714
  226. //mg_log_set(MG_LL_DEBUG);
  227. mg_mgr_init(&mgr);
  228. //sys_get_path(mh->rootDir, WEB_ROOT);
  229. strcpy(h->rootDir, "/web");
  230. if (mg_http_listen(&mgr, http_addr, http_fn, NULL) == NULL) {
  231. MG_ERROR(("cannot listen on %s\n", http_addr));
  232. return NULL;
  233. }
  234. if (mg_http_listen(&mgr, https_addr, http_fn, (void*)1) == NULL) {
  235. MG_ERROR(("cannot listen on %s\n", https_addr));
  236. return NULL;
  237. }
  238. //mh->tls.cert = mg_str(file_load2("/root/run/app/cert/server.crt",0));
  239. //mh->tls.key = mg_str(file_load2("/root/run/app/cert/server.key",0));
  240. //mh->tls.name = mg_url_host(https_addr);
  241. #else
  242. mg_mgr_init(&h->mgr, NULL);
  243. nc = mg_bind(&h->mgr, "80", http_fn);
  244. strcpy(h->rootDir, "/web");
  245. mg_set_protocol_http_websocket(nc);
  246. h->opts.document_root = h->rootDir;
  247. h->opts.enable_directory_listing = "yes";
  248. #endif
  249. while(th->quit==0) {
  250. mg_mgr_poll(&h->mgr, 300);
  251. }
  252. mg_mgr_free(&h->mgr);
  253. pthread_exit(NULL);
  254. }
  255. int web_init(void)
  256. {
  257. list_cfg_t lc={0,0,10};
  258. web_handle_t *h=&webHandle;
  259. memset(h, 0, sizeof(web_handle_t));
  260. h->list = list_init(&lc);
  261. h->buflen = sizeof(pkt_hdr_t)+sizeof(power_ch_t)+32;
  262. h->buf = (char*)malloc(h->buflen);
  263. thread_start(THREAD_ID_WEB, web_thread, h);
  264. return 0;
  265. }
  266. int web_post_bin(int type, void *data, int len)
  267. {
  268. struct mg_connection *c;
  269. web_handle_t *h=&webHandle;
  270. return list_append(h->list, type, data, len);
  271. }