Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Bluetooth / Tizen.Network.Bluetooth / BluetoothAudioImpl.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 BluetoothAudioImpl : IDisposable
22     {
23         private event EventHandler<AudioConnectionStateChangedEventArgs> _audioConnectionChanged;
24         private Interop.Bluetooth.AudioConnectionStateChangedCallback _audioConnectionChangedCallback;
25
26         private static readonly BluetoothAudioImpl _instance = new BluetoothAudioImpl();
27         private bool disposed = false;
28
29         internal event EventHandler<AudioConnectionStateChangedEventArgs> AudioConnectionStateChanged
30         {
31             add
32             {
33                 if (_audioConnectionChanged == null)
34                 {
35                     RegisterAudioConnectionChangedEvent();
36                 }
37                 _audioConnectionChanged += value;
38             }
39             remove
40             {
41                 _audioConnectionChanged -= value;
42                 if (_audioConnectionChanged == null)
43                 {
44                     UnregisterAudioConnectionChangedEvent();
45                 }
46             }
47         }
48
49         private void RegisterAudioConnectionChangedEvent()
50         {
51             _audioConnectionChangedCallback = (int result, bool connected, string deviceAddress, int profileType, IntPtr userData) =>
52             {
53                 if (_audioConnectionChanged != null)
54                 {
55                     _audioConnectionChanged(null, new AudioConnectionStateChangedEventArgs(result, connected, deviceAddress, (BluetoothAudioProfileType)profileType));
56                 }
57             };
58             int ret = Interop.Bluetooth.SetAudioConnectionStateChangedCallback(_audioConnectionChangedCallback, IntPtr.Zero);
59             if (ret != (int)BluetoothError.None)
60             {
61                 Log.Error(Globals.LogTag, "Failed to set audio connection changed callback, Error - " + (BluetoothError)ret);
62             }
63         }
64
65         private void UnregisterAudioConnectionChangedEvent()
66         {
67             int ret = Interop.Bluetooth.UnsetAudioConnectionStateChangedCallback();
68             if (ret != (int)BluetoothError.None)
69             {
70                 Log.Error(Globals.LogTag, "Failed to unset audio connection changed callback, Error - " + (BluetoothError)ret);
71             }
72         }
73
74         internal int Connect(string deviceAddress, BluetoothAudioProfileType type)
75         {
76             int ret = Interop.Bluetooth.Connect(deviceAddress, (int)type);
77             if (ret != (int)BluetoothError.None)
78             {
79                 Log.Error(Globals.LogTag, "Failed to connect device with the given profile type, Error - " + (BluetoothError)ret);
80             }
81             return ret;
82         }
83
84         internal int Disconnect(string deviceAddress, BluetoothAudioProfileType type)
85         {
86             int ret = Interop.Bluetooth.Disconnect(deviceAddress, (int)type);
87             if (ret != (int)BluetoothError.None)
88             {
89                 Log.Error(Globals.LogTag, "Failed to disconnect device with the given profile type, Error - " + (BluetoothError)ret);
90             }
91             return ret;
92         }
93
94         internal static BluetoothAudioImpl Instance
95         {
96             get
97             {
98                 return _instance;
99             }
100         }
101
102         private BluetoothAudioImpl ()
103         {
104             Log.Info(Globals.LogTag, "Initializing audio");
105             initialize();
106         }
107
108         ~BluetoothAudioImpl()
109         {
110             Dispose(false);
111         }
112
113         public void Dispose()
114         {
115             Dispose(true);
116             GC.SuppressFinalize(this);
117         }
118
119         private void Dispose(bool disposing)
120         {
121             if (disposed)
122                 return;
123
124             if (disposing)
125             {
126                 // Free managed objects.
127             }
128             //Free unmanaged objects
129             deinitialize();
130             RemoveAllRegisteredEvent();
131             disposed = true;
132         }
133
134         private void initialize()
135         {
136             if (Globals.IsInitialize)
137             {
138                 int ret = Interop.Bluetooth.InitializeAudio ();
139                 if (ret != (int)BluetoothError.None)
140                 {
141                     Log.Error(Globals.LogTag, "Failed to initialize bluetoothaudio, Error - " + (BluetoothError)ret);
142                     Globals.IsAudioInitialize = false;
143                     BluetoothErrorFactory.ThrowBluetoothException (ret);
144                 }
145                 else
146                 {
147                     Globals.IsAudioInitialize = true;
148                 }
149             }
150             else
151             {
152                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotInitialized);
153             }
154         }
155
156         private void deinitialize()
157         {
158             if (Globals.IsAudioInitialize) {
159                 int ret = Interop.Bluetooth.DeinitializeAudio ();
160                 if (ret != (int)BluetoothError.None) {
161                     Log.Error (Globals.LogTag, "Failed to deinitialize bluetoothaudio, Error - " + (BluetoothError)ret);
162                 }
163             }
164         }
165
166         private void RemoveAllRegisteredEvent()
167         {
168             if (_audioConnectionChanged != null)
169             {
170                 UnregisterAudioConnectionChangedEvent();
171             }
172         }
173     }
174 }