summaryrefslogtreecommitdiff
path: root/extmod/nimble/modbluetooth_nimble.c
diff options
context:
space:
mode:
authorJim Mussared <jim.mussared@gmail.com>2021-01-22 17:30:25 +1100
committerDamien George <damien@micropython.org>2021-01-22 18:15:12 +1100
commitaa136b4d78c7f81e63bde20ae17ab69bbbf456db (patch)
treea58ffd937bebd1d830db637152ca9e72f9c1c7fd /extmod/nimble/modbluetooth_nimble.c
parent49dd9ba1a5e50c400a3cfd604dd884bf9fea628c (diff)
extmod/modbluetooth: Add ble.hci_cmd(ogf, ocf, req, resp) function.
This allows sending arbitrary HCI commands and getting the response. The return value of the function is the status of the command. This is intended for debugging and not to be a part of the public API, and must be enabled via mpconfigboard.h. It's currently only implemented for NimBLE bindings. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
Diffstat (limited to 'extmod/nimble/modbluetooth_nimble.c')
-rw-r--r--extmod/nimble/modbluetooth_nimble.c23
1 files changed, 22 insertions, 1 deletions
diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c
index 9ceeab7d7..8bead890d 100644
--- a/extmod/nimble/modbluetooth_nimble.c
+++ b/extmod/nimble/modbluetooth_nimble.c
@@ -81,9 +81,11 @@ STATIC int ble_hs_err_to_errno(int err) {
return 0;
}
if (err >= 0 && (unsigned)err < MP_ARRAY_SIZE(ble_hs_err_to_errno_table) && ble_hs_err_to_errno_table[err]) {
+ // Return an MP_Exxx error code.
return ble_hs_err_to_errno_table[err];
} else {
- return MP_EIO;
+ // Pass through the BLE error code.
+ return -err;
}
}
@@ -1660,6 +1662,25 @@ int mp_bluetooth_l2cap_recvinto(uint16_t conn_handle, uint16_t cid, uint8_t *buf
#endif // MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS
+#if MICROPY_PY_BLUETOOTH_ENABLE_HCI_CMD
+
+// For ble_hs_hci_cmd_tx
+#include "nimble/host/src/ble_hs_hci_priv.h"
+
+int mp_bluetooth_hci_cmd(uint16_t ogf, uint16_t ocf, const uint8_t *req, size_t req_len, uint8_t *resp, size_t resp_len, uint8_t *status) {
+ int rc = ble_hs_hci_cmd_tx(BLE_HCI_OP(ogf, ocf), req, req_len, resp, resp_len);
+ if (rc < BLE_HS_ERR_HCI_BASE || rc >= BLE_HS_ERR_HCI_BASE + 0x100) {
+ // The controller didn't handle the command (e.g. HCI timeout).
+ return ble_hs_err_to_errno(rc);
+ } else {
+ // The command executed, but had an error (i.e. invalid parameter).
+ *status = rc - BLE_HS_ERR_HCI_BASE;
+ return 0;
+ }
+}
+
+#endif // MICROPY_PY_BLUETOOTH_ENABLE_HCI_CMD
+
#if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
STATIC int ble_store_ram_read(int obj_type, const union ble_store_key *key, union ble_store_value *value) {