localconnectivityservice/legacymodemplugin/src/legacymodemplugin.cpp
changeset 0 c3e98f10fcf4
equal deleted inserted replaced
-1:000000000000 0:c3e98f10fcf4
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Main handler for incoming requests
       
    15 *
       
    16 */
       
    17 
       
    18 #include "legacymodemplugin.h"
       
    19 #include "monitorspeakerparser.h"
       
    20 #include "debug.h"
       
    21 
       
    22 const TInt KErrorReplyLength = 9;  // CR+LF+"ERROR"+CR+LF
       
    23 
       
    24 // ---------------------------------------------------------------------------
       
    25 // Two-phased constructor.
       
    26 // ---------------------------------------------------------------------------
       
    27 //
       
    28 CLegacyModemPlugin* CLegacyModemPlugin::NewL()
       
    29     {
       
    30     CLegacyModemPlugin* self = new (ELeave) CLegacyModemPlugin();
       
    31     CleanupStack::PushL(self);
       
    32     self->ConstructL();
       
    33     CleanupStack::Pop(self);
       
    34     return self;
       
    35     }
       
    36 
       
    37 // ---------------------------------------------------------------------------
       
    38 // Destructor.
       
    39 // ---------------------------------------------------------------------------
       
    40 //
       
    41 CLegacyModemPlugin::~CLegacyModemPlugin()
       
    42 	{
       
    43 	iHandlers.ResetAndDestroy();
       
    44 	iHandlers.Close();
       
    45     iReplyBuffer.Close();
       
    46 	}
       
    47 
       
    48 // ---------------------------------------------------------------------------
       
    49 // CLegacyModemPlugin::CLegacyModemPlugin
       
    50 // ---------------------------------------------------------------------------
       
    51 //
       
    52 CLegacyModemPlugin::CLegacyModemPlugin() : CATExtPluginBase()
       
    53     {
       
    54     iHandler = NULL;
       
    55     iHcCmd = NULL;
       
    56     iHcReply = NULL;
       
    57     }
       
    58 
       
    59 // ---------------------------------------------------------------------------
       
    60 // CLegacyModemPlugin::ConstructL
       
    61 // ---------------------------------------------------------------------------
       
    62 //
       
    63 void CLegacyModemPlugin::ConstructL()
       
    64     {
       
    65     CLegacyModemPluginBase* handler = NULL;
       
    66     handler = CMonitorSpeakerParser::NewL( this );
       
    67     CleanupStack::PushL( handler );
       
    68     iHandlers.AppendL( handler );
       
    69     CleanupStack::Pop( handler );
       
    70    	}
       
    71 
       
    72 // ---------------------------------------------------------------------------
       
    73 // Reports connection identifier name to the extension plugin.
       
    74 // ---------------------------------------------------------------------------
       
    75 //
       
    76 void CLegacyModemPlugin::ReportConnectionName( const TDesC8& /*aName*/ )
       
    77     {
       
    78     }
       
    79 
       
    80 // ---------------------------------------------------------------------------
       
    81 // Reports the support status of an AT command. This is a synchronous API.
       
    82 // ---------------------------------------------------------------------------
       
    83 //
       
    84 TBool CLegacyModemPlugin::IsCommandSupported( const TDesC8& aCmd )
       
    85     {
       
    86     TRACE_FUNC_ENTRY
       
    87     iHcCmd = NULL;
       
    88     iHcReply = NULL;
       
    89     TInt i;
       
    90     TInt count = iHandlers.Count();
       
    91     for ( i=0; i<count; i++ )
       
    92         {
       
    93         CLegacyModemPluginBase* handler = iHandlers[i];
       
    94         TBool supported = handler->IsCommandSupported( aCmd );
       
    95         if ( supported )
       
    96             {
       
    97             iHandler = handler;
       
    98             TRACE_FUNC_EXIT
       
    99             return ETrue;
       
   100             }
       
   101         }
       
   102     iHandler = NULL;
       
   103     TRACE_FUNC_EXIT
       
   104     return EFalse;
       
   105     }
       
   106 
       
   107 // ---------------------------------------------------------------------------
       
   108 // Handles an AT command. Cancelling of the pending request is done by
       
   109 // HandleCommandCancel(). The implementation in the extension plugin should
       
   110 // be asynchronous.
       
   111 // ---------------------------------------------------------------------------
       
   112 //
       
   113 void CLegacyModemPlugin::HandleCommand( const TDesC8& aCmd,
       
   114                                         RBuf8& aReply,
       
   115                                         TBool aReplyNeeded )
       
   116 	{
       
   117 	TRACE_FUNC_ENTRY
       
   118 	if ( iHandler )
       
   119 	    {
       
   120 	    iHcCmd = &aCmd;
       
   121 	    iHcReply = &aReply;
       
   122 	    iHandler->HandleCommand( aCmd, aReply, aReplyNeeded );
       
   123 	    }
       
   124 	TRACE_FUNC_EXIT
       
   125     }
       
   126 
       
   127 // ---------------------------------------------------------------------------
       
   128 // Cancels a pending HandleCommand request.
       
   129 // ---------------------------------------------------------------------------
       
   130 //
       
   131 void CLegacyModemPlugin::HandleCommandCancel()
       
   132     {
       
   133     TRACE_FUNC_ENTRY
       
   134 	if ( iHandler )
       
   135 	    {
       
   136 	    iHandler->HandleCommandCancel();
       
   137 	    }
       
   138 	TRACE_FUNC_EXIT
       
   139     }
       
   140 
       
   141 // ---------------------------------------------------------------------------
       
   142 // Next reply part's length.
       
   143 // The value must be equal or less than KDefaultCmdBufLength.
       
   144 // When the reply from this method is zero, ATEXT stops calling
       
   145 // GetNextPartOfReply().
       
   146 // ---------------------------------------------------------------------------
       
   147 //
       
   148 TInt CLegacyModemPlugin::NextReplyPartLength()
       
   149     {
       
   150     TRACE_FUNC_ENTRY
       
   151     if ( iReplyBuffer.Length() < KDefaultCmdBufLength )
       
   152         {
       
   153         TRACE_FUNC_EXIT
       
   154         return iReplyBuffer.Length();
       
   155         }
       
   156     TRACE_FUNC_EXIT
       
   157     return KDefaultCmdBufLength;
       
   158     }
       
   159 
       
   160 // ---------------------------------------------------------------------------
       
   161 // Gets the next part of reply initially set by HandleCommandComplete().
       
   162 // Length of aNextReply must be equal or less than KDefaultCmdBufLength.
       
   163 // ---------------------------------------------------------------------------
       
   164 //
       
   165 TInt CLegacyModemPlugin::GetNextPartOfReply( RBuf8& aNextReply )
       
   166     {
       
   167     TRACE_FUNC_ENTRY
       
   168     TInt retVal = CreatePartOfReply( aNextReply );
       
   169     TRACE_FUNC_EXIT
       
   170     return retVal;
       
   171     }
       
   172 
       
   173 // ---------------------------------------------------------------------------
       
   174 // Receives unsolicited results. Cancelling of the pending request is done by
       
   175 // by ReceiveUnsolicitedResultCancel(). The implementation in the extension
       
   176 // plugin should be asynchronous.
       
   177 // ---------------------------------------------------------------------------
       
   178 //
       
   179 void CLegacyModemPlugin::ReceiveUnsolicitedResult()
       
   180     {
       
   181     TRACE_FUNC_ENTRY
       
   182     TRACE_FUNC_EXIT
       
   183     }
       
   184 
       
   185 // ---------------------------------------------------------------------------
       
   186 // Cancels a pending ReceiveUnsolicitedResult request.
       
   187 // ---------------------------------------------------------------------------
       
   188 //
       
   189 void CLegacyModemPlugin::ReceiveUnsolicitedResultCancel()
       
   190     {
       
   191     TRACE_FUNC_ENTRY
       
   192     TRACE_FUNC_EXIT
       
   193     }
       
   194 
       
   195 // ---------------------------------------------------------------------------
       
   196 // Reports NVRAM status change to the plugins.
       
   197 // ---------------------------------------------------------------------------
       
   198 //
       
   199 void CLegacyModemPlugin::ReportNvramStatusChange( const TDesC8& /*aNvram*/ )
       
   200     {
       
   201     TRACE_FUNC_ENTRY
       
   202     TRACE_FUNC_EXIT
       
   203     }
       
   204 
       
   205 // ---------------------------------------------------------------------------
       
   206 // Reports about external handle command error condition.
       
   207 // This is for cases when for example DUN decided the reply contained an
       
   208 // error condition but the plugin is still handling the command internally.
       
   209 // Example: "AT+TEST;+TEST2" was given in command line; "AT+TEST" returns
       
   210 // non-EReplyTypeError condition and "AT+TEST2" returns EReplyTypeError.
       
   211 // As the plugin(s) returning the non-EReplyTypeError may still have some
       
   212 // ongoing operation then these plugins are notified about the external
       
   213 // EReplyTypeError in command line processing. It is to be noted that
       
   214 // HandleCommandCancel() is not sufficient to stop the processing as the
       
   215 // command handling has already finished.
       
   216 // ---------------------------------------------------------------------------
       
   217 //
       
   218 void CLegacyModemPlugin::ReportExternalHandleCommandError()
       
   219     {
       
   220     TRACE_FUNC_ENTRY
       
   221     TRACE_FUNC_EXIT
       
   222     }
       
   223 
       
   224 // ---------------------------------------------------------------------------
       
   225 // Creates part of reply from the global reply buffer to the destination
       
   226 // buffer. Used with APIs which need the next part of reply in multipart reply
       
   227 // requests.
       
   228 // ---------------------------------------------------------------------------
       
   229 //
       
   230 TInt CLegacyModemPlugin::CreatePartOfReply( RBuf8& aDstBuffer )
       
   231     {
       
   232     TRACE_FUNC_ENTRY
       
   233     if ( iReplyBuffer.Length() <= 0 )
       
   234         {
       
   235         TRACE_FUNC_EXIT
       
   236         return KErrGeneral;
       
   237         }
       
   238     TInt partLength = NextReplyPartLength();
       
   239     if ( iReplyBuffer.Length() < partLength )
       
   240         {
       
   241         TRACE_FUNC_EXIT
       
   242         return KErrNotFound;
       
   243         }
       
   244     aDstBuffer.Create( iReplyBuffer, partLength );
       
   245     iReplyBuffer.Delete( 0, partLength );
       
   246     if ( iReplyBuffer.Length() == 0 )
       
   247         {
       
   248         iReplyBuffer.Close();
       
   249         }
       
   250     TRACE_FUNC_EXIT
       
   251     return KErrNone;
       
   252     }
       
   253 
       
   254 // ---------------------------------------------------------------------------
       
   255 // Creates an AT command reply based on the reply type and completes the
       
   256 // request to ATEXT. Uses iReplyBuffer for reply storage.
       
   257 // ---------------------------------------------------------------------------
       
   258 //
       
   259 TInt CLegacyModemPlugin::CreateReplyAndComplete(
       
   260     TATExtensionReplyType aReplyType,
       
   261     const TDesC8& aSrcBuffer,
       
   262     TInt aError )
       
   263     {
       
   264     TRACE_FUNC_ENTRY
       
   265     iReplyBuffer.Close();
       
   266     if ( aError != KErrNone )
       
   267         {
       
   268         HandleCommandCompleted( aError, EReplyTypeUndefined );
       
   269         iHcCmd = NULL;
       
   270         iHcReply = NULL;
       
   271         TRACE_FUNC_EXIT
       
   272         return KErrNone;
       
   273         }
       
   274     if ( !iHcReply )
       
   275         {
       
   276         TRACE_FUNC_EXIT
       
   277         return KErrGeneral;
       
   278         }
       
   279     switch ( aReplyType )
       
   280         {
       
   281         case EReplyTypeOther:
       
   282             if ( iQuietMode || !iVerboseMode )
       
   283                 {
       
   284                 iReplyBuffer.Create( KNullDesC8 );
       
   285                 }
       
   286             else
       
   287                 {
       
   288                 iReplyBuffer.Create( aSrcBuffer );
       
   289                 }
       
   290             break;
       
   291         case EReplyTypeOk:
       
   292             CreateOkOrErrorReply( iReplyBuffer, ETrue );
       
   293             break;
       
   294         case EReplyTypeError:
       
   295             CreateOkOrErrorReply( iReplyBuffer, EFalse );
       
   296             break;
       
   297         default:
       
   298             TRACE_FUNC_EXIT
       
   299             return KErrGeneral;
       
   300         }
       
   301     CreatePartOfReply( *iHcReply );
       
   302     HandleCommandCompleted( KErrNone, aReplyType );
       
   303     iHcCmd = NULL;
       
   304     iHcReply = NULL;
       
   305     TRACE_FUNC_EXIT
       
   306     return KErrNone;
       
   307     }
       
   308 
       
   309 // ---------------------------------------------------------------------------
       
   310 // Creates a buffer for "OK" or "ERROR" reply based on the line settings
       
   311 // ---------------------------------------------------------------------------
       
   312 //
       
   313 TInt CLegacyModemPlugin::CreateOkOrErrorReply( RBuf8& aReplyBuffer,
       
   314                                                TBool aOkReply )
       
   315     {
       
   316     TRACE_FUNC_ENTRY
       
   317     if ( iQuietMode )
       
   318         {
       
   319         TRACE_FUNC_EXIT
       
   320         return iReplyBuffer.Create( KNullDesC8 );
       
   321         }
       
   322     _LIT8( KErrorReplyVerbose, "ERROR" );
       
   323     _LIT8( KOkReplyVerbose,    "OK" );
       
   324     _LIT8( KErrorReplyNumeric, "4" );
       
   325     _LIT8( KOkReplyNumeric,    "0" );
       
   326     TBuf8<KErrorReplyLength> replyBuffer;
       
   327     if ( iVerboseMode )
       
   328         {
       
   329         replyBuffer.Append( iCarriageReturn );
       
   330         replyBuffer.Append( iLineFeed );
       
   331         if ( aOkReply )
       
   332             {
       
   333             replyBuffer.Append( KOkReplyVerbose );
       
   334             }
       
   335         else
       
   336             {
       
   337             replyBuffer.Append( KErrorReplyVerbose );
       
   338             }
       
   339         replyBuffer.Append( iCarriageReturn );
       
   340         replyBuffer.Append( iLineFeed );
       
   341         }
       
   342     else
       
   343         {
       
   344         if ( aOkReply )
       
   345             {
       
   346             replyBuffer.Append( KOkReplyNumeric );
       
   347             }
       
   348         else
       
   349             {
       
   350             replyBuffer.Append( KErrorReplyNumeric );
       
   351             }
       
   352         replyBuffer.Append( iCarriageReturn );
       
   353         }
       
   354     TInt retVal = aReplyBuffer.Create( replyBuffer );
       
   355     TRACE_FUNC_EXIT
       
   356     return retVal;
       
   357     }
       
   358 
       
   359 // ---------------------------------------------------------------------------
       
   360 // From MHfpExtPlugin.
       
   361 // Checks if the command is a base, set, read or test type of command
       
   362 // ---------------------------------------------------------------------------
       
   363 //
       
   364 TCmdHandlerType CLegacyModemPlugin::CheckCommandType( const TDesC8& aCmdBase,
       
   365                                                       const TDesC8& aCmdFull )
       
   366     {
       
   367     TRACE_FUNC_ENTRY
       
   368     TInt retTemp = KErrNone;
       
   369     TBuf8<KDefaultCmdBufLength> atCmdBuffer;
       
   370     atCmdBuffer.Copy( aCmdBase );
       
   371     // Check "base" command ("AT+COMMAND")
       
   372     retTemp = aCmdFull.Compare( atCmdBuffer );
       
   373     if ( retTemp == 0 )
       
   374         {
       
   375         TRACE_FUNC_EXIT
       
   376         return ECmdHandlerTypeBase;
       
   377         }
       
   378     // Check "read" command ("AT+COMMAND?")
       
   379     // Add last question mark
       
   380     atCmdBuffer.Append( '?' );
       
   381     retTemp = aCmdFull.Compare( atCmdBuffer );
       
   382     if ( retTemp == 0 )
       
   383         {
       
   384         TRACE_FUNC_EXIT
       
   385         return ECmdHandlerTypeRead;
       
   386         }
       
   387     // Check "test" command ("AT+COMMAND=?")
       
   388     // Add "=" before the question mark
       
   389     _LIT8( KAssignmentMark, "=" );
       
   390     atCmdBuffer.Insert( atCmdBuffer.Length()-1, KAssignmentMark );
       
   391     retTemp = aCmdFull.Compare( atCmdBuffer );
       
   392     if ( retTemp == 0 )
       
   393         {
       
   394         TRACE_FUNC_EXIT
       
   395         return ECmdHandlerTypeTest;
       
   396         }
       
   397     // Check "set" command ("AT+COMMAND=")
       
   398     // Remove last question mark
       
   399     atCmdBuffer.SetLength( atCmdBuffer.Length() - 1 );
       
   400     retTemp = aCmdFull.Compare( atCmdBuffer );
       
   401     if ( retTemp == 0 )
       
   402         {
       
   403         TRACE_FUNC_EXIT
       
   404         return ECmdHandlerTypeSet;
       
   405         }
       
   406     TRACE_FUNC_EXIT
       
   407     return ECmdHandlerTypeUndefined;
       
   408     }