46#define BUFMASK (BUFLEN-1)
47#define hasSpace(dir) (((dir##Write + 1) & BUFMASK) != dir##Read)
48#define hasData(dir) (dir##Write != dir##Read)
49#define inca(idx,amount) do { \
50 idx = (idx + amount) & BUFMASK; \
52#define inc1(idx) inca(idx, 1)
53#define normalize(idx) do { idx &= BUFMASK; } while (0)
73 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
79 struct timeval tv = {1, 0};
91 FD_SET(p->
fd, &r_set);
93 FD_SET(p->
fd, &w_set);
94 struct timeval tv = {1, 0};
102 if (FD_ISSET(p->
fd, &w_set)) {
110 printf(
"pump: wrote %d bytes: (", res);
111 for (i = 0; i<res; i++)
122 if (FD_ISSET(p->
fd, &r_set)) {
130 printf(
"pump: read %d bytes: (", res);
131 for (i = 0; i<res; i++)
158#define BAUD_TABLE_SIZE (sizeof(baud_table) / sizeof(int))
163packet(
const char *fname,
int _baud,
Link *_link,
unsigned short _verbose,
const int _cancellationFd)
164: cancellationFd(_cancellationFd)
176 for (
int i = 0; i < 128; i++) {
177 unsigned int carry =
crc_table[i] & 0x8000;
178 unsigned int tmp = (
crc_table[i] << 1) & 0xffff;
179 crc_table[i * 2 + (carry ? 0 : 1)] = tmp ^ 0x1021;
180 crc_table[i * 2 + (carry ? 1 : 0)] = tmp;
245 lout <<
"resetting serial connection" << endl;
269 <<
" baud, fd=" <<
fd << endl;
309 lout <<
"packet: >> ";
313 lout <<
" len=" << dec << len;
317 for (
int i = 0; i < len; i++) {
318 unsigned char c = b.
getByte(i);
352opCByte(
unsigned char a,
unsigned short *crc)
369 sigaddset(&sigs, SIGUSR1);
370 sigwait(&sigs, &dummy);
449 lout <<
"packet: BAD CRC" << endl;
452 lout <<
"packet: << ";
476 int rx_amount = (inw >
inRead) ?
478 if (rx_amount > 15) {
494 res = ioctl(
fd, TIOCMGET, &arg);
499 lout <<
"packet: < DTR:" << ((arg & TIOCM_DTR)?1:0)
500 <<
" RTS:" << ((arg & TIOCM_RTS)?1:0)
501 <<
" DCD:" << ((arg & TIOCM_CAR)?1:0)
502 <<
" DSR:" << ((arg & TIOCM_DSR)?1:0)
503 <<
" CTS:" << ((arg & TIOCM_CTS)?1:0) << endl;
504 if (!((arg & TIOCM_RTS) && (arg & TIOCM_DTR))) {
505 arg |= (TIOCM_DTR | TIOCM_RTS);
506 res = ioctl(
fd, TIOCMSET, &arg);
510 lout <<
"packet: > DTR:" << ((arg & TIOCM_DTR)?1:0)
511 <<
" RTS:" << ((arg & TIOCM_RTS)?1:0)
512 <<
" DCD:" << ((arg & TIOCM_CAR)?1:0)
513 <<
" DSR:" << ((arg & TIOCM_DSR)?1:0)
514 <<
" CTS:" << ((arg & TIOCM_CTS)?1:0) << endl;
519 if ((arg & TIOCM_DSR) == 0) {
523 lout <<
"packet: linkFATAL\n";
525 lout <<
"packet: linkFAILED\n";
void receive(bufferStore buf)
Effectively a delegate method that accepts data from our packet instance.
A generic container for an array of bytes.
void init()
Initializes the bufferStore.
void addByte(unsigned char c)
Appends a byte to the content of this instance.
unsigned long getLen() const
Retrieves the length of a bufferStore.
unsigned char getByte(long pos=0) const
Retrieves the byte at index pos.
unsigned int crc_table[256]
unsigned short receivedCRC
void send(bufferStore &b)
Send a buffer out to serial line.
friend void * pump_run(void *)
packet(const char *fname, int baud, Link *_link, unsigned short verbose, const int cancellationFd)
unsigned char * outBuffer
unsigned short inCRCstate
void setVerbose(short int)
void opByte(unsigned char a)
void opCByte(unsigned char a, unsigned short *crc)
void addToCrc(unsigned char a, unsigned short *crc)
int init_serial(const char *dev, int speed, int debug)
#define PKT_DEBUG_HANDSHAKE
static unsigned short pumpverbose
static void * pump_run(void *arg)
static const int baud_table[]
static void usr1handler(int sig)
Signal handler does nothing.
#define inca(idx, amount)