48#include <sys/select.h>
63#if NETSTACK_CONF_WITH_IPV6
67#include "lib/assert.h"
70#include <Security/Security.h>
71#include <Security/SecRandom.h>
73#include <sys/random.h>
78#define LOG_MODULE "Native"
79#define LOG_LEVEL LOG_LEVEL_MAIN
93#define SELECT_MAX SELECT_CONF_MAX
102#ifdef SELECT_CONF_TIMEOUT
103#define SELECT_TIMEOUT SELECT_CONF_TIMEOUT
105#define SELECT_TIMEOUT 1000
111#ifdef SELECT_CONF_STDIN
112#define SELECT_STDIN SELECT_CONF_STDIN
114#define SELECT_STDIN 1
119static const struct select_callback *select_callback[SELECT_MAX];
120static int select_max = 0;
122#ifdef PLATFORM_CONF_MAC_ADDR
123static uint8_t mac_addr[] = PLATFORM_CONF_MAC_ADDR;
125static uint8_t mac_addr[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
130select_set_callback(
int fd,
const struct select_callback *callback)
133 if(fd >= 0 && fd < SELECT_MAX) {
135 if(callback != NULL &&
136 (callback->set_fd == NULL || callback->handle_fd == NULL)) {
140 select_callback[fd] = callback;
143 if(callback != NULL) {
144 if(fd > select_max) {
149 for(i = SELECT_MAX - 1; i > 0; i--) {
150 if(select_callback[i] != NULL) {
163stdin_set_fd(fd_set *rset, fd_set *wset)
165 FD_SET(STDIN_FILENO, rset);
168static int (*input_handler)(
unsigned char c);
171native_uart_set_input(
int (*
input)(
unsigned char c))
173 input_handler =
input;
176stdin_handle_fd(fd_set *rset, fd_set *wset)
179 if(FD_ISSET(STDIN_FILENO, rset)) {
180 ssize_t n = read(STDIN_FILENO, &c, 1);
186 LOG_INFO(
"stdin closed (EOF); no longer monitoring stdin\n");
187 select_set_callback(STDIN_FILENO, NULL);
188 }
else if(errno != EINTR && errno != EAGAIN) {
189 LOG_ERR(
"read(stdin): %s; no longer monitoring stdin\n",
191 select_set_callback(STDIN_FILENO, NULL);
195const static struct select_callback stdin_fd = {
196 stdin_set_fd, stdin_handle_fd
205 memset(&
addr, 0,
sizeof(linkaddr_t));
206#if NETSTACK_CONF_WITH_IPV6
207 memcpy(
addr.u8, mac_addr,
sizeof(
addr.u8));
210 for(i = 0; i <
sizeof(linkaddr_t); ++i) {
211 addr.u8[i] = mac_addr[7 - i];
217#if NETSTACK_CONF_WITH_IPV6
219set_global_address(
void)
226 uip_ip6addr_copy(&
ipaddr, default_prefix);
232 LOG_INFO(
"Added global IPv6 address ");
237 uip_ip6addr_copy(&
ipaddr, default_prefix);
239 uip_ds6_defrt_add(&
ipaddr, 0);
252 if(SecRandomCopyBytes(kSecRandomDefault, CSPRNG_SEED_LEN, seed.
u8)
255 if(getrandom(seed.
u8, CSPRNG_SEED_LEN, GRND_RANDOM) == CSPRNG_SEED_LEN) {
269 if(NULL == input_handler) {
270 native_uart_set_input(serial_line_input_byte);
278#if NETSTACK_CONF_WITH_IPV6
279 set_global_address();
283 setvbuf(stdout, (
char *)NULL, _IONBF, 0);
290 select_set_callback(STDIN_FILENO, &stdin_fd);
302 tv.tv_sec = retval ? 0 : SELECT_TIMEOUT / 1000;
303 tv.tv_usec = retval ? 1 : (SELECT_TIMEOUT * 1000) % 1000000;
308 for(i = 0; i <= select_max; i++) {
309 if(select_callback[i] != NULL && select_callback[i]->set_fd(&fdr, &fdw)) {
314 retval = select(maxfd + 1, &fdr, &fdw, NULL, &tv);
317 LOG_ERR(
"select: %s\n", strerror(errno));
319 }
else if(retval > 0) {
321 for(i = 0; i <= maxfd; i++) {
322 if(select_callback[i] != NULL) {
323 select_callback[i]->handle_fd(&fdr, &fdw);
An OFB-AES-128-based CSPRNG.
Header file for the GPIO HAL.
void platform_init_stage_three()
Final stage of platform driver initialisation.
void platform_init_stage_one(void)
Basic (Stage 1) platform driver initialisation.
void platform_init_stage_two()
Stage 2 of platform driver initialisation.
void csprng_feed(struct csprng_seed *new_seed)
Mixes a new seed with the current one.
void leds_init(void)
Initialise the LED HAL.
void etimer_request_poll(void)
Make the event timer aware that the clock has changed.
void gpio_hal_init()
Initialise the GPIO HAL.
static void linkaddr_set_node_addr(linkaddr_t *addr)
Set the address of the current node.
void platform_main_loop()
The platform's main loop, if provided.
process_num_events_t process_run(void)
Run the system once - call poll handlers and process one event.
static void input(void)
Process a received 6lowpan packet.
const uip_ip6addr_t * uip_ds6_default_prefix()
Retrieve the Default IPv6 prefix.
uip_lladdr_t uip_lladdr
Host L2 address.
uip_ds6_addr_t * uip_ds6_addr_add(uip_ipaddr_t *ipaddr, unsigned long vlifetime, uint8_t type)
Add a unicast address to the interface.
void uip_ds6_set_addr_iid(uip_ipaddr_t *ipaddr, const uip_lladdr_t *lladdr)
set the last 64 bits of an IP address based on the MAC address
Header file for the LED HAL.
Header file for the logging system.
Include file for the Contiki low-layer network stack (NETSTACK)
Header file for the Packet queue buffer management.
Generic serial I/O process header filer.
This is the structure of a seed.
uint8_t u8[(AES_128_KEY_LENGTH+AES_128_BLOCK_SIZE)]
for convenience
A set of debugging macros for the IP stack.
Header file for IPv6-related data structures.
static uip_ipaddr_t ipaddr
Pointer to prefix information option in uip_buf.
static uip_ds6_addr_t * addr
Pointer to a nbr cache entry.
Header file for the uIP TCP/IP stack.