aboutsummaryrefslogtreecommitdiff
path: root/gobi-api/fixed-GobiAPI-1.0.40/Core/QMIProtocolServer.cpp
blob: 9ee6d1ca100ad85e86367f105ee0448e49a4e5dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*===========================================================================
FILE:
   QMIProtocolServer.h

DESCRIPTION:
   QMI protocol server
   
PUBLIC CLASSES AND METHODS:
   cQMIProtocolServer

Copyright (c) 2011, Code Aurora Forum. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of Code Aurora Forum nor
      the names of its contributors may be used to endorse or promote
      products derived from this software without specific prior written
      permission.


THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
===========================================================================*/

//---------------------------------------------------------------------------
// Include Files
//---------------------------------------------------------------------------
#include "StdAfx.h"
#include "QMIProtocolServer.h"
#include "QMIBuffers.h"

/*=========================================================================*/
// cQMIProtocolServer Methods
/*=========================================================================*/

/*===========================================================================
METHOD:
   cQMIProtocolServer (Public Method)

DESCRIPTION:
   Constructor

PARAMETERS:
   serviceType [ I ] - QMI service type requested
   bufferSzRx  [ I ] - Size of data buffer for incoming data
   logSz       [ I ] - Size of log (number of buffers)

SEQUENCING:
   None (constructs sequencing objects)

RETURN VALUE:
   None
===========================================================================*/
cQMIProtocolServer::cQMIProtocolServer( 
   eQMIService                serviceType,
   ULONG                      bufferSzRx,
   ULONG                      logSz )
   :  cProtocolServer( MapQMIServiceToProtocol( serviceType, false ),
                       MapQMIServiceToProtocol( serviceType, true ),
                       bufferSzRx, 
                       logSz ),
      mLastTID( (WORD)INVALID_QMI_TRANSACTION_ID ),
      mService( serviceType ),
      mMEID( "" )

{
   // Nothing to do
}

/*===========================================================================
METHOD:
   ~cQMIProtocolServer (Public Method)

DESCRIPTION:
   Destructor

SEQUENCING:
   None (constructs sequencing objects)

RETURN VALUE:
   None
===========================================================================*/
cQMIProtocolServer::~cQMIProtocolServer()
{
   // Nothing to do
}

/*===========================================================================
METHOD:
   Connect (Public Method)

DESCRIPTION:
   Connect to the configured QMI service using the given QMI
   control file

PARAMETERS:
   pControlFile   [ I ] - QMI control file

SEQUENCING:
   This method is sequenced according to the command event, i.e. any
   other thread that needs to send a command to the protocol server 
   thread will block until this method completes

RETURN VALUE:
   bool
===========================================================================*/
bool cQMIProtocolServer::Connect( LPCSTR pControlFile )
{
   // Assume failure
   bool bRC = false;
   if (IsValid( mService ) == false || mService == eQMI_SVC_CONTROL)
   {
      return bRC;
   }

   // Store the MEID
   mMEID = GetDeviceMEID( pControlFile );

   // Pass service file to base class for actual connection
   bRC = cProtocolServer::Connect( pControlFile );
   
   if (bRC == false)
   {
      TRACE( "QMI connect %d failed\n", mService );
   }
   
   return bRC;
}

/*===========================================================================
METHOD:
   GetDeviceMEID (Internal Method)

DESCRIPTION:
   Get device MEID by interfacing to the given QMI control file
  
PARAMETERS:
   deviceNode   [ I ] - QMI device node

SEQUENCING:
   None (must be called from protocol server thread)

RETURN VALUE:
   std::string (empty upon failure)
===========================================================================*/
std::string cQMIProtocolServer::GetDeviceMEID( std::string deviceNode )
{
   std::string retStr = "";
   
   int devHandle = open( deviceNode.c_str(), 0 );
   if (devHandle == INVALID_HANDLE_VALUE)
   {
      return retStr;
   }
   
   char devMEID[15];
   memset( &devMEID[0], 0, 15 );
   
   if (ioctl( devHandle, QMI_GET_MEID_IOCTL, &devMEID[0] ) != 0)
   {
      close( devHandle );
      return retStr;
   }
   
   // Enforce null
   devMEID[14] = 0;
   
   retStr = &devMEID[0];
   
   close( devHandle );
}

/*===========================================================================
METHOD:
   ValidateRequest (Internal Method)

DESCRIPTION:
   Validate a request that is about to be scheduled

SEQUENCING:
   This method is sequenced according to the command event, i.e. any
   other thread that needs to send a command to the protocol server 
   thread will block until this method completes

RETURN VALUE:
   bool
===========================================================================*/
bool cQMIProtocolServer::ValidateRequest( const sProtocolRequest & req )
{
   if (cProtocolServer::ValidateRequest( req ) == false)
   {
      return false;
   }

   sQMIServiceBuffer qmiReq( req.GetSharedBuffer() );
   return qmiReq.IsValid();
}

/*===========================================================================
METHOD:
   InitializeComm (Internal Method)

DESCRIPTION:
   Perform protocol specific communications port initialization

SEQUENCING:
   None (must be called from protocol server thread)

RETURN VALUE:
   bool
===========================================================================*/
bool cQMIProtocolServer::InitializeComm()
{
   // Setup the QMI Service type
   int result = mComm.RunIOCTL( QMI_GET_SERVICE_FILE_IOCTL, 
                                (void*)(unsigned long)mService );
   
   if (result == 0)
   {
      return true;
   }
   else
   {
      return false;
   }
}

