Skip to content

Commit 345334a

Browse files
committed
bleio: Add a new Address class
Use the new in the Adapter singleton.
1 parent d5f942a commit 345334a

File tree

11 files changed

+260
-26
lines changed

11 files changed

+260
-26
lines changed

ports/nrf/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ SRC_BINDINGS_ENUMS = \
185185

186186
ifneq ($(SD), )
187187
SRC_BINDINGS_ENUMS += \
188+
bleio/Address.c \
188189
bleio/AddressType.c \
189190
bleio/UUIDType.c
190191
endif

ports/nrf/common-hal/bleio/Adapter.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@
2626
*/
2727

2828
#include <stdio.h>
29+
#include <string.h>
2930

3031
#include "ble_drv.h"
3132
#include "nrfx.h"
3233
#include "nrf_error.h"
33-
#include "py/misc.h"
34+
#include "shared-module/bleio/Address.h"
3435

3536
void common_hal_bleio_adapter_set_enabled(bool enabled) {
3637
if (enabled) {
@@ -49,12 +50,10 @@ bool common_hal_bleio_adapter_get_enabled(void) {
4950
return ble_drv_stack_enabled();
5051
}
5152

52-
void common_hal_bleio_adapter_get_address(vstr_t *vstr) {
53-
ble_drv_addr_t address;
54-
ble_drv_address_get(&address);
53+
void common_hal_bleio_adapter_get_address(bleio_address_obj_t *address) {
54+
ble_drv_addr_t drv_addr;
55+
ble_drv_address_get(&drv_addr);
5556

56-
vstr_printf(vstr, ""HEX2_FMT":"HEX2_FMT":"HEX2_FMT":" \
57-
HEX2_FMT":"HEX2_FMT":"HEX2_FMT"",
58-
address.addr[5], address.addr[4], address.addr[3],
59-
address.addr[2], address.addr[1], address.addr[0]);
57+
address->type = drv_addr.addr_type;
58+
memcpy(address->value, drv_addr.addr, BLEIO_ADDRESS_BYTES);
6059
}

ports/nrf/modules/ubluepy/modubluepy.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,8 @@ typedef enum {
9090
typedef enum {
9191
UBLUEPY_ADDR_TYPE_PUBLIC = 0,
9292
UBLUEPY_ADDR_TYPE_RANDOM_STATIC = 1,
93-
#if 0
9493
UBLUEPY_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE = 2,
9594
UBLUEPY_ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE = 3,
96-
#endif
9795
} ubluepy_addr_type_t;
9896

9997
typedef enum {

ports/nrf/modules/ubluepy/ubluepy_constants.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,6 @@ STATIC const mp_rom_map_elem_t ubluepy_constants_locals_dict_table[] = {
8282
{ MP_ROM_QSTR(MP_QSTR_EVT_GATTS_WRITE), MP_ROM_INT(80) },
8383
{ MP_ROM_QSTR(MP_QSTR_UUID_CCCD), MP_ROM_INT(0x2902) },
8484

85-
{ MP_ROM_QSTR(MP_QSTR_ADDR_TYPE_PUBLIC), MP_ROM_INT(UBLUEPY_ADDR_TYPE_PUBLIC) },
86-
{ MP_ROM_QSTR(MP_QSTR_ADDR_TYPE_RANDOM_STATIC), MP_ROM_INT(UBLUEPY_ADDR_TYPE_RANDOM_STATIC) },
87-
8885
{ MP_ROM_QSTR(MP_QSTR_ad_types), MP_ROM_PTR(&ubluepy_constants_ad_types_type) },
8986
};
9087

shared-bindings/bleio/Adapter.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626

2727
#include "py/objproperty.h"
28+
#include "shared-bindings/bleio/Address.h"
2829
#include "shared-bindings/bleio/Adapter.h"
2930

3031
//| .. currentmodule:: bleio
@@ -57,8 +58,6 @@
5758
//| MAC address of the BLE adapter. (read-only)
5859
//|
5960

60-
#define BLE_ADDRESS_LEN 17
61-
6261
STATIC mp_obj_t bleio_adapter_get_enabled(mp_obj_t self) {
6362
return mp_obj_new_bool(common_hal_bleio_adapter_get_enabled());
6463
}
@@ -81,16 +80,12 @@ const mp_obj_property_t bleio_adapter_enabled_obj = {
8180
};
8281

8382
STATIC mp_obj_t bleio_adapter_get_address(mp_obj_t self) {
84-
vstr_t vstr;
85-
vstr_init(&vstr, BLE_ADDRESS_LEN);
86-
87-
common_hal_bleio_adapter_get_address(&vstr);
88-
89-
const mp_obj_t mac_str = mp_obj_new_str(vstr.buf, vstr.len);
83+
mp_obj_t obj = bleio_address_type.make_new(&bleio_address_type, 1, 0, mp_const_none);
84+
bleio_address_obj_t *address = MP_OBJ_TO_PTR(obj);
9085

91-
vstr_clear(&vstr);
86+
common_hal_bleio_adapter_get_address(address);
9287

93-
return mac_str;
88+
return obj;
9489
}
9590
MP_DEFINE_CONST_FUN_OBJ_1(bleio_adapter_get_address_obj, bleio_adapter_get_address);
9691

shared-bindings/bleio/Adapter.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_ADAPTER_H
2828
#define MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_ADAPTER_H
2929

30-
#include "py/obj.h"
30+
#include "shared-module/bleio/Address.h"
3131

3232
const mp_obj_type_t bleio_adapter_type;
3333

3434
extern bool common_hal_bleio_adapter_get_enabled(void);
3535
extern void common_hal_bleio_adapter_set_enabled(bool enabled);
36-
extern void common_hal_bleio_adapter_get_address(vstr_t *address);
36+
extern void common_hal_bleio_adapter_get_address(bleio_address_obj_t *address);
3737

3838
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_ADAPTER_H

shared-bindings/bleio/Address.c

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 Artur Pacholec
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <string.h>
28+
#include <stdio.h>
29+
30+
#include "py/objproperty.h"
31+
#include "py/objstr.h"
32+
#include "py/runtime.h"
33+
#include "shared-bindings/bleio/Address.h"
34+
#include "shared-module/bleio/Address.h"
35+
36+
#define ADDRESS_LONG_LEN 17 // XX:XX:XX:XX:XX:XX
37+
#define ADDRESS_SHORT_LEN 12 // XXXXXXXXXXXX
38+
39+
//| .. currentmodule:: bleio
40+
//|
41+
//| :class:`Address` -- BLE address
42+
//| =========================================================
43+
//|
44+
//| Encapsulates the address of a BLE device.
45+
//|
46+
47+
//| .. class:: Address(address)
48+
//|
49+
//| Create a new Address object encapsulating the address value.
50+
//| The value itself can be one of:
51+
//|
52+
//| - a `str` value in the format of 'XXXXXXXXXXXX' or 'XX:XX:XX:XX:XX'
53+
//| - a `bytes` or `bytearray` containing 6 bytes
54+
//| - another Address object
55+
//|
56+
//| :param address: The address to encapsulate
57+
//|
58+
59+
//| .. attribute:: type
60+
//|
61+
//| The address type. One of:
62+
//|
63+
//| - `bleio.AddressType.PUBLIC`
64+
//| - `bleio.AddressType.RANDOM_STATIC`
65+
//| - `bleio.AddressType.RANDOM_PRIVATE_RESOLVABLE`
66+
//| - `bleio.AddressType.RANDOM_PRIVATE_NON_RESOLVABLE`
67+
//|
68+
STATIC mp_obj_t bleio_address_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) {
69+
mp_arg_check_num(n_args, n_kw, 1, 1, true);
70+
bleio_address_obj_t *self = m_new_obj(bleio_address_obj_t);
71+
self->base.type = &bleio_address_type;
72+
self->type = ADDRESS_PUBLIC;
73+
74+
mp_map_t kw_args;
75+
mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args);
76+
77+
enum { ARG_address };
78+
static const mp_arg_t allowed_args[] = {
79+
{ ARG_address, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
80+
};
81+
82+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
83+
mp_arg_parse_all(n_args, pos_args, &kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
84+
85+
const mp_obj_t address = args[ARG_address].u_obj;
86+
87+
if (MP_OBJ_IS_STR(address)) {
88+
GET_STR_DATA_LEN(address, str_data, str_len);
89+
const bool is_long = (str_len == ADDRESS_LONG_LEN);
90+
const bool is_short = (str_len == ADDRESS_SHORT_LEN);
91+
92+
if (is_long || is_short) {
93+
size_t i = str_len - 1;
94+
for (size_t b = 0; b < BLEIO_ADDRESS_BYTES; ++b) {
95+
self->value[b] = unichar_xdigit_value(str_data[i]) |
96+
unichar_xdigit_value(str_data[i - 1]) << 4;
97+
98+
i -= is_long ? 3 : 2;
99+
}
100+
} else {
101+
mp_raise_ValueError("Wrong address length");
102+
}
103+
} else if (MP_OBJ_IS_TYPE(address, &mp_type_bytearray) || MP_OBJ_IS_TYPE(address, &mp_type_bytes)) {
104+
mp_buffer_info_t buf_info;
105+
mp_get_buffer_raise(address, &buf_info, MP_BUFFER_READ);
106+
if (buf_info.len != BLEIO_ADDRESS_BYTES) {
107+
mp_raise_ValueError("Wrong number of bytes provided");
108+
}
109+
110+
for (size_t b = 0; b < BLEIO_ADDRESS_BYTES; ++b) {
111+
self->value[BLEIO_ADDRESS_BYTES - b - 1] = ((uint8_t*)buf_info.buf)[b];
112+
}
113+
} else if (MP_OBJ_IS_TYPE(address, &bleio_address_type)) {
114+
// deep copy
115+
bleio_address_obj_t *other = MP_OBJ_TO_PTR(address);
116+
self->type = other->type;
117+
memcpy(self->value, other->value, BLEIO_ADDRESS_BYTES);
118+
}
119+
120+
return MP_OBJ_FROM_PTR(self);
121+
}
122+
123+
STATIC void bleio_address_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
124+
bleio_address_obj_t *self = MP_OBJ_TO_PTR(self_in);
125+
126+
mp_printf(print, "Address('"HEX2_FMT":"HEX2_FMT":"HEX2_FMT":"HEX2_FMT":"HEX2_FMT":"HEX2_FMT"')",
127+
self->value[5], self->value[4], self->value[3],
128+
self->value[2], self->value[1], self->value[0]);
129+
}
130+
131+
STATIC mp_obj_t bleio_address_get_type(mp_obj_t self_in) {
132+
bleio_address_obj_t *self = MP_OBJ_TO_PTR(self_in);
133+
134+
if (self->type == ADDRESS_PUBLIC) {
135+
return (mp_obj_t)&bleio_addresstype_public_obj;
136+
} else if (self->type == ADDRESS_RANDOM_STATIC) {
137+
return (mp_obj_t)&bleio_addresstype_random_static_obj;
138+
} else if (self->type == ADDRESS_RANDOM_PRIVATE_RESOLVABLE) {
139+
return (mp_obj_t)&bleio_addresstype_random_private_resolvable_obj;
140+
} else if (self->type == ADDRESS_RANDOM_PRIVATE_NON_RESOLVABLE) {
141+
return (mp_obj_t)&bleio_addresstype_random_private_non_resolvable_obj;
142+
}
143+
144+
return mp_const_none;
145+
}
146+
MP_DEFINE_CONST_FUN_OBJ_1(bleio_address_get_type_obj, bleio_address_get_type);
147+
148+
const mp_obj_property_t bleio_address_type_obj = {
149+
.base.type = &mp_type_property,
150+
.proxy = {(mp_obj_t)&bleio_address_get_type_obj,
151+
(mp_obj_t)&mp_const_none_obj,
152+
(mp_obj_t)&mp_const_none_obj},
153+
};
154+
155+
STATIC const mp_rom_map_elem_t bleio_address_locals_dict_table[] = {
156+
{ MP_ROM_QSTR(MP_QSTR_type), MP_ROM_PTR(&bleio_address_type_obj) },
157+
};
158+
159+
STATIC MP_DEFINE_CONST_DICT(bleio_address_locals_dict, bleio_address_locals_dict_table);
160+
161+
const mp_obj_type_t bleio_address_type = {
162+
{ &mp_type_type },
163+
.name = MP_QSTR_Address,
164+
.print = bleio_address_print,
165+
.make_new = bleio_address_make_new,
166+
.locals_dict = (mp_obj_dict_t*)&bleio_address_locals_dict
167+
};

shared-bindings/bleio/Address.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 Artur Pacholec
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_ADDRESS_H
28+
#define MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_ADDRESS_H
29+
30+
#include "py/objtype.h"
31+
32+
extern const mp_obj_type_t bleio_address_type;
33+
34+
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_ADDRESS_H

shared-bindings/bleio/AddressType.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
//|
3434
//| .. class:: bleio.AddressType
3535
//|
36-
//| Enum-like class to define the type of a BLE address.
36+
//| Enum-like class to define the type of a BLE address, see also `bleio.Address`.
3737
//|
3838
//| .. data:: PUBLIC
3939
//|

shared-bindings/bleio/__init__.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include "py/obj.h"
2929
#include "shared-bindings/bleio/__init__.h"
30+
#include "shared-bindings/bleio/Address.h"
3031
#include "shared-bindings/bleio/AddressType.h"
3132
#include "shared-bindings/bleio/Descriptor.h"
3233
#include "shared-bindings/bleio/UUID.h"
@@ -46,6 +47,7 @@
4647
//| .. toctree::
4748
//| :maxdepth: 3
4849
//|
50+
//| Address
4951
//| AddressType
5052
//| Adapter
5153
//| Descriptor
@@ -61,6 +63,7 @@
6163

6264
STATIC const mp_rom_map_elem_t bleio_module_globals_table[] = {
6365
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_bleio) },
66+
{ MP_ROM_QSTR(MP_QSTR_Address), MP_ROM_PTR(&bleio_address_type) },
6467
{ MP_ROM_QSTR(MP_QSTR_AddressType), MP_ROM_PTR(&bleio_addresstype_type) },
6568
{ MP_ROM_QSTR(MP_QSTR_Descriptor), MP_ROM_PTR(&bleio_descriptor_type) },
6669
{ MP_ROM_QSTR(MP_QSTR_UUID), MP_ROM_PTR(&bleio_uuid_type) },

0 commit comments

Comments
 (0)