Skip to content

Commit 252aa45

Browse files
committed
Improve serial port config handling and logging
1 parent 461ce8b commit 252aa45

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

src/cfg.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,12 @@ cfg_handle_param(char *name, char *value)
102102
}
103103
else if (CFG_NAME_MATCH("speed"))
104104
{
105-
cfg.ttyspeed = strtoul(value, NULL, 0);
106-
if (!cfg.ttyspeed)
105+
char *end;
106+
cfg.ttyspeed = strtoul(value, &end, 0);
107+
if (!cfg.ttyspeed || value == end || '\0' != *end)
107108
{
108-
cfg.ttyspeed = DEFAULT_SPEED;
109+
CFG_ERR("invalid serial port speed: %s", value);
110+
return 0;
109111
}
110112
}
111113
else if (CFG_NAME_MATCH("mode"))

src/main.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ main(int argc, char *argv[])
164164
int err = 0, rc, err_line;
165165
char *exename;
166166
char ttyparity;
167+
char *end;
167168

168169
sig_init();
169170

@@ -261,10 +262,11 @@ main(int argc, char *argv[])
261262
else strncpy(cfg.ttyport, optarg, INTBUFSIZE);
262263
break;
263264
case 's':
264-
cfg.ttyspeed = strtoul(optarg, NULL, 0);
265-
if (!cfg.ttyspeed)
265+
cfg.ttyspeed = strtoul(optarg, &end, 10);
266+
if (!cfg.ttyspeed || optarg == end || '\0' != *end)
266267
{
267-
cfg.ttyspeed = DEFAULT_SPEED;
268+
printf("%s: -s: invalid serial port speed (%s)\n", exename, optarg);
269+
exit(-1);
268270
}
269271
break;
270272
case 'm':

src/tty.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ tty_open(ttydata_t *mod)
114114
errno = buferr;
115115
return RC_ERR;
116116
}
117+
#endif
118+
#ifdef LOG
119+
logw(2, "tty: trying to open %s (speed %d mode %s)", mod->port, mod->speed, cfg.ttymode);
117120
#endif
118121
mod->fd = open(mod->port, O_RDWR | O_NONBLOCK | O_NOCTTY);
119122
if (mod->fd < 0)
@@ -374,7 +377,9 @@ tty_transpeed(int speed)
374377
break;
375378
#endif
376379
default:
377-
logw(2, "unsupported speed (%d)", speed);
380+
#ifdef LOG
381+
logw(0, "tty: unsupported speed: %d", speed);
382+
#endif
378383
exit (-1);
379384
}
380385
return tspeed;

0 commit comments

Comments
 (0)