[Bluetooth] Add BluetoothHidDevice APIs (#574)
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Bluetooth / Tizen.Network.Bluetooth / BluetoothHidDeviceImpl.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.Threading.Tasks;
19
20 namespace Tizen.Network.Bluetooth
21 {
22     internal class BluetoothHidDeviceImpl
23     {
24         private event EventHandler<HidDeviceConnectionStateChangedEventArgs> _hidDeviceConnectionStateChanged;
25         private event EventHandler<HidDeviceDataReceivedEventArgs> _hidDeviceDataReceived;
26
27         private static readonly BluetoothHidDeviceImpl _instance = new BluetoothHidDeviceImpl();
28
29         internal event EventHandler<HidDeviceConnectionStateChangedEventArgs> ConnectionStateChanged
30         {
31             add
32             {
33                 _hidDeviceConnectionStateChanged += value;
34             }
35             remove
36             {
37                 _hidDeviceConnectionStateChanged -= value;
38             }
39         }
40
41         internal int ConnectHidDevice(string deviceAddress)
42         {
43             return Interop.Bluetooth.ConnectHidDevice(deviceAddress);
44         }
45
46         internal int DisconnectHidDevice(string deviceAddress)
47         {
48             return Interop.Bluetooth.DisconnectHidDevice(deviceAddress);
49         }
50
51         internal void SendHidDeviceMouseEvent(string deviceAddress, BluetoothHidMouseData mouseData)
52         {
53             int ret = Interop.Bluetooth.SendHidDeviceMouseEvent(deviceAddress, mouseData);
54             if (ret != (int)BluetoothError.None)
55             {
56                 Log.Error(Globals.LogTag, "Failed to send mouse event to the remote device, Error - " + (BluetoothError)ret);
57                 BluetoothErrorFactory.ThrowBluetoothException(ret);
58             }
59         }
60
61         internal void SendHidDeviceKeyEvent(string deviceAddress, BluetoothHidKeyData keyData)
62         {
63             int ret = Interop.Bluetooth.SendHidDeviceKeyEvent(deviceAddress, keyData);
64             if (ret != (int)BluetoothError.None)
65             {
66                 Log.Error(Globals.LogTag, "Failed to send key event to the remote device, Error - " + (BluetoothError)ret);
67                 BluetoothErrorFactory.ThrowBluetoothException(ret);
68             }
69         }
70
71         internal event EventHandler<HidDeviceDataReceivedEventArgs> DataReceived
72         {
73             add
74             {
75                 if (_hidDeviceDataReceived == null)
76                 {
77                     RegisterHidDataReceivedEvent();
78                 }
79                 _hidDeviceDataReceived += value;
80             }
81             remove
82             {
83                 _hidDeviceDataReceived -= value;
84                 if (_hidDeviceDataReceived == null)
85                 {
86                     UnregisterHidDataReceivedEvent();
87                 }
88             }
89         }
90
91         private void RegisterHidDataReceivedEvent()
92         {
93             Interop.Bluetooth.HidDeviceDataReceivedCallback _hidDeviceDataReceivedCallback = (ref BluetoothHidDeviceReceivedDataStruct receivedData, IntPtr userData) =>
94             {
95                 _hidDeviceDataReceived?.Invoke(null, new HidDeviceDataReceivedEventArgs(BluetoothHidDeviceReceivedData.Create(receivedData)));
96             };
97
98             int ret = Interop.Bluetooth.SetHidDeviceDataReceivedCallback(_hidDeviceDataReceivedCallback, IntPtr.Zero);
99             if (ret != (int)BluetoothError.None)
100             {
101                 Log.Error(Globals.LogTag, "Failed to set data received callback, Error - " + (BluetoothError)ret);
102                 BluetoothErrorFactory.ThrowBluetoothException(ret);
103             }
104         }
105
106         private void UnregisterHidDataReceivedEvent()
107         {
108             int ret = Interop.Bluetooth.UnsetHidDeviceDataReceivedCallback();
109             if (ret != (int)BluetoothError.None)
110             {
111                 Log.Error(Globals.LogTag, "Failed to unset data received callback, Error - " + (BluetoothError)ret);
112                 BluetoothErrorFactory.ThrowBluetoothException(ret);
113             }
114         }
115
116         internal void ReplyToReportHidDevice(string deviceAddress, BluetoothHidHeaderType headerType, BluetoothHidParamType paramType, byte[] data)
117         {
118             int ret = Interop.Bluetooth.ReplyToReportHidDevice(deviceAddress, headerType, paramType, data, data.Length, IntPtr.Zero);
119             if (ret != (int)BluetoothError.None)
120             {
121                 Log.Error(Globals.LogTag, "Failed to reply to report from hid host, Error - " + (BluetoothError)ret);
122                 BluetoothErrorFactory.ThrowBluetoothException(ret);
123             }
124         }
125
126         internal static BluetoothHidDeviceImpl Instance
127         {
128             get
129             {
130                 return _instance;
131             }
132         }
133
134         private BluetoothHidDeviceImpl()
135         {
136             Initialize();
137         }
138
139         ~BluetoothHidDeviceImpl()
140         {
141             Deinitialize();
142         }
143
144         private void Initialize()
145         {
146             if (Globals.IsInitialize)
147             {
148                 Interop.Bluetooth.HidDeviceConnectionStateChangedCallback _hidDeviceConnectionStateChangedCallback = (int result, bool isConnected, string address, IntPtr userData) =>
149                 {
150                     _hidDeviceConnectionStateChanged?.Invoke(null, new HidDeviceConnectionStateChangedEventArgs(result, isConnected, address));
151                 };
152
153                 int ret = Interop.Bluetooth.ActivateHidDevice(_hidDeviceConnectionStateChangedCallback, IntPtr.Zero);
154                 if (ret != (int)BluetoothError.None)
155                 {
156                     Log.Error(Globals.LogTag, "Failed to activate to the remote device, Error - " + (BluetoothError)ret);
157                     BluetoothErrorFactory.ThrowBluetoothException(ret);
158                 }
159             }
160             else
161             {
162                 Log.Error(Globals.LogTag, "Failed to initialize HID Device, BT not initialized");
163                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotInitialized);
164             }
165         }
166
167         private void Deinitialize()
168         {
169             int ret = Interop.Bluetooth.DeactivateHidDevice();
170             if (ret != (int)BluetoothError.None)
171             {
172                 Log.Error(Globals.LogTag, "Failed to deactivate to the remote device, Error - " + (BluetoothError)ret);
173                 BluetoothErrorFactory.ThrowBluetoothException(ret);
174             }
175         }
176     }
177 }
178