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 static readonly BluetoothOppServerImpl _instance = new BluetoothOppServerImpl();
25 internal event EventHandler<ConnectionRequestedEventArgs> ConnectionRequested;
27 internal event EventHandler<TransferProgressEventArgs> TransferProgress;
29 internal event EventHandler<TransferFinishedEventArgs> TransferFinished;
31 internal int StartServer(string filePath)
33 Interop.Bluetooth.ConnectionRequestedCallback _ConnectionRequestedCallback = (string devAddress, IntPtr userData) =>
35 ConnectionRequested?.Invoke(this, new ConnectionRequestedEventArgs(devAddress));
38 int ret = Interop.Bluetooth.InitializeOppServerCustom(filePath, _ConnectionRequestedCallback, IntPtr.Zero);
39 if (ret != (int)BluetoothError.None)
41 Log.Error(Globals.LogTag, "Failed to start bluetooth opp server, Error - " + (BluetoothError)ret);
42 BluetoothErrorFactory.ThrowBluetoothException(ret);
46 Globals.IsOppServerInitialized = true;
51 internal int StopServer()
53 if (Globals.IsOppServerInitialized)
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);
61 return (int)BluetoothError.NotInitialized;
64 internal int AcceptPush(string name, out int _transferId)
67 if (Globals.IsOppServerInitialized)
69 Interop.Bluetooth.TransferProgressCallback _TransferProgressCallback = (string file, long size, int percent, IntPtr userData) =>
71 TransferProgress?.Invoke(this, new TransferProgressEventArgs(file, size, percent));
74 Interop.Bluetooth.TransferFinishedCallback _TransferFinishedCallback = (int result, string file, long size, IntPtr userData) =>
76 TransferFinished?.Invoke(this, new TransferFinishedEventArgs(result, file, size));
79 int ret = Interop.Bluetooth.OppServerAcceptPush(_TransferProgressCallback, _TransferFinishedCallback, name, IntPtr.Zero, out _transferId);
80 if (ret != (int)BluetoothError.None)
82 Log.Error(Globals.LogTag, "Failed to accept the push request, Error - " + (BluetoothError)ret);
86 return (int)BluetoothError.NotInitialized;
89 internal int RejectPush()
91 if (Globals.IsOppServerInitialized)
93 int ret = Interop.Bluetooth.OppServerRejectPush();
94 if (ret != (int)BluetoothError.None)
96 Log.Error(Globals.LogTag, "Failed to reject the push request, Error - " + (BluetoothError)ret);
100 return (int)BluetoothError.NotInitialized;
103 internal int CancelTransferId(int TransferId)
105 if (Globals.IsOppServerInitialized)
107 int ret = Interop.Bluetooth.OppServerCancelTransfer(TransferId);
108 if (ret != (int)BluetoothError.None)
110 Log.Error(Globals.LogTag, "Failed to cancel the transferid " + TransferId + " Error - " + (BluetoothError)ret);
114 return (int)BluetoothError.NotInitialized;
117 internal int SetDestinationPath(string path)
119 if (Globals.IsOppServerInitialized)
121 int ret = Interop.Bluetooth.OppServerSetDestinationPath(path);
122 if (ret != (int)BluetoothError.None)
124 Log.Error(Globals.LogTag, "Failed to Set the desitination path " + path + " Error - " + (BluetoothError)ret);
128 return (int)BluetoothError.NotInitialized;
131 internal static BluetoothOppServerImpl Instance
140 internal class BluetoothOppClientImpl
142 private static readonly BluetoothOppClientImpl _instance = new BluetoothOppClientImpl();
144 internal event EventHandler<PushRespondedEventArgs> PushResponded;
146 internal event EventHandler<PushProgressEventArgs> PushProgress;
148 internal event EventHandler<PushFinishedEventArgs> PushFinished;
150 private BluetoothOppClientImpl()
152 Log.Info(Globals.LogTag, "Initializing OppClient");
156 ~BluetoothOppClientImpl()
161 internal int AddFile(string filePath)
164 if (Globals.IsOppClientInitialized)
166 int ret = Interop.Bluetooth.OppClientAddFile(filePath);
167 if (ret != (int)BluetoothError.None)
169 Log.Error(Globals.LogTag, "Failed to Add File, Error - " + (BluetoothError)ret);
173 return (int)BluetoothError.NotInitialized;
176 internal int ClearFile()
179 if (Globals.IsOppClientInitialized)
181 int ret = Interop.Bluetooth.OppClientClearFiles();
182 if (ret != (int)BluetoothError.None)
184 Log.Error(Globals.LogTag, "Failed to Clear Files, Error - " + (BluetoothError)ret);
188 return (int)BluetoothError.NotInitialized;
191 internal int CancelPush()
194 if (Globals.IsOppClientInitialized)
196 int ret = Interop.Bluetooth.OppClientCancelPush();
197 if (ret != (int)BluetoothError.None)
199 Log.Error(Globals.LogTag, "Failed to Clear Files, Error - " + (BluetoothError)ret);
203 return (int)BluetoothError.NotInitialized;
206 internal int PushFile(string Destination)
209 if (Globals.IsOppClientInitialized)
211 Interop.Bluetooth.PushRespondedCallback _PushRespondedCallback = (int result, string address, IntPtr userData) =>
213 PushResponded?.Invoke(this, new PushRespondedEventArgs(result, address));
216 Interop.Bluetooth.PushProgressCallback _PushProgressCallback = (string file, long size, int percent, IntPtr userData) =>
218 PushProgress?.Invoke(this, new PushProgressEventArgs(file, size, percent));
221 Interop.Bluetooth.PushFinishedCallback _PushFinishedCallback = (int result, string address, IntPtr userData) =>
223 PushFinished?.Invoke(this, new PushFinishedEventArgs(result, address));
226 int ret = Interop.Bluetooth.OppClientPushFile(Destination, _PushRespondedCallback, _PushProgressCallback, _PushFinishedCallback, IntPtr.Zero);
227 if (ret != (int)BluetoothError.None)
229 Log.Error(Globals.LogTag, "Failed to push File, Error - " + (BluetoothError)ret);
233 return (int)BluetoothError.NotInitialized;
236 private void Initialize()
238 if (Globals.IsInitialize)
241 int ret = Interop.Bluetooth.InitializeOppClient();
242 if (ret != (int)BluetoothError.None)
244 Log.Error(Globals.LogTag, "Failed to initialize bluetooth Opp Client, Error - " + (BluetoothError)ret);
245 BluetoothErrorFactory.ThrowBluetoothException(ret);
249 Globals.IsOppClientInitialized = true;
254 Log.Error(Globals.LogTag, "Failed to initialize Opp Client, BT not initialized");
255 BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotInitialized);
259 private void Deinitialize()
261 if (Globals.IsOppClientInitialized)
263 int ret = Interop.Bluetooth.DeinitializeOppClient();
264 if (ret != (int)BluetoothError.None)
266 Log.Error(Globals.LogTag, "Failed to deinitialize Opp Client, Error - " + (BluetoothError)ret);
267 BluetoothErrorFactory.ThrowBluetoothException(ret);
271 Globals.IsOppClientInitialized = false;
276 internal static BluetoothOppClientImpl Instance