arzmq-sys 0.6.3

Low-level bindings to the zeromq library
Documentation
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

#include "protoSerial.h"
#include "protoDebug.h"

#include <termios.h> // for serial I/O stuff
#include <unistd.h>  // for close(), etc
#include <sys/ioctl.h>
#include <fcntl.h>

class UnixSerial : public ProtoSerial
{
    public:
        UnixSerial();
        static UnixSerial* Create();
        virtual ~UnixSerial();
        
        bool Open(const char* deviceName = NULL, AccessMode accessMode = RDWR, bool configure = false);
        void Close();

        bool Read(char* buffer, unsigned int& numBytes);
        bool Write(const char* buffer, unsigned int& numBytes);
        
        bool Set(StatusSignal signal);
        bool Clear(StatusSignal signal);
        bool IsSet(StatusSignal signal) const;
        
        bool SetStatus(int status) ;
        int GetStatus() const;
        
    private:
        bool SetConfiguration();
        bool GetConfiguration();
        
        static int TranslateStatusSignal(StatusSignal statusSignal);
        int GetUnixStatus() const;
        bool SetUnixStatus(int status);
      
};  // end class UnixSerial


ProtoSerial* ProtoSerial::Create()
{
    return static_cast<ProtoSerial*>(new UnixSerial);
}

UnixSerial::UnixSerial()
{
}

UnixSerial::~UnixSerial()
{
    Close();
}

bool UnixSerial::Open(const char* deviceName, AccessMode accessMode, bool configure)
{
    int flags = 0;
    switch (accessMode)
    {
        case RDONLY:
            flags = O_RDONLY;
            break;
        case WRONLY:
            flags = O_WRONLY;
            break;
        case RDWR:
            flags = O_RDWR;
            break;
    }
    TRACE("opening device ...");
    if ((descriptor = open(deviceName, flags)) < 0)
    {
        PLOG(PL_ERROR, "UnixSerial::Open() device open() error: %s\n", GetErrorString());
        return false;
    }    
    
    if (configure)
    {
        if (!SetConfiguration())
        {
            PLOG(PL_ERROR, "UnixSerial::Open() error: failed to set configuration\n", GetErrorString());
            Close();
            return false;
        }
    }
    else
    {
        TRACE("getting config ...");
        if (!GetConfiguration())
        {
            PLOG(PL_ERROR, "UnixSerial::Open() error: failed to get configuration\n", GetErrorString());
            Close();
            return false;
        }
    }
    TRACE("finalizing open ..\n");
    if (!ProtoSerial::Open())
    {
        Close();
        return false;
    }
    return true;
}  // end UnixSerial::Open()

void UnixSerial::Close()
{
    ProtoSerial::Close();
    close(descriptor);
    descriptor = INVALID_HANDLE;   
}  // end UnixSerial::Close()


bool UnixSerial::Read(char* buffer, unsigned int& numBytes)
{
    
    int result = read(descriptor, buffer, numBytes);
    if (result < 0)
    {
        numBytes = 0;
        switch (errno)
        {
            case EINTR:      // system interrupt
                TRACE("EINTR\n");
            case EAGAIN:     // no data ready
                TRACE("EAGAIN\n");
                break;
            default:
                PLOG(PL_ERROR, "UnixSerial::Read() read() error: %s\n", GetErrorString());
                return false;
        }
    }
    else
    {
        numBytes = result;
    }
    return true;
}  // end UnixSerial::Read()

bool UnixSerial::Write(const char* buffer, unsigned int& numBytes)
{
    int result = write(descriptor, buffer, numBytes);
    if (result < 0)
    {
        numBytes = 0;
        switch (errno)
        {
            case EINTR:      // system interrupt
            case EAGAIN:     // output buffer full
                break;
            default:
                PLOG(PL_ERROR, "UnixSerial::Write() write() error: %s\n", GetErrorString());
                return false;
        }
    }
    else
    {
        numBytes = result;
    }
    return true;
}  // end UnixSerial::Write()

