<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Error adding app-key to device on mesh provisioner</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/78737/error-adding-app-key-to-device-on-mesh-provisioner</link><description>Heelo, i&amp;#39;m trying to use a nordic nrf5340 as a provisioner for the remaining network however i&amp;#39;m getting an error on sending the app-key to the un-provisioned node (I&amp;#39;m using the mesh provisioner example as a guide). 
 Both the devices exchange the correct</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 24 Aug 2021 11:21:05 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/78737/error-adding-app-key-to-device-on-mesh-provisioner" /><item><title>RE: Error adding app-key to device on mesh provisioner</title><link>https://devzone.nordicsemi.com/thread/326345?ContentTypeID=1</link><pubDate>Tue, 24 Aug 2021 11:21:05 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:dfbf954c-c95d-46ab-aba4-5ce23876b62b</guid><dc:creator>DavidAbreu99</dc:creator><description>&lt;p&gt;The problem is fixed, for anyone this happens here&amp;#39;s my conclusion and my fix for it.&lt;/p&gt;
&lt;p&gt;In the &amp;quot;mesh_provisioner&amp;quot; example there is a function named &amp;quot;configure_node&amp;quot; called after the provisioner assigns a unicast address to the node and saves the node in the cdb. This function performs the configuration requests and sets on the new device such as &lt;strong&gt;send the app-key&lt;/strong&gt;, &lt;strong&gt;Get and decode the new device composition&lt;/strong&gt;&amp;nbsp;and &lt;strong&gt;bind the app-key to every model on the device&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;The problem lies in the back-end algorithm, this function calls the &amp;quot;cfg_cli.c&amp;quot; script like this:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&amp;quot;Send the app-key&amp;quot; - calls &amp;quot;bt_mesh_cfg_app_key_add&amp;quot; in the cfg_cli script&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The problem exists because of the semaphore on what i understand the code, the function returns a &lt;strong&gt;k_sem_take&lt;/strong&gt; that blocks the code. The code only continues to run after the timeout ends and in that way the&amp;nbsp;&lt;strong&gt;ack_ctx&amp;nbsp;&lt;/strong&gt;is cleared and we get a &amp;quot;no match&amp;quot; error every time.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:150%;text-decoration:underline;"&gt;&lt;em&gt;&lt;strong&gt;Fix&lt;/strong&gt;&lt;/em&gt;&lt;em&gt;&lt;strong&gt;:&lt;/strong&gt;&lt;/em&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:inherit;"&gt;To fix this issue i used a thread, instead of having the&amp;nbsp;&lt;strong&gt;configure_node&lt;/strong&gt; called by the interruptions on the process i created a variable and an array (Because i receive the device to provision by uart, i decided to create a queue in case the provision takes long than the command is received):&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:inherit;"&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;uint16_t node_queue[100];
int queue_counter = 0;&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:inherit;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:inherit;"&gt;Next i created an infinite loop in the function to run in threading (And a condition because i have a queue):&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:inherit;"&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static void configure_node()
{
    while (1)
    {
        if (queue_counter &amp;gt; 0)
        {
            struct bt_mesh_cdb_node *node = bt_mesh_cdb_node_get(node_queue[0]);
            ;
            NET_BUF_SIMPLE_DEFINE(buf, BT_MESH_RX_SDU_MAX);
            struct bt_mesh_comp_p0_elem elem;
            struct bt_mesh_cdb_app_key *key;
            struct bt_mesh_comp_p0 comp;
            uint8_t status;
            int err, elem_addr;

            printk(&amp;quot;Configuring node 0x%04x...\n&amp;quot;, node-&amp;gt;addr);

            key = bt_mesh_cdb_app_key_get(app_idx);
            if (key == NULL)
            {
                printk(&amp;quot;No app-key 0x%04x\n&amp;quot;, app_idx);
                return;
            }

            /* Add Application Key */
            err = bt_mesh_cfg_app_key_add(net_idx, node-&amp;gt;addr, net_idx, app_idx,
                key-&amp;gt;keys[0].app_key, &amp;amp;status);
            if (err || status)
            {
                printk(&amp;quot;Failed to add app-key (err %d status %d)\n&amp;quot;, err, status);
                return;
            }

            /* Get the node&amp;#39;s composition data */
            err = bt_mesh_cfg_comp_data_get(net_idx, node-&amp;gt;addr, 0, &amp;amp;status, &amp;amp;buf);
            if (err || status)
            {
                printk(&amp;quot;Failed to get Composition data (err %d, status: %d)\n&amp;quot;,
                    err, status);
                return;
            }

            err = bt_mesh_comp_p0_get(&amp;amp;comp, &amp;amp;buf);
            if (err)
            {
                printk(&amp;quot;Unable to parse composition data (err: %d)\n&amp;quot;, err);
                return;
            }

            /* Bind all models to the appkey */

            elem_addr = node-&amp;gt;addr;
            while (bt_mesh_comp_p0_elem_pull(&amp;amp;comp, &amp;amp;elem))
            {
                printk(&amp;quot;Element @ 0x%04x: %u + %u models\n&amp;quot;, elem_addr,
                    elem.nsig, elem.nvnd);
                for (int i = 0; i &amp;lt; elem.nsig; i++)
                {
                    uint16_t id = bt_mesh_comp_p0_elem_mod(&amp;amp;elem, i);

                    if (id == BT_MESH_MODEL_ID_CFG_CLI ||
                        id == BT_MESH_MODEL_ID_CFG_SRV)
                    {
                        continue;
                    }
                    printk(&amp;quot;Binding AppKey to model 0x%03x:%04x\n&amp;quot;,
                        elem_addr, id);

                    err = bt_mesh_cfg_mod_app_bind(net_idx, node-&amp;gt;addr,
                        elem_addr, app_idx, id,
                        &amp;amp;status);
                    if (err || status)
                    {
                        printk(&amp;quot;Failed (err: %d, status: %d)\n&amp;quot;, err,
                            status);
                    }
                }

                for (int i = 0; i &amp;lt; elem.nvnd; i++)
                {
                    struct bt_mesh_mod_id_vnd id =
                        bt_mesh_comp_p0_elem_mod_vnd(&amp;amp;elem, i);

                    printk(&amp;quot;Binding AppKey to model 0x%03x:%04x:%04x\n&amp;quot;,
                        elem_addr, id.company, id.id);

                    err = bt_mesh_cfg_mod_app_bind_vnd(net_idx, node-&amp;gt;addr,
                        elem_addr, app_idx,
                        id.id, id.company,
                        &amp;amp;status);
                    if (err || status)
                    {
                        printk(&amp;quot;Failed (err: %d, status: %d)\n&amp;quot;, err,
                            status);
                    }
                }

                elem_addr++;
            }

            atomic_set_bit(node-&amp;gt;flags, BT_MESH_CDB_NODE_CONFIGURED);

            if (IS_ENABLED(CONFIG_BT_SETTINGS))
            {
                bt_mesh_cdb_node_store(node);
            }

            printk(&amp;quot;Configuration complete\n&amp;quot;);

            for (int i = 0; i &amp;lt; queue_counter - 1; i++)
            {
                node_queue[i] = node_queue[i + 1];
            }

            
            queue_counter--;
        }

        k_msleep(2000);
    }
}&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:inherit;"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:inherit;"&gt;I assign that function to a thread with a 2048 memory allocation because 1024 was giving me memory error:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:inherit;"&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;K_THREAD_DEFINE(config_thread_id, 2048,
                configure_node, NULL, NULL, NULL,
                7, 0, 0);&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:inherit;"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:inherit;"&gt;I don&amp;#39;t know if it&amp;#39;s the better way to do things but it was the way i found it to work correctly.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:inherit;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:inherit;"&gt;My best regards to all,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:inherit;"&gt;David Abreu.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-size:150%;"&gt;&lt;em&gt;&lt;/em&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Error adding app-key to device on mesh provisioner</title><link>https://devzone.nordicsemi.com/thread/326029?ContentTypeID=1</link><pubDate>Fri, 20 Aug 2021 20:01:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8a8bac9c-e5cb-490b-931b-73e703c3e3d7</guid><dc:creator>DavidAbreu99</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I&amp;#39;m using nRF Connect SDK v1.6.0.&lt;/p&gt;
&lt;p&gt;Probably doesn&amp;#39;t count as custom firmware, i&amp;#39;m using the mesh provisioner example and instead of provision everything automatically when found i do some filtering based on uuid and only provision the device by command. The &amp;quot;back-end&amp;quot; is untouched.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Error adding app-key to device on mesh provisioner</title><link>https://devzone.nordicsemi.com/thread/326016?ContentTypeID=1</link><pubDate>Fri, 20 Aug 2021 15:47:29 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:423d3bfc-401e-487b-b5e3-f0d8a045a88f</guid><dc:creator>Mttrinh</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Sorry for the late reply.&lt;/p&gt;
&lt;p&gt;Could you provide me with which SDK and version you are using?&lt;/p&gt;
[quote user=""](I&amp;#39;m using the mesh provisioner example as a guide)[/quote]
&lt;p&gt;So this is your own custom firmware?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>