6#include <QtBluetooth/qbluetoothserver.h>
7#include <QtBluetooth/qbluetoothsocket.h>
8#include <QtBluetooth/private/qbluetoothserver_p.h>
9#include <QtBluetooth/private/qohosbluetoothaccess_p.h>
10#include <QtBluetooth/private/qohosbluetoothcommon_p.h>
11#include <QtBluetooth/private/qohosbluetoothserver_p.h>
12#include <QtBluetooth/private/qohosbluetoothsocket_p.h>
14#include <QtCore/q20utility.h>
15#include <QtCore/qloggingcategory.h>
21using QtOhosBluetooth::QOhosBluetoothErrorCode;
23Q_DECLARE_LOGGING_CATEGORY(QT_BT_OHOS)
27QBluetoothServer::Error mapToServerError(QOhosBluetoothErrorCode errorCode)
30 case QOhosBluetoothErrorCode::PermissionDenied:
31 return QBluetoothServer::MissingPermissionsError;
32 case QOhosBluetoothErrorCode::CapabilityNotSupported:
33 case QOhosBluetoothErrorCode::ProfileNotSupported:
34 return QBluetoothServer::UnsupportedProtocolError;
35 case QOhosBluetoothErrorCode::InputOutput:
36 return QBluetoothServer::InputOutputError;
37 case QOhosBluetoothErrorCode::BluetoothDisabled:
38 case QOhosBluetoothErrorCode::ServiceStopped:
39 return QBluetoothServer::PoweredOffError;
40 case QOhosBluetoothErrorCode::InvalidParameter:
41 case QOhosBluetoothErrorCode::UserDidNotRespond:
42 case QOhosBluetoothErrorCode::UserRefused:
43 case QOhosBluetoothErrorCode::OperationFailed:
47 return QBluetoothServer::UnknownError;
52QBluetoothServerPrivate::QBluetoothServerPrivate(
53 QBluetoothServiceInfo::Protocol sType, QBluetoothServer *parent)
59QBluetoothServerPrivate::~QBluetoothServerPrivate() =
default;
61bool QBluetoothServerPrivate::isListening()
const
63 return socket !=
nullptr && socket->state() == QBluetoothSocket::SocketState::ListeningState;
66void QBluetoothServerPrivate::resetServerContext()
69 m_bluetoothServerProxyContext.reset();
72void QBluetoothServer::close()
74 d_ptr->resetServerContext();
77bool QBluetoothServer::listen(
const QBluetoothAddress &address, quint16 port)
79 if (!address.isNull()) {
82 "%s: HarmonyOS does not support multi device adapters. Ignoring local adapter address ...",
86 const auto reportError = [
this](Error error) {
87 d_ptr->m_lastError = error;
88 QMetaObject::invokeMethod(
91 Q_EMIT errorOccurred(error);
93 Qt::QueuedConnection);
96 if (serverType() != QBluetoothServiceInfo::RfcommProtocol) {
97 reportError(UnsupportedProtocolError);
101 if (!QtOhosBluetooth::checkAccessBluetoothPermissionGranted(Q_FUNC_INFO)) {
102 reportError(MissingPermissionsError);
106 auto accessProxy = QtOhosBluetooth::QOhosBluetoothAccessProxy::instance();
107 if (accessProxy->tryGetBluetoothState()
108 != QtOhosBluetooth::QOhosBluetoothAccessProxy::BluetoothState::STATE_ON) {
109 reportError(PoweredOffError);
114 reportError(ServiceAlreadyRegisteredError);
118 if (d_ptr->m_bluetoothServerProxyContext
119 && !d_ptr->m_bluetoothServerProxyContext->bluetoothServerProxy->isServiceRegistered()) {
120 d_ptr->resetServerContext();
123 if (!d_ptr->m_bluetoothServerProxyContext) {
124 d_ptr->m_bluetoothServerProxyContext = QtOhosBluetooth::makeServerProxyContext(port);
125 if (!d_ptr->m_bluetoothServerProxyContext) {
126 reportError(ServiceAlreadyRegisteredError);
129 d_ptr->m_bluetoothServerProxyContext->securityFlags = d_ptr->securityFlags;
130 d_ptr->socket = d_ptr->m_bluetoothServerProxyContext->bluetoothSocket.get();
132 auto serverProxy = d_ptr->m_bluetoothServerProxyContext->bluetoothServerProxy.get();
135 serverProxy, &QtOhosBluetooth::QOhosBluetoothServerProxy::clientAccepted,
137 [
this](
int clientSocketDescriptor) {
138 auto context = d_ptr->m_bluetoothServerProxyContext;
140 QtOhosBluetooth::QOhosBluetoothSocketProxy::dropPendingSocketHandle(
141 clientSocketDescriptor);
145 if (q20::cmp_greater_equal(
146 context->pendingConnectionSockets.size(),
147 d_ptr->maxPendingConnections)) {
150 "%s: maximum number of pending connections reached. Rejecting the client ...",
152 QtOhosBluetooth::QOhosBluetoothSocketProxy::dropPendingSocketHandle(
153 clientSocketDescriptor);
157 auto pendingConnectionSocket = std::make_unique<QBluetoothSocket>();
158 const auto socketDescriptorSet = pendingConnectionSocket->setSocketDescriptor(
159 clientSocketDescriptor, QBluetoothServiceInfo::RfcommProtocol,
160 QBluetoothSocket::SocketState::ConnectedState, QBluetoothSocket::ReadWrite);
162 if (!socketDescriptorSet) {
163 QtOhosBluetooth::QOhosBluetoothSocketProxy::dropPendingSocketHandle(
164 clientSocketDescriptor);
168 context->pendingConnectionSockets.push_back(std::move(pendingConnectionSocket));
169 Q_EMIT newConnection();
173 serverProxy, &QtOhosBluetooth::QOhosBluetoothServerProxy::errorOccurred,
175 [
this](QOhosBluetoothErrorCode errorCode) {
176 d_ptr->m_lastError = mapToServerError(errorCode);
177 Q_EMIT errorOccurred(d_ptr->m_lastError);
181 serverProxy, &QtOhosBluetooth::QOhosBluetoothServerProxy::acceptingStopped,
184 auto context = d_ptr->m_bluetoothServerProxyContext;
185 if (context && context->bluetoothServerProxy->isServiceRegistered())
189 d_ptr->socket->setSocketState(QBluetoothSocket::SocketState::UnconnectedState);
193 d_ptr->socket->setSocketState(QBluetoothSocket::SocketState::ListeningState);
197void QBluetoothServer::setMaxPendingConnections(
int numConnections)
199 d_ptr->maxPendingConnections = numConnections;
202bool QBluetoothServer::hasPendingConnections()
const
204 auto context = d_ptr->m_bluetoothServerProxyContext;
205 return context && !context->pendingConnectionSockets.empty();
208QBluetoothSocket *QBluetoothServer::nextPendingConnection()
210 auto context = d_ptr->m_bluetoothServerProxyContext;
211 if (!context || context->pendingConnectionSockets.empty())
214 auto pendingConnectionSocket = std::move(context->pendingConnectionSockets.front());
215 context->pendingConnectionSockets.pop_front();
217 return pendingConnectionSocket.release();
220QBluetoothAddress QBluetoothServer::serverAddress()
const
225quint16 QBluetoothServer::serverPort()
const
227 auto context = d_ptr->m_bluetoothServerProxyContext;
228 return context ? context->port : 0;
231void QBluetoothServer::setSecurityFlags(QBluetooth::SecurityFlags security)
236 "%s: the security flags must be set before listen(). Ignoring the new flags ...",
241 d_ptr->securityFlags = security;
244QBluetooth::SecurityFlags QBluetoothServer::securityFlags()
const
246 return d_ptr->securityFlags;
Combined button and popup list for selecting options.