| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 |
- #include "web.h"
- #include "mongoose.h"
- #include "thread.h"
- #include "list.h"
- #include "power.h"
- #include "paras.h"
- #include "cfg.h"
- #include <stdio.h>
- #include "datadef.h"
- #define TIMER_INTERVAL 1000
- typedef void (*http_fn_t)(void *arg);
- #define USE_MG716
- #ifdef USE_MG716
- 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_MG716
- mg_tls_t tls;
- #else
- mg_opts_t opts;
- #endif
-
- handle_t list;
- char rootDir[MG_PATH_MAX];
- char *buf;
- int buflen;
- }web_handle_t;
- static web_handle_t webHandle={0};
- static int is_ws(struct mg_connection *c)
- {
- int r=0;
- #ifdef USE_MG716
- r = c->is_websocket;
- #else
- r = (c->flags & MG_F_IS_WEBSOCKET)?1:0;
- #endif
- }
- static int ws_send(struct mg_connection *c, void *data, int len, int isbin)
- {
- LOGD("____ws_send, len: %d\n", len);
- #ifdef USE_MG716
- mg_ws_send(c, data, len, isbin?WEBSOCKET_OP_BINARY:WEBSOCKET_OP_TEXT);
- #else
- mg_send_websocket_frame(c, isbin?WEBSOCKET_OP_BINARY:WEBSOCKET_OP_TEXT, data, len);
- #endif
-
- return 0;
- }
- static int ws_send_pkt(web_handle_t *h, struct mg_connection *c, uint8_t type, void *data, int len, int isbin)
- {
- pkt_hdr_t *hdr=NULL;
-
- if(!h->buf || !len || h->buflen<sizeof(pkt_hdr_t)+len) {
- LOGE("____ws_send, para are invalid\n");
- return -1;
- }
- hdr = (pkt_hdr_t*)h->buf;
-
- hdr->magic = PKT_MAGIC;
- hdr->type = type;
- hdr->nack = 0;
- hdr->dlen = len;
- memcpy(hdr->data, data, len);
- ws_send(c, hdr, len+sizeof(pkt_hdr_t), isbin);
-
- return 0;
- }
- static void send_powerc(web_handle_t *h, struct mg_connection *c)
- {
- int i,xlen,offset;
- pkt_hdr_t *hdr=NULL;
- power_data_t pd=power_data_get();
-
- for(i=0; i<pd.chs; i++) {
- ws_send_pkt(h, c, PKT_TYPE_POWER_CH, &pd.pch[i], sizeof(power_ch_t), 1);
- }
- }
- static void send_powerc_once(web_handle_t *h, struct mg_connection *c)
- {
- int i,xlen,offset;
- pkt_hdr_t *hdr=NULL;
- power_data_t pd=power_data_get();
-
- pd.chs = 8;
- if(pd.chs==0) {
- return;
- }
-
- xlen = pd.chs*sizeof(power_ch_t)+sizeof(pkt_hdr_t);
- if(!h->buf || h->buflen<xlen) {
- if(h->buf) free(h->buf);
- h->buf = malloc(xlen);
- if(h->buf) {
- h->buflen = xlen;
- }
- }
- if(!h->buf) {
- return;
- }
- hdr = (pkt_hdr_t*)h->buf;
- hdr->magic = PKT_MAGIC;
- hdr->type = PKT_TYPE_POWER_CH;
- hdr->nack = 0;
- hdr->dlen = xlen-sizeof(pkt_hdr_t);
- for(i=0; i<pd.chs; i++) {
- offset = i*sizeof(power_ch_t);
- memcpy(hdr->data+offset, &pd.pch[i], sizeof(power_ch_t));
- }
- ws_send(c, h->buf, xlen, 1);
- }
- static void send_powert(web_handle_t *h, struct mg_connection *c)
- {
- int i,r;
- power_data_t pd=power_data_get();
-
- ws_send_pkt(h, c, PKT_TYPE_POWER_TOTAL, &pd.ttl, sizeof(power_total_t), 1);
- }
- static int ws_handle(web_handle_t *h, pkt_hdr_t *hdr, struct mg_connection *c)
- {
- int r=0;
-
- if (!is_ws(c)) {
- return -1;
- }
-
- if(hdr->magic!=PKT_MAGIC) {
- LOGE("___ magic: 0x%x is wrong!, correct is: 0x%x\n", hdr->magic, PKT_MAGIC);
- return -1;
- }
-
- //LOGD("_____ ws handle, type: %d, flag: %d\n", hdr->type, hdr->flag);
- switch(hdr->type) {
- case PKT_TYPE_PARAS:
- {
- //setup
- }
- break;
-
- case PKT_TYPE_POWER_CH:
- {
- if(hdr->dlen>0) {
- power_ch_t *pch=(power_ch_t*)hdr->data;
- switch(hdr->subtype) {
- case SUB_TYPE_POWER_SWITCH:
- {
- r = power_set_ch_sw(pch->info.ch, pch->power[0].status);
- }
- break;
-
- case SUB_TYPE_POWER_ALARM:
- {
- r = power_set_alarm(pch);
- }
- break;
-
- case SUB_TYPE_POWER_THRESHOLD:
- {
- r = power_set_threshold(pch);
- }
- break;
-
- case SUB_TYPE_POWER_START_DELAY:
- {
- r = power_set_start_delay(pch);
- }
- break;
-
- case SUB_TYPE_POWER_STOP_DELAY:
- {
- r = power_set_stop_delay(pch);
- }
- break;
- }
- }
- else {
- LOGD("_____ send_powerc\n");
- send_powerc_once(h, c);
- }
- }
- break;
-
- case PKT_TYPE_POWER_TOTAL:
- {
- LOGD("_____ send_powert\n");
- send_powert(h, c);
- }
- break;
-
- case PKT_TYPE_CH_ALL:
- {
- power_set_all_sw(hdr->flag);
- }
- break;
-
- case PKT_TYPE_POWER_RESET:
- {
- r = power_reset();
- }
- break;
-
- case PKT_TYPE_SCAN:
- {
- r = power_scan();
- }
- break;
-
- case PKT_TYPE_FACTORY:
- {
- //r = power_scan();
- }
- break;
-
- case PKT_TYPE_SENSOR:
- {
-
- }
- break;
-
- case PKT_TYPE_BREAKER:
- {
-
- }
- break;
-
- case PKT_TYPE_FILE:
- {
-
- }
- break;
-
- }
- }
- static void http_fn(struct mg_connection *c, int ev, void *ev_data)
- {
- int r,flag=0;
- web_handle_t *h=&webHandle;
- #ifdef USE_MG716
- if (ev == MG_EV_HTTP_MSG) {
- struct mg_http_message *hm = (struct mg_http_message *) ev_data;
- if (mg_match(hm->uri, mg_str("/ws"), NULL)) {
- mg_ws_upgrade(c, hm, NULL);
- } else if (mg_match(hm->uri, mg_str("/rest"), NULL)) {
- // Serve REST response
- mg_http_reply(c, 200, "", "{\"result\": %d}\n", 123);
- } else {
- 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_WS_MSG) {
- struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
- pkt_hdr_t *hdr=(pkt_hdr_t*)wm->data.buf;
- ws_handle(h, hdr, c);
- }
- #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) {
- paras_data_t *p=paras_get();
- //ws_send(h, c, PKT_TYPE_PARAS, p, sizeof(paras_data_t), 1);
- //set_timer(c, TIMER_INTERVAL);
- }
- else if(ev == MG_EV_WEBSOCKET_FRAME) {
- struct websocket_message *wm = (struct websocket_message *) ev_data;
- pkt_hdr_t *hdr=(pkt_hdr_t*)wm->data;
- ws_handle(h, hdr, c);
- }
- else if(ev==MG_EV_TIMER) {
-
- #if 0
- list_node_t *ln=NULL;
- r = list_take_node(h->list, &ln, 0);
- if(r==0) {
- node_t *n=&ln->data;
- ws_send(h, c, n->tp, n->buf, n->dlen, 1);
- list_back_node(h->list, ln);
- }
- #endif
- }
- #endif
- }
- static void timer_fn(void *arg)
- {
- int r;
- web_handle_t *h=(web_handle_t*)arg;
- list_node_t *ln=NULL;
- struct mg_connection* c;
-
- r = list_take_node(h->list, &ln, 0);
- if(r==0) {
- node_t *n=&ln->data;
- #ifdef USE_MG716
- for (c = h->mgr.conns; c != NULL; c = c->next) {
- #else
- for (c = mg_next(&h->mgr, NULL); c != NULL; c = mg_next(&h->mgr, c)) {
- #endif
- ws_send_pkt(h, c, n->tp, n->buf, n->dlen, 1);
- }
- list_back_node(h->list, ln);
- }
- }
- 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->attr->arg;
- #ifdef USE_MG716
- mg_log_set(MG_LL_DEBUG);
- mg_mgr_init(&h->mgr);
- //sys_get_path(mh->rootDir, WEB_ROOT);
- strcpy(h->rootDir, "/web");
- //mg_timer_add(&h->mgr, TIMER_INTERVAL, MG_TIMER_REPEAT, timer_fn, h);
- if (mg_http_listen(&h->mgr, http_addr, http_fn, NULL) == NULL) {
- MG_ERROR(("cannot listen on %s\n", http_addr));
- }
- //if (mg_http_listen(&h->mgr, https_addr, http_fn, (void*)1) == NULL) {
- // MG_ERROR(("cannot listen on %s\n", https_addr));
- //}
-
- //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, 300);
- }
- mg_mgr_free(&h->mgr);
- }
- int web_init(void)
- {
- list_cfg_t lc={0,0,10};
- web_handle_t *h=&webHandle;
-
- memset(h, 0, sizeof(web_handle_t));
- h->list = list_init(&lc);
-
- h->buflen = sizeof(pkt_hdr_t)+sizeof(power_ch_t)+32;
- h->buf = (char*)malloc(h->buflen);
-
- thread_start(THREAD_ID_WEB, web_thread, h);
- return 0;
- }
- int web_post(int type, void *data, int len)
- {
- struct mg_connection *c;
- web_handle_t *h=&webHandle;
-
- return list_append(h->list, type, data, len);
- }
|