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.

LAUNCHXL-CC2650: not able to append data in pointer array using malloc using CC2650

Part Number: LAUNCHXL-CC2650
Other Parts Discussed in Thread: CC2650

static bStatus_t simpleProfile_WriteAttrCB(uint16_t connHandle,
                                           gattAttribute_t *pAttr,
                                           uint8_t *pValue, uint16_t len,
                                           uint16_t offset, uint8_t method)
{
    bStatus_t status = SUCCESS;
    uint8 notifyApp = 0xFF;

    if (pAttr->type.len == ATT_BT_UUID_SIZE)
    {
        // 16-bit UUID
        uint16 uuid = BUILD_UINT16(pAttr->type.uuid[0], pAttr->type.uuid[1]);
        switch (uuid)
        {
        case SIMPLEPROFILE_CHAR1_UUID:
        case SIMPLEPROFILE_CHAR3_UUID:

            //Validate the value
            // Make sure it's not a blob oper
            if (offset == 0)
            {
                if (len != 1)
                {
                    status = ATT_ERR_INVALID_VALUE_SIZE;
                }
            }
            else
            {
                status = ATT_ERR_ATTR_NOT_LONG;
            }

            //Write the value
            if (status == SUCCESS)
            {
                uint8 *pCurValue = (uint8 *) pAttr->pValue;
                *pCurValue = pValue[0];

                if (pAttr->pValue == &simpleProfileChar1)
                {
                    notifyApp = SIMPLEPROFILE_CHAR1;
                }
                else
                {
                    notifyApp = SIMPLEPROFILE_CHAR3;
                }
            }

            break;

        case GATT_CLIENT_CHAR_CFG_UUID:
            status = GATTServApp_ProcessCCCWriteReq(connHandle, pAttr, pValue,
                                                    len, offset,
                                                    GATT_CLIENT_CFG_NOTIFY);
            break;

        case SIMPLEPROFILE_CHAR5_UUID:
            //Validate the value
            // Make sure it's not a blob oper

            if (offset == 0)
            {
                if (len > SIMPLEPROFILE_CHAR5_LEN)
                {
                    status = ATT_ERR_INVALID_VALUE_SIZE;
                }
            }
            else
            {
                status = ATT_ERR_ATTR_NOT_LONG;
            }

            //Write the value
            if (status == SUCCESS)
            {
                uint8 *pCurValue = (uint8 *) pAttr->pValue; //-------------------------------------------------------------------------------
                memcpy(pCurValue, pValue, len); //--------------------------------------//here i get the correct length of received data bytes - put breakpoint here to see
//my code starts here
                test_len = len;
                if (round_number == 0)
                {
                    if (test_len > 0)
                    {
                        ptr = (uint8*) malloc(test_len);
                        if (ptr == NULL)
                        {
                            //                printf("Error! memory not allocated.");
                            exit(0);
                        }
                        for (uint8 i = 0; i < test_len; ++i)
                        {
                            ptr[i] = simpleProfileChar5[i];
                        }
                        prev_len = test_len;
                    }
                }
                else if (round_number > 0)
                {
                    new_len = prev_len + test_len;
                    ptr = realloc(ptr, new_len); //allocated new memory size
                    if (ptr == NULL)
                    {
                        //                printf("Error! memory not allocated.");
                        exit(0);
                    }
                    else
                    {
                        int j = 0;
                        for (int i = prev_len; i <= new_len; i++)
                        {
                            ptr[i] = simpleProfileChar5[j];
                            j++;
                        }
                        prev_len = new_len;
                    }
                }
                //my code ends here
//                free(ptr);
//                round_number++;
                if (pAttr->pValue == &simpleProfileChar5) //-- org 1
                {
                    notifyApp = SIMPLEPROFILE_CHAR5;
                } //-- org 1

//            EPD_2IN66_Display(simpleProfileChar5); // this function call writes received data array from android bluetooth to epd display
            }
            break;

        default:
            // Should never get here! (characteristics 2 and 4 do not have write permissions)
            status = ATT_ERR_ATTR_NOT_FOUND;
            break;
        }
    }

I am using CC2650 lauchpad

ble sdk ble_sdk_2_02_07_06

 Simple BLE peripheral Example is modified as per 

https://e2e.ti.com/support/wireless-connectivity/bluetooth-group/bluetooth/f/bluetooth-forum/1061682/launchxl-cc2650-simpleprofilechar5-array-values-are-overwritten-when-more-than-20-bytes-are-received-from-ble-scanner 

this thread.

I am using malloc and realloc to append data received from BLE device.

I have attached my modified code ,

first I get 200 bytes Successfully then I again try for next packet of 200 bytes - - It fails.

If I comment malloc and realloc modified code I receive data 200 bytes per packet.

(I want to receive image in CC2650 from another BLE)

  • Hi Gajendra,

    I believe you should be using the iCall_malloc call here instead of malloc. malloc will not work well in the TI-RTOS environment because it will allocate memory independent of the thread, whereas iCall_malloc works inside the TI-RTOS environment.

    Best,

    Nate

  • Hello Nathan Block, thank you for reply.

    I will try you suggestions and let you know,

    but can you tell me that, can I save 6kb image using iCall_malloc call without any issues?

  • Hi Gajendra,

    That will depend on the memory you have available on the device. You can monitor your memory usage in CCS to see if you're coming close to running out of memory. You might also consider using external memory or upgrading to a device with more on-board memory if this is unfeasible.

    I would also recommend saving it with multiple calls to malloc. It would not be smart to try to save a 6 kB contiguous block of memory.

    Best,

    Nate

  • As I have already given most of the details that

    I am using CC2650 launchpad,

    what sdk is being used,

    how I have done little modification in "simple BLE peripheral" example (previous e2e thread link is given) so can say my code is"simple BLE peripheral" 

    Also my previous question

    "but can you tell me that, can I save 6kb image using iCall_malloc call without any issues?"

    where I am clearly talking about use of iCall_malloc calls

    Can you please tell me is it possible?

  • Is there any other TI expert available who can help?

  • I tried using iCall_malloc.

    But after receiving 6 packets of 200 bytes my code stops working.
    It halts at 

    Static loader_exit(void) function.

    and after i press resume, it 

    again halts at void abort(void)

    {

    loader_exit();

    for(;;)

    }

  • Hi Gajendra,

    I appreciate your frustration. If I understand correctly, there are two questions you're asking here:

    1. Is it possible to reserve the 6kB of memory for the image? - Theoretically you should be able to reserve 6 kB of memory on your chip, but you'll need to make sure there's enough space in memory for this beforehand. You can monitor your memory usage using CCS.

    2. Why does your code stop working after 6 packets of 200 bytes. - This will be trickier to figure out. You might be running out of memory, or you might be running into an error. Are you running OAD? The loader_exit() function indicates so. I would not recommend running OAD on the CC2650 device, and it likely won't have enough memory to hold an entire OAD image.

    Best,

    Nate