localconnectivityservice/lccustomplugin/src/lccustomplugin.cpp
changeset 0 c3e98f10fcf4
child 7 a2f12998bb04
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 
       
    19 #include "lccustomplugin.h"
       
    20 #include "lclistallcmd.h"
       
    21 #include "debug.h"
       
    22 
       
    23 const TInt KErrorReplyLength = 9;  // CR+LF+"ERROR"+CR+LF
       
    24 
       
    25 // ---------------------------------------------------------------------------
       
    26 // Two-phased constructor.
       
    27 // ---------------------------------------------------------------------------
       
    28 //
       
    29 CLcCustomPlugin* CLcCustomPlugin::NewL()
       
    30     {
       
    31     CLcCustomPlugin* self = new (ELeave) CLcCustomPlugin();
       
    32     CleanupStack::PushL(self);
       
    33     self->ConstructL();
       
    34     CleanupStack::Pop(self);
       
    35     return self;
       
    36     }
       
    37 
       
    38 // ---------------------------------------------------------------------------
       
    39 // Destructor.
       
    40 // ---------------------------------------------------------------------------
       
    41 //
       
    42 CLcCustomPlugin::~CLcCustomPlugin()
       
    43 	{
       
    44 	iHandlers.ResetAndDestroy();
       
    45 	iHandlers.Close();
       
    46     iReplyBuffer.Close();
       
    47 	}
       
    48 
       
    49 // ---------------------------------------------------------------------------
       
    50 // CLcCustomPlugin::CLcCustomPlugin
       
    51 // ---------------------------------------------------------------------------
       
    52 //
       
    53 CLcCustomPlugin::CLcCustomPlugin() : CATExtPluginBase()
       
    54     {
       
    55     iHandler = NULL;
       
    56     }
       
    57 
       
    58 // ---------------------------------------------------------------------------
       
    59 // CLcCustomPlugin::ConstructL
       
    60 // ---------------------------------------------------------------------------
       
    61 //
       
    62 void CLcCustomPlugin::ConstructL()
       
    63     {
       
    64     CLcCustomPluginBase* handler = NULL;
       
    65     handler = CLcListAllCmd::NewL( this );
       
    66     CleanupStack::PushL( handler );
       
    67     iHandlers.AppendL( handler );
       
    68     CleanupStack::Pop( handler );
       
    69    	}
       
    70 
       
    71 // ---------------------------------------------------------------------------
       
    72 // Reports connection identifier name to the extension plugin.
       
    73 // ---------------------------------------------------------------------------
       
    74 //
       
    75 void CLcCustomPlugin::ReportConnectionName( const TDesC8& /*aName*/ )
       
    76     {
       
    77     }
       
    78 
       
    79 // ---------------------------------------------------------------------------
       
    80 // Reports the support status of an AT command. This is a synchronous API.
       
    81 // ---------------------------------------------------------------------------
       
    82 //
       
    83 TBool CLcCustomPlugin::IsCommandSupported( const TDesC8& aCmd )
       
    84     {
       
    85     TRACE_FUNC_ENTRY
       
    86     TInt i;
       
    87     TInt count = iHandlers.Count();
       
    88     for ( i=0; i<count; i++ )
       
    89         {
       
    90         CLcCustomPluginBase* handler = iHandlers[i];
       
    91         TBool supported = handler->IsCommandSupported( aCmd );
       
    92         if ( supported )
       
    93             {
       
    94             iHandler = handler;
       
    95             TRACE_FUNC_EXIT
       
    96             return ETrue;
       
    97             }
       
    98         }
       
    99     iHandler = NULL;
       
   100     TRACE_FUNC_EXIT
       
   101     return EFalse;
       
   102     }
       
   103 
       
   104 // ---------------------------------------------------------------------------
       
   105 // Handles an AT command. Cancelling of the pending request is done by
       
   106 // HandleCommandCancel(). The implementation in the extension plugin should
       
   107 // be asynchronous.
       
   108 // ---------------------------------------------------------------------------
       
   109 //
       
   110 void CLcCustomPlugin::HandleCommand( const TDesC8& aCmd,
       
   111                                      RBuf8& aReply,
       
   112                                      TBool aReplyNeeded )
       
   113 	{
       
   114 	TRACE_FUNC_ENTRY
       
   115 	if ( iHandler )
       
   116 	    {
       
   117 	    iHandler->HandleCommand( aCmd, aReply, aReplyNeeded );
       
   118 	    }
       
   119 	TRACE_FUNC_EXIT
       
   120     }
       
   121 
       
   122 // ---------------------------------------------------------------------------
       
   123 // Cancels a pending HandleCommand request.
       
   124 // ---------------------------------------------------------------------------
       
   125 //
       
   126 void CLcCustomPlugin::HandleCommandCancel()
       
   127     {
       
   128     TRACE_FUNC_ENTRY
       
   129 	if ( iHandler )
       
   130 	    {
       
   131 	    iHandler->HandleCommandCancel();
       
   132 	    }
       
   133 	TRACE_FUNC_EXIT
       
   134     }
       
   135 
       
   136 // ---------------------------------------------------------------------------
       
   137 // Next reply part's length.
       
   138 // The value must be equal or less than KDefaultCmdBufLength.
       
   139 // When the reply from this method is zero, ATEXT stops calling
       
   140 // GetNextPartOfReply().
       
   141 // ---------------------------------------------------------------------------
       
   142 //
       
   143 TInt CLcCustomPlugin::NextReplyPartLength()
       
   144     {
       
   145     TRACE_FUNC_ENTRY
       
   146     if ( iReplyBuffer.Length() < KDefaultCmdBufLength )
       
   147         {
       
   148         TRACE_FUNC_EXIT
       
   149         return iReplyBuffer.Length();
       
   150         }
       
   151     TRACE_FUNC_EXIT
       
   152     return KDefaultCmdBufLength;
       
   153     }
       
   154 
       
   155 // ---------------------------------------------------------------------------
       
   156 // Gets the next part of reply initially set by HandleCommandComplete().
       
   157 // Length of aNextReply must be equal or less than KDefaultCmdBufLength.
       
   158 // ---------------------------------------------------------------------------
       
   159 //
       
   160 TInt CLcCustomPlugin::GetNextPartOfReply( RBuf8& aNextReply )
       
   161     {
       
   162     TRACE_FUNC_ENTRY
       
   163     TInt retVal = CreatePartOfReply( aNextReply );
       
   164     TRACE_FUNC_EXIT
       
   165     return retVal;
       
   166     }
       
   167 
       
   168 // ---------------------------------------------------------------------------
       
   169 // Receives unsolicited results. Cancelling of the pending request is done by
       
   170 // by ReceiveUnsolicitedResultCancel(). The implementation in the extension
       
   171 // plugin should be asynchronous.
       
   172 // ---------------------------------------------------------------------------
       
   173 //
       
   174 void CLcCustomPlugin::ReceiveUnsolicitedResult()
       
   175     {
       
   176     TRACE_FUNC_ENTRY
       
   177     TRACE_FUNC_EXIT
       
   178     }
       
   179 
       
   180 // ---------------------------------------------------------------------------
       
   181 // Cancels a pending ReceiveUnsolicitedResult request.
       
   182 // ---------------------------------------------------------------------------
       
   183 //
       
   184 void CLcCustomPlugin::ReceiveUnsolicitedResultCancel()
       
   185     {
       
   186     TRACE_FUNC_ENTRY
       
   187     TRACE_FUNC_EXIT
       
   188     }
       
   189 
       
   190 // ---------------------------------------------------------------------------
       
   191 // Reports NVRAM status change to the plugins.
       
   192 // ---------------------------------------------------------------------------
       
   193 //
       
   194 void CLcCustomPlugin::ReportNvramStatusChange( const TDesC8& /*aNvram*/ )
       
   195     {
       
   196     TRACE_FUNC_ENTRY
       
   197     TRACE_FUNC_EXIT
       
   198     }
       
   199 
       
   200 // ---------------------------------------------------------------------------
       
   201 // Reports about external handle command error condition.
       
   202 // This is for cases when for example DUN decided the reply contained an
       
   203 // error condition but the plugin is still handling the command internally.
       
   204 // Example: in command line "AT+TEST;ATDT1234" was given. "AT+TEST" returns
       
   205 // "OK" and "ATDT" returns "CONNECT". Because "OK" and "CONNECT" are
       
   206 // different reply types the condition is "ERROR" and DUN ends processing.
       
   207 // This solution keeps the pointer to the last AT command handling plugin
       
   208 // inside ATEXT and calls this function there to report the error.
       
   209 // It is to be noted that HandleCommandCancel() is not sufficient to stop
       
   210 // the processing as the command handling has already finished.
       
   211 // ---------------------------------------------------------------------------
       
   212 //
       
   213 void CLcCustomPlugin::ReportExternalHandleCommandError()
       
   214     {
       
   215     TRACE_FUNC_ENTRY
       
   216     TRACE_FUNC_EXIT
       
   217     }
       
   218 
       
   219 // ---------------------------------------------------------------------------
       
   220 // Creates part of reply from the global reply buffer to the destination
       
   221 // buffer. Used with APIs which need the next part of reply in multipart reply
       
   222 // requests.
       
   223 // ---------------------------------------------------------------------------
       
   224 //
       
   225 TInt CLcCustomPlugin::CreatePartOfReply( RBuf8& aDstBuffer )
       
   226     {
       
   227     TRACE_FUNC_ENTRY
       
   228     if ( iReplyBuffer.Length() <= 0 )
       
   229         {
       
   230         TRACE_FUNC_EXIT
       
   231         return KErrGeneral;
       
   232         }
       
   233     TInt partLength = NextReplyPartLength();
       
   234     if ( iReplyBuffer.Length() < partLength )
       
   235         {
       
   236         TRACE_FUNC_EXIT
       
   237         return KErrNotFound;
       
   238         }
       
   239     aDstBuffer.Create( iReplyBuffer, partLength );
       
   240     iReplyBuffer.Delete( 0, partLength );
       
   241     if ( iReplyBuffer.Length() == 0 )
       
   242         {
       
   243         iReplyBuffer.Close();
       
   244         }
       
   245     TRACE_FUNC_EXIT
       
   246     return KErrNone;
       
   247     }
       
   248 
       
   249 // ---------------------------------------------------------------------------
       
   250 // Creates an AT command reply based on the reply type and completes the
       
   251 // request to ATEXT. Uses iReplyBuffer for reply storage.
       
   252 // ---------------------------------------------------------------------------
       
   253 //
       
   254 TInt CLcCustomPlugin::CreateReplyAndComplete( TATExtensionReplyType aReplyType,
       
   255                                               RBuf8& aDstBuffer,
       
   256                                               const TDesC8& aSrcBuffer,
       
   257                                               TInt aError )
       
   258     {
       
   259     TRACE_FUNC_ENTRY
       
   260     iReplyBuffer.Close();
       
   261     if ( aError != KErrNone )
       
   262         {
       
   263         HandleCommandCompleted( aError, EReplyTypeUndefined );
       
   264         TRACE_FUNC_EXIT
       
   265         return KErrNone;
       
   266         }
       
   267     TInt retVal = KErrNone;
       
   268     switch ( aReplyType )
       
   269         {
       
   270         case EReplyTypeOther:
       
   271             if ( iQuietMode || !iVerboseMode )
       
   272                 {
       
   273                 iReplyBuffer.Create( KNullDesC8 );
       
   274                 }
       
   275             else
       
   276                 {
       
   277                 iReplyBuffer.Create( aSrcBuffer );
       
   278                 }
       
   279             CreatePartOfReply( aDstBuffer );
       
   280             HandleCommandCompleted( KErrNone, aReplyType );
       
   281             break;
       
   282         case EReplyTypeOk:
       
   283             CreateOkOrErrorReply( iReplyBuffer, ETrue );
       
   284             CreatePartOfReply( aDstBuffer );
       
   285             HandleCommandCompleted( KErrNone, aReplyType );
       
   286             break;
       
   287         case EReplyTypeError:
       
   288             CreateOkOrErrorReply( iReplyBuffer, EFalse );
       
   289             CreatePartOfReply( aDstBuffer );
       
   290             HandleCommandCompleted( KErrNone, aReplyType );
       
   291             break;
       
   292         default:
       
   293             retVal = KErrGeneral;
       
   294             break;
       
   295         }
       
   296     TRACE_FUNC_EXIT
       
   297     return retVal;
       
   298     }
       
   299 
       
   300 // ---------------------------------------------------------------------------
       
   301 // Creates a buffer for "OK" or "ERROR" reply based on the line settings
       
   302 // ---------------------------------------------------------------------------
       
   303 //
       
   304 TInt CLcCustomPlugin::CreateOkOrErrorReply( RBuf8& aReplyBuffer,
       
   305                                             TBool aOkReply )
       
   306     {
       
   307     TRACE_FUNC_ENTRY
       
   308     if ( iQuietMode )
       
   309         {
       
   310         TRACE_FUNC_EXIT
       
   311         return iReplyBuffer.Create( KNullDesC8 );
       
   312         }
       
   313     _LIT8( KErrorReplyVerbose, "ERROR" );
       
   314     _LIT8( KOkReplyVerbose,    "OK" );
       
   315     _LIT8( KErrorReplyNumeric, "4" );
       
   316     _LIT8( KOkReplyNumeric,    "0" );
       
   317     TBuf8<KErrorReplyLength> replyBuffer;
       
   318     if ( iVerboseMode )
       
   319         {
       
   320         replyBuffer.Append( iCarriageReturn );
       
   321         replyBuffer.Append( iLineFeed );
       
   322         if ( aOkReply )
       
   323             {
       
   324             replyBuffer.Append( KOkReplyVerbose );
       
   325             }
       
   326         else
       
   327             {
       
   328             replyBuffer.Append( KErrorReplyVerbose );
       
   329             }
       
   330         replyBuffer.Append( iCarriageReturn );
       
   331         replyBuffer.Append( iLineFeed );
       
   332         }
       
   333     else
       
   334         {
       
   335         if ( aOkReply )
       
   336             {
       
   337             replyBuffer.Append( KOkReplyNumeric );
       
   338             }
       
   339         else
       
   340             {
       
   341             replyBuffer.Append( KErrorReplyNumeric );
       
   342             }
       
   343         replyBuffer.Append( iCarriageReturn );
       
   344         }
       
   345     TInt retVal = aReplyBuffer.Create( replyBuffer );
       
   346     TRACE_FUNC_EXIT
       
   347     return retVal;
       
   348     }
       
   349 
       
   350 // ---------------------------------------------------------------------------
       
   351 // From MLcCustomPlugin.
       
   352 // Returns the array of supported commands
       
   353 // ---------------------------------------------------------------------------
       
   354 //
       
   355 TInt CLcCustomPlugin::GetSupportedCommands( RPointerArray<HBufC8>& aCmds )
       
   356     {
       
   357     TRACE_FUNC_ENTRY
       
   358     // Force superclass call here:
       
   359     TInt retVal = CATExtPluginBase::GetSupportedCommands( aCmds );
       
   360     TRACE_FUNC_EXIT
       
   361     return retVal;
       
   362     }
       
   363 
       
   364 // ---------------------------------------------------------------------------
       
   365 // From MLcCustomPlugin.
       
   366 // Returns plugin's character value settings (from CATExtPluginBase)
       
   367 // ---------------------------------------------------------------------------
       
   368 //
       
   369 TInt CLcCustomPlugin::GetCharacterValue( TCharacterTypes aCharType,
       
   370                                          TChar& aChar )
       
   371     {
       
   372     TRACE_FUNC_ENTRY
       
   373     TInt retVal = KErrNone;
       
   374     switch ( aCharType )
       
   375         {
       
   376         case ECharTypeCR:
       
   377             aChar = iCarriageReturn;
       
   378             break;
       
   379         case ECharTypeLF:
       
   380             aChar = iLineFeed;
       
   381             break;
       
   382         case ECharTypeBS:
       
   383             aChar = iBackspace;
       
   384             break;
       
   385         default:
       
   386             retVal = KErrNotFound;
       
   387             break;
       
   388         }
       
   389     TRACE_FUNC_EXIT
       
   390     return retVal;
       
   391     }
       
   392 
       
   393 // ---------------------------------------------------------------------------
       
   394 // From MLcCustomPlugin.
       
   395 // Returns plugin's mode value settings (from CATExtPluginBase)
       
   396 // ---------------------------------------------------------------------------
       
   397 //
       
   398 TInt CLcCustomPlugin::GetModeValue( TModeTypes aModeType, TBool& aMode )
       
   399     {
       
   400     TRACE_FUNC_ENTRY
       
   401     TInt retVal = KErrNone;
       
   402     switch ( aModeType )
       
   403         {
       
   404         case EModeTypeQuiet:
       
   405             aMode = iQuietMode;
       
   406             break;
       
   407         case EModeTypeVerbose:
       
   408             aMode = iVerboseMode;
       
   409             break;
       
   410         default:
       
   411             retVal = KErrNotFound;
       
   412             break;
       
   413         }
       
   414     TRACE_FUNC_EXIT
       
   415     return retVal;
       
   416     }