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:无法使用使用 CC2650的 malloc 在指针数组中附加数据

Guru**** 2539500 points
Other Parts Discussed in Thread: CC2650

请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/wireless-connectivity/bluetooth-group/bluetooth/f/bluetooth-forum/1063825/launchxl-cc2650-not-able-to-append-data-in-pointer-array-using-malloc-using-cc2650

器件型号:LAUNCHXL-CC2650
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;
        }
    }

我使用的是 CC2650 launchpad

BLE SDK BLE_SDK_2_02_07_06

 简单 BLE 外设示例按照进行了修改  

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 

这个线程。

我使用 malloc 和 realloc 来附加从 BLE 器件接收到的数据。

我已附上修改后的代码、

首先,我成功获得200个字节,然后我再次尝试下一个200个字节的数据包-它失败了。

如果我注释  malloc 和 realloc 修改后的代码、我将每包接收200个字节的数据。

(我想从另一个 BLE 接收 CC2650中的图像)

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您好、Gajendra、

    我认为您应该在这里使用 iCall_malloc 调用、而不是 malloc。 malloc 在 TI-RTOS 环境中不会很好地工作、因为它将分配独立于线程的内存、而 iCall_malloc 在 TI-RTOS 环境中工作。

    最棒的

    不需要

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    Nathan Block 您好、感谢您的回复。

    我将尝试您的建议并告诉您、

    但是、您能告诉我、我能不 能使用 iCall_malloc 调用保存6KB 映像而不会出现任何问题吗?

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您好、Gajendra、

    这将取决于您在器件上可用的存储器。 您可以在 CCS 中监视内存使用情况、以查看是否接近内存不足。 如果不可行、您还可以考虑使用外部存储器或升级到具有更多板载存储器的器件。

    我还建议通过多次调用 malloc 来保存它。 尝试保存一个6 KB 连续内存块是不明智的。

    最棒的

    不需要

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    因为我已经提供了大部分细节

    我使用的是 CC2650 Launchpad、

    使用什么 SDK、

    我如何在"简单 BLE 外设"示例中进行少量修改(给出了之前的 e2e 线程链接)、所以可以说我的代码是"简单 BLE 外设"  

    我的上一个问题

    "但是、您能告诉我、我能不 能使用 iCall_malloc 调用保存6KB 映像而不会出现任何问题吗?"

    其中我清楚地讨论了 iCall_malloc 调用的使用

    可以告诉我、这有可能吗?

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    还有其他 TI 专家可以提供帮助吗?

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    我尝试使用  iCall_malloc。

    但是、在接收到6个200字节的数据包后、我的代码停止工作。
    它在处停止  

    静态 loader_exit (void)函数。

    在我按下"恢复"按钮后、它  

    再次在 void abort (void)暂停

    loader_exit();

    for (;;)

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您好、Gajendra、

    我感谢您的失望。 如果我理解正确、您在这里要问两个问题:

    1.是否可以为映像保留6KB 的内存? -理论上、您应该能够在芯片上保留6KB 的内存、但是您需要事先确保有足够的内存空间来实现这一点。 您可以使用 CCS 监视内存使用情况。

    2.为什么代码在6个200字节的数据包后停止工作。 ——这将是一个更棘手的问题。 您可能内存不足、或者您可能遇到错误。  是否正在运行 OAD?  loader_exit()函数会指示这种情况。 我不建议在 CC2650器件上运行 OAD、它可能没有足够的内存来保存整个 OAD 映像。

    最棒的

    不需要