Debian Codegraph @

gobi-api/GobiAPI_2012-07-12-1036/Core/Comm.cpp

raw
1 /*===========================================================================
2 FILE:
3 Comm.cpp
4
5 DESCRIPTION:
6 Implementation of cComm class
7
8 PUBLIC CLASSES AND METHODS:
9 cComm
10 This class wraps low level port communications
11
12 Copyright (c) 2012, Code Aurora Forum. All rights reserved.
13
14 Redistribution and use in source and binary forms, with or without
15 modification, are permitted provided that the following conditions are met:
16 * Redistributions of source code must retain the above copyright
17 notice, this list of conditions and the following disclaimer.
18 * Redistributions in binary form must reproduce the above copyright
19 notice, this list of conditions and the following disclaimer in the
20 documentation and/or other materials provided with the distribution.
21 * Neither the name of Code Aurora Forum nor
22 the names of its contributors may be used to endorse or promote
23 products derived from this software without specific prior written
24 permission.
25
26
27 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 POSSIBILITY OF SUCH DAMAGE.
38 ===========================================================================*/
39
40 //---------------------------------------------------------------------------
41 // Include Files
42 //---------------------------------------------------------------------------
43 #include "StdAfx.h"
44 #include "Comm.h"
45 #include "ProtocolServer.h"
46
47 //---------------------------------------------------------------------------
48 // Definitions
49 //---------------------------------------------------------------------------
50 // Thread commands
51 #define START_READ_CMD 0
52 #define STOP_READ_CMD 1
53 #define EXIT_CMD 2
54
55 /*=========================================================================*/
56 // Free Methods
57 /*=========================================================================*/
58
59 /*===========================================================================
60 METHOD:
61 RxThread (Free Method)
62
63 DESCRIPTION:
64 Thread for simulating asynchronous reads
65
66 PARAMETERS:
67 pData [ I ] Asynchronous read object
68
69 RETURN VALUE:
70 void * - thread exit value (always 0)
71 ===========================================================================*/
72 void * RxThread( void * pData )
73 {
74 cComm * pComm = (cComm*)pData;
75 if (pComm == NULL || pComm->IsValid() == false)
76 {
77 return 0;
78 }
79
80 fd_set inputSet, outputSet;
81 FD_ZERO( &inputSet );
82 FD_SET( pComm->mCommandPipe[READING], &inputSet );
83 int largestFD = pComm->mCommandPipe[READING];
84
85 int status = 0;
86 while (true)
87 {
88 // No FD_COPY() available
89 memcpy( &outputSet, &inputSet, sizeof( fd_set ) );
90
91 status = select( largestFD + 1, &outputSet, NULL, NULL, NULL );
92 if (status <= 0)
93 {
94 TRACE( "error %d in select, errno %d\n", status, errno );
95 break;
96 }
97
98 if (FD_ISSET( pComm->mCommandPipe[READING], &outputSet ) == true)
99 {
100 // Read from the pipe
101 BYTE cmd;
102 status = read( pComm->mCommandPipe[READING], &cmd, 1 );
103 if (status != 1)
104 {
105 TRACE( "cmd error %d\n", status );
106 break;
107 }
108
109 if (cmd == START_READ_CMD)
110 {
111 FD_SET( pComm->mPort, &inputSet );
112 largestFD = std::max( pComm->mPort,
113 pComm->mCommandPipe[READING] );
114 }
115 else if (cmd == STOP_READ_CMD)
116 {
117 FD_CLR( pComm->mPort, &inputSet );
118 largestFD = pComm->mCommandPipe[READING];
119 }
120 else
121 {
122 // EXIT_CMD or anything else
123 pComm->mpRxCallback = 0;
124 break;
125 }
126 }
127 else if (FD_ISSET( pComm->mPort, &outputSet ) == true)
128 {
129 // Stop watching for read data
130 FD_CLR( pComm->mPort, &inputSet );
131 largestFD = pComm->mCommandPipe[READING];
132
133 // Perform a read
134 status = read( pComm->mPort,
135 pComm->mpBuffer,
136 pComm->mBuffSz );
137
138 cIOCallback * pCallback = pComm->mpRxCallback;
139 pComm->mpRxCallback = 0;
140
141 if (pCallback == (cIOCallback *)1)
142 {
143 // We wanted to read, but not to be notified
144 }
145 else if (status >= 0)
146 {
147 pCallback->IOComplete( 0, status );
148 }
149 else
150 {
151 pCallback->IOComplete( status, 0 );
152 }
153 }
154 }
155
156 return 0;
157 };
158
159 /*=========================================================================*/
160 // cComm Methods
161 /*=========================================================================*/
162
163 /*===========================================================================
164 METHOD:
165 cComm (Public Method)
166
167 DESCRIPTION:
168 Constructor
169
170 RETURN VALUE:
171 None
172 ===========================================================================*/
173 cComm::cComm()
174 : mPortName( "" ),
175 mPort( INVALID_HANDLE_VALUE ),
176 mbCancelWrite( false ),
177 mpBuffer( 0 ),
178 mBuffSz( 0 ),
179 mRxThreadID( 0 )
180 {
181 mCommandPipe[READING] = INVALID_HANDLE_VALUE;
182 mCommandPipe[WRITING] = INVALID_HANDLE_VALUE;
183 }
184
185 /*===========================================================================
186 METHOD:
187 ~cComm (Public Method)
188
189 DESCRIPTION:
190 Destructor
191
192 RETURN VALUE:
193 None
194 ===========================================================================*/
195 cComm::~cComm()
196 {
197 // Disconnect from current port
198 Disconnect();
199
200 mCommandPipe[READING] = INVALID_HANDLE_VALUE;
201 mCommandPipe[WRITING] = INVALID_HANDLE_VALUE;
202 }
203
204 /*===========================================================================
205 METHOD:
206 IsValid (Public Method)
207
208 DESCRIPTION:
209 Is this object valid?
210
211 RETURN VALUE:
212 Bool
213 ===========================================================================*/
214 bool cComm::IsValid()
215 {
216 // Nothing to do, dependant on extended class functionality
217 return true;
218 }
219
220 /*===========================================================================
221 METHOD:
222 Connect (Public Method)
223
224 DESCRIPTION:
225 Connect to the specified port
226
227 PARAMETERS:
228 pPort [ I ] - Name of port to open (IE: /dev/qcqmi0)
229
230 RETURN VALUE:
231 bool
232 ===========================================================================*/
233 bool cComm::Connect( LPCSTR pPort )
234 {
235 if (IsValid() == false || pPort == 0 || pPort[0] == 0)
236 {
237 return false;
238 }
239
240 if (mPort != INVALID_HANDLE_VALUE)
241 {
242 Disconnect();
243 }
244
245 // Initialize command pipe for read thread
246 int nRet = pipe( mCommandPipe );
247 if (nRet != 0)
248 {
249 TRACE( "cComm:Connect() pipe creation failed %d\n", nRet );
250 return false;
251 }
252
253 // Start the read thread
254 nRet = pthread_create( &mRxThreadID,
255 0,
256 RxThread,
257 this );
258 if (nRet != 0)
259 {
260 TRACE( "cComm::Connect() pthread_create = %d\n", nRet );
261
262 Disconnect();
263 return false;
264 }
265
266 // Opening the com port
267 mPort = open( pPort, O_RDWR );
268 if (mPort == INVALID_HANDLE_VALUE)
269 {
270 Disconnect();
271 return false;
272 }
273
274 // Save port name
275 mPortName = pPort;
276
277 // Success!
278 return true;
279 }
280
281 /*===========================================================================
282 METHOD:
283 SendCtl (Public Method)
284
285 DESCRIPTION:
286 Run an IOCTL on the open file handle
287
288 PARAMETERS:
289 ioctlReq [ I ] - ioctl request value
290 pData [I/O] - input or output specific to ioctl request value
291
292 RETURN VALUE:
293 int - ioctl return value (0 for success)
294 ===========================================================================*/
295 int cComm::SendCtl(
296 UINT ioctlReq,
297 void * pData )
298 {
299 if (mPort == INVALID_HANDLE_VALUE)
300 {
301 TRACE( "Invalid file handle\n" );
302 return -EBADFD;
303 }
304
305 return ioctl( mPort, ioctlReq, pData );
306 }
307
308 /*===========================================================================
309 METHOD:
310 Disconnect (Public Method)
311
312 DESCRIPTION:
313 Disconnect from the current port
314
315 RETURN VALUE:
316 bool
317 ===========================================================================*/
318 bool cComm::Disconnect()
319 {
320 // Assume success
321 bool bRC = true;
322
323 if (mCommandPipe[WRITING] != INVALID_HANDLE_VALUE)
324 {
325 if (mRxThreadID != 0)
326 {
327 // Notify the thread to exit
328 BYTE byte = EXIT_CMD;
329 write( mCommandPipe[WRITING], &byte, 1 );
330
331 // And wait for it
332 TRACE( "cComm::Disconnnect() joining thread %lu\n", mRxThreadID );
333 int nRC = pthread_join( mRxThreadID, 0 );
334 if (nRC != 0)
335 {
336 TRACE( "failed to join thread %d\n", nRC );
337 bRC = false;
338 }
339
340 mRxThreadID = 0;
341 }
342
343 close( mCommandPipe[WRITING] );
344 close( mCommandPipe[READING] );
345 mCommandPipe[READING] = INVALID_HANDLE_VALUE;
346 mCommandPipe[WRITING] = INVALID_HANDLE_VALUE;
347 }
348
349 if (mPort != INVALID_HANDLE_VALUE)
350 {
351 close( mPort );
352 mPort = INVALID_HANDLE_VALUE;
353 }
354
355 // Double check
356 mpRxCallback = 0;
357
358 mPortName.clear();
359 return bRC;
360 }
361
362 /*===========================================================================
363 METHOD:
364 ConfigureSettings (Public Method)
365
366 DESCRIPTION:
367 Configure the port with the passed in parameters
368
369 PARAMETERS:
370 pSettings [ I ] - Desired port settings
371
372 RETURN VALUE:
373 bool
374 ===========================================================================*/
375 bool cComm::ConfigureSettings( termios * pSettings )
376 {
377 if (mPort == INVALID_HANDLE_VALUE || pSettings == 0)
378 {
379 return false;
380 }
381
382 tcflush( mPort, TCIOFLUSH );
383 int nRC = tcsetattr( mPort, TCSANOW, pSettings );
384 if (nRC == -1)
385 {
386 return false;
387 }
388
389 // Success!
390 return true;
391 }
392
393 /*===========================================================================
394 METHOD:
395 GetSettings (Public Method)
396
397 DESCRIPTION:
398 Return the current port settings
399
400 PARAMETERS:
401 pSettings [ I ] - Current port settings
402
403 RETURN VALUE:
404 bool
405 ===========================================================================*/
406 bool cComm::GetSettings( termios * pSettings )
407 {
408 if (mPort == INVALID_HANDLE_VALUE || pSettings == 0)
409 {
410 return false;
411 }
412
413 // Get the COM port settings
414 int nRC = tcgetattr( mPort, pSettings );
415 if (nRC == -1)
416 {
417 return false;
418 }
419
420 // Success!
421 return true;
422 }
423
424 /*===========================================================================
425 METHOD:
426 CancelIO (Public Method)
427
428 DESCRIPTION:
429 Cancel any in-progress I/O
430
431 PARAMETERS:
432
433 RETURN VALUE:
434 bool
435 ===========================================================================*/
436 bool cComm::CancelIO()
437 {
438 if (mPort == INVALID_HANDLE_VALUE)
439 {
440 return false;
441 }
442
443 bool bRxCancel = CancelRx();
444 bool bTxCancel = CancelTx();
445
446 return (bRxCancel && bTxCancel);
447 }
448
449 /*===========================================================================
450 METHOD:
451 CancelRx (Public Method)
452
453 DESCRIPTION:
454 Cancel any in-progress receive operation
455
456 RETURN VALUE:
457 bool
458 ===========================================================================*/
459 bool cComm::CancelRx()
460 {
461 if (mPort == INVALID_HANDLE_VALUE
462 || mCommandPipe[WRITING] == INVALID_HANDLE_VALUE
463 || mpRxCallback == 0
464 || mRxThreadID == 0)
465 {
466 TRACE( "cannot cancel, thread not active\n" );
467 mpRxCallback = 0;
468 return false;
469 }
470
471 // Notify the thread to stop reading
472 BYTE byte = STOP_READ_CMD;
473 int nRC = write( mCommandPipe[WRITING], &byte, 1 );
474 if (nRC != 1)
475 {
476 TRACE( "error %d canceling read\n", nRC );
477 return false;
478 }
479
480 // Remove the old callback
481 mpRxCallback = 0;
482
483 return true;
484 }
485
486 /*===========================================================================
487 METHOD:
488 CancelTx (Public Method)
489
490 DESCRIPTION:
491 Cancel any in-progress transmit operation
492
493 RETURN VALUE:
494 bool
495 ===========================================================================*/
496 bool cComm::CancelTx()
497 {
498 if (mPort == INVALID_HANDLE_VALUE)
499 {
500 return false;
501 }
502
503 mbCancelWrite = true;
504
505 return true;
506 }
507
508 /*===========================================================================
509 METHOD:
510 RxData (Public Method)
511
512 DESCRIPTION:
513 Receive data
514
515 PARAMETERS:
516 pBuf [ I ] - Buffer to contain received data
517 bufSz [ I ] - Amount of data to be received
518 pCallback [ I ] - Callback object to be exercised when the
519 operation completes
520
521 RETURN VALUE:
522 bool
523 ===========================================================================*/
524 bool cComm::RxData(
525 BYTE * pBuf,
526 ULONG bufSz,
527 cIOCallback * pCallback )
528 {
529 if (IsValid() == false || mpRxCallback != 0)
530 {
531 return false;
532 }
533
534 if (pCallback == 0)
535 {
536 // Not interested in being notified, but we still need a value
537 // for this so that only one outstanding I/O operation is active
538 // at any given point in time
539 mpRxCallback = (cIOCallback * )1;
540 }
541 else
542 {
543 mpRxCallback = pCallback;
544 }
545
546 mpBuffer = pBuf;
547 mBuffSz = bufSz;
548
549 // Notify the thread to stop reading
550 BYTE byte = START_READ_CMD;
551 int nRC = write( mCommandPipe[WRITING], &byte, 1 );
552 if (nRC != 1)
553 {
554 TRACE( "error %d starting read\n", nRC );
555 return false;
556 }
557
558 return true;
559 }
560
561 /*===========================================================================
562 METHOD:
563 TxData (Public Method)
564
565 DESCRIPTION:
566 Transmit data
567
568 PARAMETERS:
569 pBuf [ I ] - Data to be transmitted
570 bufSz [ I ] - Amount of data to be transmitted
571
572 RETURN VALUE:
573 bool
574 ===========================================================================*/
575 bool cComm::TxData(
576 const BYTE * pBuf,
577 ULONG bufSz )
578 {
579 if (IsValid() == false)
580 {
581 return false;
582 }
583
584 #ifdef DEBUG
585 ULONGLONG nStart = GetTickCount();
586 #endif
587
588 // Allow ourselves to be interupted
589 mbCancelWrite = false;
590
591 // This seems a bit pointless, but we're still going verify
592 // the device is ready for writing, and give it up to
593 // (1000 + num bytes) MS to be ready (in 100 MS chunks)
594
595 struct timeval TimeOut;
596 fd_set set;
597
598 int nReady = 0;
599 int nCount = 0;
600
601 while ( nReady == 0 )
602 {
603 if (mbCancelWrite == true)
604 {
605 TRACE( "cComm::TxData() write canceled before device was ready\n" );
606 return false;
607 }
608
609 if (nCount >= (1000 + bufSz) / 100)
610 {
611 // Timeout is expired
612 break;
613 }
614
615 FD_ZERO( &set );
616 FD_SET( mPort, &set );
617 TimeOut.tv_sec = 0;
618 TimeOut.tv_usec = 100000;
619 nReady = select( mPort + 1, NULL, &set, NULL, &TimeOut );
620
621 nCount++;
622 }
623
624 if (nReady <= 0)
625 {
626 TRACE( "cComm::TxData() Unable to get device ready for"
627 " Write, error %d: %s\n",
628 nReady,
629 strerror( nReady) );
630 return false;
631 }
632
633 int nRet = write( mPort, pBuf, bufSz );
634 if (nRet != bufSz)
635 {
636 TRACE( "cComm::TxData() write returned %d instead of %lu\n",
637 nRet,
638 bufSz );
639 return false;
640 }
641
642 #ifdef DEBUG
643 TRACE( "Write of %lu bytes took %llu miliseconds\n", bufSz, GetTickCount() - nStart );
644 #endif
645
646 return true;
647 }