bluetoothengine/bteng/src/btengclient.cpp
changeset 0 f63038272f30
child 19 43824b19ee35
equal deleted inserted replaced
-1:000000000000 0:f63038272f30
       
     1 /*
       
     2 * Copyright (c) 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:  Client-side implementation of BTEng
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <e32base.h>
       
    21 #include <btmanclient.h>
       
    22 
       
    23 #include "btengclient.h"
       
    24 #include "debug.h"
       
    25 
       
    26 /**  Number of retries for connecting to BTEng server side */
       
    27 const TInt KBTEngConnectAttempts = 3;
       
    28 
       
    29 
       
    30 // ======== LOCAL FUNCTIONS ========
       
    31 
       
    32 // ---------------------------------------------------------------------------
       
    33 // ?description
       
    34 // ---------------------------------------------------------------------------
       
    35 //
       
    36 TInt ClientStart()
       
    37     {
       
    38     TRACE_FUNC_ENTRY
       
    39     const TUidType serverUid( KNullUid, KNullUid, KBTEngServerUid3 );
       
    40         // Create a new server process. Simultaneous launching of two processes 
       
    41         // should be detected when the second one attempts to create the server 
       
    42         // object, failing with KErrAlreadyExists.
       
    43     RProcess server;
       
    44     TInt err = server.Create( KBTEngServerName, KNullDesC, serverUid );
       
    45     if( err != KErrNone )
       
    46         {
       
    47         TRACE_INFO( ( _L( "[BTEng]\t Server process creation failed %d" ), err ) );
       
    48         return err;
       
    49         }
       
    50     TRequestStatus status;
       
    51     server.Rendezvous( status );
       
    52     if( status != KRequestPending )
       
    53         {
       
    54         TRACE_INFO( ( _L( "[BTEng]\t wrong status (%d), kill process"), 
       
    55                             status.Int() ) )
       
    56         server.Kill( KErrCancel );  // Abort startup
       
    57         }
       
    58     else
       
    59         {
       
    60         TRACE_INFO( ( _L( "[BTEng]\t Server process launched") ) )
       
    61         server.Resume();
       
    62         }
       
    63     User::WaitForRequest( status );     // Wait for start or death
       
    64     err = status.Int();
       
    65     if( server.ExitType() == EExitPanic )
       
    66         {
       
    67         // The server actually panicked; inform the client.
       
    68         err = KErrDied;
       
    69         }
       
    70     server.Close();
       
    71     TRACE_FUNC_RES( ( _L( "result %d" ), err ) )
       
    72     return err;
       
    73     }
       
    74 
       
    75 
       
    76 // ======== MEMBER FUNCTIONS ========
       
    77 
       
    78 // ---------------------------------------------------------------------------
       
    79 // C++ default constructor
       
    80 // ---------------------------------------------------------------------------
       
    81 //
       
    82 RBTEng::RBTEng()
       
    83 :   RSessionBase()
       
    84     {
       
    85     }
       
    86 
       
    87 
       
    88 // ---------------------------------------------------------------------------
       
    89 // ?implementation_description
       
    90 // ---------------------------------------------------------------------------
       
    91 //
       
    92 TInt RBTEng::Connect()
       
    93     {
       
    94     TRACE_FUNC_ENTRY
       
    95     TInt err = KErrNone;
       
    96     for( TInt i = 0; i < KBTEngConnectAttempts; i++ )
       
    97         {
       
    98         err = CreateSession( KBTEngServerName, Version() );
       
    99         if( err == KErrNone )
       
   100             {
       
   101             break;  // Success
       
   102             }
       
   103         else if( err == KErrNotFound || err == KErrServerTerminated )
       
   104             {
       
   105                 // Session could not be created, 
       
   106                 // first the server needs to be started.
       
   107             err = ClientStart();
       
   108             }
       
   109         if( err != KErrNone && err != KErrAlreadyExists )
       
   110             {
       
   111                 // If the server cannot be started for other reasons than 
       
   112                 // KErrAlreadyExists, give up...
       
   113             break;
       
   114             }
       
   115         }
       
   116     TRACE_FUNC_RES( ( _L("result %d"), err ) )
       
   117     return err;
       
   118     }
       
   119 
       
   120 
       
   121 // ---------------------------------------------------------------------------
       
   122 // ?implementation_description
       
   123 // ---------------------------------------------------------------------------
       
   124 //
       
   125 TVersion RBTEng::Version()
       
   126     {
       
   127     return TVersion( KBTEngServerVersionMajor, KBTEngServerVersionMinor, 
       
   128                       KBTEngServerVersionBuild );
       
   129     }
       
   130 
       
   131 
       
   132 // ---------------------------------------------------------------------------
       
   133 // ?implementation_description
       
   134 // ---------------------------------------------------------------------------
       
   135 //
       
   136 TInt RBTEng::SetPowerState( const TBTPowerStateValue aState, const TBool aTemp )
       
   137     {
       
   138     return SendReceive( EBTEngSetPowerState, TIpcArgs( aState, aTemp ) );
       
   139     }
       
   140 
       
   141 
       
   142 // ---------------------------------------------------------------------------
       
   143 // ?implementation_description
       
   144 // ---------------------------------------------------------------------------
       
   145 //
       
   146 TInt RBTEng::SetVisibilityMode( const TBTVisibilityMode aMode, const TInt aTime )
       
   147     {
       
   148     return SendReceive( EBTEngSetVisibilityMode, TIpcArgs( aMode, aTime ) );
       
   149     }
       
   150 
       
   151 
       
   152 // ---------------------------------------------------------------------------
       
   153 // ?implementation_description
       
   154 // ---------------------------------------------------------------------------
       
   155 //
       
   156 TInt RBTEng::ConnectDevice( const TBTDevAddr& aAddr, 
       
   157     const TBTDeviceClass& aDeviceClass  )
       
   158     {
       
   159     TRACE_BDADDR(aAddr);
       
   160     TBTDevAddrPckgBuf addrPkg( aAddr );
       
   161     TBTEngDevClassPkg codPkg( aDeviceClass.DeviceClass() );
       
   162     return SendReceive( EBTEngConnectDevice, TIpcArgs( &addrPkg, &codPkg ) );
       
   163     }
       
   164 
       
   165 
       
   166 // ---------------------------------------------------------------------------
       
   167 // ?implementation_description
       
   168 // ---------------------------------------------------------------------------
       
   169 //
       
   170 TInt RBTEng::CancelConnectDevice( const TBTDevAddr& aAddr )
       
   171     {
       
   172     TRACE_BDADDR(aAddr);
       
   173     TBTDevAddrPckgBuf addrPkg( aAddr );
       
   174     return SendReceive( EBTEngCancelConnectDevice, TIpcArgs( &addrPkg ) );
       
   175     }
       
   176 
       
   177 
       
   178 // ---------------------------------------------------------------------------
       
   179 // ?implementation_description
       
   180 // ---------------------------------------------------------------------------
       
   181 //
       
   182 TInt RBTEng::DisconnectDevice( const TBTDevAddr& aAddr, 
       
   183     TBTDisconnectType aDiscType )
       
   184     {
       
   185     TRACE_BDADDR(aAddr);
       
   186     TBTDevAddrPckgBuf addrPkg( aAddr );
       
   187     TBTEngParamPkg discPkg( aDiscType );
       
   188     return SendReceive( EBTEngDisconnectDevice, TIpcArgs( &addrPkg, &discPkg ) );
       
   189     }
       
   190 
       
   191 
       
   192 // ---------------------------------------------------------------------------
       
   193 // ?implementation_description
       
   194 // ---------------------------------------------------------------------------
       
   195 //
       
   196 TInt RBTEng::IsDeviceConnected( const TBTDevAddr& aAddr, 
       
   197     TBTEngConnectionStatus& aConnected )
       
   198     {
       
   199     TRACE_BDADDR(aAddr);
       
   200     TBTDevAddrPckgBuf addrPkg( aAddr );
       
   201     TBTEngParamPkg connPkg( aConnected );
       
   202     TInt err = SendReceive( EBTEngIsDeviceConnected, TIpcArgs( &addrPkg, &connPkg ) );
       
   203     aConnected = (TBTEngConnectionStatus) connPkg();
       
   204     return err;
       
   205     }
       
   206 
       
   207 // ---------------------------------------------------------------------------
       
   208 // ?implementation_description
       
   209 // ---------------------------------------------------------------------------
       
   210 //
       
   211 TInt RBTEng::IsDeviceConnectable( const TBTDevAddr& aAddr,
       
   212     const TBTDeviceClass& aDeviceClass, TBool& aConnectable )
       
   213     {
       
   214     TRACE_BDADDR(aAddr);
       
   215     TBTDevAddrPckgBuf addrPkg( aAddr );
       
   216     TBTEngDevClassPkg codPkg( aDeviceClass.DeviceClass() );
       
   217     TBTEngParamPkg connPkg( aConnectable );
       
   218     TInt err = SendReceive( EBTEngIsDeviceConnectable, TIpcArgs( &addrPkg, &codPkg, &connPkg ) );
       
   219     aConnectable = connPkg();
       
   220     return err;
       
   221     }
       
   222 
       
   223 // ---------------------------------------------------------------------------
       
   224 // ?implementation_description
       
   225 // ---------------------------------------------------------------------------
       
   226 //
       
   227 TInt RBTEng::GetConnectedAddresses( TDes8& aArrayPkg, TBTEngParamPkg& aProfilePkg )
       
   228     {
       
   229     return SendReceive( EBTEngGetConnectedAddresses, 
       
   230                          TIpcArgs( &aArrayPkg, &aProfilePkg ) );
       
   231     }
       
   232 
       
   233 
       
   234 // ---------------------------------------------------------------------------
       
   235 // ?implementation_description
       
   236 // ---------------------------------------------------------------------------
       
   237 //
       
   238 TInt RBTEng::NotifyConnectionEvents( TDes8& aEvent, TRequestStatus& aStatus )
       
   239     {
       
   240         // Use the third slot for the event package.
       
   241     SendReceive( EBTEngNotifyConnectionEvents, 
       
   242                   TIpcArgs( NULL, NULL, &aEvent ), aStatus );
       
   243     return KErrNone;
       
   244     }
       
   245 
       
   246 
       
   247 // ---------------------------------------------------------------------------
       
   248 // ?implementation_description
       
   249 // ---------------------------------------------------------------------------
       
   250 //
       
   251 TInt RBTEng::CancelNotifyConnectionEvents()
       
   252     {
       
   253     return SendReceive( EBTEngCancelEventNotifier, TIpcArgs() );
       
   254     }
       
   255 
       
   256 
       
   257 // ---------------------------------------------------------------------------
       
   258 // Set a pairing observer in BTEngine.
       
   259 // ---------------------------------------------------------------------------
       
   260 //
       
   261 TInt RBTEng::SetPairingObserver( const TBTDevAddr& aAddr, TBool aActivate )
       
   262     {
       
   263     TBTDevAddrPckgBuf addrPkg( aAddr );
       
   264     return SendReceive( EBTEngSetPairingObserver, TIpcArgs( &addrPkg, aActivate ) );
       
   265     }
       
   266 
       
   267 
       
   268 // ---------------------------------------------------------------------------
       
   269 // ?implementation_description
       
   270 // ---------------------------------------------------------------------------
       
   271 //
       
   272 TInt RBTEng::PrepareDiscovery()
       
   273     {
       
   274     return SendReceive( EBTEngPrepareDiscovery, TIpcArgs() );
       
   275     }
       
   276 
       
   277 // ---------------------------------------------------------------------------
       
   278 // Start to pair a device in BTEngine.
       
   279 // ---------------------------------------------------------------------------
       
   280 //
       
   281 void RBTEng::PairDevice( const TBTDevAddrPckgBuf& aAddr, 
       
   282         const TUint32& aDeviceClass, TRequestStatus& aStatus  )
       
   283     {
       
   284     SendReceive( EBTEngPairDevice, TIpcArgs( &aAddr, aDeviceClass ), aStatus );    
       
   285     }
       
   286 
       
   287 // ---------------------------------------------------------------------------
       
   288 // cancel pairing request
       
   289 // ---------------------------------------------------------------------------
       
   290 //
       
   291 void RBTEng::CancelPairDevice( )
       
   292     {
       
   293     (void) SendReceive( EBTEngCancelPairDevice);        
       
   294     }