Jackdaw additions
[contiki-2.x.git] / examples / rime / example-rudolph2.c
blob4c7de1156b26d6b735a2b954c1bde363e40a1b97
1 /*
2 * Copyright (c) 2007, Swedish Institute of Computer Science.
3 * All rights reserved.
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.
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.
29 * This file is part of the Contiki operating system.
31 * $Id: example-rudolph2.c,v 1.6 2010/01/15 10:24:37 nifi Exp $
34 /**
35 * \file
36 * Testing the rudolph2 code in Rime
37 * \author
38 * Adam Dunkels <adam@sics.se>
41 #include "contiki.h"
42 #include "net/rime.h"
43 #include "net/rime/rudolph2.h"
45 #include "dev/button-sensor.h"
47 #include "dev/leds.h"
49 #include "cfs/cfs.h"
51 #include <stdio.h>
53 #if CONTIKI_TARGET_NETSIM
54 #include "ether.h"
55 #include "node.h"
56 #endif /* CONTIKI_TARGET_NETSIM */
58 #define FILESIZE 2000
60 /*---------------------------------------------------------------------------*/
61 PROCESS(example_rudolph2_process, "Rudolph2 example");
62 AUTOSTART_PROCESSES(&example_rudolph2_process);
63 /*---------------------------------------------------------------------------*/
64 static void
65 write_chunk(struct rudolph2_conn *c, int offset, int flag,
66 uint8_t *data, int datalen)
68 int fd;
69 #if CONTIKI_TARGET_NETSIM
71 char buf[100];
72 sprintf(buf, "%d%%", (100 * (offset + datalen)) / FILESIZE);
73 ether_set_text(buf);
75 #endif /* CONTIKI_TARGET_NETSIM */
77 if(flag == RUDOLPH2_FLAG_NEWFILE) {
78 /*printf("+++ rudolph2 new file incoming at %lu\n", clock_time());*/
79 leds_on(LEDS_RED);
80 fd = cfs_open("codeprop.out", CFS_WRITE);
81 } else {
82 fd = cfs_open("codeprop.out", CFS_WRITE + CFS_APPEND);
85 if(datalen > 0) {
86 int ret;
87 cfs_seek(fd, offset, CFS_SEEK_SET);
88 ret = cfs_write(fd, data, datalen);
91 cfs_close(fd);
93 if(flag == RUDOLPH2_FLAG_LASTCHUNK) {
94 int i;
95 printf("+++ rudolph2 entire file received at %d, %d\n",
96 rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1]);
97 leds_off(LEDS_RED);
98 leds_on(LEDS_YELLOW);
100 fd = cfs_open("hej", CFS_READ);
101 for(i = 0; i < FILESIZE; ++i) {
102 unsigned char buf;
103 cfs_read(fd, &buf, 1);
104 if(buf != (unsigned char)i) {
105 printf("%d.%d: error: diff at %d, %d != %d\n",
106 rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
107 i, i, buf);
108 break;
111 #if CONTIKI_TARGET_NETSIM
112 ether_send_done();
113 #endif
114 cfs_close(fd);
117 static int
118 read_chunk(struct rudolph2_conn *c, int offset, uint8_t *to, int maxsize)
120 int fd;
121 int ret;
123 fd = cfs_open("hej", CFS_READ);
125 cfs_seek(fd, offset, CFS_SEEK_SET);
126 ret = cfs_read(fd, to, maxsize);
127 /* printf("%d.%d: read_chunk %d bytes at %d, %d\n",
128 rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
129 ret, offset, (unsigned char)to[0]);*/
130 cfs_close(fd);
131 return ret;
133 const static struct rudolph2_callbacks rudolph2_call = {write_chunk,
134 read_chunk};
135 static struct rudolph2_conn rudolph2;
136 /*---------------------------------------------------------------------------*/
137 #include "node-id.h"
139 PROCESS_THREAD(example_rudolph2_process, ev, data)
141 static int fd;
142 PROCESS_EXITHANDLER(rudolph2_close(&rudolph2);)
143 PROCESS_BEGIN();
145 PROCESS_PAUSE();
148 rudolph2_open(&rudolph2, 142, &rudolph2_call);
149 SENSORS_ACTIVATE(button_sensor);
151 PROCESS_PAUSE();
153 if(rimeaddr_node_addr.u8[0] == 1 &&
154 rimeaddr_node_addr.u8[1] == 1) {
156 int i;
158 fd = cfs_open("hej", CFS_WRITE);
159 for(i = 0; i < FILESIZE; i++) {
160 unsigned char buf = i;
161 cfs_write(fd, &buf, 1);
163 cfs_close(fd);
165 rudolph2_send(&rudolph2, CLOCK_SECOND * 2);
166 #if CONTIKI_TARGET_NETSIM
167 ether_send_done();
168 #endif /* CONTIKI_TARGET_NETSIM */
172 while(1) {
174 PROCESS_WAIT_EVENT_UNTIL(ev == sensors_event &&
175 data == &button_sensor);
176 rudolph2_stop(&rudolph2);
179 PROCESS_END();
181 /*---------------------------------------------------------------------------*/