i'm actually on a p2p chord node project and i have a problem when i try to create some command to collect information like temperature, light, id etc..
static void handle_cmd(volatile char* cmdLine)
{
int i = 0;
volatile char* word[500] = {NULL, NULL, NULL};
while((word[i] = parse_cmd_line(&cmdLine, ' ')) != NULL )
{
//printf("word[%i] = %s\n", i, word[i]);
i++;
}
//Analyse the 3 words :
if(strcmp(word[0], "help") == 0)
{
print_usage();
}
else if(strcmp(word[0], "print") == 0)
{
printf("test print...\n");
}
else if(strcmp(word[0], "temperature") == 0)
{
printf("temperature ");
temperature_sensor();
}
else if(strcmp(word[0], "id") == 0)
{
serial_number();
}
/*else if(strcmp(word[0], "light") == 0)
{
light_sensor();
}*/
else if(strcmp(word[0], "search") == 0)
{
//search_key(arg[1]);
}
else if(strcmp(word[0], "send") == 0)
{
if(strlen(word[1]) == 4)
{
printf("sending %s to %x", word[2], strtoul(word[1], NULL, 16));
send_packet(strtoul(word[1], NULL, 16), word[2]);
}
}
else
{
printf("\nCommand unknown !");
}
printf("command received : |%s|%s|%s|\n", word[0], word[1], word[2]);
}
It works for help
, print
and id
but not for temperature
. I don't know if it's a problem with a max length of strcmp()
or a problem with the size of my array word[]
but when i try with temp
or temper
it works.
i would like to create bigger command in the future like "send xxx to xxx" . Thanks !
Edit : here is parse_cmd_line()
.
volatile char* parse_cmd_line(volatile char** cmdLine, char separator) {
volatile char* startOfWord = *cmdLine;
int wordLenght = 0;
while (((*cmdLine)[wordLenght] != separator) &&
((*cmdLine)[wordLenght] != '\0') && (wordLenght < 10))
wordLenght++;
// printf("\nwordLenght = %i", wordLenght);
// printf("\nlettre = %c", (*cmdLine)[wordLenght]);
// If no word was found, return NULL
if ((wordLenght == 0) ||
((wordLenght >= 10) &&
(((*cmdLine)[wordLenght] != '\0') || ((*cmdLine)[wordLenght] != ' '))))
return NULL;
// else cut the word and return it
startOfWord[wordLenght] = '\0';
(*cmdLine) += wordLenght + 1;
return startOfWord;
}