1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-12-25 06:23:53 +01:00

[projects WEAtHeR]

* added routing and duplicate detection
This commit is contained in:
Oliver Hahm 2011-04-06 11:10:28 +02:00
parent 430bad0d69
commit f7d6ef4709
4 changed files with 59 additions and 24 deletions

View File

@ -1,7 +1,7 @@
#include <stdio.h>
#include <sht11.h>
#include <board.h>
#include <swtimer.h>
#include <hwtimer.h>
#include <ltc4150.h>
#include <board_uart0.h>
#include <posix_io.h>
@ -18,8 +18,16 @@
#include "weather_protocol.h"
#include "protocol_msg_gateway.h"
#define SHELL_STACK_SIZE (2048)
#define PH_STACK_SIZE (2048)
#define SECOND (1000 * 1000)
#define MINUTE (60 * SECOND)
#define SENDING_INTERVAL (1 * SECOND)
/* size of weather data packet without hop list */
#define EMPTY_WDP_SIZE (sizeof(weather_data_pkt_t) - MAX_HOP_LIST)
#define SHELL_STACK_SIZE (4048)
#define PH_STACK_SIZE (4048)
/* per default not acting as data sink */
static uint8_t data_sink = 0;
@ -68,15 +76,19 @@ void weather_sink(char* unused) {
static void handle_packet(void* msg, int msg_size, packet_info_t* packet_info) {
weather_packet_header_t *header = (weather_packet_header_t*) msg;
printf("\n\t Pkt received from phy: %u\n"
/* packet origins at current node, just ignore it */
if (header->src == cc1100_get_address()) {
return;
}
printf("\n\t Pkt received from phy: %u (%u bytes)\n"
"\t -> SEQ: %u | TYPE: %u | SRC: %hu \n\n",
packet_info->phy_src,
msg_size,
header->seq_nr,
header->type,
header->src
);
/* when destination is set, but I'm not the receiver, pass to routing */
/* while not acting as sink route the packet */
if (!data_sink) {
if (header->type == WEATHER_DATA) {
weather_data_pkt_t* wdp = (weather_data_pkt_t*) msg;
@ -97,7 +109,7 @@ static void handle_packet(void* msg, int msg_size, packet_info_t* packet_info) {
return;
}
/* in all other cases handle the packet */
/* if current node acts as sink, handle packet */
switch (header->type) {
case WEATHER_HELLO: {
if (msg_size < sizeof(weather_hello_pkt_t)) {
@ -116,16 +128,21 @@ static void handle_packet(void* msg, int msg_size, packet_info_t* packet_info) {
case WEATHER_DATA: {
weather_data_pkt_t* wdp = (weather_data_pkt_t*) msg;
time_t local_time = rtc_time(NULL);
uint8_t i;
/* <node_id_source>;<node_id_sink>;<timestamp_source>;<timestamp_sink>;<temperature>;<humidity_relative>;<humitidy_absolut>;<energy_counter> */
printf("%hu;%u;%04lX;%04lX;%.2f;%.2f;%.2f;%.2f\n",
printf("%hu;%u;%04lX;%04lX;%.2f;%.2f;%.2f;%.2f;",
header->src,
1,
cc1100_get_address(),
wdp->timestamp,
local_time,
wdp->temperature,
wdp->relhum,
wdp->relhum_temp,
wdp->energy);
for (i = 0; i < wdp->hop_counter; i++) {
printf("%03u-", wdp->hops[i]);
}
puts("");
break;
}
default: {
@ -158,7 +175,9 @@ int main(void) {
wdp.header.seq_nr = 0;
wdp.header.type = WEATHER_DATA;
wdp.hop_counter = 1;
printf("EMPTY: %lu, WDP: %lu\n", EMPTY_WDP_SIZE, sizeof(weather_data_pkt_t));
puts("");
puts("WEAtHeR: Wireless Energy-Aware mulTi-Hop sEnsor Reading.");
/* <node_id_source>;<node_id_sink>;<timestamp_source>;<timestamp_sink>;<temperature>;<humidity_relative>;<humitidy_absolut>;<energy_counter> */
@ -167,8 +186,9 @@ int main(void) {
thread_create(shell_stack_buffer, SHELL_STACK_SIZE, PRIORITY_MAIN-1, CREATE_STACKTEST, shell_runner, "shell");
init_protocol_msg_gateway();
/* create thread for radio packet handling */
int pid = thread_create(ph_stack_buffer, PH_STACK_SIZE, PRIORITY_MAIN-5, CREATE_STACKTEST, protocol_handler_thread, "protocol_handler");
int pid = thread_create(ph_stack_buffer, PH_STACK_SIZE, PRIORITY_MAIN-2, CREATE_STACKTEST, protocol_handler_thread, "protocol_handler");
set_protocol_handler_thread(pid);
ltc4150_start();
@ -187,16 +207,22 @@ int main(void) {
wdp.temperature = sht11_val.temperature;
wdp.relhum = sht11_val.relhum;
wdp.relhum_temp = sht11_val.relhum_temp;
if (cc1100_send_csmaca(0, WEATHER_PROTOCOL_NR, 0, (char*)&wdp, sizeof(weather_data_pkt_t))) {
printf("Successfully sent packet: \n");
wdp.hops[0] = cc1100_get_address();
/* send packet with one entry in hop list */
if (cc1100_send_csmaca(0, WEATHER_PROTOCOL_NR, 0, (char*)&wdp, (EMPTY_WDP_SIZE + 1)) > 0) {
printf("Sending %lu bytes.\n", (EMPTY_WDP_SIZE + 1));
wdp.header.seq_nr++;
}
else {
puts("Error on sending packet!");
}
}
LED_GREEN_TOGGLE;
}
else {
LED_RED_TOGGLE;
}
swtimer_usleep(60 * 1000*1000);
hwtimer_wait(SENDING_INTERVAL);
}
puts("Something went wrong.");
}

View File

@ -19,9 +19,6 @@ packet_t packet_buffer[PACKET_BUFFER_SIZE];
static void protocol_msg_gateway(void* payload, int msg_size, protocol_t protocol, packet_info_t* packet_info) {
msg_t m;
// if ((((int16_t) packet_info->phy_src) > (((int16_t) cc1100_get_address()) + 10)) || (((int16_t) packet_info->phy_src) < (((int16_t) cc1100_get_address()) - 10))) {
// return;
// }
if (protocol_handler_pid <= 0) {
puts("protocol_handler(): received packet without protocol handler. msg dropped.");
return;
@ -45,6 +42,7 @@ static void protocol_msg_gateway(void* payload, int msg_size, protocol_t protoco
}
void init_protocol_msg_gateway() {
puts("Init protocol msg gateway");
cc1100_set_packet_monitor(protocol_msg_gateway);
}

View File

@ -5,6 +5,7 @@
#include <radio/radio.h>
#define WEATHER_PROTOCOL_NR 6
#define MAX_HOP_LIST (11)
typedef enum {
WEATHER_HELLO,
@ -16,7 +17,6 @@ typedef struct {
uint16_t seq_nr;
uint8_t src;
uint8_t type;
uint8_t resevered;
} weather_packet_header_t;
typedef struct __attribute__ ((packed)) {
@ -29,13 +29,15 @@ typedef struct {
char mesg[40];
} weather_chat_pkt_t;
typedef struct {
typedef struct __attribute__ ((packed)) {
weather_packet_header_t header;
time_t timestamp;
double temperature;
double relhum;
double relhum_temp;
double energy;
uint8_t hop_counter;
uint8_t hops[MAX_HOP_LIST];
} weather_data_pkt_t;
#endif

View File

@ -9,18 +9,17 @@ static source_timestamp_t sources[MAX_SOURCES];
static uint8_t update_sources(uint8_t id, time_t timestamp) {
uint8_t i;
puts("updating sources list");
for (i = 0; i < MAX_SOURCES; i++) {
/* source id found */
if (sources[i].id == id) {
printf("source already known, comparing timestamps: %04lX : %04lX\n", sources[i].timestamp, timestamp);
/* more current timestamp received, updating */
if (sources[i].timestamp < timestamp) {
sources[i].timestamp = timestamp;
return 1;
}
/* older, but still valid timestamp, not updating */
else if (sources[i].timestamp < timestamp + MAX_INTERVAL) {
return 1;
}
/* timestamp too old, discard this packet */
else {
puts("Timestamp too old, not routing");
@ -29,6 +28,7 @@ static uint8_t update_sources(uint8_t id, time_t timestamp) {
}
/* source id not yet stored creating new entry */
else if (!sources[i].id) {
puts("got to know a new source");
sources[i].id = id;
sources[i].timestamp = timestamp;
return 1;
@ -40,8 +40,9 @@ static uint8_t update_sources(uint8_t id, time_t timestamp) {
void route_packet(void* msg, int msg_size) {
weather_packet_header_t *header = (weather_packet_header_t*) msg;
weather_data_pkt_t* wdp = NULL;
if (header->type == WEATHER_DATA) {
weather_data_pkt_t* wdp = (weather_data_pkt_t*) msg;
wdp = (weather_data_pkt_t*) msg;
if (!update_sources(wdp->header.src, wdp->timestamp)) {
return;
}
@ -49,6 +50,14 @@ void route_packet(void* msg, int msg_size) {
if ((100.0 * rand()/(double) RAND_MAX) <= FLOODING_PROB) {
printf("Broadcasting packet...");
/* if broadcasting weather data, append current hop */
if (wdp != NULL) {
if (wdp->hop_counter < MAX_HOP_LIST) {
wdp->hops[wdp->hop_counter] = cc1100_get_address();
wdp->hop_counter++;
msg_size++;
}
}
if (cc1100_send_csmaca(0, WEATHER_PROTOCOL_NR, 0, (char*)msg, msg_size)) {
puts("successful!");
}