1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-12-26 23:11:19 +01:00

added etx_beaconing files

This commit is contained in:
Stephan Arndt 2013-03-03 17:47:11 +01:00
parent 31b65059a8
commit e8256783f4
2 changed files with 189 additions and 0 deletions

View File

@ -0,0 +1,153 @@
/*
* etx_beaconing.c
*
* Created on: Feb 26, 2013
* Author: stephan
*/
#include "etx_beaconing.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <hwtimer.h>
#include <thread.h>
#include <transceiver.h>
#define MS 10000
unsigned int etx_beacon_pid;
unsigned int etx_radio_pid;
//Buffer
char etx_beacon_buf[ETX_BEACON_STACKSIZE];
char etx_radio_buf[ETX_BEACON_STACKSIZE];
uint8_t etx_send_buf[ETX_BUF_SIZE];
uint8_t etx_rec_buf[ETX_BUF_SIZE];
//Message queue for radio
msg_t msg_q[ETX_RCV_BUFFER_SIZE];
//Transceiver command for sending ETX probes
transceiver_command_t tcmd;
//Message to send probes with
msg_t mesg;
static etx_probe_t * get_etx_send_buf(void) {
return ((etx_probe_t *) &(etx_send_buf[0]));
}
static etx_probe_t * get_etx_rec_buf(void) {
return ((etx_probe_t *) &(etx_rec_buf[0]));
}
void init_etx_beaconing(void) {
//set code
puts("INit of etx beaconing");
etx_send_buf[0] = ETX_BEACON;
etx_beacon_pid = thread_create(etx_beacon_buf, ETX_BEACON_STACKSIZE,
PRIORITY_MAIN - 1, CREATE_STACKTEST,
etx_beacon, "etx_beacon");
etx_radio_pid = thread_create(etx_radio_buf, ETX_BEACON_STACKSIZE,
PRIORITY_MAIN - 1, CREATE_STACKTEST,
etx_radio, "etx_radio");
//register at transceiver
transceiver_register(TRANSCEIVER_CC1100, etx_radio_pid);
}
void etx_beacon(void) {
/*
* Sends a message every second +-10% jitter.
* A correcting variable keeps this timer from drifting off from the
* 1 second interval.
*/
radio_packet_t p;
uint8_t test = 0; //TODO delete, change to true or sth.
uint8_t jittercorrection = 10;
uint8_t jitter = (uint8_t) (rand() % 21);
while (test < 10) {
//Prepare the ETX packet
mesg.type = SND_PKT;
mesg.content.ptr = (char*) &tcmd;
tcmd.transceivers = TRANSCEIVER_CC1100;
tcmd.data = &p;
p.length = (get_etx_send_buf())->length + 2; //opt.+lngth+variable (addr,pkt_count) pairs
p.dst = 0;
p.data = (uint8_t *) &etx_send_buf;
msg_send(&mesg, transceiver_pid, 1);
test++;
hwtimer_wait(HWTIMER_TICKS(80*MS + jittercorrection*MS + jitter*MS));
/*
* The jitter correction is needed to stay at a base interval of 1 sec.
* between the wakeups. It takes the old jittervalue in account and
* modifies the time to wait accordingly.
*/
jittercorrection = 20 - jitter;
//the jitter is a number between 0 and 20
jitter = (uint8_t) (rand() % 21);
}
}
void handle_etx_beacon(void) {
/*
* Handle the ETX probe that has been received and update all infos.
* For now, just print a nice output of what I got, so i see it works as
* intended.
*/
etx_probe_t * probe = get_etx_rec_buf();
printf("ETX beacon package received with following values:\n"
"\tPackage Option:%x\n"
"\tPackage Length:%u\n\n", probe->code, probe->length);
for (uint8_t i = 0; i < probe->length / 3; i++) {
printf("\tIPv6 Short Addr:%u%u\n"
"\tPackets f. Addr:%u\n\n", probe->data[i * 3],
probe->data[i * 3 + 1], probe->data[i * 3 + 2]);
}
}
void etx_radio(void) {
msg_t m;
radio_packet_t *p;
etx_probe_t *etx_pkt;
msg_init_queue(msg_q, ETX_RCV_BUFFER_SIZE);
while (1) {
msg_receive(&m);
if (m.type == PKT_PENDING) {
p = (radio_packet_t*) m.content.ptr;
etx_pkt = (etx_probe_t *) p->data;
if (etx_pkt->code == ETX_BEACON) {
//copy to receive buffer
memcpy(etx_rec_buf, p->data, etx_pkt->length + 2);
handle_etx_beacon();
}
p->processing--;
}
else if (m.type == ENOBUFFER) {
puts("Transceiver buffer full");
}
else {
//packet is not for me
}
}
}

View File

@ -0,0 +1,36 @@
/*
* etx_beaconing.h
*
* Created on: Feb 26, 2013
* Author: stephan
*/
#ifndef ETX_BEACONING_H_
#define ETX_BEACONING_H_
#include "rpl_structs.h"
#define ETX_BEACON_STACKSIZE 2048
//[option|length|ipaddr.|packetcount] with up to 10 ipaddr|packetcount pairs
// 1 Byte 1 Byte 2 Byte 1 Byte
#define ETX_BUF_SIZE (32)
#define ETX_RCV_BUFFER_SIZE (64)
//ETX beaconing type (!ATTENTION! this is non-standard)
#define ETX_BEACON 0x20
//prototypes
void init_etx_beaconing(void);
void etx_beacon(void);
void etx_radio(void);
typedef struct __attribute__((packed)) {
uint8_t code;
uint8_t length;
uint8_t* data;
} etx_probe_t;
#endif /* ETX_BEACONING_H_ */