Commit f53806f6 authored by Guillaume Gonnet's avatar Guillaume Gonnet
Browse files

Add command parser.

parent edbcb027
#
# Cubsat command module.
#
# Copyright (C) 2019, ENSIMAG students
# This project is under the MIT license
MODULE := cubsat-app-cmds
CFLAGS += -Wno-unused-parameter
include $(RIOTBASE)/Makefile.base
/*
Base commands.
Copyright (C) 2019, ENSIMAG students
This project is under the MIT license
*/
#include "commands.h"
// Debug "d" command.
int cmds_debug(int argc, char **argv)
{
// TODO.
return APP_CMD_OK;
}
// Reset "r" command.
int cmds_reset(int argc, char **argv)
{
// TODO.
return APP_CMD_OK;
}
/*
Controll commands (received from LoRaWAN or UART RX).
Copyright (C) 2019, ENSIMAG students
This project is under the MIT license
*/
#include "commands.h"
#include <string.h>
// All command entries.
static app_cmd_ent_t cmd_entries[] = {
{ "d", cmds_debug },
{ "r", cmds_reset },
};
// Number of entries.
#define CMD_COUNT (sizeof(cmd_entries) / sizeof(*cmd_entries))
// Parse a command.
int cmds_parse(char *line)
{
// Count the number of arguments.
int argc = 1;
for (char *s = line; *s; s++) {
if (*s == ';') argc++;
}
// Split line into arguments.
char *argv[argc];
char *c = line;
for (char i = 0, *s = line; *s; s++) {
if (*s == ';') {
*s++ = '\0';
argv[(int)i++] = c;
c = s;
}
}
argv[argc - 1] = c;
// Get the command to run.
char *cmd = argv[0];
for (size_t i = 0; i < CMD_COUNT; i++) {
if (!strcmp(cmd, cmd_entries[i].name))
return cmd_entries[i].func(argc, argv);
}
// Command is not found.
return APP_CMD_NOT_FOUND;
}
/*
Controll commands (received from LoRaWAN or UART RX).
Copyright (C) 2019, ENSIMAG students
This project is under the MIT license
*/
#pragma once
/*
This module provides some commands for controlling this benchmark application.
There are two ways for sending a command: via a LoRaWAN packet on port 6 and via
Serial RX (message format is "§[msg]\n", without quotes, [msg] contains the
message).
Command messages are as following: "[cmd];[arg1];[arg2];.." (without quotes, no
new line at end), where [cmd] is the name of the command, [arg1] .. [argN] are
the command arguments.
*/
// An application command.
typedef int (*app_cmd_t)(int argc, char **argv);
// A command entry.
typedef struct {
char *name;
app_cmd_t func;
} app_cmd_ent_t;
// Standard command errors.
enum {
APP_CMD_OK = 0,
APP_CMD_NOT_FOUND = -50,
APP_CMD_INV_PARAMS = -51,
};
// Parse a command.
int cmds_parse(char *line);
// All application commands.
int cmds_debug(int argc, char **argv);
int cmds_reset(int argc, char **argv);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment