Commissioner stuck in petitioning when started by API call

Hi,

I can successfully start commissioning role when I use cli and the leader gets the commissioner active state. But when I try the same using API all, the leader gets stuck in the petition state. Following is what I did in my software.

// custom cli command to start commissioning
otError cli_cmsn (void *aContext, uint8_t aArgsLength, char *aArgs[]) {
	otOperationalDataset aDataset;
	otError error;
	
	otInstance *instance = openthread_get_default_instance();
	if (instance == NULL) {
		otCliOutputFormat ("get instantace  error");
		return OT_ERROR_FAILED;
	}

	otCliOutputFormat ("\n\r ifconfig down ");
	error = otIp6SetEnabled(instance, false);	// ifconfig down
	if (error != OT_ERROR_NONE) {
		otCliOutputFormat ("ifconfig down error");
		return (error);
	}

	otCliOutputFormat ("\n\r Thread stop ");
	error = otThreadSetEnabled (instance, false);	// thread stop
	if (error != OT_ERROR_NONE) {
		otCliOutputFormat ("thread stop error");
		return (error);
	}

	otCliOutputFormat ("\n\rCreate new dataset ");
	error = otDatasetCreateNewNetwork(instance, &aDataset);	// create new dataset
	if (error != OT_ERROR_NONE) {
		otCliOutputFormat ("create new dataset error");
		return (error);
	}

	otCliOutputFormat ("\n\r active new dataset ");
	error = otDatasetSetActive(instance, &aDataset);	// active new dataset
	if (error != OT_ERROR_NONE) {
		otCliOutputFormat ("active new dataset error");
		return (error);
	}

	otCliOutputFormat ("\n\r ifconfig up ");
	error = otIp6SetEnabled(instance, true);	// ifconfig up
	if (error != OT_ERROR_NONE) {
		otCliOutputFormat ("ifconfig up error");
		return (error);
	}

	otCliOutputFormat ("\n\r thread start ");
	error = otThreadSetEnabled (instance, true); // thread start
	if (error != OT_ERROR_NONE) {
		otCliOutputFormat ("thread start error");
		return (error);
	}

	// spawn a thread to handle wait to connect to network
	k_tid_t my_tid = k_thread_create(&cmsn_thread_data, cmsn_stack_area,
                                 K_THREAD_STACK_SIZEOF(cmsn_stack_area),
                                 cmsn_handler,
                                 NULL, NULL, NULL,
                                 CMSN_THREAD_PRIORITY, 0, K_NO_WAIT);
	return (OT_ERROR_NONE);
}

// Thread handler to busy wait ot role change
void cmsn_handler (void *, void *, void *) {
	otError error;
	
	while (ot_connected == true) {
		otCliOutputFormat (".");
		k_msleep (500);
	}
	while (ot_connected == false) {
		otCliOutputFormat (".");
		k_msleep (500);
	}
	
	otInstance *instance = openthread_get_default_instance();
	
	otCliOutputFormat ("\n\r commissioner start ");
	error = otCommissionerStart (instance, aStateCallback, aJoinerCallback, NULL);
	if (error != OT_ERROR_NONE) {
		otCliOutputFormat ("commissioner start error %d\n\r", error);
		return (error);
	}
}

I use a thread to busy wait till thread instance is back in router or leader role. The bool 'ot_connected' will be true when the role changes to router/leader.

I have only one device (leader) in this network. I have waited till device role becomes leader as well, but same result. But if I manually start using cli command 'commissioner start', it works.

How to solve this?

Cheers,

Kaushalya

Parents Reply
  • Hi Charlie, 

    Thanks, I have to following in my prj.conf

    CONFIG_OPENTHREAD_FTD=y
    CONFIG_OPENTHREAD_COMMISSIONER=y
    This is how I got the commissioner cli command got enabled. I cant see anything like OPENTHREAD_CONFIG_COMMISSIONER_ENABLE in my .config. I presume the CONFIG_OPENTHREAD_COMMISSIONER is same.
    Also I can use CLI to get the commissioner role to active without any problem, only API control has this issue.
    Cheers,
    Kaushalya
Children
  • After having a discussion with OpenThread git forum (thanks Jonathan ***), I managed to solve the issue.  OT API are not thread safe. So the issue was I invoked the otCommissionerStart () function from within a separate thread. The solution was to register a call back to the openthread_set_state_changed_cb() and let that call back invoke the otCommissionerStart ().

    I just noted it here in case someone else falls into the same pit. 

    Cheers,

    Kaushalya

Related