| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- #include "web.h"
- #include "mongoose.h"
- #include "thread.h"
- #include "list.h"
- #include "cfg.h"
- #include <stdio.h>
- #include "datadef.h"
- #define TIMER_INTERVAL 1000
- typedef void (*http_fn_t)(void *arg);
- #ifdef USE_MG714
- typedef struct mg_tls_opts mg_tls_t;
- #else
- typedef struct mg_serve_http_opts mg_opts_t;
- #define MG_PATH_MAX 128
- #endif
- typedef struct {
- struct mg_connection *c;
- int ev;
- void *ev_data;
- }mg_stream_t;
- typedef struct {
- pthread_t tid;
-
- struct mg_mgr mgr;
- #ifdef USE_MG714
- mg_tls_t tls;
- #else
- mg_opts_t opts;
- #endif
-
- handle_t list;
- char rootDir[MG_PATH_MAX];
- }web_handle_t;
- static web_handle_t webHandle={0};
- static void set_timer(struct mg_connection *c, int ms)
- {
- mg_set_timer(c, mg_time()+ms/1000.0f);
- }
- static int ws_send(struct mg_connection *c, void *data, int len, int isbin)
- {
- pkt_hdr_t *h=(pkt_hdr_t*)malloc(len+sizeof(pkt_hdr_t));
- if(!h) {
- return -1;
- }
- h->magic = PKT_MAGIC;
- h->type = PKT_TYPE_POWER_TOTAL;
- h->nack = 0;
- h->dlen = len;
- memcpy(h->data, data, len);
- mg_send_websocket_frame(c, isbin?WEBSOCKET_OP_BINARY:WEBSOCKET_OP_TEXT, h, len+sizeof(pkt_hdr_t));
- free(h);
- return 0;
- }
- static int ws_handle(pkt_hdr_t *hdr)
- {
- switch(hdr->type) {
- case PKT_TYPE_PARAS:
- {
-
- }
- break;
-
- case PKT_TYPE_POWER_CH:
- {
-
- }
- break;
-
- case PKT_TYPE_SENSOR:
- {
-
- }
- break;
-
- case PKT_TYPE_BREAKER:
- {
-
- }
- break;
-
- }
- }
- static void http_fn(struct mg_connection *c, int ev, void *ev_data)
- {
- int r,flag=0;
- web_handle_t *h=&webHandle;
- mg_stream_t s={c,ev,ev_data};
- #ifdef USE_MG714
- if (ev == MG_EV_HTTP_MSG) {
- struct mg_http_serve_opts opts = {.root_dir = h->rootDir};
- mg_http_serve_dir(c, ev_data, &opts);
- }
- else if(ev == MG_EV_ACCEPT) {
- if (c->fn_data) {
- mg_tls_init(c, &h->tls);
- }
- }
- #else
- if (ev == MG_EV_HTTP_REQUEST) {
- mg_serve_http(c, (struct http_message *)ev_data, h->opts);
- }
- else if(ev == MG_EV_WEBSOCKET_HANDSHAKE_DONE) {
- set_timer(c, TIMER_INTERVAL);
- }
- else if(ev == MG_EV_WEBSOCKET_FRAME) {
- struct websocket_message *wm = (struct websocket_message *) ev_data;
- pkt_hdr_t *pkt=(pkt_hdr_t*)wm->data;
- ws_handle(pkt);
- }
- else if(ev==MG_EV_TIMER) {
- list_node_t *ln=NULL;
- r = list_take_node(h->list, &ln, 0);
- if(r==0) {
- node_t *n=&ln->data;
- ws_send(c, n->buf, n->dlen, 1);
- list_back_node(list_append, ln);
- }
- }
- #endif
- }
- static void *web_thread(void *arg)
- {
- struct mg_connection *nc;
- const char *http_addr="http://[::]:80";
- const char *https_addr="https://[::]:443";
- thread_handle_t *th=(thread_handle_t*)arg;
- web_handle_t *h=(web_handle_t*)th->arg;
- #ifdef USE_MG714
- //mg_log_set(MG_LL_DEBUG);
- mg_mgr_init(&mgr);
- //sys_get_path(mh->rootDir, WEB_ROOT);
- strcpy(h->rootDir, "/web");
- if (mg_http_listen(&mgr, http_addr, http_fn, NULL) == NULL) {
- MG_ERROR(("cannot listen on %s\n", http_addr));
- return NULL;
- }
- if (mg_http_listen(&mgr, https_addr, http_fn, (void*)1) == NULL) {
- MG_ERROR(("cannot listen on %s\n", https_addr));
- return NULL;
- }
-
- //mh->tls.cert = mg_str(file_load2("/root/run/app/cert/server.crt",0));
- //mh->tls.key = mg_str(file_load2("/root/run/app/cert/server.key",0));
- //mh->tls.name = mg_url_host(https_addr);
- #else
- mg_mgr_init(&h->mgr, NULL);
- nc = mg_bind(&h->mgr, "80", http_fn);
- strcpy(h->rootDir, "/web");
-
- mg_set_protocol_http_websocket(nc);
- h->opts.document_root = h->rootDir;
- h->opts.enable_directory_listing = "yes";
- #endif
-
- while(th->quit==0) {
- mg_mgr_poll(&h->mgr, 200);
- }
- mg_mgr_free(&h->mgr);
-
- pthread_exit(NULL);
- }
- int web_init(void)
- {
- list_cfg_t lc={0,0,10};
- web_handle_t *h=&webHandle;
-
- h->list = list_init(&lc);
- thread_start(THREAD_ID_WEB, web_thread, h);
- return 0;
- }
- int web_send(void *data, int len, int isbin)
- {
- struct mg_connection *c;
- web_handle_t *h=&webHandle;
-
- for (c = mg_next(&h->mgr, NULL); c != NULL; c = mg_next(&h->mgr, c)) {
- ws_send(c, data, len, isbin);
- }
-
- return 0;
- }
|