summaryrefslogtreecommitdiff
path: root/src/FNetBt_BluetoothSppSystemAdapter.h
blob: a2ee53d83017da2cd89da7a250e055d991d9636a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//
// Open Service Platform
// Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
//
// 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.
//
/**
 * @file    FNetBt_BluetoothSppSystemAdapter.h
 * @brief   This is the header file for the _BluetoothSppSystemAdapter class.
 *
 * This header file contains the declaration of the _BluetoothSppSystemAdapter class.
 */
#ifndef _FNET_BT_INTERNAL_BLUETOOTH_SPP_SYSTEM_ADAPTER_H_
#define _FNET_BT_INTERNAL_BLUETOOTH_SPP_SYSTEM_ADAPTER_H_

#include <unique_ptr.h>
#include <bluetooth.h>
#include <FBaseObject.h>
#include <FBaseResult.h>
#include <FNetBtBluetoothTypes.h>

static const int _BT_INVALID_SOCKET_FD = -1;

// forward declarations
namespace Tizen { namespace Base {
class ByteBuffer;
class String;
class UuId;

namespace Collection {
class ArrayList;
template<class keyType, class valueType> class HashMapT;
}
} }

namespace Tizen { namespace Net { namespace Bluetooth
{

// forward declarations
class BluetoothDevice;
class _IBluetoothSocketListener;

/**
 * @class   _BluetoothSppSystemAdapter
 * @brief   This class provides functions related to RFCOMM socket (SPP) and have a role of adaptation OSP classes and the
 *          underlying subsystem. This class is a type of singleton, therefore only one instance should be allowed.
 */
class _BluetoothSppSystemAdapter
	: public Tizen::Base::Object
{
public:
	/**
	 * Creates an instance of this class if there is no instance in this application context.
	 * Gets the exist instance because this class is a singleton, otherwise.
	 *
	 * @return      the instance of this class
	 */
	static _BluetoothSppSystemAdapter* GetInstance(void);


	//-----------------------------------------------------------------------------------------------------------------
	// Server Socket
	//-----------------------------------------------------------------------------------------------------------------

	/**
	 * Create and initialize a server socket, and start to listen the socket events. @n
	 * An instance of _IBluetoothSocketListener class is registered internally.
	 *
	 * @return      An error code
	 * @param[in]   serviceUuid             The service UUID
	 * @param[in]   listener                The instance of _IBluetoothSocketListener to be registered
	 * @param[out]  serverSocketFd          The server socket FD
	 * @exception   E_SUCCESS               The method is successful.
	 * @exception   E_SERVICE_UNAVAILABLE   The service is not available.
	 * @exception   E_SYSTEM                A system error has occurred.
	 */
	result OpenServerSocket(const Tizen::Base::UuId& serviceUuid, _IBluetoothSocketListener& listener, int& serverSocketFd);

	/**
	 * Close the specified server socket. @n
	 * All child sockets involved in this server socket will be automatically disconnected.
	 *
	 * @return      An error code
	 * @param[in]   serverSocketFd          The server socket FD
	 * @exception   E_SUCCESS               The method is successful.
	 * @exception   E_SYSTEM                A system error has occurred.
	 */
	result CloseServerSocket(int serverSocketFd);

	/**
	 * Accept the connection request received to the specified server socket. @n
	 * A new child socket is created if the accept is successful.
	 *
	 * @return      An error code
	 * @param[in]   serverSocketFd          The server socket FD
	 * @param[out]  socketFd                The child socket FD for the connection
	 * @exception   E_SUCCESS               The method is successful.
	 * @exception   E_SYSTEM                A system error has occurred.
	 */
	result AcceptSocketRequest(int serverSocketFd, int& socketFd);

	/**
	 * Reject the connection request received to the specified server socket.
	 *
	 * @return      An error code
	 * @param[in]   serverSocketFd          The server socket FD
	 * @exception   E_SUCCESS               The method is successful.
	 * @exception   E_SYSTEM                A system error has occurred.
	 */
	result RejectSocketRequest(int serverSocketFd);


	//-----------------------------------------------------------------------------------------------------------------
	// socket
	//-----------------------------------------------------------------------------------------------------------------

	/**
	 * Connect with the specified server. @n
	 * A new socket FD can be acquired on
	 * OnSocketConnected() callback method invoked when the connection is completely established.
	 *
	 * @return      An error code
	 * @param[in]   serverAddress           The address of the server
	 * @param[in]   serviceUuid             The service UUID
	 * @param[in]   listener                The socket event listener
	 * @exception   E_SUCCESS               The method is successful.
	 * @exception   E_SYSTEM                A system error has occurred.
	 */
	result ConnectSocket(const Tizen::Base::ByteBuffer& serverAddress, const Tizen::Base::UuId& serviceUuid,
			_IBluetoothSocketListener& listener);

	/**
	 * Close the connection related to the specified socket. @n
	 *
	 * @return      An error code
	 * @param[in]   socketFd                The socket FD
	 * @exception   E_SUCCESS               The method is successful.
	 * @exception   E_SYSTEM                A system error has occurred.
	 */
	result DisconnectSocket(int socketFd);

	/**
	 * Send data through the connection related to the specified socket. @n
	 *
	 * @return      An error code
	 * @param[in]   socketFd                The socket FD
	 * @param[in]   data                    The data to be sent
	 * @exception   E_SUCCESS               The method is successful.
	 * @exception   E_SYSTEM                A system error has occurred.
	 */
	result SendSocketData(int socketFd, const Tizen::Base::ByteBuffer& data);


	//-----------------------------------------------------------------------------------------------------------------
	// Callback methods
	//-----------------------------------------------------------------------------------------------------------------

	/**
	 * Callback called by the underlying system to notify that the RFCOMM server socket is received the connection request.
	 */
	static void OnSocketConnectionRequested(int serverSocketFd, const char* pRemoteAddress, void* pUserData);

	/**
	 * Callback called by the underlying system to notify that the connection state has changed to connected or disconnected.
	 */
	static void OnSocketConnectionStateChanged(int status, bt_socket_connection_state_e connectionState,
			bt_socket_connection_s* pConnectionInfo, void* pUserData);

	/**
	 * Callback used by the underlying system to notify that data is received on the established connection.
	 */
	static void OnSocketDataReceived(bt_socket_received_data_s* pData, void* pUserData);

private:
	_BluetoothSppSystemAdapter(void);
	~_BluetoothSppSystemAdapter(void);

	_BluetoothSppSystemAdapter(const _BluetoothSppSystemAdapter& value);
	_BluetoothSppSystemAdapter& operator =(const _BluetoothSppSystemAdapter& value);

	bool Construct(void);

	static void InitSingleton(void);
	static void DeinitializeSingleton(void);

private:
	std::unique_ptr< Tizen::Base::Collection::HashMapT<int, _IBluetoothSocketListener*> > __pServerSocketMap;
	std::unique_ptr< Tizen::Base::Collection::HashMapT<int, _IBluetoothSocketListener*> > __pSocketMap;
	std::unique_ptr< Tizen::Base::Collection::HashMapT<Tizen::Base::String, _IBluetoothSocketListener*> > __pClientSocketMap;
	static _BluetoothSppSystemAdapter* __pInstance;
	friend class std::default_delete<_BluetoothSppSystemAdapter>;
}; // _BluetoothSppSystemAdapter

} } }
#endif // _FNET_BT_INTERNAL_BLUETOOTH_SPP_SYSTEM_ADAPTER_H_