mirror of
https://github.com/cesanta/mongoose.git
synced 2025-06-08 01:42:52 +08:00
clang-format *ALL* the things!
PUBLISHED_FROM=faf0beb7545eb426c941fc366b6f87667723eb4c
This commit is contained in:
parent
dcf1cedec9
commit
183fc7ced9
@ -1,3 +1,4 @@
|
|||||||
|
/* clang-format off */
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2015, Texas Instruments Incorporated
|
* Copyright (c) 2015, Texas Instruments Incorporated
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
/* clang-format off */
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2015-2016, Texas Instruments Incorporated
|
* Copyright (c) 2015-2016, Texas Instruments Incorporated
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
/* clang-format off */
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2015, Texas Instruments Incorporated
|
* Copyright (c) 2015, Texas Instruments Incorporated
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
|
@ -37,7 +37,8 @@
|
|||||||
|
|
||||||
#include "wifi.h"
|
#include "wifi.h"
|
||||||
|
|
||||||
static const char *upload_form = "\
|
static const char *upload_form =
|
||||||
|
"\
|
||||||
<h1>Upload file</h1> \
|
<h1>Upload file</h1> \
|
||||||
<form action='/upload' method='POST' enctype='multipart/form-data'> \
|
<form action='/upload' method='POST' enctype='multipart/form-data'> \
|
||||||
<input type='file' name='file'> \
|
<input type='file' name='file'> \
|
||||||
@ -75,8 +76,7 @@ void mg_ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
|
|||||||
("HTTP request from %s: %.*s %.*s", addr, (int) hm->method.len,
|
("HTTP request from %s: %.*s %.*s", addr, (int) hm->method.len,
|
||||||
hm->method.p, (int) hm->uri.len, hm->uri.p));
|
hm->method.p, (int) hm->uri.len, hm->uri.p));
|
||||||
if (mg_vcmp(&hm->uri, "/upload") == 0 ||
|
if (mg_vcmp(&hm->uri, "/upload") == 0 ||
|
||||||
(mg_vcmp(&hm->uri, "/") == 0 &&
|
(mg_vcmp(&hm->uri, "/") == 0 && mg_stat("SL:index.html", &st) != 0)) {
|
||||||
mg_stat("SL:index.html", &st) != 0)) {
|
|
||||||
mg_send(nc, upload_form, strlen(upload_form));
|
mg_send(nc, upload_form, strlen(upload_form));
|
||||||
nc->flags |= MG_F_SEND_AND_CLOSE;
|
nc->flags |= MG_F_SEND_AND_CLOSE;
|
||||||
break;
|
break;
|
||||||
@ -107,8 +107,8 @@ void mg_ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void mg_init(struct mg_mgr *mgr) {
|
static void mg_init(struct mg_mgr *mgr) {
|
||||||
WiFi_Params wifiParams;
|
WiFi_Params wifiParams;
|
||||||
WiFi_Handle handle;
|
WiFi_Handle handle;
|
||||||
|
|
||||||
LOG(LL_INFO, ("MG task running"));
|
LOG(LL_INFO, ("MG task running"));
|
||||||
|
|
||||||
@ -153,23 +153,23 @@ static void mg_init(struct mg_mgr *mgr) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
Board_initGeneral();
|
Board_initGeneral();
|
||||||
Board_initGPIO();
|
Board_initGPIO();
|
||||||
Board_initWiFi();
|
Board_initWiFi();
|
||||||
|
|
||||||
setvbuf(stdout, NULL, _IOLBF, 0);
|
setvbuf(stdout, NULL, _IOLBF, 0);
|
||||||
setvbuf(stderr, NULL, _IOLBF, 0);
|
setvbuf(stderr, NULL, _IOLBF, 0);
|
||||||
cs_log_set_level(LL_INFO);
|
cs_log_set_level(LL_INFO);
|
||||||
cs_log_set_file(stdout);
|
cs_log_set_file(stdout);
|
||||||
|
|
||||||
if (!mg_start_task(MG_TASK_PRIORITY, MG_TASK_STACK_SIZE, mg_init)) {
|
if (!mg_start_task(MG_TASK_PRIORITY, MG_TASK_STACK_SIZE, mg_init)) {
|
||||||
LOG(LL_ERROR, ("Error starting Mongoose task"));
|
LOG(LL_ERROR, ("Error starting Mongoose task"));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
osi_start();
|
osi_start();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SimpleLinkHttpServerCallback(SlHttpServerEvent_t *e,
|
void SimpleLinkHttpServerCallback(SlHttpServerEvent_t *e,
|
||||||
|
@ -88,8 +88,8 @@ bool wifi_setup_ap(const char *ssid, const char *pass, int channel) {
|
|||||||
sl_Stop(0);
|
sl_Stop(0);
|
||||||
int role = sl_Start(NULL, NULL, NULL);
|
int role = sl_Start(NULL, NULL, NULL);
|
||||||
if (role != ROLE_AP) {
|
if (role != ROLE_AP) {
|
||||||
LOG(LL_ERROR, ("Expected ROLE_AP (%d), got %d", ROLE_AP, role));
|
LOG(LL_ERROR, ("Expected ROLE_AP (%d), got %d", ROLE_AP, role));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (sl_NetAppStart(SL_NET_APP_DHCP_SERVER_ID) != 0) {
|
if (sl_NetAppStart(SL_NET_APP_DHCP_SERVER_ID) != 0) {
|
||||||
LOG(LL_ERROR, ("DHCP server failed to start"));
|
LOG(LL_ERROR, ("DHCP server failed to start"));
|
||||||
|
@ -34,7 +34,7 @@ static void handle_request(struct mg_connection *nc) {
|
|||||||
|
|
||||||
static void handle_upload(struct mg_connection *nc, int ev, void *p) {
|
static void handle_upload(struct mg_connection *nc, int ev, void *p) {
|
||||||
struct file_writer_data *data = (struct file_writer_data *) nc->user_data;
|
struct file_writer_data *data = (struct file_writer_data *) nc->user_data;
|
||||||
struct mg_http_multipart_part *mp = (struct mg_http_multipart_part*)p;
|
struct mg_http_multipart_part *mp = (struct mg_http_multipart_part *) p;
|
||||||
|
|
||||||
switch (ev) {
|
switch (ev) {
|
||||||
case MG_EV_HTTP_PART_BEGIN: {
|
case MG_EV_HTTP_PART_BEGIN: {
|
||||||
|
@ -10,7 +10,7 @@ struct device_settings {
|
|||||||
|
|
||||||
static const char *s_http_port = "8000";
|
static const char *s_http_port = "8000";
|
||||||
static struct mg_serve_http_opts s_http_server_opts;
|
static struct mg_serve_http_opts s_http_server_opts;
|
||||||
static struct device_settings s_settings = { "value1", "value2" };
|
static struct device_settings s_settings = {"value1", "value2"};
|
||||||
|
|
||||||
static void handle_save(struct mg_connection *nc, struct http_message *hm) {
|
static void handle_save(struct mg_connection *nc, struct http_message *hm) {
|
||||||
// Get form variables and store settings values
|
// Get form variables and store settings values
|
||||||
|
@ -10,7 +10,7 @@ struct device_settings {
|
|||||||
|
|
||||||
static const char *s_http_port = "8000";
|
static const char *s_http_port = "8000";
|
||||||
static struct mg_serve_http_opts s_http_server_opts;
|
static struct mg_serve_http_opts s_http_server_opts;
|
||||||
static struct device_settings s_settings = { "value1", "value2" };
|
static struct device_settings s_settings = {"value1", "value2"};
|
||||||
|
|
||||||
static void handle_save(struct mg_connection *nc, struct http_message *hm) {
|
static void handle_save(struct mg_connection *nc, struct http_message *hm) {
|
||||||
// Get form variables and store settings values
|
// Get form variables and store settings values
|
||||||
|
@ -10,7 +10,7 @@ struct device_settings {
|
|||||||
|
|
||||||
static const char *s_http_port = "8000";
|
static const char *s_http_port = "8000";
|
||||||
static struct mg_serve_http_opts s_http_server_opts;
|
static struct mg_serve_http_opts s_http_server_opts;
|
||||||
static struct device_settings s_settings = { "value1", "value2" };
|
static struct device_settings s_settings = {"value1", "value2"};
|
||||||
|
|
||||||
static void handle_save(struct mg_connection *nc, struct http_message *hm) {
|
static void handle_save(struct mg_connection *nc, struct http_message *hm) {
|
||||||
// Get form variables and store settings values
|
// Get form variables and store settings values
|
||||||
|
@ -11,16 +11,11 @@ static struct mg_serve_http_opts s_http_server_opts;
|
|||||||
static const char *s_login_uri = "/login.html";
|
static const char *s_login_uri = "/login.html";
|
||||||
static const char *s_secret = ":-)"; // Must be known only to server
|
static const char *s_secret = ":-)"; // Must be known only to server
|
||||||
|
|
||||||
|
|
||||||
static void generate_ssid(const char *user_name, const char *expiration_date,
|
static void generate_ssid(const char *user_name, const char *expiration_date,
|
||||||
char *ssid, size_t ssid_size) {
|
char *ssid, size_t ssid_size) {
|
||||||
char hash[33];
|
char hash[33];
|
||||||
cs_md5(hash,
|
cs_md5(hash, user_name, strlen(user_name), ":", (size_t) 1, expiration_date,
|
||||||
user_name, strlen(user_name),
|
strlen(expiration_date), ":", (size_t) 1, s_secret, strlen(s_secret),
|
||||||
":", (size_t) 1,
|
|
||||||
expiration_date, strlen(expiration_date),
|
|
||||||
":", (size_t) 1,
|
|
||||||
s_secret, strlen(s_secret),
|
|
||||||
NULL);
|
NULL);
|
||||||
snprintf(ssid, ssid_size, "%s|%s|%s", user_name, expiration_date, hash);
|
snprintf(ssid, ssid_size, "%s|%s|%s", user_name, expiration_date, hash);
|
||||||
}
|
}
|
||||||
@ -36,8 +31,8 @@ static int check_auth(struct http_message *hm) {
|
|||||||
// Look for session ID in the Cookie.
|
// Look for session ID in the Cookie.
|
||||||
// That session ID can be validated against the database that stores
|
// That session ID can be validated against the database that stores
|
||||||
// current active sessions.
|
// current active sessions.
|
||||||
mg_http_parse_header(
|
mg_http_parse_header(mg_get_http_header(hm, "Cookie"), "ssid", ssid,
|
||||||
mg_get_http_header(hm, "Cookie"), "ssid", ssid, sizeof(ssid));
|
sizeof(ssid));
|
||||||
if (sscanf(ssid, "%[^|]|%[^|]|", name, expire) == 2) {
|
if (sscanf(ssid, "%[^|]|%[^|]|", name, expire) == 2) {
|
||||||
generate_ssid(name, expire, calculated_ssid, sizeof(calculated_ssid));
|
generate_ssid(name, expire, calculated_ssid, sizeof(calculated_ssid));
|
||||||
if (strcmp(ssid, calculated_ssid) == 0) {
|
if (strcmp(ssid, calculated_ssid) == 0) {
|
||||||
@ -59,7 +54,6 @@ static void check_login_form_submission(struct mg_connection *c,
|
|||||||
// A real authentication mechanism should be employed here.
|
// A real authentication mechanism should be employed here.
|
||||||
// Also, the whole site should be served through HTTPS.
|
// Also, the whole site should be served through HTTPS.
|
||||||
if (strcmp(name, "Joe") == 0 && strcmp(password, "Doe") == 0) {
|
if (strcmp(name, "Joe") == 0 && strcmp(password, "Doe") == 0) {
|
||||||
|
|
||||||
// Generate expiry date
|
// Generate expiry date
|
||||||
time_t t = time(NULL) + 3600; // Valid for 1 hour
|
time_t t = time(NULL) + 3600; // Valid for 1 hour
|
||||||
snprintf(expire_epoch, sizeof(expire_epoch), "%lu", (unsigned long) t);
|
snprintf(expire_epoch, sizeof(expire_epoch), "%lu", (unsigned long) t);
|
||||||
@ -73,7 +67,7 @@ static void check_login_form_submission(struct mg_connection *c,
|
|||||||
"Location: /\r\n\r\n",
|
"Location: /\r\n\r\n",
|
||||||
ssid, expire);
|
ssid, expire);
|
||||||
} else {
|
} else {
|
||||||
mg_printf(c, "%s", "HTTP/1.1 302 Moved\r\nLocation: /\r\n\r\n");
|
mg_printf(c, "%s", "HTTP/1.1 302 Moved\r\nLocation: /\r\n\r\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,8 +16,8 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
|
|||||||
|
|
||||||
switch (ev) {
|
switch (ev) {
|
||||||
case MG_EV_CONNECT:
|
case MG_EV_CONNECT:
|
||||||
if (* (int *) ev_data != 0) {
|
if (*(int *) ev_data != 0) {
|
||||||
fprintf(stderr, "connect() failed: %s\n", strerror(* (int *) ev_data));
|
fprintf(stderr, "connect() failed: %s\n", strerror(*(int *) ev_data));
|
||||||
s_exit_flag = 1;
|
s_exit_flag = 1;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -54,8 +54,8 @@ int main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (i + 1 != argc) {
|
if (i + 1 != argc) {
|
||||||
fprintf(stderr, "Usage: %s [%s] [--hexdump <file>] <URL>\n",
|
fprintf(stderr, "Usage: %s [%s] [--hexdump <file>] <URL>\n", argv[0],
|
||||||
argv[0], s_show_headers_opt);
|
s_show_headers_opt);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,15 +31,16 @@ static int rpc_sum(char *buf, int len, struct mg_rpc_request *req) {
|
|||||||
|
|
||||||
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
|
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
|
||||||
struct http_message *hm = (struct http_message *) ev_data;
|
struct http_message *hm = (struct http_message *) ev_data;
|
||||||
static const char *methods[] = { "sum", NULL };
|
static const char *methods[] = {"sum", NULL};
|
||||||
static mg_rpc_handler_t handlers[] = { rpc_sum, NULL };
|
static mg_rpc_handler_t handlers[] = {rpc_sum, NULL};
|
||||||
char buf[100];
|
char buf[100];
|
||||||
|
|
||||||
switch (ev) {
|
switch (ev) {
|
||||||
case MG_EV_HTTP_REQUEST:
|
case MG_EV_HTTP_REQUEST:
|
||||||
mg_rpc_dispatch(hm->body.p, hm->body.len, buf, sizeof(buf),
|
mg_rpc_dispatch(hm->body.p, hm->body.len, buf, sizeof(buf), methods,
|
||||||
methods, handlers);
|
handlers);
|
||||||
mg_printf(nc, "HTTP/1.0 200 OK\r\nContent-Length: %d\r\n"
|
mg_printf(nc,
|
||||||
|
"HTTP/1.0 200 OK\r\nContent-Length: %d\r\n"
|
||||||
"Content-Type: application/json\r\n\r\n%s",
|
"Content-Type: application/json\r\n\r\n%s",
|
||||||
(int) strlen(buf), buf);
|
(int) strlen(buf), buf);
|
||||||
nc->flags |= MG_F_SEND_AND_CLOSE;
|
nc->flags |= MG_F_SEND_AND_CLOSE;
|
||||||
|
@ -37,7 +37,7 @@ int main(void) {
|
|||||||
* and subscriptions
|
* and subscriptions
|
||||||
*/
|
*/
|
||||||
|
|
||||||
for(;;) {
|
for (;;) {
|
||||||
mg_mgr_poll(&mgr, 1000);
|
mg_mgr_poll(&mgr, 1000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,9 +20,10 @@ static void ev_handler(struct mg_connection *c, int ev, void *p) {
|
|||||||
sleep(3);
|
sleep(3);
|
||||||
|
|
||||||
/* Send the reply */
|
/* Send the reply */
|
||||||
snprintf(reply, sizeof(reply), "{ \"uri\": \"%.*s\" }\n",
|
snprintf(reply, sizeof(reply), "{ \"uri\": \"%.*s\" }\n", (int) hm->uri.len,
|
||||||
(int) hm->uri.len, hm->uri.p);
|
hm->uri.p);
|
||||||
mg_printf(c, "HTTP/1.1 200 OK\r\n"
|
mg_printf(c,
|
||||||
|
"HTTP/1.1 200 OK\r\n"
|
||||||
"Content-Type: application/json\r\n"
|
"Content-Type: application/json\r\n"
|
||||||
"Content-Length: %d\r\n"
|
"Content-Length: %d\r\n"
|
||||||
"\r\n"
|
"\r\n"
|
||||||
|
@ -30,7 +30,8 @@ static void signal_handler(int sig_num) {
|
|||||||
static void show_usage_and_exit(const char *prog_name) {
|
static void show_usage_and_exit(const char *prog_name) {
|
||||||
fprintf(stderr, "%s\n", "Copyright (c) 2014 CESANTA SOFTWARE");
|
fprintf(stderr, "%s\n", "Copyright (c) 2014 CESANTA SOFTWARE");
|
||||||
fprintf(stderr, "%s\n", "Usage:");
|
fprintf(stderr, "%s\n", "Usage:");
|
||||||
fprintf(stderr, " %s\n [-d debug_file] [-l] [tcp|ssl]://[ip:]port[:cert][:ca_cert]",
|
fprintf(stderr,
|
||||||
|
" %s\n [-d debug_file] [-l] [tcp|ssl]://[ip:]port[:cert][:ca_cert]",
|
||||||
prog_name);
|
prog_name);
|
||||||
fprintf(stderr, "%s\n", "Examples:");
|
fprintf(stderr, "%s\n", "Examples:");
|
||||||
fprintf(stderr, " %s\n -d hexdump.txt ssl://google.com:443", prog_name);
|
fprintf(stderr, " %s\n -d hexdump.txt ssl://google.com:443", prog_name);
|
||||||
@ -40,7 +41,7 @@ static void show_usage_and_exit(const char *prog_name) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void on_stdin_read(struct mg_connection *nc, int ev, void *p) {
|
static void on_stdin_read(struct mg_connection *nc, int ev, void *p) {
|
||||||
int ch = * (int *) p;
|
int ch = *(int *) p;
|
||||||
|
|
||||||
(void) ev;
|
(void) ev;
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@
|
|||||||
#include "mongoose.h"
|
#include "mongoose.h"
|
||||||
|
|
||||||
static void *stdin_thread(void *param) {
|
static void *stdin_thread(void *param) {
|
||||||
int ch, sock = * (int *) param;
|
int ch, sock = *(int *) param;
|
||||||
while ((ch = getchar()) != EOF) {
|
while ((ch = getchar()) != EOF) {
|
||||||
unsigned char c = (unsigned char) ch;
|
unsigned char c = (unsigned char) ch;
|
||||||
send(sock, &c, 1, 0); // Forward all types characters to the socketpair
|
send(sock, &c, 1, 0); // Forward all types characters to the socketpair
|
||||||
@ -93,7 +93,7 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
// The other end of a pair goes inside the server
|
// The other end of a pair goes inside the server
|
||||||
ioconn = mg_add_sock(&mgr, fds[0], client_handler);
|
ioconn = mg_add_sock(&mgr, fds[0], client_handler);
|
||||||
ioconn->flags |= MG_F_USER_1; // Mark this so we know this is a stdin
|
ioconn->flags |= MG_F_USER_1; // Mark this so we know this is a stdin
|
||||||
ioconn->user_data = server_conn;
|
ioconn->user_data = server_conn;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
@ -34,8 +34,10 @@ static void push_frame_to_clients(struct mg_mgr *mgr,
|
|||||||
for (nc = mg_next(mgr, NULL); nc != NULL; nc = mg_next(mgr, nc)) {
|
for (nc = mg_next(mgr, NULL); nc != NULL; nc = mg_next(mgr, nc)) {
|
||||||
if (!(nc->flags & MG_F_USER_2)) continue; // Ignore un-marked requests
|
if (!(nc->flags & MG_F_USER_2)) continue; // Ignore un-marked requests
|
||||||
|
|
||||||
mg_printf(nc, "--w00t\r\nContent-Type: image/jpeg\r\n"
|
mg_printf(nc,
|
||||||
"Content-Length: %lu\r\n\r\n", (unsigned long) wm->size);
|
"--w00t\r\nContent-Type: image/jpeg\r\n"
|
||||||
|
"Content-Length: %lu\r\n\r\n",
|
||||||
|
(unsigned long) wm->size);
|
||||||
mg_send(nc, wm->data, wm->size);
|
mg_send(nc, wm->data, wm->size);
|
||||||
mg_send(nc, "\r\n", 2);
|
mg_send(nc, "\r\n", 2);
|
||||||
printf("Image pushed to %p\n", nc);
|
printf("Image pushed to %p\n", nc);
|
||||||
@ -50,7 +52,8 @@ static void send_command_to_the_device(struct mg_mgr *mgr,
|
|||||||
const struct mg_str *cmd) {
|
const struct mg_str *cmd) {
|
||||||
struct mg_connection *nc;
|
struct mg_connection *nc;
|
||||||
for (nc = mg_next(mgr, NULL); nc != NULL; nc = mg_next(mgr, nc)) {
|
for (nc = mg_next(mgr, NULL); nc != NULL; nc = mg_next(mgr, nc)) {
|
||||||
if (!(nc->flags & MG_F_IS_WEBSOCKET)) continue; // Ignore non-websocket requests
|
if (!(nc->flags & MG_F_IS_WEBSOCKET))
|
||||||
|
continue; // Ignore non-websocket requests
|
||||||
|
|
||||||
mg_send_websocket_frame(nc, WEBSOCKET_OP_TEXT, cmd->p, cmd->len);
|
mg_send_websocket_frame(nc, WEBSOCKET_OP_TEXT, cmd->p, cmd->len);
|
||||||
printf("Sent API command [%.*s] to %p\n", (int) cmd->len, cmd->p, nc);
|
printf("Sent API command [%.*s] to %p\n", (int) cmd->len, cmd->p, nc);
|
||||||
@ -75,15 +78,15 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
|
|||||||
switch (ev) {
|
switch (ev) {
|
||||||
case MG_EV_HTTP_REQUEST:
|
case MG_EV_HTTP_REQUEST:
|
||||||
if (mg_vcmp(&hm->uri, "/mjpg") == 0) {
|
if (mg_vcmp(&hm->uri, "/mjpg") == 0) {
|
||||||
nc->flags |= MG_F_USER_2; /* Set a mark on image requests */
|
nc->flags |= MG_F_USER_2; /* Set a mark on image requests */
|
||||||
mg_printf(nc, "%s",
|
mg_printf(nc, "%s",
|
||||||
"HTTP/1.0 200 OK\r\n"
|
"HTTP/1.0 200 OK\r\n"
|
||||||
"Cache-Control: no-cache\r\n"
|
"Cache-Control: no-cache\r\n"
|
||||||
"Pragma: no-cache\r\n"
|
"Pragma: no-cache\r\n"
|
||||||
"Expires: Thu, 01 Dec 1994 16:00:00 GMT\r\n"
|
"Expires: Thu, 01 Dec 1994 16:00:00 GMT\r\n"
|
||||||
"Connection: close\r\n"
|
"Connection: close\r\n"
|
||||||
"Content-Type: multipart/x-mixed-replace; "
|
"Content-Type: multipart/x-mixed-replace; "
|
||||||
"boundary=--w00t\r\n\r\n");
|
"boundary=--w00t\r\n\r\n");
|
||||||
} else if (mg_vcmp(&hm->uri, "/api") == 0 && hm->body.len > 0) {
|
} else if (mg_vcmp(&hm->uri, "/api") == 0 && hm->body.len > 0) {
|
||||||
/*
|
/*
|
||||||
* RESTful API call. HTTP message body should be a JSON message.
|
* RESTful API call. HTTP message body should be a JSON message.
|
||||||
@ -129,13 +132,13 @@ int main(int argc, char *argv[]) {
|
|||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
mg_set_protocol_http_websocket(nc);
|
mg_set_protocol_http_websocket(nc);
|
||||||
web_root_opts.document_root = "./web_root";
|
web_root_opts.document_root = "./web_root";
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* We explicitly hand over control to the Mongoose manager
|
* We explicitly hand over control to the Mongoose manager
|
||||||
* in this event loop and we can easily multiplex other activities.
|
* in this event loop and we can easily multiplex other activities.
|
||||||
*/
|
*/
|
||||||
for(;;) {
|
for (;;) {
|
||||||
mg_mgr_poll(&mgr, 1000);
|
mg_mgr_poll(&mgr, 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,7 +53,8 @@ static void send_mjpg_frame(struct mg_connection *nc, const char *file_path) {
|
|||||||
|
|
||||||
/* Send those buffer through the websocket connection */
|
/* Send those buffer through the websocket connection */
|
||||||
mg_send_websocket_frame(nc, WEBSOCKET_OP_BINARY, buf, sizeof(buf));
|
mg_send_websocket_frame(nc, WEBSOCKET_OP_BINARY, buf, sizeof(buf));
|
||||||
printf("Sent mjpg frame, %lu bytes after skippping %d frames\n", (unsigned long) sizeof(buf), skipped_frames);
|
printf("Sent mjpg frame, %lu bytes after skippping %d frames\n",
|
||||||
|
(unsigned long) sizeof(buf), skipped_frames);
|
||||||
skipped_frames = 0;
|
skipped_frames = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -64,8 +65,10 @@ static void send_mjpg_frame(struct mg_connection *nc, const char *file_path) {
|
|||||||
*/
|
*/
|
||||||
static void set_led(int v) {
|
static void set_led(int v) {
|
||||||
char cmd[512];
|
char cmd[512];
|
||||||
snprintf(cmd, sizeof(cmd), "for i in 22 23 24; do"
|
snprintf(cmd, sizeof(cmd),
|
||||||
" echo %d >/sys/class/gpio/gpio$i/value; done", v);
|
"for i in 22 23 24; do"
|
||||||
|
" echo %d >/sys/class/gpio/gpio$i/value; done",
|
||||||
|
v);
|
||||||
system(cmd);
|
system(cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,7 +76,7 @@ static void set_led(int v) {
|
|||||||
* Parse control JSON and perform command:
|
* Parse control JSON and perform command:
|
||||||
* for now only LED on/off is supported.
|
* for now only LED on/off is supported.
|
||||||
*/
|
*/
|
||||||
static void perform_control_command(const char* data, size_t len) {
|
static void perform_control_command(const char *data, size_t len) {
|
||||||
struct json_token toks[200], *onoff;
|
struct json_token toks[200], *onoff;
|
||||||
parse_json(data, len, toks, sizeof(toks));
|
parse_json(data, len, toks, sizeof(toks));
|
||||||
onoff = find_json_token(toks, "onoff");
|
onoff = find_json_token(toks, "onoff");
|
||||||
@ -86,16 +89,16 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
|
|||||||
|
|
||||||
switch (ev) {
|
switch (ev) {
|
||||||
case MG_EV_CONNECT:
|
case MG_EV_CONNECT:
|
||||||
printf("Reconnect: %s\n", * (int *) ev_data == 0 ? "ok" : "failed");
|
printf("Reconnect: %s\n", *(int *) ev_data == 0 ? "ok" : "failed");
|
||||||
if (* (int *) ev_data == 0) {
|
if (*(int *) ev_data == 0) {
|
||||||
/*
|
/*
|
||||||
* Tune the tcp send buffer size, so that we can skip frames
|
* Tune the tcp send buffer size, so that we can skip frames
|
||||||
* when the connection is congested. This helps maintaining a
|
* when the connection is congested. This helps maintaining a
|
||||||
* reasonable latency.
|
* reasonable latency.
|
||||||
*/
|
*/
|
||||||
int sndbuf_size = 512;
|
int sndbuf_size = 512;
|
||||||
if(setsockopt(nc->sock, SOL_SOCKET, SO_SNDBUF,
|
if (setsockopt(nc->sock, SOL_SOCKET, SO_SNDBUF, (void *) &sndbuf_size,
|
||||||
(void *) &sndbuf_size, sizeof(int)) == -1) {
|
sizeof(int)) == -1) {
|
||||||
perror("failed to tune TCP send buffer size\n");
|
perror("failed to tune TCP send buffer size\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -111,7 +114,7 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
|
|||||||
break;
|
break;
|
||||||
case MG_EV_WEBSOCKET_FRAME:
|
case MG_EV_WEBSOCKET_FRAME:
|
||||||
printf("Got control command: [%.*s]\n", (int) wm->size, wm->data);
|
printf("Got control command: [%.*s]\n", (int) wm->size, wm->data);
|
||||||
perform_control_command((const char*)wm->data, wm->size);
|
perform_control_command((const char *) wm->data, wm->size);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -124,11 +127,13 @@ static void *generate_mjpg_data_thread_func(void *param) {
|
|||||||
char cmd[400];
|
char cmd[400];
|
||||||
(void) param;
|
(void) param;
|
||||||
|
|
||||||
snprintf(cmd, sizeof(cmd), "raspistill -w %d -h %d -n -q 100 -tl %d "
|
snprintf(cmd, sizeof(cmd),
|
||||||
"-t 999999999 -v %s -o %s >/dev/null 2>&1", s_width, s_height,
|
"raspistill -w %d -h %d -n -q 100 -tl %d "
|
||||||
s_still_period, s_vertical_flip ? "-vf" : "", s_mjpg_file);
|
"-t 999999999 -v %s -o %s >/dev/null 2>&1",
|
||||||
|
s_width, s_height, s_still_period, s_vertical_flip ? "-vf" : "",
|
||||||
|
s_mjpg_file);
|
||||||
|
|
||||||
for(;;) {
|
for (;;) {
|
||||||
int ret = system(cmd);
|
int ret = system(cmd);
|
||||||
if (WIFSIGNALED(ret)) exit(1);
|
if (WIFSIGNALED(ret)) exit(1);
|
||||||
sleep(1);
|
sleep(1);
|
||||||
@ -152,7 +157,7 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
mg_mgr_init(&mgr, NULL);
|
mg_mgr_init(&mgr, NULL);
|
||||||
|
|
||||||
for(;;) {
|
for (;;) {
|
||||||
mg_mgr_poll(&mgr, s_poll_interval_ms);
|
mg_mgr_poll(&mgr, s_poll_interval_ms);
|
||||||
|
|
||||||
/* Reconnect if disconnected */
|
/* Reconnect if disconnected */
|
||||||
|
@ -12,7 +12,7 @@ struct device_settings {
|
|||||||
|
|
||||||
static const char *s_http_port = "8000";
|
static const char *s_http_port = "8000";
|
||||||
static struct mg_serve_http_opts s_http_server_opts;
|
static struct mg_serve_http_opts s_http_server_opts;
|
||||||
static struct device_settings s_settings = { "value1", "value2" };
|
static struct device_settings s_settings = {"value1", "value2"};
|
||||||
|
|
||||||
static void handle_save(struct mg_connection *nc, struct http_message *hm) {
|
static void handle_save(struct mg_connection *nc, struct http_message *hm) {
|
||||||
/* Get form variables and store settings values */
|
/* Get form variables and store settings values */
|
||||||
@ -40,9 +40,9 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
|
|||||||
switch (ev) {
|
switch (ev) {
|
||||||
case MG_EV_HTTP_REQUEST:
|
case MG_EV_HTTP_REQUEST:
|
||||||
if (mg_vcmp(&hm->uri, "/save") == 0) {
|
if (mg_vcmp(&hm->uri, "/save") == 0) {
|
||||||
handle_save(nc, hm); /* Handle RESTful call */
|
handle_save(nc, hm); /* Handle RESTful call */
|
||||||
} else {
|
} else {
|
||||||
mg_serve_http(nc, hm, s_http_server_opts); /* Serve static content */
|
mg_serve_http(nc, hm, s_http_server_opts); /* Serve static content */
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case MG_EV_SSI_CALL:
|
case MG_EV_SSI_CALL:
|
||||||
@ -63,11 +63,11 @@ int main(int argc, char *argv[]) {
|
|||||||
mg_set_protocol_http_websocket(nc);
|
mg_set_protocol_http_websocket(nc);
|
||||||
s_http_server_opts.document_root = "./web_root";
|
s_http_server_opts.document_root = "./web_root";
|
||||||
s_http_server_opts.auth_domain = "example.com";
|
s_http_server_opts.auth_domain = "example.com";
|
||||||
//mgr.hexdump_file = "/dev/stdout";
|
// mgr.hexdump_file = "/dev/stdout";
|
||||||
|
|
||||||
/* If our current directory */
|
/* If our current directory */
|
||||||
if (argc > 0 && (p = strrchr(argv[0], '/'))) {
|
if (argc > 0 && (p = strrchr(argv[0], '/'))) {
|
||||||
snprintf(path, sizeof(path), "%.*s/web_root", (int)(p - argv[0]), argv[0]);
|
snprintf(path, sizeof(path), "%.*s/web_root", (int) (p - argv[0]), argv[0]);
|
||||||
s_http_server_opts.document_root = path;
|
s_http_server_opts.document_root = path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ int main(void) {
|
|||||||
|
|
||||||
// Set up HTTP server parameters
|
// Set up HTTP server parameters
|
||||||
mg_set_protocol_http_websocket(nc);
|
mg_set_protocol_http_websocket(nc);
|
||||||
s_http_server_opts.document_root = "."; // Serve current directory
|
s_http_server_opts.document_root = "."; // Serve current directory
|
||||||
s_http_server_opts.dav_document_root = "."; // Allow access via WebDav
|
s_http_server_opts.dav_document_root = "."; // Allow access via WebDav
|
||||||
s_http_server_opts.enable_directory_listing = "yes";
|
s_http_server_opts.enable_directory_listing = "yes";
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ static void ev_handler(struct mg_connection *nc, int ev, void *p) {
|
|||||||
switch (ev) {
|
switch (ev) {
|
||||||
case MG_EV_RECV:
|
case MG_EV_RECV:
|
||||||
mg_send(nc, io->buf, io->len); // Echo message back
|
mg_send(nc, io->buf, io->len); // Echo message back
|
||||||
mbuf_remove(io, io->len); // Discard message from recv buffer
|
mbuf_remove(io, io->len); // Discard message from recv buffer
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
|
@ -22,8 +22,8 @@ static void ev_handler(struct mg_connection *nc, int ev, void *p) {
|
|||||||
(void) p;
|
(void) p;
|
||||||
switch (ev) {
|
switch (ev) {
|
||||||
case MG_EV_RECV:
|
case MG_EV_RECV:
|
||||||
mg_send(nc, io->buf, io->len); // Echo message back
|
mg_send(nc, io->buf, io->len); // Echo message back
|
||||||
mbuf_remove(io, io->len); // Discard message from recv buffer
|
mbuf_remove(io, io->len); // Discard message from recv buffer
|
||||||
// In case of UDP, Mongoose creates new virtual connection for
|
// In case of UDP, Mongoose creates new virtual connection for
|
||||||
// incoming messages
|
// incoming messages
|
||||||
// We can keep it (and it will be reused for another messages from
|
// We can keep it (and it will be reused for another messages from
|
||||||
|
Loading…
Reference in New Issue
Block a user