Skip to content

Commit 20b8d51

Browse files
committed
nrf: Move the Descriptor class from ubluepy to the shared bleio module
1 parent f4940c9 commit 20b8d51

File tree

9 files changed

+300
-92
lines changed

9 files changed

+300
-92
lines changed

ports/nrf/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ DRIVERS_SRC_C += $(addprefix modules/,\
134134
ubluepy/ubluepy_characteristic.c \
135135
ubluepy/ubluepy_delegate.c \
136136
ubluepy/ubluepy_constants.c \
137-
ubluepy/ubluepy_descriptor.c \
138137
ubluepy/ubluepy_scanner.c \
139138
ubluepy/ubluepy_scan_entry.c \
140139
)
@@ -168,6 +167,7 @@ ifneq ($(SD), )
168167
SRC_COMMON_HAL += \
169168
bleio/__init__.c \
170169
bleio/Adapter.c \
170+
bleio/Descriptor.c \
171171
bleio/UUID.c
172172
endif
173173

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 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 "common-hal/bleio/Descriptor.h"
29+
30+
void common_hal_bleio_descriptor_construct(bleio_descriptor_obj_t *self, bleio_uuid_obj_t *uuid) {
31+
self->uuid = uuid;
32+
}
33+
34+
void common_hal_bleio_descriptor_print(bleio_descriptor_obj_t *self, const mp_print_t *print) {
35+
mp_printf(print, "Descriptor(uuid: 0x" HEX2_FMT HEX2_FMT ")",
36+
self->uuid->value[1], self->uuid->value[0]);
37+
}
38+
39+
mp_int_t common_hal_bleio_descriptor_get_handle(bleio_descriptor_obj_t *self) {
40+
return self->handle;
41+
}
42+
43+
mp_int_t common_hal_bleio_descriptor_get_uuid(bleio_descriptor_obj_t *self) {
44+
return self->uuid->value[0] | (self->uuid->value[1] << 8);
45+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 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+
#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_DESCRIPTOR_H
29+
#define MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_DESCRIPTOR_H
30+
31+
#include "py/obj.h"
32+
#include "common-hal/bleio/UUID.h"
33+
34+
typedef struct {
35+
mp_obj_base_t base;
36+
uint16_t handle;
37+
bleio_uuid_obj_t *uuid;
38+
} bleio_descriptor_obj_t;
39+
40+
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_DESCRIPTOR_H

ports/nrf/modules/ubluepy/modubluepy.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ STATIC const mp_rom_map_elem_t mp_module_ubluepy_globals_table[] = {
5252
{ MP_ROM_QSTR(MP_QSTR_Service), MP_ROM_PTR(&ubluepy_service_type) },
5353
{ MP_ROM_QSTR(MP_QSTR_Characteristic), MP_ROM_PTR(&ubluepy_characteristic_type) },
5454
{ MP_ROM_QSTR(MP_QSTR_constants), MP_ROM_PTR(&ubluepy_constants_type) },
55-
#if MICROPY_PY_UBLUEPY_DESCRIPTOR
56-
{ MP_ROM_QSTR(MP_QSTR_Descriptor), MP_ROM_PTR(&ubluepy_descriptor_type) },
57-
#endif
5855
};
5956

6057

ports/nrf/modules/ubluepy/modubluepy.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,6 @@ typedef struct _ubluepy_characteristic_obj_t {
136136
mp_obj_t value_data;
137137
} ubluepy_characteristic_obj_t;
138138

139-
typedef struct _ubluepy_descriptor_obj_t {
140-
mp_obj_base_t base;
141-
uint16_t handle;
142-
bleio_uuid_obj_t * p_uuid;
143-
} ubluepy_descriptor_obj_t;
144-
145139
typedef struct _ubluepy_delegate_obj_t {
146140
mp_obj_base_t base;
147141
} ubluepy_delegate_obj_t;

ports/nrf/modules/ubluepy/ubluepy_descriptor.c

Lines changed: 0 additions & 82 deletions
This file was deleted.

shared-bindings/bleio/Descriptor.c

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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

Comments
 (0)