bluetoothengine/bthid/bthidclient/src/bthidclient.cpp
changeset 0 f63038272f30
child 22 613943a21004
equal deleted inserted replaced
-1:000000000000 0:f63038272f30
       
     1 /*
       
     2 * Copyright (c) 2007 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:  BT HID client implementation
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include <btmanclient.h>
       
    20 #include "bthidclient.h"
       
    21 #include "bthidclientsrv.h"
       
    22 #include "debug.h"
       
    23 #include "hiduids.h"
       
    24 
       
    25 static TInt StartServer();
       
    26 
       
    27 // ---------------------------------------------------------
       
    28 // Connect
       
    29 // Handles connection to server(creates session)
       
    30 // ---------------------------------------------------------
       
    31 //
       
    32 EXPORT_C TInt RBTHidClient::Connect()
       
    33     {
       
    34         TRACE_FUNC
       
    35 
       
    36     TInt retVal = CreateSession(KBTHidSrvName, Version());
       
    37     if (retVal != KErrNone && retVal != KErrAlreadyExists)
       
    38         {
       
    39         retVal = StartServer();
       
    40         if (retVal == KErrNone || retVal == KErrAlreadyExists)
       
    41             {
       
    42             retVal = CreateSession(KBTHidSrvName, Version());
       
    43             }
       
    44         }
       
    45         TRACE_INFO((_L("[BTHID] ret %d "), retVal))
       
    46 
       
    47     if (retVal != KErrNone)
       
    48         {
       
    49         Close();
       
    50         }
       
    51 
       
    52     return retVal;
       
    53     }
       
    54 
       
    55 // ---------------------------------------------------------
       
    56 // Version
       
    57 // Defines server version number
       
    58 // ---------------------------------------------------------
       
    59 //
       
    60 TVersion RBTHidClient::Version() const
       
    61     {
       
    62     return (TVersion(KBTHIDServMajorVersionNumber,
       
    63             KBTHIDServMinorVersionNumber, KBTHIDServBuildVersionNumber));
       
    64     }
       
    65 
       
    66 // ---------------------------------------------------------
       
    67 // ConnectToHID
       
    68 // Establishes a Bluetooth HID connection
       
    69 // ---------------------------------------------------------
       
    70 //
       
    71 EXPORT_C void RBTHidClient::ConnectDevice(const TBTDevAddrPckgBuf& aAddrBuf,
       
    72         TDes8& aDiagnostic, TRequestStatus& aStatus)
       
    73     {
       
    74     SendReceive(EBTHIDServConnectDevice, TIpcArgs(&aAddrBuf, &aDiagnostic),
       
    75             aStatus);
       
    76     }
       
    77 
       
    78 // ---------------------------------------------------------
       
    79 // CancelConnectDevice
       
    80 // Cancel Connection attempt
       
    81 // ---------------------------------------------------------
       
    82 //
       
    83 EXPORT_C void RBTHidClient::CancelConnectDevice() const
       
    84     {
       
    85     SendReceive(EBTHIDServCancelConnect);
       
    86     }
       
    87 
       
    88 // ---------------------------------------------------------
       
    89 // NotifyStatusChange
       
    90 // Request for a notify for change of connection status
       
    91 // ---------------------------------------------------------
       
    92 //
       
    93 EXPORT_C void RBTHidClient::NotifyStatusChange(
       
    94         THIDStateUpdateBuf& aUpdateParams, TDes8& aDiagnostic,
       
    95         TRequestStatus& aStatus)
       
    96     {
       
    97     SendReceive(EBTHIDServNotifyConnectionChange, TIpcArgs(&aUpdateParams,
       
    98             &aDiagnostic), aStatus);
       
    99     }
       
   100 
       
   101 // ---------------------------------------------------------
       
   102 // CancelNotifyStatusChange
       
   103 // Cancel a outstanding request for a notify for change of connection status
       
   104 // ---------------------------------------------------------
       
   105 //
       
   106 EXPORT_C void RBTHidClient::CancelNotifyStatusChange() const
       
   107     {
       
   108     SendReceive(EBTHIDServCancelNotify);
       
   109     }
       
   110 
       
   111 // ---------------------------------------------------------
       
   112 // DisconnectDevice
       
   113 // Disconnect from the selected HID device
       
   114 // ---------------------------------------------------------
       
   115 //
       
   116 EXPORT_C void RBTHidClient::DisconnectDevice(
       
   117         const TBTDevAddrPckgBuf& aAddrBuf, TBTDisconnectType aDiscType,
       
   118         TRequestStatus& aStatus)
       
   119     {
       
   120     TBTHidParamPkg discPkg(aDiscType);
       
   121     SendReceive(EBTHIDServDisconnectDevice, TIpcArgs(&aAddrBuf, &discPkg),
       
   122             aStatus);
       
   123     }
       
   124 
       
   125 // ---------------------------------------------------------
       
   126 // DisconnectAllGracefully
       
   127 // Disconnect from the all HID devices
       
   128 // ---------------------------------------------------------
       
   129 //
       
   130 EXPORT_C void RBTHidClient::DisconnectAllGracefully(TRequestStatus& aStatus)
       
   131     {
       
   132         TRACE_FUNC
       
   133     SendReceive(EBTHidSrvDisconnectAllGracefully, aStatus);
       
   134     }
       
   135 
       
   136 // ---------------------------------------------------------
       
   137 // IsConnected
       
   138 // Request for the connection status for a specified device
       
   139 // ---------------------------------------------------------
       
   140 //
       
   141 EXPORT_C TInt RBTHidClient::IsConnected(const TBTDevAddr& aBTaddr)
       
   142     {
       
   143     TPckgC<TBTDevAddr> addrPck(aBTaddr);
       
   144     return SendReceive(EBTHIDServIsConnected, TIpcArgs(&addrPck));
       
   145     }
       
   146 
       
   147 // ---------------------------------------------------------
       
   148 // GetConnections
       
   149 // Request for a list of current connected HID device addresses
       
   150 // ---------------------------------------------------------
       
   151 //
       
   152 EXPORT_C void RBTHidClient::GetConnections(TDes8& aAddrs, TInt aProfile)
       
   153     {
       
   154         TRACE_FUNC
       
   155     SendReceive(EBTHIDGetConnections, TIpcArgs(&aAddrs, aProfile));
       
   156     }
       
   157 
       
   158 TInt StartServer()
       
   159     {
       
   160         TRACE_STATIC_FUNC
       
   161     const TUid KKBTHIDSrvUid3 =
       
   162         {
       
   163         BTHID_SRV_UID
       
   164         };
       
   165     const TUidType serverUid(KNullUid,KNullUid, KKBTHIDSrvUid3);
       
   166     RProcess server;
       
   167     TInt r = server.Create(KBTHidServerFilename, KNullDesC, serverUid);
       
   168     if (r != KErrNone)
       
   169         return r;
       
   170     server.SetPriority(EPriorityHigh);
       
   171 
       
   172     TRequestStatus stat;
       
   173     server.Rendezvous(stat);
       
   174     if (stat != KRequestPending)
       
   175         {
       
   176         server.Kill(0);
       
   177         }
       
   178     else
       
   179         {
       
   180         server.Resume();
       
   181         }
       
   182 
       
   183     User::WaitForRequest(stat);
       
   184         TRACE_INFO((_L("[BTHID] Server started, code %d"), stat.Int()))
       
   185     r = (server.ExitType() == EExitPanic) ? KErrGeneral : stat.Int();
       
   186     server.Close();
       
   187     return r;
       
   188     }
       
   189 // End of File