Contiki 3.x
contiki-main.c
1 /**
2  * Copyright (c) 2014, Analog Devices, Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted (subject to the limitations in the
6  * disclaimer below) provided that the following conditions are met:
7  *
8  * - Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * - Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the
14  * distribution.
15  *
16  * - Neither the name of Analog Devices, Inc. nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
21  * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
22  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
23  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
29  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
31  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
32  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 /**
35  * \author Jim Paris <jim.paris@rigado.com>
36  */
37 
38 #include <stdbool.h>
39 #include <stdio.h>
40 #include <string.h>
41 
42 #include "contiki.h"
43 #include "net/netstack.h"
44 
45 #include "dev/serial-line.h"
46 
47 #include "net/ip/uip.h"
48 
49 #include "dev/button-sensor.h"
50 #include "dev/leds.h"
51 
52 #if WITH_UIP6
53 #include "net/ipv6/uip-ds6.h"
54 #endif /* WITH_UIP6 */
55 
56 #include "net/rime/rime.h"
57 
58 #include "uart.h"
59 #include "watchdog.h"
60 
61 SENSORS(&button_sensor);
62 
63 #ifndef SERIAL_ID
64 #define SERIAL_ID { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }
65 #endif
66 
67 uint8_t serial_id[] = SERIAL_ID;
68 uint16_t node_id = 0x0102;
69 
70 /*---------------------------------------------------------------------------*/
71 static void
72 set_rime_addr(void)
73 {
74  linkaddr_t addr;
75  int i;
76 
77  memset(&addr, 0, sizeof(linkaddr_t));
78 #if UIP_CONF_IPV6
79  memcpy(addr.u8, serial_id, sizeof(addr.u8));
80 #else
81  if(node_id == 0) {
82  for(i = 0; i < sizeof(linkaddr_t); ++i) {
83  addr.u8[i] = serial_id[7 - i];
84  }
85  } else {
86  addr.u8[0] = node_id & 0xff;
87  addr.u8[1] = node_id >> 8;
88  }
89 #endif
91  printf("Rime started with address ");
92  for(i = 0; i < sizeof(addr.u8) - 1; i++) {
93  printf("%d.", addr.u8[i]);
94  }
95  printf("%d\n", addr.u8[i]);
96 }
97 /*---------------------------------------------------------------------------*/
98 static void
99 set_rf_params(void)
100 {
101  int chan;
102  NETSTACK_RADIO.set_value(RADIO_PARAM_CHANNEL, RF_CHANNEL);
103  NETSTACK_RADIO.get_value(RADIO_PARAM_CHANNEL, &chan);
104  printf("RF channel set to %d Hz\n", chan);
105 }
106 /*---------------------------------------------------------------------------*/
107 int contiki_argc = 0;
108 char **contiki_argv;
109 
110 int
111 main(int argc, char **argv)
112 {
113  watchdog_init();
114  leds_init();
115  uart_init(115200);
116  clock_init();
117 
118 #if UIP_CONF_IPV6
119 #if UIP_CONF_IPV6_RPL
120  printf(CONTIKI_VERSION_STRING " started with IPV6, RPL\n");
121 #else
122  printf(CONTIKI_VERSION_STRING " started with IPV6\n");
123 #endif
124 #else
125  printf(CONTIKI_VERSION_STRING " started\n");
126 #endif
127 
128  contiki_argc = argc;
129  contiki_argv = argv;
130 
131  process_init();
132  process_start(&etimer_process, NULL);
133  ctimer_init();
134  rtimer_init();
135 
136  set_rime_addr();
137 
138  queuebuf_init();
139 
140  set_rf_params();
141  netstack_init();
142  printf("MAC %s RDC %s NETWORK %s\n",
143  NETSTACK_MAC.name, NETSTACK_RDC.name, NETSTACK_NETWORK.name);
144 
145 #if WITH_UIP6
146  memcpy(&uip_lladdr.addr, serial_id, sizeof(uip_lladdr.addr));
147 
148  process_start(&tcpip_process, NULL);
149  printf("Tentative link-local IPv6 address ");
150  {
151  uip_ds6_addr_t *lladdr;
152  int i;
153  lladdr = uip_ds6_get_link_local(-1);
154  for(i = 0; i < 8; i++) {
155  printf("%02x%02x%c", lladdr->ipaddr.u8[i * 2],
156  lladdr->ipaddr.u8[i * 2 + 1],
157  i == 7 ? '\n' : ':');
158  }
159  /* make it hardcoded... */
160  lladdr->state = ADDR_AUTOCONF;
161  }
162 #else
163  process_start(&tcpip_process, NULL);
164 #endif
165 
166  serial_line_init();
167  process_start(&sensors_process, NULL);
168 
169  autostart_start(autostart_processes);
170 
171  while(1) {
173 
174  process_run();
175  }
176 
177  return 0;
178 }
179 /*---------------------------------------------------------------------------*/
180 void
181 log_message(char *m1, char *m2)
182 {
183  printf("%s%s\n", m1, m2);
184 }
185 /*---------------------------------------------------------------------------*/
186 void
187 uip_log(char *m)
188 {
189  printf("%s\n", m);
190 }
191 /*---------------------------------------------------------------------------*/
192 void
193 _xassert(const char *file, int line)
194 {
195  printf("%s:%u: failed assertion\n", file, line);
196  for(;;) {
197  continue;
198  }
199 }
CCIF uip_lladdr_t uip_lladdr
Host L2 address.
Definition: uip6.c:115
Unicast address structure.
Definition: uip-ds6.h:172
Header file for the uIP TCP/IP stack.
void rtimer_init(void)
Initialize the real-time scheduler.
Definition: rtimer.c:61
void uart_init(const unsigned int uart_num, uint32_t module_clk_hz, const uint32_t baud)
Initialize UART.
Definition: uart.c:154
Network interface and stateless autoconfiguration (RFC 4862)
#define NULL
The null pointer.
void uip_log(char *msg)
Print out a uIP log message.
Definition: uip-log.c:3
void clock_init(void)
Initialize the clock library.
Definition: clock.c:76
int main(void)
This is main...
Definition: ethconfig.c:49
void process_init(void)
Initialize the process module.
Definition: process.c:208
Header file for the Rime stack
Generic serial I/O process header filer.
void watchdog_periodic(void)
Writes the WDT clear sequence.
Definition: watchdog.c:64
void ctimer_init(void)
Initialize the callback timer library.
Definition: ctimer.c:91
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
int process_run(void)
Run the system once - call poll handlers and process one event.
Definition: process.c:302
void linkaddr_set_node_addr(linkaddr_t *t)
Set the address of the current node.
Definition: linkaddr.c:72
void watchdog_init(void)
Copyright (c) 2014, Analog Devices, Inc.
Definition: watchdog.c:42
Include file for the Contiki low-layer network stack (NETSTACK)