• 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 PLATFORM_SIMULATION_H_
36 #define PLATFORM_SIMULATION_H_
37 
38 #include <openthread-core-config.h>
39 #include <openthread/config.h>
40 
41 #include <assert.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <time.h>
46 
47 #include <arpa/inet.h>
48 #include <fcntl.h>
49 #include <netinet/in.h>
50 #include <poll.h>
51 #include <signal.h>
52 #include <sys/select.h>
53 #include <sys/socket.h>
54 #include <sys/stat.h>
55 #include <sys/time.h>
56 #include <unistd.h>
57 
58 #include <openthread/instance.h>
59 
60 #include "openthread-core-config.h"
61 #include "platform-config.h"
62 
63 enum
64 {
65     OT_SIM_EVENT_ALARM_FIRED        = 0,
66     OT_SIM_EVENT_RADIO_RECEIVED     = 1,
67     OT_SIM_EVENT_UART_WRITE         = 2,
68     OT_SIM_EVENT_RADIO_SPINEL_WRITE = 3,
69     OT_SIM_EVENT_OTNS_STATUS_PUSH   = 5,
70     OT_EVENT_DATA_MAX_SIZE          = 1024,
71 };
72 
73 OT_TOOL_PACKED_BEGIN
74 struct Event
75 {
76     uint64_t mDelay;
77     uint8_t  mEvent;
78     uint16_t mDataLength;
79     uint8_t  mData[OT_EVENT_DATA_MAX_SIZE];
80 } OT_TOOL_PACKED_END;
81 
82 enum
83 {
84     MAX_NETWORK_SIZE = OPENTHREAD_SIMULATION_MAX_NETWORK_SIZE,
85 };
86 
87 /**
88  * Unique node ID.
89  */
90 extern uint32_t gNodeId;
91 
92 /**
93  * Initializes the alarm service used by OpenThread.
94  */
95 void platformAlarmInit(uint32_t aSpeedUpFactor);
96 
97 /**
98  * Retrieves the time remaining until the alarm fires.
99  *
100  * @param[out]  aTimeout  A pointer to the timeval struct.
101  */
102 void platformAlarmUpdateTimeout(struct timeval *aTimeout);
103 
104 /**
105  * Performs alarm driver processing.
106  *
107  * @param[in]  aInstance  The OpenThread instance structure.
108  */
109 void platformAlarmProcess(otInstance *aInstance);
110 
111 /**
112  * Returns the duration to the next alarm event time (in micro seconds)
113  *
114  * @returns The duration (in micro seconds) to the next alarm event.
115  */
116 uint64_t platformAlarmGetNext(void);
117 
118 /**
119  * Returns the current alarm time.
120  *
121  * @returns The current alarm time.
122  */
123 uint64_t platformAlarmGetNow(void);
124 
125 /**
126  * Advances the alarm time by @p aDelta.
127  *
128  * @param[in]  aDelta  The amount of time to advance.
129  */
130 void platformAlarmAdvanceNow(uint64_t aDelta);
131 
132 /**
133  * Initializes the radio service used by OpenThread.
134  */
135 void platformRadioInit(void);
136 
137 /**
138  * Shuts down the radio service used by OpenThread.
139  */
140 void platformRadioDeinit(void);
141 
142 /**
143  * Inputs a received radio frame.
144  *
145  * @param[in]  aInstance   A pointer to the OpenThread instance.
146  * @param[in]  aBuf        A pointer to the received radio frame.
147  * @param[in]  aBufLength  The size of the received radio frame.
148  */
149 void platformRadioReceive(otInstance *aInstance, uint8_t *aBuf, uint16_t aBufLength);
150 
151 /**
152  * Updates the file descriptor sets with file descriptors used by the BLE radio driver.
153  *
154  * @param[in,out]  aReadFdSet   A pointer to the read file descriptors.
155  * @param[in,out]  aWriteFdSet  A pointer to the write file descriptors.
156  * @param[in,out]  aTimeout     A pointer to the timeout.
157  * @param[in,out]  aMaxFd       A pointer to the max file descriptor.
158  */
159 void platformRadioUpdateFdSet(fd_set *aReadFdSet, fd_set *aWriteFdSet, struct timeval *aTimeout, int *aMaxFd);
160 
161 /**
162  * Performs radio driver processing.
163  *
164  * @param[in]  aInstance    The OpenThread instance structure.
165  * @param[in]  aReadFdSet   A pointer to the read file descriptors.
166  * @param[in]  aWriteFdSet  A pointer to the write file descriptors.
167  */
168 void platformRadioProcess(otInstance *aInstance, const fd_set *aReadFdSet, const fd_set *aWriteFdSet);
169 
170 /**
171  * Initializes the random number service used by OpenThread.
172  */
173 void platformRandomInit(void);
174 
175 /**
176  * This functions set the file name to use for logging.
177  *
178  * @param[in] aName  The file name.
179  */
180 void platformLoggingSetFileName(const char *aName);
181 
182 /**
183  * Initializes the platform logging service.
184  *
185  * @param[in] aName    The log module name to set with syslog.
186  */
187 void platformLoggingInit(const char *aName);
188 
189 /**
190  * Finalizes the platform logging service.
191  */
192 void platformLoggingDeinit(void);
193 
194 /**
195  * Updates the file descriptor sets with file descriptors used by the UART driver.
196  *
197  * @param[in,out]  aReadFdSet   A pointer to the read file descriptors.
198  * @param[in,out]  aWriteFdSet  A pointer to the write file descriptors.
199  * @param[in,out]  aMaxFd       A pointer to the max file descriptor.
200  */
201 void platformUartUpdateFdSet(fd_set *aReadFdSet, fd_set *aWriteFdSet, fd_set *aErrorFdSet, int *aMaxFd);
202 
203 /**
204  * Performs radio driver processing.
205  */
206 void platformUartProcess(void);
207 
208 /**
209  * Restores the Uart.
210  */
211 void platformUartRestore(void);
212 
213 /**
214  * Sends a simulation event.
215  *
216  * @param[in]   aEvent  A pointer to the simulation event to send
217  */
218 void otSimSendEvent(const struct Event *aEvent);
219 
220 /**
221  * Sends Uart data through simulation.
222  *
223  * @param[in]   aData       A pointer to the UART data.
224  * @param[in]   aLength     Length of UART data.
225  */
226 void otSimSendUartWriteEvent(const uint8_t *aData, uint16_t aLength);
227 
228 /**
229  * Checks if radio transmitting is pending.
230  *
231  * @returns Whether radio transmitting is pending.
232  */
233 bool platformRadioIsTransmitPending(void);
234 
235 /**
236  * Parses an environment variable as an unsigned 16-bit integer.
237  *
238  * If the environment variable does not exist, this function does nothing.
239  * If it is not a valid integer, this function will terminate the process with an error message.
240  *
241  * @param[in]   aEnvName  The name of the environment variable.
242  * @param[out]  aValue    A pointer to the unsigned 16-bit integer.
243  */
244 void parseFromEnvAsUint16(const char *aEnvName, uint16_t *aValue);
245 
246 #if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
247 
248 /**
249  * Initializes the TREL service.
250  *
251  * @param[in] aSpeedUpFactor   The time speed-up factor.
252  */
253 void platformTrelInit(uint32_t aSpeedUpFactor);
254 
255 /**
256  * Shuts down the TREL service.
257  */
258 void platformTrelDeinit(void);
259 
260 /**
261  * Updates the file descriptor sets with file descriptors used by the TREL.
262  *
263  * @param[in,out]  aReadFdSet   A pointer to the read file descriptors.
264  * @param[in,out]  aWriteFdSet  A pointer to the write file descriptors.
265  * @param[in,out]  aTimeout     A pointer to the timeout.
266  * @param[in,out]  aMaxFd       A pointer to the max file descriptor.
267  */
268 void platformTrelUpdateFdSet(fd_set *aReadFdSet, fd_set *aWriteFdSet, struct timeval *aTimeout, int *aMaxFd);
269 
270 /**
271  * Performs TREL processing.
272  *
273  * @param[in]  aInstance    The OpenThread instance structure.
274  * @param[in]  aReadFdSet   A pointer to the read file descriptors.
275  * @param[in]  aWriteFdSet  A pointer to the write file descriptors.
276  */
277 void platformTrelProcess(otInstance *aInstance, const fd_set *aReadFdSet, const fd_set *aWriteFdSet);
278 
279 #endif // OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
280 
281 #if OPENTHREAD_SIMULATION_IMPLEMENT_INFRA_IF && OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE
282 
283 /**
284  * Initializes the platform infra-if module.
285  */
286 void platformInfraIfInit(void);
287 
288 /**
289  * Shuts down the platform infra-if module.
290  */
291 void platformInfraIfDeinit(void);
292 
293 /**
294  * Updates the file descriptor sets with file descriptors used by the infra-if module
295  *
296  * @param[in,out]  aReadFdSet   A pointer to the read file descriptors.
297  * @param[in,out]  aWriteFdSet  A pointer to the write file descriptors.
298  * @param[in,out]  aMaxFd       A pointer to the max file descriptor.
299  */
300 void platformInfraIfUpdateFdSet(fd_set *aReadFdSet, fd_set *aWriteFdSet, int *aMaxFd);
301 
302 /**
303  * Performs infra-if module processing.
304  *
305  * @param[in]  aInstance    The OpenThread instance structure.
306  * @param[in]  aReadFdSet   A pointer to the read file descriptors.
307  * @param[in]  aWriteFdSet  A pointer to the write file descriptors.
308  */
309 void platformInfraIfProcess(otInstance *aInstance, const fd_set *aReadFdSet, const fd_set *aWriteFdSet);
310 
311 #endif // OPENTHREAD_SIMULATION_IMPLEMENT_INFRA_IF && OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE
312 
313 #if OPENTHREAD_CONFIG_MULTICAST_DNS_ENABLE && OPENTHREAD_SIMULATION_MDNS_SOCKET_IMPLEMENT_POSIX
314 
315 /**
316  * Updates the file descriptor sets with file descriptors used by the mDNS socket.
317  *
318  * @param[in,out]  aReadFdSet   A pointer to the read file descriptors.
319  * @param[in,out]  aMaxFd       A pointer to the max file descriptor.
320  */
321 void platformMdnsSocketUpdateFdSet(fd_set *aReadFdSet, int *aMaxFd);
322 
323 /**
324  * Performs mDNs Socket processing.
325  *
326  * @param[in]  aInstance    The OpenThread instance structure.
327  * @param[in]  aReadFdSet   A pointer to the read file descriptors.
328  */
329 void platformMdnsSocketProcess(otInstance *aInstance, const fd_set *aReadFdSet);
330 
331 #endif
332 
333 /**
334  * Shuts down the BLE service used by OpenThread.
335  */
336 void platformBleDeinit(void);
337 
338 /**
339  * Updates the file descriptor sets with file descriptors used by the radio driver.
340  *
341  * @param[in,out]  aReadFdSet   A pointer to the read file descriptors.
342  * @param[in,out]  aWriteFdSet  A pointer to the write file descriptors.
343  * @param[in,out]  aTimeout     A pointer to the timeout.
344  * @param[in,out]  aMaxFd       A pointer to the max file descriptor.
345  */
346 void platformBleUpdateFdSet(fd_set *aReadFdSet, fd_set *aWriteFdSet, struct timeval *aTimeout, int *aMaxFd);
347 
348 /**
349  * Performs BLE driver processing.
350  *
351  * @param[in]  aInstance    The OpenThread instance structure.
352  * @param[in]  aReadFdSet   A pointer to the read file descriptors.
353  * @param[in]  aWriteFdSet  A pointer to the write file descriptors.
354  */
355 void platformBleProcess(otInstance *aInstance, const fd_set *aReadFdSet, const fd_set *aWriteFdSet);
356 
357 #endif // PLATFORM_SIMULATION_H_
358