2011-04-04 20:33:18 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <cc1100.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include "weather_protocol.h"
|
|
|
|
#include "weather_routing.h"
|
|
|
|
|
2011-04-06 15:50:31 +02:00
|
|
|
uint8_t gossip_probability;
|
|
|
|
|
2011-04-04 20:33:18 +02:00
|
|
|
static source_timestamp_t sources[MAX_SOURCES];
|
|
|
|
|
|
|
|
static uint8_t update_sources(uint8_t id, time_t timestamp) {
|
|
|
|
uint8_t i;
|
2011-04-06 11:10:28 +02:00
|
|
|
|
|
|
|
puts("updating sources list");
|
2011-04-04 20:33:18 +02:00
|
|
|
for (i = 0; i < MAX_SOURCES; i++) {
|
|
|
|
/* source id found */
|
|
|
|
if (sources[i].id == id) {
|
2011-04-06 11:10:28 +02:00
|
|
|
printf("source already known, comparing timestamps: %04lX : %04lX\n", sources[i].timestamp, timestamp);
|
2011-04-04 20:33:18 +02:00
|
|
|
/* more current timestamp received, updating */
|
|
|
|
if (sources[i].timestamp < timestamp) {
|
|
|
|
sources[i].timestamp = timestamp;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/* timestamp too old, discard this packet */
|
|
|
|
else {
|
|
|
|
puts("Timestamp too old, not routing");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* source id not yet stored creating new entry */
|
|
|
|
else if (!sources[i].id) {
|
2011-04-06 11:10:28 +02:00
|
|
|
puts("got to know a new source");
|
2011-04-04 20:33:18 +02:00
|
|
|
sources[i].id = id;
|
|
|
|
sources[i].timestamp = timestamp;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
puts("No more sources could be stored!");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void route_packet(void* msg, int msg_size) {
|
|
|
|
weather_packet_header_t *header = (weather_packet_header_t*) msg;
|
2011-04-06 11:10:28 +02:00
|
|
|
weather_data_pkt_t* wdp = NULL;
|
2011-04-04 20:33:18 +02:00
|
|
|
if (header->type == WEATHER_DATA) {
|
2011-04-06 11:10:28 +02:00
|
|
|
wdp = (weather_data_pkt_t*) msg;
|
2011-04-04 20:33:18 +02:00
|
|
|
if (!update_sources(wdp->header.src, wdp->timestamp)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-06 15:50:31 +02:00
|
|
|
if ((100.0 * rand()/(double) RAND_MAX) <= gossip_probability) {
|
2011-04-04 20:33:18 +02:00
|
|
|
printf("Broadcasting packet...");
|
2011-04-06 11:10:28 +02:00
|
|
|
/* 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++;
|
|
|
|
}
|
|
|
|
}
|
2011-04-04 20:33:18 +02:00
|
|
|
if (cc1100_send_csmaca(0, WEATHER_PROTOCOL_NR, 0, (char*)msg, msg_size)) {
|
|
|
|
puts("successful!");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
puts("failed!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|