30d649e89814738b3b7f0e4737fc5299a77c73bf
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Bluetooth / Tizen.Network.Bluetooth / BluetoothOpp.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
20 namespace Tizen.Network.Bluetooth
21 {
22     /// <summary>
23     /// A class which is used to handle the connection and send and receive the object over Opp profile.
24     /// </summary>
25     /// <privilege> http://tizen.org/privilege/bluetooth </privilege>
26     public class BluetoothOppServer
27     {
28         private static BluetoothOppServerImpl _impl;
29         private static BluetoothOppServer _instance;
30
31         public BluetoothOppServer()
32         {
33             _impl = BluetoothOppServerImpl.Instance;
34         }
35
36         /// <summary>
37         /// (event) ConnectionRequested is called when OPP client requests for connection.
38         /// </summary>
39         public event EventHandler<ConnectionRequestedEventArgs> ConnectionRequested
40         {
41             add
42             {
43                 _impl.ConnectionRequested += value;
44             }
45             remove
46             {
47                 _impl.ConnectionRequested -= value;
48             }
49         }
50
51         /// <summary>
52         /// (event) TransferProgress is called when the file transfer state is changed.
53         /// </summary>
54         public event EventHandler<TransferProgressEventArgs> TransferProgress
55         {
56             add
57             {
58                 _impl.TransferProgress += value;
59             }
60             remove
61             {
62                 _impl.TransferProgress -= value;
63             }
64         }
65
66         /// <summary>
67         /// (event) TransferFinished is called when the file tranfser is completed.
68         /// </summary>
69         public event EventHandler<TransferFinishedEventArgs> TransferFinished
70         {
71             add
72             {
73                 _impl.TransferFinished += value;
74             }
75             remove
76             {
77                 _impl.TransferFinished -= value;
78             }
79         }
80         /// <summary>
81         /// Register the Opp Server with the Opp service.
82         /// </summary>
83         /// <remarks>
84         /// The device must be bonded with remote device by CreateBond().
85         /// If connection request is received from OPP Client, ConnectionRequested event will be invoked.
86         /// </remarks>
87         /// <param name="FilePath"> Path to store the files.</param>
88         /// <feature>http://tizen.org/feature/network.bluetooth.opp</feature>
89         /// <exception cref="NotSupportedException">Thrown when the required feature is not Supported.</exception>
90         /// <exception cref="NotSupportedException">Thrown when the BT/BTLE is not Supported.</exception>
91         /// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not Enabled or Other Bluetooth Errors.</exception>
92         public static BluetoothOppServer StartServer(string FilePath)
93         {
94             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
95             {
96                 if (_instance == null)
97                 {
98                     BluetoothOppServer server = new BluetoothOppServer();
99                     if (server != null)
100                         _instance = server;
101                 }
102                 int ret = _impl.StartServer(FilePath);
103                 if (ret != (int)BluetoothError.None)
104                 {
105                     Log.Error(Globals.LogTag, "Failed to Opp Start Server - " + (BluetoothError)ret);
106                     BluetoothErrorFactory.ThrowBluetoothException(ret);
107                 }
108                 return _instance;
109             }
110             else
111             {
112                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
113             }
114             return null;
115         }
116
117         /// <summary>
118         /// Stops the Opp Server.
119         /// </summary>
120         /// <feature>http://tizen.org/feature/network.bluetooth.opp</feature>
121         /// <exception cref="NotSupportedException">Thrown when the required feature is not Supported.</exception>
122         /// <exception cref="NotSupportedException">Thrown when the BT/BTLE is not Supported.</exception>
123         /// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not Enabled or Other Bluetooth Errors.</exception>
124         public void StopServer()
125         {
126             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
127             {
128                 int ret = _impl.StopServer();
129                 if (ret != (int)BluetoothError.None)
130                 {
131                     Log.Error(Globals.LogTag, "Failed to Stop the Opp Server - " + (BluetoothError)ret);
132                     BluetoothErrorFactory.ThrowBluetoothException(ret);
133                 }
134                 else
135                 {
136                     if (_instance != null)
137                         _instance = null;
138                 }
139             }
140             else
141             {
142                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
143             }
144         }
145
146         /// <summary>
147         /// Accept File Push request.
148         /// </summary>
149         /// <param name="FileName"> File name to accept.</param>
150         /// <feature>http://tizen.org/feature/network.bluetooth.opp</feature>
151         /// <exception cref="NotSupportedException">Thrown when the required feature is not Supported.</exception>
152         /// <exception cref="NotSupportedException">Thrown when the BT/BTLE is not Supported.</exception>
153         /// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not Enabled or Other Bluetooth Errors.</exception>
154         public int AcceptPush(string FileName)
155         {
156             int _transitionId = -1;
157             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
158             {
159                 int ret = _impl.AcceptPush(FileName, out _transitionId);
160                 if (ret != (int)BluetoothError.None)
161                 {
162                     Log.Error(Globals.LogTag, "Failed to Accept Push - " + (BluetoothError)ret);
163                     BluetoothErrorFactory.ThrowBluetoothException(ret);
164                 }
165             }
166             else
167             {
168                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
169             }
170             return _transitionId;
171         }
172
173         /// <summary>
174         /// Reject File Push request.
175         /// </summary>
176         /// <feature>http://tizen.org/feature/network.bluetooth.opp</feature>
177         /// <exception cref="NotSupportedException">Thrown when the required feature is not Supported.</exception>
178         /// <exception cref="NotSupportedException">Thrown when the BT/BTLE is not Supported.</exception>
179         /// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not Enabled or Other Bluetooth Errors.</exception>
180         public void RejectPush()
181         {
182             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
183             {
184                 int ret = _impl.RejectPush();
185                 if (ret != (int)BluetoothError.None)
186                 {
187                     Log.Error(Globals.LogTag, "Failed to Reject Push - " + (BluetoothError)ret);
188                     BluetoothErrorFactory.ThrowBluetoothException(ret);
189                 }
190             }
191             else
192             {
193                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
194             }
195         }
196
197         /// <summary>
198         /// Cancel the ongoing transfer session.
199         /// </summary>
200         /// <param name="TransferId"> tranfer ID.</param>
201         /// <feature>http://tizen.org/feature/network.bluetooth.opp</feature>
202         /// <exception cref="NotSupportedException">Thrown when the required feature is not Supported.</exception>
203         /// <exception cref="NotSupportedException">Thrown when the BT/BTLE is not Supported.</exception>
204         /// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not Enabled or Other Bluetooth Errors.</exception>
205         public void CancelTransfer(int TransferId)
206         {
207             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
208             {
209                 int ret = _impl.CancelTransferId(TransferId);
210                 if (ret != (int)BluetoothError.None)
211                 {
212                     Log.Error(Globals.LogTag, "Failed to Cancel Transfer - " + (BluetoothError)ret);
213                     BluetoothErrorFactory.ThrowBluetoothException(ret);
214                 }
215             }
216             else
217             {
218                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
219             }
220         }
221
222         /// <summary>
223         /// Cancel the ongoing transfer session.
224         /// </summary>
225         /// <param name="FilePath"> Path to store the files.</param>
226         /// <feature>http://tizen.org/feature/network.bluetooth.opp</feature>
227         /// <exception cref="NotSupportedException">Thrown when the required feature is not Supported.</exception>
228         /// <exception cref="NotSupportedException">Thrown when the BT/BTLE is not Supported.</exception>
229         /// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not Enabled or Other Bluetooth Errors.</exception>
230         public void SetDestinationPath(string FilePath)
231         {
232             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
233             {
234                 int ret = _impl.SetDestinationPath(FilePath);
235                 if (ret != (int)BluetoothError.None)
236                 {
237                     Log.Error(Globals.LogTag, "Failed to Set Destination Path - " + (BluetoothError)ret);
238                     BluetoothErrorFactory.ThrowBluetoothException(ret);
239                 }
240             }
241             else
242             {
243                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
244             }
245         }
246     }
247
248     /// <summary>
249     /// A class which is used to handle the connection and send and receive the object over Opp profile.
250     /// </summary>
251     /// <privilege> http://tizen.org/privilege/bluetooth </privilege>
252     public class BluetoothOppClient : BluetoothProfile
253     {
254         internal BluetoothOppClient()
255         {
256         }
257
258         /// <summary>
259         /// (event) PushResponded is called when remote OPP Server responds to a File push request.
260         /// </summary>
261         public event EventHandler<PushRespondedEventArgs> PushResponded
262         {
263             add
264             {
265                 BluetoothOppClientImpl.Instance.PushResponded += value;
266             }
267             remove
268             {
269                 BluetoothOppClientImpl.Instance.PushResponded -= value;
270             }
271         }
272
273         /// <summary>
274         /// (event) PushProgress is called when the file transfer state is changed.
275         /// </summary>
276         public event EventHandler<PushProgressEventArgs> PushProgress
277         {
278             add
279             {
280                 BluetoothOppClientImpl.Instance.PushProgress += value;
281             }
282             remove
283             {
284                 BluetoothOppClientImpl.Instance.PushProgress -= value;
285             }
286         }
287
288         /// <summary>
289         /// (event) PushFinished is called when the file tranfser is completed.
290         /// </summary>
291         public event EventHandler<PushFinishedEventArgs> PushFinished
292         {
293             add
294             {
295                 BluetoothOppClientImpl.Instance.PushFinished += value;
296             }
297             remove
298             {
299                 BluetoothOppClientImpl.Instance.PushFinished -= value;
300             }
301         }
302
303         /// <summary>
304         /// Add File path to be pushed.
305         /// </summary>
306         /// <param name="FilePath"> file for sending.</param>
307         /// <feature>http://tizen.org/feature/network.bluetooth.opp</feature>
308         /// <exception cref="NotSupportedException">Thrown when the required feature is not Supported.</exception>
309         /// <exception cref="NotSupportedException">Thrown when the BT/BTLE is not Supported.</exception>
310         /// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not Enabled or Other Bluetooth Errors.</exception>
311         public void AddFile(string FilePath)
312         {
313             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
314             {
315                 int ret = BluetoothOppClientImpl.Instance.AddFile(FilePath);
316                 if (ret != (int)BluetoothError.None)
317                 {
318                     Log.Error(Globals.LogTag, "Failed to Set File Path - " + (BluetoothError)ret);
319                     BluetoothErrorFactory.ThrowBluetoothException(ret);
320                 }
321             }
322             else
323             {
324                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
325             }
326         }
327
328         /// <summary>
329         /// Clears all the File paths.
330         /// </summary>
331         /// <feature>http://tizen.org/feature/network.bluetooth.opp</feature>
332         /// <exception cref="NotSupportedException">Thrown when the required feature is not Supported.</exception>
333         /// <exception cref="NotSupportedException">Thrown when the BT/BTLE is not Supported.</exception>
334         /// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not Enabled or Other Bluetooth Errors.</exception>
335         public void ClearFiles()
336         {
337             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
338             {
339                 int ret = BluetoothOppClientImpl.Instance.ClearFile();
340                 if (ret != (int)BluetoothError.None)
341                 {
342                     Log.Error(Globals.LogTag, "Failed to Clear the Files - " + (BluetoothError)ret);
343                     BluetoothErrorFactory.ThrowBluetoothException(ret);
344                 }
345             }
346             else
347             {
348                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
349             }
350         }
351
352         /// <summary>
353         /// Cancels the ongoing push session.
354         /// </summary>
355         /// <feature>http://tizen.org/feature/network.bluetooth.opp</feature>
356         /// <exception cref="NotSupportedException">Thrown when the required feature is not Supported.</exception>
357         /// <exception cref="NotSupportedException">Thrown when the BT/BTLE is not Supported.</exception>
358         /// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not Enabled or Other Bluetooth Errors.</exception>
359         public void CancelPush()
360         {
361             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
362             {
363                 int ret = BluetoothOppClientImpl.Instance.CancelPush();
364                 if (ret != (int)BluetoothError.None)
365                 {
366                     Log.Error(Globals.LogTag, "Failed to Cancel Push Operation - " + (BluetoothError)ret);
367                     BluetoothErrorFactory.ThrowBluetoothException(ret);
368                 }
369             }
370             else
371             {
372                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
373             }
374         }
375
376         /// <summary>
377         /// Pushes the file set through AddFile.
378         /// </summary>
379         /// <param name="Destination"> destination device address.</param>
380         /// <feature>http://tizen.org/feature/network.bluetooth.opp</feature>
381         /// <exception cref="NotSupportedException">Thrown when the required feature is not Supported.</exception>
382         /// <exception cref="NotSupportedException">Thrown when the BT/BTLE is not Supported.</exception>
383         /// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not Enabled or Other Bluetooth Errors.</exception>
384         public void PushFile(string Destination)
385         {
386             if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize)
387             {
388                 int ret = BluetoothOppClientImpl.Instance.PushFile(Destination);
389                 if (ret != (int)BluetoothError.None)
390                 {
391                     Log.Error(Globals.LogTag, "Failed to Cancel Push Operation - " + (BluetoothError)ret);
392                     BluetoothErrorFactory.ThrowBluetoothException(ret);
393                 }
394             }
395             else
396             {
397                 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
398             }
399         }
400     }
401 }
402