openport.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * void openport (struct _file_ * port, flag_t flags);
  11. *
  12. * open the serial port named by port->name and set file descriptor
  13. * port->file; datatype struct _file_ is define in tools/types.h;
  14. *
  15. * this function no longer initializes port settings because there
  16. * are too many differences in constants, variables and functions
  17. * between Linux, OpenBSD, MacOSX and Windows to cleanly implement
  18. * a single approach to serial port configuration; this means that
  19. * users should manually configure a port and then leave it alone;
  20. *
  21. * use stty on Linux systems and the Control Panel on Windows;
  22. *
  23. * port configuration code for Linux and Windows can be enabled if
  24. * needed by defining SERIAL_CONFIG at compile time; this will not
  25. * restore original port setting when the port is closed;
  26. *
  27. * Contributor(s):
  28. * Charles Maier <cmaier@qca.qualcomm.com>
  29. * Mathieu Olivari <mathieu@qca.qualcomm.com>
  30. *
  31. *--------------------------------------------------------------------*/
  32. #ifndef OPENPORT_SOURCE
  33. #define OPENPORT_SOURCE
  34. #include <unistd.h>
  35. #include <stdlib.h>
  36. #include <errno.h>
  37. #if defined (WIN32)
  38. # include <windows.h>
  39. #elif defined (__linux__)
  40. # include <termios.h>
  41. #elif defined (__APPLE__)
  42. # include <termios.h>
  43. # include <net/ethernet.h>
  44. #elif defined (__OpenBSD__)
  45. # include <termios.h>
  46. #else
  47. #error "Unknown Environment"
  48. #endif
  49. #include "../tools/types.h"
  50. #include "../tools/files.h"
  51. #include "../tools/flags.h"
  52. #include "../tools/error.h"
  53. #include "../serial/serial.h"
  54. void openport (struct _file_ * port, flag_t flags)
  55. {
  56. #if defined (WIN32)
  57. HANDLE hSerial;
  58. COMMTIMEOUTS timeouts;
  59. hSerial = CreateFile (port->name, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  60. if (hSerial == INVALID_HANDLE_VALUE)
  61. {
  62. error (1, errno, "%s", port->name);
  63. }
  64. if (_anyset (flags, UART_DEFAULT))
  65. {
  66. DCB dcbSerial =
  67. {
  68. 0
  69. };
  70. dcbSerial.DCBlength = sizeof (dcbSerial);
  71. if (! GetCommState (hSerial, & dcbSerial))
  72. {
  73. error (1, errno, "Can't read state: %s", port->name);
  74. }
  75. if (_anyset (flags, UART_VERBOSE))
  76. {
  77. printf ("getting %s ", port->name);
  78. printf ("Baud %6d ", dcbSerial.BaudRate);
  79. printf ("Data %d ", dcbSerial.ByteSize);
  80. printf ("Stop %d ", dcbSerial.StopBits);
  81. printf ("Parity %d\n", dcbSerial.Parity);
  82. }
  83. dcbSerial.BaudRate = CBR_115200;
  84. dcbSerial.ByteSize = DATABITS_8;
  85. dcbSerial.StopBits = ONESTOPBIT;
  86. dcbSerial.Parity = NOPARITY;
  87. if (_anyset (flags, UART_VERBOSE))
  88. {
  89. printf ("setting %s ", port->name);
  90. printf ("Baud %6d ", dcbSerial.BaudRate);
  91. printf ("Data %d ", dcbSerial.ByteSize);
  92. printf ("Stop %d ", dcbSerial.StopBits);
  93. printf ("Parity %d\n", dcbSerial.Parity);
  94. }
  95. if (! SetCommState (hSerial, & dcbSerial))
  96. {
  97. error (1, errno, "Can't save state: %s", port->name);
  98. }
  99. }
  100. timeouts.ReadIntervalTimeout = 10;
  101. timeouts.ReadTotalTimeoutConstant = 50;
  102. timeouts.ReadTotalTimeoutMultiplier = 10;
  103. timeouts.WriteTotalTimeoutConstant = 50;
  104. timeouts.WriteTotalTimeoutMultiplier = 10;
  105. if (! SetCommTimeouts (hSerial, & timeouts))
  106. {
  107. error (1, errno, "Can't set timeouts: %s", port->name);
  108. }
  109. CloseHandle (hSerial);
  110. if ((port->file = open (port->name, O_BINARY | O_RDWR)) == - 1)
  111. {
  112. error (1, errno, "%s", port->name);
  113. }
  114. #else
  115. if ((port->file = open (port->name, O_BINARY | O_RDWR)) == - 1)
  116. {
  117. error (1, errno, "%s", port->name);
  118. }
  119. if (_anyset (flags, UART_DEFAULT))
  120. {
  121. struct termios termios;
  122. #if 1
  123. /*
  124. * POSIX generic code;
  125. */
  126. tcgetattr (port->file, & termios);
  127. cfmakeraw (& termios);
  128. termios.c_cflag |= CS8;
  129. termios.c_cflag &= ~ (CSTOPB);
  130. cfsetospeed (& termios, B115200);
  131. #else
  132. /*
  133. * Linux specific code;
  134. */
  135. termios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
  136. termios.c_iflag = IGNPAR;
  137. termios.c_oflag = 0;
  138. termios.c_lflag = 0;
  139. termios.c_cc [VTIME] = 0;
  140. termios.c_cc [VMIN] = 5;
  141. #endif
  142. tcsetattr (port->file, TCSANOW, & termios);
  143. }
  144. #endif
  145. return;
  146. }
  147. #endif