Debian Codegraph @

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