|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2017 Glenn Ruben Bakke |
| 7 | + * Copyright (c) 2018 Artur Pacholec |
| 8 | + * |
| 9 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | + * of this software and associated documentation files (the "Software"), to deal |
| 11 | + * in the Software without restriction, including without limitation the rights |
| 12 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | + * copies of the Software, and to permit persons to whom the Software is |
| 14 | + * furnished to do so, subject to the following conditions: |
| 15 | + * |
| 16 | + * The above copyright notice and this permission notice shall be included in |
| 17 | + * all copies or substantial portions of the Software. |
| 18 | + * |
| 19 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | + * THE SOFTWARE. |
| 26 | + */ |
| 27 | + |
| 28 | +#include "py/objproperty.h" |
| 29 | +#include "py/runtime.h" |
| 30 | +#include "shared-bindings/bleio/Descriptor.h" |
| 31 | +#include "shared-bindings/bleio/UUID.h" |
| 32 | + |
| 33 | +enum { |
| 34 | + DescriptorUuidCharacteristicExtendedProperties = 0x2900, |
| 35 | + DescriptorUuidCharacteristicUserDescription = 0x2901, |
| 36 | + DescriptorUuidClientCharacteristicConfiguration = 0x2902, |
| 37 | + DescriptorUuidServerCharacteristicConfiguration = 0x2903, |
| 38 | + DescriptorUuidCharacteristicPresentationFormat = 0x2904, |
| 39 | + DescriptorUuidCharacteristicAggregateFormat = 0x2905, |
| 40 | + DescriptorUuidValidRange = 0x2906, |
| 41 | + DescriptorUuidExternalReportReference = 0x2907, |
| 42 | + DescriptorUuidReportReference = 0x2908, |
| 43 | + DescriptorUuidNumberOfDigitals = 0x2909, |
| 44 | + DescriptorUuidValueTriggerSetting = 0x290A, |
| 45 | + DescriptorUuidEnvironmentalSensingConfiguration = 0x290B, |
| 46 | + DescriptorUuidEnvironmentalSensingMeasurement = 0x290C, |
| 47 | + DescriptorUuidEnvironmentalSensingTriggerSetting = 0x290D, |
| 48 | + DescriptorUuidTimeTriggerSetting = 0x290E, |
| 49 | +}; |
| 50 | + |
| 51 | +//| .. currentmodule:: bleio |
| 52 | +//| |
| 53 | +//| :class:`Descriptor` -- BLE descriptor |
| 54 | +//| ========================================================= |
| 55 | +//| |
| 56 | +//| Stores information about a BLE descriptor. |
| 57 | +//| Descriptors are encapsulated by BLE characteristics and provide contextual |
| 58 | +//| information about the characteristic. |
| 59 | +//| |
| 60 | + |
| 61 | +//| .. class:: Descriptor(uuid) |
| 62 | +//| |
| 63 | +//| Create a new descriptor object with the UUID uuid. |
| 64 | +//| The value can be either of type `bleio.UUID` or any value allowed by the `bleio.UUID` constructor. |
| 65 | + |
| 66 | +//| .. attribute:: handle |
| 67 | +//| |
| 68 | +//| The descriptor handle. (read-only) |
| 69 | +//| |
| 70 | + |
| 71 | +//| .. attribute:: uuid |
| 72 | +//| |
| 73 | +//| The descriptor uuid. (read-only) |
| 74 | +//| |
| 75 | +STATIC mp_obj_t bleio_descriptor_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) { |
| 76 | + mp_arg_check_num(n_args, n_kw, 0, 1, true); |
| 77 | + bleio_descriptor_obj_t *self = m_new_obj(bleio_descriptor_obj_t); |
| 78 | + self->base.type = type; |
| 79 | + |
| 80 | + mp_map_t kw_args; |
| 81 | + mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args); |
| 82 | + |
| 83 | + enum { ARG_uuid }; |
| 84 | + static const mp_arg_t allowed_args[] = { |
| 85 | + { ARG_uuid, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, |
| 86 | + }; |
| 87 | + |
| 88 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 89 | + mp_arg_parse_all(n_args, pos_args, &kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 90 | + |
| 91 | + const mp_obj_t uuid_arg = args[ARG_uuid].u_obj; |
| 92 | + |
| 93 | + bleio_uuid_obj_t *uuid; |
| 94 | + if (MP_OBJ_IS_TYPE(uuid_arg, &bleio_uuid_type)) { |
| 95 | + uuid = MP_OBJ_TO_PTR(uuid_arg); |
| 96 | + } else { |
| 97 | + uuid = MP_OBJ_TO_PTR(bleio_uuid_type.make_new(&bleio_uuid_type, 1, 0, &uuid_arg)); |
| 98 | + } |
| 99 | + |
| 100 | + common_hal_bleio_descriptor_construct(self, uuid); |
| 101 | + |
| 102 | + return MP_OBJ_FROM_PTR(self); |
| 103 | +} |
| 104 | + |
| 105 | +STATIC void bleio_descriptor_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { |
| 106 | + bleio_descriptor_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 107 | + |
| 108 | + common_hal_bleio_descriptor_print(self, print); |
| 109 | +} |
| 110 | + |
| 111 | +STATIC mp_obj_t bleio_descriptor_get_handle(mp_obj_t self_in) { |
| 112 | + bleio_descriptor_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 113 | + |
| 114 | + return mp_obj_new_int(common_hal_bleio_descriptor_get_handle(self)); |
| 115 | +} |
| 116 | +MP_DEFINE_CONST_FUN_OBJ_1(bleio_descriptor_get_handle_obj, bleio_descriptor_get_handle); |
| 117 | + |
| 118 | +const mp_obj_property_t bleio_descriptor_handle_obj = { |
| 119 | + .base.type = &mp_type_property, |
| 120 | + .proxy = {(mp_obj_t)&bleio_descriptor_get_handle_obj, |
| 121 | + (mp_obj_t)&mp_const_none_obj, |
| 122 | + (mp_obj_t)&mp_const_none_obj}, |
| 123 | +}; |
| 124 | + |
| 125 | +STATIC mp_obj_t bleio_descriptor_get_uuid(mp_obj_t self_in) { |
| 126 | + bleio_descriptor_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 127 | + const mp_obj_t uuid = mp_obj_new_int(common_hal_bleio_descriptor_get_uuid(self)); |
| 128 | + |
| 129 | + return bleio_uuid_type.make_new(&bleio_uuid_type, 1, 0, &uuid); |
| 130 | +} |
| 131 | +MP_DEFINE_CONST_FUN_OBJ_1(bleio_descriptor_get_uuid_obj, bleio_descriptor_get_uuid); |
| 132 | + |
| 133 | +const mp_obj_property_t bleio_descriptor_uuid_obj = { |
| 134 | + .base.type = &mp_type_property, |
| 135 | + .proxy = {(mp_obj_t)&bleio_descriptor_get_uuid_obj, |
| 136 | + (mp_obj_t)&mp_const_none_obj, |
| 137 | + (mp_obj_t)&mp_const_none_obj}, |
| 138 | +}; |
| 139 | + |
| 140 | +STATIC const mp_rom_map_elem_t bleio_descriptor_locals_dict_table[] = { |
| 141 | + // Properties |
| 142 | + { MP_ROM_QSTR(MP_QSTR_handle), MP_ROM_PTR(&bleio_descriptor_handle_obj) }, |
| 143 | + { MP_ROM_QSTR(MP_QSTR_uuid), MP_ROM_PTR(&bleio_descriptor_uuid_obj) }, |
| 144 | + |
| 145 | + // Static variables |
| 146 | + { MP_ROM_QSTR(MP_QSTR_CHARACTERISTIC_EXTENDED_PROPERTIES), MP_ROM_INT(DescriptorUuidCharacteristicExtendedProperties) }, |
| 147 | + { MP_ROM_QSTR(MP_QSTR_CHARACTERISTIC_USER_DESCRIPTION), MP_ROM_INT(DescriptorUuidCharacteristicUserDescription) }, |
| 148 | + { MP_ROM_QSTR(MP_QSTR_CLIENT_CHARACTERISTIC_CONFIGURATION), MP_ROM_INT(DescriptorUuidClientCharacteristicConfiguration) }, |
| 149 | + { MP_ROM_QSTR(MP_QSTR_SERVER_CHARACTERISTIC_CONFIGURATION), MP_ROM_INT(DescriptorUuidServerCharacteristicConfiguration) }, |
| 150 | + { MP_ROM_QSTR(MP_QSTR_CHARACTERISTIC_PRESENTATION_FORMAT), MP_ROM_INT(DescriptorUuidCharacteristicPresentationFormat) }, |
| 151 | + { MP_ROM_QSTR(MP_QSTR_CHARACTERISTIC_AGGREGATE_FORMAT), MP_ROM_INT(DescriptorUuidCharacteristicAggregateFormat) }, |
| 152 | + { MP_ROM_QSTR(MP_QSTR_VALID_RANGE), MP_ROM_INT(DescriptorUuidValidRange) }, |
| 153 | + { MP_ROM_QSTR(MP_QSTR_EXTERNAL_REPORT_REFERENCE), MP_ROM_INT(DescriptorUuidExternalReportReference) }, |
| 154 | + { MP_ROM_QSTR(MP_QSTR_REPORT_REFERENCE), MP_ROM_INT(DescriptorUuidReportReference) }, |
| 155 | + { MP_ROM_QSTR(MP_QSTR_NUMBER_OF_DIGITALS), MP_ROM_INT(DescriptorUuidNumberOfDigitals) }, |
| 156 | + { MP_ROM_QSTR(MP_QSTR_VALUE_TRIGGER_SETTING), MP_ROM_INT(DescriptorUuidValueTriggerSetting) }, |
| 157 | + { MP_ROM_QSTR(MP_QSTR_ENVIRONMENTAL_SENSING_CONFIGURATION), MP_ROM_INT(DescriptorUuidEnvironmentalSensingConfiguration) }, |
| 158 | + { MP_ROM_QSTR(MP_QSTR_ENVIRONMENTAL_SENSING_MEASUREMENT ), MP_ROM_INT(DescriptorUuidEnvironmentalSensingMeasurement) }, |
| 159 | + { MP_ROM_QSTR(MP_QSTR_ENVIRONMENTAL_SENSING_TRIGGER_SETTING), MP_ROM_INT(DescriptorUuidEnvironmentalSensingTriggerSetting) }, |
| 160 | + { MP_ROM_QSTR(MP_QSTR_TIME_TRIGGER_SETTING), MP_ROM_INT(DescriptorUuidTimeTriggerSetting) } |
| 161 | +}; |
| 162 | + |
| 163 | +STATIC MP_DEFINE_CONST_DICT(bleio_descriptor_locals_dict, bleio_descriptor_locals_dict_table); |
| 164 | + |
| 165 | +const mp_obj_type_t bleio_descriptor_type = { |
| 166 | + { &mp_type_type }, |
| 167 | + .name = MP_QSTR_Descriptor, |
| 168 | + .print = bleio_descriptor_print, |
| 169 | + .make_new = bleio_descriptor_make_new, |
| 170 | + .locals_dict = (mp_obj_dict_t*)&bleio_descriptor_locals_dict |
| 171 | +}; |
0 commit comments