web.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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 ws_send(web_handle_t *h, struct mg_connection *c, uint8_t type, void *data, int len, int isbin)
  39. {
  40. pkt_hdr_t *hdr=NULL;
  41. if(!h->buf || !len || h->buflen<sizeof(pkt_hdr_t)+len) {
  42. LOGE("____ws_send, para are invalid\n");
  43. return -1;
  44. }
  45. hdr = (pkt_hdr_t*)h->buf;
  46. hdr->magic = PKT_MAGIC;
  47. hdr->type = type;
  48. hdr->nack = 0;
  49. hdr->dlen = len;
  50. memcpy(hdr->data, data, len);
  51. #ifdef USE_MG716
  52. mg_ws_send(c, hdr, len+sizeof(pkt_hdr_t), isbin?WEBSOCKET_OP_BINARY:WEBSOCKET_OP_TEXT);
  53. #else
  54. mg_send_websocket_frame(c, isbin?WEBSOCKET_OP_BINARY:WEBSOCKET_OP_TEXT, hdr, len+sizeof(pkt_hdr_t));
  55. #endif
  56. return 0;
  57. }
  58. static void send_powerc(web_handle_t *h, struct mg_connection *c)
  59. {
  60. int i,r;
  61. power_data_t pd=power_data_get();
  62. for(i=0; i<pd.chs; i++) {
  63. ws_send(h, c, PKT_TYPE_POWER_CH, &pd.pch[i], sizeof(power_ch_t), 1);
  64. }
  65. }
  66. static void send_powert(web_handle_t *h, struct mg_connection *c)
  67. {
  68. int i,r;
  69. power_data_t pd=power_data_get();
  70. if(r==0) {
  71. ws_send(h, c, PKT_TYPE_POWER_TOTAL, &pd.ttl, sizeof(power_total_t), 1);
  72. }
  73. }
  74. static int is_websocket(struct mg_connection *c)
  75. {
  76. int r=0;
  77. #ifdef USE_MG716
  78. r = c->is_websocket;
  79. #else
  80. r = (c->flags & MG_F_IS_WEBSOCKET)?1:0;
  81. #endif
  82. }
  83. static int ws_handle(web_handle_t *h, pkt_hdr_t *hdr, struct mg_connection *c)
  84. {
  85. int r=0;
  86. if (!is_websocket(c)) {
  87. return -1;
  88. }
  89. if(hdr->magic!=PKT_MAGIC) {
  90. LOGE("___ magic: 0x%x is wrong!, correct is: 0x%x\n", hdr->magic, PKT_MAGIC);
  91. return -1;
  92. }
  93. //LOGD("_____ ws handle, type: %d, flag: %d\n", hdr->type, hdr->flag);
  94. switch(hdr->type) {
  95. case PKT_TYPE_PARAS:
  96. {
  97. //setup
  98. }
  99. break;
  100. case PKT_TYPE_POWER_CH:
  101. {
  102. if(hdr->dlen>0) {
  103. power_ch_t *pch=(power_ch_t*)hdr->data;
  104. switch(hdr->subtype) {
  105. case SUB_TYPE_POWER_SWITCH:
  106. {
  107. r = power_set_ch_sw(pch->info.ch, pch->power[0].status);
  108. }
  109. break;
  110. case SUB_TYPE_POWER_ALARM:
  111. {
  112. r = power_set_alarm(pch);
  113. }
  114. break;
  115. case SUB_TYPE_POWER_THRESHOLD:
  116. {
  117. r = power_set_threshold(pch);
  118. }
  119. break;
  120. case SUB_TYPE_POWER_START_DELAY:
  121. {
  122. r = power_set_start_delay(pch);
  123. }
  124. break;
  125. case SUB_TYPE_POWER_STOP_DELAY:
  126. {
  127. r = power_set_stop_delay(pch);
  128. }
  129. break;
  130. }
  131. }
  132. else {
  133. LOGD("_____ send_powerc\n");
  134. send_powerc(h, c);
  135. }
  136. }
  137. break;
  138. case PKT_TYPE_POWER_TOTAL:
  139. {
  140. LOGD("_____ send_powert\n");
  141. send_powert(h, c);
  142. }
  143. break;
  144. case PKT_TYPE_CH_ALL:
  145. {
  146. power_set_all_sw(hdr->flag);
  147. }
  148. break;
  149. case PKT_TYPE_POWER_RESET:
  150. {
  151. r = power_reset();
  152. }
  153. break;
  154. case PKT_TYPE_SCAN:
  155. {
  156. r = power_scan();
  157. }
  158. break;
  159. case PKT_TYPE_FACTORY:
  160. {
  161. //r = power_scan();
  162. }
  163. break;
  164. case PKT_TYPE_SENSOR:
  165. {
  166. }
  167. break;
  168. case PKT_TYPE_BREAKER:
  169. {
  170. }
  171. break;
  172. case PKT_TYPE_FILE:
  173. {
  174. }
  175. break;
  176. }
  177. }
  178. static void http_fn(struct mg_connection *c, int ev, void *ev_data)
  179. {
  180. int r,flag=0;
  181. web_handle_t *h=&webHandle;
  182. #ifdef USE_MG716
  183. if (ev == MG_EV_HTTP_MSG) {
  184. struct mg_http_message *hm = (struct mg_http_message *) ev_data;
  185. if (mg_match(hm->uri, mg_str("/ws"), NULL)) {
  186. mg_ws_upgrade(c, hm, NULL);
  187. } else if (mg_match(hm->uri, mg_str("/rest"), NULL)) {
  188. // Serve REST response
  189. mg_http_reply(c, 200, "", "{\"result\": %d}\n", 123);
  190. } else {
  191. struct mg_http_serve_opts opts = {.root_dir = h->rootDir};
  192. mg_http_serve_dir(c, ev_data, &opts);
  193. }
  194. }
  195. else if(ev == MG_EV_ACCEPT) {
  196. if (c->fn_data) {
  197. mg_tls_init(c, &h->tls);
  198. }
  199. }
  200. else if(ev == MG_EV_WS_MSG) {
  201. struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
  202. pkt_hdr_t *hdr=(pkt_hdr_t*)wm->data.buf;
  203. ws_handle(h, hdr, c);
  204. }
  205. #else
  206. if (ev == MG_EV_HTTP_REQUEST) {
  207. mg_serve_http(c, (struct http_message *)ev_data, h->opts);
  208. }
  209. else if(ev == MG_EV_WEBSOCKET_HANDSHAKE_DONE) {
  210. paras_data_t *p=paras_get();
  211. //ws_send(h, c, PKT_TYPE_PARAS, p, sizeof(paras_data_t), 1);
  212. //set_timer(c, TIMER_INTERVAL);
  213. }
  214. else if(ev == MG_EV_WEBSOCKET_FRAME) {
  215. struct websocket_message *wm = (struct websocket_message *) ev_data;
  216. pkt_hdr_t *hdr=(pkt_hdr_t*)wm->data;
  217. ws_handle(h, hdr, c);
  218. }
  219. else if(ev==MG_EV_TIMER) {
  220. #if 0
  221. list_node_t *ln=NULL;
  222. r = list_take_node(h->list, &ln, 0);
  223. if(r==0) {
  224. node_t *n=&ln->data;
  225. ws_send(h, c, n->tp, n->buf, n->dlen, 1);
  226. list_back_node(h->list, ln);
  227. }
  228. #endif
  229. }
  230. #endif
  231. }
  232. static void timer_fn(void *arg)
  233. {
  234. int r;
  235. web_handle_t *h=(web_handle_t*)arg;
  236. list_node_t *ln=NULL;
  237. struct mg_connection* c;
  238. r = list_take_node(h->list, &ln, 0);
  239. if(r==0) {
  240. node_t *n=&ln->data;
  241. #ifdef USE_MG716
  242. for (c = h->mgr.conns; c != NULL; c = c->next) {
  243. #else
  244. for (c = mg_next(&h->mgr, NULL); c != NULL; c = mg_next(&h->mgr, c)) {
  245. #endif
  246. ws_send(h, c, n->tp, n->buf, n->dlen, 1);
  247. }
  248. list_back_node(h->list, ln);
  249. }
  250. }
  251. static void web_thread(void *arg)
  252. {
  253. struct mg_connection *nc;
  254. const char *http_addr="http://[::]:80";
  255. const char *https_addr="https://[::]:443";
  256. //thread_handle_t *th=(thread_handle_t*)arg;
  257. //web_handle_t *h=(web_handle_t*)th->attr->arg;
  258. web_handle_t *h=&webHandle;
  259. #ifdef USE_MG716
  260. mg_log_set(MG_LL_DEBUG);
  261. mg_mgr_init(&h->mgr);
  262. //sys_get_path(mh->rootDir, WEB_ROOT);
  263. strcpy(h->rootDir, "/web");
  264. mg_timer_add(&h->mgr, TIMER_INTERVAL, MG_TIMER_REPEAT, timer_fn, h);
  265. if (mg_http_listen(&h->mgr, http_addr, http_fn, NULL) == NULL) {
  266. MG_ERROR(("cannot listen on %s\n", http_addr));
  267. }
  268. //if (mg_http_listen(&h->mgr, https_addr, http_fn, (void*)1) == NULL) {
  269. // MG_ERROR(("cannot listen on %s\n", https_addr));
  270. //}
  271. //mh->tls.cert = mg_str(file_load2("/root/run/app/cert/server.crt",0));
  272. //mh->tls.key = mg_str(file_load2("/root/run/app/cert/server.key",0));
  273. //mh->tls.name = mg_url_host(https_addr);
  274. #else
  275. mg_mgr_init(&h->mgr, NULL);
  276. nc = mg_bind(&h->mgr, "80", http_fn);
  277. strcpy(h->rootDir, "/web");
  278. mg_set_protocol_http_websocket(nc);
  279. h->opts.document_root = h->rootDir;
  280. h->opts.enable_directory_listing = "yes";
  281. #endif
  282. //while(th->quit==0) {
  283. while(1) {
  284. mg_mgr_poll(&h->mgr, 300);
  285. }
  286. mg_mgr_free(&h->mgr);
  287. }
  288. int web_init(void)
  289. {
  290. list_cfg_t lc={0,0,10};
  291. web_handle_t *h=&webHandle;
  292. memset(h, 0, sizeof(web_handle_t));
  293. h->list = list_init(&lc);
  294. h->buflen = sizeof(pkt_hdr_t)+sizeof(power_ch_t)+32;
  295. h->buf = (char*)malloc(h->buflen);
  296. //web_thread(0);
  297. thread_start(THREAD_ID_WEB, web_thread, h);
  298. return 0;
  299. }
  300. int web_post(int type, void *data, int len)
  301. {
  302. struct mg_connection *c;
  303. web_handle_t *h=&webHandle;
  304. return list_append(h->list, type, data, len);
  305. }
  306. #if 1
  307. void WWDG_IRQHandler() {
  308. LOGD("___0\n");
  309. }
  310. void PVD_IRQHandler() {
  311. LOGD("___1\n");
  312. }
  313. void TAMP_STAMP_IRQHandler(){
  314. LOGD("___2\n");
  315. }
  316. void RTC_WKUP_IRQHandler(){
  317. LOGD("___3\n");
  318. }
  319. void FLASH_IRQHandler(){
  320. LOGD("___4\n");
  321. }
  322. void RCC_IRQHandler(){
  323. LOGD("___5\n");
  324. }
  325. //void EXTI0_IRQHandler(){
  326. // LOGD("___6\n");
  327. //}
  328. //void EXTI1_IRQHandler(){
  329. // LOGD("___7\n");
  330. //}
  331. //void EXTI2_IRQHandler(){
  332. // LOGD("___8\n");
  333. //}
  334. //void EXTI3_IRQHandler(){
  335. // LOGD("___9\n");
  336. //}
  337. //void EXTI4_IRQHandler(){
  338. // LOGD("___a\n");
  339. //}
  340. void DMA1_Stream0_IRQHandler(){
  341. LOGD("___b\n");
  342. }
  343. void DMA1_Stream1_IRQHandler(){
  344. LOGD("___c\n");
  345. }
  346. void DMA1_Stream2_IRQHandler(){
  347. LOGD("___d\n");
  348. }
  349. void DMA1_Stream3_IRQHandler(){
  350. LOGD("___e\n");
  351. }
  352. void DMA1_Stream4_IRQHandler(){
  353. LOGD("___f\n");
  354. }
  355. void DMA1_Stream5_IRQHandler(){
  356. LOGD("___g\n");
  357. }
  358. void DMA1_Stream6_IRQHandler(){
  359. LOGD("___h\n");
  360. }
  361. void ADC_IRQHandler(){
  362. LOGD("___i\n");
  363. }
  364. void CAN1_TX_IRQHandler(){
  365. LOGD("___j\n");
  366. }
  367. void CAN1_RX0_IRQHandler(){
  368. LOGD("___k\n");
  369. }
  370. void CAN1_RX1_IRQHandler(){
  371. LOGD("___l\n");
  372. }
  373. void CAN1_SCE_IRQHandler(){
  374. LOGD("___m\n");
  375. }
  376. //void EXTI9_5_IRQHandler(){
  377. // LOGD("___n\n");
  378. //}
  379. void TIM1_BRK_TIM9_IRQHandler(){
  380. LOGD("___o\n");
  381. }
  382. void TIM1_UP_TIM10_IRQHandler(){
  383. LOGD("___p\n");
  384. }
  385. void TIM1_TRG_COM_TIM11_IRQHandler(){
  386. LOGD("___q\n");
  387. }
  388. void TIM1_CC_IRQHandler(){
  389. LOGD("___r\n");
  390. }
  391. void TIM2_IRQHandler(){
  392. LOGD("___s\n");
  393. }
  394. void TIM3_IRQHandler(){
  395. LOGD("___t\n");
  396. }
  397. void TIM4_IRQHandler(){
  398. LOGD("___u\n");
  399. }
  400. void I2C1_EV_IRQHandler(){
  401. LOGD("___v\n");
  402. }
  403. void I2C1_ER_IRQHandler(){
  404. LOGD("___w\n");
  405. }
  406. void I2C2_EV_IRQHandler(){
  407. LOGD("___x\n");
  408. }
  409. void I2C2_ER_IRQHandler(){
  410. LOGD("___y\n");
  411. }
  412. void SPI1_IRQHandler(){
  413. LOGD("___z\n");
  414. }
  415. void SPI2_IRQHandler(){
  416. LOGD("___a1\n");
  417. }
  418. //void USART1_IRQHandler(){
  419. // LOGD("___a2\n");
  420. //}
  421. //void USART2_IRQHandler(){
  422. // LOGD("___a3\n");
  423. //}
  424. //void USART3_IRQHandler(){
  425. // LOGD("___a4\n");
  426. //}
  427. //void EXTI15_10_IRQHandler(){
  428. // LOGD("___a5\n");
  429. //}
  430. void RTC_Alarm_IRQHandler(){
  431. LOGD("___a6\n");
  432. }
  433. void OTG_FS_WKUP_IRQHandler(){
  434. LOGD("___a7\n");
  435. }
  436. void TIM8_BRK_TIM12_IRQHandler(){
  437. LOGD("___a8\n");
  438. }
  439. void TIM8_UP_TIM13_IRQHandler(){
  440. LOGD("___a9\n");
  441. }
  442. void TIM8_TRG_COM_TIM14_IRQHandler(){
  443. LOGD("___aa\n");
  444. }
  445. void TIM8_CC_IRQHandler(){
  446. LOGD("___ab\n");
  447. }
  448. void DMA1_Stream7_IRQHandler(){
  449. LOGD("___ac\n");
  450. }
  451. void FMC_IRQHandler(){
  452. LOGD("___ad\n");
  453. }
  454. //void SDIO_IRQHandler(){
  455. // LOGD("___ae\n");
  456. //}
  457. void TIM5_IRQHandler(){
  458. LOGD("___af\n");
  459. }
  460. void SPI3_IRQHandler(){
  461. LOGD("___ag\n");
  462. }
  463. void UART4_IRQHandler(){
  464. LOGD("___ah\n");
  465. }
  466. void UART5_IRQHandler(){
  467. LOGD("___b1\n");
  468. }
  469. void TIM6_DAC_IRQHandler(){
  470. LOGD("___b2\n");
  471. }
  472. void TIM7_IRQHandler(){
  473. LOGD("___b3\n");
  474. }
  475. void DMA2_Stream0_IRQHandler(){
  476. LOGD("___b4\n");
  477. }
  478. void DMA2_Stream1_IRQHandler(){
  479. LOGD("___b5\n");
  480. }
  481. void DMA2_Stream2_IRQHandler(){
  482. LOGD("___b6\n");
  483. }
  484. void DMA2_Stream3_IRQHandler(){
  485. LOGD("___b7\n");
  486. }
  487. void DMA2_Stream4_IRQHandler(){
  488. LOGD("___b8\n");
  489. }
  490. //void ETH_IRQHandler(){
  491. // LOGD("___b9\n");
  492. //}
  493. void ETH_WKUP_IRQHandler(){
  494. LOGD("___c1\n");
  495. }
  496. void CAN2_TX_IRQHandler(){
  497. LOGD("___c2\n");
  498. }
  499. void CAN2_RX0_IRQHandler(){
  500. LOGD("___c3\n");
  501. }
  502. void CAN2_RX1_IRQHandler(){
  503. LOGD("___c4\n");
  504. }
  505. void CAN2_SCE_IRQHandler(){
  506. LOGD("___c5\n");
  507. }
  508. void OTG_FS_IRQHandler(){
  509. LOGD("___c6\n");
  510. }
  511. void DMA2_Stream5_IRQHandler(){
  512. LOGD("___c7\n");
  513. }
  514. void DMA2_Stream6_IRQHandler(){
  515. LOGD("___c8\n");
  516. }
  517. void DMA2_Stream7_IRQHandler(){
  518. LOGD("___c9\n");
  519. }
  520. //void USART6_IRQHandler(){
  521. // LOGD("___d1\n");
  522. //}
  523. void I2C3_EV_IRQHandler(){
  524. LOGD("___d2\n");
  525. }
  526. void I2C3_ER_IRQHandler(){
  527. LOGD("___d3\n");
  528. }
  529. void OTG_HS_EP1_OUT_IRQHandler(){
  530. LOGD("___d4\n");
  531. }
  532. void OTG_HS_EP1_IN_IRQHandler(){
  533. LOGD("___d5\n");
  534. }
  535. void OTG_HS_WKUP_IRQHandler(){
  536. LOGD("___d6\n");
  537. }
  538. void OTG_HS_IRQHandler(){
  539. LOGD("___d7\n");
  540. }
  541. void DCMI_IRQHandler(){
  542. LOGD("___d8\n");
  543. }
  544. void HASH_RNG_IRQHandler(){
  545. LOGD("___d9\n");
  546. }
  547. void FPU_IRQHandler(){
  548. LOGD("___e1\n");
  549. }
  550. #endif