/*===========================================================================
METHOD:
   CleanupComm (Internal Method)

DESCRIPTION:
   Perform protocol specific communications port cleanup

SEQUENCING:
   None (must be called from protocol server thread)

RETURN VALUE:
   bool
===========================================================================*/
bool cQMIProtocolServer::CleanupComm()
{
   mComm.RunIOCTL(0x8BE4, NULL);
   // Nothing to actually do here
   return true;
}

/*===========================================================================
METHOD:
   DecodeRxData (Internal Method)

DESCRIPTION:
   Decode incoming data into QMI indications/responses

PARAMETERS:
   bytesReceived  [ I ] - Number of bytes to decoded
   rspIdx         [ O ] - Log index of last valid response (not used)
   bAbortTx       [ O ] - Response aborts current transmission? (not used)

SEQUENCING:
   None (must be called from protocol server thread)

RETURN VALUE:
   bool - Was a response received?
===========================================================================*/
bool cQMIProtocolServer::DecodeRxData( 
   ULONG                      bytesReceived,
   ULONG &                    rspIdx,
   bool &                     bAbortTx )
{
   // Assume failure
   bool bRC = false;

   rspIdx = INVALID_LOG_INDEX;
   bAbortTx = false;

   // Something to decode from?
   if (bytesReceived == 0)
   {
      return bRC;
   }

   // Set protocol type (we have to be dealing with a valid QMI service)
   eProtocolType pt = MapQMIServiceToProtocol( mService, false );
   if (pt == ePROTOCOL_ENUM_BEGIN)
   {
      return bRC;
   }

   sSharedBuffer * pTmp = 0;
   pTmp = new sSharedBuffer( mpRxBuffer, bytesReceived, pt );
   if (pTmp != 0)
   {
      sQMIServiceBuffer tmpBuf( pTmp );
      if (tmpBuf.IsValid() == true)
      {
         rspIdx = mLog.AddBuffer( tmpBuf );
         if (IsResponse( tmpBuf ) == true)
         {
            bRC = true;
         }
         else
         {
            rspIdx = INVALID_LOG_INDEX;
         }
      }
   }

   return bRC;
}

/*===========================================================================
METHOD:
   EncodeTxData (Internal Method)

DESCRIPTION:
   Encode data for transmission

PARAMETERS:
   pBuffer        [ I ] - Data to be encoded
   bEncoded       [ O ] - Do we even encode data?

SEQUENCING:
   None (must be called from protocol server thread)

RETURN VALUE:
   sSharedBuffer * - Encoded data (0 upon error when encoding is indicated)
===========================================================================*/
sSharedBuffer * cQMIProtocolServer::EncodeTxData( 
   sSharedBuffer *            pBuffer,
   bool &                     bEncoded )
{
   WORD tid = ++mLastTID;
   if (tid == (WORD)INVALID_QMI_TRANSACTION_ID)
   {
      tid++;
   }

   sQMIServiceBuffer tmpBuf( pBuffer );
   tmpBuf.SetTransactionID( tid );
   
   // No actual encoding required as we alter the original request
   bEncoded = false;
   return 0;
};

/*===========================================================================
METHOD:
   IsResponse (Internal Method)

DESCRIPTION:
   Is the passed in data a response to the current request?

PARAMETERS:
   rsp         [ I ] - Candidate response

SEQUENCING:
   None (must be called from protocol server thread)

RETURN VALUE:
   bool
===========================================================================*/
bool cQMIProtocolServer::IsResponse( const sProtocolBuffer & rsp )
{
   // Assume not
   bool bRC = false;
   if ( (mpActiveRequest == 0) 
   ||   (mpActiveRequest->mRequest.IsValid() == false)
   ||   (mpActiveRequest->mbWaitingForResponse == false)
   ||   (rsp.IsValid() == false) )
   {
      return bRC;
   }

   sQMIServiceBuffer qmiReq( mpActiveRequest->mRequest.GetSharedBuffer() );
   sQMIServiceBuffer qmiRsp( rsp.GetSharedBuffer() );

   if (qmiReq.IsValid() == false || qmiRsp.IsValid() == false)
   {
      return bRC;
   }

   if (qmiRsp.IsResponse() == false)
   {
      return bRC;
   }

   WORD reqID = qmiReq.GetTransactionID();
   WORD rspID = qmiRsp.GetTransactionID();

   if ( (reqID == (WORD)INVALID_QMI_TRANSACTION_ID)
   ||   (rspID == (WORD)INVALID_QMI_TRANSACTION_ID)
   ||   (reqID != rspID) )
   {
      return bRC;
   }

   // Sadly there are documentated cases of firmware returning responses
   // with a matching transaction ID but a mismatching message ID.  There 
   // is no reason for this to be considered valid behavior as of yet
   ULONG reqMsgID = qmiReq.GetMessageID();
   ULONG rspMsgID = qmiRsp.GetMessageID();

   if (reqMsgID != rspMsgID)
   {
      return bRC;
   }

   bRC = true;
   return bRC; 
}