From 482f0e987b02d8b9b7dc168708e3989f431ce6d8 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 12 Apr 2017 17:47:40 +0700 Subject: [PATCH] wrap up gattc long write --- .../examples/Peripheral/ancs/ancs.ino | 10 ++++---- .../src/BLEClientCharacteristic.cpp | 24 ++++++++++++------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/ancs/ancs.ino b/libraries/Bluefruit52Lib/examples/Peripheral/ancs/ancs.ino index 82560c22..5b13dc6a 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/ancs/ancs.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/ancs/ancs.ino @@ -104,10 +104,9 @@ void ancs_notification_callback(AncsNotification_t* notif) Serial.printf("%-20s | ", app_id); // Get Application Name ( not work yet) - // memset(buffer, 0, sizeof(buffer)); - // n = bleancs.getAppAttribute(app_id, ANCS_APP_ATTR_DISPLAY_NAME, buffer, sizeof(buffer)); - // PRINT_INT(n); - // Serial.printf("%-20s | ", buffer); + memset(buffer, 0, sizeof(buffer)); + n = bleancs.getAppAttribute(app_id, ANCS_APP_ATTR_DISPLAY_NAME, buffer, sizeof(buffer)); + Serial.printf("%-15 | ", buffer); Serial.println(); @@ -129,9 +128,10 @@ void connect_callback(void) Serial.println("Found it"); // ANCS requires pairing to work, it makes sense to request security here as well - Serial.println("Attempt to bond/pair with iOS, please press PAIR on your phone"); + Serial.print("Attempt to bond/pair with iOS, please press PAIR on your phone ... "); if ( Bluefruit.requestPairing() ) { + Serial.println("Done"); Serial.println("Enable Notification"); Serial.println(); bleancs.enableNotification(); diff --git a/libraries/Bluefruit52Lib/src/BLEClientCharacteristic.cpp b/libraries/Bluefruit52Lib/src/BLEClientCharacteristic.cpp index 221ed430..a831fc90 100644 --- a/libraries/Bluefruit52Lib/src/BLEClientCharacteristic.cpp +++ b/libraries/Bluefruit52Lib/src/BLEClientCharacteristic.cpp @@ -137,13 +137,15 @@ err_t BLEClientCharacteristic::_write_and_wait_rsp(ble_gattc_write_params_t* par return xSemaphoreTake(_sem, ms2tick(BLE_GENERIC_TIMEOUT) ) ? ERROR_NONE : NRF_ERROR_TIMEOUT; } -uint16_t BLEClientCharacteristic::write_resp(const void* data, int len) +uint16_t BLEClientCharacteristic::write_resp(const void* data, int len32) { // Break into multiple MTU-3 packet // TODO Currently SD132 v2.0 MTU is fixed with max payload = 20 // SD132 v3.0 could negotiate MTU to higher number // For BLE_GATT_OP_PREP_WRITE_REQ, max data is 18 ( 2 byte is used for offset) enum { MTU_MPS = 20 } ; + + uint16_t len = (uint16_t) len32; const bool long_write = (len > MTU_MPS); const uint8_t* u8data = (const uint8_t*) data; @@ -162,7 +164,7 @@ uint16_t BLEClientCharacteristic::write_resp(const void* data, int len) .handle = _chr.handle_value, .offset = 0, .len = len, - .p_value = (uint8_t* ) u8data + .p_value = (uint8_t*) u8data }; status = _write_and_wait_rsp(¶m); @@ -172,7 +174,7 @@ uint16_t BLEClientCharacteristic::write_resp(const void* data, int len) /*------------- Long Write Prepare -------------*/ const uint16_t max_packet = MTU_MPS - 2; // 2 bytes for offset - int remaining = len; + uint16_t remaining = len; while( remaining ) { uint16_t packet_len = min16(max_packet, remaining); @@ -182,12 +184,11 @@ uint16_t BLEClientCharacteristic::write_resp(const void* data, int len) .write_op = BLE_GATT_OP_PREP_WRITE_REQ, .flags = 0, .handle = _chr.handle_value, - .offset = (uint16_t) len-remaining, + .offset = len-remaining, .len = packet_len, - .p_value = (uint8_t* ) u8data + .p_value = (uint8_t*) u8data }; status = _write_and_wait_rsp(¶m); - if ( ERROR_NONE != status ) break; remaining -= packet_len; @@ -205,13 +206,18 @@ uint16_t BLEClientCharacteristic::write_resp(const void* data, int len) .p_value = NULL }; - status = _write_and_wait_rsp(¶m); + // Last BLE_GATTC_EVT_WRITE_RSP for BLE_GATT_OP_EXEC_WRITE_REQ does not + // contain characteristic's handle. Therefor BLEGatt couldn't forward the + // event to us. Just skip the wait for now +// status = _write_and_wait_rsp(¶m); + status = sd_ble_gattc_write(_service->connHandle(), ¶m); } vSemaphoreDelete(_sem); _sem = NULL; - return (status == ERROR_NONE) ? (uint16_t) len : 0; + VERIFY_STATUS(status, 0); + return len; } uint16_t BLEClientCharacteristic::write(const void* data, int len) @@ -338,7 +344,7 @@ void BLEClientCharacteristic::_eventHandler(ble_evt_t* evt) if (_sem) { // TODO check evt->evt.gattc_evt.gatt_status - if (_sem) xSemaphoreGive(_sem); + xSemaphoreGive(_sem); } } break;