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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
|
/*===========================================================================
FILE:
Comm.cpp
DESCRIPTION:
Implementation of cComm class
PUBLIC CLASSES AND METHODS:
cComm
This class wraps low level port communications
Copyright (c) 2011, Code Aurora Forum. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Code Aurora Forum nor
the names of its contributors may be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
===========================================================================*/
//---------------------------------------------------------------------------
// Include Files
//---------------------------------------------------------------------------
#include "StdAfx.h"
#include "Comm.h"
#include "ProtocolServer.h"
//---------------------------------------------------------------------------
// Definitions
//---------------------------------------------------------------------------
// Thread commands
#define START_READ_CMD 0
#define STOP_READ_CMD 1
#define EXIT_CMD 2
/*=========================================================================*/
// Free Methods
/*=========================================================================*/
/*===========================================================================
METHOD:
RxThread (Free Method)
DESCRIPTION:
Thread for simulating asynchronous reads
PARAMETERS:
pData [ I ] Asynchronous read object
RETURN VALUE:
void * - thread exit value (always 0)
===========================================================================*/
void * RxThread( void * pData )
{
cComm * pComm = (cComm*)pData;
if (pComm == NULL || pComm->IsValid() == false)
{
return 0;
}
fd_set inputSet, outputSet;
FD_ZERO( &inputSet );
FD_SET( pComm->mCommandPipe[READING], &inputSet );
int largestFD = pComm->mCommandPipe[READING];
int status = 0;
while (true)
{
// No FD_COPY() available
memcpy( &outputSet, &inputSet, sizeof( fd_set ) );
status = select( largestFD + 1, &outputSet, NULL, NULL, NULL );
if (status <= 0)
{
TRACE( "error %d in select, errno %d\n", status, errno );
break;
}
if (FD_ISSET( pComm->mCommandPipe[READING], &outputSet ) == true)
{
// Read from the pipe
BYTE cmd;
status = read( pComm->mCommandPipe[READING], &cmd, 1 );
if (status != 1)
{
TRACE( "cmd error %d\n", status );
break;
}
if (cmd == START_READ_CMD)
{
FD_SET( pComm->mPort, &inputSet );
largestFD = std::max( pComm->mPort,
pComm->mCommandPipe[READING] );
}
else if (cmd == STOP_READ_CMD)
{
FD_CLR( pComm->mPort, &inputSet );
largestFD = pComm->mCommandPipe[READING];
}
else
{
// EXIT_CMD or anything else
break;
}
}
else if (FD_ISSET( pComm->mPort, &outputSet ) == true)
{
// Stop watching for read data
FD_CLR( pComm->mPort, &inputSet );
largestFD = pComm->mCommandPipe[READING];
// Perform a read
status = read( pComm->mPort,
pComm->mpBuffer,
pComm->mBuffSz );
cIOCallback * pCallback = pComm->mpRxCallback;
pComm->mpRxCallback = 0;
if (pCallback == (cIOCallback *)1)
{
// We wanted to read, but not to be notified
}
else if (status >= 0)
{
pCallback->IOComplete( 0, status );
}
else
{
pCallback->IOComplete( status, 0 );
}
}
}
return 0;
};
/*=========================================================================*/
// cComm Methods
/*=========================================================================*/
/*===========================================================================
METHOD:
cComm (Public Method)
DESCRIPTION:
Constructor
RETURN VALUE:
None
===========================================================================*/
cComm::cComm()
: mPortName( "" ),
mPort( INVALID_HANDLE_VALUE ),
mpRxCallback( 0 ),
mbCancelWrite( false ),
mpBuffer( 0 ),
mBuffSz( 0 ),
mRxThreadID( 0 )
{
mCommandPipe[READING] = INVALID_HANDLE_VALUE;
mCommandPipe[WRITING] = INVALID_HANDLE_VALUE;
}
/*===========================================================================
METHOD:
~cComm (Public Method)
DESCRIPTION:
Destructor
RETURN VALUE:
None
===========================================================================*/
cComm::~cComm()
{
// Disconnect from current port
Disconnect();
mCommandPipe[READING] = INVALID_HANDLE_VALUE;
mCommandPipe[WRITING] = INVALID_HANDLE_VALUE;
}
/*===========================================================================
METHOD:
IsValid (Public Method)
DESCRIPTION:
Is this object valid?
RETURN VALUE:
Bool
===========================================================================*/
bool cComm::IsValid()
{
// Nothing to do, dependant on extended class functionality
return true;
}
/*===========================================================================
METHOD:
Connect (Public Method)
DESCRIPTION:
Connect to the specified port
PARAMETERS:
pPort [ I ] - Name of port to open (IE: /dev/qcqmi0)
RETURN VALUE:
bool
===========================================================================*/
bool cComm::Connect( LPCSTR pPort )
{
if (IsValid() == false || pPort == 0 || pPort[0] == 0)
{
return false;
}
if (mPort != INVALID_HANDLE_VALUE)
{
Disconnect();
}
// Initialize command pipe for read thread
int nRet = pipe( mCommandPipe );
if (nRet != 0)
{
TRACE( "cComm:Connect() pipe creation failed %d\n", nRet );
return false;
}
// Start the read thread
nRet = pthread_create( &mRxThreadID,
0,
RxThread,
this );
if (nRet != 0)
{
TRACE( "cComm::Connect() pthread_create = %d\n", nRet );
Disconnect();
return false;
}
// Opening the com port
mPort = open( pPort, O_RDWR );
if (mPort == INVALID_HANDLE_VALUE)
{
Disconnect();
return false;
}
// Save port name
mPortName = pPort;
// Success!
return true;
}
/*===========================================================================
METHOD:
RunIOCTL (Public Method)
DESCRIPTION:
Run an IOCTL on the open file handle
PARAMETERS:
ioctlReq [ I ] - ioctl request value
pData [I/O] - input or output specific to ioctl request value
RETURN VALUE:
int - ioctl return value (0 for success)
===========================================================================*/
int cComm::RunIOCTL(
UINT ioctlReq,
void * pData )
{
if (mPort == INVALID_HANDLE_VALUE)
{
TRACE( "Invalid file handle\n" );
return -EBADFD;
}
return ioctl( mPort, ioctlReq, pData );
}
/*===========================================================================
METHOD:
Disconnect (Public Method)
DESCRIPTION:
Disconnect from the current port
RETURN VALUE:
bool
===========================================================================*/
bool cComm::Disconnect()
{
// Assume success
bool bRC = true;
if (mCommandPipe[WRITING] != INVALID_HANDLE_VALUE)
{
if (mRxThreadID != 0)
{
// Notify the thread to exit
BYTE byte = EXIT_CMD;
write( mCommandPipe[WRITING], &byte, 1 );
// And wait for it
TRACE( "cComm::Disconnnect() joining thread\n" );
int nRC = pthread_join( mRxThreadID, 0 );
if (nRC != 0)
{
TRACE( "failed to join thread %d\n", nRC );
bRC = false;
}
mRxThreadID = 0;
}
close( mCommandPipe[WRITING] );
close( mCommandPipe[READING] );
mCommandPipe[READING] = INVALID_HANDLE_VALUE;
mCommandPipe[WRITING] = INVALID_HANDLE_VALUE;
}
if (mPort != INVALID_HANDLE_VALUE)
{
close( mPort );
mPort = INVALID_HANDLE_VALUE;
}
mPortName.clear();
return bRC;
}
/*===========================================================================
METHOD:
ConfigureSettings (Public Method)
DESCRIPTION:
Configure the port with the passed in parameters
PARAMETERS:
pSettings [ I ] - Desired port settings
RETURN VALUE:
bool
===========================================================================*/
bool cComm::ConfigureSettings( termios * pSettings )
{
if (mPort == INVALID_HANDLE_VALUE || pSettings == 0)
{
return false;
}
tcflush( mPort, TCIOFLUSH );
int nRC = tcsetattr( mPort, TCSANOW, pSettings );
if (nRC == -1)
{
return false;
}
// Success!
return true;
}
/*===========================================================================
METHOD:
GetSettings (Public Method)
DESCRIPTION:
Return the current port settings
PARAMETERS:
pSettings [ I ] - Current port settings
RETURN VALUE:
bool
===========================================================================*/
bool cComm::GetSettings( termios * pSettings )
{
if (mPort == INVALID_HANDLE_VALUE || pSettings == 0)
{
return false;
}
// Get the COM port settings
int nRC = tcgetattr( mPort, pSettings );
if (nRC == -1)
{
return false;
}
// Success!
return true;
}
/*===========================================================================
METHOD:
CancelIO (Public Method)
DESCRIPTION:
Cancel any in-progress I/O
PARAMETERS:
RETURN VALUE:
bool
===========================================================================*/
bool cComm::CancelIO()
{
if (mPort == INVALID_HANDLE_VALUE)
{
return false;
}
bool bRxCancel = CancelRx();
bool bTxCancel = CancelTx();
return (bRxCancel && bTxCancel);
}
/*===========================================================================
METHOD:
CancelRx (Public Method)
DESCRIPTION:
Cancel any in-progress receive operation
RETURN VALUE:
bool
===========================================================================*/
bool cComm::CancelRx()
{
if (mPort == INVALID_HANDLE_VALUE
|| mCommandPipe[WRITING] == INVALID_HANDLE_VALUE
|| mpRxCallback == 0
|| mRxThreadID == 0)
{
TRACE( "cannot cancel, thread not active\n" );
return false;
}
// Notify the thread to stop reading
BYTE byte = STOP_READ_CMD;
int nRC = write( mCommandPipe[WRITING], &byte, 1 );
if (nRC != 1)
{
TRACE( "error %d canceling read\n", nRC );
return false;
}
// Remove the old callback
mpRxCallback = 0;
return true;
}
/*===========================================================================
METHOD:
CancelTx (Public Method)
DESCRIPTION:
Cancel any in-progress transmit operation
RETURN VALUE:
bool
===========================================================================*/
bool cComm::CancelTx()
{
if (mPort == INVALID_HANDLE_VALUE)
{
return false;
}
mbCancelWrite = true;
return true;
}
/*===========================================================================
METHOD:
RxData (Public Method)
DESCRIPTION:
Receive data
PARAMETERS:
pBuf [ I ] - Buffer to contain received data
bufSz [ I ] - Amount of data to be received
pCallback [ I ] - Callback object to be exercised when the
operation completes
RETURN VALUE:
bool
===========================================================================*/
bool cComm::RxData(
BYTE * pBuf,
ULONG bufSz,
cIOCallback * pCallback )
{
if (IsValid() == false || mpRxCallback != 0)
{
return false;
}
if (pCallback == 0)
{
// Not interested in being notified, but we still need a value
// for this so that only one outstanding I/O operation is active
// at any given point in time
mpRxCallback = (cIOCallback * )1;
}
else
{
mpRxCallback = pCallback;
}
mpBuffer = pBuf;
mBuffSz = bufSz;
// Notify the thread to stop reading
BYTE byte = START_READ_CMD;
int nRC = write( mCommandPipe[WRITING], &byte, 1 );
if (nRC != 1)
{
TRACE( "error %d starting read\n", nRC );
return false;
}
return true;
}
/*===========================================================================
METHOD:
TxData (Public Method)
DESCRIPTION:
Transmit data
PARAMETERS:
pBuf [ I ] - Data to be transmitted
bufSz [ I ] - Amount of data to be transmitted
RETURN VALUE:
bool
===========================================================================*/
bool cComm::TxData(
const BYTE * pBuf,
ULONG bufSz )
{
if (IsValid() == false)
{
return false;
}
#ifdef DEBUG
ULONGLONG nStart = GetTickCount();
#endif
// Allow ourselves to be interupted
mbCancelWrite = false;
// This seems a bit pointless, but we're still going verify
// the device is ready for writing, and give it up to
// (1000 + num bytes) MS to be ready (in 100 MS chunks)
struct timeval TimeOut;
fd_set set;
int nReady = 0;
int nCount = 0;
while ( nReady == 0 )
{
if (mbCancelWrite == true)
{
TRACE( "cComm::TxData() write canceled before device was ready\n" );
return false;
}
if (nCount >= (1000 + bufSz) / 100)
{
// Timeout is expired
break;
}
FD_ZERO( &set );
FD_SET( mPort, &set );
TimeOut.tv_sec = 0;
TimeOut.tv_usec = 100000;
nReady = select( mPort + 1, NULL, &set, NULL, &TimeOut );
nCount++;
}
if (nReady <= 0)
{
TRACE( "cComm::TxData() Unable to get device ready for"
" Write, error %d: %s\n",
nReady,
strerror( nReady) );
return false;
}
int nRet = write( mPort, pBuf, bufSz );
if (nRet != bufSz)
{
TRACE( "cComm::TxData() write returned %d instead of %lu\n",
nRet,
bufSz );
return false;
}
#ifdef DEBUG
TRACE( "Write of %lu bytes took %llu miliseconds\n", bufSz, GetTickCount() - nStart );
#endif
return true;
}
|