<?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>Nrf Connect SDK peripheral always reads RSSI as 127 upon a connection</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/85043/nrf-connect-sdk-peripheral-always-reads-rssi-as-127-upon-a-connection</link><description>Hello, I&amp;#39;m using an nRF5340 board with nRF Connect SDK as a BLE peripheral device, and I&amp;#39;m trying to get RSSI value when another device connects to it. 
 So I&amp;#39;m trying to read it as in the Zephyr hci_pwr_ctrl example. Specifically, I&amp;#39;m calling this function</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 24 Feb 2022 08:50:44 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/85043/nrf-connect-sdk-peripheral-always-reads-rssi-as-127-upon-a-connection" /><item><title>RE: Nrf Connect SDK peripheral always reads RSSI as 127 upon a connection</title><link>https://devzone.nordicsemi.com/thread/354735?ContentTypeID=1</link><pubDate>Thu, 24 Feb 2022 08:50:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:507ee5ec-6946-4418-8c6e-b470875c3453</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;I&amp;nbsp;see the same in nRF Connect SDK 1.6.1. There is no error indicating that it is not ready (the status still indicates 0). This is fixed in&amp;nbsp;1.7 and later, though. For 1.6 I suggest you simply discard 127 as that can never be a valid value.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Nrf Connect SDK peripheral always reads RSSI as 127 upon a connection</title><link>https://devzone.nordicsemi.com/thread/354685?ContentTypeID=1</link><pubDate>Wed, 23 Feb 2022 18:54:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:fd8c691c-6ca7-42ab-9891-ce416d45fd83</guid><dc:creator>CodeLoader</dc:creator><description>&lt;p&gt;So I was doing basically that, and what I&amp;#39;ve found out, is that right away after the connection the BLE controller seems to be not ready for the RSSI query, and it returns 127.&amp;nbsp;&lt;br /&gt;It always returns a normal negative value when I query RSSI after receiving a BLE message, but I need it to be resolved in the connection handler&lt;br /&gt;I created an infinite loop to run this RSSI-querying function in the connection handler until it gives a normal value, and it usually gets it from 10th-20th attempt. Although sometimes it&amp;#39;s at the first attempt.&lt;/p&gt;
