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")) else if (CFG_NAME_MATCH("speed"))
{ {
cfg.ttyspeed = strtoul(value, NULL, 0); char *end;
if (!cfg.ttyspeed) 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")) else if (CFG_NAME_MATCH("mode"))

View File

@ -164,6 +164,7 @@ main(int argc, char *argv[])
int err = 0, rc, err_line; int err = 0, rc, err_line;
char *exename; char *exename;
char ttyparity; char ttyparity;
char *end;
sig_init(); sig_init();
@ -261,10 +262,11 @@ main(int argc, char *argv[])
else strncpy(cfg.ttyport, optarg, INTBUFSIZE); else strncpy(cfg.ttyport, optarg, INTBUFSIZE);
break; break;
case 's': case 's':
cfg.ttyspeed = strtoul(optarg, NULL, 0); cfg.ttyspeed = strtoul(optarg, &end, 10);
if (!cfg.ttyspeed) 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; break;
case 'm': case 'm':

View File

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