node.c

00001 /*
00002  * Copyright (c) 2004, Swedish Institute of Computer Science.
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 copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the Institute nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  *
00029  * This file is part of the Contiki operating system.
00030  *
00031  * Author: Adam Dunkels <adam@sics.se>
00032  *
00033  * $Id: node.c,v 1.10 2008/01/04 23:23:29 oliverschmidt Exp $
00034  */
00035 #include "node.h"
00036 #include "contiki.h"
00037 #include "net/uip.h"
00038 #include "net/rime.h"
00039 #include <string.h>
00040 #include <stdlib.h>
00041 #include <arpa/inet.h>
00042 
00043 extern in_addr_t gwaddr;
00044 
00045 static clock_time_t drift, timer;
00046 
00047 struct node node;
00048 
00049 static int fd;
00050 
00051 static void init_node_log(void);
00052 
00053 /*------------------------------------------------------------------------------*/
00054 void
00055 node_init(int id, int posx, int posy, int b)
00056 {
00057   uip_ipaddr_t addr;
00058   
00059   node.id = id;
00060   node.x = posx;
00061   node.y = posy;
00062   /*  node.type = NODE_TYPE_NORMAL;*/
00063 
00064   if(b) {
00065 #ifdef __CYGWIN__
00066     addr = *(uip_ipaddr_t *)&gwaddr;
00067 #else /* __CYGWIN__ */
00068     uip_ipaddr(&addr, 192,168,1,2);
00069 #endif /* __CYGWIN__ */
00070   } else {
00071     uip_ipaddr(&addr, 172,16,posx,posy);
00072   }
00073   uip_sethostaddr(&addr);
00074   
00075   {
00076     rimeaddr_t nodeaddr;
00077     
00078     nodeaddr.u8[0] = posx;
00079     nodeaddr.u8[1] = posy;
00080     rimeaddr_set_node_addr(&nodeaddr);
00081   }
00082 
00083   drift = rand() % 95726272;
00084 
00085   init_node_log();
00086 }
00087 /*------------------------------------------------------------------------------*/
00088 #include <sys/time.h>
00089 
00090 clock_time_t
00091 node_time(void)
00092 {
00093   struct timeval tv;
00094   
00095   gettimeofday(&tv, NULL);
00096  
00097   return tv.tv_sec * 1000 + tv.tv_usec / 1000/* + drift*/;
00098 }
00099 /*------------------------------------------------------------------------------*/
00100 unsigned long
00101 node_seconds(void)
00102 {
00103   struct timeval tv;
00104   
00105   gettimeofday(&tv, NULL);
00106  
00107   return tv.tv_sec;
00108 }
00109 /*------------------------------------------------------------------------------*/
00110 void
00111 node_set_time(clock_time_t time)
00112 {
00113   timer = time;
00114 }
00115 /*------------------------------------------------------------------------------*/
00116 int
00117 node_x(void)
00118 {
00119   return node.x;
00120 }
00121 /*------------------------------------------------------------------------------*/
00122 int
00123 node_y(void)
00124 {
00125   return node.y;
00126 }
00127 /*------------------------------------------------------------------------------*/
00128 #include <fcntl.h>
00129 #include <stdarg.h>
00130 #include <stdio.h>
00131 #include <unistd.h>
00132 
00133 static void
00134 init_node_log(void)
00135 {
00136   fd = open("log", O_CREAT | O_WRONLY | O_APPEND, 0666);
00137 }
00138 
00139 void
00140 node_log(const char *fmt, ...)
00141 {
00142   va_list ap;
00143   char buf[4096];
00144   int len;
00145 
00146   len = sprintf(buf, "Node %d (%d, %d): ", node.id, node.x, node.y);
00147   va_start(ap, fmt);
00148   vsprintf(&buf[len], fmt, ap);
00149   write(fd, buf, strlen(buf));
00150   va_end(ap);
00151 }
00152 /*------------------------------------------------------------------------------*/

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