Contiki-Inga 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 #include <stdio.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <sys/select.h>
38 
39 #ifdef __CYGWIN__
40 #include "net/wpcap-drv.h"
41 #endif /* __CYGWIN__ */
42 
43 #include "contiki.h"
44 #include "net/netstack.h"
45 
46 #include "ctk/ctk.h"
47 #include "ctk/ctk-curses.h"
48 
49 #include "dev/serial-line.h"
50 
51 #include "net/ip/uip.h"
52 
53 #include "dev/button-sensor.h"
54 #include "dev/pir-sensor.h"
55 #include "dev/vib-sensor.h"
56 
57 #if WITH_UIP6
58 #include "net/ipv6/uip-ds6.h"
59 #endif /* WITH_UIP6 */
60 
61 #include "net/rime/rime.h"
62 
63 #ifdef SELECT_CONF_MAX
64 #define SELECT_MAX SELECT_CONF_MAX
65 #else
66 #define SELECT_MAX 8
67 #endif
68 
69 static const struct select_callback *select_callback[SELECT_MAX];
70 static int select_max = 0;
71 
72 SENSORS(&pir_sensor, &vib_sensor, &button_sensor);
73 
74 static uint8_t serial_id[] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};
75 static uint16_t node_id = 0x0102;
76 /*---------------------------------------------------------------------------*/
77 int
78 select_set_callback(int fd, const struct select_callback *callback)
79 {
80  int i;
81  if(fd >= 0 && fd < SELECT_MAX) {
82  /* Check that the callback functions are set */
83  if(callback != NULL &&
84  (callback->set_fd == NULL || callback->handle_fd == NULL)) {
85  callback = NULL;
86  }
87 
88  select_callback[fd] = callback;
89 
90  /* Update fd max */
91  if(callback != NULL) {
92  if(fd > select_max) {
93  select_max = fd;
94  }
95  } else {
96  select_max = 0;
97  for(i = SELECT_MAX - 1; i > 0; i--) {
98  if(select_callback[i] != NULL) {
99  select_max = i;
100  break;
101  }
102  }
103  }
104  return 1;
105  }
106  return 0;
107 }
108 /*---------------------------------------------------------------------------*/
109 static int
110 stdin_set_fd(fd_set *rset, fd_set *wset)
111 {
112  FD_SET(STDIN_FILENO, rset);
113  return 1;
114 }
115 static void
116 stdin_handle_fd(fd_set *rset, fd_set *wset)
117 {
118  char c;
119  if(FD_ISSET(STDIN_FILENO, rset)) {
120  if(read(STDIN_FILENO, &c, 1) > 0) {
121  serial_line_input_byte(c);
122  }
123  }
124 }
125 const static struct select_callback stdin_fd = {
126  stdin_set_fd, stdin_handle_fd
127 };
128 /*---------------------------------------------------------------------------*/
129 static void
130 set_rime_addr(void)
131 {
132  linkaddr_t addr;
133  int i;
134 
135  memset(&addr, 0, sizeof(linkaddr_t));
136 #if UIP_CONF_IPV6
137  memcpy(addr.u8, serial_id, sizeof(addr.u8));
138 #else
139  if(node_id == 0) {
140  for(i = 0; i < sizeof(linkaddr_t); ++i) {
141  addr.u8[i] = serial_id[7 - i];
142  }
143  } else {
144  addr.u8[0] = node_id & 0xff;
145  addr.u8[1] = node_id >> 8;
146  }
147 #endif
148  linkaddr_set_node_addr(&addr);
149  printf("Rime started with address ");
150  for(i = 0; i < sizeof(addr.u8) - 1; i++) {
151  printf("%d.", addr.u8[i]);
152  }
153  printf("%d\n", addr.u8[i]);
154 }
155 
156 
157 /*---------------------------------------------------------------------------*/
158 int contiki_argc = 0;
159 char **contiki_argv;
160 
161 int
162 main(int argc, char **argv)
163 {
164 #if UIP_CONF_IPV6
165 #if UIP_CONF_IPV6_RPL
166  printf(CONTIKI_VERSION_STRING " started with IPV6, RPL\n");
167 #else
168  printf(CONTIKI_VERSION_STRING " started with IPV6\n");
169 #endif
170 #else
171  printf(CONTIKI_VERSION_STRING " started\n");
172 #endif
173 
174  /* crappy way of remembering and accessing argc/v */
175  contiki_argc = argc;
176  contiki_argv = argv;
177 
178  /* native under windows is hardcoded to use the first one or two args */
179  /* for wpcap configuration so this needs to be "removed" from */
180  /* contiki_args (used by the native-border-router) */
181 #ifdef __CYGWIN__
182  contiki_argc--;
183  contiki_argv++;
184 #ifdef UIP_FALLBACK_INTERFACE
185  contiki_argc--;
186  contiki_argv++;
187 #endif
188 #endif
189 
190  process_init();
191  process_start(&etimer_process, NULL);
192  ctimer_init();
193 
194 #if WITH_GUI
195  process_start(&ctk_process, NULL);
196 #endif
197 
198  set_rime_addr();
199 
200  queuebuf_init();
201 
202  netstack_init();
203  printf("MAC %s RDC %s NETWORK %s\n", NETSTACK_MAC.name, NETSTACK_RDC.name, NETSTACK_NETWORK.name);
204 
205 #if WITH_UIP6
206  memcpy(&uip_lladdr.addr, serial_id, sizeof(uip_lladdr.addr));
207 
208  process_start(&tcpip_process, NULL);
209 #ifdef __CYGWIN__
210  process_start(&wpcap_process, NULL);
211 #endif
212  printf("Tentative link-local IPv6 address ");
213  {
214  uip_ds6_addr_t *lladdr;
215  int i;
216  lladdr = uip_ds6_get_link_local(-1);
217  for(i = 0; i < 7; ++i) {
218  printf("%02x%02x:", lladdr->ipaddr.u8[i * 2],
219  lladdr->ipaddr.u8[i * 2 + 1]);
220  }
221  /* make it hardcoded... */
222  lladdr->state = ADDR_AUTOCONF;
223 
224  printf("%02x%02x\n", lladdr->ipaddr.u8[14], lladdr->ipaddr.u8[15]);
225  }
226 #else
227  process_start(&tcpip_process, NULL);
228 #endif
229 
230  serial_line_init();
231 
232  autostart_start(autostart_processes);
233 
234  /* Make standard output unbuffered. */
235  setvbuf(stdout, (char *)NULL, _IONBF, 0);
236 
237  select_set_callback(STDIN_FILENO, &stdin_fd);
238  while(1) {
239  fd_set fdr;
240  fd_set fdw;
241  int maxfd;
242  int i;
243  int retval;
244  struct timeval tv;
245 
246  retval = process_run();
247 
248  tv.tv_sec = 0;
249  tv.tv_usec = retval ? 1 : 1000;
250 
251  FD_ZERO(&fdr);
252  FD_ZERO(&fdw);
253  maxfd = 0;
254  for(i = 0; i <= select_max; i++) {
255  if(select_callback[i] != NULL && select_callback[i]->set_fd(&fdr, &fdw)) {
256  maxfd = i;
257  }
258  }
259 
260  retval = select(maxfd + 1, &fdr, &fdw, NULL, &tv);
261  if(retval < 0) {
262  perror("select");
263  } else if(retval > 0) {
264  /* timeout => retval == 0 */
265  for(i = 0; i <= maxfd; i++) {
266  if(select_callback[i] != NULL) {
267  select_callback[i]->handle_fd(&fdr, &fdw);
268  }
269  }
270  }
271 
273 
274 #if WITH_GUI
275  if(console_resize()) {
276  ctk_restore();
277  }
278 #endif /* WITH_GUI */
279  }
280 
281  return 0;
282 }
283 /*---------------------------------------------------------------------------*/
284 void
285 log_message(char *m1, char *m2)
286 {
287  fprintf(stderr, "%s%s\n", m1, m2);
288 }
289 /*---------------------------------------------------------------------------*/
290 void
291 uip_log(char *m)
292 {
293  fprintf(stderr, "%s\n", m);
294 }
295 /*---------------------------------------------------------------------------*/