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