Contiki-NG
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 
50 /*---------------------------------------------------------------------------*/
51 /* Log configuration */
52 #include "sys/log.h"
53 #define LOG_MODULE "BR"
54 #define LOG_LEVEL LOG_LEVEL_INFO
55 
56 #include <stdlib.h>
57 
58 extern long slip_sent;
59 extern long slip_received;
60 
61 static uint8_t mac_set;
62 
63 extern int contiki_argc;
64 extern char **contiki_argv;
65 extern const char *slip_config_ipaddr;
66 
67 CMD_HANDLERS(border_router_cmd_handler);
68 
69 PROCESS(border_router_process, "Border router process");
70 
71 /*---------------------------------------------------------------------------*/
72 static void
73 request_mac(void)
74 {
75  write_to_slip((uint8_t *)"?M", 2);
76 }
77 /*---------------------------------------------------------------------------*/
78 void
79 border_router_set_mac(const uint8_t *data)
80 {
81  memcpy(uip_lladdr.addr, data, sizeof(uip_lladdr.addr));
82  linkaddr_set_node_addr((linkaddr_t *)uip_lladdr.addr);
83 
84  /* is this ok - should instead remove all addresses and
85  add them back again - a bit messy... ?*/
86  PROCESS_CONTEXT_BEGIN(&tcpip_process);
87  uip_ds6_init();
88  NETSTACK_ROUTING.init();
89  PROCESS_CONTEXT_END(&tcpip_process);
90 
91  mac_set = 1;
92 }
93 /*---------------------------------------------------------------------------*/
94 void
95 border_router_print_stat()
96 {
97  printf("bytes received over SLIP: %ld\n", slip_received);
98  printf("bytes sent over SLIP: %ld\n", slip_sent);
99 }
100 /*---------------------------------------------------------------------------*/
101 PROCESS_THREAD(border_router_process, ev, data)
102 {
103  static struct etimer et;
104 
105  PROCESS_BEGIN();
106  prefix_set = 0;
107 
108  PROCESS_PAUSE();
109 
110  process_start(&border_router_cmd_process, NULL);
111 
112  LOG_INFO("RPL-Border router started\n");
113 
114  slip_config_handle_arguments(contiki_argc, contiki_argv);
115 
116  /* tun init is also responsible for setting up the SLIP connection */
117  tun_init();
118 
119  while(!mac_set) {
120  etimer_set(&et, CLOCK_SECOND);
121  request_mac();
123  }
124 
125  if(slip_config_ipaddr != NULL) {
126  uip_ipaddr_t prefix;
127 
128  if(uiplib_ipaddrconv((const char *)slip_config_ipaddr, &prefix)) {
129  LOG_INFO("Setting prefix ");
130  LOG_INFO_6ADDR(&prefix);
131  LOG_INFO_("\n");
132  set_prefix_64(&prefix);
133  } else {
134  LOG_ERR("Parse error: %s\n", slip_config_ipaddr);
135  exit(0);
136  }
137  }
138 
139  print_local_addresses();
140 
141  while(1) {
142  etimer_set(&et, CLOCK_SECOND * 2);
144  /* do anything here??? */
145  }
146 
147  PROCESS_END();
148 }
149 /*---------------------------------------------------------------------------*/
uip_lladdr_t uip_lladdr
Host L2 address.
Definition: uip6.c:107
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
#define PROCESS_CONTEXT_END(p)
End a context switch.
Definition: process.h:440
Border router header file
void uip_ds6_init(void)
Initialize data structures.
Definition: uip-ds6.c:116
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
#define PROCESS_WAIT_EVENT_UNTIL(c)
Wait for an event to be posted to the process, with an extra condition.
Definition: process.h:157
Simple command handler
#define PROCESS_PAUSE()
Yield the process for a short while.
Definition: process.h:221
void(* init)(void)
Initialize the routing protocol.
Definition: routing.h:63
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82
Routing driver header file
int etimer_expired(struct etimer *et)
Check if an event timer has expired.
Definition: etimer.c:213
A timer.
Definition: etimer.h:76
Sets up some commands for the border router
#define PROCESS_CONTEXT_BEGIN(p)
Switch context to another process.
Definition: process.h:426
PROCESS_THREAD(cc2538_rf_process, ev, data)
Implementation of the cc2538 RF driver process.
Definition: cc2538-rf.c:1110
#define uiplib_ipaddrconv
Convert a textual representation of an IP address to a numerical representation.
Definition: uiplib.h:71
Header file for the logging system
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
Definition: etimer.c:177
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
void linkaddr_set_node_addr(linkaddr_t *t)
Set the address of the current node.
Definition: linkaddr.c:75