<?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>Multi-NUS: Sending several messages in a row</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/110250/multi-nus-sending-several-messages-in-a-row</link><description>Hi, 
 
 
 In my setup I have one central where several peripherals are connected. I&amp;#39;m using the Multi-NUS service to communicate from the central to the peripherals. Here you see a snippet of sending a broadcast to all peripherals. I&amp;#39;m using a single</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 18 Apr 2024 19:24:22 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/110250/multi-nus-sending-several-messages-in-a-row" /><item><title>RE: Multi-NUS: Sending several messages in a row</title><link>https://devzone.nordicsemi.com/thread/479538?ContentTypeID=1</link><pubDate>Thu, 18 Apr 2024 19:24:22 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:96e50418-55ca-4095-9a37-00818a2a4af7</guid><dc:creator>Phobios</dc:creator><description>&lt;p&gt;Hi Jared,&lt;/p&gt;
&lt;p&gt;thanks a lot; I tried it and it works fine. I have altered it a bit and I am using&amp;nbsp;bt_conn_index(...) to get the connection index, then I only need the semaphore array and not the&amp;nbsp;bt_nus_client array.&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;pre class="ui-code" data-mode="text"&gt;uint8_t ble_connector_t::on_ble_data_received(bt_nus_client *nus, const uint8_t *const ble_data, uint16_t len) {
    .....
    uint8_t conn_index = bt_conn_index(nus_client-&amp;gt;conn);
    k_sem_take(&amp;amp;ble_send_semaphore[conn_index], K_MSEC(5000));
    ....
}

void ble_connector_t::on_ble_data_sent(bt_nus_client *nus, uint8_t err, const uint8_t *const data, uint16_t len) {
    uint8_t conn_index = bt_conn_index(nus_client-&amp;gt;conn);
    k_sem_give(&amp;amp;ble_send_semaphore[conn_index]);
    ...
}&lt;/pre&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;Best Regards,&lt;/div&gt;
&lt;div&gt;Manuel&lt;/div&gt;
&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Multi-NUS: Sending several messages in a row</title><link>https://devzone.nordicsemi.com/thread/479398?ContentTypeID=1</link><pubDate>Thu, 18 Apr 2024 11:30:24 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6cc2454d-fd5e-478d-80bb-21cdef8bf747</guid><dc:creator>Jared</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;That is a very good point.&lt;/p&gt;
&lt;p&gt;What if you make two arrays, one containing the semaphores for each link and another containing the&amp;nbsp;bt_nus_client pointer for each link. You then make sure that they are indexed correspondingly, meaning semaphore[i] contains the semaphore for the same link as&amp;nbsp;&lt;span&gt;bt_nus_client[i] points to.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;bt_nus_client&amp;nbsp;is passed as parameter to the callback handler&amp;nbsp;on_ble_data_sent. You can then iterate through the&amp;nbsp;bt_nus_client&amp;nbsp;array and then find the correct index, and then free the semaphore corresponding to that index.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Something like this.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="text"&gt;K_SEM_DEFINE(ble_send_semaphore, 1, 1);

semaphore[num_nus_conns]
bt_nus_client [num_nus_conns]



void ble_connector_t::on_ble_data_sent(bt_nus_client *nus, uint8_t err, const uint8_t *const data, uint16_t len) {

	for (size_t i = 0; i&amp;lt; num_nus_conns; i++)
	{
		if(bt_nus_client[i] == nus)
		{
			k_sem_give(&amp;amp;semaphore[i]);
		}
	}

     
}

