bluetoothengine/bthid/bthidengplugin/src/bthidengplugin.cpp
changeset 0 f63038272f30
child 9 a42ed326b458
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:  Bluetooth Hid ECom plug-in class definition.
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include <ecom/ecom.h>
       
    20 #include <ecom/implementationproxy.h>
       
    21 #include <btmanclient.h>
       
    22 #include "bthidengplugin.h"
       
    23 #include "bthidclient.h"
       
    24 
       
    25 #include "debug.h"
       
    26 
       
    27 //Used to identify the type of request to the AO
       
    28 enum TRequestId
       
    29     {
       
    30     ERequestConnect = 1,
       
    31     ERequestDisconnect = 2,
       
    32     ENotifyProfileStatusChange = 3,
       
    33     ERequestDisconnectAll,
       
    34     };
       
    35 
       
    36 CBTHidPlugin::CBTHidPlugin()
       
    37     {
       
    38     }
       
    39 
       
    40 CBTHidPlugin* CBTHidPlugin::NewL()
       
    41     {
       
    42     CBTHidPlugin* self = new (ELeave) CBTHidPlugin();
       
    43     CleanupStack::PushL(self);
       
    44     self->ConstructL();
       
    45     CleanupStack::Pop(self);
       
    46     return self;
       
    47     }
       
    48 
       
    49 void CBTHidPlugin::ConstructL()
       
    50     {
       
    51         TRACE_FUNC
       
    52         LEAVE_IF_ERROR( iClient.Connect() );
       
    53 
       
    54     iActive4ProfileStatus = CGenericActive::NewL(*this,
       
    55             CActive::EPriorityStandard, ENotifyProfileStatusChange);
       
    56     iDiagnosticAddress.Zero();
       
    57     iClient.NotifyStatusChange(iHIDStateUpdatePckg, iDiagnosticAddress,
       
    58             iActive4ProfileStatus->iStatus);
       
    59     iActive4ProfileStatus->GoActive();
       
    60 
       
    61         TRACE_FUNC_EXIT
       
    62     }
       
    63 
       
    64 CBTHidPlugin::~CBTHidPlugin()
       
    65     {
       
    66     delete iActive4ClientReq;
       
    67     delete iActive4ProfileStatus;
       
    68     if (iClient.Handle())
       
    69         {
       
    70         TRequestStatus req = KRequestPending;
       
    71         iClient.DisconnectAllGracefully(req);
       
    72         User::WaitForRequest(req);
       
    73         }
       
    74     iClient.Close();
       
    75         TRACE_FUNC
       
    76     }
       
    77 
       
    78 void CBTHidPlugin::SetObserver(MBTEngPluginObserver* aObserver)
       
    79     {
       
    80     iObserver = aObserver;
       
    81     }
       
    82 
       
    83 void CBTHidPlugin::GetSupportedProfiles(RProfileArray& aProfiles)
       
    84     {
       
    85     aProfiles.Reset();
       
    86     aProfiles.Append(EBTProfileHID);
       
    87     }
       
    88 
       
    89 TBool CBTHidPlugin::IsProfileSupported(const TBTProfile aProfile) const
       
    90     {
       
    91     return (aProfile == EBTProfileHID);
       
    92     }
       
    93 
       
    94 TInt CBTHidPlugin::Connect(const TBTDevAddr& aAddr)
       
    95     {
       
    96         TRACE_FUNC
       
    97     return HandleAsyncRequest(aAddr, ERequestConnect);
       
    98     }
       
    99 
       
   100 void CBTHidPlugin::CancelConnect(const TBTDevAddr& aAddr)
       
   101     {
       
   102         TRACE_FUNC
       
   103     (void) aAddr;
       
   104     iClient.CancelConnectDevice();
       
   105     }
       
   106 
       
   107 TInt CBTHidPlugin::Disconnect(const TBTDevAddr& aAddr,
       
   108         TBTDisconnectType aDiscType)
       
   109     {
       
   110         TRACE_FUNC
       
   111     (void) aDiscType;
       
   112     return HandleAsyncRequest(aAddr, ERequestDisconnect);
       
   113     }
       
   114 
       
   115 void CBTHidPlugin::GetConnections(RBTDevAddrArray& aAddrArray,
       
   116         TBTProfile aConnectedProfile)
       
   117     {
       
   118     if (aConnectedProfile == EBTProfileHID)
       
   119         {
       
   120         TBuf8<KBTDevAddrSize * 2> addrbuf;
       
   121         iClient.GetConnections(addrbuf, aConnectedProfile);
       
   122         TPtrC8 ptr(addrbuf);
       
   123         while (ptr.Length() >= KBTDevAddrSize)
       
   124             {
       
   125             aAddrArray.Append(TBTDevAddr(ptr.Left(KBTDevAddrSize)));
       
   126             ptr.Set(ptr.Mid(KBTDevAddrSize));
       
   127             }
       
   128         }
       
   129     }
       
   130 
       
   131 void CBTHidPlugin::HandleNotifyProfileStatusChange(CGenericActive& aActive)
       
   132     {
       
   133     if (aActive.iStatus.Int() == KErrNone && iObserver)
       
   134         {
       
   135         TBool retStatus = EFalse;
       
   136 
       
   137         THIDStateUpdate& HIDStateUpdate = iHIDStateUpdatePckg();
       
   138 
       
   139         if (HIDStateUpdate.iState == EBTDeviceConnected
       
   140                 || HIDStateUpdate.iState == EBTDeviceLinkRestored)
       
   141             {
       
   142             retStatus = ETrue;
       
   143             ReportProfileConnectionEvents(HIDStateUpdate.iDeviceAddress,
       
   144                     retStatus);
       
   145             }
       
   146 
       
   147         if (HIDStateUpdate.iState == EBTDeviceDisconnected
       
   148                 || HIDStateUpdate.iState == EBTDeviceLinkLost
       
   149                 || HIDStateUpdate.iState == EBTDeviceUnplugged)
       
   150             {
       
   151             retStatus = EFalse;
       
   152             ReportProfileConnectionEvents(HIDStateUpdate.iDeviceAddress,
       
   153                     retStatus);
       
   154             }
       
   155         iDiagnosticAddress.Zero();
       
   156         iClient.NotifyStatusChange(iHIDStateUpdatePckg, iDiagnosticAddress,
       
   157                 aActive.iStatus);
       
   158         aActive.GoActive();
       
   159         }
       
   160     else
       
   161         {
       
   162         if (aActive.iStatus.Int() != KErrNone)
       
   163             {
       
   164             THIDStateUpdate& HIDStateUpdate = iHIDStateUpdatePckg();
       
   165             if (iDiagnosticAddress.Length() >= KBTDevAddrSize)
       
   166                 {
       
   167                 RBTDevAddrArray array;
       
   168                 TPtrC8 ptr(iDiagnosticAddress);
       
   169                 while (ptr.Length() >= KBTDevAddrSize)
       
   170                     {
       
   171                         TRAP_IGNORE(array.AppendL(TBTDevAddr(ptr.Left(KBTDevAddrSize))););
       
   172 #ifdef _DEBUG
       
   173                     const TPtrC8 myPtr(array[array.Count() - 1].Des());
       
   174 #endif
       
   175                         TRACE_INFO((_L8("conflict <%S>"), &myPtr))
       
   176                     ptr.Set(ptr.Mid(KBTDevAddrSize));
       
   177                     }
       
   178 
       
   179                 iBTDevAddrPckgBuf() = HIDStateUpdate.iDeviceAddress;
       
   180                 iObserver->ConnectComplete(iBTDevAddrPckgBuf(),
       
   181                         EBTProfileHID, aActive.iStatus.Int(), &array);
       
   182                 array.Close();
       
   183                 }
       
   184             else
       
   185                 {
       
   186                 iObserver->ConnectComplete(HIDStateUpdate.iDeviceAddress,
       
   187                         EBTProfileHID, aActive.iStatus.Int());
       
   188                 }
       
   189             iClient.NotifyStatusChange(iHIDStateUpdatePckg,
       
   190                     iDiagnosticAddress, aActive.iStatus);
       
   191             aActive.GoActive();
       
   192             }
       
   193         }
       
   194     }
       
   195 
       
   196 void CBTHidPlugin::HandelRequestConnectComplete()
       
   197     {
       
   198     if (iActive4ClientReq->iStatus.Int() != KErrNone) // might have conflicts, decode iDiagnostic
       
   199         {
       
   200         if (iDiagnostic.Length() >= KBTDevAddrSize)
       
   201             {
       
   202             RBTDevAddrArray array;
       
   203             TPtrC8 ptr(iDiagnostic);
       
   204             while (ptr.Length() >= KBTDevAddrSize)
       
   205                 {
       
   206                     TRAP_IGNORE(array.AppendL(TBTDevAddr(ptr.Left(KBTDevAddrSize))););
       
   207 #ifdef _DEBUG
       
   208                 const TPtrC8 myPtr(array[array.Count() - 1].Des());
       
   209 #endif
       
   210                     TRACE_INFO((_L8("conflict <%S>"), &myPtr))
       
   211                 ptr.Set(ptr.Mid(KBTDevAddrSize));
       
   212                 }
       
   213             iObserver->ConnectComplete(iBTDevAddrPckgBuf(), EBTProfileHID,
       
   214                     iActive4ClientReq->iStatus.Int(), &array);
       
   215             array.Close();
       
   216             }
       
   217         else
       
   218             {
       
   219             iObserver->ConnectComplete(iBTDevAddrPckgBuf(), EBTProfileHID,
       
   220                     iActive4ClientReq->iStatus.Int());
       
   221             }
       
   222         }
       
   223     else
       
   224         {
       
   225         TInt profile = 0;
       
   226         if (iDiagnostic.Length() >= sizeof(TInt))
       
   227             {
       
   228             TPckg<TInt> pckg(profile);
       
   229             pckg.Copy(iDiagnostic.Mid(0, sizeof(TInt)));
       
   230             }
       
   231         ReportProfileConnectionEvents(iBTDevAddrPckgBuf(), ETrue);
       
   232         }
       
   233     delete iActive4ClientReq;
       
   234     iActive4ClientReq = NULL;
       
   235     }
       
   236 
       
   237 void CBTHidPlugin::HandelRequestDisconnectComplete()
       
   238     {
       
   239     if (iActive4ClientReq->iStatus.Int() != KErrNone)
       
   240         {
       
   241         iObserver->DisconnectComplete(iBTDevAddrPckgBuf(), EBTProfileHID,
       
   242                 iActive4ClientReq->iStatus.Int());
       
   243         }
       
   244     else
       
   245         {
       
   246         TInt profile = 0;
       
   247         if (iDiagnostic.Length() >= sizeof(TInt))
       
   248             {
       
   249             TPckg<TInt> pckg(profile);
       
   250             pckg.Copy(iDiagnostic.Mid(0, sizeof(TInt)));
       
   251             }
       
   252         ReportProfileConnectionEvents(iBTDevAddrPckgBuf(), EFalse);
       
   253         }
       
   254     delete iActive4ClientReq;
       
   255     iActive4ClientReq = NULL;
       
   256     }
       
   257 
       
   258 void CBTHidPlugin::RequestCompletedL(CGenericActive& aActive)
       
   259     {
       
   260         TRACE_FUNC
       
   261     switch (aActive.RequestId())
       
   262         {
       
   263         case ENotifyProfileStatusChange:
       
   264             {
       
   265             HandleNotifyProfileStatusChange(aActive);
       
   266             break;
       
   267             }
       
   268         case ERequestConnect:
       
   269             {
       
   270             HandelRequestConnectComplete();
       
   271             break;
       
   272             }
       
   273         case ERequestDisconnect:
       
   274             {
       
   275             HandelRequestDisconnectComplete();
       
   276             break;
       
   277             }
       
   278         case ERequestDisconnectAll:
       
   279             {
       
   280             iObserver->DisconnectComplete(iBTDevAddrPckgBuf(), EBTProfileHID,
       
   281                     iActive4ClientReq->iStatus.Int());
       
   282             break;
       
   283             }
       
   284         }
       
   285     }
       
   286 
       
   287 TBTEngConnectionStatus CBTHidPlugin::IsConnected(const TBTDevAddr& aAddr)
       
   288     {
       
   289     TInt stat = 0;
       
   290     stat = iClient.IsConnected(aAddr);
       
   291         TRACE_INFO((_L("CBTHidPlugin::IsConnected() = %d"), stat))
       
   292     return ((TBTEngConnectionStatus) stat);
       
   293     }
       
   294 
       
   295 void CBTHidPlugin::CancelRequest(CGenericActive& aActive)
       
   296     {
       
   297     if (aActive.RequestId() == ENotifyProfileStatusChange)
       
   298         {
       
   299         iClient.CancelNotifyStatusChange();
       
   300         }
       
   301     else
       
   302         {
       
   303         if (aActive.RequestId() == ERequestConnect)
       
   304             {
       
   305             iClient.CancelConnectDevice();
       
   306             }
       
   307         }
       
   308     }
       
   309 
       
   310 TInt CBTHidPlugin::HandleAsyncRequest(const TBTDevAddr& aAddr,
       
   311         TInt aRequestId)
       
   312     {
       
   313     TInt err = KErrNone;
       
   314     if (!iClient.Handle())
       
   315         {
       
   316         err = iClient.Connect();
       
   317         }
       
   318     if (err)
       
   319         {
       
   320         return err;
       
   321         }
       
   322     if (iActive4ClientReq)
       
   323         {
       
   324         err = KErrServerBusy;
       
   325         }
       
   326     if (!err)
       
   327         {
       
   328         iActive4ClientReq = CGenericActive::New(*this,
       
   329                 CActive::EPriorityStandard, aRequestId);
       
   330         if (iActive4ClientReq)
       
   331             {
       
   332             iBTDevAddrPckgBuf() = aAddr;
       
   333             if (aRequestId == ERequestConnect)
       
   334                 {
       
   335                 iDiagnostic.Zero();
       
   336                 iClient.ConnectDevice(iBTDevAddrPckgBuf, iDiagnostic,
       
   337                         iActive4ClientReq->iStatus);
       
   338                 }
       
   339             else
       
   340                 {
       
   341                 iClient.DisconnectDevice(iBTDevAddrPckgBuf, EBTDiscImmediate,
       
   342                         iActive4ClientReq->iStatus);
       
   343                 }
       
   344             iActive4ClientReq->GoActive();
       
   345             }
       
   346         else
       
   347             {
       
   348             err = KErrNoMemory;
       
   349             }
       
   350         }
       
   351     return err;
       
   352     }
       
   353 
       
   354 void CBTHidPlugin::ReportProfileConnectionEvents(const TBTDevAddr& aAddr,
       
   355         TBool aConnected)
       
   356     {
       
   357         TRACE_FUNC
       
   358         TRACE_INFO((_L("CBTHidPlugin::ReportProfileConnectionEvents() == %d"), aConnected))
       
   359 
       
   360     if (iObserver)
       
   361         {
       
   362         if (aConnected)
       
   363             {
       
   364             iObserver->ConnectComplete(aAddr, EBTProfileHID, KErrNone);
       
   365             }
       
   366         else
       
   367             {
       
   368             iObserver->DisconnectComplete(aAddr, EBTProfileHID, KErrNone);
       
   369             }
       
   370         }
       
   371     }
       
   372 
       
   373 //End of File