mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-15 15:16:12 -04:00
Signed-off-by: Alexey Polyudov <apolyudov@google.com> Change-Id: I4bf367877a986910bb03e1c54aa972b4bcd59942
121 lines
4.3 KiB
C++
121 lines
4.3 KiB
C++
#include "platform_v2/public/wifi_lan.h"
|
|
|
|
#include "platform_v2/public/logging.h"
|
|
#include "platform_v2/public/mutex_lock.h"
|
|
|
|
namespace location {
|
|
namespace nearby {
|
|
|
|
bool WifiLanMedium::StartAdvertising(
|
|
const std::string& service_id,
|
|
const std::string& wifi_lan_service_info_name) {
|
|
return impl_->StartAdvertising(service_id, wifi_lan_service_info_name);
|
|
}
|
|
|
|
bool WifiLanMedium::StopAdvertising(const std::string& service_id) {
|
|
return impl_->StopAdvertising(service_id);
|
|
}
|
|
|
|
bool WifiLanMedium::StartDiscovery(const std::string& service_id,
|
|
DiscoveredServiceCallback callback) {
|
|
{
|
|
MutexLock lock(&mutex_);
|
|
discovered_service_callback_ = std::move(callback);
|
|
services_.clear();
|
|
}
|
|
return impl_->StartDiscovery(
|
|
service_id,
|
|
{
|
|
.service_discovered_cb =
|
|
[this](api::WifiLanService& service,
|
|
const std::string& service_id) {
|
|
MutexLock lock(&mutex_);
|
|
auto pair = services_.emplace(
|
|
&service, absl::make_unique<ServiceDiscoveryInfo>());
|
|
auto& context = *pair.first->second;
|
|
if (!pair.second) {
|
|
NEARBY_LOG(INFO, "Adding (again) service=%p, impl=%p",
|
|
&context.service, &service);
|
|
return;
|
|
}
|
|
context.service = WifiLanService(&service);
|
|
NEARBY_LOG(INFO, "Adding service=%p, impl=%p", &context.service,
|
|
&service);
|
|
discovered_service_callback_.service_discovered_cb(
|
|
context.service, service_id);
|
|
},
|
|
.service_lost_cb =
|
|
[this](api::WifiLanService& service,
|
|
const std::string& service_id) {
|
|
MutexLock lock(&mutex_);
|
|
auto item = services_.extract(&service);
|
|
auto& context = *item.mapped();
|
|
NEARBY_LOG(INFO, "Removing service=%p, impl=%p",
|
|
&context.service, &service);
|
|
discovered_service_callback_.service_lost_cb(context.service,
|
|
service_id);
|
|
},
|
|
});
|
|
}
|
|
|
|
bool WifiLanMedium::StopDiscovery(const std::string& service_id) {
|
|
{
|
|
MutexLock lock(&mutex_);
|
|
discovered_service_callback_ = {};
|
|
services_.clear();
|
|
NEARBY_LOG(INFO, "WifiLan Discovery disabled: impl=%p", &GetImpl());
|
|
}
|
|
return impl_->StopDiscovery(service_id);
|
|
}
|
|
|
|
bool WifiLanMedium::StartAcceptingConnections(
|
|
const std::string& service_id, AcceptedConnectionCallback callback) {
|
|
{
|
|
MutexLock lock(&mutex_);
|
|
accepted_connection_callback_ = std::move(callback);
|
|
}
|
|
return impl_->StartAcceptingConnections(
|
|
service_id,
|
|
{
|
|
.accepted_cb =
|
|
[this](api::WifiLanSocket& socket,
|
|
const std::string& service_id) {
|
|
MutexLock lock(&mutex_);
|
|
auto pair = sockets_.emplace(
|
|
&socket, absl::make_unique<AcceptedConnectionInfo>());
|
|
auto& context = *pair.first->second;
|
|
if (!pair.second) {
|
|
NEARBY_LOG(INFO, "Adding (again) socket=%p, impl=%p",
|
|
&context.socket, &socket);
|
|
return;
|
|
}
|
|
context.socket = WifiLanSocket(&socket);
|
|
NEARBY_LOG(INFO, "Adding socket=%p, impl=%p", &context.socket,
|
|
&socket);
|
|
accepted_connection_callback_.accepted_cb(context.socket,
|
|
service_id);
|
|
},
|
|
});
|
|
}
|
|
|
|
bool WifiLanMedium::StopAcceptingConnections(const std::string& service_id) {
|
|
{
|
|
MutexLock lock(&mutex_);
|
|
accepted_connection_callback_ = {};
|
|
sockets_.clear();
|
|
NEARBY_LOG(INFO, "WifiLan accepted connection disabled: impl=%p",
|
|
&GetImpl());
|
|
}
|
|
return impl_->StopDiscovery(service_id);
|
|
}
|
|
|
|
WifiLanSocket WifiLanMedium::Connect(WifiLanService& service,
|
|
const std::string& service_id) {
|
|
NEARBY_LOG(INFO, "WifiLanMedium::Connect: service=%p [impl=%p]", &service,
|
|
&service.GetImpl());
|
|
return WifiLanSocket(impl_->Connect(service.GetImpl(), service_id));
|
|
}
|
|
|
|
} // namespace nearby
|
|
} // namespace location
|