Skip to content

Commit d5f942a

Browse files
committed
bleio: Add a AddressType enum-like class
1 parent 20b8d51 commit d5f942a

File tree

4 files changed

+158
-5
lines changed

4 files changed

+158
-5
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/AddressType.c \
188189
bleio/UUIDType.c
189190
endif
190191

shared-bindings/bleio/AddressType.c

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* This file is part of the Micro Python 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 "shared-bindings/bleio/AddressType.h"
28+
29+
//| .. currentmodule:: bleio
30+
//|
31+
//| :class:`AddressType` -- defines the type of a BLE address
32+
//| =============================================================
33+
//|
34+
//| .. class:: bleio.AddressType
35+
//|
36+
//| Enum-like class to define the type of a BLE address.
37+
//|
38+
//| .. data:: PUBLIC
39+
//|
40+
//| The address is public
41+
//|
42+
//| .. data:: RANDOM_STATIC
43+
//|
44+
//| The address is random static
45+
//|
46+
//| .. data:: RANDOM_PRIVATE_RESOLVABLE
47+
//|
48+
//| The address is random private resolvable
49+
//|
50+
//| .. data:: RANDOM_PRIVATE_NON_RESOLVABLE
51+
//|
52+
//| The address is private non-resolvable
53+
//|
54+
const mp_obj_type_t bleio_addresstype_type;
55+
56+
const bleio_addresstype_obj_t bleio_addresstype_public_obj = {
57+
{ &bleio_addresstype_type },
58+
};
59+
60+
const bleio_addresstype_obj_t bleio_addresstype_random_static_obj = {
61+
{ &bleio_addresstype_type },
62+
};
63+
64+
const bleio_addresstype_obj_t bleio_addresstype_random_private_resolvable_obj = {
65+
{ &bleio_addresstype_type },
66+
};
67+
68+
const bleio_addresstype_obj_t bleio_addresstype_random_private_non_resolvable_obj = {
69+
{ &bleio_addresstype_type },
70+
};
71+
72+
STATIC const mp_rom_map_elem_t bleio_addresstype_locals_dict_table[] = {
73+
{ MP_ROM_QSTR(MP_QSTR_PUBLIC), MP_ROM_PTR(&bleio_addresstype_public_obj) },
74+
{ MP_ROM_QSTR(MP_QSTR_RANDOM_STATIC), MP_ROM_PTR(&bleio_addresstype_random_static_obj) },
75+
{ MP_ROM_QSTR(MP_QSTR_RANDOM_PRIVATE_RESOLVABLE), MP_ROM_PTR(&bleio_addresstype_random_private_resolvable_obj) },
76+
{ MP_ROM_QSTR(MP_QSTR_RANDOM_PRIVATE_NON_RESOLVABLE), MP_ROM_PTR(&bleio_addresstype_random_private_non_resolvable_obj) },
77+
};
78+
STATIC MP_DEFINE_CONST_DICT(bleio_addresstype_locals_dict, bleio_addresstype_locals_dict_table);
79+
80+
STATIC void bleio_addresstype_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
81+
qstr type = MP_QSTR_PUBLIC;
82+
83+
if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&bleio_addresstype_random_static_obj)) {
84+
type = MP_QSTR_RANDOM_STATIC;
85+
} else if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&bleio_addresstype_random_private_resolvable_obj)) {
86+
type = MP_QSTR_RANDOM_PRIVATE_RESOLVABLE;
87+
} else if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&bleio_addresstype_random_private_non_resolvable_obj)) {
88+
type = MP_QSTR_RANDOM_PRIVATE_NON_RESOLVABLE;
89+
}
90+
91+
mp_printf(print, "%q.%q.%q", MP_QSTR_bleio, MP_QSTR_AddressType, type);
92+
}
93+
94+
const mp_obj_type_t bleio_addresstype_type = {
95+
{ &mp_type_type },
96+
.name = MP_QSTR_AddressType,
97+
.print = bleio_addresstype_print,
98+
.locals_dict = (mp_obj_t)&bleio_addresstype_locals_dict,
99+
};

shared-bindings/bleio/AddressType.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* This file is part of the Micro Python 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_ADDRESSTYPE_H
28+
#define MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_ADDRESSTYPE_H
29+
30+
#include "py/obj.h"
31+
32+
typedef enum {
33+
ADDRESS_PUBLIC,
34+
ADDRESS_RANDOM_STATIC,
35+
ADDRESS_RANDOM_PRIVATE_RESOLVABLE,
36+
ADDRESS_RANDOM_PRIVATE_NON_RESOLVABLE
37+
} bleio_address_type_t;
38+
39+
extern const mp_obj_type_t bleio_addresstype_type;
40+
41+
typedef struct {
42+
mp_obj_base_t base;
43+
} bleio_addresstype_obj_t;
44+
45+
extern const bleio_addresstype_obj_t bleio_addresstype_public_obj;
46+
extern const bleio_addresstype_obj_t bleio_addresstype_random_static_obj;
47+
extern const bleio_addresstype_obj_t bleio_addresstype_random_private_resolvable_obj;
48+
extern const bleio_addresstype_obj_t bleio_addresstype_random_private_non_resolvable_obj;
49+
50+
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_ADDRESSTYPE_H

shared-bindings/bleio/__init__.c

Lines changed: 8 additions & 5 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/AddressType.h"
3031
#include "shared-bindings/bleio/Descriptor.h"
3132
#include "shared-bindings/bleio/UUID.h"
3233
#include "shared-bindings/bleio/UUIDType.h"
@@ -45,6 +46,7 @@
4546
//| .. toctree::
4647
//| :maxdepth: 3
4748
//|
49+
//| AddressType
4850
//| Adapter
4951
//| Descriptor
5052
//| UUID
@@ -58,15 +60,16 @@
5860
//|
5961

6062
STATIC const mp_rom_map_elem_t bleio_module_globals_table[] = {
61-
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_bleio) },
62-
{ MP_ROM_QSTR(MP_QSTR_Descriptor), MP_ROM_PTR(&bleio_descriptor_type) },
63-
{ MP_ROM_QSTR(MP_QSTR_UUID), MP_ROM_PTR(&bleio_uuid_type) },
63+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_bleio) },
64+
{ MP_ROM_QSTR(MP_QSTR_AddressType), MP_ROM_PTR(&bleio_addresstype_type) },
65+
{ MP_ROM_QSTR(MP_QSTR_Descriptor), MP_ROM_PTR(&bleio_descriptor_type) },
66+
{ MP_ROM_QSTR(MP_QSTR_UUID), MP_ROM_PTR(&bleio_uuid_type) },
6467

6568
// Properties
66-
{ MP_ROM_QSTR(MP_QSTR_adapter), MP_ROM_PTR(&common_hal_bleio_adapter_obj) },
69+
{ MP_ROM_QSTR(MP_QSTR_adapter), MP_ROM_PTR(&common_hal_bleio_adapter_obj) },
6770

6871
// Enum-like Classes.
69-
{ MP_ROM_QSTR(MP_QSTR_UUIDType), MP_ROM_PTR(&bleio_uuidtype_type) },
72+
{ MP_ROM_QSTR(MP_QSTR_UUIDType), MP_ROM_PTR(&bleio_uuidtype_type) },
7073
};
7174

7275
STATIC MP_DEFINE_CONST_DICT(bleio_module_globals, bleio_module_globals_table);

0 commit comments

Comments
 (0)