aboutsummaryrefslogtreecommitdiff
path: root/extmod/modbluetooth.c
AgeCommit message (Collapse)Author
2022-06-02all: Remove third argument to MP_REGISTER_MODULE.Damien George
It's no longer needed because this macro is now processed after preprocessing the source code via cpp (in the qstr extraction stage), which means unused MP_REGISTER_MODULE's are filtered out by the preprocessor. Signed-off-by: Damien George <damien@micropython.org>
2022-05-18extmod: Make extmod modules use MP_REGISTER_MODULE.Jim Mussared
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-05-03all: Use mp_obj_malloc everywhere it's applicable.Jim Mussared
This replaces occurences of foo_t *foo = m_new_obj(foo_t); foo->base.type = &foo_type; with foo_t *foo = mp_obj_malloc(foo_t, &foo_type); Excludes any places where base is a sub-field or when new0/memset is used. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-01-13extmod/modbluetooth: Fix conditional compilation of ringbuf_put_uuid.Damien George
This fixes a bug introduced in a76604afba109d990e466cdcd5a69a82077a7f56 Signed-off-by: Damien George <damien@micropython.org>
2021-12-01extmod/modbluetooth: Support gap_connect(None) to cancel a connection.Jonathan Hogg
Allow cancellation of in-progress peripheral connections.
2021-10-26extmod/modbluetooth: Add connection interval to gap_connect.Jim Mussared
This forwards through directly to the NimBLE and BTStack connect functions. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-08-14extmod/modbluetooth: Add send_update arg to gatts_write.Jim Mussared
This allows the write to trigger a notification or indication, but only to subscribed clients. This is different to gatts_notify/gatts_indicate, which will unconditionally notify/indicate. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-07-23extmod/modbluetooth: Clamp MTU values to 32->UINT16_MAX.Jim Mussared
2021-03-16extmod/modbluetooth: Free temp arrays in gatts register services.Damien George
This helps to reduce memory fragmentation, by freeing the heap data as soon as it is not needed. It also helps the compiler keeps a reference to the beginning of both arrays, which need to be traceable by the GC (otherwise some compilers may optimise this reference to something else). Signed-off-by: Damien George <damien@micropython.org>
2021-02-19extmod/modbluetooth: Separate enabling of "client" from "central".Jim Mussared
Previously, the MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE macro controlled enabling both the central mode and the GATT client functionality (because usually the two go together). This commits adds a new MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT macro that separately enables the GATT client functionality. This defaults to MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE. This also fixes a bug in the NimBLE bindings where a notification or indication would not be received by a peripheral (acting as client) as gap_event_cb wasn't handling it. Now both central_gap_event_cb and peripheral_gap_event_cb share the same common handler for these events. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-01-22extmod/modbluetooth: Add ble.hci_cmd(ogf, ocf, req, resp) function.Jim Mussared
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>
2020-12-02extmod/modbluetooth: Add support for passkey authentication.Jim Mussared
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2020-12-02extmod/modbluetooth: Simplify synchronous invoke_irq_handler signature.Jim Mussared
Rather than dealing with the different int types, just pass them all as a single array of mp_int_t with n_unsigned (before addr) and n_signed (after addr). Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2020-12-02extmod/modbluetooth: Add support for bonding (key persistence).Jim Mussared
This adds `_IRQ_GET_SECRET` and `_IRQ_SET_SECRET` events to allow the BT stack to request the Python code retrive/store/delete secret key data. The actual keys and values are opaque to Python and stack-specific. Only NimBLE is implemented (pending moving btstack to sync events). The secret store is designed to be compatible with BlueKitchen's TLV store API. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2020-12-02extmod/modbluetooth: Add gap_pair(conn_handle) func to intiate pairing.Jim Mussared
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2020-12-02extmod/modbluetooth: Allow configuration of pairing/bonding parameters.Jim Mussared
This allows setting the security and MITM-protection requirements. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2020-12-02extmod/modbluetooth: Add _IRQ_ENCRYPTION_UPDATE event.Andrew Leech
This allows the application to be notified if any of encrypted, authenticated and bonded state change, as well as the encryption key size. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2020-12-02extmod/modbluetooth: Add compile-config flag to enable pairing/bonding.Jim Mussared
Enable it on STM32/Unix NimBLE only (pairing/bonding requires synchronous events and full bindings). Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2020-12-02extmod/modbluetooth: Allow user-specified reason in read request IRQ.Jim Mussared
Instead of returning None/bool from the IRQ, return None/int (where a zero value means success). This mirrors how the L2CAP_ACCEPT return value works. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2020-12-02extmod/modbluetooth: Allow setting char/desc enc/auth options.Andrew Leech
This widens the characteristic/descriptor flags to 16-bit, to allow setting encryption/authentication requirements. Sets the required flags for NimBLE and btstack implementations. The BLE.FLAG_* constants will eventually be deprecated in favour of copy and paste Python constants (like the IRQs). Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2020-12-02extmod/modbluetooth: Add _IRQ_CONNECTION_UPDATE event.Andrew Leech
This allows the application to be notified of changes to the connection interval, connection latency and supervision timeout. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2020-11-24extmod/modbluetooth: Add API for L2CAP channels.Jim Mussared
Also known as L2CAP "connection oriented channels". This provides a socket-like data transfer mechanism for BLE. Currently only implemented for NimBLE on STM32 / Unix. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2020-11-13extmod/modbluetooth: Re-instate optional no-ringbuf modbluetooth.Jim Mussared
This requires that the event handlers are called from non-interrupt context (i.e. the MicroPython scheduler). This will allow the BLE stack (e.g. NimBLE) to run from the scheduler rather than an IRQ like PENDSV, and therefore be able to invoke Python callbacks directly/synchronously. This allows writing Python BLE handlers for events that require immediate response such as _IRQ_READ_REQUEST (which was previous a hard IRQ) and future events relating to pairing/bonding. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2020-11-13extmod/modbluetooth: Combine gattc-data-available callbacks into one.Jim Mussared
Instead of having the stack indicate a "start", "data"..., "end", pass through the data in one callback as an array of chunks of data. This is because the upcoming non-ringbuffer modbluetooth implementation cannot buffer the data in the ringbuffer and requires instead a single callback with all the data, to pass to the Python callback. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2020-11-13extmod/modbluetooth: Make UUID type accessible outside modbluetooth.c.Jim Mussared
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2020-09-25extmod/modbluetooth: Change module-owned bytes objects to memoryview.Damien George
A read-only memoryview object is a better representation of the data, which is owned by the ubluetooth module and may change between calls to the user's irq callback function. Signed-off-by: Damien George <damien@micropython.org>
2020-09-18extmod/modbluetooth: Implement MTU.Jim Mussared
2020-09-15extmod/modbluetooth: Print UUIDs correctly.Jim Mussared
In particular, the printed string can now be re-evaluated to construct the same UUID object.
2020-09-15extmod/modbluetooth: Make BLE.irq() method positional only.Jim Mussared
Simplifcation now that the trigger arg has been removed.
2020-09-15extmod/modbluetooth: Fix handling of optional data/uuid args.Jim Mussared
For the following 3 functions, previously the code relied on whether the arg was passed at all, to make it optional. Now it allows them to be explicitly `None` to indicate they are not used: - gatts_notify(..., [data]) - gattc_discover_services(..., [uuid]) - gattc_discover_characteristics(..., [uuid]) Also ensure that the uuid arguments are actually instances of the uuid type, and fix handling of the 5th arg in gattc_discover_characteristics().
2020-09-08extmod/modbluetooth: Implement configuration of address modes.Jim Mussared
Changes `BLE.config('mac')` to return a tuple (addr_mode, addr). Adds `BLE.config(addr_mode=...)` to set the addressing mode.
2020-08-26extmod/bluetooth: Support active scanning in BLE.gap_scan().Andrew Leech
This adds an additional optional parameter to gap_scan() to select active scanning, where scan responses are returned as well as normal scan results. This parameter is False by default which retains the existing behaviour.
2020-08-26extmod/modbluetooth: Fix race between READ_REQUEST and other IRQs.Jim Mussared
The READ_REQUEST callback is handled as a hard interrupt (because the BLE stack needs an immediate response from it so it can continue) and so calls to Python require extra protection: - the caller-owned tuple passed into the callback must be separate from the tuple used by other callback events (which are soft interrupts); - the GC and scheduler must be locked during callback execution.
2020-07-20extmod/modbluetooth: Add event for "indicate acknowledgement".Jim Mussared
This commit adds the IRQ_GATTS_INDICATE_DONE BLE event which will be raised with the status of gatts_indicate (unlike notify, indications require acknowledgement). An example of its use is added to ble_temperature.py, and to the multitests in ble_characteristic.py. Implemented for btstack and nimble bindings, tested in both directions between unix/btstack and pybd/nimble.
2020-07-20extmod/modbluetooth: Fix so it builds in peripheral-only mode.Jim Mussared
2020-07-20extmod/modbluetooth: Ignore unused self_in in ble_gatts_indicate.Jim Mussared
2020-07-18extmod/btstack: Schedule notify/indicate/write ops for bg completion.Jim Mussared
The goal of this commit is to allow using ble.gatts_notify() at any time, even if the stack is not ready to send the notification right now. It also addresses the same issue for ble.gatts_indicate() and ble.gattc_write() (without response). In addition this commit fixes the case where the buffer passed to write-with-response wasn't copied, meaning it could be modified by the caller, affecting the in-progress write. The changes are: - gatts_notify/indicate will now run in the background if the ACL buffer is currently full, meaning that notify/indicate can be called at any time. - gattc_write(mode=0) (no response) will now allow for one outstanding write. - gattc_write(mode=1) (with response) will now copy the buffer so that it can't be modified by the caller while the write is in progress. All four paths also now track the buffer while the operation is in progress, which prevents the GC free'ing the buffer while it's still needed.
2020-06-05extmod/modbluetooth: Support bigger characteristic values.Jim Mussared
The ring buffer previously used a single unsigned byte field to save the length, meaning that it would overflow for large characteristic value responses. With this commit it now use a 16-bit length instead and has code to explicitly truncate at UINT16_MAX (although this should be impossible to achieve in practice).
2020-06-05extmod/modbluetooth: Implement read done event.Jim Mussared
On btstack there's no status associated with the read result, it comes through as a separate event. This allows you to detect read failures or timeouts.
2020-06-05extmod/modbluetooth: Allow discovery of svc/char by uuid.Jim Mussared
In most situations this is a more efficient way of going straight to the service and characteristic you need.
2020-06-05extmod/modbluetooth: Add discover complete events for svc/char/desc.Jim Mussared
Without this it's difficult to implement a state machine correctly if the desired services are not found.
2020-06-05extmod/modbluetooth: Make modbluetooth event not a bitfield.Jim Mussared
There doesn't appear to be any use for only triggering on specific events, so it's just easier to number them sequentially. This makes them smaller values so they take up only 1 byte in the ringbuf, only 1 byte for the opcode in the bytecode, and makes room for more events. Also add a couple of new event types that need to be implemented (to avoid re-numbering later). And rename _COMPLETE and _STATUS to _DONE for consistency. In the future the "trigger" keyword argument can be reinstated by requiring the user to compute the bitmask, eg: ble.irq(handler, 1 << _IRQ_SCAN_RESULT | 1 << _IRQ_SCAN_DONE)
2020-05-11extmod/modbluetooth: Add support for changing the GAP device name.Damien George
This commit allows the user to set/get the GAP device name used by service 0x1800, characteristic 0x2a00. The usage is: BLE.config(gap_name="myname") print(BLE.config("gap_name")) As part of this change the compile-time setting MICROPY_PY_BLUETOOTH_DEFAULT_NAME is renamed to MICROPY_PY_BLUETOOTH_DEFAULT_GAP_NAME to emphasise its link to GAP and this new "gap_name" config value. And the default value of this for the NimBLE bindings is changed from "PYBD" to "MPY NIMBLE" to be more generic.
2020-04-29extmod/modbluetooth: Fix sign compare and unused variable warnings.Jim Mussared
2020-04-29extmod/modbluetooth: Don't hold atomic section during mp_sched_schedule.Jim Mussared
Because, for example, on unix the atomic section isn't re-entrant, and mp_sched_schedule() will try to re-acquire the atomic section.
2020-04-07extmod/modbluetooth: Provide FLAG_WRITE_NO_RESPONSE for characteristics.Damien George
This flag is supported and needs to be set if characteristics are write- without-response.
2020-04-05all: Use MP_ERROR_TEXT for all error messages.Jim Mussared
2020-03-11extmod/modbluetooth: Change scan result's "connectable" to "adv_type".Damien George
This commit changes the BLE _IRQ_SCAN_RESULT data from: addr_type, addr, connectable, rssi, adv_data to: addr_type, addr, adv_type, rssi, adv_data This allows _IRQ_SCAN_RESULT to handle all scan result types (not just connectable and non-connectable passive scans), and to distinguish between them using adv_type which is an integer taking values 0x00-0x04 per the BT specification. This is a breaking change to the API, albeit a very minor one: the existing connectable value was a boolean and True now becomes 0x00, False becomes 0x02. Documentation is updated and a test added. Fixes #5738.
2020-03-11extmod/modbluetooth: Unify error handling in remaining places.Thomas Friebel
Most error handling is done via `bluetooth_handle_errno()` already. Replace the remaining few manual checks with calls to that function.
2020-03-11extmod/nimble: Clarify active state and check for active in all methods.Jim Mussared
This commit ensures that the BLE stack is active before allowing operations that may otherwise crash if it's not active. It also clarifies the state better (adding the "stopping" state) and renames mp_bluetooth_is_enabled to the more self-explanatory mp_bluetooth_is_active.