bool UnixSerial::SetConfiguration()
{
    // Set serial port configuration
    struct termios attr;
    if (tcgetattr(descriptor, &attr) < 0)
    {
        PLOG(PL_ERROR, "UnixSerial::SetConfiguration: Error getting serial port configuration!\n");
        return false;   
    }
    
    // TBD - Should we init to a default CLOCAL config first?
    //cfmakeraw(&attr);
    //attr.c_cflag |= CLOCAL;
    
    // PARITY
    if (use_parity)
        attr.c_cflag |= PARENB;   // use parity
    else
        attr.c_cflag &= ~PARENB;  // no parity
    // BYTE SIZE
    attr.c_cflag &= ~CSIZE;  // First, clear byte size mask
    switch (byte_size)
    {
        case 5:
            attr.c_cflag |= CS5;
            break;
        case 6:
            attr.c_cflag |= CS6;
            break;
        case 7:
            attr.c_cflag |= CS7;
            break;
        case 8:
            attr.c_cflag |= CS8;
            break;
        default:
            PLOG(PL_ERROR, "UnixSerial::SetConfiguration: invalid byte size setting!\n");
            return false;
    }
    
    // LOCAL CONTROL
    if (local_control)
        attr.c_cflag |= CLOCAL;  // local flow control (ignore modem control lines)
    else
        attr.c_cflag &= ~CLOCAL; // slave to modem control lines
    
    attr.c_cflag |= CREAD;
    
    // ECHO
    if (echo_input)
        attr.c_lflag |= ECHO;  // local flow control (ignore modem control lines)
    else
        attr.c_lflag &= ~ECHO; // slave to modem control lines
    
    // BAUD RATE
    speed_t speed;
    switch(baud_rate)
    {
        // TBD - support more rates
        case 1200:
            speed = B1200;
            break;
        case 2400:
            speed = B2400;
            break;
        case 4800:
            speed = B4800;
            break;
        case 9600:
            speed = B9600;
            break;
        case 19200:
            speed = B19200;
            break;
        case 38400:
            speed = B38400;
            break;
        case 57600:
            speed = B57600;
            break;
        default:
            PLOG(PL_ERROR, "UnixSerial::SetConfiguration() error: unsupported or invalid baud rate\n");
            return false;
    }
    if (0 != cfsetispeed(&attr, speed))
    {
        PLOG(PL_ERROR, "UnixSerial::SetConfiguration() cfsetispeed() error: %s\n", GetErrorString());
        return false;   
    }
    if (0 != cfsetospeed(&attr, speed))
    {
        PLOG(PL_ERROR, "UnixSerial::SetConfiguration() cfsetospeed() error: %s\n", GetErrorString());
        return false;   
    }
    // READ TIMEOUT
    if (read_timeout < 0.0)  
    {
        // wait forever for at least one character
        attr.c_cc[VTIME]    = 0;
        attr.c_cc[VMIN]     = 1; 
    }
    else if (0.0 == read_timeout)
    {
        // return immediately with what has been buffered
        attr.c_cc[VTIME]    = 0;
        attr.c_cc[VMIN]     = 0; 
    }
    else
    {
        // return when 1 char is read or on timeout
        if (read_timeout < 0.1) read_timeout = 0.1;
        attr.c_cc[VTIME] = (int)(read_timeout*10.0 + 0.5);
        attr.c_cc[VMIN]  = 0; 
    }
   
    // Set the attributes
    if (tcsetattr(descriptor, TCSANOW, &attr) < 0)
    {
        PLOG(PL_ERROR, "UnixSerial::SetConfiguration() tcsetattr() error: %s\n", GetErrorString());
        return false;   
    }

#ifdef ASYNC_LOW_LATENCY  // (LINUX only?)  
    // (TBD) Move this out of the SetConfiguration() method?
    // _Attempt_ to set low latency if applicable
    struct serial_struct serinfo;
    if (0 != ioctl(descriptor, TIOCGSERIAL, &serinfo) < 0)
    {
        PLOG(PL_ERROR, "UnixSerial::SetConfiguration() warning: ioctl(TIOCGSERIAL) failure: %s\n", GetErrorString());
    }
    else
    {
        if (low_latency)
            serinfo.flags |= ASYNC_LOW_LATENCY;
        else
            serinfo.flags &= ~ASYNC_LOW_LATENCY;
        if (ioctl(descriptor, TIOCSSERIAL, &serinfo) < 0) 
        {
            PLOG(PL_ERROR, "UnixSerial::SetConfiguration() warning: ioctl(TIOCSSERIAL) error: %s\n", GetErrorString());
        }
    }
#endif // ASYNC_LOW_LATENCY
    return true;
}  // UnixSerial::SetConfiguration()

