• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2016, The OpenThread Authors.
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *  1. Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *  2. Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *  3. Neither the name of the copyright holder nor the
13  *     names of its contributors may be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *  POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /**
30  * @file
31  *   This file implements the OpenThread Instance API.
32  */
33 
34 #include "openthread-core-config.h"
35 
36 #include <openthread/instance.h>
37 #include <openthread/platform/misc.h>
38 
39 #include "common/new.hpp"
40 #include "instance/instance.hpp"
41 
42 #if !defined(OPENTHREAD_BUILD_DATETIME)
43 #ifdef __ANDROID__
44 #ifdef OPENTHREAD_CONFIG_ANDROID_NDK_ENABLE
45 #include <sys/system_properties.h>
46 #else
47 #include <cutils/properties.h>
48 #endif
49 #else // __ANDROID__
50 #if defined(__DATE__)
51 #define OPENTHREAD_BUILD_DATETIME __DATE__ " " __TIME__
52 #endif
53 #endif // __ANDROID__
54 #endif // !defined(OPENTHREAD_BUILD_DATETIME)
55 
56 using namespace ot;
57 
58 #if OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
59 #if OPENTHREAD_CONFIG_MULTIPLE_STATIC_INSTANCE_ENABLE
otInstanceInitMultiple(uint8_t aIdx)60 otInstance *otInstanceInitMultiple(uint8_t aIdx)
61 {
62     Instance *instance;
63 
64     instance = Instance::InitMultiple(aIdx);
65 
66     return instance;
67 }
68 #endif // OPENTHREAD_CONFIG_MULTIPLE_STATIC_INSTANCE_ENABLE
otInstanceInit(void * aInstanceBuffer,size_t * aInstanceBufferSize)69 otInstance *otInstanceInit(void *aInstanceBuffer, size_t *aInstanceBufferSize)
70 {
71     Instance *instance;
72 
73     instance = Instance::Init(aInstanceBuffer, aInstanceBufferSize);
74 
75     return instance;
76 }
77 #else
otInstanceInitSingle(void)78 otInstance *otInstanceInitSingle(void) { return &Instance::InitSingle(); }
79 #endif // #if OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
80 
otInstanceGetId(otInstance * aInstance)81 uint32_t otInstanceGetId(otInstance *aInstance) { return AsCoreType(aInstance).GetId(); }
82 
otInstanceIsInitialized(otInstance * aInstance)83 bool otInstanceIsInitialized(otInstance *aInstance)
84 {
85 #if OPENTHREAD_MTD || OPENTHREAD_FTD
86     return AsCoreType(aInstance).IsInitialized();
87 #else
88     OT_UNUSED_VARIABLE(aInstance);
89     return true;
90 #endif // OPENTHREAD_MTD || OPENTHREAD_FTD
91 }
92 
otInstanceFinalize(otInstance * aInstance)93 void otInstanceFinalize(otInstance *aInstance) { AsCoreType(aInstance).Finalize(); }
94 
otInstanceReset(otInstance * aInstance)95 void otInstanceReset(otInstance *aInstance) { AsCoreType(aInstance).Reset(); }
96 
97 #if OPENTHREAD_CONFIG_PLATFORM_BOOTLOADER_MODE_ENABLE
otInstanceResetToBootloader(otInstance * aInstance)98 otError otInstanceResetToBootloader(otInstance *aInstance) { return AsCoreType(aInstance).ResetToBootloader(); }
99 #endif
100 
101 #if OPENTHREAD_CONFIG_UPTIME_ENABLE
otInstanceGetUptime(otInstance * aInstance)102 uint64_t otInstanceGetUptime(otInstance *aInstance) { return AsCoreType(aInstance).Get<Uptime>().GetUptime(); }
103 
otInstanceGetUptimeAsString(otInstance * aInstance,char * aBuffer,uint16_t aSize)104 void otInstanceGetUptimeAsString(otInstance *aInstance, char *aBuffer, uint16_t aSize)
105 {
106     AssertPointerIsNotNull(aBuffer);
107 
108     AsCoreType(aInstance).Get<Uptime>().GetUptime(aBuffer, aSize);
109 }
110 #endif
111 
112 #if OPENTHREAD_MTD || OPENTHREAD_FTD
otSetStateChangedCallback(otInstance * aInstance,otStateChangedCallback aCallback,void * aContext)113 otError otSetStateChangedCallback(otInstance *aInstance, otStateChangedCallback aCallback, void *aContext)
114 {
115     return AsCoreType(aInstance).Get<Notifier>().RegisterCallback(aCallback, aContext);
116 }
117 
otRemoveStateChangeCallback(otInstance * aInstance,otStateChangedCallback aCallback,void * aContext)118 void otRemoveStateChangeCallback(otInstance *aInstance, otStateChangedCallback aCallback, void *aContext)
119 {
120     AsCoreType(aInstance).Get<Notifier>().RemoveCallback(aCallback, aContext);
121 }
122 
otInstanceFactoryReset(otInstance * aInstance)123 void otInstanceFactoryReset(otInstance *aInstance) { AsCoreType(aInstance).FactoryReset(); }
124 
otInstanceErasePersistentInfo(otInstance * aInstance)125 otError otInstanceErasePersistentInfo(otInstance *aInstance) { return AsCoreType(aInstance).ErasePersistentInfo(); }
126 #endif // OPENTHREAD_MTD || OPENTHREAD_FTD
127 
128 #if OPENTHREAD_RADIO
otInstanceResetRadioStack(otInstance * aInstance)129 void otInstanceResetRadioStack(otInstance *aInstance) { AsCoreType(aInstance).ResetRadioStack(); }
130 #endif
131 
otGetVersionString(void)132 const char *otGetVersionString(void)
133 {
134     /**
135      * PLATFORM_VERSION_ATTR_PREFIX and PLATFORM_VERSION_ATTR_SUFFIX are
136      * intended to be used to specify compiler directives to indicate
137      * what linker section the platform version string should be stored.
138      *
139      * This is useful for specifying an exact location of where the version
140      * string will be located so that it can be easily retrieved from the
141      * raw firmware image.
142      *
143      * If PLATFORM_VERSION_ATTR_PREFIX is unspecified, the keyword `static`
144      * is used instead.
145      *
146      * If both are unspecified, the location of the string in the firmware
147      * image will be undefined and may change.
148      */
149 
150 #if !defined(OPENTHREAD_BUILD_DATETIME) && defined(__ANDROID__)
151 
152 #ifdef OPENTHREAD_CONFIG_ANDROID_NDK_ENABLE
153     static char sVersion[100 + PROP_VALUE_MAX];
154     char        dateTime[PROP_VALUE_MAX];
155 
156     __system_property_get("ro.build.date", dateTime);
157 #else
158     static char sVersion[100 + PROPERTY_VALUE_MAX];
159     char        dateTime[PROPERTY_VALUE_MAX];
160 
161     property_get("ro.build.date", dateTime, "Thu Jan 1 1970 UTC 00:00:00");
162 #endif
163 
164     snprintf(sVersion, sizeof(sVersion), "%s/%s ;%s ; %s", PACKAGE_NAME, PACKAGE_VERSION,
165              OPENTHREAD_CONFIG_PLATFORM_INFO, dateTime);
166 #else
167 
168 #ifdef PLATFORM_VERSION_ATTR_PREFIX
169     PLATFORM_VERSION_ATTR_PREFIX
170 #else
171     static
172 #endif
173     const char sVersion[] = PACKAGE_NAME "/" PACKAGE_VERSION "; " OPENTHREAD_CONFIG_PLATFORM_INFO
174 #ifdef OPENTHREAD_BUILD_DATETIME
175                                          "; " OPENTHREAD_BUILD_DATETIME
176 #endif
177 #ifdef PLATFORM_VERSION_ATTR_SUFFIX
178                                              PLATFORM_VERSION_ATTR_SUFFIX
179 #endif
180         ; // Trailing semicolon to end statement.
181 
182 #endif
183 
184     return sVersion;
185 }
186 
otGetRadioVersionString(otInstance * aInstance)187 const char *otGetRadioVersionString(otInstance *aInstance)
188 {
189     return AsCoreType(aInstance).Get<Radio>().GetVersionString();
190 }
191