<?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>Zephyr Ring Buffer: why is no wrap-around happening?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/119313/zephyr-ring-buffer-why-is-no-wrap-around-happening</link><description>Hi, I have a BLE device in Observer role - it will constantly scan for Broadcasters broadcasting our unique UUID and put these scanned devices into a Ring Buffer. 
 The reason I thought of using a Ring Buffer is because newly scanned devices should replace</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 28 Feb 2025 08:16:31 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/119313/zephyr-ring-buffer-why-is-no-wrap-around-happening" /><item><title>RE: Zephyr Ring Buffer: why is no wrap-around happening?</title><link>https://devzone.nordicsemi.com/thread/525207?ContentTypeID=1</link><pubDate>Fri, 28 Feb 2025 08:16:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1838b5f4-289b-4aa4-b457-28744b3ed058</guid><dc:creator>nik2k</dc:creator><description>&lt;p&gt;Hi, this&amp;nbsp; seems like a way more comprehensive approach than the simple scan + dump routine that I had in mind.&amp;nbsp; I ditched the ring buffer idea and am using a k_fifo to send the devices from the ble-scan-callback to the main thread. I&amp;#39;ll try putting the table logic in main. Thanks a lot for taking the time to write such an informative response!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Zephyr Ring Buffer: why is no wrap-around happening?</title><link>https://devzone.nordicsemi.com/thread/525203?ContentTypeID=1</link><pubDate>Fri, 28 Feb 2025 08:09:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d5500409-2075-4d7e-8582-290f5a1e3188</guid><dc:creator>nik2k</dc:creator><description>&lt;p&gt;Hi, thanks for the clarification. I definitely misunderstood the intended use case of the ring buffer API. I studied the code you linked a little more and am now using a ringbuffer somewhere else in my code (for offloading UART data processing).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Zephyr Ring Buffer: why is no wrap-around happening?</title><link>https://devzone.nordicsemi.com/thread/524833?ContentTypeID=1</link><pubDate>Wed, 26 Feb 2025 14:07:21 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:95c09c85-ffda-4bb9-b26c-aaf640b3b761</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;I think you misunderstood, or the documentation is not clear enough.&lt;/p&gt;
&lt;p&gt;The ring buffer supports wraparound but only if the space is free when wrapping around.&lt;/p&gt;
&lt;p&gt;At this line in &lt;a href="https://github.com/zephyrproject-rtos/zephyr/blob/main/lib/utils/ring_buffer.c#L157"&gt;ring_buf_item_put&lt;/a&gt;, there is an explicit check to see if the new item is going to a free space or not. In other words, overwriting is not allowed.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Zephyr Ring Buffer: why is no wrap-around happening?</title><link>https://devzone.nordicsemi.com/thread/524770?ContentTypeID=1</link><pubDate>Wed, 26 Feb 2025 09:29:34 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3cfd2b3b-8781-4317-acae-1e2a0b730276</guid><dc:creator>Jakob Ruhe</dc:creator><description>&lt;p&gt;The function `&lt;span&gt;ring_buf_put()` returns the number of bytes written to the ringbuffer. In your case the ringbuffer gets full after the first 2 writes and therefore you will not be able to write anything more to it before you remove something from it.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;A&amp;nbsp;side note: You can build and run the above example on your machine instead by building for the `native_sim` board. It is very powerful.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Noe, I am not sure exactly what you want to accomplish but to me it sounds that a ringbuffer is not ideal for your use case.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;If you want to keep track of some devices, perhaps count number of packets from them or whatever, then just use a table. I shared a code snippet for that in this thread:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/118744/scan-does-not-shows-short-long-names-for-hr-peripheral-s"&gt;Scan does not shows short/long names for HR peripheral(s) - Nordic Q&amp;amp;A - Nordic DevZone - Nordic DevZone&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;The code snippet looks like this:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#define NAME_LEN 30

typedef struct {
  bool used;
  bt_addr_le_t address;
  char name[NAME_LEN];
  int64_t last_seen;
} device_t;

static device_t s_devices[50];

device_t *find_device(const bt_addr_le_t *address)
{
	for (ssize_t i = 0; i &amp;lt; ARRAY_SIZE(s_devices); i++) {
		if (s_devices[i].used &amp;amp;&amp;amp; bt_addr_le_eq(&amp;amp;s_devices[i].address, address)) {
			return &amp;amp;s_devices[i];
		}
	}
	return NULL;
}

device_t *add_device(const bt_addr_le_t *address)
{
	for (ssize_t i = 0; i &amp;lt; ARRAY_SIZE(s_devices); i++) {
		if (!s_devices[i].used) {
			s_devices[i].used = true;
			s_devices[i].address = *address;
			s_devices[i].last_seen = k_uptime_get();
			return &amp;amp;s_devices[i];
		}
	}
	return NULL;
}&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I leave as an exercise to you to write a function that goes through the table and sets `used` to `false` if a device has not been heard for a while.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Good luck!&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>