otSetStateChangedCallback () doesnt seem to register the callback.

Hi,

In my app I have been using the following to register my state change callback. This works. Only issue is it seem to only pass ot_instance, I cant pass a user data to the callback. 

openthread_set_state_changed_cb (on_thread_state_changed)
To pass user data, I tried 
error = otSetStateChangedCallback (openthread_get_default_context(), on_thread_state_changed, &aUserData)
The error returned is error_none. But then my callback function, on_thread_state_changed(), is not get invoked. 
my callback signature is 
static void on_thread_state_changed(uint32_t flags, void *context, void *aContex)
I have also tried 
static void on_thread_state_changed(uint32_t flags, void *context)
But both yield the same result, callback not being invoked. 
I can see the thread state changes by cli. 
What is going wrong here?
Cheers,
Kaushalya
  • Hi Kaushalya,

    Are you registering the callback with both openthread_set_state_changed_cb() and otSetStateChangedCallback()? You should only register the callback with one of these.

    What is it you want to achieve by sending user data to the state change callback?

    Please note that openthread_set_state_changed_cb() is deprecated, and you should use openthread_state_changed_cb_register() instead.

    Best regards,
    Marte

  • Hi Marte, Sorry, I accidently closed this thread.

    Are you registering the callback with both openthread_set_state_changed_cb() and otSetStateChangedCallback()?

    No I used one at a time. This is how I first tried the openthread_state_changed_cb_register (...) 

    static void on_thread_state_changed(uint32_t flags, void *context) {
        struct openthread_context *ot_context = context;
        .
        .
    }
    
    void main () {
        .
        .
        struct openthread_state_changed_cb ot_state_change_cb;
        ot_state_change_cb.user_data = &aUserData;
        ot_state_change_cb.state_changed_cb = on_thread_state_changed;
    
        error = openthread_start(openthread_get_default_context());
        openthread_state_changed_cb_register (openthread_get_default_context(), &ot_state_change_cb);
    }

    After failing to invoke the callback, I tried otSetStateChangedCallback(...) as follows.

    static void on_thread_state_changed(uint32_t flags, void *context) {
        struct openthread_context *ot_context = context;
        .
        .
    }
    
    void main () {
    .
    .
    
        error = openthread_start(openthread_get_default_context());
        error = otSetStateChangedCallback (openthread_get_default_context(), on_thread_state_changed, &aUserData);
    }

    Neither seem to work. Only thing seemed to worked was the original openthread_set_state_changed_cb(on_thread_state_changed);

    Also one other thing I am not clear about is how the aUserData is passed in the callback 

    on_thread_state_changed (uint32_t flags, void *context, void *aContex) as aContext is used to pass the ot_context. The 

    ot_context doesnt have any member to carry user data. The typedef of the openthread state change callback function doesnt have any other void * to pass &aUserData.
    I am using SDK 2.3.0.
    Cheers,
    Kaushalya

  • Hi Marte,

    I think I managed to solve it by doing the following. I remember I have tried it like this initially and it didn't work. Anyway, would like to know your opinion about the implementation.

    static void on_thread_state_changed(otChangedFlags flags, struct openthread_context *ot_context, void *user_data) {
        userdata *aUserdata = (userdata*) user_data;
        .
        .
    }
    
    void main () {
        .
        .
    	static struct openthread_state_changed_cb ot_state_change_cb;
    
    	ot_state_change_cb.user_data = &aUserData;
    	ot_state_change_cb.state_changed_cb = on_thread_state_changed;
    	openthread_state_changed_cb_register (openthread_get_default_context(), &ot_state_change_cb);
    
    	error = openthread_start(openthread_get_default_context());
    }

    What I have done is to invoke the openthread_state_changed_cb_register () before I call openthread_start ().

    Also I modified the signature of the callback. This callback seems to take a void *user_data, so I can successfully pass my user_data struct.

    Please let me know if this implementation is acceptable.

    Cheers,

    Kaushalya

  • Hi Kaushalya,

    Marte is currently on vacation so I will pick up this case while they are out of office. I need some time to sync up with the information here, but I will get back to you.

    Kind regards,
    Andreas

  • Hi,

    I've had a look at your solution and discussed it with some of my colleagues and we both agree that what you've done looks like it should be working fine.

    Feel free to raise a new case or ask follow up questions in case you have any new questions

    Kind regards,
    Andreas

Related