4#include <QtBluetooth/private/qbluetoothserver_ohos_p.h>
5#include <QtBluetooth/private/qohosbluetoothserver_p.h>
10#include <unordered_map>
18std::unordered_map<quint16, std::weak_ptr<QOhosBluetoothServerProxyContext>> &serverProxyContexts()
20 static std::unordered_map<quint16, std::weak_ptr<QOhosBluetoothServerProxyContext>> contexts;
24void dropExpiredServerProxyContexts()
26 auto &contexts = serverProxyContexts();
27 for (
auto context = contexts.begin(); context != contexts.end();)
28 context = context->second.expired() ? contexts.erase(context) :
std::next(context);
31std::optional<quint16> tryFindFreeServerPort()
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);
46 dropExpiredServerProxyContexts();
48 const auto optServerPort = port != 0
49 ? std::make_optional(port)
50 : tryFindFreeServerPort();
51 if (!optServerPort || tryGetServerProxyContext(*optServerPort))
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;
60 return serverProxyContext;
65 auto &contexts = serverProxyContexts();
66 const auto found = contexts.find(port);
67 return found != contexts.end()
68 ? found->second.lock()
74 if (serviceName.isEmpty())
77 for (
const auto &context : serverProxyContexts()) {
78 auto serverProxyContext = context.second.lock();
79 if (!serverProxyContext)
82 if (serverProxyContext->bluetoothServerProxy->tryGetRegisteredServiceName() == serviceName)
std::shared_ptr< QOhosBluetoothServerProxyContext > makeServerProxyContext(quint16 port)
bool isServiceNameAlreadyRegistered(const QString &serviceName)
std::shared_ptr< QOhosBluetoothServerProxyContext > tryGetServerProxyContext(quint16 port)