<?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>bt_conn_disconnect() - ASSERTION FAILED [err == 0] k_sem_take return err -11</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/112882/bt_conn_disconnect---assertion-failed-err-0-k_sem_take-return-err--11</link><description>Hi, 
 I just started developing a BLE Central device on a nRF52840 DK. After successfully connect my DevKit with a phone acting as a peripheral (using nRF Connect Mobile), The DK was able to subscribe to some services I configured on the phone and receive</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 29 Jul 2024 07:17:39 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/112882/bt_conn_disconnect---assertion-failed-err-0-k_sem_take-return-err--11" /><item><title>RE: bt_conn_disconnect() - ASSERTION FAILED [err == 0] k_sem_take return err -11</title><link>https://devzone.nordicsemi.com/thread/495936?ContentTypeID=1</link><pubDate>Mon, 29 Jul 2024 07:17:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:283f4912-68de-4513-b149-9a165230fa96</guid><dc:creator>Victor Groux</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I&amp;#39;m quite surprised but this &lt;code&gt;&amp;#39;\0&amp;#39;&lt;/code&gt; was indeed missing. Thank you very much.&lt;/p&gt;
&lt;p&gt;Concerning bt_conn_foreach, I think it did not work because it was executed in an interrupt. Using first the flag logic suggested by Hieu solved this problem. After using a flag, I changed the logic to use the workqueue and now it works fine.&lt;/p&gt;
&lt;p&gt;Again, thanks&lt;/p&gt;
&lt;p&gt;Victor&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: bt_conn_disconnect() - ASSERTION FAILED [err == 0] k_sem_take return err -11</title><link>https://devzone.nordicsemi.com/thread/495882?ContentTypeID=1</link><pubDate>Sat, 27 Jul 2024 17:48:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:58384e18-2488-4fe5-8ea3-a357d5cd8c8c</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;Victor, I think the reason you have the garbage after the device name is because&amp;nbsp;log_strdup does not make use of the struct bt_data-&amp;gt;datalen.&lt;/p&gt;
&lt;p&gt;Easiest would be to append a &amp;#39;\0&amp;#39; to data-&amp;gt;data[bt_data-&amp;gt;data_len] and then log_strdup might handle this better.&lt;/p&gt;
&lt;p&gt;I am also unsure why&amp;nbsp;bt_conn_foreach does not work for disconnecting for each connection. If you are pretty sure that you have more than one device connected at this point, then&amp;nbsp;bt_conn_foreach should have looped through them. I did a quick test at my end and it seems that it disconnects both devices that I was connected to.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: bt_conn_disconnect() - ASSERTION FAILED [err == 0] k_sem_take return err -11</title><link>https://devzone.nordicsemi.com/thread/495159?ContentTypeID=1</link><pubDate>Tue, 23 Jul 2024 08:51:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6a503ca6-0506-4a5d-8203-04e9a5f73f4a</guid><dc:creator>Victor Groux</dc:creator><description>&lt;p&gt;Hi Simon,&lt;/p&gt;
&lt;p&gt;If you mean to directly use the &lt;code&gt;data-&amp;gt;data&lt;/code&gt; to print with the&amp;nbsp;print format&amp;nbsp;&lt;code&gt;%s&lt;/code&gt;, I&amp;#39;ve already tried but it didn&amp;#39;t solve the output, even using &lt;code&gt;printk()&lt;/code&gt; instead of the logging API alone or with the config &lt;code&gt;CONFIG_LOG_PRINTK=y&lt;/code&gt; didn&amp;#39;t change anything at all.&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;br /&gt;Victor&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: bt_conn_disconnect() - ASSERTION FAILED [err == 0] k_sem_take return err -11</title><link>https://devzone.nordicsemi.com/thread/495141?ContentTypeID=1</link><pubDate>Tue, 23 Jul 2024 07:28:12 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9510c834-0f42-4ac3-98b5-6cf4ae900713</guid><dc:creator>Simonr</dc:creator><description>&lt;p&gt;Hi&lt;/p&gt;
&lt;p&gt;Hieu is out of office so I have taken over this case in his place&lt;/p&gt;
&lt;p&gt;Okay, it seems like the Device name fills the remaining possible bytes with gibberish for some reason here, but I&amp;#39;m not able to see why exactly. I&amp;#39;m not familiar with the log_strdup wrapper, but could you maybe use just a string instead of the wrapper, as I&amp;#39;m suspecting the wrapper to be the reason for this.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Simon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: bt_conn_disconnect() - ASSERTION FAILED [err == 0] k_sem_take return err -11</title><link>https://devzone.nordicsemi.com/thread/494278?ContentTypeID=1</link><pubDate>Wed, 17 Jul 2024 07:16:37 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:70d4ab9d-2d97-4dcd-847c-c35eb30b6910</guid><dc:creator>Victor Groux</dc:creator><description>&lt;p&gt;Update:&lt;/p&gt;
&lt;p&gt;I just&amp;nbsp;remove the while loop in my main function and use the system workqueue to call my disconnect function. It works exactly as I want and I don&amp;#39;t have&amp;nbsp;pending print anymore.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
[quote userid="134549" url="~/f/nordic-q-a/112882/bt_conn_disconnect---assertion-failed-err-0-k_sem_take-return-err--11/494113"][00:00:00.462,860] &amp;lt;inf&amp;gt; main: Device name: Zenfone 9Ϋ��(�&amp;lt;�&lt;br /&gt;...&lt;br /&gt;[00:00:02.539,001] &amp;lt;inf&amp;gt; main: Device name: Zenfone 9��89����[/quote]
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;However, I still have this artifacts. The only peripheral&amp;#39;s device name that is print normally is a Sony WH-1000XM4. Around me, there are some Jabra headsets, a nRF52805, a WH-1000XM3 and some connected watches too that&amp;nbsp;don&amp;#39;t print well&amp;nbsp;either.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: bt_conn_disconnect() - ASSERTION FAILED [err == 0] k_sem_take return err -11</title><link>https://devzone.nordicsemi.com/thread/494113?ContentTypeID=1</link><pubDate>Tue, 16 Jul 2024 11:30:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a98fce1b-c7f2-4ca0-8cd9-46265869eb03</guid><dc:creator>Victor Groux</dc:creator><description>&lt;p&gt;Hi Hieu,&lt;/p&gt;
&lt;p&gt;Thanks for the answer, I couldn&amp;#39;t respond earlier due to vacation.&lt;/p&gt;
&lt;p&gt;As you advised, I replace every &lt;code&gt;printk()&lt;/code&gt; call by the logging API (&lt;code&gt;LOG_INF()&lt;/code&gt;, sometimes&amp;nbsp;&lt;code&gt;LOG_WRN()&lt;/code&gt;, etc...).&lt;/p&gt;
&lt;p&gt;In the button&amp;#39;s callback function, I replace the &lt;code&gt;ble_disconnect_all()&lt;/code&gt; call to set a flag as true and at the end of my &lt;code&gt;main()&lt;/code&gt; function, I put a&amp;nbsp;infinite loop that check the value of that flag and if it&amp;#39;s true, it will call &lt;code&gt;ble_disconnect_all()&lt;/code&gt; and then set the flag to false.&lt;/p&gt;
&lt;p&gt;It works great but now, the logging API&amp;#39;s calls doesn&amp;#39;t print anything.&amp;nbsp;It seems&amp;nbsp;that the &lt;code&gt;while(1)&lt;/code&gt; block the logging API but I don&amp;#39;t really understand why.&lt;br /&gt;When I put a break in my if statement inside the loop, every &lt;code&gt;LOG_XXX()&lt;/code&gt; print is suddenly flush to the serial terminal only when the break triggered. Using the blocking &lt;code&gt;printk()&lt;/code&gt; instead of the Logging API doesn&amp;#39;t made this problem appears.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;int main(void)
{
  LOG_INIT();

  // Init Bluetooth subsystem
  int err = bt_enable(NULL);
  if (err)
  {
    LOG_ERR(&amp;quot;Bluetooth init failed (err %d)&amp;quot;, err);
    return  1;
  }
  LOG_INF(&amp;quot;Bluetooth initialized&amp;quot;);
  flag_ble_disconnect = false;

  // Init Button
  err = button_init();
  if (err)
  {
    LOG_ERR(&amp;quot;Button init failed (err %d)&amp;quot;, err);
    return err;
  }
  
  start_scan();

  while (1)
  {
    if (flag_ble_disconnect)
    {
      ble_disconnect_all();
      flag_ble_disconnect = false;
      
      // Without this break, every LOG_XXX() seems to be ignored.
      // With this break, the LOG_XXX() prints ONLY after the break&amp;#39;s trigger...
      break;
      // It is a problem because if this break is here, the disconnect function can&amp;#39;t be called multiple time.
    }
  }
  return 0;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Can the workqueue resolve this problem ? If yes, why ?&lt;/p&gt;
&lt;p&gt;Isn&amp;#39;t this flag logic come with some concurrence problems like data races ?&lt;/p&gt;
&lt;p&gt;And I have one more question. In the bt_parse_data callback, I check if the data type is BT_DATA_NAME_COMPLETE and if it&amp;#39;s the case, I&amp;#39;m logging it. However the name that is printed contains some artifacts that aren&amp;#39;t the same every time it is printed. Why is it printed like that and how can I resolve this print ?&lt;pre class="ui-code" data-mode="c_cpp"&gt;static bool parse_data_cb(struct bt_data *data, void *user_data)
{
  bt_addr_le_t *addr = user_data;

  switch (data-&amp;gt;type)
  {
    case BT_DATA_NAME_SHORTENED:
    case BT_DATA_NAME_COMPLETE:
      LOG_INF(&amp;quot;Device name: %s&amp;quot;, log_strdup(data-&amp;gt;data));
      
      // Connecting to my phone. This condition will change in the future
      if (strncmp(data-&amp;gt;data, &amp;quot;Zenfone 9&amp;quot;, data-&amp;gt;data_len) == 0)
      {
        int err = bt_le_scan_stop();
        if (err)
        {
          LOG_ERR(&amp;quot;Stop LE scan failed (err %d)&amp;quot;, err);
          return false;
        }

        struct bt_conn_le_create_param *create_param = BT_CONN_LE_CREATE_CONN;
        struct bt_le_conn_param *param = BT_LE_CONN_PARAM_DEFAULT;
        
        err = bt_conn_le_create(addr, create_param, param, &amp;amp;default_conn);
        if (err)
        {
          LOG_WRN(&amp;quot;Connection failed (err %d)&amp;quot;, err);
          start_scan();
        }
      }
      break;
    default:
      break;
  }

  return true;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;The print:&lt;/p&gt;
&lt;pre&gt;[00:00:00.462,860] &amp;lt;inf&amp;gt; main: Device name: Zenfone 9Ϋ��(�&amp;lt;�&lt;br /&gt;...&lt;br /&gt;[00:00:02.539,001] &amp;lt;inf&amp;gt; main: Device name: Zenfone 9��89����&lt;/pre&gt;
&lt;p&gt;Anyway, it&amp;#39;s just a visual problem in the terminal, the connection works perfectly. This issue appears with all&amp;nbsp;devices that are advertising&amp;nbsp;their&amp;nbsp;name (there are about 10 different peripherals around me that are advertising their name).&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;It is my first embedded application so I&amp;nbsp;don&amp;#39;t really have a good understanding of what is going under the hood and how&amp;nbsp;it&amp;#39;s working.&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;br /&gt;Victor&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: bt_conn_disconnect() - ASSERTION FAILED [err == 0] k_sem_take return err -11</title><link>https://devzone.nordicsemi.com/thread/493253?ContentTypeID=1</link><pubDate>Wed, 10 Jul 2024 23:14:23 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c38803d3-c0ff-498b-8474-efe7869580a0</guid><dc:creator>Hieu</dc:creator><description>&lt;p&gt;Hi Victor,&lt;/p&gt;
&lt;p&gt;In your code, &lt;span&gt;last_press_time&lt;/span&gt; could be &lt;span&gt;volatile&lt;/span&gt;, which helps ensuring that the value is up to date. It could also be local to the callback, but that should not affect performance/behavior.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://docs.nordicsemi.com/bundle/ncs-2.6.1/page/kconfig/index.html#CONFIG_PRINTK"&gt;printk() by default is in-place and blocking&lt;/a&gt;, so the printk() calls will&amp;nbsp;prolong the execution of the callback. It might be a good idea to replace them with &lt;a href="https://docs.nordicsemi.com/bundle/ncs-2.6.1/page/zephyr/services/logging/index.html"&gt;Logging APIs&lt;/a&gt;&amp;nbsp;or &lt;a href="https://docs.nordicsemi.com/bundle/ncs-2.6.1/page/kconfig/index.html#CONFIG_LOG_PRINTK"&gt;CONFIG_LOG_PRINTK&lt;/a&gt;, both together with&amp;nbsp;&lt;a href="https://docs.nordicsemi.com/bundle/ncs-2.6.1/page/kconfig/index.html#CONFIG_LOG_MODE_DEFERRED"&gt;CONFIG_LOG_MODE_DEFERRED&lt;/a&gt;,&amp;nbsp;to make printk() non-blocking.&lt;/p&gt;
&lt;p&gt;Otherwise, I think the logic is&amp;nbsp;reasonable.&amp;nbsp;I also discuss a bit further on debouncing here:&amp;nbsp;&amp;nbsp;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/112194/zephyr-debounce-support"&gt;Zephyr debounce support&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Please let me know if the flag handling&amp;nbsp;improves the situation. If that works, you might also want to consider upgrading it&amp;nbsp;to using &lt;a href="https://academy.nordicsemi.com/courses/nrf-connect-sdk-fundamentals/lessons/lesson-7-multithreaded-applications/"&gt;workqueue&lt;/a&gt;&amp;nbsp;or &lt;a href="https://docs.nordicsemi.com/bundle/ncs-2.6.1/page/zephyr/kernel/services/index.html"&gt;other Zephyr kernel services&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;Hieu&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: bt_conn_disconnect() - ASSERTION FAILED [err == 0] k_sem_take return err -11</title><link>https://devzone.nordicsemi.com/thread/493108?ContentTypeID=1</link><pubDate>Wed, 10 Jul 2024 09:49:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:480038e5-9d36-402e-88ed-88612ffe5b2d</guid><dc:creator>Victor Groux</dc:creator><description>&lt;p&gt;Hi Hieu,&lt;/p&gt;
&lt;p&gt;I thought about this problem when I wrote the code. I made a protection with a timer to be sure that the handler isn&amp;#39;t called multiple times. This is my callback and init functions concerning that button:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#define BUTTON_TIMEOUT_MS 250

static uint64_t last_press_time = 0;

static const struct gpio_dt_spec button = GPIO_DT_SPEC_GET(DT_NODELABEL(button0), gpios);
static struct gpio_callback button_cb_data;

static void button_disconnect_cb(const struct device *dev, struct gpio_callback *cb, uint32_t pins)
{
  uint64_t now = k_uptime_get();
  if ((now - last_press_time) &amp;gt; BUTTON_TIMEOUT_MS)
  {
    printk(&amp;quot;Disconnect every devices...\n&amp;quot;);
    ble_disconnect_all();
    last_press_time = now;
  }
  else
  {
    printk(&amp;quot;Pls wait a bit\n&amp;quot;);
  }
}

int button_init(void)
{
  if (!device_is_ready(button.port))
  {
    printk(&amp;quot;Error: button device %s not ready\n&amp;quot;, button.port-&amp;gt;name);
    return 1;
  }
  int ret = gpio_pin_configure_dt(&amp;amp;button, GPIO_INPUT);
  if (ret)
  {
    printk(&amp;quot;Error: %d: failed to configure %s pin %d\n&amp;quot;, ret, button.port-&amp;gt;name, button.pin);
    return 1;
  }
  ret = gpio_pin_interrupt_configure_dt(&amp;amp;button, GPIO_INT_EDGE_TO_ACTIVE);
  if (ret)
  {
    printk(&amp;quot;Error: %d: failed to configure interrupt on %s pin %d\n&amp;quot;, ret, button.port-&amp;gt;name, button.pin);
    return 1;
  }

  // Setup button callback
  gpio_init_callback(&amp;amp;button_cb_data, button_disconnect_cb, BIT(button.pin));
  gpio_add_callback(button.port, &amp;amp;button_cb_data);
  return 0;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Do you think that&amp;#39;s a good way to resolve button bounce ? Is it enough ?&lt;/p&gt;
&lt;p&gt;I&amp;#39;m going to try with the&amp;nbsp;flag logic.&lt;/p&gt;
&lt;p&gt;Thanks,&lt;br /&gt;Victor&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: bt_conn_disconnect() - ASSERTION FAILED [err == 0] k_sem_take return err -11</title><link>https://devzone.nordicsemi.com/thread/493097?ContentTypeID=1</link><pubDate>Wed, 10 Jul 2024 09:22:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5b902038-9588-4694-ba86-35c7642e3341</guid><dc:creator>Hieu</dc:creator><description>&lt;p&gt;Hi Victor,&lt;/p&gt;
&lt;p&gt;I suspect that the button was bouncing, causing the handler to be called multiple times.&lt;/p&gt;
&lt;p&gt;Could you please update the logic so that the button handler simply raises a flag, and the disconnection code happens elsewhere when that flag is raised?&lt;/p&gt;
&lt;p&gt;Hieu&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>