&lt;p&gt;So I wonder, why there is no error status or any way to signal this not-readiness?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Nrf Connect SDK peripheral always reads RSSI as 127 upon a connection</title><link>https://devzone.nordicsemi.com/thread/354658?ContentTypeID=1</link><pubDate>Wed, 23 Feb 2022 15:37:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:fe180c81-47a4-4ce9-8eb7-807525358718</guid><dc:creator>Einar Thorsrud</dc:creator><description>[quote user="CodeLoader"]I&amp;#39;m not sure I understand you, how can I call&amp;nbsp;&lt;span&gt;bt_hci_rp_read_rssi as it is a struct not a function?&lt;/span&gt;[/quote]
&lt;p&gt;hah, good point. I must admit I was very quick when looking at this earlier&amp;nbsp;&lt;span class="emoticon" data-url="https://devzone.nordicsemi.com/cfs-file/__key/system/emoji/1f642.svg" title="Slight smile"&gt;&amp;#x1f642;&lt;/span&gt;&lt;/p&gt;
[quote user="CodeLoader"]Maybe in the latest Zephyr update it was changed? I&amp;#39;m using Zephyr 1.6.1, by the way.[/quote]
&lt;p&gt;I assume you are referring to nRF Connect SDK 1.6.1 (as Zephyr 1.6.1 is really old)? If so,&amp;nbsp;then the approach&amp;nbsp;demonstrated&amp;nbsp;in the&amp;nbsp;hci_pwr_ctrl should work, and I just&amp;nbsp;tested on a nRF5340 DK, where I get the RSSI reading without any issues.&lt;/p&gt;
[quote user="CodeLoader"]And what is&amp;nbsp;sdc_hci_cmd_sp_read_rssi_return_t? Can you please provide a code example for better understanding?[/quote]
&lt;p&gt;That is the type of the struct holding the rssi in the SoftDevice controller API. The only code example you should refer to is the&amp;nbsp;hci_pwr_ctrl which does this. If. you are using 1.6.1, then that would be this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static void read_conn_rssi(uint16_t handle, int8_t *rssi)
{
	struct net_buf *buf, *rsp = NULL;
	struct bt_hci_cp_read_rssi *cp;
	struct bt_hci_rp_read_rssi *rp;

	int err;

	buf = bt_hci_cmd_create(BT_HCI_OP_READ_RSSI, sizeof(*cp));
	if (!buf) {
		printk(&amp;quot;Unable to allocate command buffer\n&amp;quot;);
		return;
	}

	cp = net_buf_add(buf, sizeof(*cp));
	cp-&amp;gt;handle = sys_cpu_to_le16(handle);

	err = bt_hci_cmd_send_sync(BT_HCI_OP_READ_RSSI, buf, &amp;amp;rsp);
	if (err) {
		uint8_t reason = rsp ?
			((struct bt_hci_rp_read_rssi *)rsp-&amp;gt;data)-&amp;gt;status : 0;
		printk(&amp;quot;Read RSSI err: %d reason 0x%02x\n&amp;quot;, err, reason);
		return;
	}

	rp = (void *)rsp-&amp;gt;data;
	*rssi = rp-&amp;gt;rssi;

	net_buf_unref(rsp);
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I suggest you use that as reference, and simply copy-paste the handling from there, and verifies it still works in your app. After that you can always modify as needed.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Nrf Connect SDK peripheral always reads RSSI as 127 upon a connection</title><link>https://devzone.nordicsemi.com/thread/354636?ContentTypeID=1</link><pubDate>Wed, 23 Feb 2022 14:53:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9bcf61c6-6113-4bab-869d-945d9b3541bc</guid><dc:creator>CodeLoader</dc:creator><description>&lt;p&gt;To me it seems my attempt to read RSSI is the same as in the hci_pwr_ctrl example, the only difference is that casting from void pointers is needed in C++.&lt;br /&gt;As in the example it is like this:&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;err = bt_hci_cmd_send_sync(BT_HCI_OP_READ_RSSI, buf, &amp;amp;rsp);
if (err) {
	uint8_t reason = rsp ?
		((struct bt_hci_rp_read_rssi *)rsp-&amp;gt;data)-&amp;gt;status : 0;
	printk(&amp;quot;Read RSSI err: %d reason 0x%02x\n&amp;quot;, err, reason);
	return;
}

rp = (void *)rsp-&amp;gt;data;
*rssi = rp-&amp;gt;rssi;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I&amp;#39;m not sure I understand you, how can I call&amp;nbsp;&lt;span&gt;bt_hci_rp_read_rssi as it is a struct not a function? And I&amp;#39;m reading its fields after it&amp;#39;s returned in the response buffer. Maybe in the latest Zephyr update it was changed? I&amp;#39;m using Zephyr 1.6.1, by the way.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="text"&gt;struct bt_hci_rp_read_rssi {
	uint8_t  status;
	uint16_t handle;
	int8_t   rssi;
} __packed;&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;And what is&amp;nbsp;sdc_hci_cmd_sp_read_rssi_return_t? Can you please provide a code example for better understanding?&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Nrf Connect SDK peripheral always reads RSSI as 127 upon a connection</title><link>https://devzone.nordicsemi.com/thread/354612?ContentTypeID=1</link><pubDate>Wed, 23 Feb 2022 14:02:07 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:815191b9-4699-4337-a93f-a0502447b5e0</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Have you considered doing&amp;nbsp;as demonstrated by the hci_pwr_ctrl sample (see how&amp;nbsp;read_conn_rssi() is implemented in&amp;nbsp;zephyr/samples/bluetooth/hci_pwr_ctrl/src/main.c)?&lt;/p&gt;
&lt;p&gt;Ignoring the above and looking at what you have attempted to do, you configurations look good. However, while I must admit I am rusty in modern C++,&amp;nbsp; your way of obtaining the&amp;nbsp;RSSI before you print it looks odd to me. Have you tried simply calling&amp;nbsp;bt_hci_rp_read_rssi() the old fashioned&amp;nbsp;way, and the accessing rssi in the returned struct(sdc_hci_cmd_sp_read_rssi_return_t)?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Nrf Connect SDK peripheral always reads RSSI as 127 upon a connection</title><link>https://devzone.nordicsemi.com/thread/354445?ContentTypeID=1</link><pubDate>Tue, 22 Feb 2022 22:03:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:46aa7f56-9eba-4cf0-8178-ce8fde25268c</guid><dc:creator>CodeLoader</dc:creator><description>&lt;p&gt;Also, extended advertising features work and setting TX power in the same connection handler also works&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>