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

Calling CLI lists without console

Hi,

In the Thread API under the Command Line Interface section, there are the following functions:

void otCliConsoleInit(otInstance *aInstance, otCliConsoleOutputCallback aCallback, void *aContext)

void otCliConsoleInputLine(char *aBuf, uint16_t aBufLength)	

I'm assuming these functions can be used to manually input in main() an OpenThread command from the list shown here and log the output response without using the CLI console?

I hope someone can shed some light on this.

  • Hello Roger!

    Yes you are right! What is more, there is no requirement to run otCliUartInit simultanously (but it is possible).

    I have just run following code:

    otCliConsoleInit(m_app.p_ot_instance, console_output, NULL);
    
    char command[] = "help\n";
    otCliConsoleInputLine(command, strlen(command));
    

    The result (whole list of commands) is passed then to console_output function as a non-null terminated string and its size.

    Give me know if you need more details on this feature.

  • Hi Lukasz,

    Thanks for your reply, it is very much appreciated.

    I'm having some difficulties running the code for a while now.

    How does one pass the second argument console_output to typedef int(* otCliConsoleOutputCallback)(const char * aBuf, uint16_t aBufLength, void * aContext)?

    I tried the following: otCliConsoleInit(m_app.p_ot_instance, (otCliConsoleOutputCallback)console_output, NULL) where I declared console_output as a character array. I then tried to print console_output, but with no success. At the moment, my code executes when a key from SEGGER RTT viewer is detected.

  • Hello,

    I have just modified CLI example from our nRF5 SDK for Thread v0.10.

    You can modify main section of the main.c to print CLI output on RTT:

    int console_output(const char *p_buf, uint16_t buf_length, void *p_context)
    {
    	(void)p_context;
    
    	// We need to ensure that p_buf is null terminated string in order to use NRF_LOG_INFO
    	// macro to print it.
    	// Currently OT always return p_buf as null-terminated string so below lines could
    	// be removed. But there is possibility that this will change over time.
    	char str[512];
    	memcpy(str, p_buf, buf_length);
    	str[buf_length] = 0;
    	
    	NRF_LOG_INFO("CLI Output: %s\r\n", (uint32_t)str);
    	
    	return buf_length;
    }
     
    int main(int argc, char *argv[])
    {
        NRF_LOG_INIT(NULL);
    
        thread_init();
    
        timer_init();
        thread_bsp_init();
        leds_init();
    
    	otCliConsoleInit(m_app.p_ot_instance, console_output, NULL);
    
    	char command[] = "state\n";
    	otCliConsoleInputLine(command, strlen(command));
    
        while (true)
        {
            otTaskletsProcess(m_app.p_ot_instance);
            PlatformProcessDrivers(m_app.p_ot_instance);
        }
    }
    
  • Hello Lukasz,

    Many thanks for the detailed description. It works very well.

    One more thing I noticed when trying different CLI commands is when I give the following command, for example:

    otCliConsoleInit(m_app.p_ot_instance, console_output, NULL);
    
    c = SEGGER_RTT_GetKey();
        if (c == 'a')
        {
              SEGGER_RTT_WriteString(0, "Get key character 'a' pressed\r\n");
              char command[] = "ping FF03::1\n";
              // void otCliConsoleInputLine(char * aBuf, uint16_t aBufLength)
              // This method is called to feed in a console input line
              otCliConsoleInputLine(command, strlen(command));
        }
    

    I'm able to ping the node at first, but the MCU freezes after the key was pressed. Any ideas why this might be happening?

  • Hi Lukasz,

    Got it working for the ping command by not initialising otCliUartInit(p_instance).

    Thanks again for all your help!

Related