openport.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or
  8. * without modification, are permitted (subject to the limitations
  9. * in the disclaimer below) provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer in the documentation and/or other materials
  18. * provided with the distribution.
  19. *
  20. * * Neither the name of Qualcomm Atheros nor the names of
  21. * its contributors may be used to endorse or promote products
  22. * derived from this software without specific prior written
  23. * permission.
  24. *
  25. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  26. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  27. * COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  28. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  29. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  31. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  33. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  37. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  38. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. *--------------------------------------------------------------------*/
  41. /*====================================================================*
  42. *
  43. * void openport (struct _file_ * port, flag_t flags);
  44. *
  45. * open the serial port named by port->name and set file descriptor
  46. * port->file; datatype struct _file_ is define in tools/types.h;
  47. *
  48. * this function no longer initializes port settings because there
  49. * are too many differences in constants, variables and functions
  50. * between Linux, OpenBSD, MacOSX and Windows to cleanly implement
  51. * a single approach to serial port configuration; this means that
  52. * users should manually configure a port and then leave it alone;
  53. *
  54. * use stty on Linux systems and the Control Panel on Windows;
  55. *
  56. * port configuration code for Linux and Windows can be enabled if
  57. * needed by defining SERIAL_CONFIG at compile time; this will not
  58. * restore original port setting when the port is closed;
  59. *
  60. *
  61. * Contributor(s):
  62. * Charles Maier
  63. * Mathieu Olivari
  64. *
  65. *--------------------------------------------------------------------*/
  66. #ifndef OPENPORT_SOURCE
  67. #define OPENPORT_SOURCE
  68. #include <unistd.h>
  69. #include <stdlib.h>
  70. #include <errno.h>
  71. #if defined (WIN32)
  72. # include <windows.h>
  73. #elif defined (__linux__)
  74. # include <termios.h>
  75. #elif defined (__APPLE__)
  76. # include <termios.h>
  77. # include <net/ethernet.h>
  78. #elif defined (__OpenBSD__) || defined (__NetBSD__) || defined (__FreeBSD__)
  79. # include <termios.h>
  80. #else
  81. #error "Unknown Environment"
  82. #endif
  83. #include "../tools/types.h"
  84. #include "../tools/files.h"
  85. #include "../tools/flags.h"
  86. #include "../tools/error.h"
  87. #include "../serial/serial.h"
  88. void openport (struct _file_ * port, flag_t flags)
  89. {
  90. #if defined (WIN32)
  91. HANDLE hSerial;
  92. COMMTIMEOUTS timeouts;
  93. hSerial = CreateFile (port->name, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  94. if (hSerial == INVALID_HANDLE_VALUE)
  95. {
  96. error (1, errno, "%s", port->name);
  97. }
  98. if (_anyset (flags, UART_DEFAULT))
  99. {
  100. DCB dcbSerial =
  101. {
  102. 0
  103. };
  104. dcbSerial.DCBlength = sizeof (dcbSerial);
  105. if (!GetCommState (hSerial, &dcbSerial))
  106. {
  107. error (1, errno, "Can't read state: %s", port->name);
  108. }
  109. if (_anyset (flags, UART_VERBOSE))
  110. {
  111. printf ("getting %s ", port->name);
  112. printf ("Baud %6d ", dcbSerial.BaudRate);
  113. printf ("Data %d ", dcbSerial.ByteSize);
  114. printf ("Stop %d ", dcbSerial.StopBits);
  115. printf ("Parity %d\n", dcbSerial.Parity);
  116. }
  117. dcbSerial.BaudRate = CBR_115200;
  118. dcbSerial.ByteSize = DATABITS_8;
  119. dcbSerial.StopBits = ONESTOPBIT;
  120. dcbSerial.Parity = NOPARITY;
  121. if (_anyset (flags, UART_VERBOSE))
  122. {
  123. printf ("setting %s ", port->name);
  124. printf ("Baud %6d ", dcbSerial.BaudRate);
  125. printf ("Data %d ", dcbSerial.ByteSize);
  126. printf ("Stop %d ", dcbSerial.StopBits);
  127. printf ("Parity %d\n", dcbSerial.Parity);
  128. }
  129. if (!SetCommState (hSerial, &dcbSerial))
  130. {
  131. error (1, errno, "Can't save state: %s", port->name);
  132. }
  133. }
  134. timeouts.ReadIntervalTimeout = 10;
  135. timeouts.ReadTotalTimeoutConstant = 50;
  136. timeouts.ReadTotalTimeoutMultiplier = 10;
  137. timeouts.WriteTotalTimeoutConstant = 50;
  138. timeouts.WriteTotalTimeoutMultiplier = 10;
  139. if (!SetCommTimeouts (hSerial, &timeouts))
  140. {
  141. error (1, errno, "Can't set timeouts: %s", port->name);
  142. }
  143. CloseHandle (hSerial);
  144. if ((port->file = open (port->name, O_BINARY|O_RDWR)) == -1)
  145. {
  146. error (1, errno, "%s", port->name);
  147. }
  148. #else
  149. if ((port->file = open (port->name, O_BINARY|O_RDWR)) == -1)
  150. {
  151. error (1, errno, "%s", port->name);
  152. }
  153. if (_anyset (flags, UART_DEFAULT))
  154. {
  155. struct termios termios;
  156. #if 1
  157. /*
  158. * POSIX generic code;
  159. */
  160. tcgetattr (port->file, &termios);
  161. cfmakeraw (&termios);
  162. termios.c_cflag |= CS8;
  163. termios.c_cflag &= ~(CSTOPB);
  164. cfsetospeed (&termios, B115200);
  165. #else
  166. /*
  167. * Linux specific code;
  168. */
  169. termios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
  170. termios.c_iflag = IGNPAR;
  171. termios.c_oflag = 0;
  172. termios.c_lflag = 0;
  173. termios.c_cc [VTIME] = 0;
  174. termios.c_cc [VMIN] = 5;
  175. #endif
  176. tcsetattr (port->file, TCSANOW, &termios);
  177. }
  178. #endif
  179. return;
  180. }
  181. #endif