2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
18 * @file FNetBt_BluetoothSppSystemAdapter.h
19 * @brief This is the header file for the _BluetoothSppSystemAdapter class.
21 * This header file contains the declaration of the _BluetoothSppSystemAdapter class.
23 #ifndef _FNET_BT_INTERNAL_BLUETOOTH_SPP_SYSTEM_ADAPTER_H_
24 #define _FNET_BT_INTERNAL_BLUETOOTH_SPP_SYSTEM_ADAPTER_H_
26 #include <unique_ptr.h>
27 #include <bluetooth.h>
28 #include <FBaseObject.h>
29 #include <FBaseResult.h>
30 #include <FNetBtBluetoothTypes.h>
32 static const int _BT_INVALID_SOCKET_FD = -1;
34 // forward declarations
35 namespace Tizen { namespace Base {
40 namespace Collection {
42 template<class keyType, class valueType> class HashMapT;
46 namespace Tizen { namespace Net { namespace Bluetooth
49 // forward declarations
50 class BluetoothDevice;
51 class _IBluetoothSocketListener;
54 * @class _BluetoothSppSystemAdapter
55 * @brief This class provides functions related to RFCOMM socket (SPP) and have a role of adaptation OSP classes and the
56 * underlying subsystem. This class is a type of singleton, therefore only one instance should be allowed.
58 class _BluetoothSppSystemAdapter
59 : public Tizen::Base::Object
63 * Creates an instance of this class if there is no instance in this application context.
64 * Gets the exist instance because this class is a singleton, otherwise.
66 * @return the instance of this class
68 static _BluetoothSppSystemAdapter* GetInstance(void);
71 //-----------------------------------------------------------------------------------------------------------------
73 //-----------------------------------------------------------------------------------------------------------------
76 * Create and initialize a server socket, and start to listen the socket events. @n
77 * An instance of _IBluetoothSocketListener class is registered internally.
79 * @return An error code
80 * @param[in] serviceUuid The service UUID
81 * @param[in] listener The instance of _IBluetoothSocketListener to be registered
82 * @param[out] serverSocketFd The server socket FD
83 * @exception E_SUCCESS The method is successful.
84 * @exception E_SERVICE_UNAVAILABLE The service is not available.
85 * @exception E_SYSTEM A system error has occurred.
87 result OpenServerSocket(const Tizen::Base::UuId& serviceUuid, _IBluetoothSocketListener& listener, int& serverSocketFd);
90 * Close the specified server socket. @n
91 * All child sockets involved in this server socket will be automatically disconnected.
93 * @return An error code
94 * @param[in] serverSocketFd The server socket FD
95 * @exception E_SUCCESS The method is successful.
96 * @exception E_SYSTEM A system error has occurred.
98 result CloseServerSocket(int serverSocketFd);
101 * Accept the connection request received to the specified server socket. @n
102 * A new child socket is created if the accept is successful.
104 * @return An error code
105 * @param[in] serverSocketFd The server socket FD
106 * @param[out] socketFd The child socket FD for the connection
107 * @exception E_SUCCESS The method is successful.
108 * @exception E_SYSTEM A system error has occurred.
110 result AcceptSocketRequest(int serverSocketFd, int& socketFd);
113 * Reject the connection request received to the specified server socket.
115 * @return An error code
116 * @param[in] serverSocketFd The server socket FD
117 * @exception E_SUCCESS The method is successful.
118 * @exception E_SYSTEM A system error has occurred.
120 result RejectSocketRequest(int serverSocketFd);
123 //-----------------------------------------------------------------------------------------------------------------
125 //-----------------------------------------------------------------------------------------------------------------
128 * Connect with the specified server. @n
129 * A new socket FD can be acquired on
130 * OnSocketConnected() callback method invoked when the connection is completely established.
132 * @return An error code
133 * @param[in] serverAddress The address of the server
134 * @param[in] serviceUuid The service UUID
135 * @param[in] listener The socket event listener
136 * @exception E_SUCCESS The method is successful.
137 * @exception E_SYSTEM A system error has occurred.
139 result ConnectSocket(const Tizen::Base::ByteBuffer& serverAddress, const Tizen::Base::UuId& serviceUuid,
140 _IBluetoothSocketListener& listener);
143 * Close the connection related to the specified socket. @n
145 * @return An error code
146 * @param[in] socketFd The socket FD
147 * @exception E_SUCCESS The method is successful.
148 * @exception E_SYSTEM A system error has occurred.
150 result DisconnectSocket(int socketFd);
153 * Send data through the connection related to the specified socket. @n
155 * @return An error code
156 * @param[in] socketFd The socket FD
157 * @param[in] data The data to be sent
158 * @exception E_SUCCESS The method is successful.
159 * @exception E_SYSTEM A system error has occurred.
161 result SendSocketData(int socketFd, const Tizen::Base::ByteBuffer& data);
164 //-----------------------------------------------------------------------------------------------------------------
166 //-----------------------------------------------------------------------------------------------------------------
169 * Callback called by the underlying system to notify that the RFCOMM server socket is received the connection request.
171 static void OnSocketConnectionRequested(int serverSocketFd, const char* pRemoteAddress, void* pUserData);
174 * Callback called by the underlying system to notify that the connection state has changed to connected or disconnected.
176 static void OnSocketConnectionStateChanged(int status, bt_socket_connection_state_e connectionState,
177 bt_socket_connection_s* pConnectionInfo, void* pUserData);
180 * Callback used by the underlying system to notify that data is received on the established connection.
182 static void OnSocketDataReceived(bt_socket_received_data_s* pData, void* pUserData);
185 _BluetoothSppSystemAdapter(void);
186 ~_BluetoothSppSystemAdapter(void);
188 _BluetoothSppSystemAdapter(const _BluetoothSppSystemAdapter& value);
189 _BluetoothSppSystemAdapter& operator =(const _BluetoothSppSystemAdapter& value);
191 bool Construct(void);
193 static void InitSingleton(void);
196 std::unique_ptr< Tizen::Base::Collection::HashMapT<int, _IBluetoothSocketListener*> > __pServerSocketMap;
197 std::unique_ptr< Tizen::Base::Collection::HashMapT<int, _IBluetoothSocketListener*> > __pSocketMap;
198 std::unique_ptr< Tizen::Base::Collection::HashMapT<Tizen::Base::String, _IBluetoothSocketListener*> > __pClientSocketMap;
199 static _BluetoothSppSystemAdapter* __pInstance;
200 }; // _BluetoothSppSystemAdapter
203 #endif // _FNET_BT_INTERNAL_BLUETOOTH_SPP_SYSTEM_ADAPTER_H_