34 QOhosBluetoothAccessProxy::instance().get(),
35 &QOhosBluetoothAccessProxy::stateChanged,
37 [
this](QOhosBluetoothAccessProxy::BluetoothState bluetoothState) {
38 if (bluetoothState == QOhosBluetoothAccessProxy::BluetoothState::STATE_ON)
41 if (!m_serverSocketHandle)
44 const auto self = weak_from_this().lock();
49 QT_BT_OHOS,
"%s: bluetooth is turned off. Closing the server ...", Q_FUNC_INFO);
50 reportError(QOhosBluetoothErrorCode::BluetoothDisabled);
57 if (!checkAccessBluetoothPermissionGranted(Q_FUNC_INFO)) {
58 reportError(QOhosBluetoothErrorCode::PermissionDenied);
62 struct SppListenResult
64 int serverSocketId = invalidSocketDescriptor;
65 std::optional<QOhosBluetoothErrorCode> optErrorCode = std::nullopt;
68 const auto listenResult = QOhosJsThreadGateway::evalWithPromise<SppListenResult>(
69 [&](QOhosJsState &jsState, QOhosTaskPromise<SppListenResult> evalPromise) {
70 auto sppOptions = QNapi::makeObject(
73 {
"uuid", uuid.toStdString()},
75 {
"type", jsState.mapOhosEnumToJs(SppType::SPP_RFCOMM)},
78 auto thenCatchPromises = std::move(evalPromise).makeThenCatchBranches(Q_FUNC_INFO);
82 "@ohos.bluetooth.socket.sppListen(*)",
84 serviceName.toStdString(),
86 [thenPromise = std::move(thenCatchPromises.first)](
87 const QNapi::CallbackInfo &callbackInfo) {
90 callbackInfo.getLeadingArgs(Q_FUNC_INFO, error, data);
91 int socketId = data.IsNumber()
92 ? QNapi::checkedCast<QNapi::Number>(data).Int32Value()
93 : invalidSocketDescriptor;
95 if (socketId == invalidSocketDescriptor) {
98 socketId, tryGetKnownErrorCode(error, Q_FUNC_INFO)});
102 thenPromise(SppListenResult{socketId, {}});
105 }
catch (
const Napi::Error &error) {
106 thenCatchPromises.second(
108 invalidSocketDescriptor,
109 tryGetKnownErrorCode(error.Value(), Q_FUNC_INFO)});
113 if (listenResult.optErrorCode) {
115 QT_BT_OHOS,
"%s: sppListen() failed with error code: %u", Q_FUNC_INFO,
116 qToUnderlying(*listenResult.optErrorCode));
117 reportError(*listenResult.optErrorCode);
121 if (listenResult.serverSocketId == invalidSocketDescriptor) {
122 qCCritical(QT_BT_OHOS,
"%s: sppListen() reported no server socket", Q_FUNC_INFO);
123 reportError(QOhosBluetoothErrorCode::OperationFailed);
127 m_serverSocketHandle = makeSppServerSocketHandle(listenResult.serverSocketId);
128 m_registeredServiceName = serviceName;
129 m_optRegisteredServiceId = ++m_registeredServicesCounter;
136 if (!checkAccessBluetoothPermissionGranted(Q_FUNC_INFO)) {
137 reportError(QOhosBluetoothErrorCode::PermissionDenied);
141 if (!m_serverSocketHandle) {
143 QT_BT_OHOS,
"%s: Cannot accept clients connections. There is no server socket created",
148 auto optAcceptErrorCode = QOhosJsThreadGateway::eval(
149 [&](QOhosJsState &jsState) -> std::optional<QOhosBluetoothErrorCode> {
152 "@ohos.bluetooth.socket.sppAccept(*)",
154 *m_serverSocketHandle,
155 [selfWeakRef = weak_from_this(), armedServerSocketWatch = QtOhos::makeWeakPtr(m_serverSocketHandle)](
156 const QNapi::CallbackInfo &callbackInfo) {
159 callbackInfo.getLeadingArgs(Q_FUNC_INFO, error, data);
160 int clientSocketId = data.IsNumber()
161 ? QNapi::checkedCast<QNapi::Number>(data).Int32Value()
162 : invalidSocketDescriptor;
164 if (clientSocketId == invalidSocketDescriptor) {
165 const bool abortedByOwnServerSocketClose =
166 armedServerSocketWatch.expired();
167 if (abortedByOwnServerSocketClose) {
169 "%s: sppAccept() was aborted by closing the server socket",
174 const auto errorCode =
175 tryGetKnownErrorCode(error, Q_FUNC_INFO)
176 .value_or(QOhosBluetoothErrorCode::OperationFailed);
178 "%s: sppAccept() failed with error code: %u", Q_FUNC_INFO,
179 qToUnderlying(errorCode));
180 QtOhos::invokeInQtThread(
181 [selfWeakRef, errorCode]() {
182 if (
const auto self = selfWeakRef.lock())
183 self->onAcceptFailed(errorCode);
188 auto clientSocketHandle = makeSppClientSocketHandle(clientSocketId);
190 QtOhos::invokeInQtThread(
191 [selfWeakRef, clientSocketHandle]() {
192 if (
const auto self = selfWeakRef.lock())
193 self->onClientAccepted(clientSocketHandle);
198 }
catch (
const Napi::Error &error) {
199 return tryGetKnownErrorCode(error.Value(), Q_FUNC_INFO);
203 if (optAcceptErrorCode) {
205 QT_BT_OHOS,
"%s: sppAccept() failed with error code: %u", Q_FUNC_INFO,
206 qToUnderlying(*optAcceptErrorCode));
207 reportError(*optAcceptErrorCode);