Skip to content

Commit 3bd65fb

Browse files
committed
nrf: Move the Peripheral class to bleio as Device
This was the last class from ubluepy and so that module is now gone. The Device class offers both Peripheral and Central functionality. See the inline docs for more info.
1 parent bda7342 commit 3bd65fb

File tree

20 files changed

+831
-948
lines changed

20 files changed

+831
-948
lines changed

ports/nrf/Makefile

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,6 @@ SRC_C += \
127127
peripherals/nrf/timers.c \
128128
supervisor/shared/memory.c
129129

130-
DRIVERS_SRC_C += $(addprefix modules/,\
131-
ubluepy/modubluepy.c \
132-
ubluepy/ubluepy_peripheral.c \
133-
)
134130

135131
SRC_COMMON_HAL += \
136132
analogio/AnalogIn.c \
@@ -163,6 +159,7 @@ SRC_COMMON_HAL += \
163159
bleio/Adapter.c \
164160
bleio/Characteristic.c \
165161
bleio/Descriptor.c \
162+
bleio/Device.c \
166163
bleio/Scanner.c \
167164
bleio/Service.c \
168165
bleio/UUID.c

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include "ble_drv.h"
2828
#include "shared-module/bleio/Characteristic.h"
29+
#include "shared-module/bleio/Device.h"
2930

