Removed the use of CFS. Instead, provide callbacks for the using application to fill in data.

This commit is contained in:
adamdunkels 2007-03-22 23:54:40 +00:00
parent 62430052ff
commit dd276a9c8a
6 changed files with 203 additions and 108 deletions

View file

@ -28,7 +28,7 @@
*
* This file is part of the Contiki operating system.
*
* $Id: rudolph0.c,v 1.2 2007/03/21 23:18:23 adamdunkels Exp $
* $Id: rudolph0.c,v 1.3 2007/03/22 23:54:40 adamdunkels Exp $
*/
/**
@ -40,7 +40,6 @@
#include "net/rime.h"
#include "rudolph0.h"
#include "cfs/cfs.h"
#include <stddef.h> /* for offsetof */
@ -71,9 +70,12 @@ enum {
static void
read_new_datapacket(struct rudolph0_conn *c)
{
int len;
int len = 0;
len = cfs_read(c->cfs_fd, c->current.data, RUDOLPH0_DATASIZE);
if(c->cb->read_chunk) {
len = c->cb->read_chunk(c, c->current.h.chunk * RUDOLPH0_DATASIZE,
c->current.data, RUDOLPH0_DATASIZE);
}
c->current.datalen = len;
PRINTF("read_new_datapacket len %d\n", len);
@ -124,36 +126,30 @@ recv(struct sabc_conn *sabc)
if(p->h.type == TYPE_DATA) {
if(c->current.h.version != p->h.version) {
PRINTF("rudolph0 new version %d\n", p->h.version);
if(c->cfs_fd != -1) {
cfs_close(c->cfs_fd);
}
c->cfs_fd = c->cb->new_file(c);
c->current.h.version = p->h.version;
c->current.h.chunk = 0;
if(c->cfs_fd != -1) {
if(p->h.chunk != 0) {
send_nack(c);
c->cb->write_chunk(c, 0, RUDOLPH0_FLAG_NEWFILE, p->data, 0);
if(p->h.chunk != 0) {
send_nack(c);
} else {
cfs_write(c->cfs_fd, p->data, p->datalen);
c->cb->write_chunk(c, 0, RUDOLPH0_FLAG_NONE, p->data, p->datalen);
c->current.h.chunk++;
}
}
} else if(p->h.version == c->current.h.version) {
if(c->cfs_fd != -1) {
if(p->h.chunk == c->current.h.chunk) {
PRINTF("received chunk %d\n", p->h.chunk);
cfs_write(c->cfs_fd, p->data, p->datalen);
c->current.h.chunk++;
if(p->datalen < RUDOLPH0_DATASIZE) {
cfs_close(c->cfs_fd);
c->cfs_fd = -1;
c->cb->received_file(c);
}
} else if(p->h.chunk > c->current.h.chunk) {
PRINTF("received chunk %d > %d, sending NACK\n", p->h.chunk, c->current.h.chunk);
send_nack(c);
if(p->h.chunk == c->current.h.chunk) {
PRINTF("received chunk %d\n", p->h.chunk);
if(p->datalen < RUDOLPH0_DATASIZE) {
c->cb->write_chunk(c, c->current.h.chunk * RUDOLPH0_DATASIZE,
RUDOLPH0_FLAG_LASTCHUNK, p->data, p->datalen);
} else {
c->cb->write_chunk(c, c->current.h.chunk * RUDOLPH0_DATASIZE,
RUDOLPH0_FLAG_NONE, p->data, p->datalen);
}
c->current.h.chunk++;
} else if(p->h.chunk > c->current.h.chunk) {
PRINTF("received chunk %d > %d, sending NACK\n", p->h.chunk, c->current.h.chunk);
send_nack(c);
}
} else { /* p->h.version < c->current.h.version */
/* Ignore packets with old version */
@ -173,11 +169,9 @@ recv_nack(struct uabc_conn *uabc)
if(p->h.version == c->current.h.version) {
PRINTF("Reseting chunk to %d\n", p->h.chunk);
c->current.h.chunk = p->h.chunk;
cfs_seek(c->cfs_fd, c->current.h.chunk * RUDOLPH0_DATASIZE);
} else {
PRINTF("Wrong version, reseting chunk to 0\n");
c->current.h.chunk = 0;
cfs_seek(c->cfs_fd, 0);
}
read_new_datapacket(c);
sabc_set_timer(&c->c, SENDING_TIME);
@ -196,7 +190,6 @@ rudolph0_open(struct rudolph0_conn *c, u16_t channel,
c->cb = cb;
c->current.h.version = 0;
c->state = STATE_RECEIVER;
c->cfs_fd = -1;
}
/*---------------------------------------------------------------------------*/
void
@ -207,13 +200,9 @@ rudolph0_close(struct rudolph0_conn *c)
}
/*---------------------------------------------------------------------------*/
void
rudolph0_send(struct rudolph0_conn *c, int cfs_fd)
rudolph0_send(struct rudolph0_conn *c)
{
if(c->cfs_fd != -1) {
cfs_close(c->cfs_fd);
}
c->state = STATE_SENDER;
c->cfs_fd = cfs_fd;
c->current.h.version++;
c->current.h.chunk = 0;
c->current.h.type = TYPE_DATA;
@ -222,6 +211,12 @@ rudolph0_send(struct rudolph0_conn *c, int cfs_fd)
sabc_send_stubborn(&c->c, SENDING_TIME);
}
/*---------------------------------------------------------------------------*/
void
rudolph0_stop(struct rudolph0_conn *c)
{
sabc_cancel(&c->c);
}
/*---------------------------------------------------------------------------*/
int
rudolph0_version(struct rudolph0_conn *c)
{

View file

@ -28,7 +28,7 @@
*
* This file is part of the Contiki operating system.
*
* $Id: rudolph0.h,v 1.2 2007/03/21 23:18:23 adamdunkels Exp $
* $Id: rudolph0.h,v 1.3 2007/03/22 23:54:40 adamdunkels Exp $
*/
/**
@ -48,9 +48,17 @@
struct rudolph0_conn;
enum {
RUDOLPH0_FLAG_NONE,
RUDOLPH0_FLAG_NEWFILE,
RUDOLPH0_FLAG_LASTCHUNK,
};
struct rudolph0_callbacks {
int (* new_file)(struct rudolph0_conn *c);
void (* received_file)(struct rudolph0_conn *c);
void (* write_chunk)(struct rudolph0_conn *c, int offset, int flag,
char *data, int len);
int (* read_chunk)(struct rudolph0_conn *c, int offset, char *to,
int maxsize);
};
#define RUDOLPH0_DATASIZE 64
@ -71,7 +79,6 @@ struct rudolph0_conn {
struct sabc_conn c;
struct uabc_conn nackc;
const struct rudolph0_callbacks *cb;
int cfs_fd;
u8_t state;
struct rudolph0_datapacket current;
};
@ -79,7 +86,8 @@ struct rudolph0_conn {
void rudolph0_open(struct rudolph0_conn *c, u16_t channel,
const struct rudolph0_callbacks *cb);
void rudolph0_close(struct rudolph0_conn *c);
void rudolph0_send(struct rudolph0_conn *c, int cfs_fd);
void rudolph0_send(struct rudolph0_conn *c);
void rudolph0_stop(struct rudolph0_conn *c);
void rudolph0_set_version(struct rudolph0_conn *c, int version);
int rudolph0_version(struct rudolph0_conn *c);

View file

@ -28,7 +28,7 @@
*
* This file is part of the Contiki operating system.
*
* $Id: rudolph1.c,v 1.1 2007/03/21 23:14:40 adamdunkels Exp $
* $Id: rudolph1.c,v 1.2 2007/03/22 23:54:40 adamdunkels Exp $
*/
/**
@ -81,10 +81,15 @@ enum {
static int
read_data(struct rudolph1_conn *c, char *dataptr, int chunk)
{
int len;
int len = 0;
cfs_seek(c->cfs_fd, chunk * RUDOLPH1_DATASIZE);
len = cfs_read(c->cfs_fd, dataptr, RUDOLPH1_DATASIZE);
if(c->cb->read_chunk) {
len = c->cb->read_chunk(c, chunk * RUDOLPH1_DATASIZE,
dataptr, RUDOLPH1_DATASIZE);
}
/* cfs_seek(c->cfs_fd, chunk * RUDOLPH1_DATASIZE);
len = cfs_read(c->cfs_fd, dataptr, RUDOLPH1_DATASIZE);*/
return len;
}
/*---------------------------------------------------------------------------*/
@ -108,12 +113,22 @@ format_data(struct rudolph1_conn *c, int chunk)
static void
write_data(struct rudolph1_conn *c, int chunk, u8_t *data, int datalen)
{
cfs_seek(c->cfs_fd, chunk * RUDOLPH1_DATASIZE);
cfs_write(c->cfs_fd, data, datalen);
if(chunk == 0) {
c->cb->write_chunk(c, 0, RUDOLPH1_FLAG_NEWFILE, data, 0);
}
/* cfs_seek(c->cfs_fd, chunk * RUDOLPH1_DATASIZE);
cfs_write(c->cfs_fd, data, datalen);*/
if(datalen < RUDOLPH1_DATASIZE) {
PRINTF("%d: get %d bytes, file complete\n",
rimeaddr_node_addr.u16, datalen);
c->cb->received_file(c, c->cfs_fd);
/* c->cb->received_file(c, c->cfs_fd);*/
c->cb->write_chunk(c, chunk * RUDOLPH1_DATASIZE,
RUDOLPH1_FLAG_LASTCHUNK, data, datalen);
} else {
c->cb->write_chunk(c, chunk * RUDOLPH1_DATASIZE,
RUDOLPH1_FLAG_NONE, data, datalen);
}
}
/*---------------------------------------------------------------------------*/
@ -138,34 +153,36 @@ handle_data(struct rudolph1_conn *c, struct rudolph1_datapacket *p)
{
if(LT(c->version, p->h.version)) {
PRINTF("rudolph1 new version %d\n", p->h.version);
c->cfs_fd = c->cb->new_file(c);
/* c->cfs_fd = c->cb->new_file(c);*/
c->version = p->h.version;
c->chunk = 1; /* Next chunk is 1. */
if(c->cfs_fd != -1) {
/* if(c->cfs_fd != -1) {*/
if(p->h.chunk != 0) {
send_nack(c);
} else {
write_data(c, 0, p->data, p->datalen);
}
}
/* }*/
} else if(p->h.version == c->version) {
if(c->cfs_fd != -1) {
/* if(c->cfs_fd != -1) {*/
if(p->h.chunk == c->chunk) {
PRINTF("%d: received chunk %d\n",
rimeaddr_node_addr.u16, p->h.chunk);
cfs_seek(c->cfs_fd, c->chunk * RUDOLPH1_DATASIZE);
write_data(c, p->h.chunk, p->data, p->datalen);
c->chunk++;
/* cfs_seek(c->cfs_fd, c->chunk * RUDOLPH1_DATASIZE);
cfs_write(c->cfs_fd, p->data, p->datalen);
c->chunk++;
if(p->datalen < RUDOLPH1_DATASIZE) {
c->cb->received_file(c, c->cfs_fd);
}
}*/
} else if(p->h.chunk > c->chunk) {
PRINTF("%d: received chunk %d > %d, sending NACK\n",
rimeaddr_node_addr.u16,
p->h.chunk, c->chunk);
send_nack(c);
}
}
/* } */
} else { /* p->h.version < c->current.h.version */
/* Ignore packets with old version */
}
@ -254,9 +271,9 @@ rudolph1_close(struct rudolph1_conn *c)
}
/*---------------------------------------------------------------------------*/
void
rudolph1_send(struct rudolph1_conn *c, int cfs_fd)
rudolph1_send(struct rudolph1_conn *c)
{
c->cfs_fd = cfs_fd;
/* c->cfs_fd = cfs_fd;*/
c->version++;
c->chunk = 0;
c->trickle_interval = TRICKLE_INTERVAL;
@ -265,3 +282,11 @@ rudolph1_send(struct rudolph1_conn *c, int cfs_fd)
ctimer_set(&c->t, DATA_INTERVAL, send_next_packet, c);
}
/*---------------------------------------------------------------------------*/
void
rudolph1_stop(struct rudolph1_conn *c)
{
/* XXX */
ctimer_stop(&c->t);
printf("rudolph1_stop: not implemented\n");
}
/*---------------------------------------------------------------------------*/

View file

@ -28,7 +28,7 @@
*
* This file is part of the Contiki operating system.
*
* $Id: rudolph1.h,v 1.1 2007/03/21 23:14:40 adamdunkels Exp $
* $Id: rudolph1.h,v 1.2 2007/03/22 23:54:40 adamdunkels Exp $
*/
/**
@ -48,9 +48,17 @@
struct rudolph1_conn;
enum {
RUDOLPH1_FLAG_NONE,
RUDOLPH1_FLAG_NEWFILE,
RUDOLPH1_FLAG_LASTCHUNK,
};
struct rudolph1_callbacks {
int (* new_file)(struct rudolph1_conn *c);
void (* received_file)(struct rudolph1_conn *c, int cfs_fd);
void (* write_chunk)(struct rudolph1_conn *c, int offset, int flag,
char *data, int len);
int (* read_chunk)(struct rudolph1_conn *c, int offset, char *to,
int maxsize);
};
struct rudolph1_conn {
@ -58,7 +66,6 @@ struct rudolph1_conn {
struct uabc_conn uabc;
const struct rudolph1_callbacks *cb;
struct ctimer t;
int cfs_fd;
u16_t chunk;
u8_t version;
u8_t trickle_interval;
@ -68,6 +75,7 @@ struct rudolph1_conn {
void rudolph1_open(struct rudolph1_conn *c, u16_t channel,
const struct rudolph1_callbacks *cb);
void rudolph1_close(struct rudolph1_conn *c);
void rudolph1_send(struct rudolph1_conn *c, int cfs_fd);
void rudolph1_send(struct rudolph1_conn *c);
void rudolph1_stop(struct rudolph1_conn *c);
#endif /* __RUDOLPH1_H__ */

View file

@ -28,7 +28,7 @@
*
* This file is part of the Contiki operating system.
*
* $Id: test-rudolph0.c,v 1.1 2007/03/21 23:24:24 adamdunkels Exp $
* $Id: test-rudolph0.c,v 1.2 2007/03/22 23:58:57 adamdunkels Exp $
*/
/**
@ -52,32 +52,60 @@
PROCESS(test_rudolph0_process, "Rudolph0 test");
AUTOSTART_PROCESSES(&test_rudolph0_process);
/*---------------------------------------------------------------------------*/
static int
newfile(struct rudolph0_conn *c)
{
printf("+++ rudolph0 new file incoming at %lu\n", clock_time());
return cfs_open("hej", CFS_WRITE);
}
static void
recv(struct rudolph0_conn *c)
write_chunk(struct rudolph0_conn *c, int offset, int flag,
char *data, int datalen)
{
int fd;
int i;
printf("+++ rudolph0 entire file received at %lu\n", clock_time());
if(flag == RUDOLPH0_FLAG_NEWFILE) {
printf("+++ rudolph0 new file incoming at %lu\n", clock_time());
fd = cfs_open("codeprop.out", CFS_WRITE);
} else {
fd = cfs_open("codeprop.out", CFS_WRITE + CFS_APPEND);
}
if(datalen > 0) {
int ret;
cfs_seek(fd, offset);
ret = cfs_write(fd, data, datalen);
printf("write_chunk wrote %d bytes at %d, %d\n", ret, offset, (unsigned char)data[0]);
}
cfs_close(fd);
if(flag == RUDOLPH0_FLAG_LASTCHUNK) {
int i;
printf("+++ rudolph0 entire file received at %lu\n", clock_time());
fd = cfs_open("hej", CFS_READ);
for(i = 0; i < 200; ++i) {
unsigned char buf;
cfs_read(fd, &buf, 1);
if(buf != i) {
printf("error: diff at %d, %d != %d\n", i, i, buf);
break;
}
}
cfs_close(fd);
}
}
static int
read_chunk(struct rudolph0_conn *c, int offset, char *to, int maxsize)
{
int fd;
int ret;
fd = cfs_open("hej", CFS_READ);
for(i = 0; i < 200; ++i) {
unsigned char buf;
cfs_read(fd, &buf, 1);
if(buf != i) {
printf("error: diff at %d, %d != %d\n", i, i, buf);
}
}
cfs_seek(fd, offset);
ret = cfs_read(fd, to, maxsize);
printf("read_chunk %d bytes at %d, %d\n", ret, offset, (unsigned char)to[0]);
cfs_close(fd);
return ret;
}
const static struct rudolph0_callbacks rudolph0_call = {newfile,
recv};
const static struct rudolph0_callbacks rudolph0_call = {write_chunk,
read_chunk};
static struct rudolph0_conn rudolph0;
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(test_rudolph0_process, ev, data)
@ -105,9 +133,11 @@ PROCESS_THREAD(test_rudolph0_process, ev, data)
while(1) {
PROCESS_WAIT_EVENT_UNTIL(ev == sensors_event &&
data == &button_sensor);
cfs_close(fd);
fd = cfs_open("hej", CFS_READ);
rudolph0_send(&rudolph0, fd);
rudolph0_send(&rudolph0);
PROCESS_WAIT_EVENT_UNTIL(ev == sensors_event &&
data == &button_sensor);
rudolph0_stop(&rudolph0);
}
PROCESS_END();

View file

@ -28,7 +28,7 @@
*
* This file is part of the Contiki operating system.
*
* $Id: test-rudolph1.c,v 1.1 2007/03/21 23:24:24 adamdunkels Exp $
* $Id: test-rudolph1.c,v 1.2 2007/03/22 23:58:57 adamdunkels Exp $
*/
/**
@ -52,46 +52,72 @@
PROCESS(test_rudolph1_process, "Rudolph1 test");
AUTOSTART_PROCESSES(&test_rudolph1_process);
/*---------------------------------------------------------------------------*/
static int
newfile(struct rudolph1_conn *c)
{
printf("+++ rudolph1 new file incoming at %lu\n", clock_time());
fflush(NULL);
return cfs_open("hej", CFS_WRITE);
}
static void
recv(struct rudolph1_conn *c, int cfs_fd)
write_chunk(struct rudolph1_conn *c, int offset, int flag,
char *data, int datalen)
{
int fd;
int i;
printf("+++ rudolph1 entire file received at %lu\n", clock_time());
fflush(NULL);
cfs_close(cfs_fd);
if(flag == RUDOLPH1_FLAG_NEWFILE) {
printf("+++ rudolph1 new file incoming at %lu\n", clock_time());
fd = cfs_open("codeprop.out", CFS_WRITE);
} else {
fd = cfs_open("codeprop.out", CFS_WRITE + CFS_APPEND);
}
if(datalen > 0) {
int ret;
cfs_seek(fd, offset);
ret = cfs_write(fd, data, datalen);
printf("write_chunk wrote %d bytes at %d, %d\n", ret, offset, (unsigned char)data[0]);
}
fd = cfs_open("hej", CFS_READ);
for(i = 0; i < 200; ++i) {
unsigned char buf;
cfs_read(fd, &buf, 1);
if(buf != i) {
printf("error: diff at %d, %d != %d\n", i, i, buf);
cfs_close(fd);
if(flag == RUDOLPH1_FLAG_LASTCHUNK) {
int i;
printf("+++ rudolph1 entire file received at %lu\n", clock_time());
fd = cfs_open("hej", CFS_READ);
for(i = 0; i < 200; ++i) {
unsigned char buf;
cfs_read(fd, &buf, 1);
if(buf != i) {
printf("error: diff at %d, %d != %d\n", i, i, buf);
break;
}
}
cfs_close(fd);
}
}
const static struct rudolph1_callbacks rudolph1_call = {newfile,
recv};
static int
read_chunk(struct rudolph1_conn *c, int offset, char *to, int maxsize)
{
int fd;
int ret;
fd = cfs_open("hej", CFS_READ);
cfs_seek(fd, offset);
ret = cfs_read(fd, to, maxsize);
printf("read_chunk %d bytes at %d, %d\n", ret, offset, (unsigned char)to[0]);
cfs_close(fd);
return ret;
}
const static struct rudolph1_callbacks rudolph1_call = {write_chunk,
read_chunk};
static struct rudolph1_conn rudolph1;
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(test_rudolph1_process, ev, data)
{
static int fd;
PROCESS_BEGIN();
process_start(&cfs_ram_process, NULL);
PROCESS_PAUSE();
{
int i, fd;
int i;
fd = cfs_open("hej", CFS_WRITE);
for(i = 0; i < 200; i++) {
@ -107,8 +133,11 @@ PROCESS_THREAD(test_rudolph1_process, ev, data)
while(1) {
PROCESS_WAIT_EVENT_UNTIL(ev == sensors_event &&
data == &button_sensor);
rudolph1_send(&rudolph1);
rudolph1_send(&rudolph1, cfs_open("hej", CFS_READ));
PROCESS_WAIT_EVENT_UNTIL(ev == sensors_event &&
data == &button_sensor);
rudolph1_stop(&rudolph1);
}
PROCESS_END();