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 if(read(STDIN_FILENO, &c, 1) > 0) {
185const static struct select_callback stdin_fd = {
186 stdin_set_fd, stdin_handle_fd
195 memset(&
addr, 0,
sizeof(linkaddr_t));
196#if NETSTACK_CONF_WITH_IPV6
197 memcpy(
addr.u8, mac_addr,
sizeof(
addr.u8));
200 for(i = 0; i <
sizeof(linkaddr_t); ++i) {
201 addr.u8[i] = mac_addr[7 - i];
207#if NETSTACK_CONF_WITH_IPV6
209set_global_address(
void)
216 uip_ip6addr_copy(&
ipaddr, default_prefix);
222 LOG_INFO(
"Added global IPv6 address ");
227 uip_ip6addr_copy(&
ipaddr, default_prefix);
229 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) {
268 if(NULL == input_handler) {
269 native_uart_set_input(serial_line_input_byte);
277#if NETSTACK_CONF_WITH_IPV6
278 set_global_address();
282 setvbuf(stdout, (
char *)NULL, _IONBF, 0);
289 select_set_callback(STDIN_FILENO, &stdin_fd);
301 tv.tv_sec = retval ? 0 : SELECT_TIMEOUT / 1000;
302 tv.tv_usec = retval ? 1 : (SELECT_TIMEOUT * 1000) % 1000000;
307 for(i = 0; i <= select_max; i++) {
308 if(select_callback[i] != NULL && select_callback[i]->set_fd(&fdr, &fdw)) {
313 retval = select(maxfd + 1, &fdr, &fdw, NULL, &tv);
318 }
else if(retval > 0) {
320 for(i = 0; i <= maxfd; i++) {
321 if(select_callback[i] != NULL) {
322 select_callback[i]->handle_fd(&fdr, &fdw);
332log_message(
char *m1,
char *m2)
334 fprintf(stderr,
"%s%s\n", m1, m2);
340 fprintf(stderr,
"%s\n", m);
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.
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.