Skip to content

Commit

Permalink
Move StatusEvent implementation inside the config module
Browse files Browse the repository at this point in the history
  • Loading branch information
GwendalRaoul committed Jul 30, 2024
1 parent 38fbd19 commit d03e1a6
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 190 deletions.
232 changes: 195 additions & 37 deletions lib/wpc_proto/internal_modules/proto_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));
Expand All @@ -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());
Expand All @@ -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()
Expand All @@ -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();
}
Expand Down Expand Up @@ -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;
}
}

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;
}
13 changes: 7 additions & 6 deletions lib/wpc_proto/internal_modules/proto_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
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
Loading

0 comments on commit d03e1a6

Please sign in to comment.