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-rudolph0.c,v 1.5 2010/01/15 10:24:37 nifi Exp $
36 * Testing the rudolph0 code in Rime
38 * Adam Dunkels <adam@sics.se>
43 #include "net/rime/rudolph0.h"
45 #include "dev/button-sensor.h"
53 /*---------------------------------------------------------------------------*/
54 PROCESS(example_rudolph0_process
, "Rudolph0 example");
55 AUTOSTART_PROCESSES(&example_rudolph0_process
);
56 /*---------------------------------------------------------------------------*/
58 write_chunk(struct rudolph0_conn
*c
, int offset
, int flag
,
59 uint8_t *data
, int datalen
)
63 if(flag
== RUDOLPH0_FLAG_NEWFILE
) {
64 /* printf("+++ rudolph0 new file incoming at %lu\n", clock_time());*/
66 fd
= cfs_open("codeprop.out", CFS_WRITE
);
68 fd
= cfs_open("codeprop.out", CFS_WRITE
+ CFS_APPEND
);
73 cfs_seek(fd
, offset
, CFS_SEEK_SET
);
74 ret
= cfs_write(fd
, data
, datalen
);
75 /* printf("write_chunk wrote %d bytes at %d, %d\n", ret, offset, (unsigned char)data[0]);*/
80 if(flag
== RUDOLPH0_FLAG_LASTCHUNK
) {
82 /* printf("+++ rudolph0 entire file received at %lu\n", clock_time());*/
85 fd
= cfs_open("hej", CFS_READ
);
86 for(i
= 0; i
< FILESIZE
; ++i
) {
88 cfs_read(fd
, &buf
, 1);
89 if(buf
!= (unsigned char)i
) {
90 printf("error: diff at %d, %d != %d\n", i
, i
, buf
);
98 read_chunk(struct rudolph0_conn
*c
, int offset
, uint8_t *to
, int maxsize
)
103 fd
= cfs_open("hej", CFS_READ
);
105 cfs_seek(fd
, offset
, CFS_SEEK_SET
);
106 ret
= cfs_read(fd
, to
, maxsize
);
107 /* printf("read_chunk %d bytes at %d, %d\n", ret, offset, (unsigned char)to[0]);*/
111 const static struct rudolph0_callbacks rudolph0_call
= {write_chunk
,
113 static struct rudolph0_conn rudolph0
;
114 /*---------------------------------------------------------------------------*/
115 PROCESS_THREAD(example_rudolph0_process
, ev
, data
)
119 PROCESS_EXITHANDLER(rudolph0_close(&rudolph0
);)
126 rudolph0_open(&rudolph0
, 138, &rudolph0_call
);
127 SENSORS_ACTIVATE(button_sensor
);
130 PROCESS_WAIT_EVENT_UNTIL(ev
== sensors_event
&&
131 data
== &button_sensor
);
135 fd
= cfs_open("hej", CFS_WRITE
);
136 for(i
= 0; i
< FILESIZE
; i
++) {
137 unsigned char buf
= i
;
138 cfs_write(fd
, &buf
, 1);
142 rudolph0_send(&rudolph0
, CLOCK_SECOND
/ 4);
144 PROCESS_WAIT_EVENT_UNTIL(ev
== sensors_event
&&
145 data
== &button_sensor
);
146 rudolph0_stop(&rudolph0
);
151 /*---------------------------------------------------------------------------*/