diff --git a/ports/nrf/.gitignore b/ports/nrf/.gitignore index fc9f24b50d..cda23c7a90 100644 --- a/ports/nrf/.gitignore +++ b/ports/nrf/.gitignore @@ -1,8 +1,8 @@ # Old Nordic soft devices that don't allow redistribution ######################################################### -bluetooth/s132_nrf52_2.0.1/ +drivers/bluetooth/s132_nrf52_2.0.1/ -!bluetooth/*/*.hex +!drivers/bluetooth/*/*.hex # Build files ##################### diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile index f7e4a29884..2e345b9c7b 100755 --- a/ports/nrf/Makefile +++ b/ports/nrf/Makefile @@ -174,6 +174,7 @@ SRC_COMMON_HAL += \ bleio/Adapter.c \ bleio/Broadcaster.c \ bleio/Characteristic.c \ + bleio/CharacteristicBuffer.c \ bleio/Descriptor.c \ bleio/Peripheral.c \ bleio/Scanner.c \ diff --git a/ports/nrf/bluetooth/ble_drv.c b/ports/nrf/bluetooth/ble_drv.c index 7082b1e59c..cdc56319bd 100644 --- a/ports/nrf/bluetooth/ble_drv.c +++ b/ports/nrf/bluetooth/ble_drv.c @@ -71,6 +71,20 @@ void ble_drv_add_event_handler(ble_drv_evt_handler_t func, void *param) { m_event_handlers = handler; } +void ble_drv_remove_event_handler(ble_drv_evt_handler_t func, void *param) { + event_handler_t *it = m_event_handlers; + event_handler_t **prev = &m_event_handlers; + while (it != NULL) { + if ((it->func == func) && (it->param == param)) { + // Splice out the matching handler. + *prev = it->next; + return; + } + prev = &(it->next); + it = it->next; + } +} + void SD_EVT_IRQHandler(void) { uint32_t evt_id; while (sd_evt_get(&evt_id) != NRF_ERROR_NOT_FOUND) { diff --git a/ports/nrf/bluetooth/ble_drv.h b/ports/nrf/bluetooth/ble_drv.h index 696aff1da1..3d1f551c53 100644 --- a/ports/nrf/bluetooth/ble_drv.h +++ b/ports/nrf/bluetooth/ble_drv.h @@ -52,5 +52,6 @@ typedef void (*ble_drv_evt_handler_t)(ble_evt_t*, void*); void ble_drv_reset(); void ble_drv_add_event_handler(ble_drv_evt_handler_t func, void *param); +void ble_drv_remove_event_handler(ble_drv_evt_handler_t func, void *param); #endif // MICROPY_INCLUDED_NRF_BLUETOOTH_BLE_DRV_H diff --git a/ports/nrf/bluetooth/download_ble_stack.sh b/ports/nrf/bluetooth/download_ble_stack.sh deleted file mode 100755 index 7e35c0680b..0000000000 --- a/ports/nrf/bluetooth/download_ble_stack.sh +++ /dev/null @@ -1,32 +0,0 @@ -#!/bin/bash - -function download_s132_nrf52_2_0_1 -{ - echo "" - echo "####################################" - echo "### Downloading s132_nrf52_2.0.1 ###" - echo "####################################" - echo "" - - mkdir -p "${1}/s132_nrf52_2.0.1" - cd "${1}/s132_nrf52_2.0.1" - wget https://www.nordicsemi.com/api/sitecore/Products/MedialibraryZipDownload2 --post-data="ids=863031714A574444AADFE444EBE5BA9B|&fileName=DeviceDownload" -O temp.zip - unzip -u temp.zip - unzip -u s132nrf52201.zip - rm temp.zip s132nrf52201.zip - cd - -} - -SCRIPT_DIR="$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd)" - -if [ $# -eq 0 ]; then - echo "No Bluetooth LE stack defined, downloading all." - download_s132_nrf52_2_0_1 "${SCRIPT_DIR}" -else - case $1 in - "s132_nrf52_2_0_1" ) - download_s132_nrf52_2_0_1 "${SCRIPT_DIR}" ;; - esac -fi - -exit 0 diff --git a/ports/nrf/common-hal/bleio/Characteristic.c b/ports/nrf/common-hal/bleio/Characteristic.c index a955395bd5..bebdb31fa2 100644 --- a/ports/nrf/common-hal/bleio/Characteristic.c +++ b/ports/nrf/common-hal/bleio/Characteristic.c @@ -33,6 +33,7 @@ #include "py/runtime.h" #include "common-hal/bleio/__init__.h" +#include "common-hal/bleio/Characteristic.h" #include "shared-module/bleio/Characteristic.h" // TODO - should these be per object?? ***** @@ -190,28 +191,41 @@ STATIC void gattc_write(bleio_characteristic_obj_t *characteristic, mp_buffer_in STATIC void characteristic_on_ble_evt(ble_evt_t *ble_evt, void *param) { switch (ble_evt->header.evt_id) { - case BLE_GATTS_EVT_HVN_TX_COMPLETE: - m_tx_in_progress -= ble_evt->evt.gatts_evt.params.hvn_tx_complete.count; - break; + case BLE_GATTS_EVT_HVN_TX_COMPLETE: + m_tx_in_progress -= ble_evt->evt.gatts_evt.params.hvn_tx_complete.count; + break; - case BLE_GATTC_EVT_READ_RSP: - { - ble_gattc_evt_read_rsp_t *response = &ble_evt->evt.gattc_evt.params.read_rsp; - m_read_characteristic->value_data = mp_obj_new_bytearray(response->len, response->data); - // Flag to busy-wait loop that we've read the characteristic. - m_read_characteristic = NULL; - break; - } - - case BLE_GATTC_EVT_WRITE_RSP: - // Someone else can write now. - sd_mutex_release(m_write_mutex); - break; + case BLE_GATTC_EVT_READ_RSP: + { + ble_gattc_evt_read_rsp_t *response = &ble_evt->evt.gattc_evt.params.read_rsp; + m_read_characteristic->value_data = mp_obj_new_bytearray(response->len, response->data); + // Flag to busy-wait loop that we've read the characteristic. + m_read_characteristic = NULL; + break; } + + case BLE_GATTC_EVT_WRITE_RSP: + // Someone else can write now. + sd_mutex_release(m_write_mutex); + break; + + // For debugging. + default: + mp_printf(&mp_plat_print, "Unhandled characteristic event: 0x%04x\n", ble_evt->header.evt_id); + break; + } + } -void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self) { - ble_drv_add_event_handler(characteristic_on_ble_evt, NULL); +void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self, bleio_uuid_obj_t *uuid, bleio_characteristic_properties_t props) { + self->service = NULL; + self->uuid = uuid; + self->value_data = NULL; + self->props = props; + self->handle = BLE_GATT_HANDLE_INVALID; + + ble_drv_add_event_handler(characteristic_on_ble_evt, self); + } void common_hal_bleio_characteristic_get_value(bleio_characteristic_obj_t *self) { diff --git a/ports/nrf/common-hal/bleio/Characteristic.h b/ports/nrf/common-hal/bleio/Characteristic.h new file mode 100644 index 0000000000..bce1eec1d3 --- /dev/null +++ b/ports/nrf/common-hal/bleio/Characteristic.h @@ -0,0 +1,46 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * 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. + */ + +#ifndef MICROPY_INCLUDED_COMMON_HAL_BLEIO_CHARACTERISTIC_H +#define MICROPY_INCLUDED_COMMON_HAL_BLEIO_CHARACTERISTIC_H + +#include "shared-module/bleio/Characteristic.h" +#include "shared-module/bleio/Service.h" +#include "common-hal/bleio/UUID.h" + +typedef struct { + mp_obj_base_t base; + bleio_service_obj_t *service; + bleio_uuid_obj_t *uuid; + mp_obj_t value_data; + uint16_t handle; + bleio_characteristic_properties_t props; + uint16_t user_desc_handle; + uint16_t cccd_handle; + uint16_t sccd_handle; +} bleio_characteristic_obj_t; + +#endif // MICROPY_INCLUDED_COMMON_HAL_BLEIO_CHARACTERISTIC_H diff --git a/ports/nrf/common-hal/bleio/CharacteristicBuffer.c b/ports/nrf/common-hal/bleio/CharacteristicBuffer.c new file mode 100644 index 0000000000..4bd3e147ff --- /dev/null +++ b/ports/nrf/common-hal/bleio/CharacteristicBuffer.c @@ -0,0 +1,75 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * 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 +#include + +#include "ble_drv.h" +#include "ble_gatts.h" +#include "nrf_soc.h" + +#include "py/runtime.h" + +#include "common-hal/bleio/__init__.h" +#include "common-hal/bleio/CharacteristicBuffer.h" + +STATIC void characteristic_buffer_on_ble_evt(ble_evt_t *ble_evt, void *param) { + bleio_characteristic_buffer_obj_t *self = (bleio_characteristic_buffer_obj_t *) param; + switch (ble_evt->header.evt_id) { + case BLE_GATTS_EVT_WRITE: { + ble_gatts_evt_write_t *evt_write = &ble_evt->evt.gatts_evt.params.write; + // Event handle must match the handle for my characteristic. + if (evt_write->handle == self->characteristic->handle) { + // Push all the data onto the ring buffer. + for (size_t i = 0; i < evt_write->len; i++) { + ringbuf_put(&self->ringbuf, evt_write->data[i]); + } + break; + } + } + } + +} + +// Assumes that buffer_size has been validated before call. +void common_hal_bleio_characteristic_buffer_construct(bleio_characteristic_buffer_obj_t *self, bleio_characteristic_obj_t *characteristic, size_t buffer_size) { + + self->characteristic = characteristic; + // This is a macro. + ringbuf_alloc(&self->ringbuf, buffer_size); + + ble_drv_add_event_handler(characteristic_buffer_on_ble_evt, self); + +} + +// Returns a uint8_t byte value, or -1 if no data is available. +int common_hal_bleio_characteristic_buffer_read(bleio_characteristic_buffer_obj_t *self) { + return ringbuf_get(&self->ringbuf); +} + +void common_hal_bleio_characteristic_buffer_deinit(bleio_characteristic_buffer_obj_t *self) { + ble_drv_remove_event_handler(characteristic_buffer_on_ble_evt, self); +} diff --git a/ports/nrf/common-hal/bleio/CharacteristicBuffer.h b/ports/nrf/common-hal/bleio/CharacteristicBuffer.h new file mode 100644 index 0000000000..f9ff7dd66d --- /dev/null +++ b/ports/nrf/common-hal/bleio/CharacteristicBuffer.h @@ -0,0 +1,41 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * 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. + */ + +#ifndef MICROPY_INCLUDED_COMMON_HAL_BLEIO_CHARACTERISTICBUFFER_H +#define MICROPY_INCLUDED_COMMON_HAL_BLEIO_CHARACTERISTICBUFFER_H + +#include "py/ringbuf.h" + +#include "shared-bindings/bleio/Characteristic.h" + +typedef struct { + mp_obj_base_t base; + bleio_characteristic_obj_t *characteristic; + // Ring buffer storing consecutive incoming values. + ringbuf_t ringbuf; +} bleio_characteristic_buffer_obj_t; + +#endif // MICROPY_INCLUDED_COMMON_HAL_BLEIO_CHARACTERISTICBUFFER_H diff --git a/ports/nrf/common-hal/bleio/Peripheral.c b/ports/nrf/common-hal/bleio/Peripheral.c index 7d5d6a6a6c..0f2506de01 100644 --- a/ports/nrf/common-hal/bleio/Peripheral.c +++ b/ports/nrf/common-hal/bleio/Peripheral.c @@ -268,7 +268,7 @@ STATIC void peripheral_on_ble_evt(ble_evt_t *ble_evt, void *self_in) { } default: - mp_printf(&mp_plat_print, "Unhandled event: 0x%04x\n", ble_evt->header.evt_id); + mp_printf(&mp_plat_print, "Unhandled peripheral event: 0x%04x\n", ble_evt->header.evt_id); break; } } diff --git a/ports/nrf/common-hal/bleio/Service.c b/ports/nrf/common-hal/bleio/Service.c index c913c02aea..26a1f1cff5 100644 --- a/ports/nrf/common-hal/bleio/Service.c +++ b/ports/nrf/common-hal/bleio/Service.c @@ -28,6 +28,7 @@ #include "ble.h" #include "py/runtime.h" #include "common-hal/bleio/__init__.h" +#include "common-hal/bleio/Characteristic.h" #include "shared-bindings/bleio/Service.h" #include "shared-bindings/bleio/Adapter.h" @@ -87,6 +88,10 @@ void common_hal_bleio_service_add_all_characteristics(bleio_service_obj_t *self) mp_raise_OSError_msg_varg(translate("Failed to add characteristic, err 0x%04x"), err_code); } + if (characteristic->handle != BLE_GATT_HANDLE_INVALID) { + mp_raise_ValueError(translate("Characteristic already in use by another Service.")); + } + characteristic->user_desc_handle = handles.user_desc_handle; characteristic->cccd_handle = handles.cccd_handle; characteristic->sccd_handle = handles.sccd_handle; diff --git a/shared-bindings/bleio/Characteristic.c b/shared-bindings/bleio/Characteristic.c index 1d414b6d30..0a3fada139 100644 --- a/shared-bindings/bleio/Characteristic.c +++ b/shared-bindings/bleio/Characteristic.c @@ -55,13 +55,13 @@ STATIC mp_obj_t bleio_characteristic_make_new(const mp_obj_type_t *type, size_t mp_arg_check_num(n_args, n_kw, 1, 1, true); bleio_characteristic_obj_t *self = m_new_obj(bleio_characteristic_obj_t); self->base.type = &bleio_characteristic_type; - self->service = NULL; - self->value_data = NULL; mp_map_t kw_args; mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args); - enum { ARG_uuid, ARG_broadcast, ARG_indicate, ARG_notify, ARG_read, ARG_write, ARG_write_no_response }; + enum { + ARG_uuid, ARG_broadcast, ARG_indicate, ARG_notify, ARG_read, ARG_write, ARG_write_no_response, + }; static const mp_arg_t allowed_args[] = { { MP_QSTR_uuid, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = mp_const_none} }, { MP_QSTR_broadcast, MP_ARG_KW_ONLY| MP_ARG_BOOL, {.u_bool = false} }, @@ -83,14 +83,16 @@ STATIC mp_obj_t bleio_characteristic_make_new(const mp_obj_type_t *type, size_t self->uuid = MP_OBJ_TO_PTR(uuid); - self->props.broadcast = args[ARG_broadcast].u_bool; - self->props.indicate = args[ARG_indicate].u_bool; - self->props.notify = args[ARG_notify].u_bool; - self->props.read = args[ARG_read].u_bool; - self->props.write = args[ARG_write].u_bool; - self->props.write_no_response = args[ARG_write_no_response].u_bool; + bleio_characteristic_properties_t properties; - common_hal_bleio_characteristic_construct(self); + properties.broadcast = args[ARG_broadcast].u_bool; + properties.indicate = args[ARG_indicate].u_bool; + properties.notify = args[ARG_notify].u_bool; + properties.read = args[ARG_read].u_bool; + properties.write = args[ARG_write].u_bool; + properties.write_no_response = args[ARG_write_no_response].u_bool; + + common_hal_bleio_characteristic_construct(self, uuid, properties); return MP_OBJ_FROM_PTR(self); } diff --git a/shared-bindings/bleio/Characteristic.h b/shared-bindings/bleio/Characteristic.h index aec60ebbca..206cbcd403 100644 --- a/shared-bindings/bleio/Characteristic.h +++ b/shared-bindings/bleio/Characteristic.h @@ -27,11 +27,11 @@ #ifndef MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_CHARACTERISTIC_H #define MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_CHARACTERISTIC_H -#include "shared-module/bleio/Characteristic.h" +#include "common-hal/bleio/Characteristic.h" extern const mp_obj_type_t bleio_characteristic_type; -extern void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self); +extern void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self, bleio_uuid_obj_t *uuid, bleio_characteristic_properties_t props); extern void common_hal_bleio_characteristic_get_value(bleio_characteristic_obj_t *self); extern void common_hal_bleio_characteristic_set_value(bleio_characteristic_obj_t *self, mp_buffer_info_t *bufinfo); diff --git a/shared-bindings/bleio/CharacteristicBuffer.c b/shared-bindings/bleio/CharacteristicBuffer.c new file mode 100644 index 0000000000..770594d3a5 --- /dev/null +++ b/shared-bindings/bleio/CharacteristicBuffer.c @@ -0,0 +1,119 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 Dan Halbert for Adafruit Industries + * + * 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 "py/objproperty.h" +#include "py/runtime.h" +#include "shared-bindings/bleio/CharacteristicBuffer.h" +#include "shared-bindings/bleio/UUID.h" + +//| .. currentmodule:: bleio +//| +//| :class:`CharacteristicBuffer` -- GATT Service incoming values buffer. +//| ========================================================= +//| +//| Accumulates a Characteristic's incoming values in a FIFO buffer. +//| +//| .. class:: CharacteristicBuffer(Characteristic, buffer_size=0) +//| +//| Create a new Characteristic object identified by the specified UUID. +//| +//| :param bleio.Characteristic characteristic: The characteristic to monitor +//| :param int buffer_size: Size of ring buffer that stores incoming data coming from client. +//| Must be >= 1. +STATIC mp_obj_t bleio_characteristic_buffer_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) { + mp_arg_check_num(n_args, n_kw, 1, 2, true); + bleio_characteristic_buffer_obj_t *self = m_new_obj(bleio_characteristic_buffer_obj_t); + self->base.type = &bleio_characteristic_buffer_type; + + mp_map_t kw_args; + mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args); + + enum { ARG_characteristic, ARG_buffer_size, }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_characteristic, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_buffer_size, MP_ARG_REQUIRED | MP_ARG_INT }, + }; + + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args, pos_args, &kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + const mp_obj_t characteristic = args[ARG_characteristic].u_obj; + const int buffer_size = args[ARG_buffer_size].u_int; + + if (buffer_size < 1) { + mp_raise_ValueError(translate("buffer_size must be >= 1")); + } + + if (!MP_OBJ_IS_TYPE(characteristic, &bleio_characteristic_type)) { + mp_raise_ValueError(translate("Expected a Characteristic")); + } + + self->characteristic = MP_OBJ_TO_PTR(characteristic); + + common_hal_bleio_characteristic_buffer_construct(self, self->characteristic, buffer_size); + + return MP_OBJ_FROM_PTR(self); +} + + +//| .. method:: read() +//| +//| Read a single byte from the buffer. If no character is available, return None. +STATIC mp_obj_t bleio_characteristic_buffer_read(mp_obj_t self_in) { + bleio_characteristic_buffer_obj_t *self = MP_OBJ_TO_PTR(self_in); + + int byte = common_hal_bleio_characteristic_buffer_read(self); + if (byte == -1) { + return mp_const_none; + } + + return MP_OBJ_NEW_SMALL_INT(byte); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_characteristic_buffer_read_obj, bleio_characteristic_buffer_read); + +//| .. method:: deinit() +//| +//| Disable permanently. +STATIC mp_obj_t bleio_characteristic_buffer_deinit(mp_obj_t self_in) { + bleio_characteristic_buffer_obj_t *self = MP_OBJ_TO_PTR(self_in); + common_hal_bleio_characteristic_buffer_deinit(self); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_characteristic_buffer_deinit_obj, bleio_characteristic_buffer_deinit); + +STATIC const mp_rom_map_elem_t bleio_characteristic_buffer_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&bleio_characteristic_buffer_read_obj) }, + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&bleio_characteristic_buffer_deinit_obj) }, +}; + +STATIC MP_DEFINE_CONST_DICT(bleio_characteristic_buffer_locals_dict, bleio_characteristic_buffer_locals_dict_table); + +const mp_obj_type_t bleio_characteristic_buffer_type = { + { &mp_type_type }, + .name = MP_QSTR_CharacteristicBuffer, + .make_new = bleio_characteristic_buffer_make_new, + .locals_dict = (mp_obj_dict_t*)&bleio_characteristic_buffer_locals_dict +}; diff --git a/shared-bindings/bleio/CharacteristicBuffer.h b/shared-bindings/bleio/CharacteristicBuffer.h new file mode 100644 index 0000000000..c5d7580da3 --- /dev/null +++ b/shared-bindings/bleio/CharacteristicBuffer.h @@ -0,0 +1,39 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * 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. + */ + +#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_CHARACTERISTICBUFFER_H +#define MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_CHARACTERISTICBUFFER_H + +#include "common-hal/bleio/CharacteristicBuffer.h" + +extern const mp_obj_type_t bleio_characteristic_buffer_type; + +extern void common_hal_bleio_characteristic_buffer_construct(bleio_characteristic_buffer_obj_t *self, bleio_characteristic_obj_t *characteristic, size_t buffer_size); +// Returns a uint8_t byte value, or -1 if no data is available. +int common_hal_bleio_characteristic_buffer_read(bleio_characteristic_buffer_obj_t *self); +int common_hal_bleio_characteristic_buffer_deinit(bleio_characteristic_buffer_obj_t *self); + +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_CHARACTERISTICBUFFER_H diff --git a/shared-bindings/bleio/__init__.c b/shared-bindings/bleio/__init__.c index d30d599421..e23efe36c7 100644 --- a/shared-bindings/bleio/__init__.c +++ b/shared-bindings/bleio/__init__.c @@ -31,6 +31,7 @@ #include "shared-bindings/bleio/AdvertisementData.h" #include "shared-bindings/bleio/Broadcaster.h" #include "shared-bindings/bleio/Characteristic.h" +#include "shared-bindings/bleio/CharacteristicBuffer.h" #include "shared-bindings/bleio/Descriptor.h" #include "shared-bindings/bleio/Peripheral.h" #include "shared-bindings/bleio/ScanEntry.h" @@ -77,6 +78,7 @@ STATIC const mp_rom_map_elem_t bleio_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_AdvertisementData), MP_ROM_PTR(&bleio_advertisementdata_type) }, { MP_ROM_QSTR(MP_QSTR_Broadcaster), MP_ROM_PTR(&bleio_broadcaster_type) }, { MP_ROM_QSTR(MP_QSTR_Characteristic), MP_ROM_PTR(&bleio_characteristic_type) }, + { MP_ROM_QSTR(MP_QSTR_CharacteristicBuffer), MP_ROM_PTR(&bleio_characteristic_buffer_type) }, { MP_ROM_QSTR(MP_QSTR_Descriptor), MP_ROM_PTR(&bleio_descriptor_type) }, { MP_ROM_QSTR(MP_QSTR_Peripheral), MP_ROM_PTR(&bleio_peripheral_type) }, { MP_ROM_QSTR(MP_QSTR_ScanEntry), MP_ROM_PTR(&bleio_scanentry_type) }, diff --git a/shared-module/bleio/Characteristic.h b/shared-module/bleio/Characteristic.h index 977ec81976..958837e64f 100644 --- a/shared-module/bleio/Characteristic.h +++ b/shared-module/bleio/Characteristic.h @@ -27,26 +27,15 @@ #ifndef MICROPY_INCLUDED_SHARED_MODULE_BLEIO_CHARACTERISTIC_H #define MICROPY_INCLUDED_SHARED_MODULE_BLEIO_CHARACTERISTIC_H -#include "shared-module/bleio/Service.h" -#include "common-hal/bleio/UUID.h" - +// Flags for each characteristic property. Common across ports. typedef struct { - mp_obj_base_t base; - bleio_service_obj_t *service; - bleio_uuid_obj_t *uuid; - mp_obj_t value_data; - uint16_t handle; - struct { bool broadcast : 1; bool read : 1; bool write_no_response : 1; bool write : 1; bool notify : 1; bool indicate : 1; - } props; - uint16_t user_desc_handle; - uint16_t cccd_handle; - uint16_t sccd_handle; -} bleio_characteristic_obj_t; +} bleio_characteristic_properties_t; + #endif // MICROPY_INCLUDED_SHARED_MODULE_BLEIO_CHARACTERISTIC_H