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