Contiki 2.5
node.c
1 /*
2  * Copyright (c) 2004, Swedish Institute of Computer Science.
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 copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * Author: Adam Dunkels <adam@sics.se>
32  *
33  * $Id: node.c,v 1.10 2008/01/04 23:23:29 oliverschmidt Exp $
34  */
35 #include "node.h"
36 #include "contiki.h"
37 #include "net/uip.h"
38 #include "net/rime.h"
39 #include <string.h>
40 #include <stdlib.h>
41 #include <arpa/inet.h>
42 
43 extern in_addr_t gwaddr;
44 
45 static clock_time_t drift, timer;
46 
47 struct node node;
48 
49 static int fd;
50 
51 static void init_node_log(void);
52 
53 /*------------------------------------------------------------------------------*/
54 void
55 node_init(int id, int posx, int posy, int b)
56 {
57  uip_ipaddr_t addr;
58 
59  node.id = id;
60  node.x = posx;
61  node.y = posy;
62  /* node.type = NODE_TYPE_NORMAL;*/
63 
64  if(b) {
65 #ifdef __CYGWIN__
66  addr = *(uip_ipaddr_t *)&gwaddr;
67 #else /* __CYGWIN__ */
68  uip_ipaddr(&addr, 192,168,1,2);
69 #endif /* __CYGWIN__ */
70  } else {
71  uip_ipaddr(&addr, 172,16,posx,posy);
72  }
73  uip_sethostaddr(&addr);
74 
75  {
76  rimeaddr_t nodeaddr;
77 
78  nodeaddr.u8[0] = posx;
79  nodeaddr.u8[1] = posy;
80  rimeaddr_set_node_addr(&nodeaddr);
81  }
82 
83  drift = rand() % 95726272;
84 
85  init_node_log();
86 }
87 /*------------------------------------------------------------------------------*/
88 #include <sys/time.h>
89 
90 clock_time_t
91 node_time(void)
92 {
93  struct timeval tv;
94 
95  gettimeofday(&tv, NULL);
96 
97  return tv.tv_sec * 1000 + tv.tv_usec / 1000/* + drift*/;
98 }
99 /*------------------------------------------------------------------------------*/
100 unsigned long
101 node_seconds(void)
102 {
103  struct timeval tv;
104 
105  gettimeofday(&tv, NULL);
106 
107  return tv.tv_sec;
108 }
109 /*------------------------------------------------------------------------------*/
110 void
111 node_set_time(clock_time_t time)
112 {
113  timer = time;
114 }
115 /*------------------------------------------------------------------------------*/
116 int
117 node_x(void)
118 {
119  return node.x;
120 }
121 /*------------------------------------------------------------------------------*/
122 int
123 node_y(void)
124 {
125  return node.y;
126 }
127 /*------------------------------------------------------------------------------*/
128 #include <fcntl.h>
129 #include <stdarg.h>
130 #include <stdio.h>
131 #include <unistd.h>
132 
133 static void
134 init_node_log(void)
135 {
136  fd = open("log", O_CREAT | O_WRONLY | O_APPEND, 0666);
137 }
138 
139 void
140 node_log(const char *fmt, ...)
141 {
142  va_list ap;
143  char buf[4096];
144  int len;
145 
146  len = sprintf(buf, "Node %d (%d, %d): ", node.id, node.x, node.y);
147  va_start(ap, fmt);
148  vsprintf(&buf[len], fmt, ap);
149  write(fd, buf, strlen(buf));
150  va_end(ap);
151 }
152 /*------------------------------------------------------------------------------*/