diff --git a/lib/wpc_proto/internal_modules/proto_config.c b/lib/wpc_proto/internal_modules/proto_config.c index 25d387d..7e46346 100644 --- a/lib/wpc_proto/internal_modules/proto_config.c +++ b/lib/wpc_proto/internal_modules/proto_config.c @@ -14,6 +14,9 @@ #define MAX_LOG_LEVEL INFO_LOG_LEVEL #include "logger.h" +// Wirepas Gateway's protobuff message definition version +// see lib/wpc_proto/deps/backend-apis/gateway_to_backend/README.md +#define GW_PROTO_MESSAGE_VERSION 1 /** Structure to hold config from node */ typedef struct sink_config @@ -42,7 +45,9 @@ typedef struct sink_config /* TODO : Protect config access */ /** Sink config storage */ -sink_config_t m_sink_config; +static sink_config_t m_sink_config; + +static onEventStatus_cb_f m_onProtoEventStatus_cb = NULL; /* Typedef used to avoid warning at compile time */ typedef app_res_e (*func_1_param)(void * param); @@ -85,7 +90,7 @@ static bool get_value_from_node(void * f, return true; } -void convert_role_to_proto_format(app_role_t role, wp_NodeRole * proto_role) +static void convert_role_to_proto_format(app_role_t role, wp_NodeRole * proto_role) { pb_size_t flags_count = 0; proto_role->role = GET_BASE_ROLE(role); @@ -159,8 +164,7 @@ static bool initialize_config_variables() return res; } - -void Proto_config_fill_config(wp_SinkReadConfig * config_p) +static void fill_sink_read_config(wp_SinkReadConfig * config_p) { _Static_assert( member_size(wp_AppConfigData, app_config_data) == member_size(msap_app_config_data_write_req_pl_t, app_config_data)); @@ -177,45 +181,45 @@ void Proto_config_fill_config(wp_SinkReadConfig * config_p) .has_network_channel = true, .network_channel = m_sink_config.network_channel, .has_app_config = true, - .has_channel_map = false, - .has_are_keys_set = true, - .are_keys_set = m_sink_config.CipherKeySet - && m_sink_config.AuthenticationKeySet, + .has_channel_map = false, + .has_are_keys_set = true, + .are_keys_set = m_sink_config.CipherKeySet + && m_sink_config.AuthenticationKeySet, + + .has_current_ac_range = (m_sink_config.ac_range_min_cur == 0 ? false : true), - .has_current_ac_range = (m_sink_config.ac_range_min_cur == 0 ? false : true), - .current_ac_range = {.min_ms = m_sink_config.ac_range_min_cur, - .max_ms = m_sink_config.ac_range_max_cur}, + .max_ms = m_sink_config.ac_range_max_cur}, /* Read only parameters */ - .has_ac_limits = true, + .has_ac_limits = true, .ac_limits = {.min_ms = m_sink_config.ac_limit_min, - .max_ms = m_sink_config.ac_limit_max}, - .has_max_mtu = true, - .max_mtu = m_sink_config.max_mtu, - .has_channel_limits = true, + .max_ms = m_sink_config.ac_limit_max}, + .has_max_mtu = true, + .max_mtu = m_sink_config.max_mtu, + .has_channel_limits = true, .channel_limits = {.min_channel = m_sink_config.ch_range_min, - .max_channel = m_sink_config.ch_range_max}, - .has_hw_magic = true, - .hw_magic = m_sink_config.hw_magic, - .has_stack_profile = true, - .stack_profile = m_sink_config.stack_profile, - .has_app_config_max_size = true, - .app_config_max_size = m_sink_config.app_config_max_size, - .has_firmware_version = true, + .max_channel = m_sink_config.ch_range_max}, + .has_hw_magic = true, + .hw_magic = m_sink_config.hw_magic, + .has_stack_profile = true, + .stack_profile = m_sink_config.stack_profile, + .has_app_config_max_size = true, + .app_config_max_size = m_sink_config.app_config_max_size, + .has_firmware_version = true, .firmware_version = {.major = m_sink_config.version[0], .minor = m_sink_config.version[1], .maint = m_sink_config.version[2], - .dev = m_sink_config.version[3] }, + .dev = m_sink_config.version[3] }, /* State of sink */ - .has_sink_state = true, - .sink_state = ((status & APP_STACK_STOPPED) ? wp_OnOffState_OFF : wp_OnOffState_ON), + .has_sink_state = true, + .sink_state = ((status & APP_STACK_STOPPED) ? wp_OnOffState_OFF : wp_OnOffState_ON), /* Scratchpad info for the sink */ - .has_stored_scratchpad = false, - .has_stored_status = false, - .has_stored_type = false, - .has_processed_scratchpad = false, - .has_firmware_area_id = false, - .has_target_and_action = false + .has_stored_scratchpad = false, + .has_stored_status = false, + .has_stored_type = false, + .has_processed_scratchpad = false, + .has_firmware_area_id = false, + .has_target_and_action = false }; strcpy(config_p->sink_id, Common_get_sink_id()); @@ -241,13 +245,89 @@ void Proto_config_fill_config(wp_SinkReadConfig * config_p) } } +static void onStackStatusReceived(uint8_t status) +{ + bool res; + wp_StatusEvent * message_StatusEvent_p; + uint8_t * encoded_message_p; + wp_GenericMessage message = wp_GenericMessage_init_zero; + wp_WirepasMessage message_wirepas = wp_WirepasMessage_init_zero; + message.wirepas = &message_wirepas; + + LOGI("Status received : %d\n", status); + + Proto_config_on_stack_boot_status(status); + + // Allocate the needed space for only the submessage we want to send + message_StatusEvent_p = Platform_malloc(sizeof(wp_StatusEvent)); + if (message_StatusEvent_p == NULL) + { + LOGE("Not enough memory to encode StatusEvent\n"); + return; + } + + // Allocate needed buffer for encoded message + encoded_message_p = Platform_malloc(WPC_PROTO_MAX_RESPONSE_SIZE); + if (encoded_message_p == NULL) + { + LOGE("Not enough memory for output buffer"); + Platform_free(message_StatusEvent_p, sizeof(wp_StatusEvent)); + return; + } + + message_wirepas.status_event = message_StatusEvent_p; + + *message_StatusEvent_p = (wp_StatusEvent){ + .version = GW_PROTO_MESSAGE_VERSION, + .state = wp_OnOffState_ON, // gateway state, always ONLINE + .configs_count = 1, + }; + + message_StatusEvent_p->has_gw_model = (strlen(Common_get_gateway_model()) != 0); + strcpy(message_StatusEvent_p->gw_model, Common_get_gateway_model()); + message_StatusEvent_p->has_gw_version = (strlen(Common_get_gateway_version()) != 0); + strcpy(message_StatusEvent_p->gw_version, Common_get_gateway_version()); + + fill_sink_read_config(&message_StatusEvent_p->configs[0]); + + Common_fill_event_header(&message_StatusEvent_p->header); + + // Using the module static buffer + pb_ostream_t stream = pb_ostream_from_buffer(encoded_message_p, WPC_PROTO_MAX_RESPONSE_SIZE); + + /* Now we are ready to encode the message! */ + res = pb_encode(&stream, wp_GenericMessage_fields, &message); + + /* Release buffer as we don't need it anymore */ + Platform_free(message_StatusEvent_p, sizeof(wp_StatusEvent)); + + if (!res) { + LOGE("Encoding failed: %s\n", PB_GET_ERROR(&stream)); + } + else + { + LOGI("Msg size %d\n", stream.bytes_written); + if (m_onProtoEventStatus_cb != NULL) + { + m_onProtoEventStatus_cb(encoded_message_p, stream.bytes_written); + } + } + + Platform_free(encoded_message_p, WPC_PROTO_MAX_RESPONSE_SIZE); +} bool Proto_config_init(void) { /* Read initial config from sink */ initialize_config_variables(); - return APP_RES_PROTO_OK; + if (WPC_register_for_stack_status(onStackStatusReceived) != APP_RES_OK) + { + LOGE("Stack status already registered\n"); + return false; + } + + return true; } void Proto_config_close() @@ -263,7 +343,7 @@ void Proto_config_on_stack_boot_status(uint8_t status) { LOGI("Stack started\n"); } - + // After a reboot, read again the variables initialize_config_variables(); } @@ -297,7 +377,85 @@ app_proto_res_e Proto_config_handle_get_configs(wp_GetConfigsReq *req, req->header.req_id, Common_convert_error_code(APP_RES_OK)); resp->configs_count = 1; - Proto_config_fill_config(&resp->configs[0]); + fill_sink_read_config(&resp->configs[0]); return APP_RES_PROTO_OK; -} \ No newline at end of file +} + +app_proto_res_e Proto_config_get_current_event_status(bool online, + uint8_t * event_status_p, + size_t * event_status_size_p) +{ + bool status; + wp_GenericMessage message = wp_GenericMessage_init_zero; + wp_WirepasMessage message_wirepas = wp_WirepasMessage_init_zero; + message.wirepas = &message_wirepas; + + // Allocate the needed space for only the submessage we want to send + wp_StatusEvent * message_StatusEvent_p = Platform_malloc(sizeof(wp_StatusEvent)); + if (message_StatusEvent_p == NULL) + { + LOGE("Not enough memory to encode StatusEvent\n"); + return APP_RES_PROTO_NOT_ENOUGH_MEMORY; + } + + message_wirepas.status_event = message_StatusEvent_p; + + *message_StatusEvent_p = (wp_StatusEvent){ + .version = GW_PROTO_MESSAGE_VERSION, + .configs_count = 0, + }; + + if (online) + { + // Generate status event with state ONLINE + message_StatusEvent_p->state = wp_OnOffState_ON; + + // Add current config for online node + message_StatusEvent_p->configs_count = 1; + fill_sink_read_config(&message_StatusEvent_p->configs[0]); + } + else + { + // Generate status event with state OFFLINE + message_StatusEvent_p->state = wp_OnOffState_OFF; + } + + message_StatusEvent_p->has_gw_model = (strlen(Common_get_gateway_model()) != 0); + strcpy(message_StatusEvent_p->gw_model, Common_get_gateway_model()); + message_StatusEvent_p->has_gw_version = (strlen(Common_get_gateway_version()) != 0); + strcpy(message_StatusEvent_p->gw_version, Common_get_gateway_version()); + + Common_fill_event_header(&message_StatusEvent_p->header); + + // Using the module static buffer + pb_ostream_t stream = pb_ostream_from_buffer(event_status_p, *event_status_size_p); + + /* Now we are ready to encode the message! */ + status = pb_encode(&stream, wp_GenericMessage_fields, &message); + + /* Release buffer as we don't need it anymore */ + Platform_free(message_StatusEvent_p, sizeof(wp_StatusEvent)); + + if (!status) { + LOGE("StatusEvent encoding failed: %s\n", PB_GET_ERROR(&stream)); + *event_status_size_p = 0; + return APP_RES_PROTO_CANNOT_GENERATE_RESPONSE; + } + + LOGI("Msg size %d\n", stream.bytes_written); + *event_status_size_p = stream.bytes_written; + return APP_RES_PROTO_OK; +} + +app_proto_res_e Proto_config_register_for_event_status(onEventStatus_cb_f onProtoEventStatus_cb) +{ + // Only support one "client" for now + if (m_onProtoEventStatus_cb != NULL) + { + return APP_RES_PROTO_ALREADY_REGISTERED; + } + + m_onProtoEventStatus_cb = onProtoEventStatus_cb; + return APP_RES_PROTO_OK; +} diff --git a/lib/wpc_proto/internal_modules/proto_config.h b/lib/wpc_proto/internal_modules/proto_config.h index 80fc35b..25ab87f 100644 --- a/lib/wpc_proto/internal_modules/proto_config.h +++ b/lib/wpc_proto/internal_modules/proto_config.h @@ -10,11 +10,6 @@ #include "wpc.h" #include "wpc_proto.h" -/** - * \brief Fill config stucture with stored config - * \param config_p pointer to config to fill - */ -void Proto_config_fill_config(wp_SinkReadConfig * config_p); /** * \brief Intialize the module in charge of data handling config @@ -38,4 +33,10 @@ app_proto_res_e Proto_config_handle_set_config(wp_SetConfigReq *req, app_proto_res_e Proto_config_handle_get_configs(wp_GetConfigsReq *req, wp_GetConfigsResp *resp); -#endif \ No newline at end of file +app_proto_res_e Proto_config_get_current_event_status(bool online, + uint8_t * event_status_p, + size_t * event_status_size_p); + +app_proto_res_e Proto_config_register_for_event_status(onEventStatus_cb_f onProtoEventStatus_cb); + +#endif diff --git a/lib/wpc_proto/wpc_proto.c b/lib/wpc_proto/wpc_proto.c index ec1cc7e..dee5d48 100644 --- a/lib/wpc_proto/wpc_proto.c +++ b/lib/wpc_proto/wpc_proto.c @@ -21,10 +21,6 @@ #include #include -// Wirepas Gateway's protobuff message definition version -// see lib/wpc_proto/deps/backend-apis/gateway_to_backend/README.md -#define GW_PROTO_MESSAGE_VERSION 1 - // API version implemented in the gateway // to fill implemented_api_version in GatewayInfo // see lib/wpc_proto/deps/backend-apis/gateway_to_backend/protocol_buffers_files/config_message.proto @@ -33,79 +29,6 @@ // Max possible size of encoded message #define MAX_PROTOBUF_SIZE WP_CONFIG_MESSAGE_PB_H_MAX_SIZE -static onEventStatus_cb_f m_onProtoEventStatus_cb = NULL; - - -static void onStackStatusReceived(uint8_t status) -{ - bool res; - wp_StatusEvent * message_StatusEvent_p; - uint8_t * encoded_message_p; - wp_GenericMessage message = wp_GenericMessage_init_zero; - wp_WirepasMessage message_wirepas = wp_WirepasMessage_init_zero; - message.wirepas = &message_wirepas; - - LOGI("Status received : %d\n", status); - - Proto_config_on_stack_boot_status(status); - - // Allocate the needed space for only the submessage we want to send - message_StatusEvent_p = Platform_malloc(sizeof(wp_StatusEvent)); - if (message_StatusEvent_p == NULL) - { - LOGE("Not enough memory to encode StatusEvent\n"); - return; - } - - // Allocate needed buffer for encoded message - encoded_message_p = Platform_malloc(WPC_PROTO_MAX_RESPONSE_SIZE); - if (encoded_message_p == NULL) - { - LOGE("Not enough memory for output buffer"); - Platform_free(message_StatusEvent_p, sizeof(wp_StatusEvent)); - return; - } - - message_wirepas.status_event = message_StatusEvent_p; - - *message_StatusEvent_p = (wp_StatusEvent){ - .version = GW_PROTO_MESSAGE_VERSION, - .state = wp_OnOffState_ON, // gateway state, always ONLINE - .configs_count = 1, - }; - - message_StatusEvent_p->has_gw_model = (strlen(Common_get_gateway_model()) != 0); - strcpy(message_StatusEvent_p->gw_model, Common_get_gateway_model()); - message_StatusEvent_p->has_gw_version = (strlen(Common_get_gateway_version()) != 0); - strcpy(message_StatusEvent_p->gw_version, Common_get_gateway_version()); - - Proto_config_fill_config(&message_StatusEvent_p->configs[0]); - - Common_fill_event_header(&message_StatusEvent_p->header); - - // Using the module static buffer - pb_ostream_t stream = pb_ostream_from_buffer(encoded_message_p, WPC_PROTO_MAX_RESPONSE_SIZE); - - /* Now we are ready to encode the message! */ - res = pb_encode(&stream, wp_GenericMessage_fields, &message); - - /* Release buffer as we don't need it anymore */ - Platform_free(message_StatusEvent_p, sizeof(wp_StatusEvent)); - - if (!res) { - LOGE("Encoding failed: %s\n", PB_GET_ERROR(&stream)); - } - else - { - LOGI("Msg size %d\n", stream.bytes_written); - if (m_onProtoEventStatus_cb != NULL) - { - m_onProtoEventStatus_cb(encoded_message_p, stream.bytes_written); - } - } - - Platform_free(encoded_message_p, WPC_PROTO_MAX_RESPONSE_SIZE); -} static int open_and_check_connection(unsigned long baudrate, const char * port_name) { @@ -143,12 +66,6 @@ app_proto_res_e WPC_Proto_initialize(const char * port_name, Common_init(gateway_id, gateway_model, gateway_version, sink_id); Proto_data_init(); Proto_config_init(); - - if (WPC_register_for_stack_status(onStackStatusReceived) != APP_RES_OK) - { - LOGE("Stack status already registered\n"); - return APP_RES_PROTO_ALREADY_REGISTERED; - } LOGI("WPC proto initialized with gw_id = %s, gw_model = %s, gw_version = %s and sink_id = %s\n", gateway_id, @@ -174,14 +91,7 @@ app_proto_res_e WPC_Proto_register_for_data_rx_event(onDataRxEvent_cb_f onDataRx app_proto_res_e WPC_Proto_register_for_event_status(onEventStatus_cb_f onProtoEventStatus_cb) { - // Only support one "client" for now - if (m_onProtoEventStatus_cb != NULL) - { - return APP_RES_PROTO_ALREADY_REGISTERED; - } - - m_onProtoEventStatus_cb = onProtoEventStatus_cb; - return APP_RES_PROTO_OK; + return Proto_config_register_for_event_status(onProtoEventStatus_cb); } app_proto_res_e WPC_Proto_handle_request(const uint8_t * request_p, @@ -322,60 +232,7 @@ app_proto_res_e WPC_Proto_get_current_event_status(bool online, uint8_t * event_status_p, size_t * event_status_size_p) { - bool status; - wp_GenericMessage message = wp_GenericMessage_init_zero; - wp_WirepasMessage message_wirepas = wp_WirepasMessage_init_zero; - message.wirepas = &message_wirepas; - - // Allocate the needed space for only the submessage we want to send - wp_StatusEvent * message_StatusEvent_p = Platform_malloc(sizeof(wp_StatusEvent)); - if (message_StatusEvent_p == NULL) - { - LOGE("Not enough memory to encode StatusEvent\n"); - return APP_RES_PROTO_NOT_ENOUGH_MEMORY; - } - - message_wirepas.status_event = message_StatusEvent_p; - - *message_StatusEvent_p = (wp_StatusEvent){ - .version = GW_PROTO_MESSAGE_VERSION, - .configs_count = 0, - }; - - if (online) - { - // Generate status event with state ONLINE - message_StatusEvent_p->state = wp_OnOffState_ON; - } - else - { - // Generate status event with state OFFLINE - message_StatusEvent_p->state = wp_OnOffState_OFF; - } - - message_StatusEvent_p->has_gw_model = (strlen(Common_get_gateway_model()) != 0); - strcpy(message_StatusEvent_p->gw_model, Common_get_gateway_model()); - message_StatusEvent_p->has_gw_version = (strlen(Common_get_gateway_version()) != 0); - strcpy(message_StatusEvent_p->gw_version, Common_get_gateway_version()); - - Common_fill_event_header(&message_StatusEvent_p->header); - - // Using the module static buffer - pb_ostream_t stream = pb_ostream_from_buffer(event_status_p, *event_status_size_p); - - /* Now we are ready to encode the message! */ - status = pb_encode(&stream, wp_GenericMessage_fields, &message); - - /* Release buffer as we don't need it anymore */ - Platform_free(message_StatusEvent_p, sizeof(wp_StatusEvent)); - - if (!status) { - LOGE("StatusEvent encoding failed: %s\n", PB_GET_ERROR(&stream)); - *event_status_size_p = 0; - return APP_RES_PROTO_CANNOT_GENERATE_RESPONSE; - } - - LOGI("Msg size %d\n", stream.bytes_written); - *event_status_size_p = stream.bytes_written; - return APP_RES_PROTO_OK; + return Proto_config_get_current_event_status(online, + event_status_p, + event_status_size_p); }