0

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;
}
David Collins
  • 2,852
  • 9
  • 13
Klyyde
  • 1
  • 3
  • 3
    Can you show us `parse_cmd_line()`? – David Collins Jun 21 '18 at 14:11
  • i edit the post with the function – Klyyde Jun 21 '18 at 14:15
  • 2
    `temperature` is over 10 characters. The `if` never runs and the end if your word is sqeezed. – Umaiki Jun 21 '18 at 14:15
  • Try: `else printf("\nCommand [%s] unknown !", word[0]);` to see what you're getting that's not comparing equal to `"temperature"`. – Mike Housky Jun 21 '18 at 14:16
  • 1
    `strcmp()` comes from standard C lib, it is reliable. Just print your array `word[]` to see the content. Be careful with space, tab, and etc. – Joy Allen Jun 21 '18 at 14:18
  • 1
    The use of `volatile` here generates a [pretty bad code smell](https://en.wikipedia.org/wiki/Code_smell). – Andrew Henle Jun 21 '18 at 14:19
  • @Umaiki: It looks like Klyyde identified the problem, and that `parse_cmd_line()` will fail if its argument has length > 10 characters. – David Collins Jun 21 '18 at 14:19
  • You should copy your `cmdLine` variable into `startOfWord`. Because when you execute this line `(*cmdLine) += wordLenght + 1;`. `startOfWord` is still pointing to `cmdLine` ... – Umaiki Jun 21 '18 at 16:19
  • @MikeHousky yeah you're right it returns me : Command [(null)] unknown !command received : |(null)|(null)|(null)| – Klyyde Jun 21 '18 at 20:26
  • @Umaiki when i copy cmdLine into startOfWord my commands are not working. It returns like if i havn't wrote something : "Command [] unknown !command received : |(null)|(null)|(null)|" – Klyyde Jun 21 '18 at 21:00
  • ok i just change the value of `wordLenght >= 10` into `wordLenght >= 20` and it seems to work and same for `wordLenght < 10` – Klyyde Jun 21 '18 at 21:05
  • first feeling always need a confirmation ;) – Umaiki Jun 22 '18 at 07:24

1 Answers1

-1

May you can use strtok() to replace parse_cmd_line(). strtok() is a function in standard C lib, which can split string for you.

See how to use strtok(), Just type man strtok

Joy Allen
  • 402
  • 3
  • 8