Skip to content

Commit bda7342

Browse files
committed
nrf: Move the Service class from ubluepy to the shared bleio module
1 parent cc78249 commit bda7342

File tree

15 files changed

+346
-255
lines changed

15 files changed

+346
-255
lines changed

ports/nrf/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ SRC_C += \
130130
DRIVERS_SRC_C += $(addprefix modules/,\
131131
ubluepy/modubluepy.c \
132132
ubluepy/ubluepy_peripheral.c \
133-
ubluepy/ubluepy_service.c \
134133
)
135134

136135
SRC_COMMON_HAL += \
@@ -165,6 +164,7 @@ SRC_COMMON_HAL += \
165164
bleio/Characteristic.c \
166165
bleio/Descriptor.c \
167166
bleio/Scanner.c \
167+
bleio/Service.c \
168168
bleio/UUID.c
169169
endif
170170

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ void common_hal_bleio_characteristic_read_value(bleio_characteristic_obj_t *self
3636
}
3737

3838
void common_hal_bleio_characteristic_write_value(bleio_characteristic_obj_t *self, mp_buffer_info_t *bufinfo) {
39-
ubluepy_service_obj_t *service = MP_OBJ_TO_PTR(self->service);
40-
ubluepy_role_type_t role = service->p_periph->role;
39+
ubluepy_peripheral_obj_t *peripheral = MP_OBJ_TO_PTR(self->service->periph);
40+
ubluepy_role_type_t role = peripheral->role;
4141

4242
if (role == UBLUEPY_ROLE_PERIPHERAL) {
4343
// TODO: Add indications

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 "ble_drv.h"
28+
#include "shared-module/bleio/Service.h"
29+
30+
void common_hal_bleio_service_construct(bleio_service_obj_t *self) {
31+
ble_drv_service_add(self);
32+
}
33+
34+
void common_hal_bleio_service_add_characteristic(bleio_service_obj_t *self, bleio_characteristic_obj_t *characteristic) {
35+
if (ble_drv_characteristic_add(characteristic)) {
36+
characteristic->service = self;
37+
}
38+
}

ports/nrf/drivers/bluetooth/ble_drv.c

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -267,26 +267,29 @@ bool ble_drv_uuid_add_vs(uint8_t * p_uuid, uint8_t * idx) {
267267
return true;
268268
}
269269

270-
bool ble_drv_service_add(ubluepy_service_obj_t * p_service_obj) {
270+
void ble_drv_service_add(bleio_service_obj_t *service) {
271271
SD_TEST_OR_ENABLE();
272272

273-
ble_uuid_t uuid;
274-
uuid.type = BLE_UUID_TYPE_BLE;
275-
uuid.uuid = p_service_obj->p_uuid->value[0] | (p_service_obj->p_uuid->value[1] << 8);
273+
ble_uuid_t uuid = {
274+
.type = BLE_UUID_TYPE_BLE,
275+
.uuid = service->uuid->value[0] | (service->uuid->value[1] << 8)
276+
};
276277

277-
if (p_service_obj->p_uuid->type == UUID_TYPE_128BIT) {
278-
uuid.type = p_service_obj->p_uuid->uuid_vs_idx;
278+
if (service->uuid->type == UUID_TYPE_128BIT) {
279+
uuid.type = service->uuid->uuid_vs_idx;
279280
}
280281

281-
uint32_t err_code = sd_ble_gatts_service_add(p_service_obj->type,
282-
&uuid,
283-
&p_service_obj->handle);
284-
if (err_code != 0) {
285-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
286-
translate("Can not add Service. status: 0x%08lX"), err_code));
282+
uint8_t service_type = BLE_GATTS_SRVC_TYPE_PRIMARY;
283+
if (service->is_secondary) {
284+
service_type = BLE_GATTS_SRVC_TYPE_SECONDARY;
287285
}
288286

289-
return true;
287+
if (sd_ble_gatts_service_add(service_type,
288+
&uuid,
289+
&service->handle) != 0) {
290+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
291+
translate("Can not add Service.")));
292+
}
290293
}
291294

292295
bool ble_drv_characteristic_add(bleio_characteristic_obj_t *characteristic) {
@@ -320,7 +323,7 @@ bool ble_drv_characteristic_add(bleio_characteristic_obj_t *characteristic) {
320323
char_md.p_cccd_md = NULL;
321324
}
322325

323-
uuid.type = BLE_UUID_TYPE_BLE;
326+
uuid.type = BLE_UUID_TYPE_BLE;
324327
if (characteristic->uuid->type == UUID_TYPE_128BIT)
325328
uuid.type = characteristic->uuid->uuid_vs_idx;
326329

@@ -416,12 +419,12 @@ bool ble_drv_advertise_data(ubluepy_advertise_data_t * p_adv_params) {
416419
bool type_128bit_present = false;
417420

418421
for (uint8_t i = 0; i < p_adv_params->num_of_services; i++) {
419-
ubluepy_service_obj_t * p_service = (ubluepy_service_obj_t *)p_adv_params->p_services[i];
420-
if (p_service->p_uuid->type == UUID_TYPE_16BIT) {
422+
bleio_service_obj_t * p_service = (bleio_service_obj_t *)p_adv_params->p_services[i];
423+
if (p_service->uuid->type == UUID_TYPE_16BIT) {
421424
type_16bit_present = true;
422425
}
423426

424-
if (p_service->p_uuid->type == UUID_TYPE_128BIT) {
427+
if (p_service->uuid->type == UUID_TYPE_128BIT) {
425428
type_128bit_present = true;
426429
}
427430
}
@@ -439,12 +442,12 @@ bool ble_drv_advertise_data(ubluepy_advertise_data_t * p_adv_params) {
439442
uint8_t encoded_size = 0;
440443

441444
for (uint8_t i = 0; i < p_adv_params->num_of_services; i++) {
442-
ubluepy_service_obj_t * p_service = (ubluepy_service_obj_t *)p_adv_params->p_services[i];
445+
bleio_service_obj_t * p_service = (bleio_service_obj_t *)p_adv_params->p_services[i];
443446

444447
ble_uuid_t uuid;
445-
uuid.type = p_service->p_uuid->type;
446-
uuid.uuid = p_service->p_uuid->value[0];
447-
uuid.uuid += p_service->p_uuid->value[1] << 8;
448+
uuid.type = p_service->uuid->type;
449+
uuid.uuid = p_service->uuid->value[0];
450+
uuid.uuid += p_service->uuid->value[1] << 8;
448451
// calculate total size of uuids
449452
if (sd_ble_uuid_encode(&uuid, &encoded_size, NULL) != 0) {
450453
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
@@ -488,12 +491,12 @@ bool ble_drv_advertise_data(ubluepy_advertise_data_t * p_adv_params) {
488491
uint8_t encoded_size = 0;
489492

490493
for (uint8_t i = 0; i < p_adv_params->num_of_services; i++) {
491-
ubluepy_service_obj_t * p_service = (ubluepy_service_obj_t *)p_adv_params->p_services[i];
494+
bleio_service_obj_t * p_service = (bleio_service_obj_t *)p_adv_params->p_services[i];
492495

493496
ble_uuid_t uuid;
494-
uuid.type = p_service->p_uuid->uuid_vs_idx;
495-
uuid.uuid = p_service->p_uuid->value[0];
496-
uuid.uuid += p_service->p_uuid->value[1] << 8;
497+
uuid.type = p_service->uuid->uuid_vs_idx;
498+
uuid.uuid = p_service->uuid->value[0];
499+
uuid.uuid += p_service->uuid->value[1] << 8;
497500

498501
// calculate total size of uuids
499502
if (sd_ble_uuid_encode(&uuid, &encoded_size, NULL) != 0) {
@@ -640,8 +643,8 @@ void ble_drv_attr_s_read(uint16_t conn_handle, uint16_t handle, uint16_t len, ui
640643
}
641644

642645
void ble_drv_attr_s_write(bleio_characteristic_obj_t *characteristic, mp_buffer_info_t *bufinfo) {
643-
ubluepy_service_obj_t *service = MP_OBJ_TO_PTR(characteristic->service);
644-
uint16_t conn_handle = service->p_periph->conn_handle;
646+
ubluepy_peripheral_obj_t *peripheral = MP_OBJ_TO_PTR(characteristic->service->periph);
647+
uint16_t conn_handle = peripheral->conn_handle;
645648
ble_gatts_value_t gatts_value;
646649

647650
memset(&gatts_value, 0, sizeof(gatts_value));
@@ -659,8 +662,8 @@ void ble_drv_attr_s_write(bleio_characteristic_obj_t *characteristic, mp_buffer_
659662
}
660663

661664
void ble_drv_attr_s_notify(bleio_characteristic_obj_t *characteristic, mp_buffer_info_t *bufinfo) {
662-
ubluepy_service_obj_t *service = MP_OBJ_TO_PTR(characteristic->service);
663-
uint16_t conn_handle = service->p_periph->conn_handle;
665+
ubluepy_peripheral_obj_t *peripheral = MP_OBJ_TO_PTR(characteristic->service->periph);
666+
uint16_t conn_handle = peripheral->conn_handle;
664667
ble_gatts_hvx_params_t hvx_params;
665668
uint16_t hvx_len = bufinfo->len;
666669

@@ -706,11 +709,13 @@ void ble_drv_adv_report_handler_set(bleio_scanner_obj_t *self, ble_drv_adv_evt_c
706709

707710

708711
void ble_drv_attr_c_read(bleio_characteristic_obj_t *characteristic, ble_drv_gattc_char_data_callback_t cb) {
709-
ubluepy_service_obj_t *service = MP_OBJ_TO_PTR(characteristic->service);
712+
bleio_service_obj_t *service = characteristic->service;
713+
ubluepy_peripheral_obj_t *peripheral = MP_OBJ_TO_PTR(service->periph);
714+
710715
mp_gattc_char_data_observer = characteristic;
711716
gattc_char_data_handle = cb;
712717

713-
const uint32_t err_code = sd_ble_gattc_read(service->p_periph->conn_handle,
718+
const uint32_t err_code = sd_ble_gattc_read(peripheral->conn_handle,
714719
characteristic->handle,
715720
0);
716721
if (err_code != 0) {
@@ -724,8 +729,8 @@ void ble_drv_attr_c_read(bleio_characteristic_obj_t *characteristic, ble_drv_gat
724729
}
725730

726731
void ble_drv_attr_c_write(bleio_characteristic_obj_t *characteristic, mp_buffer_info_t *bufinfo) {
727-
ubluepy_service_obj_t *service = MP_OBJ_TO_PTR(characteristic->service);
728-
uint16_t conn_handle = service->p_periph->conn_handle;
732+
ubluepy_peripheral_obj_t *peripheral = MP_OBJ_TO_PTR(characteristic->service->periph);
733+
uint16_t conn_handle = peripheral->conn_handle;
729734

730735
ble_gattc_write_params_t write_params;
731736
write_params.write_op = BLE_GATT_OP_WRITE_REQ;

ports/nrf/drivers/bluetooth/ble_drv.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
#include "shared-module/bleio/Characteristic.h"
3636
#include "shared-module/bleio/Scanner.h"
37+
#include "shared-module/bleio/Service.h"
3738

3839
#include "modubluepy.h"
3940

@@ -92,7 +93,7 @@ void ble_drv_address_get(ble_drv_addr_t * p_addr);
9293

9394
bool ble_drv_uuid_add_vs(uint8_t * p_uuid, uint8_t * idx);
9495

95-
bool ble_drv_service_add(ubluepy_service_obj_t * p_service_obj);
96+
void ble_drv_service_add(bleio_service_obj_t *service);
9697

9798
bool ble_drv_characteristic_add(bleio_characteristic_obj_t *characteristic);
9899

ports/nrf/modules/ubluepy/modubluepy.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ STATIC const mp_rom_map_elem_t mp_module_ubluepy_globals_table[] = {
3636
#if MICROPY_PY_UBLUEPY_PERIPHERAL
3737
{ MP_ROM_QSTR(MP_QSTR_Peripheral), MP_ROM_PTR(&ubluepy_peripheral_type) },
3838
#endif
39-
{ MP_ROM_QSTR(MP_QSTR_Service), MP_ROM_PTR(&ubluepy_service_type) },
4039
};
4140

4241
STATIC MP_DEFINE_CONST_DICT(mp_module_ubluepy_globals, mp_module_ubluepy_globals_table);

ports/nrf/modules/ubluepy/modubluepy.h

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,8 @@ p.advertise(device_name="micr", services=[s])
7474
#include "common-hal/bleio/UUID.h"
7575
#include "py/obj.h"
7676

77-
extern const mp_obj_type_t ubluepy_service_type;
7877
extern const mp_obj_type_t ubluepy_peripheral_type;
7978

80-
typedef enum {
81-
UBLUEPY_SERVICE_PRIMARY = 1,
82-
UBLUEPY_SERVICE_SECONDARY = 2
83-
} ubluepy_service_type_t;
84-
8579
typedef enum {
8680
UBLUEPY_ADDR_TYPE_PUBLIC = 0,
8781
UBLUEPY_ADDR_TYPE_RANDOM_STATIC = 1,
@@ -104,17 +98,6 @@ typedef struct _ubluepy_peripheral_obj_t {
10498
mp_obj_t service_list;
10599
} ubluepy_peripheral_obj_t;
106100

107-
typedef struct _ubluepy_service_obj_t {
108-
mp_obj_base_t base;
109-
uint16_t handle;
110-
uint8_t type;
111-
bleio_uuid_obj_t * p_uuid;
112-
ubluepy_peripheral_obj_t * p_periph;
113-
mp_obj_t char_list;
114-
uint16_t start_handle;
115-
uint16_t end_handle;
116-
} ubluepy_service_obj_t;
117-
118101
typedef struct _ubluepy_advertise_data_t {
119102
uint8_t * p_device_name;
120103
uint8_t device_name_len;

ports/nrf/modules/ubluepy/ubluepy_peripheral.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "ble_drv.h"
3636
#include "common-hal/bleio/UUID.h"
3737
#include "shared-bindings/bleio/Characteristic.h"
38+
#include "shared-bindings/bleio/Service.h"
3839
#include "shared-bindings/bleio/UUID.h"
3940

4041
STATIC void ubluepy_peripheral_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) {
@@ -271,9 +272,9 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(ubluepy_peripheral_disconnect_obj, peripheral_d
271272
///
272273
STATIC mp_obj_t peripheral_add_service(mp_obj_t self_in, mp_obj_t service) {
273274
ubluepy_peripheral_obj_t * self = MP_OBJ_TO_PTR(self_in);
274-
ubluepy_service_obj_t * p_service = MP_OBJ_TO_PTR(service);
275+
bleio_service_obj_t * p_service = MP_OBJ_TO_PTR(service);
275276

276-
p_service->p_periph = self;
277+
p_service->periph = self_in;
277278

278279
mp_obj_list_append(self->service_list, service);
279280

@@ -294,13 +295,13 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(ubluepy_peripheral_get_services_obj, peripheral
294295
#if MICROPY_PY_UBLUEPY_CENTRAL
295296

296297
void static disc_add_service(mp_obj_t self, ble_drv_service_data_t * p_service_data) {
297-
ubluepy_service_obj_t * p_service = m_new_obj(ubluepy_service_obj_t);
298-
p_service->base.type = &ubluepy_service_type;
298+
bleio_service_obj_t * p_service = m_new_obj(bleio_service_obj_t);
299+
p_service->base.type = &bleio_service_type;
299300

300301
bleio_uuid_obj_t * p_uuid = m_new_obj(bleio_uuid_obj_t);
301302
p_uuid->base.type = &bleio_uuid_type;
302303

303-
p_service->p_uuid = p_uuid;
304+
p_service->uuid = p_uuid;
304305

305306
p_uuid->type = p_service_data->uuid_type;
306307
p_uuid->value[0] = p_service_data->uuid & 0xFF;
@@ -316,7 +317,7 @@ void static disc_add_service(mp_obj_t self, ble_drv_service_data_t * p_service_d
316317
}
317318

318319
void static disc_add_char(mp_obj_t service_in, ble_drv_char_data_t * p_desc_data) {
319-
ubluepy_service_obj_t * p_service = MP_OBJ_TO_PTR(service_in);
320+
bleio_service_obj_t * p_service = MP_OBJ_TO_PTR(service_in);
320321
bleio_characteristic_obj_t * p_char = m_new_obj(bleio_characteristic_obj_t);
321322
p_char->base.type = &bleio_characteristic_type;
322323

@@ -410,7 +411,7 @@ STATIC mp_obj_t peripheral_connect(mp_uint_t n_args, const mp_obj_t *pos_args, m
410411
mp_uint_t num_services;
411412
mp_obj_get_array(self->service_list, &num_services, &services);
412413

413-
ubluepy_service_obj_t * p_service = (ubluepy_service_obj_t *)services[num_services - 1];
414+
bleio_service_obj_t * p_service = (bleio_service_obj_t *)services[num_services - 1];
414415

415416
service_disc_retval = ble_drv_discover_services(self,
416417
self->conn_handle,
@@ -424,7 +425,7 @@ STATIC mp_obj_t peripheral_connect(mp_uint_t n_args, const mp_obj_t *pos_args, m
424425
mp_obj_get_array(self->service_list, &num_services, &services);
425426

426427
for (uint16_t s = 0; s < num_services; s++) {
427-
ubluepy_service_obj_t * p_service = (ubluepy_service_obj_t *)services[s];
428+
bleio_service_obj_t * p_service = (bleio_service_obj_t *)services[s];
428429
bool char_disc_retval = ble_drv_discover_characteristic(p_service,
429430
self->conn_handle,
430431
p_service->start_handle,

0 commit comments

Comments
 (0)