1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/sys/ping/ping.c

96 lines
1.9 KiB
C
Raw Normal View History

#include <stdio.h>
#include <stdlib.h>
2013-12-16 17:54:58 +01:00
#include "thread.h"
#include "msg.h"
#include "cc110x/cc1100.h"
#include "lpc2387.h"
2013-04-02 14:34:42 +02:00
#include "vtimer.h"
#include "timex.h"
#include "gpioint.h"
2013-12-16 17:54:58 +01:00
#include "ping.h"
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;
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);
}
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();
}
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));
if (trans_ok < 0) {
2013-06-22 17:58:19 +02:00
print_failed();
}
}
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();
}
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);
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));
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
}
}
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);
}
2013-06-22 17:58:19 +02:00
void print_success(void)
{
printf("%s%f%s\n", "time=", rtt, "ms");
}
2013-06-22 17:58:19 +02:00
void print_failed(void)
{
printf("%s\n", "ping failed");
}
2013-06-22 17:58:19 +02:00
void init_payload(void)
{
pipa = malloc(sizeof(pipa));
pipa->payload = NULL;
}