Saving data to flash


#1

i want to save some values to flash but my code crashes here is the code:
#define PARAMETERSNUMBER 51
static const ascii * FLH_HANDLE = “handle”;
ascii *pin;
ascii *serverApn;
ascii *userApn;
ascii *passApn;
ascii *parameters[PARAMETERSNUMBER];
void chooseCommand(ascii *tempData,u32 count,adl_atCmdPreParser_t *paras){
switch ( count ) {
case PARAM_PIN:{
pin=tempData;
parameters[PARAM_PIN]=pin;
}
break;

case PARAM_SERVER_APN:{           
         serverApn=tempData;
          parameters[PARAM_SERVER_APN]=serverApn;
}
         break;
        
case PARAM_USER_APN:{         
         userApn=tempData;              
         parameters[PARAM_USER_APN]=userApn;
}
         break;
        
case PARAM_PASSWORD_APN:{             
         passApn=tempData;
         parameters[PARAM_PASSWORD_APN]=passApn;      
}
         break;
        
default:
    break;

}
}

void storeToFlash(){
s8 rsp=adl_flhSubscribe ( FLH_HANDLE, PARAMETERSNUMBER);
s16 index;
if (rsp==OK) {
TRACE ((TRACE_LEVEL_FCT,“OK FLASH HANDLE”));
}
else{
TRACE ((TRACE_LEVEL_FCT,“ERR FLASH HANDLE %d”,rsp));
}
for (index=0;index<PARAMETERSNUMBER;index++){
TRACE((TRACE_LEVEL_FCT,“storing %s”,parameters[index]));
if (parameters[index]!=NULL){
rsp=adl_flhWrite(FLH_HANDLE,index,wm_strlen(parameters[index]),parameters[index]);
if (rsp==OK){
TRACE ((TRACE_LEVEL_FCT,“OK FLASH WRITE”));
}
else{
TRACE ((TRACE_LEVEL_FCT,“ERR FLASH WRITE”));
}
}
}
for (index=0;index<PARAMETERSNUMBER;index++){
s32 length = adl_flhExist ( FLH_HANDLE, index );
ascii * ParamStr;
rsp=adl_flhRead(FLH_HANDLE,index,length,ParamStr);
TRACE((TRACE_LEVEL_FCT,“restored %s”,ParamStr));
if (rsp==OK){
TRACE ((TRACE_LEVEL_FCT,“OK FLASH READ”));
}
else{
TRACE ((TRACE_LEVEL_FCT,“ERR FLASH READ”));
}
}
}

FUNC void tokeniseWscript(adl_atCmdPreParser_t *paras,ascii separator){
u32 count=-1;
ascii *data;
ascii *tempData;
data=paras->StrData;
TRACE((TRACE_LEVEL_FCT,“Started parsing”));
tempData=wm_strtok(data+11,",");
while (tempData!=NULL){
count++;
chooseCommand(tempData,count,paras);
tempData=wm_strtok(NULL,",");
}
TRACE((TRACE_LEVEL_FCT,“Ended parsing”));
storeToFlash();
}


#2

what is wrong with this code?


#3

You’re not allocating memory.
You are using pointers but you are not actually pointing them to any free space to use.


#4

ok i tried your suggestion and i try to allocate memory the thing is that now crashes earlier :frowning:
void chooseCommand(ascii *tempData,u32 count,adl_atCmdPreParser_t *paras){
switch ( count ) {
case PARAM_PIN:{
TRACE (( TRACE_LEVEL_FCT, “PIN number” ));
TRACE (( TRACE_LEVEL_FCT, tempData));
pin= (ascii *)adl_memGet((u16)wm_strlen(tempData));
wm_strcpy(pin,tempData);
parameters[PARAM_PIN]=pin;
}
break;

case PARAM_SERVER_APN:{
         TRACE (( TRACE_LEVEL_FCT, "server apn"));             
         TRACE (( TRACE_LEVEL_FCT, serverApn));
         serverApn= (ascii *)adl_memGet((u16)wm_strlen(tempData)); // here is the crash Cannot access memory at address 0x200000
         wm_strcpy(serverApn,tempData);
         parameters[PARAM_SERVER_APN]=serverApn;
}
         break;
        
case PARAM_USER_APN:{
         TRACE (( TRACE_LEVEL_FCT, "user apn " ));
         userApn= (ascii *)adl_memGet((u16)wm_strlen(tempData));
          wm_strcpy(userApn,tempData);              
         parameters[PARAM_USER_APN]=userApn;
         TRACE (( TRACE_LEVEL_FCT,userApn));
}
         break;
        
case PARAM_PASSWORD_APN:{
         TRACE (( TRACE_LEVEL_FCT, "pass apn" ));
         passApn= (ascii *)adl_memGet((u16)wm_strlen(tempData));           
        wm_strcpy(passApn,tempData);
         parameters[PARAM_PASSWORD_APN]=passApn;
         TRACE (( TRACE_LEVEL_FCT,passApn));       
}
         break;
        
default:
    break;

}
}


