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
|
/*
* NAME:
* serial.c -- Copyright (C) 1998 David Corcoran
*
* DESCRIPTION:
* This provides Unix/Mac serial driver support
*
* AUTHOR:
* David Corcoran, 7/22/98
*
* Modified by Mark Hartman for Macintosh support, 7/15/98
*
* LICENSE: See file LICENSE.
*
*/
#include <stdio.h> /* Standard Includes */
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>
#include "serial.h"
#ifdef CPU_MAC_PPC /* For Macintosh Support */
#include <CTBUtilities.h>
#include <CRMSerialDevices.h>
#include <ConnectionTools.h>
#include <CommResources.h>
#endif
#ifdef CPU_ICAP_PC /* For Unix Support */
#include <termios.h>
#include <sys/signal.h>
#include <sys/types.h>
#include <sys/time.h>
#endif
#ifdef DATA_DUMP_FILE
FILE *filep; /* Handle to Data Dump File */
#endif
struct IO_Specs {
HANDLE handle;
BYTE baud;
BYTE bits;
char parity;
long blocktime;
} ioport;
#ifdef CPU_MAC_PPC
typedef struct SerialPortData
{
ConnHandle pConn; /* CommToolbox connection handle */
} SerialPortData, *SPPtr, **SPHdl;
#endif
static char _rcsid[] UNUSED = "$Id: serial.c,v 1.2 1999/06/07 22:37:30 corcoran Exp $";
/*
* InititalizePort -- initialize a serial port
* This functions opens the serial port specified and sets the
* line parameters baudrate, bits per byte and parity according to the
* parameters. If no serial port is specified, the Communications
* Manager's dialog is invoked to permit the user to select not only
* the port, but all the other (baud/bits/parity) details as well.
* Selections made by the user in this case will override the
* parameters passed.
*/
bool
IO_InitializePort(int baud, int bits, char parity, char* port)
{
#ifdef CPU_MAC_PPC
HANDLE handle;
SPPtr sp;
CMErr theErr;
short procID;
CMBufferSizes buf;
register short i;
Point pt = {100,100};
char config[512], cItem[64];
InitCRM(); /* Init the Comm Resource Manager */
theErr = InitCM(); /* And the Communications Manager */
if (theErr != cmNoErr) /* If we got an error, no tools available */
return FALSE; /* So send back the default error */
procID = CMGetProcID("\pSerial Tool"); /* We want to use the Serial Tool */
if (procID == -1) /* If it's not there... */
return FALSE; /* Send back the default error */
for (i=0; i <= 7; buf[i++] = 0) /* Clear the CTB bufsizes array */
;
handle = NewHandle(sizeof(SerialPortData)); /* Allocate our memory for the port */
HLock(handle); /* And lock it down */
sp = (SPPtr)*handle; /* Dereference it */
sp->pConn = CMNew(procID, (cmNoMenus | cmQuiet), buf, 0L, 0L); /* Create a connection */
if (!sp->pConn) /* If it could not be created...*/
{
DisposeHandle(handle); /* Release the memory and... */
return FALSE; /* Send back the default error */
}
if (!port[0]) /* If no port name was passed */
{
theErr = CMChoose(&sp->pConn, pt, 0L); /* If the user didn't specify, dialog */
switch (theErr) /* What did we get back? */
{
case chooseDisaster: /* Total screwup */
DisposeHandle(handle); /* Release the memory and... */
return FALSE; /* Send back the default error */
break;
case chooseOKMinor: /* Minor changes */
case chooseOKMajor: /* Major changes */
theErr = cmNoErr; /* All OK */
break;
default: /* Other error conditions */
theErr = -1; /* Generic "no way" error */
break;
}
}
else
{
config[0] = 0; /* Null out the string */
sprintf(cItem, "Baud %d ", baud); /* Get the baud rate */
strcat(config, cItem); /* Tack it on */
sprintf(cItem, "dataBits %d ", bits); /* And the bits per char */
strcat(config, cItem); /* Tack it on */
strcat(config, "Parity "); /* Start the parity stuff */
switch (parity)
{
case 'O':
case 'o':
strcat(config, "Odd ");
break;
case 'E':
case 'e':
strcat(config, "Even ");
break;
default:
strcat(config, "None ");
break;
}
sprintf(cItem, "Port \"%s\"", port); /* Get the name of the port */
strcat(config, cItem); /* Tack it on */
theErr = CMSetConfig(sp->pConn, config); /* Set the configuration */
}
if (theErr != cmNoErr) /* If we got an error */
{
CMDispose(sp->pConn); /* Dispose the connection... */
DisposeHandle(handle); /* Release the memory and... */
return FALSE; /* Send back the default error */
}
theErr = CMOpen(sp->pConn, FALSE, 0L, -1); /* Open the connection (synchronous) */
if (theErr != cmNoErr) /* If we got an error */
{
CMDispose(sp->pConn); /* Dispose the connection... */
DisposeHandle(handle); /* Release the memory and... */
return FALSE; /* Send back the default error */
}
ioport.handle = handle; /* handle for iostructure */
HUnlock(handle); /* Unlock the handle */
return TRUE; /* And do so */
#endif
/* UNIX SERIAL SUPPORT */
#ifdef CPU_ICAP_PC
HANDLE handle;
struct termios newtio;
handle = open(port, O_RDWR | O_NOCTTY); /* Try user input depending on port */
if (handle < 0) { /* Problems with /dev/smartcard */
return FALSE;
}
#ifdef DATA_DUMP_FILE
filep = fopen( DATA_DUMP_FILE, "a");
#endif
/*
* Zero the struct to make sure that we don't use any garbage
* from the stack.
*/
memset(&newtio, 0, sizeof(newtio));
/*
* Set the baudrate
*/
switch (baud) {
case 9600: /* Baudrate 9600 */
newtio.c_cflag = B9600;
break;
case 19200: /* Baudrate 19200 */
newtio.c_cflag = B19200;
break;
case 38400: /* Baudrate 38400 */
newtio.c_cflag = B38400;
break;
case 57600: /* Baudrate 57600 */
newtio.c_cflag = B57600;
break;
case 115200: /* Baudrate 115200 */
newtio.c_cflag = B115200;
break;
case 230400: /* Baudrate 230400 */
newtio.c_cflag = B230400;
break;
default:
close(handle);
return FALSE;
}
/*
* Set the bits.
*/
switch (bits) {
case 5: /* Five bits */
newtio.c_cflag |= CS5;
break;
case 6: /* Six bits */
newtio.c_cflag |= CS6;
break;
case 7: /* Seven bits */
newtio.c_cflag |= CS7;
break;
case 8: /* Eight bits */
newtio.c_cflag |= CS8;
break;
default:
close(handle);
return FALSE;
}
/*
* Set the parity (Odd Even None)
*/
switch (parity) {
case 'O': /* Odd Parity */
newtio.c_cflag |= PARODD | PARENB | INPCK;
break;
case 'E': /* Even Parity */
newtio.c_cflag &= (~PARODD);
newtio.c_cflag |= PARENB | INPCK;
break;
case 'N': /* No Parity */
break;
default:
close(handle);
return FALSE;
}
/*
* Setting Raw Input and Defaults
*/
newtio.c_cflag |= CREAD|HUPCL|CLOCAL;
newtio.c_iflag &= ~(IGNPAR|PARMRK|INLCR|IGNCR|ICRNL);
newtio.c_iflag |= BRKINT;
newtio.c_lflag &= ~(ICANON|ECHO|ISTRIP);
newtio.c_oflag = 0;
newtio.c_lflag = 0;
newtio.c_cc[VMIN] = 1;
newtio.c_cc[VTIME] = 0;
if (tcflush(handle, TCIFLUSH) < 0) { /* Flush the serial port */
close(handle);
return FALSE;
}
if (tcsetattr(handle, TCSANOW, &newtio) < 0) { /* Set the parameters */
close(handle);
return FALSE;
}
ioport.handle = handle; /* Record the handle */
ioport.baud = baud; /* Record the baudrate */
ioport.bits = bits; /* Record the bits */
ioport.parity = parity; /* Record the parity */
ioport.blocktime = 10; /* Default Beginning Blocktime */
return TRUE;
#endif
}
HANDLE
IO_ReturnHandle() {
return ioport.handle; /* Return the current used handle */
}
int
IO_UpdateReturnBlock(int blocktime) { /* Sets the blocking timeout value */
#ifdef CPU_MAC_PPC
ioport.blocktime = (long)blocktime * 60; /* For Mac multiply 60 for seconds */
#endif
#ifdef CPU_ICAP_PC
ioport.blocktime = (long)blocktime; /* For Unix seconds by default */
#endif
return ioport.blocktime; /* Return the current blocktime */
}
int
IO_ReturnBaudRate() {
return ioport.baud; /* Return the current baudrate */
}
bool
IO_FlushBuffer() {
#ifdef CPU_MAC_PPC
SPPtr sp;
CMErr theErr = cmNoErr;
char theBuf[32];
long charsToRead;
CMFlags rFlags;
HANDLE handle;
handle = ioport.handle; /* Get the handle */
HLock(handle); /* Lock down the handle */
sp = (SPPtr)*handle; /* And dereference it */
while (theErr == cmNoErr)
{
charsToRead = 31; /* Read up to 31 characters */
theErr = CMRead(sp->pConn, theBuf,
&charsToRead, cmData, FALSE,
0L, 0L, &rFlags); /* Attempt a one-shot read */
}
HUnlock(handle);
return TRUE;
#endif
#ifdef CPU_ICAP_PC
HANDLE handle = ioport.handle;
if (tcflush(handle, TCIFLUSH) == 0)
return TRUE;
return FALSE;
#endif
}
bool
IO_Read( int readsize, BYTE *response ) {
#ifdef CPU_MAC_PPC
SPPtr sp;
CMErr theErr = cmNoErr;
long count = readsize;
CMFlags rFlags;
HANDLE handle;
long blocktime;
bool BretVal;
handle = ioport.handle; /* Get the handle */
blocktime = ioport.blocktime; /* Get the blocktime */
HLock(handle); /* Lock down the handle */
sp = (SPPtr)*handle; /* And dereference it */
theErr = CMRead(sp->pConn,response,
&count, cmData, FALSE,
0L, blocktime, &rFlags); /* Attempt a one-shot read */
BretVal = (theErr == cmNoErr && count != 0); /* No error and data present = success */
HUnlock(handle); /* Unlock the handle */
return BretVal; /* And return the success */
#endif
#ifdef CPU_ICAP_PC
fd_set rfds;
struct timeval tv;
int rval;
BYTE c;
HANDLE handle;
int count = 0;
handle = ioport.handle;
tv.tv_sec = ioport.blocktime;
tv.tv_usec = 0;
FD_ZERO(&rfds);
for (count=0; count < readsize; count++) {
FD_SET(handle, &rfds);
rval = select (handle+1, &rfds, NULL, NULL, &tv);
if (FD_ISSET(handle, &rfds)) {
read(handle, &c, 1);
#ifdef DATA_DUMP_FILE
fprintf(filep, "<- %02x ", c );
#endif
response[count] = c;
}
else {
tcflush(handle, TCIFLUSH);
return FALSE;
}
}
return TRUE;
#endif
}
bool
IO_Write(BYTE c) {
#ifdef CPU_MAC_PPC
SPPtr sp;
HANDLE handle;
CMErr theErr = cmNoErr;
long count = 1;
handle = ioport.handle;
IO_FlushBuffer(handle); /* Flush before write */
HLock(handle); /* Lock down the handle */
sp = (SPPtr)*handle; /* And dereference it */
theErr = CMWrite(sp->pConn, &c, &count,
cmData, FALSE, 0L, 0L, 0L); /* Write a single character to the port */
HUnlock(handle); /* Unlock the handle */
return (theErr == cmNoErr); /* And return the boolean success flag */
#endif
#ifdef CPU_ICAP_PC
HANDLE handle = ioport.handle;
tcflush(handle, TCIFLUSH); /* Flush the port */
#ifdef DATA_DUMP_FILE
fprintf(filep, "-> %02x ", c );
#endif
if (write(handle, &c, 1) == 1) { /* Write one byte */
return TRUE;
}
return FALSE;
#endif
}
bool
IO_Close() {
#ifdef CPU_MAC_PPC
SPPtr sp;
HANDLE handle;
CMErr theErr = cmNoErr;
handle = ioport.handle;
HLock(handle); /* Lock down the handle */
sp = (SPPtr)*handle; /* And dereference it */
theErr = CMClose(sp->pConn, FALSE,
0L, -1L, TRUE); /* Close the connection now (kill I/O) */
CMDispose(sp->pConn); /* Dispose of the connection record */
HUnlock(handle); /* Purely for form */
DisposeHandle(handle); /* Dispose of the handle */
return TRUE; /* And we're done */
#endif
#ifdef CPU_ICAP_PC
HANDLE handle = ioport.handle;
if ( close (handle) == 0 ) { /* Close the handle */
#ifdef DATA_DUMP_FILE
fclose(filep); /* Close the Data Dump */
#endif
return TRUE;
} else {
return FALSE; /* Done ! */
}
#endif
}
|