web.c 8.2 KB

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