1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
/*
* Support for tty settings using termios
*
* (c) 2016 Steve Bennett <steveb@workware.net.au>
*
*/
/* termios support is required */
#include <jim-tty.h>
#include <termios.h>
static const struct {
unsigned baud;
speed_t speed;
} baudtable[] = {
{ 0, B0 },
{ 50, B50 },
{ 75, B75 },
{ 110, B110 },
{ 134, B134 },
{ 150, B150 },
{ 200, B200 },
{ 300, B300 },
{ 600, B600 },
{ 1200, B1200 },
{ 1800, B1800 },
{ 2400, B2400 },
{ 4800, B4800 },
{ 9600, B9600 },
{ 19200, B19200 },
{ 38400, B38400 },
{ 57600, B57600 },
{ 115200, B115200 },
#ifdef B230400
{ 230400, B230400 },
#endif
#ifdef B460800
{ 460800, B460800 }
#endif
};
struct flag_name_map {
const char *name;
unsigned value;
};
static const struct flag_name_map parity_map[] = {
{ "none", 0 },
{ "even", PARENB },
{ "odd", PARENB | PARODD },
};
static const struct flag_name_map data_size_map[] = {
{ "5", CS5 },
{ "6", CS6 },
{ "7", CS7 },
{ "8", CS8 },
};
static const struct flag_name_map stop_size_map[] = {
{ "1", 0 },
{ "2", CSTOPB },
};
static const struct flag_name_map input_map[] = {
{ "raw", 0 },
{ "cooked", ICANON },
};
static const struct flag_name_map output_map[] = {
{ "raw", 0 },
{ "cooked", OPOST },
};
static const char * const tty_settings_names[] = {
"baud",
"data",
"handshake",
"input",
"output",
"parity",
"stop",
"vmin",
"vtime",
"echo",
NULL
};
enum {
OPT_BAUD,
OPT_DATA,
OPT_HANDSHAKE,
OPT_INPUT,
OPT_OUTPUT,
OPT_PARITY,
OPT_STOP,
OPT_VMIN,
OPT_VTIME,
OPT_ECHO
};
#define ARRAYSIZE(A) (sizeof(A)/sizeof(*(A)))
/**
* Search the flag/name map for an entry with the given name.
* (Actually, just matching the first char)
* Returns a pointer to the entry if found, or NULL if not.
*/
static const struct flag_name_map *flag_name_to_value(const struct flag_name_map *map, int len, const char *name)
{
int i;
for (i = 0; i < len; i++) {
/* Only need to compare the first character since all names are unique in the first char */
if (*name == *map[i].name) {
return &map[i];
}
}
return NULL;
}
/**
* Search the flag/name map for an entry with the matching value.
* Returns the corresponding name if found, or NULL if no match.
*/
static const char *flag_value_to_name(const struct flag_name_map *map, int len, unsigned value)
{
int i;
for (i = 0; i < len; i++) {
if (value == map[i].value) {
return map[i].name;
}
}
return NULL;
}
/**
* If 'str2' is not NULL, appends 'str1' and 'str2' to the list.
* Otherwise does nothing.
*/
static void JimListAddPair(Jim_Interp *interp, Jim_Obj *listObjPtr, const char *str1, const char *str2)
{
if (str2) {
Jim_ListAppendElement(interp, listObjPtr, Jim_NewStringObj(interp, str1, -1));
Jim_ListAppendElement(interp, listObjPtr, Jim_NewStringObj(interp, str2, -1));
}
}
Jim_Obj *Jim_GetTtySettings(Jim_Interp *interp, int fd)
{
struct termios tio;
size_t i;
const char *p;
Jim_Obj *listObjPtr;
speed_t speed;
int baud;
if (tcgetattr(fd, &tio) < 0) {
return NULL;
}
listObjPtr = Jim_NewListObj(interp, NULL, 0);
p = flag_value_to_name(parity_map, ARRAYSIZE(parity_map), tio.c_cflag & (PARENB | PARODD));
JimListAddPair(interp, listObjPtr, "parity", p);
p = flag_value_to_name(data_size_map, ARRAYSIZE(data_size_map), tio.c_cflag & CSIZE);
JimListAddPair(interp, listObjPtr, "data", p);
p = flag_value_to_name(stop_size_map, ARRAYSIZE(stop_size_map), tio.c_cflag & CSTOPB);
JimListAddPair(interp, listObjPtr, "stop", p);
if (tio.c_iflag & (IXON | IXOFF)) {
p = "xonxoff";
}
else if (tio.c_cflag & CRTSCTS) {
p = "rtscts";
}
else {
p = "none";
}
JimListAddPair(interp, listObjPtr, "handshake", p);
p = flag_value_to_name(input_map, ARRAYSIZE(input_map), tio.c_lflag & ICANON);
JimListAddPair(interp, listObjPtr, "input", p);
p = flag_value_to_name(output_map, ARRAYSIZE(output_map), tio.c_oflag & OPOST);
JimListAddPair(interp, listObjPtr, "output", p);
Jim_ListAppendElement(interp, listObjPtr, Jim_NewStringObj(interp, "vmin", -1));
Jim_ListAppendElement(interp, listObjPtr, Jim_NewIntObj(interp, tio.c_cc[VMIN]));
Jim_ListAppendElement(interp, listObjPtr, Jim_NewStringObj(interp, "vtime", -1));
Jim_ListAppendElement(interp, listObjPtr, Jim_NewIntObj(interp, tio.c_cc[VTIME]));
speed = cfgetispeed(&tio);
baud = 0;
for (i = 0; i < sizeof(baudtable) / sizeof(*baudtable); i++) {
if (baudtable[i].speed == speed) {
baud = baudtable[i].baud;
break;
}
}
Jim_ListAppendElement(interp, listObjPtr, Jim_NewStringObj(interp, "baud", -1));
Jim_ListAppendElement(interp, listObjPtr, Jim_NewIntObj(interp, baud));
return listObjPtr;
}
int Jim_SetTtySettings(Jim_Interp *interp, int fd, Jim_Obj *dictObjPtr)
{
int len = Jim_ListLength(interp, dictObjPtr);
int i;
struct termios tio;
if (tcgetattr(fd, &tio) < 0) {
return -1;
}
for (i = 0; i < len; i += 2) {
Jim_Obj *nameObj = Jim_ListGetIndex(interp, dictObjPtr, i);
Jim_Obj *valueObj = Jim_ListGetIndex(interp, dictObjPtr, i + 1);
int opt;
const struct flag_name_map *p;
long l;
size_t j;
if (Jim_GetEnum(interp, nameObj, tty_settings_names, &opt, "setting", JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK) {
return JIM_ERR;
}
switch (opt) {
case OPT_BAUD:
if (Jim_GetLong(interp, valueObj, &l) != JIM_OK) {
goto badvalue;
}
for (j = 0; j < ARRAYSIZE(baudtable); j++) {
if (baudtable[j].baud == l) {
break;
}
}
if (j == ARRAYSIZE(baudtable)) {
goto badvalue;
}
cfsetospeed(&tio, baudtable[j].speed);
cfsetispeed(&tio, baudtable[j].speed);
break;
case OPT_PARITY:
p = flag_name_to_value(parity_map, ARRAYSIZE(parity_map), Jim_String(valueObj));
if (p == NULL) {
badvalue:
Jim_SetResultFormatted(interp, "bad value for %#s: %#s", nameObj, valueObj);
return JIM_ERR;
}
tio.c_cflag &= ~(PARENB | PARODD);
tio.c_cflag |= p->value;
break;
case OPT_STOP:
p = flag_name_to_value(stop_size_map, ARRAYSIZE(stop_size_map), Jim_String(valueObj));
if (p == NULL) {
goto badvalue;
}
tio.c_cflag &= ~CSTOPB;
tio.c_cflag |= p->value;
break;
case OPT_DATA:
p = flag_name_to_value(data_size_map, ARRAYSIZE(data_size_map), Jim_String(valueObj));
if (p == NULL) {
goto badvalue;
}
tio.c_cflag &= ~CSIZE;
tio.c_cflag |= p->value;
break;
case OPT_HANDSHAKE:
tio.c_iflag &= ~(IXON | IXOFF);
tio.c_cflag &= ~(CRTSCTS);
if (Jim_CompareStringImmediate(interp, valueObj, "xonxoff")) {
tio.c_iflag |= (IXON | IXOFF);
}
else if (Jim_CompareStringImmediate(interp, valueObj, "rtscts")) {
tio.c_cflag |= CRTSCTS;
}
else if (!Jim_CompareStringImmediate(interp, valueObj, "none")) {
goto badvalue;
}
break;
case OPT_VMIN:
if (Jim_GetLong(interp, valueObj, &l) != JIM_OK) {
goto badvalue;
}
tio.c_cc[VMIN] = l;
break;
case OPT_VTIME:
if (Jim_GetLong(interp, valueObj, &l) != JIM_OK) {
goto badvalue;
}
tio.c_cc[VTIME] = l;
break;
case OPT_OUTPUT:
p = flag_name_to_value(output_map, ARRAYSIZE(output_map), Jim_String(valueObj));
if (p == NULL) {
goto badvalue;
}
tio.c_oflag &= ~OPOST;
tio.c_oflag |= p->value;
break;
case OPT_INPUT:
p = flag_name_to_value(input_map, ARRAYSIZE(input_map), Jim_String(valueObj));
if (p == NULL) {
goto badvalue;
}
if (p->value) {
tio.c_lflag |= (ECHO | ECHOE | ECHOK | ECHONL | ICANON | IEXTEN | ISIG | NOFLSH | TOSTOP);
tio.c_iflag |= ICRNL;
}
else {
tio.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL | ICANON | IEXTEN | ISIG | NOFLSH | TOSTOP);
tio.c_iflag &= ~ICRNL;
}
break;
case OPT_ECHO:
if (Jim_GetLong(interp, valueObj, &l) != JIM_OK) {
goto badvalue;
}
if (l) {
tio.c_lflag |= ECHO;
}
else {
tio.c_lflag &= ~ECHO;
}
break;
}
}
if (tcsetattr(fd, TCSAFLUSH, &tio) < 0) {
return -1;
}
return 0;
}
|