--- mesh/node.c | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/mesh/node.c b/mesh/node.c index 6fe70742d..f9a2d5722 100644 --- a/mesh/node.c +++ b/mesh/node.c @@ -47,6 +47,12 @@ #define MIN_COMP_SIZE 14 +/* Composition data header size contains the length of belows: + * CID, PID, VID, CRPL and Feature bits + */ +#define COMP_HDR_SIZE 10 +#define COMP_FEATURE_BITS_SIZE 2 + #define MESH_NODE_PATH_PREFIX "/node" /* Default values for a new locally created node */ @@ -1394,15 +1400,39 @@ static bool check_req_node(struct managed_obj_request *req) uint16_t attach_len = node_generate_comp(req->attach, attach_comp, sizeof(attach_comp)); - /* Ignore feature bits in Composition Compare */ - node_comp[8] = 0; - attach_comp[8] = 0; - + /* Ignore CID, VID, PID, CRPL and feature bits + * in Composition Compare + */ if (node_len != attach_len || - memcmp(node_comp, attach_comp, node_len)) { + memcmp(node_comp + COMP_HDR_SIZE, + attach_comp + COMP_HDR_SIZE, + node_len - COMP_HDR_SIZE)) { l_debug("Failed to verify app's composition data"); return false; } + + /* Compare CID, VID, PID and CRPL */ + if (!memcmp(node_comp, attach_comp, + COMP_HDR_SIZE - COMP_FEATURE_BITS_SIZE)) + return true; + + l_debug("Composition data update"); + + if (!mesh_config_write_comp(req->attach->cfg, + req->node->comp.cid, req->node->comp.pid, + req->node->comp.vid, req->node->comp.crpl)) { + + l_debug("Failed to update composition data"); + return false; + } + + if (!mesh_config_save(req->attach->cfg, true, NULL, NULL)) { + l_debug("Failed to store composition data"); + return false; + } + + memcpy(&req->attach->comp, &req->node->comp, + sizeof(struct node_composition)); } return true; -- 2.20.1