Debian Codegraph @

gobi-api/Gobi_2012-06-18-1054/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 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 mbCancelWrite( false ),
176 mpBuffer( 0 ),
177 mBuffSz( 0 ),
178 mRxThreadID( 0 )
179 {
180 mCommandPipe[READING] = INVALID_HANDLE_VALUE;
181 mCommandPipe[WRITING] = INVALID_HANDLE_VALUE;
182 }
183
184 /*===========================================================================
185 METHOD:
186 ~cComm (Public Method)
187
188 DESCRIPTION:
189 Destructor
190
191 RETURN VALUE:
192 None
193 ===========================================================================*/
194 cComm::~cComm()
195 {
196 // Disconnect from current port
197 Disconnect();
198
199 mCommandPipe[READING] = INVALID_HANDLE_VALUE;
200 mCommandPipe[WRITING] = INVALID_HANDLE_VALUE;
201 }
202
203 /*===========================================================================
204 METHOD:
205 IsValid (Public Method)
206
207 DESCRIPTION:
208 Is this object valid?
209
210 RETURN VALUE:
211 Bool
212 ===========================================================================*/
213 bool cComm::IsValid()
214 {
215 // Nothing to do, dependant on extended class functionality
216 return true;
217 }
218
219 /*===========================================================================
220 METHOD:
221 Connect (Public Method)
222
223 DESCRIPTION:
224 Connect to the specified port
225
226 PARAMETERS:
227 pPort [ I ] - Name of port to open (IE: /dev/qcqmi0)
228
229 RETURN VALUE:
230 bool
231 ===========================================================================*/
232 bool cComm::Connect( LPCSTR pPort )
233 {
234 if (IsValid() == false || pPort == 0 || pPort[0] == 0)
235 {
236 return false;
237 }
238
239 if (mPort != INVALID_HANDLE_VALUE)
240 {
241 Disconnect();
242 }
243
244 // Initialize command pipe for read thread
245 int nRet = pipe( mCommandPipe );
246 if (nRet != 0)
247 {
248 TRACE( "cComm:Connect() pipe creation failed %d\n", nRet );
249 return false;
250 }
251
252 // Start the read thread
253 nRet = pthread_create( &mRxThreadID,
254 0,
255 RxThread,
256 this );
257 if (nRet != 0)
258 {
259 TRACE( "cComm::Connect() pthread_create = %d\n", nRet );
260
261 Disconnect();
262 return false;
263 }
264
265 // Opening the com port
266 mPort = open( pPort, O_RDWR );
267 if (mPort == INVALID_HANDLE_VALUE)
268 {
269 Disconnect();
270 return false;
271 }
272
273 // Save port name
274 mPortName = pPort;
275
276 // Success!
277 return true;
278 }
279
280 /*===========================================================================
281 METHOD:
282 SendCtl (Public Method)
283
284 DESCRIPTION:
285 Run an IOCTL on the open file handle
286
287 PARAMETERS:
288 ioctlReq [ I ] - ioctl request value
289 pData [I/O] - input or output specific to ioctl request value
290
291 RETURN VALUE:
292 int - ioctl return value (0 for success)
293 ===========================================================================*/
294 int cComm::SendCtl(
295 UINT ioctlReq,
296 void * pData )
297 {
298 if (mPort == INVALID_HANDLE_VALUE)
299 {
300 TRACE( "Invalid file handle\n" );
301 return -EBADFD;
302 }
303
304 return ioctl( mPort, ioctlReq, pData );
305 }
306
307 /*===========================================================================
308 METHOD:
309 Disconnect (Public Method)
310
311 DESCRIPTION:
312 Disconnect from the current port
313
314 RETURN VALUE:
315 bool
316 ===========================================================================*/
317 bool cComm::Disconnect()
318 {
319 // Assume success
320 bool bRC = true;
321
322 if (mCommandPipe[WRITING] != INVALID_HANDLE_VALUE)
323 {
324 if (mRxThreadID != 0)
325 {
326 // Notify the thread to exit
327 BYTE byte = EXIT_CMD;
328 write( mCommandPipe[WRITING], &byte, 1 );
329
330 // And wait for it
331 TRACE( "cComm::Disconnnect() joining thread %lu\n", mRxThreadID );
332 int nRC = pthread_join( mRxThreadID, 0 );
333 if (nRC != 0)
334 {
335 TRACE( "failed to join thread %d\n", nRC );
336 bRC = false;
337 }
338
339 mRxThreadID = 0;
340 }
341
342 close( mCommandPipe[WRITING] );
343 close( mCommandPipe[READING] );
344 mCommandPipe[READING] = INVALID_HANDLE_VALUE;
345 mCommandPipe[WRITING] = INVALID_HANDLE_VALUE;
346 }
347
348 if (mPort != INVALID_HANDLE_VALUE)
349 {
350 close( mPort );
351 mPort = INVALID_HANDLE_VALUE;
352 }
353
354 mPortName.clear();
355 return bRC;
356 }
357
358 /*===========================================================================
359 METHOD:
360 ConfigureSettings (Public Method)
361
362 DESCRIPTION:
363 Configure the port with the passed in parameters
364
365 PARAMETERS:
366 pSettings [ I ] - Desired port settings
367
368 RETURN VALUE:
369 bool
370 ===========================================================================*/
371 bool cComm::ConfigureSettings( termios * pSettings )
372 {
373 if (mPort == INVALID_HANDLE_VALUE || pSettings == 0)
374 {
375 return false;
376 }
377
378 tcflush( mPort, TCIOFLUSH );
379 int nRC = tcsetattr( mPort, TCSANOW, pSettings );
380 if (nRC == -1)
381 {
382 return false;
383 }
384
385 // Success!
386 return true;
387 }
388
389 /*===========================================================================
390 METHOD:
391 GetSettings (Public Method)
392
393 DESCRIPTION:
394 Return the current port settings
395
396 PARAMETERS:
397 pSettings [ I ] - Current port settings
398
399 RETURN VALUE:
400 bool
401 ===========================================================================*/
402 bool cComm::GetSettings( termios * pSettings )
403 {
404 if (mPort == INVALID_HANDLE_VALUE || pSettings == 0)
405 {
406 return false;
407 }
408
409 // Get the COM port settings
410 int nRC = tcgetattr( mPort, pSettings );
411 if (nRC == -1)
412 {
413 return false;
414 }
415
416 // Success!
417 return true;
418 }
419
420 /*===========================================================================
421 METHOD:
422 CancelIO (Public Method)
423
424 DESCRIPTION:
425 Cancel any in-progress I/O
426
427 PARAMETERS:
428
429 RETURN VALUE:
430 bool
431 ===========================================================================*/
432 bool cComm::CancelIO()
433 {
434 if (mPort == INVALID_HANDLE_VALUE)
435 {
436 return false;
437 }
438
439 bool bRxCancel = CancelRx();
440 bool bTxCancel = CancelTx();
441
442 return (bRxCancel && bTxCancel);
443 }
444
445 /*===========================================================================
446 METHOD:
447 CancelRx (Public Method)
448
449 DESCRIPTION:
450 Cancel any in-progress receive operation
451
452 RETURN VALUE:
453 bool
454 ===========================================================================*/
455 bool cComm::CancelRx()
456 {
457 if (mPort == INVALID_HANDLE_VALUE
458 || mCommandPipe[WRITING] == INVALID_HANDLE_VALUE
459 || mpRxCallback == 0
460 || mRxThreadID == 0)
461 {
462 TRACE( "cannot cancel, thread not active\n" );
463 return false;
464 }
465
466 // Notify the thread to stop reading
467 BYTE byte = STOP_READ_CMD;
468 int nRC = write( mCommandPipe[WRITING], &byte, 1 );
469 if (nRC != 1)
470 {
471 TRACE( "error %d canceling read\n", nRC );
472 return false;
473 }
474
475 // Remove the old callback
476 mpRxCallback = 0;
477
478 return true;
479 }
480
481 /*===========================================================================
482 METHOD:
483 CancelTx (Public Method)
484
485 DESCRIPTION:
486 Cancel any in-progress transmit operation
487
488 RETURN VALUE:
489 bool
490 ===========================================================================*/
491 bool cComm::CancelTx()
492 {
493 if (mPort == INVALID_HANDLE_VALUE)
494 {
495 return false;
496 }
497
498 mbCancelWrite = true;
499
500 return true;
501 }
502
503 /*===========================================================================
504 METHOD:
505 RxData (Public Method)
506
507 DESCRIPTION:
508 Receive data
509
510 PARAMETERS:
511 pBuf [ I ] - Buffer to contain received data
512 bufSz [ I ] - Amount of data to be received
513 pCallback [ I ] - Callback object to be exercised when the
514 operation completes
515
516 RETURN VALUE:
517 bool
518 ===========================================================================*/
519 bool cComm::RxData(
520 BYTE * pBuf,
521 ULONG bufSz,
522 cIOCallback * pCallback )
523 {
524 if (IsValid() == false || mpRxCallback != 0)
525 {
526 return false;
527 }
528
529 if (pCallback == 0)
530 {
531 // Not interested in being notified, but we still need a value
532 // for this so that only one outstanding I/O operation is active
533 // at any given point in time
534 mpRxCallback = (cIOCallback * )1;
535 }
536 else
537 {
538 mpRxCallback = pCallback;
539 }
540
541 mpBuffer = pBuf;
542 mBuffSz = bufSz;
543
544 // Notify the thread to stop reading
545 BYTE byte = START_READ_CMD;
546 int nRC = write( mCommandPipe[WRITING], &byte, 1 );
547 if (nRC != 1)
548 {
549 TRACE( "error %d starting read\n", nRC );
550 return false;
551 }
552
553 return true;
554 }
555
556 /*===========================================================================
557 METHOD:
558 TxData (Public Method)
559
560 DESCRIPTION:
561 Transmit data
562
563 PARAMETERS:
564 pBuf [ I ] - Data to be transmitted
565 bufSz [ I ] - Amount of data to be transmitted
566
567 RETURN VALUE:
568 bool
569 ===========================================================================*/
570 bool cComm::TxData(
571 const BYTE * pBuf,
572 ULONG bufSz )
573 {
574 if (IsValid() == false)
575 {
576 return false;
577 }
578
579 #ifdef DEBUG
580 ULONGLONG nStart = GetTickCount();
581 #endif
582
583 // Allow ourselves to be interupted
584 mbCancelWrite = false;
585
586 // This seems a bit pointless, but we're still going verify
587 // the device is ready for writing, and give it up to
588 // (1000 + num bytes) MS to be ready (in 100 MS chunks)
589
590 struct timeval TimeOut;
591 fd_set set;
592
593 int nReady = 0;
594 int nCount = 0;
595
596 while ( nReady == 0 )
597 {
598 if (mbCancelWrite == true)
599 {
600 TRACE( "cComm::TxData() write canceled before device was ready\n" );
601 return false;
602 }
603
604 if (nCount >= (1000 + bufSz) / 100)
605 {
606 // Timeout is expired
607 break;
608 }
609
610 FD_ZERO( &set );
611 FD_SET( mPort, &set );
612 TimeOut.tv_sec = 0;
613 TimeOut.tv_usec = 100000;
614 nReady = select( mPort + 1, NULL, &set, NULL, &TimeOut );
615
616 nCount++;
617 }
618
619 if (nReady <= 0)
620 {
621 TRACE( "cComm::TxData() Unable to get device ready for"
622 " Write, error %d: %s\n",
623 nReady,
624 strerror( nReady) );
625 return false;
626 }
627
628 int nRet = write( mPort, pBuf, bufSz );
629 if (nRet != bufSz)
630 {
631 TRACE( "cComm::TxData() write returned %d instead of %lu\n",
632 nRet,
633 bufSz );
634 return false;
635 }
636
637 #ifdef DEBUG
638 TRACE( "Write of %lu bytes took %llu miliseconds\n", bufSz, GetTickCount() - nStart );
639 #endif
640
641 return true;
642 }