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