2020-06-25 20:57:17 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2018 Dan Halbert for Adafruit Industries
|
|
|
|
* Copyright (c) 2018 Artur Pacholec
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "shared-bindings/_bleio/Connection.h"
|
|
|
|
|
2020-08-13 00:03:39 -04:00
|
|
|
#include "att.h"
|
|
|
|
|
2020-06-25 20:57:17 -04:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
Merge tag 'v1.17' into merge-1.17
F-strings, new machine.I2S class, ESP32-C3 support and LEGO_HUB_NO6 board
This release of MicroPython adds support for f-strings (PEP-498), with a
few limitations compared to normal Python. F-strings are essentially
syntactic sugar for "".format() and make formatting strings a lot more
convenient. Other improvements to the core runtime include pretty printing
OSError when it has two arguments (an errno code and a string), scheduling
of KeyboardInterrupt on the main thread, and support for a single argument
to the optimised form of StopIteration.
In the machine module a new I2S class has been added, with support for
esp32 and stm32 ports. This provides a consistent API for transmit and
receive of audio data in blocking, non-blocking and asyncio-based
operation. Also, the json module has support for the "separators" argument
in the dump and dumps functions, and framebuf now includes a way to blit
between frame buffers of different formats using a palette. A new,
portable machine.bitstream function is also added which can output a stream
of bits with configurable timing, and is used as the basis for driving
WS2812 LEDs in a common way across ports.
There has been some restructuring of the repository directory layout, with
all third-party code now in the lib/ directory. And a new top-level
directory shared/ has been added with first-party code that was previously
in lib/ moved there.
The docs have seen further improvement with enhancements and additions to
the rp2 parts, as well as a new quick reference for the zephyr port.
The terms master/slave have been replaced with controller/peripheral,
mainly relating to I2C and SPI usage. And u-module references have been
replaced with just the module name without the u-prefix to help clear up
the intended usage of modules in MicroPython.
For the esp8266 and esp32 ports, hidden networks are now included in WLAN
scan results. On the esp32 the RMT class is enhanced with idle_level and
write_pulses modes. There is initial support for ESP32-C3 chips with
GENERIC_C3 and GENERIC_C3_USB boards.
The javascript port has had its Makefile and garbage collector
implementation reworked so it compiles and runs with latest the Emscripten
using asyncify.
The mimxrt port sees the addition of hardware I2C and SPI support, as well
as some additional methods to the machine module. There is also support
for Hyperflash chips.
The nrf port now has full VFS storage support, enables source-line on
traceback, and has .mpy features consistent with other ports.
For the rp2 port there is now more configurability for boards, and more
boards added.
The stm32 port has a new LEGO_HUB_NO6 board definition with detailed
information how to get this LEGO Hub running stock MicroPython. There is
also now support to change the CPU frequency on STM32WB MCUs. And USBD_xxx
descriptor options have been renamed to MICROPY_HW_USB_xxx.
Thanks to everyone who contributed to this release: Amir Gonnen, Andrew
Scheller, Bryan Tong Minh, Chris Wilson, Damien George, Daniel Mizyrycki,
David Lechner, David P, Fernando, finefoot, Frank Pilhofer, Glenn Ruben
Bakke, iabdalkader, Jeff Epler, Jim Mussared, Jonathan Hogg, Josh Klar,
Josh Lloyd, Julia Hathaway, Krzysztof Adamski, Matúš Olekšák, Michael
Weiss, Michel Bouwmans, Mike Causer, Mike Teachman, Ned Konz, NitiKaur,
oclyke, Patrick Van Oosterwijck, Peter Hinch, Peter Züger, Philipp
Ebensberger, robert-hh, Roberto Colistete Jr, Sashkoiv, Seon Rozenblum,
Tobias Thyrrestrup, Tom McDermott, Will Sowerbutts, Yonatan Goldschmidt.
What follows is a detailed list of changes, generated from the git commit
history, and organised into sections.
Main components
===============
all:
- fix signed shifts and NULL access errors from -fsanitize=undefined
- update to point to files in new shared/ directory
py core:
- mpstate: make exceptions thread-local
- mpstate: schedule KeyboardInterrupt on main thread
- mperrno: add MP_ECANCELED error code
- makeqstrdefs.py: don't include .h files explicitly in preprocessing
- mark unused arguments from bytecode decoding macros
- objexcept: pretty print OSError also when it has 2 arguments
- makeversionhdr: add --tags arg to git describe
- vm: simplify handling of MP_OBJ_STOP_ITERATION in yield-from opcode
- objexcept: make mp_obj_exception_get_value support subclassed excs
- support single argument to optimised MP_OBJ_STOP_ITERATION
- introduce and use mp_raise_type_arg helper
- modsys: optimise sys.exit for code size by using exception helpers
- objexcept: make mp_obj_new_exception_arg1 inline
- obj: fix formatting of comment for mp_obj_is_integer
- emitnative: reuse need_reg_all func in need_stack_settled
- emitnative: ensure stack settling is safe mid-branch
- runtime: fix bool unary op for subclasses of native types
- builtinimport: fix condition for including do_execute_raw_code()
- mkrules: automatically build mpy-cross if it doesn't exist
- implement partial PEP-498 (f-string) support
- lexer: clear fstring_args vstr on lexer free
- mkrules.mk: do submodule sync in "make submodules"
extmod:
- btstack: add missing call to mp_bluetooth_hci_uart_deinit
- btstack: check that BLE is active before performing operations
- uasyncio: get addr and bind server socket before creating task
- axtls-include: add axtls_os_port.h to customise axTLS
- update for move of crypto-algorithms, re1.5, uzlib to lib
- moduselect: conditionally compile select()
- nimble: fix leak in l2cap_send if send-while-stalled
- btstack/btstack.mk: use -Wno-implicit-fallthrough, not =0
- utime: always invoke mp_hal_delay_ms when >= to 0ms
- modbluetooth: clamp MTU values to 32->UINT16_MAX
- nimble: allow modbluetooth binding to hook "sent HCI packet"
- nimble: add "memory stalling" mechanism for l2cap_send
- uasyncio: in open_connection use address info in socket creation
- modujson: add support for dump/dumps separators keyword-argument
- modlwip: fix close and clean up of UDP and raw sockets
- modbluetooth: add send_update arg to gatts_write
- add machine.bitstream
- modframebuf: enable blit between different formats via a palette
lib:
- tinyusb: update to version 0.10.1
- pico-sdk: update to version 1.2.0
- utils/stdout_helpers: make mp_hal_stdout_tx_strn_cooked efficient
- axtls: switch to repo at micropython/axtls
- axtls: update to latest axtls 2.1.5 wih additional commits
- re1.5: move re1.5 code from extmod to lib
- uzlib: move uzlib code from extmod to lib
- crypto-algorithms: move crypto-algorithms code from extmod to lib
- update README's based on contents of these dirs
drivers:
- neopixel: add common machine.bitstream-based neopixel module
- neopixel: optimize fill() for speed
- neopixel: reduce code size of driver
- cyw43: fix cyw43_deinit so it can be called many times in a row
- cyw43: make wifi join fail if interface is not active
mpy-cross:
- disable stack check when building with Emscripten
Support components
==================
docs:
- library: document new esp32.RMT features and fix wait_done
- library: warn that ustruct doesn't handle spaces in format strings
- esp8266/tutorial: change flash mode from dio to dout
- replace master/slave with controller/peripheral in I2C and SPI
- rp2: enhance quickref and change image to Pico pinout
- rp2: update general section to give a brief technical overview
- library/utime.rst: clarify behaviour and precision of sleep ms/us
- library/uasyncio.rst: document stream readexactly() method
- library/machine.I2S.rst: fix use of sd pin in examples
- zephyr: add quick reference for the Zephyr port
- library/zephyr: add libraries specific to the Zephyr port
- templates: add unix and zephyr quickref links to top-index
- rename ufoo.rst to foo.rst
- replace ufoo with foo in all docs
- library/index.rst: clarify module naming and purpose
- library/builtins.rst: add module title
- library/network.rst: simplify socket import
- add docs for machine.bitstream and neopixel module
- library: fix usage of :term: for frozen module reference
- esp8266: use monospace for software tools
- reference: mention that slicing a memoryview causes allocation
examples: no changes specific to this component/port
tests:
- extmod: make uasyncio_heaplock test more deterministic
- cpydiff/modules_struct_whitespace_in_format: run black
- extmod/ujson: add tests for dump/dumps separators argument
- run-multitests.py: add broadcast and wait facility
- multi_bluetooth/ble_subscribe.py: add test for subscription
- extmod/vfs_fat_finaliser.py: ensure alloc at never-used GC blocks
- basics: split f-string debug printing to separate file with .exp
- pybnative: make while.py test run on boards without pyb.delay
tools:
- autobuild: add scripts to build release firmware
- remove obsolete build-stm-latest.sh script
- ci.sh: run apt-get update in ci_powerpc_setup
- makemanifest.py: allow passing flags to mpy-tool.py
- autobuild: add mimxrt port to build scripts for nightly builds
- pyboard.py: add cmd-line option to make soft reset configurable
- mpremote: swap order of PID and VID in connect-list output
- ci.sh: build unix dev variant as part of macOS CI
- ci.sh: build GENERIC_C3 board as part of esp32 CI
- autobuild: use separate IDF version to build newer esp32 SoCs
- autobuild: add FeatherS2 and TinyS2 to esp32 auto builds
- mpremote: add seek whence for mounted files
- mpremote: raise OSError on unsupported RemoteFile.seek
- autobuild: add the MIMXRT1050_EVKB board to the daily builds
- ci.sh: add mpy-cross build to nrf port
- codeformat.py: include ports/nrf/modules/nrf in code formatting
- gen-cpydiff.py: don't rename foo to ufoo in diff output
- autobuild: add auto build for Silicognition wESP32
- mpremote: fix connect-list in case VID/PID are None
- mpremote: add "devs" shortcut for "connect list"
- mpremote: remove support for pyb.USB_VCP in/out specialisation
- autobuild: don't use "-B" for make, it's already a fresh build
- pyboard.py: move --no-exclusive/--soft-reset out of mutex group
- pyboard.py: make --no-follow use same variable as --follow
- pyboard.py: add --exclusive to match --no-exclusive
- pyboard.py: make --no-soft-reset consistent with other args
- uncrustify: force 1 newline at end of file
- mpremote: bump version to 0.0.6
CI:
- workflows: add workflow to build and test javascript port
- workflows: switch from Coveralls to Codecov
- workflows: switch from lcov to gcov
- workflows: add workflow to build and test unix dev variant
The ports
=========
all ports:
- use common mp_hal_stdout_tx_strn_cooked instead of custom one
- update for move of crypto-algorithms, uzlib to lib
- rename USBD_VID/PID config macros to MICROPY_HW_USB_VID/PID
bare-arm port: no changes specific to this component/port
cc3200 port: no changes specific to this component/port
esp8266 port:
- add __len__ to NeoPixel driver to support iterating
- Makefile: add more libm files to build
- include hidden networks in WLAN.scan results
- replace esp.neopixel with machine.bitstream
- remove dead code for end_ticks in machine_bitstream
esp32 port:
- boards/sdkconfig.base: disable MEMPROT_FEATURE to alloc from IRAM
- add __len__ to NeoPixel driver to support iterating
- main: allow MICROPY_DIR to be overridden
- esp32_rmt: fix RMT looping in newer IDF versions
- esp32_rmt: enhance RMT with idle_level and write_pulses modes
- add new machine.I2S class for I2S protocol support
- machine_spi: calculate actual attained baudrate
- machine_hw_spi: use a 2 item SPI queue for long transfers
- machine_dac: add MICROPY_PY_MACHINE_DAC option, enable by default
- machine_i2s: add MICROPY_PY_MACHINE_I2S option, enable by default
- fix use of mp_int_t, size_t and uintptr_t
- add initial support for ESP32C3 SoCs
- boards/GENERIC_C3: add generic C3-based board
- modmachine: release the GIL in machine.idle()
- mphalport: always yield at least once in delay_ms
- machine_uart: add flow kw-arg to enable hardware flow control
- boards: add Silicognition wESP32 board configuration
- mpconfigport.h: enable reverse and inplace special methods
- include hidden networks in WLAN.scan results
- makeimg.py: get bootloader and partition offset from sdkconfig
- enable MICROPY_PY_FSTRINGS by default
- machine_hw_spi: release GIL during transfers
- machine_pin: make check for non-output pins respect chip variant
- replace esp.neopixel with machine.bitstream
- remove dead code for end_ticks in machine_bitstream
- boards: add GENERIC_C3_USB board with USB serial/JTAG support
javascript port:
- rework Makefile and GC so it works with latest Emscripten
- Makefile: suppress compiler errors from array bounds
- Makefile: change variable to EXPORTED_RUNTIME_METHODS
mimxrt port:
- move calc_weekday helper function to timeutils
- machine_spi: add the SPI class to the machine module
- moduos: seed the PRNG on boot using the TRNG
- boards: set vfs partition start to 1 MBbyte
- main: skip running main.py if boot.py failed
- main: extend the information returned by help()
- mimxrt_flash: remove commented-out code
- modmachine: add a few minor methods to the machine module
- machine_led: use mp_raise_msg_varg helper
- machine_i2c: add hardware-based machine.I2C to machine module
- add support for Hyperflash chips
- boards: add support for the MIMXRT1050_EVKB board
- machine_pin: implement ioctl for Pin
minimal port:
- Makefile: add support for building with user C modules
nrf port:
- modules: replace master/slave with controller/peripheral in SPI
- boards/common.ld: calculate unused flash region
- modules/nrf: add new nrf module with flash block device
- drivers: add support for using flash block device with SoftDevice
- mpconfigport.h: expose nrf module when MICROPY_PY_NRF is set
- README: update README.md to reflect internal file systems
- mpconfigport.h: tune FAT FS configuration
- Makefile: add _fs_size linker script override from make
- modules/uos: allow a board to configure MICROPY_VFS_FAT/LFS1/LFS2
- mpconfigport.h: enable MICROPY_PY_IO_FILEIO when an FS is enabled
- qstrdefsport.h: add entries for in-built FS mount points
- main: add auto mount and auto format hook for internal flash FS
- boards: enable needed features for FAT/LFS1/LFS2
- facilitate use of freeze manifest
- boards: set FROZEN_MANIFEST blank when SD present on nrf51 targets
- modules/scripts: add file system formatting script
- Makefile: set default manifest file for all targets
- mphalport: add dummy function for mp_hal_time_ns()
- boards: enable MICROPY_VFS_LFS2 for all target boards
- modules/uos: add ilistdir to uos module
- modules/nrf: add function to enable/disable DCDC
- enable source line on tracebacks
- set .mpy features consistent with documentation and other ports
pic16bit port: no changes specific to this component/port
powerpc port: no changes specific to this component/port
qemu-arm port: no changes specific to this component/port
rp2 port:
- use 0=Monday datetime convention in RTC
- machine_rtc: in RTC.datetime, compute weekday automatically
- CMakeLists.txt: suppress compiler errors for pico-sdk and tinyusb
- tusb_config.h: set CFG_TUD_CDC_EP_BUFSIZE to 256
- machine_uart: add hardware flow control support
- machine_uart: allow overriding default machine UART pins
- machine_i2c: allow boards to configure I2C pins using new macros
- machine_spi: allow boards to configure SPI pins using new macros
- machine_uart: fix poll ioctl to also check hardware FIFO
- machine_uart: fix read when FIFO has chars but ringbuf doesn't
- tusb_port: allow boards to configure USB VID and PID
- boards/ADAFRUIT_FEATHER_RP2040: configure custom VID/PID
- boards/ADAFRUIT_FEATHER_RP2040: configure I2C/SPI default pins
- boards/SPARKFUN_PROMICRO: configure UART/I2C/SPI default pins
- boards/SPARKFUN_THINGPLUS: configure I2C/SPI default pins
- boards: add Adafruit ItsyBitsy RP2040
- boards: add Adafruit QT Py RP2040
- boards: add Pimoroni Pico LiPo 4MB
- boards: add Pimoroni Pico LiPo 16MB
- boards: add Pimoroni Tiny 2040
- CMakeLists.txt: allow a board's cmake to set the manifest path
- enable MICROPY_PY_FSTRINGS by default
- Makefile: add "submodules" target, to match other ports
- rp2_flash: disable IRQs while calling flash_erase/program
- CMakeLists.txt: add option to enable double tap reset to bootrom
- mpconfigport.h: allow boards to add root pointers
samd port:
- add support for building with user C modules
stm32 port:
- softtimer: add soft_timer_reinsert() helper function
- mpbthciport: change from systick to soft-timer for BT scheduling
- provide a custom BTstack runloop that integrates with soft timer
- usb: make irq's default trigger enable all events
- boardctrl: skip running main.py if boot.py had an error
- sdio: fix undefined reference to DMA stream on H7
- dma: add DMAMUX configuration for H7 to fix dma_nohal_init
- main: call mp_deinit() at end of main
- adc: allow using ADC12 and ADC3 for H7
- adc: define the ADC instance used for internal channels
- adc: simplify and generalise how pin_adcX table is defined
- add new machine.I2S class for I2S protocol support
- boards/NUCLEO_F446RE: fix I2C1 pin assignment to match datasheet
- replace master/slave with controller/peripheral in I2C and SPI
- systick: always POLL_HOOK when delaying for milliseconds
- sdram: make SDRAM test cache aware, and optional failure with msg
- boards/NUCLEO_F446RE: enable CAN bus support
- boards: add support for SparkFun STM32 MicroMod Processor board
- uart: fix LPUART1 baudrate set/get
- uart: support low baudrates on LPUART1
- boards/STM32F429DISC: set correct UART2 pins and add UART3/6
- boards/NUCLEO_F439ZI: add board definition for NUCLEO_F439ZI
- boards/LEGO_HUB_NO6: add board definition for LEGO_HUB_NO6
- Makefile: update to only pull in used Bluetooth library
- README.md: update supported MCUs, and submodule and mboot use
- usbd_desc: rename USBD_xxx descriptor opts to MICROPY_HW_USB_xxx
- usbd_cdc_interface: rename USBD_CDC_xx opts to MICROPY_HW_USB_xx
- powerctrl: support changing frequency on WB MCUs
- boards/NUCLEO_H743ZI2: add modified version of NUCLEO_H743ZI
- mbedtls: fix compile warning about uninitialized val
- enable MICROPY_PY_FSTRINGS by default
- add implementation of machine.bitstream
- Makefile: allow GIT_SUBMODULES and LIBS to be extended
- stm32_it: support TIM17 IRQs on WB MCUs
- disable computed goto on constrained boards
- storage: make extended-block-device more configurable
- boards/LEGO_HUB_NO6: change SPI flash storage to use hardware SPI
- boards/LEGO_HUB_NO6: skip first 1MiB of SPI flash for storage
- boards/LEGO_HUB_NO6: add make commands to backup/restore firmware
teensy port: no changes specific to this component/port
unix port:
- modffi: add option to lock GC in callback, and cfun access
- Makefile: add back LIB_SRC_C to list of object files
- variants: enable help and help("modules") on standard and dev
- Makefile: disable error compression on arm-linux-gnueabi-gcc
windows port:
- Makefile: add .exe extension to executables name
- appveyor: update to VS 2017 and use Python 3.8 for build/test
zephyr port:
- machine_spi: add support for hardware SPI
2021-10-14 15:38:41 -04:00
|
|
|
#include "shared/runtime/interrupt_char.h"
|
2020-06-25 20:57:17 -04:00
|
|
|
#include "py/gc.h"
|
|
|
|
#include "py/objlist.h"
|
|
|
|
#include "py/objstr.h"
|
|
|
|
#include "py/qstr.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "shared-bindings/_bleio/__init__.h"
|
|
|
|
#include "shared-bindings/_bleio/Adapter.h"
|
|
|
|
#include "shared-bindings/_bleio/Attribute.h"
|
|
|
|
#include "shared-bindings/_bleio/Characteristic.h"
|
|
|
|
#include "shared-bindings/_bleio/Service.h"
|
|
|
|
#include "shared-bindings/_bleio/UUID.h"
|
|
|
|
#include "supervisor/shared/tick.h"
|
|
|
|
|
|
|
|
#define BLE_ADV_LENGTH_FIELD_SIZE 1
|
|
|
|
#define BLE_ADV_AD_TYPE_FIELD_SIZE 1
|
|
|
|
#define BLE_AD_TYPE_FLAGS_DATA_SIZE 1
|
|
|
|
|
2020-06-26 17:23:20 -04:00
|
|
|
// static const ble_gap_sec_params_t pairing_sec_params = {
|
|
|
|
// .bond = 1,
|
|
|
|
// .mitm = 0,
|
|
|
|
// .lesc = 0,
|
|
|
|
// .keypress = 0,
|
|
|
|
// .oob = 0,
|
|
|
|
// .io_caps = BLE_GAP_IO_CAPS_NONE,
|
|
|
|
// .min_key_size = 7,
|
|
|
|
// .max_key_size = 16,
|
|
|
|
// .kdist_own = { .enc = 1, .id = 1},
|
|
|
|
// .kdist_peer = { .enc = 1, .id = 1},
|
|
|
|
// };
|
2020-06-25 20:57:17 -04:00
|
|
|
|
|
|
|
#define CONNECTION_DEBUG (1)
|
|
|
|
#if CONNECTION_DEBUG
|
|
|
|
#define CONNECTION_DEBUG_PRINTF(...) printf(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define CONNECTION_DEBUG_PRINTF(...)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static volatile bool m_discovery_in_process;
|
|
|
|
static volatile bool m_discovery_successful;
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
// FIX static bleio_service_obj_t *m_char_discovery_service;
|
|
|
|
// FIX static bleio_characteristic_obj_t *m_desc_discovery_characteristic;
|
2020-06-26 17:23:20 -04:00
|
|
|
|
|
|
|
// bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
|
|
|
|
// bleio_connection_internal_t *self = (bleio_connection_internal_t*)self_in;
|
|
|
|
|
|
|
|
// if (BLE_GAP_EVT_BASE <= ble_evt->header.evt_id && ble_evt->header.evt_id <= BLE_GAP_EVT_LAST &&
|
|
|
|
// ble_evt->evt.gap_evt.conn_handle != self->conn_handle) {
|
|
|
|
// return false;
|
|
|
|
// }
|
|
|
|
// if (BLE_GATTS_EVT_BASE <= ble_evt->header.evt_id && ble_evt->header.evt_id <= BLE_GATTS_EVT_LAST &&
|
|
|
|
// ble_evt->evt.gatts_evt.conn_handle != self->conn_handle) {
|
|
|
|
// return false;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// switch (ble_evt->header.evt_id) {
|
|
|
|
// case BLE_GAP_EVT_DISCONNECTED:
|
|
|
|
// // Adapter.c does the work for this event.
|
|
|
|
// break;
|
|
|
|
|
|
|
|
// case BLE_GAP_EVT_PHY_UPDATE_REQUEST: {
|
|
|
|
// ble_gap_phys_t const phys = {
|
|
|
|
// .rx_phys = BLE_GAP_PHY_AUTO,
|
|
|
|
// .tx_phys = BLE_GAP_PHY_AUTO,
|
|
|
|
// };
|
|
|
|
// sd_ble_gap_phy_update(ble_evt->evt.gap_evt.conn_handle, &phys);
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// case BLE_GAP_EVT_PHY_UPDATE: { // 0x22
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// case BLE_GAP_EVT_DATA_LENGTH_UPDATE_REQUEST:
|
|
|
|
// // SoftDevice will respond to a length update request.
|
|
|
|
// sd_ble_gap_data_length_update(self->conn_handle, NULL, NULL);
|
|
|
|
// break;
|
|
|
|
|
|
|
|
// case BLE_GAP_EVT_DATA_LENGTH_UPDATE: { // 0x24
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// case BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST: {
|
|
|
|
// ble_gatts_evt_exchange_mtu_request_t *request =
|
|
|
|
// &ble_evt->evt.gatts_evt.params.exchange_mtu_request;
|
|
|
|
|
|
|
|
// uint16_t new_mtu = BLE_GATTS_VAR_ATTR_LEN_MAX;
|
|
|
|
// if (request->client_rx_mtu < new_mtu) {
|
|
|
|
// new_mtu = request->client_rx_mtu;
|
|
|
|
// }
|
|
|
|
// if (new_mtu < BLE_GATT_ATT_MTU_DEFAULT) {
|
|
|
|
// new_mtu = BLE_GATT_ATT_MTU_DEFAULT;
|
|
|
|
// }
|
|
|
|
// if (self->mtu > 0) {
|
|
|
|
// new_mtu = self->mtu;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// self->mtu = new_mtu;
|
|
|
|
// sd_ble_gatts_exchange_mtu_reply(self->conn_handle, new_mtu);
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// case BLE_GATTC_EVT_EXCHANGE_MTU_RSP: {
|
|
|
|
// ble_gattc_evt_exchange_mtu_rsp_t *response =
|
|
|
|
// &ble_evt->evt.gattc_evt.params.exchange_mtu_rsp;
|
|
|
|
|
|
|
|
// self->mtu = response->server_rx_mtu;
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// case BLE_GATTS_EVT_WRITE:
|
|
|
|
// // A client wrote a value.
|
|
|
|
// // If we are bonded and it's a CCCD (UUID 0x2902), store the CCCD value.
|
|
|
|
// if (self->conn_handle != BLE_CONN_HANDLE_INVALID &&
|
|
|
|
// self->pair_status == PAIR_PAIRED &&
|
|
|
|
// ble_evt->evt.gatts_evt.params.write.uuid.type == BLE_UUID_TYPE_BLE &&
|
|
|
|
// ble_evt->evt.gatts_evt.params.write.uuid.uuid == 0x2902) {
|
|
|
|
// //
|
|
|
|
// // Save sys_attr data (CCCD state) in bonding area at
|
|
|
|
// // next opportunity, but also remember time of this
|
|
|
|
// // request, so we can consolidate closely-spaced requests.
|
|
|
|
// self->do_bond_cccds = true;
|
|
|
|
// self->do_bond_cccds_request_time = supervisor_ticks_ms64();
|
|
|
|
// }
|
|
|
|
// // Return false so other handlers get this event as well.
|
|
|
|
// return false;
|
|
|
|
|
|
|
|
// case BLE_GATTS_EVT_SYS_ATTR_MISSING:
|
|
|
|
// sd_ble_gatts_sys_attr_set(self->conn_handle, NULL, 0, 0);
|
|
|
|
// break;
|
|
|
|
|
|
|
|
// #if CIRCUITPY_VERBOSE_BLE
|
|
|
|
// // Use read authorization to snoop on all reads when doing verbose debugging.
|
|
|
|
// case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST: {
|
|
|
|
|
|
|
|
// ble_gatts_evt_rw_authorize_request_t *request =
|
|
|
|
// &ble_evt->evt.gatts_evt.params.authorize_request;
|
|
|
|
|
|
|
|
// mp_printf(&mp_plat_print, "Read %x offset %d ", request->request.read.handle, request->request.read.offset);
|
|
|
|
// uint8_t value_bytes[22];
|
|
|
|
// ble_gatts_value_t value;
|
|
|
|
// value.offset = request->request.read.offset;
|
|
|
|
// value.len = 22;
|
|
|
|
// value.p_value = value_bytes;
|
|
|
|
|
|
|
|
// sd_ble_gatts_value_get(self->conn_handle, request->request.read.handle, &value);
|
|
|
|
// size_t len = value.len;
|
|
|
|
// if (len > 22) {
|
|
|
|
// len = 22;
|
|
|
|
// }
|
|
|
|
// for (uint8_t i = 0; i < len; i++) {
|
|
|
|
// mp_printf(&mp_plat_print, " %02x", value_bytes[i]);
|
|
|
|
// }
|
|
|
|
// mp_printf(&mp_plat_print, "\n");
|
|
|
|
// ble_gatts_rw_authorize_reply_params_t reply;
|
|
|
|
// reply.type = request->type;
|
|
|
|
// reply.params.read.gatt_status = BLE_GATT_STATUS_SUCCESS;
|
|
|
|
// reply.params.read.update = false;
|
|
|
|
// reply.params.read.offset = request->request.read.offset;
|
|
|
|
// sd_ble_gatts_rw_authorize_reply(self->conn_handle, &reply);
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
// #endif
|
|
|
|
|
|
|
|
// case BLE_GATTS_EVT_HVN_TX_COMPLETE: // Capture this for now. 0x55
|
|
|
|
// break;
|
|
|
|
// case BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST: {
|
|
|
|
// self->conn_params_updating = true;
|
|
|
|
// ble_gap_evt_conn_param_update_request_t *request =
|
|
|
|
// &ble_evt->evt.gap_evt.params.conn_param_update_request;
|
|
|
|
// sd_ble_gap_conn_param_update(self->conn_handle, &request->conn_params);
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
// case BLE_GAP_EVT_CONN_PARAM_UPDATE: { // 0x12
|
|
|
|
// ble_gap_evt_conn_param_update_t *result =
|
|
|
|
// &ble_evt->evt.gap_evt.params.conn_param_update;
|
|
|
|
|
|
|
|
// #if CIRCUITPY_VERBOSE_BLE
|
|
|
|
// ble_gap_conn_params_t *cp = &ble_evt->evt.gap_evt.params.conn_param_update.conn_params;
|
|
|
|
// mp_printf(&mp_plat_print, "conn params updated: min_ci %d max_ci %d s_l %d sup_timeout %d\n", cp->min_conn_interval, cp->max_conn_interval, cp->slave_latency, cp->conn_sup_timeout);
|
|
|
|
// #endif
|
|
|
|
|
|
|
|
// memcpy(&self->conn_params, &result->conn_params, sizeof(ble_gap_conn_params_t));
|
|
|
|
// self->conn_params_updating = false;
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
// case BLE_GAP_EVT_SEC_PARAMS_REQUEST: {
|
|
|
|
// // First time pairing.
|
|
|
|
// // 1. Either we or peer initiate the process
|
|
|
|
// // 2. Peer asks for security parameters using BLE_GAP_EVT_SEC_PARAMS_REQUEST.
|
|
|
|
// // 3. Pair Key exchange ("just works" implemented now; TODO: out-of-band key pairing)
|
|
|
|
// // 4. Connection is secured: BLE_GAP_EVT_CONN_SEC_UPDATE
|
|
|
|
// // 5. Long-term Keys exchanged: BLE_GAP_EVT_AUTH_STATUS
|
|
|
|
|
|
|
|
// bonding_clear_keys(&self->bonding_keys);
|
|
|
|
// self->ediv = EDIV_INVALID;
|
|
|
|
// ble_gap_sec_keyset_t keyset = {
|
|
|
|
// .keys_own = {
|
|
|
|
// .p_enc_key = &self->bonding_keys.own_enc,
|
|
|
|
// .p_id_key = NULL,
|
|
|
|
// .p_sign_key = NULL,
|
|
|
|
// .p_pk = NULL
|
|
|
|
// },
|
|
|
|
|
|
|
|
// .keys_peer = {
|
|
|
|
// .p_enc_key = &self->bonding_keys.peer_enc,
|
|
|
|
// .p_id_key = &self->bonding_keys.peer_id,
|
|
|
|
// .p_sign_key = NULL,
|
|
|
|
// .p_pk = NULL
|
|
|
|
// }
|
|
|
|
// };
|
|
|
|
|
|
|
|
// sd_ble_gap_sec_params_reply(self->conn_handle, BLE_GAP_SEC_STATUS_SUCCESS,
|
|
|
|
// self->is_central ? NULL : &pairing_sec_params,
|
|
|
|
// &keyset);
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// case BLE_GAP_EVT_LESC_DHKEY_REQUEST:
|
|
|
|
// // TODO for LESC pairing:
|
|
|
|
// // sd_ble_gap_lesc_dhkey_reply(...);
|
|
|
|
// break;
|
|
|
|
|
|
|
|
// case BLE_GAP_EVT_AUTH_STATUS: { // 0x19
|
|
|
|
// // Key exchange completed.
|
|
|
|
// ble_gap_evt_auth_status_t* status = &ble_evt->evt.gap_evt.params.auth_status;
|
|
|
|
// self->sec_status = status->auth_status;
|
|
|
|
// if (status->auth_status == BLE_GAP_SEC_STATUS_SUCCESS) {
|
|
|
|
// self->ediv = self->bonding_keys.own_enc.master_id.ediv;
|
|
|
|
// self->pair_status = PAIR_PAIRED;
|
|
|
|
// // Save keys in bonding area at next opportunity.
|
|
|
|
// self->do_bond_keys = true;
|
|
|
|
// } else {
|
|
|
|
// // Inform busy-waiter pairing has failed.
|
|
|
|
// self->pair_status = PAIR_NOT_PAIRED;
|
|
|
|
// }
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// case BLE_GAP_EVT_SEC_INFO_REQUEST: { // 0x14
|
|
|
|
// // Peer asks for the stored keys.
|
|
|
|
// // - load key and return if bonded previously.
|
|
|
|
// // - Else return NULL --> Initiate key exchange
|
|
|
|
// ble_gap_evt_sec_info_request_t* sec_info_request = &ble_evt->evt.gap_evt.params.sec_info_request;
|
|
|
|
// (void) sec_info_request;
|
|
|
|
// if ( bonding_load_keys(self->is_central, sec_info_request->master_id.ediv, &self->bonding_keys) ) {
|
|
|
|
// sd_ble_gap_sec_info_reply(
|
|
|
|
// self->conn_handle,
|
|
|
|
// &self->bonding_keys.own_enc.enc_info,
|
|
|
|
// &self->bonding_keys.peer_id.id_info,
|
|
|
|
// NULL);
|
|
|
|
// self->ediv = self->bonding_keys.own_enc.master_id.ediv;
|
|
|
|
// } else {
|
|
|
|
// // We don't have stored keys. Ask for keys.
|
|
|
|
// sd_ble_gap_sec_info_reply(self->conn_handle, NULL, NULL, NULL);
|
|
|
|
// }
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// case BLE_GAP_EVT_CONN_SEC_UPDATE: { // 0x1a
|
|
|
|
// // We get this both on first-time pairing and on subsequent pairings using stored keys.
|
|
|
|
// ble_gap_conn_sec_t* conn_sec = &ble_evt->evt.gap_evt.params.conn_sec_update.conn_sec;
|
|
|
|
// if (conn_sec->sec_mode.sm <= 1 && conn_sec->sec_mode.lv <= 1) {
|
|
|
|
// // Security setup did not succeed:
|
|
|
|
// // mode 0, level 0 means no access
|
|
|
|
// // mode 1, level 1 means open link
|
|
|
|
// // mode >=1 and/or level >=1 means encryption is set up
|
|
|
|
// self->pair_status = PAIR_NOT_PAIRED;
|
|
|
|
// } else {
|
|
|
|
// if (bonding_load_cccd_info(self->is_central, self->conn_handle, self->ediv)) {
|
|
|
|
// // Did an sd_ble_gatts_sys_attr_set() with the stored sys_attr values.
|
|
|
|
// } else {
|
|
|
|
// // No matching bonding found, so use fresh system attributes.
|
|
|
|
// sd_ble_gatts_sys_attr_set(self->conn_handle, NULL, 0, 0);
|
|
|
|
// }
|
|
|
|
// self->pair_status = PAIR_PAIRED;
|
|
|
|
// }
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// default:
|
|
|
|
// return false;
|
|
|
|
// }
|
|
|
|
// return true;
|
|
|
|
// }
|
2020-06-25 20:57:17 -04:00
|
|
|
|
|
|
|
void bleio_connection_clear(bleio_connection_internal_t *self) {
|
2020-08-13 00:03:39 -04:00
|
|
|
mp_obj_list_clear(MP_OBJ_FROM_PTR(self->remote_service_list));
|
2020-06-25 20:57:17 -04:00
|
|
|
|
2020-08-13 00:03:39 -04:00
|
|
|
self->conn_handle = BLE_CONN_HANDLE_INVALID;
|
2020-06-25 20:57:17 -04:00
|
|
|
self->pair_status = PAIR_NOT_PAIRED;
|
2020-08-13 00:03:39 -04:00
|
|
|
self->is_central = false;
|
2021-03-15 09:57:36 -04:00
|
|
|
// FIX bonding_clear_keys(&self->bonding_keys);
|
2020-06-25 20:57:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool common_hal_bleio_connection_get_paired(bleio_connection_obj_t *self) {
|
|
|
|
if (self->connection == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return self->connection->pair_status == PAIR_PAIRED;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool common_hal_bleio_connection_get_connected(bleio_connection_obj_t *self) {
|
|
|
|
if (self->connection == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-08-13 00:03:39 -04:00
|
|
|
return self->connection->conn_handle != BLE_CONN_HANDLE_INVALID;
|
2020-06-25 20:57:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_bleio_connection_disconnect(bleio_connection_internal_t *self) {
|
2020-08-13 00:03:39 -04:00
|
|
|
hci_disconnect(self->conn_handle);
|
2020-06-25 20:57:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_bleio_connection_pair(bleio_connection_internal_t *self, bool bond) {
|
|
|
|
self->pair_status = PAIR_WAITING;
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
// FIX check_nrf_error(sd_ble_gap_authenticate(self->conn_handle, &pairing_sec_params));
|
2020-06-25 20:57:17 -04:00
|
|
|
|
|
|
|
while (self->pair_status == PAIR_WAITING && !mp_hal_is_interrupted()) {
|
|
|
|
RUN_BACKGROUND_TASKS;
|
|
|
|
}
|
|
|
|
if (mp_hal_is_interrupted()) {
|
|
|
|
return;
|
|
|
|
}
|
2021-03-15 09:57:36 -04:00
|
|
|
// FIX check_sec_status(self->sec_status);
|
2020-06-25 20:57:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
mp_float_t common_hal_bleio_connection_get_connection_interval(bleio_connection_internal_t *self) {
|
|
|
|
while (self->conn_params_updating && !mp_hal_is_interrupted()) {
|
|
|
|
RUN_BACKGROUND_TASKS;
|
|
|
|
}
|
2021-03-15 09:57:36 -04:00
|
|
|
// FIX return 1.25f * self->conn_params.min_conn_interval;
|
2020-06-26 17:23:20 -04:00
|
|
|
return 0.0f;
|
2020-06-25 20:57:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the current negotiated MTU length, minus overhead.
|
|
|
|
mp_int_t common_hal_bleio_connection_get_max_packet_length(bleio_connection_internal_t *self) {
|
2020-08-13 00:03:39 -04:00
|
|
|
return (self->mtu == 0 ? BT_ATT_DEFAULT_LE_MTU : self->mtu) - 3;
|
2020-06-25 20:57:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_bleio_connection_set_connection_interval(bleio_connection_internal_t *self, mp_float_t new_interval) {
|
2020-06-26 17:23:20 -04:00
|
|
|
// self->conn_params_updating = true;
|
|
|
|
// uint16_t interval = new_interval / 1.25f;
|
|
|
|
// self->conn_params.min_conn_interval = interval;
|
|
|
|
// self->conn_params.max_conn_interval = interval;
|
|
|
|
// uint32_t status = NRF_ERROR_BUSY;
|
|
|
|
// while (status == NRF_ERROR_BUSY) {
|
|
|
|
// status = sd_ble_gap_conn_param_update(self->conn_handle, &self->conn_params);
|
|
|
|
// RUN_BACKGROUND_TASKS;
|
|
|
|
// }
|
|
|
|
// check_nrf_error(status);
|
2020-06-25 20:57:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// service_uuid may be NULL, to discover all services.
|
2020-06-26 17:23:20 -04:00
|
|
|
// STATIC bool discover_next_services(bleio_connection_internal_t* connection, uint16_t start_handle, ble_uuid_t *service_uuid) {
|
|
|
|
// m_discovery_successful = false;
|
|
|
|
// m_discovery_in_process = true;
|
|
|
|
|
|
|
|
// uint32_t nrf_err = NRF_ERROR_BUSY;
|
|
|
|
// while (nrf_err == NRF_ERROR_BUSY) {
|
|
|
|
// nrf_err = sd_ble_gattc_primary_services_discover(connection->conn_handle, start_handle, service_uuid);
|
|
|
|
// }
|
|
|
|
// check_nrf_error(nrf_err);
|
|
|
|
|
|
|
|
// // Wait for a discovery event.
|
|
|
|
// while (m_discovery_in_process) {
|
|
|
|
// MICROPY_VM_HOOK_LOOP;
|
|
|
|
// }
|
|
|
|
// return m_discovery_successful;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// STATIC bool discover_next_characteristics(bleio_connection_internal_t* connection, bleio_service_obj_t *service, uint16_t start_handle) {
|
|
|
|
// m_char_discovery_service = service;
|
|
|
|
|
|
|
|
// ble_gattc_handle_range_t handle_range;
|
|
|
|
// handle_range.start_handle = start_handle;
|
|
|
|
// handle_range.end_handle = service->end_handle;
|
|
|
|
|
|
|
|
// m_discovery_successful = false;
|
|
|
|
// m_discovery_in_process = true;
|
|
|
|
|
|
|
|
// uint32_t err_code = sd_ble_gattc_characteristics_discover(connection->conn_handle, &handle_range);
|
|
|
|
// if (err_code != NRF_SUCCESS) {
|
|
|
|
// return false;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // Wait for a discovery event.
|
|
|
|
// while (m_discovery_in_process) {
|
|
|
|
// MICROPY_VM_HOOK_LOOP;
|
|
|
|
// }
|
|
|
|
// return m_discovery_successful;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// STATIC bool discover_next_descriptors(bleio_connection_internal_t* connection, bleio_characteristic_obj_t *characteristic, uint16_t start_handle, uint16_t end_handle) {
|
|
|
|
// m_desc_discovery_characteristic = characteristic;
|
|
|
|
|
|
|
|
// ble_gattc_handle_range_t handle_range;
|
|
|
|
// handle_range.start_handle = start_handle;
|
|
|
|
// handle_range.end_handle = end_handle;
|
|
|
|
|
|
|
|
// m_discovery_successful = false;
|
|
|
|
// m_discovery_in_process = true;
|
|
|
|
|
|
|
|
// uint32_t err_code = sd_ble_gattc_descriptors_discover(connection->conn_handle, &handle_range);
|
|
|
|
// if (err_code != NRF_SUCCESS) {
|
|
|
|
// return false;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // Wait for a discovery event.
|
|
|
|
// while (m_discovery_in_process) {
|
|
|
|
// MICROPY_VM_HOOK_LOOP;
|
|
|
|
// }
|
|
|
|
// return m_discovery_successful;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// STATIC void on_primary_srv_discovery_rsp(ble_gattc_evt_prim_srvc_disc_rsp_t *response, bleio_connection_internal_t* connection) {
|
|
|
|
// for (size_t i = 0; i < response->count; ++i) {
|
|
|
|
// ble_gattc_service_t *gattc_service = &response->services[i];
|
|
|
|
|
|
|
|
// bleio_service_obj_t *service = m_new_obj(bleio_service_obj_t);
|
|
|
|
// service->base.type = &bleio_service_type;
|
|
|
|
|
|
|
|
// // Initialize several fields at once.
|
|
|
|
// bleio_service_from_connection(service, bleio_connection_new_from_internal(connection));
|
|
|
|
|
|
|
|
// service->is_remote = true;
|
|
|
|
// service->start_handle = gattc_service->handle_range.start_handle;
|
|
|
|
// service->end_handle = gattc_service->handle_range.end_handle;
|
|
|
|
// service->handle = gattc_service->handle_range.start_handle;
|
|
|
|
|
|
|
|
// if (gattc_service->uuid.type != BLE_UUID_TYPE_UNKNOWN) {
|
|
|
|
// // Known service UUID.
|
|
|
|
// bleio_uuid_obj_t *uuid = m_new_obj(bleio_uuid_obj_t);
|
|
|
|
// uuid->base.type = &bleio_uuid_type;
|
|
|
|
// bleio_uuid_construct_from_nrf_ble_uuid(uuid, &gattc_service->uuid);
|
|
|
|
// service->uuid = uuid;
|
|
|
|
// } else {
|
|
|
|
// // The discovery response contained a 128-bit UUID that has not yet been registered with the
|
|
|
|
// // softdevice via sd_ble_uuid_vs_add(). We need to fetch the 128-bit value and register it.
|
|
|
|
// // For now, just set the UUID to NULL.
|
|
|
|
// service->uuid = NULL;
|
|
|
|
// }
|
2020-08-13 00:03:39 -04:00
|
|
|
//
|
|
|
|
// mp_obj_list_append(MP_OBJ_FROM_PTR(connection->remote_service_list),
|
|
|
|
// MP_OBJ_FROM_PTR(service));
|
2020-06-26 17:23:20 -04:00
|
|
|
// }
|
2020-08-13 00:03:39 -04:00
|
|
|
//
|
2020-06-26 17:23:20 -04:00
|
|
|
// if (response->count > 0) {
|
|
|
|
// m_discovery_successful = true;
|
|
|
|
// }
|
|
|
|
// m_discovery_in_process = false;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// STATIC void on_char_discovery_rsp(ble_gattc_evt_char_disc_rsp_t *response, bleio_connection_internal_t* connection) {
|
|
|
|
// for (size_t i = 0; i < response->count; ++i) {
|
|
|
|
// ble_gattc_char_t *gattc_char = &response->chars[i];
|
|
|
|
|
|
|
|
// bleio_characteristic_obj_t *characteristic = m_new_obj(bleio_characteristic_obj_t);
|
|
|
|
// characteristic->base.type = &bleio_characteristic_type;
|
|
|
|
|
|
|
|
// bleio_uuid_obj_t *uuid = NULL;
|
|
|
|
|
|
|
|
// if (gattc_char->uuid.type != BLE_UUID_TYPE_UNKNOWN) {
|
|
|
|
// // Known characteristic UUID.
|
|
|
|
// uuid = m_new_obj(bleio_uuid_obj_t);
|
|
|
|
// uuid->base.type = &bleio_uuid_type;
|
|
|
|
// bleio_uuid_construct_from_nrf_ble_uuid(uuid, &gattc_char->uuid);
|
|
|
|
// } else {
|
|
|
|
// // The discovery response contained a 128-bit UUID that has not yet been registered with the
|
|
|
|
// // softdevice via sd_ble_uuid_vs_add(). We need to fetch the 128-bit value and register it.
|
|
|
|
// // For now, just leave the UUID as NULL.
|
|
|
|
// }
|
|
|
|
|
|
|
|
// bleio_characteristic_properties_t props =
|
|
|
|
// (gattc_char->char_props.broadcast ? CHAR_PROP_BROADCAST : 0) |
|
|
|
|
// (gattc_char->char_props.indicate ? CHAR_PROP_INDICATE : 0) |
|
|
|
|
// (gattc_char->char_props.notify ? CHAR_PROP_NOTIFY : 0) |
|
|
|
|
// (gattc_char->char_props.read ? CHAR_PROP_READ : 0) |
|
|
|
|
// (gattc_char->char_props.write ? CHAR_PROP_WRITE : 0) |
|
|
|
|
// (gattc_char->char_props.write_wo_resp ? CHAR_PROP_WRITE_NO_RESPONSE : 0);
|
|
|
|
|
|
|
|
// // Call common_hal_bleio_characteristic_construct() to initalize some fields and set up evt handler.
|
|
|
|
// common_hal_bleio_characteristic_construct(
|
|
|
|
// characteristic, m_char_discovery_service, gattc_char->handle_value, uuid,
|
|
|
|
// props, SECURITY_MODE_OPEN, SECURITY_MODE_OPEN,
|
|
|
|
// GATT_MAX_DATA_LENGTH, false, // max_length, fixed_length: values may not matter for gattc
|
|
|
|
// NULL);
|
|
|
|
|
2020-08-13 00:03:39 -04:00
|
|
|
// mp_obj_list_append(MP_OBJ_FROM_PTR(m_char_discovery_service->characteristic_list),
|
|
|
|
// MP_OBJ_FROM_PTR(characteristic));
|
2020-06-26 17:23:20 -04:00
|
|
|
// }
|
|
|
|
|
|
|
|
// if (response->count > 0) {
|
|
|
|
// m_discovery_successful = true;
|
|
|
|
// }
|
|
|
|
// m_discovery_in_process = false;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// STATIC void on_desc_discovery_rsp(ble_gattc_evt_desc_disc_rsp_t *response, bleio_connection_internal_t* connection) {
|
|
|
|
// for (size_t i = 0; i < response->count; ++i) {
|
|
|
|
// ble_gattc_desc_t *gattc_desc = &response->descs[i];
|
|
|
|
|
|
|
|
// // Remember handles for certain well-known descriptors.
|
|
|
|
// switch (gattc_desc->uuid.uuid) {
|
|
|
|
// case BLE_UUID_DESCRIPTOR_CLIENT_CHAR_CONFIG:
|
|
|
|
// m_desc_discovery_characteristic->cccd_handle = gattc_desc->handle;
|
|
|
|
// break;
|
|
|
|
|
|
|
|
// case BLE_UUID_DESCRIPTOR_SERVER_CHAR_CONFIG:
|
|
|
|
// m_desc_discovery_characteristic->sccd_handle = gattc_desc->handle;
|
|
|
|
// break;
|
|
|
|
|
|
|
|
// case BLE_UUID_DESCRIPTOR_CHAR_USER_DESC:
|
|
|
|
// m_desc_discovery_characteristic->user_desc_handle = gattc_desc->handle;
|
|
|
|
// break;
|
|
|
|
|
|
|
|
// default:
|
|
|
|
// // TODO: sd_ble_gattc_descriptors_discover() can return things that are not descriptors,
|
|
|
|
// // so ignore those.
|
|
|
|
// // https://devzone.nordicsemi.com/f/nordic-q-a/49500/sd_ble_gattc_descriptors_discover-is-returning-attributes-that-are-not-descriptors
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// bleio_descriptor_obj_t *descriptor = m_new_obj(bleio_descriptor_obj_t);
|
|
|
|
// descriptor->base.type = &bleio_descriptor_type;
|
|
|
|
|
|
|
|
// bleio_uuid_obj_t *uuid = NULL;
|
|
|
|
|
|
|
|
// if (gattc_desc->uuid.type != BLE_UUID_TYPE_UNKNOWN) {
|
|
|
|
// // Known descriptor UUID.
|
|
|
|
// uuid = m_new_obj(bleio_uuid_obj_t);
|
|
|
|
// uuid->base.type = &bleio_uuid_type;
|
|
|
|
// bleio_uuid_construct_from_nrf_ble_uuid(uuid, &gattc_desc->uuid);
|
|
|
|
// } else {
|
|
|
|
// // The discovery response contained a 128-bit UUID that has not yet been registered with the
|
|
|
|
// // softdevice via sd_ble_uuid_vs_add(). We need to fetch the 128-bit value and register it.
|
|
|
|
// // For now, just leave the UUID as NULL.
|
|
|
|
// }
|
|
|
|
|
|
|
|
// common_hal_bleio_descriptor_construct(
|
|
|
|
// descriptor, m_desc_discovery_characteristic, uuid,
|
|
|
|
// SECURITY_MODE_OPEN, SECURITY_MODE_OPEN,
|
|
|
|
// GATT_MAX_DATA_LENGTH, false, mp_const_empty_bytes);
|
|
|
|
// descriptor->handle = gattc_desc->handle;
|
|
|
|
|
2020-08-13 00:03:39 -04:00
|
|
|
// mp_obj_list_append(MP_OBJ_FROM_PTR(m_desc_discovery_characteristic->descriptor_list),
|
|
|
|
// MP_OBJ_FROM_PTR(descriptor));
|
2020-06-26 17:23:20 -04:00
|
|
|
// }
|
|
|
|
|
|
|
|
// if (response->count > 0) {
|
|
|
|
// m_discovery_successful = true;
|
|
|
|
// }
|
|
|
|
// m_discovery_in_process = false;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// STATIC bool discovery_on_ble_evt(ble_evt_t *ble_evt, mp_obj_t payload) {
|
|
|
|
// bleio_connection_internal_t* connection = MP_OBJ_TO_PTR(payload);
|
|
|
|
// switch (ble_evt->header.evt_id) {
|
|
|
|
// case BLE_GAP_EVT_DISCONNECTED:
|
|
|
|
// m_discovery_successful = false;
|
|
|
|
// m_discovery_in_process = false;
|
|
|
|
// break;
|
|
|
|
|
|
|
|
// case BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP:
|
|
|
|
// on_primary_srv_discovery_rsp(&ble_evt->evt.gattc_evt.params.prim_srvc_disc_rsp, connection);
|
|
|
|
// break;
|
|
|
|
|
|
|
|
// case BLE_GATTC_EVT_CHAR_DISC_RSP:
|
|
|
|
// on_char_discovery_rsp(&ble_evt->evt.gattc_evt.params.char_disc_rsp, connection);
|
|
|
|
// break;
|
|
|
|
|
|
|
|
// case BLE_GATTC_EVT_DESC_DISC_RSP:
|
|
|
|
// on_desc_discovery_rsp(&ble_evt->evt.gattc_evt.params.desc_disc_rsp, connection);
|
|
|
|
// break;
|
|
|
|
|
|
|
|
// default:
|
|
|
|
// // CONNECTION_DEBUG_PRINTF(&mp_plat_print, "Unhandled discovery event: 0x%04x\n", ble_evt->header.evt_id);
|
|
|
|
// return false;
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
// return true;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// STATIC void discover_remote_services(bleio_connection_internal_t *self, mp_obj_t service_uuids_whitelist) {
|
|
|
|
// ble_drv_add_event_handler(discovery_on_ble_evt, self);
|
|
|
|
|
|
|
|
// // Start over with an empty list.
|
2020-08-13 00:03:39 -04:00
|
|
|
// self->remote_service_list = mp_obj_new_list(0, NULL);
|
|
|
|
|
2020-06-26 17:23:20 -04:00
|
|
|
|
|
|
|
// if (service_uuids_whitelist == mp_const_none) {
|
|
|
|
// // List of service UUID's not given, so discover all available services.
|
|
|
|
|
|
|
|
// uint16_t next_service_start_handle = BLE_GATT_HANDLE_START;
|
|
|
|
|
|
|
|
// while (discover_next_services(self, next_service_start_handle, MP_OBJ_NULL)) {
|
|
|
|
// // discover_next_services() appends to remote_services_list.
|
|
|
|
|
|
|
|
// // Get the most recently discovered service, and then ask for services
|
|
|
|
// // whose handles start after the last attribute handle inside that service.
|
2020-08-13 00:03:39 -04:00
|
|
|
// // There must be at least one if discover_next_services() returned true.
|
|
|
|
// const bleio_service_obj_t *service =
|
|
|
|
// self->remote_service_list->items[self->remote_service_list->len - 1];
|
2020-06-26 17:23:20 -04:00
|
|
|
// next_service_start_handle = service->end_handle + 1;
|
|
|
|
// }
|
|
|
|
// } else {
|
|
|
|
// mp_obj_iter_buf_t iter_buf;
|
|
|
|
// mp_obj_t iterable = mp_getiter(service_uuids_whitelist, &iter_buf);
|
|
|
|
// mp_obj_t uuid_obj;
|
|
|
|
// while ((uuid_obj = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
|
2021-04-22 20:55:39 -04:00
|
|
|
// if (!mp_obj_is_type(uuid_obj, &bleio_uuid_type)) {
|
2020-06-26 17:23:20 -04:00
|
|
|
// mp_raise_TypeError(translate("non-UUID found in service_uuids_whitelist"));
|
|
|
|
// }
|
|
|
|
// bleio_uuid_obj_t *uuid = MP_OBJ_TO_PTR(uuid_obj);
|
|
|
|
|
|
|
|
// ble_uuid_t nrf_uuid;
|
|
|
|
// bleio_uuid_convert_to_nrf_ble_uuid(uuid, &nrf_uuid);
|
|
|
|
|
|
|
|
// // Service might or might not be discovered; that's ok. Caller has to check
|
|
|
|
// // Central.remote_services to find out.
|
|
|
|
// // We only need to call this once for each service to discover.
|
|
|
|
// discover_next_services(self, BLE_GATT_HANDLE_START, &nrf_uuid);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
2020-08-13 00:03:39 -04:00
|
|
|
// for (size_t i = 0; i < self->remote_service_list->len; i++) {
|
|
|
|
// bleio_service_obj_t *service = MP_OBJ_TO_PTR(self->remote_service_list->items[i]);
|
2020-06-26 17:23:20 -04:00
|
|
|
// // Skip the service if it had an unknown (unregistered) UUID.
|
|
|
|
// if (service->uuid == NULL) {
|
|
|
|
// continue;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// uint16_t next_char_start_handle = service->start_handle;
|
|
|
|
|
|
|
|
// // Stop when we go past the end of the range of handles for this service or
|
|
|
|
// // discovery call returns nothing.
|
|
|
|
// // discover_next_characteristics() appends to the characteristic_list.
|
|
|
|
// while (next_char_start_handle <= service->end_handle &&
|
|
|
|
// discover_next_characteristics(self, service, next_char_start_handle)) {
|
|
|
|
|
|
|
|
|
|
|
|
// // Get the most recently discovered characteristic, and then ask for characteristics
|
|
|
|
// // whose handles start after the last attribute handle inside that characteristic.
|
|
|
|
// const bleio_characteristic_obj_t *characteristic =
|
|
|
|
// MP_OBJ_TO_PTR(service->characteristic_list->items[service->characteristic_list->len - 1]);
|
|
|
|
|
|
|
|
// next_char_start_handle = characteristic->handle + 1;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // Got characteristics for this service. Now discover descriptors for each characteristic.
|
|
|
|
// size_t char_list_len = service->characteristic_list->len;
|
|
|
|
// for (size_t char_idx = 0; char_idx < char_list_len; ++char_idx) {
|
|
|
|
// bleio_characteristic_obj_t *characteristic =
|
|
|
|
// MP_OBJ_TO_PTR(service->characteristic_list->items[char_idx]);
|
|
|
|
// const bool last_characteristic = char_idx == char_list_len - 1;
|
|
|
|
// bleio_characteristic_obj_t *next_characteristic = last_characteristic
|
|
|
|
// ? NULL
|
|
|
|
// : MP_OBJ_TO_PTR(service->characteristic_list->items[char_idx + 1]);
|
|
|
|
|
|
|
|
// // Skip the characteristic if it had an unknown (unregistered) UUID.
|
|
|
|
// if (characteristic->uuid == NULL) {
|
|
|
|
// continue;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// uint16_t next_desc_start_handle = characteristic->handle + 1;
|
|
|
|
|
|
|
|
// // Don't run past the end of this service or the beginning of the next characteristic.
|
|
|
|
// uint16_t next_desc_end_handle = next_characteristic == NULL
|
|
|
|
// ? service->end_handle
|
|
|
|
// : next_characteristic->handle - 1;
|
|
|
|
|
|
|
|
// // Stop when we go past the end of the range of handles for this service or
|
|
|
|
// // discovery call returns nothing.
|
2020-08-11 16:21:16 -04:00
|
|
|
// // discover_next_descriptors() appends to the descriptor_linked_list.
|
2020-06-26 17:23:20 -04:00
|
|
|
// while (next_desc_start_handle <= service->end_handle &&
|
|
|
|
// next_desc_start_handle <= next_desc_end_handle &&
|
|
|
|
// discover_next_descriptors(self, characteristic,
|
|
|
|
// next_desc_start_handle, next_desc_end_handle)) {
|
|
|
|
// // Get the most recently discovered descriptor, and then ask for descriptors
|
|
|
|
// // whose handles start after that descriptor's handle.
|
2020-08-13 00:03:39 -04:00
|
|
|
// // There must be at least one if discover_next_descriptors() returned true.
|
|
|
|
// const bleio_descriptor_obj_t *descriptor =
|
|
|
|
// characteristic->descriptor_list->items[characteristic->descriptor_list->len - 1];
|
2020-06-26 17:23:20 -04:00
|
|
|
// next_desc_start_handle = descriptor->handle + 1;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // This event handler is no longer needed.
|
|
|
|
// ble_drv_remove_event_handler(discovery_on_ble_evt, self);
|
|
|
|
|
|
|
|
// }
|
2020-06-25 20:57:17 -04:00
|
|
|
|
|
|
|
mp_obj_tuple_t *common_hal_bleio_connection_discover_remote_services(bleio_connection_obj_t *self, mp_obj_t service_uuids_whitelist) {
|
2021-03-15 09:57:36 -04:00
|
|
|
// FIX discover_remote_services(self->connection, service_uuids_whitelist);
|
2020-06-25 20:57:17 -04:00
|
|
|
bleio_connection_ensure_connected(self);
|
|
|
|
// Convert to a tuple and then clear the list so the callee will take ownership.
|
2021-03-15 09:57:36 -04:00
|
|
|
mp_obj_tuple_t *services_tuple =
|
|
|
|
mp_obj_new_tuple(self->connection->remote_service_list->len,
|
|
|
|
self->connection->remote_service_list->items);
|
|
|
|
mp_obj_list_clear(MP_OBJ_FROM_PTR(self->connection->remote_service_list));
|
|
|
|
return services_tuple;
|
2020-06-25 20:57:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t bleio_connection_get_conn_handle(bleio_connection_obj_t *self) {
|
|
|
|
if (self == NULL || self->connection == NULL) {
|
|
|
|
return BLE_CONN_HANDLE_INVALID;
|
|
|
|
}
|
|
|
|
return self->connection->conn_handle;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
mp_obj_t bleio_connection_new_from_internal(bleio_connection_internal_t *internal) {
|
2020-06-25 20:57:17 -04:00
|
|
|
if (internal->connection_obj != mp_const_none) {
|
|
|
|
return internal->connection_obj;
|
|
|
|
}
|
|
|
|
bleio_connection_obj_t *connection = m_new_obj(bleio_connection_obj_t);
|
|
|
|
connection->base.type = &bleio_connection_type;
|
|
|
|
connection->connection = internal;
|
|
|
|
internal->connection_obj = connection;
|
|
|
|
|
|
|
|
return MP_OBJ_FROM_PTR(connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the connection that uses the given conn_handle. Return NULL if not found.
|
|
|
|
bleio_connection_internal_t *bleio_conn_handle_to_connection(uint16_t conn_handle) {
|
2020-08-13 00:03:39 -04:00
|
|
|
bleio_connection_internal_t *connection;
|
|
|
|
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
|
|
|
|
connection = &bleio_connections[i];
|
|
|
|
if (connection->conn_handle == conn_handle) {
|
|
|
|
return connection;
|
|
|
|
}
|
|
|
|
}
|
2020-06-25 20:57:17 -04:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|