cbsatplugin/atmisccmdplugin/src/atcommandparser.cpp
branchRCL_3
changeset 12 b23265fb36da
child 16 95674584745d
equal deleted inserted replaced
11:f7fbeaeb166a 12: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 //
       
     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"
       
    22 
       
    23 
       
    24 // Max buffer length for an MD5 digest
       
    25 const TInt KSCPMaxHashLength( 32 );
       
    26 
       
    27 
       
    28 TAtCommandParser::TAtCommandParser()
       
    29     : iCmdType(EUnknown), iCmdHandlerType(ECmdHandlerTypeUndefined)  
       
    30     {
       
    31     }
       
    32 
       
    33 TAtCommandParser::TAtCommandParser(const TDesC8& aCmd)
       
    34     : iCmdType(EUnknown), iCmdHandlerType(ECmdHandlerTypeUndefined)  
       
    35     {
       
    36     ParseAtCommand(aCmd);
       
    37     }
       
    38 
       
    39 
       
    40 void TAtCommandParser::ParseAtCommand(const TDesC8& aCmd)
       
    41     {
       
    42     TRACE_FUNC_ENTRY
       
    43     iCmd.Assign(aCmd);
       
    44     iCmd.Mark();
       
    45     TChar c = 0;
       
    46     // First extract the AT command "AT+COMMAND"
       
    47     while(!iCmd.Eos() && !(c=='=' || c=='?'))
       
    48         {
       
    49         iCmd.Inc();
       
    50         c = iCmd.Peek();
       
    51         }
       
    52           
       
    53     TPtrC8 token = iCmd.MarkedToken();   
       
    54     
       
    55     _LIT8(KAtCfun, "AT+CFUN");
       
    56     _LIT8(KAtCbc, "AT+CBC");
       
    57     _LIT8(KAtClck, "AT+CLCK");
       
    58     _LIT8(KAtCpwd, "AT+CPWD");
       
    59     _LIT8(KAtCpin, "AT+CPIN");
       
    60     _LIT8(KAtCusd, "AT+CUSD");
       
    61     _LIT8(KAtCnum, "AT+CNUM");
       
    62     
       
    63     Trace(KDebugPrintS, "token: ", &token);
       
    64     // Determine the AT command type
       
    65     if(!token.Compare(KAtCfun))
       
    66         {
       
    67         iCmdType = ECmdAtCfun;
       
    68         }
       
    69     else if(!token.Compare(KAtCbc))
       
    70         {
       
    71         iCmdType = ECmdAtCbc;
       
    72         }
       
    73     else if(!token.Compare(KAtClck))
       
    74         {
       
    75         iCmdType = ECmdAtClck;
       
    76         }
       
    77     else if(!token.Compare(KAtCpwd))
       
    78         {
       
    79         iCmdType = ECmdAtCpwd;
       
    80         }
       
    81     else if(!token.Compare(KAtCpin))
       
    82         {
       
    83         iCmdType = ECmdAtCpin;
       
    84         }
       
    85     else if(!token.Compare(KAtCusd))
       
    86         {
       
    87         iCmdType = ECmdAtCusd;
       
    88         }
       
    89     else if(!token.Compare(KAtCnum))
       
    90         {
       
    91         iCmdType = ECmdAtCnum;
       
    92         }
       
    93     else
       
    94         {
       
    95         iCmdType = EUnknown;
       
    96         TRACE_FUNC_EXIT
       
    97         return;
       
    98         }
       
    99         
       
   100     // Now find out the AT command handler type
       
   101     if(iCmd.Eos())
       
   102         {
       
   103         iCmdHandlerType = ECmdHandlerTypeBase;
       
   104         }
       
   105     else if(iCmd.Peek() == '?')
       
   106         {
       
   107         iCmdHandlerType = ECmdHandlerTypeRead;
       
   108         }
       
   109     else if(iCmd.Peek() == '=')
       
   110         {
       
   111         iCmd.Inc();
       
   112         if(iCmd.Peek() == '?')
       
   113             {
       
   114             iCmd.Inc();
       
   115             iCmdHandlerType = ECmdHandlerTypeTest;
       
   116             }
       
   117         else
       
   118             {
       
   119             iCmdHandlerType = ECmdHandlerTypeSet;
       
   120             }
       
   121         }
       
   122     else
       
   123         {
       
   124         iCmdHandlerType = ECmdHandlerTypeUndefined;
       
   125         }
       
   126     TRACE_FUNC_EXIT
       
   127     }
       
   128 
       
   129 TAtCommandParser::TCommandType TAtCommandParser::Command() const
       
   130     {
       
   131     return iCmdType;
       
   132     }
       
   133 
       
   134 TAtCommandParser::TCommandHandlerType TAtCommandParser::CommandHandlerType() const
       
   135     {
       
   136     return iCmdHandlerType;
       
   137     }
       
   138 
       
   139 TPtrC8 TAtCommandParser::NextParam()
       
   140     {
       
   141     TRACE_FUNC_ENTRY
       
   142     iCmd.SkipSpace(); // Skip front spaces
       
   143     iCmd.Mark();
       
   144     TChar chr = 0;
       
   145     
       
   146     if(!iCmd.Eos())
       
   147         {
       
   148         chr = iCmd.Peek();
       
   149         while(!iCmd.Eos() && chr != ',' && !chr.IsSpace() && !chr.IsControl())
       
   150             {// Stop at any of those chars: comma, space or control
       
   151             iCmd.Inc();
       
   152             chr = iCmd.Peek();
       
   153             }
       
   154         }
       
   155     
       
   156     // Extract the token at this point            
       
   157     TPtrC8 retVal = iCmd.MarkedToken();
       
   158     
       
   159     // Skip comma, space and control chars
       
   160     while(!iCmd.Eos() && (chr == ',' || chr.IsSpace() || chr.IsControl()))
       
   161         {
       
   162         iCmd.Inc();
       
   163         chr = iCmd.Peek();
       
   164         }
       
   165     TRACE_FUNC_EXIT
       
   166     return retVal;
       
   167     }
       
   168 
       
   169 TPtrC8 TAtCommandParser::NextTextParam(TInt& aError)
       
   170     {
       
   171     TPtrC8 param = NextParam();
       
   172     
       
   173     if (param.Compare(KNullDesC8) == 0)
       
   174         {
       
   175         aError = KErrNotFound;
       
   176         return param; 
       
   177         }
       
   178     
       
   179     if(param.Length()<2 
       
   180             || param[0] != '"'
       
   181             || param[param.Length()-1] != '"')
       
   182         {
       
   183         aError = KErrArgument;
       
   184         return param.Left(0);
       
   185         }
       
   186     aError = KErrNone;
       
   187     return param.Mid(1, param.Length() - 2);
       
   188     }
       
   189 
       
   190 TInt TAtCommandParser::NextIntParam(TInt& aValue)
       
   191     {
       
   192     TRACE_FUNC_ENTRY
       
   193     TInt retVal =KErrNone;
       
   194     TPtrC8 param = NextParam();
       
   195     if (param.Compare(KNullDesC8) == 0)
       
   196         {
       
   197         retVal = KErrNotFound;
       
   198         }
       
   199     else
       
   200         {
       
   201         TLex8 lex(param);
       
   202         retVal = lex.Val(aValue);
       
   203         }
       
   204     TRACE_FUNC_EXIT
       
   205     return retVal;
       
   206     }
       
   207 
       
   208 TInt TAtCommandParser::HashSecurityCode(const TDesC8& aPasscode, TDes8& aHashCode)
       
   209     {
       
   210     TRACE_FUNC_ENTRY
       
   211     TInt ret = KErrNone;
       
   212 
       
   213     // Get MD5 Hash
       
   214     // see remotemgmt component CSCPServer::HashISACode() for encoding algorithm
       
   215     CMD5* hashObject = NULL;
       
   216     TRAP( ret, hashObject = CMD5::NewL() );
       
   217 
       
   218     if(ret != KErrNone)
       
   219         {
       
   220         TRACE_FUNC_EXIT
       
   221         return ret;
       
   222         }
       
   223     
       
   224     RBuf pwdBuffer;
       
   225     ret = pwdBuffer.Create(KSCPMaxHashLength);
       
   226     if(ret != KErrNone)
       
   227         {
       
   228         delete hashObject;
       
   229         TRACE_FUNC_EXIT
       
   230         return ret;
       
   231         }
       
   232     pwdBuffer.Copy(aPasscode); // convert to TDes16
       
   233 
       
   234     // add TDes16 to a binary buffer
       
   235     TUint16* inputPtr = const_cast<TUint16*>( pwdBuffer.Ptr() ); 
       
   236     TPtrC8 inputData( reinterpret_cast<TUint8*>(inputPtr), pwdBuffer.Length()*2 );
       
   237     
       
   238     TPtrC8 hash = hashObject->Final( inputData );
       
   239     delete hashObject;
       
   240 
       
   241     pwdBuffer.Zero();
       
   242     pwdBuffer.Copy(hash);
       
   243 
       
   244     // Compute the hash sum as four 32-bit integers.
       
   245     TInt64 hashSum = *(reinterpret_cast<TInt32*>(&pwdBuffer[0])) + 
       
   246                      *(reinterpret_cast<TInt32*>(&pwdBuffer[4])) + 
       
   247                      *(reinterpret_cast<TInt32*>(&pwdBuffer[8])) +
       
   248                      *(reinterpret_cast<TInt32*>(&pwdBuffer[12]));
       
   249     pwdBuffer.Close();
       
   250      
       
   251     // Create a five-digit security code from this number
       
   252     TInt isaCode = ( hashSum % 90000 ) + 10000;
       
   253 
       
   254     // save encoded security code to TDes
       
   255     aHashCode.Zero();
       
   256     aHashCode.AppendNum(isaCode);
       
   257 
       
   258     TRACE_FUNC_EXIT
       
   259     return KErrNone;
       
   260     }
       
   261