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 * Initializes the alarm service used by OpenThread. 95 * 96 */ 97 void platformAlarmInit(uint32_t aSpeedUpFactor); 98 99 /** 100 * 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 * Performs alarm driver processing. 109 * 110 * @param[in] aInstance The OpenThread instance structure. 111 * 112 */ 113 void platformAlarmProcess(otInstance *aInstance); 114 115 /** 116 * 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 * Returns the current alarm time. 125 * 126 * @returns The current alarm time. 127 * 128 */ 129 uint64_t platformAlarmGetNow(void); 130 131 /** 132 * 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 * Initializes the radio service used by OpenThread. 141 * 142 */ 143 void platformRadioInit(void); 144 145 /** 146 * Shuts down the radio service used by OpenThread. 147 * 148 */ 149 void platformRadioDeinit(void); 150 151 /** 152 * 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 * Updates the file descriptor sets with file descriptors used by the BLE 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 * 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 * Initializes the random number service used by OpenThread. 184 * 185 */ 186 void platformRandomInit(void); 187 188 /** 189 * This functions set the file name to use for logging. 190 * 191 * @param[in] aName The file name. 192 * 193 */ 194 void platformLoggingSetFileName(const char *aName); 195 196 /** 197 * Initializes the platform logging service. 198 * 199 * @param[in] aName The log module name to set with syslog. 200 * 201 */ 202 void platformLoggingInit(const char *aName); 203 204 /** 205 * Finalizes the platform logging service. 206 * 207 */ 208 void platformLoggingDeinit(void); 209 210 /** 211 * Updates the file descriptor sets with file descriptors used by the UART driver. 212 * 213 * @param[in,out] aReadFdSet A pointer to the read file descriptors. 214 * @param[in,out] aWriteFdSet A pointer to the write file descriptors. 215 * @param[in,out] aMaxFd A pointer to the max file descriptor. 216 * 217 */ 218 void platformUartUpdateFdSet(fd_set *aReadFdSet, fd_set *aWriteFdSet, fd_set *aErrorFdSet, int *aMaxFd); 219 220 /** 221 * Performs radio driver processing. 222 * 223 */ 224 void platformUartProcess(void); 225 226 /** 227 * Restores the Uart. 228 * 229 */ 230 void platformUartRestore(void); 231 232 /** 233 * Sends a simulation event. 234 * 235 * @param[in] aEvent A pointer to the simulation event to send 236 * 237 */ 238 void otSimSendEvent(const struct Event *aEvent); 239 240 /** 241 * Sends Uart data through simulation. 242 * 243 * @param[in] aData A pointer to the UART data. 244 * @param[in] aLength Length of UART data. 245 * 246 */ 247 void otSimSendUartWriteEvent(const uint8_t *aData, uint16_t aLength); 248 249 /** 250 * Checks if radio transmitting is pending. 251 * 252 * @returns Whether radio transmitting is pending. 253 * 254 */ 255 bool platformRadioIsTransmitPending(void); 256 257 /** 258 * Parses an environment variable as an unsigned 16-bit integer. 259 * 260 * If the environment variable does not exist, this function does nothing. 261 * If it is not a valid integer, this function will terminate the process with an error message. 262 * 263 * @param[in] aEnvName The name of the environment variable. 264 * @param[out] aValue A pointer to the unsigned 16-bit integer. 265 * 266 */ 267 void parseFromEnvAsUint16(const char *aEnvName, uint16_t *aValue); 268 269 #if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE 270 271 /** 272 * Initializes the TREL service. 273 * 274 * @param[in] aSpeedUpFactor The time speed-up factor. 275 * 276 */ 277 void platformTrelInit(uint32_t aSpeedUpFactor); 278 279 /** 280 * Shuts down the TREL service. 281 * 282 */ 283 void platformTrelDeinit(void); 284 285 /** 286 * Updates the file descriptor sets with file descriptors used by the TREL. 287 * 288 * @param[in,out] aReadFdSet A pointer to the read file descriptors. 289 * @param[in,out] aWriteFdSet A pointer to the write file descriptors. 290 * @param[in,out] aTimeout A pointer to the timeout. 291 * @param[in,out] aMaxFd A pointer to the max file descriptor. 292 * 293 */ 294 void platformTrelUpdateFdSet(fd_set *aReadFdSet, fd_set *aWriteFdSet, struct timeval *aTimeout, int *aMaxFd); 295 296 /** 297 * Performs TREL processing. 298 * 299 * @param[in] aInstance The OpenThread instance structure. 300 * @param[in] aReadFdSet A pointer to the read file descriptors. 301 * @param[in] aWriteFdSet A pointer to the write file descriptors. 302 * 303 */ 304 void platformTrelProcess(otInstance *aInstance, const fd_set *aReadFdSet, const fd_set *aWriteFdSet); 305 306 #endif // OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE 307 308 #if OPENTHREAD_SIMULATION_IMPLEMENT_INFRA_IF && OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE 309 310 /** 311 * Initializes the platform infra-if module. 312 * 313 */ 314 void platformInfraIfInit(void); 315 316 /** 317 * Shuts down the platform infra-if module. 318 * 319 */ 320 void platformInfraIfDeinit(void); 321 322 /** 323 * Updates the file descriptor sets with file descriptors used by the infra-if module 324 * 325 * @param[in,out] aReadFdSet A pointer to the read file descriptors. 326 * @param[in,out] aWriteFdSet A pointer to the write file descriptors. 327 * @param[in,out] aMaxFd A pointer to the max file descriptor. 328 * 329 */ 330 void platformInfraIfUpdateFdSet(fd_set *aReadFdSet, fd_set *aWriteFdSet, int *aMaxFd); 331 332 /** 333 * Performs infra-if module processing. 334 * 335 * @param[in] aInstance The OpenThread instance structure. 336 * @param[in] aReadFdSet A pointer to the read file descriptors. 337 * @param[in] aWriteFdSet A pointer to the write file descriptors. 338 * 339 */ 340 void platformInfraIfProcess(otInstance *aInstance, const fd_set *aReadFdSet, const fd_set *aWriteFdSet); 341 342 #endif // OPENTHREAD_SIMULATION_IMPLEMENT_INFRA_IF && OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE 343 344 #if OPENTHREAD_CONFIG_MULTICAST_DNS_ENABLE && OPENTHREAD_SIMULATION_MDNS_SOCKET_IMPLEMENT_POSIX 345 346 /** 347 * Updates the file descriptor sets with file descriptors used by the mDNS socket. 348 * 349 * @param[in,out] aReadFdSet A pointer to the read file descriptors. 350 * @param[in,out] aMaxFd A pointer to the max file descriptor. 351 * 352 */ 353 void platformMdnsSocketUpdateFdSet(fd_set *aReadFdSet, int *aMaxFd); 354 355 /** 356 * Performs mDNs Socket processing. 357 * 358 * @param[in] aInstance The OpenThread instance structure. 359 * @param[in] aReadFdSet A pointer to the read file descriptors. 360 * 361 */ 362 void platformMdnsSocketProcess(otInstance *aInstance, const fd_set *aReadFdSet); 363 364 #endif 365 366 /** 367 * Shuts down the BLE service used by OpenThread. 368 * 369 */ 370 void platformBleDeinit(void); 371 372 /** 373 * Updates the file descriptor sets with file descriptors used by the radio driver. 374 * 375 * @param[in,out] aReadFdSet A pointer to the read file descriptors. 376 * @param[in,out] aWriteFdSet A pointer to the write file descriptors. 377 * @param[in,out] aTimeout A pointer to the timeout. 378 * @param[in,out] aMaxFd A pointer to the max file descriptor. 379 * 380 */ 381 void platformBleUpdateFdSet(fd_set *aReadFdSet, fd_set *aWriteFdSet, struct timeval *aTimeout, int *aMaxFd); 382 383 /** 384 * Performs BLE driver processing. 385 * 386 * @param[in] aInstance The OpenThread instance structure. 387 * @param[in] aReadFdSet A pointer to the read file descriptors. 388 * @param[in] aWriteFdSet A pointer to the write file descriptors. 389 * 390 */ 391 void platformBleProcess(otInstance *aInstance, const fd_set *aReadFdSet, const fd_set *aWriteFdSet); 392 393 #endif // PLATFORM_SIMULATION_H_ 394