diff --git a/include/main.h b/include/main.h index f31fe63..f997ffa 100644 --- a/include/main.h +++ b/include/main.h @@ -4,18 +4,18 @@ #include "mgos.h" #include "mgos_gpio.h" -#define CHANNEL_CHANGE_COOLDOWN_SECONDS 0.250 -#define CHANNEL_MAX 16 -#define GPIO_INVALID 255 -#define GPIO_MIN 0 -#define GPIO_MAX 16 +#define CHANNEL_CHANGE_COOLDOWN_SECONDS 0.250 +#define CHANNEL_MAX 16 +#define GPIO_INVALID 255 +#define GPIO_MIN 0 +#define GPIO_MAX 16 struct channel_t { uint8_t button_gpio; uint8_t led_gpio; uint8_t relay_gpio; - bool relay_state; - double button_last_change; + bool relay_state; + double button_last_change; }; void statusled_blink(); diff --git a/include/mqtt.h b/include/mqtt.h index 3335f20..886f669 100644 --- a/include/mqtt.h +++ b/include/mqtt.h @@ -4,9 +4,9 @@ #include "mgos.h" #include "mgos_mqtt.h" -#define MQTT_TOPIC_PREFIX "" -#define MQTT_TOPIC_BROADCAST_CMD "/mongoose/broadcast" -#define MQTT_TOPIC_BROADCAST_STAT "/mongoose/broadcast/stat" +#define MQTT_TOPIC_PREFIX "" +#define MQTT_TOPIC_BROADCAST_CMD "/mongoose/broadcast" +#define MQTT_TOPIC_BROADCAST_STAT "/mongoose/broadcast/stat" void mqtt_init(); void mqtt_publish_stat(const char *stat, const char *fmt, ...); diff --git a/libs/ota-http-client/include/mgos_ota_http_client.h b/libs/ota-http-client/include/mgos_ota_http_client.h index 8d1b3f9..0ab57d0 100644 --- a/libs/ota-http-client/include/mgos_ota_http_client.h +++ b/libs/ota-http-client/include/mgos_ota_http_client.h @@ -20,6 +20,7 @@ extern "C" { bool mgos_ota_http_client_init(void); void mgos_ota_http_start(struct update_context *ctx, const char *url); + #endif #ifdef __cplusplus diff --git a/libs/ota-http-client/src/mgos_ota_http_client.c b/libs/ota-http-client/src/mgos_ota_http_client.c index 616338a..64974d9 100644 --- a/libs/ota-http-client/src/mgos_ota_http_client.c +++ b/libs/ota-http-client/src/mgos_ota_http_client.c @@ -17,104 +17,118 @@ static void fw_download_handler(struct mg_connection *c, int ev, void *p, void *user_data) { - struct mbuf *io = &c->recv_mbuf; - struct update_context *ctx = (struct update_context *) user_data; - int res = 0; + struct mbuf * io = &c->recv_mbuf; + struct update_context *ctx = (struct update_context *)user_data; + int res = 0; struct mg_str *loc; - (void) p; + + (void)p; switch (ev) { - case MG_EV_CONNECT: { - int result = *((int *) p); - if (result != 0) LOG(LL_ERROR, ("connect error: %d", result)); - break; + case MG_EV_CONNECT: { + int result = *((int *)p); + if (result != 0) { + LOG(LL_ERROR, ("connect error: %d", result)); } - case MG_EV_RECV: { - if (ctx->file_size == 0) { - LOG(LL_DEBUG, ("Looking for HTTP header")); - struct http_message hm; - int parsed = mg_parse_http(io->buf, io->len, &hm, 0); - if (parsed <= 0) { - return; - } - if (hm.resp_code != 200) { - if (hm.resp_code == 304) { - ctx->result = 1; - ctx->need_reboot = false; - ctx->status_msg = "Not Modified"; - updater_finish(ctx); - } else if ((hm.resp_code == 301 || hm.resp_code == 302) && - (loc = mg_get_http_header(&hm, "Location")) != NULL) { - /* NUL-terminate the URL. Every header must be followed by \r\n, - * so there is deifnitely space there. */ - ((char *) loc->p)[loc->len] = '\0'; - /* We were told to look elsewhere. Detach update context from this - * connection so that it doesn't get finalized when it's closed. */ - mgos_ota_http_start(ctx, loc->p); - c->user_data = NULL; - } else { - ctx->result = -hm.resp_code; - ctx->need_reboot = false; - ctx->status_msg = "Invalid HTTP response code"; - updater_finish(ctx); - } - c->flags |= MG_F_CLOSE_IMMEDIATELY; - return; - } - if (hm.body.len != 0) { - LOG(LL_DEBUG, ("HTTP header: file size: %d", (int) hm.body.len)); - if (hm.body.len == (size_t) ~0) { - LOG(LL_ERROR, ("Invalid content-length, perhaps chunked-encoding")); - ctx->status_msg = - "Invalid content-length, perhaps chunked-encoding"; - c->flags |= MG_F_CLOSE_IMMEDIATELY; - break; - } else { - ctx->file_size = hm.body.len; - } + break; + } - mbuf_remove(io, parsed); - } + case MG_EV_RECV: { + if (ctx->file_size == 0) { + LOG(LL_DEBUG, ("Looking for HTTP header")); + struct http_message hm; + int parsed = mg_parse_http(io->buf, io->len, &hm, 0); + if (parsed <= 0) { + return; } + if (hm.resp_code != 200) { + if (hm.resp_code == 304) { + ctx->result = 1; + ctx->need_reboot = false; + ctx->status_msg = "Not Modified"; + updater_finish(ctx); + } else if ((hm.resp_code == 301 || hm.resp_code == 302) && + (loc = mg_get_http_header(&hm, "Location")) != NULL) { + /* NUL-terminate the URL. Every header must be followed by \r\n, + * so there is deifnitely space there. */ + ((char *)loc->p)[loc->len] = '\0'; - if (io->len != 0) { - res = updater_process(ctx, io->buf, io->len); - mbuf_remove(io, io->len); - - if (res == 0) { - if (is_write_finished(ctx)) res = updater_finalize(ctx); - if (res == 0) { - /* Need more data, everything is OK */ - break; - } - } - - if (res < 0) { - /* Error */ - LOG(LL_ERROR, ("Update error: %d %s", ctx->result, ctx->status_msg)); + /* We were told to look elsewhere. Detach update context from this + * connection so that it doesn't get finalized when it's closed. */ + mgos_ota_http_start(ctx, loc->p); + c->user_data = NULL; + } else { + ctx->result = -hm.resp_code; + ctx->need_reboot = false; + ctx->status_msg = "Invalid HTTP response code"; + updater_finish(ctx); } c->flags |= MG_F_CLOSE_IMMEDIATELY; + return; } + if (hm.body.len != 0) { + LOG(LL_DEBUG, ("HTTP header: file size: %d", (int)hm.body.len)); + if (hm.body.len == (size_t) ~0) { + LOG(LL_ERROR, ("Invalid content-length, perhaps chunked-encoding")); + ctx->status_msg = + "Invalid content-length, perhaps chunked-encoding"; + c->flags |= MG_F_CLOSE_IMMEDIATELY; + break; + } else { + ctx->file_size = hm.body.len; + } + + mbuf_remove(io, parsed); + } + } + + if (io->len != 0) { + res = updater_process(ctx, io->buf, io->len); + mbuf_remove(io, io->len); + + if (res == 0) { + if (is_write_finished(ctx)) { + res = updater_finalize(ctx); + } + if (res == 0) { + /* Need more data, everything is OK */ + break; + } + } + + if (res < 0) { + /* Error */ + LOG(LL_ERROR, ("Update error: %d %s", ctx->result, ctx->status_msg)); + } + c->flags |= MG_F_CLOSE_IMMEDIATELY; + } + break; + } + + case MG_EV_CLOSE: { + if (ctx == NULL) { break; } - case MG_EV_CLOSE: { - if (ctx == NULL) break; - if (is_write_finished(ctx)) updater_finalize(ctx); - - if (!is_update_finished(ctx)) { - /* Update failed or connection was terminated by server */ - if (ctx->status_msg == NULL) ctx->status_msg = "Update failed"; - ctx->result = -1; - } else if (is_reboot_required(ctx)) { - LOG(LL_INFO, ("Rebooting device")); - mgos_system_restart_after(100); - } - updater_finish(ctx); - updater_context_free(ctx); - c->user_data = NULL; - break; + if (is_write_finished(ctx)) { + updater_finalize(ctx); } + + if (!is_update_finished(ctx)) { + /* Update failed or connection was terminated by server */ + if (ctx->status_msg == NULL) { + ctx->status_msg = "Update failed"; + } + ctx->result = -1; + } else if (is_reboot_required(ctx)) { + LOG(LL_INFO, ("Rebooting device")); + mgos_system_restart_after(100); + } + updater_finish(ctx); + updater_context_free(ctx); + c->user_data = NULL; + break; + } } } @@ -128,12 +142,12 @@ void mgos_ota_http_start(struct update_context *ctx, const char *url) { #if MG_ENABLE_SSL if (strlen(url) > 8 && strncmp(url, "https://", 8) == 0) { opts.ssl_server_name = mgos_sys_config_get_update_ssl_server_name(); - opts.ssl_ca_cert = mgos_sys_config_get_update_ssl_ca_file(); - opts.ssl_cert = mgos_sys_config_get_update_ssl_client_cert_file(); + opts.ssl_ca_cert = mgos_sys_config_get_update_ssl_ca_file(); + opts.ssl_cert = mgos_sys_config_get_update_ssl_client_cert_file(); } #endif - char ehb[150]; + char ehb[150]; char *extra_headers = ehb; mg_asprintf(&extra_headers, sizeof(ehb), "X-MGOS-Device-ID: %s %s\r\n" @@ -145,15 +159,17 @@ void mgos_ota_http_start(struct update_context *ctx, const char *url) { mgos_sys_ro_vars_get_fw_id()); struct mg_connection *c = mg_connect_http_opt( - mgos_get_mgr(), fw_download_handler, ctx, opts, url, extra_headers, NULL); + mgos_get_mgr(), fw_download_handler, ctx, opts, url, extra_headers, NULL); - if (extra_headers != ehb) free(extra_headers); + if (extra_headers != ehb) { + free(extra_headers); + } if (c == NULL) { LOG(LL_ERROR, ("Failed to connect to %s", url)); - ctx->result = -10; + ctx->result = -10; ctx->need_reboot = false; - ctx->status_msg = "Failed to connect"; + ctx->status_msg = "Failed to connect"; updater_finish(ctx); return; } @@ -163,18 +179,24 @@ void mgos_ota_http_start(struct update_context *ctx, const char *url) { static void mgos_ota_timer_cb(void *arg) { const struct mgos_config_update *mcu = mgos_sys_config_get_update(); - if (mcu->url == NULL) return; + + if (mcu->url == NULL) { + return; + } struct update_context *ctx = updater_context_create(); - if (ctx == NULL) return; + if (ctx == NULL) { + return; + } ctx->ignore_same_version = true; ctx->fctx.commit_timeout = mcu->commit_timeout; mgos_ota_http_start(ctx, mcu->url); - (void) arg; + (void)arg; } bool mgos_ota_http_client_init(void) { const struct mgos_config_update *mcu = mgos_sys_config_get_update(); + if (mcu->url != NULL && mcu->interval > 0) { LOG(LL_INFO, ("Updates from %s, every %d seconds", mcu->url, mcu->interval)); diff --git a/libs/ota-http-server/include/mgos_ota_http_server.h b/libs/ota-http-server/include/mgos_ota_http_server.h index 3eb5d2f..6199581 100644 --- a/libs/ota-http-server/include/mgos_ota_http_server.h +++ b/libs/ota-http-server/include/mgos_ota_http_server.h @@ -14,6 +14,7 @@ extern "C" { #if MGOS_ENABLE_UPDATER bool mgos_ota_http_server_init(void); + #endif #ifdef __cplusplus diff --git a/libs/ota-http-server/src/mgos_ota_http_server.c b/libs/ota-http-server/src/mgos_ota_http_server.c index af0aca8..219a6ec 100644 --- a/libs/ota-http-server/src/mgos_ota_http_server.c +++ b/libs/ota-http-server/src/mgos_ota_http_server.c @@ -16,104 +16,119 @@ #include "mgos_utils.h" static void handle_update_post(struct mg_connection *c, int ev, void *p) { - struct mg_http_multipart_part *mp = (struct mg_http_multipart_part *) p; - struct update_context *ctx = (struct update_context *) c->user_data; - if (ctx == NULL && ev != MG_EV_HTTP_MULTIPART_REQUEST) return; + struct mg_http_multipart_part *mp = (struct mg_http_multipart_part *)p; + struct update_context * ctx = (struct update_context *)c->user_data; + + if (ctx == NULL && ev != MG_EV_HTTP_MULTIPART_REQUEST) { + return; + } switch (ev) { - case MG_EV_HTTP_MULTIPART_REQUEST: { - ctx = updater_context_create(); - if (ctx != NULL) { - ctx->nc = c; - c->user_data = ctx; - } else { - c->flags |= MG_F_CLOSE_IMMEDIATELY; - } - break; + case MG_EV_HTTP_MULTIPART_REQUEST: { + ctx = updater_context_create(); + if (ctx != NULL) { + ctx->nc = c; + c->user_data = ctx; + } else { + c->flags |= MG_F_CLOSE_IMMEDIATELY; } - case MG_EV_HTTP_PART_BEGIN: { - LOG(LL_DEBUG, ("MG_EV_HTTP_PART_BEGIN: %p %s %s", ctx, mp->var_name, - mp->file_name)); - /* We use ctx->file_name as a temp buffer for non-file variable values. */ - if (mp->file_name[0] == '\0') { - ctx->file_name[0] = '\0'; - } - break; - } - case MG_EV_HTTP_PART_DATA: { - LOG(LL_DEBUG, ("MG_EV_HTTP_PART_DATA: %p %s %s %d", ctx, mp->var_name, - mp->file_name, (int) mp->data.len)); + break; + } - if (mp->file_name[0] == '\0') { - /* It's a non-file form variable. */ - size_t l = strlen(ctx->file_name); - size_t avail = sizeof(ctx->file_name) - l - 1; - strncat(ctx->file_name, mp->data.p, MIN(mp->data.len, avail)); - break; - } else if (!is_update_finished(ctx)) { - updater_process(ctx, mp->data.p, mp->data.len); - LOG(LL_DEBUG, ("updater_process res: %d", ctx->result)); - } else { - /* Don't close connection just yet, not all browsers like that. */ - } + case MG_EV_HTTP_PART_BEGIN: { + LOG(LL_DEBUG, ("MG_EV_HTTP_PART_BEGIN: %p %s %s", ctx, mp->var_name, + mp->file_name)); + /* We use ctx->file_name as a temp buffer for non-file variable values. */ + if (mp->file_name[0] == '\0') { + ctx->file_name[0] = '\0'; + } + break; + } + + case MG_EV_HTTP_PART_DATA: { + LOG(LL_DEBUG, ("MG_EV_HTTP_PART_DATA: %p %s %s %d", ctx, mp->var_name, + mp->file_name, (int)mp->data.len)); + + if (mp->file_name[0] == '\0') { + /* It's a non-file form variable. */ + size_t l = strlen(ctx->file_name); + size_t avail = sizeof(ctx->file_name) - l - 1; + strncat(ctx->file_name, mp->data.p, MIN(mp->data.len, avail)); + break; + } else if (!is_update_finished(ctx)) { + updater_process(ctx, mp->data.p, mp->data.len); + LOG(LL_DEBUG, ("updater_process res: %d", ctx->result)); + } else { + /* Don't close connection just yet, not all browsers like that. */ + } + break; + } + + case MG_EV_HTTP_PART_END: { + LOG(LL_DEBUG, ("MG_EV_HTTP_PART_END: %p %s %s %d", ctx, mp->var_name, + mp->file_name, mp->status)); + /* Part finished with an error. REQUEST_END will follow. */ + if (mp->status < 0) { break; } - case MG_EV_HTTP_PART_END: { - LOG(LL_DEBUG, ("MG_EV_HTTP_PART_END: %p %s %s %d", ctx, mp->var_name, - mp->file_name, mp->status)); - /* Part finished with an error. REQUEST_END will follow. */ - if (mp->status < 0) break; - if (mp->file_name[0] == '\0') { - /* It's a non-file form variable. Value is in ctx->file_name. */ - LOG(LL_DEBUG, ("Got var: %s=%s", mp->var_name, ctx->file_name)); - /* Commit timeout can be set after flashing. */ - if (strcmp(mp->var_name, "commit_timeout") == 0) { - ctx->fctx.commit_timeout = atoi(ctx->file_name); - } - } else { - /* End of the fw part, but there may still be parts with vars to follow, - * which can modify settings (that can be applied post-flashing). */ + if (mp->file_name[0] == '\0') { + /* It's a non-file form variable. Value is in ctx->file_name. */ + LOG(LL_DEBUG, ("Got var: %s=%s", mp->var_name, ctx->file_name)); + /* Commit timeout can be set after flashing. */ + if (strcmp(mp->var_name, "commit_timeout") == 0) { + ctx->fctx.commit_timeout = atoi(ctx->file_name); } + } else { + /* End of the fw part, but there may still be parts with vars to follow, + * which can modify settings (that can be applied post-flashing). */ + } + break; + } + + case MG_EV_HTTP_MULTIPART_REQUEST_END: { + LOG(LL_DEBUG, + ("MG_EV_HTTP_MULTIPART_REQUEST_END: %p %d", ctx, mp->status)); + /* Whatever happens, this is the last thing we do. */ + c->flags |= MG_F_SEND_AND_CLOSE; + + if (ctx == NULL) { break; } - case MG_EV_HTTP_MULTIPART_REQUEST_END: { - LOG(LL_DEBUG, - ("MG_EV_HTTP_MULTIPART_REQUEST_END: %p %d", ctx, mp->status)); - /* Whatever happens, this is the last thing we do. */ + if (is_write_finished(ctx)) { + updater_finalize(ctx); + } + if (!is_update_finished(ctx)) { + ctx->result = -1; + ctx->status_msg = "Update aborted"; + updater_finish(ctx); + } + if (mp->status < 0) { + /* mp->status < 0 means connection is dead, do not send reply */ + } else { + int code = (ctx->result > 0 ? 200 : 400); + mg_send_response_line(c, code, + "Content-Type: text/plain\r\n" + "Connection: close\r\n"); + mg_printf(c, "%s\r\n", + ctx->status_msg ? ctx->status_msg : "Unknown error"); + if (is_reboot_required(ctx)) { + LOG(LL_INFO, ("Rebooting device")); + mgos_system_restart_after(101); + } c->flags |= MG_F_SEND_AND_CLOSE; - - if (ctx == NULL) break; - if (is_write_finished(ctx)) updater_finalize(ctx); - if (!is_update_finished(ctx)) { - ctx->result = -1; - ctx->status_msg = "Update aborted"; - updater_finish(ctx); - } - if (mp->status < 0) { - /* mp->status < 0 means connection is dead, do not send reply */ - } else { - int code = (ctx->result > 0 ? 200 : 400); - mg_send_response_line(c, code, - "Content-Type: text/plain\r\n" - "Connection: close\r\n"); - mg_printf(c, "%s\r\n", - ctx->status_msg ? ctx->status_msg : "Unknown error"); - if (is_reboot_required(ctx)) { - LOG(LL_INFO, ("Rebooting device")); - mgos_system_restart_after(101); - } - c->flags |= MG_F_SEND_AND_CLOSE; - } - updater_context_free(ctx); - c->user_data = NULL; - break; } + updater_context_free(ctx); + c->user_data = NULL; + break; + } } } struct mg_connection *s_update_request_conn; static void mgos_ota_result_cb(struct update_context *ctx) { - if (ctx != updater_context_get_current()) return; + if (ctx != updater_context_get_current()) { + return; + } if (s_update_request_conn != NULL) { int code = (ctx->result > 0 ? 200 : 500); mg_send_response_line(s_update_request_conn, code, @@ -122,112 +137,117 @@ static void mgos_ota_result_cb(struct update_context *ctx) { mg_printf(s_update_request_conn, "(%d) %s\r\n", ctx->result, ctx->status_msg); s_update_request_conn->flags |= MG_F_SEND_AND_CLOSE; - s_update_request_conn = NULL; + s_update_request_conn = NULL; } } static void update_handler(struct mg_connection *c, int ev, void *ev_data, void *user_data) { switch (ev) { - case MG_EV_HTTP_MULTIPART_REQUEST: - case MG_EV_HTTP_PART_BEGIN: - case MG_EV_HTTP_PART_DATA: - case MG_EV_HTTP_PART_END: - case MG_EV_HTTP_MULTIPART_REQUEST_END: { - if (mgos_sys_config_get_update_enable_post()) { - handle_update_post(c, ev, ev_data); - } else { - mg_send_response_line(c, 400, - "Content-Type: text/plain\r\n" - "Connection: close\r\n"); - mg_printf(c, "POST updates are disabled."); - c->flags |= MG_F_SEND_AND_CLOSE; - } + case MG_EV_HTTP_MULTIPART_REQUEST: + case MG_EV_HTTP_PART_BEGIN: + case MG_EV_HTTP_PART_DATA: + case MG_EV_HTTP_PART_END: + case MG_EV_HTTP_MULTIPART_REQUEST_END: { + if (mgos_sys_config_get_update_enable_post()) { + handle_update_post(c, ev, ev_data); + } else { + mg_send_response_line(c, 400, + "Content-Type: text/plain\r\n" + "Connection: close\r\n"); + mg_printf(c, "POST updates are disabled."); + c->flags |= MG_F_SEND_AND_CLOSE; + } + return; + } + + case MG_EV_HTTP_REQUEST: { + struct http_message *hm = (struct http_message *)ev_data; + if (updater_context_get_current() != NULL) { + mg_send_response_line(c, 409, + "Content-Type: text/plain\r\n" + "Connection: close\r\n"); + mg_printf(c, "Another update is in progress.\r\n"); + c->flags |= MG_F_SEND_AND_CLOSE; return; } - case MG_EV_HTTP_REQUEST: { - struct http_message *hm = (struct http_message *) ev_data; - if (updater_context_get_current() != NULL) { + const struct mgos_config_update *mcu = mgos_sys_config_get_update(); + char * url = mcu->url; + int commit_timeout = mcu->commit_timeout; + bool ignore_same_version = true; + struct mg_str params = + (mg_vcmp(&hm->method, "POST") == 0 ? hm->body : hm->query_string); + size_t buf_len = params.len; + char * buf = calloc(params.len, 1), *p = buf; + int len = mg_get_http_var(¶ms, "url", p, buf_len); + if (len > 0) { + url = p; + p += len + 1; + buf_len -= len + 1; + } + len = mg_get_http_var(¶ms, "commit_timeout", p, buf_len); + if (len > 0) { + commit_timeout = atoi(p); + } + len = mg_get_http_var(¶ms, "ignore_same_version", p, buf_len); + if (len > 0) { + ignore_same_version = (atoi(p) > 0); + } + if (url != NULL) { + s_update_request_conn = c; + struct update_context *ctx = updater_context_create(); + if (ctx == NULL) { mg_send_response_line(c, 409, "Content-Type: text/plain\r\n" "Connection: close\r\n"); - mg_printf(c, "Another update is in progress.\r\n"); + mg_printf(c, "Failed to create updater context.\r\n"); c->flags |= MG_F_SEND_AND_CLOSE; return; } - const struct mgos_config_update *mcu = mgos_sys_config_get_update(); - char *url = mcu->url; - int commit_timeout = mcu->commit_timeout; - bool ignore_same_version = true; - struct mg_str params = - (mg_vcmp(&hm->method, "POST") == 0 ? hm->body : hm->query_string); - size_t buf_len = params.len; - char *buf = calloc(params.len, 1), *p = buf; - int len = mg_get_http_var(¶ms, "url", p, buf_len); - if (len > 0) { - url = p; - p += len + 1; - buf_len -= len + 1; - } - len = mg_get_http_var(¶ms, "commit_timeout", p, buf_len); - if (len > 0) { - commit_timeout = atoi(p); - } - len = mg_get_http_var(¶ms, "ignore_same_version", p, buf_len); - if (len > 0) { - ignore_same_version = (atoi(p) > 0); - } - if (url != NULL) { - s_update_request_conn = c; - struct update_context *ctx = updater_context_create(); - if (ctx == NULL) { - mg_send_response_line(c, 409, - "Content-Type: text/plain\r\n" - "Connection: close\r\n"); - mg_printf(c, "Failed to create updater context.\r\n"); - c->flags |= MG_F_SEND_AND_CLOSE; - return; - } - ctx->ignore_same_version = ignore_same_version; - ctx->fctx.commit_timeout = commit_timeout; - ctx->result_cb = mgos_ota_result_cb; - mgos_ota_http_start(ctx, url); - - } else { - mg_send_response_line(c, 400, - "Content-Type: text/plain\r\n" - "Connection: close\r\n"); - mg_printf(c, "Update URL not specified and none is configured.\r\n"); - c->flags |= MG_F_SEND_AND_CLOSE; - } - free(buf); - break; - } - case MG_EV_CLOSE: { - if (s_update_request_conn == c) { - /* Client went away while waiting for response. */ - s_update_request_conn = NULL; - } - break; + ctx->ignore_same_version = ignore_same_version; + ctx->fctx.commit_timeout = commit_timeout; + ctx->result_cb = mgos_ota_result_cb; + mgos_ota_http_start(ctx, url); + } else { + mg_send_response_line(c, 400, + "Content-Type: text/plain\r\n" + "Connection: close\r\n"); + mg_printf(c, "Update URL not specified and none is configured.\r\n"); + c->flags |= MG_F_SEND_AND_CLOSE; } + free(buf); + break; } - (void) user_data; + + case MG_EV_CLOSE: { + if (s_update_request_conn == c) { + /* Client went away while waiting for response. */ + s_update_request_conn = NULL; + } + break; + } + } + (void)user_data; } static void update_action_handler(struct mg_connection *c, int ev, void *p, void *user_data) { - if (ev != MG_EV_HTTP_REQUEST) return; - struct http_message *hm = (struct http_message *) p; - bool is_commit = (mg_vcmp(&hm->uri, "/update/commit") == 0); + if (ev != MG_EV_HTTP_REQUEST) { + return; + } + struct http_message *hm = (struct http_message *)p; + bool is_commit = (mg_vcmp(&hm->uri, "/update/commit") == 0); bool ok = - (is_commit ? mgos_upd_commit() : mgos_upd_revert(false /* reboot */)); + (is_commit ? mgos_upd_commit() : mgos_upd_revert(false /* reboot */)); mg_send_response_line(c, (ok ? 200 : 400), "Content-Type: text/html\r\n" "Connection: close"); mg_printf(c, "\r\n%s\r\n", (ok ? "Ok" : "Error")); c->flags |= MG_F_SEND_AND_CLOSE; - if (ok && !is_commit) mgos_system_restart_after(100); - (void) user_data; + if (ok && !is_commit) { + mgos_system_restart_after(100); + } + (void)user_data; } bool mgos_ota_http_server_init(void) { diff --git a/libs/rpc-service-ota/include/mgos_rpc_service_ota.h b/libs/rpc-service-ota/include/mgos_rpc_service_ota.h index 1a56f61..dbf4530 100644 --- a/libs/rpc-service-ota/include/mgos_rpc_service_ota.h +++ b/libs/rpc-service-ota/include/mgos_rpc_service_ota.h @@ -1,7 +1,7 @@ /* -* Copyright (c) 2016 Cesanta Software Limited -* All rights reserved -*/ + * Copyright (c) 2016 Cesanta Software Limited + * All rights reserved + */ #ifndef CS_FW_SRC_MGOS_UPDATER_MG_RPC_H_ #define CS_FW_SRC_MGOS_UPDATER_MG_RPC_H_ diff --git a/libs/rpc-service-ota/src/mgos_rpc_service_ota.c b/libs/rpc-service-ota/src/mgos_rpc_service_ota.c index 14056e4..d272ec2 100644 --- a/libs/rpc-service-ota/src/mgos_rpc_service_ota.c +++ b/libs/rpc-service-ota/src/mgos_rpc_service_ota.c @@ -18,7 +18,9 @@ static struct mg_rpc_request_info *s_update_req; static void mg_rpc_updater_result(struct update_context *ctx) { - if (s_update_req == NULL) return; + if (s_update_req == NULL) { + return; + } mg_rpc_send_errorf(s_update_req, (ctx->result > 0 ? 0 : -1), ctx->status_msg); s_update_req = NULL; } @@ -26,12 +28,12 @@ static void mg_rpc_updater_result(struct update_context *ctx) { static void handle_update_req(struct mg_rpc_request_info *ri, void *cb_arg, struct mg_rpc_frame_info *fi, struct mg_str args) { - char *blob_url = NULL; - struct json_token url_tok = JSON_INVALID_TOKEN; - int commit_timeout = 0; + char * blob_url = NULL; + struct json_token url_tok = JSON_INVALID_TOKEN; + int commit_timeout = 0; struct update_context *ctx = NULL; - LOG(LL_DEBUG, ("Update request received: %.*s", (int) args.len, args.p)); + LOG(LL_DEBUG, ("Update request received: %.*s", (int)args.len, args.p)); const char *reply = "Malformed request"; @@ -41,7 +43,9 @@ static void handle_update_req(struct mg_rpc_request_info *ri, void *cb_arg, json_scanf(args.p, args.len, ri->args_fmt, &url_tok, &commit_timeout); - if (url_tok.len == 0 || url_tok.type != JSON_TYPE_STRING) goto clean; + if (url_tok.len == 0 || url_tok.type != JSON_TYPE_STRING) { + goto clean; + } LOG(LL_DEBUG, ("URL: %.*s commit_timeout: %d", url_tok.len, url_tok.ptr, commit_timeout)); @@ -65,20 +69,22 @@ static void handle_update_req(struct mg_rpc_request_info *ri, void *cb_arg, goto clean; } ctx->fctx.commit_timeout = commit_timeout; - ctx->result_cb = mg_rpc_updater_result; - s_update_req = ri; + ctx->result_cb = mg_rpc_updater_result; + s_update_req = ri; mgos_ota_http_start(ctx, blob_url); free(blob_url); return; clean: - if (blob_url != NULL) free(blob_url); + if (blob_url != NULL) { + free(blob_url); + } LOG(LL_ERROR, ("Failed to start update: %s", reply)); mg_rpc_send_errorf(ri, -1, reply); ri = NULL; - (void) cb_arg; - (void) fi; + (void)cb_arg; + (void)fi; } static void handle_commit_req(struct mg_rpc_request_info *ri, void *cb_arg, @@ -90,9 +96,9 @@ static void handle_commit_req(struct mg_rpc_request_info *ri, void *cb_arg, mg_rpc_send_errorf(ri, -1, NULL); } ri = NULL; - (void) cb_arg; - (void) fi; - (void) args; + (void)cb_arg; + (void)fi; + (void)args; } static void handle_revert_req(struct mg_rpc_request_info *ri, void *cb_arg, @@ -105,9 +111,9 @@ static void handle_revert_req(struct mg_rpc_request_info *ri, void *cb_arg, mg_rpc_send_errorf(ri, -1, NULL); } ri = NULL; - (void) cb_arg; - (void) fi; - (void) args; + (void)cb_arg; + (void)fi; + (void)args; } static void handle_create_snapshot_req(struct mg_rpc_request_info *ri, @@ -115,31 +121,32 @@ static void handle_create_snapshot_req(struct mg_rpc_request_info *ri, struct mg_rpc_frame_info *fi, struct mg_str args) { const char *err_msg = NULL; - int ret = -1; + int ret = -1; + if (mgos_upd_is_committed()) { ret = mgos_upd_create_snapshot(); if (ret >= 0) { - bool set_as_revert = false; - int commit_timeout = -1; + bool set_as_revert = false; + int commit_timeout = -1; json_scanf(args.p, args.len, ri->args_fmt, &set_as_revert, &commit_timeout); if (set_as_revert) { struct mgos_upd_boot_state bs; if (mgos_upd_boot_get_state(&bs)) { bs.is_committed = false; - bs.revert_slot = ret; + bs.revert_slot = ret; if (mgos_upd_boot_set_state(&bs)) { if (commit_timeout >= 0 && !mgos_upd_set_commit_timeout(commit_timeout)) { - ret = -4; + ret = -4; err_msg = "Failed to set commit timeout"; } } else { - ret = -3; + ret = -3; err_msg = "Failed to set boot state"; } } else { - ret = -2; + ret = -2; err_msg = "Failed to get boot state"; } } @@ -147,7 +154,7 @@ static void handle_create_snapshot_req(struct mg_rpc_request_info *ri, err_msg = "Failed to create snapshot"; } } else { - ret = -1; + ret = -1; err_msg = "Cannot create snapshots in uncommitted state"; } if (ret >= 0) { @@ -155,8 +162,8 @@ static void handle_create_snapshot_req(struct mg_rpc_request_info *ri, } else { mg_rpc_send_errorf(ri, ret, err_msg); } - (void) cb_arg; - (void) fi; + (void)cb_arg; + (void)fi; } static void handle_get_boot_state_req(struct mg_rpc_request_info *ri, @@ -164,6 +171,7 @@ static void handle_get_boot_state_req(struct mg_rpc_request_info *ri, struct mg_rpc_frame_info *fi, struct mg_str args) { struct mgos_upd_boot_state bs; + if (!mgos_upd_boot_get_state(&bs)) { mg_rpc_send_errorf(ri, -1, NULL); } else { @@ -173,9 +181,9 @@ static void handle_get_boot_state_req(struct mg_rpc_request_info *ri, bs.active_slot, bs.is_committed, bs.revert_slot, mgos_upd_get_commit_timeout()); } - (void) cb_arg; - (void) fi; - (void) args; + (void)cb_arg; + (void)fi; + (void)args; } static void handle_set_boot_state_req(struct mg_rpc_request_info *ri, @@ -184,6 +192,7 @@ static void handle_set_boot_state_req(struct mg_rpc_request_info *ri, struct mg_str args) { int ret = 0; struct mgos_upd_boot_state bs; + if (mgos_upd_boot_get_state(&bs)) { int commit_timeout = -1; if (json_scanf(args.p, args.len, ri->args_fmt, &bs.active_slot, @@ -203,13 +212,16 @@ static void handle_set_boot_state_req(struct mg_rpc_request_info *ri, } else { mg_rpc_send_errorf(ri, ret, NULL); } - (void) cb_arg; - (void) fi; + (void)cb_arg; + (void)fi; } bool mgos_rpc_service_ota_init(void) { struct mg_rpc *mg_rpc = mgos_rpc_get_global(); - if (mg_rpc == NULL) return true; + + if (mg_rpc == NULL) { + return true; + } mg_rpc_add_handler(mg_rpc, "OTA.Update", "{url: %T, commit_timeout: %d}", handle_update_req, NULL); mg_rpc_add_handler(mg_rpc, "OTA.Commit", "", handle_commit_req, NULL); diff --git a/src/channel.c b/src/channel.c index 3c16c66..9b8f71e 100644 --- a/src/channel.c +++ b/src/channel.c @@ -3,13 +3,15 @@ #include "frozen/frozen.h" static struct channel_t s_channels[CHANNEL_MAX]; -static int s_num_channels=0; +static int s_num_channels = 0; static bool valid_gpio(const int gpio) { - if (gpio == -1) + if (gpio == -1) { return true; - if (gpio < GPIO_MIN || gpio > GPIO_MAX) + } + if (gpio < GPIO_MIN || gpio > GPIO_MAX) { return false; + } return true; } @@ -18,19 +20,19 @@ int channel_get_total() { } bool channel_init(const char *fn) { - char *json; - void *h = NULL; + char * json; + void * h = NULL; struct json_token val; bool ret = false; - char *name = NULL; - int statusled = -1; - bool statusled_invert = false; + char *name = NULL; + int statusled = -1; + bool statusled_invert = false; int idx; memset(s_channels, -1, sizeof(s_channels)); - s_num_channels=0; + s_num_channels = 0; json = json_fread(fn); if (!json) { @@ -54,11 +56,12 @@ bool channel_init(const char *fn) { // Traverse Array while ((h = json_next_elem(json, strlen(json), h, ".channels", &idx, &val)) != NULL) { - int led=-1, relay=-1, button=-1; + int led = -1, relay = -1, button = -1; LOG(LL_DEBUG, ("[%d]: [%.*s]", idx, val.len, val.ptr)); - if (val.len==0 || !val.ptr) + if (val.len == 0 || !val.ptr) { continue; + } if (json_scanf(val.ptr, val.len, "{led:%d, relay:%d, button:%d}", &led, &relay, &button) != 3) { LOG(LL_ERROR, ("Incomplete Channel JSON: require 'led' and 'relay' and 'button' fields")); goto exit; @@ -76,60 +79,67 @@ bool channel_init(const char *fn) { LOG(LL_ERROR, ("Button GPIO (%d) out of bounds [%d,%d]", button, GPIO_MIN, GPIO_MAX)); goto exit; } - if (idx==CHANNEL_MAX) { + if (idx == CHANNEL_MAX) { LOG(LL_ERROR, ("Too many channels (max is %d)", CHANNEL_MAX)); goto exit; } - s_channels[idx].button_gpio=button; - s_channels[idx].relay_gpio=relay; - s_channels[idx].led_gpio=led; + s_channels[idx].button_gpio = button; + s_channels[idx].relay_gpio = relay; + s_channels[idx].led_gpio = led; } - ret=true; + ret = true; exit: if (ret) { - if (statusled!=-1) - statusled_init(statusled,statusled_invert); + if (statusled != -1) { + statusled_init(statusled, statusled_invert); + } - s_num_channels=idx+1; + s_num_channels = idx + 1; LOG(LL_INFO, ("Configuring %d channels", s_num_channels)); - for (; idx>=0; idx--) { + for (; idx >= 0; idx--) { s_channels[idx].relay_state = 0; - if (s_channels[idx].relay_gpio!=GPIO_INVALID) { + if (s_channels[idx].relay_gpio != GPIO_INVALID) { mgos_gpio_set_mode(s_channels[idx].relay_gpio, MGOS_GPIO_MODE_OUTPUT); mgos_gpio_write(s_channels[idx].relay_gpio, s_channels[idx].relay_state); } - if (s_channels[idx].led_gpio!=GPIO_INVALID) { + if (s_channels[idx].led_gpio != GPIO_INVALID) { mgos_gpio_set_mode(s_channels[idx].led_gpio, MGOS_GPIO_MODE_OUTPUT); mgos_gpio_write(s_channels[idx].led_gpio, s_channels[idx].relay_state); } - if (s_channels[idx].button_gpio!=GPIO_INVALID) { + if (s_channels[idx].button_gpio != GPIO_INVALID) { mgos_gpio_set_mode(s_channels[idx].button_gpio, MGOS_GPIO_MODE_INPUT); mgos_gpio_set_button_handler(s_channels[idx].button_gpio, MGOS_GPIO_PULL_UP, MGOS_GPIO_INT_EDGE_POS, 200, channel_handler, NULL); } } } - if (name) free(name); - if (json) free(json); + if (name) { + free(name); + } + if (json) { + free(json); + } return ret; } uint8_t channel_gpio_by_idx(int idx) { - if (idx<0 || idx>=s_num_channels) + if (idx < 0 || idx >= s_num_channels) { return GPIO_INVALID; + } return s_channels[idx].button_gpio; } uint8_t channel_idx_by_gpio(int gpio) { uint8_t i; - for(i=0; i=channel_get_total()) + if (idx < 0 || idx >= channel_get_total()) { return; + } s_channels[idx].button_last_change = now; - s_channels[idx].relay_state = state; - if (s_channels[idx].relay_gpio!=GPIO_INVALID) + s_channels[idx].relay_state = state; + if (s_channels[idx].relay_gpio != GPIO_INVALID) { mgos_gpio_write(s_channels[idx].relay_gpio, state); - if (s_channels[idx].led_gpio!=GPIO_INVALID) + } + if (s_channels[idx].led_gpio != GPIO_INVALID) { mgos_gpio_write(s_channels[idx].led_gpio, state); + } mqtt_publish_stat("channel", "{idx: %d, relay_state: %d}", idx, channel_get(idx)); } bool channel_get(int idx) { - if (idx<0 || idx>=channel_get_total()) + if (idx < 0 || idx >= channel_get_total()) { return false; + } return s_channels[idx].relay_state == 1; } void channel_handler(int gpio, void *arg) { uint8_t idx; - double now = mg_time(); - bool state; + double now = mg_time(); + bool state; idx = channel_idx_by_gpio(gpio); if (idx == GPIO_INVALID) { @@ -168,7 +182,7 @@ void channel_handler(int gpio, void *arg) { return; } - if (now= (int) strlen(MQTT_TOPIC_BROADCAST_CMD) && 0 == strncmp(MQTT_TOPIC_BROADCAST_CMD, topic, strlen(MQTT_TOPIC_BROADCAST_CMD))) + if (topic_len >= (int)strlen(MQTT_TOPIC_BROADCAST_CMD) && 0 == strncmp(MQTT_TOPIC_BROADCAST_CMD, topic, strlen(MQTT_TOPIC_BROADCAST_CMD))) { mqtt_broadcast_cmd_id(); - (void) nc; - (void) ud; + } + (void)nc; + (void)ud; } static void mqtt_ev(struct mg_connection *nc, int ev, void *ev_data, void *user_data) { switch (ev) { - case MG_EV_MQTT_CONNACK: - mqtt_broadcast_cmd_id(); + case MG_EV_MQTT_CONNACK: + mqtt_broadcast_cmd_id(); break; } - (void) nc; - (void) ev_data; - (void) user_data; + (void)nc; + (void)ev_data; + (void)user_data; } - void mqtt_publish_stat(const char *stat, const char *fmt, ...) { - char topic[80]; - char msg[200]; + char topic[80]; + char msg[200]; struct json_out out = JSON_OUT_BUF(msg, 200); - va_list ap; + va_list ap; - snprintf(topic, sizeof(topic)-1, "%s%s/stat/%s", MQTT_TOPIC_PREFIX, mgos_sys_config_get_device_id(), stat); + snprintf(topic, sizeof(topic) - 1, "%s%s/stat/%s", MQTT_TOPIC_PREFIX, mgos_sys_config_get_device_id(), stat); va_start(ap, fmt); json_vprintf(&out, fmt, ap); va_end(ap); - mgos_mqtt_pub((char*)topic, (char*)msg, strlen(msg), 0, false); + mgos_mqtt_pub((char *)topic, (char *)msg, strlen(msg), 0, false); LOG(LL_INFO, ("Sent topic='%s' msg='%s'", topic, msg)); statusled_blink(); } @@ -90,7 +92,7 @@ void mqtt_init() { mgos_mqtt_sub(MQTT_TOPIC_BROADCAST_CMD, mqtt_cb, NULL); // Subscribe to broadcast/appname - snprintf(topic, sizeof(topic)-1, "%s/%s", MQTT_TOPIC_BROADCAST_CMD, MGOS_APP); + snprintf(topic, sizeof(topic) - 1, "%s/%s", MQTT_TOPIC_BROADCAST_CMD, MGOS_APP); mgos_mqtt_sub(topic, mqtt_cb, NULL); return; diff --git a/src/rpc.c b/src/rpc.c index bd23ee6..c5a2d17 100644 --- a/src/rpc.c +++ b/src/rpc.c @@ -21,8 +21,8 @@ static bool rpc_args_to_idx_and_gpio(struct mg_rpc_request_info *ri, struct mg_s return false; } - if (idx<0 || idx>=channel_get_total()) { - mg_rpc_send_errorf(ri, 400, "idx must be between 0 and %d", channel_get_total()-1); + if (idx < 0 || idx >= channel_get_total()) { + mg_rpc_send_errorf(ri, 400, "idx must be between 0 and %d", channel_get_total() - 1); ri = NULL; return false; } @@ -35,51 +35,53 @@ static bool rpc_args_to_idx_and_gpio(struct mg_rpc_request_info *ri, struct mg_s } *return_gpio = gpio; - *return_idx = idx; + *return_idx = idx; return true; } static void rpc_channel_toggle_handler(struct mg_rpc_request_info *ri, void *cb_arg, struct mg_rpc_frame_info *fi, struct mg_str args) { uint8_t gpio; - int idx; + int idx; rpc_log(ri, args); - if (!rpc_args_to_idx_and_gpio(ri, args, &idx, &gpio)) + if (!rpc_args_to_idx_and_gpio(ri, args, &idx, &gpio)) { return; + } channel_handler(gpio, NULL); mg_rpc_send_responsef(ri, "{idx: %d, relay_state: %d}", idx, channel_get(idx)); ri = NULL; - (void) ri; - (void) cb_arg; - (void) fi; - (void) args; + (void)ri; + (void)cb_arg; + (void)fi; + (void)args; } static void rpc_channel_get_handler(struct mg_rpc_request_info *ri, void *cb_arg, struct mg_rpc_frame_info *fi, struct mg_str args) { uint8_t gpio; - int idx; + int idx; rpc_log(ri, args); - if (!rpc_args_to_idx_and_gpio(ri, args, &idx, &gpio)) + if (!rpc_args_to_idx_and_gpio(ri, args, &idx, &gpio)) { return; + } mg_rpc_send_responsef(ri, "{idx: %d, relay_state: %d}", idx, channel_get(idx)); ri = NULL; - (void) ri; - (void) cb_arg; - (void) fi; - (void) args; + (void)ri; + (void)cb_arg; + (void)fi; + (void)args; } static void rpc_channel_set_handler(struct mg_rpc_request_info *ri, void *cb_arg, struct mg_rpc_frame_info *fi, struct mg_str args) { uint8_t gpio; - int idx; - int value; + int idx; + int value; rpc_log(ri, args); @@ -89,8 +91,8 @@ static void rpc_channel_set_handler(struct mg_rpc_request_info *ri, void *cb_arg return; } - if (idx<0 || idx>=channel_get_total()) { - mg_rpc_send_errorf(ri, 400, "idx must be between 0 and %d", channel_get_total()-1); + if (idx < 0 || idx >= channel_get_total()) { + mg_rpc_send_errorf(ri, 400, "idx must be between 0 and %d", channel_get_total() - 1); ri = NULL; return; } @@ -102,18 +104,19 @@ static void rpc_channel_set_handler(struct mg_rpc_request_info *ri, void *cb_arg return; } - channel_set(idx, (bool) value); + channel_set(idx, (bool)value); mg_rpc_send_responsef(ri, "{idx: %d, relay_state: %d}", idx, channel_get(idx)); ri = NULL; - (void) ri; - (void) cb_arg; - (void) fi; - (void) args; + (void)ri; + (void)cb_arg; + (void)fi; + (void)args; } void rpc_init() { struct mg_rpc *c = mgos_rpc_get_global(); + mg_rpc_add_handler(c, "Channel.Toggle", "{idx: %d}", rpc_channel_toggle_handler, NULL); mg_rpc_add_handler(c, "Channel.Get", "{idx: %d}", rpc_channel_get_handler, NULL); mg_rpc_add_handler(c, "Channel.Set", "{idx: %d, value: %d}", rpc_channel_set_handler, NULL); diff --git a/src/statusled.c b/src/statusled.c index bea972d..e24820f 100644 --- a/src/statusled.c +++ b/src/statusled.c @@ -1,29 +1,30 @@ #include "main.h" -static mgos_timer_id statusled_timer_id = 0; -static uint8_t statusled_gpio = 0; -static bool statusled_off_state = 0; +static mgos_timer_id statusled_timer_id = 0; +static uint8_t statusled_gpio = 0; +static bool statusled_off_state = 0; static void statusled_off_cb(void *arg) { mgos_gpio_write(statusled_gpio, statusled_off_state); - statusled_timer_id=0; - (void) arg; + statusled_timer_id = 0; + (void)arg; } void statusled_blink() { - if (statusled_gpio == GPIO_INVALID) + if (statusled_gpio == GPIO_INVALID) { return; + } mgos_gpio_write(statusled_gpio, !statusled_off_state); if (statusled_timer_id) { mgos_clear_timer(statusled_timer_id); - statusled_timer_id=0; + statusled_timer_id = 0; } statusled_timer_id = mgos_set_timer(100, false, statusled_off_cb, NULL); } void statusled_init(uint8_t gpio, bool off_state) { - statusled_gpio = gpio; + statusled_gpio = gpio; statusled_off_state = off_state; mgos_gpio_set_mode(statusled_gpio, MGOS_GPIO_MODE_OUTPUT); mgos_gpio_write(statusled_gpio, statusled_off_state); diff --git a/unittest/frozen/frozen.c b/unittest/frozen/frozen.c index ed8bc8b..3020b1e 100644 --- a/unittest/frozen/frozen.c +++ b/unittest/frozen/frozen.c @@ -17,7 +17,7 @@ * license, as set out in . */ -#define _CRT_SECURE_NO_WARNINGS /* Disable deprecation warning in VS2005+ */ +#define _CRT_SECURE_NO_WARNINGS /* Disable deprecation warning in VS2005+ */ #include "frozen.h" #include @@ -28,7 +28,7 @@ #if !defined(WEAK) #if (defined(__GNUC__) || defined(__TI_COMPILER_VERSION__)) && !defined(_WIN32) -#define WEAK __attribute__((weak)) +#define WEAK __attribute__((weak)) #else #define WEAK #endif @@ -37,18 +37,19 @@ #ifdef _WIN32 #undef snprintf #undef vsnprintf -#define snprintf cs_win_snprintf -#define vsnprintf cs_win_vsnprintf +#define snprintf cs_win_snprintf +#define vsnprintf cs_win_vsnprintf int cs_win_snprintf(char *str, size_t size, const char *format, ...); int cs_win_vsnprintf(char *str, size_t size, const char *format, va_list ap); + #if _MSC_VER >= 1700 #include #else -typedef _int64 int64_t; -typedef unsigned _int64 uint64_t; +typedef _int64 int64_t; +typedef unsigned _int64 uint64_t; #endif -#define PRId64 "I64d" -#define PRIu64 "I64u" +#define PRId64 "I64d" +#define PRIu64 "I64u" #else /* _WIN32 */ /* wants this for C++ */ #ifndef __STDC_FORMAT_MACROS @@ -58,89 +59,92 @@ typedef unsigned _int64 uint64_t; #endif /* _WIN32 */ #ifndef INT64_FMT -#define INT64_FMT PRId64 +#define INT64_FMT PRId64 #endif #ifndef UINT64_FMT -#define UINT64_FMT PRIu64 +#define UINT64_FMT PRIu64 #endif #ifndef va_copy -#define va_copy(x, y) x = y +#define va_copy(x, y) x = y #endif #ifndef JSON_MAX_PATH_LEN -#define JSON_MAX_PATH_LEN 256 +#define JSON_MAX_PATH_LEN 256 #endif struct frozen { - const char *end; - const char *cur; + const char * end; + const char * cur; - const char *cur_name; - size_t cur_name_len; + const char * cur_name; + size_t cur_name_len; /* For callback API */ - char path[JSON_MAX_PATH_LEN]; - size_t path_len; - void *callback_data; + char path[JSON_MAX_PATH_LEN]; + size_t path_len; + void * callback_data; json_walk_callback_t callback; }; struct fstate { const char *ptr; - size_t path_len; + size_t path_len; }; -#define SET_STATE(fr, ptr, str, len) \ - struct fstate fstate = {(ptr), (fr)->path_len}; \ +#define SET_STATE(fr, ptr, str, len) \ + struct fstate fstate = { (ptr), (fr)->path_len }; \ append_to_path((fr), (str), (len)); #define CALL_BACK(fr, tok, value, len) \ do { \ if ((fr)->callback && \ ((fr)->path_len == 0 || (fr)->path[(fr)->path_len - 1] != '.')) { \ - struct json_token t = {(value), (len), (tok)}; \ + struct json_token t = { (value), (len), (tok) }; \ \ /* Call the callback with the given value and current name */ \ (fr)->callback((fr)->callback_data, (fr)->cur_name, (fr)->cur_name_len, \ (fr)->path, &t); \ \ /* Reset the name */ \ - (fr)->cur_name = NULL; \ + (fr)->cur_name = NULL; \ (fr)->cur_name_len = 0; \ } \ } while (0) static int append_to_path(struct frozen *f, const char *str, int size) { - int n = f->path_len; + int n = f->path_len; int left = sizeof(f->path) - n - 1; - if (size > left) size = left; + + if (size > left) { + size = left; + } memcpy(f->path + n, str, size); f->path[n + size] = '\0'; - f->path_len += size; + f->path_len += size; return n; } static void truncate_path(struct frozen *f, size_t len) { - f->path_len = len; + f->path_len = len; f->path[len] = '\0'; } static int parse_object(struct frozen *f); static int parse_value(struct frozen *f); -#define EXPECT(cond, err_code) \ - do { \ - if (!(cond)) return (err_code); \ +#define EXPECT(cond, err_code) \ + do { \ + if (!(cond)) { return (err_code); } \ } while (0) -#define TRY(expr) \ - do { \ - int _n = expr; \ - if (_n < 0) return _n; \ +#define TRY(expr) \ + do { \ + int _n = expr; \ + if (_n < 0) { return _n; } \ } while (0) -#define END_OF_STRING (-1) +#define END_OF_STRING (-1) static int left(const struct frozen *f) { return f->end - f->cur; @@ -151,16 +155,19 @@ static int is_space(int ch) { } static void skip_whitespaces(struct frozen *f) { - while (f->cur < f->end && is_space(*f->cur)) f->cur++; + while (f->cur < f->end && is_space(*f->cur)) { + f->cur++; + } } static int cur(struct frozen *f) { skip_whitespaces(f); - return f->cur >= f->end ? END_OF_STRING : *(unsigned char *) f->cur; + return f->cur >= f->end ? END_OF_STRING : *(unsigned char *)f->cur; } static int test_and_skip(struct frozen *f, int expected) { int ch = cur(f); + if (ch == expected) { f->cur++; return 0; @@ -182,23 +189,25 @@ static int is_hex_digit(int ch) { static int get_escape_len(const char *s, int len) { switch (*s) { - case 'u': - return len < 6 ? JSON_STRING_INCOMPLETE - : is_hex_digit(s[1]) && is_hex_digit(s[2]) && - is_hex_digit(s[3]) && is_hex_digit(s[4]) - ? 5 - : JSON_STRING_INVALID; - case '"': - case '\\': - case '/': - case 'b': - case 'f': - case 'n': - case 'r': - case 't': - return len < 2 ? JSON_STRING_INCOMPLETE : 1; - default: - return JSON_STRING_INVALID; + case 'u': + return len < 6 ? JSON_STRING_INCOMPLETE + : is_hex_digit(s[1]) && is_hex_digit(s[2]) && + is_hex_digit(s[3]) && is_hex_digit(s[4]) + ? 5 + : JSON_STRING_INVALID; + + case '"': + case '\\': + case '/': + case 'b': + case 'f': + case 'n': + case 'r': + case 't': + return len < 2 ? JSON_STRING_INCOMPLETE : 1; + + default: + return JSON_STRING_INVALID; } } @@ -218,26 +227,31 @@ static int parse_identifier(struct frozen *f) { } static int get_utf8_char_len(unsigned char ch) { - if ((ch & 0x80) == 0) return 1; + if ((ch & 0x80) == 0) { + return 1; + } switch (ch & 0xf0) { - case 0xf0: - return 4; - case 0xe0: - return 3; - default: - return 2; + case 0xf0: + return 4; + + case 0xe0: + return 3; + + default: + return 2; } } /* string = '"' { quoted_printable_chars } '"' */ static int parse_string(struct frozen *f) { int n, ch = 0, len = 0; + TRY(test_and_skip(f, '"')); { SET_STATE(f, f->cur, "", 0); for (; f->cur < f->end; f->cur += len) { - ch = *(unsigned char *) f->cur; - len = get_utf8_char_len((unsigned char) ch); + ch = *(unsigned char *)f->cur; + len = get_utf8_char_len((unsigned char)ch); EXPECT(ch >= 32 && len > 0, JSON_STRING_INVALID); /* No control chars */ EXPECT(len <= left(f), JSON_STRING_INCOMPLETE); if (ch == '\\') { @@ -248,7 +262,8 @@ static int parse_string(struct frozen *f) { CALL_BACK(f, JSON_TYPE_STRING, fstate.ptr, f->cur - fstate.ptr); f->cur++; break; - }; + } + ; } } return ch == '"' ? 0 : JSON_STRING_INCOMPLETE; @@ -257,24 +272,35 @@ static int parse_string(struct frozen *f) { /* number = [ '-' ] digit+ [ '.' digit+ ] [ ['e'|'E'] ['+'|'-'] digit+ ] */ static int parse_number(struct frozen *f) { int ch = cur(f); + SET_STATE(f, f->cur, "", 0); - if (ch == '-') f->cur++; + if (ch == '-') { + f->cur++; + } EXPECT(f->cur < f->end, JSON_STRING_INCOMPLETE); EXPECT(is_digit(f->cur[0]), JSON_STRING_INVALID); - while (f->cur < f->end && is_digit(f->cur[0])) f->cur++; + while (f->cur < f->end && is_digit(f->cur[0])) { + f->cur++; + } if (f->cur < f->end && f->cur[0] == '.') { f->cur++; EXPECT(f->cur < f->end, JSON_STRING_INCOMPLETE); EXPECT(is_digit(f->cur[0]), JSON_STRING_INVALID); - while (f->cur < f->end && is_digit(f->cur[0])) f->cur++; + while (f->cur < f->end && is_digit(f->cur[0])) { + f->cur++; + } } if (f->cur < f->end && (f->cur[0] == 'e' || f->cur[0] == 'E')) { f->cur++; EXPECT(f->cur < f->end, JSON_STRING_INCOMPLETE); - if ((f->cur[0] == '+' || f->cur[0] == '-')) f->cur++; + if ((f->cur[0] == '+' || f->cur[0] == '-')) { + f->cur++; + } EXPECT(f->cur < f->end, JSON_STRING_INCOMPLETE); EXPECT(is_digit(f->cur[0]), JSON_STRING_INVALID); - while (f->cur < f->end && is_digit(f->cur[0])) f->cur++; + while (f->cur < f->end && is_digit(f->cur[0])) { + f->cur++; + } } truncate_path(f, fstate.path_len); CALL_BACK(f, JSON_TYPE_NUMBER, fstate.ptr, f->cur - fstate.ptr); @@ -283,8 +309,9 @@ static int parse_number(struct frozen *f) { /* array = '[' [ value { ',' value } ] ']' */ static int parse_array(struct frozen *f) { - int i = 0, current_path_len; + int i = 0, current_path_len; char buf[20]; + CALL_BACK(f, JSON_TYPE_ARRAY_START, NULL, 0); TRY(test_and_skip(f, '[')); { @@ -294,12 +321,14 @@ static int parse_array(struct frozen *f) { snprintf(buf, sizeof(buf), "[%d]", i); i++; current_path_len = append_to_path(f, buf, strlen(buf)); - f->cur_name = - f->path + strlen(f->path) - strlen(buf) + 1 /*opening brace*/; + f->cur_name = + f->path + strlen(f->path) - strlen(buf) + 1 /*opening brace*/; f->cur_name_len = strlen(buf) - 2 /*braces*/; TRY(parse_value(f)); truncate_path(f, current_path_len); - if (cur(f) == ',') f->cur++; + if (cur(f) == ',') { + f->cur++; + } } TRY(test_and_skip(f, ']')); truncate_path(f, fstate.path_len); @@ -312,10 +341,15 @@ static int parse_array(struct frozen *f) { static int expect(struct frozen *f, const char *s, int len, enum json_token_type tok_type) { int i, n = left(f); + SET_STATE(f, f->cur, "", 0); for (i = 0; i < len; i++) { - if (i >= n) return JSON_STRING_INCOMPLETE; - if (f->cur[i] != s[i]) return JSON_STRING_INVALID; + if (i >= n) { + return JSON_STRING_INCOMPLETE; + } + if (f->cur[i] != s[i]) { + return JSON_STRING_INVALID; + } } f->cur += len; truncate_path(f, fstate.path_len); @@ -330,39 +364,46 @@ static int parse_value(struct frozen *f) { int ch = cur(f); switch (ch) { - case '"': - TRY(parse_string(f)); - break; - case '{': - TRY(parse_object(f)); - break; - case '[': - TRY(parse_array(f)); - break; - case 'n': - TRY(expect(f, "null", 4, JSON_TYPE_NULL)); - break; - case 't': - TRY(expect(f, "true", 4, JSON_TYPE_TRUE)); - break; - case 'f': - TRY(expect(f, "false", 5, JSON_TYPE_FALSE)); - break; - case '-': - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - TRY(parse_number(f)); - break; - default: - return ch == END_OF_STRING ? JSON_STRING_INCOMPLETE : JSON_STRING_INVALID; + case '"': + TRY(parse_string(f)); + break; + + case '{': + TRY(parse_object(f)); + break; + + case '[': + TRY(parse_array(f)); + break; + + case 'n': + TRY(expect(f, "null", 4, JSON_TYPE_NULL)); + break; + + case 't': + TRY(expect(f, "true", 4, JSON_TYPE_TRUE)); + break; + + case 'f': + TRY(expect(f, "false", 5, JSON_TYPE_FALSE)); + break; + + case '-': + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + TRY(parse_number(f)); + break; + + default: + return ch == END_OF_STRING ? JSON_STRING_INCOMPLETE : JSON_STRING_INVALID; } return 0; @@ -371,6 +412,7 @@ static int parse_value(struct frozen *f) { /* key = identifier | string */ static int parse_key(struct frozen *f) { int ch = cur(f); + if (is_alpha(ch)) { TRY(parse_identifier(f)); } else if (ch == '"') { @@ -383,14 +425,15 @@ static int parse_key(struct frozen *f) { /* pair = key ':' value */ static int parse_pair(struct frozen *f) { - int current_path_len; + int current_path_len; const char *tok; + skip_whitespaces(f); tok = f->cur; TRY(parse_key(f)); { - f->cur_name = *tok == '"' ? tok + 1 : tok; - f->cur_name_len = *tok == '"' ? f->cur - tok - 2 : f->cur - tok; + f->cur_name = *tok == '"' ? tok + 1 : tok; + f->cur_name_len = *tok == '"' ? f->cur - tok - 2 : f->cur - tok; current_path_len = append_to_path(f, f->cur_name, f->cur_name_len); } TRY(test_and_skip(f, ':')); @@ -407,7 +450,9 @@ static int parse_object(struct frozen *f) { SET_STATE(f, f->cur - 1, ".", 1); while (cur(f) != '}') { TRY(parse_pair(f)); - if (cur(f) == ',') f->cur++; + if (cur(f) == ',') { + f->cur++; + } } TRY(test_and_skip(f, '}')); truncate_path(f, fstate.path_len); @@ -417,19 +462,24 @@ static int parse_object(struct frozen *f) { } static int doit(struct frozen *f) { - if (f->cur == 0 || f->end < f->cur) return JSON_STRING_INVALID; - if (f->end == f->cur) return JSON_STRING_INCOMPLETE; + if (f->cur == 0 || f->end < f->cur) { + return JSON_STRING_INVALID; + } + if (f->end == f->cur) { + return JSON_STRING_INCOMPLETE; + } return parse_value(f); } int json_escape(struct json_out *out, const char *p, size_t len) WEAK; + int json_escape(struct json_out *out, const char *p, size_t len) { - size_t i, cl, n = 0; + size_t i, cl, n = 0; const char *hex_digits = "0123456789abcdef"; - const char *specials = "btnvfr"; + const char *specials = "btnvfr"; for (i = 0; i < len; i++) { - unsigned char ch = ((unsigned char *) p)[i]; + unsigned char ch = ((unsigned char *)p)[i]; if (ch == '"' || ch == '\\') { n += out->printer(out, "\\", 1); n += out->printer(out, p + i, 1); @@ -452,20 +502,25 @@ int json_escape(struct json_out *out, const char *p, size_t len) { } int json_printer_buf(struct json_out *out, const char *buf, size_t len) WEAK; + int json_printer_buf(struct json_out *out, const char *buf, size_t len) { size_t avail = out->u.buf.size - out->u.buf.len; - size_t n = len < avail ? len : avail; + size_t n = len < avail ? len : avail; + memcpy(out->u.buf.buf + out->u.buf.len, buf, n); out->u.buf.len += n; if (out->u.buf.size > 0) { size_t idx = out->u.buf.len; - if (idx >= out->u.buf.size) idx = out->u.buf.size - 1; + if (idx >= out->u.buf.size) { + idx = out->u.buf.size - 1; + } out->u.buf.buf[idx] = '\0'; } return len; } int json_printer_file(struct json_out *out, const char *buf, size_t len) WEAK; + int json_printer_file(struct json_out *out, const char *buf, size_t len) { return fwrite(buf, 1, len, out->u.fp); } @@ -499,23 +554,28 @@ static int b64rev(int c) { } static unsigned char hexdec(const char *s) { -#define HEXTOI(x) (x >= '0' && x <= '9' ? x - '0' : x - 'W') - int a = tolower(*(const unsigned char *) s); - int b = tolower(*(const unsigned char *) (s + 1)); +#define HEXTOI(x) (x >= '0' && x <= '9' ? x - '0' : x - 'W') + int a = tolower(*(const unsigned char *)s); + int b = tolower(*(const unsigned char *)(s + 1)); return (HEXTOI(a) << 4) | HEXTOI(b); } static int b64enc(struct json_out *out, const unsigned char *p, int n) { char buf[4]; - int i, len = 0; + int i, len = 0; + for (i = 0; i < n; i += 3) { int a = p[i], b = i + 1 < n ? p[i + 1] : 0, c = i + 2 < n ? p[i + 2] : 0; buf[0] = b64idx(a >> 2); buf[1] = b64idx((a & 3) << 4 | (b >> 4)); buf[2] = b64idx((b & 15) << 2 | (c >> 6)); buf[3] = b64idx(c & 63); - if (i + 1 >= n) buf[2] = '='; - if (i + 2 >= n) buf[3] = '='; + if (i + 1 >= n) { + buf[2] = '='; + } + if (i + 2 >= n) { + buf[3] = '='; + } len += out->printer(out, buf, sizeof(buf)); } return len; @@ -523,7 +583,8 @@ static int b64enc(struct json_out *out, const unsigned char *p, int n) { static int b64dec(const char *src, int n, char *dst) { const char *end = src + n; - int len = 0; + int len = 0; + while (src + 3 < end) { int a = b64rev(src[0]), b = b64rev(src[1]), c = b64rev(src[2]), d = b64rev(src[3]); @@ -540,10 +601,12 @@ static int b64dec(const char *src, int n, char *dst) { } int json_vprintf(struct json_out *out, const char *fmt, va_list xap) WEAK; + int json_vprintf(struct json_out *out, const char *fmt, va_list xap) { - int len = 0; + int len = 0; const char *quote = "\"", *null = "null"; - va_list ap; + va_list ap; + va_copy(ap, xap); while (*fmt != '\0') { @@ -551,30 +614,30 @@ int json_vprintf(struct json_out *out, const char *fmt, va_list xap) { len += out->printer(out, fmt, 1); fmt++; } else if (fmt[0] == '%') { - char buf[21]; + char buf[21]; size_t skip = 2; if (fmt[1] == 'l' && fmt[2] == 'l' && (fmt[3] == 'd' || fmt[3] == 'u')) { - int64_t val = va_arg(ap, int64_t); + int64_t val = va_arg(ap, int64_t); const char *fmt2 = fmt[3] == 'u' ? "%" UINT64_FMT : "%" INT64_FMT; snprintf(buf, sizeof(buf), fmt2, val); - len += out->printer(out, buf, strlen(buf)); + len += out->printer(out, buf, strlen(buf)); skip += 2; } else if (fmt[1] == 'z' && fmt[2] == 'u') { size_t val = va_arg(ap, size_t); - snprintf(buf, sizeof(buf), "%lu", (unsigned long) val); - len += out->printer(out, buf, strlen(buf)); + snprintf(buf, sizeof(buf), "%lu", (unsigned long)val); + len += out->printer(out, buf, strlen(buf)); skip += 1; } else if (fmt[1] == 'M') { json_printf_callback_t f = va_arg(ap, json_printf_callback_t); len += f(out, &ap); } else if (fmt[1] == 'B') { - int val = va_arg(ap, int); + int val = va_arg(ap, int); const char *str = val ? "true" : "false"; len += out->printer(out, str, strlen(str)); } else if (fmt[1] == 'H') { - const char *hex = "0123456789abcdef"; - int i, n = va_arg(ap, int); + const char * hex = "0123456789abcdef"; + int i, n = va_arg(ap, int); const unsigned char *p = va_arg(ap, const unsigned char *); len += out->printer(out, quote, 1); for (i = 0; i < n; i++) { @@ -590,11 +653,11 @@ int json_vprintf(struct json_out *out, const char *fmt, va_list xap) { len += out->printer(out, quote, 1); } else if (fmt[1] == 'Q' || (fmt[1] == '.' && fmt[2] == '*' && fmt[3] == 'Q')) { - size_t l = 0; + size_t l = 0; const char *p; if (fmt[1] == '.') { - l = (size_t) va_arg(ap, int); + l = (size_t)va_arg(ap, int); skip += 2; } p = va_arg(ap, char *); @@ -622,13 +685,13 @@ int json_vprintf(struct json_out *out, const char *fmt, va_list xap) { */ const char *end_of_format_specifier = "sdfFgGlhuIcx.*-0123456789"; - int n = strspn(fmt + 1, end_of_format_specifier); - char *pbuf = buf; - int need_len, size = sizeof(buf); - char fmt2[20]; - va_list ap_copy; + int n = strspn(fmt + 1, end_of_format_specifier); + char * pbuf = buf; + int need_len, size = sizeof(buf); + char fmt2[20]; + va_list ap_copy; strncpy(fmt2, fmt, - n + 1 > (int) sizeof(fmt2) ? sizeof(fmt2) : (size_t) n + 1); + n + 1 > (int)sizeof(fmt2) ? sizeof(fmt2) : (size_t)n + 1); fmt2[n + 1] = '\0'; va_copy(ap_copy, ap); @@ -644,17 +707,19 @@ int json_vprintf(struct json_out *out, const char *fmt, va_list xap) { while (need_len < 0) { free(pbuf); size *= 2; - if ((pbuf = (char *) malloc(size)) == NULL) break; + if ((pbuf = (char *)malloc(size)) == NULL) { + break; + } va_copy(ap_copy, ap); need_len = vsnprintf(pbuf, size, fmt2, ap_copy); va_end(ap_copy); } - } else if (need_len >= (int) sizeof(buf)) { + } else if (need_len >= (int)sizeof(buf)) { /* * resulting string doesn't fit into a stack-allocated buffer `buf`, * so we need to allocate a new buffer from heap and use it */ - if ((pbuf = (char *) malloc(need_len + 1)) != NULL) { + if ((pbuf = (char *)malloc(need_len + 1)) != NULL) { va_copy(ap_copy, ap); vsnprintf(pbuf, need_len + 1, fmt2, ap_copy); va_end(ap_copy); @@ -662,7 +727,7 @@ int json_vprintf(struct json_out *out, const char *fmt, va_list xap) { } if (pbuf == NULL) { buf[0] = '\0'; - pbuf = buf; + pbuf = buf; } /* @@ -673,27 +738,30 @@ int json_vprintf(struct json_out *out, const char *fmt, va_list xap) { */ if ((n + 1 == strlen("%" PRId64) && strcmp(fmt2, "%" PRId64) == 0) || (n + 1 == strlen("%" PRIu64) && strcmp(fmt2, "%" PRIu64) == 0)) { - (void) va_arg(ap, int64_t); + (void)va_arg(ap, int64_t); skip += strlen(PRIu64) - 1; } else if (strcmp(fmt2, "%.*s") == 0) { - (void) va_arg(ap, int); - (void) va_arg(ap, char *); + (void)va_arg(ap, int); + (void)va_arg(ap, char *); } else { switch (fmt2[n]) { - case 'u': - case 'd': - (void) va_arg(ap, int); - break; - case 'g': - case 'f': - (void) va_arg(ap, double); - break; - case 'p': - (void) va_arg(ap, void *); - break; - default: - /* many types are promoted to int */ - (void) va_arg(ap, int); + case 'u': + case 'd': + (void)va_arg(ap, int); + break; + + case 'g': + case 'f': + (void)va_arg(ap, double); + break; + + case 'p': + (void)va_arg(ap, void *); + break; + + default: + /* many types are promoted to int */ + (void)va_arg(ap, int); } } @@ -725,9 +793,11 @@ int json_vprintf(struct json_out *out, const char *fmt, va_list xap) { } int json_printf(struct json_out *out, const char *fmt, ...) WEAK; + int json_printf(struct json_out *out, const char *fmt, ...) { - int n; + int n; va_list ap; + va_start(ap, fmt); n = json_vprintf(out, fmt, ap); va_end(ap); @@ -735,21 +805,25 @@ int json_printf(struct json_out *out, const char *fmt, ...) { } int json_printf_array(struct json_out *out, va_list *ap) WEAK; + int json_printf_array(struct json_out *out, va_list *ap) { - int len = 0; - char *arr = va_arg(*ap, char *); - size_t i, arr_size = va_arg(*ap, size_t); - size_t elem_size = va_arg(*ap, size_t); - const char *fmt = va_arg(*ap, char *); + int len = 0; + char * arr = va_arg(*ap, char *); + size_t i, arr_size = va_arg(*ap, size_t); + size_t elem_size = va_arg(*ap, size_t); + const char *fmt = va_arg(*ap, char *); + len += json_printf(out, "[", 1); for (i = 0; arr != NULL && i < arr_size / elem_size; i++) { union { int64_t i; - double d; + double d; } val; memcpy(&val, arr + i * elem_size, elem_size > sizeof(val) ? sizeof(val) : elem_size); - if (i > 0) len += json_printf(out, ", "); + if (i > 0) { + len += json_printf(out, ", "); + } if (strchr(fmt, 'f') != NULL) { len += json_printf(out, fmt, val.d); } else { @@ -763,8 +837,10 @@ int json_printf_array(struct json_out *out, va_list *ap) { #ifdef _WIN32 int cs_win_vsnprintf(char *str, size_t size, const char *format, va_list ap) WEAK; + int cs_win_vsnprintf(char *str, size_t size, const char *format, va_list ap) { int res = _vsnprintf(str, size, format, ap); + va_end(ap); if (res >= size) { str[size - 1] = '\0'; @@ -773,27 +849,31 @@ int cs_win_vsnprintf(char *str, size_t size, const char *format, va_list ap) { } int cs_win_snprintf(char *str, size_t size, const char *format, ...) WEAK; + int cs_win_snprintf(char *str, size_t size, const char *format, ...) { - int res; + int res; va_list ap; + va_start(ap, format); res = vsnprintf(str, size, format, ap); va_end(ap); return res; } + #endif /* _WIN32 */ int json_walk(const char *json_string, int json_string_length, json_walk_callback_t callback, void *callback_data) WEAK; + int json_walk(const char *json_string, int json_string_length, json_walk_callback_t callback, void *callback_data) { struct frozen frozen; memset(&frozen, 0, sizeof(frozen)); - frozen.end = json_string + json_string_length; - frozen.cur = json_string; + frozen.end = json_string + json_string_length; + frozen.cur = json_string; frozen.callback_data = callback_data; - frozen.callback = callback; + frozen.callback = callback; TRY(doit(&frozen)); @@ -801,17 +881,17 @@ int json_walk(const char *json_string, int json_string_length, } struct scan_array_info { - char path[JSON_MAX_PATH_LEN]; + char path[JSON_MAX_PATH_LEN]; struct json_token *token; }; static void json_scanf_array_elem_cb(void *callback_data, const char *name, size_t name_len, const char *path, const struct json_token *token) { - struct scan_array_info *info = (struct scan_array_info *) callback_data; + struct scan_array_info *info = (struct scan_array_info *)callback_data; - (void) name; - (void) name_len; + (void)name; + (void)name_len; if (strcmp(path, info->path) == 0) { *info->token = *token; @@ -820,9 +900,11 @@ static void json_scanf_array_elem_cb(void *callback_data, const char *name, int json_scanf_array_elem(const char *s, int len, const char *path, int idx, struct json_token *token) WEAK; + int json_scanf_array_elem(const char *s, int len, const char *path, int idx, struct json_token *token) { struct scan_array_info info; + info.token = token; memset(token, 0, sizeof(*token)); snprintf(info.path, sizeof(info.path), "%s[%d]", path, idx); @@ -831,32 +913,39 @@ int json_scanf_array_elem(const char *s, int len, const char *path, int idx, } struct json_scanf_info { - int num_conversions; - char *path; + int num_conversions; + char * path; const char *fmt; - void *target; - void *user_data; - int type; + void * target; + void * user_data; + int type; }; int json_unescape(const char *src, int slen, char *dst, int dlen) WEAK; + int json_unescape(const char *src, int slen, char *dst, int dlen) { - char *send = (char *) src + slen, *dend = dst + dlen, *orig_dst = dst, *p; + char * send = (char *)src + slen, *dend = dst + dlen, *orig_dst = dst, *p; const char *esc1 = "\"\\/bfnrt", *esc2 = "\"\\/\b\f\n\r\t"; while (src < send) { if (*src == '\\') { - if (++src >= send) return JSON_STRING_INCOMPLETE; + if (++src >= send) { + return JSON_STRING_INCOMPLETE; + } if (*src == 'u') { /* TODO(lsm): \uXXXX escapes drag utf8 lib... Do it at some stage */ return JSON_STRING_INVALID; - } else if ((p = (char *) strchr(esc1, *src)) != NULL) { - if (dst < dend) *dst = esc2[p - esc1]; + } else if ((p = (char *)strchr(esc1, *src)) != NULL) { + if (dst < dend) { + *dst = esc2[p - esc1]; + } } else { return JSON_STRING_INVALID; } } else { - if (dst < dend) *dst = *src; + if (dst < dend) { + *dst = *src; + } } dst++; src++; @@ -868,11 +957,11 @@ int json_unescape(const char *src, int slen, char *dst, int dlen) { static void json_scanf_cb(void *callback_data, const char *name, size_t name_len, const char *path, const struct json_token *token) { - struct json_scanf_info *info = (struct json_scanf_info *) callback_data; + struct json_scanf_info *info = (struct json_scanf_info *)callback_data; char buf[32]; /* Must be enough to hold numbers */ - (void) name; - (void) name_len; + (void)name; + (void)name_len; if (strcmp(path, info->path) != 0) { /* It's not the path we're looking for, so, just ignore this callback */ @@ -888,125 +977,140 @@ static void json_scanf_cb(void *callback_data, const char *name, } switch (info->type) { - case 'B': - info->num_conversions++; - switch (sizeof(bool)) { - case sizeof(char): - *(char *) info->target = (token->type == JSON_TYPE_TRUE ? 1 : 0); - break; - case sizeof(int): - *(int *) info->target = (token->type == JSON_TYPE_TRUE ? 1 : 0); - break; - default: - /* should never be here */ - abort(); - } + case 'B': + info->num_conversions++; + switch (sizeof(bool)) { + case sizeof(char): + *(char *)info->target = (token->type == JSON_TYPE_TRUE ? 1 : 0); break; - case 'M': { - union { - void *p; - json_scanner_t f; - } u = {info->target}; - info->num_conversions++; - u.f(token->ptr, token->len, info->user_data); - break; - } - case 'Q': { - char **dst = (char **) info->target; - if (token->type == JSON_TYPE_NULL) { - *dst = NULL; - } else { - int unescaped_len = json_unescape(token->ptr, token->len, NULL, 0); - if (unescaped_len >= 0 && - (*dst = (char *) malloc(unescaped_len + 1)) != NULL) { - info->num_conversions++; - json_unescape(token->ptr, token->len, *dst, unescaped_len); - (*dst)[unescaped_len] = '\0'; - } - } - break; - } - case 'H': { - char **dst = (char **) info->user_data; - int i, len = token->len / 2; - *(int *) info->target = len; - if ((*dst = (char *) malloc(len + 1)) != NULL) { - for (i = 0; i < len; i++) { - (*dst)[i] = hexdec(token->ptr + 2 * i); - } - (*dst)[len] = '\0'; - info->num_conversions++; - } - break; - } - case 'V': { - char **dst = (char **) info->target; - int len = token->len * 4 / 3 + 2; - if ((*dst = (char *) malloc(len + 1)) != NULL) { - int n = b64dec(token->ptr, token->len, *dst); - (*dst)[n] = '\0'; - *(int *) info->user_data = n; - info->num_conversions++; - } - break; - } - case 'T': - info->num_conversions++; - *(struct json_token *) info->target = *token; + + case sizeof(int): + *(int *)info->target = (token->type == JSON_TYPE_TRUE ? 1 : 0); break; + default: - /* Before scanf, copy into tmp buffer in order to 0-terminate it */ - if (token->len < (int) sizeof(buf)) { - memcpy(buf, token->ptr, token->len); - buf[token->len] = '\0'; - info->num_conversions += sscanf(buf, info->fmt, info->target); + /* should never be here */ + abort(); + } + break; + + case 'M': { + union { + void * p; + json_scanner_t f; + } u = { info->target }; + info->num_conversions++; + u.f(token->ptr, token->len, info->user_data); + break; + } + + case 'Q': { + char **dst = (char **)info->target; + if (token->type == JSON_TYPE_NULL) { + *dst = NULL; + } else { + int unescaped_len = json_unescape(token->ptr, token->len, NULL, 0); + if (unescaped_len >= 0 && + (*dst = (char *)malloc(unescaped_len + 1)) != NULL) { + info->num_conversions++; + json_unescape(token->ptr, token->len, *dst, unescaped_len); + (*dst)[unescaped_len] = '\0'; } - break; + } + break; + } + + case 'H': { + char **dst = (char **)info->user_data; + int i, len = token->len / 2; + *(int *)info->target = len; + if ((*dst = (char *)malloc(len + 1)) != NULL) { + for (i = 0; i < len; i++) { + (*dst)[i] = hexdec(token->ptr + 2 * i); + } + (*dst)[len] = '\0'; + info->num_conversions++; + } + break; + } + + case 'V': { + char **dst = (char **)info->target; + int len = token->len * 4 / 3 + 2; + if ((*dst = (char *)malloc(len + 1)) != NULL) { + int n = b64dec(token->ptr, token->len, *dst); + (*dst)[n] = '\0'; + *(int *)info->user_data = n; + info->num_conversions++; + } + break; + } + + case 'T': + info->num_conversions++; + *(struct json_token *)info->target = *token; + break; + + default: + /* Before scanf, copy into tmp buffer in order to 0-terminate it */ + if (token->len < (int)sizeof(buf)) { + memcpy(buf, token->ptr, token->len); + buf[token->len] = '\0'; + info->num_conversions += sscanf(buf, info->fmt, info->target); + } + break; } } int json_vscanf(const char *s, int len, const char *fmt, va_list ap) WEAK; + int json_vscanf(const char *s, int len, const char *fmt, va_list ap) { - char path[JSON_MAX_PATH_LEN] = "", fmtbuf[20]; - int i = 0; + char path[JSON_MAX_PATH_LEN] = "", fmtbuf[20]; + int i = 0; char *p = NULL; - struct json_scanf_info info = {0, path, fmtbuf, NULL, NULL, 0}; + struct json_scanf_info info = { 0, path, fmtbuf, NULL, NULL, 0 }; while (fmt[i] != '\0') { if (fmt[i] == '{') { strcat(path, "."); i++; } else if (fmt[i] == '}') { - if ((p = strrchr(path, '.')) != NULL) *p = '\0'; + if ((p = strrchr(path, '.')) != NULL) { + *p = '\0'; + } i++; } else if (fmt[i] == '%') { info.target = va_arg(ap, void *); - info.type = fmt[i + 1]; + info.type = fmt[i + 1]; switch (fmt[i + 1]) { - case 'M': - case 'V': - case 'H': - info.user_data = va_arg(ap, void *); - /* FALLTHROUGH */ - case 'B': - case 'Q': - case 'T': - i += 2; - break; - default: { - const char *delims = ", \t\r\n]}"; - int conv_len = strcspn(fmt + i + 1, delims) + 1; - snprintf(fmtbuf, sizeof(fmtbuf), "%.*s", conv_len, fmt + i); - i += conv_len; - i += strspn(fmt + i, delims); - break; - } + case 'M': + case 'V': + case 'H': + info.user_data = va_arg(ap, void *); + + /* FALLTHROUGH */ + case 'B': + case 'Q': + case 'T': + i += 2; + break; + + default: { + const char *delims = ", \t\r\n]}"; + int conv_len = strcspn(fmt + i + 1, delims) + 1; + snprintf(fmtbuf, sizeof(fmtbuf), "%.*s", conv_len, fmt + i); + i += conv_len; + i += strspn(fmt + i, delims); + break; + } } json_walk(s, len, json_scanf_cb, &info); } else if (is_alpha(fmt[i]) || get_utf8_char_len(fmt[i]) > 1) { - const char *delims = ": \r\n\t"; - int key_len = strcspn(&fmt[i], delims); - if ((p = strrchr(path, '.')) != NULL) p[1] = '\0'; + const char *delims = ": \r\n\t"; + int key_len = strcspn(&fmt[i], delims); + if ((p = strrchr(path, '.')) != NULL) { + p[1] = '\0'; + } sprintf(path + strlen(path), "%.*s", key_len, &fmt[i]); i += key_len + strspn(fmt + i + key_len, delims); } else { @@ -1017,9 +1121,11 @@ int json_vscanf(const char *s, int len, const char *fmt, va_list ap) { } int json_scanf(const char *str, int len, const char *fmt, ...) WEAK; + int json_scanf(const char *str, int len, const char *fmt, ...) { - int result; + int result; va_list ap; + va_start(ap, fmt); result = json_vscanf(str, len, fmt, ap); va_end(ap); @@ -1027,9 +1133,11 @@ int json_scanf(const char *str, int len, const char *fmt, ...) { } int json_vfprintf(const char *file_name, const char *fmt, va_list ap) WEAK; + int json_vfprintf(const char *file_name, const char *fmt, va_list ap) { - int res = -1; - FILE *fp = fopen(file_name, "wb"); + int res = -1; + FILE *fp = fopen(file_name, "wb"); + if (fp != NULL) { struct json_out out = JSON_OUT_FILE(fp); res = json_vprintf(&out, fmt, ap); @@ -1040,9 +1148,11 @@ int json_vfprintf(const char *file_name, const char *fmt, va_list ap) { } int json_fprintf(const char *file_name, const char *fmt, ...) WEAK; + int json_fprintf(const char *file_name, const char *fmt, ...) { - int result; + int result; va_list ap; + va_start(ap, fmt); result = json_vfprintf(file_name, fmt, ap); va_end(ap); @@ -1050,15 +1160,17 @@ int json_fprintf(const char *file_name, const char *fmt, ...) { } char *json_fread(const char *path) WEAK; + char *json_fread(const char *path) { FILE *fp; char *data = NULL; + if ((fp = fopen(path, "rb")) == NULL) { } else if (fseek(fp, 0, SEEK_END) != 0) { fclose(fp); } else { size_t size = ftell(fp); - data = (char *) malloc(size + 1); + data = (char *)malloc(size + 1); if (data != NULL) { fseek(fp, 0, SEEK_SET); /* Some platforms might not have rewind(), Oo */ if (fread(data, 1, size, fp) != size) { @@ -1074,27 +1186,35 @@ char *json_fread(const char *path) { struct json_setf_data { const char *json_path; - const char *base; /* Pointer to the source JSON string */ - int matched; /* Matched part of json_path */ - int pos; /* Offset of the mutated value begin */ - int end; /* Offset of the mutated value end */ - int prev; /* Offset of the previous token end */ + const char *base; /* Pointer to the source JSON string */ + int matched; /* Matched part of json_path */ + int pos; /* Offset of the mutated value begin */ + int end; /* Offset of the mutated value end */ + int prev; /* Offset of the previous token end */ }; static int get_matched_prefix_len(const char *s1, const char *s2) { int i = 0; - while (s1[i] && s2[i] && s1[i] == s2[i]) i++; + + while (s1[i] && s2[i] && s1[i] == s2[i]) { + i++; + } return i; } static void json_vsetf_cb(void *userdata, const char *name, size_t name_len, const char *path, const struct json_token *t) { - struct json_setf_data *data = (struct json_setf_data *) userdata; + struct json_setf_data *data = (struct json_setf_data *)userdata; int off, len = get_matched_prefix_len(path, data->json_path); - if (t->ptr == NULL) return; + + if (t->ptr == NULL) { + return; + } off = t->ptr - data->base; // printf("--%d %s %d\n", t->type, path, off); - if (len > data->matched) data->matched = len; + if (len > data->matched) { + data->matched = len; + } /* * If there is no exact path match, set the mutation position to tbe end @@ -1126,19 +1246,21 @@ static void json_vsetf_cb(void *userdata, const char *name, size_t name_len, off + 1 > data->prev) { data->prev = off + 1; } - (void) name; - (void) name_len; + (void)name; + (void)name_len; } int json_vsetf(const char *s, int len, struct json_out *out, const char *json_path, const char *json_fmt, va_list ap) WEAK; + int json_vsetf(const char *s, int len, struct json_out *out, const char *json_path, const char *json_fmt, va_list ap) { struct json_setf_data data; + memset(&data, 0, sizeof(data)); data.json_path = json_path; - data.base = s; - data.end = len; + data.base = s; + data.end = len; // printf("S:[%.*s] %s %p\n", len, s, json_path, json_fmt); json_walk(s, len, json_vsetf_cb, &data); // printf("-> %d %d %d\n", data.prev, data.pos, data.end); @@ -1148,8 +1270,12 @@ int json_vsetf(const char *s, int len, struct json_out *out, /* Trim comma after the value that begins at object/array start */ if (s[data.prev - 1] == '{' || s[data.prev - 1] == '[') { int i = data.end; - while (i < len && is_space(s[i])) i++; - if (s[i] == ',') data.end = i + 1; /* Point after comma */ + while (i < len && is_space(s[i])) { + i++; + } + if (s[i] == ',') { + data.end = i + 1; /* Point after comma */ + } } json_printf(out, "%.*s", len - data.end, s + data.end); } else { @@ -1164,7 +1290,9 @@ int json_vsetf(const char *s, int len, struct json_out *out, if (s[data.prev - 1] != '{' && s[data.prev - 1] != '[' && depth == 0) { json_printf(out, ","); } - if (off > 0 && json_path[off - 1] != '.') break; + if (off > 0 && json_path[off - 1] != '.') { + break; + } json_printf(out, "%.*Q:", 1, json_path + off); off += n; if (json_path[off] != '\0') { @@ -1178,8 +1306,8 @@ int json_vsetf(const char *s, int len, struct json_out *out, /* Close brackets/braces of the added missing keys */ for (; off > data.matched; off--) { - int ch = json_path[off]; - const char *p = ch == '.' ? "}" : ch == '[' ? "]" : ""; + int ch = json_path[off]; + const char *p = ch == '.' ? "}" : ch == '[' ? "]" : ""; json_printf(out, "%s", p); } @@ -1191,10 +1319,12 @@ int json_vsetf(const char *s, int len, struct json_out *out, int json_setf(const char *s, int len, struct json_out *out, const char *json_path, const char *json_fmt, ...) WEAK; + int json_setf(const char *s, int len, struct json_out *out, const char *json_path, const char *json_fmt, ...) { - int result; + int result; va_list ap; + va_start(ap, json_fmt); result = json_vsetf(s, len, out, json_path, json_fmt, ap); va_end(ap); @@ -1203,12 +1333,14 @@ int json_setf(const char *s, int len, struct json_out *out, struct prettify_data { struct json_out *out; - int level; - int last_token; + int level; + int last_token; }; static void indent(struct json_out *out, int level) { - while (level-- > 0) out->printer(out, " ", 2); + while (level-- > 0) { + out->printer(out, " ", 2); + } } static void print_key(struct prettify_data *pd, const char *path, @@ -1218,11 +1350,13 @@ static void print_key(struct prettify_data *pd, const char *path, pd->last_token != JSON_TYPE_OBJECT_START) { pd->out->printer(pd->out, ",", 1); } - if (path[0] != '\0') pd->out->printer(pd->out, "\n", 1); + if (path[0] != '\0') { + pd->out->printer(pd->out, "\n", 1); + } indent(pd->out, pd->level); if (path[0] != '\0' && path[strlen(path) - 1] != ']') { pd->out->printer(pd->out, "\"", 1); - pd->out->printer(pd->out, name, (int) name_len); + pd->out->printer(pd->out, name, (int)name_len); pd->out->printer(pd->out, "\"", 1); pd->out->printer(pd->out, ": ", 2); } @@ -1230,53 +1364,65 @@ static void print_key(struct prettify_data *pd, const char *path, static void prettify_cb(void *userdata, const char *name, size_t name_len, const char *path, const struct json_token *t) { - struct prettify_data *pd = (struct prettify_data *) userdata; + struct prettify_data *pd = (struct prettify_data *)userdata; + switch (t->type) { - case JSON_TYPE_OBJECT_START: - case JSON_TYPE_ARRAY_START: - print_key(pd, path, name, name_len); - pd->out->printer(pd->out, t->type == JSON_TYPE_ARRAY_START ? "[" : "{", - 1); - pd->level++; - break; - case JSON_TYPE_OBJECT_END: - case JSON_TYPE_ARRAY_END: - pd->level--; - if (pd->last_token != JSON_TYPE_INVALID && - pd->last_token != JSON_TYPE_ARRAY_START && - pd->last_token != JSON_TYPE_OBJECT_START) { - pd->out->printer(pd->out, "\n", 1); - indent(pd->out, pd->level); - } - pd->out->printer(pd->out, t->type == JSON_TYPE_ARRAY_END ? "]" : "}", 1); - break; - case JSON_TYPE_NUMBER: - case JSON_TYPE_NULL: - case JSON_TYPE_TRUE: - case JSON_TYPE_FALSE: - case JSON_TYPE_STRING: - print_key(pd, path, name, name_len); - if (t->type == JSON_TYPE_STRING) pd->out->printer(pd->out, "\"", 1); - pd->out->printer(pd->out, t->ptr, t->len); - if (t->type == JSON_TYPE_STRING) pd->out->printer(pd->out, "\"", 1); - break; - default: - break; + case JSON_TYPE_OBJECT_START: + case JSON_TYPE_ARRAY_START: + print_key(pd, path, name, name_len); + pd->out->printer(pd->out, t->type == JSON_TYPE_ARRAY_START ? "[" : "{", + 1); + pd->level++; + break; + + case JSON_TYPE_OBJECT_END: + case JSON_TYPE_ARRAY_END: + pd->level--; + if (pd->last_token != JSON_TYPE_INVALID && + pd->last_token != JSON_TYPE_ARRAY_START && + pd->last_token != JSON_TYPE_OBJECT_START) { + pd->out->printer(pd->out, "\n", 1); + indent(pd->out, pd->level); + } + pd->out->printer(pd->out, t->type == JSON_TYPE_ARRAY_END ? "]" : "}", 1); + break; + + case JSON_TYPE_NUMBER: + case JSON_TYPE_NULL: + case JSON_TYPE_TRUE: + case JSON_TYPE_FALSE: + case JSON_TYPE_STRING: + print_key(pd, path, name, name_len); + if (t->type == JSON_TYPE_STRING) { + pd->out->printer(pd->out, "\"", 1); + } + pd->out->printer(pd->out, t->ptr, t->len); + if (t->type == JSON_TYPE_STRING) { + pd->out->printer(pd->out, "\"", 1); + } + break; + + default: + break; } pd->last_token = t->type; } int json_prettify(const char *s, int len, struct json_out *out) WEAK; + int json_prettify(const char *s, int len, struct json_out *out) { - struct prettify_data pd = {out, 0, JSON_TYPE_INVALID}; + struct prettify_data pd = { out, 0, JSON_TYPE_INVALID }; + return json_walk(s, len, prettify_cb, &pd); } int json_prettify_file(const char *file_name) WEAK; + int json_prettify_file(const char *file_name) { - int res = -1; - char *s = json_fread(file_name); + int res = -1; + char *s = json_fread(file_name); FILE *fp; + if (s != NULL && (fp = fopen(file_name, "wb")) != NULL) { struct json_out out = JSON_OUT_FILE(fp); res = json_prettify(s, strlen(s), &out); @@ -1296,13 +1442,13 @@ int json_prettify_file(const char *file_name) { } struct next_data { - void *handle; // Passed handle. Changed if a next entry is found - const char *path; // Path to the iterated object/array - int path_len; // Path length - optimisation - int found; // Non-0 if found the next entry - struct json_token *key; // Object's key - struct json_token *val; // Object's value - int *idx; // Array index + void * handle; // Passed handle. Changed if a next entry is found + const char * path; // Path to the iterated object/array + int path_len; // Path length - optimisation + int found; // Non-0 if found the next entry + struct json_token *key; // Object's key + struct json_token *val; // Object's value + int * idx; // Array index }; static void next_set_key(struct next_data *d, const char *name, int name_len, @@ -1313,38 +1459,55 @@ static void next_set_key(struct next_data *d, const char *name, int name_len, d->key->len = 0; d->key->ptr = NULL; } - if (d->idx != NULL) *d->idx = atoi(name); + if (d->idx != NULL) { + *d->idx = atoi(name); + } } else { /* Object. Set key and make index -1 */ if (d->key != NULL) { d->key->ptr = name; d->key->len = name_len; } - if (d->idx != NULL) *d->idx = -1; + if (d->idx != NULL) { + *d->idx = -1; + } } } static void next_cb(void *userdata, const char *name, size_t name_len, const char *path, const struct json_token *t) { - struct next_data *d = (struct next_data *) userdata; - const char *p = path + d->path_len; - if (d->found) return; - if (d->path_len >= (int) strlen(path)) return; - if (strncmp(d->path, path, d->path_len) != 0) return; - if (strchr(p + 1, '.') != NULL) return; /* More nested objects - skip */ - if (strchr(p + 1, '[') != NULL) return; /* Ditto for arrays */ + struct next_data *d = (struct next_data *)userdata; + const char * p = path + d->path_len; + + if (d->found) { + return; + } + if (d->path_len >= (int)strlen(path)) { + return; + } + if (strncmp(d->path, path, d->path_len) != 0) { + return; + } + if (strchr(p + 1, '.') != NULL) { + return; /* More nested objects - skip */ + } + if (strchr(p + 1, '[') != NULL) { + return; /* Ditto for arrays */ + } // {OBJECT,ARRAY}_END types do not pass name, _START does. Save key. if (t->type == JSON_TYPE_OBJECT_START || t->type == JSON_TYPE_ARRAY_START) { // printf("SAV %s %d %p\n", path, t->type, t->ptr); next_set_key(d, name, name_len, p[0] == '['); - } else if (d->handle == NULL || d->handle < (void *) t->ptr) { + } else if (d->handle == NULL || d->handle < (void *)t->ptr) { // printf("END %s %d %p\n", path, t->type, t->ptr); if (t->type != JSON_TYPE_OBJECT_END && t->type != JSON_TYPE_ARRAY_END) { next_set_key(d, name, name_len, p[0] == '['); } - if (d->val != NULL) *d->val = *t; - d->handle = (void *) t->ptr; - d->found = 1; + if (d->val != NULL) { + *d->val = *t; + } + d->handle = (void *)t->ptr; + d->found = 1; } } @@ -1353,13 +1516,15 @@ static void *json_next(const char *s, int len, void *handle, const char *path, struct json_token tmpval, *v = val == NULL ? &tmpval : val; struct json_token tmpkey, *k = key == NULL ? &tmpkey : key; int tmpidx, *pidx = i == NULL ? &tmpidx : i; - struct next_data data = {handle, path, strlen(path), 0, k, v, pidx}; + struct next_data data = { handle, path, strlen(path), 0, k, v, pidx }; + json_walk(s, len, next_cb, &data); return data.found ? data.handle : NULL; } void *json_next_key(const char *s, int len, void *handle, const char *path, struct json_token *key, struct json_token *val) WEAK; + void *json_next_key(const char *s, int len, void *handle, const char *path, struct json_token *key, struct json_token *val) { return json_next(s, len, handle, path, key, val, NULL); @@ -1367,6 +1532,7 @@ void *json_next_key(const char *s, int len, void *handle, const char *path, void *json_next_elem(const char *s, int len, void *handle, const char *path, int *idx, struct json_token *val) WEAK; + void *json_next_elem(const char *s, int len, void *handle, const char *path, int *idx, struct json_token *val) { return json_next(s, len, handle, path, NULL, val, idx); diff --git a/unittest/frozen/frozen.h b/unittest/frozen/frozen.h index 7cbfb04..7170492 100644 --- a/unittest/frozen/frozen.h +++ b/unittest/frozen/frozen.h @@ -56,8 +56,8 @@ enum json_token_type { * `json_scanf()` with the format specifier `%T`. */ struct json_token { - const char *ptr; /* Points to the beginning of the value */ - int len; /* Value length */ + const char * ptr; /* Points to the beginning of the value */ + int len; /* Value length */ enum json_token_type type; /* Type of the token, possible values are above */ }; @@ -65,8 +65,8 @@ struct json_token { { 0, 0, JSON_TYPE_INVALID } /* Error codes */ -#define JSON_STRING_INVALID -1 -#define JSON_STRING_INCOMPLETE -2 +#define JSON_STRING_INVALID -1 +#define JSON_STRING_INCOMPLETE -2 /* * Callback-based SAX-like API. @@ -88,7 +88,7 @@ struct json_token { * - type: JSON_TYPE_OBJECT_START, name: "2", path: ".bar[2]", value: NULL * - type: JSON_TYPE_TRUE, name: "baz", path: ".bar[2].baz", value: "true" * - type: JSON_TYPE_OBJECT_END, name: NULL, path: ".bar[2]", value: "{ \"baz\": - *true }" + * true }" * - type: JSON_TYPE_ARRAY_END, name: NULL, path: ".bar", value: "[ 1, 2, { *\"baz\": true } ]" * - type: JSON_TYPE_OBJECT_END, name: NULL, path: "", value: "{ \"foo\": 123, @@ -114,13 +114,13 @@ struct json_out { int (*printer)(struct json_out *, const char *str, size_t len); union { struct { - char *buf; + char * buf; size_t size; size_t len; - } buf; + } buf; void *data; FILE *fp; - } u; + } u; }; extern int json_printer_buf(struct json_out *, const char *, size_t); @@ -132,11 +132,11 @@ extern int json_printer_file(struct json_out *, const char *, size_t); { buf, len, 0 } \ } \ } -#define JSON_OUT_FILE(fp) \ - { \ - json_printer_file, { \ - { (char *) fp, 0, 0 } \ - } \ +#define JSON_OUT_FILE(fp) \ + { \ + json_printer_file, { \ + { (char *)fp, 0, 0 } \ + } \ } typedef int (*json_printf_callback_t)(struct json_out *, va_list *ap); diff --git a/unittest/main.c b/unittest/main.c index 3117d28..fabf31f 100644 --- a/unittest/main.c +++ b/unittest/main.c @@ -1,7 +1,7 @@ #include "test.h" -int test_failures=0; -int assert_count=0; +int test_failures = 0; +int assert_count = 0; uint32_t mqtt_pub_count; uint32_t mqtt_sub_count; diff --git a/unittest/mgos_gpio.c b/unittest/mgos_gpio.c index 0bdab03..7c61beb 100644 --- a/unittest/mgos_gpio.c +++ b/unittest/mgos_gpio.c @@ -10,22 +10,23 @@ bool mgos_gpio_set_mode(int pin, enum mgos_gpio_mode mode) { } void mgos_gpio_write(int pin, bool level) { - LOG(LL_INFO, ("Setting pin=%d to %s", pin, level?"HIGH":"LOW")); + LOG(LL_INFO, ("Setting pin=%d to %s", pin, level ? "HIGH" : "LOW")); } bool mgos_gpio_set_button_handler(int pin, enum mgos_gpio_pull_type pull_type, enum mgos_gpio_int_mode int_mode, int debounce_ms, mgos_gpio_int_handler_f cb, void *arg) { - s_handler_cb = cb; + s_handler_cb = cb; s_handler_cb_arg = arg; return true; - (void) debounce_ms; - (void) int_mode; - (void) pull_type; - (void) pin; + + (void)debounce_ms; + (void)int_mode; + (void)pull_type; + (void)pin; } void mgos_gpio_inject(int pin) { - if (s_handler_cb) + if (s_handler_cb) { s_handler_cb(pin, s_handler_cb_arg); + } } - diff --git a/unittest/mgos_gpio.h b/unittest/mgos_gpio.h index c61998f..6894be9 100644 --- a/unittest/mgos_gpio.h +++ b/unittest/mgos_gpio.h @@ -4,18 +4,18 @@ #include "mgos.h" enum mgos_gpio_mode { - MGOS_GPIO_MODE_INPUT = 0, /* input mode */ - MGOS_GPIO_MODE_OUTPUT = 1 /* output mode */ + MGOS_GPIO_MODE_INPUT = 0, /* input mode */ + MGOS_GPIO_MODE_OUTPUT = 1 /* output mode */ }; enum mgos_gpio_pull_type { MGOS_GPIO_PULL_NONE = 0, - MGOS_GPIO_PULL_UP = 1, /* pin is pilled to the high voltage */ - MGOS_GPIO_PULL_DOWN = 2 /* pin is pulled to the low voltage */ + MGOS_GPIO_PULL_UP = 1, /* pin is pilled to the high voltage */ + MGOS_GPIO_PULL_DOWN = 2 /* pin is pulled to the low voltage */ }; enum mgos_gpio_int_mode { - MGOS_GPIO_INT_NONE = 0, + MGOS_GPIO_INT_NONE = 0, MGOS_GPIO_INT_EDGE_POS = 1, /* positive edge */ MGOS_GPIO_INT_EDGE_NEG = 2, /* negative edge */ MGOS_GPIO_INT_EDGE_ANY = 3, /* any edge - positive or negative */ diff --git a/unittest/mgos_mock.c b/unittest/mgos_mock.c index f4a1a6f..4393ed3 100644 --- a/unittest/mgos_mock.c +++ b/unittest/mgos_mock.c @@ -8,36 +8,41 @@ int _mgos_timers = 0; int log_print_prefix(enum cs_log_level l, const char *func, const char *file) { char ll_str[6]; - switch(l) { - case LL_ERROR: - strncpy(ll_str, "ERROR", sizeof(ll_str)); - break; - case LL_WARN: - strncpy(ll_str, "WARN", sizeof(ll_str)); - break; - case LL_INFO: - strncpy(ll_str, "INFO", sizeof(ll_str)); - break; - case LL_DEBUG: - strncpy(ll_str, "DEBUG", sizeof(ll_str)); - break; - case LL_VERBOSE_DEBUG: - strncpy(ll_str, "VERB", sizeof(ll_str)); - break; - default: // LL_NONE - return 0; + switch (l) { + case LL_ERROR: + strncpy(ll_str, "ERROR", sizeof(ll_str)); + break; + + case LL_WARN: + strncpy(ll_str, "WARN", sizeof(ll_str)); + break; + + case LL_INFO: + strncpy(ll_str, "INFO", sizeof(ll_str)); + break; + + case LL_DEBUG: + strncpy(ll_str, "DEBUG", sizeof(ll_str)); + break; + + case LL_VERBOSE_DEBUG: + strncpy(ll_str, "VERB", sizeof(ll_str)); + break; + + default: // LL_NONE + return 0; } - printf ("%-5s %-15s %-40s| ", ll_str, file, func); + printf("%-5s %-15s %-40s| ", ll_str, file, func); return 1; } mgos_timer_id mgos_set_timer(int msecs, int flags, timer_callback cb, void *cb_arg) { _mgos_timers++; LOG(LL_INFO, ("Installing timer -- %d timers currently installed", _mgos_timers)); - (void) msecs; - (void) flags; - (void) cb; - (void) cb_arg; + (void)msecs; + (void)flags; + (void)cb; + (void)cb_arg; return _mgos_timers; } @@ -45,17 +50,17 @@ mgos_timer_id mgos_set_timer(int msecs, int flags, timer_callback cb, void *cb_a void mgos_clear_timer(mgos_timer_id id) { _mgos_timers--; LOG(LL_INFO, ("Clearing timer -- %d timers currently installed", _mgos_timers)); - (void) id; + (void)id; return; } double mg_time() { - return (float) time(NULL); + return (float)time(NULL); } double mgos_uptime() { - return (double) time(NULL); + return (double)time(NULL); } char *mgos_sys_ro_vars_get_mac_address() { diff --git a/unittest/mgos_mock.h b/unittest/mgos_mock.h index d91a8e6..8d2d1ef 100644 --- a/unittest/mgos_mock.h +++ b/unittest/mgos_mock.h @@ -8,32 +8,32 @@ #include #include "mongoose/mongoose.h" -#define MGOS_APP "unittest" +#define MGOS_APP "unittest" // mgos_log enum cs_log_level { - LL_NONE = -1, - LL_ERROR = 0, - LL_WARN = 1, - LL_INFO = 2, - LL_DEBUG = 3, + LL_NONE = -1, + LL_ERROR = 0, + LL_WARN = 1, + LL_INFO = 2, + LL_DEBUG = 3, LL_VERBOSE_DEBUG = 4, - _LL_MIN = -2, - _LL_MAX = 5, + _LL_MIN = -2, + _LL_MAX = 5, }; int log_print_prefix(enum cs_log_level l, const char *func, const char *file); -#define LOG(l, x) \ - do { \ - if (log_print_prefix(l, __func__, __FILE__)) printf x; \ - printf("\r\n"); \ +#define LOG(l, x) \ + do { \ + if (log_print_prefix(l, __func__, __FILE__)) { printf x; } \ + printf("\r\n"); \ } while (0) // mgos_timer -#define MGOS_TIMER_REPEAT 1 +#define MGOS_TIMER_REPEAT 1 typedef uintptr_t mgos_timer_id; typedef void (*timer_callback)(void *param); diff --git a/unittest/mgos_mqtt.c b/unittest/mgos_mqtt.c index 0f075f7..a060529 100644 --- a/unittest/mgos_mqtt.c +++ b/unittest/mgos_mqtt.c @@ -1,35 +1,35 @@ #include "mgos.h" #include "mgos_mqtt.h" -uint32_t mqtt_pub_count=0; -uint32_t mqtt_sub_count=0; +uint32_t mqtt_pub_count = 0; +uint32_t mqtt_sub_count = 0; static sub_handler_t s_handler; -static void *s_handler_ud; +static void * s_handler_ud; static mg_event_handler_t s_global_handler; static void *s_global_handler_ud; void mgos_mqtt_pub(char *t, char *m, int m_len, int flags, bool persist) { - LOG(LL_INFO, ("Sending topic='%s' msg='%s' persist=%s", t, m, persist?"ON":"OFF")); + LOG(LL_INFO, ("Sending topic='%s' msg='%s' persist=%s", t, m, persist ? "ON" : "OFF")); mqtt_pub_count++; } void mgos_mqtt_sub(char *t, sub_handler_t cb, void *ud) { LOG(LL_INFO, ("Subscribing to topic='%s'", t)); - s_handler = cb; + s_handler = cb; s_handler_ud = ud; } void mgos_mqtt_inject(char *topic, char *msg) { LOG(LL_INFO, ("Injecting topic='%s' msg='%s'", topic, msg)); mqtt_sub_count++; - if (s_handler) + if (s_handler) { s_handler(NULL, topic, strlen(topic), msg, strlen(msg), s_handler_ud); + } } void mgos_mqtt_add_global_handler(mqtt_event_handler_t handler, void *ud) { - s_global_handler = handler; + s_global_handler = handler; s_global_handler_ud = ud; } - diff --git a/unittest/mgos_mqtt.h b/unittest/mgos_mqtt.h index 278a33b..f34cb66 100644 --- a/unittest/mgos_mqtt.h +++ b/unittest/mgos_mqtt.h @@ -11,7 +11,7 @@ typedef void (*sub_handler_t)(struct mg_connection *nc, const char *topic, void *ud); typedef void (*mqtt_event_handler_t)(struct mg_connection *nc, int ev, - void *ev_data, void *user_data); + void *ev_data, void *user_data); void mgos_mqtt_pub(char *t, char *m, int m_len, int flags, bool persist); void mgos_mqtt_sub(char *t, sub_handler_t cb, void *ud); diff --git a/unittest/mgos_net.h b/unittest/mgos_net.h index f9ea7ac..c055a1a 100644 --- a/unittest/mgos_net.h +++ b/unittest/mgos_net.h @@ -5,8 +5,8 @@ #include #include -#define MGOS_NET_IF_WIFI_STA 0 -#define MGOS_NET_IF_WIFI_AP 1 +#define MGOS_NET_IF_WIFI_STA 0 +#define MGOS_NET_IF_WIFI_AP 1 enum mgos_net_if_type { MGOS_NET_IF_TYPE_WIFI, diff --git a/unittest/mongoose/mongoose.h b/unittest/mongoose/mongoose.h index 6b8b802..001c628 100644 --- a/unittest/mongoose/mongoose.h +++ b/unittest/mongoose/mongoose.h @@ -1,6 +1,7 @@ #ifdef MG_MODULE_LINES #line 1 "mongoose/src/common.h" #endif + /* * Copyright (c) 2004-2013 Sergey Lyubka * Copyright (c) 2013-2015 Cesanta Software Limited @@ -23,7 +24,7 @@ #ifndef CS_MONGOOSE_SRC_COMMON_H_ #define CS_MONGOOSE_SRC_COMMON_H_ -#define MG_VERSION "6.10" +#define MG_VERSION "6.10" /* Local tweaks, applied before any of Mongoose's own headers. */ #ifdef MG_LOCALS @@ -41,58 +42,58 @@ * For the "custom" platform, includes and dependencies can be * provided through mg_locals.h. */ -#define CS_P_CUSTOM 0 -#define CS_P_UNIX 1 -#define CS_P_WINDOWS 2 -#define CS_P_ESP32 15 -#define CS_P_ESP8266 3 -#define CS_P_CC3100 6 -#define CS_P_CC3200 4 -#define CS_P_CC3220 17 -#define CS_P_MSP432 5 -#define CS_P_TM4C129 14 -#define CS_P_MBED 7 -#define CS_P_WINCE 8 -#define CS_P_NXP_LPC 13 -#define CS_P_NXP_KINETIS 9 -#define CS_P_NRF51 12 -#define CS_P_NRF52 10 -#define CS_P_PIC32 11 -#define CS_P_STM32 16 +#define CS_P_CUSTOM 0 +#define CS_P_UNIX 1 +#define CS_P_WINDOWS 2 +#define CS_P_ESP32 15 +#define CS_P_ESP8266 3 +#define CS_P_CC3100 6 +#define CS_P_CC3200 4 +#define CS_P_CC3220 17 +#define CS_P_MSP432 5 +#define CS_P_TM4C129 14 +#define CS_P_MBED 7 +#define CS_P_WINCE 8 +#define CS_P_NXP_LPC 13 +#define CS_P_NXP_KINETIS 9 +#define CS_P_NRF51 12 +#define CS_P_NRF52 10 +#define CS_P_PIC32 11 +#define CS_P_STM32 16 /* Next id: 18 */ /* If not specified explicitly, we guess platform by defines. */ #ifndef CS_PLATFORM #if defined(TARGET_IS_MSP432P4XX) || defined(__MSP432P401R__) -#define CS_PLATFORM CS_P_MSP432 +#define CS_PLATFORM CS_P_MSP432 #elif defined(cc3200) || defined(TARGET_IS_CC3200) -#define CS_PLATFORM CS_P_CC3200 +#define CS_PLATFORM CS_P_CC3200 #elif defined(cc3220) || defined(TARGET_IS_CC3220) -#define CS_PLATFORM CS_P_CC3220 +#define CS_PLATFORM CS_P_CC3220 #elif defined(__unix__) || defined(__APPLE__) -#define CS_PLATFORM CS_P_UNIX +#define CS_PLATFORM CS_P_UNIX #elif defined(WINCE) -#define CS_PLATFORM CS_P_WINCE +#define CS_PLATFORM CS_P_WINCE #elif defined(_WIN32) -#define CS_PLATFORM CS_P_WINDOWS +#define CS_PLATFORM CS_P_WINDOWS #elif defined(__MBED__) -#define CS_PLATFORM CS_P_MBED +#define CS_PLATFORM CS_P_MBED #elif defined(__USE_LPCOPEN) -#define CS_PLATFORM CS_P_NXP_LPC +#define CS_PLATFORM CS_P_NXP_LPC #elif defined(FRDM_K64F) || defined(FREEDOM) -#define CS_PLATFORM CS_P_NXP_KINETIS +#define CS_PLATFORM CS_P_NXP_KINETIS #elif defined(PIC32) -#define CS_PLATFORM CS_P_PIC32 +#define CS_PLATFORM CS_P_PIC32 #elif defined(ESP_PLATFORM) -#define CS_PLATFORM CS_P_ESP32 +#define CS_PLATFORM CS_P_ESP32 #elif defined(ICACHE_FLASH) -#define CS_PLATFORM CS_P_ESP8266 +#define CS_PLATFORM CS_P_ESP8266 #elif defined(TARGET_IS_TM4C129_RA0) || defined(TARGET_IS_TM4C129_RA1) || \ - defined(TARGET_IS_TM4C129_RA2) -#define CS_PLATFORM CS_P_TM4C129 + defined(TARGET_IS_TM4C129_RA2) +#define CS_PLATFORM CS_P_TM4C129 #elif defined(STM32) -#define CS_PLATFORM CS_P_STM32 +#define CS_PLATFORM CS_P_STM32 #endif #ifndef CS_PLATFORM @@ -101,14 +102,14 @@ #endif /* !defined(CS_PLATFORM) */ -#define MG_NET_IF_SOCKET 1 -#define MG_NET_IF_SIMPLELINK 2 -#define MG_NET_IF_LWIP_LOW_LEVEL 3 -#define MG_NET_IF_PIC32 4 +#define MG_NET_IF_SOCKET 1 +#define MG_NET_IF_SIMPLELINK 2 +#define MG_NET_IF_LWIP_LOW_LEVEL 3 +#define MG_NET_IF_PIC32 4 -#define MG_SSL_IF_OPENSSL 1 -#define MG_SSL_IF_MBEDTLS 2 -#define MG_SSL_IF_SIMPLELINK 3 +#define MG_SSL_IF_OPENSSL 1 +#define MG_SSL_IF_MBEDTLS 2 +#define MG_SSL_IF_SIMPLELINK 3 /* Amalgamated: #include "common/platforms/platform_unix.h" */ /* Amalgamated: #include "common/platforms/platform_windows.h" */ @@ -130,18 +131,18 @@ #if !defined(WEAK) #if (defined(__GNUC__) || defined(__TI_COMPILER_VERSION__)) && !defined(_WIN32) -#define WEAK __attribute__((weak)) +#define WEAK __attribute__((weak)) #else #define WEAK #endif #endif #ifdef __GNUC__ -#define NORETURN __attribute__((noreturn)) -#define NOINLINE __attribute__((noinline)) -#define WARN_UNUSED_RESULT __attribute__((warn_unused_result)) -#define NOINSTR __attribute__((no_instrument_function)) -#define DO_NOT_WARN_UNUSED __attribute__((unused)) +#define NORETURN __attribute__((noreturn)) +#define NOINLINE __attribute__((noinline)) +#define WARN_UNUSED_RESULT __attribute__((warn_unused_result)) +#define NOINSTR __attribute__((no_instrument_function)) +#define DO_NOT_WARN_UNUSED __attribute__((unused)) #else #define NORETURN #define NOINLINE @@ -151,7 +152,7 @@ #endif /* __GNUC__ */ #ifndef ARRAY_SIZE -#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0])) +#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0])) #endif #endif /* CS_COMMON_PLATFORM_H_ */ @@ -180,7 +181,7 @@ #endif #ifndef _WINSOCK_DEPRECATED_NO_WARNINGS -#define _WINSOCK_DEPRECATED_NO_WARNINGS 1 +#define _WINSOCK_DEPRECATED_NO_WARNINGS 1 #endif #ifndef _CRT_SECURE_NO_WARNINGS @@ -217,122 +218,122 @@ typedef int bool; #endif #if defined(_MSC_VER) && _MSC_VER >= 1800 -#define strdup _strdup +#define strdup _strdup #endif #ifndef EINPROGRESS -#define EINPROGRESS WSAEINPROGRESS +#define EINPROGRESS WSAEINPROGRESS #endif #ifndef EWOULDBLOCK -#define EWOULDBLOCK WSAEWOULDBLOCK +#define EWOULDBLOCK WSAEWOULDBLOCK #endif #ifndef __func__ -#define STRX(x) #x -#define STR(x) STRX(x) -#define __func__ __FILE__ ":" STR(__LINE__) +#define STRX(x) #x +#define STR(x) STRX(x) +#define __func__ __FILE__ ":" STR(__LINE__) #endif -#define snprintf _snprintf -#define vsnprintf _vsnprintf -#define to64(x) _atoi64(x) +#define snprintf _snprintf +#define vsnprintf _vsnprintf +#define to64(x) _atoi64(x) #if !defined(__MINGW32__) && !defined(__MINGW64__) -#define popen(x, y) _popen((x), (y)) -#define pclose(x) _pclose(x) -#define fileno _fileno +#define popen(x, y) _popen((x), (y)) +#define pclose(x) _pclose(x) +#define fileno _fileno #endif #if defined(_MSC_VER) && _MSC_VER >= 1400 -#define fseeko(x, y, z) _fseeki64((x), (y), (z)) +#define fseeko(x, y, z) _fseeki64((x), (y), (z)) #else -#define fseeko(x, y, z) fseek((x), (y), (z)) +#define fseeko(x, y, z) fseek((x), (y), (z)) #endif #if defined(_MSC_VER) && _MSC_VER <= 1200 -typedef unsigned long uintptr_t; -typedef long intptr_t; +typedef unsigned long uintptr_t; +typedef long intptr_t; #endif -typedef int socklen_t; +typedef int socklen_t; #if _MSC_VER >= 1700 #include #else -typedef signed char int8_t; -typedef unsigned char uint8_t; -typedef int int32_t; -typedef unsigned int uint32_t; -typedef short int16_t; -typedef unsigned short uint16_t; -typedef __int64 int64_t; +typedef signed char int8_t; +typedef unsigned char uint8_t; +typedef int int32_t; +typedef unsigned int uint32_t; +typedef short int16_t; +typedef unsigned short uint16_t; +typedef __int64 int64_t; typedef unsigned __int64 uint64_t; #endif -typedef SOCKET sock_t; -typedef uint32_t in_addr_t; +typedef SOCKET sock_t; +typedef uint32_t in_addr_t; #ifndef UINT16_MAX -#define UINT16_MAX 65535 +#define UINT16_MAX 65535 #endif #ifndef UINT32_MAX -#define UINT32_MAX 4294967295 +#define UINT32_MAX 4294967295 #endif #ifndef pid_t -#define pid_t HANDLE +#define pid_t HANDLE #endif -#define INT64_FMT "I64d" -#define INT64_X_FMT "I64x" -#define SIZE_T_FMT "Iu" +#define INT64_FMT "I64d" +#define INT64_X_FMT "I64x" +#define SIZE_T_FMT "Iu" typedef struct _stati64 cs_stat_t; #ifndef S_ISDIR -#define S_ISDIR(x) (((x) &_S_IFMT) == _S_IFDIR) +#define S_ISDIR(x) (((x) & _S_IFMT) == _S_IFDIR) #endif #ifndef S_ISREG -#define S_ISREG(x) (((x) &_S_IFMT) == _S_IFREG) +#define S_ISREG(x) (((x) & _S_IFMT) == _S_IFREG) #endif -#define DIRSEP '\\' +#define DIRSEP '\\' #define CS_DEFINE_DIRENT #ifndef va_copy #ifdef __va_copy -#define va_copy __va_copy +#define va_copy __va_copy #else -#define va_copy(x, y) (x) = (y) +#define va_copy(x, y) (x) = (y) #endif #endif #ifndef MG_MAX_HTTP_REQUEST_SIZE -#define MG_MAX_HTTP_REQUEST_SIZE 8192 +#define MG_MAX_HTTP_REQUEST_SIZE 8192 #endif #ifndef MG_MAX_HTTP_SEND_MBUF -#define MG_MAX_HTTP_SEND_MBUF 4096 +#define MG_MAX_HTTP_SEND_MBUF 4096 #endif #ifndef MG_MAX_HTTP_HEADERS -#define MG_MAX_HTTP_HEADERS 40 +#define MG_MAX_HTTP_HEADERS 40 #endif #ifndef CS_ENABLE_STDIO -#define CS_ENABLE_STDIO 1 +#define CS_ENABLE_STDIO 1 #endif #ifndef MG_ENABLE_BROADCAST -#define MG_ENABLE_BROADCAST 1 +#define MG_ENABLE_BROADCAST 1 #endif #ifndef MG_ENABLE_DIRECTORY_LISTING -#define MG_ENABLE_DIRECTORY_LISTING 1 +#define MG_ENABLE_DIRECTORY_LISTING 1 #endif #ifndef MG_ENABLE_FILESYSTEM -#define MG_ENABLE_FILESYSTEM 1 +#define MG_ENABLE_FILESYSTEM 1 #endif #ifndef MG_ENABLE_HTTP_CGI -#define MG_ENABLE_HTTP_CGI MG_ENABLE_FILESYSTEM +#define MG_ENABLE_HTTP_CGI MG_ENABLE_FILESYSTEM #endif #ifndef MG_NET_IF -#define MG_NET_IF MG_NET_IF_SOCKET +#define MG_NET_IF MG_NET_IF_SOCKET #endif unsigned int sleep(unsigned int seconds); /* https://stackoverflow.com/questions/16647819/timegm-cross-platform */ -#define timegm _mkgmtime +#define timegm _mkgmtime #define gmtime_r(a, b) \ do { \ @@ -349,7 +350,7 @@ unsigned int sleep(unsigned int seconds); #if CS_PLATFORM == CS_P_UNIX #ifndef _XOPEN_SOURCE -#define _XOPEN_SOURCE 600 +#define _XOPEN_SOURCE 600 #endif /* wants this for C++ */ @@ -369,7 +370,7 @@ unsigned int sleep(unsigned int seconds); /* Enable 64-bit file offsets */ #ifndef _FILE_OFFSET_BITS -#define _FILE_OFFSET_BITS 64 +#define _FILE_OFFSET_BITS 64 #endif #include @@ -402,10 +403,10 @@ unsigned int sleep(unsigned int seconds); #ifdef __APPLE__ #include #ifndef BYTE_ORDER -#define LITTLE_ENDIAN __DARWIN_LITTLE_ENDIAN -#define BIG_ENDIAN __DARWIN_BIG_ENDIAN -#define PDP_ENDIAN __DARWIN_PDP_ENDIAN -#define BYTE_ORDER __DARWIN_BYTE_ORDER +#define LITTLE_ENDIAN __DARWIN_LITTLE_ENDIAN +#define BIG_ENDIAN __DARWIN_BIG_ENDIAN +#define PDP_ENDIAN __DARWIN_PDP_ENDIAN +#define BYTE_ORDER __DARWIN_BYTE_ORDER #endif #endif @@ -416,18 +417,19 @@ unsigned int sleep(unsigned int seconds); * implement a shim. */ #if !(defined(__cplusplus) && __cplusplus >= 201103L) && \ - !(defined(__DARWIN_C_LEVEL) && __DARWIN_C_LEVEL >= 200809L) + !(defined(__DARWIN_C_LEVEL) && __DARWIN_C_LEVEL >= 200809L) long long strtoll(const char *, char **, int); + #endif -typedef int sock_t; -#define INVALID_SOCKET (-1) -#define SIZE_T_FMT "zu" +typedef int sock_t; +#define INVALID_SOCKET (-1) +#define SIZE_T_FMT "zu" typedef struct stat cs_stat_t; -#define DIRSEP '/' -#define to64(x) strtoll(x, NULL, 10) -#define INT64_FMT PRId64 -#define INT64_X_FMT PRIx64 +#define DIRSEP '/' +#define to64(x) strtoll(x, NULL, 10) +#define INT64_FMT PRId64 +#define INT64_X_FMT PRIx64 #ifndef __cdecl #define __cdecl @@ -435,56 +437,56 @@ typedef struct stat cs_stat_t; #ifndef va_copy #ifdef __va_copy -#define va_copy __va_copy +#define va_copy __va_copy #else -#define va_copy(x, y) (x) = (y) +#define va_copy(x, y) (x) = (y) #endif #endif -#define closesocket(x) close(x) +#define closesocket(x) close(x) #ifndef MG_MAX_HTTP_REQUEST_SIZE -#define MG_MAX_HTTP_REQUEST_SIZE 8192 +#define MG_MAX_HTTP_REQUEST_SIZE 8192 #endif #ifndef MG_MAX_HTTP_SEND_MBUF -#define MG_MAX_HTTP_SEND_MBUF 4096 +#define MG_MAX_HTTP_SEND_MBUF 4096 #endif #ifndef MG_MAX_HTTP_HEADERS -#define MG_MAX_HTTP_HEADERS 40 +#define MG_MAX_HTTP_HEADERS 40 #endif #ifndef CS_ENABLE_STDIO -#define CS_ENABLE_STDIO 1 +#define CS_ENABLE_STDIO 1 #endif #ifndef MG_ENABLE_BROADCAST -#define MG_ENABLE_BROADCAST 1 +#define MG_ENABLE_BROADCAST 1 #endif #ifndef MG_ENABLE_DIRECTORY_LISTING -#define MG_ENABLE_DIRECTORY_LISTING 1 +#define MG_ENABLE_DIRECTORY_LISTING 1 #endif #ifndef MG_ENABLE_FILESYSTEM -#define MG_ENABLE_FILESYSTEM 1 +#define MG_ENABLE_FILESYSTEM 1 #endif #ifndef MG_ENABLE_HTTP_CGI -#define MG_ENABLE_HTTP_CGI MG_ENABLE_FILESYSTEM +#define MG_ENABLE_HTTP_CGI MG_ENABLE_FILESYSTEM #endif #ifndef MG_NET_IF -#define MG_NET_IF MG_NET_IF_SOCKET +#define MG_NET_IF MG_NET_IF_SOCKET #endif #ifndef MG_HOSTS_FILE_NAME -#define MG_HOSTS_FILE_NAME "/etc/hosts" +#define MG_HOSTS_FILE_NAME "/etc/hosts" #endif #ifndef MG_RESOLV_CONF_FILE_NAME -#define MG_RESOLV_CONF_FILE_NAME "/etc/resolv.conf" +#define MG_RESOLV_CONF_FILE_NAME "/etc/resolv.conf" #endif #endif /* CS_PLATFORM == CS_P_UNIX */ @@ -492,6 +494,7 @@ typedef struct stat cs_stat_t; #ifdef MG_MODULE_LINES #line 1 "common/platforms/platform_esp32.h" #endif + /* * Copyright (c) 2014-2016 Cesanta Software Limited * All rights reserved @@ -513,23 +516,23 @@ typedef struct stat cs_stat_t; #include #include -#define SIZE_T_FMT "u" +#define SIZE_T_FMT "u" typedef struct stat cs_stat_t; -#define DIRSEP '/' -#define to64(x) strtoll(x, NULL, 10) -#define INT64_FMT PRId64 -#define INT64_X_FMT PRIx64 +#define DIRSEP '/' +#define to64(x) strtoll(x, NULL, 10) +#define INT64_FMT PRId64 +#define INT64_X_FMT PRIx64 #define __cdecl -#define _FILE_OFFSET_BITS 32 +#define _FILE_OFFSET_BITS 32 -#define MG_LWIP 1 +#define MG_LWIP 1 #ifndef MG_NET_IF -#define MG_NET_IF MG_NET_IF_SOCKET +#define MG_NET_IF MG_NET_IF_SOCKET #endif #ifndef CS_ENABLE_STDIO -#define CS_ENABLE_STDIO 1 +#define CS_ENABLE_STDIO 1 #endif #endif /* CS_PLATFORM == CS_P_ESP32 */ @@ -537,6 +540,7 @@ typedef struct stat cs_stat_t; #ifdef MG_MODULE_LINES #line 1 "common/platforms/platform_esp8266.h" #endif + /* * Copyright (c) 2014-2016 Cesanta Software Limited * All rights reserved @@ -556,39 +560,39 @@ typedef struct stat cs_stat_t; #include #include -#define SIZE_T_FMT "u" +#define SIZE_T_FMT "u" typedef struct stat cs_stat_t; -#define DIRSEP '/' +#define DIRSEP '/' #if !defined(MGOS_VFS_DEFINE_DIRENT) #define CS_DEFINE_DIRENT #endif -#define to64(x) strtoll(x, NULL, 10) -#define INT64_FMT PRId64 -#define INT64_X_FMT PRIx64 +#define to64(x) strtoll(x, NULL, 10) +#define INT64_FMT PRId64 +#define INT64_X_FMT PRIx64 #define __cdecl -#define _FILE_OFFSET_BITS 32 +#define _FILE_OFFSET_BITS 32 #if !defined(RTOS_SDK) && !defined(__cplusplus) -#define fileno(x) -1 +#define fileno(x) - 1 #endif -#define MG_LWIP 1 +#define MG_LWIP 1 /* struct timeval is defined in sys/time.h. */ -#define LWIP_TIMEVAL_PRIVATE 0 +#define LWIP_TIMEVAL_PRIVATE 0 #ifndef MG_NET_IF #include #if LWIP_SOCKET /* RTOS SDK has LWIP sockets */ -#define MG_NET_IF MG_NET_IF_SOCKET +#define MG_NET_IF MG_NET_IF_SOCKET #else -#define MG_NET_IF MG_NET_IF_LWIP_LOW_LEVEL +#define MG_NET_IF MG_NET_IF_LWIP_LOW_LEVEL #endif #endif #ifndef CS_ENABLE_STDIO -#define CS_ENABLE_STDIO 1 +#define CS_ENABLE_STDIO 1 #endif #endif /* CS_PLATFORM == CS_P_ESP8266 */ @@ -596,6 +600,7 @@ typedef struct stat cs_stat_t; #ifdef MG_MODULE_LINES #line 1 "common/platforms/platform_cc3100.h" #endif + /* * Copyright (c) 2014-2016 Cesanta Software Limited * All rights reserved @@ -613,8 +618,8 @@ typedef struct stat cs_stat_t; #include #include -#define MG_NET_IF MG_NET_IF_SIMPLELINK -#define MG_SSL_IF MG_SSL_IF_SIMPLELINK +#define MG_NET_IF MG_NET_IF_SIMPLELINK +#define MG_SSL_IF MG_SSL_IF_SIMPLELINK /* * CC3100 SDK and STM32 SDK include headers w/out path, just like @@ -627,14 +632,14 @@ typedef struct stat cs_stat_t; #undef timeval typedef int sock_t; -#define INVALID_SOCKET (-1) +#define INVALID_SOCKET (-1) -#define to64(x) strtoll(x, NULL, 10) -#define INT64_FMT PRId64 -#define INT64_X_FMT PRIx64 -#define SIZE_T_FMT "u" +#define to64(x) strtoll(x, NULL, 10) +#define INT64_FMT PRId64 +#define INT64_X_FMT PRIx64 +#define SIZE_T_FMT "u" -#define SOMAXCONN 8 +#define SOMAXCONN 8 const char *inet_ntop(int af, const void *src, char *dst, socklen_t size); char *inet_ntoa(struct in_addr in); @@ -645,6 +650,7 @@ int inet_pton(int af, const char *src, void *dst); #ifdef MG_MODULE_LINES #line 1 "common/platforms/platform_cc3200.h" #endif + /* * Copyright (c) 2014-2016 Cesanta Software Limited * All rights reserved @@ -668,27 +674,27 @@ int inet_pton(int af, const char *src, void *dst); #include #endif -#define MG_NET_IF MG_NET_IF_SIMPLELINK -#define MG_SSL_IF MG_SSL_IF_SIMPLELINK +#define MG_NET_IF MG_NET_IF_SIMPLELINK +#define MG_SSL_IF MG_SSL_IF_SIMPLELINK /* Only SPIFFS supports directories, SLFS does not. */ #if defined(CC3200_FS_SPIFFS) && !defined(MG_ENABLE_DIRECTORY_LISTING) -#define MG_ENABLE_DIRECTORY_LISTING 1 +#define MG_ENABLE_DIRECTORY_LISTING 1 #endif /* Amalgamated: #include "common/platforms/simplelink/cs_simplelink.h" */ -typedef int sock_t; -#define INVALID_SOCKET (-1) -#define SIZE_T_FMT "u" +typedef int sock_t; +#define INVALID_SOCKET (-1) +#define SIZE_T_FMT "u" typedef struct stat cs_stat_t; -#define DIRSEP '/' -#define to64(x) strtoll(x, NULL, 10) -#define INT64_FMT PRId64 -#define INT64_X_FMT PRIx64 +#define DIRSEP '/' +#define to64(x) strtoll(x, NULL, 10) +#define INT64_FMT PRId64 +#define INT64_X_FMT PRIx64 #define __cdecl -#define fileno(x) -1 +#define fileno(x) - 1 /* Some functions we implement for Mongoose. */ @@ -698,7 +704,7 @@ extern "C" { #ifdef __TI_COMPILER_VERSION__ struct SlTimeval_t; -#define timeval SlTimeval_t +#define timeval SlTimeval_t int gettimeofday(struct timeval *t, void *tz); int settimeofday(const struct timeval *tv, const void *tz); @@ -712,37 +718,37 @@ int asprintf(char **strp, const char *fmt, ...); #include typedef unsigned int mode_t; -typedef size_t _off_t; -typedef long ssize_t; +typedef size_t _off_t; +typedef long ssize_t; struct stat { - int st_ino; + int st_ino; mode_t st_mode; - int st_nlink; + int st_nlink; time_t st_mtime; - off_t st_size; + off_t st_size; }; int _stat(const char *pathname, struct stat *st); int stat(const char *pathname, struct stat *st); -#define __S_IFMT 0170000 +#define __S_IFMT 0170000 -#define __S_IFDIR 0040000 -#define __S_IFCHR 0020000 -#define __S_IFREG 0100000 +#define __S_IFDIR 0040000 +#define __S_IFCHR 0020000 +#define __S_IFREG 0100000 -#define __S_ISTYPE(mode, mask) (((mode) &__S_IFMT) == (mask)) +#define __S_ISTYPE(mode, mask) (((mode) & __S_IFMT) == (mask)) -#define S_IFDIR __S_IFDIR -#define S_IFCHR __S_IFCHR -#define S_IFREG __S_IFREG -#define S_ISDIR(mode) __S_ISTYPE((mode), __S_IFDIR) -#define S_ISREG(mode) __S_ISTYPE((mode), __S_IFREG) +#define S_IFDIR __S_IFDIR +#define S_IFCHR __S_IFCHR +#define S_IFREG __S_IFREG +#define S_ISDIR(mode) __S_ISTYPE((mode), __S_IFDIR) +#define S_ISREG(mode) __S_ISTYPE((mode), __S_IFREG) /* 5.x series compilers don't have va_copy, 16.x do. */ #if __TI_COMPILER_VERSION__ < 16000000 -#define va_copy(apc, ap) ((apc) = (ap)) +#define va_copy(apc, ap) ((apc) = (ap)) #endif #endif /* __TI_COMPILER_VERSION__ */ @@ -752,13 +758,13 @@ int stat(const char *pathname, struct stat *st); #endif #if (defined(CC3200_FS_SPIFFS) || defined(CC3200_FS_SLFS)) && \ - !defined(MG_ENABLE_FILESYSTEM) -#define MG_ENABLE_FILESYSTEM 1 + !defined(MG_ENABLE_FILESYSTEM) +#define MG_ENABLE_FILESYSTEM 1 #define CS_DEFINE_DIRENT #endif #ifndef CS_ENABLE_STDIO -#define CS_ENABLE_STDIO 1 +#define CS_ENABLE_STDIO 1 #endif #ifdef __cplusplus @@ -770,6 +776,7 @@ int stat(const char *pathname, struct stat *st); #ifdef MG_MODULE_LINES #line 1 "common/platforms/platform_msp432.h" #endif + /* * Copyright (c) 2014-2016 Cesanta Software Limited * All rights reserved @@ -792,22 +799,22 @@ int stat(const char *pathname, struct stat *st); #include #endif -#define MG_NET_IF MG_NET_IF_SIMPLELINK -#define MG_SSL_IF MG_SSL_IF_SIMPLELINK +#define MG_NET_IF MG_NET_IF_SIMPLELINK +#define MG_SSL_IF MG_SSL_IF_SIMPLELINK /* Amalgamated: #include "common/platforms/simplelink/cs_simplelink.h" */ -typedef int sock_t; -#define INVALID_SOCKET (-1) -#define SIZE_T_FMT "u" +typedef int sock_t; +#define INVALID_SOCKET (-1) +#define SIZE_T_FMT "u" typedef struct stat cs_stat_t; -#define DIRSEP '/' -#define to64(x) strtoll(x, NULL, 10) -#define INT64_FMT PRId64 -#define INT64_X_FMT PRIx64 +#define DIRSEP '/' +#define to64(x) strtoll(x, NULL, 10) +#define INT64_FMT PRId64 +#define INT64_X_FMT PRIx64 #define __cdecl -#define fileno(x) -1 +#define fileno(x) - 1 /* Some functions we implement for Mongoose. */ @@ -817,8 +824,9 @@ extern "C" { #ifdef __TI_COMPILER_VERSION__ struct SlTimeval_t; -#define timeval SlTimeval_t +#define timeval SlTimeval_t int gettimeofday(struct timeval *t, void *tz); + #endif /* TI's libc does not have stat & friends, add them. */ @@ -827,46 +835,47 @@ int gettimeofday(struct timeval *t, void *tz); #include typedef unsigned int mode_t; -typedef size_t _off_t; -typedef long ssize_t; +typedef size_t _off_t; +typedef long ssize_t; struct stat { - int st_ino; + int st_ino; mode_t st_mode; - int st_nlink; + int st_nlink; time_t st_mtime; - off_t st_size; + off_t st_size; }; int _stat(const char *pathname, struct stat *st); -#define stat(a, b) _stat(a, b) -#define __S_IFMT 0170000 +#define stat(a, b) _stat(a, b) -#define __S_IFDIR 0040000 -#define __S_IFCHR 0020000 -#define __S_IFREG 0100000 +#define __S_IFMT 0170000 -#define __S_ISTYPE(mode, mask) (((mode) &__S_IFMT) == (mask)) +#define __S_IFDIR 0040000 +#define __S_IFCHR 0020000 +#define __S_IFREG 0100000 -#define S_IFDIR __S_IFDIR -#define S_IFCHR __S_IFCHR -#define S_IFREG __S_IFREG -#define S_ISDIR(mode) __S_ISTYPE((mode), __S_IFDIR) -#define S_ISREG(mode) __S_ISTYPE((mode), __S_IFREG) +#define __S_ISTYPE(mode, mask) (((mode) & __S_IFMT) == (mask)) + +#define S_IFDIR __S_IFDIR +#define S_IFCHR __S_IFCHR +#define S_IFREG __S_IFREG +#define S_ISDIR(mode) __S_ISTYPE((mode), __S_IFDIR) +#define S_ISREG(mode) __S_ISTYPE((mode), __S_IFREG) /* As of 5.2.7, TI compiler does not support va_copy() yet. */ -#define va_copy(apc, ap) ((apc) = (ap)) +#define va_copy(apc, ap) ((apc) = (ap)) #endif /* __TI_COMPILER_VERSION__ */ #ifndef CS_ENABLE_STDIO -#define CS_ENABLE_STDIO 1 +#define CS_ENABLE_STDIO 1 #endif #if (defined(CC3200_FS_SPIFFS) || defined(CC3200_FS_SLFS)) && \ - !defined(MG_ENABLE_FILESYSTEM) -#define MG_ENABLE_FILESYSTEM 1 + !defined(MG_ENABLE_FILESYSTEM) +#define MG_ENABLE_FILESYSTEM 1 #endif #ifdef __cplusplus @@ -878,6 +887,7 @@ int _stat(const char *pathname, struct stat *st); #ifdef MG_MODULE_LINES #line 1 "common/platforms/platform_tm4c129.h" #endif + /* * Copyright (c) 2014-2016 Cesanta Software Limited * All rights reserved @@ -900,33 +910,33 @@ int _stat(const char *pathname, struct stat *st); #include #endif -#define SIZE_T_FMT "u" +#define SIZE_T_FMT "u" typedef struct stat cs_stat_t; -#define DIRSEP '/' -#define to64(x) strtoll(x, NULL, 10) -#define INT64_FMT PRId64 -#define INT64_X_FMT PRIx64 +#define DIRSEP '/' +#define to64(x) strtoll(x, NULL, 10) +#define INT64_FMT PRId64 +#define INT64_X_FMT PRIx64 #define __cdecl #ifndef MG_NET_IF #include #if LWIP_SOCKET -#define MG_NET_IF MG_NET_IF_SOCKET +#define MG_NET_IF MG_NET_IF_SOCKET #else -#define MG_NET_IF MG_NET_IF_LWIP_LOW_LEVEL +#define MG_NET_IF MG_NET_IF_LWIP_LOW_LEVEL #endif -#define MG_LWIP 1 +#define MG_LWIP 1 #elif MG_NET_IF == MG_NET_IF_SIMPLELINK /* Amalgamated: #include "common/platforms/simplelink/cs_simplelink.h" */ #endif #ifndef CS_ENABLE_STDIO -#define CS_ENABLE_STDIO 1 +#define CS_ENABLE_STDIO 1 #endif #ifdef __TI_COMPILER_VERSION__ /* As of 5.2.8, TI compiler does not support va_copy() yet. */ -#define va_copy(apc, ap) ((apc) = (ap)) +#define va_copy(apc, ap) ((apc) = (ap)) #endif /* __TI_COMPILER_VERSION__ */ #ifdef __cplusplus @@ -938,6 +948,7 @@ typedef struct stat cs_stat_t; #ifdef MG_MODULE_LINES #line 1 "common/platforms/platform_mbed.h" #endif + /* * Copyright (c) 2014-2016 Cesanta Software Limited * All rights reserved @@ -968,10 +979,10 @@ typedef struct stat cs_stat_t; #include typedef struct stat cs_stat_t; -#define DIRSEP '/' +#define DIRSEP '/' #ifndef CS_ENABLE_STDIO -#define CS_ENABLE_STDIO 1 +#define CS_ENABLE_STDIO 1 #endif /* @@ -982,32 +993,32 @@ typedef struct stat cs_stat_t; */ #if defined(__ARMCC_VERSION) || defined(__ICCARM__) #define _TIMEVAL_DEFINED -#define gettimeofday _gettimeofday +#define gettimeofday _gettimeofday /* copied from GCC on ARM; for some reason useconds are signed */ typedef long suseconds_t; /* microseconds (signed) */ struct timeval { - time_t tv_sec; /* seconds */ - suseconds_t tv_usec; /* and microseconds */ + time_t tv_sec; /* seconds */ + suseconds_t tv_usec; /* and microseconds */ }; #endif #if MG_NET_IF == MG_NET_IF_SIMPLELINK -#define MG_SIMPLELINK_NO_OSI 1 +#define MG_SIMPLELINK_NO_OSI 1 #include typedef int sock_t; -#define INVALID_SOCKET (-1) +#define INVALID_SOCKET (-1) -#define to64(x) strtoll(x, NULL, 10) -#define INT64_FMT PRId64 -#define INT64_X_FMT PRIx64 -#define SIZE_T_FMT "u" +#define to64(x) strtoll(x, NULL, 10) +#define INT64_FMT PRId64 +#define INT64_X_FMT PRIx64 +#define SIZE_T_FMT "u" -#define SOMAXCONN 8 +#define SOMAXCONN 8 const char *inet_ntop(int af, const void *src, char *dst, socklen_t size); char *inet_ntoa(struct in_addr in); @@ -1022,6 +1033,7 @@ in_addr_t inet_addr(const char *cp); #ifdef MG_MODULE_LINES #line 1 "common/platforms/platform_nrf51.h" #endif + /* * Copyright (c) 2014-2016 Cesanta Software Limited * All rights reserved @@ -1037,36 +1049,38 @@ in_addr_t inet_addr(const char *cp); #include #include -#define to64(x) strtoll(x, NULL, 10) +#define to64(x) strtoll(x, NULL, 10) -#define MG_NET_IF MG_NET_IF_LWIP_LOW_LEVEL -#define MG_LWIP 1 -#define MG_ENABLE_IPV6 1 +#define MG_NET_IF MG_NET_IF_LWIP_LOW_LEVEL +#define MG_LWIP 1 +#define MG_ENABLE_IPV6 1 /* * For ARM C Compiler, make lwip to export `struct timeval`; for other * compilers, suppress it. */ #if !defined(__ARMCC_VERSION) -#define LWIP_TIMEVAL_PRIVATE 0 +#define LWIP_TIMEVAL_PRIVATE 0 #else struct timeval; int gettimeofday(struct timeval *tp, void *tzp); + #endif -#define INT64_FMT PRId64 -#define SIZE_T_FMT "u" +#define INT64_FMT PRId64 +#define SIZE_T_FMT "u" /* * ARM C Compiler doesn't have strdup, so we provide it */ -#define CS_ENABLE_STRDUP defined(__ARMCC_VERSION) +#define CS_ENABLE_STRDUP defined(__ARMCC_VERSION) #endif /* CS_PLATFORM == CS_P_NRF51 */ #endif /* CS_COMMON_PLATFORMS_PLATFORM_NRF51_H_ */ #ifdef MG_MODULE_LINES #line 1 "common/platforms/platform_nrf52.h" #endif + /* * Copyright (c) 2014-2016 Cesanta Software Limited * All rights reserved @@ -1083,14 +1097,14 @@ int gettimeofday(struct timeval *tp, void *tzp); #include #include -#define to64(x) strtoll(x, NULL, 10) +#define to64(x) strtoll(x, NULL, 10) -#define MG_NET_IF MG_NET_IF_LWIP_LOW_LEVEL -#define MG_LWIP 1 -#define MG_ENABLE_IPV6 1 +#define MG_NET_IF MG_NET_IF_LWIP_LOW_LEVEL +#define MG_LWIP 1 +#define MG_ENABLE_IPV6 1 #if !defined(ENOSPC) -#define ENOSPC 28 /* No space left on device */ +#define ENOSPC 28 /* No space left on device */ #endif /* @@ -1098,22 +1112,23 @@ int gettimeofday(struct timeval *tp, void *tzp); * compilers, suppress it. */ #if !defined(__ARMCC_VERSION) -#define LWIP_TIMEVAL_PRIVATE 0 +#define LWIP_TIMEVAL_PRIVATE 0 #endif -#define INT64_FMT PRId64 -#define SIZE_T_FMT "u" +#define INT64_FMT PRId64 +#define SIZE_T_FMT "u" /* * ARM C Compiler doesn't have strdup, so we provide it */ -#define CS_ENABLE_STRDUP defined(__ARMCC_VERSION) +#define CS_ENABLE_STRDUP defined(__ARMCC_VERSION) #endif /* CS_PLATFORM == CS_P_NRF52 */ #endif /* CS_COMMON_PLATFORMS_PLATFORM_NRF52_H_ */ #ifdef MG_MODULE_LINES #line 1 "common/platforms/simplelink/cs_simplelink.h" #endif + /* * Copyright (c) 2014-2016 Cesanta Software Limited * All rights reserved @@ -1145,6 +1160,7 @@ int gettimeofday(struct timeval *tp, void *tzp); #include #include #else + /* We want to disable SL_INC_STD_BSD_API_NAMING, so we include user.h ourselves * and undef it. */ #define PROVISIONING_API_H_ @@ -1158,44 +1174,44 @@ int gettimeofday(struct timeval *tp, void *tzp); /* Now define only the subset of the BSD API that we use. * Notably, close(), read() and write() are not defined. */ -#define AF_INET SL_AF_INET +#define AF_INET SL_AF_INET -#define socklen_t SlSocklen_t -#define sockaddr SlSockAddr_t -#define sockaddr_in SlSockAddrIn_t -#define in_addr SlInAddr_t +#define socklen_t SlSocklen_t +#define sockaddr SlSockAddr_t +#define sockaddr_in SlSockAddrIn_t +#define in_addr SlInAddr_t -#define SOCK_STREAM SL_SOCK_STREAM -#define SOCK_DGRAM SL_SOCK_DGRAM +#define SOCK_STREAM SL_SOCK_STREAM +#define SOCK_DGRAM SL_SOCK_DGRAM -#define htonl sl_Htonl -#define ntohl sl_Ntohl -#define htons sl_Htons -#define ntohs sl_Ntohs +#define htonl sl_Htonl +#define ntohl sl_Ntohl +#define htons sl_Htons +#define ntohs sl_Ntohs #ifndef EACCES -#define EACCES SL_EACCES +#define EACCES SL_EACCES #endif #ifndef EAFNOSUPPORT -#define EAFNOSUPPORT SL_EAFNOSUPPORT +#define EAFNOSUPPORT SL_EAFNOSUPPORT #endif #ifndef EAGAIN -#define EAGAIN SL_EAGAIN +#define EAGAIN SL_EAGAIN #endif #ifndef EBADF -#define EBADF SL_EBADF +#define EBADF SL_EBADF #endif #ifndef EINVAL -#define EINVAL SL_EINVAL +#define EINVAL SL_EINVAL #endif #ifndef ENOMEM -#define ENOMEM SL_ENOMEM +#define ENOMEM SL_ENOMEM #endif #ifndef EWOULDBLOCK -#define EWOULDBLOCK SL_EWOULDBLOCK +#define EWOULDBLOCK SL_EWOULDBLOCK #endif -#define SOMAXCONN 8 +#define SOMAXCONN 8 #ifdef __cplusplus extern "C" { @@ -1228,48 +1244,48 @@ int sl_set_ssl_opts(int sock, struct mg_connection *nc); /* Compatibility with older versions of SimpleLink */ #if SL_MAJOR_VERSION_NUM < 2 -#define SL_ERROR_BSD_EAGAIN SL_EAGAIN -#define SL_ERROR_BSD_EALREADY SL_EALREADY -#define SL_ERROR_BSD_ENOPROTOOPT SL_ENOPROTOOPT -#define SL_ERROR_BSD_ESECDATEERROR SL_ESECDATEERROR -#define SL_ERROR_BSD_ESECSNOVERIFY SL_ESECSNOVERIFY -#define SL_ERROR_FS_FAILED_TO_ALLOCATE_MEM SL_FS_ERR_FAILED_TO_ALLOCATE_MEM +#define SL_ERROR_BSD_EAGAIN SL_EAGAIN +#define SL_ERROR_BSD_EALREADY SL_EALREADY +#define SL_ERROR_BSD_ENOPROTOOPT SL_ENOPROTOOPT +#define SL_ERROR_BSD_ESECDATEERROR SL_ESECDATEERROR +#define SL_ERROR_BSD_ESECSNOVERIFY SL_ESECSNOVERIFY +#define SL_ERROR_FS_FAILED_TO_ALLOCATE_MEM SL_FS_ERR_FAILED_TO_ALLOCATE_MEM #define SL_ERROR_FS_FILE_HAS_NOT_BEEN_CLOSE_CORRECTLY \ SL_FS_FILE_HAS_NOT_BEEN_CLOSE_CORRECTLY -#define SL_ERROR_FS_FILE_NAME_EXIST SL_FS_FILE_NAME_EXIST -#define SL_ERROR_FS_FILE_NOT_EXISTS SL_FS_ERR_FILE_NOT_EXISTS -#define SL_ERROR_FS_NO_AVAILABLE_NV_INDEX SL_FS_ERR_NO_AVAILABLE_NV_INDEX -#define SL_ERROR_FS_NOT_ENOUGH_STORAGE_SPACE SL_FS_ERR_NO_AVAILABLE_BLOCKS -#define SL_ERROR_FS_NOT_SUPPORTED SL_FS_ERR_NOT_SUPPORTED -#define SL_ERROR_FS_WRONG_FILE_NAME SL_FS_WRONG_FILE_NAME -#define SL_ERROR_FS_INVALID_HANDLE SL_FS_ERR_INVALID_HANDLE -#define SL_NETCFG_MAC_ADDRESS_GET SL_MAC_ADDRESS_GET -#define SL_SOCKET_FD_ZERO SL_FD_ZERO -#define SL_SOCKET_FD_SET SL_FD_SET -#define SL_SOCKET_FD_ISSET SL_FD_ISSET -#define SL_SO_SECURE_DOMAIN_NAME_VERIFICATION SO_SECURE_DOMAIN_NAME_VERIFICATION +#define SL_ERROR_FS_FILE_NAME_EXIST SL_FS_FILE_NAME_EXIST +#define SL_ERROR_FS_FILE_NOT_EXISTS SL_FS_ERR_FILE_NOT_EXISTS +#define SL_ERROR_FS_NO_AVAILABLE_NV_INDEX SL_FS_ERR_NO_AVAILABLE_NV_INDEX +#define SL_ERROR_FS_NOT_ENOUGH_STORAGE_SPACE SL_FS_ERR_NO_AVAILABLE_BLOCKS +#define SL_ERROR_FS_NOT_SUPPORTED SL_FS_ERR_NOT_SUPPORTED +#define SL_ERROR_FS_WRONG_FILE_NAME SL_FS_WRONG_FILE_NAME +#define SL_ERROR_FS_INVALID_HANDLE SL_FS_ERR_INVALID_HANDLE +#define SL_NETCFG_MAC_ADDRESS_GET SL_MAC_ADDRESS_GET +#define SL_SOCKET_FD_ZERO SL_FD_ZERO +#define SL_SOCKET_FD_SET SL_FD_SET +#define SL_SOCKET_FD_ISSET SL_FD_ISSET +#define SL_SO_SECURE_DOMAIN_NAME_VERIFICATION SO_SECURE_DOMAIN_NAME_VERIFICATION -#define SL_FS_READ FS_MODE_OPEN_READ -#define SL_FS_WRITE FS_MODE_OPEN_WRITE +#define SL_FS_READ FS_MODE_OPEN_READ +#define SL_FS_WRITE FS_MODE_OPEN_WRITE -#define SL_FI_FILE_SIZE(fi) ((fi).FileLen) -#define SL_FI_FILE_MAX_SIZE(fi) ((fi).AllocatedLen) +#define SL_FI_FILE_SIZE(fi) ((fi).FileLen) +#define SL_FI_FILE_MAX_SIZE(fi) ((fi).AllocatedLen) -#define SlDeviceVersion_t SlVersionFull -#define sl_DeviceGet sl_DevGet -#define SL_DEVICE_GENERAL SL_DEVICE_GENERAL_CONFIGURATION -#define SL_LEN_TYPE _u8 -#define SL_OPT_TYPE _u8 +#define SlDeviceVersion_t SlVersionFull +#define sl_DeviceGet sl_DevGet +#define SL_DEVICE_GENERAL SL_DEVICE_GENERAL_CONFIGURATION +#define SL_LEN_TYPE _u8 +#define SL_OPT_TYPE _u8 #else /* SL_MAJOR_VERSION_NUM >= 2 */ #define FS_MODE_OPEN_CREATE(max_size, flag) \ (SL_FS_CREATE | SL_FS_CREATE_MAX_SIZE(max_size)) -#define SL_FI_FILE_SIZE(fi) ((fi).Len) -#define SL_FI_FILE_MAX_SIZE(fi) ((fi).MaxSize) +#define SL_FI_FILE_SIZE(fi) ((fi).Len) +#define SL_FI_FILE_MAX_SIZE(fi) ((fi).MaxSize) -#define SL_LEN_TYPE _u16 -#define SL_OPT_TYPE _u16 +#define SL_LEN_TYPE _u16 +#define SL_OPT_TYPE _u16 #endif /* SL_MAJOR_VERSION_NUM < 2 */ @@ -1302,7 +1318,7 @@ int slfs_open(const unsigned char *fname, uint32_t flags); #pragma warning(disable : 4204) /* missing c99 support */ #ifndef _WINSOCK_DEPRECATED_NO_WARNINGS -#define _WINSOCK_DEPRECATED_NO_WARNINGS 1 +#define _WINSOCK_DEPRECATED_NO_WARNINGS 1 #endif #ifndef _CRT_SECURE_NO_WARNINGS @@ -1322,120 +1338,121 @@ int slfs_open(const unsigned char *fname, uint32_t flags); #include #include -#define strdup _strdup +#define strdup _strdup #ifndef EINPROGRESS -#define EINPROGRESS WSAEINPROGRESS +#define EINPROGRESS WSAEINPROGRESS #endif #ifndef EWOULDBLOCK -#define EWOULDBLOCK WSAEWOULDBLOCK +#define EWOULDBLOCK WSAEWOULDBLOCK #endif #ifndef EAGAIN -#define EAGAIN EWOULDBLOCK +#define EAGAIN EWOULDBLOCK #endif #ifndef __func__ -#define STRX(x) #x -#define STR(x) STRX(x) -#define __func__ __FILE__ ":" STR(__LINE__) +#define STRX(x) #x +#define STR(x) STRX(x) +#define __func__ __FILE__ ":" STR(__LINE__) #endif -#define snprintf _snprintf -#define fileno _fileno -#define vsnprintf _vsnprintf -#define sleep(x) Sleep((x) *1000) -#define to64(x) _atoi64(x) -#define rmdir _rmdir +#define snprintf _snprintf +#define fileno _fileno +#define vsnprintf _vsnprintf +#define sleep(x) Sleep((x) * 1000) +#define to64(x) _atoi64(x) +#define rmdir _rmdir #if defined(_MSC_VER) && _MSC_VER >= 1400 -#define fseeko(x, y, z) _fseeki64((x), (y), (z)) +#define fseeko(x, y, z) _fseeki64((x), (y), (z)) #else -#define fseeko(x, y, z) fseek((x), (y), (z)) +#define fseeko(x, y, z) fseek((x), (y), (z)) #endif -typedef int socklen_t; +typedef int socklen_t; #if _MSC_VER >= 1700 #include #else -typedef signed char int8_t; -typedef unsigned char uint8_t; -typedef int int32_t; -typedef unsigned int uint32_t; -typedef short int16_t; -typedef unsigned short uint16_t; -typedef __int64 int64_t; +typedef signed char int8_t; +typedef unsigned char uint8_t; +typedef int int32_t; +typedef unsigned int uint32_t; +typedef short int16_t; +typedef unsigned short uint16_t; +typedef __int64 int64_t; typedef unsigned __int64 uint64_t; #endif -typedef SOCKET sock_t; -typedef uint32_t in_addr_t; +typedef SOCKET sock_t; +typedef uint32_t in_addr_t; #ifndef UINT16_MAX -#define UINT16_MAX 65535 +#define UINT16_MAX 65535 #endif #ifndef UINT32_MAX -#define UINT32_MAX 4294967295 +#define UINT32_MAX 4294967295 #endif #ifndef pid_t -#define pid_t HANDLE +#define pid_t HANDLE #endif -#define INT64_FMT "I64d" -#define INT64_X_FMT "I64x" +#define INT64_FMT "I64d" +#define INT64_X_FMT "I64x" /* TODO(alashkin): check if this is correct */ -#define SIZE_T_FMT "u" +#define SIZE_T_FMT "u" -#define DIRSEP '\\' +#define DIRSEP '\\' #define CS_DEFINE_DIRENT #ifndef va_copy #ifdef __va_copy -#define va_copy __va_copy +#define va_copy __va_copy #else -#define va_copy(x, y) (x) = (y) +#define va_copy(x, y) (x) = (y) #endif #endif #ifndef MG_MAX_HTTP_REQUEST_SIZE -#define MG_MAX_HTTP_REQUEST_SIZE 8192 +#define MG_MAX_HTTP_REQUEST_SIZE 8192 #endif #ifndef MG_MAX_HTTP_SEND_MBUF -#define MG_MAX_HTTP_SEND_MBUF 4096 +#define MG_MAX_HTTP_SEND_MBUF 4096 #endif #ifndef MG_MAX_HTTP_HEADERS -#define MG_MAX_HTTP_HEADERS 40 +#define MG_MAX_HTTP_HEADERS 40 #endif #ifndef CS_ENABLE_STDIO -#define CS_ENABLE_STDIO 1 +#define CS_ENABLE_STDIO 1 #endif -#define abort() DebugBreak(); +#define abort() DebugBreak(); #ifndef BUFSIZ -#define BUFSIZ 4096 +#define BUFSIZ 4096 #endif + /* * Explicitly disabling MG_ENABLE_THREADS for WinCE * because they are enabled for _WIN32 by default */ #ifndef MG_ENABLE_THREADS -#define MG_ENABLE_THREADS 0 +#define MG_ENABLE_THREADS 0 #endif #ifndef MG_ENABLE_FILESYSTEM -#define MG_ENABLE_FILESYSTEM 1 +#define MG_ENABLE_FILESYSTEM 1 #endif #ifndef MG_NET_IF -#define MG_NET_IF MG_NET_IF_SOCKET +#define MG_NET_IF MG_NET_IF_SOCKET #endif typedef struct _stati64 { @@ -1450,30 +1467,30 @@ typedef struct _stati64 { */ #ifndef ENOENT -#define ENOENT ERROR_PATH_NOT_FOUND +#define ENOENT ERROR_PATH_NOT_FOUND #endif #ifndef EACCES -#define EACCES ERROR_ACCESS_DENIED +#define EACCES ERROR_ACCESS_DENIED #endif #ifndef ENOMEM -#define ENOMEM ERROR_NOT_ENOUGH_MEMORY +#define ENOMEM ERROR_NOT_ENOUGH_MEMORY #endif #ifndef _UINTPTR_T_DEFINED typedef unsigned int *uintptr_t; #endif -#define _S_IFREG 2 -#define _S_IFDIR 4 +#define _S_IFREG 2 +#define _S_IFDIR 4 #ifndef S_ISDIR -#define S_ISDIR(x) (((x) &_S_IFDIR) != 0) +#define S_ISDIR(x) (((x) & _S_IFDIR) != 0) #endif #ifndef S_ISREG -#define S_ISREG(x) (((x) &_S_IFREG) != 0) +#define S_ISREG(x) (((x) & _S_IFREG) != 0) #endif int open(const char *filename, int oflag, int pmode); @@ -1485,6 +1502,7 @@ const char *strerror(); #ifdef MG_MODULE_LINES #line 1 "common/platforms/platform_nxp_lpc.h" #endif + /* * Copyright (c) 2014-2016 Cesanta Software Limited * All rights reserved @@ -1499,19 +1517,19 @@ const char *strerror(); #include #include -#define SIZE_T_FMT "u" +#define SIZE_T_FMT "u" typedef struct stat cs_stat_t; -#define INT64_FMT "lld" -#define INT64_X_FMT "llx" +#define INT64_FMT "lld" +#define INT64_X_FMT "llx" #define __cdecl -#define MG_LWIP 1 +#define MG_LWIP 1 -#define MG_NET_IF MG_NET_IF_LWIP_LOW_LEVEL +#define MG_NET_IF MG_NET_IF_LWIP_LOW_LEVEL /* * LPCXpress comes with 3 C library implementations: Newlib, NewlibNano and - *Redlib. + * Redlib. * See https://community.nxp.com/message/630860 for more details. * * Redlib is the default and lacks certain things, so we provide them. @@ -1519,20 +1537,20 @@ typedef struct stat cs_stat_t; #ifdef __REDLIB_INTERFACE_VERSION__ /* Let LWIP define timeval for us. */ -#define LWIP_TIMEVAL_PRIVATE 1 +#define LWIP_TIMEVAL_PRIVATE 1 -#define va_copy(d, s) __builtin_va_copy(d, s) +#define va_copy(d, s) __builtin_va_copy(d, s) -#define CS_ENABLE_TO64 1 -#define to64(x) cs_to64(x) +#define CS_ENABLE_TO64 1 +#define to64(x) cs_to64(x) -#define CS_ENABLE_STRDUP 1 +#define CS_ENABLE_STRDUP 1 #else #include -#define LWIP_TIMEVAL_PRIVATE 0 -#define to64(x) strtoll(x, NULL, 10) +#define LWIP_TIMEVAL_PRIVATE 0 +#define to64(x) strtoll(x, NULL, 10) #endif @@ -1541,6 +1559,7 @@ typedef struct stat cs_stat_t; #ifdef MG_MODULE_LINES #line 1 "common/platforms/platform_nxp_kinetis.h" #endif + /* * Copyright (c) 2014-2016 Cesanta Software Limited * All rights reserved @@ -1556,25 +1575,26 @@ typedef struct stat cs_stat_t; #include #include -#define SIZE_T_FMT "u" +#define SIZE_T_FMT "u" typedef struct stat cs_stat_t; -#define to64(x) strtoll(x, NULL, 10) -#define INT64_FMT "lld" -#define INT64_X_FMT "llx" +#define to64(x) strtoll(x, NULL, 10) +#define INT64_FMT "lld" +#define INT64_X_FMT "llx" #define __cdecl -#define MG_LWIP 1 +#define MG_LWIP 1 -#define MG_NET_IF MG_NET_IF_LWIP_LOW_LEVEL +#define MG_NET_IF MG_NET_IF_LWIP_LOW_LEVEL /* struct timeval is defined in sys/time.h. */ -#define LWIP_TIMEVAL_PRIVATE 0 +#define LWIP_TIMEVAL_PRIVATE 0 #endif /* CS_PLATFORM == CS_P_NXP_KINETIS */ #endif /* CS_COMMON_PLATFORMS_PLATFORM_NXP_KINETIS_H_ */ #ifdef MG_MODULE_LINES #line 1 "common/platforms/platform_pic32.h" #endif + /* * Copyright (c) 2014-2016 Cesanta Software Limited * All rights reserved @@ -1585,7 +1605,7 @@ typedef struct stat cs_stat_t; #if CS_PLATFORM == CS_P_PIC32 -#define MG_NET_IF MG_NET_IF_PIC32 +#define MG_NET_IF MG_NET_IF_PIC32 #include #include @@ -1598,13 +1618,13 @@ typedef struct stat cs_stat_t; #include typedef TCP_SOCKET sock_t; -#define to64(x) strtoll(x, NULL, 10) +#define to64(x) strtoll(x, NULL, 10) -#define SIZE_T_FMT "lu" -#define INT64_FMT "lld" +#define SIZE_T_FMT "lu" +#define INT64_FMT "lld" #ifndef CS_ENABLE_STDIO -#define CS_ENABLE_STDIO 1 +#define CS_ENABLE_STDIO 1 #endif char *inet_ntoa(struct in_addr in); @@ -1615,6 +1635,7 @@ char *inet_ntoa(struct in_addr in); #ifdef MG_MODULE_LINES #line 1 "common/platforms/platform_stm32.h" #endif + /* * Copyright (c) 2014-2016 Cesanta Software Limited * All rights reserved @@ -1635,18 +1656,18 @@ char *inet_ntoa(struct in_addr in); #include #include -#define to64(x) strtoll(x, NULL, 10) -#define INT64_FMT PRId64 -#define SIZE_T_FMT "u" +#define to64(x) strtoll(x, NULL, 10) +#define INT64_FMT PRId64 +#define SIZE_T_FMT "u" typedef struct stat cs_stat_t; -#define DIRSEP '/' +#define DIRSEP '/' #ifndef CS_ENABLE_STDIO -#define CS_ENABLE_STDIO 1 +#define CS_ENABLE_STDIO 1 #endif #ifndef MG_ENABLE_FILESYSTEM -#define MG_ENABLE_FILESYSTEM 1 +#define MG_ENABLE_FILESYSTEM 1 #endif #define CS_DEFINE_DIRENT @@ -1656,6 +1677,7 @@ typedef struct stat cs_stat_t; #ifdef MG_MODULE_LINES #line 1 "common/platforms/lwip/mg_lwip.h" #endif + /* * Copyright (c) 2014-2016 Cesanta Software Limited * All rights reserved @@ -1665,7 +1687,7 @@ typedef struct stat cs_stat_t; #define CS_COMMON_PLATFORMS_LWIP_MG_LWIP_H_ #ifndef MG_LWIP -#define MG_LWIP 0 +#define MG_LWIP 0 #endif #if MG_LWIP @@ -1699,14 +1721,14 @@ typedef struct stat cs_stat_t; #else /* We really need the definitions from sockets.h. */ #undef LWIP_SOCKET -#define LWIP_SOCKET 1 +#define LWIP_SOCKET 1 #include #undef LWIP_SOCKET -#define LWIP_SOCKET 0 +#define LWIP_SOCKET 0 #endif -#define INVALID_SOCKET (-1) -#define SOMAXCONN 10 +#define INVALID_SOCKET (-1) +#define SOMAXCONN 10 typedef int sock_t; #if MG_NET_IF == MG_NET_IF_LWIP_LOW_LEVEL @@ -1715,11 +1737,12 @@ struct mg_connection; uint32_t mg_lwip_get_poll_delay_ms(struct mg_mgr *mgr); void mg_lwip_set_keepalive_params(struct mg_connection *nc, int idle, int interval, int count); + #endif /* For older version of LWIP */ #ifndef ipX_2_ip -#define ipX_2_ip(x) (x) +#define ipX_2_ip(x) (x) #endif #endif /* MG_LWIP */ @@ -1728,6 +1751,7 @@ void mg_lwip_set_keepalive_params(struct mg_connection *nc, int idle, #ifdef MG_MODULE_LINES #line 1 "common/cs_md5.h" #endif + /* * Copyright (c) 2014 Cesanta Software Limited * All rights reserved @@ -1739,7 +1763,7 @@ void mg_lwip_set_keepalive_params(struct mg_connection *nc, int idle, /* Amalgamated: #include "common/platform.h" */ #ifndef CS_DISABLE_MD5 -#define CS_DISABLE_MD5 0 +#define CS_DISABLE_MD5 0 #endif #ifdef __cplusplus @@ -1764,6 +1788,7 @@ void cs_md5_final(unsigned char *md, cs_md5_ctx *c); #ifdef MG_MODULE_LINES #line 1 "common/cs_sha1.h" #endif + /* * Copyright (c) 2014 Cesanta Software Limited * All rights reserved @@ -1773,7 +1798,7 @@ void cs_md5_final(unsigned char *md, cs_md5_ctx *c); #define CS_COMMON_SHA1_H_ #ifndef CS_DISABLE_SHA1 -#define CS_DISABLE_SHA1 0 +#define CS_DISABLE_SHA1 0 #endif #if !CS_DISABLE_SHA1 @@ -1792,6 +1817,7 @@ typedef struct { void cs_sha1_init(cs_sha1_ctx *); void cs_sha1_update(cs_sha1_ctx *, const unsigned char *data, uint32_t len); + void cs_sha1_final(unsigned char digest[20], cs_sha1_ctx *); void cs_hmac_sha1(const unsigned char *key, size_t key_len, const unsigned char *text, size_t text_len, @@ -1806,6 +1832,7 @@ void cs_hmac_sha1(const unsigned char *key, size_t key_len, #ifdef MG_MODULE_LINES #line 1 "common/cs_time.h" #endif + /* * Copyright (c) 2014-2016 Cesanta Software Limited * All rights reserved @@ -1839,6 +1866,7 @@ double cs_timegm(const struct tm *tm); #ifdef MG_MODULE_LINES #line 1 "common/mg_str.h" #endif + /* * Copyright (c) 2014-2016 Cesanta Software Limited * All rights reserved @@ -1913,6 +1941,7 @@ const char *mg_strstr(const struct mg_str haystack, const struct mg_str needle); #ifdef MG_MODULE_LINES #line 1 "common/mbuf.h" #endif + /* * Copyright (c) 2015 Cesanta Software Limited * All rights reserved @@ -1938,7 +1967,7 @@ extern "C" { #endif #ifndef MBUF_SIZE_MULTIPLIER -#define MBUF_SIZE_MULTIPLIER 1.5 +#define MBUF_SIZE_MULTIPLIER 1.5 #endif /* Memory buffer descriptor */ @@ -1995,6 +2024,7 @@ void mbuf_trim(struct mbuf *); #ifdef MG_MODULE_LINES #line 1 "common/base64.h" #endif + /* * Copyright (c) 2014 Cesanta Software Limited * All rights reserved @@ -2004,7 +2034,7 @@ void mbuf_trim(struct mbuf *); #define CS_COMMON_BASE64_H_ #ifndef DISABLE_BASE64 -#define DISABLE_BASE64 0 +#define DISABLE_BASE64 0 #endif #if !DISABLE_BASE64 @@ -2044,6 +2074,7 @@ int cs_base64_decode(const unsigned char *s, int len, char *dst, int *dec_len); #ifdef MG_MODULE_LINES #line 1 "common/str_util.h" #endif + /* * Copyright (c) 2015 Cesanta Software Limited * All rights reserved @@ -2059,18 +2090,18 @@ int cs_base64_decode(const unsigned char *s, int len, char *dst, int *dec_len); /* Amalgamated: #include "common/mg_str.h" */ #ifndef CS_ENABLE_STRDUP -#define CS_ENABLE_STRDUP 0 +#define CS_ENABLE_STRDUP 0 #endif #ifndef CS_ENABLE_TO64 -#define CS_ENABLE_TO64 0 +#define CS_ENABLE_TO64 0 #endif /* * Expands to a string representation of its argument: e.g. * `CS_STRINGIFY_LIT(5) expands to "5"` */ -#define CS_STRINGIFY_LIT(x) #x +#define CS_STRINGIFY_LIT(x) #x /* * Expands to a string representation of its argument, which is allowed @@ -2081,7 +2112,7 @@ int cs_base64_decode(const unsigned char *s, int len, char *dst, int *dec_len); * * expands to 123. */ -#define CS_STRINGIFY_MACRO(x) CS_STRINGIFY_LIT(x) +#define CS_STRINGIFY_MACRO(x) CS_STRINGIFY_LIT(x) #ifdef __cplusplus extern "C" { @@ -2090,6 +2121,7 @@ extern "C" { size_t c_strnlen(const char *s, size_t maxlen); int c_snprintf(char *buf, size_t buf_size, const char *format, ...); int c_vsnprintf(char *buf, size_t buf_size, const char *format, va_list ap); + /* * Find the first occurrence of find in s, where the search is limited to the * first slen characters of s. @@ -2111,14 +2143,17 @@ void cs_from_hex(char *to, const char *p, size_t len); #if CS_ENABLE_STRDUP char *strdup(const char *src); + #endif #if CS_ENABLE_TO64 #include + /* * Simple string -> int64 conversion routine. */ int64_t cs_to64(const char *s); + #endif /* @@ -2188,6 +2223,7 @@ int mg_match_prefix_n(const struct mg_str pattern, const struct mg_str str); #line 1 "common/queue.h" #endif /* clang-format off */ + /*- * Copyright (c) 1991, 1993 * The Regents of the University of California. All rights reserved. @@ -2221,7 +2257,7 @@ int mg_match_prefix_n(const struct mg_str pattern, const struct mg_str str); */ #ifndef _SYS_QUEUE_H_ -#define _SYS_QUEUE_H_ +#define _SYS_QUEUE_H_ /* * This file defines four types of data structures: singly-linked lists, @@ -2299,649 +2335,651 @@ int mg_match_prefix_n(const struct mg_str pattern, const struct mg_str str); #ifdef QUEUE_MACRO_DEBUG /* Store the last 2 places the queue element or head was altered */ struct qm_trace { - unsigned long lastline; - unsigned long prevline; - const char *lastfile; - const char *prevfile; + unsigned long lastline; + unsigned long prevline; + const char *lastfile; + const char *prevfile; }; -#define TRACEBUF struct qm_trace trace; -#define TRACEBUF_INITIALIZER { __LINE__, 0, __FILE__, NULL } , -#define TRASHIT(x) do {(x) = (void *)-1;} while (0) -#define QMD_SAVELINK(name, link) void **name = (void *)&(link) +#define TRACEBUF struct qm_trace trace; +#define TRACEBUF_INITIALIZER { __LINE__, 0, __FILE__, NULL }, +#define TRASHIT(x) do { (x) = (void *)-1; } while (0) +#define QMD_SAVELINK(name, link) void **name = (void *)&(link) -#define QMD_TRACE_HEAD(head) do { \ - (head)->trace.prevline = (head)->trace.lastline; \ - (head)->trace.prevfile = (head)->trace.lastfile; \ - (head)->trace.lastline = __LINE__; \ - (head)->trace.lastfile = __FILE__; \ +#define QMD_TRACE_HEAD(head) do { \ + (head)->trace.prevline = (head)->trace.lastline; \ + (head)->trace.prevfile = (head)->trace.lastfile; \ + (head)->trace.lastline = __LINE__; \ + (head)->trace.lastfile = __FILE__; \ } while (0) -#define QMD_TRACE_ELEM(elem) do { \ - (elem)->trace.prevline = (elem)->trace.lastline; \ - (elem)->trace.prevfile = (elem)->trace.lastfile; \ - (elem)->trace.lastline = __LINE__; \ - (elem)->trace.lastfile = __FILE__; \ +#define QMD_TRACE_ELEM(elem) do { \ + (elem)->trace.prevline = (elem)->trace.lastline; \ + (elem)->trace.prevfile = (elem)->trace.lastfile; \ + (elem)->trace.lastline = __LINE__; \ + (elem)->trace.lastfile = __FILE__; \ } while (0) #else -#define QMD_TRACE_ELEM(elem) -#define QMD_TRACE_HEAD(head) -#define QMD_SAVELINK(name, link) -#define TRACEBUF -#define TRACEBUF_INITIALIZER -#define TRASHIT(x) -#endif /* QUEUE_MACRO_DEBUG */ +#define QMD_TRACE_ELEM(elem) +#define QMD_TRACE_HEAD(head) +#define QMD_SAVELINK(name, link) +#define TRACEBUF +#define TRACEBUF_INITIALIZER +#define TRASHIT(x) +#endif /* QUEUE_MACRO_DEBUG */ #ifdef __cplusplus + /* * In C++ there can be structure lists and class lists: */ -#define QUEUE_TYPEOF(type) type +#define QUEUE_TYPEOF(type) type #else -#define QUEUE_TYPEOF(type) struct type +#define QUEUE_TYPEOF(type) struct type #endif /* * Singly-linked List declarations. */ -#define SLIST_HEAD(name, type) \ -struct name { \ - struct type *slh_first; /* first element */ \ -} +#define SLIST_HEAD(name, type) \ + struct name { \ + struct type *slh_first; /* first element */ \ + } -#define SLIST_CLASS_HEAD(name, type) \ -struct name { \ - class type *slh_first; /* first element */ \ -} +#define SLIST_CLASS_HEAD(name, type) \ + struct name { \ + class type *slh_first; /* first element */ \ + } -#define SLIST_HEAD_INITIALIZER(head) \ - { NULL } +#define SLIST_HEAD_INITIALIZER(head) \ + { NULL } -#define SLIST_ENTRY(type) \ -struct { \ - struct type *sle_next; /* next element */ \ -} +#define SLIST_ENTRY(type) \ + struct { \ + struct type *sle_next; /* next element */ \ + } -#define SLIST_CLASS_ENTRY(type) \ -struct { \ - class type *sle_next; /* next element */ \ -} +#define SLIST_CLASS_ENTRY(type) \ + struct { \ + class type *sle_next; /* next element */ \ + } /* * Singly-linked List functions. */ -#define SLIST_EMPTY(head) ((head)->slh_first == NULL) +#define SLIST_EMPTY(head) ((head)->slh_first == NULL) -#define SLIST_FIRST(head) ((head)->slh_first) +#define SLIST_FIRST(head) ((head)->slh_first) -#define SLIST_FOREACH(var, head, field) \ - for ((var) = SLIST_FIRST((head)); \ - (var); \ - (var) = SLIST_NEXT((var), field)) +#define SLIST_FOREACH(var, head, field) \ + for ((var) = SLIST_FIRST((head)); \ + (var); \ + (var) = SLIST_NEXT((var), field)) -#define SLIST_FOREACH_FROM(var, head, field) \ - for ((var) = ((var) ? (var) : SLIST_FIRST((head))); \ - (var); \ - (var) = SLIST_NEXT((var), field)) +#define SLIST_FOREACH_FROM(var, head, field) \ + for ((var) = ((var) ? (var) : SLIST_FIRST((head))); \ + (var); \ + (var) = SLIST_NEXT((var), field)) -#define SLIST_FOREACH_SAFE(var, head, field, tvar) \ - for ((var) = SLIST_FIRST((head)); \ - (var) && ((tvar) = SLIST_NEXT((var), field), 1); \ - (var) = (tvar)) +#define SLIST_FOREACH_SAFE(var, head, field, tvar) \ + for ((var) = SLIST_FIRST((head)); \ + (var) && ((tvar) = SLIST_NEXT((var), field), 1); \ + (var) = (tvar)) -#define SLIST_FOREACH_FROM_SAFE(var, head, field, tvar) \ - for ((var) = ((var) ? (var) : SLIST_FIRST((head))); \ - (var) && ((tvar) = SLIST_NEXT((var), field), 1); \ - (var) = (tvar)) +#define SLIST_FOREACH_FROM_SAFE(var, head, field, tvar) \ + for ((var) = ((var) ? (var) : SLIST_FIRST((head))); \ + (var) && ((tvar) = SLIST_NEXT((var), field), 1); \ + (var) = (tvar)) -#define SLIST_FOREACH_PREVPTR(var, varp, head, field) \ - for ((varp) = &SLIST_FIRST((head)); \ - ((var) = *(varp)) != NULL; \ - (varp) = &SLIST_NEXT((var), field)) +#define SLIST_FOREACH_PREVPTR(var, varp, head, field) \ + for ((varp) = &SLIST_FIRST((head)); \ + ((var) = *(varp)) != NULL; \ + (varp) = &SLIST_NEXT((var), field)) -#define SLIST_INIT(head) do { \ - SLIST_FIRST((head)) = NULL; \ +#define SLIST_INIT(head) do { \ + SLIST_FIRST((head)) = NULL; \ } while (0) -#define SLIST_INSERT_AFTER(slistelm, elm, field) do { \ - SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \ - SLIST_NEXT((slistelm), field) = (elm); \ +#define SLIST_INSERT_AFTER(slistelm, elm, field) do { \ + SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \ + SLIST_NEXT((slistelm), field) = (elm); \ } while (0) -#define SLIST_INSERT_HEAD(head, elm, field) do { \ - SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \ - SLIST_FIRST((head)) = (elm); \ +#define SLIST_INSERT_HEAD(head, elm, field) do { \ + SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \ + SLIST_FIRST((head)) = (elm); \ } while (0) -#define SLIST_NEXT(elm, field) ((elm)->field.sle_next) +#define SLIST_NEXT(elm, field) ((elm)->field.sle_next) -#define SLIST_REMOVE(head, elm, type, field) do { \ - QMD_SAVELINK(oldnext, (elm)->field.sle_next); \ - if (SLIST_FIRST((head)) == (elm)) { \ - SLIST_REMOVE_HEAD((head), field); \ - } \ - else { \ - QUEUE_TYPEOF(type) *curelm = SLIST_FIRST(head); \ - while (SLIST_NEXT(curelm, field) != (elm)) \ - curelm = SLIST_NEXT(curelm, field); \ - SLIST_REMOVE_AFTER(curelm, field); \ - } \ - TRASHIT(*oldnext); \ +#define SLIST_REMOVE(head, elm, type, field) do { \ + QMD_SAVELINK(oldnext, (elm)->field.sle_next); \ + if (SLIST_FIRST((head)) == (elm)) { \ + SLIST_REMOVE_HEAD((head), field); \ + } \ + else { \ + QUEUE_TYPEOF(type) * curelm = SLIST_FIRST(head); \ + while (SLIST_NEXT(curelm, field) != (elm)) { \ + curelm = SLIST_NEXT(curelm, field); } \ + SLIST_REMOVE_AFTER(curelm, field); \ + } \ + TRASHIT(*oldnext); \ } while (0) -#define SLIST_REMOVE_AFTER(elm, field) do { \ - SLIST_NEXT(elm, field) = \ - SLIST_NEXT(SLIST_NEXT(elm, field), field); \ +#define SLIST_REMOVE_AFTER(elm, field) do { \ + SLIST_NEXT(elm, field) = \ + SLIST_NEXT(SLIST_NEXT(elm, field), field); \ } while (0) -#define SLIST_REMOVE_HEAD(head, field) do { \ - SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \ +#define SLIST_REMOVE_HEAD(head, field) do { \ + SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \ } while (0) -#define SLIST_SWAP(head1, head2, type) do { \ - QUEUE_TYPEOF(type) *swap_first = SLIST_FIRST(head1); \ - SLIST_FIRST(head1) = SLIST_FIRST(head2); \ - SLIST_FIRST(head2) = swap_first; \ +#define SLIST_SWAP(head1, head2, type) do { \ + QUEUE_TYPEOF(type) * swap_first = SLIST_FIRST(head1); \ + SLIST_FIRST(head1) = SLIST_FIRST(head2); \ + SLIST_FIRST(head2) = swap_first; \ } while (0) /* * Singly-linked Tail queue declarations. */ -#define STAILQ_HEAD(name, type) \ -struct name { \ - struct type *stqh_first;/* first element */ \ - struct type **stqh_last;/* addr of last next element */ \ -} +#define STAILQ_HEAD(name, type) \ + struct name { \ + struct type *stqh_first; /* first element */ \ + struct type **stqh_last; /* addr of last next element */ \ + } -#define STAILQ_CLASS_HEAD(name, type) \ -struct name { \ - class type *stqh_first; /* first element */ \ - class type **stqh_last; /* addr of last next element */ \ -} +#define STAILQ_CLASS_HEAD(name, type) \ + struct name { \ + class type *stqh_first; /* first element */ \ + class type **stqh_last; /* addr of last next element */ \ + } -#define STAILQ_HEAD_INITIALIZER(head) \ - { NULL, &(head).stqh_first } +#define STAILQ_HEAD_INITIALIZER(head) \ + { NULL, &(head).stqh_first } -#define STAILQ_ENTRY(type) \ -struct { \ - struct type *stqe_next; /* next element */ \ -} +#define STAILQ_ENTRY(type) \ + struct { \ + struct type *stqe_next; /* next element */ \ + } -#define STAILQ_CLASS_ENTRY(type) \ -struct { \ - class type *stqe_next; /* next element */ \ -} +#define STAILQ_CLASS_ENTRY(type) \ + struct { \ + class type *stqe_next; /* next element */ \ + } /* * Singly-linked Tail queue functions. */ -#define STAILQ_CONCAT(head1, head2) do { \ - if (!STAILQ_EMPTY((head2))) { \ - *(head1)->stqh_last = (head2)->stqh_first; \ - (head1)->stqh_last = (head2)->stqh_last; \ - STAILQ_INIT((head2)); \ - } \ +#define STAILQ_CONCAT(head1, head2) do { \ + if (!STAILQ_EMPTY((head2))) { \ + *(head1)->stqh_last = (head2)->stqh_first; \ + (head1)->stqh_last = (head2)->stqh_last; \ + STAILQ_INIT((head2)); \ + } \ } while (0) -#define STAILQ_EMPTY(head) ((head)->stqh_first == NULL) +#define STAILQ_EMPTY(head) ((head)->stqh_first == NULL) -#define STAILQ_FIRST(head) ((head)->stqh_first) +#define STAILQ_FIRST(head) ((head)->stqh_first) -#define STAILQ_FOREACH(var, head, field) \ - for((var) = STAILQ_FIRST((head)); \ - (var); \ - (var) = STAILQ_NEXT((var), field)) +#define STAILQ_FOREACH(var, head, field) \ + for ((var) = STAILQ_FIRST((head)); \ + (var); \ + (var) = STAILQ_NEXT((var), field)) -#define STAILQ_FOREACH_FROM(var, head, field) \ - for ((var) = ((var) ? (var) : STAILQ_FIRST((head))); \ - (var); \ - (var) = STAILQ_NEXT((var), field)) +#define STAILQ_FOREACH_FROM(var, head, field) \ + for ((var) = ((var) ? (var) : STAILQ_FIRST((head))); \ + (var); \ + (var) = STAILQ_NEXT((var), field)) -#define STAILQ_FOREACH_SAFE(var, head, field, tvar) \ - for ((var) = STAILQ_FIRST((head)); \ - (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \ - (var) = (tvar)) +#define STAILQ_FOREACH_SAFE(var, head, field, tvar) \ + for ((var) = STAILQ_FIRST((head)); \ + (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \ + (var) = (tvar)) -#define STAILQ_FOREACH_FROM_SAFE(var, head, field, tvar) \ - for ((var) = ((var) ? (var) : STAILQ_FIRST((head))); \ - (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \ - (var) = (tvar)) +#define STAILQ_FOREACH_FROM_SAFE(var, head, field, tvar) \ + for ((var) = ((var) ? (var) : STAILQ_FIRST((head))); \ + (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \ + (var) = (tvar)) -#define STAILQ_INIT(head) do { \ - STAILQ_FIRST((head)) = NULL; \ - (head)->stqh_last = &STAILQ_FIRST((head)); \ +#define STAILQ_INIT(head) do { \ + STAILQ_FIRST((head)) = NULL; \ + (head)->stqh_last = &STAILQ_FIRST((head)); \ } while (0) -#define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \ - if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\ - (head)->stqh_last = &STAILQ_NEXT((elm), field); \ - STAILQ_NEXT((tqelm), field) = (elm); \ +#define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \ + if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL) { \ + (head)->stqh_last = &STAILQ_NEXT((elm), field); } \ + STAILQ_NEXT((tqelm), field) = (elm); \ } while (0) -#define STAILQ_INSERT_HEAD(head, elm, field) do { \ - if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \ - (head)->stqh_last = &STAILQ_NEXT((elm), field); \ - STAILQ_FIRST((head)) = (elm); \ +#define STAILQ_INSERT_HEAD(head, elm, field) do { \ + if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) { \ + (head)->stqh_last = &STAILQ_NEXT((elm), field); } \ + STAILQ_FIRST((head)) = (elm); \ } while (0) -#define STAILQ_INSERT_TAIL(head, elm, field) do { \ - STAILQ_NEXT((elm), field) = NULL; \ - *(head)->stqh_last = (elm); \ - (head)->stqh_last = &STAILQ_NEXT((elm), field); \ +#define STAILQ_INSERT_TAIL(head, elm, field) do { \ + STAILQ_NEXT((elm), field) = NULL; \ + *(head)->stqh_last = (elm); \ + (head)->stqh_last = &STAILQ_NEXT((elm), field); \ } while (0) -#define STAILQ_LAST(head, type, field) \ - (STAILQ_EMPTY((head)) ? NULL : \ - __containerof((head)->stqh_last, \ - QUEUE_TYPEOF(type), field.stqe_next)) +#define STAILQ_LAST(head, type, field) \ + (STAILQ_EMPTY((head)) ? NULL : \ + __containerof((head)->stqh_last, \ + QUEUE_TYPEOF(type), field.stqe_next)) -#define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next) +#define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next) -#define STAILQ_REMOVE(head, elm, type, field) do { \ - QMD_SAVELINK(oldnext, (elm)->field.stqe_next); \ - if (STAILQ_FIRST((head)) == (elm)) { \ - STAILQ_REMOVE_HEAD((head), field); \ - } \ - else { \ - QUEUE_TYPEOF(type) *curelm = STAILQ_FIRST(head); \ - while (STAILQ_NEXT(curelm, field) != (elm)) \ - curelm = STAILQ_NEXT(curelm, field); \ - STAILQ_REMOVE_AFTER(head, curelm, field); \ - } \ - TRASHIT(*oldnext); \ +#define STAILQ_REMOVE(head, elm, type, field) do { \ + QMD_SAVELINK(oldnext, (elm)->field.stqe_next); \ + if (STAILQ_FIRST((head)) == (elm)) { \ + STAILQ_REMOVE_HEAD((head), field); \ + } \ + else { \ + QUEUE_TYPEOF(type) * curelm = STAILQ_FIRST(head); \ + while (STAILQ_NEXT(curelm, field) != (elm)) { \ + curelm = STAILQ_NEXT(curelm, field); } \ + STAILQ_REMOVE_AFTER(head, curelm, field); \ + } \ + TRASHIT(*oldnext); \ } while (0) -#define STAILQ_REMOVE_AFTER(head, elm, field) do { \ - if ((STAILQ_NEXT(elm, field) = \ - STAILQ_NEXT(STAILQ_NEXT(elm, field), field)) == NULL) \ - (head)->stqh_last = &STAILQ_NEXT((elm), field); \ +#define STAILQ_REMOVE_AFTER(head, elm, field) do { \ + if ((STAILQ_NEXT(elm, field) = \ + STAILQ_NEXT(STAILQ_NEXT(elm, field), field)) == NULL) { \ + (head)->stqh_last = &STAILQ_NEXT((elm), field); } \ } while (0) -#define STAILQ_REMOVE_HEAD(head, field) do { \ - if ((STAILQ_FIRST((head)) = \ - STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \ - (head)->stqh_last = &STAILQ_FIRST((head)); \ +#define STAILQ_REMOVE_HEAD(head, field) do { \ + if ((STAILQ_FIRST((head)) = \ + STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) { \ + (head)->stqh_last = &STAILQ_FIRST((head)); } \ } while (0) -#define STAILQ_SWAP(head1, head2, type) do { \ - QUEUE_TYPEOF(type) *swap_first = STAILQ_FIRST(head1); \ - QUEUE_TYPEOF(type) **swap_last = (head1)->stqh_last; \ - STAILQ_FIRST(head1) = STAILQ_FIRST(head2); \ - (head1)->stqh_last = (head2)->stqh_last; \ - STAILQ_FIRST(head2) = swap_first; \ - (head2)->stqh_last = swap_last; \ - if (STAILQ_EMPTY(head1)) \ - (head1)->stqh_last = &STAILQ_FIRST(head1); \ - if (STAILQ_EMPTY(head2)) \ - (head2)->stqh_last = &STAILQ_FIRST(head2); \ +#define STAILQ_SWAP(head1, head2, type) do { \ + QUEUE_TYPEOF(type) * swap_first = STAILQ_FIRST(head1); \ + QUEUE_TYPEOF(type) * *swap_last = (head1)->stqh_last; \ + STAILQ_FIRST(head1) = STAILQ_FIRST(head2); \ + (head1)->stqh_last = (head2)->stqh_last; \ + STAILQ_FIRST(head2) = swap_first; \ + (head2)->stqh_last = swap_last; \ + if (STAILQ_EMPTY(head1)) { \ + (head1)->stqh_last = &STAILQ_FIRST(head1); } \ + if (STAILQ_EMPTY(head2)) { \ + (head2)->stqh_last = &STAILQ_FIRST(head2); } \ } while (0) /* * List declarations. */ -#define LIST_HEAD(name, type) \ -struct name { \ - struct type *lh_first; /* first element */ \ -} +#define LIST_HEAD(name, type) \ + struct name { \ + struct type *lh_first; /* first element */ \ + } -#define LIST_CLASS_HEAD(name, type) \ -struct name { \ - class type *lh_first; /* first element */ \ -} +#define LIST_CLASS_HEAD(name, type) \ + struct name { \ + class type *lh_first; /* first element */ \ + } -#define LIST_HEAD_INITIALIZER(head) \ - { NULL } +#define LIST_HEAD_INITIALIZER(head) \ + { NULL } -#define LIST_ENTRY(type) \ -struct { \ - struct type *le_next; /* next element */ \ - struct type **le_prev; /* address of previous next element */ \ -} +#define LIST_ENTRY(type) \ + struct { \ + struct type *le_next; /* next element */ \ + struct type **le_prev; /* address of previous next element */ \ + } -#define LIST_CLASS_ENTRY(type) \ -struct { \ - class type *le_next; /* next element */ \ - class type **le_prev; /* address of previous next element */ \ -} +#define LIST_CLASS_ENTRY(type) \ + struct { \ + class type *le_next; /* next element */ \ + class type **le_prev; /* address of previous next element */ \ + } /* * List functions. */ #if (defined(_KERNEL) && defined(INVARIANTS)) -#define QMD_LIST_CHECK_HEAD(head, field) do { \ - if (LIST_FIRST((head)) != NULL && \ - LIST_FIRST((head))->field.le_prev != \ - &LIST_FIRST((head))) \ - panic("Bad list head %p first->prev != head", (head)); \ +#define QMD_LIST_CHECK_HEAD(head, field) do { \ + if (LIST_FIRST((head)) != NULL && \ + LIST_FIRST((head))->field.le_prev != \ + &LIST_FIRST((head))) { \ + panic("Bad list head %p first->prev != head", (head)); } \ } while (0) -#define QMD_LIST_CHECK_NEXT(elm, field) do { \ - if (LIST_NEXT((elm), field) != NULL && \ - LIST_NEXT((elm), field)->field.le_prev != \ - &((elm)->field.le_next)) \ - panic("Bad link elm %p next->prev != elm", (elm)); \ +#define QMD_LIST_CHECK_NEXT(elm, field) do { \ + if (LIST_NEXT((elm), field) != NULL && \ + LIST_NEXT((elm), field)->field.le_prev != \ + &((elm)->field.le_next)) { \ + panic("Bad link elm %p next->prev != elm", (elm)); } \ } while (0) -#define QMD_LIST_CHECK_PREV(elm, field) do { \ - if (*(elm)->field.le_prev != (elm)) \ - panic("Bad link elm %p prev->next != elm", (elm)); \ +#define QMD_LIST_CHECK_PREV(elm, field) do { \ + if (*(elm)->field.le_prev != (elm)) { \ + panic("Bad link elm %p prev->next != elm", (elm)); } \ } while (0) #else -#define QMD_LIST_CHECK_HEAD(head, field) -#define QMD_LIST_CHECK_NEXT(elm, field) -#define QMD_LIST_CHECK_PREV(elm, field) +#define QMD_LIST_CHECK_HEAD(head, field) +#define QMD_LIST_CHECK_NEXT(elm, field) +#define QMD_LIST_CHECK_PREV(elm, field) #endif /* (_KERNEL && INVARIANTS) */ -#define LIST_EMPTY(head) ((head)->lh_first == NULL) +#define LIST_EMPTY(head) ((head)->lh_first == NULL) -#define LIST_FIRST(head) ((head)->lh_first) +#define LIST_FIRST(head) ((head)->lh_first) -#define LIST_FOREACH(var, head, field) \ - for ((var) = LIST_FIRST((head)); \ - (var); \ - (var) = LIST_NEXT((var), field)) +#define LIST_FOREACH(var, head, field) \ + for ((var) = LIST_FIRST((head)); \ + (var); \ + (var) = LIST_NEXT((var), field)) -#define LIST_FOREACH_FROM(var, head, field) \ - for ((var) = ((var) ? (var) : LIST_FIRST((head))); \ - (var); \ - (var) = LIST_NEXT((var), field)) +#define LIST_FOREACH_FROM(var, head, field) \ + for ((var) = ((var) ? (var) : LIST_FIRST((head))); \ + (var); \ + (var) = LIST_NEXT((var), field)) -#define LIST_FOREACH_SAFE(var, head, field, tvar) \ - for ((var) = LIST_FIRST((head)); \ - (var) && ((tvar) = LIST_NEXT((var), field), 1); \ - (var) = (tvar)) +#define LIST_FOREACH_SAFE(var, head, field, tvar) \ + for ((var) = LIST_FIRST((head)); \ + (var) && ((tvar) = LIST_NEXT((var), field), 1); \ + (var) = (tvar)) -#define LIST_FOREACH_FROM_SAFE(var, head, field, tvar) \ - for ((var) = ((var) ? (var) : LIST_FIRST((head))); \ - (var) && ((tvar) = LIST_NEXT((var), field), 1); \ - (var) = (tvar)) +#define LIST_FOREACH_FROM_SAFE(var, head, field, tvar) \ + for ((var) = ((var) ? (var) : LIST_FIRST((head))); \ + (var) && ((tvar) = LIST_NEXT((var), field), 1); \ + (var) = (tvar)) -#define LIST_INIT(head) do { \ - LIST_FIRST((head)) = NULL; \ +#define LIST_INIT(head) do { \ + LIST_FIRST((head)) = NULL; \ } while (0) -#define LIST_INSERT_AFTER(listelm, elm, field) do { \ - QMD_LIST_CHECK_NEXT(listelm, field); \ - if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\ - LIST_NEXT((listelm), field)->field.le_prev = \ - &LIST_NEXT((elm), field); \ - LIST_NEXT((listelm), field) = (elm); \ - (elm)->field.le_prev = &LIST_NEXT((listelm), field); \ +#define LIST_INSERT_AFTER(listelm, elm, field) do { \ + QMD_LIST_CHECK_NEXT(listelm, field); \ + if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL) { \ + LIST_NEXT((listelm), field)->field.le_prev = \ + &LIST_NEXT((elm), field); } \ + LIST_NEXT((listelm), field) = (elm); \ + (elm)->field.le_prev = &LIST_NEXT((listelm), field); \ } while (0) -#define LIST_INSERT_BEFORE(listelm, elm, field) do { \ - QMD_LIST_CHECK_PREV(listelm, field); \ - (elm)->field.le_prev = (listelm)->field.le_prev; \ - LIST_NEXT((elm), field) = (listelm); \ - *(listelm)->field.le_prev = (elm); \ - (listelm)->field.le_prev = &LIST_NEXT((elm), field); \ +#define LIST_INSERT_BEFORE(listelm, elm, field) do { \ + QMD_LIST_CHECK_PREV(listelm, field); \ + (elm)->field.le_prev = (listelm)->field.le_prev; \ + LIST_NEXT((elm), field) = (listelm); \ + *(listelm)->field.le_prev = (elm); \ + (listelm)->field.le_prev = &LIST_NEXT((elm), field); \ } while (0) -#define LIST_INSERT_HEAD(head, elm, field) do { \ - QMD_LIST_CHECK_HEAD((head), field); \ - if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \ - LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\ - LIST_FIRST((head)) = (elm); \ - (elm)->field.le_prev = &LIST_FIRST((head)); \ +#define LIST_INSERT_HEAD(head, elm, field) do { \ + QMD_LIST_CHECK_HEAD((head), field); \ + if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) { \ + LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field); } \ + LIST_FIRST((head)) = (elm); \ + (elm)->field.le_prev = &LIST_FIRST((head)); \ } while (0) -#define LIST_NEXT(elm, field) ((elm)->field.le_next) +#define LIST_NEXT(elm, field) ((elm)->field.le_next) -#define LIST_PREV(elm, head, type, field) \ - ((elm)->field.le_prev == &LIST_FIRST((head)) ? NULL : \ - __containerof((elm)->field.le_prev, \ - QUEUE_TYPEOF(type), field.le_next)) +#define LIST_PREV(elm, head, type, field) \ + ((elm)->field.le_prev == &LIST_FIRST((head)) ? NULL : \ + __containerof((elm)->field.le_prev, \ + QUEUE_TYPEOF(type), field.le_next)) -#define LIST_REMOVE(elm, field) do { \ - QMD_SAVELINK(oldnext, (elm)->field.le_next); \ - QMD_SAVELINK(oldprev, (elm)->field.le_prev); \ - QMD_LIST_CHECK_NEXT(elm, field); \ - QMD_LIST_CHECK_PREV(elm, field); \ - if (LIST_NEXT((elm), field) != NULL) \ - LIST_NEXT((elm), field)->field.le_prev = \ - (elm)->field.le_prev; \ - *(elm)->field.le_prev = LIST_NEXT((elm), field); \ - TRASHIT(*oldnext); \ - TRASHIT(*oldprev); \ +#define LIST_REMOVE(elm, field) do { \ + QMD_SAVELINK(oldnext, (elm)->field.le_next); \ + QMD_SAVELINK(oldprev, (elm)->field.le_prev); \ + QMD_LIST_CHECK_NEXT(elm, field); \ + QMD_LIST_CHECK_PREV(elm, field); \ + if (LIST_NEXT((elm), field) != NULL) { \ + LIST_NEXT((elm), field)->field.le_prev = \ + (elm)->field.le_prev; } \ + *(elm)->field.le_prev = LIST_NEXT((elm), field); \ + TRASHIT(*oldnext); \ + TRASHIT(*oldprev); \ } while (0) -#define LIST_SWAP(head1, head2, type, field) do { \ - QUEUE_TYPEOF(type) *swap_tmp = LIST_FIRST(head1); \ - LIST_FIRST((head1)) = LIST_FIRST((head2)); \ - LIST_FIRST((head2)) = swap_tmp; \ - if ((swap_tmp = LIST_FIRST((head1))) != NULL) \ - swap_tmp->field.le_prev = &LIST_FIRST((head1)); \ - if ((swap_tmp = LIST_FIRST((head2))) != NULL) \ - swap_tmp->field.le_prev = &LIST_FIRST((head2)); \ +#define LIST_SWAP(head1, head2, type, field) do { \ + QUEUE_TYPEOF(type) * swap_tmp = LIST_FIRST(head1); \ + LIST_FIRST((head1)) = LIST_FIRST((head2)); \ + LIST_FIRST((head2)) = swap_tmp; \ + if ((swap_tmp = LIST_FIRST((head1))) != NULL) { \ + swap_tmp->field.le_prev = &LIST_FIRST((head1)); } \ + if ((swap_tmp = LIST_FIRST((head2))) != NULL) { \ + swap_tmp->field.le_prev = &LIST_FIRST((head2)); } \ } while (0) /* * Tail queue declarations. */ -#define TAILQ_HEAD(name, type) \ -struct name { \ - struct type *tqh_first; /* first element */ \ - struct type **tqh_last; /* addr of last next element */ \ - TRACEBUF \ -} +#define TAILQ_HEAD(name, type) \ + struct name { \ + struct type *tqh_first; /* first element */ \ + struct type **tqh_last; /* addr of last next element */ \ + TRACEBUF \ + } -#define TAILQ_CLASS_HEAD(name, type) \ -struct name { \ - class type *tqh_first; /* first element */ \ - class type **tqh_last; /* addr of last next element */ \ - TRACEBUF \ -} +#define TAILQ_CLASS_HEAD(name, type) \ + struct name { \ + class type *tqh_first; /* first element */ \ + class type **tqh_last; /* addr of last next element */ \ + TRACEBUF \ + } -#define TAILQ_HEAD_INITIALIZER(head) \ - { NULL, &(head).tqh_first, TRACEBUF_INITIALIZER } +#define TAILQ_HEAD_INITIALIZER(head) \ + { NULL, &(head).tqh_first, TRACEBUF_INITIALIZER } -#define TAILQ_ENTRY(type) \ -struct { \ - struct type *tqe_next; /* next element */ \ - struct type **tqe_prev; /* address of previous next element */ \ - TRACEBUF \ -} +#define TAILQ_ENTRY(type) \ + struct { \ + struct type *tqe_next; /* next element */ \ + struct type **tqe_prev; /* address of previous next element */ \ + TRACEBUF \ + } -#define TAILQ_CLASS_ENTRY(type) \ -struct { \ - class type *tqe_next; /* next element */ \ - class type **tqe_prev; /* address of previous next element */ \ - TRACEBUF \ -} +#define TAILQ_CLASS_ENTRY(type) \ + struct { \ + class type *tqe_next; /* next element */ \ + class type **tqe_prev; /* address of previous next element */ \ + TRACEBUF \ + } /* * Tail queue functions. */ #if (defined(_KERNEL) && defined(INVARIANTS)) -#define QMD_TAILQ_CHECK_HEAD(head, field) do { \ - if (!TAILQ_EMPTY(head) && \ - TAILQ_FIRST((head))->field.tqe_prev != \ - &TAILQ_FIRST((head))) \ - panic("Bad tailq head %p first->prev != head", (head)); \ +#define QMD_TAILQ_CHECK_HEAD(head, field) do { \ + if (!TAILQ_EMPTY(head) && \ + TAILQ_FIRST((head))->field.tqe_prev != \ + &TAILQ_FIRST((head))) { \ + panic("Bad tailq head %p first->prev != head", (head)); } \ } while (0) -#define QMD_TAILQ_CHECK_TAIL(head, field) do { \ - if (*(head)->tqh_last != NULL) \ - panic("Bad tailq NEXT(%p->tqh_last) != NULL", (head)); \ +#define QMD_TAILQ_CHECK_TAIL(head, field) do { \ + if (*(head)->tqh_last != NULL) { \ + panic("Bad tailq NEXT(%p->tqh_last) != NULL", (head)); } \ } while (0) -#define QMD_TAILQ_CHECK_NEXT(elm, field) do { \ - if (TAILQ_NEXT((elm), field) != NULL && \ - TAILQ_NEXT((elm), field)->field.tqe_prev != \ - &((elm)->field.tqe_next)) \ - panic("Bad link elm %p next->prev != elm", (elm)); \ +#define QMD_TAILQ_CHECK_NEXT(elm, field) do { \ + if (TAILQ_NEXT((elm), field) != NULL && \ + TAILQ_NEXT((elm), field)->field.tqe_prev != \ + &((elm)->field.tqe_next)) { \ + panic("Bad link elm %p next->prev != elm", (elm)); } \ } while (0) -#define QMD_TAILQ_CHECK_PREV(elm, field) do { \ - if (*(elm)->field.tqe_prev != (elm)) \ - panic("Bad link elm %p prev->next != elm", (elm)); \ +#define QMD_TAILQ_CHECK_PREV(elm, field) do { \ + if (*(elm)->field.tqe_prev != (elm)) { \ + panic("Bad link elm %p prev->next != elm", (elm)); } \ } while (0) #else -#define QMD_TAILQ_CHECK_HEAD(head, field) -#define QMD_TAILQ_CHECK_TAIL(head, headname) -#define QMD_TAILQ_CHECK_NEXT(elm, field) -#define QMD_TAILQ_CHECK_PREV(elm, field) +#define QMD_TAILQ_CHECK_HEAD(head, field) +#define QMD_TAILQ_CHECK_TAIL(head, headname) +#define QMD_TAILQ_CHECK_NEXT(elm, field) +#define QMD_TAILQ_CHECK_PREV(elm, field) #endif /* (_KERNEL && INVARIANTS) */ -#define TAILQ_CONCAT(head1, head2, field) do { \ - if (!TAILQ_EMPTY(head2)) { \ - *(head1)->tqh_last = (head2)->tqh_first; \ - (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \ - (head1)->tqh_last = (head2)->tqh_last; \ - TAILQ_INIT((head2)); \ - QMD_TRACE_HEAD(head1); \ - QMD_TRACE_HEAD(head2); \ - } \ +#define TAILQ_CONCAT(head1, head2, field) do { \ + if (!TAILQ_EMPTY(head2)) { \ + *(head1)->tqh_last = (head2)->tqh_first; \ + (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \ + (head1)->tqh_last = (head2)->tqh_last; \ + TAILQ_INIT((head2)); \ + QMD_TRACE_HEAD(head1); \ + QMD_TRACE_HEAD(head2); \ + } \ } while (0) -#define TAILQ_EMPTY(head) ((head)->tqh_first == NULL) +#define TAILQ_EMPTY(head) ((head)->tqh_first == NULL) -#define TAILQ_FIRST(head) ((head)->tqh_first) +#define TAILQ_FIRST(head) ((head)->tqh_first) -#define TAILQ_FOREACH(var, head, field) \ - for ((var) = TAILQ_FIRST((head)); \ - (var); \ - (var) = TAILQ_NEXT((var), field)) +#define TAILQ_FOREACH(var, head, field) \ + for ((var) = TAILQ_FIRST((head)); \ + (var); \ + (var) = TAILQ_NEXT((var), field)) -#define TAILQ_FOREACH_FROM(var, head, field) \ - for ((var) = ((var) ? (var) : TAILQ_FIRST((head))); \ - (var); \ - (var) = TAILQ_NEXT((var), field)) +#define TAILQ_FOREACH_FROM(var, head, field) \ + for ((var) = ((var) ? (var) : TAILQ_FIRST((head))); \ + (var); \ + (var) = TAILQ_NEXT((var), field)) -#define TAILQ_FOREACH_SAFE(var, head, field, tvar) \ - for ((var) = TAILQ_FIRST((head)); \ - (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \ - (var) = (tvar)) +#define TAILQ_FOREACH_SAFE(var, head, field, tvar) \ + for ((var) = TAILQ_FIRST((head)); \ + (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \ + (var) = (tvar)) -#define TAILQ_FOREACH_FROM_SAFE(var, head, field, tvar) \ - for ((var) = ((var) ? (var) : TAILQ_FIRST((head))); \ - (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \ - (var) = (tvar)) +#define TAILQ_FOREACH_FROM_SAFE(var, head, field, tvar) \ + for ((var) = ((var) ? (var) : TAILQ_FIRST((head))); \ + (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \ + (var) = (tvar)) -#define TAILQ_FOREACH_REVERSE(var, head, headname, field) \ - for ((var) = TAILQ_LAST((head), headname); \ - (var); \ - (var) = TAILQ_PREV((var), headname, field)) +#define TAILQ_FOREACH_REVERSE(var, head, headname, field) \ + for ((var) = TAILQ_LAST((head), headname); \ + (var); \ + (var) = TAILQ_PREV((var), headname, field)) -#define TAILQ_FOREACH_REVERSE_FROM(var, head, headname, field) \ - for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname)); \ - (var); \ - (var) = TAILQ_PREV((var), headname, field)) +#define TAILQ_FOREACH_REVERSE_FROM(var, head, headname, field) \ + for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname)); \ + (var); \ + (var) = TAILQ_PREV((var), headname, field)) -#define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \ - for ((var) = TAILQ_LAST((head), headname); \ - (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \ - (var) = (tvar)) +#define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \ + for ((var) = TAILQ_LAST((head), headname); \ + (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \ + (var) = (tvar)) -#define TAILQ_FOREACH_REVERSE_FROM_SAFE(var, head, headname, field, tvar) \ - for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname)); \ - (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \ - (var) = (tvar)) +#define TAILQ_FOREACH_REVERSE_FROM_SAFE(var, head, headname, field, tvar) \ + for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname)); \ + (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \ + (var) = (tvar)) -#define TAILQ_INIT(head) do { \ - TAILQ_FIRST((head)) = NULL; \ - (head)->tqh_last = &TAILQ_FIRST((head)); \ - QMD_TRACE_HEAD(head); \ +#define TAILQ_INIT(head) do { \ + TAILQ_FIRST((head)) = NULL; \ + (head)->tqh_last = &TAILQ_FIRST((head)); \ + QMD_TRACE_HEAD(head); \ } while (0) -#define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \ - QMD_TAILQ_CHECK_NEXT(listelm, field); \ - if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\ - TAILQ_NEXT((elm), field)->field.tqe_prev = \ - &TAILQ_NEXT((elm), field); \ - else { \ - (head)->tqh_last = &TAILQ_NEXT((elm), field); \ - QMD_TRACE_HEAD(head); \ - } \ - TAILQ_NEXT((listelm), field) = (elm); \ - (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \ - QMD_TRACE_ELEM(&(elm)->field); \ - QMD_TRACE_ELEM(&(listelm)->field); \ +#define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \ + QMD_TAILQ_CHECK_NEXT(listelm, field); \ + if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL) { \ + TAILQ_NEXT((elm), field)->field.tqe_prev = \ + &TAILQ_NEXT((elm), field); } \ + else { \ + (head)->tqh_last = &TAILQ_NEXT((elm), field); \ + QMD_TRACE_HEAD(head); \ + } \ + TAILQ_NEXT((listelm), field) = (elm); \ + (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \ + QMD_TRACE_ELEM(&(elm)->field); \ + QMD_TRACE_ELEM(&(listelm)->field); \ } while (0) -#define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \ - QMD_TAILQ_CHECK_PREV(listelm, field); \ - (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \ - TAILQ_NEXT((elm), field) = (listelm); \ - *(listelm)->field.tqe_prev = (elm); \ - (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \ - QMD_TRACE_ELEM(&(elm)->field); \ - QMD_TRACE_ELEM(&(listelm)->field); \ +#define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \ + QMD_TAILQ_CHECK_PREV(listelm, field); \ + (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \ + TAILQ_NEXT((elm), field) = (listelm); \ + *(listelm)->field.tqe_prev = (elm); \ + (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \ + QMD_TRACE_ELEM(&(elm)->field); \ + QMD_TRACE_ELEM(&(listelm)->field); \ } while (0) -#define TAILQ_INSERT_HEAD(head, elm, field) do { \ - QMD_TAILQ_CHECK_HEAD(head, field); \ - if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \ - TAILQ_FIRST((head))->field.tqe_prev = \ - &TAILQ_NEXT((elm), field); \ - else \ - (head)->tqh_last = &TAILQ_NEXT((elm), field); \ - TAILQ_FIRST((head)) = (elm); \ - (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \ - QMD_TRACE_HEAD(head); \ - QMD_TRACE_ELEM(&(elm)->field); \ +#define TAILQ_INSERT_HEAD(head, elm, field) do { \ + QMD_TAILQ_CHECK_HEAD(head, field); \ + if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) { \ + TAILQ_FIRST((head))->field.tqe_prev = \ + &TAILQ_NEXT((elm), field); } \ + else{ \ + (head)->tqh_last = &TAILQ_NEXT((elm), field); } \ + TAILQ_FIRST((head)) = (elm); \ + (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \ + QMD_TRACE_HEAD(head); \ + QMD_TRACE_ELEM(&(elm)->field); \ } while (0) -#define TAILQ_INSERT_TAIL(head, elm, field) do { \ - QMD_TAILQ_CHECK_TAIL(head, field); \ - TAILQ_NEXT((elm), field) = NULL; \ - (elm)->field.tqe_prev = (head)->tqh_last; \ - *(head)->tqh_last = (elm); \ - (head)->tqh_last = &TAILQ_NEXT((elm), field); \ - QMD_TRACE_HEAD(head); \ - QMD_TRACE_ELEM(&(elm)->field); \ +#define TAILQ_INSERT_TAIL(head, elm, field) do { \ + QMD_TAILQ_CHECK_TAIL(head, field); \ + TAILQ_NEXT((elm), field) = NULL; \ + (elm)->field.tqe_prev = (head)->tqh_last; \ + *(head)->tqh_last = (elm); \ + (head)->tqh_last = &TAILQ_NEXT((elm), field); \ + QMD_TRACE_HEAD(head); \ + QMD_TRACE_ELEM(&(elm)->field); \ } while (0) -#define TAILQ_LAST(head, headname) \ - (*(((struct headname *)((head)->tqh_last))->tqh_last)) +#define TAILQ_LAST(head, headname) \ + (*(((struct headname *)((head)->tqh_last))->tqh_last)) -#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next) +#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next) -#define TAILQ_PREV(elm, headname, field) \ - (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last)) +#define TAILQ_PREV(elm, headname, field) \ + (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last)) -#define TAILQ_REMOVE(head, elm, field) do { \ - QMD_SAVELINK(oldnext, (elm)->field.tqe_next); \ - QMD_SAVELINK(oldprev, (elm)->field.tqe_prev); \ - QMD_TAILQ_CHECK_NEXT(elm, field); \ - QMD_TAILQ_CHECK_PREV(elm, field); \ - if ((TAILQ_NEXT((elm), field)) != NULL) \ - TAILQ_NEXT((elm), field)->field.tqe_prev = \ - (elm)->field.tqe_prev; \ - else { \ - (head)->tqh_last = (elm)->field.tqe_prev; \ - QMD_TRACE_HEAD(head); \ - } \ - *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \ - TRASHIT(*oldnext); \ - TRASHIT(*oldprev); \ - QMD_TRACE_ELEM(&(elm)->field); \ +#define TAILQ_REMOVE(head, elm, field) do { \ + QMD_SAVELINK(oldnext, (elm)->field.tqe_next); \ + QMD_SAVELINK(oldprev, (elm)->field.tqe_prev); \ + QMD_TAILQ_CHECK_NEXT(elm, field); \ + QMD_TAILQ_CHECK_PREV(elm, field); \ + if ((TAILQ_NEXT((elm), field)) != NULL) { \ + TAILQ_NEXT((elm), field)->field.tqe_prev = \ + (elm)->field.tqe_prev; } \ + else { \ + (head)->tqh_last = (elm)->field.tqe_prev; \ + QMD_TRACE_HEAD(head); \ + } \ + *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \ + TRASHIT(*oldnext); \ + TRASHIT(*oldprev); \ + QMD_TRACE_ELEM(&(elm)->field); \ } while (0) -#define TAILQ_SWAP(head1, head2, type, field) do { \ - QUEUE_TYPEOF(type) *swap_first = (head1)->tqh_first; \ - QUEUE_TYPEOF(type) **swap_last = (head1)->tqh_last; \ - (head1)->tqh_first = (head2)->tqh_first; \ - (head1)->tqh_last = (head2)->tqh_last; \ - (head2)->tqh_first = swap_first; \ - (head2)->tqh_last = swap_last; \ - if ((swap_first = (head1)->tqh_first) != NULL) \ - swap_first->field.tqe_prev = &(head1)->tqh_first; \ - else \ - (head1)->tqh_last = &(head1)->tqh_first; \ - if ((swap_first = (head2)->tqh_first) != NULL) \ - swap_first->field.tqe_prev = &(head2)->tqh_first; \ - else \ - (head2)->tqh_last = &(head2)->tqh_first; \ +#define TAILQ_SWAP(head1, head2, type, field) do { \ + QUEUE_TYPEOF(type) * swap_first = (head1)->tqh_first; \ + QUEUE_TYPEOF(type) * *swap_last = (head1)->tqh_last; \ + (head1)->tqh_first = (head2)->tqh_first; \ + (head1)->tqh_last = (head2)->tqh_last; \ + (head2)->tqh_first = swap_first; \ + (head2)->tqh_last = swap_last; \ + if ((swap_first = (head1)->tqh_first) != NULL) { \ + swap_first->field.tqe_prev = &(head1)->tqh_first; } \ + else{ \ + (head1)->tqh_last = &(head1)->tqh_first; } \ + if ((swap_first = (head2)->tqh_first) != NULL) { \ + swap_first->field.tqe_prev = &(head2)->tqh_first; } \ + else{ \ + (head2)->tqh_last = &(head2)->tqh_first; } \ } while (0) #endif /* !_SYS_QUEUE_H_ */ #ifdef MG_MODULE_LINES #line 1 "mongoose/src/features.h" #endif + /* * Copyright (c) 2014-2016 Cesanta Software Limited * All rights reserved @@ -2951,145 +2989,145 @@ struct { \ #define CS_MONGOOSE_SRC_FEATURES_H_ #ifndef MG_DISABLE_HTTP_DIGEST_AUTH -#define MG_DISABLE_HTTP_DIGEST_AUTH 0 +#define MG_DISABLE_HTTP_DIGEST_AUTH 0 #endif #ifndef MG_DISABLE_HTTP_KEEP_ALIVE -#define MG_DISABLE_HTTP_KEEP_ALIVE 0 +#define MG_DISABLE_HTTP_KEEP_ALIVE 0 #endif #ifndef MG_DISABLE_PFS -#define MG_DISABLE_PFS 0 +#define MG_DISABLE_PFS 0 #endif #ifndef MG_DISABLE_WS_RANDOM_MASK -#define MG_DISABLE_WS_RANDOM_MASK 0 +#define MG_DISABLE_WS_RANDOM_MASK 0 #endif #ifndef MG_ENABLE_ASYNC_RESOLVER -#define MG_ENABLE_ASYNC_RESOLVER 1 +#define MG_ENABLE_ASYNC_RESOLVER 1 #endif #ifndef MG_ENABLE_BROADCAST -#define MG_ENABLE_BROADCAST 0 +#define MG_ENABLE_BROADCAST 0 #endif #ifndef MG_ENABLE_COAP -#define MG_ENABLE_COAP 0 +#define MG_ENABLE_COAP 0 #endif #ifndef MG_ENABLE_DEBUG -#define MG_ENABLE_DEBUG 0 +#define MG_ENABLE_DEBUG 0 #endif #ifndef MG_ENABLE_DIRECTORY_LISTING -#define MG_ENABLE_DIRECTORY_LISTING 0 +#define MG_ENABLE_DIRECTORY_LISTING 0 #endif #ifndef MG_ENABLE_DNS -#define MG_ENABLE_DNS 1 +#define MG_ENABLE_DNS 1 #endif #ifndef MG_ENABLE_DNS_SERVER -#define MG_ENABLE_DNS_SERVER 0 +#define MG_ENABLE_DNS_SERVER 0 #endif #ifndef MG_ENABLE_FAKE_DAVLOCK -#define MG_ENABLE_FAKE_DAVLOCK 0 +#define MG_ENABLE_FAKE_DAVLOCK 0 #endif #ifndef MG_ENABLE_FILESYSTEM -#define MG_ENABLE_FILESYSTEM 0 +#define MG_ENABLE_FILESYSTEM 0 #endif #ifndef MG_ENABLE_GETADDRINFO -#define MG_ENABLE_GETADDRINFO 0 +#define MG_ENABLE_GETADDRINFO 0 #endif #ifndef MG_ENABLE_HEXDUMP -#define MG_ENABLE_HEXDUMP CS_ENABLE_STDIO +#define MG_ENABLE_HEXDUMP CS_ENABLE_STDIO #endif #ifndef MG_ENABLE_HTTP -#define MG_ENABLE_HTTP 1 +#define MG_ENABLE_HTTP 1 #endif #ifndef MG_ENABLE_HTTP_CGI -#define MG_ENABLE_HTTP_CGI 0 +#define MG_ENABLE_HTTP_CGI 0 #endif #ifndef MG_ENABLE_HTTP_SSI -#define MG_ENABLE_HTTP_SSI MG_ENABLE_FILESYSTEM +#define MG_ENABLE_HTTP_SSI MG_ENABLE_FILESYSTEM #endif #ifndef MG_ENABLE_HTTP_SSI_EXEC -#define MG_ENABLE_HTTP_SSI_EXEC 0 +#define MG_ENABLE_HTTP_SSI_EXEC 0 #endif #ifndef MG_ENABLE_HTTP_STREAMING_MULTIPART -#define MG_ENABLE_HTTP_STREAMING_MULTIPART 0 +#define MG_ENABLE_HTTP_STREAMING_MULTIPART 0 #endif #ifndef MG_ENABLE_HTTP_WEBDAV -#define MG_ENABLE_HTTP_WEBDAV 0 +#define MG_ENABLE_HTTP_WEBDAV 0 #endif #ifndef MG_ENABLE_HTTP_WEBSOCKET -#define MG_ENABLE_HTTP_WEBSOCKET MG_ENABLE_HTTP +#define MG_ENABLE_HTTP_WEBSOCKET MG_ENABLE_HTTP #endif #ifndef MG_ENABLE_IPV6 -#define MG_ENABLE_IPV6 0 +#define MG_ENABLE_IPV6 0 #endif #ifndef MG_ENABLE_MQTT -#define MG_ENABLE_MQTT 1 +#define MG_ENABLE_MQTT 1 #endif #ifndef MG_ENABLE_SOCKS -#define MG_ENABLE_SOCKS 0 +#define MG_ENABLE_SOCKS 0 #endif #ifndef MG_ENABLE_MQTT_BROKER -#define MG_ENABLE_MQTT_BROKER 0 +#define MG_ENABLE_MQTT_BROKER 0 #endif #ifndef MG_ENABLE_SSL -#define MG_ENABLE_SSL 0 +#define MG_ENABLE_SSL 0 #endif #ifndef MG_ENABLE_SYNC_RESOLVER -#define MG_ENABLE_SYNC_RESOLVER 0 +#define MG_ENABLE_SYNC_RESOLVER 0 #endif #ifndef MG_ENABLE_STDIO -#define MG_ENABLE_STDIO CS_ENABLE_STDIO +#define MG_ENABLE_STDIO CS_ENABLE_STDIO #endif #ifndef MG_NET_IF -#define MG_NET_IF MG_NET_IF_SOCKET +#define MG_NET_IF MG_NET_IF_SOCKET #endif #ifndef MG_SSL_IF -#define MG_SSL_IF MG_SSL_IF_OPENSSL +#define MG_SSL_IF MG_SSL_IF_OPENSSL #endif #ifndef MG_ENABLE_THREADS /* ifdef-ok */ #ifdef _WIN32 -#define MG_ENABLE_THREADS 1 +#define MG_ENABLE_THREADS 1 #else -#define MG_ENABLE_THREADS 0 +#define MG_ENABLE_THREADS 0 #endif #endif #if MG_ENABLE_DEBUG && !defined(CS_ENABLE_DEBUG) -#define CS_ENABLE_DEBUG 1 +#define CS_ENABLE_DEBUG 1 #endif /* MQTT broker requires MQTT */ #if MG_ENABLE_MQTT_BROKER && !MG_ENABLE_MQTT #undef MG_ENABLE_MQTT -#define MG_ENABLE_MQTT 1 +#define MG_ENABLE_MQTT 1 #endif #ifndef MG_ENABLE_HTTP_URL_REWRITES @@ -3098,33 +3136,34 @@ struct { \ #endif #ifndef MG_ENABLE_TUN -#define MG_ENABLE_TUN MG_ENABLE_HTTP_WEBSOCKET +#define MG_ENABLE_TUN MG_ENABLE_HTTP_WEBSOCKET #endif #ifndef MG_ENABLE_SNTP -#define MG_ENABLE_SNTP 0 +#define MG_ENABLE_SNTP 0 #endif #ifndef MG_ENABLE_EXTRA_ERRORS_DESC -#define MG_ENABLE_EXTRA_ERRORS_DESC 0 +#define MG_ENABLE_EXTRA_ERRORS_DESC 0 #endif #ifndef MG_ENABLE_CALLBACK_USERDATA -#define MG_ENABLE_CALLBACK_USERDATA 0 +#define MG_ENABLE_CALLBACK_USERDATA 0 #endif #if MG_ENABLE_CALLBACK_USERDATA -#define MG_UD_ARG(ud) , ud -#define MG_CB(cb, ud) cb, ud +#define MG_UD_ARG(ud) , ud +#define MG_CB(cb, ud) cb, ud #else #define MG_UD_ARG(ud) -#define MG_CB(cb, ud) cb +#define MG_CB(cb, ud) cb #endif #endif /* CS_MONGOOSE_SRC_FEATURES_H_ */ #ifdef MG_MODULE_LINES #line 1 "mongoose/src/net_if.h" #endif + /* * Copyright (c) 2014-2016 Cesanta Software Limited * All rights reserved @@ -3149,7 +3188,7 @@ struct { \ extern "C" { #endif /* __cplusplus */ -#define MG_MAIN_IFACE 0 +#define MG_MAIN_IFACE 0 struct mg_mgr; struct mg_connection; @@ -3213,6 +3252,7 @@ struct mg_iface *mg_if_create_iface(const struct mg_iface_vtable *vtable, struct mg_iface *mg_find_iface(struct mg_mgr *mgr, const struct mg_iface_vtable *vtable, struct mg_iface *from); + /* * Deliver a new TCP connection. Returns NULL in case on error (unable to * create connection, in which case interface state should be discarded. @@ -3225,8 +3265,10 @@ void mg_if_accept_tcp_cb(struct mg_connection *nc, union socket_address *sa, /* Callback invoked by connect methods. err = 0 -> ok, != 0 -> error. */ void mg_if_connect_cb(struct mg_connection *nc, int err); + /* Callback that reports that data has been put on the wire. */ void mg_if_sent_cb(struct mg_connection *nc, int num_sent); + /* * Receive callback. * if `own` is true, buf must be heap-allocated and ownership is transferred @@ -3234,6 +3276,7 @@ void mg_if_sent_cb(struct mg_connection *nc, int num_sent); * Core will acknowledge consumption by calling iface::recved. */ void mg_if_recv_tcp_cb(struct mg_connection *nc, void *buf, int len, int own); + /* * Receive callback. * buf must be heap-allocated and ownership is transferred to the core. @@ -3258,6 +3301,7 @@ void mg_if_timer(struct mg_connection *c, double now); #ifdef MG_MODULE_LINES #line 1 "mongoose/src/ssl_if.h" #endif + /* * Copyright (c) 2014-2016 Cesanta Software Limited * All rights reserved @@ -3295,8 +3339,8 @@ struct mg_ssl_if_conn_params { }; enum mg_ssl_if_result mg_ssl_if_conn_init( - struct mg_connection *nc, const struct mg_ssl_if_conn_params *params, - const char **err_msg); + struct mg_connection *nc, const struct mg_ssl_if_conn_params *params, + const char **err_msg); enum mg_ssl_if_result mg_ssl_if_conn_accept(struct mg_connection *nc, struct mg_connection *lc); void mg_ssl_if_conn_close_notify(struct mg_connection *nc); @@ -3316,6 +3360,7 @@ int mg_ssl_if_write(struct mg_connection *nc, const void *data, size_t len); #ifdef MG_MODULE_LINES #line 1 "mongoose/src/net.h" #endif + /* * Copyright (c) 2014 Cesanta Software Limited * All rights reserved @@ -3351,15 +3396,15 @@ int mg_ssl_if_write(struct mg_connection *nc, const void *data, size_t len); /* Amalgamated: #include "common/mbuf.h" */ #ifndef MG_VPRINTF_BUFFER_SIZE -#define MG_VPRINTF_BUFFER_SIZE 100 +#define MG_VPRINTF_BUFFER_SIZE 100 #endif #ifdef MG_USE_READ_WRITE -#define MG_RECV_FUNC(s, b, l, f) read(s, b, l) -#define MG_SEND_FUNC(s, b, l, f) write(s, b, l) +#define MG_RECV_FUNC(s, b, l, f) read(s, b, l) +#define MG_SEND_FUNC(s, b, l, f) write(s, b, l) #else -#define MG_RECV_FUNC(s, b, l, f) recv(s, b, l, f) -#define MG_SEND_FUNC(s, b, l, f) send(s, b, l, f) +#define MG_RECV_FUNC(s, b, l, f) recv(s, b, l, f) +#define MG_SEND_FUNC(s, b, l, f) send(s, b, l, f) #endif #ifdef __cplusplus @@ -3386,13 +3431,13 @@ typedef void (*mg_event_handler_t)(struct mg_connection *nc, int ev, void *ev_data MG_UD_ARG(void *user_data)); /* Events. Meaning of event parameter (evp) is given in the comment. */ -#define MG_EV_POLL 0 /* Sent to each connection on each mg_mgr_poll() call */ -#define MG_EV_ACCEPT 1 /* New connection accepted. union socket_address * */ -#define MG_EV_CONNECT 2 /* connect() succeeded or failed. int * */ -#define MG_EV_RECV 3 /* Data has been received. int *num_bytes */ -#define MG_EV_SEND 4 /* Data has been written to a socket. int *num_bytes */ -#define MG_EV_CLOSE 5 /* Connection is closed. NULL */ -#define MG_EV_TIMER 6 /* now >= conn->ev_timer_time. double * */ +#define MG_EV_POLL 0 /* Sent to each connection on each mg_mgr_poll() call */ +#define MG_EV_ACCEPT 1 /* New connection accepted. union socket_address * */ +#define MG_EV_CONNECT 2 /* connect() succeeded or failed. int * */ +#define MG_EV_RECV 3 /* Data has been received. int *num_bytes */ +#define MG_EV_SEND 4 /* Data has been written to a socket. int *num_bytes */ +#define MG_EV_CLOSE 5 /* Connection is closed. NULL */ +#define MG_EV_TIMER 6 /* now >= conn->ev_timer_time. double * */ /* * Mongoose event manager. @@ -3403,9 +3448,9 @@ struct mg_mgr { const char *hexdump_file; /* Debug hexdump file path */ #endif #if MG_ENABLE_BROADCAST - sock_t ctl[2]; /* Socketpair for mg_broadcast() */ + sock_t ctl[2]; /* Socketpair for mg_broadcast() */ #endif - void *user_data; /* User data */ + void *user_data; /* User data */ int num_ifaces; struct mg_iface **ifaces; /* network interfaces */ const char *nameserver; /* DNS server to use */ @@ -3419,24 +3464,25 @@ struct mg_connection { struct mg_connection *listener; /* Set only for accept()-ed connections */ struct mg_mgr *mgr; /* Pointer to containing manager */ - sock_t sock; /* Socket to the remote peer */ + sock_t sock; /* Socket to the remote peer */ int err; - union socket_address sa; /* Remote peer address */ - size_t recv_mbuf_limit; /* Max size of recv buffer */ - struct mbuf recv_mbuf; /* Received data */ - struct mbuf send_mbuf; /* Data scheduled for sending */ - time_t last_io_time; /* Timestamp of the last socket IO */ - double ev_timer_time; /* Timestamp of the future MG_EV_TIMER */ + union socket_address sa; /* Remote peer address */ + size_t recv_mbuf_limit; /* Max size of recv buffer */ + struct mbuf recv_mbuf; /* Received data */ + struct mbuf send_mbuf; /* Data scheduled for sending */ + time_t last_io_time; /* Timestamp of the last socket IO */ + double ev_timer_time; /* Timestamp of the future MG_EV_TIMER */ #if MG_ENABLE_SSL - void *ssl_if_data; /* SSL library data. */ + void *ssl_if_data; /* SSL library data. */ #endif - mg_event_handler_t proto_handler; /* Protocol-specific event handler */ - void *proto_data; /* Protocol-specific data */ + mg_event_handler_t proto_handler; /* Protocol-specific event handler */ + void *proto_data; /* Protocol-specific data */ void (*proto_data_destructor)(void *proto_data); - mg_event_handler_t handler; /* Event handler function */ - void *user_data; /* User-specific data */ + mg_event_handler_t handler; /* Event handler function */ + void *user_data; /* User-specific data */ union { void *v; + /* * the C standard is fussy about fitting function pointers into * void pointers, since some archs might have fat pointers for functions. @@ -3448,30 +3494,30 @@ struct mg_connection { struct mg_iface *iface; unsigned long flags; /* Flags set by Mongoose */ -#define MG_F_LISTENING (1 << 0) /* This connection is listening */ -#define MG_F_UDP (1 << 1) /* This connection is UDP */ -#define MG_F_RESOLVING (1 << 2) /* Waiting for async resolver */ -#define MG_F_CONNECTING (1 << 3) /* connect() call in progress */ -#define MG_F_SSL (1 << 4) /* SSL is enabled on the connection */ -#define MG_F_SSL_HANDSHAKE_DONE (1 << 5) /* SSL hanshake has completed */ -#define MG_F_WANT_READ (1 << 6) /* SSL specific */ -#define MG_F_WANT_WRITE (1 << 7) /* SSL specific */ -#define MG_F_IS_WEBSOCKET (1 << 8) /* Websocket specific */ +#define MG_F_LISTENING (1 << 0) /* This connection is listening */ +#define MG_F_UDP (1 << 1) /* This connection is UDP */ +#define MG_F_RESOLVING (1 << 2) /* Waiting for async resolver */ +#define MG_F_CONNECTING (1 << 3) /* connect() call in progress */ +#define MG_F_SSL (1 << 4) /* SSL is enabled on the connection */ +#define MG_F_SSL_HANDSHAKE_DONE (1 << 5) /* SSL hanshake has completed */ +#define MG_F_WANT_READ (1 << 6) /* SSL specific */ +#define MG_F_WANT_WRITE (1 << 7) /* SSL specific */ +#define MG_F_IS_WEBSOCKET (1 << 8) /* Websocket specific */ /* Flags that are settable by user */ -#define MG_F_SEND_AND_CLOSE (1 << 10) /* Push remaining data and close */ -#define MG_F_CLOSE_IMMEDIATELY (1 << 11) /* Disconnect */ -#define MG_F_WEBSOCKET_NO_DEFRAG (1 << 12) /* Websocket specific */ -#define MG_F_DELETE_CHUNK (1 << 13) /* HTTP specific */ -#define MG_F_ENABLE_BROADCAST (1 << 14) /* Allow broadcast address usage */ -#define MG_F_TUN_DO_NOT_RECONNECT (1 << 15) /* Don't reconnect tunnel */ +#define MG_F_SEND_AND_CLOSE (1 << 10) /* Push remaining data and close */ +#define MG_F_CLOSE_IMMEDIATELY (1 << 11) /* Disconnect */ +#define MG_F_WEBSOCKET_NO_DEFRAG (1 << 12) /* Websocket specific */ +#define MG_F_DELETE_CHUNK (1 << 13) /* HTTP specific */ +#define MG_F_ENABLE_BROADCAST (1 << 14) /* Allow broadcast address usage */ +#define MG_F_TUN_DO_NOT_RECONNECT (1 << 15) /* Don't reconnect tunnel */ -#define MG_F_USER_1 (1 << 20) /* Flags left for application */ -#define MG_F_USER_2 (1 << 21) -#define MG_F_USER_3 (1 << 22) -#define MG_F_USER_4 (1 << 23) -#define MG_F_USER_5 (1 << 24) -#define MG_F_USER_6 (1 << 25) +#define MG_F_USER_1 (1 << 20) /* Flags left for application */ +#define MG_F_USER_2 (1 << 21) +#define MG_F_USER_3 (1 << 22) +#define MG_F_USER_4 (1 << 23) +#define MG_F_USER_5 (1 << 24) +#define MG_F_USER_6 (1 << 25) }; /* @@ -3533,6 +3579,7 @@ void mg_mgr_free(struct mg_mgr *); time_t mg_mgr_poll(struct mg_mgr *, int milli); #if MG_ENABLE_BROADCAST + /* * Passes a message of a given length to all connections. * @@ -3547,6 +3594,7 @@ time_t mg_mgr_poll(struct mg_mgr *, int milli); */ void mg_broadcast(struct mg_mgr *mgr, mg_event_handler_t cb, void *data, size_t len); + #endif /* @@ -3610,6 +3658,7 @@ struct mg_bind_opts { const char **error_string; /* Placeholder for the error string */ struct mg_iface *iface; /* Interface instance */ #if MG_ENABLE_SSL + /* * SSL settings. * @@ -3617,11 +3666,13 @@ struct mg_bind_opts { * present to tunnel dispatcher (for tunneled connections). */ const char *ssl_cert; + /* Private key corresponding to the certificate. If ssl_cert is set but * ssl_key is not, ssl_cert is used. */ const char *ssl_key; /* CA bundle used to verify client certificates or tunnel dispatchers. */ const char *ssl_ca_cert; + /* Colon-delimited list of acceptable cipher suites. * Names depend on the library used, for example: * @@ -3645,6 +3696,7 @@ struct mg_bind_opts { struct mg_connection *mg_bind(struct mg_mgr *mgr, const char *address, MG_CB(mg_event_handler_t handler, void *user_data)); + /* * Creates a listening connection. * @@ -3676,21 +3728,25 @@ struct mg_connect_opts { struct mg_iface *iface; /* Interface instance */ const char *nameserver; /* DNS server to use, NULL for default */ #if MG_ENABLE_SSL + /* * SSL settings. * Client certificate to present to the server. */ const char *ssl_cert; + /* * Private key corresponding to the certificate. * If ssl_cert is set but ssl_key is not, ssl_cert is used. */ const char *ssl_key; + /* * Verify server certificate using this CA bundle. If set to "*", then SSL * is enabled but no cert verification is performed. */ const char *ssl_ca_cert; + /* Colon-delimited list of acceptable cipher suites. * Names depend on the library used, for example: * @@ -3703,6 +3759,7 @@ struct mg_connect_opts { * If NULL, a reasonable default is used. */ const char *ssl_cipher_suites; + /* * Server name verification. If ssl_ca_cert is set and the certificate has * passed verification, its subject will be verified against this string. @@ -3711,6 +3768,7 @@ struct mg_connect_opts { * name verification. */ const char *ssl_server_name; + /* * PSK identity and key. Identity is a NUL-terminated string and key is a hex * string. Key must be either 16 or 32 bytes (32 or 64 hex digits) for AES-128 @@ -3786,6 +3844,7 @@ struct mg_connection *mg_connect_opt(struct mg_mgr *mgr, const char *address, struct mg_connect_opts opts); #if MG_ENABLE_SSL && MG_NET_IF != MG_NET_IF_SIMPLELINK + /* * Note: This function is deprecated. Please, use SSL options in * mg_connect_opt. @@ -3802,6 +3861,7 @@ struct mg_connection *mg_connect_opt(struct mg_mgr *mgr, const char *address, */ const char *mg_set_ssl(struct mg_connection *nc, const char *cert, const char *ca_cert); + #endif /* @@ -3837,6 +3897,7 @@ int mg_vprintf(struct mg_connection *, const char *fmt, va_list ap); int mg_socketpair(sock_t[2], int sock_type); #if MG_ENABLE_SYNC_RESOLVER + /* * Convert domain name into IP address. * @@ -3848,6 +3909,7 @@ int mg_socketpair(sock_t[2], int sock_type); * Return 1 on success, 0 on failure. */ int mg_resolve(const char *domain_name, char *ip_addr_buf, size_t buf_len); + #endif /* @@ -3911,6 +3973,7 @@ double mg_time(void); #ifdef MG_MODULE_LINES #line 1 "mongoose/src/uri.h" #endif + /* * Copyright (c) 2014 Cesanta Software Limited * All rights reserved @@ -3981,6 +4044,7 @@ int mg_normalize_uri_path(const struct mg_str *in, struct mg_str *out); #ifdef MG_MODULE_LINES #line 1 "mongoose/src/util.h" #endif + /* * Copyright (c) 2014 Cesanta Software Limited * All rights reserved @@ -4004,9 +4068,9 @@ extern "C" { #ifndef MG_MAX_PATH #ifdef PATH_MAX -#define MG_MAX_PATH PATH_MAX +#define MG_MAX_PATH PATH_MAX #else -#define MG_MAX_PATH 256 +#define MG_MAX_PATH 256 #endif #endif @@ -4042,6 +4106,7 @@ int mg_base64_decode(const unsigned char *s, int len, char *dst); void mg_base64_encode(const unsigned char *src, int src_len, char *dst); #if MG_ENABLE_FILESYSTEM + /* * Performs a 64-bit `stat()` call against a given file. * @@ -4086,6 +4151,7 @@ size_t mg_fwrite(const void *ptr, size_t size, size_t count, FILE *f); #endif /* MG_ENABLE_FILESYSTEM */ #if MG_ENABLE_THREADS + /* * Starts a new detached thread. * Arguments and semantics are the same as pthead's `pthread_create()`. @@ -4093,13 +4159,15 @@ size_t mg_fwrite(const void *ptr, size_t size, size_t count, FILE *f); * that is passed to the thread function. */ void *mg_start_thread(void *(*thread_func)(void *), void *thread_func_param); + #endif void mg_set_close_on_exec(sock_t); -#define MG_SOCK_STRINGIFY_IP 1 -#define MG_SOCK_STRINGIFY_PORT 2 -#define MG_SOCK_STRINGIFY_REMOTE 4 +#define MG_SOCK_STRINGIFY_IP 1 +#define MG_SOCK_STRINGIFY_PORT 2 +#define MG_SOCK_STRINGIFY_REMOTE 4 + /* * Converts a connection's local or remote address into string. * @@ -4116,9 +4184,11 @@ void mg_set_close_on_exec(sock_t); */ int mg_conn_addr_to_str(struct mg_connection *c, char *buf, size_t len, int flags); + #if MG_NET_IF == MG_NET_IF_SOCKET /* Legacy interface. */ void mg_sock_to_str(sock_t sock, char *buf, size_t len, int flags); + #endif /* @@ -4130,6 +4200,7 @@ int mg_sock_addr_to_str(const union socket_address *sa, char *buf, size_t len, int flags); #if MG_ENABLE_HEXDUMP + /* * Generates a human-readable hexdump of memory chunk. * @@ -4152,6 +4223,7 @@ void mg_hexdumpf(FILE *fp, const void *buf, int len); */ void mg_hexdump_connection(struct mg_connection *nc, const char *path, const void *buf, int num_bytes, int ev); + #endif /* @@ -4192,6 +4264,7 @@ struct mg_str mg_url_encode(const struct mg_str src); #ifdef MG_MODULE_LINES #line 1 "mongoose/src/http.h" #endif + /* * Copyright (c) 2014 Cesanta Software Limited * All rights reserved @@ -4214,19 +4287,19 @@ extern "C" { #endif /* __cplusplus */ #ifndef MG_MAX_HTTP_HEADERS -#define MG_MAX_HTTP_HEADERS 20 +#define MG_MAX_HTTP_HEADERS 20 #endif #ifndef MG_MAX_HTTP_REQUEST_SIZE -#define MG_MAX_HTTP_REQUEST_SIZE 1024 +#define MG_MAX_HTTP_REQUEST_SIZE 1024 #endif #ifndef MG_MAX_HTTP_SEND_MBUF -#define MG_MAX_HTTP_SEND_MBUF 1024 +#define MG_MAX_HTTP_SEND_MBUF 1024 #endif #ifndef MG_CGI_ENVIRONMENT_SIZE -#define MG_CGI_ENVIRONMENT_SIZE 8192 +#define MG_CGI_ENVIRONMENT_SIZE 8192 #endif /* HTTP message */ @@ -4280,30 +4353,30 @@ struct mg_http_multipart_part { struct mg_ssi_call_ctx { struct http_message *req; /* The request being processed. */ struct mg_str file; /* Filesystem path of the file being processed. */ - struct mg_str arg; /* The argument passed to the tag: . */ + struct mg_str arg; /* The argument passed to the tag: . */ }; /* HTTP and websocket events. void *ev_data is described in a comment. */ -#define MG_EV_HTTP_REQUEST 100 /* struct http_message * */ -#define MG_EV_HTTP_REPLY 101 /* struct http_message * */ -#define MG_EV_HTTP_CHUNK 102 /* struct http_message * */ -#define MG_EV_SSI_CALL 105 /* char * */ -#define MG_EV_SSI_CALL_CTX 106 /* struct mg_ssi_call_ctx * */ +#define MG_EV_HTTP_REQUEST 100 /* struct http_message * */ +#define MG_EV_HTTP_REPLY 101 /* struct http_message * */ +#define MG_EV_HTTP_CHUNK 102 /* struct http_message * */ +#define MG_EV_SSI_CALL 105 /* char * */ +#define MG_EV_SSI_CALL_CTX 106 /* struct mg_ssi_call_ctx * */ #if MG_ENABLE_HTTP_WEBSOCKET -#define MG_EV_WEBSOCKET_HANDSHAKE_REQUEST 111 /* struct http_message * */ -#define MG_EV_WEBSOCKET_HANDSHAKE_DONE 112 /* NULL */ -#define MG_EV_WEBSOCKET_FRAME 113 /* struct websocket_message * */ -#define MG_EV_WEBSOCKET_CONTROL_FRAME 114 /* struct websocket_message * */ +#define MG_EV_WEBSOCKET_HANDSHAKE_REQUEST 111 /* struct http_message * */ +#define MG_EV_WEBSOCKET_HANDSHAKE_DONE 112 /* NULL */ +#define MG_EV_WEBSOCKET_FRAME 113 /* struct websocket_message * */ +#define MG_EV_WEBSOCKET_CONTROL_FRAME 114 /* struct websocket_message * */ #endif #if MG_ENABLE_HTTP_STREAMING_MULTIPART -#define MG_EV_HTTP_MULTIPART_REQUEST 121 /* struct http_message */ -#define MG_EV_HTTP_PART_BEGIN 122 /* struct mg_http_multipart_part */ -#define MG_EV_HTTP_PART_DATA 123 /* struct mg_http_multipart_part */ -#define MG_EV_HTTP_PART_END 124 /* struct mg_http_multipart_part */ +#define MG_EV_HTTP_MULTIPART_REQUEST 121 /* struct http_message */ +#define MG_EV_HTTP_PART_BEGIN 122 /* struct mg_http_multipart_part */ +#define MG_EV_HTTP_PART_DATA 123 /* struct mg_http_multipart_part */ +#define MG_EV_HTTP_PART_END 124 /* struct mg_http_multipart_part */ /* struct mg_http_multipart_part */ -#define MG_EV_HTTP_MULTIPART_REQUEST_END 125 +#define MG_EV_HTTP_MULTIPART_REQUEST_END 125 #endif /* @@ -4361,6 +4434,7 @@ struct mg_ssi_call_ctx { void mg_set_protocol_http_websocket(struct mg_connection *nc); #if MG_ENABLE_HTTP_WEBSOCKET + /* * Send websocket handshake to the server. * @@ -4441,9 +4515,9 @@ struct mg_connection *mg_connect_ws(struct mg_mgr *mgr, * (for example, SSL parameters) */ struct mg_connection *mg_connect_ws_opt( - struct mg_mgr *mgr, MG_CB(mg_event_handler_t ev_handler, void *user_data), - struct mg_connect_opts opts, const char *url, const char *protocol, - const char *extra_headers); + struct mg_mgr *mgr, MG_CB(mg_event_handler_t ev_handler, void *user_data), + struct mg_connect_opts opts, const char *url, const char *protocol, + const char *extra_headers); /* * Send WebSocket frame to the remote end. @@ -4470,7 +4544,7 @@ void mg_send_websocket_frame(struct mg_connection *nc, int op_and_flags, * Sends multiple websocket frames. * * Like `mg_send_websocket_frame()`, but composes a frame from multiple - *buffers. + * buffers. */ void mg_send_websocket_framev(struct mg_connection *nc, int op_and_flags, const struct mg_str *strings, int num_strings); @@ -4485,12 +4559,12 @@ void mg_printf_websocket_frame(struct mg_connection *nc, int op_and_flags, const char *fmt, ...); /* Websocket opcodes, from http://tools.ietf.org/html/rfc6455 */ -#define WEBSOCKET_OP_CONTINUE 0 -#define WEBSOCKET_OP_TEXT 1 -#define WEBSOCKET_OP_BINARY 2 -#define WEBSOCKET_OP_CLOSE 8 -#define WEBSOCKET_OP_PING 9 -#define WEBSOCKET_OP_PONG 10 +#define WEBSOCKET_OP_CONTINUE 0 +#define WEBSOCKET_OP_TEXT 1 +#define WEBSOCKET_OP_BINARY 2 +#define WEBSOCKET_OP_CLOSE 8 +#define WEBSOCKET_OP_PING 9 +#define WEBSOCKET_OP_PONG 10 /* * If set causes the FIN flag to not be set on outbound @@ -4504,7 +4578,7 @@ void mg_printf_websocket_frame(struct mg_connection *nc, int op_and_flags, * Note that mongoose will automatically defragment incoming messages, * so this flag is used only on outbound messages. */ -#define WEBSOCKET_DONT_FIN 0x100 +#define WEBSOCKET_DONT_FIN 0x100 #endif /* MG_ENABLE_HTTP_WEBSOCKET */ @@ -4516,7 +4590,7 @@ void mg_printf_websocket_frame(struct mg_connection *nc, int op_and_flags, * `+` character is decoded as a blank space character. This function * guarantees to NUL-terminate the destination. If destination is too small, * then the source string is partially decoded and `-1` is returned. - *Otherwise, + * Otherwise, * a length of the decoded string is returned, not counting final NUL. */ int mg_url_decode(const char *src, int src_len, char *dst, int dst_len, @@ -4537,6 +4611,7 @@ extern void mg_hash_sha1_v(size_t num_msgs, const uint8_t *msgs[], #ifdef MG_MODULE_LINES #line 1 "mongoose/src/http_server.h" #endif + /* * === Server API reference */ @@ -4657,6 +4732,7 @@ int mg_get_http_var(const struct mg_str *buf, const char *name, char *dst, size_t dst_len); #if MG_ENABLE_FILESYSTEM + /* * This structure defines how `mg_serve_http()` works. * Best practice is to set only required settings, and leave the rest as NULL. @@ -4716,7 +4792,7 @@ struct mg_serve_http_opts { * <!--#call COMMAND --> * * Note that <!--#include ...> directive supports three path - *specifications: + * specifications: * * <!--#include virtual="path" --> Path is relative to web server root * <!--#include abspath="path" --> Path is absolute or relative to the @@ -4759,6 +4835,7 @@ struct mg_serve_http_opts { const char *ip_acl; #if MG_ENABLE_HTTP_URL_REWRITES + /* URL rewrites. * * Comma-separated list of `uri_pattern=url_file_or_directory_path` rewrites. @@ -4903,7 +4980,7 @@ typedef struct mg_str (*mg_fu_fname_fn)(struct mg_connection *nc, */ void mg_file_upload_handler(struct mg_connection *nc, int ev, void *ev_data, mg_fu_fname_fn local_name_fn - MG_UD_ARG(void *user_data)); + MG_UD_ARG(void *user_data)); #endif /* MG_ENABLE_HTTP_STREAMING_MULTIPART */ #endif /* MG_ENABLE_FILESYSTEM */ @@ -5055,6 +5132,7 @@ void mg_send_head(struct mg_connection *n, int status_code, void mg_printf_html_escape(struct mg_connection *nc, const char *fmt, ...); #if MG_ENABLE_HTTP_URL_REWRITES + /* * Proxies a given request to a given upstream http server. The path prefix * in `mount` will be stripped of the path requested to the upstream server, @@ -5068,6 +5146,7 @@ void mg_printf_html_escape(struct mg_connection *nc, const char *fmt, ...); void mg_http_reverse_proxy(struct mg_connection *nc, const struct http_message *hm, struct mg_str mount, struct mg_str upstream); + #endif #ifdef __cplusplus @@ -5080,6 +5159,7 @@ void mg_http_reverse_proxy(struct mg_connection *nc, #ifdef MG_MODULE_LINES #line 1 "mongoose/src/http_client.h" #endif + /* * === Client API reference */ @@ -5116,21 +5196,21 @@ extern "C" { * ``` */ struct mg_connection *mg_connect_http( - struct mg_mgr *mgr, - MG_CB(mg_event_handler_t event_handler, void *user_data), const char *url, - const char *extra_headers, const char *post_data); + struct mg_mgr *mgr, + MG_CB(mg_event_handler_t event_handler, void *user_data), const char *url, + const char *extra_headers, const char *post_data); /* * Helper function that creates an outbound HTTP connection. * * Mostly identical to mg_connect_http, but allows you to provide extra - *parameters + * parameters * (for example, SSL parameters) */ struct mg_connection *mg_connect_http_opt( - struct mg_mgr *mgr, MG_CB(mg_event_handler_t ev_handler, void *user_data), - struct mg_connect_opts opts, const char *url, const char *extra_headers, - const char *post_data); + struct mg_mgr *mgr, MG_CB(mg_event_handler_t ev_handler, void *user_data), + struct mg_connect_opts opts, const char *url, const char *extra_headers, + const char *post_data); /* Creates digest authentication header for a client request. */ int mg_http_create_digest_auth_header(char *buf, size_t buf_len, @@ -5145,6 +5225,7 @@ int mg_http_create_digest_auth_header(char *buf, size_t buf_len, #ifdef MG_MODULE_LINES #line 1 "mongoose/src/mqtt.h" #endif + /* * Copyright (c) 2014 Cesanta Software Limited * All rights reserved @@ -5214,62 +5295,62 @@ struct mg_mqtt_proto_data { }; /* Message types */ -#define MG_MQTT_CMD_CONNECT 1 -#define MG_MQTT_CMD_CONNACK 2 -#define MG_MQTT_CMD_PUBLISH 3 -#define MG_MQTT_CMD_PUBACK 4 -#define MG_MQTT_CMD_PUBREC 5 -#define MG_MQTT_CMD_PUBREL 6 -#define MG_MQTT_CMD_PUBCOMP 7 -#define MG_MQTT_CMD_SUBSCRIBE 8 -#define MG_MQTT_CMD_SUBACK 9 -#define MG_MQTT_CMD_UNSUBSCRIBE 10 -#define MG_MQTT_CMD_UNSUBACK 11 -#define MG_MQTT_CMD_PINGREQ 12 -#define MG_MQTT_CMD_PINGRESP 13 -#define MG_MQTT_CMD_DISCONNECT 14 +#define MG_MQTT_CMD_CONNECT 1 +#define MG_MQTT_CMD_CONNACK 2 +#define MG_MQTT_CMD_PUBLISH 3 +#define MG_MQTT_CMD_PUBACK 4 +#define MG_MQTT_CMD_PUBREC 5 +#define MG_MQTT_CMD_PUBREL 6 +#define MG_MQTT_CMD_PUBCOMP 7 +#define MG_MQTT_CMD_SUBSCRIBE 8 +#define MG_MQTT_CMD_SUBACK 9 +#define MG_MQTT_CMD_UNSUBSCRIBE 10 +#define MG_MQTT_CMD_UNSUBACK 11 +#define MG_MQTT_CMD_PINGREQ 12 +#define MG_MQTT_CMD_PINGRESP 13 +#define MG_MQTT_CMD_DISCONNECT 14 /* MQTT event types */ -#define MG_MQTT_EVENT_BASE 200 -#define MG_EV_MQTT_CONNECT (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_CONNECT) -#define MG_EV_MQTT_CONNACK (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_CONNACK) -#define MG_EV_MQTT_PUBLISH (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PUBLISH) -#define MG_EV_MQTT_PUBACK (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PUBACK) -#define MG_EV_MQTT_PUBREC (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PUBREC) -#define MG_EV_MQTT_PUBREL (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PUBREL) -#define MG_EV_MQTT_PUBCOMP (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PUBCOMP) -#define MG_EV_MQTT_SUBSCRIBE (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_SUBSCRIBE) -#define MG_EV_MQTT_SUBACK (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_SUBACK) -#define MG_EV_MQTT_UNSUBSCRIBE (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_UNSUBSCRIBE) -#define MG_EV_MQTT_UNSUBACK (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_UNSUBACK) -#define MG_EV_MQTT_PINGREQ (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PINGREQ) -#define MG_EV_MQTT_PINGRESP (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PINGRESP) -#define MG_EV_MQTT_DISCONNECT (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_DISCONNECT) +#define MG_MQTT_EVENT_BASE 200 +#define MG_EV_MQTT_CONNECT (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_CONNECT) +#define MG_EV_MQTT_CONNACK (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_CONNACK) +#define MG_EV_MQTT_PUBLISH (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PUBLISH) +#define MG_EV_MQTT_PUBACK (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PUBACK) +#define MG_EV_MQTT_PUBREC (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PUBREC) +#define MG_EV_MQTT_PUBREL (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PUBREL) +#define MG_EV_MQTT_PUBCOMP (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PUBCOMP) +#define MG_EV_MQTT_SUBSCRIBE (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_SUBSCRIBE) +#define MG_EV_MQTT_SUBACK (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_SUBACK) +#define MG_EV_MQTT_UNSUBSCRIBE (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_UNSUBSCRIBE) +#define MG_EV_MQTT_UNSUBACK (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_UNSUBACK) +#define MG_EV_MQTT_PINGREQ (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PINGREQ) +#define MG_EV_MQTT_PINGRESP (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PINGRESP) +#define MG_EV_MQTT_DISCONNECT (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_DISCONNECT) /* Message flags */ -#define MG_MQTT_RETAIN 0x1 -#define MG_MQTT_DUP 0x4 -#define MG_MQTT_QOS(qos) ((qos) << 1) -#define MG_MQTT_GET_QOS(flags) (((flags) &0x6) >> 1) -#define MG_MQTT_SET_QOS(flags, qos) (flags) = ((flags) & ~0x6) | ((qos) << 1) +#define MG_MQTT_RETAIN 0x1 +#define MG_MQTT_DUP 0x4 +#define MG_MQTT_QOS(qos) ((qos) << 1) +#define MG_MQTT_GET_QOS(flags) (((flags) & 0x6) >> 1) +#define MG_MQTT_SET_QOS(flags, qos) (flags) = ((flags) & ~0x6) | ((qos) << 1) /* Connection flags */ -#define MG_MQTT_CLEAN_SESSION 0x02 -#define MG_MQTT_HAS_WILL 0x04 -#define MG_MQTT_WILL_RETAIN 0x20 -#define MG_MQTT_HAS_PASSWORD 0x40 -#define MG_MQTT_HAS_USER_NAME 0x80 -#define MG_MQTT_GET_WILL_QOS(flags) (((flags) &0x18) >> 3) +#define MG_MQTT_CLEAN_SESSION 0x02 +#define MG_MQTT_HAS_WILL 0x04 +#define MG_MQTT_WILL_RETAIN 0x20 +#define MG_MQTT_HAS_PASSWORD 0x40 +#define MG_MQTT_HAS_USER_NAME 0x80 +#define MG_MQTT_GET_WILL_QOS(flags) (((flags) & 0x18) >> 3) #define MG_MQTT_SET_WILL_QOS(flags, qos) \ (flags) = ((flags) & ~0x18) | ((qos) << 3) /* CONNACK return codes */ -#define MG_EV_MQTT_CONNACK_ACCEPTED 0 -#define MG_EV_MQTT_CONNACK_UNACCEPTABLE_VERSION 1 -#define MG_EV_MQTT_CONNACK_IDENTIFIER_REJECTED 2 -#define MG_EV_MQTT_CONNACK_SERVER_UNAVAILABLE 3 -#define MG_EV_MQTT_CONNACK_BAD_AUTH 4 -#define MG_EV_MQTT_CONNACK_NOT_AUTHORIZED 5 +#define MG_EV_MQTT_CONNACK_ACCEPTED 0 +#define MG_EV_MQTT_CONNACK_UNACCEPTABLE_VERSION 1 +#define MG_EV_MQTT_CONNACK_IDENTIFIER_REJECTED 2 +#define MG_EV_MQTT_CONNACK_SERVER_UNAVAILABLE 3 +#define MG_EV_MQTT_CONNACK_BAD_AUTH 4 +#define MG_EV_MQTT_CONNACK_NOT_AUTHORIZED 5 #ifdef __cplusplus extern "C" { @@ -5376,6 +5457,7 @@ int mg_mqtt_vmatch_topic_expression(const char *exp, struct mg_str topic); #ifdef MG_MODULE_LINES #line 1 "mongoose/src/mqtt_server.h" #endif + /* * Copyright (c) 2014 Cesanta Software Limited * All rights reserved @@ -5410,7 +5492,7 @@ extern "C" { #endif /* __cplusplus */ #ifndef MG_MQTT_MAX_SESSION_SUBSCRIPTIONS -#define MG_MQTT_MAX_SESSION_SUBSCRIPTIONS 512 +#define MG_MQTT_MAX_SESSION_SUBSCRIPTIONS 512 #endif struct mg_mqtt_broker; @@ -5483,6 +5565,7 @@ struct mg_mqtt_session *mg_mqtt_next(struct mg_mqtt_broker *brk, #ifdef MG_MODULE_LINES #line 1 "mongoose/src/dns.h" #endif + /* * Copyright (c) 2014 Cesanta Software Limited * All rights reserved @@ -5501,20 +5584,20 @@ struct mg_mqtt_session *mg_mqtt_next(struct mg_mqtt_broker *brk, extern "C" { #endif /* __cplusplus */ -#define MG_DNS_A_RECORD 0x01 /* Lookup IP address */ -#define MG_DNS_CNAME_RECORD 0x05 /* Lookup CNAME */ -#define MG_DNS_PTR_RECORD 0x0c /* Lookup PTR */ -#define MG_DNS_TXT_RECORD 0x10 /* Lookup TXT */ -#define MG_DNS_AAAA_RECORD 0x1c /* Lookup IPv6 address */ -#define MG_DNS_SRV_RECORD 0x21 /* Lookup SRV */ -#define MG_DNS_MX_RECORD 0x0f /* Lookup mail server for domain */ -#define MG_DNS_ANY_RECORD 0xff -#define MG_DNS_NSEC_RECORD 0x2f +#define MG_DNS_A_RECORD 0x01 /* Lookup IP address */ +#define MG_DNS_CNAME_RECORD 0x05 /* Lookup CNAME */ +#define MG_DNS_PTR_RECORD 0x0c /* Lookup PTR */ +#define MG_DNS_TXT_RECORD 0x10 /* Lookup TXT */ +#define MG_DNS_AAAA_RECORD 0x1c /* Lookup IPv6 address */ +#define MG_DNS_SRV_RECORD 0x21 /* Lookup SRV */ +#define MG_DNS_MX_RECORD 0x0f /* Lookup mail server for domain */ +#define MG_DNS_ANY_RECORD 0xff +#define MG_DNS_NSEC_RECORD 0x2f -#define MG_MAX_DNS_QUESTIONS 32 -#define MG_MAX_DNS_ANSWERS 32 +#define MG_MAX_DNS_QUESTIONS 32 +#define MG_MAX_DNS_ANSWERS 32 -#define MG_DNS_MESSAGE 100 /* High-level DNS message event */ +#define MG_DNS_MESSAGE 100 /* High-level DNS message event */ enum mg_dns_resource_record_kind { MG_DNS_INVALID_RECORD = 0, @@ -5544,7 +5627,7 @@ struct mg_dns_message { }; struct mg_dns_resource_record *mg_dns_next_record( - struct mg_dns_message *msg, int query, struct mg_dns_resource_record *prev); + struct mg_dns_message *msg, int query, struct mg_dns_resource_record *prev); /* * Parses the record data from a DNS resource record. @@ -5650,6 +5733,7 @@ void mg_set_protocol_dns(struct mg_connection *nc); #ifdef MG_MODULE_LINES #line 1 "mongoose/src/dns_server.h" #endif + /* * Copyright (c) 2014 Cesanta Software Limited * All rights reserved @@ -5672,7 +5756,7 @@ void mg_set_protocol_dns(struct mg_connection *nc); extern "C" { #endif /* __cplusplus */ -#define MG_DNS_SERVER_DEFAULT_TTL 3600 +#define MG_DNS_SERVER_DEFAULT_TTL 3600 struct mg_dns_reply { struct mg_dns_message *msg; @@ -5747,6 +5831,7 @@ void mg_dns_send_reply(struct mg_connection *nc, struct mg_dns_reply *r); #ifdef MG_MODULE_LINES #line 1 "mongoose/src/resolv.h" #endif + /* * Copyright (c) 2014 Cesanta Software Limited * All rights reserved @@ -5778,10 +5863,10 @@ typedef void (*mg_resolve_callback_t)(struct mg_dns_message *dns_message, /* Options for `mg_resolve_async_opt`. */ struct mg_resolve_async_opts { const char *nameserver; - int max_retries; /* defaults to 2 if zero */ - int timeout; /* in seconds; defaults to 5 if zero */ - int accept_literal; /* pseudo-resolve literal ipv4 and ipv6 addrs */ - int only_literal; /* only resolves literal addrs; sync cb invocation */ + int max_retries; /* defaults to 2 if zero */ + int timeout; /* in seconds; defaults to 5 if zero */ + int accept_literal; /* pseudo-resolve literal ipv4 and ipv6 addrs */ + int only_literal; /* only resolves literal addrs; sync cb invocation */ struct mg_connection **dns_conn; /* return DNS connection */ }; @@ -5831,6 +5916,7 @@ int mg_resolve_from_hosts_file(const char *host, union socket_address *usa); #ifdef MG_MODULE_LINES #line 1 "mongoose/src/coap.h" #endif + /* * Copyright (c) 2015 Cesanta Software Limited * All rights reserved @@ -5867,36 +5953,36 @@ int mg_resolve_from_hosts_file(const char *host, union socket_address *usa); #if MG_ENABLE_COAP -#define MG_COAP_MSG_TYPE_FIELD 0x2 -#define MG_COAP_CODE_CLASS_FIELD 0x4 -#define MG_COAP_CODE_DETAIL_FIELD 0x8 -#define MG_COAP_MSG_ID_FIELD 0x10 -#define MG_COAP_TOKEN_FIELD 0x20 -#define MG_COAP_OPTIOMG_FIELD 0x40 -#define MG_COAP_PAYLOAD_FIELD 0x80 +#define MG_COAP_MSG_TYPE_FIELD 0x2 +#define MG_COAP_CODE_CLASS_FIELD 0x4 +#define MG_COAP_CODE_DETAIL_FIELD 0x8 +#define MG_COAP_MSG_ID_FIELD 0x10 +#define MG_COAP_TOKEN_FIELD 0x20 +#define MG_COAP_OPTIOMG_FIELD 0x40 +#define MG_COAP_PAYLOAD_FIELD 0x80 -#define MG_COAP_ERROR 0x10000 -#define MG_COAP_FORMAT_ERROR (MG_COAP_ERROR | 0x20000) -#define MG_COAP_IGNORE (MG_COAP_ERROR | 0x40000) -#define MG_COAP_NOT_ENOUGH_DATA (MG_COAP_ERROR | 0x80000) -#define MG_COAP_NETWORK_ERROR (MG_COAP_ERROR | 0x100000) +#define MG_COAP_ERROR 0x10000 +#define MG_COAP_FORMAT_ERROR (MG_COAP_ERROR | 0x20000) +#define MG_COAP_IGNORE (MG_COAP_ERROR | 0x40000) +#define MG_COAP_NOT_ENOUGH_DATA (MG_COAP_ERROR | 0x80000) +#define MG_COAP_NETWORK_ERROR (MG_COAP_ERROR | 0x100000) -#define MG_COAP_MSG_CON 0 -#define MG_COAP_MSG_NOC 1 -#define MG_COAP_MSG_ACK 2 -#define MG_COAP_MSG_RST 3 -#define MG_COAP_MSG_MAX 3 +#define MG_COAP_MSG_CON 0 +#define MG_COAP_MSG_NOC 1 +#define MG_COAP_MSG_ACK 2 +#define MG_COAP_MSG_RST 3 +#define MG_COAP_MSG_MAX 3 -#define MG_COAP_CODECLASS_REQUEST 0 -#define MG_COAP_CODECLASS_RESP_OK 2 -#define MG_COAP_CODECLASS_CLIENT_ERR 4 -#define MG_COAP_CODECLASS_SRV_ERR 5 +#define MG_COAP_CODECLASS_REQUEST 0 +#define MG_COAP_CODECLASS_RESP_OK 2 +#define MG_COAP_CODECLASS_CLIENT_ERR 4 +#define MG_COAP_CODECLASS_SRV_ERR 5 -#define MG_COAP_EVENT_BASE 300 -#define MG_EV_COAP_CON (MG_COAP_EVENT_BASE + MG_COAP_MSG_CON) -#define MG_EV_COAP_NOC (MG_COAP_EVENT_BASE + MG_COAP_MSG_NOC) -#define MG_EV_COAP_ACK (MG_COAP_EVENT_BASE + MG_COAP_MSG_ACK) -#define MG_EV_COAP_RST (MG_COAP_EVENT_BASE + MG_COAP_MSG_RST) +#define MG_COAP_EVENT_BASE 300 +#define MG_EV_COAP_CON (MG_COAP_EVENT_BASE + MG_COAP_MSG_CON) +#define MG_EV_COAP_NOC (MG_COAP_EVENT_BASE + MG_COAP_MSG_NOC) +#define MG_EV_COAP_ACK (MG_COAP_EVENT_BASE + MG_COAP_MSG_ACK) +#define MG_EV_COAP_RST (MG_COAP_EVENT_BASE + MG_COAP_MSG_RST) /* * CoAP options. @@ -5999,6 +6085,7 @@ uint32_t mg_coap_compose(struct mg_coap_message *cm, struct mbuf *io); #ifdef MG_MODULE_LINES #line 1 "mongoose/src/sntp.h" #endif + /* * Copyright (c) 2016 Cesanta Software Limited * All rights reserved @@ -6009,19 +6096,19 @@ uint32_t mg_coap_compose(struct mg_coap_message *cm, struct mbuf *io); #if MG_ENABLE_SNTP -#define MG_SNTP_EVENT_BASE 500 +#define MG_SNTP_EVENT_BASE 500 /* * Received reply from time server. Event handler parameter contains * pointer to mg_sntp_message structure */ -#define MG_SNTP_REPLY (MG_SNTP_EVENT_BASE + 1) +#define MG_SNTP_REPLY (MG_SNTP_EVENT_BASE + 1) /* Received malformed SNTP packet */ -#define MG_SNTP_MALFORMED_REPLY (MG_SNTP_EVENT_BASE + 2) +#define MG_SNTP_MALFORMED_REPLY (MG_SNTP_EVENT_BASE + 2) /* Failed to get time from server (timeout etc) */ -#define MG_SNTP_FAILED (MG_SNTP_EVENT_BASE + 3) +#define MG_SNTP_FAILED (MG_SNTP_EVENT_BASE + 3) struct mg_sntp_message { /* if server sends this flags, user should not send requests to it */ @@ -6056,6 +6143,7 @@ struct mg_connection *mg_sntp_get_time(struct mg_mgr *mgr, #ifdef MG_MODULE_LINES #line 1 "mongoose/src/socks.h" #endif + /* * Copyright (c) 2017 Cesanta Software Limited * All rights reserved @@ -6066,10 +6154,10 @@ struct mg_connection *mg_sntp_get_time(struct mg_mgr *mgr, #if MG_ENABLE_SOCKS -#define MG_SOCKS_VERSION 5 +#define MG_SOCKS_VERSION 5 -#define MG_SOCKS_HANDSHAKE_DONE MG_F_USER_1 -#define MG_SOCKS_CONNECT_DONE MG_F_USER_2 +#define MG_SOCKS_HANDSHAKE_DONE MG_F_USER_1 +#define MG_SOCKS_CONNECT_DONE MG_F_USER_2 /* SOCKS5 handshake methods */ enum mg_socks_handshake_method { diff --git a/unittest/test.h b/unittest/test.h index 90c3ac8..a519e4b 100644 --- a/unittest/test.h +++ b/unittest/test.h @@ -10,13 +10,13 @@ extern int test_failures; extern int assert_count; -#define ASSERT(expr, errstr) \ - do { \ - if (!(expr)) { \ - LOG(LL_ERROR, ("ASSERT FAIL: "errstr)); \ - test_failures++; \ - } \ - assert_count++; \ +#define ASSERT(expr, errstr) \ + do { \ + if (!(expr)) { \ + LOG(LL_ERROR, ("ASSERT FAIL: "errstr)); \ + test_failures++; \ + } \ + assert_count++; \ } while (0) diff --git a/unittest/test_buttons.c b/unittest/test_buttons.c index f05b71f..953842c 100644 --- a/unittest/test_buttons.c +++ b/unittest/test_buttons.c @@ -5,5 +5,3 @@ int test_buttons() { channel_init("testdata/testconfig1.json"); return 0; } - -