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

the simplest of pings is working

This commit is contained in:
Stephan Arndt 2012-12-11 16:03:00 +01:00
parent aed72abf4e
commit d2381df184
2 changed files with 183 additions and 58 deletions

View File

@ -1,19 +1,39 @@
#include <main.h>
//standard stuff
#include <stdio.h>
#include <string.h>
#include <vtimer.h>
#include <thread.h>
//shell stuff
#include <posix_io.h>
#include <shell.h>
#include <shell_commands.h>
#include <board_uart0.h>
//net stuff
#include <transceiver.h>
#include <cc1100.h>
#define STACKSIZE KERNEL_CONF_STACKSIZE_DEFAULT
#define SHELL_STACK (2048)
#define RADIO_STACK (2048)
char stack[STACKSIZE];
#define SND_BUFFER_SIZE (100)
#define RCV_BUFFER_SIZE (64)
char stack_shell[SHELL_STACK];
char stack_radio[RADIO_STACK];
uint8_t snd_buffer[SND_BUFFER_SIZE][CC1100_MAX_DATA_LENGTH];
msg_t msg_q[RCV_BUFFER_SIZE];
transceiver_command_t tcmd;
msg_t mesg;
static int radio_pid;
static radio_packet_t p;
// Shell commands for this application
const shell_command_t shell_commands[] =
@ -32,65 +52,47 @@ int pid_pingthread = 0;
// see header for documentation
void init(char* arg) {
transceiver_command_t tcmd;
msg_t m;
uint16_t addr = 0;
uint16_t chan = 10;
printf("transceiver pid is %d.\n", transceiver_pid);
int res = sscanf(arg, "init %hu %hu", &addr, &chan);
if (res == 1) {
puts("Setting address, keeping default channel");
if (addr >= MIN_ADDR && addr <= MAX_ADDR
&& chan >= MIN_CHAN && chan <= MAX_CHAN) {
//addr. given, channel should be default, which is 10 for us
//check for validity in any case, just to be sure
puts("Initializing 6lowpan..");
sixlowpan_init(TRANSCEIVER_CC1100,addr,0);
puts("Address set, setting channel");
tcmd.data = &chan;
m.type = SET_CHANNEL;
m.content.ptr = (void*) &tcmd;
puts("waiting for transceiver..");
msg_send_receive(&m, &m, transceiver_pid);
puts("Channel set.");
} else {
printf("ERROR: The address for the node has to be in %d to %d.\n",
MIN_ADDR, MAX_ADDR);
}
} else if (res == 2) {
if (res > 0) {
puts("Setting address and channel");
if (addr >= MIN_ADDR && addr <= MAX_ADDR
&& chan >= MIN_CHAN && chan <= MAX_CHAN) {
//addr. & channel are given, check validity
// check validity of address and channel
puts("Initializing 6lowpan..");
sixlowpan_init(TRANSCEIVER_CC1100,addr,0);
puts("address set, setting channel");
if (!radio_pid) {
radio_pid = thread_create(stack_radio, RADIO_STACK,
PRIORITY_MAIN - 2, CREATE_STACKTEST, radio, "radio");
printf("radio pid is %d.\n", radio_pid);
}
tcmd.data = &chan;
m.type = SET_CHANNEL;
m.content.ptr = (void*) &tcmd;
if (!transceiver_pid) {
puts("starting up transceiver..");
transceiver_init(TRANSCEIVER_CC1100);
transceiver_start();
transceiver_register(TRANSCEIVER_CC1100, radio_pid);
puts("started and registered the transceiver.");
printf("transceiver pid is %d.\n", transceiver_pid);
}
puts("waiting for transceiver..");
msg_send_receive(&m, &m, transceiver_pid);
puts("channel set");
set_radio_address((uint8_t) addr);
set_radio_channel((uint8_t) chan);
printf("radio address is %d\n", get_radio_address());
puts("turning on..");
switch_to_rx();
puts("done");
} else {
printf("ERROR: The address for the node has to be in %d to %d.\n",
MIN_ADDR, MAX_ADDR);
printf("ERROR: The channel for the node has to be in %d to %d.\n",
MIN_CHAN, MAX_CHAN);
}
//addr and channel given
} else {
//no correct argument given, but radio address is mandatory
puts("Usage: init [radioaddr] (radiochannel)");
@ -149,8 +151,82 @@ void help(char* cmdname) {
}
// see header for docs
void ping() {
//TODO implement
void ping(char* arg) {
uint16_t addr;
int res = sscanf(arg, "ping %hu", &addr);
if (res > 0) {
if (addr < MAX_ADDR) {
printf("Ready to send to address %d\n", addr);
//todo do something that makes sense actually
mesg.type = SND_PKT;
mesg.content.ptr = (char*) &tcmd;
tcmd.transceivers = TRANSCEIVER_CC1100;
tcmd.data = &p;
p.length = 4;
p.dst = addr;
puts("sending..");
p.data = "ping";
msg_send(&mesg, transceiver_pid, 1);
puts("sent");
} else {
printf("ERROR: Please give an address which is in range %d to %d.",
MIN_ADDR, MAX_ADDR);
}
} else {
puts("ERROR: Please give an address which you wish to ping.");
puts("For more information on how to use ping, type 'help ping'.");
}
}
void set_radio_address(uint8_t address) {
uint16_t addr = (uint16_t) address;
tcmd.transceivers = TRANSCEIVER_CC1100;
tcmd.data = &addr;
mesg.content.ptr = (char*) &tcmd;
mesg.type = SET_ADDRESS;
msg_send_receive(&mesg, &mesg, transceiver_pid);
}
uint16_t get_radio_address(void) {
uint16_t result;
tcmd.transceivers = TRANSCEIVER_CC1100;
tcmd.data = &result;
mesg.content.ptr = (char*) &tcmd;
mesg.type = GET_ADDRESS;
msg_send_receive(&mesg, &mesg, transceiver_pid);
return result;
}
void set_radio_channel(uint8_t channel) {
uint16_t chan = (uint16_t) channel;
tcmd.transceivers = TRANSCEIVER_CC1100;
tcmd.data = &chan;
mesg.content.ptr = (char*) &tcmd;
mesg.type = SET_CHANNEL;
msg_send_receive(&mesg, &mesg, transceiver_pid);
}
void switch_to_rx(void) {
mesg.type = SWITCH_RX;
mesg.content.ptr = (char*) &tcmd;
tcmd.transceivers = TRANSCEIVER_CC1100;
msg_send(&mesg, transceiver_pid, 1);
}
void powerdown(char *unused) {
mesg.type = POWERDOWN;
mesg.content.ptr = (char*) &tcmd;
tcmd.transceivers = TRANSCEIVER_CC1100;
msg_send(&mesg, transceiver_pid, 1);
}
void stop(char* unused) {
@ -159,13 +235,49 @@ void stop(char* unused) {
}
}
void hellothread(){
while(true){
puts("Hello!");
vtimer_usleep(1000*2000); //2secs
void radio(void) {
msg_t m;
radio_packet_t *p;
uint8_t i;
msg_init_queue(msg_q, RCV_BUFFER_SIZE);
while (1) {
msg_receive(&m);
if (m.type == PKT_PENDING) {
p = (radio_packet_t*) m.content.ptr;
printf("Packet waiting, process %p...\n", p);
printf("\tLength:\t%u\n", p->length);
printf("\tSrc:\t%u\n", p->src);
printf("\tDst:\t%u\n", p->dst);
printf("\tLQI:\t%u\n", p->lqi);
printf("\tRSSI:\t%u\n", p->rssi);
for (i = 0; i < p->length; i++) {
printf("%02X ", p->data[i]);
}
p->processing--;
printf("\n");
}
else if (m.type == ENOBUFFER) {
puts("Transceiver buffer full");
}
else {
puts("Unknown packet received");
}
}
}
//Runs the shell
void shell_runner(void) {
posix_open(uart0_handler_pid, 0);
shell_t shell;
shell_init(&shell, shell_commands, uart0_readc, uart0_putc);
shell_run(&shell);
}
/**
* The main function for this application, starts the shell and waits for
* commands.
@ -174,14 +286,13 @@ int main(void) {
puts("Ping Test Application\n");
puts("For commands type 'help'!\n");
thread_create(stack, STACKSIZE, PRIORITY_MAIN -3,8, hellothread,"hello");
thread_create(stack_shell, SHELL_STACK, PRIORITY_MAIN - 1,
CREATE_STACKTEST, shell_runner, "shell");
posix_open(uart0_handler_pid, 0);
shell_t shell;
shell_init(&shell, shell_commands, uart0_readc, uart0_putc);
shell_run(&shell);
while (true) {
//sleep a sec
vtimer_usleep(1000 * 1000);
}
return 0;
}

View File

@ -2,10 +2,14 @@
#define PING_TEST_H
#include <stdint.h>
#define MIN_ADDR 1
#define MIN_CHAN MIN_ADDR
#define MAX_CHAN 14
#define MAX_ADDR 255
#define SND_BUFFER_SIZE (100)
#define RCV_BUFFER_SIZE (64)
/**
* @brief Shows a helping text.
@ -30,7 +34,15 @@ void init(char * arg);
* Sends out pings from this node to another node in a continuous manner, until
* the stop command is used.
*/
void ping();
//void ping(char * arg);
//uint16_t get_radio_address(void);
void set_radio_address(uint8_t addr);
void set_radio_channel(uint8_t chan);
void switch_to_rx(void);
/**
* Stops any current pinging-action by this node and prints a summary of how
@ -42,4 +54,6 @@ void ping();
*/
void stop(char * arg);
void radio(void);
#endif /* PING_TEST_H */