Instance API cleanup.

This commit is contained in:
Jonathan Hui
2017-01-18 11:27:20 -08:00
parent da9b2978ad
commit 52497fa45c
16 changed files with 511 additions and 375 deletions
+218
View File
@@ -0,0 +1,218 @@
/*
* Copyright (c) 2016, The OpenThread Authors.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @file
* This file implements the OpenThread Instance API.
*/
#define WPP_NAME "instance_api.tmh"
#include "openthread/instance.h"
#include "platform/misc.h"
#include "platform/settings.h"
#include "openthread-instance.h"
#include "common/logging.hpp"
#include "common/new.hpp"
#ifndef OPENTHREAD_MULTIPLE_INSTANCE
static otDEFINE_ALIGNED_VAR(sInstanceRaw, sizeof(otInstance), uint64_t);
otInstance *sInstance = NULL;
#endif
otInstance::otInstance(void) :
mReceiveIp6DatagramCallback(NULL),
mReceiveIp6DatagramCallbackContext(NULL),
mActiveScanCallback(NULL),
mActiveScanCallbackContext(NULL),
mEnergyScanCallback(NULL),
mEnergyScanCallbackContext(NULL),
mThreadNetif(mIp6)
#if OPENTHREAD_ENABLE_RAW_LINK_API
, mLinkRaw(*this)
#endif // OPENTHREAD_ENABLE_RAW_LINK_API
#if OPENTHREAD_ENABLE_APPLICATION_COAP
, mApplicationCoapServer(mIp6.mUdp, OT_DEFAULT_COAP_PORT)
#endif // OPENTHREAD_ENABLE_APPLICATION_COAP
{
}
using namespace Thread;
#ifdef __cplusplus
extern "C" {
#endif
void otInstancePostConstructor(otInstance *aInstance)
{
// restore datasets and network information
otPlatSettingsInit(aInstance);
aInstance->mThreadNetif.GetMle().Restore();
#if OPENTHREAD_CONFIG_ENABLE_AUTO_START_SUPPORT
// If auto start is configured, do that now
if (otThreadGetAutoStart(aInstance))
{
if (otIp6SetEnabled(aInstance, true) == kThreadError_None)
{
// Only try to start Thread if we could bring up the interface
if (otThreadStart(aInstance) != kThreadError_None)
{
// Bring the interface down if Thread failed to start
otIp6SetEnabled(aInstance, false);
}
}
}
#endif
}
#ifdef OPENTHREAD_MULTIPLE_INSTANCE
otInstance *otInstanceInit(void *aInstanceBuffer, size_t *aInstanceBufferSize)
{
otInstance *aInstance = NULL;
otLogFuncEntry();
otLogInfoApi("otInstanceInit");
VerifyOrExit(aInstanceBufferSize != NULL, ;);
// Make sure the input buffer is big enough
VerifyOrExit(sizeof(otInstance) <= *aInstanceBufferSize, *aInstanceBufferSize = sizeof(otInstance));
VerifyOrExit(aInstanceBuffer != NULL, ;);
// Construct the context
aInstance = new(aInstanceBuffer)otInstance();
// Execute post constructor operations
otInstancePostConstructor(aInstance);
exit:
otLogFuncExit();
return aInstance;
}
#else
otInstance *otInstanceInit()
{
otLogFuncEntry();
otLogInfoApi("otInstanceInit");
VerifyOrExit(sInstance == NULL, ;);
// Construct the context
sInstance = new(&sInstanceRaw)otInstance();
// Execute post constructor operations
otInstancePostConstructor(sInstance);
exit:
otLogFuncExit();
return sInstance;
}
#endif
void otInstanceFinalize(otInstance *aInstance)
{
otLogFuncEntry();
// Ensure we are disabled
(void)otThreadStop(aInstance);
(void)otIp6SetEnabled(aInstance, false);
#ifndef OPENTHREAD_MULTIPLE_INSTANCE
sInstance = NULL;
#endif
otLogFuncExit();
}
ThreadError otSetStateChangedCallback(otInstance *aInstance, otStateChangedCallback aCallback, void *aCallbackContext)
{
ThreadError error = kThreadError_NoBufs;
for (size_t i = 0; i < OPENTHREAD_CONFIG_MAX_STATECHANGE_HANDLERS; i++)
{
if (aInstance->mNetifCallback[i].IsFree())
{
aInstance->mNetifCallback[i].Set(aCallback, aCallbackContext);
error = aInstance->mThreadNetif.RegisterCallback(aInstance->mNetifCallback[i]);
break;
}
}
return error;
}
void otRemoveStateChangeCallback(otInstance *aInstance, otStateChangedCallback aCallback, void *aCallbackContext)
{
for (size_t i = 0; i < OPENTHREAD_CONFIG_MAX_STATECHANGE_HANDLERS; i++)
{
if (aInstance->mNetifCallback[i].IsServing(aCallback, aCallbackContext))
{
aInstance->mThreadNetif.RemoveCallback(aInstance->mNetifCallback[i]);
aInstance->mNetifCallback[i].Free();
break;
}
}
}
void otInstanceReset(otInstance *aInstance)
{
otPlatReset(aInstance);
}
void otInstanceFactoryReset(otInstance *aInstance)
{
otPlatSettingsWipe(aInstance);
otPlatReset(aInstance);
}
ThreadError otInstanceErasePersistentInfo(otInstance *aInstance)
{
ThreadError error = kThreadError_None;
VerifyOrExit(otGetDeviceRole(aInstance) == kDeviceRoleDisabled, error = kThreadError_InvalidState);
otPlatSettingsWipe(aInstance);
exit:
return error;
}
#ifdef __cplusplus
} // extern "C"
#endif