Allow ip4 address override, show ip6 address
[contiki-2.x.git] / platform / minimal-net / contiki-main.c
blobe678f312d17ee86ae4572e40bc0c6f7cfc29033f
1 /*
2 * Copyright (c) 2002, Adam Dunkels.
3 * All rights reserved.
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.
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.
30 * This file is part of the Contiki OS
32 * $Id: contiki-main.c,v 1.23 2010/02/02 17:51:55 dak664 Exp $
36 #include <stdio.h>
37 #include <time.h>
38 #include <sys/select.h>
39 #include <unistd.h>
40 #include <memory.h>
42 #include "contiki.h"
43 #include "contiki-net.h"
45 #include "dev/serial-line.h"
47 #include "net/uip.h"
48 #ifdef __CYGWIN__
49 #include "net/wpcap-drv.h"
50 #else /* __CYGWIN__ */
51 #include "net/tapdev-drv.h"
52 #endif /* __CYGWIN__ */
54 #ifdef __CYGWIN__
55 PROCINIT(&etimer_process, &tcpip_process, &wpcap_process, &serial_line_process);
56 #else /* __CYGWIN__ */
57 PROCINIT(&etimer_process, &tapdev_process, &tcpip_process, &serial_line_process);
58 #endif /* __CYGWIN__ */
60 #if UIP_CONF_IPV6
61 /*---------------------------------------------------------------------------*/
62 void
63 sprint_ip6(uip_ip6addr_t addr)
65 unsigned char i = 0;
66 unsigned char zerocnt = 0;
67 unsigned char numprinted = 0;
68 char thestring[40];
69 char * result = thestring;
71 *result++='[';
72 while (numprinted < 8) {
73 if ((addr.u16[i] == 0) && (zerocnt == 0)) {
74 while(addr.u16[zerocnt + i] == 0) zerocnt++;
75 if (zerocnt == 1) {
76 *result++ = '0';
77 numprinted++;
78 break;
80 i += zerocnt;
81 numprinted += zerocnt;
82 } else {
83 result += sprintf(result, "%x", (unsigned int)(ntohs(addr.u16[i])));
84 i++;
85 numprinted++;
87 if (numprinted != 8) *result++ = ':';
89 *result++=']';
90 *result=0;
91 printf("%s",thestring);
93 #endif /* UIP_CONF_IPV6 */
94 /*---------------------------------------------------------------------------*/
96 /*---------------------------------------------------------------------------*/
97 int
98 main(void)
101 process_init();
103 procinit_init();
105 ctimer_init();
107 autostart_start(autostart_processes);
109 /* Set default IP addresses if not specified */
110 #if !UIP_CONF_IPV6
111 uip_ipaddr_t addr;
113 uip_gethostaddr(&addr);
114 if (addr.u8[0]==0) {
115 uip_ipaddr(&addr, 10,1,1,1);
117 printf("IP Address: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&addr));
118 uip_sethostaddr(&addr);
120 uip_getnetmask(&addr);
121 if (addr.u8[0]==0) {
122 uip_ipaddr(&addr, 255,0,0,0);
123 uip_setnetmask(&addr);
125 printf("Subnet Mask: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&addr));
127 uip_getdraddr(&addr);
128 if (addr.u8[0]==0) {
129 uip_ipaddr(&addr, 10,1,1,100);
130 uip_setdraddr(&addr);
132 printf("Def. Router: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&addr));
134 #else /* !UIP_CONF_IPV6 */
135 uip_ipaddr_t ipaddr;
137 uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0);
138 uip_netif_addr_autoconf_set(&ipaddr, &uip_lladdr);
139 uip_netif_addr_add(&ipaddr, 16, 0, TENTATIVE);
140 printf("IP6 Address: ");sprint_ip6(ipaddr);printf("\n");
141 #endif /* !UIP_CONF_IPV6 */
143 /* Make standard output unbuffered. */
144 setvbuf(stdout, (char *)NULL, _IONBF, 0);
146 while(1) {
147 fd_set fds;
148 int n;
149 struct timeval tv;
151 n = process_run();
152 /* if(n > 0) {
153 printf("%d processes in queue\n");
156 tv.tv_sec = 0;
157 tv.tv_usec = 1;
158 FD_ZERO(&fds);
159 FD_SET(STDIN_FILENO, &fds);
160 select(1, &fds, NULL, NULL, &tv);
162 if(FD_ISSET(STDIN_FILENO, &fds)) {
163 char c;
164 if(read(STDIN_FILENO, &c, 1) > 0) {
165 serial_line_input_byte(c);
168 etimer_request_poll();
171 return 0;
173 /*---------------------------------------------------------------------------*/
174 void log_message(char *m1, char *m2)
176 printf("%s%s\n", m1, m2);
178 /*---------------------------------------------------------------------------*/
179 void
180 uip_log(char *m)
182 printf("uIP: '%s'\n", m);
184 /*---------------------------------------------------------------------------*/
185 unsigned short
186 sensors_light1(void)
188 static unsigned short count;
189 return count++;
191 /*---------------------------------------------------------------------------*/