Debian Codegraph @

gobi-api/GobiAPI_2012-07-12-1036/Core/QMIProtocolServer.cpp

raw
1 /*===========================================================================
2 FILE:
3 QMIProtocolServer.cpp
4
5 DESCRIPTION:
6 QMI protocol server
7
8 PUBLIC CLASSES AND METHODS:
9 cQMIProtocolServer
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 "QMIProtocolServer.h"
44 #include "QMIBuffers.h"
45 #include "Comm.h"
46 #include "Socket.h"
47
48 /*=========================================================================*/
49 // cQMIProtocolServer Methods
50 /*=========================================================================*/
51
52 /*===========================================================================
53 METHOD:
54 cQMIProtocolServer (Public Method)
55
56 DESCRIPTION:
57 Constructor
58
59 PARAMETERS:
60 serviceType [ I ] - QMI service type requested
61 bufferSzRx [ I ] - Size of data buffer for incoming data
62 logSz [ I ] - Size of log (number of buffers)
63
64 SEQUENCING:
65 None (constructs sequencing objects)
66
67 RETURN VALUE:
68 None
69 ===========================================================================*/
70 cQMIProtocolServer::cQMIProtocolServer(
71 eQMIService serviceType,
72 ULONG bufferSzRx,
73 ULONG logSz )
74 : cProtocolServer( MapQMIServiceToProtocol( serviceType, false ),
75 MapQMIServiceToProtocol( serviceType, true ),
76 bufferSzRx,
77 logSz ),
78 mLastTID( (WORD)INVALID_QMI_TRANSACTION_ID ),
79 mService( serviceType )
80 {
81 // Nothing to do
82 }
83
84 /*===========================================================================
85 METHOD:
86 ~cQMIProtocolServer (Public Method)
87
88 DESCRIPTION:
89 Destructor
90
91 SEQUENCING:
92 None (constructs sequencing objects)
93
94 RETURN VALUE:
95 None
96 ===========================================================================*/
97 cQMIProtocolServer::~cQMIProtocolServer()
98 {
99 // Nothing to do
100 }
101
102 /*===========================================================================
103 METHOD:
104 Connect (Public Method)
105
106 DESCRIPTION:
107 Connect to the configured QMI service using the given QMI
108 control file
109
110 PARAMETERS:
111 pControlFile [ I ] - QMI control file
112
113 SEQUENCING:
114 This method is sequenced according to the command event, i.e. any
115 other thread that needs to send a command to the protocol server
116 thread will block until this method completes
117
118 RETURN VALUE:
119 bool
120 ===========================================================================*/
121 bool cQMIProtocolServer::Connect( LPCSTR pControlFile )
122 {
123 // Assume failure
124 bool bRC = false;
125 if (IsValid( mService ) == false || mService == eQMI_SVC_CONTROL)
126 {
127 return bRC;
128 }
129
130 std::string name = pControlFile;
131 if (name.find( "qcqmi" ) != std::string::npos)
132 {
133 mpConnection = new cComm();
134 mConnectionType = eConnectionType_RmNet;
135 }
136 else
137 {
138 // SMD
139
140 if (name.find( "QMUXD:" ) == 0)
141 {
142 // Remove qualifier
143 name = name.substr( 6 );
144 }
145
146 mpConnection = new cSocket();
147 mConnectionType = eConnectionType_SMD;
148 }
149
150 // Pass service file to base class for actual connection
151 bRC = cProtocolServer::Connect( name.c_str() );
152
153 if (bRC == false)
154 {
155 TRACE( "QMI connect %d failed\n", mService );
156 }
157
158 return bRC;
159 }
160
161 /*===========================================================================
162 METHOD:
163 ValidateRequest (Internal Method)
164
165 DESCRIPTION:
166 Validate a request that is about to be scheduled
167
168 SEQUENCING:
169 This method is sequenced according to the command event, i.e. any
170 other thread that needs to send a command to the protocol server
171 thread will block until this method completes
172
173 RETURN VALUE:
174 bool
175 ===========================================================================*/
176 bool cQMIProtocolServer::ValidateRequest( const sProtocolRequest & req )
177 {
178 if (cProtocolServer::ValidateRequest( req ) == false)
179 {
180 return false;
181 }
182
183 sQMIServiceBuffer qmiReq( req.GetSharedBuffer() );
184 return qmiReq.IsValid();
185 }
186
187 /*===========================================================================
188 METHOD:
189 InitializeComm (Internal Method)
190
191 DESCRIPTION:
192 Perform protocol specific communications port initialization
193
194 SEQUENCING:
195 None (must be called from protocol server thread)
196
197 RETURN VALUE:
198 bool
199 ===========================================================================*/
200 bool cQMIProtocolServer::InitializeComm()
201 {
202 bool bResult = false;
203 if (mpConnection == 0)
204 {
205 return bResult;
206 }
207
208 if (mConnectionType == eConnectionType_RmNet)
209 {
210 // Setup the QMI Service type
211 int result = mpConnection->SendCtl( QMI_GET_SERVICE_FILE_IOCTL,
212 (void*)(unsigned long)mService );
213 bResult = (result == 0);
214 }
215 else if (mConnectionType == eConnectionType_SMD)
216 {
217 // Setup the QMI Service type
218 int result = mpConnection->SendCtl( eQMUXD_MSG_ALLOC_QMI_CLIENT_ID,
219 (void*)&mService );
220 bResult = (result == 0);
221 }
222
223 return bResult;
224 }
225
226 /*===========================================================================
227 METHOD:
228 CleanupComm (Internal Method)
229
230 DESCRIPTION:
231 Perform protocol specific communications port cleanup
232
233 SEQUENCING:
234 None (must be called from protocol server thread)
235
236 RETURN VALUE:
237 bool
238 ===========================================================================*/
239 bool cQMIProtocolServer::CleanupComm()
240 {
241 bool bResult = true;
242
243 if (mpConnection != 0
244 && mConnectionType == eConnectionType_SMD)
245 {
246 // Delete the QMI client
247
248 // NOTE: This is generally unnecessary, when you close the channel
249 // it is deleted
250 int result = mpConnection->SendCtl( eQMUXD_MSG_RELEASE_QMI_CLIENT_ID,
251 0 );
252 bResult = (result == 0);
253 }
254
255 // Nothing to actually do here
256 return true;
257 }
258
259 /*===========================================================================
260 METHOD:
261 DecodeRxData (Internal Method)
262
263 DESCRIPTION:
264 Decode incoming data into QMI indications/responses
265
266 PARAMETERS:
267 bytesReceived [ I ] - Number of bytes to decoded
268 rspIdx [ O ] - Log index of last valid response (not used)
269 bAbortTx [ O ] - Response aborts current transmission? (not used)
270
271 SEQUENCING:
272 None (must be called from protocol server thread)
273
274 RETURN VALUE:
275 bool - Was a response received?
276 ===========================================================================*/
277 bool cQMIProtocolServer::DecodeRxData(
278 ULONG bytesReceived,
279 ULONG & rspIdx,
280 bool & bAbortTx )
281 {
282 // Assume failure
283 bool bRC = false;
284
285 rspIdx = INVALID_LOG_INDEX;
286 bAbortTx = false;
287
288 // Something to decode from?
289 if (bytesReceived == 0)
290 {
291 return bRC;
292 }
293
294 // Set protocol type (we have to be dealing with a valid QMI service)
295 eProtocolType pt = MapQMIServiceToProtocol( mService, false );
296 if (pt == ePROTOCOL_ENUM_BEGIN)
297 {
298 return bRC;
299 }
300
301 sSharedBuffer * pTmp = 0;
302 pTmp = new sSharedBuffer( mpRxBuffer, bytesReceived, pt );
303 if (pTmp != 0)
304 {
305 sQMIServiceBuffer tmpBuf( pTmp );
306 if (tmpBuf.IsValid() == true)
307 {
308 rspIdx = mLog.AddBuffer( tmpBuf );
309 if (IsResponse( tmpBuf ) == true)
310 {
311 bRC = true;
312 }
313 else
314 {
315 rspIdx = INVALID_LOG_INDEX;
316 }
317 }
318 }
319
320 return bRC;
321 }
322
323 /*===========================================================================
324 METHOD:
325 EncodeTxData (Internal Method)
326
327 DESCRIPTION:
328 Encode data for transmission
329
330 PARAMETERS:
331 pBuffer [ I ] - Data to be encoded
332 bEncoded [ O ] - Do we even encode data?
333
334 SEQUENCING:
335 None (must be called from protocol server thread)
336
337 RETURN VALUE:
338 sSharedBuffer * - Encoded data (0 upon error when encoding is indicated)
339 ===========================================================================*/
340 sSharedBuffer * cQMIProtocolServer::EncodeTxData(
341 sSharedBuffer * pBuffer,
342 bool & bEncoded )
343 {
344 WORD tid = ++mLastTID;
345 if (tid == (WORD)INVALID_QMI_TRANSACTION_ID)
346 {
347 tid++;
348 }
349
350 sQMIServiceBuffer tmpBuf( pBuffer );
351 tmpBuf.SetTransactionID( tid );
352
353 // No actual encoding required as we alter the original request
354 bEncoded = false;
355 return 0;
356 };
357
358 /*===========================================================================
359 METHOD:
360 IsResponse (Internal Method)
361
362 DESCRIPTION:
363 Is the passed in data a response to the current request?
364
365 PARAMETERS:
366 rsp [ I ] - Candidate response
367
368 SEQUENCING:
369 None (must be called from protocol server thread)
370
371 RETURN VALUE:
372 bool
373 ===========================================================================*/
374 bool cQMIProtocolServer::IsResponse( const sProtocolBuffer & rsp )
375 {
376 // Assume not
377 bool bRC = false;
378 if ( (mpActiveRequest == 0)
379 || (mpActiveRequest->mRequest.IsValid() == false)
380 || (mpActiveRequest->mbWaitingForResponse == false)
381 || (rsp.IsValid() == false) )
382 {
383 return bRC;
384 }
385
386 sQMIServiceBuffer qmiReq( mpActiveRequest->mRequest.GetSharedBuffer() );
387 sQMIServiceBuffer qmiRsp( rsp.GetSharedBuffer() );
388
389 if (qmiReq.IsValid() == false || qmiRsp.IsValid() == false)
390 {
391 return bRC;
392 }
393
394 if (qmiRsp.IsResponse() == false)
395 {
396 return bRC;
397 }
398
399 WORD reqID = qmiReq.GetTransactionID();
400 WORD rspID = qmiRsp.GetTransactionID();
401
402 if ( (reqID == (WORD)INVALID_QMI_TRANSACTION_ID)
403 || (rspID == (WORD)INVALID_QMI_TRANSACTION_ID)
404 || (reqID != rspID) )
405 {
406 return bRC;
407 }
408
409 // Sadly there are documentated cases of firmware returning responses
410 // with a matching transaction ID but a mismatching message ID. There
411 // is no reason for this to be considered valid behavior as of yet
412 ULONG reqMsgID = qmiReq.GetMessageID();
413 ULONG rspMsgID = qmiRsp.GetMessageID();
414
415 if (reqMsgID != rspMsgID)
416 {
417 return bRC;
418 }
419
420 bRC = true;
421 return bRC;
422 }