| 1 |
/*=========================================================================== |
| 2 |
FILE: |
| 3 |
Socket.cpp |
| 4 |
|
| 5 |
DESCRIPTION: |
| 6 |
Implementation of cSocket class |
| 7 |
|
| 8 |
PUBLIC CLASSES AND METHODS: |
| 9 |
cSocket |
| 10 |
This class wraps low level communication to qmuxd |
| 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 "Socket.h" |
| 45 |
#include "ProtocolServer.h" |
| 46 |
#include <sys/socket.h> |
| 47 |
#include <sys/un.h> |
| 48 |
|
| 49 |
//--------------------------------------------------------------------------- |
| 50 |
// Definitions |
| 51 |
//--------------------------------------------------------------------------- |
| 52 |
// Thread commands |
| 53 |
#define START_READ_CMD 0 |
| 54 |
#define STOP_READ_CMD 1 |
| 55 |
#define EXIT_CMD 2 |
| 56 |
|
| 57 |
// Size of the QMUXD command payload |
| 58 |
// GET_CLIENT_ID and RELEASE_CLIENT_ID must pass in a buffer of this size |
| 59 |
#define PAYLOAD_SIZE 664 |
| 60 |
|
| 61 |
/*=========================================================================*/ |
| 62 |
// struct sQMUXDHeader |
| 63 |
/*=========================================================================*/ |
| 64 |
#pragma pack( push, 1 ) |
| 65 |
|
| 66 |
struct sQMUXDHeader |
| 67 |
{ |
| 68 |
/* Total size of header and following buffer */ |
| 69 |
int mTotalSize; |
| 70 |
|
| 71 |
/* QMUXD client ID */ |
| 72 |
int mQMUXDClientID; |
| 73 |
|
| 74 |
/* Message type */ |
| 75 |
eQMUXDMessageTypes mQMUXDMsgID; |
| 76 |
|
| 77 |
/* Duplicate of mQMUXDClientID */ |
| 78 |
int mQMUXDClientIDDuplicate; |
| 79 |
|
| 80 |
/* Transaction ID */ |
| 81 |
unsigned long mTxID; |
| 82 |
|
| 83 |
/* System error code */ |
| 84 |
int mSysErrCode; |
| 85 |
|
| 86 |
/* QMI error code (duplicate of TLV 0x02) */ |
| 87 |
int mQmiErrCode; |
| 88 |
|
| 89 |
/* SMD channel. 0 = SMD_DATA_5 */ |
| 90 |
int mQMUXDConectionType; |
| 91 |
|
| 92 |
/* QMI service ID */ |
| 93 |
int mQMUXServiceID; |
| 94 |
|
| 95 |
/* QMI client ID */ |
| 96 |
unsigned char mQMUXClientID; |
| 97 |
|
| 98 |
/* QMI flags */ |
| 99 |
unsigned char mRxFlags; |
| 100 |
|
| 101 |
/* In QMUXD this struct is not packed, so the compiler appends |
| 102 |
these two bytes */ |
| 103 |
unsigned short int mMissing2Bytes; |
| 104 |
}; |
| 105 |
|
| 106 |
#pragma pack( pop ) |
| 107 |
|
| 108 |
|
| 109 |
/*=========================================================================*/ |
| 110 |
// Free Methods |
| 111 |
/*=========================================================================*/ |
| 112 |
|
| 113 |
/*=========================================================================== |
| 114 |
METHOD: |
| 115 |
RxSocketThread (Free Method) |
| 116 |
|
| 117 |
DESCRIPTION: |
| 118 |
Thread for simulating asynchronous reads to a socket |
| 119 |
|
| 120 |
PARAMETERS: |
| 121 |
pData [ I ] cSocket pointer |
| 122 |
|
| 123 |
RETURN VALUE: |
| 124 |
void * - thread exit value (always 0) |
| 125 |
===========================================================================*/ |
| 126 |
void * RxSocketThread( void * pData ) |
| 127 |
{ |
| 128 |
cSocket * pSocket = (cSocket*)pData; |
| 129 |
if (pSocket == NULL || pSocket->IsValid() == false) |
| 130 |
{ |
| 131 |
return 0; |
| 132 |
} |
| 133 |
|
| 134 |
fd_set inputSet, outputSet; |
| 135 |
FD_ZERO( &inputSet ); |
| 136 |
FD_SET( pSocket->mCommandPipe[READING], &inputSet ); |
| 137 |
int largestFD = pSocket->mCommandPipe[READING]; |
| 138 |
|
| 139 |
int status = 0; |
| 140 |
while (true) |
| 141 |
{ |
| 142 |
// No FD_COPY() available |
| 143 |
memcpy( &outputSet, &inputSet, sizeof( fd_set ) ); |
| 144 |
|
| 145 |
// Wait until we recieve a command or data is available |
| 146 |
status = select( largestFD + 1, &outputSet, NULL, NULL, NULL ); |
| 147 |
if (status <= 0) |
| 148 |
{ |
| 149 |
TRACE( "error %d in select, errno %d\n", status, errno ); |
| 150 |
break; |
| 151 |
} |
| 152 |
|
| 153 |
if (FD_ISSET( pSocket->mCommandPipe[READING], &outputSet ) == true) |
| 154 |
{ |
| 155 |
// Read command value from the pipe |
| 156 |
BYTE cmd; |
| 157 |
status = read( pSocket->mCommandPipe[READING], &cmd, 1 ); |
| 158 |
if (status != 1) |
| 159 |
{ |
| 160 |
TRACE( "cmd error %d\n", status ); |
| 161 |
break; |
| 162 |
} |
| 163 |
|
| 164 |
if (cmd == START_READ_CMD) |
| 165 |
{ |
| 166 |
FD_SET( pSocket->mSocket, &inputSet ); |
| 167 |
largestFD = std::max( pSocket->mSocket, |
| 168 |
pSocket->mCommandPipe[READING] ); |
| 169 |
} |
| 170 |
else if (cmd == STOP_READ_CMD) |
| 171 |
{ |
| 172 |
FD_CLR( pSocket->mSocket, &inputSet ); |
| 173 |
largestFD = pSocket->mCommandPipe[READING]; |
| 174 |
} |
| 175 |
else |
| 176 |
{ |
| 177 |
// EXIT_CMD or anything else |
| 178 |
break; |
| 179 |
} |
| 180 |
} |
| 181 |
else if (FD_ISSET( pSocket->mSocket, &outputSet ) == true) |
| 182 |
{ |
| 183 |
// Stop watching for read data |
| 184 |
FD_CLR( pSocket->mSocket, &inputSet ); |
| 185 |
largestFD = pSocket->mCommandPipe[READING]; |
| 186 |
|
| 187 |
// Perform a recv for the header |
| 188 |
sQMUXDHeader recvHdr; |
| 189 |
status = recv( pSocket->mSocket, |
| 190 |
&recvHdr, |
| 191 |
sizeof( recvHdr ), |
| 192 |
0 ); |
| 193 |
if (status != sizeof( recvHdr )) |
| 194 |
{ |
| 195 |
TRACE( "recv error, bad size %d\n", status ); |
| 196 |
break; |
| 197 |
} |
| 198 |
|
| 199 |
// Calculate and read the remaining data |
| 200 |
int remainder = recvHdr.mTotalSize - sizeof( recvHdr ); |
| 201 |
if (remainder > pSocket->mBuffSz) |
| 202 |
{ |
| 203 |
TRACE( "read too large for buffer\n" ); |
| 204 |
break; |
| 205 |
} |
| 206 |
|
| 207 |
status = recv( pSocket->mSocket, |
| 208 |
pSocket->mpBuffer, |
| 209 |
remainder, |
| 210 |
0 ); |
| 211 |
|
| 212 |
// Is this one of our IOCTLS or a standard message? |
| 213 |
if (recvHdr.mQMUXDMsgID == eQMUXD_MSG_WRITE_QMI_SDU) |
| 214 |
{ |
| 215 |
cIOCallback * pCallback = pSocket->mpRxCallback; |
| 216 |
pSocket->mpRxCallback = 0; |
| 217 |
|
| 218 |
if (pCallback == (cIOCallback *)1) |
| 219 |
{ |
| 220 |
// We wanted to read, but not to be notified |
| 221 |
} |
| 222 |
else if (status >= 0) |
| 223 |
{ |
| 224 |
pCallback->IOComplete( 0, status ); |
| 225 |
} |
| 226 |
else |
| 227 |
{ |
| 228 |
pCallback->IOComplete( status, 0 ); |
| 229 |
} |
| 230 |
} |
| 231 |
else |
| 232 |
{ |
| 233 |
pSocket->mpRxCallback = 0; |
| 234 |
// Notify SendCtl() that control message completed |
| 235 |
|
| 236 |
if (recvHdr.mQMUXDMsgID == eQMUXD_MSG_ALLOC_QMI_CLIENT_ID) |
| 237 |
{ |
| 238 |
DWORD clientID; |
| 239 |
memcpy( &clientID, &pSocket->mpBuffer[0], 4 ); |
| 240 |
|
| 241 |
pSocket->mCtrlMsgComplete.Set( clientID ); |
| 242 |
} |
| 243 |
else |
| 244 |
{ |
| 245 |
// Just set the event |
| 246 |
pSocket->mCtrlMsgComplete.Set( 0 ); |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
return 0; |
| 253 |
}; |
| 254 |
|
| 255 |
/*=========================================================================*/ |
| 256 |
// cSocket Methods |
| 257 |
/*=========================================================================*/ |
| 258 |
|
| 259 |
/*=========================================================================== |
| 260 |
METHOD: |
| 261 |
cSocket (Public Method) |
| 262 |
|
| 263 |
DESCRIPTION: |
| 264 |
Constructor |
| 265 |
|
| 266 |
RETURN VALUE: |
| 267 |
None |
| 268 |
===========================================================================*/ |
| 269 |
cSocket::cSocket() |
| 270 |
: mSocket( INVALID_HANDLE_VALUE ), |
| 271 |
mbCancelWrite( false ), |
| 272 |
mpBuffer( 0 ), |
| 273 |
mBuffSz( 0 ), |
| 274 |
mRxThreadID( 0 ), |
| 275 |
mCtrlMsgComplete(), |
| 276 |
mQMUXDClientID( 0 ), |
| 277 |
mQMUXClientID( 0 ), |
| 278 |
mQMUXServiceID( 0 ), |
| 279 |
mChannelID( -1 ), |
| 280 |
mQMUXDTxID( 0 ) |
| 281 |
{ |
| 282 |
mCommandPipe[READING] = INVALID_HANDLE_VALUE; |
| 283 |
mCommandPipe[WRITING] = INVALID_HANDLE_VALUE; |
| 284 |
} |
| 285 |
|
| 286 |
/*=========================================================================== |
| 287 |
METHOD: |
| 288 |
~cSocket (Public Method) |
| 289 |
|
| 290 |
DESCRIPTION: |
| 291 |
Destructor |
| 292 |
|
| 293 |
RETURN VALUE: |
| 294 |
None |
| 295 |
===========================================================================*/ |
| 296 |
cSocket::~cSocket() |
| 297 |
{ |
| 298 |
// Disconnect from current port |
| 299 |
Disconnect(); |
| 300 |
|
| 301 |
mCommandPipe[READING] = INVALID_HANDLE_VALUE; |
| 302 |
mCommandPipe[WRITING] = INVALID_HANDLE_VALUE; |
| 303 |
} |
| 304 |
|
| 305 |
/*=========================================================================== |
| 306 |
METHOD: |
| 307 |
IsValid (Public Method) |
| 308 |
|
| 309 |
DESCRIPTION: |
| 310 |
Is this object valid? |
| 311 |
|
| 312 |
RETURN VALUE: |
| 313 |
Bool |
| 314 |
===========================================================================*/ |
| 315 |
bool cSocket::IsValid() |
| 316 |
{ |
| 317 |
// Nothing to do |
| 318 |
return true; |
| 319 |
} |
| 320 |
|
| 321 |
/*=========================================================================== |
| 322 |
METHOD: |
| 323 |
Connect (Public Method) |
| 324 |
|
| 325 |
DESCRIPTION: |
| 326 |
Connect to the specified port |
| 327 |
|
| 328 |
PARAMETERS: |
| 329 |
pChannel [ I ] - Channel number (IE: "0" = SMD_DATA_5 ) |
| 330 |
|
| 331 |
RETURN VALUE: |
| 332 |
bool |
| 333 |
===========================================================================*/ |
| 334 |
bool cSocket::Connect( LPCSTR pChannel ) |
| 335 |
{ |
| 336 |
if (IsValid() == false || pChannel == 0 || pChannel[0] == 0) |
| 337 |
{ |
| 338 |
return false; |
| 339 |
} |
| 340 |
|
| 341 |
if (mSocket != INVALID_HANDLE_VALUE) |
| 342 |
{ |
| 343 |
Disconnect(); |
| 344 |
} |
| 345 |
|
| 346 |
// Initialize command pipe for read thread |
| 347 |
int nRet = pipe( mCommandPipe ); |
| 348 |
if (nRet != 0) |
| 349 |
{ |
| 350 |
TRACE( "cSocket:Connect() pipe creation failed %d\n", nRet ); |
| 351 |
return false; |
| 352 |
} |
| 353 |
|
| 354 |
// Start the read thread |
| 355 |
nRet = pthread_create( &mRxThreadID, |
| 356 |
0, |
| 357 |
RxSocketThread, |
| 358 |
this ); |
| 359 |
if (nRet != 0) |
| 360 |
{ |
| 361 |
TRACE( "cSocket::Connect() pthread_create = %d\n", nRet ); |
| 362 |
|
| 363 |
Disconnect(); |
| 364 |
return false; |
| 365 |
} |
| 366 |
|
| 367 |
// Create a socket |
| 368 |
mSocket = socket( AF_UNIX, SOCK_STREAM, 0 ); |
| 369 |
if (mSocket == INVALID_HANDLE_VALUE) |
| 370 |
{ |
| 371 |
TRACE( "unable to create socket %d\n", errno ); |
| 372 |
|
| 373 |
Disconnect(); |
| 374 |
return false; |
| 375 |
} |
| 376 |
|
| 377 |
struct sockaddr_un clientSockAddr; |
| 378 |
memset( &clientSockAddr, 0, sizeof( clientSockAddr ) ); |
| 379 |
clientSockAddr.sun_family = AF_UNIX; |
| 380 |
|
| 381 |
// Format the client path |
| 382 |
snprintf( &clientSockAddr.sun_path[0], |
| 383 |
sizeof( clientSockAddr.sun_path ), |
| 384 |
"/var/qmux_client_socket%7lu", |
| 385 |
(unsigned long)getpid() ); |
| 386 |
|
| 387 |
// Delete if it exists already |
| 388 |
unlink( clientSockAddr.sun_path ); |
| 389 |
|
| 390 |
// Bind to a client address |
| 391 |
nRet = bind( mSocket, |
| 392 |
(struct sockaddr *)&clientSockAddr, |
| 393 |
sizeof( sockaddr_un ) ); |
| 394 |
if (nRet == -1) |
| 395 |
{ |
| 396 |
TRACE( "bad bind %d\n", errno ); |
| 397 |
|
| 398 |
Disconnect(); |
| 399 |
return false; |
| 400 |
} |
| 401 |
|
| 402 |
// Format the connection path |
| 403 |
struct sockaddr_un connectSockAddr; |
| 404 |
memset( &connectSockAddr, 0, sizeof( connectSockAddr ) ); |
| 405 |
connectSockAddr.sun_family = AF_UNIX; |
| 406 |
|
| 407 |
snprintf( &connectSockAddr.sun_path[0], |
| 408 |
sizeof( connectSockAddr.sun_path ), |
| 409 |
"/var/qmux_connect_socket" ); |
| 410 |
|
| 411 |
// Connect to server address |
| 412 |
nRet = connect( mSocket, |
| 413 |
(struct sockaddr *)&connectSockAddr, |
| 414 |
sizeof( sockaddr_un ) ); |
| 415 |
if (nRet < 0) |
| 416 |
{ |
| 417 |
TRACE( "bad connect %d\n", errno ); |
| 418 |
|
| 419 |
Disconnect(); |
| 420 |
return false; |
| 421 |
} |
| 422 |
|
| 423 |
int clientID; |
| 424 |
nRet = recv( mSocket, &clientID, sizeof( clientID ), 0 ); |
| 425 |
if (nRet != sizeof( clientID )) |
| 426 |
{ |
| 427 |
printf( "bad client ID %d\n", errno ); |
| 428 |
|
| 429 |
Disconnect(); |
| 430 |
return false; |
| 431 |
} |
| 432 |
|
| 433 |
// Save QMUXD Client ID |
| 434 |
mQMUXDClientID = clientID; |
| 435 |
|
| 436 |
// Save SMD channel |
| 437 |
mChannelID = strtol( pChannel, 0, 10 ); |
| 438 |
if (mChannelID == -1) |
| 439 |
{ |
| 440 |
Disconnect(); |
| 441 |
return false; |
| 442 |
} |
| 443 |
|
| 444 |
// Success! |
| 445 |
return true; |
| 446 |
} |
| 447 |
|
| 448 |
/*=========================================================================== |
| 449 |
METHOD: |
| 450 |
SendCtl (Public Method) |
| 451 |
|
| 452 |
DESCRIPTION: |
| 453 |
Send a control message to the lower layer |
| 454 |
|
| 455 |
PARAMETERS: |
| 456 |
msgType [ I ] - eQMUXDMessageType |
| 457 |
pData [I/O] - input or output specific to ioctl request value |
| 458 |
|
| 459 |
RETURN VALUE: |
| 460 |
int - control message return value (0 for success) |
| 461 |
===========================================================================*/ |
| 462 |
int cSocket::SendCtl( |
| 463 |
UINT msgType, |
| 464 |
void * pData ) |
| 465 |
{ |
| 466 |
if (mSocket == INVALID_HANDLE_VALUE) |
| 467 |
{ |
| 468 |
TRACE( "Invalid file handle\n" ); |
| 469 |
return -EBADFD; |
| 470 |
} |
| 471 |
|
| 472 |
BYTE msg[sizeof( sQMUXDHeader ) + PAYLOAD_SIZE]; |
| 473 |
memset( &msg[0], 0, sizeof( msg ) ); |
| 474 |
|
| 475 |
// The important QMUXD header values |
| 476 |
sQMUXDHeader * pHdr = (sQMUXDHeader *)&msg[0]; |
| 477 |
pHdr->mTotalSize = sizeof( msg ); |
| 478 |
pHdr->mQMUXDClientID = mQMUXDClientID; |
| 479 |
pHdr->mQMUXDMsgID = (eQMUXDMessageTypes)msgType; |
| 480 |
pHdr->mQMUXDClientIDDuplicate = mQMUXDClientID; |
| 481 |
|
| 482 |
// mQMUXDTxID could go to INT_MAX, but rather than dealing with possible |
| 483 |
// overflow in qmuxd or one of the lower layers, we'll stop early |
| 484 |
mQMUXDTxID++; |
| 485 |
if (mQMUXDTxID > 100000) |
| 486 |
{ |
| 487 |
mQMUXDTxID = 1; |
| 488 |
} |
| 489 |
pHdr->mTxID = ++mQMUXDTxID; |
| 490 |
|
| 491 |
// The Payload |
| 492 |
BYTE * pPayload = &msg[sizeof( sQMUXDHeader )]; |
| 493 |
if (msgType == (int)eQMUXD_MSG_ALLOC_QMI_CLIENT_ID) |
| 494 |
{ |
| 495 |
memcpy( &mQMUXServiceID, pData, 4 ); |
| 496 |
memcpy( &pPayload[0], &mQMUXServiceID, 4 ); |
| 497 |
} |
| 498 |
else if (msgType == (int)eQMUXD_MSG_RELEASE_QMI_CLIENT_ID) |
| 499 |
{ |
| 500 |
memcpy( &pPayload[0], &mQMUXServiceID, 4 ); |
| 501 |
memcpy( &pPayload[4], &mQMUXClientID, 4 ); |
| 502 |
} |
| 503 |
|
| 504 |
// Send the message |
| 505 |
int rc = send( mSocket, &msg[0], sizeof( msg ), 0 ); |
| 506 |
if (rc != sizeof( msg )) |
| 507 |
{ |
| 508 |
TRACE( "bad write %d\n", rc ); |
| 509 |
return rc; |
| 510 |
} |
| 511 |
|
| 512 |
if (mpRxCallback == 0) |
| 513 |
{ |
| 514 |
// No one is currently reading, need to trigger a read |
| 515 |
// so our data can be recieved |
| 516 |
RxData( &msg[0], sizeof( msg ), 0 ); |
| 517 |
} |
| 518 |
|
| 519 |
// Wait for the response (10s timeout) |
| 520 |
DWORD val; |
| 521 |
rc = mCtrlMsgComplete.Wait( 10000, val ); |
| 522 |
if (rc != 0) |
| 523 |
{ |
| 524 |
TRACE( "bad SendCtl() wait %d\n", rc ); |
| 525 |
return rc; |
| 526 |
} |
| 527 |
|
| 528 |
if (msgType == (int)eQMUXD_MSG_ALLOC_QMI_CLIENT_ID) |
| 529 |
{ |
| 530 |
// Grab the client ID |
| 531 |
mQMUXClientID = val; |
| 532 |
} |
| 533 |
|
| 534 |
return 0; |
| 535 |
} |
| 536 |
|
| 537 |
/*=========================================================================== |
| 538 |
METHOD: |
| 539 |
Disconnect (Public Method) |
| 540 |
|
| 541 |
DESCRIPTION: |
| 542 |
Disconnect from the current port |
| 543 |
|
| 544 |
RETURN VALUE: |
| 545 |
bool |
| 546 |
===========================================================================*/ |
| 547 |
bool cSocket::Disconnect() |
| 548 |
{ |
| 549 |
// Assume success |
| 550 |
bool bRC = true; |
| 551 |
|
| 552 |
if (mCommandPipe[WRITING] != INVALID_HANDLE_VALUE) |
| 553 |
{ |
| 554 |
if (mRxThreadID != 0) |
| 555 |
{ |
| 556 |
// Notify the thread to exit |
| 557 |
BYTE byte = EXIT_CMD; |
| 558 |
write( mCommandPipe[WRITING], &byte, 1 ); |
| 559 |
|
| 560 |
// And wait for it |
| 561 |
int nRC = pthread_join( mRxThreadID, 0 ); |
| 562 |
if (nRC != 0) |
| 563 |
{ |
| 564 |
TRACE( "failed to join thread %d\n", nRC ); |
| 565 |
bRC = false; |
| 566 |
} |
| 567 |
|
| 568 |
mRxThreadID = 0; |
| 569 |
} |
| 570 |
|
| 571 |
close( mCommandPipe[WRITING] ); |
| 572 |
close( mCommandPipe[READING] ); |
| 573 |
mCommandPipe[READING] = INVALID_HANDLE_VALUE; |
| 574 |
mCommandPipe[WRITING] = INVALID_HANDLE_VALUE; |
| 575 |
} |
| 576 |
|
| 577 |
if (mSocket != INVALID_HANDLE_VALUE) |
| 578 |
{ |
| 579 |
close( mSocket ); |
| 580 |
mSocket = INVALID_HANDLE_VALUE; |
| 581 |
} |
| 582 |
|
| 583 |
mCtrlMsgComplete.Clear(); |
| 584 |
mQMUXDClientID = 0; |
| 585 |
mQMUXClientID = 0; |
| 586 |
mQMUXServiceID = 0; |
| 587 |
mQMUXDTxID = 0; |
| 588 |
return bRC; |
| 589 |
} |
| 590 |
|
| 591 |
/*=========================================================================== |
| 592 |
METHOD: |
| 593 |
CancelIO (Public Method) |
| 594 |
|
| 595 |
DESCRIPTION: |
| 596 |
Cancel any in-progress I/O |
| 597 |
|
| 598 |
PARAMETERS: |
| 599 |
|
| 600 |
RETURN VALUE: |
| 601 |
bool |
| 602 |
===========================================================================*/ |
| 603 |
bool cSocket::CancelIO() |
| 604 |
{ |
| 605 |
if (mSocket == INVALID_HANDLE_VALUE) |
| 606 |
{ |
| 607 |
return false; |
| 608 |
} |
| 609 |
|
| 610 |
bool bRxCancel = CancelRx(); |
| 611 |
bool bTxCancel = CancelTx(); |
| 612 |
|
| 613 |
return (bRxCancel && bTxCancel); |
| 614 |
} |
| 615 |
|
| 616 |
/*=========================================================================== |
| 617 |
METHOD: |
| 618 |
CancelRx (Public Method) |
| 619 |
|
| 620 |
DESCRIPTION: |
| 621 |
Cancel any in-progress receive operation |
| 622 |
|
| 623 |
RETURN VALUE: |
| 624 |
bool |
| 625 |
===========================================================================*/ |
| 626 |
bool cSocket::CancelRx() |
| 627 |
{ |
| 628 |
if (mSocket == INVALID_HANDLE_VALUE |
| 629 |
|| mCommandPipe[WRITING] == INVALID_HANDLE_VALUE |
| 630 |
|| mpRxCallback == 0 |
| 631 |
|| mRxThreadID == 0) |
| 632 |
{ |
| 633 |
TRACE( "cannot cancel, thread not active\n" ); |
| 634 |
return false; |
| 635 |
} |
| 636 |
|
| 637 |
// Notify the thread to stop reading |
| 638 |
BYTE byte = STOP_READ_CMD; |
| 639 |
int nRC = write( mCommandPipe[WRITING], &byte, 1 ); |
| 640 |
if (nRC != 1) |
| 641 |
{ |
| 642 |
TRACE( "error %d canceling read\n", nRC ); |
| 643 |
return false; |
| 644 |
} |
| 645 |
|
| 646 |
// Remove the old callback |
| 647 |
mpRxCallback = 0; |
| 648 |
|
| 649 |
return true; |
| 650 |
} |
| 651 |
|
| 652 |
/*=========================================================================== |
| 653 |
METHOD: |
| 654 |
CancelTx (Public Method) |
| 655 |
|
| 656 |
DESCRIPTION: |
| 657 |
Cancel any in-progress transmit operation |
| 658 |
|
| 659 |
RETURN VALUE: |
| 660 |
bool |
| 661 |
===========================================================================*/ |
| 662 |
bool cSocket::CancelTx() |
| 663 |
{ |
| 664 |
if (mSocket == INVALID_HANDLE_VALUE) |
| 665 |
{ |
| 666 |
return false; |
| 667 |
} |
| 668 |
|
| 669 |
mbCancelWrite = true; |
| 670 |
|
| 671 |
return true; |
| 672 |
} |
| 673 |
|
| 674 |
/*=========================================================================== |
| 675 |
METHOD: |
| 676 |
RxData (Public Method) |
| 677 |
|
| 678 |
DESCRIPTION: |
| 679 |
Receive data |
| 680 |
|
| 681 |
PARAMETERS: |
| 682 |
pBuf [ I ] - Buffer to contain received data |
| 683 |
bufSz [ I ] - Amount of data to be received |
| 684 |
pCallback [ I ] - Callback object to be exercised when the |
| 685 |
operation completes |
| 686 |
|
| 687 |
RETURN VALUE: |
| 688 |
bool |
| 689 |
===========================================================================*/ |
| 690 |
bool cSocket::RxData( |
| 691 |
BYTE * pBuf, |
| 692 |
ULONG bufSz, |
| 693 |
cIOCallback * pCallback ) |
| 694 |
{ |
| 695 |
if (IsValid() == false || mpRxCallback != 0) |
| 696 |
{ |
| 697 |
return false; |
| 698 |
} |
| 699 |
|
| 700 |
if (pCallback == 0) |
| 701 |
{ |
| 702 |
// Not interested in being notified, but we still need a value |
| 703 |
// for this so that only one outstanding I/O operation is active |
| 704 |
// at any given point in time |
| 705 |
mpRxCallback = (cIOCallback * )1; |
| 706 |
} |
| 707 |
else |
| 708 |
{ |
| 709 |
mpRxCallback = pCallback; |
| 710 |
} |
| 711 |
|
| 712 |
mpBuffer = pBuf; |
| 713 |
mBuffSz = bufSz; |
| 714 |
|
| 715 |
// Notify the thread to start reading |
| 716 |
BYTE byte = START_READ_CMD; |
| 717 |
int nRC = write( mCommandPipe[WRITING], &byte, 1 ); |
| 718 |
if (nRC != 1) |
| 719 |
{ |
| 720 |
TRACE( "error %d starting read\n", nRC ); |
| 721 |
return false; |
| 722 |
} |
| 723 |
|
| 724 |
return true; |
| 725 |
} |
| 726 |
|
| 727 |
/*=========================================================================== |
| 728 |
METHOD: |
| 729 |
TxData (Public Method) |
| 730 |
|
| 731 |
DESCRIPTION: |
| 732 |
Transmit data |
| 733 |
|
| 734 |
PARAMETERS: |
| 735 |
pBuf [ I ] - Data to be transmitted |
| 736 |
bufSz [ I ] - Amount of data to be transmitted |
| 737 |
|
| 738 |
RETURN VALUE: |
| 739 |
bool |
| 740 |
===========================================================================*/ |
| 741 |
bool cSocket::TxData( |
| 742 |
const BYTE * pBuf, |
| 743 |
ULONG bufSz ) |
| 744 |
{ |
| 745 |
if (IsValid() == false) |
| 746 |
{ |
| 747 |
return false; |
| 748 |
} |
| 749 |
|
| 750 |
#ifdef DEBUG |
| 751 |
ULONGLONG nStart = GetTickCount(); |
| 752 |
#endif |
| 753 |
|
| 754 |
// Allow ourselves to be interupted |
| 755 |
mbCancelWrite = false; |
| 756 |
|
| 757 |
// Format the header |
| 758 |
int totalSz = sizeof( sQMUXDHeader ) + bufSz; |
| 759 |
BYTE * pMsg = new BYTE[totalSz]; |
| 760 |
if (pMsg == 0) |
| 761 |
{ |
| 762 |
TRACE( "unable to allocate buffer\n" ); |
| 763 |
return false; |
| 764 |
} |
| 765 |
memset( pMsg, 0, totalSz ); |
| 766 |
|
| 767 |
// The important QMUXD header values |
| 768 |
sQMUXDHeader * pHdr = (sQMUXDHeader *)pMsg; |
| 769 |
pHdr->mTotalSize = totalSz; |
| 770 |
pHdr->mQMUXDClientID = mQMUXDClientID; |
| 771 |
pHdr->mQMUXDMsgID = eQMUXD_MSG_WRITE_QMI_SDU; |
| 772 |
pHdr->mQMUXDClientIDDuplicate = mQMUXDClientID; |
| 773 |
|
| 774 |
// mQMUXDTxID could go to INT_MAX, but rather than dealing with possible |
| 775 |
// overflow in qmuxd or one of the lower layers, we'll stop early |
| 776 |
mQMUXDTxID++; |
| 777 |
if (mQMUXDTxID > 100000) |
| 778 |
{ |
| 779 |
mQMUXDTxID = 1; |
| 780 |
} |
| 781 |
pHdr->mTxID = ++mQMUXDTxID; |
| 782 |
|
| 783 |
pHdr->mQMUXServiceID = mQMUXServiceID; |
| 784 |
pHdr->mQMUXClientID = mQMUXClientID; |
| 785 |
|
| 786 |
// The data payload |
| 787 |
memcpy( &pMsg[sizeof( sQMUXDHeader )], pBuf, bufSz ); |
| 788 |
|
| 789 |
// Send the message |
| 790 |
int nRet = send( mSocket, pMsg, totalSz, 0 ); |
| 791 |
delete [] pMsg; |
| 792 |
if (nRet != totalSz) |
| 793 |
{ |
| 794 |
TRACE( "cSocket::TxData() write returned %d instead of %d\n", |
| 795 |
nRet, |
| 796 |
totalSz ); |
| 797 |
return false; |
| 798 |
} |
| 799 |
|
| 800 |
#ifdef DEBUG |
| 801 |
TRACE( "Write of %d bytes took %llu miliseconds\n", |
| 802 |
totalSz, |
| 803 |
GetTickCount() - nStart ); |
| 804 |
#endif |
| 805 |
|
| 806 |
return true; |
| 807 |
} |