[Bluetooth][Non-ACR] Define Interop callback to global variable (#2005)
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Bluetooth / Tizen.Network.Bluetooth / BluetoothOppImpl.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 BluetoothOppServerImpl
22     {
23         private static readonly BluetoothOppServerImpl _instance = new BluetoothOppServerImpl();
24
25         internal event EventHandler<ConnectionRequestedEventArgs> ConnectionRequested;
26         private Interop.Bluetooth.ConnectionRequestedCallback _ConnectionRequestedCallback;
27
28         internal event EventHandler<TransferProgressEventArgs> TransferProgress;
29         private Interop.Bluetooth.TransferProgressCallback _TransferProgressCallback;
30
31         internal event EventHandler<TransferFinishedEventArgs> TransferFinished;
32         private Interop.Bluetooth.TransferFinishedCallback _TransferFinishedCallback;
33
34         internal int StartServer(string filePath)
35         {
36             _ConnectionRequestedCallback = (string devAddress, IntPtr userData) =>
37             {
38                 ConnectionRequested?.Invoke(this, new ConnectionRequestedEventArgs(devAddress));
39             };
40
41             int ret = Interop.Bluetooth.InitializeOppServerCustom(filePath, _ConnectionRequestedCallback, IntPtr.Zero);
42             if (ret != (int)BluetoothError.None)
43             {
44                 Log.Error(Globals.LogTag, "Failed to start bluetooth opp server, Error - " + (BluetoothError)ret);
45                 BluetoothErrorFactory.ThrowBluetoothException(ret);
46             }
47             else
48             {
49                 Globals.IsOppServerInitialized = true;
50             }
51             return ret;
52         }
53
54         internal int StopServer()
55         {
56             if (Globals.IsOppServerInitialized)
57             {
58                 int ret = Interop.Bluetooth.DinitializeOppServer();
59                 if (ret != (int)BluetoothError.None) {
60                     Log.Error (Globals.LogTag, "Failed to stop bluetooth opp server, Error - " + (BluetoothError)ret);
61                 }
62                 return ret;
63             }
64             return (int)BluetoothError.NotInitialized;
65         }
66
67         internal int AcceptPush(string name, out int _transferId)
68         {
69             _transferId = -1;
70             if (Globals.IsOppServerInitialized)
71             {
72                 _TransferProgressCallback = (string file, long size, int percent, IntPtr userData) =>
73                 {
74                     TransferProgress?.Invoke(this, new TransferProgressEventArgs(file, size, percent));
75                 };
76
77                 _TransferFinishedCallback = (int result, string file, long size, IntPtr userData) =>
78                 {
79                     TransferFinished?.Invoke(this, new TransferFinishedEventArgs(result, file, size));
80                 };
81
82                 int ret = Interop.Bluetooth.OppServerAcceptPush(_TransferProgressCallback, _TransferFinishedCallback, name, IntPtr.Zero, out _transferId);
83                 if (ret != (int)BluetoothError.None)
84                 {
85                     Log.Error(Globals.LogTag, "Failed to accept the push request, Error - " + (BluetoothError)ret);
86                 }
87                 return ret;
88             }
89             return (int)BluetoothError.NotInitialized;
90         }
91
92         internal int RejectPush()
93         {
94             if (Globals.IsOppServerInitialized)
95             {
96                 int ret = Interop.Bluetooth.OppServerRejectPush();
97                 if (ret != (int)BluetoothError.None)
98                 {
99                     Log.Error(Globals.LogTag, "Failed to reject the push request, Error - " + (BluetoothError)ret);
100                 }
101                 return ret;
102             }
103             return (int)BluetoothError.NotInitialized;
104         }
105
106         internal int CancelTransferId(int TransferId)
107         {
108             if (Globals.IsOppServerInitialized)
109             {
110                 int ret = Interop.Bluetooth.OppServerCancelTransfer(TransferId);
111                 if (ret != (int)BluetoothError.None)
112                 {
113                     Log.Error(Globals.LogTag, "Failed to cancel the transferid " + TransferId + " Error - " + (BluetoothError)ret);
114                 }
115                 return ret;
116             }
117             return (int)BluetoothError.NotInitialized;
118         }
119
120         internal int SetDestinationPath(string path)
121         {
122             if (Globals.IsOppServerInitialized)
123             {
124                 int ret = Interop.Bluetooth.OppServerSetDestinationPath(path);
125                 if (ret != (int)BluetoothError.None)
126                 {
127                     Log.Error(Globals.LogTag, "Failed to Set the desitination path " + path + " Error - " + (BluetoothError)ret);
128                 }
129                 return ret;
130             }
131             return (int)BluetoothError.NotInitialized;
132         }
133
134         internal static BluetoothOppServerImpl Instance
135         {
136             get
137             {
138                 return _instance;
139             }
140         }
141     }
142
143     internal class BluetoothOppClientImpl
144     {
145         private static readonly BluetoothOppClientImpl _instance = new BluetoothOppClientImpl();
146
147         internal event EventHandler<PushRespondedEventArgs> PushResponded;
148         private Interop.Bluetooth.PushRespondedCallback _PushRespondedCallback;
149
150         internal event EventHandler<PushProgressEventArgs> PushProgress;
151         private Interop.Bluetooth.PushProgressCallback _PushProgressCallback;
152
153         internal event EventHandler<PushFinishedEventArgs> PushFinished;
154         private Interop.Bluetooth.PushFinishedCallback _PushFinishedCallback;
155
156         private BluetoothOppClientImpl()
157         {
158             Log.Info(Globals.LogTag, "Initializing OppClient");
159             Initialize();
160         }
161
162         ~BluetoothOppClientImpl()
163         {
164             Deinitialize();
165         }
166
167         internal int AddFile(string filePath)
168         {
169
170             if (Globals.IsOppClientInitialized)
171             {
172                 int ret = Interop.Bluetooth.OppClientAddFile(filePath);
173                 if (ret != (int)BluetoothError.None)
174                 {
175                     Log.Error(Globals.LogTag, "Failed to Add File, Error - " + (BluetoothError)ret);
176                 }
177                 return ret;
178             }
179             return (int)BluetoothError.NotInitialized;
180         }
181
182         internal int ClearFile()
183         {
184
185             if (Globals.IsOppClientInitialized)
186             {
187                 int ret = Interop.Bluetooth.OppClientClearFiles();
188                 if (ret != (int)BluetoothError.None)
189                 {
190                     Log.Error(Globals.LogTag, "Failed to Clear Files, Error - " + (BluetoothError)ret);
191                 }
192                 return ret;
193             }
194             return (int)BluetoothError.NotInitialized;
195         }
196
197         internal int CancelPush()
198         {
199
200             if (Globals.IsOppClientInitialized)
201             {
202                 int ret = Interop.Bluetooth.OppClientCancelPush();
203                 if (ret != (int)BluetoothError.None)
204                 {
205                     Log.Error(Globals.LogTag, "Failed to Clear Files, Error - " + (BluetoothError)ret);
206                 }
207                 return ret;
208             }
209             return (int)BluetoothError.NotInitialized;
210         }
211
212         internal int PushFile(string Destination)
213         {
214
215             if (Globals.IsOppClientInitialized)
216             {
217                 _PushRespondedCallback = (int result, string address, IntPtr userData) =>
218                 {
219                     PushResponded?.Invoke(this, new PushRespondedEventArgs(result, address));
220                 };
221
222                 _PushProgressCallback = (string file, long size, int percent, IntPtr userData) =>
223                 {
224                     PushProgress?.Invoke(this, new PushProgressEventArgs(file, size, percent));
225                 };
226
227                 _PushFinishedCallback = (int result, string address, IntPtr userData) =>
228                 {
229                     PushFinished?.Invoke(this, new PushFinishedEventArgs(result, address));
230                 };
231
232                 int ret = Interop.Bluetooth.OppClientPushFile(Destination, _PushRespondedCallback, _PushProgressCallback, _PushFinishedCallback, IntPtr.Zero);
233                 if (ret != (int)BluetoothError.None)
234                 {
235                     Log.Error(Globals.LogTag, "Failed to push File, Error - " + (BluetoothError)ret);
236                 }
237                 return ret;
238             }
239             return (int)BluetoothError.NotInitialized;
240         }
241
242         private void Initialize()
243         {
244             if (Globals.IsInitialize)
245             {
246
247                 int ret = Interop.Bluetooth.InitializeOppClient();
248                 if (ret != (int)BluetoothError.None)
249                 {
250                     Log.Error(Globals.LogTag, "Failed to initialize bluetooth Opp Client, Error - " + (BluetoothError)ret);
251                     BluetoothErrorFactory.ThrowBluetoothException(ret);
252                 }
253                 else
254                 {
255                     Globals.IsOppClientInitialized = true;
256                 }
257             }
258             else
259             {
260                 Log.Error(Globals.LogTag, "Failed to initialize Opp Client, BT not initialized");
261                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotInitialized);
262             }
263         }
264
265         private void Deinitialize()
266         {
267             if (Globals.IsOppClientInitialized)
268             {
269                 int ret = Interop.Bluetooth.DeinitializeOppClient();
270                 if (ret != (int)BluetoothError.None)
271                 {
272                     Log.Error(Globals.LogTag, "Failed to deinitialize Opp Client, Error - " + (BluetoothError)ret);
273                     BluetoothErrorFactory.ThrowBluetoothException(ret);
274                 }
275                 else
276                 {
277                     Globals.IsOppClientInitialized = false;
278                 }
279             }
280         }
281
282         internal static BluetoothOppClientImpl Instance
283         {
284             get
285             {
286                 return _instance;
287             }
288         }
289     }
290 }
291