summaryrefslogtreecommitdiff
path: root/shared-bindings/bleio/Peripheral.c
blob: 3a78802ac7a65df6d3b7680b1ceecef0284136b7 (plain)
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
 * This file is part of the MicroPython project, http://micropython.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2019 Dan Halbert for Adafruit Industries
 * Copyright (c) 2018 Artur Pacholec
 * Copyright (c) 2016 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 <stdio.h>

#include "ble_drv.h"
#include "py/objarray.h"
#include "py/objproperty.h"
#include "py/objstr.h"
#include "py/runtime.h"

#include "shared-bindings/bleio/Adapter.h"
#include "shared-bindings/bleio/Characteristic.h"
#include "shared-bindings/bleio/Peripheral.h"
#include "shared-bindings/bleio/Service.h"
#include "shared-bindings/bleio/UUID.h"
#include "shared-module/bleio/ScanEntry.h"

#include "common-hal/bleio/Peripheral.h"

#define ADV_INTERVAL_MIN (0.0020f)
#define ADV_INTERVAL_MIN_STRING "0.0020"
#define ADV_INTERVAL_MAX (10.24f)
#define ADV_INTERVAL_MAX_STRING "10.24"
// 20ms is recommended by Apple
#define ADV_INTERVAL_DEFAULT (0.1f)

//| .. currentmodule:: bleio
//|
//| :class:`Peripheral` -- A BLE peripheral device
//| =========================================================
//|
//| Implement a BLE peripheral which runs locally.
//| Set up using the supplied services, and then allow advertising to be started and stopped.
//|
//| Usage::
//|
//|    import bleio
//|    from adafruit_ble.advertising import ServerAdvertisement
//|
//|    # Create a peripheral and start it up.
//|    peripheral = bleio.Peripheral()
//|
//|    # Create a Service and add it to this Peripheral.
//|    service = peripheral.addService(bleio.UUID(0x180f))
//|
//|    # Create a Characteristic and add it to the Service.
//|    characteristic = service.addCharacteristic(
//|        bleio.UUID(0x2919), properties=Characteristic.READ | Characteristic.NOTIFY)
//|
//|    adv = ServerAdvertisement(peripheral)
//|    peripheral.start_advertising(adv.advertising_data_bytes, adv.scan_response_bytes)
//|
//|    while not peripheral.connected:
//|        # Wait for connection.
//|        pass
//|
//| .. class:: Peripheral(name=None)
//|
//|   Create a new Peripheral object.
//|
//|   :param str name: The name used when advertising this peripheral. If name is None,
//|     bleio.adapter.default_name will be used.
//|
STATIC mp_obj_t bleio_peripheral_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
    enum { ARG_name };
    static const mp_arg_t allowed_args[] = {
        { MP_QSTR_name, MP_ARG_OBJ, {.u_obj = mp_const_none} },
    };

    mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
    mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);

    bleio_peripheral_obj_t *self = m_new_obj(bleio_peripheral_obj_t);
    self->base.type = &bleio_peripheral_type;

    mp_obj_t name = args[ARG_name].u_obj;
    if (name == mp_const_none) {
        name = common_hal_bleio_adapter_get_default_name();
    } else if (!MP_OBJ_IS_STR(name)) {
        mp_raise_ValueError(translate("name must be a string"));
    }

    common_hal_bleio_peripheral_construct(self, name);

    return MP_OBJ_FROM_PTR(self);
}

//|   .. method:: add_service(uuid, *, secondary=False)
//|
//|   Create a new `Service` object, identitied by the specified UUID, and add it to this ``Peripheral``.
//|
//|   To mark the service as secondary, pass `True` as :py:data:`secondary`.
//|
//|   :param bleio.UUID uuid: The uuid of the service
//|   :param bool secondary: If the service is a secondary one
//
//|   :return: the new `Service`
//|
STATIC mp_obj_t bleio_peripheral_add_service(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
    bleio_peripheral_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);

    enum { ARG_uuid, ARG_secondary };
    static const mp_arg_t allowed_args[] = {
        { MP_QSTR_uuid, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = mp_const_none} },
        { MP_QSTR_secondary, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
    };

    mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
    mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);

    const mp_obj_t uuid_obj = args[ARG_uuid].u_obj;

    if (!MP_OBJ_IS_TYPE(uuid_obj, &bleio_uuid_type)) {
        mp_raise_ValueError(translate("Expected a UUID"));
    }

    const bool is_secondary = args[ARG_secondary].u_bool;
    bleio_uuid_obj_t *uuid = MP_OBJ_TO_PTR(uuid_obj);

    bleio_service_obj_t *service = m_new_obj(bleio_service_obj_t);
    service->base.type = &bleio_service_type;

    common_hal_bleio_service_construct(service, uuid, is_secondary);

    common_hal_bleio_peripheral_add_service(self, service);

    return MP_OBJ_FROM_PTR(service);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bleio_peripheral_add_service_obj, 2, bleio_peripheral_add_service);


