Debian Codegraph @

gobi-api/Gobi_2011-11-28-1533/Core/QMIBuffers.cpp

raw
1 /*===========================================================================
2 FILE:
3 QMIBuffers.cpp
4
5 DESCRIPTION:
6 QMI service protocol related structures and affliated methods
7
8 PUBLIC CLASSES AND METHODS:
9 sQMIControlRawTransactionHeader
10 sQMIServiceRawTransactionHeader
11 sQMIRawMessageHeader
12 sQMIRawContentHeader
13
14 sQMIServiceBuffer
15
16 Copyright (c) 2011, Code Aurora Forum. All rights reserved.
17
18 Redistribution and use in source and binary forms, with or without
19 modification, are permitted provided that the following conditions are met:
20 * Redistributions of source code must retain the above copyright
21 notice, this list of conditions and the following disclaimer.
22 * Redistributions in binary form must reproduce the above copyright
23 notice, this list of conditions and the following disclaimer in the
24 documentation and/or other materials provided with the distribution.
25 * Neither the name of Code Aurora Forum nor
26 the names of its contributors may be used to endorse or promote
27 products derived from this software without specific prior written
28 permission.
29
30
31 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
32 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
35 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
36 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
39 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 POSSIBILITY OF SUCH DAMAGE.
42 ===========================================================================*/
43
44 //---------------------------------------------------------------------------
45 // Include Files
46 //---------------------------------------------------------------------------
47 #include "StdAfx.h"
48 #include "QMIBuffers.h"
49
50 //---------------------------------------------------------------------------
51 // Definitions
52 //---------------------------------------------------------------------------
53
54 /*=========================================================================*/
55 // sQMIServiceBuffer Methods
56 /*=========================================================================*/
57
58 /*===========================================================================
59 METHOD:
60 sQMIServiceBuffer (Public Method)
61
62 DESCRIPTION:
63 Constructor
64
65 PARAMETERS:
66 pBuffer [ I ] - Shareable buffer that contains the DIAG data
67
68 RETURN VALUE:
69 None
70 ===========================================================================*/
71 sQMIServiceBuffer::sQMIServiceBuffer( sSharedBuffer * pBuffer )
72 : sProtocolBuffer( pBuffer )
73 {
74 sQMIServiceBuffer::Validate();
75 }
76
77 /*===========================================================================
78 METHOD:
79 ~sQMIServiceBuffer (Public Method)
80
81 DESCRIPTION:
82 Destructor
83
84 RETURN VALUE:
85 None
86 ===========================================================================*/
87 sQMIServiceBuffer::~sQMIServiceBuffer()
88 {
89 // Nothing to do
90 }
91
92 /*===========================================================================
93 METHOD:
94 GetResult (Public Method)
95
96 DESCRIPTION:
97 Return contents of mandatory result content
98
99 PARAMETERS:
100 returnCode [ I ] - The return code (should be eQMIResultCode)
101 errorCode [ I ] - The error code (should be eQMIErrorCode)
102
103 RETURN VALUE:
104 bool
105 ===========================================================================*/
106 bool sQMIServiceBuffer::GetResult(
107 ULONG & returnCode,
108 ULONG & errorCode )
109 {
110 if (IsResponse() == false)
111 {
112 return false;
113 }
114
115 std::map <ULONG, const sQMIRawContentHeader *>::const_iterator pIter;
116 pIter = mContents.find( QMI_TLV_ID_RESULT );
117 if (pIter == mContents.end())
118 {
119 return false;
120 }
121
122 const sQMIRawContentHeader * pContent = pIter->second;
123 if (pContent == 0)
124 {
125 ASSERT( 0 );
126 return false;
127 }
128
129 if (pContent->mLength != 4)
130 {
131 return false;
132 }
133
134 const WORD * pData = (const WORD *)(++pContent);
135
136 returnCode = (ULONG)*pData++;
137 errorCode = (ULONG)*pData;
138
139 return true;
140 }
141
142 /*===========================================================================
143 METHOD:
144 BuildBuffer (Static Public Method)
145
146 DESCRIPTION:
147 Build a QMI request
148
149 PARAMETERS:
150 serviceType [ I ] - QMI service type
151 msgID [ I ] - The QMI message request ID
152 bResponse [ I ] - Build a response?
153 bIndication [ I ] - Build an indication?
154 pPayload [ I ] - Payload
155 payloadLen [ I ] - Size of above payload
156
157 RETURN VALUE:
158 sSharedBuffer * : The request in an allocated buffer (0 on error)
159 ===========================================================================*/
160 sSharedBuffer * sQMIServiceBuffer::BuildBuffer(
161 eQMIService serviceType,
162 WORD msgID,
163 bool bResponse,
164 bool bIndication,
165 const BYTE * pPayload,
166 ULONG payloadLen )
167 {
168 const ULONG szTransHdr = (ULONG)sizeof(sQMIServiceRawTransactionHeader);
169 const ULONG szMsgHdr = (ULONG)sizeof(sQMIRawMessageHeader);
170 const ULONG totalHdrSz = szTransHdr + szMsgHdr;
171
172 // Truncate payload?
173 if (payloadLen > (QMI_MAX_BUFFER_SIZE - totalHdrSz))
174 {
175 payloadLen = QMI_MAX_BUFFER_SIZE - totalHdrSz;
176 }
177
178 // Make sure length agrees with pointer
179 if (pPayload == 0)
180 {
181 payloadLen = 0;
182 }
183
184 // Allocate buffer
185 PBYTE pBuffer = new BYTE[payloadLen + totalHdrSz];
186 if (pBuffer == 0)
187 {
188 return 0;
189 }
190
191 // Format header
192 sQMIServiceRawTransactionHeader * pHdr = 0;
193 pHdr = (sQMIServiceRawTransactionHeader *)&pBuffer[0];
194 pHdr->mCompound = 0;
195 pHdr->mResponse = 0;
196 pHdr->mIndication = 0;
197 pHdr->mReserved = 0;
198 pHdr->mTransactionID = 1;
199
200 bool bTX = true;
201 if (bResponse == true)
202 {
203 pHdr->mResponse = 1;
204 bTX = false;
205 }
206 else if (bIndication == true)
207 {
208 pHdr->mIndication = 1;
209 bTX = false;
210 }
211
212 pHdr++;
213
214 // Format message header
215 sQMIRawMessageHeader * pMsg = 0;
216 pMsg = (sQMIRawMessageHeader *)pHdr;
217 pMsg->mMessageID = msgID;
218 pMsg->mLength = (WORD)payloadLen;
219
220 // Copy in payload?
221 if (payloadLen > 0 && pPayload != 0)
222 {
223 memcpy( (LPVOID)&pBuffer[totalHdrSz],
224 (LPCVOID)&pPayload[0],
225 (SIZE_T)payloadLen );
226 }
227
228 // Compute total size
229 ULONG sz = payloadLen + totalHdrSz;
230
231 // Build and return the shared buffer
232 eProtocolType pt = MapQMIServiceToProtocol( serviceType, bTX );
233 sSharedBuffer * pBuf = new sSharedBuffer( sz, pBuffer, pt );
234 return pBuf;
235 }
236
237 /*===========================================================================
238 METHOD:
239 Validate (Internal Method)
240
241 DESCRIPTION:
242 Is this open unframed request/response packet valid?
243
244 RETURN VALUE:
245 bool
246 ===========================================================================*/
247 bool sQMIServiceBuffer::Validate()
248 {
249 // Assume failure
250 bool bRC = false;
251
252 // Sanity check protocol type
253 eProtocolType pt = GetType();
254 if (IsQMIProtocol( pt ) == false)
255 {
256 mbValid = bRC;
257 return bRC;
258 }
259
260 const ULONG szTransHdr = (ULONG)sizeof(sQMIServiceRawTransactionHeader);
261 const ULONG szMsgHdr = (ULONG)sizeof(sQMIRawMessageHeader);
262 const ULONG szContentHdr = (ULONG)sizeof(sQMIRawContentHeader);
263
264 // Must be enough space for both headers
265 ULONG sz = GetSize();
266 if (sz < szTransHdr + szMsgHdr)
267 {
268 mbValid = bRC;
269 return bRC;
270 }
271
272 const BYTE * pBuffer = GetBuffer();
273
274 // Obtain transaction header
275 const sQMIServiceRawTransactionHeader * pTransHdr = 0;
276 pTransHdr = (const sQMIServiceRawTransactionHeader *)pBuffer;
277 pBuffer += szTransHdr;
278
279 // This is required to be 0
280 if (pTransHdr->mCompound != 0)
281 {
282 mbValid = bRC;
283 return bRC;
284 }
285
286 // These are mutually exclusive
287 if (pTransHdr->mIndication == 1 && pTransHdr->mResponse == 1)
288 {
289 mbValid = bRC;
290 return bRC;
291 }
292
293 // Requests/responses required valid transaction IDs
294 if ( (pTransHdr->mIndication == 0)
295 && (pTransHdr->mTransactionID == (WORD)INVALID_QMI_TRANSACTION_ID) )
296 {
297 mbValid = bRC;
298 return bRC;
299 }
300
301 if ( (pTransHdr->mResponse == 1 || pTransHdr->mIndication == 1)
302 && (IsQMIProtocolRX( pt ) == false) )
303 {
304 mbValid = bRC;
305 return bRC;
306 }
307
308 if ( (pTransHdr->mResponse == 0 && pTransHdr->mIndication == 0)
309 && (IsQMIProtocolTX( pt ) == false) )
310 {
311 mbValid = bRC;
312 return bRC;
313 }
314
315 // Obtain message header
316 const sQMIRawMessageHeader * pMsgHdr = 0;
317 pMsgHdr = (const sQMIRawMessageHeader *)pBuffer;
318 pBuffer += szMsgHdr;
319
320 // Validate reported length
321 if (sz != ((ULONG)pMsgHdr->mLength + szTransHdr + szMsgHdr))
322 {
323 mbValid = bRC;
324 return bRC;
325 }
326
327 // Extract content TLV structures
328 ULONG contentProcessed = 0;
329 ULONG contentSz = (ULONG)pMsgHdr->mLength;
330 while (contentProcessed < contentSz)
331 {
332 const sQMIRawContentHeader * pContent = 0;
333 pContent = (const sQMIRawContentHeader *)pBuffer;
334
335 ULONG tlvLen = szContentHdr + pContent->mLength;
336
337 contentProcessed += tlvLen;
338 if (contentProcessed <= contentSz)
339 {
340 mContents[(ULONG)pContent->mTypeID] = pContent;
341 }
342 else
343 {
344 mContents.clear();
345
346 mbValid = bRC;
347 return bRC;
348 }
349
350 pBuffer += tlvLen;
351 }
352
353 // Validate TLV reported lengths
354 if (contentProcessed != contentSz)
355 {
356 mbValid = bRC;
357 return bRC;
358 }
359
360 // Success!
361 bRC = true;
362
363 mbValid = bRC;
364 return mbValid;
365 }
366