tools/mesh-cfgclient: Disallow model commands w/o composition
authorInga Stotland <inga.stotland@intel.com>
Thu, 23 Sep 2021 03:25:52 +0000 (20:25 -0700)
committerAyush Garg <ayush.garg@samsung.com>
Fri, 11 Mar 2022 13:38:37 +0000 (19:08 +0530)
If remote node's composition hasn't been acquired, disallow commands
that change model state (that is, bindings, subscriptions, publications).
Prompt to run "get-composition" command first.

Signed-off-by: Anuj Jain <anuj01.jain@samsung.com>
Signed-off-by: Ayush Garg <ayush.garg@samsung.com>
tools/mesh/cfgcli.c
tools/mesh/mesh-db.c
tools/mesh/remote.c
tools/mesh/remote.h

index 71bf2e7..19a4294 100644 (file)
@@ -434,6 +434,9 @@ static bool msg_recvd(uint16_t src, uint16_t idx, uint8_t *data,
 
                if (!mesh_db_node_set_composition(src, data, len))
                        bt_shell_printf("Failed to save node composition!\n");
+               else
+                       remote_set_composition(src, true);
+
                break;
 
        case OP_APPKEY_STATUS:
@@ -1233,6 +1236,12 @@ static void cmd_bind(uint32_t opcode, int argc, char *argv[])
                return bt_shell_noninteractive_quit(EXIT_FAILURE);
        }
 
+       if (!remote_has_composition(target)) {
+               bt_shell_printf("Node composition is unknown\n");
+               bt_shell_printf("Call \"get-composition\" first\n");
+               return bt_shell_noninteractive_quit(EXIT_FAILURE);
+       }
+
        n = mesh_opcode_set(opcode, msg);
 
        put_le16(parms[0], msg + n);
@@ -1429,6 +1438,12 @@ static void cmd_pub_set(int argc, char *argv[])
                return bt_shell_noninteractive_quit(EXIT_FAILURE);
        }
 
+       if (!remote_has_composition(target)) {
+               bt_shell_printf("Node composition is unknown\n");
+               bt_shell_printf("Call \"get-composition\" first\n");
+               return bt_shell_noninteractive_quit(EXIT_FAILURE);
+       }
+
        pub_addr = parms[1];
 
        grp = l_queue_find(groups, match_group_addr, L_UINT_TO_PTR(pub_addr));
@@ -1523,6 +1538,12 @@ static void subscription_cmd(int argc, char *argv[], uint32_t opcode)
                return bt_shell_noninteractive_quit(EXIT_FAILURE);
        }
 
+       if (!remote_has_composition(target)) {
+               bt_shell_printf("Node composition is unknown\n");
+               bt_shell_printf("Call \"get-composition\" first\n");
+               return bt_shell_noninteractive_quit(EXIT_FAILURE);
+       }
+
        sub_addr = parms[1];
 
        grp = l_queue_find(groups, match_group_addr, L_UINT_TO_PTR(sub_addr));
@@ -1722,6 +1743,11 @@ static void cmd_hb_sub_set(int argc, char *argv[])
        uint8_t msg[32];
        uint32_t parm_cnt;
 
+       if (IS_UNASSIGNED(target)) {
+               bt_shell_printf("Destination not set\n");
+               return bt_shell_noninteractive_quit(EXIT_FAILURE);
+       }
+
        n = mesh_opcode_set(OP_CONFIG_HEARTBEAT_SUB_SET, msg);
 
        parm_cnt = read_input_parameters(argc, argv);
index d8f11ef..b2dc58e 100644 (file)
@@ -576,11 +576,17 @@ static void load_remotes(json_object *jcfg)
                        remote_update_app_key(unicast, key_idx, updated, false);
                }
 
-               load_composition(jnode, unicast);
+               if (!load_composition(jnode, unicast))
+                       continue;
 
-               node_count++;
+               /* If "crpl" is present, composition's is available */
+               jval = NULL;
+               if (json_object_object_get_ex(jnode, "crpl", &jval) && jval)
+                       remote_set_composition(unicast, true);
 
                /* TODO: Add the rest of the configuration */
+
+               node_count++;
        }
 
        if (node_count != sz)
index 5f598cb..2f8493f 100644 (file)
@@ -35,6 +35,7 @@ struct remote_node {
        struct l_queue *net_keys;
        struct l_queue *app_keys;
        struct l_queue **els;
+       bool comp;
        uint8_t uuid[16];
        uint8_t num_ele;
 };
@@ -192,6 +193,28 @@ bool remote_set_model(uint16_t unicast, uint8_t ele_idx, uint32_t mod_id,
        return true;
 }
 
+void remote_set_composition(uint16_t addr, bool comp)
+{
+       struct remote_node *rmt;
+
+       rmt = l_queue_find(nodes, match_node_addr, L_UINT_TO_PTR(addr));
+       if (!rmt)
+               return;
+
+       rmt->comp = comp;
+}
+
+bool remote_has_composition(uint16_t addr)
+{
+       struct remote_node *rmt;
+
+       rmt = l_queue_find(nodes, match_node_addr, L_UINT_TO_PTR(addr));
+       if (!rmt)
+               return false;
+
+       return rmt->comp;
+}
+
 bool remote_add_net_key(uint16_t addr, uint16_t net_idx, bool save)
 {
        struct remote_node *rmt;
index 9feee86..965c994 100644 (file)
@@ -24,6 +24,8 @@ bool remote_add_app_key(uint16_t addr, uint16_t app_idx, bool save);
 bool remote_update_app_key(uint16_t addr, uint16_t app_idx, bool update,
                                                                bool save);
 void remote_finish_key_refresh(uint16_t addr, uint16_t net_idx);
+void remote_set_composition(uint16_t addr, bool comp);
+bool remote_has_composition(uint16_t addr);
 bool remote_del_app_key(uint16_t addr, uint16_t app_idx);
 uint16_t remote_get_subnet_idx(uint16_t addr);
 void remote_print_node(uint16_t addr);