2006-06-17 22:41:10 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2004, Swedish Institute of Computer Science.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*
|
|
|
|
* Author: Adam Dunkels <adam@sics.se>
|
|
|
|
*
|
2008-01-04 23:23:29 +00:00
|
|
|
* $Id: node.c,v 1.10 2008/01/04 23:23:29 oliverschmidt Exp $
|
2006-06-17 22:41:10 +00:00
|
|
|
*/
|
|
|
|
#include "node.h"
|
|
|
|
#include "contiki.h"
|
|
|
|
#include "net/uip.h"
|
2007-03-22 18:59:34 +00:00
|
|
|
#include "net/rime.h"
|
2006-06-17 22:41:10 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2008-01-04 23:23:29 +00:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
extern in_addr_t gwaddr;
|
2006-06-17 22:41:10 +00:00
|
|
|
|
|
|
|
static clock_time_t drift, timer;
|
|
|
|
|
|
|
|
struct node node;
|
|
|
|
|
|
|
|
static int fd;
|
|
|
|
|
|
|
|
static void init_node_log(void);
|
|
|
|
|
|
|
|
/*------------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
node_init(int id, int posx, int posy, int b)
|
|
|
|
{
|
2006-08-14 14:01:02 +00:00
|
|
|
uip_ipaddr_t addr;
|
2006-06-17 22:41:10 +00:00
|
|
|
|
|
|
|
node.id = id;
|
|
|
|
node.x = posx;
|
|
|
|
node.y = posy;
|
2006-09-26 22:10:12 +00:00
|
|
|
/* node.type = NODE_TYPE_NORMAL;*/
|
2006-06-17 22:41:10 +00:00
|
|
|
|
|
|
|
if(b) {
|
2008-01-04 23:23:29 +00:00
|
|
|
#ifdef __CYGWIN__
|
|
|
|
addr = *(uip_ipaddr_t *)&gwaddr;
|
|
|
|
#else /* __CYGWIN__ */
|
2007-03-29 22:25:39 +00:00
|
|
|
uip_ipaddr(&addr, 192,168,1,2);
|
2008-01-04 23:23:29 +00:00
|
|
|
#endif /* __CYGWIN__ */
|
2006-06-17 22:41:10 +00:00
|
|
|
} else {
|
2007-03-22 18:59:34 +00:00
|
|
|
uip_ipaddr(&addr, 172,16,posx,posy);
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
2006-08-14 14:01:02 +00:00
|
|
|
uip_sethostaddr(&addr);
|
2007-03-22 18:59:34 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
rimeaddr_t nodeaddr;
|
|
|
|
|
|
|
|
nodeaddr.u8[0] = posx;
|
|
|
|
nodeaddr.u8[1] = posy;
|
|
|
|
rimeaddr_set_node_addr(&nodeaddr);
|
|
|
|
}
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2007-11-17 18:01:00 +00:00
|
|
|
drift = rand() % 95726272;
|
2006-06-17 22:41:10 +00:00
|
|
|
|
|
|
|
init_node_log();
|
|
|
|
}
|
|
|
|
/*------------------------------------------------------------------------------*/
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
clock_time_t
|
|
|
|
node_time(void)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
|
2007-11-28 13:01:02 +00:00
|
|
|
gettimeofday(&tv, NULL);
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2007-03-13 13:07:47 +00:00
|
|
|
return tv.tv_sec * 1000 + tv.tv_usec / 1000/* + drift*/;
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
|
|
|
/*------------------------------------------------------------------------------*/
|
|
|
|
unsigned long
|
|
|
|
node_seconds(void)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
|
2007-11-28 13:01:02 +00:00
|
|
|
gettimeofday(&tv, NULL);
|
2006-06-17 22:41:10 +00:00
|
|
|
|
|
|
|
return tv.tv_sec;
|
|
|
|
}
|
|
|
|
/*------------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
node_set_time(clock_time_t time)
|
|
|
|
{
|
|
|
|
timer = time;
|
|
|
|
}
|
|
|
|
/*------------------------------------------------------------------------------*/
|
|
|
|
int
|
|
|
|
node_x(void)
|
|
|
|
{
|
|
|
|
return node.x;
|
|
|
|
}
|
|
|
|
/*------------------------------------------------------------------------------*/
|
|
|
|
int
|
|
|
|
node_y(void)
|
|
|
|
{
|
|
|
|
return node.y;
|
|
|
|
}
|
|
|
|
/*------------------------------------------------------------------------------*/
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
static void
|
|
|
|
init_node_log(void)
|
|
|
|
{
|
|
|
|
fd = open("log", O_CREAT | O_WRONLY | O_APPEND, 0666);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
node_log(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char buf[4096];
|
|
|
|
int len;
|
|
|
|
|
2007-11-17 18:01:00 +00:00
|
|
|
len = sprintf(buf, "Node %d (%d, %d): ", node.id, node.x, node.y);
|
2006-06-17 22:41:10 +00:00
|
|
|
va_start(ap, fmt);
|
2007-11-17 18:01:00 +00:00
|
|
|
vsprintf(&buf[len], fmt, ap);
|
2006-06-17 22:41:10 +00:00
|
|
|
write(fd, buf, strlen(buf));
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
/*------------------------------------------------------------------------------*/
|