| 1 |
/*=========================================================================== |
| 2 |
FILE: |
| 3 |
GobiQMICore.cpp |
| 4 |
|
| 5 |
DESCRIPTION: |
| 6 |
QUALCOMM Gobi QMI Based API Core |
| 7 |
|
| 8 |
PUBLIC CLASSES AND FUNCTIONS: |
| 9 |
cGobiQMICore |
| 10 |
|
| 11 |
Copyright (c) 2012, Code Aurora Forum. All rights reserved. |
| 12 |
|
| 13 |
Redistribution and use in source and binary forms, with or without |
| 14 |
modification, are permitted provided that the following conditions are met: |
| 15 |
* Redistributions of source code must retain the above copyright |
| 16 |
notice, this list of conditions and the following disclaimer. |
| 17 |
* Redistributions in binary form must reproduce the above copyright |
| 18 |
notice, this list of conditions and the following disclaimer in the |
| 19 |
documentation and/or other materials provided with the distribution. |
| 20 |
* Neither the name of Code Aurora Forum nor |
| 21 |
the names of its contributors may be used to endorse or promote |
| 22 |
products derived from this software without specific prior written |
| 23 |
permission. |
| 24 |
|
| 25 |
|
| 26 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 27 |
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 28 |
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 29 |
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 30 |
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 31 |
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 32 |
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 33 |
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 34 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 35 |
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 36 |
POSSIBILITY OF SUCH DAMAGE. |
| 37 |
==========================================================================*/ |
| 38 |
|
| 39 |
//--------------------------------------------------------------------------- |
| 40 |
// Include Files |
| 41 |
//--------------------------------------------------------------------------- |
| 42 |
#include "StdAfx.h" |
| 43 |
#include "GobiQMICore.h" |
| 44 |
|
| 45 |
#include "QMIBuffers.h" |
| 46 |
#include "ProtocolNotification.h" |
| 47 |
|
| 48 |
/*=========================================================================*/ |
| 49 |
// cGobiQMICore Methods |
| 50 |
/*=========================================================================*/ |
| 51 |
|
| 52 |
/*=========================================================================== |
| 53 |
METHOD: |
| 54 |
cGobiQMICore (Public Method) |
| 55 |
|
| 56 |
DESCRIPTION: |
| 57 |
Constructor |
| 58 |
|
| 59 |
RETURN VALUE: |
| 60 |
None |
| 61 |
===========================================================================*/ |
| 62 |
cGobiQMICore::cGobiQMICore() |
| 63 |
: mLastError( eGOBI_ERR_NONE ) |
| 64 |
{ |
| 65 |
mInterface[0] = 0; |
| 66 |
} |
| 67 |
|
| 68 |
/*=========================================================================== |
| 69 |
METHOD: |
| 70 |
~cGobiQMICore (Public Method) |
| 71 |
|
| 72 |
DESCRIPTION: |
| 73 |
Destructor |
| 74 |
|
| 75 |
RETURN VALUE: |
| 76 |
BOOL |
| 77 |
===========================================================================*/ |
| 78 |
cGobiQMICore::~cGobiQMICore() |
| 79 |
{ |
| 80 |
Cleanup(); |
| 81 |
} |
| 82 |
|
| 83 |
/*=========================================================================== |
| 84 |
METHOD: |
| 85 |
Initialize (Public Method) |
| 86 |
|
| 87 |
DESCRIPTION: |
| 88 |
Initialize the object |
| 89 |
|
| 90 |
RETURN VALUE: |
| 91 |
bool |
| 92 |
===========================================================================*/ |
| 93 |
bool cGobiQMICore::Initialize() |
| 94 |
{ |
| 95 |
return true; |
| 96 |
} |
| 97 |
|
| 98 |
/*=========================================================================== |
| 99 |
METHOD: |
| 100 |
Cleanup (Public Method) |
| 101 |
|
| 102 |
DESCRIPTION: |
| 103 |
Cleanup the object |
| 104 |
|
| 105 |
RETURN VALUE: |
| 106 |
bool |
| 107 |
===========================================================================*/ |
| 108 |
bool cGobiQMICore::Cleanup() |
| 109 |
{ |
| 110 |
Disconnect(); |
| 111 |
return true; |
| 112 |
} |
| 113 |
|
| 114 |
/*=========================================================================== |
| 115 |
METHOD: |
| 116 |
Connect (Public Method) |
| 117 |
|
| 118 |
DESCRIPTION: |
| 119 |
Connect to the specified Gobi device |
| 120 |
|
| 121 |
PARAMETERS: |
| 122 |
pQMIFile [ I ] - Gobi device interface to connect to |
| 123 |
services [ I ] - QMI services to connect to |
| 124 |
|
| 125 |
RETURN VALUE: |
| 126 |
std::set <eQMIService> - Services successfuly configured |
| 127 |
===========================================================================*/ |
| 128 |
std::set <eQMIService> cGobiQMICore::Connect( |
| 129 |
LPCSTR pInterface, |
| 130 |
std::set <eQMIService> & services ) |
| 131 |
{ |
| 132 |
// The services we successfully connected to |
| 133 |
std::set <eQMIService> retServices; |
| 134 |
|
| 135 |
// Clear last error recorded |
| 136 |
ClearLastError(); |
| 137 |
|
| 138 |
size_t ifaceLen = strnlen( pInterface, MAX_PATH ) + 1; |
| 139 |
if (ifaceLen >= (size_t)MAX_PATH) |
| 140 |
{ |
| 141 |
mLastError = eGOBI_ERR_INVALID_ARG; |
| 142 |
return retServices; |
| 143 |
} |
| 144 |
|
| 145 |
// Allocate configured QMI servers |
| 146 |
std::set <eQMIService>::const_iterator pIter = services.begin(); |
| 147 |
while (pIter != services.end()) |
| 148 |
{ |
| 149 |
cQMIProtocolServer * pSvr = 0; |
| 150 |
pSvr = new cQMIProtocolServer( *pIter, 8192, 512 ); |
| 151 |
if (pSvr != 0) |
| 152 |
{ |
| 153 |
// Initialize server (we don't care about the return code |
| 154 |
// since the following Connect() call will fail if we are |
| 155 |
// unable to initialize the server) |
| 156 |
pSvr->Initialize(); |
| 157 |
|
| 158 |
bool bRC = pSvr->Connect( pInterface ); |
| 159 |
if (bRC == true) |
| 160 |
{ |
| 161 |
sServerInfo si( pSvr ); |
| 162 |
std::pair <eQMIService, sServerInfo> entry( *pIter, si ); |
| 163 |
mServers.insert( entry ); |
| 164 |
|
| 165 |
retServices.insert( *pIter ); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
pIter++; |
| 170 |
} |
| 171 |
|
| 172 |
// All servers fail? |
| 173 |
if (retServices.size() == 0) |
| 174 |
{ |
| 175 |
// Yes, disconnect them all |
| 176 |
Disconnect(); |
| 177 |
|
| 178 |
// ... and set the error code |
| 179 |
mLastError = eGOBI_ERR_CONNECT; |
| 180 |
} |
| 181 |
|
| 182 |
memcpy( mInterface, pInterface, ifaceLen ); |
| 183 |
return retServices; |
| 184 |
} |
| 185 |
|
| 186 |
/*=========================================================================== |
| 187 |
METHOD: |
| 188 |
Disconnect (Public Method) |
| 189 |
|
| 190 |
DESCRIPTION: |
| 191 |
Disconnect from the currently connected Gobi device |
| 192 |
|
| 193 |
RETURN VALUE: |
| 194 |
bool |
| 195 |
===========================================================================*/ |
| 196 |
bool cGobiQMICore::Disconnect() |
| 197 |
{ |
| 198 |
// Clear last error recorded |
| 199 |
ClearLastError(); |
| 200 |
|
| 201 |
// Clear device interface |
| 202 |
mInterface[0] = 0; |
| 203 |
|
| 204 |
// Assume failure |
| 205 |
bool bRC = false; |
| 206 |
if (mServers.size() == 0) |
| 207 |
{ |
| 208 |
mLastError = eGOBI_ERR_NO_CONNECTION; |
| 209 |
return bRC; |
| 210 |
} |
| 211 |
|
| 212 |
// Disconnect/clean-up all configured QMI servers |
| 213 |
std::map <eQMIService, sServerInfo>::iterator pIter; |
| 214 |
pIter = mServers.begin(); |
| 215 |
|
| 216 |
while (pIter != mServers.end()) |
| 217 |
{ |
| 218 |
sServerInfo & si = pIter->second; |
| 219 |
cQMIProtocolServer * pSvr = si.mpServer; |
| 220 |
if (pSvr != 0) |
| 221 |
{ |
| 222 |
pSvr->Disconnect(); |
| 223 |
pSvr->Exit(); |
| 224 |
|
| 225 |
delete pSvr; |
| 226 |
} |
| 227 |
|
| 228 |
si.mLogsProcessed = 0; |
| 229 |
pIter++; |
| 230 |
} |
| 231 |
|
| 232 |
mServers.clear(); |
| 233 |
|
| 234 |
bRC = true; |
| 235 |
return bRC; |
| 236 |
} |
| 237 |
|
| 238 |
/*=========================================================================== |
| 239 |
METHOD: |
| 240 |
Send (Public Method) |
| 241 |
|
| 242 |
DESCRIPTION: |
| 243 |
Send a request using the specified QMI protocol server and wait for (and |
| 244 |
then return) the response |
| 245 |
|
| 246 |
PARAMETERS: |
| 247 |
svcID [ I ] - QMI service type |
| 248 |
msgID [ I ] - QMI message ID |
| 249 |
to [ I ] - Timeout value (in milliseconds) |
| 250 |
inLen [ I ] - Length of input buffer |
| 251 |
pIn [ I ] - Input buffer |
| 252 |
pOutLen [I/O] - Upon input the maximum number of BYTEs pOut can |
| 253 |
contain, upon output the number of BYTEs copied |
| 254 |
to pOut |
| 255 |
pOut [ O ] - Output buffer |
| 256 |
|
| 257 |
RETURN VALUE: |
| 258 |
eGobiError - The result |
| 259 |
===========================================================================*/ |
| 260 |
eGobiError cGobiQMICore::Send( |
| 261 |
ULONG svcID, |
| 262 |
ULONG msgID, |
| 263 |
ULONG to, |
| 264 |
ULONG inLen, |
| 265 |
const BYTE * pIn, |
| 266 |
ULONG * pOutLen, |
| 267 |
BYTE * pOut ) |
| 268 |
{ |
| 269 |
// Clear last error recorded |
| 270 |
ClearLastError(); |
| 271 |
|
| 272 |
if (msgID > 0xffff) |
| 273 |
{ |
| 274 |
mLastError = eGOBI_ERR_INVALID_ARG; |
| 275 |
return mLastError; |
| 276 |
} |
| 277 |
|
| 278 |
sSharedBuffer * pRequest = 0; |
| 279 |
pRequest = sQMIServiceBuffer::BuildBuffer( (eQMIService)svcID, |
| 280 |
(WORD)msgID, |
| 281 |
false, |
| 282 |
false, |
| 283 |
pIn, |
| 284 |
inLen ); |
| 285 |
|
| 286 |
if (pRequest == 0) |
| 287 |
{ |
| 288 |
mLastError = eGOBI_ERR_MEMORY; |
| 289 |
return mLastError; |
| 290 |
} |
| 291 |
|
| 292 |
// We use the event based notification approach |
| 293 |
cSyncQueue <sProtocolNotificationEvent> evts( 12, true ); |
| 294 |
cProtocolQueueNotification pn( &evts ); |
| 295 |
|
| 296 |
// Build the request object |
| 297 |
sProtocolRequest req( pRequest, 0, to, 1, 1, &pn ); |
| 298 |
if (to == 0) |
| 299 |
{ |
| 300 |
mLastError = eGOBI_ERR_INTERNAL; |
| 301 |
return mLastError; |
| 302 |
} |
| 303 |
|
| 304 |
// Grab the server |
| 305 |
std::map <eQMIService, cGobiQMICore::sServerInfo>::iterator pSvrIter; |
| 306 |
pSvrIter = mServers.find( (eQMIService)svcID ); |
| 307 |
if (pSvrIter == mServers.end()) |
| 308 |
{ |
| 309 |
mLastError = eGOBI_ERR_NO_CONNECTION; |
| 310 |
return mLastError; |
| 311 |
} |
| 312 |
|
| 313 |
sServerInfo & si = pSvrIter->second; |
| 314 |
cQMIProtocolServer * pSvr = si.mpServer; |
| 315 |
if (pSvr == 0 || pSvr->IsConnected() == false) |
| 316 |
{ |
| 317 |
mLastError = eGOBI_ERR_NO_CONNECTION; |
| 318 |
return mLastError; |
| 319 |
} |
| 320 |
|
| 321 |
// Grab the log from the server |
| 322 |
const cProtocolLog & protocolLog = pSvr->GetLog(); |
| 323 |
|
| 324 |
// Schedule the request |
| 325 |
ULONG reqID = pSvr->AddRequest( req ); |
| 326 |
if (reqID == INVALID_REQUEST_ID) |
| 327 |
{ |
| 328 |
mLastError = eGOBI_ERR_REQ_SCHEDULE; |
| 329 |
return mLastError; |
| 330 |
} |
| 331 |
|
| 332 |
// Store for external cancel |
| 333 |
si.mRequestID = reqID; |
| 334 |
|
| 335 |
bool bReq = false; |
| 336 |
bool bExit = false; |
| 337 |
DWORD idx; |
| 338 |
|
| 339 |
// Returned response |
| 340 |
sProtocolBuffer rsp; |
| 341 |
|
| 342 |
// Process up to the indicated timeout |
| 343 |
cEvent & sigEvt = evts.GetSignalEvent(); |
| 344 |
while (bExit == false) |
| 345 |
{ |
| 346 |
int wc = sigEvt.Wait( to, idx ); |
| 347 |
if (wc == ETIME) |
| 348 |
{ |
| 349 |
if (bReq == true) |
| 350 |
{ |
| 351 |
mLastError = eGOBI_ERR_RESPONSE_TO; |
| 352 |
} |
| 353 |
else |
| 354 |
{ |
| 355 |
mLastError = eGOBI_ERR_REQUEST_TO; |
| 356 |
} |
| 357 |
break; |
| 358 |
} |
| 359 |
else if (wc != 0) |
| 360 |
{ |
| 361 |
mLastError = eGOBI_ERR_INTERNAL; |
| 362 |
break; |
| 363 |
} |
| 364 |
|
| 365 |
sProtocolNotificationEvent evt; |
| 366 |
bool bEvt = evts.GetElement( idx, evt ); |
| 367 |
if (bEvt == false) |
| 368 |
{ |
| 369 |
mLastError = eGOBI_ERR_INTERNAL; |
| 370 |
bExit = true; |
| 371 |
break; |
| 372 |
} |
| 373 |
|
| 374 |
switch (evt.mEventType) |
| 375 |
{ |
| 376 |
case ePROTOCOL_EVT_REQ_ERR: |
| 377 |
mLastError = eGOBI_ERR_REQUEST; |
| 378 |
bExit = true; |
| 379 |
break; |
| 380 |
|
| 381 |
case ePROTOCOL_EVT_RSP_ERR: |
| 382 |
mLastError = eGOBI_ERR_RESPONSE; |
| 383 |
bExit = true; |
| 384 |
break; |
| 385 |
|
| 386 |
case ePROTOCOL_EVT_REQ_SENT: |
| 387 |
{ |
| 388 |
// Grab the as-sent request |
| 389 |
DWORD id = evt.mParam2; |
| 390 |
sProtocolBuffer tmpReq = protocolLog.GetBuffer( id ); |
| 391 |
sSharedBuffer * pTmpRequest = tmpReq.GetSharedBuffer(); |
| 392 |
if (pTmpRequest != 0) |
| 393 |
{ |
| 394 |
// Grab the transaction ID |
| 395 |
sQMIServiceBuffer actualReq( pTmpRequest ); |
| 396 |
si.mRequestTXID = actualReq.GetTransactionID(); |
| 397 |
} |
| 398 |
|
| 399 |
bReq = true; |
| 400 |
} |
| 401 |
break; |
| 402 |
|
| 403 |
case ePROTOCOL_EVT_RSP_RECV: |
| 404 |
// Success! |
| 405 |
rsp = protocolLog.GetBuffer( evt.mParam2 ); |
| 406 |
bExit = true; |
| 407 |
break; |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
if ( (mLastError == eGOBI_ERR_INTERNAL) |
| 412 |
|| (mLastError == eGOBI_ERR_REQUEST_TO) |
| 413 |
|| (mLastError == eGOBI_ERR_RESPONSE_TO) ) |
| 414 |
{ |
| 415 |
// Remove the request as our protocol notification object is |
| 416 |
// about to go out of scope and hence be destroyed |
| 417 |
pSvr->RemoveRequest( reqID ); |
| 418 |
} |
| 419 |
|
| 420 |
if (rsp.IsValid() == false) |
| 421 |
{ |
| 422 |
return GetCorrectedLastError(); |
| 423 |
} |
| 424 |
|
| 425 |
// Did we receive a valid QMI response? |
| 426 |
sQMIServiceBuffer qmiRsp( rsp.GetSharedBuffer() ); |
| 427 |
if (qmiRsp.IsValid() == false) |
| 428 |
{ |
| 429 |
mLastError = eGOBI_ERR_MALFORMED_RSP; |
| 430 |
return mLastError; |
| 431 |
} |
| 432 |
|
| 433 |
// Caller might not be interested in actual output (beyond error code) |
| 434 |
ULONG maxSz = 0; |
| 435 |
if (pOutLen != 0) |
| 436 |
{ |
| 437 |
maxSz = *pOutLen; |
| 438 |
} |
| 439 |
|
| 440 |
if (maxSz > 0) |
| 441 |
{ |
| 442 |
// TLV 2 is always present |
| 443 |
ULONG needSz = 0; |
| 444 |
const BYTE * pData = (const BYTE *)qmiRsp.GetRawContents( needSz ); |
| 445 |
if (needSz == 0 || pData == 0) |
| 446 |
{ |
| 447 |
return eGOBI_ERR_INVALID_RSP; |
| 448 |
} |
| 449 |
|
| 450 |
*pOutLen = needSz; |
| 451 |
if (needSz > maxSz) |
| 452 |
{ |
| 453 |
return eGOBI_ERR_BUFFER_SZ; |
| 454 |
} |
| 455 |
|
| 456 |
memcpy( pOut, pData, needSz ); |
| 457 |
} |
| 458 |
|
| 459 |
// Check the mandatory QMI result TLV for success |
| 460 |
ULONG rc = 0; |
| 461 |
ULONG ec = 0; |
| 462 |
bool bResult = qmiRsp.GetResult( rc, ec ); |
| 463 |
if (bResult == false) |
| 464 |
{ |
| 465 |
mLastError = eGOBI_ERR_MALFORMED_RSP; |
| 466 |
return mLastError; |
| 467 |
} |
| 468 |
else if (rc != 0) |
| 469 |
{ |
| 470 |
return GetCorrectedQMIError( ec ); |
| 471 |
} |
| 472 |
|
| 473 |
// Success! |
| 474 |
return eGOBI_ERR_NONE; |
| 475 |
} |
| 476 |
|
| 477 |
/*=========================================================================== |
| 478 |
METHOD: |
| 479 |
CancelSend (Public Method) |
| 480 |
|
| 481 |
DESCRIPTION: |
| 482 |
Cancel the most recent in-progress Send() based operation |
| 483 |
|
| 484 |
PARAMETERS: |
| 485 |
svcID [ I ] - Service whose outstanding request is to be cancelled |
| 486 |
pTXID [ O ] - QMI transaction ID of outstanding request |
| 487 |
|
| 488 |
RETURN VALUE: |
| 489 |
eGobiError - The result |
| 490 |
===========================================================================*/ |
| 491 |
eGobiError cGobiQMICore::CancelSend( |
| 492 |
ULONG svcID, |
| 493 |
ULONG * pTXID ) |
| 494 |
{ |
| 495 |
// Grab the server |
| 496 |
std::map <eQMIService, cGobiQMICore::sServerInfo>::iterator pSvrIter; |
| 497 |
pSvrIter = mServers.find( (eQMIService)svcID ); |
| 498 |
if (pSvrIter == mServers.end()) |
| 499 |
{ |
| 500 |
mLastError = eGOBI_ERR_NO_CONNECTION; |
| 501 |
return mLastError; |
| 502 |
} |
| 503 |
|
| 504 |
sServerInfo & si = pSvrIter->second; |
| 505 |
cQMIProtocolServer * pSvr = si.mpServer; |
| 506 |
if (pSvr == 0) |
| 507 |
{ |
| 508 |
return eGOBI_ERR_INTERNAL; |
| 509 |
} |
| 510 |
|
| 511 |
if (si.mRequestID == 0xffffffff) |
| 512 |
{ |
| 513 |
return eGOBI_ERR_NO_CANCELABLE_OP; |
| 514 |
} |
| 515 |
|
| 516 |
bool bRemove = pSvr->RemoveRequest( si.mRequestID ); |
| 517 |
if (bRemove == false) |
| 518 |
{ |
| 519 |
return eGOBI_ERR_CANCEL_OP; |
| 520 |
} |
| 521 |
|
| 522 |
if (pTXID != 0) |
| 523 |
{ |
| 524 |
*pTXID = si.mRequestTXID; |
| 525 |
} |
| 526 |
|
| 527 |
return eGOBI_ERR_NONE; |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
|