2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 namespace Tizen.Network.Bluetooth
21 internal class BluetoothOppServerImpl
23 private event EventHandler<ConnectionRequestedEventArgs> _ConnectionRequested;
24 private Interop.Bluetooth.ConnectionRequestedCallback _ConnectionRequestedCallback;
26 private event EventHandler<TransferProgressEventArgs> _TransferProgress;
27 private Interop.Bluetooth.TransferProgressCallback _TransferProgressCallback;
29 private event EventHandler<TransferFinishedEventArgs> _TransferFinished;
30 private Interop.Bluetooth.TransferFinishedCallback _TransferFinishedCallback;
32 private static readonly BluetoothOppServerImpl _instance = new BluetoothOppServerImpl();
34 internal event EventHandler<ConnectionRequestedEventArgs> ConnectionRequested
38 _ConnectionRequested += value;
46 internal event EventHandler<TransferProgressEventArgs> TransferProgress
50 _TransferProgress += value;
58 internal event EventHandler<TransferFinishedEventArgs> TransferFinished
62 _TransferFinished += value;
70 internal int StartServer(string filePath)
72 _ConnectionRequestedCallback = (string devAddress, IntPtr userData) =>
74 _ConnectionRequested?.Invoke(null, new ConnectionRequestedEventArgs(devAddress));
77 int ret = Interop.Bluetooth.InitializeOppServerCustom(filePath, _ConnectionRequestedCallback, IntPtr.Zero);
78 if (ret != (int)BluetoothError.None)
80 Log.Error(Globals.LogTag, "Failed to start bluetooth opp server, Error - " + (BluetoothError)ret);
81 BluetoothErrorFactory.ThrowBluetoothException(ret);
85 Globals.IsOppServerInitialized = true;
90 internal int StopServer()
92 if (Globals.IsOppServerInitialized)
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);
100 return (int)BluetoothError.NotInitialized;
103 internal int AcceptPush(string name, out int _transferId)
106 if (Globals.IsOppServerInitialized)
108 _TransferProgressCallback = (string file, long size, int percent, IntPtr userData) =>
110 _TransferProgress?.Invoke(null, new TransferProgressEventArgs(file, size, percent));
113 _TransferFinishedCallback = (int result, string file, long size, IntPtr userData) =>
115 _TransferFinished?.Invoke(null, new TransferFinishedEventArgs(result, file, size));
118 int ret = Interop.Bluetooth.OppServerAcceptPush(_TransferProgressCallback, _TransferFinishedCallback, name, IntPtr.Zero, out _transferId);
119 if (ret != (int)BluetoothError.None)
121 Log.Error(Globals.LogTag, "Failed to accept the push request, Error - " + (BluetoothError)ret);
125 return (int)BluetoothError.NotInitialized;
128 internal int RejectPush()
130 if (Globals.IsOppServerInitialized)
132 int ret = Interop.Bluetooth.OppServerRejectPush();
133 if (ret != (int)BluetoothError.None)
135 Log.Error(Globals.LogTag, "Failed to reject the push request, Error - " + (BluetoothError)ret);
139 return (int)BluetoothError.NotInitialized;
142 internal int CancelTransferId(int TransferId)
144 if (Globals.IsOppServerInitialized)
146 int ret = Interop.Bluetooth.OppServerCancelTransfer(TransferId);
147 if (ret != (int)BluetoothError.None)
149 Log.Error(Globals.LogTag, "Failed to cancel the transferid " + TransferId + " Error - " + (BluetoothError)ret);
153 return (int)BluetoothError.NotInitialized;
156 internal int SetDestinationPath(string path)
158 if (Globals.IsOppServerInitialized)
160 int ret = Interop.Bluetooth.OppServerSetDestinationPath(path);
161 if (ret != (int)BluetoothError.None)
163 Log.Error(Globals.LogTag, "Failed to Set the desitination path " + path + " Error - " + (BluetoothError)ret);
167 return (int)BluetoothError.NotInitialized;
170 internal static BluetoothOppServerImpl Instance
179 internal class BluetoothOppClientImpl
181 private event EventHandler<PushRespondedEventArgs> _PushResponded;
182 private Interop.Bluetooth.PushRespondedCallback _PushRespondedCallback;
184 private event EventHandler<PushProgressEventArgs> _PushProgress;
185 private Interop.Bluetooth.PushProgressCallback _PushProgressCallback;
187 private event EventHandler<PushFinishedEventArgs> _PushFinished;
188 private Interop.Bluetooth.PushFinishedCallback _PushFinishedCallback;
190 private static readonly BluetoothOppClientImpl _instance = new BluetoothOppClientImpl();
192 internal event EventHandler<PushRespondedEventArgs> PushResponded
196 _PushResponded += value;
204 internal event EventHandler<PushProgressEventArgs> PushProgress
208 _PushProgress += value;
216 internal event EventHandler<PushFinishedEventArgs> PushFinished
220 _PushFinished += value;
228 private BluetoothOppClientImpl()
230 Log.Info(Globals.LogTag, "Initializing OppClient");
234 ~BluetoothOppClientImpl()
239 internal int AddFile(string filePath)
242 if (Globals.IsOppClientInitialized)
244 int ret = Interop.Bluetooth.OppClientAddFile(filePath);
245 if (ret != (int)BluetoothError.None)
247 Log.Error(Globals.LogTag, "Failed to Add File, Error - " + (BluetoothError)ret);
251 return (int)BluetoothError.NotInitialized;
254 internal int ClearFile()
257 if (Globals.IsOppClientInitialized)
259 int ret = Interop.Bluetooth.OppClientClearFiles();
260 if (ret != (int)BluetoothError.None)
262 Log.Error(Globals.LogTag, "Failed to Clear Files, Error - " + (BluetoothError)ret);
266 return (int)BluetoothError.NotInitialized;
269 internal int CancelPush()
272 if (Globals.IsOppClientInitialized)
274 int ret = Interop.Bluetooth.OppClientCancelPush();
275 if (ret != (int)BluetoothError.None)
277 Log.Error(Globals.LogTag, "Failed to Clear Files, Error - " + (BluetoothError)ret);
281 return (int)BluetoothError.NotInitialized;
284 internal int PushFile(string Destination)
287 if (Globals.IsOppClientInitialized)
289 _PushRespondedCallback = (int result, string address, IntPtr userData) =>
291 _PushResponded?.Invoke(null, new PushRespondedEventArgs(result, address));
294 _PushProgressCallback = (string file, long size, int percent, IntPtr userData) =>
296 _PushProgress?.Invoke(null, new PushProgressEventArgs(file, size, percent));
299 _PushFinishedCallback = (int result, string address, IntPtr userData) =>
301 _PushFinished?.Invoke(null, new PushFinishedEventArgs(result, address));
304 int ret = Interop.Bluetooth.OppClientPushFile(Destination, _PushRespondedCallback, _PushProgressCallback, _PushFinishedCallback, IntPtr.Zero);
305 if (ret != (int)BluetoothError.None)
307 Log.Error(Globals.LogTag, "Failed to push File, Error - " + (BluetoothError)ret);
311 return (int)BluetoothError.NotInitialized;
314 private void Initialize()
316 if (Globals.IsInitialize)
319 int ret = Interop.Bluetooth.InitializeOppClient();
320 if (ret != (int)BluetoothError.None)
322 Log.Error(Globals.LogTag, "Failed to initialize bluetooth Opp Client, Error - " + (BluetoothError)ret);
323 BluetoothErrorFactory.ThrowBluetoothException(ret);
327 Globals.IsOppClientInitialized = true;
332 Log.Error(Globals.LogTag, "Failed to initialize Opp Client, BT not initialized");
333 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotInitialized);
337 private void Deinitialize()
339 if (Globals.IsOppClientInitialized)
341 int ret = Interop.Bluetooth.DeinitializeOppClient();
342 if (ret != (int)BluetoothError.None)
344 Log.Error(Globals.LogTag, "Failed to deinitialize Opp Client, Error - " + (BluetoothError)ret);
345 BluetoothErrorFactory.ThrowBluetoothException(ret);
349 Globals.IsOppClientInitialized = false;
354 internal static BluetoothOppClientImpl Instance