@@ -258,6 +258,7 @@ static int esp32_bt_gattc_read_cb(uint16_t conn_id,
258
258
259
259
bool mgos_bt_gattc_read (uint16_t conn_id , uint16_t handle ) {
260
260
LOG (LL_DEBUG , ("READ c %d ah %d" , conn_id , handle ));
261
+ int ret ;
261
262
bool res = false;
262
263
esp32_bt_rlock ();
263
264
struct esp32_bt_gattc_conn * conn = find_conn_by_id (conn_id );
@@ -275,11 +276,12 @@ bool mgos_bt_gattc_read(uint16_t conn_id, uint16_t handle) {
275
276
pr -> conn = conn ;
276
277
pr -> handle = handle ;
277
278
mbuf_init (& pr -> data , 0 );
278
- if ( ble_gattc_read_long (conn_id , handle , 0 , esp32_bt_gattc_read_cb , pr ) ==
279
- 0 ) {
279
+ ret = ble_gattc_read_long (conn_id , handle , 0 , esp32_bt_gattc_read_cb , pr );
280
+ if ( ret == 0 ) {
280
281
SLIST_INSERT_HEAD (& conn -> pending_reads , pr , next );
281
282
res = true;
282
283
} else {
284
+ LOG (LL_ERROR , ("ret = %d" , ret ));
283
285
esp32_bt_gattc_pending_read_free (pr );
284
286
}
285
287
out :
@@ -293,11 +295,49 @@ bool mgos_bt_gattc_subscribe(uint16_t conn_id, uint16_t handle) {
293
295
return false;
294
296
}
295
297
298
+ static int esp32_bt_gattc_write_cb (uint16_t conn_id ,
299
+ const struct ble_gatt_error * err ,
300
+ struct ble_gatt_attr * attr , void * arg ) {
301
+ bool resp_required = (bool ) (uintptr_t ) arg ;
302
+ LOG (LL_DEBUG , ("WRITE_CB c %d err %p st %d attr %p ah %d rr %d" , conn_id , err ,
303
+ (err ? err -> status : -1 ), attr , (attr ? attr -> handle : 0 ),
304
+ resp_required ));
305
+ struct esp32_bt_gattc_conn * conn = find_conn_by_id (conn_id );
306
+ if (conn == NULL ) return BLE_ATT_ERR_UNLIKELY ;
307
+ if (resp_required ) {
308
+ struct mgos_bt_gattc_write_result_arg rarg = {
309
+ .conn = conn -> gc ,
310
+ .handle = attr -> handle ,
311
+ .ok = (err -> status == 0 ),
312
+ };
313
+ mgos_event_trigger (MGOS_BT_GATTC_EV_WRITE_RESULT , & rarg );
314
+ }
315
+ return 0 ;
316
+ }
317
+
296
318
bool mgos_bt_gattc_write (uint16_t conn_id , uint16_t handle , struct mg_str data ,
297
319
bool resp_required ) {
320
+ LOG (LL_DEBUG , ("WRITE c %d ah %d rr %d" , conn_id , handle , resp_required ));
298
321
struct esp32_bt_gattc_conn * conn = find_conn_by_id (conn_id );
299
322
if (conn == NULL ) return false;
300
- return false;
323
+ int ret ;
324
+ uint16_t mtu = ble_att_mtu (conn_id );
325
+ if (data .len < mtu - 1 ) {
326
+ if (resp_required ) {
327
+ ret = ble_gattc_write_flat (conn_id , handle , data .p , data .len ,
328
+ esp32_bt_gattc_write_cb , (void * ) 1 );
329
+ } else {
330
+ ret = ble_gattc_write_no_rsp_flat (conn_id , handle , data .p , data .len );
331
+ }
332
+ } else {
333
+ struct os_mbuf * om = ble_hs_mbuf_from_flat (data .p , data .len );
334
+ ret = ble_gattc_write_long (conn_id , handle , 0 , om , esp32_bt_gattc_write_cb ,
335
+ (void * ) resp_required );
336
+ }
337
+ if (ret != 0 ) {
338
+ LOG (LL_ERROR , ("ret = %d" , ret ));
339
+ }
340
+ return (ret == 0 );
301
341
}
302
342
303
343
static int esp32_bt_gattc_add_disc_result_entry (
0 commit comments