3031
void data_callback(bleio_characteristic_obj_t *self, uint16_t length, uint8_t *data) {
3132
self->value_data = mp_obj_new_bytearray(length, data);
@@ -36,10 +37,9 @@ void common_hal_bleio_characteristic_read_value(bleio_characteristic_obj_t *self
3637
}
3738

3839
void common_hal_bleio_characteristic_write_value(bleio_characteristic_obj_t *self, mp_buffer_info_t *bufinfo) {
39-
ubluepy_peripheral_obj_t *peripheral = MP_OBJ_TO_PTR(self->service->periph);
40-
ubluepy_role_type_t role = peripheral->role;
40+
const bleio_device_obj_t *device = MP_OBJ_TO_PTR(self->service->device);
4141

42-
if (role == UBLUEPY_ROLE_PERIPHERAL) {
42+
if (device->is_peripheral) {
4343
// TODO: Add indications
4444
if (self->props.notify) {
4545
ble_drv_attr_s_notify(self, bufinfo);

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

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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 <stdio.h>
28+
29+
#include "ble_drv.h"
30+
#include "ble_gap.h"
31+
#include "ble_gatt.h"
32+
#include "ble_types.h"
33+
#include "py/runtime.h"
34+
#include "shared-bindings/bleio/Characteristic.h"
35+
#include "shared-bindings/bleio/Device.h"
36+
#include "shared-bindings/bleio/Service.h"
37+
#include "shared-bindings/bleio/UUID.h"
38+
39+
static volatile bool m_disc_evt_received;
40+
41+
STATIC void gap_event_handler(bleio_device_obj_t *device, uint16_t event_id, uint16_t conn_handle, uint16_t length, uint8_t * data) {
42+
if (event_id == BLE_GAP_EVT_CONNECTED) {
43+
device->conn_handle = conn_handle;
44+
} else if (event_id == BLE_GAP_EVT_DISCONNECTED) {
45+
device->conn_handle = BLE_CONN_HANDLE_INVALID;
46+
}
47+
}
48+
49+
STATIC void gatts_event_handler(bleio_device_obj_t *device, uint16_t event_id, uint16_t attr_handle, uint16_t length, uint8_t * data) {
50+
51+
}
52+
53+
STATIC void gattc_event_handler(bleio_device_obj_t *device, uint16_t event_id, uint16_t attr_handle, uint16_t length, uint8_t * data) {
54+
m_disc_evt_received = true;
55+
}
56+
57+
STATIC void disc_add_service(bleio_device_obj_t *device, ble_drv_service_data_t * service_data) {
58+
bleio_service_obj_t *service = m_new_obj(bleio_service_obj_t);
59+
service->base.type = &bleio_service_type;
60+
61+
bleio_uuid_obj_t *uuid = m_new_obj(bleio_uuid_obj_t);
62+
uuid->base.type = &bleio_uuid_type;
63+
uuid->type = (service_data->uuid_type == BLE_UUID_TYPE_BLE) ? UUID_TYPE_16BIT : UUID_TYPE_128BIT;
64+
uuid->value[0] = service_data->uuid & 0xFF;
65+
uuid->value[1] = service_data->uuid >> 8;
66+
67+
service->char_list = mp_obj_new_list(0, NULL);
68+
service->uuid = uuid;
69+
service->device = device;
70+
service->handle = service_data->start_handle;
71+
service->start_handle = service_data->start_handle;
72+
service->end_handle = service_data->end_handle;
73+
74+
mp_obj_list_append(device->service_list, service);
75+
}
76+
77+
STATIC void disc_add_char(bleio_service_obj_t *service, ble_drv_char_data_t *chara_data) {
78+
bleio_characteristic_obj_t *chara = m_new_obj(bleio_characteristic_obj_t);
79+
chara->base.type = &bleio_characteristic_type;
80+
81+
bleio_uuid_obj_t * p_uuid = m_new_obj(bleio_uuid_obj_t);
82+
p_uuid->base.type = &bleio_uuid_type;
83+
84+
chara->uuid = p_uuid;
85+
86+
p_uuid->type = chara_data->uuid_type;
87+
p_uuid->value[0] = chara_data->uuid & 0xFF;
88+
p_uuid->value[1] = chara_data->uuid >> 8;
89+
90+
// add characteristic specific data from discovery
91+
chara->props.broadcast = chara_data->props.broadcast;
92+
chara->props.indicate = chara_data->props.indicate;
93+
chara->props.notify = chara_data->props.notify;
94+
chara->props.read = chara_data->props.read;
95+
chara->props.write = chara_data->props.write;
96+
chara->props.write_wo_resp = chara_data->props.write_wo_resp;
97+
chara->handle = chara_data->value_handle;
98+
99+
chara->service_handle = service->handle;
100+
chara->service = service;
101+
102+
mp_obj_list_append(service->char_list, MP_OBJ_FROM_PTR(chara));
103+
}
104+
105+
106+
void common_hal_bleio_device_start_advertising(bleio_device_obj_t *device, bleio_advertisement_data_t *adv_data) {
107+
if (adv_data->connectable) {
108+
ble_drv_gap_event_handler_set(device, gap_event_handler);
109+
ble_drv_gatts_event_handler_set(device, gatts_event_handler);
110+
}
111+
112+
ble_drv_advertise_data(adv_data);
113+
}
114+
115+
void common_hal_bleio_device_stop_advertising(bleio_device_obj_t *device) {
116+
(void)device;
117+
118+
ble_drv_advertise_stop();
119+
}
120+
121+
void common_hal_bleio_device_connect(bleio_device_obj_t *device) {
122+
ble_drv_gap_event_handler_set(device, gap_event_handler);
123+
124+
ble_drv_connect(device);
125+
126+
while (device->conn_handle == BLE_CONN_HANDLE_INVALID) {
127+
run_background_tasks();
128+
// __asm volatile ("wfi");
129+
}
130+
131+
ble_drv_gattc_event_handler_set(device, gattc_event_handler);
132+
133+
// TODO: read name
134+
135+
// find services
136+
bool found_service = ble_drv_discover_services(device, BLE_GATT_HANDLE_START, disc_add_service);
137+
while (found_service) {
138+
const mp_obj_list_t *service_list = MP_OBJ_TO_PTR(device->service_list);
139+
const bleio_service_obj_t *service = service_list->items[service_list->len - 1];
140+
141+
found_service = ble_drv_discover_services(device, service->end_handle + 1, disc_add_service);
142+
}
143+
144+
// find characteristics in each service
145+
const mp_obj_list_t *service_list = MP_OBJ_TO_PTR(device->service_list);
146+
for (size_t i = 0; i < service_list->len; ++i) {
147+
bleio_service_obj_t *service = service_list->items[i];
148+
149+
bool found_char = ble_drv_discover_characteristic(device, service, service->start_handle, disc_add_char);
150+
while (found_char) {
151+
const mp_obj_list_t *char_list = MP_OBJ_TO_PTR(service->char_list);
152+
const bleio_characteristic_obj_t *characteristic = char_list->items[char_list->len - 1];
153+
154+
const uint16_t next_handle = characteristic->handle + 1;
155+
if (next_handle >= service->end_handle) {
156+
break;
157+
}
158+
159+
found_char = ble_drv_discover_characteristic(device, service, next_handle, disc_add_char);
160+
}
161+
}
162+
}
163+
164+
void common_hal_bleio_device_disconnect(bleio_device_obj_t *device) {
165+
ble_drv_disconnect(device);
166+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@
2929

3030
#include "ble_drv.h"
3131
#include "py/mphal.h"
32-
#include "shared-bindings/bleio/Scanner.h"
3332
#include "shared-bindings/bleio/ScanEntry.h"
33+
#include "shared-bindings/bleio/Scanner.h"
34+
#include "shared-module/bleio/ScanEntry.h"
3435

3536
STATIC void adv_event_handler(bleio_scanner_obj_t *self, ble_drv_adv_data_t *data) {
3637
// TODO: Don't add new entry for each item, group by address and update

0 commit comments

Comments
 (0)