2018-07-16 10:31:28 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2019-06-19 10:42:36 -04:00
|
|
|
* Copyright (c) 2019 Dan Halbert for Adafruit Industries
|
2018-07-16 10:31:28 -04:00
|
|
|
* Copyright (c) 2018 Artur Pacholec
|
2019-06-19 10:42:36 -04:00
|
|
|
* Copyright (c) 2017 Glenn Ruben Bakke
|
2018-07-16 10:31:28 -04:00
|
|
|
*
|
|
|
|
* 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"
|
2019-08-29 18:44:27 -04:00
|
|
|
#include "shared-bindings/_bleio/Attribute.h"
|
|
|
|
#include "shared-bindings/_bleio/Characteristic.h"
|
|
|
|
#include "shared-bindings/_bleio/Descriptor.h"
|
|
|
|
#include "shared-bindings/_bleio/UUID.h"
|
2018-07-16 10:31:28 -04:00
|
|
|
|
2020-05-01 18:23:27 -04:00
|
|
|
//| class Descriptor:
|
2020-05-12 20:15:28 -04:00
|
|
|
//| """Stores information about a BLE descriptor.
|
2018-07-16 10:31:28 -04:00
|
|
|
//|
|
2020-05-01 18:23:27 -04:00
|
|
|
//| Descriptors are attached to BLE characteristics and provide contextual
|
|
|
|
//| information about the characteristic."""
|
2018-07-16 10:31:28 -04:00
|
|
|
//|
|
2020-07-03 14:23:34 -04:00
|
|
|
//| def __init__(self) -> None:
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """There is no regular constructor for a Descriptor. A new local Descriptor can be created
|
|
|
|
//| and attached to a Characteristic by calling `add_to_characteristic()`.
|
|
|
|
//| Remote Descriptor objects are created by `Connection.discover_remote_services()`
|
2020-07-25 04:58:37 -04:00
|
|
|
//| as part of remote Characteristics in the remote Services that are discovered."""
|
|
|
|
//| @classmethod
|
2022-09-27 16:21:42 -04:00
|
|
|
//| def add_to_characteristic(
|
|
|
|
//| cls,
|
|
|
|
//| characteristic: Characteristic,
|
|
|
|
//| uuid: UUID,
|
|
|
|
//| *,
|
|
|
|
//| read_perm: int = Attribute.OPEN,
|
|
|
|
//| write_perm: int = Attribute.OPEN,
|
|
|
|
//| max_length: int = 20,
|
|
|
|
//| fixed_length: bool = False,
|
|
|
|
//| initial_value: ReadableBuffer = b""
|
|
|
|
//| ) -> Descriptor:
|
2020-07-25 04:58:37 -04:00
|
|
|
//| """Create a new Descriptor object, and add it to this Service.
|
2019-08-29 17:58:21 -04:00
|
|
|
//|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| :param Characteristic characteristic: The characteristic that will hold this descriptor
|
|
|
|
//| :param UUID uuid: The uuid of the descriptor
|
|
|
|
//| :param int read_perm: Specifies whether the descriptor can be read by a client, and if so, which
|
|
|
|
//| security mode is required. Must be one of the integer values `Attribute.NO_ACCESS`, `Attribute.OPEN`,
|
|
|
|
//| `Attribute.ENCRYPT_NO_MITM`, `Attribute.ENCRYPT_WITH_MITM`, `Attribute.LESC_ENCRYPT_WITH_MITM`,
|
|
|
|
//| `Attribute.SIGNED_NO_MITM`, or `Attribute.SIGNED_WITH_MITM`.
|
|
|
|
//| :param int write_perm: Specifies whether the descriptor can be written by a client, and if so, which
|
|
|
|
//| security mode is required. Values allowed are the same as ``read_perm``.
|
|
|
|
//| :param int max_length: Maximum length in bytes of the descriptor value. The maximum allowed is
|
|
|
|
//| is 512, or possibly 510 if ``fixed_length`` is False. The default, 20, is the maximum
|
|
|
|
//| number of data bytes that fit in a single BLE 4.x ATT packet.
|
|
|
|
//| :param bool fixed_length: True if the descriptor value is of fixed length.
|
2021-12-21 15:28:49 -05:00
|
|
|
//| :param ~circuitpython_typing.ReadableBuffer initial_value: The initial value for this descriptor.
|
2019-08-28 23:15:22 -04:00
|
|
|
//|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| :return: the new Descriptor."""
|
2020-05-01 18:23:27 -04:00
|
|
|
//| ...
|
2019-08-28 23:15:22 -04:00
|
|
|
STATIC mp_obj_t bleio_descriptor_add_to_characteristic(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
// class is arg[0], which we can ignore.
|
|
|
|
|
|
|
|
enum { ARG_characteristic, ARG_uuid, ARG_read_perm, ARG_write_perm,
|
|
|
|
ARG_max_length, ARG_fixed_length, ARG_initial_value };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_characteristic, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
|
|
|
{ MP_QSTR_uuid, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
2021-03-15 09:57:36 -04:00
|
|
|
{ MP_QSTR_read_perm, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = SECURITY_MODE_OPEN} },
|
|
|
|
{ MP_QSTR_write_perm, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = SECURITY_MODE_OPEN} },
|
|
|
|
{ MP_QSTR_max_length, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 20} },
|
|
|
|
{ MP_QSTR_fixed_length, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
|
|
|
|
{ MP_QSTR_initial_value, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_empty_bytes} },
|
2019-08-28 23:15:22 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
|
|
|
|
2021-10-10 02:46:14 -04:00
|
|
|
bleio_characteristic_obj_t *characteristic = mp_arg_validate_type(args[ARG_characteristic].u_obj, &bleio_characteristic_type, MP_QSTR_characteristic);
|
2019-08-28 23:15:22 -04:00
|
|
|
|
2021-10-10 02:46:14 -04:00
|
|
|
bleio_uuid_obj_t *uuid = mp_arg_validate_type(args[ARG_uuid].u_obj, &bleio_uuid_type, MP_QSTR_uuid);
|
2019-08-28 23:15:22 -04:00
|
|
|
|
|
|
|
const bleio_attribute_security_mode_t read_perm = args[ARG_read_perm].u_int;
|
|
|
|
common_hal_bleio_attribute_security_mode_check_valid(read_perm);
|
|
|
|
|
|
|
|
const bleio_attribute_security_mode_t write_perm = args[ARG_write_perm].u_int;
|
|
|
|
common_hal_bleio_attribute_security_mode_check_valid(write_perm);
|
|
|
|
|
2022-05-13 15:33:43 -04:00
|
|
|
const mp_int_t max_length_int = mp_arg_validate_int_min(args[ARG_max_length].u_int, 0, MP_QSTR_max_length);
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
const size_t max_length = (size_t)max_length_int;
|
|
|
|
const bool fixed_length = args[ARG_fixed_length].u_bool;
|
2019-08-28 23:15:22 -04:00
|
|
|
mp_obj_t initial_value = args[ARG_initial_value].u_obj;
|
|
|
|
|
|
|
|
mp_buffer_info_t initial_value_bufinfo;
|
|
|
|
if (initial_value == mp_const_none) {
|
|
|
|
if (fixed_length && max_length > 0) {
|
|
|
|
initial_value = mp_obj_new_bytes_of_zeros(max_length);
|
|
|
|
} else {
|
|
|
|
initial_value = mp_const_empty_bytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mp_get_buffer_raise(initial_value, &initial_value_bufinfo, MP_BUFFER_READ);
|
2020-08-14 23:38:58 -04:00
|
|
|
if (initial_value_bufinfo.len > max_length ||
|
|
|
|
(fixed_length && initial_value_bufinfo.len != max_length)) {
|
2021-03-15 09:57:36 -04:00
|
|
|
mp_raise_ValueError(translate("initial_value length is wrong"));
|
2020-08-14 23:38:58 -04:00
|
|
|
}
|
2019-08-28 23:15:22 -04:00
|
|
|
|
|
|
|
bleio_descriptor_obj_t *descriptor = m_new_obj(bleio_descriptor_obj_t);
|
|
|
|
descriptor->base.type = &bleio_descriptor_type;
|
|
|
|
|
|
|
|
// Range checking on max_length arg is done by the common_hal layer, because
|
|
|
|
// it may vary depending on underlying BLE implementation.
|
|
|
|
common_hal_bleio_descriptor_construct(
|
2021-10-10 02:46:14 -04:00
|
|
|
descriptor, characteristic, uuid,
|
2019-08-28 23:15:22 -04:00
|
|
|
read_perm, write_perm,
|
|
|
|
max_length, fixed_length, &initial_value_bufinfo);
|
|
|
|
|
2021-10-10 02:46:14 -04:00
|
|
|
common_hal_bleio_characteristic_add_descriptor(characteristic, descriptor);
|
2019-08-28 23:15:22 -04:00
|
|
|
|
|
|
|
return MP_OBJ_FROM_PTR(descriptor);
|
|
|
|
}
|
2021-10-07 16:36:06 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bleio_descriptor_add_to_characteristic_fun_obj, 1, bleio_descriptor_add_to_characteristic);
|
2019-08-28 23:15:22 -04:00
|
|
|
STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ(bleio_descriptor_add_to_characteristic_obj, MP_ROM_PTR(&bleio_descriptor_add_to_characteristic_fun_obj));
|
|
|
|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| uuid: UUID
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """The descriptor uuid. (read-only)"""
|
2018-07-16 10:31:28 -04:00
|
|
|
STATIC mp_obj_t bleio_descriptor_get_uuid(mp_obj_t self_in) {
|
|
|
|
bleio_descriptor_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-07-09 00:21:46 -04:00
|
|
|
|
|
|
|
bleio_uuid_obj_t *uuid = common_hal_bleio_descriptor_get_uuid(self);
|
|
|
|
return uuid ? MP_OBJ_FROM_PTR(uuid) : mp_const_none;
|
2018-07-16 10:31:28 -04:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(bleio_descriptor_get_uuid_obj, bleio_descriptor_get_uuid);
|
|
|
|
|
2022-05-01 12:24:05 -04:00
|
|
|
MP_PROPERTY_GETTER(bleio_descriptor_uuid_obj,
|
2022-05-01 11:16:53 -04:00
|
|
|
(mp_obj_t)&bleio_descriptor_get_uuid_obj);
|
2018-07-16 10:31:28 -04:00
|
|
|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| characteristic: Characteristic
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """The Characteristic this Descriptor is a part of."""
|
2019-08-06 22:55:25 -04:00
|
|
|
STATIC mp_obj_t bleio_descriptor_get_characteristic(mp_obj_t self_in) {
|
|
|
|
bleio_descriptor_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
|
|
|
|
return common_hal_bleio_descriptor_get_characteristic(self);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_descriptor_get_characteristic_obj, bleio_descriptor_get_characteristic);
|
|
|
|
|
2022-05-01 12:24:05 -04:00
|
|
|
MP_PROPERTY_GETTER(bleio_descriptor_characteristic_obj,
|
2022-05-01 11:16:53 -04:00
|
|
|
(mp_obj_t)&bleio_descriptor_get_characteristic_obj);
|
2019-08-06 22:55:25 -04:00
|
|
|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| value: bytearray
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """The value of this descriptor."""
|
2022-09-29 20:22:32 -04:00
|
|
|
//|
|
2019-08-07 23:49:09 -04:00
|
|
|
STATIC mp_obj_t bleio_descriptor_get_value(mp_obj_t self_in) {
|
|
|
|
bleio_descriptor_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
|
2019-09-14 15:40:24 -04:00
|
|
|
uint8_t temp[512];
|
|
|
|
size_t actual_len = common_hal_bleio_descriptor_get_value(self, temp, sizeof(temp));
|
|
|
|
return mp_obj_new_bytearray(actual_len, temp);
|
2019-08-07 23:49:09 -04:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_descriptor_get_value_obj, bleio_descriptor_get_value);
|
|
|
|
|
|
|
|
STATIC mp_obj_t bleio_descriptor_set_value(mp_obj_t self_in, mp_obj_t value_in) {
|
|
|
|
bleio_descriptor_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
|
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
mp_get_buffer_raise(value_in, &bufinfo, MP_BUFFER_READ);
|
|
|
|
|
|
|
|
common_hal_bleio_descriptor_set_value(self, &bufinfo);
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(bleio_descriptor_set_value_obj, bleio_descriptor_set_value);
|
|
|
|
|
2022-05-01 12:24:05 -04:00
|
|
|
MP_PROPERTY_GETSET(bleio_descriptor_value_obj,
|
2022-05-01 11:16:53 -04:00
|
|
|
(mp_obj_t)&bleio_descriptor_get_value_obj,
|
|
|
|
(mp_obj_t)&bleio_descriptor_set_value_obj);
|
2019-08-07 23:49:09 -04:00
|
|
|
|
2018-07-16 10:31:28 -04:00
|
|
|
STATIC const mp_rom_map_elem_t bleio_descriptor_locals_dict_table[] = {
|
2019-08-28 23:15:22 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_add_to_characteristic), MP_ROM_PTR(&bleio_descriptor_add_to_characteristic_obj) },
|
2018-07-16 10:31:28 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_uuid), MP_ROM_PTR(&bleio_descriptor_uuid_obj) },
|
2019-08-07 23:49:09 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_characteristic), MP_ROM_PTR(&bleio_descriptor_characteristic_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&bleio_descriptor_value_obj) },
|
2018-07-16 10:31:28 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(bleio_descriptor_locals_dict, bleio_descriptor_locals_dict_table);
|
|
|
|
|
2019-07-09 00:21:46 -04:00
|
|
|
STATIC void bleio_descriptor_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
|
|
|
bleio_descriptor_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
if (self->uuid) {
|
2019-07-16 19:53:36 -04:00
|
|
|
mp_printf(print, "Descriptor(");
|
2019-07-09 00:21:46 -04:00
|
|
|
bleio_uuid_print(print, MP_OBJ_FROM_PTR(self->uuid), kind);
|
2019-07-16 19:53:36 -04:00
|
|
|
mp_printf(print, ")");
|
2019-07-09 00:21:46 -04:00
|
|
|
} else {
|
2019-07-16 19:53:36 -04:00
|
|
|
mp_printf(print, "<Descriptor with Unregistered UUID>");
|
2019-07-09 00:21:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-16 10:31:28 -04:00
|
|
|
const mp_obj_type_t bleio_descriptor_type = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_Descriptor,
|
2019-07-09 00:21:46 -04:00
|
|
|
.print = bleio_descriptor_print,
|
2021-03-15 09:57:36 -04:00
|
|
|
.locals_dict = (mp_obj_dict_t *)&bleio_descriptor_locals_dict
|
2018-07-16 10:31:28 -04:00
|
|
|
};
|