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 PLATFORM_POSIX_H_
36 #define 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/openthread-system.h>
52 #include <openthread/platform/time.h>
53 
54 #include "common/logging.hpp"
55 
56 #include "lib/platform/exit_code.h"
57 #include "lib/url/url.hpp"
58 
59 /**
60  * @def OPENTHREAD_POSIX_VIRTUAL_TIME
61  *
62  * This setting configures whether to use virtual time.
63  *
64  */
65 #ifndef OPENTHREAD_POSIX_VIRTUAL_TIME
66 #define OPENTHREAD_POSIX_VIRTUAL_TIME 0
67 #endif
68 
69 /**
70  * This is the socket name used by daemon mode.
71  *
72  */
73 #define OPENTHREAD_POSIX_DAEMON_SOCKET_NAME OPENTHREAD_POSIX_CONFIG_DAEMON_SOCKET_BASENAME ".sock"
74 
75 #ifdef __cplusplus
76 extern "C" {
77 #endif
78 
79 enum
80 {
81     OT_SIM_EVENT_ALARM_FIRED        = 0,
82     OT_SIM_EVENT_RADIO_RECEIVED     = 1,
83     OT_SIM_EVENT_UART_WRITE         = 2,
84     OT_SIM_EVENT_RADIO_SPINEL_WRITE = 3,
85     OT_EVENT_DATA_MAX_SIZE          = 1024,
86 };
87 
88 OT_TOOL_PACKED_BEGIN
89 struct VirtualTimeEvent
90 {
91     uint64_t mDelay;
92     uint8_t  mEvent;
93     uint16_t mDataLength;
94     uint8_t  mData[OT_EVENT_DATA_MAX_SIZE];
95 } OT_TOOL_PACKED_END;
96 
97 struct RadioProcessContext
98 {
99     const fd_set *mReadFdSet;
100     const fd_set *mWriteFdSet;
101 };
102 
103 /**
104  * This function initializes the alarm service used by OpenThread.
105  *
106  * @param[in]  aSpeedUpFactor   The speed up factor.
107  * @param[in]  aRealTimeSignal  The real time signal for microsecond alarms.
108  *
109  */
110 void platformAlarmInit(uint32_t aSpeedUpFactor, int aRealTimeSignal);
111 
112 /**
113  * This function retrieves the time remaining until the alarm fires.
114  *
115  * @param[out]  aTimeval  A pointer to the timeval struct.
116  *
117  */
118 void platformAlarmUpdateTimeout(struct timeval *tv);
119 
120 /**
121  * This function performs alarm driver processing.
122  *
123  * @param[in]  aInstance  The OpenThread instance structure.
124  *
125  */
126 void platformAlarmProcess(otInstance *aInstance);
127 
128 /**
129  * This function returns the next alarm event time.
130  *
131  * @returns The next alarm fire time.
132  *
133  */
134 int32_t platformAlarmGetNext(void);
135 
136 #ifndef MS_PER_S
137 #define MS_PER_S 1000
138 #endif
139 #ifndef US_PER_MS
140 #define US_PER_MS 1000
141 #endif
142 #ifndef US_PER_S
143 #define US_PER_S (MS_PER_S * US_PER_MS)
144 #endif
145 #ifndef NS_PER_US
146 #define NS_PER_US 1000
147 #endif
148 
149 /**
150  * This function advances the alarm time by @p aDelta.
151  *
152  * @param[in]  aDelta  The amount of time to advance.
153  *
154  */
155 void platformAlarmAdvanceNow(uint64_t aDelta);
156 
157 /**
158  * This function initializes the radio service used by OpenThread.
159  *
160  * @note Even when @p aPlatformConfig->mResetRadio is false, a reset event (i.e. a PROP_LAST_STATUS between
161  * [SPINEL_STATUS_RESET__BEGIN, SPINEL_STATUS_RESET__END]) is still expected from RCP.
162  *
163  * @param[in]   aUrl  A pointer to the null-terminated radio URL.
164  *
165  */
166 void platformRadioInit(const char *aUrl);
167 
168 /**
169  * This function shuts down the radio service used by OpenThread.
170  *
171  */
172 void platformRadioDeinit(void);
173 
174 /**
175  * This function inputs a received radio frame.
176  *
177  * @param[in]  aInstance   A pointer to the OpenThread instance.
178  * @param[in]  aBuf        A pointer to the received radio frame.
179  * @param[in]  aBufLength  The size of the received radio frame.
180  *
181  */
182 void platformRadioReceive(otInstance *aInstance, uint8_t *aBuf, uint16_t aBufLength);
183 
184 /**
185  * This function updates the file descriptor sets with file descriptors used by the radio driver.
186  *
187  * @param[inout]  aReadFdSet   A pointer to the read file descriptors.
188  * @param[inout]  aWriteFdSet  A pointer to the write file descriptors.
189  * @param[inout]  aMaxFd       A pointer to the max file descriptor.
190  * @param[inout]  aTimeout     A pointer to the timeout.
191  *
192  */
193 void platformRadioUpdateFdSet(fd_set *aReadFdSet, fd_set *aWriteFdSet, int *aMaxFd, struct timeval *aTimeout);
194 
195 /**
196  * This function performs radio driver processing.
197  *
198  * @param[in]   aInstance       A pointer to the OpenThread instance.
199  * @param[in]   aReadFdSet      A pointer to the read file descriptors.
200  * @param[in]   aWriteFdSet     A pointer to the write file descriptors.
201  *
202  */
203 void platformRadioProcess(otInstance *aInstance, const fd_set *aReadFdSet, const fd_set *aWriteFdSet);
204 
205 /**
206  * This function initializes the random number service used by OpenThread.
207  *
208  */
209 void platformRandomInit(void);
210 
211 /**
212  * This function initializes the logging service used by OpenThread.
213  *
214  * @param[in] aName   A name string which will be prefixed to each log line.
215  *
216  */
217 void platformLoggingInit(const char *aName);
218 
219 /**
220  * This function updates the file descriptor sets with file descriptors used by the UART driver.
221  *
222  * @param[inout]  aReadFdSet   A pointer to the read file descriptors.
223  * @param[inout]  aWriteFdSet  A pointer to the write file descriptors.
224  * @param[inout]  aMaxFd       A pointer to the max file descriptor.
225  *
226  */
227 void platformUartUpdateFdSet(fd_set *aReadFdSet, fd_set *aWriteFdSet, fd_set *aErrorFdSet, int *aMaxFd);
228 
229 /**
230  * This function performs radio driver processing.
231  *
232  * @param[in]   aReadFdSet      A pointer to the read file descriptors.
233  * @param[in]   aWriteFdSet     A pointer to the write file descriptors.
234  * @param[in]   aErrorFdSet     A pointer to the error file descriptors.
235  *
236  */
237 void platformUartProcess(const fd_set *aReadFdSet, const fd_set *aWriteFdSet, const fd_set *aErrorFdSet);
238 
239 /**
240  * This function initializes platform netif.
241  *
242  * @note This function is called before OpenThread instance is created.
243  *
244  * @param[in]   aInterfaceName  A pointer to Thread network interface name.
245  *
246  */
247 void platformNetifInit(const char *aInterfaceName);
248 
249 /**
250  * This function sets up platform netif.
251  *
252  * @note This function is called after OpenThread instance is created.
253  *
254  * @param[in]   aInstance       A pointer to the OpenThread instance.
255  *
256  */
257 void platformNetifSetUp(void);
258 
259 /**
260  * This function tears down platform netif.
261  *
262  * @note This function is called before OpenThread instance is destructed.
263  *
264  */
265 void platformNetifTearDown(void);
266 
267 /**
268  * This function deinitializes platform netif.
269  *
270  * @note This function is called after OpenThread instance is destructed.
271  *
272  */
273 void platformNetifDeinit(void);
274 
275 /**
276  * This function updates the file descriptor sets with file descriptors used by platform netif module.
277  *
278  * @param[inout]  aReadFdSet    A pointer to the read file descriptors.
279  * @param[inout]  aWriteFdSet   A pointer to the write file descriptors.
280  * @param[inout]  aErrorFdSet   A pointer to the error file descriptors.
281  * @param[inout]  aMaxFd        A pointer to the max file descriptor.
282  *
283  */
284 void platformNetifUpdateFdSet(fd_set *aReadFdSet, fd_set *aWriteFdSet, fd_set *aErrorFdSet, int *aMaxFd);
285 
286 /**
287  * This function performs platform netif processing.
288  *
289  * @param[in]   aReadFdSet      A pointer to the read file descriptors.
290  * @param[in]   aWriteFdSet     A pointer to the write file descriptors.
291  * @param[in]   aErrorFdSet     A pointer to the error file descriptors.
292  *
293  */
294 void platformNetifProcess(const fd_set *aReadFdSet, const fd_set *aWriteFdSet, const fd_set *aErrorFdSet);
295 
296 /**
297  * This function performs notifies state changes to platform netif.
298  *
299  * @param[in]   aInstance       A pointer to the OpenThread instance.
300  * @param[in]   aFlags          Flags that denote the state change events.
301  *
302  */
303 void platformNetifStateChange(otInstance *aInstance, otChangedFlags aFlags);
304 
305 /**
306  * This function initialize virtual time simulation.
307  *
308  * @params[in]  aNodeId     Node id of this simulated device.
309  *
310  */
311 void virtualTimeInit(uint16_t aNodeId);
312 
313 /**
314  * This function deinitialize virtual time simulation.
315  *
316  */
317 void virtualTimeDeinit(void);
318 
319 /**
320  * This function performs virtual time simulation processing.
321  *
322  * @param[in]   aInstance       A pointer to the OpenThread instance.
323  * @param[in]   aReadFdSet      A pointer to the read file descriptors.
324  * @param[in]   aWriteFdSet     A pointer to the write file descriptors.
325  *
326  */
327 void virtualTimeProcess(otInstance *  aInstance,
328                         const fd_set *aReadFdSet,
329                         const fd_set *aWriteFdSet,
330                         const fd_set *aErrorFdSet);
331 
332 /**
333  * This function updates the file descriptor sets with file descriptors
334  * used by the virtual time simulation.
335  *
336  * @param[inout]  aReadFdSet   A pointer to the read file descriptors.
337  * @param[inout]  aWriteFdSet  A pointer to the write file descriptors.
338  * @param[inout]  aErrorFdSet  A pointer to the error file descriptors.
339  * @param[inout]  aMaxFd       A pointer to the max file descriptor.
340  * @param[inout]  aTimeout     A pointer to the timeout.
341  *
342  */
343 void virtualTimeUpdateFdSet(fd_set *        aReadFdSet,
344                             fd_set *        aWriteFdSet,
345                             fd_set *        aErrorFdSet,
346                             int *           aMaxFd,
347                             struct timeval *aTimeout);
348 
349 /**
350  * This function sends radio spinel event of virtual time simulation.
351  *
352  * @param[in] aData     A pointer to the spinel frame.
353  * @param[in] aLength   Length of the spinel frame.
354  *
355  */
356 void virtualTimeSendRadioSpinelWriteEvent(const uint8_t *aData, uint16_t aLength);
357 
358 /**
359  * This function receives an event of virtual time simulation.
360  *
361  * @param[out]  aEvent  A pointer to the event receiving the event.
362  *
363  */
364 void virtualTimeReceiveEvent(struct VirtualTimeEvent *aEvent);
365 
366 /**
367  * This function sends sleep event through virtual time simulation.
368  *
369  * @param[in]   aTimeout    A pointer to the time sleeping.
370  *
371  */
372 void virtualTimeSendSleepEvent(const struct timeval *aTimeout);
373 
374 /**
375  * This function performs radio spinel processing of virtual time simulation.
376  *
377  * @param[in]   aInstance   A pointer to the OpenThread instance.
378  * @param[in]   aEvent      A pointer to the current event.
379  *
380  */
381 void virtualTimeRadioSpinelProcess(otInstance *aInstance, const struct VirtualTimeEvent *aEvent);
382 
383 enum SocketBlockOption
384 {
385     kSocketBlock,
386     kSocketNonBlock,
387 };
388 
389 /**
390  * This function initializes platform TREL UDP6 driver.
391  *
392  * @param[in]   aInterfaceName   The name of network interface.
393  *
394  */
395 void platformTrelInit(const char *aInterfaceName);
396 
397 /**
398  * This function shuts down the platform TREL UDP6 platform driver.
399  *
400  */
401 void platformTrelDeinit(void);
402 
403 /**
404  * This function updates the file descriptor sets with file descriptors used by the TREL driver.
405  *
406  * @param[inout]  aReadFdSet   A pointer to the read file descriptors.
407  * @param[inout]  aWriteFdSet  A pointer to the write file descriptors.
408  * @param[inout]  aMaxFd       A pointer to the max file descriptor.
409  * @param[inout]  aTimeout     A pointer to the timeout.
410  *
411  */
412 void platformTrelUpdateFdSet(fd_set *aReadFdSet, fd_set *aWriteFdSet, int *aMaxFd, struct timeval *aTimeout);
413 
414 /**
415  * This function performs TREL driver processing.
416  *
417  * @param[in]   aInstance       A pointer to the OpenThread instance.
418  * @param[in]   aReadFdSet      A pointer to the read file descriptors.
419  * @param[in]   aWriteFdSet     A pointer to the write file descriptors.
420  *
421  */
422 void platformTrelProcess(otInstance *aInstance, const fd_set *aReadFdSet, const fd_set *aWriteFdSet);
423 
424 /**
425  * This function creates a socket with SOCK_CLOEXEC flag set.
426  *
427  * @param[in]   aDomain       The communication domain.
428  * @param[in]   aType         The semantics of communication.
429  * @param[in]   aProtocol     The protocol to use.
430  * @param[in]   aBlockOption  Whether to add nonblock flags.
431  *
432  * @returns The file descriptor of the created socket.
433  *
434  * @retval  -1  Failed to create socket.
435  *
436  */
437 int SocketWithCloseExec(int aDomain, int aType, int aProtocol, SocketBlockOption aBlockOption);
438 
439 /**
440  * The name of Thread network interface.
441  *
442  */
443 extern char gNetifName[IFNAMSIZ];
444 
445 /**
446  * The index of Thread network interface.
447  *
448  */
449 extern unsigned int gNetifIndex;
450 
451 /**
452  * This function initializes platform Backbone network.
453  *
454  * @note This function is called before OpenThread instance is created.
455  *
456  * @param[in]   aInterfaceName  A pointer to Thread network interface name.
457  *
458  */
459 void platformBackboneInit(const char *aInterfaceName);
460 
461 /**
462  * This function sets up platform Backbone network.
463  *
464  * @note This function is called after OpenThread instance is created.
465  *
466  * @param[in]   aInstance       A pointer to the OpenThread instance.
467  *
468  */
469 void platformBackboneSetUp(void);
470 
471 /**
472  * This function tears down platform Backbone network.
473  *
474  * @note This function is called before OpenThread instance is destructed.
475  *
476  */
477 void platformBackboneTearDown(void);
478 
479 /**
480  * This function shuts down the platform Backbone network.
481  *
482  * @note This function is called after OpenThread instance is destructed.
483  *
484  */
485 void platformBackboneDeinit(void);
486 
487 /**
488  * This function performs notifies state changes to platform Backbone network.
489  *
490  * @param[in]   aInstance       A pointer to the OpenThread instance.
491  * @param[in]   aFlags          Flags that denote the state change events.
492  *
493  */
494 void platformBackboneStateChange(otInstance *aInstance, otChangedFlags aFlags);
495 
496 /**
497  * A pointer to the OpenThread instance.
498  *
499  */
500 extern otInstance *gInstance;
501 
502 /**
503  * The name of Backbone network interface.
504  *
505  */
506 extern char gBackboneNetifName[IFNAMSIZ];
507 
508 /**
509  * The index of Backbone network interface.
510  *
511  */
512 extern unsigned int gBackboneNetifIndex;
513 
514 /**
515  * This function tells if the infrastructure interface is running.
516  *
517  * @returns TRUE if the infrastructure interface is running, FALSE if not.
518  *
519  */
520 bool platformInfraIfIsRunning(void);
521 
522 #ifdef __cplusplus
523 }
524 #endif
525 #endif // PLATFORM_POSIX_H_
526