2010-09-22 15:10:42 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2013-12-16 17:54:58 +01:00
|
|
|
#include "thread.h"
|
|
|
|
#include "msg.h"
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2013-02-06 13:20:21 +01:00
|
|
|
#include "cc110x/cc1100.h"
|
2010-09-22 15:10:42 +02:00
|
|
|
#include "lpc2387.h"
|
|
|
|
|
2013-04-02 14:34:42 +02:00
|
|
|
#include "vtimer.h"
|
|
|
|
#include "timex.h"
|
2010-09-22 15:10:42 +02:00
|
|
|
#include "gpioint.h"
|
2013-12-16 17:54:58 +01:00
|
|
|
#include "ping.h"
|
2010-09-22 15:10:42 +02:00
|
|
|
|
|
|
|
ping_payload *pipa;
|
|
|
|
protocol_t protocol_id = 0;
|
|
|
|
radio_address_t r_address = 0;
|
2013-04-02 14:34:42 +02:00
|
|
|
timex_t start = 0;
|
2010-09-22 15:10:42 +02:00
|
|
|
float rtt = 0;
|
|
|
|
|
2013-06-22 17:58:19 +02:00
|
|
|
void ping_handler(void *payload, int payload_size,
|
|
|
|
packet_info_t *packet_info)
|
|
|
|
{
|
|
|
|
pong(packet_info->phy_src);
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void pong_handler(void *payload, int payload_size,
|
2013-06-22 17:58:19 +02:00
|
|
|
packet_info_t *packet_info)
|
|
|
|
{
|
|
|
|
calc_rtt();
|
|
|
|
print_success();
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
|
2013-06-22 17:58:19 +02:00
|
|
|
void pong(uint16_t src)
|
|
|
|
{
|
|
|
|
int trans_ok = cc1100_send_csmaca(src, protocol_id, 2, pipa->payload,
|
|
|
|
sizeof(pipa->payload));
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (trans_ok < 0) {
|
2013-06-22 17:58:19 +02:00
|
|
|
print_failed();
|
|
|
|
}
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
|
2013-06-22 17:58:19 +02:00
|
|
|
void ping_init(protocol_t protocol, uint8_t channr, radio_address_t addr)
|
|
|
|
{
|
|
|
|
protocol_id = protocol;
|
|
|
|
r_address = addr;
|
|
|
|
cc1100_set_packet_handler(protocol, ping_handler);
|
|
|
|
cc1100_set_channel(channr);
|
|
|
|
cc1100_set_address(r_address);
|
|
|
|
init_payload();
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
|
2013-06-22 17:58:19 +02:00
|
|
|
void ping(radio_address_t addr, uint8_t channr)
|
|
|
|
{
|
|
|
|
cc1100_set_packet_handler(protocol_id, pong_handler);
|
|
|
|
cc1100_set_channel(channr);
|
|
|
|
cc1100_set_address(r_address);
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
while (1) {
|
2013-06-22 17:58:19 +02:00
|
|
|
vtimer_now(&start);
|
|
|
|
int trans_ok = cc1100_send_csmaca(addr,
|
|
|
|
protocol_id, 2, pipa->payload, sizeof(pipa->payload));
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (trans_ok < 0) {
|
2013-06-22 17:58:19 +02:00
|
|
|
print_failed();
|
|
|
|
}
|
|
|
|
|
2013-12-19 18:37:13 +01:00
|
|
|
hwtimer_wait(HWTIMER_TICKS(500 * 1000));
|
2013-06-22 17:58:19 +02:00
|
|
|
}
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
|
2013-06-22 17:58:19 +02:00
|
|
|
void calc_rtt(void)
|
|
|
|
{
|
|
|
|
timex_t end;
|
|
|
|
vtimer_now(&end);
|
|
|
|
timex_t result = vtimer_sub(end, start);
|
|
|
|
|
2013-12-19 18:37:56 +01:00
|
|
|
rtt = result.seconds + (float)result.microseconds / (1000.0 * 1000.0);
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
|
2013-06-22 17:58:19 +02:00
|
|
|
void print_success(void)
|
|
|
|
{
|
|
|
|
printf("%s%f%s\n", "time=", rtt, "ms");
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
|
2013-06-22 17:58:19 +02:00
|
|
|
void print_failed(void)
|
|
|
|
{
|
|
|
|
printf("%s\n", "ping failed");
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
|
2013-06-22 17:58:19 +02:00
|
|
|
void init_payload(void)
|
|
|
|
{
|
|
|
|
pipa = malloc(sizeof(pipa));
|
|
|
|
pipa->payload = NULL;
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|