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