Contiki 3.x
contiki-main.c
1 /*
2  * Copyright (c) 2002, Adam Dunkels.
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
11  * copyright notice, this list of conditions and the following
12  * disclaimer in the documentation and/or other materials provided
13  * with the distribution.
14  * 3. The name of the author may not be used to endorse or promote
15  * products derived from this software without specific prior
16  * written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * This file is part of the Contiki OS
31  *
32  */
33 
34 /**
35  * \ingroup platform
36  *
37  * \defgroup native_platform Native platform
38  *
39  * Platform running in the host (Windows or Linux) environment.
40  *
41  * Used mainly for development and debugging.
42  * @{
43  */
44 
45 #include <stdio.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <sys/select.h>
49 #include <errno.h>
50 
51 #ifdef __CYGWIN__
52 #include "net/wpcap-drv.h"
53 #endif /* __CYGWIN__ */
54 
55 #include "contiki.h"
56 #include "net/netstack.h"
57 
58 #include "ctk/ctk.h"
59 #include "ctk/ctk-curses.h"
60 
61 #include "dev/serial-line.h"
62 
63 #include "net/ip/uip.h"
64 
65 #include "dev/button-sensor.h"
66 #include "dev/pir-sensor.h"
67 #include "dev/vib-sensor.h"
68 
69 #if NETSTACK_CONF_WITH_IPV6
70 #include "net/ipv6/uip-ds6.h"
71 #endif /* NETSTACK_CONF_WITH_IPV6 */
72 
73 #include "net/rime/rime.h"
74 
75 #ifdef SELECT_CONF_MAX
76 #define SELECT_MAX SELECT_CONF_MAX
77 #else
78 #define SELECT_MAX 8
79 #endif
80 
81 static const struct select_callback *select_callback[SELECT_MAX];
82 static int select_max = 0;
83 
84 SENSORS(&pir_sensor, &vib_sensor, &button_sensor);
85 
86 static uint8_t serial_id[] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};
87 #if !NETSTACK_CONF_WITH_IPV6
88 static uint16_t node_id = 0x0102;
89 #endif /* !NETSTACK_CONF_WITH_IPV6 */
90 /*---------------------------------------------------------------------------*/
91 int
92 select_set_callback(int fd, const struct select_callback *callback)
93 {
94  int i;
95  if(fd >= 0 && fd < SELECT_MAX) {
96  /* Check that the callback functions are set */
97  if(callback != NULL &&
98  (callback->set_fd == NULL || callback->handle_fd == NULL)) {
99  callback = NULL;
100  }
101 
102  select_callback[fd] = callback;
103 
104  /* Update fd max */
105  if(callback != NULL) {
106  if(fd > select_max) {
107  select_max = fd;
108  }
109  } else {
110  select_max = 0;
111  for(i = SELECT_MAX - 1; i > 0; i--) {
112  if(select_callback[i] != NULL) {
113  select_max = i;
114  break;
115  }
116  }
117  }
118  return 1;
119  }
120  return 0;
121 }
122 /*---------------------------------------------------------------------------*/
123 static int
124 stdin_set_fd(fd_set *rset, fd_set *wset)
125 {
126  FD_SET(STDIN_FILENO, rset);
127  return 1;
128 }
129 static void
130 stdin_handle_fd(fd_set *rset, fd_set *wset)
131 {
132  char c;
133  if(FD_ISSET(STDIN_FILENO, rset)) {
134  if(read(STDIN_FILENO, &c, 1) > 0) {
136  }
137  }
138 }
139 const static struct select_callback stdin_fd = {
140  stdin_set_fd, stdin_handle_fd
141 };
142 /*---------------------------------------------------------------------------*/
143 static void
144 set_rime_addr(void)
145 {
146  linkaddr_t addr;
147  int i;
148 
149  memset(&addr, 0, sizeof(linkaddr_t));
150 #if NETSTACK_CONF_WITH_IPV6
151  memcpy(addr.u8, serial_id, sizeof(addr.u8));
152 #else
153  if(node_id == 0) {
154  for(i = 0; i < sizeof(linkaddr_t); ++i) {
155  addr.u8[i] = serial_id[7 - i];
156  }
157  } else {
158  addr.u8[0] = node_id & 0xff;
159  addr.u8[1] = node_id >> 8;
160  }
161 #endif
162  linkaddr_set_node_addr(&addr);
163  printf("Rime started with address ");
164  for(i = 0; i < sizeof(addr.u8) - 1; i++) {
165  printf("%d.", addr.u8[i]);
166  }
167  printf("%d\n", addr.u8[i]);
168 }
169 
170 
171 /*---------------------------------------------------------------------------*/
172 int contiki_argc = 0;
173 char **contiki_argv;
174 
175 int
176 main(int argc, char **argv)
177 {
178 #if NETSTACK_CONF_WITH_IPV6
179 #if UIP_CONF_IPV6_RPL
180  printf(CONTIKI_VERSION_STRING " started with IPV6, RPL\n");
181 #else
182  printf(CONTIKI_VERSION_STRING " started with IPV6\n");
183 #endif
184 #else
185  printf(CONTIKI_VERSION_STRING " started\n");
186 #endif
187 
188  /* crappy way of remembering and accessing argc/v */
189  contiki_argc = argc;
190  contiki_argv = argv;
191 
192  /* native under windows is hardcoded to use the first one or two args */
193  /* for wpcap configuration so this needs to be "removed" from */
194  /* contiki_args (used by the native-border-router) */
195 #ifdef __CYGWIN__
196  contiki_argc--;
197  contiki_argv++;
198 #ifdef UIP_FALLBACK_INTERFACE
199  contiki_argc--;
200  contiki_argv++;
201 #endif
202 #endif
203 
204  process_init();
205  process_start(&etimer_process, NULL);
206  ctimer_init();
207  rtimer_init();
208 
209 #if WITH_GUI
210  process_start(&ctk_process, NULL);
211 #endif
212 
213  set_rime_addr();
214 
215  netstack_init();
216  printf("MAC %s RDC %s NETWORK %s\n", NETSTACK_MAC.name, NETSTACK_RDC.name, NETSTACK_NETWORK.name);
217 
218 #if NETSTACK_CONF_WITH_IPV6
219  queuebuf_init();
220 
221  memcpy(&uip_lladdr.addr, serial_id, sizeof(uip_lladdr.addr));
222 
223  process_start(&tcpip_process, NULL);
224 #ifdef __CYGWIN__
225  process_start(&wpcap_process, NULL);
226 #endif
227  printf("Tentative link-local IPv6 address ");
228  {
229  uip_ds6_addr_t *lladdr;
230  int i;
231  lladdr = uip_ds6_get_link_local(-1);
232  for(i = 0; i < 7; ++i) {
233  printf("%02x%02x:", lladdr->ipaddr.u8[i * 2],
234  lladdr->ipaddr.u8[i * 2 + 1]);
235  }
236  /* make it hardcoded... */
237  lladdr->state = ADDR_AUTOCONF;
238 
239  printf("%02x%02x\n", lladdr->ipaddr.u8[14], lladdr->ipaddr.u8[15]);
240  }
241 #elif NETSTACK_CONF_WITH_IPV4
242  process_start(&tcpip_process, NULL);
243 #endif
244 
245  serial_line_init();
246 
247  autostart_start(autostart_processes);
248 
249  /* Make standard output unbuffered. */
250  setvbuf(stdout, (char *)NULL, _IONBF, 0);
251 
252  select_set_callback(STDIN_FILENO, &stdin_fd);
253  while(1) {
254  fd_set fdr;
255  fd_set fdw;
256  int maxfd;
257  int i;
258  int retval;
259  struct timeval tv;
260 
261  retval = process_run();
262 
263  tv.tv_sec = 0;
264  tv.tv_usec = retval ? 1 : 1000;
265 
266  FD_ZERO(&fdr);
267  FD_ZERO(&fdw);
268  maxfd = 0;
269  for(i = 0; i <= select_max; i++) {
270  if(select_callback[i] != NULL && select_callback[i]->set_fd(&fdr, &fdw)) {
271  maxfd = i;
272  }
273  }
274 
275  retval = select(maxfd + 1, &fdr, &fdw, NULL, &tv);
276  if(retval < 0) {
277  if(errno != EINTR) {
278  perror("select");
279  }
280  } else if(retval > 0) {
281  /* timeout => retval == 0 */
282  for(i = 0; i <= maxfd; i++) {
283  if(select_callback[i] != NULL) {
284  select_callback[i]->handle_fd(&fdr, &fdw);
285  }
286  }
287  }
288 
290 
291 #if WITH_GUI
292  if(console_resize()) {
293  ctk_restore();
294  }
295 #endif /* WITH_GUI */
296  }
297 
298  return 0;
299 }
300 /*---------------------------------------------------------------------------*/
301 void
302 log_message(char *m1, char *m2)
303 {
304  fprintf(stderr, "%s%s\n", m1, m2);
305 }
306 /*---------------------------------------------------------------------------*/
307 void
308 uip_log(char *m)
309 {
310  fprintf(stderr, "%s\n", m);
311 }
312 /*---------------------------------------------------------------------------*/
313 /** @} */
314 
void uip_log(char *m)
Print out a uIP log message.
Definition: contiki-main.c:187
static uip_ds6_addr_t * addr
Pointer to a router list entry.
Definition: uip-nd6.c:124
Header file for IPv6-related data structures.
void etimer_request_poll(void)
Make the event timer aware that the clock has changed.
Definition: etimer.c:145
CTK header file.
int process_run(void)
Run the system once - call poll handlers and process one event.
Definition: process.c:302
Unicast address structure.
Definition: uip-ds6.h:202
SENSORS & button_sensor
Copyright (c) 2014, Analog Devices, Inc.
Definition: contiki-main.c:61
Header file for the Rime stack
#define NULL
The null pointer.
void linkaddr_set_node_addr(linkaddr_t *t)
Set the address of the current node.
Definition: linkaddr.c:72
Header file for the uIP TCP/IP stack.
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
int main(void)
This is main...
Definition: contiki-main.c:456
void rtimer_init(void)
Initialize the real-time scheduler.
Definition: rtimer.c:61
Generic serial I/O process header filer.
CCIF uip_lladdr_t uip_lladdr
Host L2 address.
Definition: uip.c:118
void process_init(void)
Initialize the process module.
Definition: process.c:208
void ctimer_init(void)
Initialize the callback timer library.
Definition: ctimer.c:91
int serial_line_input_byte(unsigned char c)
Get one byte of input from the serial driver.
Definition: serial-line.c:60
Include file for the Contiki low-layer network stack (NETSTACK)