Skip to content

Commit 1763ffe

Browse files
committed
More UUID work; use mp_raise for exceptions
1 parent c424ad8 commit 1763ffe

18 files changed

Lines changed: 186 additions & 161 deletions

File tree

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@
3333
#include "nrfx_power.h"
3434
#include "nrf_nvic.h"
3535
#include "nrf_sdm.h"
36-
#include "py/nlr.h"
36+
#include "py/runtime.h"
3737
#include "shared-bindings/bleio/Adapter.h"
3838

3939
STATIC void softdevice_assert_handler(uint32_t id, uint32_t pc, uint32_t info) {
40-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_AssertionError,
41-
translate("Soft device assert, id: 0x%08lX, pc: 0x%08lX"), id, pc));
40+
mp_raise_msg_varg(&mp_type_AssertionError,
41+
translate("Soft device assert, id: 0x%08lX, pc: 0x%08lX"), id, pc);
4242
}
4343

4444
STATIC uint32_t ble_stack_enable(void) {
@@ -121,8 +121,7 @@ void common_hal_bleio_adapter_set_enabled(bool enabled) {
121121
}
122122

123123
if (err_code != NRF_SUCCESS) {
124-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
125-
translate("Failed to change softdevice state, error: 0x%08lX"), err_code));
124+
mp_raise_OSError_msg(translate("Failed to change softdevice state"));
126125
}
127126
}
128127

@@ -131,8 +130,7 @@ bool common_hal_bleio_adapter_get_enabled(void) {
131130

132131
const uint32_t err_code = sd_softdevice_is_enabled(&is_enabled);
133132
if (err_code != NRF_SUCCESS) {
134-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
135-
translate("Failed to get softdevice state, error: 0x%08lX"), err_code));
133+
mp_raise_OSError_msg(translate("Failed to get softdevice state"));
136134
}
137135

138136
return is_enabled;
@@ -151,8 +149,7 @@ void common_hal_bleio_adapter_get_address(bleio_address_obj_t *address) {
151149
#endif
152150

153151
if (err_code != NRF_SUCCESS) {
154-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
155-
translate("Failed to get local address, error: 0x%08lX"), err_code));
152+
mp_raise_OSError_msg(translate("Failed to get local address"));
156153
}
157154

158155
address->type = local_address.addr_type;

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

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include "ble_drv.h"
3131
#include "ble_gatts.h"
3232
#include "nrf_soc.h"
33-
#include "py/nlr.h"
33+
#include "py/runtime.h"
3434
#include "shared-module/bleio/Characteristic.h"
3535

3636
static volatile bleio_characteristic_obj_t *m_read_characteristic;
@@ -48,8 +48,7 @@ STATIC void gatts_write(bleio_characteristic_obj_t *characteristic, mp_buffer_in
4848

4949
const uint32_t err_code = sd_ble_gatts_value_set(conn_handle, characteristic->handle, &gatts_value);
5050
if (err_code != NRF_SUCCESS) {
51-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
52-
translate("Failed to write gatts value, status: 0x%08lX"), err_code));
51+
mp_raise_OSError_msg(translate("Failed to write gatts value"));
5352
}
5453
}
5554

@@ -72,8 +71,7 @@ STATIC void gatts_notify(bleio_characteristic_obj_t *characteristic, mp_buffer_i
7271

7372
const uint32_t err_code = sd_ble_gatts_hvx(device->conn_handle, &hvx_params);
7473
if (err_code != NRF_SUCCESS) {
75-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
76-
translate("Failed to notify attribute value, status: 0x%08lX"), err_code));
74+
mp_raise_OSError_msg(translate("Failed to notify attribute value"));
7775
}
7876

7977
m_tx_in_progress += 1;
@@ -87,8 +85,7 @@ STATIC void gattc_read(bleio_characteristic_obj_t *characteristic) {
8785

8886
const uint32_t err_code = sd_ble_gattc_read(device->conn_handle, characteristic->handle, 0);
8987
if (err_code != NRF_SUCCESS) {
90-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
91-
translate("Failed to read attribute value, status: 0x%08lX"), err_code));
88+
mp_raise_OSError_msg(translate("Failed to read attribute value"));
9289
}
9390

9491
while (m_read_characteristic != NULL) {
@@ -115,15 +112,13 @@ STATIC void gattc_write(bleio_characteristic_obj_t *characteristic, mp_buffer_in
115112

116113
err_code = sd_mutex_acquire(m_write_mutex);
117114
if (err_code != NRF_SUCCESS) {
118-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
119-
translate("Failed to acquire mutex, status: 0x%08lX"), err_code));
115+
mp_raise_OSError_msg(translate("Failed to acquire mutex"));
120116
}
121117
}
122118

123119
err_code = sd_ble_gattc_write(device->conn_handle, &write_params);
124120
if (err_code != NRF_SUCCESS) {
125-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
126-
translate("Failed to write attribute value, status: 0x%08lX"), err_code));
121+
mp_raise_OSError_msg(translate("Failed to write attribute value"));
127122
}
128123

129124
while (sd_mutex_acquire(m_write_mutex) == NRF_ERROR_SOC_MUTEX_ALREADY_TAKEN) {
@@ -134,8 +129,7 @@ STATIC void gattc_write(bleio_characteristic_obj_t *characteristic, mp_buffer_in
134129

135130
err_code = sd_mutex_release(m_write_mutex);
136131
if (err_code != NRF_SUCCESS) {
137-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
138-
translate("Failed to release mutex, status: 0x%08lX"), err_code));
132+
mp_raise_OSError_msg(translate("Failed to release mutex"));
139133
}
140134
}
141135

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,23 @@
2626
*/
2727

2828
#include "common-hal/bleio/Descriptor.h"
29+
#include "shared-bindings/bleio/UUID.h"
2930

3031
void common_hal_bleio_descriptor_construct(bleio_descriptor_obj_t *self, bleio_uuid_obj_t *uuid) {
32+
// TODO: set handle ???
3133
self->uuid = uuid;
3234
}
3335

3436
void common_hal_bleio_descriptor_print(bleio_descriptor_obj_t *self, const mp_print_t *print) {
35-
mp_printf(print, "Descriptor(uuid: 0x" HEX2_FMT HEX2_FMT ")",
36-
self->uuid->value[1], self->uuid->value[0]);
37+
mp_printf(print, "Descriptor(uuid=");
38+
common_hal_bleio_uuid_print(self->uuid, print);
39+
mp_printf(print, ", handle=%0x", self->handle);
3740
}
3841

3942
mp_int_t common_hal_bleio_descriptor_get_handle(bleio_descriptor_obj_t *self) {
4043
return self->handle;
4144
}
4245

43-
mp_int_t common_hal_bleio_descriptor_get_uuid(bleio_descriptor_obj_t *self) {
44-
return self->uuid->value[0] | (self->uuid->value[1] << 8);
46+
mp_obj_t common_hal_bleio_descriptor_get_uuid(bleio_descriptor_obj_t *self) {
47+
return MP_OBJ_FROM_PTR(self->uuid);
4548
}

0 commit comments

Comments
 (0)