bluetoothengine/headsetsimulator/profiles/hfpprofile/src/dataprocessing/hfpcommandparser.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 "hfpcommandparser.h"
       
    20 #include "badesca.h"
       
    21 #include "debug.h"
       
    22 
       
    23 CHsHFPParser* CHsHFPParser::NewL()
       
    24     {
       
    25     CHsHFPParser *self = CHsHFPParser::NewLC();
       
    26     CleanupStack::Pop( self );
       
    27 
       
    28     return self;
       
    29     }
       
    30 
       
    31 CHsHFPParser* CHsHFPParser::NewLC()
       
    32     {
       
    33 
       
    34     CHsHFPParser *self = new ( ELeave ) CHsHFPParser();
       
    35     CleanupStack::PushL( self );
       
    36     self->ConstructL();
       
    37     return self;
       
    38     }
       
    39 
       
    40 CHsHFPParser::~CHsHFPParser()
       
    41     {
       
    42 
       
    43     }
       
    44 
       
    45 void CHsHFPParser::ParseL( const TDesC8& aBufCommand, const TBool aFromAG,
       
    46         CHsHFPCommand& aCommand )
       
    47     {
       
    48     TRACE_FUNC_ENTRY
       
    49 
       
    50     aCommand.SetSource( aFromAG );
       
    51 
       
    52     THsHFPCommandType type = EHFPCmdUnknown;
       
    53 
       
    54     type = RetrieveTypeL( aBufCommand );
       
    55     User::LeaveIfError( aCommand.SetType( type ) );
       
    56 
       
    57     if ( type == EHFPCmdOK || type == EHFPCmdERROR || type == EHFPCmdCHUP
       
    58             || type == EHFPCmdRING || type == EHFPCmdATA || type == EHFPCmdBLDN )
       
    59         {
       
    60         User::LeaveIfError( aCommand.SetMode( ECmdModeNormal ) );
       
    61         RATParamArray params( 1 );
       
    62         User::LeaveIfError( aCommand.SetParams( params ) );
       
    63 
       
    64         }
       
    65     else if ( type == EHFPCmdATD )
       
    66         {
       
    67         aCommand.SetMode( ECmdModeOther );
       
    68 
       
    69         RBuf8 buf;
       
    70         buf.CreateL( aBufCommand );
       
    71         buf.CleanupClosePushL();
       
    72         buf.Delete( 0, KHsHFPATDCommand().Size() );
       
    73         buf.Delete( buf.Size() - KHsHFPCommandSuffix().Size(), KHsHFPCommandSuffix().Size() );
       
    74         RATParamArray params;
       
    75         params.AppendL( TATParam( buf ) );
       
    76         User::LeaveIfError( aCommand.SetParams( params ) );
       
    77         CleanupStack::PopAndDestroy( &buf );
       
    78         }
       
    79     else
       
    80         {
       
    81         THsHFPCommandMode mode = ECmdUnknownMode;
       
    82         mode = RetrieveModeL( aBufCommand, aFromAG );
       
    83         User::LeaveIfError( aCommand.SetMode( mode ) );
       
    84 
       
    85         RATParamArray params( 4 );
       
    86 
       
    87         RetrieveParamsL( aBufCommand, mode, params );
       
    88         User::LeaveIfError( aCommand.SetParams( params ) );
       
    89 
       
    90         }
       
    91 
       
    92     TRACE_FUNC_EXIT
       
    93     }
       
    94 
       
    95 CHsHFPParser::CHsHFPParser()
       
    96     {
       
    97 
       
    98     }
       
    99 
       
   100 void CHsHFPParser::ConstructL()
       
   101     {
       
   102 
       
   103     }
       
   104 
       
   105 THsHFPCommandMode CHsHFPParser::RetrieveModeL( const TDesC8 &aCommand,
       
   106         const TBool aFromAG )
       
   107     {
       
   108     TRACE_FUNC_ENTRY
       
   109 
       
   110     THsHFPCommandMode res = ECmdUnknownMode;
       
   111 
       
   112     if ( aFromAG )
       
   113         {
       
   114         if ( aCommand.Find( KHsHFPATTestModeDesAG ) != KErrNotFound )
       
   115             {
       
   116             res = ECmdModeTest;
       
   117             }
       
   118         else if ( aCommand.Find( KHsHFPATReadModeDesAG ) != KErrNotFound )
       
   119             {
       
   120             res = ECmdModeRead;
       
   121             }
       
   122         else if ( aCommand.Find( KHsHFPATWriteModeDesAG ) != KErrNotFound )
       
   123             {
       
   124             res = ECmdModeWrite;
       
   125             }
       
   126 
       
   127         }
       
   128     else
       
   129         {
       
   130         if ( aCommand.Find( KHsHFPATTestModeDes ) != KErrNotFound )
       
   131             {
       
   132             res = ECmdModeTest;
       
   133             }
       
   134         else if ( aCommand.Find( KHsHFPATReadModeDes ) != KErrNotFound )
       
   135             {
       
   136             res = ECmdModeRead;
       
   137             }
       
   138         else if ( aCommand.Find( KHsHFPATWriteModeDes ) != KErrNotFound )
       
   139             {
       
   140             res = ECmdModeWrite;
       
   141             }
       
   142 
       
   143         }
       
   144 
       
   145     if ( res == ECmdUnknownMode )
       
   146         {
       
   147         User::Leave( KErrArgument );
       
   148         }
       
   149     TRACE_FUNC_EXIT
       
   150     return res;
       
   151 
       
   152     }
       
   153 
       
   154 THsHFPCommandType CHsHFPParser::RetrieveTypeL( const TDesC8 &aCommand )
       
   155     {
       
   156     TRACE_FUNC_ENTRY
       
   157     THsHFPCommandType res = EHFPCmdUnknown;
       
   158     if ( aCommand.Find( KHsHFPBRSFCommand ) != KErrNotFound )
       
   159         {
       
   160         res = EHFPCmdBRSF;
       
   161         }
       
   162     else if ( aCommand.Find( KHsHFPCHUPCommand ) != KErrNotFound )
       
   163         {
       
   164         res = EHFPCmdCHUP;
       
   165         }
       
   166     else if ( aCommand.Find( KHsHFPCIEVCommand ) != KErrNotFound )
       
   167         {
       
   168         res = EHFPCmdCIEV;
       
   169         }
       
   170     else if ( aCommand.Find( KHsHFPOKCommand ) != KErrNotFound )
       
   171         {
       
   172         res = EHFPCmdOK;
       
   173         }
       
   174     else if ( aCommand.Find( KHsHFPERRORCommand ) != KErrNotFound )
       
   175         {
       
   176         res = EHFPCmdERROR;
       
   177         }
       
   178 
       
   179     else if ( aCommand.Find( KHsHFPCINDCommand ) != KErrNotFound )
       
   180         {
       
   181         res = EHFPCmdCIND;
       
   182         }
       
   183     else if ( aCommand.Find( KHsHFPCMERCommand ) != KErrNotFound )
       
   184         {
       
   185         res = EHFPCmdCMER;
       
   186         }
       
   187     else if ( aCommand.Find( KHsHFPCOPSCommand ) != KErrNotFound )
       
   188         {
       
   189         res = EHFPCmdCOPS;
       
   190         }
       
   191     else if ( aCommand.Find( KHsHFPRINGCommand ) != KErrNotFound )
       
   192         {
       
   193         res = EHFPCmdRING;
       
   194         }
       
   195     else if ( aCommand.Find( KHsHFPATACommand ) != KErrNotFound )
       
   196         {
       
   197         res = EHFPCmdATA;
       
   198         }
       
   199     else if ( aCommand.Find( KHsHFPVGSCommand ) != KErrNotFound )
       
   200         {
       
   201         res = EHFPCmdVGS;
       
   202         }
       
   203     else if ( aCommand.Find( KHsHFPVGMCommand ) != KErrNotFound )
       
   204         {
       
   205         res = EHFPCmdVGM;
       
   206         }
       
   207     else if ( aCommand.Find( KHsHFPBLDNCommand ) != KErrNotFound )
       
   208         {
       
   209         res = EHFPCmdBLDN;
       
   210         }
       
   211     else if ( aCommand.Find( KHsHFPATDCommand ) != KErrNotFound )
       
   212         {
       
   213         res = EHFPCmdATD;
       
   214         }
       
   215     else if ( aCommand.Find( KHsHFPCLIPCommand ) != KErrNotFound )
       
   216         {
       
   217         res = EHFPCmdCLIP;
       
   218         }
       
   219     if ( res == EHFPCmdUnknown )
       
   220         User::Leave( KErrArgument );
       
   221 
       
   222     TRACE_FUNC_EXIT
       
   223     return res;
       
   224     }
       
   225 
       
   226 void CHsHFPParser::RetrieveParamsL( const TDesC8 &aCommand,
       
   227         const THsHFPCommandMode aCmdMode, RATParamArray& aParams )
       
   228     {
       
   229     TRACE_FUNC_ENTRY
       
   230 
       
   231     RBuf8 buf;
       
   232     buf.CreateL( aCommand );
       
   233     buf.CleanupClosePushL();
       
   234 
       
   235     TInt prefixLength = 0;
       
   236     TInt suffixLength = 0;
       
   237 
       
   238     TInt modeLength = 0;
       
   239 
       
   240     TInt leftOffset = 0;
       
   241     TInt rightOffset = 0;
       
   242 
       
   243     TInt startPos = 0;
       
   244     TBool indicatorsFound = EFalse;
       
   245 
       
   246     if ( buf.Find( KHsHFPIndicatorParamsSeparator ) != KErrNotFound )
       
   247         {
       
   248         indicatorsFound = ETrue;
       
   249         }
       
   250 
       
   251     if ( indicatorsFound )
       
   252         {
       
   253         prefixLength = 9;
       
   254         suffixLength = 2;
       
   255         rightOffset = 2;
       
   256         leftOffset = 1;
       
   257         }
       
   258     else
       
   259         {
       
   260 
       
   261         rightOffset = 1;
       
   262         suffixLength = 2;
       
   263 
       
   264         switch ( aCmdMode )
       
   265 
       
   266             {
       
   267             case ECmdModeTest:
       
   268                 {
       
   269                 startPos = buf.Find( KHsHFPATTestModeDes );
       
   270                 modeLength = 2;
       
   271                 }
       
   272                 break;
       
   273             case ECmdModeRead:
       
   274                 {
       
   275                 startPos = buf.Find( KHsHFPATReadModeDes );
       
   276                 modeLength = 1;
       
   277                 }
       
   278                 break;
       
   279             case ECmdModeWrite:
       
   280                 {
       
   281                 startPos = buf.Find( KHsHFPATWriteModeDes );
       
   282                 modeLength = 1;
       
   283 
       
   284                 if ( startPos == KErrNotFound )
       
   285                     {
       
   286                     startPos = buf.Find( KHsHFPATWriteModeDesAG );
       
   287                     modeLength = 2;
       
   288                     }
       
   289 
       
   290                 }
       
   291                 break;
       
   292             default:
       
   293 
       
   294                 User::Leave( KErrArgument );
       
   295                 break;
       
   296             }
       
   297         }
       
   298 
       
   299     if ( startPos != KErrNotFound )
       
   300         {
       
   301         //strip command's prefix
       
   302         buf.Delete( 0, prefixLength + startPos + modeLength );
       
   303 
       
   304         //strip command's suffix
       
   305         buf.Delete( buf.Length() - suffixLength, suffixLength );
       
   306         }
       
   307     else
       
   308         {
       
   309         User::Leave( KErrArgument );
       
   310         }
       
   311 
       
   312     while ( buf.Size() > 0 )
       
   313         {
       
   314         TInt pos = 0;
       
   315 
       
   316         if ( indicatorsFound )
       
   317             {
       
   318             pos = buf.Find( KHsHFPIndicatorParamsSeparator );
       
   319             }
       
   320         else
       
   321             {
       
   322             pos = buf.Find( KHsHFPParamsSeparator );
       
   323             }
       
   324 
       
   325         if ( pos == KErrNotFound )
       
   326             {
       
   327             aParams.AppendL( buf );
       
   328             buf.Zero();
       
   329 
       
   330             }
       
   331         else
       
   332             {
       
   333             TPtrC8 ptr = buf.Left( pos + leftOffset );
       
   334             aParams.AppendL( ptr );
       
   335             buf.Delete( 0, pos + rightOffset );
       
   336             }
       
   337         }
       
   338 
       
   339     CleanupStack::PopAndDestroy( &buf );
       
   340     TRACE_FUNC_EXIT
       
   341     }
       
   342