This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CC2340R5: Error reading characteristics

Part Number: CC2340R5

Tool/software:

Hello,

CCS version : 12.7.1

SDK version : 8.10.01.02

Project : basic_ble

sysconf:
Enabled Peripheral + Central roles.

Сonnect the device to the Central.
The connected device uses a CC2642 microcontroller.
Getting discovery parameters.
Read the value of the received characteristic pointer (device status).
After a certain number of readings (210-230) I get an error that the pointer to the received data is NULL.
I reset the CC2340 and configure the stack anew. After that, everything repeats.
What could be the problem?
At the same time, the BLE stack works, since there is a connection with the CC2340 via BLE.

The code of the GATT event handler

static void GATT_EventHandler(uint32 event, BLEAppUtil_msgHdr_t *pMsgData)
{
  gattMsgEvent_t *gattMsg = ( gattMsgEvent_t * )pMsgData;
  uint8_t channel = Connection_getConnIndex(gattMsg->connHandle);
  attReadByTypeReq_t req;

  if ((channel == LL_INACTIVE_CONNECTIONS) || (channel == BLE_CHANNEL_PERIPHERAL))
    return;

  if (gattMsg->hdr.status == bleNotConnected) 
    return;

  switch (event) {

  case BLEAPPUTIL_ATT_MTU_UPDATED_EVENT:
    /*                   */  
    break;
   
  case BLEAPPUTIL_ATT_EXCHANGE_MTU_RSP:
    /*                   */
    break;

  case BLEAPPUTIL_ATT_FIND_BY_TYPE_VALUE_RSP:
    /*       */  
    break;

  case BLEAPPUTIL_ATT_READ_BY_TYPE_RSP:
    /*        */
    break;

  case BLEAPPUTIL_ATT_READ_RSP:
  {
    uint8_t *data = (uint8_t*)gattMsg->msg.readRsp.pValue;
    uint16_t len = gattMsg->msg.readRsp.len;

    if ((data == NULL) || (len == 0))
      break;

    // Valve state
    /*        */ 
    break;
  }

  case BLEAPPUTIL_ATT_WRITE_RSP:
    /*             */ 
    
break;
       
  case BLEAPPUTIL_ATT_ERROR_RSP:
      /*           */
      break;
  default:
    break;
  } // switch

}

  • Hi,

    Thank you for reaching out. There have been a few SDK releases since the 8.10 release which have fixed many bugs and made several improvements to the F3 SDK. Could you re-test this behavior on the 8.40 SDK? Do you have access to a Bluetooth LE Sniffer? If so, then could you take a sniffer log of the behavior as well?

    Best Regards,

    Jan

  • Hello,
    Thanks for the answer.
    I have  Bluetooth LE Sniffer. 
    I'll take the logs again and post them in the chat.
    Will also try to test the device with the new SDK.
    I will report the results obtained.

  • Hi,

    Sounds good. Looking forward to the results!

    Best Regards,

    Jan

  • Hi,
    Connected to the project SDK version 8.40.
    The problem repeated itself, after several operations of reading the characteristic the pointer msg.readRsp.pValue returns NULL.
    Launched ROV to track the heap size.
    Each time the GATT_ReadCharValue() function is called, the heap size decreases by 64 bytes.
    And after processing the BLEAPPUTIL_ATT_READ_RSP event, the heap size is not restored.
    Tell me, is it problem getting gattMsg->msg.readRsp.pValue = NULL due to a memory leak?
    Is it necessary to free the memory by the gattMsg-> msg.readRsp.pValue pointer in the BLEAPPUTIL_ATT_READ_RSP event?
    And how to do it correctly?

  • Hi,

    Thanks for quickly checking on 8.40! You are correct. The ATT_READ_RSP payload must be free'd once the data is extracted. The BLEAppUtil_free() api may be used for this purpose.

    Best Regards,

    Jan

  • Hi,
    I tried to free memory by the gattMsg->msg.readRsp.pValue pointer using BLEAppUtil_free().
    But if I perform such an operation, I get a hardfault.
    That's why I wrote, how to free memory by the gattMsg->msg.readRsp.pValue pointer correctly?

  • Hi,

    Can you share the code snippet where you attempted the free?

    Best Regards,

    Jan

  • Hi,
    A fragment of the code for handling the BLEAPPUTIL_ATT_READ_RSP event
    where data processing and memory clearing are performed upon completion of the operation

    case BLEAPPUTIL_ATT_READ_RSP: // Response received
    {
    uint8_t *data = (uint8_t*)gattMsg->msg.readRsp.pValue;
    uint16_t len = gattMsg->msg.readRsp.len;

        if ((data == NULL) || (len == 0)) {  // No data available
            if (data == NULL)
                set_error(7);
                break;
        }
        // Processing of received data
       
    if (pConnInfo->discoveryState == BLE_DISCOVERY_STATE_START) {
            if (len == sizeof(BLEM_ValveState_t)) {
                iBus_sendFrameGet(data, len, channel);
            }
        }
        // Clearing memory
       
    BLEAppUtil_free(data);

        break;
    }

    As far as I understand, the error occurs not in this place, but when trying to clear memory in event handling in BLEAppUtil_Task

    void *BLEAppUtil_Task(void *arg)
    {
        // Register to the stack and create queue and event
        BLEAppUtil_stackRegister();

        // Init the ble stack
        BLEAppUtil_stackInit();

        // Application main loop
        for (;;)
        {
            BLEAppUtil_appEvt_t pAppEvt;

            // wait until receive queue message
            if (mq_receive(BLEAppUtil_theardEntity.queueHandle, (char*)&pAppEvt, sizeof(pAppEvt), NULL) > 0)
            {
                BLEAppUtil_msgHdr_t *pMsgData = (BLEAppUtil_msgHdr_t *)pAppEvt.pData;
                bool freeMsg = FALSE;
       .......

                // Free the data
                if (pMsgData && freeMsg)
                {
                    // Use freeMsg
                    BLEAppUtil_freeMsg(pMsgData);
                }
                else if (pMsgData)
                {
                    // Use free
                    BLEAppUtil_free(pMsgData);
                    pMsgData = NULL;
                }
                else
                {
                    /* this else clause is required, even if the
                       programmer expects this will never be reached
                       Fix Misra-C Required: MISRA.IF.NO_ELSE */
                }
            }
        }
    }

  • Hi,

    Its possible we are double freeing the memory. Can you try setting the variable to NULL right after your free?

    Best Regards,

    Jan

  • Hi,
    Found a solution to the problem.
    At the end of the GATT EventHandler handler, which is located in the app_data.c module, you need to call the GATT_bm_free(&gattMsg->msg, gattMsg->method) function.
    In my opinion, this should be added to the examples provided in the SDK.