Skip to content

Commit

Permalink
Clean cache management
Browse files Browse the repository at this point in the history
Remove usage of stack status bits
Remove app config from config cache
Correct node_address type
Response to get config always ok
  • Loading branch information
StephaneTriomphe committed Jul 29, 2024
1 parent d72d2f1 commit 2f115ab
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 113 deletions.
199 changes: 88 additions & 111 deletions lib/wpc_proto/internal_modules/proto_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,25 @@
/** Structure to hold config from node */
typedef struct sink_config
{
app_addr_t node_address;
net_addr_t network_address;
bool CipherKeySet;
bool AuthenticationKeySet;
uint16_t stack_profile;
uint16_t hw_magic;
uint16_t ac_limit_min;
uint16_t ac_limit_max;
uint16_t app_config_max_size;
uint16_t version[4];
uint16_t ac_range_min_cur; // 0 means unset ?
uint16_t ac_range_max_cur;
uint8_t max_mtu;
uint8_t ch_range_min;
uint8_t ch_range_max;
uint8_t pdu_buffer_size;

bool CipherKeySet;
bool AuthenticationKeySet;
uint8_t StackStatus;
uint16_t ac_range_min_cur; // 0 means unset ?
uint16_t ac_range_max_cur;

app_role_t node_address;
app_role_t app_node_role;
wp_NodeRole wp_node_role; // same as app_node_role, different format
net_addr_t network_address;
net_channel_t network_channel;

msap_app_config_data_write_req_pl_t app_config;
app_role_t app_node_role;
} sink_config_t;

/* TODO : Protect config access */
Expand All @@ -63,22 +58,15 @@ typedef app_res_e (*func_2_param)(void * param1, void * param2);
* Pointer to second parameter (can be NULL)
* \param var_name
* Parameter name
* \param Not_condition
* if true, values are not read, no error returned
* \return True if correctly read, false otherwise
*/
static bool get_value_from_node(void * f,
void * var1,
void * var2,
char * var_name,
bool Not_condition)
char * var_name)
{
app_res_e res = APP_RES_INTERNAL_ERROR;
if (Not_condition)
{
// Values are not defined
return true;
}

if (var2 == NULL)
{
res = ((func_1_param) f)(var1);
Expand All @@ -97,102 +85,75 @@ static bool get_value_from_node(void * f,
return true;
}

static bool initialize_unmodifiable_variables()
void convert_role_to_proto_format(app_role_t role, wp_NodeRole * proto_role)
{
bool res = true;

res &= get_value_from_node(WPC_get_stack_profile, &m_sink_config.stack_profile, NULL, "Stack profile", false);
res &= get_value_from_node(WPC_get_hw_magic, &m_sink_config.hw_magic, NULL, "Hw magic", false);
res &= get_value_from_node(WPC_get_mtu, &m_sink_config.max_mtu, NULL, "MTU", false);
res &= get_value_from_node(WPC_get_pdu_buffer_size, &m_sink_config.pdu_buffer_size, NULL, "PDU Buffer Size", false);
res &= get_value_from_node(WPC_get_channel_limits, &m_sink_config.ch_range_min, &m_sink_config.ch_range_max,
"Channel Range", false);
res &= get_value_from_node(WPC_get_access_cycle_limits, &m_sink_config.ac_limit_min, &m_sink_config.ac_limit_max,
"AC Range", false);
res &= get_value_from_node(WPC_get_app_config_data_size, &m_sink_config.app_config_max_size, NULL,
"App Config Max size", false);

if (WPC_get_firmware_version(m_sink_config.version) == APP_RES_OK)
{
LOGI("Stack version is: %d.%d.%d.%d\n",
m_sink_config.version[0],
m_sink_config.version[1],
m_sink_config.version[2],
m_sink_config.version[3]);
}
else
pb_size_t flags_count = 0;
proto_role->role = GET_BASE_ROLE(role);
if (GET_ROLE_OPTIONS(role) & APP_ROLE_OPTION_LL)
{
res = false;
proto_role->flags[flags_count] = wp_NodeRole_RoleFlags_LOW_LATENCY;
flags_count++;
}

if (!res)
if (GET_ROLE_OPTIONS(role) & APP_ROLE_OPTION_AUTOROLE)
{
LOGE("All the static settings cannot be read\n");
proto_role->flags[flags_count] = wp_NodeRole_RoleFlags_AUTOROLE;
flags_count++;
}

return res;
proto_role->flags_count = flags_count;
}

static bool initialize_variables()
static bool initialize_config_variables()
{
_Static_assert(member_size(wp_NodeRole, flags) == (2 * sizeof(wp_NodeRole_RoleFlags)) );

bool res = true;
app_role_t role = 0;
pb_size_t flags_count = 0;

res &= get_value_from_node(WPC_get_stack_profile, &m_sink_config.stack_profile, NULL,
"Stack profile");
res &= get_value_from_node(WPC_get_hw_magic, &m_sink_config.hw_magic, NULL,
"Hw magic");
res &= get_value_from_node(WPC_get_mtu, &m_sink_config.max_mtu, NULL,
"MTU");
res &= get_value_from_node(WPC_get_pdu_buffer_size, &m_sink_config.pdu_buffer_size, NULL,
"PDU Buffer Size");
res &= get_value_from_node(WPC_get_channel_limits, &m_sink_config.ch_range_min, &m_sink_config.ch_range_max,
"Channel Range");
res &= get_value_from_node(WPC_get_access_cycle_limits, &m_sink_config.ac_limit_min, &m_sink_config.ac_limit_max,
"AC Range");
res &= get_value_from_node(WPC_get_app_config_data_size, &m_sink_config.app_config_max_size, NULL,
"App Config Max size");
res &= get_value_from_node(WPC_is_cipher_key_set, &m_sink_config.CipherKeySet, NULL,
"Cipher key set", false);
"Cipher key set");
res &= get_value_from_node(WPC_is_authentication_key_set, &m_sink_config.AuthenticationKeySet, NULL,
"Authentication key set", false);
res &= get_value_from_node(WPC_get_stack_status, &m_sink_config.StackStatus, NULL, "Stack Status", false);

"Authentication key set");
res &= get_value_from_node(WPC_get_stack_status, &m_sink_config.StackStatus, NULL,
"Stack Status");
res &= get_value_from_node(WPC_get_access_cycle_range, &m_sink_config.ac_range_min_cur, &m_sink_config.ac_range_max_cur,
"Current access cycle range", false);

"Current access cycle range");
res &= get_value_from_node(WPC_get_node_address, &m_sink_config.node_address, NULL,
"Network address", m_sink_config.StackStatus & APP_STACK_NODE_ADDRESS_NOT_SET);

