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_CONFIG_ANDROID_NDK_ENABLE
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) { return &Instance::InitSingle(); }
71 #endif // #if OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
72
otInstanceIsInitialized(otInstance * aInstance)73 bool otInstanceIsInitialized(otInstance *aInstance)
74 {
75 #if OPENTHREAD_MTD || OPENTHREAD_FTD
76 return AsCoreType(aInstance).IsInitialized();
77 #else
78 OT_UNUSED_VARIABLE(aInstance);
79 return true;
80 #endif // OPENTHREAD_MTD || OPENTHREAD_FTD
81 }
82
otInstanceFinalize(otInstance * aInstance)83 void otInstanceFinalize(otInstance *aInstance) { AsCoreType(aInstance).Finalize(); }
84
otInstanceReset(otInstance * aInstance)85 void otInstanceReset(otInstance *aInstance) { AsCoreType(aInstance).Reset(); }
86
87 #if OPENTHREAD_CONFIG_UPTIME_ENABLE
otInstanceGetUptime(otInstance * aInstance)88 uint64_t otInstanceGetUptime(otInstance *aInstance) { return AsCoreType(aInstance).Get<Uptime>().GetUptime(); }
89
otInstanceGetUptimeAsString(otInstance * aInstance,char * aBuffer,uint16_t aSize)90 void otInstanceGetUptimeAsString(otInstance *aInstance, char *aBuffer, uint16_t aSize)
91 {
92 AssertPointerIsNotNull(aBuffer);
93
94 AsCoreType(aInstance).Get<Uptime>().GetUptime(aBuffer, aSize);
95 }
96 #endif
97
98 #if OPENTHREAD_MTD || OPENTHREAD_FTD
otSetStateChangedCallback(otInstance * aInstance,otStateChangedCallback aCallback,void * aContext)99 otError otSetStateChangedCallback(otInstance *aInstance, otStateChangedCallback aCallback, void *aContext)
100 {
101 return AsCoreType(aInstance).Get<Notifier>().RegisterCallback(aCallback, aContext);
102 }
103
otRemoveStateChangeCallback(otInstance * aInstance,otStateChangedCallback aCallback,void * aContext)104 void otRemoveStateChangeCallback(otInstance *aInstance, otStateChangedCallback aCallback, void *aContext)
105 {
106 AsCoreType(aInstance).Get<Notifier>().RemoveCallback(aCallback, aContext);
107 }
108
otInstanceFactoryReset(otInstance * aInstance)109 void otInstanceFactoryReset(otInstance *aInstance) { AsCoreType(aInstance).FactoryReset(); }
110
otInstanceErasePersistentInfo(otInstance * aInstance)111 otError otInstanceErasePersistentInfo(otInstance *aInstance) { return AsCoreType(aInstance).ErasePersistentInfo(); }
112 #endif // OPENTHREAD_MTD || OPENTHREAD_FTD
113
114 #if OPENTHREAD_RADIO
otInstanceResetRadioStack(otInstance * aInstance)115 void otInstanceResetRadioStack(otInstance *aInstance) { AsCoreType(aInstance).ResetRadioStack(); }
116 #endif
117
otGetVersionString(void)118 const char *otGetVersionString(void)
119 {
120 /**
121 * PLATFORM_VERSION_ATTR_PREFIX and PLATFORM_VERSION_ATTR_SUFFIX are
122 * intended to be used to specify compiler directives to indicate
123 * what linker section the platform version string should be stored.
124 *
125 * This is useful for specifying an exact location of where the version
126 * string will be located so that it can be easily retrieved from the
127 * raw firmware image.
128 *
129 * If PLATFORM_VERSION_ATTR_PREFIX is unspecified, the keyword `static`
130 * is used instead.
131 *
132 * If both are unspecified, the location of the string in the firmware
133 * image will be undefined and may change.
134 */
135
136 #if !defined(OPENTHREAD_BUILD_DATETIME) && defined(__ANDROID__)
137
138 #ifdef OPENTHREAD_CONFIG_ANDROID_NDK_ENABLE
139 static char sVersion[100 + PROP_VALUE_MAX];
140 char dateTime[PROP_VALUE_MAX];
141
142 __system_property_get("ro.build.date", dateTime);
143 #else
144 static char sVersion[100 + PROPERTY_VALUE_MAX];
145 char dateTime[PROPERTY_VALUE_MAX];
146
147 property_get("ro.build.date", dateTime, "Thu Jan 1 1970 UTC 00:00:00");
148 #endif
149
150 snprintf(sVersion, sizeof(sVersion), "%s/%s ;%s ; %s", PACKAGE_NAME, PACKAGE_VERSION,
151 OPENTHREAD_CONFIG_PLATFORM_INFO, dateTime);
152 #else
153
154 #ifdef PLATFORM_VERSION_ATTR_PREFIX
155 PLATFORM_VERSION_ATTR_PREFIX
156 #else
157 static
158 #endif
159 const char sVersion[] = PACKAGE_NAME "/" PACKAGE_VERSION "; " OPENTHREAD_CONFIG_PLATFORM_INFO
160 #ifdef OPENTHREAD_BUILD_DATETIME
161 "; " OPENTHREAD_BUILD_DATETIME
162 #endif
163 #ifdef PLATFORM_VERSION_ATTR_SUFFIX
164 PLATFORM_VERSION_ATTR_SUFFIX
165 #endif
166 ; // Trailing semicolon to end statement.
167
168 #endif
169
170 return sVersion;
171 }
172
otGetRadioVersionString(otInstance * aInstance)173 const char *otGetRadioVersionString(otInstance *aInstance)
174 {
175 return AsCoreType(aInstance).Get<Radio>().GetVersionString();
176 }
177