Jonathan Hui | 4f9945c | 2016-05-10 20:48:47 -0700 | [diff] [blame] | 1 | /* |
Jonathan Hui | 4435017 | 2016-09-13 15:57:11 -0700 | [diff] [blame^] | 2 | * Copyright (c) 2016, The OpenThread Authors. |
Marcin K Szczodrak | 99870fa | 2016-05-12 22:39:11 -0700 | [diff] [blame] | 3 | * All rights reserved. |
Jonathan Hui | 4f9945c | 2016-05-10 20:48:47 -0700 | [diff] [blame] | 4 | * |
Marcin K Szczodrak | 99870fa | 2016-05-12 22:39:11 -0700 | [diff] [blame] | 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. |
Jonathan Hui | 4f9945c | 2016-05-10 20:48:47 -0700 | [diff] [blame] | 15 | * |
Marcin K Szczodrak | 99870fa | 2016-05-12 22:39:11 -0700 | [diff] [blame] | 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. |
Jonathan Hui | 4f9945c | 2016-05-10 20:48:47 -0700 | [diff] [blame] | 27 | */ |
| 28 | |
| 29 | /** |
| 30 | * @file |
| 31 | * @brief |
Jonathan Hui | 7d2e4a0 | 2016-05-27 09:16:04 -0700 | [diff] [blame] | 32 | * This file includes the platform-specific initializers. |
Jonathan Hui | 4f9945c | 2016-05-10 20:48:47 -0700 | [diff] [blame] | 33 | */ |
| 34 | |
Jonathan Hui | 7d2e4a0 | 2016-05-27 09:16:04 -0700 | [diff] [blame] | 35 | #include <assert.h> |
| 36 | #include <errno.h> |
| 37 | #include <stddef.h> |
Jonathan Hui | 4f9945c | 2016-05-10 20:48:47 -0700 | [diff] [blame] | 38 | #include <stdint.h> |
Marcin K Szczodrak | 309685f | 2016-08-01 08:48:23 -0700 | [diff] [blame] | 39 | #include <stdio.h> |
Jonathan Hui | db436d9 | 2016-06-27 17:19:37 -0700 | [diff] [blame] | 40 | #include <stdlib.h> |
Jonathan Hui | 7d2e4a0 | 2016-05-27 09:16:04 -0700 | [diff] [blame] | 41 | #include <sys/time.h> |
Jonathan Hui | 4f9945c | 2016-05-10 20:48:47 -0700 | [diff] [blame] | 42 | |
Jonathan Hui | 7d2e4a0 | 2016-05-27 09:16:04 -0700 | [diff] [blame] | 43 | #include <openthread.h> |
| 44 | #include <platform/alarm.h> |
Jonathan Hui | 9baa18e | 2016-07-07 12:18:58 -0700 | [diff] [blame] | 45 | #include <platform/uart.h> |
Jonathan Hui | db436d9 | 2016-06-27 17:19:37 -0700 | [diff] [blame] | 46 | #include "platform-posix.h" |
Jonathan Hui | 4f9945c | 2016-05-10 20:48:47 -0700 | [diff] [blame] | 47 | |
Marcin K Szczodrak | cc2e961 | 2016-05-27 13:30:35 -0700 | [diff] [blame] | 48 | uint32_t NODE_ID = 1; |
Lu Wang | 4131e57 | 2016-06-01 01:13:50 +0800 | [diff] [blame] | 49 | uint32_t WELLKNOWN_NODE_ID = 34; |
Marcin K Szczodrak | cc2e961 | 2016-05-27 13:30:35 -0700 | [diff] [blame] | 50 | |
Jonathan Hui | db436d9 | 2016-06-27 17:19:37 -0700 | [diff] [blame] | 51 | void PlatformInit(int argc, char *argv[]) |
Jonathan Hui | 7d2e4a0 | 2016-05-27 09:16:04 -0700 | [diff] [blame] | 52 | { |
Marcin K Szczodrak | 309685f | 2016-08-01 08:48:23 -0700 | [diff] [blame] | 53 | char *endptr; |
| 54 | |
Jonathan Hui | db436d9 | 2016-06-27 17:19:37 -0700 | [diff] [blame] | 55 | if (argc != 2) |
| 56 | { |
Robert Quattlebaum | 9b1de7b | 2016-09-12 13:47:18 -0700 | [diff] [blame] | 57 | exit(EXIT_FAILURE); |
Jonathan Hui | db436d9 | 2016-06-27 17:19:37 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Jonathan Hui | 4050c68 | 2016-08-10 11:52:22 -0700 | [diff] [blame] | 60 | NODE_ID = (uint32_t)strtol(argv[1], &endptr, 0); |
Marcin K Szczodrak | 309685f | 2016-08-01 08:48:23 -0700 | [diff] [blame] | 61 | |
| 62 | if (*endptr != '\0') |
| 63 | { |
| 64 | fprintf(stderr, "Invalid NODE_ID: %s\n", argv[1]); |
Robert Quattlebaum | 9b1de7b | 2016-09-12 13:47:18 -0700 | [diff] [blame] | 65 | exit(EXIT_FAILURE); |
Marcin K Szczodrak | 309685f | 2016-08-01 08:48:23 -0700 | [diff] [blame] | 66 | } |
Jonathan Hui | db436d9 | 2016-06-27 17:19:37 -0700 | [diff] [blame] | 67 | |
| 68 | posixAlarmInit(); |
| 69 | posixRadioInit(); |
| 70 | posixRandomInit(); |
Jonathan Hui | 9baa18e | 2016-07-07 12:18:58 -0700 | [diff] [blame] | 71 | otPlatUartEnable(); |
Jonathan Hui | 7d2e4a0 | 2016-05-27 09:16:04 -0700 | [diff] [blame] | 72 | } |
Jonathan Hui | 4f9945c | 2016-05-10 20:48:47 -0700 | [diff] [blame] | 73 | |
Nick Banks | ffbe65c | 2016-09-12 14:29:43 -0700 | [diff] [blame] | 74 | void PlatformProcessDrivers(otInstance *aInstance) |
Jonathan Hui | 7d2e4a0 | 2016-05-27 09:16:04 -0700 | [diff] [blame] | 75 | { |
| 76 | fd_set read_fds; |
| 77 | fd_set write_fds; |
Robert Quattlebaum | 9b1de7b | 2016-09-12 13:47:18 -0700 | [diff] [blame] | 78 | fd_set error_fds; |
Jonathan Hui | 7d2e4a0 | 2016-05-27 09:16:04 -0700 | [diff] [blame] | 79 | int max_fd = -1; |
| 80 | struct timeval timeout; |
| 81 | int rval; |
Jonathan Hui | 4f9945c | 2016-05-10 20:48:47 -0700 | [diff] [blame] | 82 | |
Jonathan Hui | 7d2e4a0 | 2016-05-27 09:16:04 -0700 | [diff] [blame] | 83 | FD_ZERO(&read_fds); |
| 84 | FD_ZERO(&write_fds); |
Robert Quattlebaum | 9b1de7b | 2016-09-12 13:47:18 -0700 | [diff] [blame] | 85 | FD_ZERO(&error_fds); |
Jonathan Hui | 4f9945c | 2016-05-10 20:48:47 -0700 | [diff] [blame] | 86 | |
Robert Quattlebaum | 9b1de7b | 2016-09-12 13:47:18 -0700 | [diff] [blame] | 87 | posixUartUpdateFdSet(&read_fds, &write_fds, &error_fds, &max_fd); |
Jonathan Hui | db436d9 | 2016-06-27 17:19:37 -0700 | [diff] [blame] | 88 | posixRadioUpdateFdSet(&read_fds, &write_fds, &max_fd); |
| 89 | posixAlarmUpdateTimeout(&timeout); |
Jonathan Hui | 4f9945c | 2016-05-10 20:48:47 -0700 | [diff] [blame] | 90 | |
Nick Banks | ffbe65c | 2016-09-12 14:29:43 -0700 | [diff] [blame] | 91 | if (!otAreTaskletsPending(aInstance)) |
Jonathan Hui | 7d2e4a0 | 2016-05-27 09:16:04 -0700 | [diff] [blame] | 92 | { |
Robert Quattlebaum | 9b1de7b | 2016-09-12 13:47:18 -0700 | [diff] [blame] | 93 | rval = select(max_fd + 1, &read_fds, &write_fds, &error_fds, &timeout); |
| 94 | |
| 95 | if ((rval < 0) && (errno != EINTR)) |
| 96 | { |
| 97 | perror("select"); |
| 98 | exit(EXIT_FAILURE); |
| 99 | } |
Jonathan Hui | 7d2e4a0 | 2016-05-27 09:16:04 -0700 | [diff] [blame] | 100 | } |
Jonathan Hui | 4f9945c | 2016-05-10 20:48:47 -0700 | [diff] [blame] | 101 | |
Jonathan Hui | 9baa18e | 2016-07-07 12:18:58 -0700 | [diff] [blame] | 102 | posixUartProcess(); |
Nick Banks | ffbe65c | 2016-09-12 14:29:43 -0700 | [diff] [blame] | 103 | posixRadioProcess(aInstance); |
| 104 | posixAlarmProcess(aInstance); |
Jonathan Hui | 7d2e4a0 | 2016-05-27 09:16:04 -0700 | [diff] [blame] | 105 | } |
Marcin K Szczodrak | cc2e961 | 2016-05-27 13:30:35 -0700 | [diff] [blame] | 106 | |