Debian Codegraph @

gobi-api/Gobi_2011-11-28-1533/GobiConnectionMgmt/GobiConnectionMgmt.cpp

raw
1 /*===========================================================================
2 FILE:
3 GobiConnectionMgmt.cpp
4
5 DESCRIPTION:
6 QUALCOMM Connection Management API for Gobi
7
8 PUBLIC CLASSES AND FUNCTIONS:
9 CGobiConnectionMgmtDLL
10 cGobiConnectionMgmt
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 "GobiConnectionMgmt.h"
45 #include "QMIBuffers.h"
46
47 //---------------------------------------------------------------------------
48 // Definitions
49 //---------------------------------------------------------------------------
50
51 // Global object
52 CGobiConnectionMgmtDLL gDLL;
53
54 // Interval between traffic processing loop iterations (milliseconds)
55 const ULONG TRAFFIC_INTERVAL_MS = 300000;
56
57 // Maximum amount of time to wait for the traffic thread to exit
58 const ULONG THREAD_EXIT_TIME = 2000;
59
60 /*===========================================================================
61 METHOD:
62 TrafficProcessThread (Free Method)
63
64 DESCRIPTION:
65 QMI traffic process thread - processes all traffic in order to fire
66 off QMI traffic related callbacks
67
68 PARAMETERS:
69 pArg [ I ] - Object to interface to
70
71 RETURN VALUE:
72 void * - thread exit value (always 0)
73 ===========================================================================*/
74 void * TrafficProcessThread( PVOID pArg )
75 {
76 // Keep running?
77 bool bRun = false;
78
79 // Create a vector of the objects to wait on
80 std::vector <cEvent *> events;
81
82 // Store the index to service type for use later
83 std::map <DWORD, eQMIService> services;
84
85 // Grab API object
86 cGobiConnectionMgmt * pAPI = (cGobiConnectionMgmt *)pArg;
87 if (pAPI != 0)
88 {
89 // Time to go to work
90 bRun = true;
91
92 // Add the thread exit event
93 events.push_back( &pAPI->mExitEvent );
94
95 // Grab signal events for our protocol servers
96 std::map <eQMIService, cGobiQMICore::sServerInfo>::const_iterator pIter;
97 pIter = pAPI->mServers.begin();
98 while (pIter != pAPI->mServers.end())
99 {
100 eQMIService svc = pIter->first;
101 cQMIProtocolServer * pServer = pAPI->GetServer( svc );
102 if (pServer != 0)
103 {
104 // Grab the log from the server
105 const cProtocolLog & log = pServer->GetLog();
106
107 // Grab the Signal event, if it exists
108 cEvent & sigEvent = log.GetSignalEvent();
109
110 services[events.size()] = svc;
111 events.push_back( &sigEvent );
112 }
113
114 pIter++;
115 }
116 }
117
118 TRACE( "GobiConnectionMgmt traffic thread [%u] started\n",
119 (UINT)pthread_self() );
120
121 // Loop waiting for exit event
122 while (bRun == true)
123 {
124 // Wait for activity
125 DWORD ignoredVal, index;
126 int nRet = WaitOnMultipleEvents( events,
127 TRAFFIC_INTERVAL_MS,
128 ignoredVal,
129 index );
130
131 // Timeout
132 if (nRet == -ETIME)
133 {
134 // Do nothing
135 }
136 // Error?
137 else if (nRet <= 0)
138 {
139 TRACE( "GobiConnectionMgmt traffic thread wait error %d\n", nRet );
140 bRun = false;
141 }
142 // Exit event?
143 else if (index == 0)
144 {
145 bRun = false;
146 }
147 else if (index < events.size())
148 {
149 // Run ProcessTraffic() for this service type
150 if (services.find( index ) != services.end())
151 {
152 pAPI->ProcessTraffic( services[index] );
153 }
154 }
155 else
156 {
157 // Fatal error
158 bRun = false;
159 }
160 }
161
162 TRACE( "GobiConnectionMgmt traffic thread [%u] exited\n",
163 (UINT)pthread_self() );
164
165 if (pAPI != 0)
166 {
167 pAPI->mThreadCleanupFinished = true;
168 }
169
170 return 0;
171 }
172
173 /*===========================================================================
174 METHOD:
175 CallbackThread (Free Method)
176
177 DESCRIPTION:
178 Thread to execute a callback asynchronously
179
180 PARAMETERS:
181 pArg [ I ] - The cGobiCMCallback object
182
183 RETURN VALUE:
184 void * - thread exit value (always 0)
185 ===========================================================================*/
186 void * CallbackThread( PVOID pArg )
187 {
188 cGobiCMCallback * pCB = (cGobiCMCallback *)pArg;
189 if (pCB == 0)
190 {
191 ASSERT( 0 );
192 return 0;
193 }
194
195 pCB->Call();
196
197 delete pCB;
198 pCB = 0;
199
200 return 0;
201 }
202
203 /*=========================================================================*/
204 // CGobiConnectionMgmtDLL Methods
205 /*=========================================================================*/
206
207 /*===========================================================================
208 METHOD:
209 CGobiConnectionMgmtDLL (Public Method)
210
211 DESCRIPTION:
212 Constructor
213
214 RETURN VALUE:
215 None
216 ===========================================================================*/
217 CGobiConnectionMgmtDLL::CGobiConnectionMgmtDLL()
218 {
219 // Create sync CS
220 pthread_mutex_init( &mSyncSection, NULL );
221 }
222
223 /*===========================================================================
224 METHOD:
225 ~CGobiConnectionMgmtDLL (Public Method)
226
227 DESCRIPTION:
228 Destructor
229
230 RETURN VALUE:
231 None
232 ===========================================================================*/
233 CGobiConnectionMgmtDLL::~CGobiConnectionMgmtDLL()
234 {
235 std::map <GOBIHANDLE, cGobiConnectionMgmt *> tmpAPI = mAPI;
236 std::map <GOBIHANDLE, cGobiConnectionMgmt *>::const_iterator pIter;
237 pIter = tmpAPI.begin();
238
239 while (pIter != tmpAPI.end())
240 {
241 cGobiConnectionMgmt * pAPI = pIter->second;
242 if (pAPI != 0)
243 {
244 pAPI->Cleanup();
245 delete pAPI;
246 }
247
248 pIter++;
249 }
250
251 mAPI.clear();
252
253 pthread_mutex_destroy( &mSyncSection );
254 }
255
256 /*===========================================================================
257 METHOD:
258 CreateAPI (Public Method)
259
260 DESCRIPTION:
261 Create a new API object
262
263 RETURN VALUE:
264 GOBIHANDLE - Handle to new API object (0 upon failure)
265 ===========================================================================*/
266 GOBIHANDLE CGobiConnectionMgmtDLL::CreateAPI()
267 {
268 pthread_mutex_lock( &mSyncSection );
269
270 cGobiConnectionMgmt * pAPI = new cGobiConnectionMgmt;
271 if (pAPI != 0)
272 {
273 bool bInit = pAPI->Initialize();
274 if (bInit == true)
275 {
276 mAPI[(GOBIHANDLE)pAPI] = pAPI;
277 }
278 }
279
280 pthread_mutex_unlock( &mSyncSection );
281
282 return (GOBIHANDLE)pAPI;
283 }
284
285 /*===========================================================================
286 METHOD:
287 DeleteAPI (Public Method)
288
289 DESCRIPTION:
290 Delete an existing API object
291
292 PARAMETERS:
293 handle [ I ] - Handle to API object to return
294
295 RETURN VALUE:
296 None
297 ===========================================================================*/
298 void CGobiConnectionMgmtDLL::DeleteAPI( GOBIHANDLE handle )
299 {
300 pthread_mutex_lock( &mSyncSection );
301
302 std::map <GOBIHANDLE, cGobiConnectionMgmt *>::iterator pIter;
303 pIter = mAPI.find( handle );
304 if (pIter != mAPI.end())
305 {
306 cGobiConnectionMgmt * pAPI = pIter->second;
307 delete pAPI;
308
309 mAPI.erase( pIter );
310 }
311
312 pthread_mutex_unlock( &mSyncSection );
313 }
314
315 /*===========================================================================
316 METHOD:
317 GetAPI (Public Method)
318
319 DESCRIPTION:
320 Return the requested API object
321
322 PARAMETERS:
323 handle [ I ] - Handle to API object to return
324
325 RETURN VALUE:
326 cGobiConnectionMgmt *
327 ===========================================================================*/
328 cGobiConnectionMgmt * CGobiConnectionMgmtDLL::GetAPI( GOBIHANDLE handle )
329 {
330 cGobiConnectionMgmt * pAPI = 0;
331
332 pthread_mutex_lock( &mSyncSection );
333
334 std::map <GOBIHANDLE, cGobiConnectionMgmt *>::const_iterator pIter;
335 pIter = mAPI.find( handle );
336 if (pIter != mAPI.end())
337 {
338 pAPI = pIter->second;
339 }
340
341 pthread_mutex_unlock( &mSyncSection );
342
343 return pAPI;
344 }
345
346 /*=========================================================================*/
347 // cGobiConnectionMgmt Methods
348 /*=========================================================================*/
349
350 /*===========================================================================
351 METHOD:
352 cGobiConnectionMgmt (Public Method)
353
354 DESCRIPTION:
355 Constructor
356
357 RETURN VALUE:
358 None
359 ===========================================================================*/
360 cGobiConnectionMgmt::cGobiConnectionMgmt()
361 : cGobiQMICore(),
362 mbThreadStarted( false ),
363 mThreadID( 0 ),
364 mThreadCleanupFinished( false )
365 {
366 // Nothing to do but init those variables
367 }
368
369 /*===========================================================================
370 METHOD:
371 ~cGobiConnectionMgmt (Public Method)
372
373 DESCRIPTION:
374 Destructor
375
376 RETURN VALUE:
377 None
378 ===========================================================================*/
379 cGobiConnectionMgmt::~cGobiConnectionMgmt()
380 {
381 Disconnect();
382 }
383
384 /*===========================================================================
385 METHOD:
386 ProcessTraffic (Internal Method)
387
388 DESCRIPTION:
389 Process traffic in a QMI server protocol log, this is done to
390 exercise QMI indication related callbacks
391
392 PARAMETERS:
393 svc [ I ] - QMI Service type
394
395 RETURN VALUE:
396 None
397 ===========================================================================*/
398 void cGobiConnectionMgmt::ProcessTraffic( eQMIService svc )
399 {
400 ULONG count = 0;
401
402 std::map <eQMIService, sServerInfo>::iterator pIter;
403 pIter = mServers.find( svc );
404 if (pIter == mServers.end())
405 {
406 return;
407 }
408
409 sServerInfo & si = pIter->second;
410 cQMIProtocolServer * pSvr = si.mpServer;
411 if (pSvr == 0)
412 {
413 return;
414 }
415
416 // Grab the service ID from the service
417 eQMIService svcID = pSvr->GetServiceType();
418 if (svcID == eQMI_SVC_ENUM_BEGIN)
419 {
420 return;
421 }
422
423 // Grab the log from the server
424 const cProtocolLog & logSvr = pSvr->GetLog();
425
426 // New items to process?
427 count = logSvr.GetCount();
428 if (count != INVALID_LOG_INDEX && count > si.mLogsProcessed)
429 {
430 for (ULONG i = si.mLogsProcessed; i < count; i++)
431 {
432 sProtocolBuffer buf = logSvr.GetBuffer( i );
433 if (buf.IsValid() == false)
434 {
435 continue;
436 }
437
438 eProtocolType pt = buf.GetType();
439 if (IsQMIProtocolRX( pt ) == false)
440 {
441 continue;
442 }
443
444 sQMIServiceBuffer qmiBuf( buf.GetSharedBuffer() );
445 if (qmiBuf.IsIndication() == false)
446 {
447 continue;
448 }
449
450 ULONG msgID = qmiBuf.GetMessageID();
451
452 tCallbackKey ck( svcID, msgID );
453 std::map <tCallbackKey, tCallbackValue>::iterator pIter;
454 pIter = mCallbacks.find( ck );
455 if (pIter == mCallbacks.end())
456 {
457 continue;
458 }
459
460 ULONG outLen = 0;
461 const BYTE * pOutput = (const BYTE *)qmiBuf.GetRawContents( outLen );
462 tCallbackValue cv = pIter->second;
463
464 cGenericCallback * pCB = 0;
465 pCB = new cGenericCallback( cv.first,
466 svcID,
467 msgID,
468 cv.second,
469 outLen,
470 pOutput );
471
472 if (pCB != 0)
473 {
474 if (pCB->Initialize() == false)
475 {
476 delete pCB;
477 }
478 }
479 }
480
481 si.mLogsProcessed = count;
482 }
483 }
484
485 /*===========================================================================
486 METHOD:
487 Connect (Public Method)
488
489 DESCRIPTION:
490 Connect to the specified Gobi device
491
492 PARAMETERS:
493 pQMIFile [ I ] - QMI control file to connect to
494 services [ I ] - QMI services to connect to
495
496 RETURN VALUE:
497 std::set <eQMIService> - Services successfuly configured
498 ===========================================================================*/
499 std::set <eQMIService> cGobiConnectionMgmt::Connect(
500 LPCSTR pQMIFile,
501 std::set <eQMIService> & services )
502 {
503 std::set <eQMIService> svcs = cGobiQMICore::Connect( pQMIFile, services );
504 if (svcs.size() > 0)
505 {
506 // Start the traffic processing thread?
507 if (mbThreadStarted == false)
508 {
509 // Clear mExitEvent;
510 mExitEvent.Clear();
511
512 pthread_create( &mThreadID,
513 NULL,
514 TrafficProcessThread,
515 this );
516
517 mbThreadStarted = true;
518 }
519 }
520
521 return svcs;
522 }
523
524 /*===========================================================================
525 METHOD:
526 Disconnect (Public Method)
527
528 DESCRIPTION:
529 Disconnect from the currently connected Gobi device
530
531 RETURN VALUE:
532 bool
533 ===========================================================================*/
534 bool cGobiConnectionMgmt::Disconnect()
535 {
536 // Clear all callback function pointers
537 mCallbacks.clear();
538
539 // Exit traffic processing thread
540 if (mbThreadStarted == true)
541 {
542 // Signal thread to exit
543 mExitEvent.Set( 0 );
544
545 // If we are not being called from the thread itself then wait for
546 // it to exit, if not then it will have to exit automatically
547 if (pthread_self() != mThreadID)
548 {
549 if (mThreadID != 0)
550 {
551 pthread_join( mThreadID, NULL );
552 }
553 }
554 }
555
556 // Clear out thread handle/ID
557 mbThreadStarted = false;
558 mThreadID = 0;
559
560 return cGobiQMICore::Disconnect();
561 }
562
563 /*===========================================================================
564 METHOD:
565 SetGenericCallback (Public Method)
566
567 DESCRIPTION:
568 Enable/disable generic callback function
569
570 PARAMETERS:
571 svcID [ I ] - Service ID to monitor
572 msgID [ I ] - Message ID to look for
573 pCallback [ I ] - Generic callback pointer
574 userValue [ I ] - User value to pass back to callback
575
576 RETURN VALUE:
577 eGobiError - Return code
578 ===========================================================================*/
579 eGobiError cGobiConnectionMgmt::SetGenericCallback(
580 ULONG svcID,
581 ULONG msgID,
582 tFNGenericCallback pCallback,
583 ULONG_PTR userValue )
584 {
585 // Assume success
586 eGobiError rc = eGOBI_ERR_NONE;
587
588 tCallbackKey ck( svcID, msgID );
589 std::map <tCallbackKey, tCallbackValue>::iterator pIter;
590 pIter = mCallbacks.find( ck );
591
592 bool bOn = (pCallback != 0 && pIter == mCallbacks.end());
593 bool bOff = (pCallback == 0 && pIter != mCallbacks.end());
594 bool bReplace = (pCallback != 0 && pIter != mCallbacks.end());
595 if (bOn == true || bReplace == true)
596 {
597 tCallbackValue cv( pCallback, userValue );
598 mCallbacks[ck] = cv;
599 }
600 else if (bOff == true)
601 {
602 mCallbacks.erase( pIter );
603 }
604
605 return rc;
606 }