[Bluetooth][Non-ACR] Define Interop callback to global variable (#2005)
[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 Interop.Bluetooth.HidDeviceConnectionStateChangedCallback _hidDeviceConnectionStateChangedCallback;
26
27         private event EventHandler<HidDeviceDataReceivedEventArgs> _hidDeviceDataReceived;
28         private Interop.Bluetooth.HidDeviceDataReceivedCallback _hidDeviceDataReceivedCallback;
29
30         private static readonly BluetoothHidDeviceImpl _instance = new BluetoothHidDeviceImpl();
31
32         internal event EventHandler<HidDeviceConnectionStateChangedEventArgs> ConnectionStateChanged
33         {
34             add
35             {
36                 _hidDeviceConnectionStateChanged += value;
37             }
38             remove
39             {
40                 _hidDeviceConnectionStateChanged -= value;
41             }
42         }
43
44         internal int ConnectHidDevice(string deviceAddress)
45         {
46             return Interop.Bluetooth.ConnectHidDevice(deviceAddress);
47         }
48
49         internal int DisconnectHidDevice(string deviceAddress)
50         {
51             return Interop.Bluetooth.DisconnectHidDevice(deviceAddress);
52         }
53
54         internal void SendHidDeviceMouseEvent(string deviceAddress, BluetoothHidMouseData mouseData)
55         {
56             int ret = Interop.Bluetooth.SendHidDeviceMouseEvent(deviceAddress, mouseData);
57             if (ret != (int)BluetoothError.None)
58             {
59                 Log.Error(Globals.LogTag, "Failed to send mouse event to the remote device, Error - " + (BluetoothError)ret);
60                 BluetoothErrorFactory.ThrowBluetoothException(ret);
61             }
62         }
63
64         internal void SendHidDeviceKeyEvent(string deviceAddress, BluetoothHidKeyData keyData)
65         {
66             int ret = Interop.Bluetooth.SendHidDeviceKeyEvent(deviceAddress, keyData);
67             if (ret != (int)BluetoothError.None)
68             {
69                 Log.Error(Globals.LogTag, "Failed to send key event to the remote device, Error - " + (BluetoothError)ret);
70                 BluetoothErrorFactory.ThrowBluetoothException(ret);
71             }
72         }
73
74         internal event EventHandler<HidDeviceDataReceivedEventArgs> DataReceived
75         {
76             add
77             {
78                 if (_hidDeviceDataReceived == null)
79                 {
80                     RegisterHidDataReceivedEvent();
81                 }
82                 _hidDeviceDataReceived += value;
83             }
84             remove
85             {
86                 _hidDeviceDataReceived -= value;
87                 if (_hidDeviceDataReceived == null)
88                 {
89                     UnregisterHidDataReceivedEvent();
90                 }
91             }
92         }
93
94         private void RegisterHidDataReceivedEvent()
95         {
96             _hidDeviceDataReceivedCallback = (ref BluetoothHidDeviceReceivedDataStruct receivedData, IntPtr userData) =>
97             {
98                 _hidDeviceDataReceived?.Invoke(null, new HidDeviceDataReceivedEventArgs(BluetoothHidDeviceReceivedData.Create(receivedData)));
99             };
100
101             int ret = Interop.Bluetooth.SetHidDeviceDataReceivedCallback(_hidDeviceDataReceivedCallback, IntPtr.Zero);
102             if (ret != (int)BluetoothError.None)
103             {
104                 Log.Error(Globals.LogTag, "Failed to set data received callback, Error - " + (BluetoothError)ret);
105                 BluetoothErrorFactory.ThrowBluetoothException(ret);
106             }
107         }
108
109         private void UnregisterHidDataReceivedEvent()
110         {
111             int ret = Interop.Bluetooth.UnsetHidDeviceDataReceivedCallback();
112             if (ret != (int)BluetoothError.None)
113             {
114                 Log.Error(Globals.LogTag, "Failed to unset data received callback, Error - " + (BluetoothError)ret);
115                 BluetoothErrorFactory.ThrowBluetoothException(ret);
116             }
117         }
118
119         internal void ReplyToReportHidDevice(string deviceAddress, BluetoothHidHeaderType headerType, BluetoothHidParamType paramType, byte[] data)
120         {
121             int ret = Interop.Bluetooth.ReplyToReportHidDevice(deviceAddress, headerType, paramType, data, data.Length, IntPtr.Zero);
122             if (ret != (int)BluetoothError.None)
123             {
124                 Log.Error(Globals.LogTag, "Failed to reply to report from hid host, Error - " + (BluetoothError)ret);
125                 BluetoothErrorFactory.ThrowBluetoothException(ret);
126             }
127         }
128
129         internal static BluetoothHidDeviceImpl Instance
130         {
131             get
132             {
133                 return _instance;
134             }
135         }
136
137         private BluetoothHidDeviceImpl()
138         {
139             Initialize();
140         }
141
142         ~BluetoothHidDeviceImpl()
143         {
144             Deinitialize();
145         }
146
147         private void Initialize()
148         {
149             if (Globals.IsInitialize)
150             {
151                 _hidDeviceConnectionStateChangedCallback = (int result, bool isConnected, string address, IntPtr userData) =>
152                 {
153                     _hidDeviceConnectionStateChanged?.Invoke(null, new HidDeviceConnectionStateChangedEventArgs(result, isConnected, address));
154                 };
155
156                 int ret = Interop.Bluetooth.ActivateHidDevice(_hidDeviceConnectionStateChangedCallback, IntPtr.Zero);
157                 if (ret != (int)BluetoothError.None)
158                 {
159                     Log.Error(Globals.LogTag, "Failed to activate to the remote device, Error - " + (BluetoothError)ret);
160                     BluetoothErrorFactory.ThrowBluetoothException(ret);
161                 }
162             }
163             else
164             {
165                 Log.Error(Globals.LogTag, "Failed to initialize HID Device, BT not initialized");
166                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotInitialized);
167             }
168         }
169
170         private void Deinitialize()
171         {
172             int ret = Interop.Bluetooth.DeactivateHidDevice();
173             if (ret != (int)BluetoothError.None)
174             {
175                 Log.Error(Globals.LogTag, "Failed to deactivate to the remote device, Error - " + (BluetoothError)ret);
176                 BluetoothErrorFactory.ThrowBluetoothException(ret);
177             }
178         }
179     }
180 }
181