bluetoothengine/btmac/src/BTMonoCmdHandler/HfpAtCmdhandler.cpp
changeset 0 f63038272f30
child 15 53b7818cd282
equal deleted inserted replaced
-1:000000000000 0:f63038272f30
       
     1 /*
       
     2 * Copyright (c) 2007 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:  BT HFP AT extension API
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "HfpAtCmdHandler.h"
       
    21 #include "debug.h"
       
    22 
       
    23 enum TActiveRequestId
       
    24     {
       
    25     EHandleCommandRequest = 1,
       
    26     EReceiveEventRequest,
       
    27     };
       
    28 
       
    29 CHFPAtCmdHandler* CHFPAtCmdHandler::NewL(MATExtObserver& aObserver)
       
    30 	{
       
    31     CHFPAtCmdHandler* self = new (ELeave) CHFPAtCmdHandler(aObserver);
       
    32     CleanupStack::PushL(self);
       
    33     self->ConstructL();
       
    34     CleanupStack::Pop(self);
       
    35     return self;
       
    36 	}
       
    37 
       
    38 CHFPAtCmdHandler::~CHFPAtCmdHandler()
       
    39 	{
       
    40     TRACE_FUNC
       
    41     delete iCommander;
       
    42     
       
    43     DeletePluginHandlers();
       
    44 
       
    45     delete iEcomListen;
       
    46     iEcomListen = NULL;
       
    47 
       
    48        if ( iATExtClient.Handle() )
       
    49         {
       
    50         iATExtClient.Close();
       
    51         }
       
    52 }
       
    53 
       
    54 void CHFPAtCmdHandler::HandleCommand(const TDesC8& aAT, const TDesC8& aReply)
       
    55     {
       
    56     TRACE_INFO((_L8("default reply '%S'"), &aReply))
       
    57     iCmdBuffer.Copy(aAT);
       
    58     iReplyBuffer.Zero();
       
    59     if (aReply.Length())
       
    60         {
       
    61         iReplyBuffer.Copy(aReply);
       
    62         iSystemReply.Copy(aReply);
       
    63         }
       
    64     iATExtClient.HandleCommand(iCommander->iStatus, 
       
    65             iCmdBuffer, iReplyBuffer, iRemainingReplyLengthPckg, iReplyTypePckg);
       
    66     iCommander->GoActive();
       
    67     }
       
    68 
       
    69 void CHFPAtCmdHandler::RequestCompletedL(CBtmcActive& aActive, TInt aErr)
       
    70     {
       
    71     TRACE_FUNC_ENTRY
       
    72     TInt err = aErr;
       
    73     switch (aActive.ServiceId())
       
    74         {
       
    75         case EHandleCommandRequest:
       
    76             {
       
    77         	if(err == KErrNone)
       
    78         		{
       
    79         		if (iRemainingReplyLengthPckg())
       
    80         		    {
       
    81                     /*RBuf8 reply2;
       
    82                     reply2.CreateL(iRemainingReplyLengthPckg() + iReplyBuffer.Length());
       
    83                     err = iATExtClient.GetNextPartOfReply(reply2, iRemainingReplyLengthPckg);
       
    84                     if (!err)
       
    85                         {
       
    86                         reply2.Insert(0, iReplyBuffer);
       
    87                         }
       
    88                     TRACE_INFO((_L8("reply '%S'"), &reply2))
       
    89                     iObserver.ATExtHandleCommandCompletedL(err, reply2);
       
    90                     reply2.Close();*/
       
    91         		    iATExtClient.GetNextPartOfReply( iRecvBuffer,
       
    92         		            iRemainingReplyLength );
       
    93         		    }
       
    94         		else
       
    95         		    {
       
    96                     TRACE_INFO((_L8("reply '%S'"), &iReplyBuffer))
       
    97                     iObserver.ATExtHandleCommandCompletedL(err, iReplyBuffer);
       
    98         		    }
       
    99         		}
       
   100             else
       
   101                 {
       
   102                 iObserver.ATExtHandleCommandCompletedL(err, iSystemReply);
       
   103                 }
       
   104             break;
       
   105             }
       
   106         }
       
   107     TRACE_FUNC_EXIT
       
   108     }
       
   109 
       
   110 void CHFPAtCmdHandler::CancelRequest(TInt aServiceId)
       
   111     {
       
   112     switch (aServiceId)
       
   113         {
       
   114         case EHandleCommandRequest:
       
   115             {
       
   116             iATExtClient.CancelHandleCommand();
       
   117             break;
       
   118             }
       
   119         }
       
   120     }
       
   121 
       
   122 TInt CHFPAtCmdHandler::HandleRunError(TInt /*aErr*/)
       
   123     {
       
   124     return KErrNone;
       
   125     }
       
   126 
       
   127 CHFPAtCmdHandler::CHFPAtCmdHandler(MATExtObserver& aObserver) 
       
   128     : iObserver(aObserver), 
       
   129       iRemainingReplyLengthPckg(iRemainingReplyLength),
       
   130       iReplyTypePckg( iReplyType )
       
   131 	{
       
   132 	TRACE_FUNC
       
   133 	}
       
   134 	
       
   135 _LIT8(KHFPAtCmd, "HFP");
       
   136 
       
   137 void CHFPAtCmdHandler::ConstructL()
       
   138     {
       
   139     TRACE_FUNC_ENTRY
       
   140     
       
   141     CleanupClosePushL( iATExtClient );
       
   142     TInt err = iATExtClient.Connect(EHfpATExtension, KHFPAtCmd);
       
   143     LEAVE_IF_ERROR(err)
       
   144     
       
   145     // Create the plugin handlers
       
   146     CreatePluginHandlersL();
       
   147     // Create the listeners
       
   148     CHFPAtEcomListen* ecomListen = CHFPAtEcomListen::NewLC( &iATExtClient, this );
       
   149     ecomListen->IssueRequest();
       
   150     
       
   151     CleanupStack::Pop( ecomListen );
       
   152     CleanupStack::Pop( &iATExtClient );
       
   153     iEcomListen = ecomListen;
       
   154     
       
   155     StartUrc();
       
   156     iCommander = CBtmcActive::NewL(*this, CActive::EPriorityStandard, EHandleCommandRequest);
       
   157     TRACE_FUNC_EXIT
       
   158     }
       
   159     
       
   160 
       
   161 // ---------------------------------------------------------------------------
       
   162 // Creates plugin handlers for this class
       
   163 // ---------------------------------------------------------------------------
       
   164 //
       
   165 void CHFPAtCmdHandler::CreatePluginHandlersL()
       
   166     {
       
   167     TRACE_INFO( _L("CHFPAtCmdHandler::CreatePluginHandlersL()") );
       
   168     if ( !iATExtClient.Handle() )
       
   169         {
       
   170         TRACE_INFO(_L("CHFPAtCmdHandler::CreatePluginHandlersL() complete") );
       
   171         User::Leave( KErrGeneral );
       
   172         }
       
   173 
       
   174     // Next create the URC handlers
       
   175     TInt i;
       
   176     TInt numOfPlugins = iATExtClient.NumberOfPlugins();
       
   177     for ( i=0; i<numOfPlugins; i++ )
       
   178         {
       
   179         AddOneUrcHandlerL();
       
   180         }
       
   181     TRACE_INFO( _L("CHFPAtCmdHandler::CreatePluginHandlersL() complete") );
       
   182     }
       
   183 
       
   184 // ---------------------------------------------------------------------------
       
   185 // From class MHFPAtEcomListen.
       
   186 // Notifies about new plugin installation
       
   187 // ---------------------------------------------------------------------------
       
   188 //
       
   189 TInt CHFPAtCmdHandler::NotifyPluginInstallation( TUid& /*aPluginUid*/ )
       
   190     {
       
   191     TRACE_INFO( _L("CHFPAtCmdHandler::NotifyPluginInstallation()" ) );
       
   192     CHFPAtUrcHandler* urcHandler = NULL;
       
   193     TRAPD( retTrap, urcHandler=AddOneUrcHandlerL() );
       
   194     if ( retTrap != KErrNone )
       
   195         {
       
   196         TRACE_INFO( _L("CHFPAtCmdHandler::NotifyPluginInstallation() (trapped!) complete" ) );
       
   197         return retTrap;
       
   198         }
       
   199     TInt retTemp = urcHandler->IssueRequest();
       
   200     if ( retTemp != KErrNone )
       
   201         {
       
   202         TRACE_INFO( _L("CHFPAtCmdHandler::NotifyPluginInstallation() (issuerequest) complete" ) );
       
   203         return retTemp;
       
   204         }
       
   205     TUid ownerUid = urcHandler->OwnerUid();
       
   206     iATExtClient.ReportListenerUpdateReady( ownerUid, EEcomTypeInstall );
       
   207 
       
   208     if ( retTemp != KErrNone )
       
   209         {
       
   210         TRACE_INFO( _L("CHFPAtCmdHandler::NotifyPluginInstallation() (recreate) complete" ));
       
   211         return retTemp;
       
   212         }
       
   213     TRACE_INFO( _L("CHFPAtCmdHandler::NotifyPluginInstallation() complete" ) );
       
   214     return KErrNone;
       
   215     }
       
   216 
       
   217 // ---------------------------------------------------------------------------
       
   218 // From class MHFPAtEcomListen.
       
   219 // Notifies about existing plugin uninstallation
       
   220 // ---------------------------------------------------------------------------
       
   221 //
       
   222 TInt CHFPAtCmdHandler::NotifyPluginUninstallation( TUid& aPluginUid )
       
   223     {
       
   224     TRACE_INFO( _L("CHFPAtCmdHandler::NotifyPluginUninstallation()" ) );
       
   225     TInt i;
       
   226     TInt count = iUrcHandlers.Count();
       
   227     for ( i=count-1; i>=0; i-- )
       
   228         {
       
   229         TUid ownerUid = iUrcHandlers[i]->OwnerUid();
       
   230         if ( ownerUid == aPluginUid )
       
   231             {
       
   232             delete iUrcHandlers[i];
       
   233             iUrcHandlers.Remove( i );
       
   234             iATExtClient.ReportListenerUpdateReady( ownerUid,
       
   235                                                  EEcomTypeUninstall );
       
   236             }
       
   237         }
       
   238 
       
   239     TRACE_INFO( _L("CHFPAtCmdHandler::NotifyPluginUninstallation() complete" ) );
       
   240     return KErrNone;
       
   241     }
       
   242     
       
   243 // ---------------------------------------------------------------------------
       
   244 // Instantiates one URC message handling class instance and adds it to the URC
       
   245 // message handler array
       
   246 // ---------------------------------------------------------------------------
       
   247 //
       
   248 CHFPAtUrcHandler* CHFPAtCmdHandler::AddOneUrcHandlerL()
       
   249     {
       
   250     TRACE_INFO( _L("CHFPAtCmdHandler::AddOneUrcHandlerL()") );
       
   251     CHFPAtUrcHandler* urcHandler = CHFPAtUrcHandler::NewLC( &iATExtClient,
       
   252                                                             iObserver );
       
   253     iUrcHandlers.AppendL( urcHandler );
       
   254     CleanupStack::Pop( urcHandler );
       
   255     TRACE_INFO( _L("CHFPAtCmdHandler::AddOneUrcHandlerL() complete") );
       
   256     return urcHandler;
       
   257     }
       
   258     
       
   259 // ---------------------------------------------------------------------------
       
   260 // Starts URC message handling
       
   261 // ---------------------------------------------------------------------------
       
   262 //
       
   263 TInt CHFPAtCmdHandler::StartUrc()
       
   264     {
       
   265     TRACE_INFO( _L("CHFPAtCmdHandler::StartUrc()") );
       
   266     TInt i;
       
   267     TInt count = iUrcHandlers.Count();
       
   268     for ( i=0; i<count; i++ )
       
   269         {
       
   270         TInt retTemp = iUrcHandlers[i]->IssueRequest();
       
   271         if ( retTemp!=KErrNone && retTemp!=KErrNotReady )
       
   272             {
       
   273             TRACE_INFO( _L("CHFPAtCmdHandler::StartUrc() (ERROR) complete") );
       
   274             return retTemp;
       
   275             }
       
   276         }
       
   277     TRACE_INFO( _L("CHFPAtCmdHandler::StartUrc() complete") );
       
   278     return KErrNone;
       
   279     }
       
   280 
       
   281 // ---------------------------------------------------------------------------
       
   282 // Stops URC message handling
       
   283 // ---------------------------------------------------------------------------
       
   284 //
       
   285 TInt CHFPAtCmdHandler::StopUrc()
       
   286     {
       
   287     TRACE_INFO( _L("CHFPAtCmdHandler::StopUrc()") );
       
   288     TInt i;
       
   289     TInt retVal = KErrNone;
       
   290     TInt count = iUrcHandlers.Count();
       
   291     for ( i=0; i<count; i++ )
       
   292         {
       
   293         retVal = iUrcHandlers[i]->Stop();
       
   294         }
       
   295     TRACE_INFO( _L("CHFPAtCmdHandler::StopUrc() complete") );
       
   296     return retVal;
       
   297     }
       
   298 
       
   299 // ---------------------------------------------------------------------------
       
   300 // Deletes all instantiated URC message handlers
       
   301 // ---------------------------------------------------------------------------
       
   302 //
       
   303 void CHFPAtCmdHandler::DeletePluginHandlers()
       
   304     {
       
   305     TRACE_INFO( _L("CHFPAtCmdHandler::DeletePluginHandlers()") );
       
   306     TInt i;
       
   307     TInt count = iUrcHandlers.Count();
       
   308     for ( i=0; i<count; i++ )
       
   309         {
       
   310         delete iUrcHandlers[i];
       
   311         iUrcHandlers[i] = NULL;
       
   312         }
       
   313     iUrcHandlers.Reset();
       
   314     iUrcHandlers.Close();
       
   315     TRACE_INFO(  _L("CHFPAtCmdHandler::DeletePluginHandlers() complete") );
       
   316     }
       
   317 
       
   318 //  End of File