bluetoothengine/bthid/bthidserver/src/socketinitiator.cpp
changeset 0 f63038272f30
equal deleted inserted replaced
-1:000000000000 0:f63038272f30
       
     1 /*
       
     2 * Copyright (c) 2008 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:  This is the implementation of application class
       
    15  *
       
    16 */
       
    17 
       
    18 #include <e32debug.h>
       
    19 #include "hiddebug.h"
       
    20 #include "socketinitiator.h"
       
    21 #include "sockinitnotifier.h"
       
    22 #include "timeouttimer.h"
       
    23 #include "bthidtypes.h"
       
    24 #include "sockets.pan"
       
    25 #include "debug.h"
       
    26 // The transport protocol of the sockets used by this object.
       
    27 _LIT(KTransportProtocol, "L2CAP");
       
    28 
       
    29 // 60*2 seconds socket connect time-out (initially this was 10 seconds, but was changed due to end-user feedback)
       
    30 const TInt CSocketInitiator::KTimeOut = 120000000;
       
    31 
       
    32 CSocketInitiator* CSocketInitiator::NewL(RSocketServ& aSocketServ,
       
    33         MSockInitNotifier& aNotifier)
       
    34     {
       
    35     CSocketInitiator* self = NewLC(aSocketServ, aNotifier);
       
    36     CleanupStack::Pop(self);
       
    37     return self;
       
    38     }
       
    39 
       
    40 CSocketInitiator* CSocketInitiator::NewLC(RSocketServ& aSocketServ,
       
    41         MSockInitNotifier& aNotifier)
       
    42     {
       
    43     CSocketInitiator* self = new (ELeave) CSocketInitiator(aSocketServ,
       
    44             aNotifier);
       
    45     CleanupStack::PushL(self);
       
    46     self->ConstructL();
       
    47     return self;
       
    48     }
       
    49 
       
    50 CSocketInitiator::CSocketInitiator(RSocketServ& aSocketServ,
       
    51         MSockInitNotifier& aNotifier) :
       
    52     CActive(CActive::EPriorityStandard), iSocketServ(aSocketServ), iNotifier(
       
    53             aNotifier), iState(EIdle)
       
    54     {
       
    55     CActiveScheduler::Add(this);
       
    56     }
       
    57 
       
    58 CSocketInitiator::~CSocketInitiator()
       
    59     {
       
    60         TRACE_INFO((_L("[BTHID]\tCSocketInitiator::~CSocketInitiator()")));
       
    61     Cancel(); // Causes DoCancel
       
    62 
       
    63     delete iTimer;
       
    64     }
       
    65 
       
    66 void CSocketInitiator::ConnectSocketsL(const TBTDevAddr& aAddress,
       
    67         TBool aUseSecurity, RSocket* aControlSocket,
       
    68         RSocket* aInterruptSocket)
       
    69     {
       
    70         TRACE_INFO((_L("[BTHID]\tCSocketInitiator::ConnectSocketsL")));
       
    71     // Store the params for later
       
    72     iControlSocket = aControlSocket;
       
    73     iInterruptSocket = aInterruptSocket;
       
    74     iUseSecurity = aUseSecurity;
       
    75 
       
    76     // First close the sockets
       
    77     iControlSocket->Close();
       
    78     iInterruptSocket->Close();
       
    79 
       
    80     // Try to open the sockets as L2CAP
       
    81     User::LeaveIfError(iControlSocket->Open(iSocketServ, KTransportProtocol));
       
    82     User::LeaveIfError(
       
    83             iInterruptSocket->Open(iSocketServ, KTransportProtocol));
       
    84 
       
    85     iSockAddress.SetBTAddr(aAddress);
       
    86     // First connect the control channel
       
    87     iSockAddress.SetPort(KL2CAPHidControl);
       
    88 
       
    89     // Set security requirements for the Control channel.
       
    90     TBTServiceSecurity sec;
       
    91     sec.SetAuthentication(aUseSecurity); //Require authentication
       
    92     sec.SetEncryption(aUseSecurity); //and encryption.
       
    93     sec.SetAuthorisation(EFalse); //and authorisation (This not needed for out-going connections)
       
    94     iSockAddress.SetSecurity(sec);
       
    95 
       
    96     // Start a timer to timeout the connect request
       
    97     if (!iTimer->IsActive())
       
    98         iTimer->After(KTimeOut);
       
    99     // Issue the connect request
       
   100     iControlSocket->Connect(iSockAddress, iStatus);
       
   101     // Update the connect state
       
   102     iState = EConnectingControl;
       
   103 
       
   104 #ifdef __WINS__
       
   105 
       
   106     User::After(1); // Fix to allow emulator client to connect to server
       
   107 #endif
       
   108 
       
   109     // Start this active object
       
   110     SetActive();
       
   111     }
       
   112 
       
   113 void CSocketInitiator::TimerExpired()
       
   114     {
       
   115         TRACE_INFO((_L("[BTHID]\tCSocketInitiator::TimerExpired()")));
       
   116     // Cancel the operation in progress
       
   117     Cancel();
       
   118     // Inform the observer of this object that a failure occurred
       
   119     // due to a timeout
       
   120     iNotifier.SocketsConnFailed(KErrTimedOut);
       
   121     }
       
   122 
       
   123 void CSocketInitiator::ConstructL()
       
   124     {
       
   125     // Create a timer
       
   126     iTimer = CTimeOutTimer::NewL(EPriorityStandard, *this);
       
   127     }
       
   128 
       
   129 void CSocketInitiator::DoCancel()
       
   130     {
       
   131         TRACE_INFO((_L("[BTHID]\tCSocketInitiator::DoCancel()")));
       
   132 
       
   133     // Cancel appropriate request
       
   134     switch (iState)
       
   135         {
       
   136         case EConnectingControl:
       
   137             {
       
   138             iControlSocket->CancelConnect();
       
   139             break;
       
   140             }
       
   141         case EConnectingInterrupt:
       
   142             {
       
   143             iInterruptSocket->CancelConnect();
       
   144             break;
       
   145             }
       
   146         default:
       
   147             {
       
   148             User::Panic(KPanicBTConnection, ESocketsBadStatus);
       
   149             break;
       
   150             }
       
   151         }
       
   152 
       
   153     //Close the sockets
       
   154     iInterruptSocket->Close();
       
   155     iControlSocket->Close();
       
   156 
       
   157     // Set the connect state back to idle
       
   158     iState = EIdle;
       
   159     }
       
   160 
       
   161 void CSocketInitiator::RunL()
       
   162     {
       
   163     // Cancel any outstanding timer
       
   164     iTimer->Cancel();
       
   165 
       
   166     // Any error stops us dead.
       
   167     if (iStatus != KErrNone)
       
   168         {
       
   169         //Close the sockets
       
   170         iControlSocket->Close();
       
   171         iInterruptSocket->Close();
       
   172 
       
   173         // Set the connect state back to idle
       
   174         iState = EIdle;
       
   175         // Inform the observer that a failure occurred
       
   176         iNotifier.SocketsConnFailed(iStatus.Int());
       
   177         }
       
   178     else
       
   179         {
       
   180         switch (iState)
       
   181             {
       
   182             case EConnectingControl:
       
   183                 {
       
   184                     TRACE_INFO((_L("[BTHID]\tCSocketInitiator::RunL(): Control channel connection request")));
       
   185                 ConnectInterruptSocket();
       
   186                 }
       
   187                 break;
       
   188 
       
   189             case EConnectingInterrupt:
       
   190                 // L2CAP Interrupt channel connection request
       
   191                 // We are connected
       
   192                 // Set the connect state back to idle
       
   193                 {
       
   194                     TRACE_INFO((_L("[BTHID]\tCSocketInitiator::RunL(): Interrupt channel connection request")));
       
   195                 iState = EIdle;
       
   196                 // Inform the observer that connection is complete
       
   197                 iNotifier.SocketsConnected();
       
   198                 break;
       
   199                 }
       
   200 
       
   201             default:
       
   202                 {
       
   203                 User::Panic(KPanicBTConnection, ESocketsBadState);
       
   204                 break;
       
   205                 }
       
   206 
       
   207             };
       
   208         }
       
   209     }
       
   210 
       
   211 void CSocketInitiator::ConnectInterruptSocket()
       
   212     {
       
   213         // Connect the interrupt channel.
       
   214         TRACE_INFO((_L("[BTHID]\tCSocketInitiator::ConnectInterruptSocket()")));
       
   215     ///for BT Stack v2.0, use authentication and encryption
       
   216     TBTServiceSecurity sec;
       
   217     sec.SetAuthentication(iUseSecurity); // 2nd L2cap channel should need no authentication.
       
   218     sec.SetEncryption(iUseSecurity); // but we need encryption.
       
   219     iSockAddress.SetSecurity(sec);
       
   220     ////
       
   221     iSockAddress.SetPort(KL2CAPHidInterrupt);
       
   222 
       
   223     // Start a timer to timeout the connect request
       
   224     if (!iTimer->IsActive())
       
   225         iTimer->After(KTimeOut);
       
   226     // Issue the connect request
       
   227     iInterruptSocket->Connect(iSockAddress, iStatus);
       
   228     // Update the connect state
       
   229     iState = EConnectingInterrupt;
       
   230 
       
   231 #ifdef __WINS__
       
   232 
       
   233     User::After(1); // Fix to allow emulator client to connect to server
       
   234 #endif
       
   235 
       
   236     SetActive();
       
   237     }