void __attribute__((weak)) thread_init(const thread_config_t* config)
{
otError error;
PlatformInit(0, NULL);
m_ot_instance = otInstanceInitSingle();
ASSERT(m_ot_instance != NULL);
NRF_LOG_INFO("Thread version: %s", otGetVersionString());
NRF_LOG_INFO("Network name: %s",
otThreadGetNetworkName(m_ot_instance));
if (!otDatasetIsCommissioned(m_ot_instance) || config->autocommission)
{
error = otLinkSetChannel(m_ot_instance, config->channel);
ASSERT(error == OT_ERROR_NONE);
NRF_LOG_INFO("Thread Channel: %d", otLinkGetChannel(m_ot_instance));
error = otLinkSetPanId(m_ot_instance, config->panid);
ASSERT(error == OT_ERROR_NONE);
NRF_LOG_INFO("Thread PANID: 0x%lx", (uint32_t)otLinkGetPanId(m_ot_instance));
}
otLinkModeConfig mode;
memset(&mode, 0, sizeof(mode));
if (config->sed) {
// sleepy end device
mode.mSecureDataRequests = true;
mode.mRxOnWhenIdle = false; // Join network as SED.
otLinkSetPollPeriod(m_ot_instance, config->poll_period);
}
else {
// regular end device
mode.mRxOnWhenIdle = true;
mode.mSecureDataRequests = true;
mode.mDeviceType = true;
mode.mNetworkData = true;
}
error = otThreadSetLinkMode(m_ot_instance, mode);
ASSERT(error == OT_ERROR_NONE);
otThreadSetChildTimeout(m_ot_instance, config->child_period);
error = otIp6SetEnabled(m_ot_instance, true);
ASSERT(error == OT_ERROR_NONE);
if (otDatasetIsCommissioned(m_ot_instance) || config->autocommission)
{
error = otThreadSetEnabled(m_ot_instance, true);
ASSERT(error == OT_ERROR_NONE);
NRF_LOG_INFO("Thread interface has been enabled.");
NRF_LOG_INFO("802.15.4 Channel: %d", otLinkGetChannel(m_ot_instance));
NRF_LOG_INFO("802.15.4 PAN ID: 0x%04x", otLinkGetPanId(m_ot_instance));
NRF_LOG_INFO("rx-on-when-idle: %s", otThreadGetLinkMode(m_ot_instance).mRxOnWhenIdle ?
"enabled" : "disabled");
}
otSetStateChangedCallback(m_ot_instance, thread_state_changed_callback, m_ot_instance);
}