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);
void mgos_ota_http_start(struct update_context *ctx, const char *url);
#endif
#ifdef __cplusplus

View File

@ -17,18 +17,22 @@
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;
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));
int result = *((int *)p);
if (result != 0) {
LOG(LL_ERROR, ("connect error: %d", result));
}
break;
}
case MG_EV_RECV: {
if (ctx->file_size == 0) {
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) {
/* NUL-terminate the URL. Every header must be followed by \r\n,
* 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
* connection so that it doesn't get finalized when it's closed. */
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;
}
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) {
LOG(LL_ERROR, ("Invalid content-length, perhaps chunked-encoding"));
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);
if (res == 0) {
if (is_write_finished(ctx)) res = updater_finalize(ctx);
if (is_write_finished(ctx)) {
res = updater_finalize(ctx);
}
if (res == 0) {
/* Need more data, everything is OK */
break;
@ -97,14 +104,21 @@ static void fw_download_handler(struct mg_connection *c, int ev, void *p,
}
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)) {
/* 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;
} else if (is_reboot_required(ctx)) {
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(
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));
@ -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));

View File

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

View File

@ -16,9 +16,12 @@
#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();
@ -30,6 +33,7 @@ static void handle_update_post(struct mg_connection *c, int ev, void *p) {
}
break;
}
case MG_EV_HTTP_PART_BEGIN: {
LOG(LL_DEBUG, ("MG_EV_HTTP_PART_BEGIN: %p %s %s", ctx, mp->var_name,
mp->file_name));
@ -39,9 +43,10 @@ static void handle_update_post(struct mg_connection *c, int ev, void *p) {
}
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));
mp->file_name, (int)mp->data.len));
if (mp->file_name[0] == '\0') {
/* 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;
}
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->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));
@ -75,14 +83,19 @@ static void handle_update_post(struct mg_connection *c, int ev, void *p) {
}
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;
if (is_write_finished(ctx)) updater_finalize(ctx);
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";
@ -113,7 +126,9 @@ static void handle_update_post(struct mg_connection *c, int ev, void *p) {
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,
@ -145,8 +160,9 @@ static void update_handler(struct mg_connection *c, int ev, void *ev_data,
}
return;
}
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) {
mg_send_response_line(c, 409,
"Content-Type: text/plain\r\n"
@ -156,13 +172,13 @@ static void update_handler(struct mg_connection *c, int ev, void *ev_data,
return;
}
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;
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;
char * buf = calloc(params.len, 1), *p = buf;
int len = mg_get_http_var(&params, "url", p, buf_len);
if (len > 0) {
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->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"
@ -203,6 +218,7 @@ static void update_handler(struct mg_connection *c, int ev, void *ev_data,
free(buf);
break;
}
case MG_EV_CLOSE: {
if (s_update_request_conn == c) {
/* 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;
}
}
(void) user_data;
(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;
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 */));
@ -226,8 +244,10 @@ static void update_action_handler(struct mg_connection *c, int ev, void *p,
"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) {

View File

@ -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_

View File

@ -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;
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));
@ -73,12 +77,14 @@ static void handle_update_req(struct mg_rpc_request_info *ri, void *cb_arg,
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,
@ -116,6 +122,7 @@ static void handle_create_snapshot_req(struct mg_rpc_request_info *ri,
struct mg_str args) {
const char *err_msg = NULL;
int ret = -1;
if (mgos_upd_is_committed()) {
ret = mgos_upd_create_snapshot();
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);

View File

@ -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,8 +20,8 @@ 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;
@ -30,7 +32,7 @@ bool channel_init(const char *fn) {
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,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));
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(); i++) {
if (gpio == s_channels[i].button_gpio)
for (i = 0; i < channel_get_total(); i++) {
if (gpio == s_channels[i].button_gpio) {
return i;
}
}
return GPIO_INVALID;
}
void channel_set(int idx, bool state) {
double now = mg_time();
if (idx<0 || idx>=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)
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;
}
@ -168,7 +182,7 @@ void channel_handler(int gpio, void *arg) {
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));
return;
}
@ -177,5 +191,5 @@ void channel_handler(int gpio, void *arg) {
state = channel_get(idx);
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();
snprintf(topic, sizeof(topic)-1, "%s/%s", MQTT_TOPIC_BROADCAST_STAT, stat);
mgos_mqtt_pub((char*)topic, (char*)msg, strlen(msg), 0, false);
snprintf(topic, sizeof(topic) - 1, "%s/%s", MQTT_TOPIC_BROADCAST_STAT, stat);
mgos_mqtt_pub((char *)topic, (char *)msg, strlen(msg), 0, false);
LOG(LL_INFO, ("Sent topic='%s' msg='%s'", topic, msg));
statusled_blink();
}
@ -25,18 +25,20 @@ static void mqtt_broadcast_cmd_id() {
memset(sta_ip, 0, sizeof(sta_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);
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);
}
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_ro_vars_get_mac_address(),
sta_ip,
ap_ip,
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);
}
@ -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));
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();
(void) nc;
(void) ud;
}
(void)nc;
(void)ud;
}
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();
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];
@ -70,13 +72,13 @@ void mqtt_publish_stat(const char *stat, const char *fmt, ...) {
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;

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;
}
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;
}
@ -45,17 +45,18 @@ static void rpc_channel_toggle_handler(struct mg_rpc_request_info *ri, void *cb_
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) {
@ -64,16 +65,17 @@ static void rpc_channel_get_handler(struct mg_rpc_request_info *ri, void *cb_arg
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) {
@ -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);

View File

@ -6,18 +6,19 @@ 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);
}

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`.
*/
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 */
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_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,7 +114,7 @@ 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;
@ -135,7 +135,7 @@ extern int json_printer_file(struct json_out *, const char *, size_t);
#define JSON_OUT_FILE(fp) \
{ \
json_printer_file, { \
{ (char *) fp, 0, 0 } \
{ (char *)fp, 0, 0 } \
} \
}

View File

@ -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;

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) {
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) {
@ -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;
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);
}
}

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) {
char ll_str[6];
switch(l) {
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() {

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) \
do { \
if (log_print_prefix(l, __func__, __FILE__)) printf x; \
if (log_print_prefix(l, __func__, __FILE__)) { printf x; } \
printf("\r\n"); \
} while (0)

View File

@ -1,17 +1,17 @@
#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++;
}
@ -24,12 +24,12 @@ void mgos_mqtt_sub(char *t, sub_handler_t cb, void *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_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");
return 0;
}