• 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  */
91 extern uint32_t gNodeId;
92 
93 /**
94  * This function initializes the alarm service used by OpenThread.
95  *
96  */
97 void platformAlarmInit(uint32_t aSpeedUpFactor);
98 
99 /**
100  * This function retrieves the time remaining until the alarm fires.
101  *
102  * @param[out]  aTimeout  A pointer to the timeval struct.
103  *
104  */
105 void platformAlarmUpdateTimeout(struct timeval *aTimeout);
106 
107 /**
108  * This function performs alarm driver processing.
109  *
110  * @param[in]  aInstance  The OpenThread instance structure.
111  *
112  */
113 void platformAlarmProcess(otInstance *aInstance);
114 
115 /**
116  * This function returns the duration to the next alarm event time (in micro seconds)
117  *
118  * @returns The duration (in micro seconds) to the next alarm event.
119  *
120  */
121 uint64_t platformAlarmGetNext(void);
122 
123 /**
124  * This function returns the current alarm time.
125  *
126  * @returns The current alarm time.
127  *
128  */
129 uint64_t platformAlarmGetNow(void);
130 
131 /**
132  * This function advances the alarm time by @p aDelta.
133  *
134  * @param[in]  aDelta  The amount of time to advance.
135  *
136  */
137 void platformAlarmAdvanceNow(uint64_t aDelta);
138 
139 /**
140  * This function initializes the radio service used by OpenThread.
141  *
142  */
143 void platformRadioInit(void);
144 
145 /**
146  * This function shuts down the radio service used by OpenThread.
147  *
148  */
149 void platformRadioDeinit(void);
150 
151 /**
152  * This function inputs a received radio frame.
153  *
154  * @param[in]  aInstance   A pointer to the OpenThread instance.
155  * @param[in]  aBuf        A pointer to the received radio frame.
156  * @param[in]  aBufLength  The size of the received radio frame.
157  *
158  */
159 void platformRadioReceive(otInstance *aInstance, uint8_t *aBuf, uint16_t aBufLength);
160 
161 /**
162  * This function updates the file descriptor sets with file descriptors used by the radio driver.
163  *
164  * @param[in,out]  aReadFdSet   A pointer to the read file descriptors.
165  * @param[in,out]  aWriteFdSet  A pointer to the write file descriptors.
166  * @param[in,out]  aTimeout     A pointer to the timeout.
167  * @param[in,out]  aMaxFd       A pointer to the max file descriptor.
168  *
169  */
170 void platformRadioUpdateFdSet(fd_set *aReadFdSet, fd_set *aWriteFdSet, struct timeval *aTimeout, int *aMaxFd);
171 
172 /**
173  * This function performs radio driver processing.
174  *
175  * @param[in]  aInstance    The OpenThread instance structure.
176  * @param[in]  aReadFdSet   A pointer to the read file descriptors.
177  * @param[in]  aWriteFdSet  A pointer to the write file descriptors.
178  *
179  */
180 void platformRadioProcess(otInstance *aInstance, const fd_set *aReadFdSet, const fd_set *aWriteFdSet);
181 
182 /**
183  * This function initializes the random number service used by OpenThread.
184  *
185  */
186 void platformRandomInit(void);
187 
188 /**
189  * This function updates the file descriptor sets with file descriptors used by the UART driver.
190  *
191  * @param[in,out]  aReadFdSet   A pointer to the read file descriptors.
192  * @param[in,out]  aWriteFdSet  A pointer to the write file descriptors.
193  * @param[in,out]  aMaxFd       A pointer to the max file descriptor.
194  *
195  */
196 void platformUartUpdateFdSet(fd_set *aReadFdSet, fd_set *aWriteFdSet, fd_set *aErrorFdSet, int *aMaxFd);
197 
198 /**
199  * This function performs radio driver processing.
200  *
201  */
202 void platformUartProcess(void);
203 
204 /**
205  * This function restores the Uart.
206  *
207  */
208 void platformUartRestore(void);
209 
210 /**
211  * This function sends a simulation event.
212  *
213  * @param[in]   aEvent  A pointer to the simulation event to send
214  *
215  */
216 void otSimSendEvent(const struct Event *aEvent);
217 
218 /**
219  * This function sends Uart data through simulation.
220  *
221  * @param[in]   aData       A pointer to the UART data.
222  * @param[in]   aLength     Length of UART data.
223  *
224  */
225 void otSimSendUartWriteEvent(const uint8_t *aData, uint16_t aLength);
226 
227 /**
228  * This function checks if radio transmitting is pending.
229  *
230  * @returns Whether radio transmitting is pending.
231  *
232  */
233 bool platformRadioIsTransmitPending(void);
234 
235 #if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
236 
237 /**
238  * This function initializes the TREL service.
239  *
240  * @param[in] aSpeedUpFactor   The time speed-up factor.
241  *
242  */
243 void platformTrelInit(uint32_t aSpeedUpFactor);
244 
245 /**
246  * This function shuts down the TREL service.
247  *
248  */
249 void platformTrelDeinit(void);
250 
251 /**
252  * This function updates the file descriptor sets with file descriptors used by the TREL.
253  *
254  * @param[in,out]  aReadFdSet   A pointer to the read file descriptors.
255  * @param[in,out]  aWriteFdSet  A pointer to the write file descriptors.
256  * @param[in,out]  aTimeout     A pointer to the timeout.
257  * @param[in,out]  aMaxFd       A pointer to the max file descriptor.
258  *
259  */
260 void platformTrelUpdateFdSet(fd_set *aReadFdSet, fd_set *aWriteFdSet, struct timeval *aTimeout, int *aMaxFd);
261 
262 /**
263  * This function performs TREL processing.
264  *
265  * @param[in]  aInstance    The OpenThread instance structure.
266  * @param[in]  aReadFdSet   A pointer to the read file descriptors.
267  * @param[in]  aWriteFdSet  A pointer to the write file descriptors.
268  *
269  */
270 void platformTrelProcess(otInstance *aInstance, const fd_set *aReadFdSet, const fd_set *aWriteFdSet);
271 
272 #endif // OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
273 
274 #endif // PLATFORM_SIMULATION_H_
275