Formatting

This commit is contained in:
Pim van Pelt
2018-04-17 15:00:29 +02:00
parent 8bf7b919cc
commit 491ba88d1a
25 changed files with 2290 additions and 1956 deletions

View File

@ -20,6 +20,7 @@ extern "C" {
bool mgos_ota_http_client_init(void); bool mgos_ota_http_client_init(void);
void mgos_ota_http_start(struct update_context *ctx, const char *url); void mgos_ota_http_start(struct update_context *ctx, const char *url);
#endif #endif
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -17,18 +17,22 @@
static void fw_download_handler(struct mg_connection *c, int ev, void *p, static void fw_download_handler(struct mg_connection *c, int ev, void *p,
void *user_data) { void *user_data) {
struct mbuf *io = &c->recv_mbuf; struct mbuf * io = &c->recv_mbuf;
struct update_context *ctx = (struct update_context *) user_data; struct update_context *ctx = (struct update_context *)user_data;
int res = 0; int res = 0;
struct mg_str *loc; struct mg_str *loc;
(void) p;
(void)p;
switch (ev) { switch (ev) {
case MG_EV_CONNECT: { case MG_EV_CONNECT: {
int result = *((int *) p); int result = *((int *)p);
if (result != 0) LOG(LL_ERROR, ("connect error: %d", result)); if (result != 0) {
LOG(LL_ERROR, ("connect error: %d", result));
}
break; break;
} }
case MG_EV_RECV: { case MG_EV_RECV: {
if (ctx->file_size == 0) { if (ctx->file_size == 0) {
LOG(LL_DEBUG, ("Looking for HTTP header")); LOG(LL_DEBUG, ("Looking for HTTP header"));
@ -47,7 +51,8 @@ static void fw_download_handler(struct mg_connection *c, int ev, void *p,
(loc = mg_get_http_header(&hm, "Location")) != NULL) { (loc = mg_get_http_header(&hm, "Location")) != NULL) {
/* NUL-terminate the URL. Every header must be followed by \r\n, /* NUL-terminate the URL. Every header must be followed by \r\n,
* so there is deifnitely space there. */ * so there is deifnitely space there. */
((char *) loc->p)[loc->len] = '\0'; ((char *)loc->p)[loc->len] = '\0';
/* We were told to look elsewhere. Detach update context from this /* We were told to look elsewhere. Detach update context from this
* connection so that it doesn't get finalized when it's closed. */ * connection so that it doesn't get finalized when it's closed. */
mgos_ota_http_start(ctx, loc->p); mgos_ota_http_start(ctx, loc->p);
@ -62,7 +67,7 @@ static void fw_download_handler(struct mg_connection *c, int ev, void *p,
return; return;
} }
if (hm.body.len != 0) { if (hm.body.len != 0) {
LOG(LL_DEBUG, ("HTTP header: file size: %d", (int) hm.body.len)); LOG(LL_DEBUG, ("HTTP header: file size: %d", (int)hm.body.len));
if (hm.body.len == (size_t) ~0) { if (hm.body.len == (size_t) ~0) {
LOG(LL_ERROR, ("Invalid content-length, perhaps chunked-encoding")); LOG(LL_ERROR, ("Invalid content-length, perhaps chunked-encoding"));
ctx->status_msg = ctx->status_msg =
@ -82,7 +87,9 @@ static void fw_download_handler(struct mg_connection *c, int ev, void *p,
mbuf_remove(io, io->len); mbuf_remove(io, io->len);
if (res == 0) { if (res == 0) {
if (is_write_finished(ctx)) res = updater_finalize(ctx); if (is_write_finished(ctx)) {
res = updater_finalize(ctx);
}
if (res == 0) { if (res == 0) {
/* Need more data, everything is OK */ /* Need more data, everything is OK */
break; break;
@ -97,14 +104,21 @@ static void fw_download_handler(struct mg_connection *c, int ev, void *p,
} }
break; break;
} }
case MG_EV_CLOSE: {
if (ctx == NULL) break;
if (is_write_finished(ctx)) updater_finalize(ctx); case MG_EV_CLOSE: {
if (ctx == NULL) {
break;
}
if (is_write_finished(ctx)) {
updater_finalize(ctx);
}
if (!is_update_finished(ctx)) { if (!is_update_finished(ctx)) {
/* Update failed or connection was terminated by server */ /* Update failed or connection was terminated by server */
if (ctx->status_msg == NULL) ctx->status_msg = "Update failed"; if (ctx->status_msg == NULL) {
ctx->status_msg = "Update failed";
}
ctx->result = -1; ctx->result = -1;
} else if (is_reboot_required(ctx)) { } else if (is_reboot_required(ctx)) {
LOG(LL_INFO, ("Rebooting device")); LOG(LL_INFO, ("Rebooting device"));
@ -147,7 +161,9 @@ void mgos_ota_http_start(struct update_context *ctx, const char *url) {
struct mg_connection *c = mg_connect_http_opt( 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) { if (c == NULL) {
LOG(LL_ERROR, ("Failed to connect to %s", url)); LOG(LL_ERROR, ("Failed to connect to %s", url));
@ -163,18 +179,24 @@ void mgos_ota_http_start(struct update_context *ctx, const char *url) {
static void mgos_ota_timer_cb(void *arg) { static void mgos_ota_timer_cb(void *arg) {
const struct mgos_config_update *mcu = mgos_sys_config_get_update(); 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(); struct update_context *ctx = updater_context_create();
if (ctx == NULL) return; if (ctx == NULL) {
return;
}
ctx->ignore_same_version = true; ctx->ignore_same_version = true;
ctx->fctx.commit_timeout = mcu->commit_timeout; ctx->fctx.commit_timeout = mcu->commit_timeout;
mgos_ota_http_start(ctx, mcu->url); mgos_ota_http_start(ctx, mcu->url);
(void) arg; (void)arg;
} }
bool mgos_ota_http_client_init(void) { bool mgos_ota_http_client_init(void) {
const struct mgos_config_update *mcu = mgos_sys_config_get_update(); const struct mgos_config_update *mcu = mgos_sys_config_get_update();
if (mcu->url != NULL && mcu->interval > 0) { if (mcu->url != NULL && mcu->interval > 0) {
LOG(LL_INFO, LOG(LL_INFO,
("Updates from %s, every %d seconds", mcu->url, mcu->interval)); ("Updates from %s, every %d seconds", mcu->url, mcu->interval));

View File

@ -14,6 +14,7 @@ extern "C" {
#if MGOS_ENABLE_UPDATER #if MGOS_ENABLE_UPDATER
bool mgos_ota_http_server_init(void); bool mgos_ota_http_server_init(void);
#endif #endif
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -16,9 +16,12 @@
#include "mgos_utils.h" #include "mgos_utils.h"
static void handle_update_post(struct mg_connection *c, int ev, void *p) { 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 mg_http_multipart_part *mp = (struct mg_http_multipart_part *)p;
struct update_context *ctx = (struct update_context *) c->user_data; struct update_context * ctx = (struct update_context *)c->user_data;
if (ctx == NULL && ev != MG_EV_HTTP_MULTIPART_REQUEST) return;
if (ctx == NULL && ev != MG_EV_HTTP_MULTIPART_REQUEST) {
return;
}
switch (ev) { switch (ev) {
case MG_EV_HTTP_MULTIPART_REQUEST: { case MG_EV_HTTP_MULTIPART_REQUEST: {
ctx = updater_context_create(); ctx = updater_context_create();
@ -30,6 +33,7 @@ static void handle_update_post(struct mg_connection *c, int ev, void *p) {
} }
break; break;
} }
case MG_EV_HTTP_PART_BEGIN: { case MG_EV_HTTP_PART_BEGIN: {
LOG(LL_DEBUG, ("MG_EV_HTTP_PART_BEGIN: %p %s %s", ctx, mp->var_name, LOG(LL_DEBUG, ("MG_EV_HTTP_PART_BEGIN: %p %s %s", ctx, mp->var_name,
mp->file_name)); mp->file_name));
@ -39,9 +43,10 @@ static void handle_update_post(struct mg_connection *c, int ev, void *p) {
} }
break; break;
} }
case MG_EV_HTTP_PART_DATA: { case MG_EV_HTTP_PART_DATA: {
LOG(LL_DEBUG, ("MG_EV_HTTP_PART_DATA: %p %s %s %d", ctx, mp->var_name, LOG(LL_DEBUG, ("MG_EV_HTTP_PART_DATA: %p %s %s %d", ctx, mp->var_name,
mp->file_name, (int) mp->data.len)); mp->file_name, (int)mp->data.len));
if (mp->file_name[0] == '\0') { if (mp->file_name[0] == '\0') {
/* It's a non-file form variable. */ /* It's a non-file form variable. */
@ -57,11 +62,14 @@ static void handle_update_post(struct mg_connection *c, int ev, void *p) {
} }
break; break;
} }
case MG_EV_HTTP_PART_END: { case MG_EV_HTTP_PART_END: {
LOG(LL_DEBUG, ("MG_EV_HTTP_PART_END: %p %s %s %d", ctx, mp->var_name, LOG(LL_DEBUG, ("MG_EV_HTTP_PART_END: %p %s %s %d", ctx, mp->var_name,
mp->file_name, mp->status)); mp->file_name, mp->status));
/* Part finished with an error. REQUEST_END will follow. */ /* Part finished with an error. REQUEST_END will follow. */
if (mp->status < 0) break; if (mp->status < 0) {
break;
}
if (mp->file_name[0] == '\0') { if (mp->file_name[0] == '\0') {
/* It's a non-file form variable. Value is in ctx->file_name. */ /* 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)); LOG(LL_DEBUG, ("Got var: %s=%s", mp->var_name, ctx->file_name));
@ -75,14 +83,19 @@ static void handle_update_post(struct mg_connection *c, int ev, void *p) {
} }
break; break;
} }
case MG_EV_HTTP_MULTIPART_REQUEST_END: { case MG_EV_HTTP_MULTIPART_REQUEST_END: {
LOG(LL_DEBUG, LOG(LL_DEBUG,
("MG_EV_HTTP_MULTIPART_REQUEST_END: %p %d", ctx, mp->status)); ("MG_EV_HTTP_MULTIPART_REQUEST_END: %p %d", ctx, mp->status));
/* Whatever happens, this is the last thing we do. */ /* Whatever happens, this is the last thing we do. */
c->flags |= MG_F_SEND_AND_CLOSE; c->flags |= MG_F_SEND_AND_CLOSE;
if (ctx == NULL) break; if (ctx == NULL) {
if (is_write_finished(ctx)) updater_finalize(ctx); break;
}
if (is_write_finished(ctx)) {
updater_finalize(ctx);
}
if (!is_update_finished(ctx)) { if (!is_update_finished(ctx)) {
ctx->result = -1; ctx->result = -1;
ctx->status_msg = "Update aborted"; ctx->status_msg = "Update aborted";
@ -113,7 +126,9 @@ static void handle_update_post(struct mg_connection *c, int ev, void *p) {
struct mg_connection *s_update_request_conn; struct mg_connection *s_update_request_conn;
static void mgos_ota_result_cb(struct update_context *ctx) { 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) { if (s_update_request_conn != NULL) {
int code = (ctx->result > 0 ? 200 : 500); int code = (ctx->result > 0 ? 200 : 500);
mg_send_response_line(s_update_request_conn, code, mg_send_response_line(s_update_request_conn, code,
@ -145,8 +160,9 @@ static void update_handler(struct mg_connection *c, int ev, void *ev_data,
} }
return; return;
} }
case MG_EV_HTTP_REQUEST: { case MG_EV_HTTP_REQUEST: {
struct http_message *hm = (struct http_message *) ev_data; struct http_message *hm = (struct http_message *)ev_data;
if (updater_context_get_current() != NULL) { if (updater_context_get_current() != NULL) {
mg_send_response_line(c, 409, mg_send_response_line(c, 409,
"Content-Type: text/plain\r\n" "Content-Type: text/plain\r\n"
@ -156,13 +172,13 @@ static void update_handler(struct mg_connection *c, int ev, void *ev_data,
return; return;
} }
const struct mgos_config_update *mcu = mgos_sys_config_get_update(); const struct mgos_config_update *mcu = mgos_sys_config_get_update();
char *url = mcu->url; char * url = mcu->url;
int commit_timeout = mcu->commit_timeout; int commit_timeout = mcu->commit_timeout;
bool ignore_same_version = true; bool ignore_same_version = true;
struct mg_str params = struct mg_str params =
(mg_vcmp(&hm->method, "POST") == 0 ? hm->body : hm->query_string); (mg_vcmp(&hm->method, "POST") == 0 ? hm->body : hm->query_string);
size_t buf_len = params.len; size_t buf_len = params.len;
char *buf = calloc(params.len, 1), *p = buf; char * buf = calloc(params.len, 1), *p = buf;
int len = mg_get_http_var(&params, "url", p, buf_len); int len = mg_get_http_var(&params, "url", p, buf_len);
if (len > 0) { if (len > 0) {
url = p; url = p;
@ -192,7 +208,6 @@ static void update_handler(struct mg_connection *c, int ev, void *ev_data,
ctx->fctx.commit_timeout = commit_timeout; ctx->fctx.commit_timeout = commit_timeout;
ctx->result_cb = mgos_ota_result_cb; ctx->result_cb = mgos_ota_result_cb;
mgos_ota_http_start(ctx, url); mgos_ota_http_start(ctx, url);
} else { } else {
mg_send_response_line(c, 400, mg_send_response_line(c, 400,
"Content-Type: text/plain\r\n" "Content-Type: text/plain\r\n"
@ -203,6 +218,7 @@ static void update_handler(struct mg_connection *c, int ev, void *ev_data,
free(buf); free(buf);
break; break;
} }
case MG_EV_CLOSE: { case MG_EV_CLOSE: {
if (s_update_request_conn == c) { if (s_update_request_conn == c) {
/* Client went away while waiting for response. */ /* Client went away while waiting for response. */
@ -211,13 +227,15 @@ static void update_handler(struct mg_connection *c, int ev, void *ev_data,
break; break;
} }
} }
(void) user_data; (void)user_data;
} }
static void update_action_handler(struct mg_connection *c, int ev, void *p, static void update_action_handler(struct mg_connection *c, int ev, void *p,
void *user_data) { void *user_data) {
if (ev != MG_EV_HTTP_REQUEST) return; if (ev != MG_EV_HTTP_REQUEST) {
struct http_message *hm = (struct http_message *) p; return;
}
struct http_message *hm = (struct http_message *)p;
bool is_commit = (mg_vcmp(&hm->uri, "/update/commit") == 0); bool is_commit = (mg_vcmp(&hm->uri, "/update/commit") == 0);
bool ok = bool ok =
(is_commit ? mgos_upd_commit() : mgos_upd_revert(false /* reboot */)); (is_commit ? mgos_upd_commit() : mgos_upd_revert(false /* reboot */));
@ -226,8 +244,10 @@ static void update_action_handler(struct mg_connection *c, int ev, void *p,
"Connection: close"); "Connection: close");
mg_printf(c, "\r\n%s\r\n", (ok ? "Ok" : "Error")); mg_printf(c, "\r\n%s\r\n", (ok ? "Ok" : "Error"));
c->flags |= MG_F_SEND_AND_CLOSE; c->flags |= MG_F_SEND_AND_CLOSE;
if (ok && !is_commit) mgos_system_restart_after(100); if (ok && !is_commit) {
(void) user_data; mgos_system_restart_after(100);
}
(void)user_data;
} }
bool mgos_ota_http_server_init(void) { bool mgos_ota_http_server_init(void) {

View File

@ -1,7 +1,7 @@
/* /*
* Copyright (c) 2016 Cesanta Software Limited * Copyright (c) 2016 Cesanta Software Limited
* All rights reserved * All rights reserved
*/ */
#ifndef CS_FW_SRC_MGOS_UPDATER_MG_RPC_H_ #ifndef CS_FW_SRC_MGOS_UPDATER_MG_RPC_H_
#define CS_FW_SRC_MGOS_UPDATER_MG_RPC_H_ #define CS_FW_SRC_MGOS_UPDATER_MG_RPC_H_

View File

@ -18,7 +18,9 @@
static struct mg_rpc_request_info *s_update_req; static struct mg_rpc_request_info *s_update_req;
static void mg_rpc_updater_result(struct update_context *ctx) { 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); mg_rpc_send_errorf(s_update_req, (ctx->result > 0 ? 0 : -1), ctx->status_msg);
s_update_req = NULL; 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, static void handle_update_req(struct mg_rpc_request_info *ri, void *cb_arg,
struct mg_rpc_frame_info *fi, struct mg_rpc_frame_info *fi,
struct mg_str args) { struct mg_str args) {
char *blob_url = NULL; char * blob_url = NULL;
struct json_token url_tok = JSON_INVALID_TOKEN; struct json_token url_tok = JSON_INVALID_TOKEN;
int commit_timeout = 0; int commit_timeout = 0;
struct update_context *ctx = NULL; 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"; 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); 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, LOG(LL_DEBUG, ("URL: %.*s commit_timeout: %d", url_tok.len, url_tok.ptr,
commit_timeout)); commit_timeout));
@ -73,12 +77,14 @@ static void handle_update_req(struct mg_rpc_request_info *ri, void *cb_arg,
return; return;
clean: clean:
if (blob_url != NULL) free(blob_url); if (blob_url != NULL) {
free(blob_url);
}
LOG(LL_ERROR, ("Failed to start update: %s", reply)); LOG(LL_ERROR, ("Failed to start update: %s", reply));
mg_rpc_send_errorf(ri, -1, reply); mg_rpc_send_errorf(ri, -1, reply);
ri = NULL; ri = NULL;
(void) cb_arg; (void)cb_arg;
(void) fi; (void)fi;
} }
static void handle_commit_req(struct mg_rpc_request_info *ri, void *cb_arg, 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); mg_rpc_send_errorf(ri, -1, NULL);
} }
ri = NULL; ri = NULL;
(void) cb_arg; (void)cb_arg;
(void) fi; (void)fi;
(void) args; (void)args;
} }
static void handle_revert_req(struct mg_rpc_request_info *ri, void *cb_arg, 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); mg_rpc_send_errorf(ri, -1, NULL);
} }
ri = NULL; ri = NULL;
(void) cb_arg; (void)cb_arg;
(void) fi; (void)fi;
(void) args; (void)args;
} }
static void handle_create_snapshot_req(struct mg_rpc_request_info *ri, static void handle_create_snapshot_req(struct mg_rpc_request_info *ri,
@ -116,6 +122,7 @@ static void handle_create_snapshot_req(struct mg_rpc_request_info *ri,
struct mg_str args) { struct mg_str args) {
const char *err_msg = NULL; const char *err_msg = NULL;
int ret = -1; int ret = -1;
if (mgos_upd_is_committed()) { if (mgos_upd_is_committed()) {
ret = mgos_upd_create_snapshot(); ret = mgos_upd_create_snapshot();
if (ret >= 0) { if (ret >= 0) {
@ -155,8 +162,8 @@ static void handle_create_snapshot_req(struct mg_rpc_request_info *ri,
} else { } else {
mg_rpc_send_errorf(ri, ret, err_msg); mg_rpc_send_errorf(ri, ret, err_msg);
} }
(void) cb_arg; (void)cb_arg;
(void) fi; (void)fi;
} }
static void handle_get_boot_state_req(struct mg_rpc_request_info *ri, 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_rpc_frame_info *fi,
struct mg_str args) { struct mg_str args) {
struct mgos_upd_boot_state bs; struct mgos_upd_boot_state bs;
if (!mgos_upd_boot_get_state(&bs)) { if (!mgos_upd_boot_get_state(&bs)) {
mg_rpc_send_errorf(ri, -1, NULL); mg_rpc_send_errorf(ri, -1, NULL);
} else { } 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, bs.active_slot, bs.is_committed, bs.revert_slot,
mgos_upd_get_commit_timeout()); mgos_upd_get_commit_timeout());
} }
(void) cb_arg; (void)cb_arg;
(void) fi; (void)fi;
(void) args; (void)args;
} }
static void handle_set_boot_state_req(struct mg_rpc_request_info *ri, 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) { struct mg_str args) {
int ret = 0; int ret = 0;
struct mgos_upd_boot_state bs; struct mgos_upd_boot_state bs;
if (mgos_upd_boot_get_state(&bs)) { if (mgos_upd_boot_get_state(&bs)) {
int commit_timeout = -1; int commit_timeout = -1;
if (json_scanf(args.p, args.len, ri->args_fmt, &bs.active_slot, 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 { } else {
mg_rpc_send_errorf(ri, ret, NULL); mg_rpc_send_errorf(ri, ret, NULL);
} }
(void) cb_arg; (void)cb_arg;
(void) fi; (void)fi;
} }
bool mgos_rpc_service_ota_init(void) { bool mgos_rpc_service_ota_init(void) {
struct mg_rpc *mg_rpc = mgos_rpc_get_global(); 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}", mg_rpc_add_handler(mg_rpc, "OTA.Update", "{url: %T, commit_timeout: %d}",
handle_update_req, NULL); handle_update_req, NULL);
mg_rpc_add_handler(mg_rpc, "OTA.Commit", "", handle_commit_req, NULL); mg_rpc_add_handler(mg_rpc, "OTA.Commit", "", handle_commit_req, NULL);

View File

@ -3,13 +3,15 @@
#include "frozen/frozen.h" #include "frozen/frozen.h"
static struct channel_t s_channels[CHANNEL_MAX]; 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) { static bool valid_gpio(const int gpio) {
if (gpio == -1) if (gpio == -1) {
return true; return true;
if (gpio < GPIO_MIN || gpio > GPIO_MAX) }
if (gpio < GPIO_MIN || gpio > GPIO_MAX) {
return false; return false;
}
return true; return true;
} }
@ -18,8 +20,8 @@ int channel_get_total() {
} }
bool channel_init(const char *fn) { bool channel_init(const char *fn) {
char *json; char * json;
void *h = NULL; void * h = NULL;
struct json_token val; struct json_token val;
bool ret = false; bool ret = false;
@ -30,7 +32,7 @@ bool channel_init(const char *fn) {
int idx; int idx;
memset(s_channels, -1, sizeof(s_channels)); memset(s_channels, -1, sizeof(s_channels));
s_num_channels=0; s_num_channels = 0;
json = json_fread(fn); json = json_fread(fn);
if (!json) { if (!json) {
@ -54,11 +56,12 @@ bool channel_init(const char *fn) {
// Traverse Array // Traverse Array
while ((h = json_next_elem(json, strlen(json), h, ".channels", &idx, &val)) != NULL) { 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)); LOG(LL_DEBUG, ("[%d]: [%.*s]", idx, val.len, val.ptr));
if (val.len==0 || !val.ptr) if (val.len == 0 || !val.ptr) {
continue; continue;
}
if (json_scanf(val.ptr, val.len, "{led:%d, relay:%d, button:%d}", &led, &relay, &button) != 3) { 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")); LOG(LL_ERROR, ("Incomplete Channel JSON: require 'led' and 'relay' and 'button' fields"));
goto exit; goto exit;
@ -76,83 +79,94 @@ bool channel_init(const char *fn) {
LOG(LL_ERROR, ("Button GPIO (%d) out of bounds [%d,%d]", button, GPIO_MIN, GPIO_MAX)); LOG(LL_ERROR, ("Button GPIO (%d) out of bounds [%d,%d]", button, GPIO_MIN, GPIO_MAX));
goto exit; goto exit;
} }
if (idx==CHANNEL_MAX) { if (idx == CHANNEL_MAX) {
LOG(LL_ERROR, ("Too many channels (max is %d)", CHANNEL_MAX)); LOG(LL_ERROR, ("Too many channels (max is %d)", CHANNEL_MAX));
goto exit; goto exit;
} }
s_channels[idx].button_gpio=button; s_channels[idx].button_gpio = button;
s_channels[idx].relay_gpio=relay; s_channels[idx].relay_gpio = relay;
s_channels[idx].led_gpio=led; s_channels[idx].led_gpio = led;
} }
ret=true; ret = true;
exit: exit:
if (ret) { if (ret) {
if (statusled!=-1) if (statusled != -1) {
statusled_init(statusled,statusled_invert); statusled_init(statusled, statusled_invert);
}
s_num_channels=idx+1; s_num_channels = idx + 1;
LOG(LL_INFO, ("Configuring %d channels", s_num_channels)); LOG(LL_INFO, ("Configuring %d channels", s_num_channels));
for (; idx>=0; idx--) { for (; idx >= 0; idx--) {
s_channels[idx].relay_state = 0; 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_set_mode(s_channels[idx].relay_gpio, MGOS_GPIO_MODE_OUTPUT);
mgos_gpio_write(s_channels[idx].relay_gpio, s_channels[idx].relay_state); 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_set_mode(s_channels[idx].led_gpio, MGOS_GPIO_MODE_OUTPUT);
mgos_gpio_write(s_channels[idx].led_gpio, s_channels[idx].relay_state); 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_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); 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 (name) {
if (json) free(json); free(name);
}
if (json) {
free(json);
}
return ret; return ret;
} }
uint8_t channel_gpio_by_idx(int idx) { 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 GPIO_INVALID;
}
return s_channels[idx].button_gpio; return s_channels[idx].button_gpio;
} }
uint8_t channel_idx_by_gpio(int gpio) { uint8_t channel_idx_by_gpio(int gpio) {
uint8_t i; uint8_t i;
for(i=0; i<channel_get_total(); i++) { for (i = 0; i < channel_get_total(); i++) {
if (gpio == s_channels[i].button_gpio) if (gpio == s_channels[i].button_gpio) {
return i; return i;
} }
}
return GPIO_INVALID; return GPIO_INVALID;
} }
void channel_set(int idx, bool state) { void channel_set(int idx, bool state) {
double now = mg_time(); double now = mg_time();
if (idx<0 || idx>=channel_get_total()) if (idx < 0 || idx >= channel_get_total()) {
return; return;
}
s_channels[idx].button_last_change = now; s_channels[idx].button_last_change = now;
s_channels[idx].relay_state = state; s_channels[idx].relay_state = state;
if (s_channels[idx].relay_gpio!=GPIO_INVALID) if (s_channels[idx].relay_gpio != GPIO_INVALID) {
mgos_gpio_write(s_channels[idx].relay_gpio, state); 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); mgos_gpio_write(s_channels[idx].led_gpio, state);
}
mqtt_publish_stat("channel", "{idx: %d, relay_state: %d}", idx, channel_get(idx)); mqtt_publish_stat("channel", "{idx: %d, relay_state: %d}", idx, channel_get(idx));
} }
bool channel_get(int idx) { bool channel_get(int idx) {
if (idx<0 || idx>=channel_get_total()) if (idx < 0 || idx >= channel_get_total()) {
return false; return false;
}
return s_channels[idx].relay_state == 1; return s_channels[idx].relay_state == 1;
} }
@ -168,7 +182,7 @@ void channel_handler(int gpio, void *arg) {
return; return;
} }
if (now<s_channels[idx].button_last_change+CHANNEL_CHANGE_COOLDOWN_SECONDS) { if (now < s_channels[idx].button_last_change + CHANNEL_CHANGE_COOLDOWN_SECONDS) {
LOG(LL_INFO, ("GPIO %d is cooling down -- skipping", gpio)); LOG(LL_INFO, ("GPIO %d is cooling down -- skipping", gpio));
return; return;
} }
@ -177,5 +191,5 @@ void channel_handler(int gpio, void *arg) {
state = channel_get(idx); state = channel_get(idx);
channel_set(idx, !state); channel_set(idx, !state);
(void) arg; (void)arg;
} }

View File

@ -11,8 +11,8 @@ static void mqtt_publish_broadcast_stat(const char *stat, const char *msg) {
statusled_blink(); statusled_blink();
snprintf(topic, sizeof(topic)-1, "%s/%s", MQTT_TOPIC_BROADCAST_STAT, stat); snprintf(topic, sizeof(topic) - 1, "%s/%s", MQTT_TOPIC_BROADCAST_STAT, stat);
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)); LOG(LL_INFO, ("Sent topic='%s' msg='%s'", topic, msg));
statusled_blink(); statusled_blink();
} }
@ -25,18 +25,20 @@ static void mqtt_broadcast_cmd_id() {
memset(sta_ip, 0, sizeof(sta_ip)); memset(sta_ip, 0, sizeof(sta_ip));
memset(ap_ip, 0, sizeof(ap_ip)); memset(ap_ip, 0, sizeof(ap_ip));
if (mgos_net_get_ip_info(MGOS_NET_IF_TYPE_WIFI, MGOS_NET_IF_WIFI_STA, &ip_info)) if (mgos_net_get_ip_info(MGOS_NET_IF_TYPE_WIFI, MGOS_NET_IF_WIFI_STA, &ip_info)) {
mgos_net_ip_to_str(&ip_info.ip, sta_ip); mgos_net_ip_to_str(&ip_info.ip, sta_ip);
if (mgos_net_get_ip_info(MGOS_NET_IF_TYPE_WIFI, MGOS_NET_IF_WIFI_AP, &ip_info)) }
if (mgos_net_get_ip_info(MGOS_NET_IF_TYPE_WIFI, MGOS_NET_IF_WIFI_AP, &ip_info)) {
mgos_net_ip_to_str(&ip_info.ip, ap_ip); mgos_net_ip_to_str(&ip_info.ip, ap_ip);
}
snprintf(resp, sizeof(resp)-1, "{\"deviceid\": \"%s\", \"macaddress\": \"%s\", \"sta_ip\": \"%s\", \"ap_ip\": \"%s\", \"app\": \"%s\", \"arch\": \"%s\", \"uptime\": %lu}", snprintf(resp, sizeof(resp) - 1, "{\"deviceid\": \"%s\", \"macaddress\": \"%s\", \"sta_ip\": \"%s\", \"ap_ip\": \"%s\", \"app\": \"%s\", \"arch\": \"%s\", \"uptime\": %lu}",
mgos_sys_config_get_device_id(), mgos_sys_config_get_device_id(),
mgos_sys_ro_vars_get_mac_address(), mgos_sys_ro_vars_get_mac_address(),
sta_ip, sta_ip,
ap_ip, ap_ip,
MGOS_APP, MGOS_APP,
mgos_sys_ro_vars_get_arch(), (unsigned long) mgos_uptime()); mgos_sys_ro_vars_get_arch(), (unsigned long)mgos_uptime());
mqtt_publish_broadcast_stat("id", resp); mqtt_publish_broadcast_stat("id", resp);
} }
@ -45,10 +47,11 @@ static void mqtt_cb(struct mg_connection *nc, const char *topic, int topic_len,
LOG(LL_INFO, ("Received topic='%.*s' msg='%.*s'", topic_len, topic, msg_len, msg)); LOG(LL_INFO, ("Received topic='%.*s' msg='%.*s'", topic_len, topic, msg_len, msg));
statusled_blink(); statusled_blink();
if (topic_len >= (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(); 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) { static void mqtt_ev(struct mg_connection *nc, int ev, void *ev_data, void *user_data) {
@ -57,12 +60,11 @@ static void mqtt_ev(struct mg_connection *nc, int ev, void *ev_data, void *user_
mqtt_broadcast_cmd_id(); mqtt_broadcast_cmd_id();
break; break;
} }
(void) nc; (void)nc;
(void) ev_data; (void)ev_data;
(void) user_data; (void)user_data;
} }
void mqtt_publish_stat(const char *stat, const char *fmt, ...) { void mqtt_publish_stat(const char *stat, const char *fmt, ...) {
char topic[80]; char topic[80];
char msg[200]; char msg[200];
@ -70,13 +72,13 @@ void mqtt_publish_stat(const char *stat, const char *fmt, ...) {
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); va_start(ap, fmt);
json_vprintf(&out, fmt, ap); json_vprintf(&out, fmt, ap);
va_end(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)); LOG(LL_INFO, ("Sent topic='%s' msg='%s'", topic, msg));
statusled_blink(); statusled_blink();
} }
@ -90,7 +92,7 @@ void mqtt_init() {
mgos_mqtt_sub(MQTT_TOPIC_BROADCAST_CMD, mqtt_cb, NULL); mgos_mqtt_sub(MQTT_TOPIC_BROADCAST_CMD, mqtt_cb, NULL);
// Subscribe to broadcast/appname // 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); mgos_mqtt_sub(topic, mqtt_cb, NULL);
return; return;

View File

@ -21,8 +21,8 @@ static bool rpc_args_to_idx_and_gpio(struct mg_rpc_request_info *ri, struct mg_s
return false; return false;
} }
if (idx<0 || idx>=channel_get_total()) { if (idx < 0 || idx >= channel_get_total()) {
mg_rpc_send_errorf(ri, 400, "idx must be between 0 and %d", channel_get_total()-1); mg_rpc_send_errorf(ri, 400, "idx must be between 0 and %d", channel_get_total() - 1);
ri = NULL; ri = NULL;
return false; return false;
} }
@ -45,17 +45,18 @@ static void rpc_channel_toggle_handler(struct mg_rpc_request_info *ri, void *cb_
rpc_log(ri, args); 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; return;
}
channel_handler(gpio, NULL); channel_handler(gpio, NULL);
mg_rpc_send_responsef(ri, "{idx: %d, relay_state: %d}", idx, channel_get(idx)); mg_rpc_send_responsef(ri, "{idx: %d, relay_state: %d}", idx, channel_get(idx));
ri = NULL; ri = NULL;
(void) ri; (void)ri;
(void) cb_arg; (void)cb_arg;
(void) fi; (void)fi;
(void) args; (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) { 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) {
@ -64,16 +65,17 @@ static void rpc_channel_get_handler(struct mg_rpc_request_info *ri, void *cb_arg
rpc_log(ri, args); 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; return;
}
mg_rpc_send_responsef(ri, "{idx: %d, relay_state: %d}", idx, channel_get(idx)); mg_rpc_send_responsef(ri, "{idx: %d, relay_state: %d}", idx, channel_get(idx));
ri = NULL; ri = NULL;
(void) ri; (void)ri;
(void) cb_arg; (void)cb_arg;
(void) fi; (void)fi;
(void) args; (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) { 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) {
@ -89,8 +91,8 @@ static void rpc_channel_set_handler(struct mg_rpc_request_info *ri, void *cb_arg
return; return;
} }
if (idx<0 || idx>=channel_get_total()) { if (idx < 0 || idx >= channel_get_total()) {
mg_rpc_send_errorf(ri, 400, "idx must be between 0 and %d", channel_get_total()-1); mg_rpc_send_errorf(ri, 400, "idx must be between 0 and %d", channel_get_total() - 1);
ri = NULL; ri = NULL;
return; return;
} }
@ -102,18 +104,19 @@ static void rpc_channel_set_handler(struct mg_rpc_request_info *ri, void *cb_arg
return; 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)); mg_rpc_send_responsef(ri, "{idx: %d, relay_state: %d}", idx, channel_get(idx));
ri = NULL; ri = NULL;
(void) ri; (void)ri;
(void) cb_arg; (void)cb_arg;
(void) fi; (void)fi;
(void) args; (void)args;
} }
void rpc_init() { void rpc_init() {
struct mg_rpc *c = mgos_rpc_get_global(); 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.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.Get", "{idx: %d}", rpc_channel_get_handler, NULL);
mg_rpc_add_handler(c, "Channel.Set", "{idx: %d, value: %d}", rpc_channel_set_handler, NULL); mg_rpc_add_handler(c, "Channel.Set", "{idx: %d, value: %d}", rpc_channel_set_handler, NULL);

View File

@ -6,18 +6,19 @@ static bool statusled_off_state = 0;
static void statusled_off_cb(void *arg) { static void statusled_off_cb(void *arg) {
mgos_gpio_write(statusled_gpio, statusled_off_state); mgos_gpio_write(statusled_gpio, statusled_off_state);
statusled_timer_id=0; statusled_timer_id = 0;
(void) arg; (void)arg;
} }
void statusled_blink() { void statusled_blink() {
if (statusled_gpio == GPIO_INVALID) if (statusled_gpio == GPIO_INVALID) {
return; return;
}
mgos_gpio_write(statusled_gpio, !statusled_off_state); mgos_gpio_write(statusled_gpio, !statusled_off_state);
if (statusled_timer_id) { if (statusled_timer_id) {
mgos_clear_timer(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); statusled_timer_id = mgos_set_timer(100, false, statusled_off_cb, NULL);
} }

File diff suppressed because it is too large Load Diff

View File

@ -56,7 +56,7 @@ enum json_token_type {
* `json_scanf()` with the format specifier `%T`. * `json_scanf()` with the format specifier `%T`.
*/ */
struct json_token { struct json_token {
const char *ptr; /* Points to the beginning of the value */ const char * ptr; /* Points to the beginning of the value */
int len; /* Value length */ int len; /* Value length */
enum json_token_type type; /* Type of the token, possible values are above */ enum json_token_type type; /* Type of the token, possible values are above */
}; };
@ -88,7 +88,7 @@ struct json_token {
* - type: JSON_TYPE_OBJECT_START, name: "2", path: ".bar[2]", value: NULL * - 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_TRUE, name: "baz", path: ".bar[2].baz", value: "true"
* - type: JSON_TYPE_OBJECT_END, name: NULL, path: ".bar[2]", value: "{ \"baz\": * - 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, { * - type: JSON_TYPE_ARRAY_END, name: NULL, path: ".bar", value: "[ 1, 2, {
*\"baz\": true } ]" *\"baz\": true } ]"
* - type: JSON_TYPE_OBJECT_END, name: NULL, path: "", value: "{ \"foo\": 123, * - type: JSON_TYPE_OBJECT_END, name: NULL, path: "", value: "{ \"foo\": 123,
@ -114,7 +114,7 @@ struct json_out {
int (*printer)(struct json_out *, const char *str, size_t len); int (*printer)(struct json_out *, const char *str, size_t len);
union { union {
struct { struct {
char *buf; char * buf;
size_t size; size_t size;
size_t len; size_t len;
} buf; } buf;
@ -135,7 +135,7 @@ extern int json_printer_file(struct json_out *, const char *, size_t);
#define JSON_OUT_FILE(fp) \ #define JSON_OUT_FILE(fp) \
{ \ { \
json_printer_file, { \ json_printer_file, { \
{ (char *) fp, 0, 0 } \ { (char *)fp, 0, 0 } \
} \ } \
} }

View File

@ -1,7 +1,7 @@
#include "test.h" #include "test.h"
int test_failures=0; int test_failures = 0;
int assert_count=0; int assert_count = 0;
uint32_t mqtt_pub_count; uint32_t mqtt_pub_count;
uint32_t mqtt_sub_count; uint32_t mqtt_sub_count;

View File

@ -10,7 +10,7 @@ bool mgos_gpio_set_mode(int pin, enum mgos_gpio_mode mode) {
} }
void mgos_gpio_write(int pin, bool level) { 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) { 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) {
@ -18,14 +18,15 @@ bool mgos_gpio_set_button_handler(int pin, enum mgos_gpio_pull_type pull_type, e
s_handler_cb_arg = arg; s_handler_cb_arg = arg;
return true; return true;
(void) debounce_ms;
(void) int_mode; (void)debounce_ms;
(void) pull_type; (void)int_mode;
(void) pin; (void)pull_type;
(void)pin;
} }
void mgos_gpio_inject(int pin) { void mgos_gpio_inject(int pin) {
if (s_handler_cb) if (s_handler_cb) {
s_handler_cb(pin, s_handler_cb_arg); s_handler_cb(pin, s_handler_cb_arg);
}
} }

View File

@ -8,36 +8,41 @@ int _mgos_timers = 0;
int log_print_prefix(enum cs_log_level l, const char *func, const char *file) { int log_print_prefix(enum cs_log_level l, const char *func, const char *file) {
char ll_str[6]; char ll_str[6];
switch(l) { switch (l) {
case LL_ERROR: case LL_ERROR:
strncpy(ll_str, "ERROR", sizeof(ll_str)); strncpy(ll_str, "ERROR", sizeof(ll_str));
break; break;
case LL_WARN: case LL_WARN:
strncpy(ll_str, "WARN", sizeof(ll_str)); strncpy(ll_str, "WARN", sizeof(ll_str));
break; break;
case LL_INFO: case LL_INFO:
strncpy(ll_str, "INFO", sizeof(ll_str)); strncpy(ll_str, "INFO", sizeof(ll_str));
break; break;
case LL_DEBUG: case LL_DEBUG:
strncpy(ll_str, "DEBUG", sizeof(ll_str)); strncpy(ll_str, "DEBUG", sizeof(ll_str));
break; break;
case LL_VERBOSE_DEBUG: case LL_VERBOSE_DEBUG:
strncpy(ll_str, "VERB", sizeof(ll_str)); strncpy(ll_str, "VERB", sizeof(ll_str));
break; break;
default: // LL_NONE default: // LL_NONE
return 0; return 0;
} }
printf ("%-5s %-15s %-40s| ", ll_str, file, func); printf("%-5s %-15s %-40s| ", ll_str, file, func);
return 1; return 1;
} }
mgos_timer_id mgos_set_timer(int msecs, int flags, timer_callback cb, void *cb_arg) { mgos_timer_id mgos_set_timer(int msecs, int flags, timer_callback cb, void *cb_arg) {
_mgos_timers++; _mgos_timers++;
LOG(LL_INFO, ("Installing timer -- %d timers currently installed", _mgos_timers)); LOG(LL_INFO, ("Installing timer -- %d timers currently installed", _mgos_timers));
(void) msecs; (void)msecs;
(void) flags; (void)flags;
(void) cb; (void)cb;
(void) cb_arg; (void)cb_arg;
return _mgos_timers; 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) { void mgos_clear_timer(mgos_timer_id id) {
_mgos_timers--; _mgos_timers--;
LOG(LL_INFO, ("Clearing timer -- %d timers currently installed", _mgos_timers)); LOG(LL_INFO, ("Clearing timer -- %d timers currently installed", _mgos_timers));
(void) id; (void)id;
return; return;
} }
double mg_time() { double mg_time() {
return (float) time(NULL); return (float)time(NULL);
} }
double mgos_uptime() { double mgos_uptime() {
return (double) time(NULL); return (double)time(NULL);
} }
char *mgos_sys_ro_vars_get_mac_address() { char *mgos_sys_ro_vars_get_mac_address() {

View File

@ -27,7 +27,7 @@ int log_print_prefix(enum cs_log_level l, const char *func, const char *file);
#define LOG(l, x) \ #define LOG(l, x) \
do { \ do { \
if (log_print_prefix(l, __func__, __FILE__)) printf x; \ if (log_print_prefix(l, __func__, __FILE__)) { printf x; } \
printf("\r\n"); \ printf("\r\n"); \
} while (0) } while (0)

View File

@ -1,17 +1,17 @@
#include "mgos.h" #include "mgos.h"
#include "mgos_mqtt.h" #include "mgos_mqtt.h"
uint32_t mqtt_pub_count=0; uint32_t mqtt_pub_count = 0;
uint32_t mqtt_sub_count=0; uint32_t mqtt_sub_count = 0;
static sub_handler_t s_handler; 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 mg_event_handler_t s_global_handler;
static void *s_global_handler_ud; static void *s_global_handler_ud;
void mgos_mqtt_pub(char *t, char *m, int m_len, int flags, bool persist) { 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++; mqtt_pub_count++;
} }
@ -24,12 +24,12 @@ void mgos_mqtt_sub(char *t, sub_handler_t cb, void *ud) {
void mgos_mqtt_inject(char *topic, char *msg) { void mgos_mqtt_inject(char *topic, char *msg) {
LOG(LL_INFO, ("Injecting topic='%s' msg='%s'", topic, msg)); LOG(LL_INFO, ("Injecting topic='%s' msg='%s'", topic, msg));
mqtt_sub_count++; mqtt_sub_count++;
if (s_handler) if (s_handler) {
s_handler(NULL, topic, strlen(topic), msg, strlen(msg), s_handler_ud); 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) { 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; s_global_handler_ud = ud;
} }

File diff suppressed because it is too large Load Diff

View File

@ -5,5 +5,3 @@ int test_buttons() {
channel_init("testdata/testconfig1.json"); channel_init("testdata/testconfig1.json");
return 0; return 0;
} }