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