1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/sys/shell/cmds/conn_can.c
Marian Buschsieweke c06335b71b
sys/shell: make cmds submodules
Previously `shell_commands` was a "catch-all" module that included
shell commands for each and every used module that has a shell
companion. Instead, the new `shell_cmds` module is now used to provide
shell commands as individually selectable submodules, e.g.
`cmd_gnrc_icmpv6_echo` now provides the ICMPv6 echo command (a.k.a.
ping).

To still have a "catch all" module to pull in shell commands of modules
already used, `shell_cmds_default` was introduced. `shell_commands`
depends now on `shell_cmds_default` for backward compatibility, but
has been deprecated. New apps should use `shell_cmds_default`
instead.

For a handful of shell commands individual selection was already
possible. Those modules now depend on the corresponding `cmd_%` module
and they have been deprecated.
2022-09-16 13:15:45 +02:00

188 lines
4.2 KiB
C

/*
* Copyright 2017 OTA keys S.A.
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup sys_shell_commands
* @{
*
* @file
* @brief Shell command implementation for CAN stack
*
* @author Vincent Dupont <vincent@otakeys.com>
*
* @}
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include "can/conn/raw.h"
#include "can/raw.h"
#include "shell.h"
#include "timex.h"
#define SC_CAN_MAX_FILTERS 10
#define xstr(a) str(a)
#define str(a) #a
static int _can_usage(void);
static int _list(int argc, char **argv)
{
(void)argc;
(void)argv;
for (int i = 0; i < CAN_DLL_NUMOF; i++) {
const char *name = raw_can_get_name_by_ifnum(i);
if (name) {
printf("CAN #%d: %s\n", i, name);
}
else {
break;
}
}
return 0;
}
static int _send(int argc, char **argv)
{
if (argc < 3 || argc > 11) {
_can_usage();
return 1;
}
conn_can_raw_t conn;
struct can_frame frame;
int ifnum = atoi(argv[1]);
if (ifnum >= CAN_DLL_NUMOF) {
puts("Invalid ifnum");
return 1;
}
frame.can_id = strtoul(argv[2], NULL, 16);
frame.can_dlc = argc - 3;
for (int i = 0; i < frame.can_dlc; i++) {
frame.data[i] = strtoul(argv[3 + i], NULL, 16);
}
conn_can_raw_create(&conn, NULL, 0, ifnum, 0);
conn_can_raw_send(&conn, &frame, 0);
return 0;
}
static int _dump(int argc, char **argv)
{
if (argc < 4) {
_can_usage();
return 0;
}
int ret = -1;
struct can_filter filters[SC_CAN_MAX_FILTERS];
conn_can_raw_t conn;
struct can_frame frame;
int ifnum = atoi(argv[1]);
int cnt = atoi(argv[2]);
uint32_t ms = strtoul(argv[3], NULL, 0);
size_t nb_filters = 1;
if (ifnum >= CAN_DLL_NUMOF) {
puts("Invalid ifnum");
return 1;
}
if (cnt) {
cnt++;
}
if (argc > 4) {
char *p = argv[4];
char *end;
int i = 0;
while (*p != '\0' && i < SC_CAN_MAX_FILTERS) {
filters[i].can_id = strtoul(p, &end, 16);
if (*end == ':') {
p = end + 1;
filters[i].can_mask = strtoul(p, &end, 16);
}
else {
filters[i].can_mask = 0xffffffff;
}
if (*end == ',') {
p = end + 1;
}
else {
p = end;
}
i++;
}
nb_filters = i;
}
else {
filters[0].can_id = 0;
filters[0].can_mask = 0;
}
conn_can_raw_create(&conn, filters, nb_filters, ifnum, 0);
while ((cnt != 1) &&
((ret = conn_can_raw_recv(&conn, &frame, ms * US_PER_MS))
== sizeof(struct can_frame))) {
printf("%-8s(%d) %8" PRIX32 " [%x] ",
raw_can_get_name_by_ifnum(ifnum), ifnum,
frame.can_id, frame.can_dlc);
for (int i = 0; i < frame.can_dlc; i++) {
printf(" %02X", frame.data[i]);
}
printf("\n");
if (cnt) {
cnt--;
}
}
if (ret < 0) {
puts("Timeout");
}
puts("Closing");
conn_can_raw_close(&conn);
return 0;
}
static int _can_usage(void)
{
puts("usage: can <command> [arguments]");
puts("commands:");
puts("\tlist");
puts("\tsend ifnum id [B1 .. B8]");
puts("\tdump ifnum nb ms [id1[:mask1][,id2[:mask2], .. id"
xstr(SC_CAN_MAX_FILTERS) ":[mask" xstr(SC_CAN_MAX_FILTERS) "]]");
return 0;
}
static int _can_handler(int argc, char **argv)
{
if (argc < 2) {
_can_usage();
return 1;
}
else if (strncmp(argv[1], "list", 5) == 0) {
return _list(argc - 1, argv + 1);
}
else if (strncmp(argv[1], "send", 5) == 0) {
return _send(argc - 1, argv + 1);
}
else if (strncmp(argv[1], "dump", 5) == 0) {
return _dump(argc - 1, argv + 1);
}
else {
printf("unknown command: %s\n", argv[1]);
return 1;
}
return 0;
}
SHELL_COMMAND(can, "CAN commands", _can_handler);