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