2007-03-15 22:06:19 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2007, 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.
|
|
|
|
*
|
2010-01-15 10:24:35 +00:00
|
|
|
* $Id: example-rudolph1.c,v 1.7 2010/01/15 10:24:37 nifi Exp $
|
2007-03-15 22:06:19 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
2007-03-21 23:24:24 +00:00
|
|
|
* Testing the rudolph1 code in Rime
|
2007-03-15 22:06:19 +00:00
|
|
|
* \author
|
|
|
|
* Adam Dunkels <adam@sics.se>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "contiki.h"
|
2007-03-25 12:10:29 +00:00
|
|
|
#include "net/rime/rudolph1.h"
|
2007-03-15 22:06:19 +00:00
|
|
|
|
|
|
|
#include "dev/button-sensor.h"
|
|
|
|
|
|
|
|
#include "dev/leds.h"
|
|
|
|
|
2007-05-15 08:09:21 +00:00
|
|
|
#include "cfs/cfs.h"
|
|
|
|
|
|
|
|
#include "sys/rtimer.h"
|
2007-03-20 12:21:17 +00:00
|
|
|
|
2007-03-15 22:06:19 +00:00
|
|
|
#include <stdio.h>
|
2007-04-02 10:04:37 +00:00
|
|
|
|
2008-11-17 22:52:10 +00:00
|
|
|
#if CONTIKI_TARGET_NETSIM
|
2007-11-17 18:01:00 +00:00
|
|
|
#include "ether.h"
|
|
|
|
#include "node.h"
|
2008-11-17 22:52:10 +00:00
|
|
|
#endif /* CONTIKI_TARGET_NETSIM */
|
2007-11-17 18:01:00 +00:00
|
|
|
|
2007-04-02 17:51:54 +00:00
|
|
|
#define FILESIZE 2000
|
2007-04-02 10:04:37 +00:00
|
|
|
|
2007-03-15 22:06:19 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-01-25 18:00:50 +00:00
|
|
|
PROCESS(example_rudolph1_process, "Rudolph1 example");
|
|
|
|
AUTOSTART_PROCESSES(&example_rudolph1_process);
|
2007-03-15 22:06:19 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2007-03-22 23:54:40 +00:00
|
|
|
write_chunk(struct rudolph1_conn *c, int offset, int flag,
|
2007-11-17 18:01:00 +00:00
|
|
|
uint8_t *data, int datalen)
|
2007-03-15 22:06:19 +00:00
|
|
|
{
|
2007-03-20 12:21:17 +00:00
|
|
|
int fd;
|
2008-11-17 22:52:10 +00:00
|
|
|
#if CONTIKI_TARGET_NETSIM
|
2007-04-02 17:51:54 +00:00
|
|
|
{
|
|
|
|
char buf[100];
|
|
|
|
sprintf(buf, "%d%%", (100 * (offset + datalen)) / FILESIZE);
|
|
|
|
ether_set_text(buf);
|
|
|
|
}
|
2008-11-17 22:52:10 +00:00
|
|
|
#endif /* CONTIKI_TARGET_NETSIM */
|
2007-05-15 08:09:21 +00:00
|
|
|
|
2007-03-22 23:54:40 +00:00
|
|
|
if(flag == RUDOLPH1_FLAG_NEWFILE) {
|
2007-04-02 10:04:37 +00:00
|
|
|
/*printf("+++ rudolph1 new file incoming at %lu\n", clock_time());*/
|
|
|
|
leds_on(LEDS_RED);
|
2007-03-22 23:54:40 +00:00
|
|
|
fd = cfs_open("codeprop.out", CFS_WRITE);
|
|
|
|
} else {
|
|
|
|
fd = cfs_open("codeprop.out", CFS_WRITE + CFS_APPEND);
|
|
|
|
}
|
2007-03-20 12:21:17 +00:00
|
|
|
|
2007-03-22 23:54:40 +00:00
|
|
|
if(datalen > 0) {
|
|
|
|
int ret;
|
2009-02-27 14:28:02 +00:00
|
|
|
cfs_seek(fd, offset, CFS_SEEK_SET);
|
2007-03-22 23:54:40 +00:00
|
|
|
ret = cfs_write(fd, data, datalen);
|
|
|
|
}
|
|
|
|
|
|
|
|
cfs_close(fd);
|
|
|
|
|
|
|
|
if(flag == RUDOLPH1_FLAG_LASTCHUNK) {
|
|
|
|
int i;
|
2007-05-15 08:09:21 +00:00
|
|
|
printf("+++ rudolph1 entire file received at %d, %d\n",
|
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1]);
|
2007-04-02 10:04:37 +00:00
|
|
|
leds_off(LEDS_RED);
|
|
|
|
leds_on(LEDS_YELLOW);
|
2007-03-22 23:54:40 +00:00
|
|
|
|
|
|
|
fd = cfs_open("hej", CFS_READ);
|
2007-04-02 10:04:37 +00:00
|
|
|
for(i = 0; i < FILESIZE; ++i) {
|
2007-03-22 23:54:40 +00:00
|
|
|
unsigned char buf;
|
|
|
|
cfs_read(fd, &buf, 1);
|
2007-04-02 10:04:37 +00:00
|
|
|
if(buf != (unsigned char)i) {
|
2007-05-15 08:09:21 +00:00
|
|
|
printf("%d.%d: error: diff at %d, %d != %d\n",
|
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
|
|
|
|
i, i, buf);
|
2007-03-22 23:54:40 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-03-20 12:21:17 +00:00
|
|
|
}
|
2008-11-17 22:52:10 +00:00
|
|
|
#if CONTIKI_TARGET_NETSIM
|
2007-04-02 10:04:37 +00:00
|
|
|
ether_send_done();
|
|
|
|
#endif
|
2007-03-22 23:54:40 +00:00
|
|
|
cfs_close(fd);
|
2007-03-20 12:21:17 +00:00
|
|
|
}
|
2007-03-15 22:06:19 +00:00
|
|
|
}
|
2007-03-22 23:54:40 +00:00
|
|
|
static int
|
2007-11-17 18:01:00 +00:00
|
|
|
read_chunk(struct rudolph1_conn *c, int offset, uint8_t *to, int maxsize)
|
2007-03-22 23:54:40 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
fd = cfs_open("hej", CFS_READ);
|
|
|
|
|
2009-02-27 14:28:02 +00:00
|
|
|
cfs_seek(fd, offset, CFS_SEEK_SET);
|
2007-03-22 23:54:40 +00:00
|
|
|
ret = cfs_read(fd, to, maxsize);
|
2007-05-15 08:09:21 +00:00
|
|
|
/* printf("%d.%d: read_chunk %d bytes at %d, %d\n",
|
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
|
|
|
|
ret, offset, (unsigned char)to[0]);*/
|
2007-03-22 23:54:40 +00:00
|
|
|
cfs_close(fd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
const static struct rudolph1_callbacks rudolph1_call = {write_chunk,
|
|
|
|
read_chunk};
|
2007-03-21 23:24:24 +00:00
|
|
|
static struct rudolph1_conn rudolph1;
|
2007-03-15 22:06:19 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-05-15 08:09:21 +00:00
|
|
|
static void
|
|
|
|
log_queuelen(struct rtimer *t, void *ptr)
|
|
|
|
{
|
2008-11-17 22:52:10 +00:00
|
|
|
#if CONTIKI_TARGET_NETSIM
|
2008-02-24 22:15:46 +00:00
|
|
|
extern uint8_t queuebuf_len, queuebuf_ref_len;
|
2007-05-15 08:09:21 +00:00
|
|
|
node_log("%d %d\n",
|
|
|
|
queuebuf_len,
|
|
|
|
queuebuf_ref_len);
|
|
|
|
rtimer_set(t, RTIMER_TIME(t) + RTIMER_ARCH_SECOND, 1,
|
|
|
|
log_queuelen, ptr);
|
2008-11-17 22:52:10 +00:00
|
|
|
#endif /* CONTIKI_TARGET_NETSIM */
|
2007-05-15 08:09:21 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-04-02 10:04:37 +00:00
|
|
|
|
2008-01-25 18:00:50 +00:00
|
|
|
PROCESS_THREAD(example_rudolph1_process, ev, data)
|
2007-03-15 22:06:19 +00:00
|
|
|
{
|
2007-03-22 23:54:40 +00:00
|
|
|
static int fd;
|
2007-05-15 08:09:21 +00:00
|
|
|
static struct rtimer t;
|
2007-03-25 12:10:29 +00:00
|
|
|
PROCESS_EXITHANDLER(rudolph1_close(&rudolph1);)
|
2007-03-15 22:06:19 +00:00
|
|
|
PROCESS_BEGIN();
|
|
|
|
|
2007-03-20 12:21:17 +00:00
|
|
|
PROCESS_PAUSE();
|
2007-03-15 22:06:19 +00:00
|
|
|
|
2007-03-20 12:21:17 +00:00
|
|
|
|
2009-10-18 17:52:08 +00:00
|
|
|
rudolph1_open(&rudolph1, 140, &rudolph1_call);
|
2010-01-15 10:24:35 +00:00
|
|
|
SENSORS_ACTIVATE(button_sensor);
|
2007-03-15 22:06:19 +00:00
|
|
|
|
2007-05-15 08:09:21 +00:00
|
|
|
rtimer_set(&t, RTIMER_NOW() + RTIMER_ARCH_SECOND, 1,
|
|
|
|
log_queuelen, NULL);
|
|
|
|
|
2007-04-02 10:04:37 +00:00
|
|
|
PROCESS_PAUSE();
|
|
|
|
|
|
|
|
if(rimeaddr_node_addr.u8[0] == 1 &&
|
|
|
|
rimeaddr_node_addr.u8[1] == 1) {
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
fd = cfs_open("hej", CFS_WRITE);
|
|
|
|
for(i = 0; i < FILESIZE; i++) {
|
|
|
|
unsigned char buf = i;
|
|
|
|
cfs_write(fd, &buf, 1);
|
|
|
|
}
|
|
|
|
cfs_close(fd);
|
|
|
|
}
|
2007-04-02 17:51:54 +00:00
|
|
|
rudolph1_send(&rudolph1, CLOCK_SECOND * 2);
|
2008-11-17 22:52:10 +00:00
|
|
|
#if CONTIKI_TARGET_NETSIM
|
2007-04-02 17:51:54 +00:00
|
|
|
ether_send_done();
|
2008-11-17 22:52:10 +00:00
|
|
|
#endif /* CONTIKI_TARGET_NETSIM */
|
2007-04-02 10:04:37 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-03-15 22:06:19 +00:00
|
|
|
while(1) {
|
|
|
|
|
2007-03-22 23:54:40 +00:00
|
|
|
PROCESS_WAIT_EVENT_UNTIL(ev == sensors_event &&
|
|
|
|
data == &button_sensor);
|
|
|
|
rudolph1_stop(&rudolph1);
|
2007-03-15 22:06:19 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
PROCESS_END();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|