mg_port.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * File : mg_port.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2017-08-19 armink first version
  23. */
  24. #include <rtthread.h>
  25. #include <finsh.h>
  26. #include <string.h>
  27. #include <lwip/dhcp.h>
  28. #include <lwip/init.h>
  29. #include <lwip/netif.h>
  30. #include "mongoose.h"
  31. static rt_mutex_t mg_locker = NULL;
  32. /**
  33. * Mongoose Library port initialization
  34. */
  35. void mongoose_port_init(void) {
  36. if (!mg_locker) {
  37. mg_locker = rt_mutex_create("mg_lock", RT_IPC_FLAG_FIFO);
  38. }
  39. }
  40. void mgos_lock(void) {
  41. rt_mutex_take(mg_locker, RT_WAITING_FOREVER);
  42. }
  43. void mgos_unlock(void) {
  44. rt_mutex_release(mg_locker);
  45. }
  46. static void http_server_ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
  47. if (ev == MG_EV_POLL)
  48. return;
  49. rt_kprintf("ev %d\r\n", ev);
  50. switch (ev) {
  51. case MG_EV_ACCEPT: {
  52. char addr[32];
  53. mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
  54. MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
  55. rt_kprintf("%p: Connection from %s\r\n", nc, addr);
  56. break;
  57. }
  58. case MG_EV_HTTP_REQUEST: {
  59. struct http_message *hm = (struct http_message *) ev_data;
  60. char addr[32];
  61. mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr), MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
  62. rt_kprintf("%p: %.*s %.*s\r\n", nc, (int) hm->method.len, hm->method.p, (int) hm->uri.len, hm->uri.p);
  63. mg_send_response_line(nc, 200, "Content-Type: text/html\r\n"
  64. "Connection: close");
  65. mg_printf(nc, "\r\n<h1>Hello, %s!</h1>\r\n"
  66. "You asked for %.*s\r\n", addr, (int) hm->uri.len, hm->uri.p);
  67. nc->flags |= MG_F_SEND_AND_CLOSE;
  68. break;
  69. }
  70. case MG_EV_RECV : {
  71. struct mbuf *io = &nc->recv_mbuf;
  72. rt_kprintf("recv data: %.*s\n", io->len, io->buf);
  73. break;
  74. }
  75. case MG_EV_CLOSE: {
  76. rt_kprintf("%p: Connection closed\r\n", nc);
  77. break;
  78. }
  79. }
  80. }
  81. static void http_client_ev_handler(struct mg_connection *c, int ev, void *p) {
  82. struct http_message *hm = (struct http_message *) p;
  83. switch (ev) {
  84. case MG_EV_HTTP_REPLY: {
  85. c->flags |= MG_F_CLOSE_IMMEDIATELY;
  86. rt_kprintf("%.*s\n", hm->message.len, hm->message.p);
  87. break;
  88. }
  89. case MG_EV_CLOSE: {
  90. rt_kprintf("session close\n");
  91. break;
  92. }
  93. }
  94. }
  95. /*
  96. * This is a callback invoked by Mongoose to signal that a poll is needed soon.
  97. * Since we're in a tight polling loop anyway (see below), we don't need to do
  98. * anything.
  99. */
  100. void mg_lwip_mgr_schedule_poll(struct mg_mgr *mgr) {
  101. }
  102. static void thread_entry_mg_poll(void* parameter) {
  103. struct mg_mgr *mgr = (struct mg_mgr *) parameter;
  104. while (1) {
  105. mg_mgr_poll(mgr, 0);
  106. rt_thread_delay(rt_tick_from_millisecond(10));
  107. }
  108. }
  109. void cs_log_print_prefix(const char *func) {
  110. rt_kprintf("[mg:%s]", func);
  111. }
  112. void cs_log_printf(const char *fmt, ...) {
  113. static char log_buf[RT_CONSOLEBUF_SIZE];
  114. va_list args;
  115. /* args point to the first variable parameter */
  116. va_start(args, fmt);
  117. /* must use vprintf to print */
  118. rt_vsprintf(log_buf, fmt, args);
  119. rt_kprintf("%s\n", log_buf);
  120. va_end(args);
  121. }
  122. static void mg(uint8_t argc, char **argv) {
  123. enum {
  124. CMD_HTTP_INDEX,
  125. };
  126. size_t i = 0;
  127. static struct mg_mgr *mgr = NULL;
  128. static rt_thread_t test_thread = NULL;
  129. static struct mg_connection *conn = NULL;
  130. const char* help_info[] = {
  131. [CMD_HTTP_INDEX] = "mg http <s:server|c:client <url>> - start http `server` or http `client url` test",
  132. };
  133. if (argc < 2) {
  134. rt_kprintf("Usage:\n");
  135. for (i = 0; i < sizeof(help_info) / sizeof(char*); i++) {
  136. rt_kprintf("%s\n", help_info[i]);
  137. }
  138. rt_kprintf("\n");
  139. } else {
  140. const char *operator = argv[1];
  141. if (!strcmp(operator, "init")) {
  142. } else {
  143. /* create locker when first running */
  144. if (!mg_locker) {
  145. mg_locker = rt_mutex_create("mg_lock", RT_IPC_FLAG_FIFO);
  146. }
  147. /* delete last mongoose manager poll thread */
  148. if (test_thread) {
  149. rt_thread_delete(test_thread);
  150. /* wait 100 ticks for thread deleted finish */
  151. rt_thread_delay(100);
  152. test_thread = NULL;
  153. }
  154. /* create and initialize mongoose manager */
  155. {
  156. /* delete last mongoose manager */
  157. if (mgr) {
  158. mg_mgr_free(mgr);
  159. rt_free(mgr);
  160. mgr = NULL;
  161. }
  162. mgr = rt_calloc(sizeof(struct mg_mgr), 1);
  163. if (mgr) {
  164. mg_mgr_init(mgr, NULL);
  165. } else {
  166. rt_kprintf("Warning: NO memory.\n");
  167. return;
  168. }
  169. }
  170. if (!strcmp(operator, "http")) {
  171. if (argc < 3) {
  172. rt_kprintf("Usage: %s.\n", help_info[CMD_HTTP_INDEX]);
  173. return;
  174. } else if (!strcmp(argv[2], "s")) {
  175. /* http server test */
  176. const char *err;
  177. struct mg_bind_opts opts = { NULL };
  178. opts.error_string = &err;
  179. conn = mg_bind_opt(mgr, ":80", http_server_ev_handler, opts);
  180. if (conn == NULL) {
  181. rt_kprintf("Failed to create listener: %s\r\n", err);
  182. return;
  183. }
  184. mg_set_protocol_http_websocket(conn);
  185. rt_kprintf("Server address: http://%s:%d/\r\n", inet_ntoa(conn->sa.sin.sin_addr), htons(conn->sa.sin.sin_port));
  186. } else if (!strcmp(argv[2], "c") && argc > 3) {
  187. /* http client test */
  188. mg_connect_http(mgr, http_client_ev_handler, argv[3], NULL, NULL);
  189. rt_kprintf("Http client request url: %s\n", argv[3]);
  190. } else {
  191. rt_kprintf("Usage: %s.\n", help_info[CMD_HTTP_INDEX]);
  192. return;
  193. }
  194. } else if (!strcmp(operator, "ws")) {
  195. } else if (!strcmp(operator, "coap")) {
  196. } else if (!strcmp(operator, "mqtt")) {
  197. } else {
  198. rt_kprintf("Usage:\n");
  199. for (i = 0; i < sizeof(help_info) / sizeof(char*); i++) {
  200. rt_kprintf("%s\n", help_info[i]);
  201. }
  202. rt_kprintf("\n");
  203. }
  204. test_thread = rt_thread_create("mg_test_poll", thread_entry_mg_poll, mgr, 2048, 8, 10);
  205. if (test_thread != NULL) {
  206. rt_thread_startup(test_thread);
  207. } else {
  208. rt_kprintf("Warning: NO memory.\n");
  209. }
  210. }
  211. }
  212. }
  213. MSH_CMD_EXPORT(mg, Mongoose Test);