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  * @brief
32  *   This file defines the platform-specific functions needed by OpenThread's example applications.
33  */
34 
35 #ifndef OPENTHREAD_SYSTEM_H_
36 #define OPENTHREAD_SYSTEM_H_
37 
38 #include <setjmp.h>
39 #include <stdbool.h>
40 #include <stdint.h>
41 #include <stdio.h>
42 #include <sys/select.h>
43 
44 #include <openthread/error.h>
45 #include <openthread/instance.h>
46 #include <openthread/platform/misc.h>
47 
48 #include "lib/spinel/radio_spinel_metrics.h"
49 
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53 
54 /**
55  * Represents default parameters for the SPI interface.
56  *
57  */
58 enum
59 {
60     OT_PLATFORM_CONFIG_SPI_DEFAULT_MODE           = 0,       ///< Default SPI Mode: CPOL=0, CPHA=0.
61     OT_PLATFORM_CONFIG_SPI_DEFAULT_SPEED_HZ       = 1000000, ///< Default SPI speed in hertz.
62     OT_PLATFORM_CONFIG_SPI_DEFAULT_CS_DELAY_US    = 20,      ///< Default delay after SPI C̅S̅ assertion, in µsec.
63     OT_PLATFORM_CONFIG_SPI_DEFAULT_RESET_DELAY_MS = 0, ///< Default delay after R̅E̅S̅E̅T̅ assertion, in milliseconds.
64     OT_PLATFORM_CONFIG_SPI_DEFAULT_ALIGN_ALLOWANCE =
65         16, ///< Default maximum number of 0xFF bytes to clip from start of MISO frame.
66     OT_PLATFORM_CONFIG_SPI_DEFAULT_SMALL_PACKET_SIZE =
67         32,                                ///< Default smallest SPI packet size we can receive in a single transaction.
68     OT_PLATFORM_CONFIG_MAX_RADIO_URLS = 2, ///< Max number of Radio URLs.
69 };
70 
71 /**
72  * Represents platform specific configurations.
73  *
74  */
75 typedef struct otPlatformConfig
76 {
77     const char *mBackboneInterfaceName;                        ///< Backbone network interface name.
78     const char *mInterfaceName;                                ///< Thread network interface name.
79     const char *mRadioUrls[OT_PLATFORM_CONFIG_MAX_RADIO_URLS]; ///< Radio URLs.
80     uint8_t     mRadioUrlNum;                                  ///< Number of Radio URLs.
81     int         mRealTimeSignal;                               ///< The real-time signal for microsecond timer.
82     uint32_t    mSpeedUpFactor;                                ///< Speed up factor.
83     bool        mPersistentInterface;                          ///< Whether persistent the interface
84     bool        mDryRun;                                       ///< If 'DryRun' is set, the posix daemon will exit
85                                                                ///< directly after initialization.
86 } otPlatformConfig;
87 
88 /**
89  * Performs all platform-specific initialization of OpenThread's drivers and initializes the OpenThread
90  * instance.
91  *
92  * @note This function is not called by the OpenThread library. Instead, the system/RTOS should call this function
93  *       when initialization of OpenThread's drivers is most appropriate.
94  *
95  * @param[in]  aPlatformConfig  Platform configuration structure.
96  *
97  * @returns A pointer to the OpenThread instance.
98  *
99  */
100 otInstance *otSysInit(otPlatformConfig *aPlatformConfig);
101 
102 /**
103  * Finalizes the OpenThread instance and performs all platform-specific deinitialization for OpenThread's
104  * drivers.
105  *
106  * @note This function is not called by the OpenThread library. Instead, the system/RTOS should call this function
107  *       when deinitialization of OpenThread's drivers is most appropriate.
108  *
109  */
110 void otSysDeinit(void);
111 
112 /**
113  * Represents a context for a select() based mainloop.
114  *
115  */
116 typedef struct otSysMainloopContext
117 {
118     fd_set         mReadFdSet;  ///< The read file descriptors.
119     fd_set         mWriteFdSet; ///< The write file descriptors.
120     fd_set         mErrorFdSet; ///< The error file descriptors.
121     int            mMaxFd;      ///< The max file descriptor.
122     struct timeval mTimeout;    ///< The timeout.
123 } otSysMainloopContext;
124 
125 /**
126  * Updates the file descriptor sets with file descriptors used by OpenThread drivers.
127  *
128  * @param[in]       aInstance   The OpenThread instance structure.
129  * @param[in,out]   aMainloop   A pointer to the mainloop context.
130  *
131  */
132 void otSysMainloopUpdate(otInstance *aInstance, otSysMainloopContext *aMainloop);
133 
134 /**
135  * Polls OpenThread's mainloop.
136  *
137  * @param[in,out]   aMainloop   A pointer to the mainloop context.
138  *
139  * @returns value returned from select().
140  *
141  */
142 int otSysMainloopPoll(otSysMainloopContext *aMainloop);
143 
144 /**
145  * Performs all platform-specific processing for OpenThread's example applications.
146  *
147  * @note This function is not called by the OpenThread library. Instead, the system/RTOS should call this function
148  *       in the main loop when processing OpenThread's drivers is most appropriate.
149  *
150  * @param[in]   aInstance   The OpenThread instance structure.
151  * @param[in]   aMainloop   A pointer to the mainloop context.
152  *
153  */
154 void otSysMainloopProcess(otInstance *aInstance, const otSysMainloopContext *aMainloop);
155 
156 /**
157  * Returns the radio url help string.
158  *
159  * @returns the radio url help string.
160  *
161  */
162 const char *otSysGetRadioUrlHelpString(void);
163 
164 extern otPlatResetReason gPlatResetReason;
165 
166 /**
167  * Returns the Thread network interface name.
168  *
169  * @returns The Thread network interface name.
170  *
171  */
172 const char *otSysGetThreadNetifName(void);
173 
174 /**
175  * Returns the Thread network interface index.
176  *
177  * @returns The Thread network interface index.
178  *
179  */
180 unsigned int otSysGetThreadNetifIndex(void);
181 
182 /**
183  * Returns the infrastructure network interface name.
184  *
185  * @returns The infrastructure network interface name, or `nullptr` if not specified.
186  *
187  */
188 const char *otSysGetInfraNetifName(void);
189 
190 /**
191  * Returns the infrastructure network interface index.
192  *
193  * @returns The infrastructure network interface index.
194  *
195  */
196 uint32_t otSysGetInfraNetifIndex(void);
197 
198 /**
199  * Returns the radio spinel metrics.
200  *
201  * @returns The radio spinel metrics.
202  *
203  */
204 const otRadioSpinelMetrics *otSysGetRadioSpinelMetrics(void);
205 
206 /**
207  * Returns the RCP interface metrics.
208  *
209  * @returns The RCP interface metrics.
210  *
211  */
212 const otRcpInterfaceMetrics *otSysGetRcpInterfaceMetrics(void);
213 
214 /**
215  * Returns the ifr_flags of the infrastructure network interface.
216  *
217  * @returns The ifr_flags of infrastructure network interface.
218  *
219  */
220 uint32_t otSysGetInfraNetifFlags(void);
221 
222 typedef struct otSysInfraNetIfAddressCounters
223 {
224     uint32_t mLinkLocalAddresses;
225     uint32_t mUniqueLocalAddresses;
226     uint32_t mGlobalUnicastAddresses;
227 } otSysInfraNetIfAddressCounters;
228 
229 /**
230  * This functions counts the number of addresses on the infrastructure network interface.
231  *
232  * @param[out] aAddressCounters  The counters of addresses on infrastructure network interface.
233  *
234  */
235 void otSysCountInfraNetifAddresses(otSysInfraNetIfAddressCounters *aAddressCounters);
236 
237 /**
238  * Sets the infrastructure network interface and the ICMPv6 socket.
239  *
240  * This function specifies the network interface name and the ICMPv6 socket on that interface. After calling this
241  * function, the caller can call otBorderRoutingInit() to let Border Routing work on that interface.
242  *
243  * @param[in] aInfraNetifName  The name of the infrastructure network interface.
244  * @param[in] aIcmp6Socket     A SOCK_RAW socket running on the infrastructure network interface.
245  *
246  */
247 void otSysSetInfraNetif(const char *aInfraNetifName, int aIcmp6Socket);
248 
249 /**
250  * Returns TRUE if the infrastructure interface is running.
251  *
252  * @returns TRUE if the infrastructure interface is running, FALSE if not.
253  *
254  */
255 bool otSysInfraIfIsRunning(void);
256 
257 #ifdef __cplusplus
258 } // end of extern "C"
259 #endif
260 
261 #endif // OPENTHREAD_SYSTEM_H_
262