blob: 5b0f837a49b6dea9b9564b648f317a9f7241e629 [file] [log] [blame]
Jonathan Hui4f9945c2016-05-10 20:48:47 -07001/*
Jonathan Hui44350172016-09-13 15:57:11 -07002 * Copyright (c) 2016, The OpenThread Authors.
Marcin K Szczodrak99870fa2016-05-12 22:39:11 -07003 * All rights reserved.
Jonathan Hui4f9945c2016-05-10 20:48:47 -07004 *
Marcin K Szczodrak99870fa2016-05-12 22:39:11 -07005 * 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 Hui4f9945c2016-05-10 20:48:47 -070015 *
Marcin K Szczodrak99870fa2016-05-12 22:39:11 -070016 * 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 Hui4f9945c2016-05-10 20:48:47 -070027 */
28
29/**
30 * @file
31 * @brief
Jonathan Hui7d2e4a02016-05-27 09:16:04 -070032 * This file includes the platform-specific initializers.
Jonathan Hui4f9945c2016-05-10 20:48:47 -070033 */
34
Jonathan Hui7d2e4a02016-05-27 09:16:04 -070035#include <assert.h>
36#include <errno.h>
37#include <stddef.h>
Jonathan Hui4f9945c2016-05-10 20:48:47 -070038#include <stdint.h>
Marcin K Szczodrak309685f2016-08-01 08:48:23 -070039#include <stdio.h>
Jonathan Huidb436d92016-06-27 17:19:37 -070040#include <stdlib.h>
Jonathan Hui7d2e4a02016-05-27 09:16:04 -070041#include <sys/time.h>
Jonathan Hui4f9945c2016-05-10 20:48:47 -070042
Jonathan Hui7d2e4a02016-05-27 09:16:04 -070043#include <openthread.h>
44#include <platform/alarm.h>
Jonathan Hui9baa18e2016-07-07 12:18:58 -070045#include <platform/uart.h>
Jonathan Huidb436d92016-06-27 17:19:37 -070046#include "platform-posix.h"
Jonathan Hui4f9945c2016-05-10 20:48:47 -070047
Marcin K Szczodrakcc2e9612016-05-27 13:30:35 -070048uint32_t NODE_ID = 1;
Lu Wang4131e572016-06-01 01:13:50 +080049uint32_t WELLKNOWN_NODE_ID = 34;
Marcin K Szczodrakcc2e9612016-05-27 13:30:35 -070050
Jonathan Huidb436d92016-06-27 17:19:37 -070051void PlatformInit(int argc, char *argv[])
Jonathan Hui7d2e4a02016-05-27 09:16:04 -070052{
Marcin K Szczodrak309685f2016-08-01 08:48:23 -070053 char *endptr;
54
Jonathan Huidb436d92016-06-27 17:19:37 -070055 if (argc != 2)
56 {
Robert Quattlebaum9b1de7b2016-09-12 13:47:18 -070057 exit(EXIT_FAILURE);
Jonathan Huidb436d92016-06-27 17:19:37 -070058 }
59
Jonathan Hui4050c682016-08-10 11:52:22 -070060 NODE_ID = (uint32_t)strtol(argv[1], &endptr, 0);
Marcin K Szczodrak309685f2016-08-01 08:48:23 -070061
62 if (*endptr != '\0')
63 {
64 fprintf(stderr, "Invalid NODE_ID: %s\n", argv[1]);
Robert Quattlebaum9b1de7b2016-09-12 13:47:18 -070065 exit(EXIT_FAILURE);
Marcin K Szczodrak309685f2016-08-01 08:48:23 -070066 }
Jonathan Huidb436d92016-06-27 17:19:37 -070067
68 posixAlarmInit();
69 posixRadioInit();
70 posixRandomInit();
Jonathan Hui9baa18e2016-07-07 12:18:58 -070071 otPlatUartEnable();
Jonathan Hui7d2e4a02016-05-27 09:16:04 -070072}
Jonathan Hui4f9945c2016-05-10 20:48:47 -070073
Nick Banksffbe65c2016-09-12 14:29:43 -070074void PlatformProcessDrivers(otInstance *aInstance)
Jonathan Hui7d2e4a02016-05-27 09:16:04 -070075{
76 fd_set read_fds;
77 fd_set write_fds;
Robert Quattlebaum9b1de7b2016-09-12 13:47:18 -070078 fd_set error_fds;
Jonathan Hui7d2e4a02016-05-27 09:16:04 -070079 int max_fd = -1;
80 struct timeval timeout;
81 int rval;
Jonathan Hui4f9945c2016-05-10 20:48:47 -070082
Jonathan Hui7d2e4a02016-05-27 09:16:04 -070083 FD_ZERO(&read_fds);
84 FD_ZERO(&write_fds);
Robert Quattlebaum9b1de7b2016-09-12 13:47:18 -070085 FD_ZERO(&error_fds);
Jonathan Hui4f9945c2016-05-10 20:48:47 -070086
Robert Quattlebaum9b1de7b2016-09-12 13:47:18 -070087 posixUartUpdateFdSet(&read_fds, &write_fds, &error_fds, &max_fd);
Jonathan Huidb436d92016-06-27 17:19:37 -070088 posixRadioUpdateFdSet(&read_fds, &write_fds, &max_fd);
89 posixAlarmUpdateTimeout(&timeout);
Jonathan Hui4f9945c2016-05-10 20:48:47 -070090
Nick Banksffbe65c2016-09-12 14:29:43 -070091 if (!otAreTaskletsPending(aInstance))
Jonathan Hui7d2e4a02016-05-27 09:16:04 -070092 {
Robert Quattlebaum9b1de7b2016-09-12 13:47:18 -070093 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 Hui7d2e4a02016-05-27 09:16:04 -0700100 }
Jonathan Hui4f9945c2016-05-10 20:48:47 -0700101
Jonathan Hui9baa18e2016-07-07 12:18:58 -0700102 posixUartProcess();
Nick Banksffbe65c2016-09-12 14:29:43 -0700103 posixRadioProcess(aInstance);
104 posixAlarmProcess(aInstance);
Jonathan Hui7d2e4a02016-05-27 09:16:04 -0700105}
Marcin K Szczodrakcc2e9612016-05-27 13:30:35 -0700106