diuhttp.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. //
  2. // gcc mongoose.c diuhttp.c -o diuhttp
  3. //
  4. #include "mongoose.h"
  5. #define _SLEN 1024
  6. static const char *s_http_port = "60535";
  7. static struct mg_serve_http_opts s_http_server_opts;
  8. // ui wants status information
  9. //
  10. static void handle_status_input(struct mg_connection *nc, struct http_message *hm) {
  11. char buf[_SLEN];
  12. char msg[][_SLEN] = {
  13. "fail error",
  14. "ok .",
  15. };
  16. //DEBUG sample message
  17. snprintf(buf, _SLEN, "ok msg=status\nroute=%s|trip=%s|stop=%s|gps=%i|tunnel=%i|date=%s|eqiupno=%s|nmsg=%s|last_token=%s|config=%s|firmware=%s|imei=%s|imsi=%s|eth0=%s",
  18. "9900", "1", "0",
  19. 1, 1,
  20. "2019-08-13 09;23:00",
  21. "9999", "0",
  22. "",
  23. "20190730v403 2019-11-05 02:36:43",
  24. "1.11 2019-07-30 16:33:08",
  25. "356136074279052",
  26. "310260877138191",
  27. "00:80:66:10:E8:8A");
  28. printf(">> status:\nbuf(%i):\n%s\n", (int)strlen(buf), buf);
  29. //snprintf(buf, _SLEN, "ok xxx=foo bar;yyy=baz qux;...");
  30. printf(">> status\n");
  31. mg_printf(nc, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n%s",
  32. (unsigned long)strlen(buf), buf);
  33. }
  34. // driver logout
  35. //
  36. static void handle_logout_input(struct mg_connection *nc, struct http_message *hm) {
  37. char msg[][_SLEN] = {
  38. "fail error",
  39. "ok .",
  40. };
  41. printf(">> logout\n");
  42. mg_printf(nc, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n%s",
  43. (unsigned long)strlen(msg[1]), msg[1]);
  44. }
  45. // (manual) next stop pressed
  46. //
  47. static void handle_nextstop_input(struct mg_connection *nc, struct http_message *hm) {
  48. char msg[][_SLEN] = {
  49. "fail error",
  50. "ok .",
  51. };
  52. printf(">> nextstop\n");
  53. mg_printf(nc, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n%s",
  54. (unsigned long)strlen(msg[1]), msg[1]);
  55. }
  56. // (manual) previous stop pressed
  57. //
  58. static void handle_prevstop_input(struct mg_connection *nc, struct http_message *hm) {
  59. char msg[][_SLEN] = {
  60. "fail error",
  61. "ok .",
  62. };
  63. printf(">> prevstop\n");
  64. mg_printf(nc, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n%s",
  65. (unsigned long)strlen(msg[1]), msg[1]);
  66. }
  67. // generic fare input
  68. //
  69. static void handle_fare_input(struct mg_connection *nc, struct http_message *hm) {
  70. int ret;
  71. char s_fare[_SLEN], s_count[_SLEN];
  72. char msg[][_SLEN] = {
  73. "fail error",
  74. "ok .",
  75. };
  76. ret = mg_get_http_var(&(hm->body), "fare", s_fare, _SLEN);
  77. if (ret<=0) { mg_http_send_error(nc, 404, NULL); return; }
  78. printf("got fare %s\n", s_fare);
  79. ret = mg_get_http_var(&(hm->body), "count", s_count, _SLEN);
  80. if (ret<=0) { mg_http_send_error(nc, 404, NULL); return; }
  81. printf("got count %s\n", s_count);
  82. mg_printf(nc, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n%s",
  83. (unsigned long)strlen(msg[1]), msg[1]);
  84. }
  85. // driver paddle (route) input
  86. //
  87. static void handle_paddle_input(struct mg_connection *nc, struct http_message *hm) {
  88. int ret;
  89. char s_paddle[_SLEN];
  90. char msg[][_SLEN] = {
  91. "fail paddle_unknown",
  92. "ok paddle",
  93. };
  94. ret = mg_get_http_var(&(hm->body), "paddle", s_paddle, _SLEN);
  95. if (ret<=0) { mg_http_send_error(nc, 404, NULL); return; }
  96. //DEBUG
  97. printf("#got paddle %s\n", s_paddle);
  98. //DEBUG
  99. if (strncmp(s_paddle,"9900", strlen("9900"))!=0) {
  100. mg_printf(nc, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n%s",
  101. (unsigned long)strlen(msg[0]), msg[0]);
  102. return;
  103. }
  104. mg_printf(nc, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n%s",
  105. (unsigned long)strlen(msg[1]), msg[1]);
  106. }
  107. // driver login
  108. //
  109. static void handle_driver_login(struct mg_connection *nc, struct http_message *hm) {
  110. int ret;
  111. char s_driver[_SLEN], s_pin[_SLEN];
  112. char msg[][_SLEN] = {
  113. "fail driver_not_found",
  114. "fail pin_invalid",
  115. "ok driver",
  116. "ok admin",
  117. };
  118. ret = mg_get_http_var(&(hm->body), "driver", s_driver, _SLEN);
  119. if (ret<=0) { mg_http_send_error(nc, 404, NULL); return; }
  120. ret = mg_get_http_var(&(hm->body), "pin", s_pin, _SLEN);
  121. if (ret<=0) { mg_http_send_error(nc, 404, NULL); return; }
  122. //DEBUG
  123. printf("#got %s %s\n", s_driver, s_pin);
  124. //DEBUG
  125. if (strncmp(s_driver,"81000", strlen("81000"))!=0) {
  126. mg_printf(nc, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n%s",
  127. (unsigned long)strlen(msg[0]), msg[0]);
  128. return;
  129. }
  130. if (strncmp(s_pin,"81000", strlen("81000"))!=0) {
  131. mg_printf(nc, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n%s",
  132. (unsigned long)strlen(msg[1]), msg[1]);
  133. return;
  134. }
  135. mg_printf(nc, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n%s",
  136. (unsigned long)strlen(msg[3]), msg[3]);
  137. }
  138. // api point
  139. //
  140. static void handle_req(struct mg_connection *nc, struct http_message *hm) {
  141. int ret;
  142. char s_action[_SLEN];
  143. char _default_msg[_SLEN] = "";
  144. struct mg_str hdr;
  145. ret = mg_get_http_var(&(hm->body), "action", s_action, _SLEN);
  146. if (ret==0) {
  147. printf(">>> %p\n", s_action);
  148. mg_http_send_error(nc, 404, NULL);
  149. return;
  150. }
  151. printf("#req: action: (%s)\n", s_action);
  152. if (strncmp(s_action, "driverlogin", strlen("driverlogin"))==0) {
  153. handle_driver_login(nc, hm);
  154. }
  155. else if (strncmp(s_action, "paddleinput", strlen("paddleinput"))==0) {
  156. handle_paddle_input(nc, hm);
  157. }
  158. else if (strncmp(s_action, "prevstop", strlen("prevstop"))==0) {
  159. handle_prevstop_input(nc, hm);
  160. }
  161. else if (strncmp(s_action, "nextstop", strlen("nextstop"))==0) {
  162. handle_nextstop_input(nc, hm);
  163. }
  164. else if (strncmp(s_action, "status", strlen("status"))==0) {
  165. handle_status_input(nc, hm);
  166. }
  167. else if (strncmp(s_action, "logout", strlen("logout"))==0) {
  168. handle_logout_input(nc, hm);
  169. }
  170. else if (strncmp(s_action, "fare", strlen("fare"))==0) {
  171. handle_fare_input(nc, hm);
  172. }
  173. else {
  174. mg_http_send_error(nc, 404, NULL);
  175. }
  176. }
  177. static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
  178. struct http_message *hm = (struct http_message *) ev_data;
  179. int debug_print = 0;
  180. int i;
  181. char buf[1024];
  182. if (debug_print) {
  183. mg_sock_addr_to_str(&(nc->sa), buf, 1023, MG_SOCK_STRINGIFY_IP);
  184. printf("%s\n", buf);
  185. }
  186. switch (ev){
  187. case MG_EV_HTTP_REQUEST:
  188. if (mg_vcmp(&hm->uri, "/req")==0) {
  189. handle_req(nc, (struct http_message *)ev_data);
  190. }
  191. else {
  192. mg_serve_http(nc, (struct http_message *) ev_data, s_http_server_opts);
  193. }
  194. break;
  195. default:
  196. //printf("? %i\n", ev);
  197. break;
  198. }
  199. }
  200. int main(void) {
  201. struct mg_mgr mgr;
  202. struct mg_connection *nc;
  203. mg_mgr_init(&mgr, NULL);
  204. printf("Starting web server on port %s\n", s_http_port);
  205. nc = mg_bind(&mgr, s_http_port, ev_handler);
  206. if (nc == NULL) {
  207. printf("Failed to create listener\n");
  208. return 1;
  209. }
  210. // Set up HTTP server parameters
  211. mg_set_protocol_http_websocket(nc);
  212. s_http_server_opts.document_root = "html";
  213. s_http_server_opts.enable_directory_listing = "no";
  214. for (;;) {
  215. mg_mgr_poll(&mgr, 1000);
  216. }
  217. mg_mgr_free(&mgr);
  218. return 0;
  219. }