// // gcc mongoose.c diuhttp.c -o diuhttp // #include "mongoose.h" #define _SLEN 1024 static const char *s_http_port = "60535"; static struct mg_serve_http_opts s_http_server_opts; // ui wants status information // static void handle_status_input(struct mg_connection *nc, struct http_message *hm) { char buf[_SLEN]; char msg[][_SLEN] = { "fail error", "ok .", }; //DEBUG sample message 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", "9900", "1", "0", 1, 1, "2019-08-13 09;23:00", "9999", "0", "", "20190730v403 2019-11-05 02:36:43", "1.11 2019-07-30 16:33:08", "356136074279052", "310260877138191", "00:80:66:10:E8:8A"); printf(">> status:\nbuf(%i):\n%s\n", (int)strlen(buf), buf); //snprintf(buf, _SLEN, "ok xxx=foo bar;yyy=baz qux;..."); printf(">> status\n"); mg_printf(nc, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n%s", (unsigned long)strlen(buf), buf); } // driver logout // static void handle_logout_input(struct mg_connection *nc, struct http_message *hm) { char msg[][_SLEN] = { "fail error", "ok .", }; printf(">> logout\n"); mg_printf(nc, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n%s", (unsigned long)strlen(msg[1]), msg[1]); } // (manual) next stop pressed // static void handle_nextstop_input(struct mg_connection *nc, struct http_message *hm) { char msg[][_SLEN] = { "fail error", "ok .", }; printf(">> nextstop\n"); mg_printf(nc, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n%s", (unsigned long)strlen(msg[1]), msg[1]); } // (manual) previous stop pressed // static void handle_prevstop_input(struct mg_connection *nc, struct http_message *hm) { char msg[][_SLEN] = { "fail error", "ok .", }; printf(">> prevstop\n"); mg_printf(nc, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n%s", (unsigned long)strlen(msg[1]), msg[1]); } // generic fare input // static void handle_fare_input(struct mg_connection *nc, struct http_message *hm) { int ret; char s_fare[_SLEN], s_count[_SLEN]; char msg[][_SLEN] = { "fail error", "ok .", }; ret = mg_get_http_var(&(hm->body), "fare", s_fare, _SLEN); if (ret<=0) { mg_http_send_error(nc, 404, NULL); return; } printf("got fare %s\n", s_fare); ret = mg_get_http_var(&(hm->body), "count", s_count, _SLEN); if (ret<=0) { mg_http_send_error(nc, 404, NULL); return; } printf("got count %s\n", s_count); mg_printf(nc, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n%s", (unsigned long)strlen(msg[1]), msg[1]); } // driver paddle (route) input // static void handle_paddle_input(struct mg_connection *nc, struct http_message *hm) { int ret; char s_paddle[_SLEN]; char msg[][_SLEN] = { "fail paddle_unknown", "ok paddle", }; ret = mg_get_http_var(&(hm->body), "paddle", s_paddle, _SLEN); if (ret<=0) { mg_http_send_error(nc, 404, NULL); return; } //DEBUG printf("#got paddle %s\n", s_paddle); //DEBUG if (strncmp(s_paddle,"9900", strlen("9900"))!=0) { mg_printf(nc, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n%s", (unsigned long)strlen(msg[0]), msg[0]); return; } mg_printf(nc, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n%s", (unsigned long)strlen(msg[1]), msg[1]); } // driver login // static void handle_driver_login(struct mg_connection *nc, struct http_message *hm) { int ret; char s_driver[_SLEN], s_pin[_SLEN]; char msg[][_SLEN] = { "fail driver_not_found", "fail pin_invalid", "ok driver", "ok admin", }; ret = mg_get_http_var(&(hm->body), "driver", s_driver, _SLEN); if (ret<=0) { mg_http_send_error(nc, 404, NULL); return; } ret = mg_get_http_var(&(hm->body), "pin", s_pin, _SLEN); if (ret<=0) { mg_http_send_error(nc, 404, NULL); return; } //DEBUG printf("#got %s %s\n", s_driver, s_pin); //DEBUG if (strncmp(s_driver,"81000", strlen("81000"))!=0) { mg_printf(nc, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n%s", (unsigned long)strlen(msg[0]), msg[0]); return; } if (strncmp(s_pin,"81000", strlen("81000"))!=0) { mg_printf(nc, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n%s", (unsigned long)strlen(msg[1]), msg[1]); return; } mg_printf(nc, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n%s", (unsigned long)strlen(msg[3]), msg[3]); } // api point // static void handle_req(struct mg_connection *nc, struct http_message *hm) { int ret; char s_action[_SLEN]; char _default_msg[_SLEN] = ""; struct mg_str hdr; ret = mg_get_http_var(&(hm->body), "action", s_action, _SLEN); if (ret==0) { printf(">>> %p\n", s_action); mg_http_send_error(nc, 404, NULL); return; } printf("#req: action: (%s)\n", s_action); if (strncmp(s_action, "driverlogin", strlen("driverlogin"))==0) { handle_driver_login(nc, hm); } else if (strncmp(s_action, "paddleinput", strlen("paddleinput"))==0) { handle_paddle_input(nc, hm); } else if (strncmp(s_action, "prevstop", strlen("prevstop"))==0) { handle_prevstop_input(nc, hm); } else if (strncmp(s_action, "nextstop", strlen("nextstop"))==0) { handle_nextstop_input(nc, hm); } else if (strncmp(s_action, "status", strlen("status"))==0) { handle_status_input(nc, hm); } else if (strncmp(s_action, "logout", strlen("logout"))==0) { handle_logout_input(nc, hm); } else if (strncmp(s_action, "fare", strlen("fare"))==0) { handle_fare_input(nc, hm); } else { mg_http_send_error(nc, 404, NULL); } } static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) { struct http_message *hm = (struct http_message *) ev_data; int debug_print = 0; int i; char buf[1024]; if (debug_print) { mg_sock_addr_to_str(&(nc->sa), buf, 1023, MG_SOCK_STRINGIFY_IP); printf("%s\n", buf); } switch (ev){ case MG_EV_HTTP_REQUEST: if (mg_vcmp(&hm->uri, "/req")==0) { handle_req(nc, (struct http_message *)ev_data); } else { mg_serve_http(nc, (struct http_message *) ev_data, s_http_server_opts); } break; default: //printf("? %i\n", ev); break; } } int main(void) { struct mg_mgr mgr; struct mg_connection *nc; mg_mgr_init(&mgr, NULL); printf("Starting web server on port %s\n", s_http_port); nc = mg_bind(&mgr, s_http_port, ev_handler); if (nc == NULL) { printf("Failed to create listener\n"); return 1; } // Set up HTTP server parameters mg_set_protocol_http_websocket(nc); s_http_server_opts.document_root = "html"; s_http_server_opts.enable_directory_listing = "no"; for (;;) { mg_mgr_poll(&mgr, 1000); } mg_mgr_free(&mgr); return 0; }