Debian Codegraph @

gobi-api/Gobi_2011-11-28-1533/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) 2011, 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 break;
124 }
125 }
126 else if (FD_ISSET( pComm->mPort, &outputSet ) == true)
127 {
128 // Stop watching for read data
129 FD_CLR( pComm->mPort, &inputSet );
130 largestFD = pComm->mCommandPipe[READING];
131
132 // Perform a read
133 status = read( pComm->mPort,
134 pComm->mpBuffer,
135 pComm->mBuffSz );
136
137 cIOCallback * pCallback = pComm->mpRxCallback;
138 pComm->mpRxCallback = 0;
139
140 if (pCallback == (cIOCallback *)1)
141 {
142 // We wanted to read, but not to be notified
143 }
144 else if (status >= 0)
145 {
146 pCallback->IOComplete( 0, status );
147 }
148 else
149 {
150 pCallback->IOComplete( status, 0 );
151 }
152 }
153 }
154
155 return 0;
156 };
157
158 /*=========================================================================*/
159 // cComm Methods
160 /*=========================================================================*/
161
162 /*===========================================================================
163 METHOD:
164 cComm (Public Method)
165
166 DESCRIPTION:
167 Constructor
168
169 RETURN VALUE:
170 None
171 ===========================================================================*/
172 cComm::cComm()
173 : mPortName( "" ),
174 mPort( INVALID_HANDLE_VALUE ),
175 mpRxCallback( 0 ),
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 RunIOCTL (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::RunIOCTL(
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 %d\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 mPortName.clear();
356 return bRC;
357 }
358
359 /*===========================================================================
360 METHOD:
361 ConfigureSettings (Public Method)
362
363 DESCRIPTION:
364 Configure the port with the passed in parameters
365
366 PARAMETERS:
367 pSettings [ I ] - Desired port settings
368
369 RETURN VALUE:
370 bool
371 ===========================================================================*/
372 bool cComm::ConfigureSettings( termios * pSettings )
373 {
374 if (mPort == INVALID_HANDLE_VALUE || pSettings == 0)
375 {
376 return false;
377 }
378
379 tcflush( mPort, TCIOFLUSH );
380 int nRC = tcsetattr( mPort, TCSANOW, pSettings );
381 if (nRC == -1)
382 {
383 return false;
384 }
385
386 // Success!
387 return true;
388 }
389
390 /*===========================================================================
391 METHOD:
392 GetSettings (Public Method)
393
394 DESCRIPTION:
395 Return the current port settings
396
397 PARAMETERS:
398 pSettings [ I ] - Current port settings
399
400 RETURN VALUE:
401 bool
402 ===========================================================================*/
403 bool cComm::GetSettings( termios * pSettings )
404 {
405 if (mPort == INVALID_HANDLE_VALUE || pSettings == 0)
406 {
407 return false;
408 }
409
410 // Get the COM port settings
411 int nRC = tcgetattr( mPort, pSettings );
412 if (nRC == -1)
413 {
414 return false;
415 }
416
417 // Success!
418 return true;
419 }
420
421 /*===========================================================================
422 METHOD:
423 CancelIO (Public Method)
424
425 DESCRIPTION:
426 Cancel any in-progress I/O
427
428 PARAMETERS:
429
430 RETURN VALUE:
431 bool
432 ===========================================================================*/
433 bool cComm::CancelIO()
434 {
435 if (mPort == INVALID_HANDLE_VALUE)
436 {
437 return false;
438 }
439
440 bool bRxCancel = CancelRx();
441 bool bTxCancel = CancelTx();
442
443 return (bRxCancel && bTxCancel);
444 }
445
446 /*===========================================================================
447 METHOD:
448 CancelRx (Public Method)
449
450 DESCRIPTION:
451 Cancel any in-progress receive operation
452
453 RETURN VALUE:
454 bool
455 ===========================================================================*/
456 bool cComm::CancelRx()
457 {
458 if (mPort == INVALID_HANDLE_VALUE
459 || mCommandPipe[WRITING] == INVALID_HANDLE_VALUE
460 || mpRxCallback == 0
461 || mRxThreadID == 0)
462 {
463 TRACE( "cannot cancel, thread not active\n" );
464 return false;
465 }
466
467 // Notify the thread to stop reading
468 BYTE byte = STOP_READ_CMD;
469 int nRC = write( mCommandPipe[WRITING], &byte, 1 );
470 if (nRC != 1)
471 {
472 TRACE( "error %d canceling read\n", nRC );
473 return false;
474 }
475
476 // Remove the old callback
477 mpRxCallback = 0;
478
479 return true;
480 }
481
482 /*===========================================================================
483 METHOD:
484 CancelTx (Public Method)
485
486 DESCRIPTION:
487 Cancel any in-progress transmit operation
488
489 RETURN VALUE:
490 bool
491 ===========================================================================*/
492 bool cComm::CancelTx()
493 {
494 if (mPort == INVALID_HANDLE_VALUE)
495 {
496 return false;
497 }
498
499 mbCancelWrite = true;
500
501 return true;
502 }
503
504 /*===========================================================================
505 METHOD:
506 RxData (Public Method)
507
508 DESCRIPTION:
509 Receive data
510
511 PARAMETERS:
512 pBuf [ I ] - Buffer to contain received data
513 bufSz [ I ] - Amount of data to be received
514 pCallback [ I ] - Callback object to be exercised when the
515 operation completes
516
517 RETURN VALUE:
518 bool
519 ===========================================================================*/
520 bool cComm::RxData(
521 BYTE * pBuf,
522 ULONG bufSz,
523 cIOCallback * pCallback )
524 {
525 if (IsValid() == false || mpRxCallback != 0)
526 {
527 return false;
528 }
529
530 if (pCallback == 0)
531 {
532 // Not interested in being notified, but we still need a value
533 // for this so that only one outstanding I/O operation is active
534 // at any given point in time
535 mpRxCallback = (cIOCallback * )1;
536 }
537 else
538 {
539 mpRxCallback = pCallback;
540 }
541
542 mpBuffer = pBuf;
543 mBuffSz = bufSz;
544
545 // Notify the thread to stop reading
546 BYTE byte = START_READ_CMD;
547 int nRC = write( mCommandPipe[WRITING], &byte, 1 );
548 if (nRC != 1)
549 {
550 TRACE( "error %d starting read\n", nRC );
551 return false;
552 }
553
554 return true;
555 }
556
557 /*===========================================================================
558 METHOD:
559 TxData (Public Method)
560
561 DESCRIPTION:
562 Transmit data
563
564 PARAMETERS:
565 pBuf [ I ] - Data to be transmitted
566 bufSz [ I ] - Amount of data to be transmitted
567
568 RETURN VALUE:
569 bool
570 ===========================================================================*/
571 bool cComm::TxData(
572 const BYTE * pBuf,
573 ULONG bufSz )
574 {
575 if (IsValid() == false)
576 {
577 return false;
578 }
579
580 #ifdef DEBUG
581 ULONGLONG nStart = GetTickCount();
582 #endif
583
584 // Allow ourselves to be interupted
585 mbCancelWrite = false;
586
587 // This seems a bit pointless, but we're still going verify
588 // the device is ready for writing, and give it up to
589 // (1000 + num bytes) MS to be ready (in 100 MS chunks)
590
591 struct timeval TimeOut;
592 fd_set set;
593
594 int nReady = 0;
595 int nCount = 0;
596
597 while ( nReady == 0 )
598 {
599 if (mbCancelWrite == true)
600 {
601 TRACE( "cComm::TxData() write canceled before device was ready\n" );
602 return false;
603 }
604
605 if (nCount >= (1000 + bufSz) / 100)
606 {
607 // Timeout is expired
608 break;
609 }
610
611 FD_ZERO( &set );
612 FD_SET( mPort, &set );
613 TimeOut.tv_sec = 0;
614 TimeOut.tv_usec = 100000;
615 nReady = select( mPort + 1, NULL, &set, NULL, &TimeOut );
616
617 nCount++;
618 }
619
620 if (nReady <= 0)
621 {
622 TRACE( "cComm::TxData() Unable to get device ready for"
623 " Write, error %d: %s\n",
624 nReady,
625 strerror( nReady) );
626 return false;
627 }
628
629 int nRet = write( mPort, pBuf, bufSz );
630 if (nRet != bufSz)
631 {
632 TRACE( "cComm::TxData() write returned %d instead of %lu\n",
633 nRet,
634 bufSz );
635 return false;
636 }
637
638 #ifdef DEBUG
639 TRACE( "Write of %lu bytes took %llu miliseconds\n", bufSz, GetTickCount() - nStart );
640 #endif
641
642 return true;
643 }