From fb26b8a7f14698411d9faae8ae3be9ed10e026df Mon Sep 17 00:00:00 2001 From: attdona Date: Sat, 23 Sep 2017 10:35:50 +0200 Subject: [PATCH] review --- burba/apps/hello-mqtt/main.c | 46 ------- burba/sys/bocia/bocia.c | 242 ++++++++++++++++++++--------------- burba/sys/bocia/bocia_file.c | 40 +++--- burba/sys/include/bocia.h | 16 +-- 4 files changed, 167 insertions(+), 177 deletions(-) diff --git a/burba/apps/hello-mqtt/main.c b/burba/apps/hello-mqtt/main.c index 9e21380..057f18b 100644 --- a/burba/apps/hello-mqtt/main.c +++ b/burba/apps/hello-mqtt/main.c @@ -65,52 +65,6 @@ uint32_t handle_command(uint8_t type, Command *command) { return sts; } -int8_t proto_input_handler(channel_reader_t *rd, proto_msg_t command_id, - void *obj) { - - proto_msg_t ack_type = {ACK_TYPE, 1}; - ack_type.dline = command_id.dline; - - Ack ack = ACK__INIT; - - ack.id = command_id.type; - - DEBUG("executing [%d,%d] message\n", command_id.type, command_id.subtype); - - switch (command_id.type) { - case COMMAND_TYPE: { - Command *cmd = (Command *)obj; - printf("command: %ld, seq: %ld\n", cmd->id, cmd->seq); - bocia_command(rd, cmd); - break; - } - case SECRET_TYPE: { - Secret *secret = (Secret *)obj; - set_ap_password(secret->key); - cc3200_reset(); - break; - } - case CONFIG_TYPE: { - bocia_set_cfg((Config *)obj); - rd->send(rd, ack_type, &ack); - break; - } - case PROFILE_TYPE: { - Profile *profile; - profile = (Profile *)obj; - DEBUG("ssid: %s - pwd: %s\n", profile->uid, profile->pwd); - sbapp_add_profile(profile->uid, profile->pwd); - rd->send(rd, ack_type, &ack); - break; - } - default: - DEBUG("custom command [%d]\n", command_id.type); - ack.status = handle_command(command_id.type, (Command *)obj); - ack.has_status = 1; - rd->send(rd, ack_type, &ack); - } - return INPUTHANDLER_OK; -} void client_handler(void) { Config *cfg; diff --git a/burba/sys/bocia/bocia.c b/burba/sys/bocia/bocia.c index 4fe8354..50a2116 100644 --- a/burba/sys/bocia/bocia.c +++ b/burba/sys/bocia/bocia.c @@ -11,7 +11,7 @@ #include "bocia.h" -#define ENABLE_DEBUG (1) +#define ENABLE_DEBUG (1) #include "debug.h" /** @@ -19,12 +19,10 @@ */ channel_reader_t *channels(void); - uint8_t broker_is_connected(void); void broker_disconnect(kernel_pid_t board_io_pid); - /** * @brief set how the board will reboot * @@ -91,145 +89,187 @@ int16_t mqtt_receive(int16_t* fd, unsigned char** packet); static Config *config = 0; +int8_t proto_input_handler(channel_reader_t *rd, proto_msg_t command_id, + void *obj) { + + proto_msg_t ack_type = {ACK_TYPE, 1}; + ack_type.dline = command_id.dline; + + Ack ack = ACK__INIT; + + ack.id = command_id.type; + + DEBUG("executing [%d,%d] message\n", command_id.type, command_id.subtype); + + switch (command_id.type) { + case COMMAND_TYPE: { + Command *cmd = (Command *)obj; + printf("command: %ld, seq: %ld\n", cmd->id, cmd->seq); + bocia_command(rd, cmd); + break; + } + case SECRET_TYPE: { + Secret *secret = (Secret *)obj; + set_ap_password(secret->key); + cc3200_reset(); + break; + } + case CONFIG_TYPE: { + bocia_set_cfg((Config *)obj); + rd->send(rd, ack_type, &ack); + break; + } + case PROFILE_TYPE: { + Profile *profile; + profile = (Profile *)obj; + DEBUG("ssid: %s - pwd: %s\n", profile->uid, profile->pwd); + sbapp_add_profile(profile->uid, profile->pwd); + rd->send(rd, ack_type, &ack); + break; + } + default: + DEBUG("custom command [%d]\n", command_id.type); + ack.status = handle_command(command_id.type, (Command *)obj); + ack.has_status = 1; + rd->send(rd, ack_type, &ack); + } + return INPUTHANDLER_OK; +} uint8_t bocia_remote_console(void) { - channel_reader_t *channel; + channel_reader_t *channel; - channel = channels(); + channel = channels(); - for (uint8_t i = 0; i < SBAPP_SOCKETS; i++) { - if (channel->debug == 1) { - return 1; - } - } - return 0; + for (uint8_t i = 0; i < SBAPP_SOCKETS; i++) { + if (channel->debug == 1) { + return 1; + } + } + return 0; } _ssize_t bocia_remote_write(const void *data, size_t count) { - channel_reader_t *channel; - channel = channels(); + channel_reader_t *channel; + channel = channels(); - for (uint8_t i = 0; i < SBAPP_SOCKETS; i++) { - if (channel->debug == 1) { - sl_Send(channel->fd, data, count, 0); - } - channel++; - } + for (uint8_t i = 0; i < SBAPP_SOCKETS; i++) { + if (channel->debug == 1) { + sl_Send(channel->fd, data, count, 0); + } + channel++; + } - return count; + return count; } void bocia_broadcast(proto_msg_t type, void *data) { - channel_reader_t *channel; - channel = channels(); - - for (uint8_t i = 0; i < SBAPP_SOCKETS; i++) { - if (channel->send) { - channel->send(channel, type, data); - } - channel++; - } + channel_reader_t *channel; + channel = channels(); + + for (uint8_t i = 0; i < SBAPP_SOCKETS; i++) { + if (channel->send) { + channel->send(channel, type, data); + } + channel++; + } } int8_t bocia_unmarshall(channel_reader_t *rd, unsigned char *data, - int8_t (*handler)(channel_reader_t *, proto_msg_t, void *)) { - void *obj; - proto_msg_t ptype; - int8_t sts; + int8_t (*handler)(channel_reader_t *, proto_msg_t, + void *)) { + void *obj; + proto_msg_t ptype; + int8_t sts; - ptype = nais_unmarshall(data, &obj); + ptype = nais_unmarshall(data, &obj); - if (ptype.subtype & (1 << DEBUG_OPTION_BIT)) { - rd->debug = 1; - } + if (ptype.subtype & (1 << DEBUG_OPTION_BIT)) { + rd->debug = 1; + } // reuse type slot for status reporting - sts = handler(rd, ptype, obj); + sts = handler(rd, ptype, obj); - nais_free(obj); + nais_free(obj); - return sts; + return sts; } void set_reboot_mode(int mode) { - if (mode == 1) { - DEBUG("reset to factory state\n"); - simplelink_to_default_state(); - bocia_delete_file(BOCIA_CFG_FILE); - PRCMOCRRegisterWrite(0, 0); - } else { - PRCMOCRRegisterWrite(0, 1); - } + if (mode == 1) { + DEBUG("reset to factory state\n"); + simplelink_to_default_state(); + bocia_delete_file(BOCIA_CFG_FILE); + PRCMOCRRegisterWrite(0, 0); + } else { + PRCMOCRRegisterWrite(0, 1); + } } - - void bocia_set_cfg(Config *new_config) { - if (config) { - nais_free(config); - } - bocia_write_cfg(new_config); - config = new_config; + if (config) { + nais_free(config); + } + bocia_write_cfg(new_config); + config = new_config; } Config *bocia_get_config(void) { - if (config) { - return config; - } - config = bocia_read_cfg(); - return config; + if (config) { + return config; + } + config = bocia_read_cfg(); + return config; } Config *bocia_toggle_board_mode(void) { - int16_t nprofiles; + int16_t nprofiles; - nprofiles = sbapp_get_profiles(); + nprofiles = sbapp_get_profiles(); - // check if provisioned - config = bocia_get_config(); - if (config && (nwp.role == ROLE_AP) && nprofiles > 0) { - sbapp_set_mode(ROLE_STA); - } else if ((config == 0) && (nwp.role == ROLE_STA)) { - sbapp_set_mode(ROLE_AP); - } + // check if provisioned + config = bocia_get_config(); + if (config && (nwp.role == ROLE_AP) && nprofiles > 0) { + sbapp_set_mode(ROLE_STA); + } else if ((config == 0) && (nwp.role == ROLE_STA)) { + sbapp_set_mode(ROLE_AP); + } - return config; + return config; } void delayed_reboot(channel_reader_t *rd) { - msg_t msg; + msg_t msg; - msg.type = REBOOT_REQUEST; - msg_send(&msg, rd->target_pid); + msg.type = REBOOT_REQUEST; + msg_send(&msg, rd->target_pid); } -void bocia_command(channel_reader_t *rd, Command* cmd) { - switch (cmd->id) { - case REBOOT_CMD: - delayed_reboot(rd); - break; - case TOGGLE_WIFI_MODE_CMD: - set_reboot_mode(0); - delayed_reboot(rd); - break; - case FACTORY_RESET_CMD: - set_reboot_mode(1); - delayed_reboot(rd); - break; - case GET_CONFIG_CMD: { - proto_msg_t type = { CONFIG_TYPE, 0 }; - - if (!config) { - config = bocia_get_config(); - } - rd->send(rd, type, config); // TODO check null pointer arg - break; - } - default: - DEBUG("invalid command id: %ld\n", cmd->id); - - } +void bocia_command(channel_reader_t *rd, Command *cmd) { + switch (cmd->id) { + case REBOOT_CMD: + delayed_reboot(rd); + break; + case TOGGLE_WIFI_MODE_CMD: + set_reboot_mode(0); + delayed_reboot(rd); + break; + case FACTORY_RESET_CMD: + set_reboot_mode(1); + delayed_reboot(rd); + break; + case GET_CONFIG_CMD: { + proto_msg_t type = {CONFIG_TYPE, 0}; + + if (!config) { + config = bocia_get_config(); + } + rd->send(rd, type, config); // TODO check null pointer arg + break; + } + default: + DEBUG("invalid command id: %ld\n", cmd->id); + } } - - diff --git a/burba/sys/bocia/bocia_file.c b/burba/sys/bocia/bocia_file.c index 4b62965..f7908e5 100644 --- a/burba/sys/bocia/bocia_file.c +++ b/burba/sys/bocia/bocia_file.c @@ -10,7 +10,7 @@ * @{ * * @file - * @brief + * @brief * * @author Attilio Dona' * @@ -22,43 +22,40 @@ #include "bocia.h" #include "simplelink.h" -#define ENABLE_DEBUG (1) +#define ENABLE_DEBUG (1) #include "debug.h" #define MAX_BIN_SIZE 128 static unsigned char _buff[MAX_BIN_SIZE]; +int write_proto(const char *fname, unsigned char *buff, size_t len); -int write_proto(const char* fname, unsigned char* buff, size_t len); - -int write_proto(const char* fname, unsigned char* buff, size_t len) { - uint32_t maxsize = 1; //3584; +int write_proto(const char *fname, unsigned char *buff, size_t len) { + uint32_t maxsize = 1; // 3584; int32_t fd = -1; int32_t sts; SlFsFileInfo_t finfo; uint32_t options = 0; // check if file exists - sts = sl_FsGetInfo((const unsigned char*)fname, 0, &finfo); + sts = sl_FsGetInfo((const unsigned char *)fname, 0, &finfo); if (sts == 0) { options = FS_MODE_OPEN_WRITE; - } - else if (sts == SL_FS_ERR_FILE_NOT_EXISTS) { + } else if (sts == SL_FS_ERR_FILE_NOT_EXISTS) { options = FS_MODE_OPEN_CREATE(maxsize, 0); - } - else { + } else { // TODO: fatal error return sts; } - sts = sl_FsOpen((const unsigned char*)fname, options, NULL, &fd); + sts = sl_FsOpen((const unsigned char *)fname, options, NULL, &fd); if (sts < 0) { return sts; } - sts = sl_FsWrite(fd, 0, (unsigned char*)buff, len); + sts = sl_FsWrite(fd, 0, (unsigned char *)buff, len); sl_FsClose(fd, NULL, NULL, 0); return sts; @@ -67,16 +64,15 @@ int write_proto(const char* fname, unsigned char* buff, size_t len) { int bocia_write_cfg(Config *cfg) { size_t blen; - blen = protobuf_c_message_get_packed_size((const ProtobufCMessage*) cfg); + blen = protobuf_c_message_get_packed_size((const ProtobufCMessage *)cfg); DEBUG("protobuf blen: %d\n", blen); - protobuf_c_message_pack((const ProtobufCMessage*) cfg, _buff); + protobuf_c_message_pack((const ProtobufCMessage *)cfg, _buff); return write_proto(BOCIA_CFG_FILE, _buff, blen); - } -Config* bocia_read_cfg(void) { +Config *bocia_read_cfg(void) { SlFsFileInfo_t finfo; int16_t sts; uint32_t blen; @@ -84,13 +80,14 @@ Config* bocia_read_cfg(void) { int32_t fd = -1; // get the len of the stored config protobuf object - sts = sl_FsGetInfo((const unsigned char*)BOCIA_CFG_FILE, 0, &finfo); + sts = sl_FsGetInfo((const unsigned char *)BOCIA_CFG_FILE, 0, &finfo); if (sts < 0) { return 0; } blen = finfo.FileLen; - sts = sl_FsOpen((const unsigned char*)BOCIA_CFG_FILE, FS_MODE_OPEN_READ, NULL, &fd); + sts = sl_FsOpen((const unsigned char *)BOCIA_CFG_FILE, FS_MODE_OPEN_READ, + NULL, &fd); if (sts < 0) { return 0; } @@ -99,12 +96,11 @@ Config* bocia_read_cfg(void) { sl_FsClose(fd, NULL, NULL, 0); obj = protobuf_c_message_unpack(&config__descriptor, NULL, blen, _buff); - return (Config*)obj; - + return (Config *)obj; } int16_t bocia_delete_file(const char *fname) { int16_t sts; - sts = sl_FsDel((unsigned char*)fname, 0); + sts = sl_FsDel((unsigned char *)fname, 0); return sts; } diff --git a/burba/sys/include/bocia.h b/burba/sys/include/bocia.h index e8ca3db..c90e855 100644 --- a/burba/sys/include/bocia.h +++ b/burba/sys/include/bocia.h @@ -89,6 +89,10 @@ typedef enum { GROUND = 0, WARNING, NORMAL } app_state; */ typedef int16_t reader_id_t; +/** + * @brief define the custom command logics + */ +uint32_t __attribute__((weak)) handle_command(uint8_t type, Command *command); /** * @brief close as soon as possible the mqtt socket @@ -108,10 +112,8 @@ channel_reader_t *bocia_register_handlers( int16_t sock, kernel_pid_t main_pid, void (*handler)(channel_reader_t *), void (*tx_handler)(struct channel_reader_t *, proto_msg_t, void *data)); - uint8_t bocia_channel_is_connected(channel_reader_t *handler); - /** * @brief save on File System the bocia configuration */ @@ -165,8 +167,8 @@ _ssize_t bocia_remote_write(const void *data, size_t count); * INPUTHANDLER_ERR otherwise * */ -int8_t __attribute__((weak)) -proto_input_handler(channel_reader_t *rd, proto_msg_t pkt_type, void *obj); +int8_t proto_input_handler(channel_reader_t *rd, proto_msg_t pkt_type, + void *obj); /** * @brief unmarshall a NAIS payload, build a protobuf and call back the handler @@ -176,8 +178,8 @@ proto_input_handler(channel_reader_t *rd, proto_msg_t pkt_type, void *obj); * */ int8_t bocia_unmarshall(channel_reader_t *rd, unsigned char *data, - int8_t (*handler)(channel_reader_t *, - proto_msg_t, void *)); + int8_t (*handler)(channel_reader_t *, proto_msg_t, + void *)); /** * @brief return board configuration setting stored on flash memory @@ -191,7 +193,6 @@ Config *bocia_get_config(void); */ void bocia_set_cfg(Config *new_config); - /** * @brief toggle between STA and AP station mode */ @@ -202,7 +203,6 @@ Config *bocia_toggle_board_mode(void); */ void bocia_command(channel_reader_t *rd, Command *cmd); - /**@}*/ #endif /* SYS_INCLUDE_BOCIA_H_ */