Improve serial port config handling and logging

This commit is contained in:
Victor Antonovich 2020-08-13 20:43:03 +04:00
parent 461ce8baa0
commit 252aa45e5d
3 changed files with 16 additions and 7 deletions

View File

@ -102,10 +102,12 @@ cfg_handle_param(char *name, char *value)
}
else if (CFG_NAME_MATCH("speed"))
{
cfg.ttyspeed = strtoul(value, NULL, 0);
if (!cfg.ttyspeed)
char *end;
cfg.ttyspeed = strtoul(value, &end, 0);
if (!cfg.ttyspeed || value == end || '\0' != *end)
{
cfg.ttyspeed = DEFAULT_SPEED;
CFG_ERR("invalid serial port speed: %s", value);
return 0;
}
}
else if (CFG_NAME_MATCH("mode"))

View File

@ -164,6 +164,7 @@ main(int argc, char *argv[])
int err = 0, rc, err_line;
char *exename;
char ttyparity;
char *end;
sig_init();
@ -261,10 +262,11 @@ main(int argc, char *argv[])
else strncpy(cfg.ttyport, optarg, INTBUFSIZE);
break;
case 's':
cfg.ttyspeed = strtoul(optarg, NULL, 0);
if (!cfg.ttyspeed)
cfg.ttyspeed = strtoul(optarg, &end, 10);
if (!cfg.ttyspeed || optarg == end || '\0' != *end)
{
cfg.ttyspeed = DEFAULT_SPEED;
printf("%s: -s: invalid serial port speed (%s)\n", exename, optarg);
exit(-1);
}
break;
case 'm':

View File

@ -114,6 +114,9 @@ tty_open(ttydata_t *mod)
errno = buferr;
return RC_ERR;
}
#endif
#ifdef LOG
logw(2, "tty: trying to open %s (speed %d mode %s)", mod->port, mod->speed, cfg.ttymode);
#endif
mod->fd = open(mod->port, O_RDWR | O_NONBLOCK | O_NOCTTY);
if (mod->fd < 0)
@ -374,7 +377,9 @@ tty_transpeed(int speed)
break;
#endif
default:
logw(2, "unsupported speed (%d)", speed);
#ifdef LOG
logw(0, "tty: unsupported speed: %d", speed);
#endif
exit (-1);
}
return tspeed;