title: Best practice to manipulate hci0 with bluez and advertising
body: <p>Several questions about this wonderful duo <em>btmgmt/bluetoothctl</em> on a peripheral device with bluez 5.55.</p>
  <p>Is it normal that the return code from <em>btmgmt</em> is almost always 0 even if there is an error? I check the code and it seems to be the expected behaviour. But I don't understand why...</p>
  <pre><code># btmgmt bredr off
  Set BR/EDR for hci0 failed with status 0x0b (Rejected)
  # echo $?
  0
  </code></pre>
  <p>Indeed when I check the <em>btmgmt</em> code. Even if the status is not equal to 0. We use the done label which returns an <strong>EXIT_SUCCESS</strong> code... How can I check properly the result of my command?</p>
  <pre><code>static void setting_rsp(uint16_t op, uint16_t id, uint8_t status, uint16_t len,
                              const void *param)
  {
      const uint32_t *rp = param;
  
      if (status != 0) {
          error(&quot;%s for hci%u failed with status 0x%02x (%s)&quot;,
              mgmt_opstr(op), id, status, mgmt_errstr(status));
          goto done;
      }
  
      if (len &lt; sizeof(*rp)) {
          error(&quot;Too small %s response (%u bytes)&quot;,
                              mgmt_opstr(op), len);
          goto done;
      }
  
      print(&quot;hci%u %s complete, settings: %s&quot;, id, mgmt_opstr(op),
                          settings2str(get_le32(rp)));
  
  done:
      bt_shell_noninteractive_quit(EXIT_SUCCESS);
  }
  </code></pre>
  <p>I have a lot of issue with <em>btmgmt</em>... I have a lot of random failure result when I run the command. And the only way for me to check the result is to use btmgmt info | grep current | grep XXX to check the current settings on hci0. Is it better to use <em>bluetoothctl</em> instead? I can see also some code in mgmt-tester.c. Do I have to used this c api instead ?
  My current way to enable/disable bluetooth :</p>
  <pre><code>// Enable
  system (&quot;/usr/bin/btmgmt -i hci0 power off&quot;);
  system (&quot;/usr/bin/btmgmt -i hci0 le on&quot;); // BLE enable
  system (&quot;/usr/bin/btmgmt -i hci0 bredr on&quot;); // BTC enable
  system (&quot;/usr/bin/btmgmt -i hci0 connectable on&quot;);
  system (&quot;/usr/bin/btmgmt -i hci0 bondable on&quot;);
  system (&quot;/usr/bin/btmgmt -i hci0 discov on&quot;);
  system (&quot;/usr/bin/btmgmt -i hci0 name XXXXX&quot;);
  system (&quot;/usr/bin/btmgmt -i hci0 advertising on&quot;);
  system (&quot;/usr/bin/btmgmt -i hci0 power on&quot;);
  
  // Disable
  system (&quot;/usr/bin/btmgmt -i hci0 discov off&quot;);
  system (&quot;/usr/bin/btmgmt -i hci0 advertising off&quot;);
  system (&quot;/usr/bin/btmgmt -i hci0 connectable off&quot;);
  system (&quot;/usr/bin/btmgmt -i hci0 power off&quot;);
  </code></pre>
  <p>I try to implement the same with <em>bluetoothctl</em>. Problem is that I cannot find a way to enable <strong>bonding</strong> with <em>bluetoothctl</em>. No way also to enable the duo mode le/btc with bluetoothctl.</p>
  <p>My last point concerns advertising. I would like to send a custom advertising data in <strong>manufacter data</strong> field. I try something like this on my peripheral device:</p>
  <pre><code># bluetoothctl
  Agent registered
  [bluetooth]# menu advertise
  
  Use &quot;help&quot; for a list of available commands in a menu.
  Use &quot;menu &lt;submenu&gt;&quot; if you want to enter any submenu.
  Use &quot;back&quot; if you want to return to menu main.
  [bluetooth]# manufacturer 0xffff 0x12 0x34 0x56 0x78
  [bluetooth]# name coucou
  [bluetooth]# back
  
  [CHG] Controller AC:64:CF:ZZ:YY:XX SupportedInstances: 0x04 (4)
  [CHG] Controller AC:64:CF:ZZ:YY:XX ActiveInstances: 0x01 (1)
  Advertising object registered
  Manufacturer: 65535
    12 34 56 78                                      .4Vx            
  Tx Power: off
  LocalName: coucou
  Appearance: off
  Discoverable: on
  [bluetooth]# 
  </code></pre>
  <p>On another device, I sniff bluetooth packet with <em>btmon</em> or <em>bluetoothctl scan on</em> but I cannot see any <strong>manufacturer data</strong> match my data.</p>