26
|
1 |
// Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|
2 |
// All rights reserved.
|
|
3 |
// This component and the accompanying materials are made available
|
|
4 |
// under the terms of "Eclipse Public License v1.0"
|
|
5 |
// which accompanies this distribution, and is available
|
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
7 |
//
|
|
8 |
// Initial Contributors:
|
|
9 |
// Nokia Corporation - initial contribution.
|
|
10 |
//
|
|
11 |
// Contributors:
|
|
12 |
//
|
|
13 |
// Description:
|
|
14 |
// This file contains the implementation of the AT command parser and common utilities
|
|
15 |
//
|
|
16 |
//
|
|
17 |
|
|
18 |
#include "atcommandparser.h"
|
|
19 |
#include <hash.h>
|
|
20 |
|
|
21 |
#include "debug.h"
|
32
|
22 |
#include "atmisccmdpluginconsts.h"
|
26
|
23 |
|
|
24 |
TAtCommandParser::TAtCommandParser()
|
|
25 |
: iCmdType(EUnknown), iCmdHandlerType(ECmdHandlerTypeUndefined)
|
|
26 |
{
|
32
|
27 |
TRACE_FUNC_ENTRY
|
|
28 |
TRACE_FUNC_EXIT
|
26
|
29 |
}
|
|
30 |
|
|
31 |
TAtCommandParser::TAtCommandParser(const TDesC8& aCmd)
|
|
32 |
: iCmdType(EUnknown), iCmdHandlerType(ECmdHandlerTypeUndefined)
|
|
33 |
{
|
32
|
34 |
TRACE_FUNC_ENTRY
|
26
|
35 |
ParseAtCommand(aCmd);
|
32
|
36 |
TRACE_FUNC_EXIT
|
26
|
37 |
}
|
|
38 |
|
|
39 |
void TAtCommandParser::ParseAtCommand(const TDesC8& aCmd)
|
|
40 |
{
|
|
41 |
TRACE_FUNC_ENTRY
|
|
42 |
iCmd.Assign(aCmd);
|
|
43 |
iCmd.Mark();
|
|
44 |
TChar c = 0;
|
|
45 |
// First extract the AT command "AT+COMMAND"
|
|
46 |
while(!iCmd.Eos() && !(c=='=' || c=='?'))
|
|
47 |
{
|
|
48 |
iCmd.Inc();
|
|
49 |
c = iCmd.Peek();
|
|
50 |
}
|
|
51 |
|
|
52 |
TPtrC8 token = iCmd.MarkedToken();
|
|
53 |
|
|
54 |
_LIT8(KAtCfun, "AT+CFUN");
|
|
55 |
_LIT8(KAtCbc, "AT+CBC");
|
|
56 |
_LIT8(KAtClck, "AT+CLCK");
|
|
57 |
_LIT8(KAtCpwd, "AT+CPWD");
|
|
58 |
_LIT8(KAtCpin, "AT+CPIN");
|
|
59 |
_LIT8(KAtCusd, "AT+CUSD");
|
|
60 |
_LIT8(KAtCnum, "AT+CNUM");
|
45
|
61 |
_LIT8(KAtCmee, "AT+CMEE");
|
|
62 |
#ifdef PROTOCOL_TDSCDMA
|
|
63 |
_LIT8(KAtHver, "AT^HVER");
|
|
64 |
_LIT8(KAtCgsn, "AT+CGSN");
|
|
65 |
_LIT8(KAtCgmr, "AT+CGMR");
|
|
66 |
_LIT8(KAtCgmi, "AT+CGMI");
|
|
67 |
_LIT8(KAtCmgw, "AT+CMGW");
|
|
68 |
_LIT8(KAtCmgd, "AT+CMGD");
|
|
69 |
_LIT8(KAtCmgf, "AT+CMGF");
|
|
70 |
#endif
|
26
|
71 |
|
|
72 |
Trace(KDebugPrintS, "token: ", &token);
|
|
73 |
// Determine the AT command type
|
32
|
74 |
if(!token.CompareF(KAtCfun))
|
26
|
75 |
{
|
|
76 |
iCmdType = ECmdAtCfun;
|
|
77 |
}
|
32
|
78 |
else if(!token.CompareF(KAtCbc))
|
26
|
79 |
{
|
|
80 |
iCmdType = ECmdAtCbc;
|
|
81 |
}
|
32
|
82 |
else if(!token.CompareF(KAtClck))
|
26
|
83 |
{
|
|
84 |
iCmdType = ECmdAtClck;
|
|
85 |
}
|
32
|
86 |
else if(!token.CompareF(KAtCpwd))
|
26
|
87 |
{
|
|
88 |
iCmdType = ECmdAtCpwd;
|
|
89 |
}
|
32
|
90 |
else if(!token.CompareF(KAtCpin))
|
26
|
91 |
{
|
|
92 |
iCmdType = ECmdAtCpin;
|
|
93 |
}
|
32
|
94 |
else if(!token.CompareF(KAtCusd))
|
26
|
95 |
{
|
|
96 |
iCmdType = ECmdAtCusd;
|
|
97 |
}
|
32
|
98 |
else if(!token.CompareF(KAtCnum))
|
26
|
99 |
{
|
|
100 |
iCmdType = ECmdAtCnum;
|
|
101 |
}
|
32
|
102 |
else if(!token.Compare(KAtCmee))
|
|
103 |
{
|
|
104 |
iCmdType = ECmdAtCmee;
|
45
|
105 |
}
|
|
106 |
#ifdef PROTOCOL_TDSCDMA
|
|
107 |
else if(!token.CompareF(KAtHver))
|
|
108 |
{
|
|
109 |
iCmdType = ECmdAtHver;
|
32
|
110 |
}
|
45
|
111 |
else if(!token.CompareF(KAtCgsn))
|
|
112 |
{
|
|
113 |
iCmdType = ECmdAtCgsn;
|
|
114 |
}
|
|
115 |
else if(!token.CompareF(KAtCgmr))
|
|
116 |
{
|
|
117 |
iCmdType = ECmdAtCgmr;
|
|
118 |
}
|
|
119 |
else if(!token.CompareF(KAtCgmi))
|
|
120 |
{
|
|
121 |
iCmdType = ECmdAtCgmi;
|
|
122 |
}
|
|
123 |
else if(!token.CompareF(KAtCmgw))
|
|
124 |
{
|
|
125 |
iCmdType = ECmdAtCmgw;
|
|
126 |
}
|
|
127 |
else if(!token.CompareF(KAtCmgd))
|
|
128 |
{
|
|
129 |
iCmdType = ECmdAtCmgd;
|
|
130 |
}
|
|
131 |
else if(!token.CompareF(KAtCmgf))
|
|
132 |
{
|
|
133 |
iCmdType = ECmdAtCmgf;
|
|
134 |
}
|
|
135 |
#endif
|
26
|
136 |
else
|
|
137 |
{
|
|
138 |
iCmdType = EUnknown;
|
|
139 |
TRACE_FUNC_EXIT
|
|
140 |
return;
|
|
141 |
}
|
|
142 |
|
|
143 |
// Now find out the AT command handler type
|
|
144 |
if(iCmd.Eos())
|
|
145 |
{
|
|
146 |
iCmdHandlerType = ECmdHandlerTypeBase;
|
|
147 |
}
|
|
148 |
else if(iCmd.Peek() == '?')
|
|
149 |
{
|
|
150 |
iCmdHandlerType = ECmdHandlerTypeRead;
|
|
151 |
}
|
|
152 |
else if(iCmd.Peek() == '=')
|
|
153 |
{
|
|
154 |
iCmd.Inc();
|
|
155 |
if(iCmd.Peek() == '?')
|
|
156 |
{
|
|
157 |
iCmd.Inc();
|
|
158 |
iCmdHandlerType = ECmdHandlerTypeTest;
|
|
159 |
}
|
|
160 |
else
|
|
161 |
{
|
|
162 |
iCmdHandlerType = ECmdHandlerTypeSet;
|
|
163 |
}
|
|
164 |
}
|
|
165 |
else
|
|
166 |
{
|
|
167 |
iCmdHandlerType = ECmdHandlerTypeUndefined;
|
|
168 |
}
|
|
169 |
TRACE_FUNC_EXIT
|
|
170 |
}
|
|
171 |
|
|
172 |
TAtCommandParser::TCommandType TAtCommandParser::Command() const
|
|
173 |
{
|
32
|
174 |
TRACE_FUNC_ENTRY
|
|
175 |
TRACE_FUNC_EXIT
|
26
|
176 |
return iCmdType;
|
|
177 |
}
|
|
178 |
|
|
179 |
TAtCommandParser::TCommandHandlerType TAtCommandParser::CommandHandlerType() const
|
|
180 |
{
|
32
|
181 |
TRACE_FUNC_ENTRY
|
|
182 |
TRACE_FUNC_EXIT
|
26
|
183 |
return iCmdHandlerType;
|
|
184 |
}
|
|
185 |
|
|
186 |
TPtrC8 TAtCommandParser::NextParam()
|
|
187 |
{
|
|
188 |
TRACE_FUNC_ENTRY
|
|
189 |
iCmd.SkipSpace(); // Skip front spaces
|
|
190 |
iCmd.Mark();
|
|
191 |
TChar chr = 0;
|
|
192 |
|
|
193 |
if(!iCmd.Eos())
|
|
194 |
{
|
|
195 |
chr = iCmd.Peek();
|
|
196 |
while(!iCmd.Eos() && chr != ',' && !chr.IsSpace() && !chr.IsControl())
|
|
197 |
{// Stop at any of those chars: comma, space or control
|
|
198 |
iCmd.Inc();
|
|
199 |
chr = iCmd.Peek();
|
|
200 |
}
|
|
201 |
}
|
|
202 |
|
|
203 |
// Extract the token at this point
|
|
204 |
TPtrC8 retVal = iCmd.MarkedToken();
|
|
205 |
|
|
206 |
// Skip comma, space and control chars
|
|
207 |
while(!iCmd.Eos() && (chr == ',' || chr.IsSpace() || chr.IsControl()))
|
|
208 |
{
|
|
209 |
iCmd.Inc();
|
|
210 |
chr = iCmd.Peek();
|
|
211 |
}
|
|
212 |
TRACE_FUNC_EXIT
|
|
213 |
return retVal;
|
|
214 |
}
|
|
215 |
|
32
|
216 |
TInt TAtCommandParser::NextTextParam(TPtrC8& aParam)
|
26
|
217 |
{
|
32
|
218 |
TRACE_FUNC_ENTRY
|
|
219 |
TInt ret = KErrNone;
|
26
|
220 |
TPtrC8 param = NextParam();
|
|
221 |
|
32
|
222 |
if (param.Length() == 0)
|
26
|
223 |
{
|
32
|
224 |
aParam.Set(NULL,0);
|
|
225 |
ret = KErrNotFound;
|
26
|
226 |
}
|
32
|
227 |
else if(param.Length() < 2 || param[0] != '"'
|
26
|
228 |
|| param[param.Length()-1] != '"')
|
|
229 |
{
|
32
|
230 |
aParam.Set(NULL,0);
|
|
231 |
ret = KErrArgument;
|
26
|
232 |
}
|
32
|
233 |
else
|
|
234 |
{
|
|
235 |
aParam.Set(param.Mid(1, param.Length() - 2));
|
|
236 |
}
|
|
237 |
TRACE_FUNC_EXIT
|
|
238 |
return ret;
|
26
|
239 |
}
|
|
240 |
|
|
241 |
TInt TAtCommandParser::NextIntParam(TInt& aValue)
|
|
242 |
{
|
|
243 |
TRACE_FUNC_ENTRY
|
|
244 |
TInt retVal =KErrNone;
|
|
245 |
TPtrC8 param = NextParam();
|
32
|
246 |
if (param.Length() == 0)
|
26
|
247 |
{
|
|
248 |
retVal = KErrNotFound;
|
|
249 |
}
|
|
250 |
else
|
|
251 |
{
|
|
252 |
TLex8 lex(param);
|
|
253 |
retVal = lex.Val(aValue);
|
|
254 |
}
|
|
255 |
TRACE_FUNC_EXIT
|
|
256 |
return retVal;
|
|
257 |
}
|
|
258 |
|
|
259 |
TInt TAtCommandParser::HashSecurityCode(const TDesC8& aPasscode, TDes8& aHashCode)
|
|
260 |
{
|
|
261 |
TRACE_FUNC_ENTRY
|
|
262 |
TInt ret = KErrNone;
|
|
263 |
|
|
264 |
// Get MD5 Hash
|
|
265 |
// see remotemgmt component CSCPServer::HashISACode() for encoding algorithm
|
|
266 |
CMD5* hashObject = NULL;
|
|
267 |
TRAP( ret, hashObject = CMD5::NewL() );
|
|
268 |
|
|
269 |
if(ret != KErrNone)
|
|
270 |
{
|
|
271 |
TRACE_FUNC_EXIT
|
|
272 |
return ret;
|
|
273 |
}
|
|
274 |
|
|
275 |
RBuf pwdBuffer;
|
|
276 |
ret = pwdBuffer.Create(KSCPMaxHashLength);
|
|
277 |
if(ret != KErrNone)
|
|
278 |
{
|
|
279 |
delete hashObject;
|
|
280 |
TRACE_FUNC_EXIT
|
|
281 |
return ret;
|
|
282 |
}
|
|
283 |
pwdBuffer.Copy(aPasscode); // convert to TDes16
|
|
284 |
|
|
285 |
// add TDes16 to a binary buffer
|
|
286 |
TUint16* inputPtr = const_cast<TUint16*>( pwdBuffer.Ptr() );
|
|
287 |
TPtrC8 inputData( reinterpret_cast<TUint8*>(inputPtr), pwdBuffer.Length()*2 );
|
|
288 |
|
|
289 |
TPtrC8 hash = hashObject->Final( inputData );
|
|
290 |
delete hashObject;
|
|
291 |
|
|
292 |
pwdBuffer.Zero();
|
|
293 |
pwdBuffer.Copy(hash);
|
|
294 |
|
|
295 |
// Compute the hash sum as four 32-bit integers.
|
|
296 |
TInt64 hashSum = *(reinterpret_cast<TInt32*>(&pwdBuffer[0])) +
|
|
297 |
*(reinterpret_cast<TInt32*>(&pwdBuffer[4])) +
|
|
298 |
*(reinterpret_cast<TInt32*>(&pwdBuffer[8])) +
|
|
299 |
*(reinterpret_cast<TInt32*>(&pwdBuffer[12]));
|
|
300 |
pwdBuffer.Close();
|
|
301 |
|
|
302 |
// Create a five-digit security code from this number
|
|
303 |
TInt isaCode = ( hashSum % 90000 ) + 10000;
|
|
304 |
|
|
305 |
// save encoded security code to TDes
|
|
306 |
aHashCode.Zero();
|
|
307 |
aHashCode.AppendNum(isaCode);
|
|
308 |
|
|
309 |
TRACE_FUNC_EXIT
|
|
310 |
return KErrNone;
|
|
311 |
}
|
|
312 |
|