-2

Short question:

What is the meaning this expression?

if (ptr->exit)

It really looks like it's just asking if the exit value of the struct is true, what I don't understand is that in the senario described below, it seems a false value entered the if condition.

why asking this

I am reading the source code for a shell script for NXP LPC54018 SDK in fsl_shell.c file in utilities folder

The code look like this

int32_t SHELL_Main(p_shell_context_t context)
{

    printf("entered shell_main\n");
    uint8_t ch;
    int32_t i;

    if (!context)
    {
        return -1;
    }

    context->exit = false;
    context->printf_data_func("\r\nSHELL (build: %s)\r\n", __DATE__);
    context->printf_data_func("Copyright (c) 2017 NXP Semiconductor\r\n");
    context->printf_data_func(context->prompt);


    while (1)
    {
        printf("start loop, context = %p\n", context);
        if (context->exit)
        {
            printf("context->exit");
            break;
        }

...

I noticed this SHELL_Main() works directly when called directly from Main.c. However, if I wrap it in xTaskCreate and then run vTaskStartScheduler(), even though the pointer passed in is the same, the exit condition was trigged.

Method A: Called directly from Main.c (works)

SHELL_Main(&context);

console

SHELL (build: Oct 26 2018)

Copyright (c) 2017 NXP Semiconductor

SHELL>> start loop, context = 0x2fec0

Method B: Wrapped by FreeRTOS task, exit condition trigged

xTaskCreate(

SHELL_Main,

"shell",

1024,

&context,

1,

NULL);

vTaskStartScheduler();

console

SHELL (build: Oct 26 2018)

Copyright (c) 2017 NXP Semiconductor

SHELL>> start loop, user_context = 0x2fec0

context->exit

Why the same pointer will trigger different condition?

P.S as suggested by the ODYN-Kon and Ashelly I double checked the exit value the pointer points to, and the result doesn't make sense to me...

while (1)
    {
    printf("start loop, context = %p\n", context);
      printf("context.exit=%s", (context->exit)?"true":"false");

        if (context->exit)
        {
            printf("wth");
            break;
        } 

result

Copyright (c) 2017 NXP Semiconductor
SHELL>> start loop, context = 0x2fec0
context.exit=falsewth
Suicide Bunny
  • 894
  • 1
  • 10
  • 23

1 Answers1

1

exit appears to be a boolean member of p_shell_context_t struct.

if (context->exit) simply means look at value of exit in struct pointed to by context and if it is true, enter the if statement block, otherwise, skip it.

Kon
  • 4,023
  • 4
  • 24
  • 38
  • thanks for the reply. what I don't get is the pointer looks the same to me from the directly called one and the wrapped by FreeRTOS task one, if that's the case, why one triggered the exit condition while the others didn't? – Suicide Bunny Oct 26 '18 at 17:47
  • What happens to `context` before it's passed to those function? How is it created? – Kon Oct 26 '18 at 17:50
  • because `context` is a pointer provided by the FreeRTOS task, which means that the task manager can change the values it points to. – AShelly Oct 26 '18 at 17:50
  • i created in Main, and passed the same one into the SHELL_Main() and xTaskCreate( SHELL_Main, ...). To verify it's the same pointer, I added the printf for %p. It looks the same to me. – Suicide Bunny Oct 26 '18 at 17:52
  • @Ashelly, that's not true, FreeRTOS doesn't have `p_shell_context_t`. It appears to be a parameter that is passed by some other library – Kon Oct 26 '18 at 17:52
  • @Ashelly it's the 4th param passed into xTaskCreate() – Suicide Bunny Oct 26 '18 at 17:53
  • @ODYN-Kon I added checker printf in the end. The execution didn't seem to reach that far (i.e. the context was not modified later, it looks like it was terminated right at the entrance) – Suicide Bunny Oct 26 '18 at 17:56
  • So how are you initializing `context`? – AShelly Oct 26 '18 at 17:57
  • I called the SHELL_Init() function and passed the context into it. Initiated there. – Suicide Bunny Oct 26 '18 at 17:59
  • I mean the pointer itself should have no problem. It works fine if I am just running SHELL_Main() in Main. – Suicide Bunny Oct 26 '18 at 18:00
  • It's possible that SHELL_Init() set's the `exit` flag in context. – Kon Oct 26 '18 at 18:01
  • Good point. I am gonna check whether the exit value the pointer points to is the same – Suicide Bunny Oct 26 '18 at 18:02
  • here's the weird thing, in both cases context->exit is false, however, in the case of the task wrap, it somehow entered the if condition... – Suicide Bunny Oct 26 '18 at 18:06
  • ill added the result into the question cause i don't know how to format here – Suicide Bunny Oct 26 '18 at 18:07
  • I run again and this time it is true. It's probably a flag set by SHELL_Init(). Thanks for the discussion! Very helpful – Suicide Bunny Oct 26 '18 at 18:36