bluetoothengine/headsetsimulator/profiles/hspprofile/src/dataprocessing/hspcommandparser.cpp
branchheadsetsimulator
changeset 60 90dbfc0435e3
equal deleted inserted replaced
59:02103bf20ee5 60:90dbfc0435e3
       
     1 /* 
       
     2  *
       
     3  * Copyright (c) <2010> Comarch S.A. and/or its subsidiary(-ies).
       
     4  * All rights reserved.
       
     5  * This component and the accompanying materials are made available
       
     6  * under the terms of the License "Eclipse Public License v1.0"
       
     7  * which accompanies this distribution, and is available
       
     8  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     9  *
       
    10  * Original Contributors:
       
    11  * Comarch S.A. - original contribution.
       
    12  *
       
    13  * Contributors:
       
    14  *
       
    15  * Description:
       
    16  *
       
    17  */
       
    18 
       
    19 #include <badesca.h>
       
    20 
       
    21 #include "hspcommandparser.h"
       
    22 #include "debug.h"
       
    23 
       
    24 CHsHSPParser* CHsHSPParser::NewL()
       
    25     {
       
    26     CHsHSPParser *self = CHsHSPParser::NewLC();
       
    27     CleanupStack::Pop( self );
       
    28 
       
    29     return self;
       
    30     }
       
    31 
       
    32 CHsHSPParser* CHsHSPParser::NewLC()
       
    33     {
       
    34 
       
    35     CHsHSPParser *self = new ( ELeave ) CHsHSPParser();
       
    36     CleanupStack::PushL( self );
       
    37     self->ConstructL();
       
    38     return self;
       
    39     }
       
    40 
       
    41 CHsHSPParser::~CHsHSPParser()
       
    42     {
       
    43 
       
    44     }
       
    45 
       
    46 void CHsHSPParser::ParseL( const TDesC8& aBufCommand, const TBool aFromAG,
       
    47         CHsHSPCommand& aCommand )
       
    48     {
       
    49     TRACE_FUNC_ENTRY
       
    50 
       
    51     aCommand.SetSource( aFromAG );
       
    52 
       
    53     THsHSPCommandType type = EHSPCmdUnknown;
       
    54 
       
    55     type = RetrieveTypeL( aBufCommand );
       
    56 
       
    57     User::LeaveIfError( aCommand.SetType( type ) );
       
    58 
       
    59     if ( type == EHSPCmdOK || type == EHSPCmdERROR || type == EHSPCmdRING )
       
    60         {
       
    61         User::LeaveIfError( aCommand.SetMode( ECmdModeNormal ) );
       
    62         RATParamArray params( 1 );
       
    63         User::LeaveIfError( aCommand.SetParams( params ) );
       
    64 
       
    65         }
       
    66     else
       
    67         {
       
    68         THsHSPCommandMode mode = ECmdUnknownMode;
       
    69         mode = RetrieveModeL( aBufCommand, aFromAG );
       
    70         User::LeaveIfError( aCommand.SetMode( mode ) );
       
    71 
       
    72         RATParamArray params( 4 );
       
    73 
       
    74         RetrieveParamsL( aBufCommand, mode, params );
       
    75         User::LeaveIfError( aCommand.SetParams( params ) );
       
    76 
       
    77         }
       
    78 
       
    79     TRACE_FUNC_EXIT
       
    80 
       
    81     }
       
    82 
       
    83 CHsHSPParser::CHsHSPParser()
       
    84     {
       
    85 
       
    86     }
       
    87 
       
    88 void CHsHSPParser::ConstructL()
       
    89     {
       
    90 
       
    91     }
       
    92 
       
    93 THsHSPCommandMode CHsHSPParser::RetrieveModeL( const TDesC8 &aCommand,
       
    94         const TBool aFromAG )
       
    95     {
       
    96     TRACE_FUNC_ENTRY
       
    97 
       
    98     THsHSPCommandMode res = ECmdUnknownMode;
       
    99 
       
   100     if ( aFromAG )
       
   101         {
       
   102         if ( aCommand.Find( KHsHSPATTestModeDesAG ) != KErrNotFound )
       
   103             {
       
   104             res = ECmdModeTest;
       
   105             }
       
   106         else if ( aCommand.Find( KHsHSPATReadModeDesAG ) != KErrNotFound )
       
   107             {
       
   108             res = ECmdModeRead;
       
   109             }
       
   110         else if ( aCommand.Find( KHsHSPATWriteModeDesAG ) != KErrNotFound )
       
   111             {
       
   112             res = ECmdModeWrite;
       
   113             }
       
   114 
       
   115         }
       
   116     else
       
   117         {
       
   118         if ( aCommand.Find( KHsHSPATTestModeDes ) != KErrNotFound )
       
   119             {
       
   120             res = ECmdModeTest;
       
   121             }
       
   122         else if ( aCommand.Find( KHsHSPATReadModeDes ) != KErrNotFound )
       
   123             {
       
   124             res = ECmdModeRead;
       
   125             }
       
   126         else if ( aCommand.Find( KHsHSPATWriteModeDes ) != KErrNotFound )
       
   127             {
       
   128             res = ECmdModeWrite;
       
   129             }
       
   130 
       
   131         }
       
   132 
       
   133     if ( res == ECmdUnknownMode )
       
   134         {
       
   135 
       
   136         User::Leave( KErrArgument );
       
   137         }
       
   138     TRACE_FUNC_EXIT
       
   139     return res;
       
   140 
       
   141     }
       
   142 
       
   143 THsHSPCommandType CHsHSPParser::RetrieveTypeL( const TDesC8 &aCommand )
       
   144     {
       
   145     TRACE_FUNC_ENTRY
       
   146     THsHSPCommandType res = EHSPCmdUnknown;
       
   147     if ( aCommand.Find( KHsHSPOKCommand ) != KErrNotFound )
       
   148         {
       
   149         res = EHSPCmdOK;
       
   150         }
       
   151     else if ( aCommand.Find( KHsHSPERRORCommand ) != KErrNotFound )
       
   152         {
       
   153         res = EHSPCmdERROR;
       
   154         }
       
   155     else if ( aCommand.Find( KHsHSPRINGCommand ) != KErrNotFound )
       
   156         {
       
   157         res = EHSPCmdRING;
       
   158         }
       
   159     else if ( aCommand.Find( KHsHSPVGSCommand ) != KErrNotFound )
       
   160         {
       
   161         res = EHSPCmdVGS;
       
   162         }
       
   163     else if ( aCommand.Find( KHsHSPVGMCommand ) != KErrNotFound )
       
   164         {
       
   165         res = EHSPCmdVGM;
       
   166         }
       
   167     else if ( aCommand.Find( KHsHSPCKPDCommand ) != KErrNotFound )
       
   168         {
       
   169         res = EHSPCmdCKPD;
       
   170         }
       
   171 
       
   172     if ( res == EHSPCmdUnknown )
       
   173         User::Leave( KErrArgument );
       
   174 
       
   175     TRACE_FUNC_EXIT
       
   176     return res;
       
   177     }
       
   178 
       
   179 void CHsHSPParser::RetrieveParamsL( const TDesC8 &aCommand,
       
   180         const THsHSPCommandMode aCmdMode, RATParamArray& aParams )
       
   181     {
       
   182     TRACE_FUNC_ENTRY
       
   183 
       
   184     RBuf8 buf;
       
   185     buf.CreateL( aCommand );
       
   186     buf.CleanupClosePushL();
       
   187 
       
   188     TInt prefixLength = 0;
       
   189     TInt suffixLength = 0;
       
   190 
       
   191     TInt modeLength = 0;
       
   192 
       
   193     TInt leftOffset = 0;
       
   194     TInt rightOffset = 0;
       
   195 
       
   196     TInt startPos = 0;
       
   197     TBool indicatorsFound = EFalse;
       
   198 
       
   199     if ( buf.Find( KHsHSPIndicatorParamsSeparator ) != KErrNotFound )
       
   200         {
       
   201         indicatorsFound = ETrue;
       
   202         }
       
   203 
       
   204     if ( indicatorsFound )
       
   205         {
       
   206         prefixLength = 9;
       
   207         suffixLength = 2;
       
   208         rightOffset = 2;
       
   209         leftOffset = 1;
       
   210         }
       
   211     else
       
   212         {
       
   213 
       
   214         rightOffset = 1;
       
   215         suffixLength = 2;
       
   216 
       
   217         switch ( aCmdMode )
       
   218 
       
   219             {
       
   220             case ECmdModeTest:
       
   221                 {
       
   222                 startPos = buf.Find( KHsHSPATTestModeDes );
       
   223                 modeLength = 2;
       
   224                 }
       
   225                 break;
       
   226             case ECmdModeRead:
       
   227                 {
       
   228                 startPos = buf.Find( KHsHSPATReadModeDes );
       
   229                 modeLength = 1;
       
   230                 }
       
   231                 break;
       
   232             case ECmdModeWrite:
       
   233                 {
       
   234                 startPos = buf.Find( KHsHSPATWriteModeDes );
       
   235                 modeLength = 1;
       
   236 
       
   237                 if ( startPos == KErrNotFound )
       
   238                     {
       
   239                     startPos = buf.Find( KHsHSPATWriteModeDesAG );
       
   240                     modeLength = 2;
       
   241                     }
       
   242 
       
   243                 }
       
   244                 break;
       
   245             default:
       
   246 
       
   247                 User::Leave( KErrArgument );
       
   248                 break;
       
   249             }
       
   250         }
       
   251 
       
   252     if ( startPos != KErrNotFound )
       
   253         {
       
   254         //strip command's prefix
       
   255         buf.Delete( 0, prefixLength + startPos + modeLength );
       
   256 
       
   257         //strip command's suffix
       
   258         buf.Delete( buf.Length() - suffixLength, suffixLength );
       
   259         }
       
   260     else
       
   261         User::Leave( KErrArgument );
       
   262 
       
   263     while ( buf.Size() > 0 )
       
   264         {
       
   265         TInt pos = 0;
       
   266 
       
   267         if ( indicatorsFound )
       
   268             {
       
   269             pos = buf.Find( KHsHSPIndicatorParamsSeparator );
       
   270             }
       
   271         else
       
   272             {
       
   273             pos = buf.Find( KHsHSPParamsSeparator );
       
   274             }
       
   275 
       
   276         if ( pos == KErrNotFound )
       
   277             {
       
   278             aParams.AppendL( buf );
       
   279             buf.Zero();
       
   280 
       
   281             }
       
   282         else
       
   283             {
       
   284             TPtrC8 ptr = ( buf.Left( pos + leftOffset ) );
       
   285             aParams.AppendL( ptr );
       
   286             buf.Delete( 0, pos + rightOffset );
       
   287             }
       
   288         }
       
   289 
       
   290     CleanupStack::PopAndDestroy( &buf );
       
   291     TRACE_FUNC_EXIT
       
   292     }
       
   293