Code re-org and clean up
cover Code re-org and clean up
This patch set is part of the on-going clean up effort that targets some of more convoluted areas of meshd source code.
The patchset provides better functional separation, smaller function granularity and better memory handling.
Inga Stotland (3):
mesh: Use static array to hold config server response
mesh: Add size checks for every opcode in config server
mesh: move model functionality out of node.c to model.c
mesh/cfgmod-server.c | 99 ++++++----- mesh/cfgmod.h | 4 +- mesh/mesh-config-json.c | 5 +- mesh/model.c | 359 +++++++++++++++++++++++++++++++++------- mesh/model.h | 32 ++-- mesh/node.c | 286 ++++---------------------------- 6 files changed, 403 insertions(+), 382 deletions(-)
Signature · 1 line
2.26.2
PATCH 1/3 mesh: Use static array to hold config server response
This eliminates dynamic allocation for long responses and local arrays for short responses. Instead, aclear text response from config server is written into a static buffer and then encoded into dynamically allocated messafe buffer to use in actual transmission.
--- mesh/cfgmod-server.c | 53 +++++++++++++------------------------------- 1 file changed, 16 insertions(+), 37 deletions(-)
mesh/cfgmod-server.c +16 -37
@@ -32,21 +32,20 @@#include "mesh/mesh-config.h" #include "mesh/cfgmod.h" -#define CFG_MAX_MSG_LEN 380 -/* Supported composition pages, sorted high to low */ /* Only page 0 is currently supported */ static const uint8_t supported_pages[] = {0 }; +static uint8_t msg[MAX_MSG_LEN ]; +static void send_pub_status (struct mesh_node *node,uint16_t net_idx,uint16_t src,uint16_t dst,uint8_t status,uint16_t ele_addr,uint32_t mod_id,uint16_t pub_addr,uint16_t idx,bool cred_flag,uint8_t ttl,uint8_t period,uint8_t retransmit) { -uint8_t msg[16 ];size_t n; n =mesh_model_opcode_set (OP_CONFIG_MODEL_PUB_STATUS , msg);
@@ -193,7 +192,6 @@ static void send_sub_status(struct mesh_node *node, uint16_t net_idx, uint8_t status, uint16_t ele_addr, uint16_t addr, uint32_t mod) { - uint8_t msg[12]; int n = mesh_model_opcode_set(OP_CONFIG_MODEL_SUB_STATUS, msg); msg[n++] = status;
@@ -224,7 +222,6 @@ static bool config_sub_get(struct mesh_node *node, uint16_t net_idx,int status;uint8_t *msg_status;uint16_t buf_size; -uint8_t msg[5 +sizeof (uint16_t ) *MAX_GRP_PER_MOD ];/* Incoming message has already been size-checked */ ele_addr =l_get_le16 (pkt);
@@ -430,7 +427,6 @@ static void send_model_app_status(struct mesh_node *node, uint16_t net_idx, uint8_t status, uint16_t addr, uint32_t id, uint16_t idx) { - uint8_t msg[12]; size_t n = mesh_model_opcode_set(OP_MODEL_APP_STATUS, msg); msg[n++] = status;
@@ -455,21 +451,14 @@ static void model_app_list(struct mesh_node *node, uint16_t net_idx, {uint16_t ele_addr;uint32_t mod_id =0xffff ; -uint8_t *msg =NULL ;uint8_t *status; -uint16_t n, buf_size; +uint16_t n;int result; - buf_size =MAX_BINDINGS *sizeof (uint16_t ); - msg =l_malloc (7 + buf_size); -if (!msg) -return ; - ele_addr =l_get_le16 (pkt);switch (size) {default : -l_free (msg);return ;case 4 : n =mesh_model_opcode_set (OP_MODEL_APP_LIST , msg);
@@ -495,7 +484,7 @@ static void model_app_list(struct mesh_node *node, uint16_t net_idx, result =mesh_model_get_bindings (node, ele_addr, mod_id, msg + n, - buf_size, &size); +MAX_MSG_LEN - n, &size); n += size;if (result >=0 ) {
@@ -503,8 +492,6 @@ static void model_app_list(struct mesh_node *node, uint16_t net_idx,mesh_model_send (node, dst, src,APP_IDX_DEV_LOCAL , net_idx,DEFAULT_TTL , false, msg, n); } - -l_free (msg); } static bool model_app_bind(struct mesh_node *node, uint16_t net_idx,
@@ -736,8 +723,6 @@ static bool cfg_srv_pkt(uint16_t src, uint16_t dst, uint16_t app_idx,struct timeval time_now;uint32_t opcode, tmp32;int b_res =MESH_STATUS_SUCCESS ; -uint8_t msg[11 ]; -uint8_t *long_msg =NULL ;struct mesh_net_heartbeat *hb;uint16_t n_idx, a_idx;uint8_t state, status;
@@ -771,9 +756,8 @@ static bool cfg_srv_pkt(uint16_t src, uint16_t dst, uint16_t app_idx,if (size !=1 )return false; - long_msg =l_malloc (CFG_MAX_MSG_LEN ); - n =mesh_model_opcode_set (OP_DEV_COMP_STATUS , long_msg); - n +=get_composition (node, pkt[0 ], long_msg + n); + n =mesh_model_opcode_set (OP_DEV_COMP_STATUS , msg); + n +=get_composition (node, pkt[0 ], msg + n);break ;
@@ -1040,14 +1024,13 @@ static bool cfg_srv_pkt(uint16_t src, uint16_t dst, uint16_t app_idx, n_idx =l_get_le16 (pkt); - long_msg =l_malloc (CFG_MAX_MSG_LEN ); - n =mesh_model_opcode_set (OP_APPKEY_LIST , long_msg); + n =mesh_model_opcode_set (OP_APPKEY_LIST , msg); - status =appkey_list (net, n_idx, long_msg + n +3 , -CFG_MAX_MSG_LEN - n -3 , &size); + status =appkey_list (net, n_idx, msg + n +3 , +MAX_MSG_LEN - n -3 , &size); - long_msg[n] = status; -l_put_le16 (n_idx, long_msg + n +1 ); + msg[n] = status; +l_put_le16 (n_idx, msg + n +1 ); n += (size +3 );break ;
@@ -1088,11 +1071,10 @@ static bool cfg_srv_pkt(uint16_t src, uint16_t dst, uint16_t app_idx,break ;case OP_NETKEY_GET : - long_msg =l_malloc (CFG_MAX_MSG_LEN ); - n =mesh_model_opcode_set (OP_NETKEY_LIST , long_msg); - size =CFG_MAX_MSG_LEN - n; + n =mesh_model_opcode_set (OP_NETKEY_LIST , msg); + size =MAX_MSG_LEN - n; -if (mesh_net_key_list_get (net, long_msg + n, &size)) +if (mesh_net_key_list_get (net, msg + n, &size)) n += size;else n =0 ;
@@ -1244,11 +1226,8 @@ static bool cfg_srv_pkt(uint16_t src, uint16_t dst, uint16_t app_idx, }if (n) -mesh_model_send (node, dst, src, -APP_IDX_DEV_LOCAL , net_idx,DEFAULT_TTL , false, - long_msg ? long_msg : msg, n); - -l_free (long_msg); +mesh_model_send (node, dst, src,APP_IDX_DEV_LOCAL , net_idx, +DEFAULT_TTL , false, msg, n);return true; }
Signature · 1 line
2.26.2
PATCH 2/3 mesh: Add size checks for every opcode in config server
In reply to Inga StotlandThis adds missing size checks for the incoming config server messages.
--- mesh/cfgmod-server.c | 46 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-)
mesh/cfgmod-server.c +37 -9
@@ -754,7 +754,7 @@ static bool cfg_srv_pkt(uint16_t src, uint16_t dst, uint16_t app_idx,case OP_DEV_COMP_GET :if (size !=1 ) -return false; +return true; n =mesh_model_opcode_set (OP_DEV_COMP_STATUS , msg); n +=get_composition (node, pkt[0 ], msg + n);
@@ -770,6 +770,9 @@ static bool cfg_srv_pkt(uint16_t src, uint16_t dst, uint16_t app_idx,/* Fall Through */ case OP_CONFIG_DEFAULT_TTL_GET : +if (opcode ==OP_CONFIG_DEFAULT_TTL_GET && size !=0 ) +return true; +l_debug ("Get/Set Default TTL" ); n =mesh_model_opcode_set (OP_CONFIG_DEFAULT_TTL_STATUS , msg);
@@ -792,6 +795,8 @@ static bool cfg_srv_pkt(uint16_t src, uint16_t dst, uint16_t app_idx,break ;case OP_CONFIG_MODEL_PUB_GET : +if (size !=4 && size !=6 ) +return true;config_pub_get (node, net_idx, src, dst, pkt, size);break ;
@@ -832,6 +837,9 @@ static bool cfg_srv_pkt(uint16_t src, uint16_t dst, uint16_t app_idx,/* Fall Through */ case OP_CONFIG_RELAY_GET : +if (opcode ==OP_CONFIG_RELAY_GET && size !=0 ) +return true; + n =mesh_model_opcode_set (OP_CONFIG_RELAY_STATUS , msg); msg[n++] =node_relay_mode_get (node, &count, &interval);
@@ -853,6 +861,9 @@ static bool cfg_srv_pkt(uint16_t src, uint16_t dst, uint16_t app_idx,/* Fall Through */ case OP_CONFIG_NETWORK_TRANSMIT_GET : +if (opcode ==OP_CONFIG_NETWORK_TRANSMIT_GET && size !=0 ) +return true; + n =mesh_model_opcode_set (OP_CONFIG_NETWORK_TRANSMIT_STATUS , msg);mesh_net_transmit_params_get (net, &count, &interval);
@@ -869,6 +880,9 @@ static bool cfg_srv_pkt(uint16_t src, uint16_t dst, uint16_t app_idx,/* Fall Through */ case OP_CONFIG_PROXY_GET : +if (opcode ==OP_CONFIG_PROXY_GET && size !=0 ) +return true; + n =mesh_model_opcode_set (OP_CONFIG_PROXY_STATUS , msg); msg[n++] =node_proxy_mode_get (node);
@@ -883,9 +897,7 @@ static bool cfg_srv_pkt(uint16_t src, uint16_t dst, uint16_t app_idx,if (n_idx >0xfff )return true; -/* -* Currently no support for proxy: node identity not supported -*/ +/* Currently setting node identity not supported */ /* Fall Through */
@@ -918,6 +930,9 @@ static bool cfg_srv_pkt(uint16_t src, uint16_t dst, uint16_t app_idx,/* Fall Through */ case OP_CONFIG_BEACON_GET : +if (opcode ==OP_CONFIG_BEACON_GET && size !=0 ) +return true; + n =mesh_model_opcode_set (OP_CONFIG_BEACON_STATUS , msg); msg[n++] =node_beacon_mode_get (node);
@@ -932,6 +947,8 @@ static bool cfg_srv_pkt(uint16_t src, uint16_t dst, uint16_t app_idx,/* Fall Through */ case OP_CONFIG_FRIEND_GET : +if (opcode ==OP_CONFIG_FRIEND_GET && size !=0 ) +return true; n =mesh_model_opcode_set (OP_CONFIG_FRIEND_STATUS , msg);
@@ -1071,13 +1088,14 @@ static bool cfg_srv_pkt(uint16_t src, uint16_t dst, uint16_t app_idx,break ;case OP_NETKEY_GET : +if (size !=0 ) +return true; + n =mesh_model_opcode_set (OP_NETKEY_LIST , msg); size =MAX_MSG_LEN - n;if (mesh_net_key_list_get (net, msg + n, &size)) n += size; -else - n =0 ;break ;case OP_MODEL_APP_BIND :
@@ -1089,21 +1107,22 @@ static bool cfg_srv_pkt(uint16_t src, uint16_t dst, uint16_t app_idx,case OP_VEND_MODEL_APP_GET :if (size !=6 )return true; +model_app_list (node, net_idx, src, dst, pkt, size);break ;case OP_MODEL_APP_GET :if (size !=4 )return true; +model_app_list (node, net_idx, src, dst, pkt, size);break ;case OP_CONFIG_HEARTBEAT_PUB_SET :l_debug ("OP_CONFIG_HEARTBEAT_PUB_SET" ); -if (size !=9 ) { -l_debug ("bad size %d" , size); +if (size !=9 )return true; - } +if (pkt[2 ] >0x11 || pkt[3 ] >0x10 || pkt[4 ] >0x7f )return true;else if (IS_VIRTUAL (l_get_le16 (pkt)))
@@ -1150,6 +1169,9 @@ static bool cfg_srv_pkt(uint16_t src, uint16_t dst, uint16_t app_idx,break ;case OP_CONFIG_HEARTBEAT_PUB_GET : +if (size !=0 ) +return true; + n =mesh_model_opcode_set (OP_CONFIG_HEARTBEAT_PUB_STATUS , msg); msg[n++] = b_res;l_put_le16 (hb->pub_dst, msg + n);
@@ -1179,6 +1201,9 @@ static bool cfg_srv_pkt(uint16_t src, uint16_t dst, uint16_t app_idx,/* Fall through */ case OP_CONFIG_HEARTBEAT_SUB_GET : +if (opcode ==OP_CONFIG_HEARTBEAT_SUB_GET && size !=0 ) +return true; +gettimeofday (&time_now,NULL ); time_now.tv_sec -= hb->sub_start;
@@ -1218,6 +1243,9 @@ static bool cfg_srv_pkt(uint16_t src, uint16_t dst, uint16_t app_idx,break ;case OP_NODE_RESET : +if (size !=0 ) +return true; + n =mesh_model_opcode_set (OP_NODE_RESET_STATUS , msg);/* Delay node removal to give it a chance to send the status */
Signature · 1 line
2.26.2
PATCH 3/3 mesh: move model functionality out of node.c to model.c
In reply to Inga StotlandThis moves the model related code out of node.c to model.c providing for better functional separation of modules.
--- mesh/cfgmod.h | 4 +- mesh/mesh-config-json.c | 5 +- mesh/model.c | 359 +++++++++++++++++++++++++++++++++------- mesh/model.h | 32 ++-- mesh/node.c | 286 ++++---------------------------- 5 files changed, 350 insertions(+), 336 deletions(-)
mesh/cfgmod.h +2 -2
@@ -17,8 +17,8 @@* */ -#define CONFIG_SRV_MODEL (VENDOR_ID_MASK | 0x0000) -#define CONFIG_CLI_MODEL (VENDOR_ID_MASK | 0x0001) +#define CONFIG_SRV_MODEL 0x0000 +#define CONFIG_CLI_MODEL 0x0001/* New List */ #define OP_APPKEY_ADD 0x00
mesh/mesh-config-json.c +1 -4
@@ -1126,18 +1126,15 @@ static bool parse_models(json_object *jmodels, struct mesh_config_element *ele)if (sscanf (str,"%04x" , &id) !=1 ) goto fail; - id |=VENDOR_ID_MASK ; }else if (len ==8 ) {if (sscanf (str,"%08x" , &id) !=1 ) goto fail; + mod->vendor = true; }else goto fail; mod->id = id; -if (len ==8 ) - mod->vendor = true; -if (json_object_object_get_ex (jmodel,"bind" , &jarray)) {if (json_object_get_type (jarray) != json_type_array || !parse_bindings (jarray, mod))
mesh/model.c +299 -60
480 lines not on this page · raw message
mesh/model.h +19 -13
@@ -60,14 +60,20 @@ struct mesh_model_ops {mesh_model_sub_cb sub; }; -struct mesh_model *mesh_model_new (uint8_t ele_idx,uint32_t mod_id); +bool mesh_model_add (struct mesh_node *node,uint8_t ele_idx,uint16_t mod_id, +struct l_dbus_message_iter *opts); +bool mesh_model_vendor_add (struct mesh_node *node,uint8_t ele_idx, +uint16_t vendor_id,uint16_t mod_id, +struct l_dbus_message_iter *opts);void mesh_model_free (void *data);uint32_t mesh_model_get_model_id (const struct mesh_model *model);bool mesh_model_register (struct mesh_node *node,uint8_t ele_idx, -uint32_t mod_id,const struct mesh_model_ops *cbs, +uint16_t mod_id,const struct mesh_model_ops *cbs,void *user_data); -struct mesh_model *mesh_model_setup (struct mesh_node *node,uint8_t ele_idx, -void *data); +bool mesh_model_add_from_storage (struct mesh_node *node,uint8_t ele_idx, +struct l_queue *mods,struct l_queue *db_mods); +void mesh_model_convert_to_storage (struct l_queue *db_mods, +struct l_queue *mods);struct mesh_model_pub *mesh_model_pub_get (struct mesh_node *node,uint16_t addr,uint32_t mod_id,int *status); int mesh_model_pub_set(struct mesh_node *node, uint16_t addr, uint32_t id,
@@ -95,8 +101,11 @@ bool mesh_model_send(struct mesh_node *node, uint16_t src, uint16_t dst, uint16_t app_idx, uint16_t net_idx, uint8_t ttl, bool segmented, const void *msg, uint16_t msg_len); -int mesh_model_publish (struct mesh_node *node,uint32_t mod_id,uint16_t src, +int mesh_model_publish (struct mesh_node *node,uint16_t mod_id,uint16_t src,uint8_t ttl,const void *msg,uint16_t msg_len); +int mesh_model_vendor_publish (struct mesh_node *node,uint16_t vendor_id, +uint16_t mod_id,uint16_t src,uint8_t ttl, +const void *msg,uint16_t msg_len); bool mesh_model_rx(struct mesh_node *node, bool szmict, uint32_t seq0, uint32_t seq, uint32_t iv_index, uint16_t net_idx, uint16_t src, uint16_t dst, uint8_t key_aid,
@@ -108,13 +117,10 @@ struct l_queue *mesh_model_get_appkeys(struct mesh_node *node);uint16_t mesh_model_opcode_set (uint32_t opcode,uint8_t *buf);bool mesh_model_opcode_get (const uint8_t *buf,uint16_t size,uint32_t *opcode,uint16_t *n); -void model_build_config (void *model,void *msg_builder); - -void mesh_model_enable_pub (struct mesh_model *mod,bool enable); -bool mesh_model_is_pub_enabled (struct mesh_model *mod); -void mesh_model_enable_sub (struct mesh_node *node,struct mesh_model *mod, -bool enable); -bool mesh_model_is_sub_enabled (struct mesh_model *mod); - +void mesh_model_build_config (void *model,void *msg_builder); +void mesh_model_update_opts (struct mesh_node *node,uint8_t ele_idx, +struct l_queue *curr,struct l_queue *updated); +uint16_t mesh_model_generate_composition (struct l_queue *mods,uint16_t buf_sz, +uint8_t *buf);void mesh_model_init (void );void mesh_model_cleanup (void );
mesh/node.c +29 -257
429 lines not on this page · raw message
Signature · 1 line
2.26.2
Applied first two of patchset.
Holding off on the 3rd until I can more test more completely.
Quoted 18 lines · Inga Stotland
This patch set is part of the on-going clean up effort that targets some of more convoluted areas of meshd source code.
The patchset provides better functional separation, smaller function granularity and better memory handling.Inga Stotland (3):
mesh: Use static array to hold config server response
mesh: Add size checks for every opcode in config server
mesh: move model functionality out ofnode.ctomodel.cmesh/cfgmod-server.c | 99 ++++++----- mesh/cfgmod.h | 4 +- mesh/mesh-config-json.c | 5 +- mesh/model.c | 359 +++++++++++++++++++++++++++++++++------- mesh/model.h | 32 ++-- mesh/node.c | 286 ++++---------------------------- 6 files changed, 403 insertions(+), 382 deletions(-)