contiki-main.c

00001 /*
00002  * Copyright (c) 2002, Adam Dunkels.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above
00011  *    copyright notice, this list of conditions and the following
00012  *    disclaimer in the documentation and/or other materials provided
00013  *    with the distribution.
00014  * 3. The name of the author may not be used to endorse or promote
00015  *    products derived from this software without specific prior
00016  *    written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
00019  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00020  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00022  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00023  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00024  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00026  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00027  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  * This file is part of the Contiki OS
00031  *
00032  * $Id: contiki-main.c,v 1.25 2010/10/19 18:29:05 adamdunkels Exp $
00033  *
00034  */
00035 
00036 #include <stdio.h>
00037 #include <time.h>
00038 #include <sys/select.h>
00039 #include <unistd.h>
00040 #include <memory.h>
00041 
00042 #include "contiki.h"
00043 #include "contiki-net.h"
00044 
00045 #include "dev/serial-line.h"
00046 
00047 #include "net/uip.h"
00048 #ifdef __CYGWIN__
00049 #include "net/wpcap-drv.h"
00050 #else /* __CYGWIN__ */
00051 #include "net/tapdev-drv.h"
00052 #endif /* __CYGWIN__ */
00053 
00054 #ifdef __CYGWIN__
00055 PROCINIT(&etimer_process, &tcpip_process, &wpcap_process, &serial_line_process);
00056 #else /* __CYGWIN__ */
00057 PROCINIT(&etimer_process, &tapdev_process, &tcpip_process, &serial_line_process);
00058 #endif /* __CYGWIN__ */
00059 
00060 #if UIP_CONF_IPV6
00061 /*---------------------------------------------------------------------------*/
00062 void
00063 sprint_ip6(uip_ip6addr_t addr)
00064 {
00065   unsigned char i = 0;
00066   unsigned char zerocnt = 0;
00067   unsigned char numprinted = 0;
00068   char thestring[40];
00069   char * result = thestring;
00070 
00071   *result++='[';
00072   while (numprinted < 8) {
00073     if ((addr.u16[i] == 0) && (zerocnt == 0)) {
00074       while(addr.u16[zerocnt + i] == 0) zerocnt++;
00075       if (zerocnt == 1) {
00076         *result++ = '0';
00077          numprinted++;
00078          break;
00079       }
00080       i += zerocnt;
00081       numprinted += zerocnt;
00082     } else {
00083       result += sprintf(result, "%x", (unsigned int)(uip_ntohs(addr.u16[i])));
00084       i++;
00085       numprinted++;
00086     }
00087     if (numprinted != 8) *result++ = ':';
00088   }
00089   *result++=']';
00090   *result=0;
00091   printf("%s",thestring);
00092 }
00093 #endif /* UIP_CONF_IPV6 */
00094 /*---------------------------------------------------------------------------*/
00095 
00096 /*---------------------------------------------------------------------------*/
00097 int
00098 main(void)
00099 {
00100 
00101   process_init();
00102 
00103   procinit_init();
00104 
00105   ctimer_init();
00106 
00107   autostart_start(autostart_processes);
00108 
00109   /* Set default IP addresses if not specified */
00110 #if !UIP_CONF_IPV6
00111   uip_ipaddr_t addr;
00112   
00113   uip_gethostaddr(&addr);
00114   if (addr.u8[0]==0) {
00115     uip_ipaddr(&addr, 10,1,1,1);
00116   }
00117   printf("IP Address:  %d.%d.%d.%d\n", uip_ipaddr_to_quad(&addr));
00118   uip_sethostaddr(&addr);
00119 
00120   uip_getnetmask(&addr);
00121   if (addr.u8[0]==0) {
00122     uip_ipaddr(&addr, 255,0,0,0);
00123     uip_setnetmask(&addr);
00124   }
00125   printf("Subnet Mask: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&addr));
00126 
00127   uip_getdraddr(&addr);
00128   if (addr.u8[0]==0) {
00129     uip_ipaddr(&addr, 10,1,1,100);
00130     uip_setdraddr(&addr);
00131   }
00132   printf("Def. Router: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&addr));
00133 
00134 #else /* !UIP_CONF_IPV6 */
00135   uint8_t i;
00136   uip_ipaddr_t ipaddr;
00137   uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0);   
00138 #if UIP_CONF_ROUTER
00139   uip_ds6_prefix_add(&ipaddr, UIP_DEFAULT_PREFIX_LEN, 0, 0, 0, 0);
00140 #else /* UIP_CONF_ROUTER */
00141   uip_ds6_prefix_add(&ipaddr, UIP_DEFAULT_PREFIX_LEN, 0);
00142 #endif /* UIP_CONF_ROUTER */
00143   uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
00144   uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF);
00145  // printf("IP6 Address: ");sprint_ip6(ipaddr);printf("\n");
00146   for (i=0;i<UIP_DS6_ADDR_NB;i++) {
00147         if (uip_ds6_if.addr_list[i].isused) {     
00148           printf("IPV6 Address: ");sprint_ip6(uip_ds6_if.addr_list[i].ipaddr);printf("\n");
00149         }
00150   }
00151 #endif /* !UIP_CONF_IPV6 */
00152 
00153   /* Make standard output unbuffered. */
00154   setvbuf(stdout, (char *)NULL, _IONBF, 0);
00155 
00156   while(1) {
00157     fd_set fds;
00158     int n;
00159     struct timeval tv;
00160     
00161     n = process_run();
00162     /*    if(n > 0) {
00163       printf("%d processes in queue\n");
00164       }*/
00165 
00166     tv.tv_sec = 0;
00167     tv.tv_usec = 1;
00168     FD_ZERO(&fds);
00169     FD_SET(STDIN_FILENO, &fds);
00170     select(1, &fds, NULL, NULL, &tv);
00171 
00172     if(FD_ISSET(STDIN_FILENO, &fds)) {
00173       char c;
00174       if(read(STDIN_FILENO, &c, 1) > 0) {
00175         serial_line_input_byte(c);
00176       }
00177     }
00178     etimer_request_poll();
00179   }
00180   
00181   return 0;
00182 }
00183 /*---------------------------------------------------------------------------*/
00184 void log_message(char *m1, char *m2)
00185 {
00186   printf("%s%s\n", m1, m2);
00187 }
00188 /*---------------------------------------------------------------------------*/
00189 void
00190 uip_log(char *m)
00191 {
00192   printf("uIP: '%s'\n", m);
00193 }
00194 /*---------------------------------------------------------------------------*/
00195 unsigned short
00196 sensors_light1(void)
00197 {
00198   static unsigned short count;
00199   return count++;
00200 }
00201 /*---------------------------------------------------------------------------*/

Generated on Mon Apr 11 14:23:41 2011 for Contiki 2.5 by  doxygen 1.6.1