update the header for Doxygen
[platform/framework/native/bluetooth.git] / src / FNetBt_BluetoothSppSystemAdapter.h
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17 /**
18  * @file    FNetBt_BluetoothSppSystemAdapter.h
19  * @brief   This is the header file for the _BluetoothSppSystemAdapter class.
20  *
21  * This header file contains the declaration of the _BluetoothSppSystemAdapter class.
22  */
23 #ifndef _FNET_BT_INTERNAL_BLUETOOTH_SPP_SYSTEM_ADAPTER_H_
24 #define _FNET_BT_INTERNAL_BLUETOOTH_SPP_SYSTEM_ADAPTER_H_
25
26 #include <unique_ptr.h>
27 #include <bluetooth.h>
28 #include <FBaseObject.h>
29 #include <FBaseResult.h>
30 #include <FNetBtBluetoothTypes.h>
31
32 static const int _BT_INVALID_SOCKET_FD = -1;
33
34 // forward declarations
35 namespace Tizen { namespace Base {
36 class ByteBuffer;
37 class String;
38 class UuId;
39
40 namespace Collection {
41 class ArrayList;
42 template<class keyType, class valueType> class HashMapT;
43 }
44 } }
45
46 namespace Tizen { namespace Net { namespace Bluetooth
47 {
48
49 // forward declarations
50 class BluetoothDevice;
51 class _IBluetoothSocketListener;
52
53 /**
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.
57  */
58 class _BluetoothSppSystemAdapter
59         : public Tizen::Base::Object
60 {
61 public:
62         /**
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.
65          *
66          * @return      the instance of this class
67          */
68         static _BluetoothSppSystemAdapter* GetInstance(void);
69
70
71         //-----------------------------------------------------------------------------------------------------------------
72         // Server Socket
73         //-----------------------------------------------------------------------------------------------------------------
74
75         /**
76          * Create and initialize a server socket, and start to listen the socket events. @n
77          * An instance of _IBluetoothSocketListener class is registered internally.
78          *
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.
86          */
87         result OpenServerSocket(const Tizen::Base::UuId& serviceUuid, _IBluetoothSocketListener& listener, int& serverSocketFd);
88
89         /**
90          * Close the specified server socket. @n
91          * All child sockets involved in this server socket will be automatically disconnected.
92          *
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.
97          */
98         result CloseServerSocket(int serverSocketFd);
99
100         /**
101          * Accept the connection request received to the specified server socket. @n
102          * A new child socket is created if the accept is successful.
103          *
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.
109          */
110         result AcceptSocketRequest(int serverSocketFd, int& socketFd);
111
112         /**
113          * Reject the connection request received to the specified server socket.
114          *
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.
119          */
120         result RejectSocketRequest(int serverSocketFd);
121
122
123         //-----------------------------------------------------------------------------------------------------------------
124         // socket
125         //-----------------------------------------------------------------------------------------------------------------
126
127         /**
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.
131          *
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.
138          */
139         result ConnectSocket(const Tizen::Base::ByteBuffer& serverAddress, const Tizen::Base::UuId& serviceUuid,
140                         _IBluetoothSocketListener& listener);
141
142         /**
143          * Close the connection related to the specified socket. @n
144          *
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.
149          */
150         result DisconnectSocket(int socketFd);
151
152         /**
153          * Send data through the connection related to the specified socket. @n
154          *
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.
160          */
161         result SendSocketData(int socketFd, const Tizen::Base::ByteBuffer& data);
162
163
164         //-----------------------------------------------------------------------------------------------------------------
165         // Callback methods
166         //-----------------------------------------------------------------------------------------------------------------
167
168         /**
169          * Callback called by the underlying system to notify that the RFCOMM server socket is received the connection request.
170          */
171         static void OnSocketConnectionRequested(int serverSocketFd, const char* pRemoteAddress, void* pUserData);
172
173         /**
174          * Callback called by the underlying system to notify that the connection state has changed to connected or disconnected.
175          */
176         static void OnSocketConnectionStateChanged(int status, bt_socket_connection_state_e connectionState,
177                         bt_socket_connection_s* pConnectionInfo, void* pUserData);
178
179         /**
180          * Callback used by the underlying system to notify that data is received on the established connection.
181          */
182         static void OnSocketDataReceived(bt_socket_received_data_s* pData, void* pUserData);
183
184 private:
185         _BluetoothSppSystemAdapter(void);
186         ~_BluetoothSppSystemAdapter(void);
187
188         _BluetoothSppSystemAdapter(const _BluetoothSppSystemAdapter& value);
189         _BluetoothSppSystemAdapter& operator =(const _BluetoothSppSystemAdapter& value);
190
191         bool Construct(void);
192
193         static void InitSingleton(void);
194
195 private:
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
201
202 } } }
203 #endif // _FNET_BT_INTERNAL_BLUETOOTH_SPP_SYSTEM_ADAPTER_H_