Debian Codegraph @

gobi-api/Gobi_2012-06-18-1054/Core/QMIProtocolServer.cpp

raw
1 /*===========================================================================
2 FILE:
3 QMIProtocolServer.h
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 mpConnection = new cSocket();
140 mConnectionType = eConnectionType_SMD;
141 }
142
143 // Pass service file to base class for actual connection
144 bRC = cProtocolServer::Connect( pControlFile );
145
146 if (bRC == false)
147 {
148 TRACE( "QMI connect %d failed\n", mService );
149 }
150
151 return bRC;
152 }
153
154 /*===========================================================================
155 METHOD:
156 ValidateRequest (Internal Method)
157
158 DESCRIPTION:
159 Validate a request that is about to be scheduled
160
161 SEQUENCING:
162 This method is sequenced according to the command event, i.e. any
163 other thread that needs to send a command to the protocol server
164 thread will block until this method completes
165
166 RETURN VALUE:
167 bool
168 ===========================================================================*/
169 bool cQMIProtocolServer::ValidateRequest( const sProtocolRequest & req )
170 {
171 if (cProtocolServer::ValidateRequest( req ) == false)
172 {
173 return false;
174 }
175
176 sQMIServiceBuffer qmiReq( req.GetSharedBuffer() );
177 return qmiReq.IsValid();
178 }
179
180 /*===========================================================================
181 METHOD:
182 InitializeComm (Internal Method)
183
184 DESCRIPTION:
185 Perform protocol specific communications port initialization
186
187 SEQUENCING:
188 None (must be called from protocol server thread)
189
190 RETURN VALUE:
191 bool
192 ===========================================================================*/
193 bool cQMIProtocolServer::InitializeComm()
194 {
195 bool bResult = false;
196 if (mpConnection == 0)
197 {
198 return bResult;
199 }
200
201 if (mConnectionType == eConnectionType_RmNet)
202 {
203 // Setup the QMI Service type
204 int result = mpConnection->SendCtl( QMI_GET_SERVICE_FILE_IOCTL,
205 (void*)(unsigned long)mService );
206 bResult = (result == 0);
207 }
208 else if (mConnectionType == eConnectionType_SMD)
209 {
210 // Setup the QMI Service type
211 int result = mpConnection->SendCtl( eQMUXD_MSG_ALLOC_QMI_CLIENT_ID,
212 (void*)&mService );
213 bResult = (result == 0);
214 }
215
216 return bResult;
217 }
218
219 /*===========================================================================
220 METHOD:
221 CleanupComm (Internal Method)
222
223 DESCRIPTION:
224 Perform protocol specific communications port cleanup
225
226 SEQUENCING:
227 None (must be called from protocol server thread)
228
229 RETURN VALUE:
230 bool
231 ===========================================================================*/
232 bool cQMIProtocolServer::CleanupComm()
233 {
234 bool bResult = true;
235
236 if (mpConnection != 0
237 && mConnectionType == eConnectionType_SMD)
238 {
239 // Delete the QMI client
240
241 // NOTE: This is generally unnecessary, when you close the channel
242 // it is deleted
243 int result = mpConnection->SendCtl( eQMUXD_MSG_RELEASE_QMI_CLIENT_ID,
244 0 );
245 bResult = (result == 0);
246 }
247
248 // Nothing to actually do here
249 return true;
250 }
251
252 /*===========================================================================
253 METHOD:
254 DecodeRxData (Internal Method)
255
256 DESCRIPTION:
257 Decode incoming data into QMI indications/responses
258
259 PARAMETERS:
260 bytesReceived [ I ] - Number of bytes to decoded
261 rspIdx [ O ] - Log index of last valid response (not used)
262 bAbortTx [ O ] - Response aborts current transmission? (not used)
263
264 SEQUENCING:
265 None (must be called from protocol server thread)
266
267 RETURN VALUE:
268 bool - Was a response received?
269 ===========================================================================*/
270 bool cQMIProtocolServer::DecodeRxData(
271 ULONG bytesReceived,
272 ULONG & rspIdx,
273 bool & bAbortTx )
274 {
275 // Assume failure
276 bool bRC = false;
277
278 rspIdx = INVALID_LOG_INDEX;
279 bAbortTx = false;
280
281 // Something to decode from?
282 if (bytesReceived == 0)
283 {
284 return bRC;
285 }
286
287 // Set protocol type (we have to be dealing with a valid QMI service)
288 eProtocolType pt = MapQMIServiceToProtocol( mService, false );
289 if (pt == ePROTOCOL_ENUM_BEGIN)
290 {
291 return bRC;
292 }
293
294 sSharedBuffer * pTmp = 0;
295 pTmp = new sSharedBuffer( mpRxBuffer, bytesReceived, pt );
296 if (pTmp != 0)
297 {
298 sQMIServiceBuffer tmpBuf( pTmp );
299 if (tmpBuf.IsValid() == true)
300 {
301 rspIdx = mLog.AddBuffer( tmpBuf );
302 if (IsResponse( tmpBuf ) == true)
303 {
304 bRC = true;
305 }
306 else
307 {
308 rspIdx = INVALID_LOG_INDEX;
309 }
310 }
311 }
312
313 return bRC;
314 }
315
316 /*===========================================================================
317 METHOD:
318 EncodeTxData (Internal Method)
319
320 DESCRIPTION:
321 Encode data for transmission
322
323 PARAMETERS:
324 pBuffer [ I ] - Data to be encoded
325 bEncoded [ O ] - Do we even encode data?
326
327 SEQUENCING:
328 None (must be called from protocol server thread)
329
330 RETURN VALUE:
331 sSharedBuffer * - Encoded data (0 upon error when encoding is indicated)
332 ===========================================================================*/
333 sSharedBuffer * cQMIProtocolServer::EncodeTxData(
334 sSharedBuffer * pBuffer,
335 bool & bEncoded )
336 {
337 WORD tid = ++mLastTID;
338 if (tid == (WORD)INVALID_QMI_TRANSACTION_ID)
339 {
340 tid++;
341 }
342
343 sQMIServiceBuffer tmpBuf( pBuffer );
344 tmpBuf.SetTransactionID( tid );
345
346 // No actual encoding required as we alter the original request
347 bEncoded = false;
348 return 0;
349 };
350
351 /*===========================================================================
352 METHOD:
353 IsResponse (Internal Method)
354
355 DESCRIPTION:
356 Is the passed in data a response to the current request?
357
358 PARAMETERS:
359 rsp [ I ] - Candidate response
360
361 SEQUENCING:
362 None (must be called from protocol server thread)
363
364 RETURN VALUE:
365 bool
366 ===========================================================================*/
367 bool cQMIProtocolServer::IsResponse( const sProtocolBuffer & rsp )
368 {
369 // Assume not
370 bool bRC = false;
371 if ( (mpActiveRequest == 0)
372 || (mpActiveRequest->mRequest.IsValid() == false)
373 || (mpActiveRequest->mbWaitingForResponse == false)
374 || (rsp.IsValid() == false) )
375 {
376 return bRC;
377 }
378
379 sQMIServiceBuffer qmiReq( mpActiveRequest->mRequest.GetSharedBuffer() );
380 sQMIServiceBuffer qmiRsp( rsp.GetSharedBuffer() );
381
382 if (qmiReq.IsValid() == false || qmiRsp.IsValid() == false)
383 {
384 return bRC;
385 }
386
387 if (qmiRsp.IsResponse() == false)
388 {
389 return bRC;
390 }
391
392 WORD reqID = qmiReq.GetTransactionID();
393 WORD rspID = qmiRsp.GetTransactionID();
394
395 if ( (reqID == (WORD)INVALID_QMI_TRANSACTION_ID)
396 || (rspID == (WORD)INVALID_QMI_TRANSACTION_ID)
397 || (reqID != rspID) )
398 {
399 return bRC;
400 }
401
402 // Sadly there are documentated cases of firmware returning responses
403 // with a matching transaction ID but a mismatching message ID. There
404 // is no reason for this to be considered valid behavior as of yet
405 ULONG reqMsgID = qmiReq.GetMessageID();
406 ULONG rspMsgID = qmiRsp.GetMessageID();
407
408 if (reqMsgID != rspMsgID)
409 {
410 return bRC;
411 }
412
413 bRC = true;
414 return bRC;
415 }