Page 1 of 1

Semaphore Hang with BLE on BLERemoteCharacteristic.WriteValue()

Posted: Fri Mar 03, 2023 5:42 pm
by wholybee
New to BLE. New to Semaphores. I mostly have my head wrapped around it, and my application is working well enough. However, because the data is less than 20 bytes, I am just using the data passed in the Notification Callback. Because I might someday like to receive more than 20 bytes, I'm trying to get BLERemoteCharacteristic.ReadValue() working.

In the code below for Notify Callback, the commented lines are what I am trying to do. When I uncomment those lines, the program hangs in the flush function on the next call to WriteValue. I can't wrap my head around it. What is going on here and how to I make it work?
--------------------------

[ 4160][BleSerialClient.cpp:15] notifyCallback(): Notify callback for characteristic
[ 4175][BleSerialClient.cpp:16] notifyCallback(): 0000ff01-0000-1000-8000-00805f9b34fb
[ 4176][ByteRingBuffer.h:61] getLength(): getLength head: 0 tail: 0
[ 4183][V][BLERemoteCharacteristic.cpp:424] readValue(): >> readValue(): uuid: 0000ff01-0000-1000-8000-00805f9b34fb, handle: 17 0x11
[ 4198][ByteRingBuffer.h:61] getLength(): getLength head: 0 tail: 0
[ 4201][V][FreeRTOS.cpp:189] take(): Semaphore taking: name: ReadCharEvt (0x3ffe18ac), owner: <N/A> for readValue
[ 4217][ByteRingBuffer.h:61] getLength(): getLength head: 0 tail: 0
[ 4218][V][FreeRTOS.cpp:198] take(): Semaphore taken: name: ReadCharEvt (0x3ffe18ac), owner: readValue
[ 4233][ByteRingBuffer.h:61] getLength(): getLength head: 0 tail: 0
[ 4234][V][FreeRTOS.cpp:63] wait(): >> wait: Semaphore waiting: name: ReadCharEvt (0x3ffe18ac), owner: readValue for readValue
.
.
.
[ 4660][BleSerialClient.cpp:314] write(): In write(uint8_t byte)
[ 4666][BleSerialClient.cpp:320] write(): 0x77
[ 4670][BleSerialClient.cpp:336] flush(): flushing 7 bytes
[ 4676][V][BLERemoteCharacteristic.cpp:577] writeValue(): >> writeValue(), length: 7
[ 4684][V][FreeRTOS.cpp:189] take(): Semaphore taking: name: WriteCharEvt (0x3ffe2408), owner: <N/A> for writeValue
[ 4694][V][FreeRTOS.cpp:198] take(): Semaphore taken: name: WriteCharEvt (0x3ffe2408), owner: writeValue
[ 4703][V][FreeRTOS.cpp:63] wait(): >> wait: Semaphore waiting: name: WriteCharEvt (0x3ffe2408), owner: writeValue for writeValue


Code: Select all

static void notifyCallback(
  BLERemoteCharacteristic* pBLERemoteCharacteristic,
  uint8_t* pData,
  size_t length,
  bool isNotify) {


    log_i("Notify callback for characteristic ");
    log_i("%s",pBLERemoteCharacteristic->getUUID().toString().c_str());

  // std::string myString = pBLERemoteCharacteristic->readValue();
  // length = myString.length();  
  // pData = reinterpret_cast<uint8_t*>(&myString[0]);


    log_i(" of data length %d", length);

		for (int i = 0; i < length; i++){
       log_i("adding data to buffer %#04x", pData[i]);
		  	BleSerialClient::receiveBuffer.add(pData[i]); 
        }  

    Serial.println("Reading:");
    char strBuf[50];
    for (int i = 0; i < length; i++){ 
        sprintf(strBuf, " - 0x%02x",pData[i]);
        Serial.print(strBuf);
    }
    Serial.println();

}  // notifiy callback



void BleSerialClient::flush()
{
 
 if (true) { 
	    if (this->transmitBufferLength > 0)
	        {   log_i("flushing %d bytes",this->transmitBufferLength);
		          TxCharacteristic->writeValue(this->transmitBuffer, this->transmitBufferLength);
		          
              this->transmitBufferLength = 0;
              
          } // buffer > 0

flush_100ms = millis();     

  } // max buffer || 100ms
} // flush