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 |
* Initial Contributors:
|
|
8 |
* Nokia Corporation - initial contribution.
|
|
9 |
*
|
|
10 |
* Contributors:
|
|
11 |
* Description :
|
|
12 |
*
|
|
13 |
*/
|
|
14 |
#include "cusdcommandhandler.h"
|
|
15 |
|
|
16 |
#include <exterror.h>
|
|
17 |
|
32
|
18 |
#include "ussdreadmessageimpl.h"
|
|
19 |
#include "ussdsendmessageimpl.h"
|
|
20 |
|
26
|
21 |
#include "atmisccmdpluginconsts.h"
|
|
22 |
#include "debug.h"
|
|
23 |
|
|
24 |
const TUint8 KPhCltUssdCarriageReturn = 0x0d;
|
|
25 |
const TUint8 KPhCltUssdDcsNotSet = 0x00; // not set
|
|
26 |
|
|
27 |
const TUint8 KQuarterShift( 2 );
|
|
28 |
const TUint8 KHighNibbleShift( 4 );
|
|
29 |
const TUint8 KDcsCharacterSet7Bit( 0x00 );
|
|
30 |
const TUint8 KDcsCharacterSet8Bit( 0x01 );
|
|
31 |
const TUint8 KDcsCharacterSet16Bit( 0x02 );
|
|
32 |
const TUint8 KDcsCharacterSet7Bit2( 0x00 );
|
|
33 |
const TUint8 KDcsCharacterSet16Bit2( 0x01 );
|
|
34 |
|
|
35 |
CCUSDCommandHandler* CCUSDCommandHandler::NewL(MATMiscCmdPlugin* aCallback, TAtCommandParser& aATCmdParser, RMobilePhone& aPhone)
|
|
36 |
{
|
|
37 |
TRACE_FUNC_ENTRY
|
|
38 |
CCUSDCommandHandler* self = new (ELeave) CCUSDCommandHandler(aCallback, aATCmdParser, aPhone);
|
|
39 |
CleanupStack::PushL(self);
|
|
40 |
self->ConstructL();
|
|
41 |
CleanupStack::Pop(self);
|
|
42 |
TRACE_FUNC_EXIT
|
|
43 |
return self;
|
|
44 |
}
|
|
45 |
|
|
46 |
CCUSDCommandHandler::CCUSDCommandHandler(MATMiscCmdPlugin* aCallback, TAtCommandParser& aATCmdParser, RMobilePhone& aPhone) :
|
32
|
47 |
CATCmdSyncBase(aCallback, aATCmdParser, aPhone)
|
26
|
48 |
{
|
|
49 |
TRACE_FUNC_ENTRY
|
|
50 |
iSendAttributes.iDcs = KPhCltUssdDcsNotSet;
|
|
51 |
iSendAttributes.iType = RMobileUssdMessaging::EUssdMORequest;
|
|
52 |
iSendAttributes.iFlags = RMobileUssdMessaging::KUssdMessageType | RMobileUssdMessaging::KUssdMessageDcs | RMobileUssdMessaging::KUssdDataFormat;
|
|
53 |
TRACE_FUNC_EXIT
|
|
54 |
}
|
|
55 |
|
|
56 |
void CCUSDCommandHandler::ConstructL()
|
|
57 |
{
|
|
58 |
TRACE_FUNC_ENTRY
|
32
|
59 |
iReply.CreateL(KDefaultCmdBufLength);
|
|
60 |
|
26
|
61 |
User::LeaveIfError(iUssdMessaging.Open(iPhone));
|
|
62 |
|
32
|
63 |
iUSSDSender = CUSSDSendMessageImpl::NewL(this, iUssdMessaging);
|
|
64 |
iUSSDReader = CUSSDReadMessageImpl::NewL(this, iUssdMessaging);
|
26
|
65 |
|
|
66 |
//start a read session for network based ussd requests
|
|
67 |
User::LeaveIfError(iUSSDReader->ReadUSSDMessage(iReceivedData, iReceiveAttributes));
|
|
68 |
TRACE_FUNC_EXIT
|
|
69 |
}
|
|
70 |
|
|
71 |
CCUSDCommandHandler::~CCUSDCommandHandler()
|
|
72 |
{
|
|
73 |
TRACE_FUNC_ENTRY
|
|
74 |
delete iUSSDSender;
|
|
75 |
delete iUSSDReader;
|
|
76 |
|
|
77 |
iReply.Close();
|
|
78 |
iUssdMessaging.Close();
|
|
79 |
TRACE_FUNC_EXIT
|
|
80 |
}
|
|
81 |
|
32
|
82 |
void CCUSDCommandHandler::HandleCommand( const TDesC8& /*aCmd*/,
|
26
|
83 |
RBuf8& /*aReply*/,
|
|
84 |
TBool /*aReplyNeeded*/ )
|
|
85 |
{
|
|
86 |
TRACE_FUNC_ENTRY
|
|
87 |
TAtCommandParser::TCommandHandlerType cmdHandlerType = iATCmdParser.CommandHandlerType();
|
32
|
88 |
iReply.Zero();
|
|
89 |
|
26
|
90 |
switch (cmdHandlerType)
|
|
91 |
{
|
|
92 |
case (TAtCommandParser::ECmdHandlerTypeTest):
|
|
93 |
{
|
|
94 |
iReply.Append(KCRLF);
|
|
95 |
iReply.Append(KCUSDSupportedCmdsList);
|
|
96 |
iCallback->CreateReplyAndComplete( EReplyTypeOther, iReply );
|
|
97 |
break;
|
|
98 |
}
|
|
99 |
case (TAtCommandParser::ECmdHandlerTypeRead):
|
|
100 |
{
|
|
101 |
iReply.Append(KCRLF);
|
|
102 |
iReply.Append(KAtCUSD);
|
|
103 |
iReply.AppendNum(iDisplayRetMessage);
|
|
104 |
iReply.Append(KCRLF);
|
|
105 |
|
|
106 |
iCallback->CreateReplyAndComplete( EReplyTypeOther, iReply );
|
|
107 |
break;
|
|
108 |
}
|
|
109 |
case (TAtCommandParser::ECmdHandlerTypeSet):
|
|
110 |
{
|
32
|
111 |
if(ParseCUSDCmd() == KErrNone)
|
26
|
112 |
{
|
|
113 |
if(iUSSDCommand.Length() == 0)
|
|
114 |
{
|
|
115 |
Trace(KDebugPrintD, "No USSD command: ", iUSSDCommand.Length());
|
32
|
116 |
|
26
|
117 |
// the USSD command is not set then this is a set
|
32
|
118 |
iReply.Append( KOKCRLF );
|
|
119 |
iCallback->CreateReplyAndComplete( EReplyTypeOther, iReply);
|
26
|
120 |
}
|
|
121 |
else
|
|
122 |
{
|
32
|
123 |
if(iUSSDSender->SendUSSDCmd(iUSSDCommand, iSendAttributes) == KErrNone)
|
26
|
124 |
{
|
|
125 |
iSendCommandSent = ETrue;
|
|
126 |
}
|
32
|
127 |
else
|
26
|
128 |
{
|
|
129 |
iCallback->CreateReplyAndComplete(EReplyTypeError);
|
|
130 |
}
|
|
131 |
}
|
|
132 |
}
|
|
133 |
else
|
|
134 |
{
|
|
135 |
iCallback->CreateReplyAndComplete(EReplyTypeError);
|
|
136 |
}
|
|
137 |
|
|
138 |
break;
|
|
139 |
}
|
|
140 |
default:
|
|
141 |
{
|
|
142 |
iCallback->CreateReplyAndComplete(EReplyTypeError);
|
|
143 |
break;
|
|
144 |
}
|
|
145 |
}
|
|
146 |
TRACE_FUNC_EXIT
|
|
147 |
}
|
|
148 |
|
|
149 |
void CCUSDCommandHandler::HandleCommandCancel()
|
|
150 |
{
|
|
151 |
TRACE_FUNC_ENTRY
|
|
152 |
iUSSDSender->Cancel();
|
32
|
153 |
iSendCommandSent = EFalse;
|
26
|
154 |
TRACE_FUNC_EXIT
|
|
155 |
}
|
|
156 |
|
32
|
157 |
TInt CCUSDCommandHandler::ParseCUSDCmd()
|
26
|
158 |
{
|
|
159 |
TRACE_FUNC_ENTRY
|
|
160 |
|
32
|
161 |
TInt tempDispValue;
|
|
162 |
TInt ret = iATCmdParser.NextIntParam(tempDispValue);
|
|
163 |
|
|
164 |
|
|
165 |
if (ret == KErrNone && tempDispValue >= 0 && tempDispValue < 3)
|
26
|
166 |
{
|
32
|
167 |
Trace(KDebugPrintD, "tempDispValue: ", tempDispValue);
|
|
168 |
iDisplayRetMessage = tempDispValue;
|
|
169 |
}
|
|
170 |
else if (ret != KErrNotFound)
|
|
171 |
{
|
|
172 |
Trace(KDebugPrintD, "First NextIntParam failed: ", ret);
|
|
173 |
TRACE_FUNC_EXIT
|
|
174 |
return KErrGeneral;
|
26
|
175 |
}
|
|
176 |
|
|
177 |
// now parse the USSD command
|
32
|
178 |
TPtrC8 command;
|
|
179 |
ret = iATCmdParser.NextTextParam(command);
|
|
180 |
if (ret == KErrNone )
|
26
|
181 |
{
|
|
182 |
if(iUSSDCommand.Create(command.Length()) != KErrNone)
|
|
183 |
{
|
32
|
184 |
Trace(KDebugPrintD, "iUSSDCommand.Create() failed ", ret);
|
26
|
185 |
TRACE_FUNC_EXIT
|
|
186 |
return KErrGeneral;
|
|
187 |
}
|
32
|
188 |
|
|
189 |
Trace(KDebugPrintS, "command: ", &command);
|
|
190 |
if(Pack(command, iUSSDCommand) < 0)
|
26
|
191 |
{
|
32
|
192 |
Trace(KDebugPrintS, "Pack() returned error, command: ", &command);
|
|
193 |
TRACE_FUNC_EXIT
|
|
194 |
return KErrGeneral;
|
|
195 |
}
|
|
196 |
}
|
|
197 |
else if (ret != KErrNotFound)
|
|
198 |
{
|
|
199 |
TRACE_FUNC_EXIT
|
|
200 |
return KErrGeneral;
|
|
201 |
}
|
|
202 |
TInt dcs = 0; // 3GPP TS 23.038 [25] Cell Broadcast Data Coding Scheme in integer format (default 0)
|
|
203 |
ret = iATCmdParser.NextIntParam(dcs);
|
|
204 |
|
|
205 |
Trace(KDebugPrintD, "ret: ", ret);
|
|
206 |
Trace(KDebugPrintD, "IsDcsValid(dcs): ", IsDcsValid(dcs));
|
|
207 |
Trace(KDebugPrintD, "iATCmdParser.NextParam().Length(): ", iATCmdParser.NextParam().Length());
|
|
208 |
if(ret != KErrNone && ret != KErrNotFound || !IsDcsValid(dcs) ||
|
|
209 |
iATCmdParser.NextParam().Length() != 0)
|
|
210 |
{
|
|
211 |
Trace(KDebugPrintD, "Second NextIntParam failed: ", ret);
|
|
212 |
TRACE_FUNC_EXIT
|
|
213 |
return KErrGeneral;
|
|
214 |
}
|
|
215 |
else
|
|
216 |
{
|
|
217 |
// dcs will have the default value if no parameter is given in the command line
|
|
218 |
Trace(KDebugPrintD, "dcs ", dcs);
|
|
219 |
iSendAttributes.iDcs = dcs;
|
|
220 |
}
|
|
221 |
|
26
|
222 |
TRACE_FUNC_EXIT
|
|
223 |
return KErrNone;
|
|
224 |
}
|
|
225 |
|
32
|
226 |
void CCUSDCommandHandler::HandleSendMessageComplete(TInt aError)
|
|
227 |
{
|
|
228 |
TRACE_FUNC_ENTRY
|
|
229 |
if (aError != KErrNone)
|
|
230 |
{
|
|
231 |
// Only respond from a Send USSD request if there has been an error
|
|
232 |
iCallback->CreateReplyAndComplete(EReplyTypeError);
|
|
233 |
}
|
|
234 |
else if (iDisplayRetMessage == 0)
|
|
235 |
{
|
|
236 |
// if display mode is 0 then no response apart from OK should be displayed
|
|
237 |
iCallback->CreateReplyAndComplete( EReplyTypeOther, KOKCRLF );
|
|
238 |
}
|
|
239 |
|
|
240 |
iUSSDCommand.Close();
|
|
241 |
TRACE_FUNC_EXIT
|
|
242 |
}
|
|
243 |
|
|
244 |
void CCUSDCommandHandler::HandleReadMessageComplete(TInt aError)
|
26
|
245 |
{
|
|
246 |
TRACE_FUNC_ENTRY
|
32
|
247 |
if(aError == KErrNone)
|
26
|
248 |
{
|
32
|
249 |
if(iDisplayRetMessage)
|
|
250 |
{
|
|
251 |
FormatUSSDReceivedData();
|
|
252 |
|
|
253 |
if(iSendCommandSent)
|
|
254 |
{
|
|
255 |
iCallback->CreateReplyAndComplete( EReplyTypeOther, iReply );
|
|
256 |
}
|
|
257 |
else
|
|
258 |
{
|
|
259 |
iCallback->HandleUnsolicitedRequest(iReply);
|
|
260 |
}
|
|
261 |
}
|
|
262 |
|
|
263 |
// as we have received the data initiated by the send USSD request we set
|
|
264 |
// this parameter to EFalse
|
|
265 |
iSendCommandSent = EFalse;
|
|
266 |
|
|
267 |
// re-issue the read request
|
|
268 |
iUSSDReader->ReadUSSDMessage(iReceivedData, iReceiveAttributes);
|
26
|
269 |
}
|
|
270 |
else
|
|
271 |
{
|
|
272 |
iCallback->CreateReplyAndComplete(EReplyTypeError);
|
|
273 |
}
|
|
274 |
TRACE_FUNC_EXIT
|
|
275 |
}
|
|
276 |
|
|
277 |
TInt CCUSDCommandHandler::Pack(const TDesC8& aIn, TDes8& aOut)
|
|
278 |
{
|
|
279 |
TRACE_FUNC_ENTRY
|
|
280 |
TInt startBit = 0;
|
|
281 |
|
|
282 |
// Ensure we've got the right length
|
|
283 |
TInt packedOctetsRequired = PackedOctetsRequired( aIn.Length() );
|
32
|
284 |
if (packedOctetsRequired > ( aOut.MaxLength() - aOut.Length() ) )
|
26
|
285 |
{
|
|
286 |
Trace(KDebugPrintD, "packedOctetsRequired: ", packedOctetsRequired);
|
|
287 |
TRACE_FUNC_EXIT
|
|
288 |
return KErrOverflow;
|
|
289 |
}
|
|
290 |
|
|
291 |
// Do the conversion
|
|
292 |
TInt elementSizeInBits = ElementSizeInBits(TSmsDataCodingScheme::ESmsAlphabet7Bit);
|
32
|
293 |
|
|
294 |
Trace(KDebugPrintD, "elementSizeInBits = : ", elementSizeInBits);
|
|
295 |
if (elementSizeInBits == 8) // 8 bit data
|
26
|
296 |
{
|
|
297 |
// Straight copy here
|
|
298 |
aOut.Append( aIn );
|
|
299 |
}
|
32
|
300 |
else if (elementSizeInBits == 7) // Need packing to 7-bit
|
|
301 |
{
|
|
302 |
Trace(KDebugPrintS, "aOut: ", &aOut);
|
|
303 |
Trace(KDebugPrintS, "aIn: ", &aIn);
|
|
304 |
|
|
305 |
// Get raw pointers and do packing
|
|
306 |
TUint8* outPtr = ( TUint8* )aOut.Ptr() + aOut.Length();
|
|
307 |
const TUint8* inPtr = aIn.Ptr();
|
26
|
308 |
|
32
|
309 |
TInt inLength = aIn.Length();
|
|
310 |
outPtr[0] = 0;
|
|
311 |
|
|
312 |
Trace(KDebugPrintD, "inLength = : ", inLength);
|
|
313 |
for (TInt i = 0; i < inLength; ++i)
|
|
314 |
{
|
|
315 |
TUint8 to = inPtr[i];
|
|
316 |
*outPtr |= ( to << startBit );
|
|
317 |
|
|
318 |
if (startBit)
|
|
319 |
{
|
|
320 |
++outPtr;
|
|
321 |
*outPtr = ( TUint8 ) ( to >> ( 8 - startBit ) );
|
|
322 |
}
|
|
323 |
|
|
324 |
startBit = (startBit + 7) % 8; // roll 0,1,2,3,4,5,6,7,0,1,2,...
|
|
325 |
}
|
|
326 |
|
|
327 |
Trace(KDebugPrintD, "startBit = : ", startBit);
|
|
328 |
|
|
329 |
if ((inPtr[inLength - 1] == KPhCltUssdCarriageReturn && startBit == 0) || startBit == 1 )
|
|
330 |
{
|
|
331 |
//We have to add one CR more
|
|
332 |
*outPtr |= ( KPhCltUssdCarriageReturn << startBit );
|
|
333 |
|
|
334 |
if (startBit)
|
26
|
335 |
{
|
|
336 |
++outPtr;
|
32
|
337 |
*outPtr = (TUint8) (KPhCltUssdCarriageReturn >> (8 - startBit));
|
|
338 |
}
|
|
339 |
else
|
|
340 |
{
|
|
341 |
++packedOctetsRequired;
|
26
|
342 |
}
|
32
|
343 |
startBit = (startBit + 7)%8;
|
26
|
344 |
}
|
32
|
345 |
|
|
346 |
// Increment the length for the packed data
|
|
347 |
aOut.SetLength(aOut.Length() + packedOctetsRequired);
|
|
348 |
|
|
349 |
Trace(KDebugPrintD, "aOut.Length() = : ", aOut.Length());
|
|
350 |
}
|
26
|
351 |
|
|
352 |
// Return number of bytes used
|
|
353 |
TRACE_FUNC_EXIT
|
|
354 |
return packedOctetsRequired;
|
|
355 |
}
|
|
356 |
|
32
|
357 |
TInt CCUSDCommandHandler::PackedOctetsRequired(TInt aNumUDUnits) const
|
26
|
358 |
{
|
|
359 |
TRACE_FUNC_ENTRY
|
|
360 |
TInt startBit = 0;
|
|
361 |
|
|
362 |
TInt octetsRequired = 0;
|
|
363 |
TInt elementSizeInBits = ElementSizeInBits(TSmsDataCodingScheme::ESmsAlphabet7Bit);
|
32
|
364 |
if (elementSizeInBits == 8)
|
26
|
365 |
{
|
|
366 |
octetsRequired=aNumUDUnits;
|
|
367 |
}
|
|
368 |
else
|
|
369 |
{
|
32
|
370 |
octetsRequired = (startBit + aNumUDUnits*elementSizeInBits + 7 )/8; // Rounds up
|
26
|
371 |
}
|
|
372 |
TRACE_FUNC_EXIT
|
|
373 |
return octetsRequired;
|
|
374 |
}
|
|
375 |
|
|
376 |
TBool CCUSDCommandHandler::IsDcsValid( const TUint8 aDcs ) const
|
|
377 |
{
|
|
378 |
TRACE_FUNC_ENTRY
|
32
|
379 |
TBool isDcsValid(EFalse);
|
26
|
380 |
// 76543210
|
|
381 |
TUint8 codingGroup = ( aDcs & 0xF0 ) >> KHighNibbleShift; // bits XXXX____
|
|
382 |
TUint8 characterSet = ( aDcs & 0x0C ) >> KQuarterShift; // bits ____XX__
|
|
383 |
TUint8 lowQuartet = ( aDcs & 0x0F ); // bits ____XXXX
|
|
384 |
|
32
|
385 |
switch (codingGroup)
|
26
|
386 |
{
|
|
387 |
case 0x00:
|
|
388 |
case 0x02:
|
|
389 |
case 0x03:
|
|
390 |
case 0x0F:
|
|
391 |
{
|
|
392 |
isDcsValid = ETrue;
|
|
393 |
break;
|
|
394 |
}
|
|
395 |
|
|
396 |
case 0x01:
|
|
397 |
{
|
|
398 |
if ( ( KDcsCharacterSet7Bit2 == lowQuartet ) ||
|
|
399 |
( KDcsCharacterSet16Bit2 == lowQuartet ) )
|
|
400 |
{
|
|
401 |
isDcsValid = ETrue;
|
|
402 |
}
|
|
403 |
break;
|
|
404 |
}
|
|
405 |
|
|
406 |
case 0x04:
|
|
407 |
case 0x05:
|
|
408 |
case 0x06:
|
|
409 |
case 0x07:
|
|
410 |
case 0x09:
|
|
411 |
{
|
|
412 |
if ( ( KDcsCharacterSet7Bit == characterSet ) ||
|
|
413 |
( KDcsCharacterSet8Bit == characterSet ) ||
|
|
414 |
( KDcsCharacterSet16Bit == characterSet ) )
|
|
415 |
{
|
|
416 |
isDcsValid = ETrue;
|
|
417 |
}
|
|
418 |
break;
|
|
419 |
}
|
32
|
420 |
default:
|
|
421 |
{
|
|
422 |
break;
|
|
423 |
}
|
26
|
424 |
}
|
|
425 |
TRACE_FUNC_EXIT
|
|
426 |
return isDcsValid;
|
|
427 |
}
|
|
428 |
|
|
429 |
TInt CCUSDCommandHandler::ElementSizeInBits(TSmsDataCodingScheme::TSmsAlphabet aAlphabet) const
|
|
430 |
{
|
|
431 |
TRACE_FUNC_ENTRY
|
|
432 |
TInt ret;
|
32
|
433 |
switch (aAlphabet)
|
26
|
434 |
{
|
|
435 |
case TSmsDataCodingScheme::ESmsAlphabet7Bit:
|
|
436 |
{
|
|
437 |
ret = 7;
|
|
438 |
break;
|
|
439 |
}
|
|
440 |
case TSmsDataCodingScheme::ESmsAlphabet8Bit:
|
|
441 |
case TSmsDataCodingScheme::ESmsAlphabetUCS2:
|
|
442 |
{
|
|
443 |
ret = 8;
|
|
444 |
break;
|
|
445 |
}
|
|
446 |
default:
|
|
447 |
{
|
|
448 |
ret = KErrGsmSMSDataCodingSchemeNotSupported;
|
|
449 |
}
|
|
450 |
}
|
|
451 |
TRACE_FUNC_EXIT
|
|
452 |
return ret;
|
|
453 |
}
|
|
454 |
|
|
455 |
void CCUSDCommandHandler::FormatUSSDReceivedData()
|
|
456 |
{
|
|
457 |
TRACE_FUNC_ENTRY
|
32
|
458 |
iReply.Zero();
|
|
459 |
|
26
|
460 |
iReply.Append(KCRLF);
|
|
461 |
iReply.Append(KAtCUSD);
|
|
462 |
iReply.AppendNum(iDisplayRetMessage);
|
|
463 |
iReply.Append(',');
|
|
464 |
iReply.Append('"');
|
|
465 |
|
|
466 |
TInt length = iReceivedData.Length();
|
|
467 |
for(TInt i = 0; i < length; ++i)
|
|
468 |
{
|
|
469 |
iReply.AppendNum(iReceivedData[i], EHex);
|
|
470 |
}
|
|
471 |
iReply.Append('"');
|
|
472 |
iReply.Append(',');
|
|
473 |
|
|
474 |
iReply.AppendNum(iReceiveAttributes.iDcs);
|
|
475 |
iReply.Append(KCRLF);
|
|
476 |
TRACE_FUNC_EXIT
|
|
477 |
}
|