Merge "[Bluetooth] Fix Pascal case Capitalization Style"
[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 event EventHandler<ConnectionRequestedEventArgs> _ConnectionRequested;
24         private Interop.Bluetooth.ConnectionRequestedCallback _ConnectionRequestedCallback;
25
26         private event EventHandler<TransferProgressEventArgs> _TransferProgress;
27         private Interop.Bluetooth.TransferProgressCallback _TransferProgressCallback;
28
29         private event EventHandler<TransferFinishedEventArgs> _TransferFinished;
30         private Interop.Bluetooth.TransferFinishedCallback _TransferFinishedCallback;
31
32         private static readonly BluetoothOppServerImpl _instance = new BluetoothOppServerImpl();
33
34         internal event EventHandler<ConnectionRequestedEventArgs> ConnectionRequested
35         {
36             add
37             {
38                 _ConnectionRequested += value;
39             }
40             remove
41             {
42                 //nothing to be done
43             }
44         }
45
46         internal event EventHandler<TransferProgressEventArgs> TransferProgress
47         {
48             add
49             {
50                 _TransferProgress += value;
51             }
52             remove
53             {
54                 //nothing to be done
55             }
56         }
57
58         internal event EventHandler<TransferFinishedEventArgs> TransferFinished
59         {
60             add
61             {
62                 _TransferFinished += value;
63             }
64             remove
65             {
66                 //nothing to be done
67             }
68         }
69
70         internal int StartServer(string filePath)
71         {
72             _ConnectionRequestedCallback = (string devAddress, IntPtr userData) =>
73             {
74                 _ConnectionRequested?.Invoke(null, new ConnectionRequestedEventArgs(devAddress));
75             };
76
77             int ret = Interop.Bluetooth.InitializeOppServerCustom(filePath, _ConnectionRequestedCallback, IntPtr.Zero);
78             if (ret != (int)BluetoothError.None)
79             {
80                 Log.Error(Globals.LogTag, "Failed to start bluetooth opp server, Error - " + (BluetoothError)ret);
81                 BluetoothErrorFactory.ThrowBluetoothException(ret);
82             }
83             else
84             {
85                 Globals.IsOppServerInitialized = true;
86             }
87             return ret;
88         }
89
90         internal int StopServer()
91         {
92             if (Globals.IsOppServerInitialized)
93             {
94                 int ret = Interop.Bluetooth.DinitializeOppServer();
95                 if (ret != (int)BluetoothError.None) {
96                     Log.Error (Globals.LogTag, "Failed to stop bluetooth opp server, Error - " + (BluetoothError)ret);
97                 }
98                 return ret;
99             }
100             return (int)BluetoothError.NotInitialized;
101         }
102
103         internal int AcceptPush(string name, out int _transferId)
104         {
105             _transferId = -1;
106             if (Globals.IsOppServerInitialized)
107             {
108                 _TransferProgressCallback = (string file, long size, int percent, IntPtr userData) =>
109                 {
110                     _TransferProgress?.Invoke(null, new TransferProgressEventArgs(file, size, percent));
111                 };
112
113                 _TransferFinishedCallback = (int result, string file, long size, IntPtr userData) =>
114                 {
115                     _TransferFinished?.Invoke(null, new TransferFinishedEventArgs(result, file, size));
116                 };
117
118                 int ret = Interop.Bluetooth.OppServerAcceptPush(_TransferProgressCallback, _TransferFinishedCallback, name, IntPtr.Zero, out _transferId);
119                 if (ret != (int)BluetoothError.None)
120                 {
121                     Log.Error(Globals.LogTag, "Failed to accept the push request, Error - " + (BluetoothError)ret);
122                 }
123                 return ret;
124             }
125             return (int)BluetoothError.NotInitialized;
126         }
127
128         internal int RejectPush()
129         {
130             if (Globals.IsOppServerInitialized)
131             {
132                 int ret = Interop.Bluetooth.OppServerRejectPush();
133                 if (ret != (int)BluetoothError.None)
134                 {
135                     Log.Error(Globals.LogTag, "Failed to reject the push request, Error - " + (BluetoothError)ret);
136                 }
137                 return ret;
138             }
139             return (int)BluetoothError.NotInitialized;
140         }
141
142         internal int CancelTransferId(int TransferId)
143         {
144             if (Globals.IsOppServerInitialized)
145             {
146                 int ret = Interop.Bluetooth.OppServerCancelTransfer(TransferId);
147                 if (ret != (int)BluetoothError.None)
148                 {
149                     Log.Error(Globals.LogTag, "Failed to cancel the transferid " + TransferId + " Error - " + (BluetoothError)ret);
150                 }
151                 return ret;
152             }
153             return (int)BluetoothError.NotInitialized;
154         }
155
156         internal int SetDestinationPath(string path)
157         {
158             if (Globals.IsOppServerInitialized)
159             {
160                 int ret = Interop.Bluetooth.OppServerSetDestinationPath(path);
161                 if (ret != (int)BluetoothError.None)
162                 {
163                     Log.Error(Globals.LogTag, "Failed to Set the desitination path " + path + " Error - " + (BluetoothError)ret);
164                 }
165                 return ret;
166             }
167             return (int)BluetoothError.NotInitialized;
168         }
169
170         internal static BluetoothOppServerImpl Instance
171         {
172             get
173             {
174                 return _instance;
175             }
176         }
177     }
178
179     internal class BluetoothOppClientImpl
180     {
181         private event EventHandler<PushRespondedEventArgs> _PushResponded;
182         private Interop.Bluetooth.PushRespondedCallback _PushRespondedCallback;
183
184         private event EventHandler<PushProgressEventArgs> _PushProgress;
185         private Interop.Bluetooth.PushProgressCallback _PushProgressCallback;
186
187         private event EventHandler<PushFinishedEventArgs> _PushFinished;
188         private Interop.Bluetooth.PushFinishedCallback _PushFinishedCallback;
189
190         private static readonly BluetoothOppClientImpl _instance = new BluetoothOppClientImpl();
191
192         internal event EventHandler<PushRespondedEventArgs> PushResponded
193         {
194             add
195             {
196                 _PushResponded += value;
197             }
198             remove
199             {
200                 //nothing to be done
201             }
202         }
203
204         internal event EventHandler<PushProgressEventArgs> PushProgress
205         {
206             add
207             {
208                 _PushProgress += value;
209             }
210             remove
211             {
212                 //nothing to be done
213             }
214         }
215
216         internal event EventHandler<PushFinishedEventArgs> PushFinished
217         {
218             add
219             {
220                 _PushFinished += value;
221             }
222             remove
223             {
224                 //nothing to be done
225             }
226         }
227
228         private BluetoothOppClientImpl()
229         {
230             Log.Info(Globals.LogTag, "Initializing OppClient");
231             Initialize();
232         }
233
234         ~BluetoothOppClientImpl()
235         {
236             Deinitialize();
237         }
238
239         internal int AddFile(string filePath)
240         {
241
242             if (Globals.IsOppClientInitialized)
243             {
244                 int ret = Interop.Bluetooth.OppClientAddFile(filePath);
245                 if (ret != (int)BluetoothError.None)
246                 {
247                     Log.Error(Globals.LogTag, "Failed to Add File, Error - " + (BluetoothError)ret);
248                 }
249                 return ret;
250             }
251             return (int)BluetoothError.NotInitialized;
252         }
253
254         internal int ClearFile()
255         {
256
257             if (Globals.IsOppClientInitialized)
258             {
259                 int ret = Interop.Bluetooth.OppClientClearFiles();
260                 if (ret != (int)BluetoothError.None)
261                 {
262                     Log.Error(Globals.LogTag, "Failed to Clear Files, Error - " + (BluetoothError)ret);
263                 }
264                 return ret;
265             }
266             return (int)BluetoothError.NotInitialized;
267         }
268
269         internal int CancelPush()
270         {
271
272             if (Globals.IsOppClientInitialized)
273             {
274                 int ret = Interop.Bluetooth.OppClientCancelPush();
275                 if (ret != (int)BluetoothError.None)
276                 {
277                     Log.Error(Globals.LogTag, "Failed to Clear Files, Error - " + (BluetoothError)ret);
278                 }
279                 return ret;
280             }
281             return (int)BluetoothError.NotInitialized;
282         }
283
284         internal int PushFile(string Destination)
285         {
286
287             if (Globals.IsOppClientInitialized)
288             {
289                 _PushRespondedCallback = (int result, string address, IntPtr userData) =>
290                 {
291                     _PushResponded?.Invoke(null, new PushRespondedEventArgs(result, address));
292                 };
293
294                 _PushProgressCallback = (string file, long size, int percent, IntPtr userData) =>
295                 {
296                     _PushProgress?.Invoke(null, new PushProgressEventArgs(file, size, percent));
297                 };
298
299                 _PushFinishedCallback = (int result, string address, IntPtr userData) =>
300                 {
301                     _PushFinished?.Invoke(null, new PushFinishedEventArgs(result, address));
302                 };
303
304                 int ret = Interop.Bluetooth.OppClientPushFile(Destination, _PushRespondedCallback, _PushProgressCallback, _PushFinishedCallback, IntPtr.Zero);
305                 if (ret != (int)BluetoothError.None)
306                 {
307                     Log.Error(Globals.LogTag, "Failed to push File, Error - " + (BluetoothError)ret);
308                 }
309                 return ret;
310             }
311             return (int)BluetoothError.NotInitialized;
312         }
313
314         private void Initialize()
315         {
316             if (Globals.IsInitialize)
317             {
318
319                 int ret = Interop.Bluetooth.InitializeOppClient();
320                 if (ret != (int)BluetoothError.None)
321                 {
322                     Log.Error(Globals.LogTag, "Failed to initialize bluetooth Opp Client, Error - " + (BluetoothError)ret);
323                     BluetoothErrorFactory.ThrowBluetoothException(ret);
324                 }
325                 else
326                 {
327                     Globals.IsOppClientInitialized = true;
328                 }
329             }
330             else
331             {
332                 Log.Error(Globals.LogTag, "Failed to initialize Opp Client, BT not initialized");
333                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotInitialized);
334             }
335         }
336
337         private void Deinitialize()
338         {
339             if (Globals.IsOppClientInitialized)
340             {
341                 int ret = Interop.Bluetooth.DeinitializeOppClient();
342                 if (ret != (int)BluetoothError.None)
343                 {
344                     Log.Error(Globals.LogTag, "Failed to deinitialize Opp Client, Error - " + (BluetoothError)ret);
345                     BluetoothErrorFactory.ThrowBluetoothException(ret);
346                 }
347                 else
348                 {
349                     Globals.IsOppClientInitialized = false;
350                 }
351             }
352         }
353
354         internal static BluetoothOppClientImpl Instance
355         {
356             get
357             {
358                 return _instance;
359             }
360         }
361     }
362 }
363