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