• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 includes the platform-specific initializers.
33  */
34 
35 #ifndef OT_PLATFORM_POSIX_H_
36 #define OT_PLATFORM_POSIX_H_
37 
38 #include "openthread-posix-config.h"
39 
40 #include <errno.h>
41 #include <net/if.h>
42 #include <stdint.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <sys/select.h>
46 #include <sys/time.h>
47 
48 #include <openthread/error.h>
49 #include <openthread/instance.h>
50 #include <openthread/ip6.h>
51 #include <openthread/logging.h>
52 #include <openthread/openthread-system.h>
53 #include <openthread/platform/time.h>
54 
55 #include "lib/platform/exit_code.h"
56 #include "lib/url/url.hpp"
57 
58 /**
59  * @def OPENTHREAD_POSIX_VIRTUAL_TIME
60  *
61  * This setting configures whether to use virtual time.
62  *
63  */
64 #ifndef OPENTHREAD_POSIX_VIRTUAL_TIME
65 #define OPENTHREAD_POSIX_VIRTUAL_TIME 0
66 #endif
67 
68 /**
69  * This is the socket name used by daemon mode.
70  *
71  */
72 #define OPENTHREAD_POSIX_DAEMON_SOCKET_NAME OPENTHREAD_POSIX_CONFIG_DAEMON_SOCKET_BASENAME ".sock"
73 
74 #ifdef __cplusplus
75 extern "C" {
76 #endif
77 
78 enum
79 {
80     OT_SIM_EVENT_ALARM_FIRED        = 0,
81     OT_SIM_EVENT_RADIO_RECEIVED     = 1,
82     OT_SIM_EVENT_UART_WRITE         = 2,
83     OT_SIM_EVENT_RADIO_SPINEL_WRITE = 3,
84     OT_EVENT_DATA_MAX_SIZE          = 1024,
85 };
86 
87 OT_TOOL_PACKED_BEGIN
88 struct VirtualTimeEvent
89 {
90     uint64_t mDelay;
91     uint8_t  mEvent;
92     uint16_t mDataLength;
93     uint8_t  mData[OT_EVENT_DATA_MAX_SIZE];
94 } OT_TOOL_PACKED_END;
95 
96 /**
97  * Initializes the alarm service used by OpenThread.
98  *
99  * @param[in]  aSpeedUpFactor   The speed up factor.
100  * @param[in]  aRealTimeSignal  The real time signal for microsecond alarms.
101  *
102  */
103 void platformAlarmInit(uint32_t aSpeedUpFactor, int aRealTimeSignal);
104 
105 /**
106  * Retrieves the time remaining until the alarm fires.
107  *
108  * @param[out]  aTimeval  A pointer to the timeval struct.
109  *
110  */
111 void platformAlarmUpdateTimeout(struct timeval *tv);
112 
113 /**
114  * Performs alarm driver processing.
115  *
116  * @param[in]  aInstance  The OpenThread instance structure.
117  *
118  */
119 void platformAlarmProcess(otInstance *aInstance);
120 
121 /**
122  * Returns the next alarm event time.
123  *
124  * @returns The next alarm fire time.
125  *
126  */
127 int32_t platformAlarmGetNext(void);
128 
129 #ifndef MS_PER_S
130 #define MS_PER_S 1000
131 #endif
132 #ifndef US_PER_MS
133 #define US_PER_MS 1000
134 #endif
135 #ifndef US_PER_S
136 #define US_PER_S (MS_PER_S * US_PER_MS)
137 #endif
138 #ifndef NS_PER_US
139 #define NS_PER_US 1000
140 #endif
141 
142 /**
143  * Advances the alarm time by @p aDelta.
144  *
145  * @param[in]  aDelta  The amount of time to advance.
146  *
147  */
148 void platformAlarmAdvanceNow(uint64_t aDelta);
149 
150 /**
151  * Initializes the radio service used by OpenThread.
152  *
153  * @note Even when @p aPlatformConfig->mResetRadio is false, a reset event (i.e. a PROP_LAST_STATUS between
154  * [SPINEL_STATUS_RESET__BEGIN, SPINEL_STATUS_RESET__END]) is still expected from RCP.
155  *
156  * @param[in]   aUrl  A pointer to the null-terminated radio URL.
157  *
158  */
159 void platformRadioInit(const char *aUrl);
160 
161 /**
162  * Shuts down the radio service used by OpenThread.
163  *
164  */
165 void platformRadioDeinit(void);
166 
167 /**
168  * Inputs a received radio frame.
169  *
170  * @param[in]  aInstance   A pointer to the OpenThread instance.
171  * @param[in]  aBuf        A pointer to the received radio frame.
172  * @param[in]  aBufLength  The size of the received radio frame.
173  *
174  */
175 void platformRadioReceive(otInstance *aInstance, uint8_t *aBuf, uint16_t aBufLength);
176 
177 /**
178  * Updates the file descriptor sets with file descriptors used by the radio driver.
179  *
180  * @param[in]   aContext    A pointer to the mainloop context.
181  *
182  */
183 void platformRadioUpdateFdSet(otSysMainloopContext *aContext);
184 
185 /**
186  * Performs radio driver processing.
187  *
188  * @param[in]   aContext    A pointer to the mainloop context.
189  *
190  */
191 void platformRadioProcess(otInstance *aInstance, const otSysMainloopContext *aContext);
192 
193 /**
194  * Initializes the random number service used by OpenThread.
195  *
196  */
197 void platformRandomInit(void);
198 
199 /**
200  * Initializes the logging service used by OpenThread.
201  *
202  * @param[in] aName   A name string which will be prefixed to each log line.
203  *
204  */
205 void platformLoggingInit(const char *aName);
206 
207 /**
208  * Updates the file descriptor sets with file descriptors used by the UART driver.
209  *
210  * @param[in]   aContext    A pointer to the mainloop context.
211  *
212  */
213 void platformUartUpdateFdSet(otSysMainloopContext *aContext);
214 
215 /**
216  * Performs radio driver processing.
217  *
218  * @param[in]   aContext    A pointer to the mainloop context.
219  *
220  */
221 void platformUartProcess(const otSysMainloopContext *aContext);
222 
223 /**
224  * Initializes platform netif.
225  *
226  * @note This function is called before OpenThread instance is created.
227  *
228  * @param[in]   aInterfaceName  A pointer to Thread network interface name.
229  *
230  */
231 void platformNetifInit(otPlatformConfig *aPlatformConfig);
232 
233 /**
234  * Sets up platform netif.
235  *
236  * @note This function is called after OpenThread instance is created.
237  *
238  * @param[in]   aInstance       A pointer to the OpenThread instance.
239  *
240  */
241 void platformNetifSetUp(void);
242 
243 /**
244  * Tears down platform netif.
245  *
246  * @note This function is called before OpenThread instance is destructed.
247  *
248  */
249 void platformNetifTearDown(void);
250 
251 /**
252  * Deinitializes platform netif.
253  *
254  * @note This function is called after OpenThread instance is destructed.
255  *
256  */
257 void platformNetifDeinit(void);
258 
259 /**
260  * Updates the file descriptor sets with file descriptors used by platform netif module.
261  *
262  * @param[in,out]  aContext  A pointer to the mainloop context.
263  *
264  */
265 void platformNetifUpdateFdSet(otSysMainloopContext *aContext);
266 
267 /**
268  * Performs platform netif processing.
269  *
270  * @param[in]  aContext  A pointer to the mainloop context.
271  *
272  */
273 void platformNetifProcess(const otSysMainloopContext *aContext);
274 
275 /**
276  * Performs notifies state changes to platform netif.
277  *
278  * @param[in]   aInstance       A pointer to the OpenThread instance.
279  * @param[in]   aFlags          Flags that denote the state change events.
280  *
281  */
282 void platformNetifStateChange(otInstance *aInstance, otChangedFlags aFlags);
283 
284 /**
285  * Initialize virtual time simulation.
286  *
287  * @params[in]  aNodeId     Node id of this simulated device.
288  *
289  */
290 void virtualTimeInit(uint16_t aNodeId);
291 
292 /**
293  * Deinitialize virtual time simulation.
294  *
295  */
296 void virtualTimeDeinit(void);
297 
298 /**
299  * Performs virtual time simulation processing.
300  *
301  * @param[in]  aContext  A pointer to the mainloop context.
302  *
303  */
304 void virtualTimeProcess(otInstance *aInstance, const otSysMainloopContext *aContext);
305 
306 /**
307  * Updates the file descriptor sets with file descriptors
308  * used by the virtual time simulation.
309  *
310  * @param[in,out]  aContext  A pointer to the mainloop context.
311  *
312  */
313 void virtualTimeUpdateFdSet(otSysMainloopContext *aContext);
314 
315 /**
316  * Sends radio spinel event of virtual time simulation.
317  *
318  * @param[in] aData     A pointer to the spinel frame.
319  * @param[in] aLength   Length of the spinel frame.
320  *
321  */
322 void virtualTimeSendRadioSpinelWriteEvent(const uint8_t *aData, uint16_t aLength);
323 
324 /**
325  * Receives an event of virtual time simulation.
326  *
327  * @param[out]  aEvent  A pointer to the event receiving the event.
328  *
329  */
330 void virtualTimeReceiveEvent(struct VirtualTimeEvent *aEvent);
331 
332 /**
333  * Sends sleep event through virtual time simulation.
334  *
335  * @param[in]   aTimeout    A pointer to the time sleeping.
336  *
337  */
338 void virtualTimeSendSleepEvent(const struct timeval *aTimeout);
339 
340 /**
341  * Performs radio spinel processing of virtual time simulation.
342  *
343  * @param[in]   aInstance   A pointer to the OpenThread instance.
344  * @param[in]   aEvent      A pointer to the current event.
345  *
346  */
347 void virtualTimeRadioSpinelProcess(otInstance *aInstance, const struct VirtualTimeEvent *aEvent);
348 
349 enum SocketBlockOption
350 {
351     kSocketBlock,
352     kSocketNonBlock,
353 };
354 
355 /**
356  * Initializes platform TREL UDP6 driver.
357  *
358  * @param[in]   aTrelUrl   The TREL URL (configuration for TREL platform).
359  *
360  */
361 void platformTrelInit(const char *aTrelUrl);
362 
363 /**
364  * Shuts down the platform TREL UDP6 platform driver.
365  *
366  */
367 void platformTrelDeinit(void);
368 
369 /**
370  * Updates the file descriptor sets with file descriptors used by the TREL driver.
371  *
372  * @param[in,out]  aContext  A pointer to the mainloop context.
373  *
374  */
375 void platformTrelUpdateFdSet(otSysMainloopContext *aContext);
376 
377 /**
378  * Performs TREL driver processing.
379  *
380  * @param[in]  aContext  A pointer to the mainloop context.
381  *
382  */
383 void platformTrelProcess(otInstance *aInstance, const otSysMainloopContext *aContext);
384 
385 /**
386  * Creates a socket with SOCK_CLOEXEC flag set.
387  *
388  * @param[in]   aDomain       The communication domain.
389  * @param[in]   aType         The semantics of communication.
390  * @param[in]   aProtocol     The protocol to use.
391  * @param[in]   aBlockOption  Whether to add nonblock flags.
392  *
393  * @returns The file descriptor of the created socket.
394  *
395  * @retval  -1  Failed to create socket.
396  *
397  */
398 int SocketWithCloseExec(int aDomain, int aType, int aProtocol, SocketBlockOption aBlockOption);
399 
400 /**
401  * The name of Thread network interface.
402  *
403  */
404 extern char gNetifName[IFNAMSIZ];
405 
406 /**
407  * The index of Thread network interface.
408  *
409  */
410 extern unsigned int gNetifIndex;
411 
412 /**
413  * A pointer to the OpenThread instance.
414  *
415  */
416 extern otInstance *gInstance;
417 
418 /**
419  * Initializes backtrace module.
420  *
421  */
422 void platformBacktraceInit(void);
423 
424 #ifdef __cplusplus
425 }
426 #endif
427 #endif // OT_PLATFORM_POSIX_H_
428