2 * Copyright (c) 2007, Swedish Institute of Computer Science.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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 $
36 * Testing the rudolph2 code in Rime
38 * Adam Dunkels <adam@sics.se>
43 #include "net/rime/rudolph2.h"
45 #include "dev/button-sensor.h"
53 #if CONTIKI_TARGET_NETSIM
56 #endif /* CONTIKI_TARGET_NETSIM */
60 /*---------------------------------------------------------------------------*/
61 PROCESS(example_rudolph2_process
, "Rudolph2 example");
62 AUTOSTART_PROCESSES(&example_rudolph2_process
);
63 /*---------------------------------------------------------------------------*/
65 write_chunk(struct rudolph2_conn
*c
, int offset
, int flag
,
66 uint8_t *data
, int datalen
)
69 #if CONTIKI_TARGET_NETSIM
72 sprintf(buf
, "%d%%", (100 * (offset
+ datalen
)) / FILESIZE
);
75 #endif /* CONTIKI_TARGET_NETSIM */
77 if(flag
== RUDOLPH2_FLAG_NEWFILE
) {
78 /*printf("+++ rudolph2 new file incoming at %lu\n", clock_time());*/
80 fd
= cfs_open("codeprop.out", CFS_WRITE
);
82 fd
= cfs_open("codeprop.out", CFS_WRITE
+ CFS_APPEND
);
87 cfs_seek(fd
, offset
, CFS_SEEK_SET
);
88 ret
= cfs_write(fd
, data
, datalen
);
93 if(flag
== RUDOLPH2_FLAG_LASTCHUNK
) {
95 printf("+++ rudolph2 entire file received at %d, %d\n",
96 rimeaddr_node_addr
.u8
[0], rimeaddr_node_addr
.u8
[1]);
100 fd
= cfs_open("hej", CFS_READ
);
101 for(i
= 0; i
< FILESIZE
; ++i
) {
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],
111 #if CONTIKI_TARGET_NETSIM
118 read_chunk(struct rudolph2_conn
*c
, int offset
, uint8_t *to
, int maxsize
)
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]);*/
133 const static struct rudolph2_callbacks rudolph2_call
= {write_chunk
,
135 static struct rudolph2_conn rudolph2
;
136 /*---------------------------------------------------------------------------*/
139 PROCESS_THREAD(example_rudolph2_process
, ev
, data
)
142 PROCESS_EXITHANDLER(rudolph2_close(&rudolph2
);)
148 rudolph2_open(&rudolph2
, 142, &rudolph2_call
);
149 SENSORS_ACTIVATE(button_sensor
);
153 if(rimeaddr_node_addr
.u8
[0] == 1 &&
154 rimeaddr_node_addr
.u8
[1] == 1) {
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);
165 rudolph2_send(&rudolph2
, CLOCK_SECOND
* 2);
166 #if CONTIKI_TARGET_NETSIM
168 #endif /* CONTIKI_TARGET_NETSIM */
174 PROCESS_WAIT_EVENT_UNTIL(ev
== sensors_event
&&
175 data
== &button_sensor
);
176 rudolph2_stop(&rudolph2
);
181 /*---------------------------------------------------------------------------*/