res &= get_value_from_node(WPC_get_role, &role, NULL,
"Node role", m_sink_config.StackStatus & APP_STACK_ROLE_NOT_SET);
m_sink_config.app_node_role = role;
m_sink_config.wp_node_role.role = GET_BASE_ROLE(role);
if (GET_ROLE_OPTIONS(role) & APP_ROLE_OPTION_LL)
{
m_sink_config.wp_node_role.flags[flags_count] = wp_NodeRole_RoleFlags_LOW_LATENCY;
flags_count++;
}
if (GET_ROLE_OPTIONS(role) & APP_ROLE_OPTION_AUTOROLE)
{
m_sink_config.wp_node_role.flags[flags_count] = wp_NodeRole_RoleFlags_AUTOROLE;
flags_count++;
}
m_sink_config.wp_node_role.flags_count = flags_count;

"Network address");
res &= get_value_from_node(WPC_get_role, &m_sink_config.app_node_role, NULL,
"Node role");
res &= get_value_from_node(WPC_get_network_address, &m_sink_config.network_address, NULL,
"Network address", m_sink_config.StackStatus & APP_STACK_NETWORK_ADDRESS_NOT_SET);

"Network address");
res &= get_value_from_node(WPC_get_network_channel, &m_sink_config.network_channel, NULL,
"Network channel", m_sink_config.StackStatus & APP_STACK_NETWORK_CHANNEL_NOT_SET);
"Network channel");

if ( !(m_sink_config.StackStatus & APP_STACK_APP_DATA_NOT_SET))
if (WPC_get_firmware_version(m_sink_config.version) == APP_RES_OK)
{
uint16_t diag_data_interval = 0 ; // needed to avoid trouble with pointer alignement
if (WPC_get_app_config_data(&m_sink_config.app_config.sequence_number,
&diag_data_interval,
m_sink_config.app_config.app_config_data,
m_sink_config.app_config_max_size)
!= APP_RES_OK)
{
LOGE("Cannot get App config data from node\n");
res = false;
}
m_sink_config.app_config.diag_data_interval = diag_data_interval;
LOGI("Stack version is: %d.%d.%d.%d\n",
m_sink_config.version[0],
m_sink_config.version[1],
m_sink_config.version[2],
m_sink_config.version[3]);
}
else
{
res = false;
}