bool UnixSerial::GetConfiguration()
{
    // Get serial port attributes
    struct termios attr;
    if (tcgetattr(descriptor, &attr) < 0)
    {
        PLOG(PL_ERROR, "UnixSerial::SetConfiguration: Error getting serial port configuration!\n");
        return false;   
    }
    
    // BAUD RATE
    speed_t speed = cfgetispeed(&attr);
    switch(speed)
    {
        // TBD - support more rates
        case B1200:
            baud_rate = 1200;
            break;
        case B2400:
            baud_rate = 2400;
            break;
        case B4800:
            baud_rate = 4800;
            break;
        case B9600:
            baud_rate = 9600;
            break;
        case B19200:
            baud_rate = 19200;
            break;
        case B38400:
            baud_rate = 38400;
            break;
        case B57600:
            baud_rate = 57600;
            break;
        default:
            PLOG(PL_ERROR, "UnixSerial::GetConfiguration() error: unsupported or invalid baud rate\n");
            return false;
    }
    
    // BYTE SIZE
    switch (attr.c_cflag & CSIZE)
    {
        case CS5:
            byte_size = 5; // 5 bits per byte
            break;
        case CS6:
            byte_size = 6; // 6 bits per byte
            break;
        case CS7:
            byte_size = 7; // 7 bits per byte
            break;
        case CS8:
            byte_size = 8; // 8 bits per byte
            break;
        default:
            PLOG(PL_ERROR, "UnixSerial::GetConfiguration() error: unsupported or invalid byte size\n");
            return false;
    }
    
    // PARITY
    use_parity = (0 != (attr.c_cflag & PARENB));  
    
    // LOCAL CONTROL
    local_control = (0 != (attr.c_cflag & CLOCAL));  
    
    // READ TIMEOUT
    if (0 != attr.c_cc[VTIME])
        read_timeout = 0.1 * (double)attr.c_cc[VTIME];
    else if (0 != attr.c_cc[VMIN])
        read_timeout = -1.0;  // TBD - set VMIN to 1 ???
    else
        read_timeout = 0.0;
    
    return true;
    
}  // end  UnixSerial::GetConfiguration()


int UnixSerial::TranslateStatusSignal(StatusSignal statusSignal)
{
    switch (statusSignal)
    {
        case DTR:
            return (int)TIOCM_DTR; 
        case RTS:
            return TIOCM_RTS;
        case CTS:
            return TIOCM_CTS;
        case DCD:
            return TIOCM_CD;
        case DSR:
            return TIOCM_DSR;
        case RNG:
            return TIOCM_RNG;
    }
    return 0;
}  // end UnixSerial::TranslateStatusSignal()


bool UnixSerial::SetUnixStatus(int unixStatus)
{
    if (!IsOpen()) return 0;
    if (ioctl(descriptor, TIOCMSET, &unixStatus) < 0)
    {
        PLOG(PL_ERROR, "ProtoSerial::SetStatus() ioctl(TIOCMSET) error: %s\n", GetErrorString());
        return false;   
    }
    return true;
}  // end UnixSerial::SetUnixStatus()

bool UnixSerial::SetStatus(int status)
{
    int unixStatus = 0;
    if (0 != (status & DTR))
        unixStatus |= TIOCM_DTR;
    if (0 != (status & RTS))
        unixStatus |= TIOCM_RTS;
    if (0 != (status & CTS))
        unixStatus |= TIOCM_CTS;
    if (0 != (status & DCD))
        unixStatus |= TIOCM_CD;
    if (0 != (status & DSR))
        unixStatus |= TIOCM_DSR;
    if (0 != (status & RNG))
        unixStatus |= TIOCM_RNG;
    return SetUnixStatus(unixStatus);
}  // end UnixSerial::SetStatus()

int UnixSerial::GetUnixStatus() const
{
    if (!IsOpen()) return 0;
    int unixStatus;
    if (ioctl(descriptor, TIOCMGET, &unixStatus) < 0)
    {
        PLOG(PL_ERROR, "ProtoSerial::GetStatus() ioctl(TIOCMGET) error: %s\n", GetErrorString());
        return 0;   
    }
    return unixStatus;
}  // end ProtoSerial::GetUnixStatus()

int UnixSerial::GetStatus() const
{
    int unixStatus = GetUnixStatus();
    int status = 0;
    if (0 != (unixStatus & TIOCM_DTR))
        status |= DTR;
    if (0 != (unixStatus & TIOCM_RTS))
        status |= RTS;
    if (0 != (unixStatus & TIOCM_CTS))
        status |= CTS;
    if (0 != (unixStatus & TIOCM_CD))
        status |= DCD;
    if (0 != (unixStatus & TIOCM_DSR))
        status |= DSR;
    if (0 != (unixStatus & TIOCM_RNG))
        status |= RNG;
    return status;
}  // end UnixSerial::GetStatus()

bool UnixSerial::Set(StatusSignal statusSignal)
{
    int unixStatus = GetUnixStatus();
    unixStatus |= TranslateStatusSignal(statusSignal);
    return SetUnixStatus(unixStatus);
}  // end UnixSerial::Set()

bool UnixSerial::Clear(StatusSignal statusSignal)
{
    int unixStatus = GetUnixStatus();
    unixStatus &= ~TranslateStatusSignal(statusSignal);
    return SetUnixStatus(unixStatus);
}  // end UnixSerial::Clear()

bool UnixSerial::IsSet(StatusSignal statusSignal) const
{
    int unixStatus = GetUnixStatus();
    int unixSignal = TranslateStatusSignal(statusSignal);
    return (0 != (unixStatus & unixSignal));
}  // end UnixSerial::IsSet()