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

Merge pull request #760 from Kijewski/shell-args

shell: Use argc and argv in shell handlers
This commit is contained in:
Christian Mehlis 2014-02-26 15:37:14 +01:00
commit 92eaa51f5c
21 changed files with 416 additions and 365 deletions

View File

@ -68,9 +68,10 @@ static void appserver_thread(void)
ccnl_riot_appserver_start(relay_pid);
}
static void riot_ccn_appserver(char *str)
static void riot_ccn_appserver(int argc, char **argv)
{
(void) str; /* unused */
(void) argc; /* the function takes no arguments */
(void) argv;
if (appserver_pid) {
/* already running */
@ -82,17 +83,15 @@ static void riot_ccn_appserver(char *str)
}
#endif
static void riot_ccn_express_interest(char *str)
static void riot_ccn_express_interest(int argc, char **argv)
{
char *given_interest = strtok(str, " "); /* str=interest, skip that */
given_interest = strtok(NULL, " ");
static const char *default_interest = "/ccnx/0.7.1/doc/technical/CanonicalOrder.txt";
if (!given_interest) {
if (argc < 2) {
strncpy(small_buf, default_interest, 100); // null terminated
}
else {
strncpy(small_buf, given_interest, 100);
strncpy(small_buf, argv[1], 100);
}
DEBUG("in='%s'\n", small_buf);
@ -111,34 +110,18 @@ static void riot_ccn_express_interest(char *str)
puts("done");
}
static void riot_ccn_register_prefix(char *str)
static void riot_ccn_register_prefix(int argc, char **argv)
{
char *given_prefix = strtok(str, " ");
given_prefix = strtok(NULL, " ");
static const char *default_prefix = "/ccnx/0.7.1/doc/technical";
if (!given_prefix) {
strncpy(small_buf, default_prefix, 100);
}
else {
strncpy(small_buf, given_prefix, 100);
if (argc < 4) {
puts("enter: prefix </path/to/abc> <type> <faceid>");
return;
}
strncpy(small_buf, argv[1], 100);
DEBUG("prefix='%s'\n", small_buf);
char *type = strtok(NULL, " ");
if (!type) {
puts("enter: prefix /path/to/abc <type> faceid");
return;
}
char *faceid = strtok(NULL, " ");//"2"; // 0=trans;1=msg
if (!faceid) {
puts("enter: prefix /path/to/abc <type> faceid");
return;
}
char *type = argv[2];
char *faceid = argv[3]; // 0=trans;1=msg
int content_len = ccnl_riot_client_publish(relay_pid, small_buf, faceid, type, big_buf);
@ -152,49 +135,42 @@ static void relay_thread(void)
ccnl_riot_relay_start(shell_max_cache_entries, shell_threshold_prefix, shell_threshold_aggregate);
}
static void riot_ccn_relay_start(char *str)
static void riot_ccn_relay_start(int argc, char **argv)
{
if (relay_pid) {
/* already running */
return;
}
char *toc_str = strtok(str, " ");
toc_str = strtok(NULL, " ");
if (!toc_str) {
if (argc < 2) {
shell_max_cache_entries = CCNL_DEFAULT_MAX_CACHE_ENTRIES;
}
else {
shell_max_cache_entries = atoi(toc_str);
shell_max_cache_entries = atoi(argv[1]);
}
toc_str = strtok(NULL, " ");
if (!toc_str) {
if (argc < 3) {
shell_threshold_prefix = CCNL_DEFAULT_THRESHOLD_PREFIX;
}
else {
shell_threshold_prefix = atoi(toc_str);
shell_threshold_prefix = atoi(argv[2]);
}
toc_str = strtok(NULL, " ");
if (!toc_str) {
if (argc < 4) {
shell_threshold_aggregate = CCNL_DEFAULT_THRESHOLD_AGGREGATE;
}
else {
shell_threshold_aggregate = atoi(toc_str);
shell_threshold_aggregate = atoi(argv[3]);
}
relay_pid = thread_create(relay_stack, KERNEL_CONF_STACKSIZE_PRINTF, PRIORITY_MAIN - 2, CREATE_STACKTEST, relay_thread, "relay");
DEBUG("ccn-lite relay on thread_id %d...\n", relay_pid);
}
static void riot_ccn_relay_stop(char *str)
static void riot_ccn_relay_stop(int argc, char **argv)
{
(void) str; /* unused */
(void) argc; /* the function takes no arguments */
(void) argv;
msg_t m;
m.content.value = 0;
@ -206,9 +182,10 @@ static void riot_ccn_relay_stop(char *str)
}
#if RIOT_CCN_TESTS
static void riot_ccn_pit_test(char *str)
static void riot_ccn_pit_test(int argc, char **argv)
{
(void) str; /* unused */
(void) argc; /* the function takes no arguments */
(void) argv;
char name[] = "/riot/test";
@ -254,9 +231,10 @@ static void riot_ccn_pit_test(char *str)
printf("done: tried to send %d interests\n", segment);
}
static void riot_ccn_fib_test(char *str)
static void riot_ccn_fib_test(int argc, char **argv)
{
(void) str; /* unused */
(void) argc; /* the function takes no arguments */
(void) argv;
char type[] = "newTRANSface";
char faceid[] = "42";
@ -283,9 +261,10 @@ static void riot_ccn_fib_test(char *str)
}
#endif
static void riot_ccn_populate(char *str)
static void riot_ccn_populate(int argc, char **argv)
{
(void) str; /* unused */
(void) argc; /* the function takes no arguments */
(void) argv;
msg_t m;
m.content.value = 0;
@ -293,9 +272,10 @@ static void riot_ccn_populate(char *str)
msg_send(&m, relay_pid, 1);
}
static void riot_ccn_stat(char *str)
static void riot_ccn_stat(int argc, char **argv)
{
(void) str; /* unused */
(void) argc; /* the function takes no arguments */
(void) argv;
msg_t m;
m.content.value = 0;

View File

@ -13,47 +13,37 @@
/* RPL shell command handlers */
/**
* @brief Shell command to initializes RPL and Destiny
*
* @param[in] str Shell input
*/
void rpl_udp_init(char *str);
void rpl_udp_init(int argc, char **argv);
/**
* @brief Shell command to set node's ID
*
* @param[in] str Shell input
*/
void rpl_udp_set_id(char *id);
void rpl_udp_set_id(int argc, char **argv);
/**
* @brief Loops through the routing table
*
* @param[in] unused Guess what
*/
void rpl_udp_loop(char *unused);
void rpl_udp_loop(int argc, char **argv);
/**
* @brief Shows the routing table
*
* @param[in] unused Guess what
*/
void rpl_udp_table(char *unused);
void rpl_udp_table(int argc, char **argv);
/**
* @brief Shows the dodag
*
* @param[in] unused Guess what
*/
void rpl_udp_dodag(char *unused);
void rpl_udp_dodag(int argc, char **argv);
/* UDP shell command handlers */
void udp_server(char *unused);
void udp_send(char *str);
void udp_server(int argc, char **argv);
void udp_send(int argc, char **argv);
/* helper command handlers */
void rpl_udp_ip(char *unused);
void rpl_udp_ip(int argc, char **argv);
void rpl_udp_ignore(char *addr);
void rpl_udp_ignore(int argc, char **argv);
/* monitoring thread */
void rpl_udp_monitor(void);

View File

@ -40,23 +40,25 @@ extern uint8_t ipv6_ext_hdr_len;
msg_t msg_q[RCV_BUFFER_SIZE];
/* prints current IPv6 adresses */
void rpl_udp_ip(char *unused)
void rpl_udp_ip(int argc, char **argv)
{
(void) unused;
(void) argc;
(void) argv;
ipv6_iface_print_addrs();
}
void rpl_udp_set_id(char *id_str)
void rpl_udp_set_id(int argc, char **argv)
{
int res = sscanf(id_str, "set %hu", &id);
if (res < 1) {
printf("Usage: init address\n");
if (argc != 2) {
printf("Usage: %s address\n", argv[0]);
printf("\taddress must be an 8 bit integer\n");
printf("\n\t(Current address is %u)\n", id);
return;
}
id = atoi(argv[1]);
printf("Set node ID to %u\n", id);
}
@ -119,7 +121,7 @@ void rpl_udp_monitor(void)
transceiver_command_t tcmd;
void rpl_udp_ignore(char *addr)
void rpl_udp_ignore(int argc, char **argv)
{
uint16_t a;
@ -134,13 +136,13 @@ void rpl_udp_ignore(char *addr)
tcmd.transceivers = TRANSCEIVER_CC1100;
tcmd.data = &a;
a = atoi(addr + strlen("ign "));
if (strlen(addr) > strlen("ign ")) {
if (argc == 2) {
a = atoi(argv[1]);
printf("sending to transceiver (%u): %u\n", transceiver_pid, (*(uint8_t *)tcmd.data));
msg_send(&mesg, transceiver_pid, 1);
}
else {
puts("Usage:\tign <addr>");
printf("Usage: %s <addr>\n", argv[0]);
}
}

View File

@ -40,26 +40,22 @@ ipv6_addr_t std_addr;
uint8_t is_root = 0;
void rpl_udp_init(char *str)
void rpl_udp_init(int argc, char **argv)
{
transceiver_command_t tcmd;
msg_t m;
uint8_t chan = RADIO_CHANNEL;
char *toc_str = strtok(str, " ");
toc_str = strtok(NULL, " ");
if (!toc_str) {
printf("Usage: init (r|n)\n");
if (argc != 2) {
printf("Usage: %s (r|n)\n", argv[0]);
printf("\tr\tinitialize as root\n");
printf("\tn\tinitialize as node router\n");
return;
}
char command = *toc_str;
uint8_t state;
char command = argv[1][0];
if ((command == 'n') || (command == 'r')) {
printf("INFO: Initialize as %s on address %d\n", ((command == 'n') ? "node" : "root"), id);
@ -118,9 +114,10 @@ void rpl_udp_init(char *str)
/* start transceiver watchdog */
}
void rpl_udp_loop(char *unused)
void rpl_udp_loop(int argc, char **argv)
{
(void) unused;
(void) argc;
(void) argv;
rpl_routing_entry_t *rtable;
@ -161,9 +158,10 @@ void rpl_udp_loop(char *unused)
printf("########################\n");
}
void rpl_udp_table(char *unused)
void rpl_udp_table(int argc, char **argv)
{
(void) unused;
(void) argc;
(void) argv;
rpl_routing_entry_t *rtable;
rtable = rpl_get_routing_table();
@ -187,9 +185,10 @@ void rpl_udp_table(char *unused)
printf("$\n");
}
void rpl_udp_dodag(char *unused)
void rpl_udp_dodag(int argc, char **argv)
{
(void) unused;
(void) argc;
(void) argv;
printf("---------------------------\n");
rpl_dodag_t *mydodag = rpl_get_my_dodag();

View File

@ -19,6 +19,7 @@
*/
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <inttypes.h>
@ -41,9 +42,11 @@ char addr_str[IPV6_MAX_ADDR_STR_LEN];
void init_udp_server(void);
/* UDP server thread */
void udp_server(char *unused)
void udp_server(int argc, char **argv)
{
(void) unused;
(void) argc;
(void) argv;
int udp_server_thread_pid = thread_create(udp_server_stack_buffer, KERNEL_CONF_STACKSIZE_MAIN, PRIORITY_MAIN, CREATE_STACKTEST, init_udp_server, "init_udp_server");
printf("UDP SERVER ON PORT %d (THREAD PID: %d)\n", HTONS(SERVER_PORT), udp_server_thread_pid);
}
@ -83,7 +86,7 @@ void init_udp_server(void)
}
/* UDP send command */
void udp_send(char *str)
void udp_send(int argc, char **argv)
{
int sock;
sockaddr6_t sa;
@ -92,11 +95,16 @@ void udp_send(char *str)
int address;
char text[5];
if (sscanf(str, "send %i %s", &address, text) < 2) {
if (argc != 3) {
printf("usage: send <addr> <text>\n");
return;
}
address = atoi(argv[1]);
strncpy(text, argv[2], sizeof (text));
text[sizeof (text) - 1] = 0;
sock = destiny_socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
if (-1 == sock) {

View File

@ -9,6 +9,6 @@
#define __PS_H
void thread_print_all(void);
void _ps_handler(char *);
void _ps_handler(int argc, char **argv);
#endif /* __PS_H */

View File

@ -22,10 +22,12 @@
#define __SHELL_H
#include <stdint.h>
typedef void (*shell_command_handler_t)(int argc, char **argv);
typedef struct shell_command_t {
char *name;
char *desc;
void (*handler)(char *);
shell_command_handler_t handler;
} shell_command_t;
typedef struct shell_t {

View File

@ -23,13 +23,11 @@
#include "cc110x.h"
void _cc110x_get_set_address_handler(char *addr)
void _cc110x_get_set_address_handler(int argc, char **argv)
{
int16_t a;
if (argc > 1) {
int16_t a = atoi(argv[1]);
a = atoi(addr + 5);
if (strlen(addr) > 5) {
printf("[cc110x] Setting address %i ... ", a);
cc1100_set_address((radio_address_t)a);
@ -45,13 +43,11 @@ void _cc110x_get_set_address_handler(char *addr)
}
}
void _cc110x_get_set_channel_handler(char *addr)
void _cc110x_get_set_channel_handler(int argc, char **argv)
{
int16_t a;
if (argc > 1) {
int16_t a = atoi(argv[1]);
a = atoi(addr + 5);
if (strlen(addr) > 5) {
printf("[cc110x] Setting channel %i...", a);
cc1100_set_channel(a);

View File

@ -44,10 +44,12 @@ static inline uint8_t sector_read(unsigned char *read_buf, unsigned long sector,
return 0;
}
void _get_sectorsize(char *unused)
void _get_sectorsize(int argc, char **argv)
{
unsigned short ssize;
(void) argc;
(void) argv;
unsigned short ssize;
if (MCI_ioctl(GET_SECTOR_SIZE, &ssize) == RES_OK) {
printf("[disk] sector size is %u\n", ssize);
}
@ -56,10 +58,12 @@ void _get_sectorsize(char *unused)
}
}
void _get_blocksize(char *unused)
void _get_blocksize(int argc, char **argv)
{
unsigned long bsize;
(void) argc;
(void) argv;
unsigned long bsize;
if (MCI_ioctl(GET_BLOCK_SIZE, &bsize) == RES_OK) {
printf("[disk] block size is %lu\n", bsize);
}
@ -68,10 +72,12 @@ void _get_blocksize(char *unused)
}
}
void _get_sectorcount(char *unused)
void _get_sectorcount(int argc, char **argv)
{
unsigned long scount;
(void) argc;
(void) argv;
unsigned long scount;
if (MCI_ioctl(GET_SECTOR_COUNT, &scount) == RES_OK) {
printf("[disk] sector count is %lu\n", scount);
}
@ -80,14 +86,13 @@ void _get_sectorcount(char *unused)
}
}
void _read_sector(char *sector)
void _read_sector(int argc, char **argv)
{
unsigned long sectornr, scount;
unsigned short ssize;
if (strlen(sector) > strlen(DISK_READ_SECTOR_CMD) + 1) {
sectornr = atol(sector + strlen(DISK_READ_SECTOR_CMD) + 1);
if (argc == 2) {
sectornr = atol(argv[1]);
if ((MCI_ioctl(GET_SECTOR_COUNT, &scount) == RES_OK) && (MCI_ioctl(GET_SECTOR_SIZE, &ssize) == RES_OK)) {
unsigned char read_buf[ssize];
@ -100,65 +105,57 @@ void _read_sector(char *sector)
printf("[disk] Error while reading sector %lu\n", sectornr);
}
else {
printf("[disk] Usage:\n%s <SECTOR>\n", DISK_READ_SECTOR_CMD);
printf("[disk] Usage:\n%s <SECTOR>\n", argv[0]);
return;
}
}
void _read_bytes(char *bytes)
void _read_bytes(int argc, char **argv)
{
unsigned long sector = 1, scount, offset;
unsigned short ssize, length;
char *tok;
/* tokenize user input */
tok = strtok(bytes + strlen(DISK_READ_BYTES_CMD) + 1, " ");
if (argc != 3) {
printf("[disk] Usage:\n%s <OFFSET> <LENGTH>\n", argv[0]);
return;
}
if (tok) {
offset = atol(tok);
tok = strtok(NULL, " ");
offset = atol(argv[1]);
length = atoi(argv[2]);
if (tok) {
length = atoi(tok);
/* get card info */
if ((MCI_ioctl(GET_SECTOR_COUNT, &scount) == RES_OK) && (MCI_ioctl(GET_SECTOR_SIZE, &ssize) == RES_OK)) {
/* calculate sector and offset position */
sector = (offset / ssize) + 1;
offset = (offset % ssize);
/* preapre buffer (size must be a multiple of sector size) */
unsigned char read_buf[((length / ssize) + 1) * 512];
if (length) {
/* get card info */
if ((MCI_ioctl(GET_SECTOR_COUNT, &scount) == RES_OK) && (MCI_ioctl(GET_SECTOR_SIZE, &ssize) == RES_OK)) {
/* calculate sector and offset position */
sector = (offset / ssize) + 1;
offset = (offset % ssize);
/* preapre buffer (size must be a multiple of sector size) */
unsigned char read_buf[((length / ssize) + 1) * 512];
/* read from several sectors */
if (length > (ssize - offset)) {
/* buffer offset */
unsigned long j = 0;
/* chunk from current sector */
unsigned short tmp = ssize - offset;
/* read from several sectors */
if (length > (ssize - offset)) {
/* buffer offset */
unsigned long j = 0;
/* chunk from current sector */
unsigned short tmp = ssize - offset;
while (length) {
sector_read(read_buf + j, sector++, tmp, offset);
/* decrease length and recalculate chunk */
length -= tmp;
tmp = (length >= ssize) ? ssize : length;
}
while (length) {
sector_read(read_buf + j, sector++, tmp, offset);
/* decrease length and recalculate chunk */
length -= tmp;
tmp = (length >= ssize) ? ssize : length;
}
return;
} /* length > (ssize - offset) */
/* read only one sector */
else {
if (sector_read(read_buf, sector, length, offset)) {
return;
}
} /* length < (ssize - offset) */
} /* ioctl */
printf("[disk] Error while reading sector %lu\n", sector);
return;
} /* length > (ssize - offset) */
/* read only one sector */
else {
if (sector_read(read_buf, sector, length, offset)) {
return;
} /* length */
} /* strtok #2 */
} /* strtok #1 */
}
} /* length < (ssize - offset) */
} /* ioctl */
printf("[disk] Error while reading sector %lu\n", sector);
return;
printf("[disk] Usage:\n%s <OFFSET> <LENGTH>\n", DISK_READ_BYTES_CMD);
}

View File

@ -20,8 +20,10 @@
extern void heap_stats(void);
void _heap_handler(char *unused)
void _heap_handler(int argc, char **argv)
{
(void) unused;
(void) argc;
(void) argv;
heap_stats();
}

View File

@ -20,16 +20,13 @@
#include <stdlib.h>
#include "config.h"
void _id_handler(char *id)
void _id_handler(int argc, char **argv)
{
long newid;
newid = atoi(id + 3);
if (strlen(id) < 3) {
if (argc < 2) {
printf("Current id: %u\n", sysconfig.id);
}
else {
long newid = atoi(argv[1]);
printf("Setting new id %lu\n", newid);
sysconfig.id = newid;

View File

@ -18,16 +18,19 @@
#include <stdio.h>
#include "ltc4150.h"
void _get_current_handler(char *unused)
void _get_current_handler(int argc, char **argv)
{
(void) unused;
(void) argc;
(void) argv;
printf("Power usage: %.4f mA (%.4f mA avg/ %.4f mAh total / %i usec)\n", ltc4150_get_current_mA(), ltc4150_get_avg_mA(), ltc4150_get_total_mAh(), ltc4150_get_interval());
printf("Power usage: %.4f mA (%.4f mA avg/ %.4f mAh total / %i usec)\n",
ltc4150_get_current_mA(), ltc4150_get_avg_mA(), ltc4150_get_total_mAh(), ltc4150_get_interval());
}
void _reset_current_handler(char *unused)
void _reset_current_handler(int argc, char **argv)
{
(void) unused;
(void) argc;
(void) argv;
ltc4150_start();
}

View File

@ -23,27 +23,26 @@
#include "hwtimer.h"
#include "random.h"
void _mersenne_init(char *str)
void _mersenne_init(int argc, char **argv)
{
int initval;
char *toc_str = strtok(str, " ");
toc_str = strtok(NULL, " ");
if (!toc_str) {
if (argc == 1) {
initval = hwtimer_now();
printf("PRNG inizialized to current time: %d\n", initval);
printf("PRNG initialized to current time: %d\n", initval);
}
else {
initval = atoi(toc_str);
printf("PRNG inizialized given value: %d\n", initval);
initval = atoi(argv[1]);
printf("PRNG initialized given value: %d\n", initval);
}
genrand_init(initval);
}
void _mersenne_get(char *str)
void _mersenne_get(int argc, char **argv)
{
(void) str;
(void) argc;
(void) argv;
printf("%" PRIu32 "\n", genrand_uint32());
}

View File

@ -17,9 +17,10 @@
#include "ps.h"
void _ps_handler(char *unused)
void _ps_handler(int argc, char **argv)
{
(void) unused;
(void) argc;
(void) argv;
thread_print_all();
}

View File

@ -22,7 +22,7 @@
#ifdef MODULE_RTC
#include "rtc.h"
void _gettime_handler(void)
static void _gettime_handler(void)
{
struct tm now;
rtc_get_localtime(&now);
@ -30,40 +30,44 @@ void _gettime_handler(void)
printf("%s", asctime(&now));
}
void _settime_handler(char *c)
static void _settime_handler(char **argv)
{
struct tm now;
short i1, i2, i3;
int res;
uint16_t month, epoch_year;
res = sscanf(c, "date %hu-%hu-%u %u:%u:%u",
&epoch_year,
&month,
(unsigned int *) & (now.tm_mday),
(unsigned int *) & (now.tm_hour),
(unsigned int *) & (now.tm_min),
(unsigned int *) & (now.tm_sec));
do {
res = sscanf(argv[1], "%hd-%hd-%hd", &i1, &i2, &i3);
if (res != 3) {
break;
}
now.tm_year = i1 - 1900;
now.tm_mon = i2 - 1;
now.tm_mday = i3;
if (res < 6) {
printf("Usage: date YYYY-MM-DD hh:mm:ss\n");
return;
}
else {
res = sscanf(argv[2], "%hd:%hd:%hd", &i1, &i2, &i3);
if (res != 3) {
break;
}
now.tm_hour = i1;
now.tm_min = i2;
now.tm_sec = i3;
rtc_set_localtime(&now);
puts("OK");
}
return;
} while (0);
now.tm_year = epoch_year - 1900;
now.tm_mon = month - 1;
rtc_set_localtime(&now);
printf("Usage: %s YYYY-MM-DD hh:mm:ss\n", argv[0]);
}
void _date_handler(char *c)
void _date_handler(int argc, char **argv)
{
if (strlen(c) == 4) {
if (argc != 3) {
_gettime_handler();
}
else {
_settime_handler(c);
_settime_handler(argv);
}
}

View File

@ -25,8 +25,11 @@
extern float sht11_temperature_offset;
void _get_humidity_handler(char *unused)
void _get_humidity_handler(int argc, char **argv)
{
(void) argc;
(void) argv;
uint8_t success;
sht11_val_t sht11_val;
success = sht11_read_sensor(&sht11_val, HUMIDITY | TEMPERATURE);
@ -39,8 +42,11 @@ void _get_humidity_handler(char *unused)
(double) sht11_val.relhum, (double) sht11_val.relhum_temp);
}
}
void _get_temperature_handler(char *unused)
void _get_temperature_handler(int argc, char **argv)
{
(void) argc;
(void) argv;
uint8_t success;
sht11_val_t sht11_val;
success = sht11_read_sensor(&sht11_val, TEMPERATURE);
@ -52,8 +58,11 @@ void _get_temperature_handler(char *unused)
printf("Temperature: %-6.2f°C\n", (double) sht11_val.temperature);
}
}
void _get_weather_handler(char *unused)
void _get_weather_handler(int argc, char **argv)
{
(void) argc;
(void) argv;
uint8_t success;
sht11_val_t sht11_val;
success = sht11_read_sensor(&sht11_val, HUMIDITY | TEMPERATURE);
@ -68,13 +77,13 @@ void _get_weather_handler(char *unused)
}
}
void _set_offset_handler(char *offset)
void _set_offset_handler(int argc, char **argv)
{
if (strlen(offset) == 6) {
puts("Usage: offset <OFFSET>");
if (argc != 2) {
printf("Usage: %s <OFFSET>\n", argv[0]);
}
else {
sht11_temperature_offset = atoi(offset + 7);
sht11_temperature_offset = atoi(argv[1]);
printf("Temperature offset set to %f\n", (double) sht11_temperature_offset);
}
}

View File

@ -17,8 +17,10 @@
#include "kernel.h"
void _reboot_handler(char *unused)
void _reboot_handler(int argc, char **argv)
{
(void) unused;
(void) argc;
(void) argv;
reboot();
}

View File

@ -52,7 +52,7 @@
/* checked for type safety */
void _transceiver_get_set_address_handler(char *addr)
void _transceiver_get_set_address_handler(int argc, char **argv)
{
msg_t mesg;
transceiver_command_t tcmd;
@ -67,8 +67,8 @@ void _transceiver_get_set_address_handler(char *addr)
tcmd.data = &a;
mesg.content.ptr = (char *) &tcmd;
if (strlen(addr) > 5) {
a = atoi(addr + 5);
if (argc > 1) {
a = atoi(argv[1]);
printf("[transceiver] trying to set address %" PRIu16 "\n", a);
mesg.type = SET_ADDRESS;
}
@ -81,7 +81,7 @@ void _transceiver_get_set_address_handler(char *addr)
}
/* checked for type safety */
void _transceiver_get_set_channel_handler(char *chan)
void _transceiver_get_set_channel_handler(int argc, char **argv)
{
msg_t mesg;
transceiver_command_t tcmd;
@ -96,8 +96,8 @@ void _transceiver_get_set_channel_handler(char *chan)
tcmd.data = &c;
mesg.content.ptr = (char *) &tcmd;
if (strlen(chan) > 5) {
c = atoi(chan + 5);
if (argc > 1) {
c = atoi(argv[1]);
printf("[transceiver] Trying to set channel %" PRIi32 "\n", c);
mesg.type = SET_CHANNEL;
}
@ -114,93 +114,85 @@ void _transceiver_get_set_channel_handler(char *chan)
}
}
void _transceiver_send_handler(char *pkt)
void _transceiver_send_handler(int argc, char **argv)
{
msg_t mesg;
transceiver_command_t tcmd;
char text_msg[TEXT_SIZE];
if (transceiver_pid < 0) {
puts("Transceiver not initialized");
return;
}
if (argc != 3) {
printf("Usage:\t%s <ADDR> <MSG>\n", argv[0]);
return;
}
radio_packet_t p;
int8_t response;
radio_address_t addr;
char *tok;
if (transceiver_pid < 0) {
puts("Transceiver not initialized");
return;
}
transceiver_command_t tcmd;
tcmd.transceivers = _TC_TYPE;
tcmd.data = &p;
tok = strtok(pkt + 7, " ");
char text_msg[TEXT_SIZE];
memset(text_msg, 0, TEXT_SIZE);
strcpy(text_msg, argv[2]);
if (tok) {
addr = atoi(tok);
tok = strtok(NULL, " ");
p.data = (uint8_t *) text_msg;
p.length = strlen(text_msg) + 1;
p.dst = atoi(argv[1]);
if (tok) {
memset(text_msg, 0, TEXT_SIZE);
memcpy(text_msg, tok, strlen(tok));
p.data = (uint8_t *) text_msg;
p.length = strlen(text_msg) + 1;
p.dst = addr;
mesg.type = SND_PKT;
mesg.content.ptr = (char *)&tcmd;
printf("[transceiver] Sending packet of length %" PRIu16 " to %" PRIu16 ": %s\n", p.length, p.dst, (char*) p.data);
msg_send_receive(&mesg, &mesg, transceiver_pid);
response = mesg.content.value;
printf("[transceiver] Packet sent: %" PRIi8 "\n", response);
return;
}
}
puts("Usage:\ttxtsnd <ADDR> <MSG>");
}
/* checked for type safety */
void _transceiver_monitor_handler(char *mode)
{
msg_t mesg;
transceiver_command_t tcmd;
uint8_t m;
if (transceiver_pid < 0) {
puts("Transceiver not initialized");
return;
}
tcmd.transceivers = _TC_TYPE;
tcmd.data = &m;
mesg.type = SND_PKT;
mesg.content.ptr = (char *) &tcmd;
if (strlen(mode) > 8) {
m = atoi(mode + 8);
printf("Setting monitor mode: %" PRIu8 "\n", m);
mesg.type = SET_MONITOR;
msg_send(&mesg, transceiver_pid, 1);
}
else {
puts("Usage:\nmonitor <MODE>");
}
printf("[transceiver] Sending packet of length %" PRIu16 " to %" PRIu16 ": %s\n", p.length, p.dst, (char*) p.data);
msg_send_receive(&mesg, &mesg, transceiver_pid);
int8_t response = mesg.content.value;
printf("[transceiver] Packet sent: %" PRIi8 "\n", response);
}
/* checked for type safety */
void _transceiver_get_set_pan_handler(char *pan) {
transceiver_command_t tcmd;
msg_t mesg;
int32_t p;
void _transceiver_monitor_handler(int argc, char **argv)
{
if (transceiver_pid < 0) {
puts("Transceiver not initialized");
return;
}
else if (argc != 2) {
printf("Usage:\n%s <MODE>\n", argv[0]);
return;
}
uint8_t m = atoi(argv[1]);
printf("Setting monitor mode: %" PRIu8 "\n", m);
transceiver_command_t tcmd;
tcmd.transceivers = _TC_TYPE;
tcmd.data = &m;
msg_t mesg;
mesg.content.ptr = (char *) &tcmd;
mesg.type = SET_MONITOR;
msg_send(&mesg, transceiver_pid, 1);
}
/* checked for type safety */
void _transceiver_get_set_pan_handler(int argc, char **argv)
{
if (transceiver_pid < 0) {
puts("Transceiver not initialized");
return;
}
int32_t p;
transceiver_command_t tcmd;
tcmd.transceivers = _TC_TYPE;
tcmd.data = &p;
msg_t mesg;
mesg.content.ptr = (char*) &tcmd;
if (strlen(pan) > 4) {
p = atoi(pan+4);
if (argc > 1) {
p = atoi(argv[1]);
printf("[transceiver] Trying to set pan %" PRIi32 "\n", p);
mesg.type = SET_PAN;
}
@ -218,37 +210,37 @@ void _transceiver_get_set_pan_handler(char *pan) {
/* checked for type safety */
#ifdef DBG_IGNORE
void _transceiver_set_ignore_handler(char *addr)
void _transceiver_set_ignore_handler(int argc, char **argv)
{
transceiver_command_t tcmd;
msg_t mesg;
radio_address_t a;
int16_t response;
if (transceiver_pid < 0) {
puts("Transceiver not initialized");
return;
}
else if (argc != 2) {
printf("Usage:\n%s <address>\n", argv[1]);
}
radio_address_t a;
transceiver_command_t tcmd;
tcmd.transceivers = _TC_TYPE;
tcmd.data = &a;
msg_t mesg;
mesg.content.ptr = (char*) &tcmd;
if (strlen(addr) > 4) {
a = atoi(addr + 4);
printf("[transceiver] trying to add address %" PRIu16 " to the ignore list \n", a);
mesg.type = DBG_IGN;
msg_send_receive(&mesg, &mesg, transceiver_pid);
response = a;
if (response == -1) {
printf("Error: ignore list full\n");
}
else {
printf("Success (added at index %" PRIi16 ").\n", response);
}
a = atoi(addr + 4);
printf("[transceiver] trying to add address %" PRIu16 " to the ignore list \n", a);
mesg.type = DBG_IGN;
msg_send_receive(&mesg, &mesg, transceiver_pid);
int16_t response = a;
if (response == -1) {
printf("Error: ignore list full\n");
}
else {
puts("Usage:\nign <address>");
printf("Success (added at index %" PRIi16 ").\n", response);
}
}
#endif

View File

@ -23,34 +23,34 @@
#include <stdlib.h>
#include "shell_commands.h"
extern void _reboot_handler(char *unused);
extern void _reboot_handler(int argc, char **argv);
#ifdef MODULE_CONFIG
extern void _id_handler(char *id);
extern void _id_handler(int argc, char **argv);
#endif
#ifdef MODULE_LPC_COMMON
extern void _heap_handler(char *unused);
extern void _heap_handler(int argc, char **argv);
#endif
#ifdef MODULE_PS
extern void _ps_handler(char *unused);
extern void _ps_handler(int argc, char **argv);
#endif
#ifdef MODULE_RTC
extern void _date_handler(char *now);
extern void _date_handler(int argc, char **argv);
#endif
#ifdef MODULE_SHT11
extern void _get_temperature_handler(char *unused);
extern void _get_humidity_handler(char *unused);
extern void _get_weather_handler(char *unused);
extern void _set_offset_handler(char *offset);
extern void _get_temperature_handler(int argc, char **argv);
extern void _get_humidity_handler(int argc, char **argv);
extern void _get_weather_handler(int argc, char **argv);
extern void _set_offset_handler(int argc, char **argv);
#endif
#ifdef MODULE_LTC4150
extern void _get_current_handler(char *unused);
extern void _reset_current_handler(char *unused);
extern void _get_current_handler(int argc, char **argv);
extern void _reset_current_handler(int argc, char **argv);
#endif
@ -70,43 +70,43 @@ extern void _reset_current_handler(char *unused);
#endif
#else /* WITHOUT MODULE_TRANSCEIVER */
#ifdef MODULE_CC110X
extern void _cc110x_get_set_address_handler(char *addr);
extern void _cc110x_get_set_channel_handler(char *addr);
extern void _cc110x_get_set_address_handler(int argc, char **argv);
extern void _cc110x_get_set_channel_handler(int argc, char **argv);
#endif
#endif
#ifdef MODULE_TRANSCEIVER
#ifdef _TC_ADDR
extern void _transceiver_get_set_address_handler(char *addr);
extern void _transceiver_get_set_address_handler(int argc, char **argv);
#endif
#ifdef _TC_CHAN
extern void _transceiver_get_set_channel_handler(char *chan);
extern void _transceiver_get_set_channel_handler(int argc, char **argv);
#endif
#ifdef _TC_SEND
extern void _transceiver_send_handler(char *pkt);
extern void _transceiver_send_handler(int argc, char **argv);
#endif
#ifdef _TC_MON
extern void _transceiver_monitor_handler(char *mode);
extern void _transceiver_monitor_handler(int argc, char **argv);
#endif
#ifdef _TC_PAN
extern void _transceiver_get_set_pan_handler(char *chan);
extern void _transceiver_get_set_pan_handler(int argc, char **argv);
#endif
#ifdef _TC_IGN
extern void _transceiver_set_ignore_handler(char *addr);
extern void _transceiver_set_ignore_handler(int argc, char **argv);
#endif
#endif
#ifdef MODULE_MCI
extern void _get_sectorsize(char *unused);
extern void _get_blocksize(char *unused);
extern void _get_sectorcount(char *unused);
extern void _read_sector(char *sector);
extern void _read_bytes(char *bytes);
extern void _get_sectorsize(int argc, char **argv);
extern void _get_blocksize(int argc, char **argv);
extern void _get_sectorcount(int argc, char **argv);
extern void _read_sector(int argc, char **argv);
extern void _read_bytes(int argc, char **argv);
#endif
#ifdef MODULE_RANDOM
extern void _mersenne_init(char *str);
extern void _mersenne_get(char *str);
extern void _mersenne_init(int argc, char **argv);
extern void _mersenne_get(int argc, char **argv);
#endif
const shell_command_t _shell_command_list[] = {
@ -134,7 +134,6 @@ const shell_command_t _shell_command_list[] = {
{"rstcur", "Resets coulomb counter.", _reset_current_handler},
#endif
#ifdef MODULE_TRANSCEIVER
#ifdef _TC_ADDR
{"addr", "Gets or sets the address for the transceiver", _transceiver_get_set_address_handler},
@ -161,7 +160,6 @@ const shell_command_t _shell_command_list[] = {
#endif
#endif
#ifdef MODULE_MCI
{DISK_READ_SECTOR_CMD, "Reads the specified sector of inserted memory card", _read_sector},
{DISK_READ_BYTES_CMD, "Reads the specified bytes from inserted memory card", _read_bytes},
@ -173,5 +171,6 @@ const shell_command_t _shell_command_list[] = {
{ "mersenne_init", "initializes the PRNG", _mersenne_init },
{ "mersenne_get", "returns 32 bit of pseudo randomness", _mersenne_get },
#endif
{NULL, NULL, NULL}
};

View File

@ -33,7 +33,7 @@
#include "shell.h"
#include "shell_commands.h"
static void(*find_handler(const shell_command_t *command_list, char *command))(char *)
static shell_command_handler_t find_handler(const shell_command_t *command_list, char *command)
{
const shell_command_t *command_lists[] = {
command_list,
@ -90,25 +90,84 @@ static void print_help(const shell_command_t *command_list)
static void handle_input_line(shell_t *shell, char *line)
{
char *saveptr;
char *command = strtok_r(line, " ", &saveptr);
static const char *INCORRECT_QUOTING = "shell: incorrect quoting";
void (*handler)(char *) = NULL;
if (command) {
handler = find_handler(shell->command_list, command);
if (handler != NULL) {
line[strlen(command)] = ' ';
handler(line);
}
else {
if (strcmp("help", command) == 0) {
print_help(shell->command_list);
/* first we need to calculate the number of arguments */
unsigned argc = 0;
char *pos = line;
while (1) {
if (*pos > ' ') {
/* found an argument */
if (*pos == '"') {
/* it's an quoted argument */
do {
++pos;
if (!*pos) {
puts(INCORRECT_QUOTING);
return;
}
} while (*pos != '"');
if (pos[1] > ' ') {
puts(INCORRECT_QUOTING);
return;
}
}
else {
puts("shell: command not found.");
/* it's an unquoted argument */
do {
++pos;
if (*pos == '"') {
puts(INCORRECT_QUOTING);
return;
}
} while (*pos > ' ');
}
/* count the number of arguments we got */
++argc;
}
/* zero out the current position (space or quotation mark) and advance */
if (*pos > 0) {
*pos = 0;
++pos;
}
else {
break;
}
}
if (!argc) {
return;
}
/* then we fill the argv array */
char *argv[argc + 1];
argv[argc] = NULL;
pos = line;
for (unsigned i = 0; i < argc; ++i) {
while (!*pos) {
++pos;
}
if (*pos == '"') {
++pos;
}
argv[i] = pos;
while (*pos) {
++pos;
}
}
/* then we call the appropriate handler */
shell_command_handler_t handler = find_handler(shell->command_list, argv[0]);
if (handler != NULL) {
handler(argc, argv);
}
else {
if (strcmp("help", argv[0]) == 0) {
print_help(shell->command_list);
}
else {
puts("shell: command not found.");
}
}
}

View File

@ -28,34 +28,44 @@
#define SHELL_BUFSIZE (UART0_BUFSIZE)
void print_teststart(char *unused)
static void print_teststart(int argc, char **argv)
{
(void) unused;
(void) argc;
(void) argv;
printf("[TEST_START]\n");
}
void print_testend(char *unused)
static void print_testend(int argc, char **argv)
{
(void) unused;
(void) argc;
(void) argv;
printf("[TEST_END]\n");
}
int shell_readc(void)
static void print_echo(int argc, char **argv)
{
for (int i = 0; i < argc; ++i) {
printf("“%s” ", argv[i]);
}
puts("");
}
static int shell_readc(void)
{
char c = 0;
posix_read(uart0_handler_pid, &c, 1);
return c;
}
void shell_putchar(int c)
static void shell_putchar(int c)
{
putchar(c);
}
const shell_command_t shell_commands[] = {
static const shell_command_t shell_commands[] = {
{ "start_test", "starts a test", print_teststart },
{ "end_test", "ends a test", print_testend },
{ "echo", "prints the input command", print_echo },
{ NULL, NULL, NULL }
};