@@ -77,6 +77,7 @@ static mp_obj_t mp_gatts_observer;
7777#if (BLUETOOTH_SD == 130 ) || (BLUETOOTH_SD == 132 )
7878static volatile bool m_primary_service_found ;
7979static volatile bool m_characteristic_found ;
80+ static volatile bool m_write_done ;
8081
8182static volatile ble_drv_adv_evt_callback_t adv_event_handler ;
8283static volatile ble_drv_gattc_evt_callback_t gattc_event_handler ;
@@ -89,6 +90,7 @@ static mp_obj_t mp_gattc_observer;
8990static mp_obj_t mp_gattc_disc_service_observer ;
9091static mp_obj_t mp_gattc_disc_char_observer ;
9192static mp_obj_t mp_gattc_char_data_observer ;
93+ static mp_obj_t mp_gattc_char_data_observer ;
9294#endif
9395
9496#if (BLUETOOTH_SD != 100 ) && (BLUETOOTH_SD != 110 )
@@ -679,23 +681,34 @@ void ble_drv_attr_c_read(uint16_t conn_handle, uint16_t handle, mp_obj_t obj, bl
679681 }
680682}
681683
682- void ble_drv_attr_c_write (uint16_t conn_handle , uint16_t handle , uint16_t len , uint8_t * p_data ) {
684+ void ble_drv_attr_c_write (uint16_t conn_handle , uint16_t handle , uint16_t len , uint8_t * p_data , bool w_response ) {
685+
686+ ble_gattc_write_params_t write_params ;
687+
688+ if (w_response ) {
689+ write_params .write_op = BLE_GATT_OP_WRITE_REQ ;
690+ } else {
691+ write_params .write_op = BLE_GATT_OP_WRITE_CMD ;
692+ }
683693
684- ble_gattc_write_params_t write_params ;
694+ write_params .flags = BLE_GATT_EXEC_WRITE_FLAG_PREPARED_CANCEL ;
695+ write_params .handle = handle ;
696+ write_params .offset = 0 ;
697+ write_params .len = len ;
698+ write_params .p_value = p_data ;
685699
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 ;
700+ uint32_t err_code = sd_ble_gattc_write (conn_handle , & write_params );
692701
693- uint32_t err_code = sd_ble_gattc_write ( conn_handle , & write_params ) ;
702+ m_write_done = ! w_response ;
694703
695- if (err_code != 0 ) {
704+ if (err_code != 0 ) {
696705 nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_OSError ,
697706 "Can not write attribute value. status: 0x" HEX2_FMT , (uint16_t )err_code ));
698- }
707+ }
708+
709+ while (m_write_done != true) {
710+ ;
711+ }
699712}
700713
701714void ble_drv_scan_start (void ) {
@@ -1007,6 +1020,7 @@ static void ble_evt_handler(ble_evt_t * p_ble_evt) {
10071020
10081021 case BLE_GATTC_EVT_WRITE_RSP :
10091022 BLE_DRIVER_LOG ("BLE EVT WRITE RESPONSE\n" );
1023+ m_write_done = true;
10101024 break ;
10111025
10121026 case BLE_GATTC_EVT_HVX :
0 commit comments