Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Bluetooth / Tizen.Network.Bluetooth / BluetoothHidImpl.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
19 namespace Tizen.Network.Bluetooth
20 {
21     internal class BluetoothHidImpl : IDisposable
22     {
23         private event EventHandler<HidConnectionStateChangedEventArgs> _hidConnectionChanged;
24         private Interop.Bluetooth.HidConnectionStateChangedCallback _hidConnectionChangedCallback;
25
26         private static readonly BluetoothHidImpl _instance = new BluetoothHidImpl();
27         private bool disposed = false;
28
29         internal event EventHandler<HidConnectionStateChangedEventArgs> HidConnectionStateChanged
30         {
31             add
32             {
33                 _hidConnectionChanged += value;
34             }
35             remove
36             {
37                 //nothing to be done
38             }
39         }
40
41         internal int Connect(string deviceAddress)
42         {
43             if (Globals.IsHidInitialize)
44             {
45                 int ret = Interop.Bluetooth.Connect (deviceAddress);
46                 if (ret != (int)BluetoothError.None) {
47                     Log.Error (Globals.LogTag, "Failed to connect device with the hid service, Error - " + (BluetoothError)ret);
48                 }
49                 return ret;
50             }
51             return (int)BluetoothError.NotInitialized;
52         }
53
54         internal int Disconnect(string deviceAddress)
55         {
56             if (Globals.IsHidInitialize)
57             {
58                 int ret = Interop.Bluetooth.Disconnect (deviceAddress);
59                 if (ret != (int)BluetoothError.None) {
60                     Log.Error (Globals.LogTag, "Failed to disconnect device with the hid service, Error - " + (BluetoothError)ret);
61                 }
62                 return ret;
63             }
64             return (int)BluetoothError.NotInitialized;
65         }
66
67         internal static BluetoothHidImpl Instance
68         {
69             get
70             {
71                 return _instance;
72             }
73         }
74         private BluetoothHidImpl ()
75         {
76             initialize();
77         }
78         ~BluetoothHidImpl()
79         {
80             Dispose(false);
81         }
82
83         public void Dispose()
84         {
85             Dispose(true);
86             GC.SuppressFinalize(this);
87         }
88
89         private void Dispose(bool disposing)
90         {
91             if (disposed)
92                 return;
93
94             if (disposing)
95             {
96                 // Free managed objects.
97             }
98             //Free unmanaged objects
99             deinitialize();
100             disposed = true;
101         }
102
103         private void initialize()
104         {
105             if (Globals.IsInitialize)
106             {
107                 _hidConnectionChangedCallback = (int result, bool connected, string deviceAddress, IntPtr userData) =>
108                 {
109                     if (_hidConnectionChanged != null)
110                     {
111                         _hidConnectionChanged(null, new HidConnectionStateChangedEventArgs(result, connected, deviceAddress));
112                     }
113                 };
114
115                 int ret = Interop.Bluetooth.InitializeHid (_hidConnectionChangedCallback, IntPtr.Zero);
116                 if (ret != (int)BluetoothError.None)
117                 {
118                     Log.Error(Globals.LogTag, "Failed to initialize bluetooth hid, Error - " + (BluetoothError)ret);
119                     BluetoothErrorFactory.ThrowBluetoothException (ret);
120                 }
121                 else
122                 {
123                     Globals.IsHidInitialize = true;
124                 }
125             }
126             else
127             {
128                 Log.Error(Globals.LogTag, "Failed to initialize HID, BT not initialized");
129                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotInitialized);
130             }
131         }
132
133         private void deinitialize()
134         {
135             if (Globals.IsHidInitialize)
136             {
137                 int ret = Interop.Bluetooth.DeinitializeHid ();
138                 if (ret != (int)BluetoothError.None) {
139                     Log.Error (Globals.LogTag, "Failed to deinitialize bluetooth hid, Error - " + (BluetoothError)ret);
140                     BluetoothErrorFactory.ThrowBluetoothException (ret);
141                 } else {
142                     Globals.IsHidInitialize = false;
143                 }
144             }
145         }
146     }
147 }
148