bluetoothengine/headsetsimulator/core/src/hscore.cpp
branchheadsetsimulator
changeset 60 90dbfc0435e3
equal deleted inserted replaced
59:02103bf20ee5 60:90dbfc0435e3
       
     1 /*
       
     2  * Component Name: Headset Simulator
       
     3  * Author: Comarch S.A.
       
     4  * Version: 1.0
       
     5  * Copyright (c) 2010 Comarch S.A.
       
     6  *  
       
     7  * This Software is submitted by Comarch S.A. to Symbian Foundation Limited on 
       
     8  * the basis of the Member Contribution Agreement entered between Comarch S.A. 
       
     9  * and Symbian Foundation Limited on 5th June 2009 (“Agreement”) and may be 
       
    10  * used only in accordance with the terms and conditions of the Agreement. 
       
    11  * Any other usage, duplication or redistribution of this Software is not 
       
    12  * allowed without written permission of Comarch S.A.
       
    13  * 
       
    14  */
       
    15 
       
    16 #include "hscore.h"
       
    17 #include "hsprofilemanager.h"
       
    18 #include "hsobserver.h"
       
    19 #include "hsserver.h"
       
    20 #include "hsaudioserver.h"
       
    21 #include "hsclientmanager.h"
       
    22 #include "hsclient.h"
       
    23 #include "hstools.h"
       
    24 #include "hsremotecontrolserver.h"
       
    25 #include "debug.h"
       
    26 
       
    27 EXPORT_C CHsCore* CHsCore::NewL( const TUUID& aService )
       
    28     {
       
    29     CHsCore* self = CHsCore::NewLC( aService );
       
    30     CleanupStack::Pop( self );
       
    31     return self;
       
    32     }
       
    33 
       
    34 EXPORT_C CHsCore* CHsCore::NewLC( const TUUID& aService )
       
    35     {
       
    36     CHsCore* self = new ( ELeave ) CHsCore( aService );
       
    37     CleanupStack::PushL( self );
       
    38     self->ConstructL();
       
    39     return self;
       
    40     }
       
    41 
       
    42 EXPORT_C CHsCore::~CHsCore()
       
    43     {
       
    44     TRACE_FUNC_ENTRY
       
    45 
       
    46     DestroyComponents();
       
    47     iSocketServ.Close();
       
    48 
       
    49     TRACE_FUNC_EXIT
       
    50     }
       
    51 
       
    52 CHsCore::CHsCore( const TUUID& aService ) :
       
    53     iService( aService )
       
    54     {
       
    55 
       
    56     }
       
    57 
       
    58 void CHsCore::ConstructL()
       
    59     {
       
    60     TRACE_FUNC_ENTRY
       
    61 
       
    62     User::LeaveIfError( iSocketServ.Connect() );
       
    63     CreateComponentsL();
       
    64     RegisterProperty();
       
    65     InitializeSimulationL();
       
    66     TRACE_FUNC_EXIT
       
    67     }
       
    68 
       
    69 EXPORT_C void CHsCore::StartSimulationL( const TUid& aCoDUid,
       
    70         const TUid& aSdpUid, const TUid& aProfileUid, const TDesC& aBTName )
       
    71     {
       
    72     TRACE_FUNC_ENTRY
       
    73 
       
    74     InitializeSimulationL( aBTName );
       
    75 
       
    76     LoadSdpL( aSdpUid );
       
    77     LoadCodL( aCoDUid );
       
    78     LoadProfileL( aProfileUid );
       
    79 
       
    80     User::LeaveIfError( StartHSServer() );
       
    81     User::LeaveIfError( StartAudioServer( ETrue ) );
       
    82 
       
    83     TRACE_FUNC_EXIT
       
    84     }
       
    85 
       
    86 EXPORT_C void CHsCore::StartSimulationL( const TDesC8& aCoDName,
       
    87         const TDesC8& aSdpName, const TDesC8& aProfileName,
       
    88         const TDesC& aBTName )
       
    89     {
       
    90     TRACE_FUNC_ENTRY
       
    91 
       
    92     InitializeSimulationL( aBTName );
       
    93 
       
    94     LoadSdpL( aSdpName );
       
    95     LoadCodL( aCoDName );
       
    96     LoadProfileL( aProfileName );
       
    97 
       
    98     User::LeaveIfError( StartHSServer() );
       
    99     User::LeaveIfError( StartAudioServer( ETrue ) );
       
   100 
       
   101     TRACE_FUNC_EXIT
       
   102     }
       
   103 
       
   104 EXPORT_C void CHsCore::StopSimulation()
       
   105     {
       
   106     TRACE_FUNC_ENTRY
       
   107     DestroyPluginManager();
       
   108     DestroyClientManager();
       
   109     DestroyHSServer();
       
   110     DestroyAudioServer();
       
   111 
       
   112     CreateHSServer();
       
   113     CreateAudioServer( ETrue );
       
   114     CreateClientManager();
       
   115     CreatePluginManager();
       
   116 
       
   117     TRAP_IGNORE(InitializeSimulationL());
       
   118     TRACE_FUNC_EXIT
       
   119     }
       
   120 
       
   121 EXPORT_C void CHsCore::ConnectWithName( const TDesC &aName )
       
   122     {
       
   123     TRACE_FUNC_ENTRY
       
   124     if ( iObserver )
       
   125         {
       
   126         iObserver->HandleConnectingToClient();
       
   127         }
       
   128     if ( iHsBTManager )
       
   129         {
       
   130         TRAPD(err, iHsBTManager->FindNameL(aName));
       
   131         if ( err != KErrNone && iObserver )
       
   132             {
       
   133             iObserver->HandleUnableToConnectClient();
       
   134             }
       
   135         TRACE_INFO((_L("Connect with name: %S, error = %d"), &aName, err))
       
   136         }
       
   137     else if ( iObserver )
       
   138         {
       
   139         iObserver->HandleUnableToConnectClient();
       
   140         }
       
   141     TRACE_FUNC_EXIT
       
   142     }
       
   143 
       
   144 EXPORT_C void CHsCore::ConnectWithDevAddress( const TDesC& aDevAddress )
       
   145     {
       
   146     TRACE_FUNC_ENTRY
       
   147     if ( iObserver )
       
   148         {
       
   149         iObserver->HandleConnectingToClient();
       
   150         }
       
   151     if ( iHsBTManager )
       
   152         {
       
   153         TRAPD(err, iHsBTManager->FindDevL(aDevAddress));
       
   154         if ( err != KErrNone && iObserver )
       
   155             {
       
   156             iObserver->HandleUnableToConnectClient();
       
   157             }
       
   158         }
       
   159     else if ( iObserver )
       
   160         {
       
   161         iObserver->HandleUnableToConnectClient();
       
   162         }
       
   163     TRACE_FUNC_EXIT
       
   164     }
       
   165 
       
   166 EXPORT_C void CHsCore::ConnectWithLastConnectedL()
       
   167     {
       
   168     TRACE_FUNC_ENTRY
       
   169 
       
   170     TBTDevAddr addr;
       
   171     User::LeaveIfError( GetLastConnected( addr ) );
       
   172 
       
   173     RBuf lastDevAddrBuf;
       
   174 
       
   175     lastDevAddrBuf.CreateL( KMaxBluetoothNameLen );
       
   176     addr.GetReadable( lastDevAddrBuf );
       
   177 
       
   178     ConnectWithDevAddress( lastDevAddrBuf );
       
   179 
       
   180     TRACE_FUNC_EXIT
       
   181     }
       
   182 
       
   183 EXPORT_C void CHsCore::CancelConnecting()
       
   184     {
       
   185     TRACE_FUNC_ENTRY
       
   186     if ( iHsBTManager )
       
   187         {
       
   188         iHsBTManager->CancelConnecting();
       
   189         }
       
   190     TRACE_FUNC_EXIT
       
   191     }
       
   192 
       
   193 EXPORT_C void CHsCore::DisconnectClients()
       
   194     {
       
   195     TRACE_FUNC_ENTRY
       
   196     if ( iHSClientManager )
       
   197         {
       
   198         iHSClientManager->DisconnectClients();
       
   199         }
       
   200     TRACE_FUNC_EXIT
       
   201     }
       
   202 
       
   203 EXPORT_C TInt CHsCore::Send( const TDesC8 &aData, TInt aNumber )
       
   204     {
       
   205     TRACE_FUNC_ENTRY
       
   206     TInt ret = KErrNone;
       
   207     if ( iHSClientManager && iHSProfileManager )
       
   208         {
       
   209         TBuf8 <KHsClientBufferLength> tmp;
       
   210 
       
   211         TRAP(ret, iHSProfileManager->HandleCommandL(aData, tmp, EFalse));
       
   212         if ( KErrNone == ret )
       
   213             {
       
   214             ret = iHSClientManager->Send( tmp, aNumber );
       
   215             }
       
   216         }
       
   217     else
       
   218         {
       
   219         ret = KErrNotReady;
       
   220         }
       
   221     TRACE_FUNC_EXIT
       
   222     return ret;
       
   223     }
       
   224 
       
   225 EXPORT_C void CHsCore::SetObserver( MHsObserver& aObserver )
       
   226     {
       
   227     TRACE_FUNC_ENTRY
       
   228     iObserver = &aObserver;
       
   229     TRACE_FUNC_EXIT
       
   230     }
       
   231 
       
   232 EXPORT_C TInt CHsCore::AcceptIncomingCall()
       
   233     {
       
   234     TRACE_FUNC_ENTRY
       
   235 
       
   236     TBuf8 <KHsClientBufferLength> tmp;
       
   237     TRAPD(ret,iHSProfileManager->AcceptIncomingCallL(tmp));
       
   238 
       
   239     if ( KErrNone == ret )
       
   240         {
       
   241         ret = iHSClientManager->Send( tmp );
       
   242         }
       
   243 
       
   244     TRACE_FUNC_EXIT
       
   245     return ret;
       
   246     }
       
   247 
       
   248 EXPORT_C TInt CHsCore::ReleaseOngoingCall()
       
   249     {
       
   250     TBuf8 <KHsClientBufferLength> tmp;
       
   251     TRAPD(ret,iHSProfileManager->ReleaseOngoingCallL(tmp));
       
   252 
       
   253     if ( KErrNone == ret )
       
   254         {
       
   255         ret = iHSClientManager->Send( tmp );
       
   256         }
       
   257 
       
   258     TRACE_FUNC_EXIT
       
   259     return ret;
       
   260     }
       
   261 
       
   262 void CHsCore::InitializeSimulationL( const TDesC& aBTName )
       
   263     {
       
   264     TRACE_FUNC_ENTRY
       
   265 
       
   266     User::LeaveIfNull( iHsBTManager );
       
   267     if ( aBTName != KNullDesC )
       
   268         {
       
   269         SetBTName( aBTName );
       
   270         iHsBTManager->TurnBtOnL( aBTName );
       
   271         }
       
   272     else
       
   273         {
       
   274         iHsBTManager->TurnBtOnL();
       
   275         }
       
   276 
       
   277     TRACE_FUNC_EXIT
       
   278     }
       
   279 
       
   280 void CHsCore::RegisterProperty()
       
   281     {
       
   282     TRACE_FUNC_ENTRY
       
   283     // Return value may be ignored, no influence on main Headset functionality
       
   284     RProperty::Define( KHSProperty, ELastDevice, RProperty::EByteArray,
       
   285             KLastDevAddrLength );
       
   286 
       
   287     TRACE_FUNC_EXIT
       
   288     }
       
   289 
       
   290 void CHsCore::CreateComponentsL()
       
   291     {
       
   292     TRACE_FUNC_ENTRY
       
   293     User::LeaveIfError( CreateControlServer() );
       
   294     User::LeaveIfError( CreateHSServer() );
       
   295     User::LeaveIfError( CreateAudioServer( ETrue ) );
       
   296     User::LeaveIfError( CreateClientManager() );
       
   297     User::LeaveIfError( CreatePluginManager() );
       
   298     User::LeaveIfError( CreateBTManager() );
       
   299 
       
   300     TRACE_FUNC_EXIT
       
   301     }
       
   302 
       
   303 void CHsCore::DestroyComponents()
       
   304     {
       
   305     TRACE_FUNC_ENTRY
       
   306 
       
   307     DestroyControlServer();
       
   308     DestroyPluginManager();
       
   309     DestroyClientManager();
       
   310     DestroyHSServer();
       
   311     DestroyAudioServer();
       
   312     DestroyBTManager();
       
   313 
       
   314     TRACE_FUNC_EXIT
       
   315     }
       
   316 
       
   317 TInt CHsCore::CreateHSServer()
       
   318     {
       
   319     TRACE_FUNC_ENTRY
       
   320     TInt ret = KErrNone;
       
   321 
       
   322     if ( !iHSServer )
       
   323         {
       
   324         TRAP(ret, iHSServer = CHsServer::NewL(iSocketServ));
       
   325 
       
   326         if ( ret == KErrNone )
       
   327             {
       
   328             iHSServer->SetNewClientObserver( *this );
       
   329             }
       
   330         }
       
   331     else
       
   332         {
       
   333         ret = KErrAlreadyExists;
       
   334         }
       
   335 
       
   336     TRACE_FUNC_EXIT
       
   337     return ret;
       
   338     }
       
   339 
       
   340 TInt CHsCore::StartHSServer()
       
   341     {
       
   342     TRACE_FUNC_ENTRY
       
   343 
       
   344     TInt ret = KErrNone;
       
   345     if ( !iHSServer )
       
   346         {
       
   347         ret = KErrNotReady;
       
   348         }
       
   349     else
       
   350         {
       
   351         if ( !iHSServer->IsReady() )
       
   352             {
       
   353             ret = iHSServer->Listen();
       
   354             }
       
   355         else
       
   356             {
       
   357             ret = KErrInUse;
       
   358             }
       
   359         }
       
   360 
       
   361     if ( ret == KErrNone && iHSProfileManager )
       
   362         {
       
   363         iHSProfileManager->SetSdpServicePort( iHSServer->GetPort() );
       
   364         iHSProfileManager->RefreshSdp();
       
   365         }
       
   366 
       
   367     TRACE_FUNC_EXIT
       
   368     return ret;
       
   369     }
       
   370 
       
   371 void CHsCore::StopHSServer()
       
   372     {
       
   373     TRACE_FUNC_ENTRY
       
   374     if ( ( iHSServer ) && ( iHSServer->IsReady() ) )
       
   375         {
       
   376         iHSServer->Shutdown();
       
   377         }
       
   378 
       
   379     TRACE_FUNC_EXIT
       
   380     }
       
   381 
       
   382 void CHsCore::DestroyHSServer()
       
   383     {
       
   384     TRACE_FUNC_ENTRY
       
   385 
       
   386     if ( iHSServer )
       
   387         {
       
   388         delete iHSServer;
       
   389         iHSServer = NULL;
       
   390         }
       
   391     TRACE_FUNC_EXIT
       
   392     }
       
   393 
       
   394 TInt CHsCore::CreateAudioServer( TBool aPullAudio )
       
   395     {
       
   396     TRACE_FUNC_ENTRY
       
   397 
       
   398     TInt ret = KErrNone;
       
   399 
       
   400     if ( iAudioServer )
       
   401         {
       
   402         ret = KErrAlreadyExists;
       
   403         }
       
   404     else
       
   405         {
       
   406         TRAP(ret, iAudioServer = CHsAudioServer::NewL(iSocketServ, aPullAudio));
       
   407         }
       
   408 
       
   409     TRACE_FUNC_EXIT
       
   410 
       
   411     return ret;
       
   412     }
       
   413 
       
   414 TInt CHsCore::StartAudioServer( TBool aPullAudio )
       
   415     {
       
   416     TRACE_FUNC_ENTRY
       
   417     TInt ret = KErrNone;
       
   418 
       
   419     if ( iAudioServer && !iAudioServer->IsListen()
       
   420             && !iAudioServer->IsConnected() )
       
   421         {
       
   422         iAudioServer->SetPullAudio( aPullAudio );
       
   423         TRAP(ret, iAudioServer->ListenL());
       
   424         }
       
   425     else
       
   426         {
       
   427         ret = KErrNotReady;
       
   428         }
       
   429 
       
   430     TRACE_FUNC_EXIT
       
   431 
       
   432     return ret;
       
   433 
       
   434     }
       
   435 
       
   436 void CHsCore::StopAudioServer()
       
   437     {
       
   438     TRACE_FUNC_ENTRY
       
   439     if ( iAudioServer )
       
   440         {
       
   441         if ( iAudioServer->IsConnected() )
       
   442             {
       
   443             iAudioServer->Disconnect();
       
   444             }
       
   445         if ( iAudioServer->IsListen() )
       
   446             {
       
   447             iAudioServer->Shutdown();
       
   448             }
       
   449         }
       
   450     TRACE_FUNC_EXIT
       
   451     }
       
   452 
       
   453 void CHsCore::DestroyAudioServer()
       
   454     {
       
   455     TRACE_FUNC_ENTRY
       
   456 
       
   457     if ( iAudioServer )
       
   458         {
       
   459         delete iAudioServer;
       
   460         iAudioServer = NULL;
       
   461         }
       
   462     TRACE_FUNC_EXIT
       
   463     }
       
   464 
       
   465 TInt CHsCore::CreateClientManager()
       
   466     {
       
   467     TRACE_FUNC_ENTRY
       
   468     TInt err = KErrNone;
       
   469 
       
   470     if ( iHSClientManager )
       
   471         {
       
   472         err = KErrAlreadyExists;
       
   473         }
       
   474     else
       
   475         {
       
   476         TRAP(err, iHSClientManager = CHsClientManager::NewL());
       
   477         }
       
   478     TRACE_FUNC_EXIT
       
   479     return err;
       
   480     }
       
   481 
       
   482 void CHsCore::DestroyClientManager()
       
   483     {
       
   484     TRACE_FUNC_ENTRY
       
   485     if ( iHSClientManager )
       
   486         {
       
   487         delete iHSClientManager;
       
   488         iHSClientManager = NULL;
       
   489         }
       
   490     TRACE_FUNC_EXIT
       
   491     }
       
   492 
       
   493 TInt CHsCore::CreatePluginManager()
       
   494     {
       
   495     TRACE_FUNC_ENTRY
       
   496     TInt err = KErrNone;
       
   497     if ( iHSProfileManager )
       
   498         {
       
   499         err = KErrAlreadyExists;
       
   500         }
       
   501     else
       
   502         {
       
   503         TRAP(err, iHSProfileManager = CHsProfileManager::NewL());
       
   504         }
       
   505     TRACE_FUNC_EXIT
       
   506     return err;
       
   507     }
       
   508 
       
   509 void CHsCore::DestroyPluginManager()
       
   510     {
       
   511     TRACE_FUNC_ENTRY
       
   512 
       
   513     if ( iHSProfileManager )
       
   514         {
       
   515         delete iHSProfileManager;
       
   516         iHSProfileManager = NULL;
       
   517         }
       
   518 
       
   519     TRACE_FUNC_EXIT
       
   520     }
       
   521 
       
   522 TInt CHsCore::CreateBTManager()
       
   523     {
       
   524     TRACE_FUNC_ENTRY
       
   525     TInt err = KErrNone;
       
   526 
       
   527     if ( !iHsBTManager )
       
   528         {
       
   529         TRAP(err, iHsBTManager = CHsBTManager::NewL(iSocketServ, this));
       
   530         if ( err == KErrNone )
       
   531             {
       
   532             iHsBTManager->SetService( iService );
       
   533             }
       
   534         }
       
   535     else
       
   536         {
       
   537         err = KErrAlreadyExists;
       
   538         }
       
   539 
       
   540     TRACE_FUNC_EXIT
       
   541     return err;
       
   542     }
       
   543 
       
   544 void CHsCore::DestroyBTManager()
       
   545     {
       
   546     TRACE_FUNC_ENTRY
       
   547     if ( iHsBTManager )
       
   548         {
       
   549         delete iHsBTManager;
       
   550         iHsBTManager = NULL;
       
   551         }
       
   552     TRACE_FUNC_EXIT
       
   553     }
       
   554 
       
   555 TInt CHsCore::CreateControlServer()
       
   556     {
       
   557     TRACE_FUNC_ENTRY
       
   558     TInt err = KErrNone;
       
   559     if ( !iHSControlServer )
       
   560         {
       
   561         TRAP(err, iHSControlServer = CHsControlServer::NewL(iSocketServ, this);
       
   562                 iHSControlServer->SetConnectionObserver( *this );
       
   563                 iHSControlServer->StartRemoteControlServerL());
       
   564         }
       
   565     else
       
   566         {
       
   567         err = KErrAlreadyExists;
       
   568         }
       
   569     TRACE_FUNC_EXIT
       
   570     return err;
       
   571     }
       
   572 
       
   573 void CHsCore::DestroyControlServer()
       
   574     {
       
   575     TRACE_FUNC_ENTRY
       
   576     if ( iHSControlServer )
       
   577         {
       
   578         delete iHSControlServer;
       
   579         iHSControlServer = NULL;
       
   580         }
       
   581     TRACE_FUNC_EXIT
       
   582 
       
   583     }
       
   584 
       
   585 void CHsCore::SetBTName( const TBTFriendlyName &aBTName )
       
   586     {
       
   587     iBTFriendlyName = aBTName;
       
   588     }
       
   589 
       
   590 TInt CHsCore::GetLastConnected( TBTDevAddr &aBTDevAddr )
       
   591     {
       
   592     TRACE_FUNC_ENTRY
       
   593     RBuf bufAddr;
       
   594     TInt error = bufAddr.Create( KLastDevAddrLength );
       
   595     if ( KErrNone != error )
       
   596         {
       
   597 
       
   598         return error;
       
   599         }
       
   600 
       
   601     error = RProperty::Get( KHSProperty, ELastDevice, bufAddr );
       
   602     TRACE_INFO( (_L("Last-connected dev addr: %S"), &bufAddr) )
       
   603     if ( error == KErrNone )
       
   604         {
       
   605         error = aBTDevAddr.SetReadable( bufAddr );
       
   606 
       
   607         }
       
   608     bufAddr.Close();
       
   609     TRACE_FUNC_EXIT
       
   610     return error;
       
   611     }
       
   612 
       
   613 TInt CHsCore::SaveClient( CBluetoothSocket* aSocket )
       
   614     {
       
   615     TRACE_FUNC_ENTRY
       
   616     TInt error = KErrNone;
       
   617     TBTSockAddr sockAddr;
       
   618     aSocket->RemoteName( sockAddr );
       
   619 
       
   620     TBTDevAddr devAddr = sockAddr.BTAddr();
       
   621 
       
   622     RBuf bufAddr;
       
   623 
       
   624     error = bufAddr.Create( KLastDevAddrLength );
       
   625     if ( KErrNone == error )
       
   626         {
       
   627         devAddr.GetReadable( bufAddr );
       
   628         TRACE_INFO( (_L("Read address:: %S"), &bufAddr) );
       
   629         error = RProperty::Set( KHSProperty, ELastDevice, bufAddr );
       
   630         bufAddr.Close();
       
   631         }
       
   632 
       
   633     TRACE_FUNC_EXIT
       
   634     return error;
       
   635     }
       
   636 
       
   637 void CHsCore::SetSdpRecordServicePortFromServer()
       
   638     {
       
   639     TRACE_FUNC_ENTRY
       
   640     if ( iHSServer && iHSProfileManager )
       
   641         {
       
   642         iHSProfileManager->SetSdpServicePort( iHSServer->GetPort() );
       
   643         }
       
   644     TRACE_FUNC_EXIT
       
   645     }
       
   646 
       
   647 void CHsCore::LoadProfileL( const TDesC8& aProfileName )
       
   648     {
       
   649     TRACE_FUNC_ENTRY
       
   650 
       
   651     User::LeaveIfNull( iHSProfileManager );
       
   652     iHSProfileManager->LookupAndManageProfileL( aProfileName );
       
   653 
       
   654     TRACE_FUNC_EXIT
       
   655     }
       
   656 
       
   657 void CHsCore::LoadProfileL( const TUid& aProfileUid )
       
   658     {
       
   659     TRACE_FUNC_ENTRY
       
   660 
       
   661     User::LeaveIfNull( iHSProfileManager );
       
   662     iHSProfileManager->LookupAndManageProfileL( aProfileUid );
       
   663 
       
   664     TRACE_FUNC_EXIT
       
   665     }
       
   666 
       
   667 void CHsCore::LoadSdpL( const TDesC8& aSdpName )
       
   668     {
       
   669     TRACE_FUNC_ENTRY
       
   670 
       
   671     User::LeaveIfNull( iHSProfileManager );
       
   672 
       
   673     SetSdpRecordServicePortFromServer();
       
   674     iHSProfileManager->LookupAndManageSdpL( aSdpName );
       
   675     TRACE_INFO(_L("iHSProfileManager lookup and manage SDP success") );
       
   676 
       
   677     iHSProfileManager->LoadSdpL();
       
   678 
       
   679     TRACE_FUNC_EXIT
       
   680     }
       
   681 
       
   682 void CHsCore::LoadSdpL( const TUid& aSdpUid )
       
   683     {
       
   684     TRACE_FUNC_ENTRY
       
   685     User::LeaveIfNull( iHSProfileManager );
       
   686 
       
   687     SetSdpRecordServicePortFromServer();
       
   688     iHSProfileManager->LookupAndManageSdpL( aSdpUid );
       
   689     TRACE_INFO(_L("iHSProfileManager lookup and manage SDP success") );
       
   690 
       
   691     iHSProfileManager->LoadSdpL();
       
   692 
       
   693     TRACE_FUNC_EXIT
       
   694     }
       
   695 
       
   696 void CHsCore::LoadCodL( const TDesC8& aCoDName )
       
   697     {
       
   698     TRACE_FUNC_ENTRY
       
   699 
       
   700     User::LeaveIfNull( iHSProfileManager );
       
   701 
       
   702     iHSProfileManager->LookupAndManageCodL( aCoDName );
       
   703     iHSProfileManager->LoadCodL();
       
   704 
       
   705     TRACE_FUNC_EXIT
       
   706     }
       
   707 
       
   708 void CHsCore::LoadCodL( const TUid& aCoDUid )
       
   709     {
       
   710     TRACE_FUNC_ENTRY
       
   711 
       
   712     User::LeaveIfNull( iHSProfileManager );
       
   713 
       
   714     iHSProfileManager->LookupAndManageCodL( aCoDUid );
       
   715     iHSProfileManager->LoadCodL();
       
   716 
       
   717     TRACE_FUNC_EXIT
       
   718     }
       
   719 
       
   720 void CHsCore::HandleNewClientL( CBluetoothSocket* aClientSocket, TInt aErr )
       
   721     {
       
   722     TRACE_FUNC_ENTRY
       
   723 
       
   724     if ( ( KErrNone == aErr ) && ( iHSClientManager ) )
       
   725         {
       
   726         CHsClient *clientTmp = NULL;
       
   727         if ( aClientSocket )
       
   728             {
       
   729             clientTmp = CHsClient::NewL( aClientSocket, this, iHSClientManager );
       
   730 
       
   731             //Return value may be ignored
       
   732             SaveClient( aClientSocket );
       
   733             iHSClientManager->HandleNewClientL( clientTmp, aErr );
       
   734 
       
   735             TBuf8 <KHsClientBufferLength> tmp;
       
   736 
       
   737             iHSProfileManager->HandleClientConnected( tmp );
       
   738             if ( tmp.Compare( KNullDesC8 ) != 0 )
       
   739                 {
       
   740                 iHSClientManager->Send( tmp, KHsDefaultClientNumber );
       
   741                 }
       
   742 
       
   743             if ( iObserver )
       
   744                 {
       
   745                 iObserver->HandleConnectedToClient();
       
   746                 }
       
   747             }
       
   748         }
       
   749     else
       
   750         {
       
   751         if ( iHsBTManager )
       
   752             {
       
   753             iHsBTManager->CancelConnecting();
       
   754             }
       
   755         if ( iObserver )
       
   756             {
       
   757             iObserver->HandleUnableToConnectClient();
       
   758             }
       
   759         }
       
   760 
       
   761     TRACE_FUNC_EXIT
       
   762     }
       
   763 
       
   764 void CHsCore::HandleClientConnectL( TInt aErr )
       
   765     {
       
   766     TRACE_FUNC_ENTRY
       
   767     TRACE_INFO( (_L("aErr = %d"), aErr))
       
   768 
       
   769     TRACE_FUNC_EXIT
       
   770     }
       
   771 
       
   772 void CHsCore::HandleClientDisconnectL( TInt aErr )
       
   773     {
       
   774     TRACE_FUNC_ENTRY
       
   775     TRACE_INFO( (_L("aErr = %d"), aErr))
       
   776 
       
   777     if ( iObserver )
       
   778         {
       
   779         iObserver->HandleDisconnectedFromClient();
       
   780         }
       
   781     if ( iAudioServer )
       
   782         {
       
   783         iAudioServer->ListenL();
       
   784         }
       
   785     if ( iHSProfileManager )
       
   786         {
       
   787         iHSProfileManager->HandleClientDisconnected( aErr );
       
   788         }
       
   789 
       
   790     TRACE_FUNC_EXIT
       
   791     }
       
   792 
       
   793 void CHsCore::HandleClientReceiveCompleteL( const TDesC8 &aData, TInt aErr )
       
   794     {
       
   795     TRACE_FUNC_ENTRY
       
   796     TRACE_INFO( (_L("aErr = %d"), aErr))
       
   797 
       
   798     if ( iHSProfileManager )
       
   799         {
       
   800         iBufferFromProfileToClient.Zero();
       
   801 
       
   802         TRACE_INFO( (_L8(" Received data from AG client [%S]") , &aData) )
       
   803         iHSProfileManager->HandleCommandL( aData, iBufferFromProfileToClient,
       
   804                 ETrue );
       
   805 
       
   806         if ( iHSClientManager )
       
   807             {
       
   808             iHSClientManager->Send( iBufferFromProfileToClient );
       
   809             }
       
   810         }
       
   811     TRACE_FUNC_EXIT
       
   812     }
       
   813 void CHsCore::HandleClientSendCompleteL( TInt aErr )
       
   814     {
       
   815     TRACE_FUNC_ENTRY
       
   816     TRACE_INFO( (_L("aErr = %d"), aErr))
       
   817 
       
   818     TRACE_FUNC_EXIT
       
   819     }
       
   820 
       
   821 void CHsCore::HandleClientOtherProblemL( TInt aErr )
       
   822     {
       
   823     TRACE_FUNC_ENTRY
       
   824     TRACE_INFO( (_L("aErr = %d"), aErr))
       
   825 
       
   826     TRACE_FUNC_EXIT
       
   827     }
       
   828 
       
   829 void CHsCore::HandleDesiredDeviceFound( const TDesC& /*aDevName*/,
       
   830         const TBTDevAddr& aDevAddr, const TInt aPort )
       
   831     {
       
   832     TRACE_FUNC_ENTRY
       
   833     TBTSockAddr sockaddr;
       
   834     sockaddr.SetBTAddr( aDevAddr );
       
   835     sockaddr.SetPort( aPort );
       
   836     TRAPD(err, iHSServer->GetSocketAndTryToConnectL(sockaddr));
       
   837     if ( err != KErrNone )
       
   838         {
       
   839         TRACE_INFO( (_L("CHsServer::GetSocketAndTryToConnectL() error = %d"), err) )
       
   840         TRAP_IGNORE( HandleNewClientL(NULL, err));
       
   841 
       
   842         }
       
   843 
       
   844     TRACE_FUNC_EXIT
       
   845     }
       
   846 
       
   847 void CHsCore::HandleSdpQueryError( TInt aErr )
       
   848     {
       
   849     TRACE_FUNC_ENTRY
       
   850     TRACE_INFO( (_L("aErr = %d"), aErr))
       
   851     if ( iObserver )
       
   852         {
       
   853         iObserver->HandleUnableToConnectClient();
       
   854         }
       
   855     TRACE_FUNC_EXIT
       
   856     }
       
   857 
       
   858 void CHsCore::HandleServiceNotFound( TInt aErr )
       
   859     {
       
   860     TRACE_FUNC_ENTRY
       
   861     TRACE_INFO( (_L("aErr = %d"), aErr))
       
   862     if ( iObserver )
       
   863         {
       
   864         iObserver->HandleUnableToConnectClient();
       
   865         }
       
   866     TRACE_FUNC_EXIT
       
   867     }
       
   868 
       
   869 void CHsCore::HandlePortNotFound( TInt aErr )
       
   870     {
       
   871     TRACE_FUNC_ENTRY
       
   872     TRACE_INFO( (_L("aErr = %d"), aErr))
       
   873     if ( iObserver )
       
   874         {
       
   875         iObserver->HandleUnableToConnectClient();
       
   876         }
       
   877     TRACE_FUNC_EXIT
       
   878     }
       
   879 
       
   880 void CHsCore::HandleDesiredDeviceNotFound( TInt aErr )
       
   881     {
       
   882     TRACE_FUNC_ENTRY
       
   883     TRACE_INFO( (_L("aErr = %d"), aErr))
       
   884     if ( iObserver )
       
   885         {
       
   886         iObserver->HandleUnableToConnectClient();
       
   887         }
       
   888     TRACE_FUNC_EXIT
       
   889     }
       
   890 
       
   891 void CHsCore::HandleRemoteControllerConnected( TInt aErr )
       
   892     {
       
   893     TRACE_FUNC_ENTRY
       
   894     if ( iObserver && aErr == KErrNone )
       
   895         {
       
   896         iObserver->HandleConnectedToRemoteController();
       
   897         }
       
   898     TRACE_FUNC_EXIT
       
   899     }
       
   900 
       
   901 void CHsCore::HandleRemoteControllerDisconnected( TInt /** aErr */)
       
   902     {
       
   903     TRACE_FUNC_ENTRY
       
   904     if ( iObserver )
       
   905         {
       
   906         iObserver->HandleDisconnectedFromRemoteController();
       
   907         }
       
   908     TRACE_FUNC_EXIT
       
   909     }