Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenThread API: How to get child table using OpenThread API? #5259

Closed
jakubstandarski opened this issue Jul 17, 2020 · 11 comments
Closed

OpenThread API: How to get child table using OpenThread API? #5259

jakubstandarski opened this issue Jul 17, 2020 · 11 comments

Comments

@jakubstandarski
Copy link

Hello Guys!
I am not sure whether it is a good place to ask such a question nevertheless I hope you will help :)
I have the OpenThread CLI application and I would like to create UDP datagram with the payload consisted of child table information.
So my question is: how to get child table info and which API should I use?
I have done some research, and I consider to play with otThreadGetChildInfoByIndex() placed in for/while loop. Does it make sense??

Best regards!

@abtink
Copy link
Member

abtink commented Jul 17, 2020

@BusKetZz yes. That would work. You can use otThreadGetMaxAllowedChildren() to get max number of chidlren.
The ncp_base code for CHILD_TABLE property getter can be good example:

template <> otError NcpBase::HandlePropertyGet<SPINEL_PROP_THREAD_CHILD_TABLE>(void)
{
    otError     error = OT_ERROR_NONE;
    otChildInfo childInfo;
    uint16_t    maxChildren;

    maxChildren = otThreadGetMaxAllowedChildren(mInstance);

    for (uint16_t index = 0; index < maxChildren; index++)
    {
        if ((otThreadGetChildInfoByIndex(mInstance, index, &childInfo) != OT_ERROR_NONE) || childInfo.mIsStateRestoring)
        {
            continue;
        }

        SuccessOrExit(error = mEncoder.OpenStruct());
        SuccessOrExit(error = EncodeChildInfo(childInfo));
        SuccessOrExit(error = mEncoder.CloseStruct());
    }

exit:
    return error;
}

Or you can check the CLI Interpreter::ProcessChild() implementation (almost the same pattern).

@jakubstandarski
Copy link
Author

jakubstandarski commented Jul 17, 2020

@abtink
Thank you very much.
So this is my idea:

static void get_child_table(otInstance *instance, otChildInfo *child_table, uint16_t max_children)
{
    for (uint16_t child_index = 0; child_index < max_children; child_index++) {
        if (otThreadGetChildInfoByIndex(instance, child_index, child_table[child_index]) == OT_ERROR_NOT_FOUND) {
              break;
        }
    }
}

Would it work?

@abtink
Copy link
Member

abtink commented Jul 17, 2020

I think you want to go through all entries (entire range of indexes) and skip over the ones that are not found, i.e., do cannot break when entry for an index is not in use as the entries may be freed in any order.

@jakubstandarski
Copy link
Author

So let's modify it a little bit:

static void get_child_table(otInstance *instance, otChildInfo *child_table, uint16_t max_children)
{
    for (uint16_t child_index = 0; child_index < max_children; child_index++) {
        otThreadGetChildInfoByIndex(instance, child_index, child_table[child_index])             
    }
}

This way, I can form UDP payload consisting of an array of otChildInfo type members.

@abtink
Copy link
Member

abtink commented Jul 17, 2020

@BusKetZz , you want to follow the pattern I mentioned in the first reply, i.e. go through all indexes, skip over the ones that otThreadGetChildInfoByIndex() returns an error.

@jakubstandarski
Copy link
Author

@abtink
So my idea is that:

static void get_child_table(otInstance *instance, otChildInfo *child_table, uint16_t max_children)
{
    otChildInfo child_info;    

    for (uint16_t child_index = 0; child_index < max_children; child_index++) {
        if (otThreadGetChildInfoByIndex(instance, child_index, child_info) == OT_ERROR_NONE) {
             *child_table = child_info;
              child_table++;
        }             
    }
}

Forgive me that I show and ask for help while prototyping, but I think this is the best place to do that :)

@jakubstandarski
Copy link
Author

jakubstandarski commented Jul 20, 2020

I have additional problem with the build process:

openthread/examples/apps/cli/main.c:228: undefined reference to `otThreadGetMaxAllowedChildren'

Similary the same issue with otThreadGetChildInfoByIndex():

openthread/examples/apps/cli/main.c:204: undefined reference to `otThreadGetChildInfoByIndex'

Should I add some files into build process? As I see, file implementing this API is included into compilation so I do not understand why it is not working.

I have added -DOPENTHREAD_FTD=1 flag but it does not change anything, error still occurs.

@jwhui
Copy link
Member

jwhui commented Jul 20, 2020

@BusKetZz , the otThreadGetChildInfoByIndex() is only available for FTD builds.

You should either:

  1. Disable MTD and RCP in your build options, or

  2. Wrap your child table code with #if OPENTHREAD_FTD.

Again, you can refer to the CLI code for example usage.

@jakubstandarski
Copy link
Author

@jwhui
Thank for your answer. Could you tell me how to disable MTD and RCP from build?

@jwhui
Copy link
Member

jwhui commented Jul 21, 2020

@jwhui
Thank for your answer. Could you tell me how to disable MTD and RCP from build?

@BusKetZz , if you are using the convenience makefiles in examples/, you can look for configure_OPTIONS:

configure_OPTIONS = \
--enable-cli \
--enable-ftd \
--enable-mtd \
--enable-ncp \
--enable-radio-only \
--enable-linker-map \
--with-examples=cc2538 \
$(NULL)

Remove mentions of --enable-mtd and --enable-radio-only.

@jakubstandarski
Copy link
Author

jakubstandarski commented Jul 23, 2020

Thank you very much for all the information. I have solved my issue by adding #ifdef OPENTHREAD_FTD directive before the code, which uses otThreadGetChildInfoByIndex() and otThreadGetMaxAllowedChildren() function calls.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants