This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

cli

Hi,

I am using cli and made my own command like bellow, as you see i have a command with 3 sub commands. But i think my idea is primitive because if i have 1000 sub command so i have to write many functions!!!!

Another problem of using this method is that in putty first i have to write N1 as a sub command and in the next line SEND as a command and then i can see my results. For example :

uart_cli:~$   Do A

uart_cli:~$   DO

But i want  to type like  "SEND N1'' which is contain command and sub command and then get my results. Kindly let me how to do that and let me know if it is possible, how many sub command i can type with that? is there any limitation? For example can i have something like bellow?

uart_cli:~$  DO A L g H Z K

which A, L,H,Z,K are sub commands of DO.

//sub commands
static void cmd_DO_A(nrf_cli_t const * p_cli, size_t argc, char **argv)
      { 
           A = 1;    
      }

static void cmd_DO_B(nrf_cli_t const * p_cli, size_t argc, char **argv)
      { 
          A = 2;
      }

static void cmd_DO_C(nrf_cli_t const * p_cli, size_t argc, char **argv)
      { 
            A = 3;
      }
// Command
static void cmd_DO(nrf_cli_t const * p_cli, size_t argc, char **argv)
{ 
      ASSERT(p_cli);
      //
      //
      //
      //

      switch(A)
      {
        case 1:
                
                break;
         case 2:
             
                break;
         case 3:
                
                break;

  • Hi,

    Please see the bellow code. this is what i mean. Because i think the problem is in Sub commands definitions.I will appreciate if you give me more guidance.

    Kind regards 

    uint8_t     A= 0;
    
    //sub commands
    static void DO_A (nrf_cli_t const * p_cli, size_t argc, char **argv)
          { 
              A = 1;    
          }
    
    static void DO_B (nrf_cli_t const * p_cli, size_t argc, char **argv)
          { 
              A = 2;
          }
    
    static void DO_C (nrf_cli_t const * p_cli, size_t argc, char **argv)
          { 
              A = 3;
          }
    static char m_dynamic_cmd_buffer[][20] = {"Do_A", "Do_B", "Do_C"};
    
    // Command
    static void cmd_send(nrf_cli_t const * p_cli, size_t argc, char **argv)
    {
        for (size_t i = 1; i < argc; i++)
        {
        	if (!strcmp(argv[i], m_dynamic_cmd_buffer[i]))
        		{
        		//
        		//
        		//
                  switch(A)
                   {
                    case 1:
                        data[0] = 0x19;
                            
                            break;
                     case 2:
                            data[0] = 0x20;
                            
                            break;
                    }
                }    
        }                    
    }
    
    static void dynamic_cmd_get(size_t idx, nrf_cli_static_entry_t * p_static)
    {
        ASSERT(p_static);
    
        if (idx < m_dynamic_cmd_cnt)
        {
            /* m_dynamic_cmd_buffer must be sorted alphabetically to ensure correct CLI completion */
            p_static->p_syntax = m_dynamic_cmd_buffer[idx];
            p_static->handler  = NULL;
            p_static->p_subcmd = NULL;
            p_static->p_help = NULL;
        }
        else
        {
            /* if there are no more dynamic commands available p_syntax must be set to NULL */
            p_static->p_syntax = NULL;
        }
    }
    
    
    NRF_CLI_CREATE_DYNAMIC_CMD(m_sub_dynamic_set, dynamic_cmd_get);
    NRF_CLI_CMD_REGISTER(Do,
                         &m_sub_dynamic_set,
                         "Demonstrate dynamic command usage.",
                         cmd_Do);      

  • if i have 1000 sub command so i have to write many functions!!!!

    Obviously, if you have lots of commands, you are going to have to write lots of code to handle them all! There's no escaping that!

    I think the Nordic CLI is really just for the purpose of simple demos.

    If you really do need a complex, extensive UI, then you are going to have to do a proper, full design yourself.

    This is a common task, not specific to Nordic - so you should be able to find plenty of general solutions, libraries, etc ...

  • Thank for answering awneil. But could plz let me know about error in line18?

    conflicting types for 'm_dynamic_cmd_buffer'

  • error in line18

    I have no idea what you're talking about!

    I don't see that mentioned anywhere else in this thread!

  • please see this code, im using the same concept which jacub said but i have an error for conflicting types for 'm_dynamic_cmd_buffer'. what is the problem?

    uint8_t     A= 0;
    
    //sub commands
    static void DO_A (nrf_cli_t const * p_cli, size_t argc, char **argv)
          { 
              A = 1;    
          }
    
    static void DO_B (nrf_cli_t const * p_cli, size_t argc, char **argv)
          { 
              A = 2;
          }
    
    static void DO_C (nrf_cli_t const * p_cli, size_t argc, char **argv)
          { 
              A = 3;
          }
    static char m_dynamic_cmd_buffer[][20] = {"Do_A", "Do_B", "Do_C"};
    
    // Command
    static void cmd_send(nrf_cli_t const * p_cli, size_t argc, char **argv)
    {
        for (size_t i = 1; i < argc; i++)
        {
        	if (!strcmp(argv[i], m_dynamic_cmd_buffer[i]))
        		{
        		//
        		//
        		//
                  switch(A)
                   {
                    case 1:
                        data[0] = 0x19;
                            
                            break;
                     case 2:
                            data[0] = 0x20;
                            
                            break;
                    }
                }    
        }                    
    }
    
    static void dynamic_cmd_get(size_t idx, nrf_cli_static_entry_t * p_static)
    {
        ASSERT(p_static);
    
        if (idx < m_dynamic_cmd_cnt)
        {
            /* m_dynamic_cmd_buffer must be sorted alphabetically to ensure correct CLI completion */
            p_static->p_syntax = m_dynamic_cmd_buffer[idx];
            p_static->handler  = NULL;
            p_static->p_subcmd = NULL;
            p_static->p_help = NULL;
        }
        else
        {
            /* if there are no more dynamic commands available p_syntax must be set to NULL */
            p_static->p_syntax = NULL;
        }
    }
    
    
    NRF_CLI_CREATE_DYNAMIC_CMD(m_sub_dynamic_set, dynamic_cmd_get);
    NRF_CLI_CMD_REGISTER(Do,
                         &m_sub_dynamic_set,
                         "Demonstrate dynamic command usage.",
                         cmd_Do);      
     

Related