//|   .. attribute:: connected (read-only)
//|
//|     True if connected to a BLE Central device.
//|
STATIC mp_obj_t bleio_peripheral_get_connected(mp_obj_t self_in) {
    bleio_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in);

    return mp_obj_new_bool(common_hal_bleio_peripheral_get_connected(self));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_peripheral_get_connected_obj, bleio_peripheral_get_connected);

const mp_obj_property_t bleio_peripheral_connected_obj = {
    .base.type = &mp_type_property,
    .proxy = { (mp_obj_t)&bleio_peripheral_get_connected_obj,
               (mp_obj_t)&mp_const_none_obj,
               (mp_obj_t)&mp_const_none_obj },
};

//|   .. attribute:: services
//|
//|     A `tuple` of `bleio.Service` that are offered by this peripheral. (read-only)
//|
STATIC mp_obj_t bleio_peripheral_get_services(mp_obj_t self_in) {
    bleio_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in);
    // Return list as a tuple so user won't be able to change it.
    mp_obj_list_t *services_list = common_hal_bleio_peripheral_get_services(self);
    return mp_obj_new_tuple(services_list->len, services_list->items);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_peripheral_get_services_obj, bleio_peripheral_get_services);

const mp_obj_property_t bleio_peripheral_services_obj = {
    .base.type = &mp_type_property,
    .proxy = { (mp_obj_t)&bleio_peripheral_get_services_obj,
               (mp_obj_t)&mp_const_none_obj,
               (mp_obj_t)&mp_const_none_obj },
};

//|   .. attribute:: name
//|
//|     The peripheral's name, included when advertising. (read-only)
//|
STATIC mp_obj_t bleio_peripheral_get_name(mp_obj_t self_in) {
    bleio_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in);

    return common_hal_bleio_peripheral_get_name(self);
}
MP_DEFINE_CONST_FUN_OBJ_1(bleio_peripheral_get_name_obj, bleio_peripheral_get_name);

const mp_obj_property_t bleio_peripheral_name_obj = {
    .base.type = &mp_type_property,
    .proxy = { (mp_obj_t)&bleio_peripheral_get_name_obj,
               (mp_obj_t)&mp_const_none_obj,
               (mp_obj_t)&mp_const_none_obj },
};

//|   .. method:: start_advertising(data, *, scan_response=None, connectable=True, interval=0.1)
//|
//|     Starts advertising the peripheral. The peripheral's name and
//|     services are included in the advertisement packets.
//|
//|     :param buf data: advertising data packet bytes
//|     :param buf scan_response: scan response data packet bytes. ``None`` if no scan response is needed.
//|     :param bool connectable:  If `True` then other devices are allowed to connect to this peripheral.
//|     :param float interval:  advertising interval, in seconds
//|
STATIC mp_obj_t bleio_peripheral_start_advertising(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
    bleio_peripheral_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);

    enum { ARG_data, ARG_scan_response, ARG_connectable, ARG_interval };
    static const mp_arg_t allowed_args[] = {
        { MP_QSTR_data, MP_ARG_REQUIRED | MP_ARG_OBJ },
        { MP_QSTR_scan_response, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
        { MP_QSTR_connectable, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} },
        { MP_QSTR_interval, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
    };

    mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
    mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);

    mp_buffer_info_t data_bufinfo;
    mp_get_buffer_raise(args[ARG_data].u_obj, &data_bufinfo, MP_BUFFER_READ);

    // Pass an empty buffer if scan_response not provided.
    mp_buffer_info_t scan_response_bufinfo = { 0 };
    if (args[ARG_scan_response].u_obj != mp_const_none) {
        mp_get_buffer_raise(args[ARG_scan_response].u_obj, &scan_response_bufinfo, MP_BUFFER_READ);
    }

    if (args[ARG_interval].u_obj == MP_OBJ_NULL) {
        args[ARG_interval].u_obj = mp_obj_new_float(ADV_INTERVAL_DEFAULT);
    }

    const mp_float_t interval = mp_obj_float_get(args[ARG_interval].u_obj);
    if (interval < ADV_INTERVAL_MIN || interval > ADV_INTERVAL_MAX) {
        mp_raise_ValueError_varg(translate("interval must be in range %s-%s"),
                                 ADV_INTERVAL_MIN_STRING, ADV_INTERVAL_MAX_STRING);
    }

    common_hal_bleio_peripheral_start_advertising(self, args[ARG_connectable].u_bool, interval,
                                                  &data_bufinfo, &scan_response_bufinfo);

    return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bleio_peripheral_start_advertising_obj, 2, bleio_peripheral_start_advertising);

