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/as_core_type.hpp"
40 #include "common/locator_getters.hpp"
41 #include "common/new.hpp"
42 #include "radio/radio.hpp"
43
44 #if !defined(OPENTHREAD_BUILD_DATETIME)
45 #ifdef __ANDROID__
46 #ifdef OPENTHREAD_ENABLE_ANDROID_NDK
47 #include <sys/system_properties.h>
48 #else
49 #include <cutils/properties.h>
50 #endif
51 #else // __ANDROID__
52 #if defined(__DATE__)
53 #define OPENTHREAD_BUILD_DATETIME __DATE__ " " __TIME__
54 #endif
55 #endif // __ANDROID__
56 #endif // !defined(OPENTHREAD_BUILD_DATETIME)
57
58 using namespace ot;
59
60 #if OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
otInstanceInit(void * aInstanceBuffer,size_t * aInstanceBufferSize)61 otInstance *otInstanceInit(void *aInstanceBuffer, size_t *aInstanceBufferSize)
62 {
63 Instance *instance;
64
65 instance = Instance::Init(aInstanceBuffer, aInstanceBufferSize);
66
67 return instance;
68 }
69 #else
otInstanceInitSingle(void)70 otInstance *otInstanceInitSingle(void)
71 {
72 return &Instance::InitSingle();
73 }
74 #endif // #if OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
75
otInstanceIsInitialized(otInstance * aInstance)76 bool otInstanceIsInitialized(otInstance *aInstance)
77 {
78 #if OPENTHREAD_MTD || OPENTHREAD_FTD
79 return AsCoreType(aInstance).IsInitialized();
80 #else
81 OT_UNUSED_VARIABLE(aInstance);
82 return true;
83 #endif // OPENTHREAD_MTD || OPENTHREAD_FTD
84 }
85
otInstanceFinalize(otInstance * aInstance)86 void otInstanceFinalize(otInstance *aInstance)
87 {
88 AsCoreType(aInstance).Finalize();
89 }
90
otInstanceReset(otInstance * aInstance)91 void otInstanceReset(otInstance *aInstance)
92 {
93 AsCoreType(aInstance).Reset();
94 }
95
96 #if OPENTHREAD_CONFIG_UPTIME_ENABLE
otInstanceGetUptime(otInstance * aInstance)97 uint64_t otInstanceGetUptime(otInstance *aInstance)
98 {
99 return AsCoreType(aInstance).Get<Uptime>().GetUptime();
100 }
101
otInstanceGetUptimeAsString(otInstance * aInstance,char * aBuffer,uint16_t aSize)102 void otInstanceGetUptimeAsString(otInstance *aInstance, char *aBuffer, uint16_t aSize)
103 {
104 AsCoreType(aInstance).Get<Uptime>().GetUptime(aBuffer, aSize);
105 }
106 #endif
107
108 #if OPENTHREAD_MTD || OPENTHREAD_FTD
otSetStateChangedCallback(otInstance * aInstance,otStateChangedCallback aCallback,void * aContext)109 otError otSetStateChangedCallback(otInstance *aInstance, otStateChangedCallback aCallback, void *aContext)
110 {
111 return AsCoreType(aInstance).Get<Notifier>().RegisterCallback(aCallback, aContext);
112 }
113
otRemoveStateChangeCallback(otInstance * aInstance,otStateChangedCallback aCallback,void * aContext)114 void otRemoveStateChangeCallback(otInstance *aInstance, otStateChangedCallback aCallback, void *aContext)
115 {
116 AsCoreType(aInstance).Get<Notifier>().RemoveCallback(aCallback, aContext);
117 }
118
otInstanceFactoryReset(otInstance * aInstance)119 void otInstanceFactoryReset(otInstance *aInstance)
120 {
121 AsCoreType(aInstance).FactoryReset();
122 }
123
otInstanceErasePersistentInfo(otInstance * aInstance)124 otError otInstanceErasePersistentInfo(otInstance *aInstance)
125 {
126 return AsCoreType(aInstance).ErasePersistentInfo();
127 }
128 #endif // OPENTHREAD_MTD || OPENTHREAD_FTD
129
130 #if OPENTHREAD_RADIO
otInstanceResetRadioStack(otInstance * aInstance)131 void otInstanceResetRadioStack(otInstance *aInstance)
132 {
133 AsCoreType(aInstance).ResetRadioStack();
134 }
135 #endif
136
otGetVersionString(void)137 const char *otGetVersionString(void)
138 {
139 /**
140 * PLATFORM_VERSION_ATTR_PREFIX and PLATFORM_VERSION_ATTR_SUFFIX are
141 * intended to be used to specify compiler directives to indicate
142 * what linker section the platform version string should be stored.
143 *
144 * This is useful for specifying an exact location of where the version
145 * string will be located so that it can be easily retrieved from the
146 * raw firmware image.
147 *
148 * If PLATFORM_VERSION_ATTR_PREFIX is unspecified, the keyword `static`
149 * is used instead.
150 *
151 * If both are unspecified, the location of the string in the firmware
152 * image will be undefined and may change.
153 */
154
155 #if !defined(OPENTHREAD_BUILD_DATETIME) && defined(__ANDROID__)
156
157 #ifdef OPENTHREAD_ENABLE_ANDROID_NDK
158 static char sVersion[100 + PROP_VALUE_MAX];
159 char dateTime[PROP_VALUE_MAX];
160
161 __system_property_get("ro.build.date", dateTime);
162 #else
163 static char sVersion[100 + PROPERTY_VALUE_MAX];
164 char dateTime[PROPERTY_VALUE_MAX];
165
166 property_get("ro.build.date", dateTime, "Thu Jan 1 1970 UTC 00:00:00");
167 #endif
168
169 snprintf(sVersion, sizeof(sVersion), "%s/%s ;%s ; %s", PACKAGE_NAME, PACKAGE_VERSION,
170 OPENTHREAD_CONFIG_PLATFORM_INFO, dateTime);
171 #else
172
173 #ifdef PLATFORM_VERSION_ATTR_PREFIX
174 PLATFORM_VERSION_ATTR_PREFIX
175 #else
176 static
177 #endif
178 const char sVersion[] = PACKAGE_NAME "/" PACKAGE_VERSION "; " OPENTHREAD_CONFIG_PLATFORM_INFO
179 #ifdef OPENTHREAD_BUILD_DATETIME
180 "; " OPENTHREAD_BUILD_DATETIME
181 #endif
182 #ifdef PLATFORM_VERSION_ATTR_SUFFIX
183 PLATFORM_VERSION_ATTR_SUFFIX
184 #endif
185 ; // Trailing semicolon to end statement.
186
187 #endif
188
189 return sVersion;
190 }
191
otGetRadioVersionString(otInstance * aInstance)192 const char *otGetRadioVersionString(otInstance *aInstance)
193 {
194 return AsCoreType(aInstance).Get<Radio>().GetVersionString();
195 }
196