Contiki-NG
Loading...
Searching...
No Matches
border-router-native.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011, Swedish Institute of Computer Science.
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
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * This file is part of the Contiki operating system.
30 *
31 */
32/**
33 * \file
34 * border-router
35 * \author
36 * Niclas Finne <nfi@sics.se>
37 * Joakim Eriksson <joakime@sics.se>
38 * Nicolas Tsiftes <nvt@sics.se>
39 */
40
41#include "contiki.h"
42#include "contiki-net.h"
43
44#include "net/routing/routing.h"
45#include "rpl-border-router.h"
46#include "cmd.h"
47#include "border-router.h"
48#include "border-router-cmds.h"
49#include "tun6-net.h"
50
51/*---------------------------------------------------------------------------*/
52/* Log configuration */
53#include "sys/log.h"
54#define LOG_MODULE "BR"
55#define LOG_LEVEL LOG_LEVEL_INFO
56
57#include <stdlib.h>
58#include <stdbool.h>
59
60extern long slip_sent;
61extern long slip_received;
62
63static bool is_mac_set;
64
65extern int contiki_argc;
66extern char **contiki_argv;
67
68CMD_HANDLERS(border_router_cmd_handler);
69
70PROCESS(border_router_process, "Border router process");
71
72/*---------------------------------------------------------------------------*/
73static void
74request_mac(void)
75{
76 write_to_slip((uint8_t *)"?M", 2);
77}
78/*---------------------------------------------------------------------------*/
79void
80border_router_set_mac(const uint8_t *data)
81{
82 if(is_mac_set) {
83 /* only set MAC address once */
84 return;
85 }
86
87 memcpy(uip_lladdr.addr, data, sizeof(uip_lladdr.addr));
88 linkaddr_set_node_addr((linkaddr_t *)uip_lladdr.addr);
89
90 /* is this ok - should instead remove all addresses and
91 add them back again - a bit messy... ?*/
92 PROCESS_CONTEXT_BEGIN(&tcpip_process);
94 NETSTACK_ROUTING.init();
95 PROCESS_CONTEXT_END(&tcpip_process);
96
97 is_mac_set = true;
98}
99/*---------------------------------------------------------------------------*/
100void
101border_router_print_stat()
102{
103 printf("bytes received over SLIP: %ld\n", slip_received);
104 printf("bytes sent over SLIP: %ld\n", slip_sent);
105}
106/*---------------------------------------------------------------------------*/
107PROCESS_THREAD(border_router_process, ev, data)
108{
109 static struct etimer et;
110
112
113 is_mac_set = false;
114 prefix_set = 0;
115
117
118 process_start(&border_router_cmd_process, NULL);
119
120 LOG_INFO("RPL-Border router started\n");
121
122 slip_config_handle_arguments(contiki_argc, contiki_argv);
123
124 /* tun init is also responsible for setting up the SLIP connection */
125 tun_init();
126
127 while(!is_mac_set) {
129 request_mac();
131 }
132
133 const char *config_ipaddr = tun6_net_get_prefix();
134 if(config_ipaddr != NULL) {
135 uip_ipaddr_t prefix;
136
137 if(uiplib_ipaddrconv(config_ipaddr, &prefix)) {
138 LOG_INFO("Setting prefix ");
139 LOG_INFO_6ADDR(&prefix);
140 LOG_INFO_("\n");
141 set_prefix_64(&prefix);
142 } else {
143 LOG_ERR("Parse error: %s\n", config_ipaddr);
144 exit(EXIT_FAILURE);
145 }
146 }
147
148 print_local_addresses();
149
150 while(1) {
151 etimer_set(&et, CLOCK_SECOND * 2);
153 /* do anything here??? */
154 }
155
156 PROCESS_END();
157}
158/*---------------------------------------------------------------------------*/
Sets up some commands for the border router.
Border router header file.
Simple command handler.
#define CLOCK_SECOND
A second, measured in system clock time.
Definition clock.h:105
static bool etimer_expired(struct etimer *et)
Check if an event timer has expired.
Definition etimer.h:201
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
Definition etimer.c:177
static void linkaddr_set_node_addr(linkaddr_t *addr)
Set the address of the current node.
Definition linkaddr.h:124
#define PROCESS(name, strname)
Declare a process.
Definition process.h:309
#define PROCESS_PAUSE()
Yield the process for a short while.
Definition process.h:223
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition process.h:122
#define PROCESS_WAIT_EVENT_UNTIL(c)
Wait for an event to be posted to the process, with an extra condition.
Definition process.h:159
#define PROCESS_END()
Define the end of a process.
Definition process.h:133
void process_start(struct process *p, process_data_t data)
Start a process.
Definition process.c:121
#define PROCESS_CONTEXT_BEGIN(p)
Switch context to another process.
Definition process.h:429
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition process.h:275
#define PROCESS_CONTEXT_END(p)
End a context switch.
Definition process.h:443
#define uiplib_ipaddrconv
Convert a textual representation of an IP address to a numerical representation.
Definition uiplib.h:71
uip_lladdr_t uip_lladdr
Host L2 address.
Definition uip6.c:107
void uip_ds6_init(void)
Initialize data structures.
Definition uip-ds6.c:116
Header file for the logging system.
Routing driver header file.
A timer.
Definition etimer.h:79
void(* init)(void)
Initialize the routing protocol.
Definition routing.h:63