Skip to content

Commit d83c4fa

Browse files
authored
Merge pull request adafruit#51 from glennrub/ubluepy_char_write_with_response
Ubluepy char write with response
2 parents 5be5380 + a81de24 commit d83c4fa

3 files changed

Lines changed: 42 additions & 18 deletions

File tree

nrf5/drivers/bluetooth/ble_drv.c

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ static mp_obj_t mp_gatts_observer;
7777
#if (BLUETOOTH_SD == 130) || (BLUETOOTH_SD == 132)
7878
static volatile bool m_primary_service_found;
7979
static volatile bool m_characteristic_found;
80+
static volatile bool m_write_done;
8081

8182
static volatile ble_drv_adv_evt_callback_t adv_event_handler;
8283
static volatile ble_drv_gattc_evt_callback_t gattc_event_handler;
@@ -679,23 +680,34 @@ void ble_drv_attr_c_read(uint16_t conn_handle, uint16_t handle, mp_obj_t obj, bl
679680
}
680681
}
681682

682-
void ble_drv_attr_c_write(uint16_t conn_handle, uint16_t handle, uint16_t len, uint8_t * p_data) {
683+
void ble_drv_attr_c_write(uint16_t conn_handle, uint16_t handle, uint16_t len, uint8_t * p_data, bool w_response) {
683684

684-
ble_gattc_write_params_t write_params;
685+
ble_gattc_write_params_t write_params;
685686

686-
write_params.write_op = BLE_GATT_OP_WRITE_CMD;
687-
write_params.flags = BLE_GATT_EXEC_WRITE_FLAG_PREPARED_CANCEL;
688-
write_params.handle = handle;
689-
write_params.offset = 0;
690-
write_params.len = len;
691-
write_params.p_value = p_data;
687+
if (w_response) {
688+
write_params.write_op = BLE_GATT_OP_WRITE_REQ;
689+
} else {
690+
write_params.write_op = BLE_GATT_OP_WRITE_CMD;
691+
}
692+
693+
write_params.flags = BLE_GATT_EXEC_WRITE_FLAG_PREPARED_CANCEL;
694+
write_params.handle = handle;
695+
write_params.offset = 0;
696+
write_params.len = len;
697+
write_params.p_value = p_data;
692698

693-
uint32_t err_code = sd_ble_gattc_write(conn_handle, &write_params);
699+
m_write_done = !w_response;
694700

695-
if (err_code != 0) {
701+
uint32_t err_code = sd_ble_gattc_write(conn_handle, &write_params);
702+
703+
if (err_code != 0) {
696704
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
697705
"Can not write attribute value. status: 0x" HEX2_FMT, (uint16_t)err_code));
698-
}
706+
}
707+
708+
while (m_write_done != true) {
709+
;
710+
}
699711
}
700712

701713
void ble_drv_scan_start(void) {
@@ -1007,6 +1019,7 @@ static void ble_evt_handler(ble_evt_t * p_ble_evt) {
10071019

10081020
case BLE_GATTC_EVT_WRITE_RSP:
10091021
BLE_DRIVER_LOG("BLE EVT WRITE RESPONSE\n");
1022+
m_write_done = true;
10101023
break;
10111024

10121025
case BLE_GATTC_EVT_HVX:

nrf5/drivers/bluetooth/ble_drv.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ void ble_drv_attr_s_write(uint16_t conn_handle, uint16_t handle, uint16_t len, u
104104

105105
void ble_drv_attr_s_notify(uint16_t conn_handle, uint16_t handle, uint16_t len, uint8_t * p_data);
106106

107-
void ble_drv_attr_c_write(uint16_t conn_handle, uint16_t handle, uint16_t len, uint8_t * p_data);
107+
void ble_drv_attr_c_write(uint16_t conn_handle, uint16_t handle, uint16_t len, uint8_t * p_data, bool w_response);
108108

109109
void ble_drv_scan_start(void);
110110

nrf5/modules/ubluepy/ubluepy_characteristic.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,20 @@ STATIC mp_obj_t char_read(mp_obj_t self_in) {
111111
}
112112
STATIC MP_DEFINE_CONST_FUN_OBJ_1(ubluepy_characteristic_read_obj, char_read);
113113

114-
/// \method write(data)
114+
/// \method write(data, [with_response=False])
115115
/// Write Characteristic value.
116116
///
117-
STATIC mp_obj_t char_write(mp_obj_t self_in, mp_obj_t data) {
118-
ubluepy_characteristic_obj_t * self = MP_OBJ_TO_PTR(self_in);
117+
STATIC mp_obj_t char_write(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
118+
ubluepy_characteristic_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
119+
mp_obj_t data = pos_args[1];
120+
121+
static const mp_arg_t allowed_args[] = {
122+
{ MP_QSTR_with_response, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false } },
123+
};
124+
125+
// parse args
126+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
127+
mp_arg_parse_all(n_args - 2, pos_args + 2, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
119128

120129
mp_buffer_info_t bufinfo;
121130
mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ);
@@ -137,16 +146,18 @@ STATIC mp_obj_t char_write(mp_obj_t self_in, mp_obj_t data) {
137146
}
138147
} else {
139148
#if MICROPY_PY_UBLUEPY_CENTRAL
149+
bool with_response = args[0].u_bool;
150+
140151
ble_drv_attr_c_write(self->p_service->p_periph->conn_handle,
141152
self->handle,
142153
bufinfo.len,
143-
bufinfo.buf);
154+
bufinfo.buf,
155+
with_response);
144156
#endif
145157
}
146158
return mp_const_none;
147159
}
148-
STATIC MP_DEFINE_CONST_FUN_OBJ_2(ubluepy_characteristic_write_obj, char_write);
149-
160+
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(ubluepy_characteristic_write_obj, 2, char_write);
150161

151162
/// \method properties()
152163
/// Read Characteristic value properties.

0 commit comments

Comments
 (0)