#5

do you have some other solution ?


#6

Please use the ‘Code’ button so that the layout of your code is preserved!
It is currently unreadable; it should look something like this:

FUNC void tokeniseWscript(adl_atCmdPreParser_t *paras,ascii separator){
    u32 count=-1;
    ascii *data;
    ascii *tempData;
    data=paras->StrData;
    TRACE((TRACE_LEVEL_FCT,"Started parsing"));
    tempData=wm_strtok(data+11,",");
    while (tempData!=NULL){
        count++;
        chooseCommand(tempData,count,paras);
        tempData=wm_strtok(NULL,",");
      }   
    TRACE((TRACE_LEVEL_FCT,"Ended parsing"));
    storeToFlash(); 
}

See the forum User’s Guide for further details:
wavecom.com/modules/movie/sc … m.php?f=24

you could further increase legibility by using some more spaces:

FUNC void tokeniseWscript( adl_atCmdPreParser_t *paras, ascii separator ){
    u32    count = -1;
    ascii *data;
    ascii *tempData;

    data=paras->StrData;

    TRACE(( TRACE_LEVEL_FCT, "Started parsing" ));

    tempData = wm_strtok( data+11, "," );
    while( tempData != NULL ){
        count++;
        chooseCommand( tempData, count, paras );
        tempData = wm_strtok( NULL, "," );
    }  
 
    TRACE(( TRACE_LEVEL_FCT, "Ended parsing" ));
    storeToFlash(); 
}

#7

ok forget about this i made a silly error tried to print serverapn variable before allocating memory


#8

OK - but please remember about the ‘Code’ button for future posts!

You may also wish to consider how the legibility of your code could be improved - the easier it is to read, the more likely you are to get help with it! :slight_smile:
Or even to spot your own errors! :blush:


#9

ok boss :smiley:


#10

tobias was right i wasn’t allocating memory indeed tank you all


#11

i am still puzzled by some things here
i managed to store data and retreive data from flash the thing is that when i store a string 0000 or test or something else i get back 0000 test what’s up whit this ?


#12

i have this

#define PARAMETERSNUMBER 51 
   static const ascii * FLH_HANDLE = "handle"; 
   ascii  *parameters[PARAMETERSNUMBER]; 
   // iterate throught the parameters
   for (index=0;index<PARAMETERSNUMBER;index++){
    // if that parameters has been entered
      if (parameters[index]!=NULL){
           TRACE((TRACE_LEVEL_FLASH_R_W,parameters[index]));
           // store the parameter to the flash
           rsp=adl_flhWrite(FLH_HANDLE,index,wm_strlen(parameters[index]),parameters[index]);
            s32 length = adl_flhExist ( FLH_HANDLE, index );
           ascii * ParamStr;
           ParamStr=(ascii *)adl_memGet(length);
           rsp=adl_flhRead(FLH_HANDLE,index,length,ParamStr);
           TRACE((TRACE_LEVEL_FLASH_R_W,ParamStr));
           adl_memRelease(ParamStr);
          if (rsp==OK){
          TRACE ((TRACE_LEVEL_FLASH_R_W,"OK FLASH WRITE"));
          }
          else{
          TRACE ((TRACE_LEVEL_FLASH_R_W,"ERR FLASH WRITE"));
          }
      }
   }

if i store “devicename” it is retreived ok but if i store to flash “0000” then when i retreive it i get “0000\005” and if i have “1” then i get “1000\005” can someone help me out why is this happening?


#13

The problem is that after reading back the string you don’t have a terminating 0 (null) for your C string.
When you write to the Flash you are using wm_strlen() which gives the length of the string, excluding the terminating null. That’s what you want, but when you read it back you need to add the null again.
Something like this might work -

s32 length = adl_flhExist ( FLH_HANDLE, index ); 
ascii * ParamStr; 
ParamStr=(ascii *)adl_memGet(length+1);  // Add 1 on to space allocated for terminating 0
rsp=adl_flhRead(FLH_HANDLE,index,length,ParamStr); 
ParamStr[length] = 0;  // Terminate the string

#14

ok thanks that was the problem indeed. Thanks for your help


#15

Yes - the documentation is misleading in this respect - see: wavecom.com/modules/movie/sc … php?p=6103

:unamused: