1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2018 Artur Pacholec
* Copyright (c) 2017 Glenn Ruben Bakke
*
* 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 <string.h>
#include "py/objarray.h"
#include "py/objproperty.h"
#include "py/objstr.h"
#include "py/objtuple.h"
#include "shared-bindings/bleio/Address.h"
#include "shared-bindings/bleio/ScanEntry.h"
#include "shared-bindings/bleio/UUID.h"
#include "shared-module/bleio/AdvertisementData.h"
#include "shared-module/bleio/ScanEntry.h"
// Work-in-progress: orphaned for now.
//| :orphan:
//|
//| .. currentmodule:: bleio
//|
//| :class:`ScanEntry` -- BLE scan response entry
//| =========================================================
//|
//| Encapsulates information about a device that was received as a
//| response to a BLE scan request.
//|
//| .. attribute:: address
//|
//| The address of the device. (read-only)
//| This attribute is of type `bleio.Address`.
//|
//| .. attribute:: manufacturer_specific_data
//|
//| The manufacturer-specific data present in the advertisement packet. (read-only)
//|
//| .. attribute:: name
//|
//| The name of the device. (read-only)
//| This attribute might be `None` if the data was missing from the advertisement packet.
//|
//| .. attribute:: raw_data
//|
//| All the advertisement data present in the packet. (read-only)
//|
//| .. attribute:: rssi
//|
//| The signal strength of the device at the time of the scan. (read-only)
//|
//| .. attribute:: service_uuids
//|
//| The address of the device. (read-only)
//| This attribute is a list of `bleio.UUID`.
//| This attribute might be empty or incomplete, depending on the advertisement packet.
//| Currently only 16-bit UUIDS are listed.
//|
//| .. attribute:: tx_power_level
//|
//| The transmit power level of the device. (read-only)
//| This attribute might be `None` if the data was missing from the advertisement packet.
//|
static uint8_t find_data_item(mp_obj_array_t *data_in, uint8_t type, uint8_t **data_out) {
uint16_t i = 0;
while (i < data_in->len) {
const uint8_t item_len = ((uint8_t*)data_in->items)[i];
const uint8_t item_type = ((uint8_t*)data_in->items)[i + 1];
if (item_type != type) {
i += (item_len + 1);
continue;
}
*data_out = &((uint8_t*)data_in->items)[i + 2];
return item_len;
}
return 0;
}
STATIC mp_obj_t scanentry_get_name(mp_obj_t self_in);
STATIC void bleio_scanentry_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
bleio_scanentry_obj_t *self = (bleio_scanentry_obj_t *)self_in;
mp_printf(print, "ScanEntry(address: %02x:%02x:%02x:%02x:%02x:%02x",
self->address.value[5], self->address.value[4], self->address.value[3],
self->address.value[1], self->address.value[1], self->address.value[0]);
const mp_obj_t name_obj = scanentry_get_name(self_in);
if (name_obj != mp_const_none) {
mp_obj_str_t *str = MP_OBJ_TO_PTR(name_obj);
mp_printf(print, " name: %s", str->data);
}
mp_print_str(print, ")");
}
STATIC mp_obj_t bleio_scanentry_get_address(mp_obj_t self_in) {
bleio_scanentry_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_obj_t obj = bleio_address_type.make_new(&bleio_address_type, 1, 0, (mp_obj_t)&mp_const_none_obj);
bleio_address_obj_t *address = MP_OBJ_TO_PTR(obj);
address->type = self->address.type;
memcpy(address->value, self->address.value, BLEIO_ADDRESS_BYTES);
return obj;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bluepy_scanentry_get_address_obj, bleio_scanentry_get_address);
const mp_obj_property_t bleio_scanentry_address_obj = {
.base.type = &mp_type_property,
.proxy = { (mp_obj_t)&bluepy_scanentry_get_address_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj },
};
STATIC mp_obj_t scanentry_get_manufacturer_specific_data(mp_obj_t self_in) {
bleio_scanentry_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_obj_array_t *data = MP_OBJ_TO_PTR(self->data);
uint8_t *manuf_data;
const uint8_t manuf_data_len = find_data_item(data, AdManufacturerSpecificData, &manuf_data);
if (manuf_data_len == 0) {
return mp_const_none;
}
return mp_obj_new_bytearray_by_ref(manuf_data_len, manuf_data);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(scanentry_get_manufacturer_specific_data_obj, scanentry_get_manufacturer_specific_data);
const mp_obj_property_t bleio_scanentry_manufacturer_specific_data_obj = {
.base.type = &mp_type_property,
.proxy = { (mp_obj_t)&scanentry_get_manufacturer_specific_data_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj },
};
STATIC mp_obj_t scanentry_get_name(mp_obj_t self_in) {
bleio_scanentry_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_obj_array_t *data = MP_OBJ_TO_PTR(self->data);
uint8_t *name;
// Try for Complete but settle for Shortened
uint8_t name_len = find_data_item(data, AdCompleteLocalName, &name);
if (name_len == 0) {
name_len = find_data_item(data, AdShortenedLocalName, &name);
}
if (name_len == 0) {
return mp_const_none;
}
return mp_obj_new_str((const char*)name, name_len - 1);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bluepy_scanentry_get_name_obj, scanentry_get_name);
const mp_obj_property_t bleio_scanentry_name_obj = {
.base.type = &mp_type_property,
.proxy = { (mp_obj_t)&bluepy_scanentry_get_name_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj },
};
STATIC mp_obj_t scanentry_get_raw_data(mp_obj_t self_in) {
bleio_scanentry_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_obj_t entries = mp_obj_new_list(0, NULL);
mp_obj_array_t *data = MP_OBJ_TO_PTR(self->data);
uint16_t i = 0;
while (i < data->len) {
mp_obj_tuple_t *entry = MP_OBJ_TO_PTR(mp_obj_new_tuple(2, NULL));
const uint8_t item_len = ((uint8_t*)data->items)[i];
const uint8_t item_type = ((uint8_t*)data->items)[i + 1];
entry->items[0] = MP_OBJ_NEW_SMALL_INT(item_type);
entry->items[1] = mp_obj_new_bytearray(item_len - 1, &((uint8_t*)data->items)[i + 2]);
mp_obj_list_append(entries, MP_OBJ_FROM_PTR(entry));
i += (item_len + 1);
}
return entries;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_scanentry_get_raw_data_obj, scanentry_get_raw_data);
const mp_obj_property_t bleio_scanentry_raw_data_obj = {
.base.type = &mp_type_property,
.proxy = { (mp_obj_t)&bleio_scanentry_get_raw_data_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj },
};
STATIC mp_obj_t scanentry_get_rssi(mp_obj_t self_in) {
bleio_scanentry_obj_t *self = MP_OBJ_TO_PTR(self_in);
return mp_obj_new_int(self->rssi);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bluepy_scanentry_get_rssi_obj, scanentry_get_rssi);
const mp_obj_property_t bleio_scanentry_rssi_obj = {
.base.type = &mp_type_property,
.proxy = { (mp_obj_t)&bluepy_scanentry_get_rssi_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj },
};
STATIC mp_obj_t scanentry_get_service_uuids(mp_obj_t self_in) {
bleio_scanentry_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_obj_array_t *data = MP_OBJ_TO_PTR(self->data);
uint8_t *uuids;
// Try for Complete but settle for Incomplete
uint8_t uuids_len = find_data_item(data, AdCompleteListOf16BitServiceClassUUIDs, &uuids);
if (uuids_len == 0) {
uuids_len = find_data_item(data, AdIncompleteListOf16BitServiceClassUUIDs, &uuids);
}
mp_obj_t entries = mp_obj_new_list(0, NULL);
for (size_t i = 0; i < uuids_len / sizeof(uint16_t); ++i) {
const mp_obj_t uuid_int = mp_obj_new_int(uuids[sizeof(uint16_t) * i] | (uuids[sizeof(uint16_t) * i + 1] << 8));
const mp_obj_t uuid_obj = bleio_uuid_type.make_new(&bleio_uuid_type, 1, &uuid_int, NULL);
mp_obj_list_append(entries, uuid_obj);
}
// TODO: 32-bit UUIDs
// TODO: 128-bit UUIDs
return entries;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(scanentry_get_service_uuids_obj, scanentry_get_service_uuids);
const mp_obj_property_t bleio_scanentry_service_uuids_obj = {
.base.type = &mp_type_property,
.proxy = { (mp_obj_t)&scanentry_get_service_uuids_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj },
};
STATIC mp_obj_t scanentry_get_tx_power_level(mp_obj_t self_in) {
bleio_scanentry_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_obj_array_t *data = MP_OBJ_TO_PTR(self->data);
uint8_t *tx_power;
const uint8_t tx_power_len = find_data_item(data, AdTxPowerLevel, &tx_power);
if (tx_power_len == 0) {
return mp_const_none;
}
return mp_obj_new_int((int8_t)(*tx_power));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(scanentry_get_tx_power_level_obj, scanentry_get_tx_power_level);
const mp_obj_property_t bleio_scanentry_tx_power_level_obj = {
.base.type = &mp_type_property,
.proxy = { (mp_obj_t)&scanentry_get_tx_power_level_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj },
};
STATIC const mp_rom_map_elem_t bleio_scanentry_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_address), MP_ROM_PTR(&bleio_scanentry_address_obj) },
{ MP_ROM_QSTR(MP_QSTR_manufacturer_specific_data), MP_ROM_PTR(&bleio_scanentry_manufacturer_specific_data_obj) },
{ MP_ROM_QSTR(MP_QSTR_name), MP_ROM_PTR(&bleio_scanentry_name_obj) },
{ MP_ROM_QSTR(MP_QSTR_raw_data), MP_ROM_PTR(&bleio_scanentry_raw_data_obj) },
{ MP_ROM_QSTR(MP_QSTR_rssi), MP_ROM_PTR(&bleio_scanentry_rssi_obj) },
{ MP_ROM_QSTR(MP_QSTR_service_uuids), MP_ROM_PTR(&bleio_scanentry_service_uuids_obj) },
{ MP_ROM_QSTR(MP_QSTR_tx_power_level), MP_ROM_PTR(&bleio_scanentry_tx_power_level_obj) },
};
STATIC MP_DEFINE_CONST_DICT(bleio_scanentry_locals_dict, bleio_scanentry_locals_dict_table);
const mp_obj_type_t bleio_scanentry_type = {
{ &mp_type_type },
.name = MP_QSTR_ScanEntry,
.print = bleio_scanentry_print,
.locals_dict = (mp_obj_dict_t*)&bleio_scanentry_locals_dict
};
|