Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qbluetoothserver_ohos_p.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include <QtBluetooth/private/qbluetoothserver_ohos_p.h>
5#include <QtBluetooth/private/qohosbluetoothserver_p.h>
6
7#include <iterator>
8#include <limits>
9#include <optional>
10#include <unordered_map>
11
12QT_BEGIN_NAMESPACE
13
14namespace QtOhosBluetooth {
15
16namespace {
17
18std::unordered_map<quint16, std::weak_ptr<QOhosBluetoothServerProxyContext>> &serverProxyContexts()
19{
20 static std::unordered_map<quint16, std::weak_ptr<QOhosBluetoothServerProxyContext>> contexts;
21 return contexts;
22}
23
24void dropExpiredServerProxyContexts()
25{
26 auto &contexts = serverProxyContexts();
27 for (auto context = contexts.begin(); context != contexts.end();)
28 context = context->second.expired() ? contexts.erase(context) : std::next(context);
29}
30
31std::optional<quint16> tryFindFreeServerPort()
32{
33 constexpr int maxServerPort = std::numeric_limits<quint8>::max();
34 for (int candidatePort = 1; candidatePort <= maxServerPort; ++candidatePort) {
35 if (!tryGetServerProxyContext(quint16(candidatePort)))
36 return quint16(candidatePort);
37 }
38
39 return std::nullopt;
40}
41
42}
43
45{
46 dropExpiredServerProxyContexts();
47
48 const auto optServerPort = port != 0
49 ? std::make_optional(port)
50 : tryFindFreeServerPort();
51 if (!optServerPort || tryGetServerProxyContext(*optServerPort))
52 return nullptr;
53
54 auto serverProxyContext = std::make_shared<QOhosBluetoothServerProxyContext>();
55 serverProxyContext->port = *optServerPort;
56 serverProxyContext->bluetoothServerProxy = std::make_shared<QOhosBluetoothServerProxy>();
57 serverProxyContext->bluetoothSocket = std::make_shared<QBluetoothSocket>();
58 serverProxyContexts()[serverProxyContext->port] = serverProxyContext;
59
60 return serverProxyContext;
61}
62
64{
65 auto &contexts = serverProxyContexts();
66 const auto found = contexts.find(port);
67 return found != contexts.end()
68 ? found->second.lock()
69 : nullptr;
70}
71
72bool isServiceNameAlreadyRegistered(const QString &serviceName)
73{
74 if (serviceName.isEmpty())
75 return false;
76
77 for (const auto &context : serverProxyContexts()) {
78 auto serverProxyContext = context.second.lock();
79 if (!serverProxyContext)
80 continue;
81
82 if (serverProxyContext->bluetoothServerProxy->tryGetRegisteredServiceName() == serviceName)
83 return true;
84 }
85
86 return false;
87}
88
89}
90
91QT_END_NAMESPACE
std::shared_ptr< QOhosBluetoothServerProxyContext > makeServerProxyContext(quint16 port)
bool isServiceNameAlreadyRegistered(const QString &serviceName)
std::shared_ptr< QOhosBluetoothServerProxyContext > tryGetServerProxyContext(quint16 port)