web.c 9.7 KB

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