change device name and const problem

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
DeTo_1826166
Level 3
Level 3
First like received First like given

Hello,

I want to change the device name dynamically, it's working correctly when the device is powered.

But after the reset, the content of data in flash is the inizialisation value of the constant & with the value stored in flash

I declare a constant with initialisation value  (is it not supposed to be initialised only once ?)

const char8 deviceNameInFlash[20] = {'D','e','f','a','u','l','t')};

----------------------------

In the stack :

   case CYBLE_EVT_STACK_ON: /* This event is received when component is Started */

  CyBle_GapSetLocalName(deviceNameInFlash);

  CyBle_GappStartAdvertisement(CYBLE_ADVERTISING_FAST);

break;

---------------------------

  if(CYBLE_SHUTBASSCV_DEVICENAME_CHAR_HANDLE == wrReqParam->handleValPair.attrHandle)

            {

              for(i=0;i<wrReqParam->handleValPair.value.len;i++)

            {/*Client has to write the ASCI values of the characters that are in the name*/

                tempName=wrReqParam->handleValPair.value.val;

              

            }   

            StoreInFlash(tempName, wrReqParam->handleValPair.value.len); 

           

           

                communHandle.attrHandle = CYBLE_SHUTBASSCV_DEVICENAME_CHAR_HANDLE;

                communHandle.value.val = (uint8*) tempName;

                communHandle.value.len = wrReqParam->handleValPair.value.len;

                CyBle_GattsWriteAttributeValue(&communHandle,FALSE,&connectionHandle,FALSE);

   

            }

void StoreInFlash( char8 *newData, uint8 length)

{

    CYBLE_API_RESULT_T result;

    CyBle_ExitLPM();

    do

    {

        result = CyBle_StoreAppData((uint8*)newData, (uint8*)&deviceNameInFlash,length,0);

       

    }

    while (result!=CYBLE_ERROR_OK);

    CyBle_GapSetLocalName((const char *)newData);   

}

If the user replace the name by "test" , when disconnect/reconnect we have 'test'

But after device reset we have : "testult"

So the data in flash is reseted, and i don't understand why, "const " is not supposed to mean initialized once ?

Thanks for your answers.

0 Likes
1 Solution
Anonymous
Not applicable

Strings are stored as nonzero characters followed by a null character which is equal to 00. If you are not writing the 00 to flash, then the code will read the string from memory and will keep reading characters until it hits the null character from the initialization value of "default"0x00

Things to try:

Make sure to write the null character of the string to flash as well.

Try checking if there is already a value in memory before reading/writing/using the value, otherwise you might be overwriting it without checking if it is no longer the default value.

View solution in original post

3 Replies
Anonymous
Not applicable

Strings are stored as nonzero characters followed by a null character which is equal to 00. If you are not writing the 00 to flash, then the code will read the string from memory and will keep reading characters until it hits the null character from the initialization value of "default"0x00

Things to try:

Make sure to write the null character of the string to flash as well.

Try checking if there is already a value in memory before reading/writing/using the value, otherwise you might be overwriting it without checking if it is no longer the default value.

DeTo_1826166
Level 3
Level 3
First like received First like given

Thanks that was indeed the problem.

Other think to care is that the Device name in the configuration UI have to be longer or equal at the device name replacment otherwise it is cut (same characters number than the Device name in the configuration UI).

Anonymous
Not applicable

Yep. That is commented in the function documentation, but mentioning it is helpful too.

If you write the advertisement packet yourself you won't have that issue, but if you want a longer possible name you can start with a really long default name, and then shorten it on bootup/initialization.

Thanks,

Epratt

0 Likes