netbird: shield: fix error handling and information output
This commit is contained in:
parent
d7677ffa84
commit
9f5e4cd0e9
|
|
@ -528,6 +528,7 @@ struct shield_command {
|
||||||
const char *name;
|
const char *name;
|
||||||
const char *default_shieldcmd;
|
const char *default_shieldcmd;
|
||||||
const char *fdtshieldcmd;
|
const char *fdtshieldcmd;
|
||||||
|
void (*init)(void);
|
||||||
};
|
};
|
||||||
|
|
||||||
#define SHIELD_COM_IO 0
|
#define SHIELD_COM_IO 0
|
||||||
|
|
@ -539,7 +540,8 @@ static struct shield_command known_shield_commands[] = {
|
||||||
"comio",
|
"comio",
|
||||||
"shield comio mode rs232",
|
"shield comio mode rs232",
|
||||||
"fdt get value serial0 /aliases serial0;" \
|
"fdt get value serial0 /aliases serial0;" \
|
||||||
"fdt set $serial0 status okay"
|
"fdt set $serial0 status okay",
|
||||||
|
comio_shield_init
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
SHIELD_DUALCAN,
|
SHIELD_DUALCAN,
|
||||||
|
|
@ -548,7 +550,8 @@ static struct shield_command known_shield_commands[] = {
|
||||||
"fdt get value can0 /aliases d_can0;" \
|
"fdt get value can0 /aliases d_can0;" \
|
||||||
"fdt get value can1 /aliases d_can1;" \
|
"fdt get value can1 /aliases d_can1;" \
|
||||||
"fdt set $can0 status okay;" \
|
"fdt set $can0 status okay;" \
|
||||||
"fdt set $can1 status okay;" \
|
"fdt set $can1 status okay;",
|
||||||
|
can_shield_init
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -585,8 +588,9 @@ static void shield_config(void)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Shield found: %s\n", cmd->name);
|
printf("Shield: %s\n", cmd->name);
|
||||||
|
|
||||||
|
cmd->init();
|
||||||
shieldcmd = cmd->default_shieldcmd;
|
shieldcmd = cmd->default_shieldcmd;
|
||||||
|
|
||||||
/* If a shield configuration set by linux take it without bd check, we asume that Linux knows
|
/* If a shield configuration set by linux take it without bd check, we asume that Linux knows
|
||||||
|
|
@ -597,8 +601,6 @@ static void shield_config(void)
|
||||||
shieldcmd = shieldcmd_linux;
|
shieldcmd = shieldcmd_linux;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Shield command: %s\n", shieldcmd);
|
|
||||||
|
|
||||||
setenv("shieldcmd", shieldcmd);
|
setenv("shieldcmd", shieldcmd);
|
||||||
|
|
||||||
set_fdtshieldcmd(cmd->fdtshieldcmd);
|
set_fdtshieldcmd(cmd->fdtshieldcmd);
|
||||||
|
|
@ -606,9 +608,6 @@ static void shield_config(void)
|
||||||
|
|
||||||
static void shield_init(void)
|
static void shield_init(void)
|
||||||
{
|
{
|
||||||
can_shield_init();
|
|
||||||
comio_shield_init();
|
|
||||||
|
|
||||||
shield_config();
|
shield_config();
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -50,13 +50,14 @@ int shield_set_mode(const char* shield_type, int argc, char * const argv[])
|
||||||
return shields[i]->setmode(argv, argc);
|
return shields[i]->setmode(argv, argc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
printf("Shield %s is unknown\n", shield_type);
|
printf("## Error: No %s shield installed\n", shield_type);
|
||||||
return -1;
|
/* Do not return error, to not show usage (request by rs) */
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int do_shieldmode(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
|
static int do_shieldmode(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
|
||||||
{
|
{
|
||||||
if (argc < 3) {
|
if (argc < 2) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -140,14 +140,32 @@ static int configure_shieldmode(int mode)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int get_rs232(const char *mode)
|
enum mode_nr {
|
||||||
|
RS232,
|
||||||
|
RS485,
|
||||||
|
UNKNOWN
|
||||||
|
};
|
||||||
|
|
||||||
|
struct mode {
|
||||||
|
enum mode_nr nr;
|
||||||
|
const char* name;
|
||||||
|
int argc;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct mode modes[] = {
|
||||||
|
{RS232, "rs232", 0},
|
||||||
|
{RS485, "rs485", 2}
|
||||||
|
};
|
||||||
|
|
||||||
|
static const struct mode *get_mode(const char *mode)
|
||||||
{
|
{
|
||||||
if (strcmp("rs232", mode) == 0) {
|
int i;
|
||||||
return 1;
|
for (i = 0; i < ARRAY_SIZE(modes); i++) {
|
||||||
}
|
if (strcmp(modes[i].name, mode) == 0) {
|
||||||
else {
|
return &modes[i];
|
||||||
return 0;
|
}
|
||||||
}
|
}
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int get_termination(const char* termination)
|
static int get_termination(const char* termination)
|
||||||
|
|
@ -167,6 +185,7 @@ static int get_mode_from_args(char * const argv[], int argc)
|
||||||
{
|
{
|
||||||
int termination = 0;
|
int termination = 0;
|
||||||
int rs232 = 0;
|
int rs232 = 0;
|
||||||
|
const struct mode *selected_mode;
|
||||||
|
|
||||||
assert(argc >= 2);
|
assert(argc >= 2);
|
||||||
|
|
||||||
|
|
@ -175,18 +194,31 @@ static int get_mode_from_args(char * const argv[], int argc)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
rs232 = get_rs232(argv[1]);
|
selected_mode = get_mode(argv[1]);
|
||||||
|
if (selected_mode == NULL) {
|
||||||
|
debug("Mode %s not supported\n", argv[1]);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
if (argc > 2) {
|
debug ("Mode %s, index %d, argc %d\n", selected_mode->name,
|
||||||
if (rs232 || strcmp("termination", argv[2])) {
|
selected_mode->nr, selected_mode->argc);
|
||||||
|
|
||||||
|
if (selected_mode->argc != argc - 2) {
|
||||||
|
debug("Invalid argument count for mode %s (should %d is %d)\n",
|
||||||
|
argv[1], selected_mode->argc, argc - 2);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (selected_mode->nr == RS485) {
|
||||||
|
if (strcmp("termination", argv[2])) {
|
||||||
debug("Invalid arguments, do not configure termination\n");
|
debug("Invalid arguments, do not configure termination\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
termination = get_termination(argv[3]);
|
termination = get_termination(argv[3]);
|
||||||
if (termination < 0) {
|
if (termination < 0) {
|
||||||
return -1;
|
debug("Invalid termination %s\n", argv[3]);
|
||||||
}
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue