bluetoothengine/btmac/src/btmac/btmman.cpp
changeset 0 f63038272f30
child 2 0b192a3a05a4
equal deleted inserted replaced
-1:000000000000 0:f63038272f30
       
     1 /*
       
     2 * Copyright (c) 2005-2006 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:  CBtmMan definition
       
    15 *  Version     : %version: 15.1.7 %
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include <btdevice.h>
       
    21 #include <AccessoryTypes.h>
       
    22 #include <btengdiscovery.h>
       
    23 #include <btengdomaincrkeys.h>
       
    24 #include <centralrepository.h>
       
    25 #include "btmchandlerapi.h"
       
    26 #include "btmman.h"
       
    27 #include "btmactive.h"
       
    28 #include "btmstate.h"
       
    29 #include "btmslisten.h"
       
    30 #include "debug.h"
       
    31 
       
    32 
       
    33 // CONSTANTS
       
    34 #ifdef _DEBUG // only used in traces
       
    35 const TInt KBTDevAddrReadable = KBTDevAddrSize * 2;
       
    36 #endif
       
    37 
       
    38 const TInt KDestroyObsoleteStateOneShot = 20;
       
    39 
       
    40 void CompleteRequest(TRequestStatus* aStatus, TInt aErr)
       
    41     {
       
    42     User::RequestComplete(aStatus, aErr);
       
    43     }
       
    44 
       
    45 // ======== MEMBER FUNCTIONS ========
       
    46 
       
    47 CBtmMan* CBtmMan::NewL(TPluginParams& aParams)
       
    48     {
       
    49     CBtmMan* self = new (ELeave) CBtmMan(aParams);
       
    50     CleanupStack::PushL(self);
       
    51     self->ConstructL();
       
    52     CleanupStack::Pop(self);
       
    53     return self;
       
    54     }
       
    55 
       
    56 // -----------------------------------------------------------------------------
       
    57 // Destructor.
       
    58 // -----------------------------------------------------------------------------
       
    59 //
       
    60 CBtmMan::~CBtmMan()
       
    61     {
       
    62     TRACE_FUNC_ENTRY
       
    63     delete iActive;
       
    64     delete iCmdHandler;
       
    65     iTrashBin.ResetAndDestroy();
       
    66     iTrashBin.Close();
       
    67     delete iState;
       
    68     iMonoStateProp.Close();
       
    69     iMonoAddrProp.Close();
       
    70     iSockServ.Close();
       
    71     DeleteAllRegisteredServices();
       
    72     delete iBteng;
       
    73     iServices.Close();
       
    74     TRACE_FUNC_EXIT
       
    75     }
       
    76 
       
    77 void CBtmMan::ChangeStateL(CBtmState* aState)
       
    78     {
       
    79     TRACE_FUNC_ENTRY
       
    80     TRACE_ASSERT(!!aState, EBTPanicNullPointer);
       
    81     if (iState)
       
    82         {
       
    83         iTrashBin.AppendL(iState);
       
    84         }
       
    85         
       
    86     iState = aState;
       
    87     TInt err = KErrNone;
       
    88     TRAP(err, iState->EnterL());
       
    89     if (err)
       
    90         {
       
    91         ChangeStateL(iState->ErrorOnEntryL(err));
       
    92         }
       
    93   
       
    94     if (iTrashBin.Count())
       
    95         {
       
    96         if (!iActive)
       
    97             {
       
    98             iActive = CBtmActive::NewL(*this, CActive::EPriorityStandard, KDestroyObsoleteStateOneShot);
       
    99             }
       
   100         if (!iActive->IsActive())
       
   101             {
       
   102             iActive->iStatus = KRequestPending;
       
   103             iActive->GoActive();
       
   104             TRequestStatus* sta = &(iActive->iStatus);
       
   105             CompleteRequest(sta, KErrNone);
       
   106             }
       
   107         }
       
   108     TRACE_FUNC_EXIT
       
   109     }
       
   110 
       
   111 RSocketServ& CBtmMan::SockServ()
       
   112     {
       
   113     return iSockServ;
       
   114     }
       
   115 
       
   116 void CBtmMan::NewAccessory(const TBTDevAddr& aAddr, TProfiles aProfile)
       
   117     {
       
   118     Observer().NewAccessory(aAddr, aProfile);
       
   119     }
       
   120     
       
   121 void CBtmMan::AccessoryDisconnected(const TBTDevAddr& aAddr, TProfiles aProfile)    
       
   122     {
       
   123     Observer().AccessoryDisconnected(aAddr, aProfile);
       
   124     }
       
   125 
       
   126 void CBtmMan::RemoteAudioOpened(const TBTDevAddr& aAddr, TProfiles aProfile)   
       
   127     {
       
   128     Observer().RemoteAudioOpened(aAddr, aProfile);
       
   129     }
       
   130 
       
   131 void CBtmMan::RemoteAudioClosed(const TBTDevAddr& aAddr, TProfiles aProfile)
       
   132     {
       
   133     Observer().RemoteAudioClosed(aAddr, aProfile);
       
   134     }
       
   135 
       
   136 void CBtmMan::RegisterServiceL(TUint aService, TUint aChannel)
       
   137     {
       
   138  	TRACE_FUNC_ENTRY
       
   139     TInt count = iServices.Count();
       
   140     
       
   141     for (TInt i = count - 1; i >= 0; i--)
       
   142         {
       
   143         if (iServices[i].iService == aService)
       
   144             {
       
   145             if (iServices[i].iChannel == aChannel)
       
   146                 {
       
   147                 TRACE_INFO((_L("Profile 0x%04X already registered at Channel %d"), aService, aChannel))
       
   148                 return;
       
   149                 }
       
   150             else
       
   151                 {
       
   152                 iBteng->DeleteSdpRecord(iServices[i].iHandle); 
       
   153                 iServices.Remove(i);
       
   154                 break;
       
   155                 }
       
   156             }
       
   157         }
       
   158 
       
   159  	TSdpServRecordHandle handle;
       
   160  	TInt err = iBteng->RegisterSdpRecord(aService, aChannel, handle); 
       
   161     LEAVE_IF_ERROR(err);
       
   162     TBtmService record;
       
   163     record.iChannel = aChannel;
       
   164     record.iService = aService;
       
   165     record.iHandle = handle;
       
   166     iServices.AppendL(record);
       
   167     
       
   168     
       
   169     
       
   170 	TRACE_INFO((_L("Profile 0x%04X new registered at Channel %d"), aService, aChannel))
       
   171 	TRACE_FUNC_EXIT
       
   172     }
       
   173 
       
   174 TInt CBtmMan::GetLastUsedChannel(TUint aService)
       
   175     {
       
   176     TInt count = iServices.Count();
       
   177     for (TInt i = count - 1; i >= 0; i--)
       
   178            {
       
   179            if (iServices[i].iService == aService)
       
   180                {
       
   181                return iServices[i].iChannel;
       
   182                }
       
   183            }
       
   184     
       
   185     return KErrNotFound;
       
   186     }
       
   187 
       
   188 TInt CBtmMan::AudioLinkLatency()
       
   189     {
       
   190     return iState->AudioLinkLatency();
       
   191     }
       
   192 TBool CBtmMan::IsTrashBinEmpty()
       
   193     {
       
   194     return iTrashBin.Count() == 0 ? ETrue : EFalse;
       
   195     }
       
   196 
       
   197 void CBtmMan::DeleteRegisteredService(TUint aService)
       
   198     {
       
   199     TRACE_FUNC_ENTRY
       
   200     TInt count = iServices.Count();
       
   201     for (TInt i = count - 1; i >= 0; i--)
       
   202         {
       
   203         if (iServices[i].iService == aService)
       
   204             {
       
   205             iBteng->DeleteSdpRecord(iServices[i].iHandle);
       
   206             iServices.Remove(i);
       
   207             break;
       
   208             }
       
   209         }
       
   210     TRACE_FUNC_EXIT
       
   211     }
       
   212 
       
   213 void CBtmMan::DeleteAllRegisteredServices()
       
   214     {
       
   215     TInt count = iServices.Count();
       
   216     if (count > 0)
       
   217         {
       
   218         TRACE_FUNC_ENTRY
       
   219         for (TInt i = 0; i < count; i++)
       
   220             {
       
   221             iBteng->DeleteSdpRecord(iServices[i].iHandle);
       
   222             }        
       
   223         iServices.Reset();
       
   224         TRACE_FUNC_EXIT
       
   225         }
       
   226     }
       
   227 
       
   228 void CBtmMan::LoadCmdHandlerL(TBtmcProfileId aProfile, const TBTDevAddr& aAddr, TBool aAccessoryInitiated)
       
   229     {
       
   230     if (!iCmdHandler)
       
   231         {
       
   232         iCmdHandler = CBtmcHandlerApi::NewL(*this, aProfile, aAddr.Des(), aAccessoryInitiated);
       
   233         }
       
   234     }
       
   235     
       
   236 void CBtmMan::DeleteCmdHandler()
       
   237     {
       
   238     delete iCmdHandler;
       
   239     iCmdHandler = NULL;
       
   240     }
       
   241     
       
   242 void CBtmMan::NewProtocolDataL(const TDesC8& aData)
       
   243     {
       
   244     if(iCmdHandler)
       
   245         {
       
   246         iCmdHandler->HandleProtocolDataL(aData);
       
   247         }
       
   248     else
       
   249         {
       
   250         // Panic in udeb builds only, disconnect remote in urel
       
   251         __ASSERT_DEBUG(EFalse, PANIC(EBTPanicNullCmdHandler));
       
   252         LEAVE(KErrNotReady);
       
   253         }
       
   254     }
       
   255 
       
   256 TBool CBtmMan::IsAccInuse() const
       
   257     {
       
   258     return iAccInuse;
       
   259     }
       
   260 
       
   261 TBool CBtmMan::IsEdrSupported() const
       
   262     {
       
   263     return iEdr;
       
   264     }
       
   265     
       
   266 void CBtmMan::ConnectToAccessory(const TBTDevAddr& aAddr, TRequestStatus& aStatus)
       
   267     {
       
   268     TRACE_FUNC_ENTRY
       
   269     TRACE_INFO_SEG(
       
   270         {TBuf<KBTDevAddrReadable> buf; aAddr.GetReadable(buf); Trace(_L("BT Addr %S"), &buf);})
       
   271     TRAPD(err, iState->ConnectL(aAddr, aStatus));
       
   272     if (err)
       
   273         {
       
   274         CompleteRequest(&aStatus, err);
       
   275         }
       
   276     TRACE_FUNC_EXIT
       
   277     }
       
   278 
       
   279 void CBtmMan::CancelConnectToAccessory(const TBTDevAddr& aAddr)
       
   280     {
       
   281     TRACE_FUNC_ENTRY
       
   282     TRACE_INFO_SEG(
       
   283         {TBuf<KBTDevAddrReadable> buf; aAddr.GetReadable(buf); Trace(_L("BT Addr %S"), &buf);})
       
   284     TRAP_IGNORE(iState->CancelConnectL());
       
   285     (void) aAddr;
       
   286     TRACE_FUNC_EXIT
       
   287     }
       
   288 
       
   289 void CBtmMan::DisconnectAccessory(const TBTDevAddr& aAddr, TRequestStatus& aStatus)
       
   290     {
       
   291     TRACE_FUNC_ENTRY
       
   292     TRACE_INFO_SEG(
       
   293         {TBuf<KBTDevAddrReadable> buf; aAddr.GetReadable(buf); Trace(_L("BT Addr %S"), &buf);});      
       
   294     TRAPD(err, iState->DisconnectL(aAddr, aStatus));
       
   295     if (err)
       
   296         {
       
   297         CompleteRequest(&aStatus, err);
       
   298         }
       
   299     TRACE_FUNC_EXIT
       
   300     }
       
   301 
       
   302 void CBtmMan::OpenAudioLink(const TBTDevAddr& aAddr, TRequestStatus& aStatus)
       
   303     {
       
   304     TRACE_FUNC_ENTRY
       
   305     TRACE_INFO_SEG(
       
   306         {TBuf<KBTDevAddrReadable> buf; aAddr.GetReadable(buf); 
       
   307          Trace(_L("Request to open audio to BT Addr %S"), &buf);})
       
   308     TRAPD(err, iState->OpenAudioLinkL(aAddr, aStatus));
       
   309     if (err)
       
   310         {
       
   311         CompleteRequest(&aStatus, err);
       
   312         }
       
   313     TRACE_FUNC_EXIT    
       
   314     }
       
   315 
       
   316 void CBtmMan::CancelOpenAudioLink(const TBTDevAddr& aAddr)
       
   317     {
       
   318     TRAP_IGNORE(iState->CancelOpenAudioLinkL(aAddr));
       
   319     }
       
   320 
       
   321 void CBtmMan::CloseAudioLink(const TBTDevAddr& aAddr, TRequestStatus& aStatus)
       
   322     {
       
   323     TRACE_FUNC_ENTRY
       
   324     TRACE_INFO_SEG(
       
   325         {TBuf<KBTDevAddrReadable> buf; aAddr.GetReadable(buf); Trace(_L("BT Addr %S"), &buf);})        
       
   326     TRAPD(err, iState->CloseAudioLinkL(aAddr, aStatus));
       
   327     if (err)
       
   328         {
       
   329         CompleteRequest(&aStatus, err);
       
   330         }
       
   331     TRACE_FUNC_EXIT
       
   332     }
       
   333 
       
   334 void CBtmMan::CancelCloseAudioLink(const TBTDevAddr& aAddr)
       
   335     {
       
   336     TRACE_FUNC_ENTRY
       
   337     TRACE_INFO_SEG(
       
   338         {TBuf<KBTDevAddrReadable> buf; aAddr.GetReadable(buf); Trace(_L("BT Addr %S"), &buf);})        
       
   339     TRAP_IGNORE(iState->CancelCloseAudioLinkL(aAddr));
       
   340     TRACE_FUNC_EXIT
       
   341     }
       
   342     
       
   343 void CBtmMan::AccInUse()
       
   344     {
       
   345     TRACE_FUNC_ENTRY
       
   346     iAccInuse = ETrue;
       
   347     iState->AccInUse();
       
   348     TRACE_FUNC_EXIT
       
   349     }
       
   350 
       
   351 void CBtmMan::AccOutOfUse()
       
   352     {
       
   353     TRACE_FUNC
       
   354     iAccInuse = EFalse;
       
   355     }
       
   356 
       
   357 TProfiles CBtmMan::PluginType()
       
   358     {
       
   359     return EAnyMonoAudioProfiles;
       
   360     }
       
   361     
       
   362 
       
   363 void CBtmMan::ActivateRemoteVolumeControl()
       
   364     {
       
   365     TRACE_FUNC
       
   366     if(iCmdHandler)
       
   367         iCmdHandler->ActivateRemoteVolumeControl();
       
   368     else
       
   369         {
       
   370         TRACE_INFO((_L("Null cmd handler")))
       
   371         }
       
   372     }
       
   373 
       
   374 void CBtmMan::DeActivateRemoteVolumeControl()
       
   375     {
       
   376     if (iCmdHandler)
       
   377         iCmdHandler->DeActivateRemoteVolumeControl();
       
   378     }
       
   379 
       
   380 TInt CBtmMan::GetRemoteSupportedFeature()
       
   381     {
       
   382     if (iCmdHandler)
       
   383         return iCmdHandler->GetRemoteSupportedFeature();
       
   384     return 0;
       
   385     }
       
   386     
       
   387 CBtmMan::CBtmMan(TPluginParams& aParams)
       
   388     : CBTAccPlugin(aParams)
       
   389     {
       
   390     TRACE_FUNC
       
   391     }
       
   392 
       
   393 void CBtmMan::RequestCompletedL(CBtmActive& /*aActive*/)
       
   394     {
       
   395     TRACE_FUNC
       
   396     TRACE_INFO((_L("TRASHBIN SIZE: %d"), iTrashBin.Count()))
       
   397     iTrashBin.ResetAndDestroy();
       
   398     if( iState )
       
   399         {
       
   400         iState->StartListenerL();
       
   401         }
       
   402         
       
   403     }
       
   404 
       
   405 void CBtmMan::CancelRequest(CBtmActive& /*aActive*/)
       
   406     {
       
   407     }
       
   408 
       
   409 void CBtmMan::SlcIndicateL(TBool aSlc)
       
   410     {
       
   411     if (iState)
       
   412         iState->SlcIndicateL(aSlc);
       
   413     }
       
   414 
       
   415 void CBtmMan::SendProtocolDataL(const TDesC8& aData)
       
   416     {
       
   417     if (iState)
       
   418         iState->SendProtocolDataL(aData);
       
   419     }
       
   420 
       
   421 void CBtmMan::ConstructL()
       
   422     {
       
   423     TRACE_FUNC_ENTRY
       
   424     LEAVE_IF_ERROR(iSockServ.Connect());
       
   425     TInt edr = EBTEScoNotSupported;
       
   426     CRepository* cenrep = NULL;
       
   427     TRAP_IGNORE(cenrep = CRepository::NewL(KCRUidBluetoothEngine));
       
   428     if (cenrep)
       
   429         {
       
   430         cenrep->Get(KBTEScoSupportedLV, edr);
       
   431         delete cenrep;
       
   432         }
       
   433     iEdr = (edr == EBTEScoSupported) ? ETrue : EFalse;
       
   434     TRACE_INFO((_L("EDR feature %d"), iEdr))
       
   435     iBteng = CBTEngDiscovery::NewL();
       
   436     CBtmState* state = CBtmsListen::NewL(*this);
       
   437     CleanupStack::PushL(state);
       
   438     ChangeStateL(state);
       
   439     CleanupStack::Pop(state);
       
   440     TRACE_FUNC_EXIT
       
   441     }
       
   442