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 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
|
/*
* BRLTTY - A background process providing access to the console screen (when in
* text mode) for a blind person using a refreshable braille display.
*
* Copyright (C) 1995-2025 by The BRLTTY Developers.
*
* BRLTTY comes with ABSOLUTELY NO WARRANTY.
*
* This is free software, placed under the terms of the
* GNU Lesser General Public License, as published by the Free Software
* Foundation; either version 2.1 of the License, or (at your option) any
* later version. Please see the file LICENSE-LGPL for details.
*
* Web Page: http://brltty.app/
*
* This software is maintained by Dave Mielke <dave@mielke.cc>.
*/
#include "prologue.h"
#include <string.h>
#include <errno.h>
#include "log.h"
#include "io_log.h"
#include "strfmt.h"
#include "parameters.h"
#include "parse.h"
#include "device.h"
#include "async_wait.h"
#if defined(USE_PKG_SERIAL_NONE)
#include "serial_none.h"
#elif defined(USE_PKG_SERIAL_GRUB)
#include "serial_grub.h"
#elif defined(USE_PKG_SERIAL_MSDOS)
#include "serial_msdos.h"
#elif defined(USE_PKG_SERIAL_TERMIOS)
#include "serial_termios.h"
#elif defined(USE_PKG_SERIAL_WINDOWS)
#include "serial_windows.h"
#else /* serial package */
#error serial I/O package not selected
#include "serial_none.h"
#endif /* serial package */
#include "serial_internal.h"
const char *
serialGetDevicePath (SerialDevice *serial) {
return serial->devicePath;
}
const SerialBaudEntry *
serialGetBaudEntry (unsigned int baud) {
const SerialBaudEntry *entry = serialBaudTable;
while (entry->baud) {
if (baud == entry->baud) return entry;
entry += 1;
}
logMessage(LOG_WARNING, "undefined serial baud: %u", baud);
return NULL;
}
static void
serialInitializeAttributes (SerialAttributes *attributes) {
memset(attributes, 0, sizeof(*attributes));
serialPutInitialAttributes(attributes);
{
const SerialBaudEntry *entry = serialGetBaudEntry(SERIAL_DEFAULT_BAUD);
if (entry) {
if (!serialPutSpeed(attributes, entry->speed)) {
logMessage(LOG_WARNING, "default serial baud not supported: %u", SERIAL_DEFAULT_BAUD);
}
}
}
if (!serialPutDataBits(attributes, SERIAL_DEFAULT_DATA_BITS)) {
logMessage(LOG_WARNING, "default serial data bits not supported: %u", SERIAL_DEFAULT_DATA_BITS);
}
if (!serialPutStopBits(attributes, SERIAL_DEFAULT_STOP_BITS)) {
logMessage(LOG_WARNING, "default serial stop bits not supported: %u", SERIAL_DEFAULT_STOP_BITS);
}
if (!serialPutParity(attributes, SERIAL_DEFAULT_PARITY)) {
logMessage(LOG_WARNING, "default serial parity not supported: %u", SERIAL_DEFAULT_PARITY);
}
if (serialPutFlowControl(attributes, SERIAL_DEFAULT_FLOW_CONTROL)) {
logMessage(LOG_WARNING, "default serial flow control not supported: 0X%04X", SERIAL_DEFAULT_FLOW_CONTROL);
}
{
int state = 0;
if (!serialPutModemState(attributes, state)) {
logMessage(LOG_WARNING, "default serial modem state not supported: %d", state);
}
}
}
int
serialSetBaud (SerialDevice *serial, unsigned int baud) {
const SerialBaudEntry *entry = serialGetBaudEntry(baud);
if (entry) {
logMessage(LOG_CATEGORY(SERIAL_IO), "set baud: %u", baud);
if (serialPutSpeed(&serial->pendingAttributes, entry->speed)) return 1;
logUnsupportedBaud(baud);
}
return 0;
}
int
serialValidateBaud (unsigned int *baud, const char *description, const char *word, const unsigned int *choices) {
if (!*word || isUnsignedInteger(baud, word)) {
const SerialBaudEntry *entry = serialGetBaudEntry(*baud);
if (entry) {
if (!choices) return 1;
while (*choices) {
if (*baud == *choices) return 1;
choices += 1;
}
logMessage(LOG_ERR, "unsupported %s: %u", description, *baud);
} else {
logMessage(LOG_ERR, "undefined %s: %u", description, *baud);
}
} else {
logMessage(LOG_ERR, "invalid %s: %u", description, *baud);
}
return 0;
}
int
serialSetDataBits (SerialDevice *serial, unsigned int bits) {
logMessage(LOG_CATEGORY(SERIAL_IO), "set data bits: %u", bits);
if (serialPutDataBits(&serial->pendingAttributes, bits)) return 1;
logUnsupportedDataBits(bits);
return 0;
}
int
serialSetStopBits (SerialDevice *serial, SerialStopBits bits) {
logMessage(LOG_CATEGORY(SERIAL_IO), "set stop bits: %u", bits);
if (serialPutStopBits(&serial->pendingAttributes, bits)) return 1;
logUnsupportedStopBits(bits);
return 0;
}
int
serialSetParity (SerialDevice *serial, SerialParity parity) {
logMessage(LOG_CATEGORY(SERIAL_IO), "set parity: %u", parity);
if (serialPutParity(&serial->pendingAttributes, parity)) return 1;
logUnsupportedParity(parity);
return 0;
}
#ifdef HAVE_POSIX_THREADS
static void
serialFlowControlProc_inputCTS (SerialDevice *serial) {
int up = serialTestLineCTS(serial);
while (!serial->flowControlStop) {
serialSetLineRTS(serial, up);
serialWaitLineCTS(serial, (up = !up), 0);
}
}
THREAD_FUNCTION(serialFlowControlThread) {
SerialDevice *serial = argument;
serial->currentFlowControlProc(serial);
return NULL;
}
static int
serialStartFlowControlThread (SerialDevice *serial) {
if (!serial->flowControlRunning && serial->currentFlowControlProc) {
pthread_t thread;
pthread_attr_t attributes;
pthread_attr_init(&attributes);
pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_DETACHED);
serial->flowControlStop = 0;
if (createThread("serial-input-cts", &thread, &attributes,
serialFlowControlThread, serial)) {
logSystemError("pthread_create");
return 0;
}
serial->flowControlThread = thread;
serial->flowControlRunning = 1;
}
return 1;
}
static void
serialStopFlowControlThread (SerialDevice *serial) {
if (serial->flowControlRunning) {
serial->flowControlStop = 1;
serial->flowControlRunning = 0;
}
}
#endif /* HAVE_POSIX_THREADS */
int
serialSetFlowControl (SerialDevice *serial, SerialFlowControl flow) {
logMessage(LOG_CATEGORY(SERIAL_IO), "set flow control: 0X%02X", flow);
flow = serialPutFlowControl(&serial->pendingAttributes, flow);
#ifdef HAVE_POSIX_THREADS
if (flow & SERIAL_FLOW_INPUT_CTS) {
flow &= ~SERIAL_FLOW_INPUT_CTS;
serial->pendingFlowControlProc = serialFlowControlProc_inputCTS;
} else {
serial->pendingFlowControlProc = NULL;
}
{
int state = !!serial->pendingFlowControlProc;
if (!serialPutModemState(&serial->pendingAttributes, state)) {
logMessage(LOG_WARNING, "unsupported serial modem state: %d", state);
}
}
#endif /* HAVE_POSIX_THREADS */
if (!flow) return 1;
logUnsupportedFlowControl(flow);
return 0;
}
unsigned int
serialGetCharacterSize (const SerialParameters *parameters) {
unsigned int size = 1 /* start bit */ + parameters->dataBits;
size += (parameters->stopBits == SERIAL_STOP_1)? 1: 2;
if (parameters->parity != SERIAL_PARITY_NONE) size += 1;
return size;
}
unsigned int
serialGetCharacterBits (SerialDevice *serial) {
const SerialAttributes *attributes = &serial->pendingAttributes;
return 1 /* start bit */
+ serialGetDataBits(attributes)
+ serialGetParityBits(attributes)
+ serialGetStopBits(attributes)
;
}
int
serialDiscardInput (SerialDevice *serial) {
logMessage(LOG_CATEGORY(SERIAL_IO), "discard input");
return serialCancelInput(serial);
}
int
serialDiscardOutput (SerialDevice *serial) {
logMessage(LOG_CATEGORY(SERIAL_IO), "discard output");
return serialCancelOutput(serial);
}
int
serialFlushOutput (SerialDevice *serial) {
logMessage(LOG_CATEGORY(SERIAL_IO), "flush output");
if (serial->stream) {
if (fflush(serial->stream) == EOF) {
logSystemError("fflush");
return 0;
}
}
return 1;
}
int
serialAwaitOutput (SerialDevice *serial) {
if (!serialFlushOutput(serial)) return 0;
if (!serialDrainOutput(serial)) return 0;
return 1;
}
static void
serialCopyAttributes (SerialAttributes *destination, const SerialAttributes *source) {
memcpy(destination, source, sizeof(*destination));
}
static int
serialCompareAttributes (const SerialAttributes *attributes, const SerialAttributes *reference) {
return memcmp(attributes, reference, sizeof(*attributes)) == 0;
}
static int
serialReadAttributes (SerialDevice *serial) {
return serialGetAttributes(serial, &serial->currentAttributes);
}
static int
serialWriteAttributes (SerialDevice *serial, const SerialAttributes *attributes) {
if (!serialCompareAttributes(attributes, &serial->currentAttributes)) {
if (!serialAwaitOutput(serial)) return 0;
logBytes(LOG_CATEGORY(SERIAL_IO), "attributes", attributes, sizeof(*attributes));
if (!serialPutAttributes(serial, attributes)) return 0;
serialCopyAttributes(&serial->currentAttributes, attributes);
}
return 1;
}
static int
serialFlushAttributes (SerialDevice *serial) {
#ifdef HAVE_POSIX_THREADS
int restartFlowControlThread = serial->pendingFlowControlProc != serial->currentFlowControlProc;
if (restartFlowControlThread) serialStopFlowControlThread(serial);
#endif /* HAVE_POSIX_THREADS */
if (!serialWriteAttributes(serial, &serial->pendingAttributes)) return 0;
#ifdef HAVE_POSIX_THREADS
if (restartFlowControlThread) {
serial->currentFlowControlProc = serial->pendingFlowControlProc;
if (!serialStartFlowControlThread(serial)) return 0;
}
#endif /* HAVE_POSIX_THREADS */
return 1;
}
int
serialSetParameters (SerialDevice *serial, const SerialParameters *parameters) {
if (!serialSetBaud(serial, parameters->baud)) return 0;
if (!serialSetDataBits(serial, parameters->dataBits)) return 0;
if (!serialSetStopBits(serial, parameters->stopBits)) return 0;
if (!serialSetParity(serial, parameters->parity)) return 0;
if (!serialSetFlowControl(serial, parameters->flowControl)) return 0;
return serialFlushAttributes(serial);
}
int
serialAwaitInput (SerialDevice *serial, int timeout) {
if (!serialFlushAttributes(serial)) return 0;
if (!serialPollInput(serial, timeout)) return 0;
return 1;
}
ssize_t
serialReadData (
SerialDevice *serial,
void *buffer, size_t size,
int initialTimeout, int subsequentTimeout
) {
if (!serialFlushAttributes(serial)) return -1;
{
ssize_t result = serialGetData(serial, buffer, size, initialTimeout, subsequentTimeout);
if (result > 0) {
logBytes(LOG_CATEGORY(SERIAL_IO), "input", buffer, result);
}
return result;
}
}
int
serialReadChunk (
SerialDevice *serial,
void *buffer, size_t *offset, size_t count,
int initialTimeout, int subsequentTimeout
) {
unsigned char *byte = buffer;
const unsigned char *const first = byte;
const unsigned char *const end = first + count;
int timeout = *offset? subsequentTimeout: initialTimeout;
if (!serialFlushAttributes(serial)) return 0;
byte += *offset;
while (byte < end) {
ssize_t result = serialGetData(serial, byte, 1, timeout, subsequentTimeout);
if (!result) {
result = -1;
errno = EAGAIN;
}
if (result == -1) {
if (errno == EINTR) continue;
return 0;
}
byte += 1;
*offset += 1;
timeout = subsequentTimeout;
}
if (byte > first) {
logBytes(LOG_CATEGORY(SERIAL_IO), "input", first, (byte - first));
}
return 1;
}
ssize_t
serialWriteData (
SerialDevice *serial,
const void *data, size_t size
) {
if (!serialFlushAttributes(serial)) return -1;
if (size > 0) logBytes(LOG_CATEGORY(SERIAL_IO), "output", data, size);
return serialPutData(serial, data, size);
}
static int
serialReadLines (SerialDevice *serial, SerialLines *lines) {
int result = serialGetLines(serial);
if (result) *lines = serial->linesState;
return result;
}
static int
serialWriteLines (SerialDevice *serial, SerialLines high, SerialLines low) {
return serialPutLines(serial, high, low);
}
static int
serialSetLine (SerialDevice *serial, SerialLines line, int up) {
return serialWriteLines(serial, up?line:0, up?0:line);
}
int
serialSetLineRTS (SerialDevice *serial, int up) {
return serialSetLine(serial, SERIAL_LINE_RTS, up);
}
int
serialSetLineDTR (SerialDevice *serial, int up) {
return serialSetLine(serial, SERIAL_LINE_DTR, up);
}
static int
serialTestLines (SerialDevice *serial, SerialLines high, SerialLines low) {
SerialLines lines;
if (serialReadLines(serial, &lines))
if (((lines & high) == high) && ((~lines & low) == low))
return 1;
return 0;
}
int
serialTestLineCTS (SerialDevice *serial) {
return serialTestLines(serial, SERIAL_LINE_CTS, 0);
}
int
serialTestLineDSR (SerialDevice *serial) {
return serialTestLines(serial, SERIAL_LINE_DSR, 0);
}
static int
serialDefineWaitLines (SerialDevice *serial, SerialLines lines) {
if (lines != serial->waitLines) {
if (!serialRegisterWaitLines(serial, lines)) return 0;
serial->waitLines = lines;
}
return 1;
}
static int
serialAwaitLineChange (SerialDevice *serial) {
return serialMonitorWaitLines(serial);
}
static int
serialWaitLines (SerialDevice *serial, SerialLines high, SerialLines low) {
SerialLines lines = high | low;
int ok = 0;
if (serialDefineWaitLines(serial, lines)) {
while (!serialTestLines(serial, high, low))
if (!serialAwaitLineChange(serial))
goto done;
ok = 1;
}
done:
serialDefineWaitLines(serial, 0);
return ok;
}
static int
serialWaitFlank (SerialDevice *serial, SerialLines line, int up) {
int ok = 0;
if (serialDefineWaitLines(serial, line)) {
while (!serialTestLines(serial, up?0:line, up?line:0))
if (!serialAwaitLineChange(serial))
goto done;
if (serialAwaitLineChange(serial)) ok = 1;
}
done:
serialDefineWaitLines(serial, 0);
return ok;
}
int
serialWaitLine (SerialDevice *serial, SerialLines line, int up, int flank) {
return flank? serialWaitFlank(serial, line, up):
serialWaitLines(serial, up?line:0, up?0:line);
}
int
serialWaitLineCTS (SerialDevice *serial, int up, int flank) {
return serialWaitLine(serial, SERIAL_LINE_CTS, up, flank);
}
int
serialWaitLineDSR (SerialDevice *serial, int up, int flank) {
return serialWaitLine(serial, SERIAL_LINE_DSR, up, flank);
}
int
serialPrepareDevice (SerialDevice *serial) {
if (serialReadAttributes(serial)) {
serialCopyAttributes(&serial->originalAttributes, &serial->currentAttributes);
serialInitializeAttributes(&serial->pendingAttributes);
serial->linesState = 0;
serial->waitLines = 0;
#ifdef HAVE_POSIX_THREADS
serial->currentFlowControlProc = NULL;
serial->pendingFlowControlProc = NULL;
serial->flowControlRunning = 0;
#endif /* HAVE_POSIX_THREADS */
return 1;
}
return 0;
}
int
serialParseBaud (unsigned int *baud, const char *string) {
if (isUnsignedInteger(baud, string)) return 1;
logMessage(LOG_WARNING, "invalid serial baud: %s", string);
return 0;
}
int
serialParseDataBits (unsigned int *bits, const char *string) {
if (isUnsignedInteger(bits, string)) return 1;
logMessage(LOG_WARNING, "invalid serial data bit count: %s", string);
return 0;
}
int
serialParseStopBits (unsigned int *bits, const char *string) {
if (isUnsignedInteger(bits, string)) return 1;
logMessage(LOG_WARNING, "invalid serial stop bit count: %s", string);
return 0;
}
int
serialParseParity (SerialParity *parity, const char *string) {
if (isAbbreviation(string, "none")) {
*parity = SERIAL_PARITY_NONE;
} else if (isAbbreviation(string, "odd")) {
*parity = SERIAL_PARITY_ODD;
} else if (isAbbreviation(string, "even")) {
*parity = SERIAL_PARITY_EVEN;
} else if (isAbbreviation(string, "space")) {
*parity = SERIAL_PARITY_SPACE;
} else if (isAbbreviation(string, "mark")) {
*parity = SERIAL_PARITY_MARK;
} else {
logMessage(LOG_WARNING, "invalid serial parity: %s", string);
return 0;
}
return 1;
}
int
serialParseFlowControl (SerialFlowControl *flow, const char *string) {
if (isAbbreviation(string, "none")) {
*flow = SERIAL_FLOW_NONE;
} else if (isAbbreviation(string, "hardware")) {
*flow = SERIAL_FLOW_HARDWARE;
} else {
logMessage(LOG_WARNING, "invalid serial flow control: %s", string);
return 0;
}
return 1;
}
static int
serialConfigureBaud (SerialDevice *serial, const char *string) {
if (string && *string) {
unsigned int baud;
if (!serialParseBaud(&baud, string)) return 0;
if (!serialSetBaud(serial, baud)) return 0;
}
return 1;
}
static int
serialConfigureDataBits (SerialDevice *serial, const char *string) {
if (string && *string) {
unsigned int bits;
if (!serialParseDataBits(&bits, string)) return 0;
if (!serialSetDataBits(serial, bits)) return 0;
}
return 1;
}
static int
serialConfigureStopBits (SerialDevice *serial, const char *string) {
if (string && *string) {
unsigned int bits;
if (!serialParseStopBits(&bits, string)) return 0;
if (!serialSetStopBits(serial, bits)) return 0;
}
return 1;
}
static int
serialConfigureParity (SerialDevice *serial, const char *string) {
if (string && *string) {
SerialParity parity;
if (!serialParseParity(&parity, string)) return 0;
if (!serialSetParity(serial, parity)) return 0;
}
return 1;
}
static int
serialConfigureFlowControl (SerialDevice *serial, const char *string) {
if (string && *string) {
SerialFlowControl flow;
if (!serialParseFlowControl(&flow, string)) return 0;
if (!serialSetFlowControl(serial, flow)) return 0;
}
return 1;
}
typedef enum {
SERIAL_PARM_NAME,
SERIAL_PARM_BAUD,
SERIAL_PARM_DATA_BITS,
SERIAL_PARM_STOP_BITS,
SERIAL_PARM_DATA_PARITY,
SERIAL_PARM_FLOW_CONTROL
} SerialDeviceParameter;
static const char *const serialDeviceParameterNames[] = {
"name",
"baud",
"dataBits",
"stopBits",
"parity",
"flowControl",
NULL
};
static char **
serialGetDeviceParameters (const char *identifier) {
if (!identifier) identifier = "";
return getDeviceParameters(serialDeviceParameterNames, identifier);
}
SerialDevice *
serialOpenDevice (const char *identifier) {
char **parameters = serialGetDeviceParameters(identifier);
if (parameters) {
SerialDevice *serial;
if ((serial = malloc(sizeof(*serial)))) {
memset(serial, 0, sizeof(*serial));
{
const char *name = parameters[SERIAL_PARM_NAME];
if (!*name) name = SERIAL_FIRST_DEVICE;
serial->devicePath = getDevicePath(name);
}
if (serial->devicePath) {
serial->fileDescriptor = -1;
serial->stream = NULL;
if (serialConnectDevice(serial, serial->devicePath)) {
int ok = 1;
if (!serialConfigureBaud(serial, parameters[SERIAL_PARM_BAUD])) ok = 0;
if (!serialConfigureDataBits(serial, parameters[SERIAL_PARM_DATA_BITS])) ok = 0;
if (!serialConfigureStopBits(serial, parameters[SERIAL_PARM_STOP_BITS])) ok = 0;
if (!serialConfigureParity(serial, parameters[SERIAL_PARM_DATA_PARITY])) ok = 0;
if (!serialConfigureFlowControl(serial, parameters[SERIAL_PARM_FLOW_CONTROL])) ok = 0;
deallocateStrings(parameters);
if (ok) return serial;
serialCloseDevice(serial);
return NULL;
}
free(serial->devicePath);
}
free(serial);
} else {
logMallocError();
}
deallocateStrings(parameters);
}
return NULL;
}
void
serialCloseDevice (SerialDevice *serial) {
#ifdef HAVE_POSIX_THREADS
serialStopFlowControlThread(serial);
#endif /* HAVE_POSIX_THREADS */
serialWriteAttributes(serial, &serial->originalAttributes);
if (serial->stream) {
fclose(serial->stream);
} else if (serial->fileDescriptor != -1) {
close(serial->fileDescriptor);
} else {
serialDisconnectDevice(serial);
}
free(serial->devicePath);
free(serial);
}
const char *
serialMakeDeviceIdentifier (SerialDevice *serial, char *buffer, size_t size) {
STR_BEGIN(buffer, size);
STR_PRINTF(
"%s%c%s%c%s",
SERIAL_DEVICE_QUALIFIER,
PARAMETER_QUALIFIER_CHARACTER,
serialDeviceParameterNames[SERIAL_PARM_NAME],
PARAMETER_ASSIGNMENT_CHARACTER,
serialGetDevicePath(serial)
);
STR_END;
return buffer;
}
int
serialRestartDevice (SerialDevice *serial, unsigned int baud) {
SerialLines highLines = 0;
SerialLines lowLines = 0;
int usingB0;
#ifdef HAVE_POSIX_THREADS
SerialFlowControlProc *flowControlProc = serial->pendingFlowControlProc;
#endif /* HAVE_POSIX_THREADS */
logMessage(LOG_CATEGORY(SERIAL_IO), "restarting");
if (serial->stream) {
#if defined(GRUB_RUNTIME)
#else /* clearerr() */
clearerr(serial->stream);
#endif /* clear error on stdio stream */
}
serialClearError(serial);
if (!serialDiscardOutput(serial)) return 0;
#ifdef HAVE_POSIX_THREADS
serial->pendingFlowControlProc = NULL;
#endif /* HAVE_POSIX_THREADS */
#ifdef B0
if (!serialPutSpeed(&serial->pendingAttributes, B0)) return 0;
usingB0 = 1;
#else /* B0 */
usingB0 = 0;
#endif /* B0 */
if (!serialFlushAttributes(serial)) {
if (!usingB0) return 0;
if (!serialSetBaud(serial, baud)) return 0;
if (!serialFlushAttributes(serial)) return 0;
usingB0 = 0;
}
if (!usingB0) {
SerialLines lines;
if (!serialReadLines(serial, &lines)) return 0;
{
static const SerialLines linesTable[] = {SERIAL_LINE_DTR, SERIAL_LINE_RTS, 0};
const SerialLines *line = linesTable;
while (*line) {
*((lines & *line)? &highLines: &lowLines) |= *line;
line += 1;
}
}
if (highLines)
if (!serialWriteLines(serial, 0, highLines|lowLines))
return 0;
}
asyncWait(SERIAL_DEVICE_RESTART_DELAY);
if (!serialDiscardInput(serial)) return 0;
if (!usingB0)
if (!serialWriteLines(serial, highLines, lowLines))
return 0;
#ifdef HAVE_POSIX_THREADS
serial->pendingFlowControlProc = flowControlProc;
#endif /* HAVE_POSIX_THREADS */
if (!serialSetBaud(serial, baud)) return 0;
if (!serialFlushAttributes(serial)) return 0;
logMessage(LOG_CATEGORY(SERIAL_IO), "restarted");
return 1;
}
FILE *
serialGetStream (SerialDevice *serial) {
if (!serial->stream) {
if (!serialEnsureFileDescriptor(serial)) return NULL;
#if defined(GRUB_RUNTIME)
errno = ENOSYS;
#else /* fdopen() */
serial->stream = fdopen(serial->fileDescriptor, "ab+");
#endif /* create stdio stream */
if (!serial->stream) {
logSystemError("fdopen");
return NULL;
}
}
return serial->stream;
}
int
isSerialDeviceIdentifier (const char **identifier) {
#ifdef ALLOW_DOS_DEVICE_NAMES
if (isDosDevice(*identifier, "COM")) return 1;
#endif /* ALLOW_DOS_DEVICE_NAMES */
if (hasQualifier(identifier, SERIAL_DEVICE_QUALIFIER)) return 1;
if (hasNoQualifier(*identifier)) return 1;
return 0;
}
|