From: h.sandeep Date: Wed, 6 Sep 2017 06:46:57 +0000 (+0530) Subject: [Bluetooth][TCSACR-79] Add OPP(Object Push Profile) APIs X-Git-Tag: preview1-00160^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F66%2F147966%2F3;p=platform%2Fcore%2Fcsapi%2Ftizenfx.git [Bluetooth][TCSACR-79] Add OPP(Object Push Profile) APIs Change-Id: I38e45b25dd9ea176b0e447cc748c0352a730bbbc Signed-off-by: DoHyun Pyun Signed-off-by: Gowtham Anandha Babu --- diff --git a/src/Tizen.Network.Bluetooth/Interop/Interop.Bluetooth.cs b/src/Tizen.Network.Bluetooth/Interop/Interop.Bluetooth.cs index 87accbb..faf2040 100755 --- a/src/Tizen.Network.Bluetooth/Interop/Interop.Bluetooth.cs +++ b/src/Tizen.Network.Bluetooth/Interop/Interop.Bluetooth.cs @@ -65,6 +65,23 @@ internal static partial class Interop internal delegate void HidConnectionStateChangedCallback(int result, bool connected, string deviceAddress, IntPtr userData); [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void ConnectionRequestedCallback(string deviceAddress, IntPtr userData); + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void PushRequestedCallback(string file, long size, IntPtr userData); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void TransferProgressCallback(string file, long size, int percent, IntPtr userData); + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void TransferFinishedCallback(int result, string file, long size, IntPtr userData); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void PushRespondedCallback(int result, string deviceAddress, IntPtr userData); + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void PushProgressCallback(string file, long size, int percent, IntPtr userData); + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void PushFinishedCallback(int result, string deviceAddress, IntPtr userData); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] internal delegate void TargetConnectionStateChangedCallback(bool connected, string deviceAddress, IntPtr userData); [UnmanagedFunctionPointer(CallingConvention.Cdecl)] internal delegate void EqualizerStateChangedCallback(int equalizer, IntPtr userData); @@ -370,6 +387,48 @@ internal static partial class Interop [DllImport(Libraries.Bluetooth, EntryPoint = "bt_hid_host_disconnect")] internal static extern int Disconnect(string deviceAddress); + // Bluetooth OPP + // Opp Server + [DllImport(Libraries.Bluetooth, EntryPoint = "bt_opp_server_initialize")] + internal static extern int InitializeOppServer(string deviceAddress, PushRequestedCallback pushRequestedCb, IntPtr userData); + + [DllImport(Libraries.Bluetooth, EntryPoint = "bt_opp_server_deinitialize")] + internal static extern int DinitializeOppServer(); + + [DllImport(Libraries.Bluetooth, EntryPoint = "bt_opp_server_initialize_by_connection_request")] + internal static extern int InitializeOppServerCustom(string deviceAddress, ConnectionRequestedCallback connectionRequestedCb, IntPtr userData); + + [DllImport(Libraries.Bluetooth, EntryPoint = "bt_opp_server_accept")] + internal static extern int OppServerAcceptPush(TransferProgressCallback transferProgressCb, TransferFinishedCallback transferFinishedCb, string name, IntPtr userData, out int transferId); + + [DllImport(Libraries.Bluetooth, EntryPoint = "bt_opp_server_reject")] + internal static extern int OppServerRejectPush(); + + [DllImport(Libraries.Bluetooth, EntryPoint = "bt_opp_server_cancel_transfer")] + internal static extern int OppServerCancelTransfer(int transferId); + + [DllImport(Libraries.Bluetooth, EntryPoint = "bt_opp_server_set_destination")] + internal static extern int OppServerSetDestinationPath(string path); + + // Opp Client + [DllImport(Libraries.Bluetooth, EntryPoint = "bt_opp_client_initialize")] + internal static extern int InitializeOppClient(); + + [DllImport(Libraries.Bluetooth, EntryPoint = "bt_opp_client_dinitialize")] + internal static extern int DeinitializeOppClient(); + + [DllImport(Libraries.Bluetooth, EntryPoint = "bt_opp_client_add_file")] + internal static extern int OppClientAddFile(string filePath); + + [DllImport(Libraries.Bluetooth, EntryPoint = "bt_opp_client_clear_files")] + internal static extern int OppClientClearFiles(); + + [DllImport(Libraries.Bluetooth, EntryPoint = "bt_opp_client_cancel_push")] + internal static extern int OppClientCancelPush(); + + [DllImport(Libraries.Bluetooth, EntryPoint = "bt_opp_client_push_files")] + internal static extern int OppClientPushFile(string filePath, PushRespondedCallback pushRespondedCb, PushProgressCallback pushProgressCb, PushFinishedCallback pushFinishedCb, IntPtr userData); + //Bluetooth Avrcp [DllImport(Libraries.Bluetooth, EntryPoint = "bt_avrcp_target_initialize")] internal static extern int InitializeAvrcp(TargetConnectionStateChangedCallback targetStateChangedCb, IntPtr userData); diff --git a/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothAdapterImpl.cs b/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothAdapterImpl.cs index 7adcffe..444c461 100644 --- a/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothAdapterImpl.cs +++ b/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothAdapterImpl.cs @@ -26,6 +26,8 @@ namespace Tizen.Network.Bluetooth internal static bool IsInitialize = false; internal static bool IsAudioInitialize = false; internal static bool IsHidInitialize = false; + internal static bool IsOppServerInitialized = false; + internal static bool IsOppClientInitialized = false; } internal partial class BluetoothAdapterImpl : IDisposable diff --git a/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothDevice.cs b/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothDevice.cs index b0debee..24f1593 100644 --- a/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothDevice.cs +++ b/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothDevice.cs @@ -672,6 +672,12 @@ namespace Tizen.Network.Bluetooth profile = (hid as T); } + else if (type.Equals("Tizen.Network.Bluetooth.BluetoothOppClient")) + { + BluetoothOppClient oppClient = new BluetoothOppClient(); + profile = (oppClient as T); + } + if (profile != null) { profile.RemoteAddress = RemoteDeviceAddress; diff --git a/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothError.cs b/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothError.cs index 9f418e3..253d50b 100644 --- a/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothError.cs +++ b/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothError.cs @@ -18,8 +18,15 @@ using System; namespace Tizen.Network.Bluetooth { + /// + /// A class which is used to Throw the Bluetooth Error exceptions. + /// static internal class BluetoothErrorFactory { + /// + /// Exceptions for Bluetooth Errors. + /// + /// Thrown when the Bluetooth Error happens. static internal void ThrowBluetoothException(int exception) { BluetoothError error = (BluetoothError)exception; diff --git a/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothEventArgs.cs b/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothEventArgs.cs index c7e6740..c1a979b 100644 --- a/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothEventArgs.cs +++ b/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothEventArgs.cs @@ -1041,4 +1041,254 @@ namespace Tizen.Network.Bluetooth /// public bool Completed { get; } } + + /// + /// An extended EventArgs class which contains the connection state and address of the remote Bluetooth device. + /// + public class ConnectionRequestedEventArgs : EventArgs + { + private string _address; + + internal ConnectionRequestedEventArgs(string address) + { + _address = address; + } + + /// + /// The address. + /// + public string Address + { + get + { + return _address; + } + } + } + + /// + /// An extended EventArgs class which contains the file transfer progress state, file transfer progress by percent. + /// + public class TransferProgressEventArgs : EventArgs + { + private string _file; + private long _size; + private int _percent; + + internal TransferProgressEventArgs(string file, long size, int percent) + { + _file = file; + _size = size; + _percent = percent; + } + + /// + /// The File name. + /// + public string File + { + get + { + return _file; + } + } + + /// + /// The File size. + /// + public long Size + { + get + { + return _size; + } + } + + /// + /// The File transfer percent. + /// + public int Percent + { + get + { + return _percent; + } + } + } + + /// + /// An extended EventArgs class which contains the file transfer finished state and file state. + /// + public class TransferFinishedEventArgs : EventArgs + { + private string _file; + private long _size; + private int _result; + + internal TransferFinishedEventArgs(int result, string file, long size) + { + _file = file; + _size = size; + _result = result; + } + + /// + /// The File name. + /// + public string File + { + get + { + return _file; + } + } + + /// + /// The File size. + /// + public long Size + { + get + { + return _size; + } + } + + /// + /// The return value. + /// + public int Result + { + get + { + return _result; + } + } + } + + /// + /// An extended EventArgs class which contains the Push Request respond state + /// + + public class PushRespondedEventArgs : EventArgs + { + int _result; + string _address; + + internal PushRespondedEventArgs(int result, string address) + { + _address = address; + _result = result; + } + + /// + /// The return value. + /// + public int Result + { + get + { + return _result; + } + } + + /// + /// The address. + /// + public string Address + { + get + { + return _address; + } + } + } + + /// + /// An extended EventArgs class which contains the file push progress state, push progress by percent. + /// + public class PushProgressEventArgs : EventArgs + { + private string _file; + private long _size; + private int _percent; + + internal PushProgressEventArgs(string file, long size, int percent) + { + _file = file; + _size = size; + _percent = percent; + } + + /// + /// The File name. + /// + public string File + { + get + { + return _file; + } + } + + /// + /// The File size. + /// + public long Size + { + get + { + return _size; + } + } + + /// + /// The File transfer percent. + /// + public int Percent + { + get + { + return _percent; + } + } + } + + /// + /// An extended EventArgs class which contains the Push Request respond state + /// + + public class PushFinishedEventArgs : EventArgs + { + int _result; + string _address; + + internal PushFinishedEventArgs(int result, string address) + { + _address = address; + _result = result; + } + + /// + /// The return value. + /// + public int Result + { + get + { + return _result; + } + } + + /// + /// The address. + /// + public string Address + { + get + { + return _address; + } + } + } } diff --git a/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothOpp.cs b/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothOpp.cs new file mode 100644 index 0000000..1963510 --- /dev/null +++ b/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothOpp.cs @@ -0,0 +1,402 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; + + +namespace Tizen.Network.Bluetooth +{ + /// + /// A class which is used to handle the connection and send and receive the object over Opp profile. + /// + /// http://tizen.org/privilege/bluetooth + public class BluetoothOppServer + { + private static BluetoothOppServerImpl _impl; + private static BluetoothOppServer _instance; + + public BluetoothOppServer() + { + _impl = BluetoothOppServerImpl.Instance; + } + + /// + /// (event) ConnectionRequested is called when OPP client requests for connection. + /// + public event EventHandler ConnectionRequested + { + add + { + _impl.ConnectionRequested += value; + } + remove + { + _impl.ConnectionRequested -= value; + } + } + + /// + /// (event) TransferProgress is called when the file transfer state is changed. + /// + public event EventHandler TransferProgress + { + add + { + _impl.TransferProgress += value; + } + remove + { + _impl.TransferProgress -= value; + } + } + + /// + /// (event) TransferFinished is called when the file tranfser is completed. + /// + public event EventHandler TransferFinished + { + add + { + _impl.TransferFinished += value; + } + remove + { + _impl.TransferFinished -= value; + } + } + /// + /// Register the Opp Server with the Opp service. + /// + /// + /// The device must be bonded with remote device by CreateBond(). + /// If connection request is received from OPP Client, ConnectionRequested event will be invoked. + /// + /// Path to store the files. + /// http://tizen.org/feature/network.bluetooth.opp + /// Thrown when the required feature is not Supported. + /// Thrown when the BT/BTLE is not Supported. + /// Thrown when the BT/BTLE is not Enabled or Other Bluetooth Errors. + public static BluetoothOppServer StartServer(string FilePath) + { + if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize) + { + if (_instance == null) + { + BluetoothOppServer server = new BluetoothOppServer(); + if (server != null) + _instance = server; + } + int ret = _impl.StartServer(FilePath); + if (ret != (int)BluetoothError.None) + { + Log.Error(Globals.LogTag, "Failed to Opp Start Server - " + (BluetoothError)ret); + BluetoothErrorFactory.ThrowBluetoothException(ret); + } + return _instance; + } + else + { + BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled); + } + return null; + } + + /// + /// Stops the Opp Server. + /// + /// http://tizen.org/feature/network.bluetooth.opp + /// Thrown when the required feature is not Supported. + /// Thrown when the BT/BTLE is not Supported. + /// Thrown when the BT/BTLE is not Enabled or Other Bluetooth Errors. + public void StopServer() + { + if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize) + { + int ret = _impl.StopServer(); + if (ret != (int)BluetoothError.None) + { + Log.Error(Globals.LogTag, "Failed to Stop the Opp Server - " + (BluetoothError)ret); + BluetoothErrorFactory.ThrowBluetoothException(ret); + } + else + { + if (_instance != null) + _instance = null; + } + } + else + { + BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled); + } + } + + /// + /// Accept File Push request. + /// + /// File name to accept. + /// http://tizen.org/feature/network.bluetooth.opp + /// Thrown when the required feature is not Supported. + /// Thrown when the BT/BTLE is not Supported. + /// Thrown when the BT/BTLE is not Enabled or Other Bluetooth Errors. + public int AcceptPush(string FileName) + { + int _transitionId = -1; + if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize) + { + int ret = _impl.AcceptPush(FileName, out _transitionId); + if (ret != (int)BluetoothError.None) + { + Log.Error(Globals.LogTag, "Failed to Accept Push - " + (BluetoothError)ret); + BluetoothErrorFactory.ThrowBluetoothException(ret); + } + } + else + { + BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled); + } + return _transitionId; + } + + /// + /// Reject File Push request. + /// + /// http://tizen.org/feature/network.bluetooth.opp + /// Thrown when the required feature is not Supported. + /// Thrown when the BT/BTLE is not Supported. + /// Thrown when the BT/BTLE is not Enabled or Other Bluetooth Errors. + public void RejectPush() + { + if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize) + { + int ret = _impl.RejectPush(); + if (ret != (int)BluetoothError.None) + { + Log.Error(Globals.LogTag, "Failed to Reject Push - " + (BluetoothError)ret); + BluetoothErrorFactory.ThrowBluetoothException(ret); + } + } + else + { + BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled); + } + } + + /// + /// Cancel the ongoing transfer session. + /// + /// tranfer ID. + /// http://tizen.org/feature/network.bluetooth.opp + /// Thrown when the required feature is not Supported. + /// Thrown when the BT/BTLE is not Supported. + /// Thrown when the BT/BTLE is not Enabled or Other Bluetooth Errors. + public void CancelTransfer(int TransferId) + { + if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize) + { + int ret = _impl.CancelTransferId(TransferId); + if (ret != (int)BluetoothError.None) + { + Log.Error(Globals.LogTag, "Failed to Cancel Transfer - " + (BluetoothError)ret); + BluetoothErrorFactory.ThrowBluetoothException(ret); + } + } + else + { + BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled); + } + } + + /// + /// Cancel the ongoing transfer session. + /// + /// Path to store the files. + /// http://tizen.org/feature/network.bluetooth.opp + /// Thrown when the required feature is not Supported. + /// Thrown when the BT/BTLE is not Supported. + /// Thrown when the BT/BTLE is not Enabled or Other Bluetooth Errors. + public void SetDestinationPath(string FilePath) + { + if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize) + { + int ret = _impl.SetDestinationPath(FilePath); + if (ret != (int)BluetoothError.None) + { + Log.Error(Globals.LogTag, "Failed to Set Destination Path - " + (BluetoothError)ret); + BluetoothErrorFactory.ThrowBluetoothException(ret); + } + } + else + { + BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled); + } + } + } + + /// + /// A class which is used to handle the connection and send and receive the object over Opp profile. + /// + /// http://tizen.org/privilege/bluetooth + public class BluetoothOppClient : BluetoothProfile + { + internal BluetoothOppClient() + { + } + + /// + /// (event) PushResponded is called when remote OPP Server responds to a File push request. + /// + public event EventHandler PushResponded + { + add + { + BluetoothOppClientImpl.Instance.PushResponded += value; + } + remove + { + BluetoothOppClientImpl.Instance.PushResponded -= value; + } + } + + /// + /// (event) PushProgress is called when the file transfer state is changed. + /// + public event EventHandler PushProgress + { + add + { + BluetoothOppClientImpl.Instance.PushProgress += value; + } + remove + { + BluetoothOppClientImpl.Instance.PushProgress -= value; + } + } + + /// + /// (event) PushFinished is called when the file tranfser is completed. + /// + public event EventHandler PushFinished + { + add + { + BluetoothOppClientImpl.Instance.PushFinished += value; + } + remove + { + BluetoothOppClientImpl.Instance.PushFinished -= value; + } + } + + /// + /// Add File path to be pushed. + /// + /// file for sending. + /// http://tizen.org/feature/network.bluetooth.opp + /// Thrown when the required feature is not Supported. + /// Thrown when the BT/BTLE is not Supported. + /// Thrown when the BT/BTLE is not Enabled or Other Bluetooth Errors. + public void AddFile(string FilePath) + { + if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize) + { + int ret = BluetoothOppClientImpl.Instance.AddFile(FilePath); + if (ret != (int)BluetoothError.None) + { + Log.Error(Globals.LogTag, "Failed to Set File Path - " + (BluetoothError)ret); + BluetoothErrorFactory.ThrowBluetoothException(ret); + } + } + else + { + BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled); + } + } + + /// + /// Clears all the File paths. + /// + /// http://tizen.org/feature/network.bluetooth.opp + /// Thrown when the required feature is not Supported. + /// Thrown when the BT/BTLE is not Supported. + /// Thrown when the BT/BTLE is not Enabled or Other Bluetooth Errors. + public void ClearFiles() + { + if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize) + { + int ret = BluetoothOppClientImpl.Instance.ClearFile(); + if (ret != (int)BluetoothError.None) + { + Log.Error(Globals.LogTag, "Failed to Clear the Files - " + (BluetoothError)ret); + BluetoothErrorFactory.ThrowBluetoothException(ret); + } + } + else + { + BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled); + } + } + + /// + /// Cancels the ongoing push session. + /// + /// http://tizen.org/feature/network.bluetooth.opp + /// Thrown when the required feature is not Supported. + /// Thrown when the BT/BTLE is not Supported. + /// Thrown when the BT/BTLE is not Enabled or Other Bluetooth Errors. + public void CancelPush() + { + if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize) + { + int ret = BluetoothOppClientImpl.Instance.CancelPush(); + if (ret != (int)BluetoothError.None) + { + Log.Error(Globals.LogTag, "Failed to Cancel Push Operation - " + (BluetoothError)ret); + BluetoothErrorFactory.ThrowBluetoothException(ret); + } + } + else + { + BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled); + } + } + + /// + /// Pushes the file set through AddFile. + /// + /// destination device address. + /// http://tizen.org/feature/network.bluetooth.opp + /// Thrown when the required feature is not Supported. + /// Thrown when the BT/BTLE is not Supported. + /// Thrown when the BT/BTLE is not Enabled or Other Bluetooth Errors. + public void PushFile(string Destination) + { + if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize) + { + int ret = BluetoothOppClientImpl.Instance.PushFile(Destination); + if (ret != (int)BluetoothError.None) + { + Log.Error(Globals.LogTag, "Failed to Cancel Push Operation - " + (BluetoothError)ret); + BluetoothErrorFactory.ThrowBluetoothException(ret); + } + } + else + { + BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled); + } + } + } +} + diff --git a/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothOppImpl.cs b/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothOppImpl.cs new file mode 100644 index 0000000..a2fefad --- /dev/null +++ b/src/Tizen.Network.Bluetooth/Tizen.Network.Bluetooth/BluetoothOppImpl.cs @@ -0,0 +1,363 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; + +namespace Tizen.Network.Bluetooth +{ + internal class BluetoothOppServerImpl + { + private event EventHandler _ConnectionRequested; + private Interop.Bluetooth.ConnectionRequestedCallback _ConnectionRequestedCallback; + + private event EventHandler _TransferProgress; + private Interop.Bluetooth.TransferProgressCallback _TransferProgressCallback; + + private event EventHandler _TransferFinished; + private Interop.Bluetooth.TransferFinishedCallback _TransferFinishedCallback; + + private static readonly BluetoothOppServerImpl _instance = new BluetoothOppServerImpl(); + + internal event EventHandler ConnectionRequested + { + add + { + _ConnectionRequested += value; + } + remove + { + //nothing to be done + } + } + + internal event EventHandler TransferProgress + { + add + { + _TransferProgress += value; + } + remove + { + //nothing to be done + } + } + + internal event EventHandler TransferFinished + { + add + { + _TransferFinished += value; + } + remove + { + //nothing to be done + } + } + + internal int StartServer(string filePath) + { + _ConnectionRequestedCallback = (string devAddress, IntPtr userData) => + { + _ConnectionRequested?.Invoke(null, new ConnectionRequestedEventArgs(devAddress)); + }; + + int ret = Interop.Bluetooth.InitializeOppServerCustom(filePath, _ConnectionRequestedCallback, IntPtr.Zero); + if (ret != (int)BluetoothError.None) + { + Log.Error(Globals.LogTag, "Failed to start bluetooth opp server, Error - " + (BluetoothError)ret); + BluetoothErrorFactory.ThrowBluetoothException(ret); + } + else + { + Globals.IsOppServerInitialized = true; + } + return ret; + } + + internal int StopServer() + { + if (Globals.IsOppServerInitialized) + { + int ret = Interop.Bluetooth.DinitializeOppServer(); + if (ret != (int)BluetoothError.None) { + Log.Error (Globals.LogTag, "Failed to stop bluetooth opp server, Error - " + (BluetoothError)ret); + } + return ret; + } + return (int)BluetoothError.NotInitialized; + } + + internal int AcceptPush(string name, out int _transferId) + { + _transferId = -1; + if (Globals.IsOppServerInitialized) + { + _TransferProgressCallback = (string file, long size, int percent, IntPtr userData) => + { + _TransferProgress?.Invoke(null, new TransferProgressEventArgs(file, size, percent)); + }; + + _TransferFinishedCallback = (int result, string file, long size, IntPtr userData) => + { + _TransferFinished?.Invoke(null, new TransferFinishedEventArgs(result, file, size)); + }; + + int ret = Interop.Bluetooth.OppServerAcceptPush(_TransferProgressCallback, _TransferFinishedCallback, name, IntPtr.Zero, out _transferId); + if (ret != (int)BluetoothError.None) + { + Log.Error(Globals.LogTag, "Failed to accept the push request, Error - " + (BluetoothError)ret); + } + return ret; + } + return (int)BluetoothError.NotInitialized; + } + + internal int RejectPush() + { + if (Globals.IsOppServerInitialized) + { + int ret = Interop.Bluetooth.OppServerRejectPush(); + if (ret != (int)BluetoothError.None) + { + Log.Error(Globals.LogTag, "Failed to reject the push request, Error - " + (BluetoothError)ret); + } + return ret; + } + return (int)BluetoothError.NotInitialized; + } + + internal int CancelTransferId(int TransferId) + { + if (Globals.IsOppServerInitialized) + { + int ret = Interop.Bluetooth.OppServerCancelTransfer(TransferId); + if (ret != (int)BluetoothError.None) + { + Log.Error(Globals.LogTag, "Failed to cancel the transferid " + TransferId + " Error - " + (BluetoothError)ret); + } + return ret; + } + return (int)BluetoothError.NotInitialized; + } + + internal int SetDestinationPath(string path) + { + if (Globals.IsOppServerInitialized) + { + int ret = Interop.Bluetooth.OppServerSetDestinationPath(path); + if (ret != (int)BluetoothError.None) + { + Log.Error(Globals.LogTag, "Failed to Set the desitination path " + path + " Error - " + (BluetoothError)ret); + } + return ret; + } + return (int)BluetoothError.NotInitialized; + } + + internal static BluetoothOppServerImpl Instance + { + get + { + return _instance; + } + } + } + + internal class BluetoothOppClientImpl + { + private event EventHandler _PushResponded; + private Interop.Bluetooth.PushRespondedCallback _PushRespondedCallback; + + private event EventHandler _PushProgress; + private Interop.Bluetooth.PushProgressCallback _PushProgressCallback; + + private event EventHandler _PushFinished; + private Interop.Bluetooth.PushFinishedCallback _PushFinishedCallback; + + private static readonly BluetoothOppClientImpl _instance = new BluetoothOppClientImpl(); + + internal event EventHandler PushResponded + { + add + { + _PushResponded += value; + } + remove + { + //nothing to be done + } + } + + internal event EventHandler PushProgress + { + add + { + _PushProgress += value; + } + remove + { + //nothing to be done + } + } + + internal event EventHandler PushFinished + { + add + { + _PushFinished += value; + } + remove + { + //nothing to be done + } + } + + private BluetoothOppClientImpl() + { + Log.Info(Globals.LogTag, "Initializing OppClient"); + initialize(); + } + + ~BluetoothOppClientImpl() + { + deinitialize(); + } + + internal int AddFile(string filePath) + { + + if (Globals.IsOppClientInitialized) + { + int ret = Interop.Bluetooth.OppClientAddFile(filePath); + if (ret != (int)BluetoothError.None) + { + Log.Error(Globals.LogTag, "Failed to Add File, Error - " + (BluetoothError)ret); + } + return ret; + } + return (int)BluetoothError.NotInitialized; + } + + internal int ClearFile() + { + + if (Globals.IsOppClientInitialized) + { + int ret = Interop.Bluetooth.OppClientClearFiles(); + if (ret != (int)BluetoothError.None) + { + Log.Error(Globals.LogTag, "Failed to Clear Files, Error - " + (BluetoothError)ret); + } + return ret; + } + return (int)BluetoothError.NotInitialized; + } + + internal int CancelPush() + { + + if (Globals.IsOppClientInitialized) + { + int ret = Interop.Bluetooth.OppClientCancelPush(); + if (ret != (int)BluetoothError.None) + { + Log.Error(Globals.LogTag, "Failed to Clear Files, Error - " + (BluetoothError)ret); + } + return ret; + } + return (int)BluetoothError.NotInitialized; + } + + internal int PushFile(string Destination) + { + + if (Globals.IsOppClientInitialized) + { + _PushRespondedCallback = (int result, string address, IntPtr userData) => + { + _PushResponded?.Invoke(null, new PushRespondedEventArgs(result, address)); + }; + + _PushProgressCallback = (string file, long size, int percent, IntPtr userData) => + { + _PushProgress?.Invoke(null, new PushProgressEventArgs(file, size, percent)); + }; + + _PushFinishedCallback = (int result, string address, IntPtr userData) => + { + _PushFinished?.Invoke(null, new PushFinishedEventArgs(result, address)); + }; + + int ret = Interop.Bluetooth.OppClientPushFile(Destination, _PushRespondedCallback, _PushProgressCallback, _PushFinishedCallback, IntPtr.Zero); + if (ret != (int)BluetoothError.None) + { + Log.Error(Globals.LogTag, "Failed to push File, Error - " + (BluetoothError)ret); + } + return ret; + } + return (int)BluetoothError.NotInitialized; + } + + private void initialize() + { + if (Globals.IsInitialize) + { + + int ret = Interop.Bluetooth.InitializeOppClient(); + if (ret != (int)BluetoothError.None) + { + Log.Error(Globals.LogTag, "Failed to initialize bluetooth Opp Client, Error - " + (BluetoothError)ret); + BluetoothErrorFactory.ThrowBluetoothException(ret); + } + else + { + Globals.IsOppClientInitialized = true; + } + } + else + { + Log.Error(Globals.LogTag, "Failed to initialize Opp Client, BT not initialized"); + BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotInitialized); + } + } + + private void deinitialize() + { + if (Globals.IsOppClientInitialized) + { + int ret = Interop.Bluetooth.DeinitializeOppClient(); + if (ret != (int)BluetoothError.None) + { + Log.Error(Globals.LogTag, "Failed to deinitialize Opp Client, Error - " + (BluetoothError)ret); + BluetoothErrorFactory.ThrowBluetoothException(ret); + } + else + { + Globals.IsOppClientInitialized = false; + } + } + } + + internal static BluetoothOppClientImpl Instance + { + get + { + return _instance; + } + } + } +} +