Jackdaw additions
[contiki-2.x.git] / examples / rime / example-rudolph0.c
blobd718129a8d47d697717137ce03b97ec83daef810
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-rudolph0.c,v 1.5 2010/01/15 10:24:37 nifi Exp $
34 /**
35 * \file
36 * Testing the rudolph0 code in Rime
37 * \author
38 * Adam Dunkels <adam@sics.se>
41 #include "contiki.h"
42 #include "cfs/cfs.h"
43 #include "net/rime/rudolph0.h"
45 #include "dev/button-sensor.h"
47 #include "dev/leds.h"
49 #include <stdio.h>
51 #define FILESIZE 200
53 /*---------------------------------------------------------------------------*/
54 PROCESS(example_rudolph0_process, "Rudolph0 example");
55 AUTOSTART_PROCESSES(&example_rudolph0_process);
56 /*---------------------------------------------------------------------------*/
57 static void
58 write_chunk(struct rudolph0_conn *c, int offset, int flag,
59 uint8_t *data, int datalen)
61 int fd;
63 if(flag == RUDOLPH0_FLAG_NEWFILE) {
64 /* printf("+++ rudolph0 new file incoming at %lu\n", clock_time());*/
65 leds_on(LEDS_RED);
66 fd = cfs_open("codeprop.out", CFS_WRITE);
67 } else {
68 fd = cfs_open("codeprop.out", CFS_WRITE + CFS_APPEND);
71 if(datalen > 0) {
72 int ret;
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]);*/
78 cfs_close(fd);
80 if(flag == RUDOLPH0_FLAG_LASTCHUNK) {
81 int i;
82 /* printf("+++ rudolph0 entire file received at %lu\n", clock_time());*/
83 leds_off(LEDS_RED);
84 leds_on(LEDS_YELLOW);
85 fd = cfs_open("hej", CFS_READ);
86 for(i = 0; i < FILESIZE; ++i) {
87 unsigned char buf;
88 cfs_read(fd, &buf, 1);
89 if(buf != (unsigned char)i) {
90 printf("error: diff at %d, %d != %d\n", i, i, buf);
91 break;
94 cfs_close(fd);
97 static int
98 read_chunk(struct rudolph0_conn *c, int offset, uint8_t *to, int maxsize)
100 int fd;
101 int ret;
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]);*/
108 cfs_close(fd);
109 return ret;
111 const static struct rudolph0_callbacks rudolph0_call = {write_chunk,
112 read_chunk};
113 static struct rudolph0_conn rudolph0;
114 /*---------------------------------------------------------------------------*/
115 PROCESS_THREAD(example_rudolph0_process, ev, data)
117 static int fd;
119 PROCESS_EXITHANDLER(rudolph0_close(&rudolph0);)
121 PROCESS_BEGIN();
123 PROCESS_PAUSE();
126 rudolph0_open(&rudolph0, 138, &rudolph0_call);
127 SENSORS_ACTIVATE(button_sensor);
129 while(1) {
130 PROCESS_WAIT_EVENT_UNTIL(ev == sensors_event &&
131 data == &button_sensor);
133 int i;
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);
140 cfs_close(fd);
142 rudolph0_send(&rudolph0, CLOCK_SECOND / 4);
144 PROCESS_WAIT_EVENT_UNTIL(ev == sensors_event &&
145 data == &button_sensor);
146 rudolph0_stop(&rudolph0);
149 PROCESS_END();
151 /*---------------------------------------------------------------------------*/