| 1 |
|
| 2 |
#include "protoSerial.h" |
| 3 |
#include "protoDebug.h" |
| 4 |
|
| 5 |
#include <termios.h> // for serial I/O stuff |
| 6 |
#include <unistd.h> // for close(), etc |
| 7 |
#include <sys/ioctl.h> |
| 8 |
#include <fcntl.h> |
| 9 |
|
| 10 |
class UnixSerial : public ProtoSerial |
| 11 |
{ |
| 12 |
public: |
| 13 |
UnixSerial(); |
| 14 |
static UnixSerial* Create(); |
| 15 |
virtual ~UnixSerial(); |
| 16 |
|
| 17 |
bool Open(const char* deviceName = NULL, AccessMode accessMode = RDWR, bool configure = false); |
| 18 |
void Close(); |
| 19 |
|
| 20 |
bool Read(char* buffer, unsigned int& numBytes); |
| 21 |
bool Write(const char* buffer, unsigned int& numBytes); |
| 22 |
|
| 23 |
bool Set(StatusSignal signal); |
| 24 |
bool Clear(StatusSignal signal); |
| 25 |
bool IsSet(StatusSignal signal) const; |
| 26 |
|
| 27 |
bool SetStatus(int status) ; |
| 28 |
int GetStatus() const; |
| 29 |
|
| 30 |
private: |
| 31 |
bool SetConfiguration(); |
| 32 |
bool GetConfiguration(); |
| 33 |
|
| 34 |
static int TranslateStatusSignal(StatusSignal statusSignal); |
| 35 |
int GetUnixStatus() const; |
| 36 |
bool SetUnixStatus(int status); |
| 37 |
|
| 38 |
}; // end class UnixSerial |
| 39 |
|
| 40 |
|
| 41 |
ProtoSerial* ProtoSerial::Create() |
| 42 |
{ |
| 43 |
return static_cast<ProtoSerial*>(new UnixSerial); |
| 44 |
} |
| 45 |
|
| 46 |
UnixSerial::UnixSerial() |
| 47 |
{ |
| 48 |
} |
| 49 |
|
| 50 |
UnixSerial::~UnixSerial() |
| 51 |
{ |
| 52 |
Close(); |
| 53 |
} |
| 54 |
|
| 55 |
bool UnixSerial::Open(const char* deviceName, AccessMode accessMode, bool configure) |
| 56 |
{ |
| 57 |
int flags = 0; |
| 58 |
switch (accessMode) |
| 59 |
{ |
| 60 |
case RDONLY: |
| 61 |
flags = O_RDONLY; |
| 62 |
break; |
| 63 |
case WRONLY: |
| 64 |
flags = O_WRONLY; |
| 65 |
break; |
| 66 |
case RDWR: |
| 67 |
flags = O_RDWR; |
| 68 |
break; |
| 69 |
} |
| 70 |
TRACE("opening device ..."); |
| 71 |
if ((descriptor = open(deviceName, flags)) < 0) |
| 72 |
{ |
| 73 |
PLOG(PL_ERROR, "UnixSerial::Open() device open() error: %s\n", GetErrorString()); |
| 74 |
return false; |
| 75 |
} |
| 76 |
|
| 77 |
if (configure) |
| 78 |
{ |
| 79 |
if (!SetConfiguration()) |
| 80 |
{ |
| 81 |
PLOG(PL_ERROR, "UnixSerial::Open() error: failed to set configuration\n", GetErrorString()); |
| 82 |
Close(); |
| 83 |
return false; |
| 84 |
} |
| 85 |
} |
| 86 |
else |
| 87 |
{ |
| 88 |
TRACE("getting config ..."); |
| 89 |
if (!GetConfiguration()) |
| 90 |
{ |
| 91 |
PLOG(PL_ERROR, "UnixSerial::Open() error: failed to get configuration\n", GetErrorString()); |
| 92 |
Close(); |
| 93 |
return false; |
| 94 |
} |
| 95 |
} |
| 96 |
TRACE("finalizing open ..\n"); |
| 97 |
if (!ProtoSerial::Open()) |
| 98 |
{ |
| 99 |
Close(); |
| 100 |
return false; |
| 101 |
} |
| 102 |
return true; |
| 103 |
} // end UnixSerial::Open() |
| 104 |
|
| 105 |
void UnixSerial::Close() |
| 106 |
{ |
| 107 |
ProtoSerial::Close(); |
| 108 |
close(descriptor); |
| 109 |
descriptor = INVALID_HANDLE; |
| 110 |
} // end UnixSerial::Close() |
| 111 |
|
| 112 |
|
| 113 |
bool UnixSerial::Read(char* buffer, unsigned int& numBytes) |
| 114 |
{ |
| 115 |
|
| 116 |
int result = read(descriptor, buffer, numBytes); |
| 117 |
if (result < 0) |
| 118 |
{ |
| 119 |
numBytes = 0; |
| 120 |
switch (errno) |
| 121 |
{ |
| 122 |
case EINTR: // system interrupt |
| 123 |
TRACE("EINTR\n"); |
| 124 |
case EAGAIN: // no data ready |
| 125 |
TRACE("EAGAIN\n"); |
| 126 |
break; |
| 127 |
default: |
| 128 |
PLOG(PL_ERROR, "UnixSerial::Read() read() error: %s\n", GetErrorString()); |
| 129 |
return false; |
| 130 |
} |
| 131 |
} |
| 132 |
else |
| 133 |
{ |
| 134 |
numBytes = result; |
| 135 |
} |
| 136 |
return true; |
| 137 |
} // end UnixSerial::Read() |
| 138 |
|
| 139 |
bool UnixSerial::Write(const char* buffer, unsigned int& numBytes) |
| 140 |
{ |
| 141 |
int result = write(descriptor, buffer, numBytes); |
| 142 |
if (result < 0) |
| 143 |
{ |
| 144 |
numBytes = 0; |
| 145 |
switch (errno) |
| 146 |
{ |
| 147 |
case EINTR: // system interrupt |
| 148 |
case EAGAIN: // output buffer full |
| 149 |
break; |
| 150 |
default: |
| 151 |
PLOG(PL_ERROR, "UnixSerial::Write() write() error: %s\n", GetErrorString()); |
| 152 |
return false; |
| 153 |
} |
| 154 |
} |
| 155 |
else |
| 156 |
{ |
| 157 |
numBytes = result; |
| 158 |
} |
| 159 |
return true; |
| 160 |
} // end UnixSerial::Write() |
| 161 |
|
| 162 |
bool UnixSerial::SetConfiguration() |
| 163 |
{ |
| 164 |
// Set serial port configuration |
| 165 |
struct termios attr; |
| 166 |
if (tcgetattr(descriptor, &attr) < 0) |
| 167 |
{ |
| 168 |
PLOG(PL_ERROR, "UnixSerial::SetConfiguration: Error getting serial port configuration!\n"); |
| 169 |
return false; |
| 170 |
} |
| 171 |
|
| 172 |
// TBD - Should we init to a default CLOCAL config first? |
| 173 |
//cfmakeraw(&attr); |
| 174 |
//attr.c_cflag |= CLOCAL; |
| 175 |
|
| 176 |
// PARITY |
| 177 |
if (use_parity) |
| 178 |
attr.c_cflag |= PARENB; // use parity |
| 179 |
else |
| 180 |
attr.c_cflag &= ~PARENB; // no parity |
| 181 |
// BYTE SIZE |
| 182 |
attr.c_cflag &= ~CSIZE; // First, clear byte size mask |
| 183 |
switch (byte_size) |
| 184 |
{ |
| 185 |
case 5: |
| 186 |
attr.c_cflag |= CS5; |
| 187 |
break; |
| 188 |
case 6: |
| 189 |
attr.c_cflag |= CS6; |
| 190 |
break; |
| 191 |
case 7: |
| 192 |
attr.c_cflag |= CS7; |
| 193 |
break; |
| 194 |
case 8: |
| 195 |
attr.c_cflag |= CS8; |
| 196 |
break; |
| 197 |
default: |
| 198 |
PLOG(PL_ERROR, "UnixSerial::SetConfiguration: invalid byte size setting!\n"); |
| 199 |
return false; |
| 200 |
} |
| 201 |
|
| 202 |
// LOCAL CONTROL |
| 203 |
if (local_control) |
| 204 |
attr.c_cflag |= CLOCAL; // local flow control (ignore modem control lines) |
| 205 |
else |
| 206 |
attr.c_cflag &= ~CLOCAL; // slave to modem control lines |
| 207 |
|
| 208 |
attr.c_cflag |= CREAD; |
| 209 |
|
| 210 |
// ECHO |
| 211 |
if (echo_input) |
| 212 |
attr.c_lflag |= ECHO; // local flow control (ignore modem control lines) |
| 213 |
else |
| 214 |
attr.c_lflag &= ~ECHO; // slave to modem control lines |
| 215 |
|
| 216 |
// BAUD RATE |
| 217 |
speed_t speed; |
| 218 |
switch(baud_rate) |
| 219 |
{ |
| 220 |
// TBD - support more rates |
| 221 |
case 1200: |
| 222 |
speed = B1200; |
| 223 |
break; |
| 224 |
case 2400: |
| 225 |
speed = B2400; |
| 226 |
break; |
| 227 |
case 4800: |
| 228 |
speed = B4800; |
| 229 |
break; |
| 230 |
case 9600: |
| 231 |
speed = B9600; |
| 232 |
break; |
| 233 |
case 19200: |
| 234 |
speed = B19200; |
| 235 |
break; |
| 236 |
case 38400: |
| 237 |
speed = B38400; |
| 238 |
break; |
| 239 |
case 57600: |
| 240 |
speed = B57600; |
| 241 |
break; |
| 242 |
default: |
| 243 |
PLOG(PL_ERROR, "UnixSerial::SetConfiguration() error: unsupported or invalid baud rate\n"); |
| 244 |
return false; |
| 245 |
} |
| 246 |
if (0 != cfsetispeed(&attr, speed)) |
| 247 |
{ |
| 248 |
PLOG(PL_ERROR, "UnixSerial::SetConfiguration() cfsetispeed() error: %s\n", GetErrorString()); |
| 249 |
return false; |
| 250 |
} |
| 251 |
if (0 != cfsetospeed(&attr, speed)) |
| 252 |
{ |
| 253 |
PLOG(PL_ERROR, "UnixSerial::SetConfiguration() cfsetospeed() error: %s\n", GetErrorString()); |
| 254 |
return false; |
| 255 |
} |
| 256 |
// READ TIMEOUT |
| 257 |
if (read_timeout < 0.0) |
| 258 |
{ |
| 259 |
// wait forever for at least one character |
| 260 |
attr.c_cc[VTIME] = 0; |
| 261 |
attr.c_cc[VMIN] = 1; |
| 262 |
} |
| 263 |
else if (0.0 == read_timeout) |
| 264 |
{ |
| 265 |
// return immediately with what has been buffered |
| 266 |
attr.c_cc[VTIME] = 0; |
| 267 |
attr.c_cc[VMIN] = 0; |
| 268 |
} |
| 269 |
else |
| 270 |
{ |
| 271 |
// return when 1 char is read or on timeout |
| 272 |
if (read_timeout < 0.1) read_timeout = 0.1; |
| 273 |
attr.c_cc[VTIME] = (int)(read_timeout*10.0 + 0.5); |
| 274 |
attr.c_cc[VMIN] = 0; |
| 275 |
} |
| 276 |
|
| 277 |
// Set the attributes |
| 278 |
if (tcsetattr(descriptor, TCSANOW, &attr) < 0) |
| 279 |
{ |
| 280 |
PLOG(PL_ERROR, "UnixSerial::SetConfiguration() tcsetattr() error: %s\n", GetErrorString()); |
| 281 |
return false; |
| 282 |
} |
| 283 |
|
| 284 |
#ifdef ASYNC_LOW_LATENCY // (LINUX only?) |
| 285 |
// (TBD) Move this out of the SetConfiguration() method? |
| 286 |
// _Attempt_ to set low latency if applicable |
| 287 |
struct serial_struct serinfo; |
| 288 |
if (0 != ioctl(descriptor, TIOCGSERIAL, &serinfo) < 0) |
| 289 |
{ |
| 290 |
PLOG(PL_ERROR, "UnixSerial::SetConfiguration() warning: ioctl(TIOCGSERIAL) failure: %s\n", GetErrorString()); |
| 291 |
} |
| 292 |
else |
| 293 |
{ |
| 294 |
if (low_latency) |
| 295 |
serinfo.flags |= ASYNC_LOW_LATENCY; |
| 296 |
else |
| 297 |
serinfo.flags &= ~ASYNC_LOW_LATENCY; |
| 298 |
if (ioctl(descriptor, TIOCSSERIAL, &serinfo) < 0) |
| 299 |
{ |
| 300 |
PLOG(PL_ERROR, "UnixSerial::SetConfiguration() warning: ioctl(TIOCSSERIAL) error: %s\n", GetErrorString()); |
| 301 |
} |
| 302 |
} |
| 303 |
#endif // ASYNC_LOW_LATENCY |
| 304 |
return true; |
| 305 |
} // UnixSerial::SetConfiguration() |
| 306 |
|
| 307 |
bool UnixSerial::GetConfiguration() |
| 308 |
{ |
| 309 |
// Get serial port attributes |
| 310 |
struct termios attr; |
| 311 |
if (tcgetattr(descriptor, &attr) < 0) |
| 312 |
{ |
| 313 |
PLOG(PL_ERROR, "UnixSerial::SetConfiguration: Error getting serial port configuration!\n"); |
| 314 |
return false; |
| 315 |
} |
| 316 |
|
| 317 |
// BAUD RATE |
| 318 |
speed_t speed = cfgetispeed(&attr); |
| 319 |
switch(speed) |
| 320 |
{ |
| 321 |
// TBD - support more rates |
| 322 |
case B1200: |
| 323 |
baud_rate = 1200; |
| 324 |
break; |
| 325 |
case B2400: |
| 326 |
baud_rate = 2400; |
| 327 |
break; |
| 328 |
case B4800: |
| 329 |
baud_rate = 4800; |
| 330 |
break; |
| 331 |
case B9600: |
| 332 |
baud_rate = 9600; |
| 333 |
break; |
| 334 |
case B19200: |
| 335 |
baud_rate = 19200; |
| 336 |
break; |
| 337 |
case B38400: |
| 338 |
baud_rate = 38400; |
| 339 |
break; |
| 340 |
case B57600: |
| 341 |
baud_rate = 57600; |
| 342 |
break; |
| 343 |
default: |
| 344 |
PLOG(PL_ERROR, "UnixSerial::GetConfiguration() error: unsupported or invalid baud rate\n"); |
| 345 |
return false; |
| 346 |
} |
| 347 |
|
| 348 |
// BYTE SIZE |
| 349 |
switch (attr.c_cflag & CSIZE) |
| 350 |
{ |
| 351 |
case CS5: |
| 352 |
byte_size = 5; // 5 bits per byte |
| 353 |
break; |
| 354 |
case CS6: |
| 355 |
byte_size = 6; // 6 bits per byte |
| 356 |
break; |
| 357 |
case CS7: |
| 358 |
byte_size = 7; // 7 bits per byte |
| 359 |
break; |
| 360 |
case CS8: |
| 361 |
byte_size = 8; // 8 bits per byte |
| 362 |
break; |
| 363 |
default: |
| 364 |
PLOG(PL_ERROR, "UnixSerial::GetConfiguration() error: unsupported or invalid byte size\n"); |
| 365 |
return false; |
| 366 |
} |
| 367 |
|
| 368 |
// PARITY |
| 369 |
use_parity = (0 != (attr.c_cflag & PARENB)); |
| 370 |
|
| 371 |
// LOCAL CONTROL |
| 372 |
local_control = (0 != (attr.c_cflag & CLOCAL)); |
| 373 |
|
| 374 |
// READ TIMEOUT |
| 375 |
if (0 != attr.c_cc[VTIME]) |
| 376 |
read_timeout = 0.1 * (double)attr.c_cc[VTIME]; |
| 377 |
else if (0 != attr.c_cc[VMIN]) |
| 378 |
read_timeout = -1.0; // TBD - set VMIN to 1 ??? |
| 379 |
else |
| 380 |
read_timeout = 0.0; |
| 381 |
|
| 382 |
return true; |
| 383 |
|
| 384 |
} // end UnixSerial::GetConfiguration() |
| 385 |
|
| 386 |
|
| 387 |
int UnixSerial::TranslateStatusSignal(StatusSignal statusSignal) |
| 388 |
{ |
| 389 |
switch (statusSignal) |
| 390 |
{ |
| 391 |
case DTR: |
| 392 |
return (int)TIOCM_DTR; |
| 393 |
case RTS: |
| 394 |
return TIOCM_RTS; |
| 395 |
case CTS: |
| 396 |
return TIOCM_CTS; |
| 397 |
case DCD: |
| 398 |
return TIOCM_CD; |
| 399 |
case DSR: |
| 400 |
return TIOCM_DSR; |
| 401 |
case RNG: |
| 402 |
return TIOCM_RNG; |
| 403 |
} |
| 404 |
return 0; |
| 405 |
} // end UnixSerial::TranslateStatusSignal() |
| 406 |
|
| 407 |
|
| 408 |
bool UnixSerial::SetUnixStatus(int unixStatus) |
| 409 |
{ |
| 410 |
if (!IsOpen()) return 0; |
| 411 |
if (ioctl(descriptor, TIOCMSET, &unixStatus) < 0) |
| 412 |
{ |
| 413 |
PLOG(PL_ERROR, "ProtoSerial::SetStatus() ioctl(TIOCMSET) error: %s\n", GetErrorString()); |
| 414 |
return false; |
| 415 |
} |
| 416 |
return true; |
| 417 |
} // end UnixSerial::SetUnixStatus() |
| 418 |
|
| 419 |
bool UnixSerial::SetStatus(int status) |
| 420 |
{ |
| 421 |
int unixStatus = 0; |
| 422 |
if (0 != (status & DTR)) |
| 423 |
unixStatus |= TIOCM_DTR; |
| 424 |
if (0 != (status & RTS)) |
| 425 |
unixStatus |= TIOCM_RTS; |
| 426 |
if (0 != (status & CTS)) |
| 427 |
unixStatus |= TIOCM_CTS; |
| 428 |
if (0 != (status & DCD)) |
| 429 |
unixStatus |= TIOCM_CD; |
| 430 |
if (0 != (status & DSR)) |
| 431 |
unixStatus |= TIOCM_DSR; |
| 432 |
if (0 != (status & RNG)) |
| 433 |
unixStatus |= TIOCM_RNG; |
| 434 |
return SetUnixStatus(unixStatus); |
| 435 |
} // end UnixSerial::SetStatus() |
| 436 |
|
| 437 |
int UnixSerial::GetUnixStatus() const |
| 438 |
{ |
| 439 |
if (!IsOpen()) return 0; |
| 440 |
int unixStatus; |
| 441 |
if (ioctl(descriptor, TIOCMGET, &unixStatus) < 0) |
| 442 |
{ |
| 443 |
PLOG(PL_ERROR, "ProtoSerial::GetStatus() ioctl(TIOCMGET) error: %s\n", GetErrorString()); |
| 444 |
return 0; |
| 445 |
} |
| 446 |
return unixStatus; |
| 447 |
} // end ProtoSerial::GetUnixStatus() |
| 448 |
|
| 449 |
int UnixSerial::GetStatus() const |
| 450 |
{ |
| 451 |
int unixStatus = GetUnixStatus(); |
| 452 |
int status = 0; |
| 453 |
if (0 != (unixStatus & TIOCM_DTR)) |
| 454 |
status |= DTR; |
| 455 |
if (0 != (unixStatus & TIOCM_RTS)) |
| 456 |
status |= RTS; |
| 457 |
if (0 != (unixStatus & TIOCM_CTS)) |
| 458 |
status |= CTS; |
| 459 |
if (0 != (unixStatus & TIOCM_CD)) |
| 460 |
status |= DCD; |
| 461 |
if (0 != (unixStatus & TIOCM_DSR)) |
| 462 |
status |= DSR; |
| 463 |
if (0 != (unixStatus & TIOCM_RNG)) |
| 464 |
status |= RNG; |
| 465 |
return status; |
| 466 |
} // end UnixSerial::GetStatus() |
| 467 |
|
| 468 |
bool UnixSerial::Set(StatusSignal statusSignal) |
| 469 |
{ |
| 470 |
int unixStatus = GetUnixStatus(); |
| 471 |
unixStatus |= TranslateStatusSignal(statusSignal); |
| 472 |
return SetUnixStatus(unixStatus); |
| 473 |
} // end UnixSerial::Set() |
| 474 |
|
| 475 |
bool UnixSerial::Clear(StatusSignal statusSignal) |
| 476 |
{ |
| 477 |
int unixStatus = GetUnixStatus(); |
| 478 |
unixStatus &= ~TranslateStatusSignal(statusSignal); |
| 479 |
return SetUnixStatus(unixStatus); |
| 480 |
} // end UnixSerial::Clear() |
| 481 |
|
| 482 |
bool UnixSerial::IsSet(StatusSignal statusSignal) const |
| 483 |
{ |
| 484 |
int unixStatus = GetUnixStatus(); |
| 485 |
int unixSignal = TranslateStatusSignal(statusSignal); |
| 486 |
return (0 != (unixStatus & unixSignal)); |
| 487 |
} // end UnixSerial::IsSet() |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|