web.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include "web.h"
  2. #include "mongoose.h"
  3. #include "thread.h"
  4. #include "list.h"
  5. #include "cfg.h"
  6. #include <stdio.h>
  7. #include "datadef.h"
  8. #define TIMER_INTERVAL 1000
  9. typedef void (*http_fn_t)(void *arg);
  10. #ifdef USE_MG714
  11. typedef struct mg_tls_opts mg_tls_t;
  12. #else
  13. typedef struct mg_serve_http_opts mg_opts_t;
  14. #define MG_PATH_MAX 128
  15. #endif
  16. typedef struct {
  17. struct mg_connection *c;
  18. int ev;
  19. void *ev_data;
  20. }mg_stream_t;
  21. typedef struct {
  22. pthread_t tid;
  23. struct mg_mgr mgr;
  24. #ifdef USE_MG714
  25. mg_tls_t tls;
  26. #else
  27. mg_opts_t opts;
  28. #endif
  29. handle_t list;
  30. char rootDir[MG_PATH_MAX];
  31. }web_handle_t;
  32. static web_handle_t webHandle={0};
  33. static void set_timer(struct mg_connection *c, int ms)
  34. {
  35. mg_set_timer(c, mg_time()+ms/1000.0f);
  36. }
  37. static int ws_send(struct mg_connection *c, void *data, int len, int isbin)
  38. {
  39. pkt_hdr_t *h=(pkt_hdr_t*)malloc(len+sizeof(pkt_hdr_t));
  40. if(!h) {
  41. return -1;
  42. }
  43. h->magic = PKT_MAGIC;
  44. h->type = PKT_TYPE_POWER_TOTAL;
  45. h->nack = 0;
  46. h->dlen = len;
  47. memcpy(h->data, data, len);
  48. mg_send_websocket_frame(c, isbin?WEBSOCKET_OP_BINARY:WEBSOCKET_OP_TEXT, h, len+sizeof(pkt_hdr_t));
  49. free(h);
  50. return 0;
  51. }
  52. static int ws_handle(pkt_hdr_t *hdr)
  53. {
  54. switch(hdr->type) {
  55. case PKT_TYPE_PARAS:
  56. {
  57. }
  58. break;
  59. case PKT_TYPE_POWER_CH:
  60. {
  61. }
  62. break;
  63. case PKT_TYPE_SENSOR:
  64. {
  65. }
  66. break;
  67. case PKT_TYPE_BREAKER:
  68. {
  69. }
  70. break;
  71. }
  72. }
  73. static void http_fn(struct mg_connection *c, int ev, void *ev_data)
  74. {
  75. int r,flag=0;
  76. web_handle_t *h=&webHandle;
  77. mg_stream_t s={c,ev,ev_data};
  78. #ifdef USE_MG714
  79. if (ev == MG_EV_HTTP_MSG) {
  80. struct mg_http_serve_opts opts = {.root_dir = h->rootDir};
  81. mg_http_serve_dir(c, ev_data, &opts);
  82. }
  83. else if(ev == MG_EV_ACCEPT) {
  84. if (c->fn_data) {
  85. mg_tls_init(c, &h->tls);
  86. }
  87. }
  88. #else
  89. if (ev == MG_EV_HTTP_REQUEST) {
  90. mg_serve_http(c, (struct http_message *)ev_data, h->opts);
  91. }
  92. else if(ev == MG_EV_WEBSOCKET_HANDSHAKE_DONE) {
  93. set_timer(c, TIMER_INTERVAL);
  94. }
  95. else if(ev == MG_EV_WEBSOCKET_FRAME) {
  96. struct websocket_message *wm = (struct websocket_message *) ev_data;
  97. pkt_hdr_t *pkt=(pkt_hdr_t*)wm->data;
  98. ws_handle(pkt);
  99. }
  100. else if(ev==MG_EV_TIMER) {
  101. list_node_t *ln=NULL;
  102. r = list_take_node(h->list, &ln, 0);
  103. if(r==0) {
  104. node_t *n=&ln->data;
  105. ws_send(c, n->buf, n->dlen, 1);
  106. list_back_node(list_append, ln);
  107. }
  108. }
  109. #endif
  110. }
  111. static void *web_thread(void *arg)
  112. {
  113. struct mg_connection *nc;
  114. const char *http_addr="http://[::]:80";
  115. const char *https_addr="https://[::]:443";
  116. thread_handle_t *th=(thread_handle_t*)arg;
  117. web_handle_t *h=(web_handle_t*)th->arg;
  118. #ifdef USE_MG714
  119. //mg_log_set(MG_LL_DEBUG);
  120. mg_mgr_init(&mgr);
  121. //sys_get_path(mh->rootDir, WEB_ROOT);
  122. strcpy(h->rootDir, "/web");
  123. if (mg_http_listen(&mgr, http_addr, http_fn, NULL) == NULL) {
  124. MG_ERROR(("cannot listen on %s\n", http_addr));
  125. return NULL;
  126. }
  127. if (mg_http_listen(&mgr, https_addr, http_fn, (void*)1) == NULL) {
  128. MG_ERROR(("cannot listen on %s\n", https_addr));
  129. return NULL;
  130. }
  131. //mh->tls.cert = mg_str(file_load2("/root/run/app/cert/server.crt",0));
  132. //mh->tls.key = mg_str(file_load2("/root/run/app/cert/server.key",0));
  133. //mh->tls.name = mg_url_host(https_addr);
  134. #else
  135. mg_mgr_init(&h->mgr, NULL);
  136. nc = mg_bind(&h->mgr, "80", http_fn);
  137. strcpy(h->rootDir, "/web");
  138. mg_set_protocol_http_websocket(nc);
  139. h->opts.document_root = h->rootDir;
  140. h->opts.enable_directory_listing = "yes";
  141. #endif
  142. while(th->quit==0) {
  143. mg_mgr_poll(&h->mgr, 200);
  144. }
  145. mg_mgr_free(&h->mgr);
  146. pthread_exit(NULL);
  147. }
  148. int web_init(void)
  149. {
  150. list_cfg_t lc={0,0,10};
  151. web_handle_t *h=&webHandle;
  152. h->list = list_init(&lc);
  153. thread_start(THREAD_ID_WEB, web_thread, h);
  154. return 0;
  155. }
  156. int web_send(void *data, int len, int isbin)
  157. {
  158. struct mg_connection *c;
  159. web_handle_t *h=&webHandle;
  160. for (c = mg_next(&h->mgr, NULL); c != NULL; c = mg_next(&h->mgr, c)) {
  161. ws_send(c, data, len, isbin);
  162. }
  163. return 0;
  164. }