//|   .. method:: stop_advertising()
//|
//|     Stop sending advertising packets.
STATIC mp_obj_t bleio_peripheral_stop_advertising(mp_obj_t self_in) {
    bleio_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in);

    common_hal_bleio_peripheral_stop_advertising(self);

    return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_peripheral_stop_advertising_obj, bleio_peripheral_stop_advertising);

//|   .. method:: disconnect()
//|
//|     Disconnects from the remote central.
//|     Normally the central initiates a disconnection. Use this only
//|     if necessary for your application.
//|
STATIC mp_obj_t bleio_peripheral_disconnect(mp_obj_t self_in) {
    bleio_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in);

    common_hal_bleio_peripheral_disconnect(self);

    return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_peripheral_disconnect_obj, bleio_peripheral_disconnect);

//|   .. method:: discover_remote_services(service_uuids_whitelist=None)
//|      Do BLE discovery for all services or for the given service UUIDS,
//|      to find their handles and characteristics, and return the discovered services.
//|      `Peripheral.connected` must be True.
//|
//|   :param iterable service_uuids_whitelist: an iterable of :py:class:~`UUID` objects for the services
//|      provided by the peripheral that you want to use.
//|      The peripheral may provide more services, but services not listed are ignored
//|      and will not be returned.
//|
//|      If service_uuids_whitelist is None, then all services will undergo discovery, which can be slow.
//|
//|      If the service UUID is 128-bit, or its characteristic UUID's are 128-bit, you
//|      you must have already created a :py:class:~`UUID` object for that UUID in order for the
//|      service or characteristic to be discovered. Creating the UUID causes the UUID to be registered
//|      for use. (This restriction may be lifted in the future.)
//|
//|      Thought it is unusual for a peripheral to act as a BLE client, it can do so, and
//|      needs to be able to do discovery on its peer (a central).
//|      Examples include a peripheral accessing a central that provides Current Time Service,
//|      Apple Notification Center Service, or Battery Service.
//|
//|    :return: A tuple of services provided by the remote central.
//|
STATIC mp_obj_t bleio_peripheral_discover_remote_services(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
    bleio_peripheral_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);

    enum { ARG_service_uuids_whitelist };
    static const mp_arg_t allowed_args[] = {
        { MP_QSTR_service_uuids_whitelist, MP_ARG_OBJ, {.u_obj = mp_const_none} },
    };

    mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
    mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);

    if (!common_hal_bleio_peripheral_get_connected(self)) {
        mp_raise_ValueError(translate("Not connected"));
    }

    return MP_OBJ_FROM_PTR(common_hal_bleio_peripheral_discover_remote_services(
                               MP_OBJ_FROM_PTR(self),
                               args[ARG_service_uuids_whitelist].u_obj));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bleio_peripheral_discover_remote_services_obj, 1, bleio_peripheral_discover_remote_services);

//|   .. method:: pair()
//|
//|     Request pairing with connected central.
STATIC mp_obj_t bleio_peripheral_pair(mp_obj_t self_in) {
    bleio_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in);

    common_hal_bleio_peripheral_pair(self);

    return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_peripheral_pair_obj, bleio_peripheral_pair);

STATIC const mp_rom_map_elem_t bleio_peripheral_locals_dict_table[] = {
    // Methods
    { MP_ROM_QSTR(MP_QSTR_add_service),              MP_ROM_PTR(&bleio_peripheral_add_service_obj) },
    { MP_ROM_QSTR(MP_QSTR_start_advertising),        MP_ROM_PTR(&bleio_peripheral_start_advertising_obj) },
    { MP_ROM_QSTR(MP_QSTR_stop_advertising),         MP_ROM_PTR(&bleio_peripheral_stop_advertising_obj) },
    { MP_ROM_QSTR(MP_QSTR_disconnect),               MP_ROM_PTR(&bleio_peripheral_disconnect_obj) },
    { MP_ROM_QSTR(MP_QSTR_discover_remote_services), MP_ROM_PTR(&bleio_peripheral_discover_remote_services_obj) },
    { MP_ROM_QSTR(MP_QSTR_pair),                     MP_ROM_PTR(&bleio_peripheral_pair_obj) },

    // Properties
    { MP_ROM_QSTR(MP_QSTR_connected),       MP_ROM_PTR(&bleio_peripheral_connected_obj) },
    { MP_ROM_QSTR(MP_QSTR_name),            MP_ROM_PTR(&bleio_peripheral_name_obj) },
    { MP_ROM_QSTR(MP_QSTR_services),        MP_ROM_PTR(&bleio_peripheral_services_obj) },
};

STATIC MP_DEFINE_CONST_DICT(bleio_peripheral_locals_dict, bleio_peripheral_locals_dict_table);

const mp_obj_type_t bleio_peripheral_type = {
    { &mp_type_type },
    .name = MP_QSTR_Peripheral,
    .make_new = bleio_peripheral_make_new,
    .locals_dict = (mp_obj_dict_t*)&bleio_peripheral_locals_dict
};