int ble_connector_t::send_broadcast(uint8_t *data, uint16_t len) {
    int err = 0;
	LOG_INF(&amp;quot;BLE broadcast: Length:%d&amp;quot;, len);

	const size_t num_nus_conns = bt_conn_ctx_count(&amp;amp;conns_ctx_lib);
	for (size_t i = 0; i &amp;lt; num_nus_conns; i++) {
		const struct bt_conn_ctx *ctx = bt_conn_ctx_get_by_id(&amp;amp;conns_ctx_lib, i);
		if (ctx) {
			struct bt_nus_client[i] = (struct bt_nus_client*) ctx-&amp;gt;data;
			if (nus_client[i]) {
				err = k_sem_take(&amp;amp;semaphore[i]);, K_MSEC(5000));
				if(err) {
					LOG_WRN(&amp;quot;BLE semaphore timeout, dropping packet (err %d)&amp;quot;, err);
				} else {
					err = bt_nus_client_send(nus_client[i], data, len);
					if (err) {
						LOG_WRN(&amp;quot;Failed to send data over BLE connection (err %d)&amp;quot;, err);
						k_sem_give(&amp;amp;semaphore[i]);
					}
				}
			}
			bt_conn_ctx_release(&amp;amp;conns_ctx_lib, (void *)ctx-&amp;gt;data);
		}
	}
	return err;
}&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Note, this is just psudo code, it&amp;#39;s not supposed to run, but I think it gives an overview of my suggestion.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;regards&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Jared&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Multi-NUS: Sending several messages in a row</title><link>https://devzone.nordicsemi.com/thread/479017?ContentTypeID=1</link><pubDate>Tue, 16 Apr 2024 13:51:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f90e4d44-08b1-4fbb-baa5-4e43f4ec1fa9</guid><dc:creator>Phobios</dc:creator><description>&lt;p&gt;Hi Jared,&lt;/p&gt;
&lt;p&gt;yes I&amp;#39;m basing on the blogpost.&lt;/p&gt;
&lt;p&gt;The error is -120 (-EALREADY) cause the bit&amp;nbsp;NUS_C_RX_WRITE_PENDING is already set.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;int bt_nus_client_send(struct bt_nus_client *nus_c, const uint8_t *data, uint16_t len) {
	int err;

	if (!nus_c-&amp;gt;conn) {
		return -ENOTCONN;
	}

	if (atomic_test_and_set_bit(&amp;amp;nus_c-&amp;gt;state, NUS_C_RX_WRITE_PENDING)) {
		return -EALREADY;
	}

	nus_c-&amp;gt;rx_write_params.func = on_sent;
	nus_c-&amp;gt;rx_write_params.handle = nus_c-&amp;gt;handles.rx;
	nus_c-&amp;gt;rx_write_params.offset = 0;
	nus_c-&amp;gt;rx_write_params.data = data;
	nus_c-&amp;gt;rx_write_params.length = len;

	err = bt_gatt_write(nus_c-&amp;gt;conn, &amp;amp;nus_c-&amp;gt;rx_write_params);
	if (err) {
		atomic_clear_bit(&amp;amp;nus_c-&amp;gt;state, NUS_C_RX_WRITE_PENDING);
	}

	return err;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I&amp;#39;ll try that with the semaphore array but how should I assign the semaphore index to the connections? On taking the semaphore I could use the index which I&amp;#39;m using with&amp;nbsp;bt_conn_ctx_get_by_id(...) in the send_broadcast(...) function. But in the on_ble_data_sent function I do not have the id. So how should I know which semaphore index I should give when on_ble_data_sent is called?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Multi-NUS: Sending several messages in a row</title><link>https://devzone.nordicsemi.com/thread/478988?ContentTypeID=1</link><pubDate>Tue, 16 Apr 2024 12:39:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:05e533c9-d9ef-4aa1-a2fe-33e7614f4528</guid><dc:creator>Jared</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Are you basing it on &lt;a href="https://devzone.nordicsemi.com/guides/nrf-connect-sdk-guides/b/software/posts/enter-the-multi-nus-a-simple-wireless-uart-network"&gt;this &lt;/a&gt;blogpost?&lt;/p&gt;
&lt;p&gt;I think the best would be to have one semaphore per link, that would you could send the messages in parallel but at the same time avoiding writing to more than one link at once,&lt;/p&gt;
&lt;p&gt;When you try write to one device more than once, what error code does it fail with?&lt;/p&gt;
&lt;p&gt;regards&lt;/p&gt;
&lt;p&gt;Jared&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>