if (!res)
{
LOGE("All the variable settings cannot be read\n");
LOGE("All the settings cannot be read\n");
}

return res;
Expand All @@ -208,17 +169,14 @@ void Proto_config_fill_config(wp_SinkReadConfig * config_p)

*config_p = (wp_SinkReadConfig){
/* Sink minimal config */
.has_node_role = ((status & APP_STACK_ROLE_NOT_SET) ? false : true),
.node_role = m_sink_config.wp_node_role,
.has_node_address = ((status & APP_STACK_NODE_ADDRESS_NOT_SET) ? false : true),
.has_node_role = true,
.has_node_address = true,
.node_address = m_sink_config.node_address,
.has_network_address = ((status & APP_STACK_NETWORK_ADDRESS_NOT_SET) ? false : true),
.has_network_address = true,
.network_address = m_sink_config.network_address,
.has_network_channel = ((status & APP_STACK_NETWORK_CHANNEL_NOT_SET) ? false : true),
.has_network_channel = true,
.network_channel = m_sink_config.network_channel,
.has_app_config = ((status & APP_STACK_APP_DATA_NOT_SET) ? false : true),
.app_config = { .diag_interval_s = m_sink_config.app_config.diag_data_interval,
.seq = m_sink_config.app_config.sequence_number},
.has_app_config = true,
.has_channel_map = false,
.has_are_keys_set = true,
.are_keys_set = m_sink_config.CipherKeySet
Expand Down Expand Up @@ -259,18 +217,34 @@ void Proto_config_fill_config(wp_SinkReadConfig * config_p)
.has_firmware_area_id = false,
.has_target_and_action = false
};
memcpy(config_p->app_config.app_config_data,
m_sink_config.app_config.app_config_data, MAXIMUM_APP_CONFIG_SIZE);

strcpy(config_p->sink_id, Common_get_sink_id());

convert_role_to_proto_format(m_sink_config.app_node_role, &config_p->node_role);

if (MAXIMUM_APP_CONFIG_SIZE >= m_sink_config.app_config_max_size)
{
uint16_t diag_data_interval = 0; // needed to avoid trouble with pointer alignement
uint8_t seq = 0;

if (WPC_get_app_config_data(&seq,
&diag_data_interval,
config_p->app_config.app_config_data,
m_sink_config.app_config_max_size)
!= APP_RES_OK)
{
LOGE("Cannot get App config data from node\n");
config_p->has_app_config = false;
}
config_p->app_config.seq = seq;
config_p->app_config.diag_interval_s = diag_data_interval;
}
}

bool Proto_config_init(void)
{
/* Read initial config from sink */
initialize_unmodifiable_variables();
initialize_variables();
return true;
return initialize_config_variables();
}

app_proto_res_e Proto_config_handle_set_config(wp_SetConfigReq *req,
Expand All @@ -290,14 +264,17 @@ app_proto_res_e Proto_config_handle_get_configs(wp_GetConfigsReq *req,
res = WPC_get_stack_status(&m_sink_config.StackStatus);
if (res != APP_RES_OK)
{
LOGE("Get stack status failed\n");
LOGE("WPC_get_config failed, res=%d\n",res);
}
else
{
LOGI("WPC_get_config success\n");
}

LOGE("WPC_get_config res=%d\n", res);

// as config is available in cache, response is ok
Common_Fill_response_header(&resp->header,
req->header.req_id,
Common_convert_error_code(res));
Common_convert_error_code(APP_RES_OK));
resp->configs_count = 1;
Proto_config_fill_config(&resp->configs[0]);

Expand Down
2 changes: 1 addition & 1 deletion lib/wpc_proto/internal_modules/proto_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "wpc_proto.h"

/**
* \brief Intialize the module in charge of data handling congig
* \brief Intialize the module in charge of data handling config
* \return True if successful, False otherwise
*/
bool Proto_config_init(void);
Expand Down
2 changes: 1 addition & 1 deletion lib/wpc_proto/wpc_proto.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ app_proto_res_e WPC_Proto_initialize(const char * port_name,
Proto_data_init();
Proto_config_init();

LOGI("WPC proto initialize with gw_id = %s, gw_model = %s, gw_version = %s and sink_id = %s\n",
LOGI("WPC proto initialized with gw_id = %s, gw_model = %s, gw_version = %s and sink_id = %s\n",
gateway_id,
gateway_model,
gateway_version,
Expand Down

0 comments on commit 2f115ab

Please sign in to comment.