From e6d52359cf75ad2d8db88c59889ecf0adde58b51 Mon Sep 17 00:00:00 2001 From: hai007 Date: Mon, 23 Nov 2020 22:28:06 -0800 Subject: [PATCH] Roll forward to cl/343785060 Signed-off-by: hai007 --- cpp/core/BUILD | 1 + cpp/core/internal/mediums/wifi_lan.cc | 46 +- cpp/core/internal/mediums/wifi_lan.h | 7 +- cpp/core/internal/mediums/wifi_lan_test.cc | 22 +- cpp/core/internal/p2p_cluster_pcp_handler.cc | 66 ++- .../internal/service_controller_router.cc | 3 +- cpp/core/internal/wifi_lan_service_info.cc | 39 +- cpp/core/internal/wifi_lan_service_info.h | 15 +- .../internal/wifi_lan_service_info_test.cc | 22 +- cpp/core/listeners.h | 5 + cpp/platform/api/wifi_lan.h | 33 +- cpp/platform/base/BUILD | 17 + cpp/platform/base/byte_utils.cc | 25 + cpp/platform/base/byte_utils.h | 24 + cpp/platform/base/byte_utils_test.cc | 30 ++ cpp/platform/base/medium_environment.cc | 67 +-- cpp/platform/base/medium_environment.h | 9 +- cpp/platform/base/nsd_service_info.h | 67 +++ cpp/platform/impl/g3/wifi_lan.cc | 117 +++-- cpp/platform/impl/g3/wifi_lan.h | 60 +-- cpp/platform/public/wifi_lan.cc | 55 ++- cpp/platform/public/wifi_lan.h | 21 +- cpp/platform/public/wifi_lan_test.cc | 37 +- proto/BUILD | 15 - proto/connections/offline_wire_formats.proto | 2 + proto/connections_enums.proto | 3 + proto/error_code_enums.proto | 434 ------------------ 27 files changed, 500 insertions(+), 742 deletions(-) create mode 100644 cpp/platform/base/byte_utils.cc create mode 100644 cpp/platform/base/byte_utils.h create mode 100644 cpp/platform/base/byte_utils_test.cc create mode 100644 cpp/platform/base/nsd_service_info.h delete mode 100644 proto/error_code_enums.proto diff --git a/cpp/core/BUILD b/cpp/core/BUILD index 1e5c1d42..5e9db38b 100644 --- a/cpp/core/BUILD +++ b/cpp/core/BUILD @@ -39,6 +39,7 @@ cc_library( ], deps = [ "//platform/base", + "//platform/base:util", "//platform/public:comm", "//platform/public:logging", "//platform/public:types", diff --git a/cpp/core/internal/mediums/wifi_lan.cc b/cpp/core/internal/mediums/wifi_lan.cc index a81aea21..70057c11 100644 --- a/cpp/core/internal/mediums/wifi_lan.cc +++ b/cpp/core/internal/mediums/wifi_lan.cc @@ -20,14 +20,13 @@ bool WifiLan::IsAvailable() const { bool WifiLan::IsAvailableLocked() const { return medium_.IsValid(); } bool WifiLan::StartAdvertising(const std::string& service_id, - const std::string& service_info_name, - const std::string& endpoint_info_name) { + const NsdServiceInfo& nsd_service_info) { MutexLock lock(&mutex_); - if (service_info_name.empty()) { - NEARBY_LOG( - INFO, - "Refusing to turn on WifiLan advertising. Empty service info name."); + if (!nsd_service_info.IsValid()) { + NEARBY_LOGS(INFO) + << "Refusing to turn on WifiLan advertising. nsd_service_info is not " + "valid."; return false; } @@ -37,16 +36,19 @@ bool WifiLan::StartAdvertising(const std::string& service_id, return false; } - if (!medium_.StartAdvertising(service_id, service_info_name, - endpoint_info_name)) { - NEARBY_LOG( - INFO, "Failed to turn on WifiLan advertising with service info name=%s", - service_info_name.c_str()); + if (!medium_.StartAdvertising(service_id, nsd_service_info)) { + NEARBY_LOGS(INFO) + << "Failed to turn on WifiLan advertising with wifi_lan_service=" + << &nsd_service_info + << ", service_info_name=" << nsd_service_info.GetServiceInfoName() + << ", service_id=" << service_id; return false; } - NEARBY_LOGS(INFO) << "Turned on WifiLan advertising with service info name=" - << service_info_name << ", service id=" << service_id; + NEARBY_LOGS(INFO) << "Turned on WifiLan advertising with wifi_lan_service=" + << &nsd_service_info << ", service_info_name=" + << nsd_service_info.GetServiceInfoName() + << ", service_id=" << service_id; advertising_info_.Add(service_id); return true; } @@ -59,7 +61,7 @@ bool WifiLan::StopAdvertising(const std::string& service_id) { return false; } - NEARBY_LOG(INFO, "Turned off WifiLan advertising with service id=%s", + NEARBY_LOG(INFO, "Turned off WifiLan advertising with service_id=%s", service_id.c_str()); bool ret = medium_.StopAdvertising(service_id); // Reset our bundle of advertising state to mark that we're no longer @@ -84,7 +86,7 @@ bool WifiLan::StartDiscovery(const std::string& service_id, if (service_id.empty()) { NEARBY_LOG(INFO, - "Refusing to start WifiLan discovering with empty service id."); + "Refusing to start WifiLan discovering with empty service_id."); return false; } @@ -108,7 +110,7 @@ bool WifiLan::StartDiscovery(const std::string& service_id, return false; } - NEARBY_LOG(INFO, "Turned on WifiLan discovering with service id=%s", + NEARBY_LOG(INFO, "Turned on WifiLan discovering with service_id=%s", service_id.c_str()); // Mark the fact that we're currently performing a WifiLan discovering. discovering_info_.Add(service_id); @@ -125,7 +127,7 @@ bool WifiLan::StopDiscovery(const std::string& service_id) { return false; } - NEARBY_LOG(INFO, "Turned off WifiLan discovering with service id=%s", + NEARBY_LOG(INFO, "Turned off WifiLan discovering with service_id=%s", service_id.c_str()); bool ret = medium_.StopDiscovery(service_id); discovering_info_.Clear(); @@ -149,7 +151,7 @@ bool WifiLan::StartAcceptingConnections(const std::string& service_id, if (service_id.empty()) { NEARBY_LOG(INFO, "Refusing to start accepting WifiLan connections with empty " - "service id."); + "service_id."); return false; } @@ -209,8 +211,10 @@ bool WifiLan::IsAcceptingConnectionsLocked(const std::string& service_id) { WifiLanSocket WifiLan::Connect(WifiLanService& wifi_lan_service, const std::string& service_id) { MutexLock lock(&mutex_); - NEARBY_LOG(INFO, "WifiLan::Connect: service=%p, service_info_name=%s", - &wifi_lan_service, wifi_lan_service.GetServiceName().c_str()); + NEARBY_LOGS(INFO) << "WifiLan::Connect: wifi_lan_service=" + << &wifi_lan_service << ", service_info_name=" + << wifi_lan_service.GetServiceInfo().GetServiceInfoName() + << ", service_id=" << service_id; // Socket to return. To allow for NRVO to work, it has to be a single object. WifiLanSocket socket; @@ -240,7 +244,7 @@ WifiLanSocket WifiLan::Connect(WifiLanService& wifi_lan_service, WifiLanService WifiLan::GetRemoteWifiLanService(const std::string& ip_address, int port) { MutexLock lock(&mutex_); - return medium_.FindRemoteService(ip_address, port); + return medium_.GetRemoteService(ip_address, port); } std::pair WifiLan::GetServiceAddress( diff --git a/cpp/core/internal/mediums/wifi_lan.h b/cpp/core/internal/mediums/wifi_lan.h index 076ce9bb..6e9c47ac 100644 --- a/cpp/core/internal/mediums/wifi_lan.h +++ b/cpp/core/internal/mediums/wifi_lan.h @@ -23,12 +23,11 @@ class WifiLan { // Returns true, if WifiLan communications are supported by a platform. bool IsAvailable() const ABSL_LOCKS_EXCLUDED(mutex_); - // Sets custom service info name, endpoint info name and then enables WifiLan - // advertising. + // Sets custom service info name, endpoint info name in NsdServiceInfo and + // then enables WifiLan advertising. // Returns true, if name is successfully set, and false otherwise. bool StartAdvertising(const std::string& service_id, - const std::string& service_info_name, - const std::string& endpoint_info_name) + const NsdServiceInfo& nsd_service_info) ABSL_LOCKS_EXCLUDED(mutex_); // Disables WifiLan advertising, and restores service info name to diff --git a/cpp/core/internal/mediums/wifi_lan_test.cc b/cpp/core/internal/mediums/wifi_lan_test.cc index 20e75ac1..fb905b59 100644 --- a/cpp/core/internal/mediums/wifi_lan_test.cc +++ b/cpp/core/internal/mediums/wifi_lan_test.cc @@ -20,6 +20,7 @@ constexpr absl::string_view kServiceID{"com.google.location.nearby.apps.test"}; constexpr absl::string_view kServiceInfoName{ "Simulated WifiLan service encrypted string #1"}; constexpr absl::string_view kEndpointName{"Simulated endpoint name"}; +constexpr absl::string_view kEndpointInfoKey{"n"}; class WifiLanTest : public ::testing::Test { protected: @@ -59,8 +60,11 @@ TEST_F(WifiLanTest, CanStartAdvertising) { }, }); - EXPECT_TRUE(wifi_lan_a.StartAdvertising(service_id, service_info_name, - endpoint_info_name)); + NsdServiceInfo nsd_service_info; + nsd_service_info.SetServiceInfoName(service_info_name); + nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey), + endpoint_info_name); + EXPECT_TRUE(wifi_lan_a.StartAdvertising(service_id, nsd_service_info)); EXPECT_TRUE(found_latch.Await(kWaitDuration).result()); EXPECT_TRUE(wifi_lan_a.StopAdvertising(service_id)); EXPECT_TRUE(wifi_lan_b.StopDiscovery(service_id)); @@ -77,8 +81,11 @@ TEST_F(WifiLanTest, CanStartDiscovery) { CountDownLatch accept_latch(1); CountDownLatch lost_latch(1); - wifi_lan_b.StartAdvertising(service_id, service_info_name, - endpoint_info_name); + NsdServiceInfo nsd_service_info; + nsd_service_info.SetServiceInfoName(service_info_name); + nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey), + endpoint_info_name); + wifi_lan_b.StartAdvertising(service_id, nsd_service_info); EXPECT_TRUE(wifi_lan_a.StartDiscovery( service_id, { @@ -110,8 +117,11 @@ TEST_F(WifiLanTest, CanStartAcceptingConnectionsAndConnect) { CountDownLatch found_latch(1); CountDownLatch accept_latch(1); - wifi_lan_a.StartAdvertising(service_id, service_info_name, - endpoint_info_name); + NsdServiceInfo nsd_service_info; + nsd_service_info.SetServiceInfoName(service_info_name); + nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey), + endpoint_info_name); + wifi_lan_a.StartAdvertising(service_id, nsd_service_info); wifi_lan_a.StartAcceptingConnections( service_id, { diff --git a/cpp/core/internal/p2p_cluster_pcp_handler.cc b/cpp/core/internal/p2p_cluster_pcp_handler.cc index d0108e66..f3d76db3 100644 --- a/cpp/core/internal/p2p_cluster_pcp_handler.cc +++ b/cpp/core/internal/p2p_cluster_pcp_handler.cc @@ -9,6 +9,7 @@ #include "core/internal/mediums/webrtc/webrtc_socket_wrapper.h" #include "core/internal/webrtc_endpoint_channel.h" #include "core/internal/wifi_lan_endpoint_channel.h" +#include "platform/base/nsd_service_info.h" #include "platform/base/types.h" #include "platform/public/crypto.h" #include "proto/connections_enums.pb.h" @@ -456,9 +457,9 @@ bool P2pClusterPcpHandler::IsRecognizedWifiLanEndpoint( } void P2pClusterPcpHandler::WifiLanServiceDiscoveredHandler( - ClientProxy* client, WifiLanService& service, + ClientProxy* client, WifiLanService& wifi_lan_service, const std::string& service_id) { - RunOnPcpHandlerThread([this, client, service_id, &service]() { + RunOnPcpHandlerThread([this, client, service_id, &wifi_lan_service]() { // Make sure we are still discovering before proceeding. if (!client->IsDiscovering()) { NEARBY_LOG( @@ -469,10 +470,8 @@ void P2pClusterPcpHandler::WifiLanServiceDiscoveredHandler( return; } - // Parse the WifiLan service name. - WifiLanServiceInfo service_info(service.GetServiceName(), - service.GetTxtRecord(std::string{ - WifiLanServiceInfo::kKeyEndpointInfo})); + // Parse the WifiLanServiceInfo. + WifiLanServiceInfo service_info(wifi_lan_service.GetServiceInfo()); // Make sure the WifiLan service name points to a valid // endpoint we're discovering. @@ -482,7 +481,7 @@ void P2pClusterPcpHandler::WifiLanServiceDiscoveredHandler( NEARBY_LOG( INFO, "Invoking BasePcpHandler::OnEndpointFound() for WifiLan " - "service=%s; id=%s; name=%s", + "service_id=%s; endpoint_id=%s; endpoint_info=%s", service_id.c_str(), service_info.GetEndpointId().c_str(), absl::BytesToHexString(service_info.GetEndpointInfo().data()).c_str()); OnEndpointFound(client, std::make_shared(WifiLanEndpoint{ @@ -493,22 +492,19 @@ void P2pClusterPcpHandler::WifiLanServiceDiscoveredHandler( proto::connections::Medium::WIFI_LAN, service_info.GetWebRtcState(), }, - service, + wifi_lan_service, })); }); } void P2pClusterPcpHandler::WifiLanServiceLostHandler( - ClientProxy* client, WifiLanService& service, + ClientProxy* client, WifiLanService& wifi_lan_service, const std::string& service_id) { - std::string service_info_name = service.GetServiceName(); - std::string endpoint_info_name = - service.GetTxtRecord(std::string{WifiLanServiceInfo::kKeyEndpointInfo}); - NEARBY_LOG( - INFO, "WifiLan: [LOST, SCHED] service_info_name=%s, endpoint_info_name=%", - service_info_name.c_str(), endpoint_info_name.c_str()); - RunOnPcpHandlerThread([this, client, service_id, service_info_name, - endpoint_info_name]() { + NsdServiceInfo nsd_service_info = wifi_lan_service.GetServiceInfo(); + NEARBY_LOG(INFO, + "WifiLan: [LOST, SCHED] wifi_lan_service=%p, service_info_name=%s", + &wifi_lan_service, nsd_service_info.GetServiceInfoName().c_str()); + RunOnPcpHandlerThread([this, client, service_id, nsd_service_info]() { // Make sure we are still discovering before proceeding. if (!client->IsDiscovering()) { NEARBY_LOG( @@ -519,8 +515,8 @@ void P2pClusterPcpHandler::WifiLanServiceLostHandler( return; } - // Parse the WifiLan service name. - WifiLanServiceInfo service_info(service_info_name, endpoint_info_name); + // Parse the WifiLanServiceInfo. + WifiLanServiceInfo service_info(nsd_service_info); // Make sure the WifiLan service name points to a valid // endpoint we're discovering. @@ -1043,7 +1039,9 @@ proto::connections::Medium P2pClusterPcpHandler::StartWifiLanAdvertising( RunOnPcpHandlerThread([this, client, local_endpoint_info, socket = std::move(socket)]() mutable { std::string remote_service_info_name = - socket.GetRemoteWifiLanService().GetServiceName(); + socket.GetRemoteWifiLanService() + .GetServiceInfo() + .GetServiceInfoName(); auto channel = absl::make_unique( remote_service_info_name, socket); ByteArray remote_service_info{remote_service_info_name}; @@ -1074,32 +1072,30 @@ proto::connections::Medium P2pClusterPcpHandler::StartWifiLanAdvertising( local_endpoint_info, ByteArray{}, web_rtc_state}; - std::string service_info_name(service_info); - if (service_info_name.empty()) { - NEARBY_LOG(INFO, - "P2pClusterPcpHandler::StartWifiLanAdvertising: generate " - "WifiLanServiceInfo failed"); + NsdServiceInfo nsd_service_info{service_info}; + if (!nsd_service_info.IsValid()) { + NEARBY_LOGS(INFO) + << "P2pClusterPcpHandler::StartWifiLanAdvertising: generate " + "NsdServiceInfo failed"; wifi_lan_medium_.StopAcceptingConnections(service_id); return proto::connections::UNKNOWN_MEDIUM; } else { - NEARBY_LOG(INFO, - "P2pClusterPcpHandler::StartWifiLanAdvertising: generate " - "WifiLanServiceInfo succeeded; service_info_name=%s", - service_info_name.c_str()); + NEARBY_LOGS(INFO) + << "P2pClusterPcpHandler::StartWifiLanAdvertising: generate " + "NsdServiceInfo succeeded; service_info_name=" + << nsd_service_info.GetServiceInfoName(); } - auto local_endpoint_info_name = service_info.GetEndpointInfoName(); NEARBY_LOG( INFO, "P2pClusterPcpHandler::StartWifiLanAdvertising: service=%s: come up", service_id.c_str()); - if (!wifi_lan_medium_.StartAdvertising(service_id, service_info_name, - local_endpoint_info_name)) { + if (!wifi_lan_medium_.StartAdvertising(service_id, nsd_service_info)) { NEARBY_LOG(INFO, "P2pClusterPcpHandler::StartWifiLanAdvertising: failed to " "start advertising, service_info_name=%s", - service_info_name.c_str()); + nsd_service_info.GetServiceInfoName().c_str()); wifi_lan_medium_.StopAcceptingConnections(service_id); return proto::connections::UNKNOWN_MEDIUM; } @@ -1123,10 +1119,10 @@ proto::connections::Medium P2pClusterPcpHandler::StartWifiLanDiscovery( BasePcpHandler::ConnectImplResult P2pClusterPcpHandler::WifiLanConnectImpl( ClientProxy* client, WifiLanEndpoint* endpoint) { - WifiLanService& service = endpoint->wifi_lan_service; + WifiLanService& wifi_lan_service = endpoint->wifi_lan_service; WifiLanSocket wifi_lan_socket = - wifi_lan_medium_.Connect(service, endpoint->service_id); + wifi_lan_medium_.Connect(wifi_lan_service, endpoint->service_id); if (!wifi_lan_socket.IsValid()) { return BasePcpHandler::ConnectImplResult{ .status = {Status::kWifiLanError}, diff --git a/cpp/core/internal/service_controller_router.cc b/cpp/core/internal/service_controller_router.cc index 8c716828..fe281993 100644 --- a/cpp/core/internal/service_controller_router.cc +++ b/cpp/core/internal/service_controller_router.cc @@ -34,6 +34,7 @@ const std::size_t kMaxEndpointInfoLength = 131u; ServiceControllerRouter::~ServiceControllerRouter() { NEARBY_LOG(INFO, "ServiceControllerRouter going down."); + service_controller_.reset(); // And make sure that cleanup is the last thing we do. serializer_.Shutdown(); } @@ -390,8 +391,8 @@ void ServiceControllerRouter::ReleaseServiceControllerForClient( ClientProxy* client) { clients_.erase(client); + // service_controller_ won't be released here. Instead, in desctructor. if (clients_.empty()) { - service_controller_.reset(); current_strategy_ = Strategy{}; } } diff --git a/cpp/core/internal/wifi_lan_service_info.cc b/cpp/core/internal/wifi_lan_service_info.cc index 4a796eb2..19090efd 100644 --- a/cpp/core/internal/wifi_lan_service_info.cc +++ b/cpp/core/internal/wifi_lan_service_info.cc @@ -49,18 +49,22 @@ WifiLanServiceInfo::WifiLanServiceInfo(Version version, Pcp pcp, web_rtc_state_ = web_rtc_state; } -WifiLanServiceInfo::WifiLanServiceInfo(absl::string_view service_info_name, - absl::string_view endpoint_info_name) { - ByteArray service_info_bytes = Base64Utils::Decode(service_info_name); - endpoint_info_ = Base64Utils::Decode(endpoint_info_name); - if (endpoint_info_.size() > kMaxEndpointInfoLength) { - NEARBY_LOG(INFO, - "Cannot deserialize EndpointInfo: expecting endpoint info " - "max %d raw bytes, got %" PRIu64, - kMaxEndpointInfoLength, endpoint_info_.size()); - return; +WifiLanServiceInfo::WifiLanServiceInfo(const NsdServiceInfo& nsd_service_info) { + auto txt_endpoint_info_name = + nsd_service_info.GetTxtRecord(std::string(kKeyEndpointInfo)); + if (!txt_endpoint_info_name.empty()) { + endpoint_info_ = Base64Utils::Decode(txt_endpoint_info_name); + if (endpoint_info_.size() > kMaxEndpointInfoLength) { + NEARBY_LOG(INFO, + "Cannot deserialize EndpointInfo: expecting endpoint info " + "max %d raw bytes, got %" PRIu64, + kMaxEndpointInfoLength, endpoint_info_.size()); + return; + } } + auto service_info_name = nsd_service_info.GetServiceInfoName(); + ByteArray service_info_bytes = Base64Utils::Decode(service_info_name); if (service_info_bytes.Empty()) { NEARBY_LOG( INFO, @@ -139,9 +143,9 @@ WifiLanServiceInfo::WifiLanServiceInfo(absl::string_view service_info_name, } } -WifiLanServiceInfo::operator std::string() const { +WifiLanServiceInfo::operator NsdServiceInfo() const { if (!IsValid()) { - return ""; + return {}; } // The upper 3 bits are the Version. @@ -174,11 +178,12 @@ WifiLanServiceInfo::operator std::string() const { absl::StrAppend(&out, std::string(1, field_byte)); } - return Base64Utils::Encode(ByteArray{std::move(out)}); -} - -std::string WifiLanServiceInfo::GetEndpointInfoName() const { - return Base64Utils::Encode(endpoint_info_); + NsdServiceInfo nsd_service_info; + nsd_service_info.SetServiceInfoName( + Base64Utils::Encode(ByteArray{std::move(out)})); + nsd_service_info.SetTxtRecord(std::string(kKeyEndpointInfo), + Base64Utils::Encode(endpoint_info_)); + return nsd_service_info; } } // namespace connections diff --git a/cpp/core/internal/wifi_lan_service_info.h b/cpp/core/internal/wifi_lan_service_info.h index 5a9c58cc..45028176 100644 --- a/cpp/core/internal/wifi_lan_service_info.h +++ b/cpp/core/internal/wifi_lan_service_info.h @@ -6,6 +6,7 @@ #include "core/internal/base_pcp_handler.h" #include "core/internal/pcp.h" #include "platform/base/byte_array.h" +#include "platform/base/nsd_service_info.h" #include "absl/strings/string_view.h" namespace location { @@ -36,23 +37,15 @@ class WifiLanServiceInfo { const ByteArray& uwb_address, WebRtcState web_rtc_state); - // Constructs WifiLanService through packed string of WifiLanServiceInfo and - // EndpointInfo. - // - // service_info_name - A packed string of |WifiLanServiceInfo|. It does - // not include endpoint_info which should be stored - // in next param bleow. - // endpoint_info_name - The endpoint info packed string. - WifiLanServiceInfo(absl::string_view service_info_name, - absl::string_view endpoint_info_name); + // Constructs WifiLanServiceInfo through NsdServiceInfo. + explicit WifiLanServiceInfo(const NsdServiceInfo& nsd_service_info); WifiLanServiceInfo(const WifiLanServiceInfo&) = default; WifiLanServiceInfo& operator=(const WifiLanServiceInfo&) = default; WifiLanServiceInfo(WifiLanServiceInfo&&) = default; WifiLanServiceInfo& operator=(WifiLanServiceInfo&&) = default; ~WifiLanServiceInfo() = default; - explicit operator std::string() const; - std::string GetEndpointInfoName() const; + explicit operator NsdServiceInfo() const; bool IsValid() const { return !endpoint_id_.empty(); } Version GetVersion() const { return version_; } diff --git a/cpp/core/internal/wifi_lan_service_info_test.cc b/cpp/core/internal/wifi_lan_service_info_test.cc index 3463a18b..ffe93040 100644 --- a/cpp/core/internal/wifi_lan_service_info_test.cc +++ b/cpp/core/internal/wifi_lan_service_info_test.cc @@ -4,6 +4,7 @@ #include #include "platform/base/base64_utils.h" +#include "platform/base/nsd_service_info.h" #include "gtest/gtest.h" namespace location { @@ -49,11 +50,9 @@ TEST(WifiLanServiceInfoTest, ConstructionFromSerializedStringWorks) { endpoint_info, ByteArray{}, kWebRtcState}; - std::string wifi_lan_service_info_string{org_wifi_lan_service_info}; - auto endpoint_info_name = org_wifi_lan_service_info.GetEndpointInfoName(); + NsdServiceInfo nsd_service_info{org_wifi_lan_service_info}; - WifiLanServiceInfo wifi_lan_service_info{wifi_lan_service_info_string, - endpoint_info_name}; + WifiLanServiceInfo wifi_lan_service_info{nsd_service_info}; EXPECT_TRUE(wifi_lan_service_info.IsValid()); EXPECT_EQ(kPcp, wifi_lan_service_info.GetPcp()); @@ -160,14 +159,15 @@ TEST(WifiLanServiceInfoTest, ConstructionFailsWithLongServiceIdHash) { EXPECT_FALSE(wifi_lan_service_info.IsValid()); } -TEST(WifiLanServiceInfoTest, ConstructionFailsWithShortStringLength) { - char wifi_lan_service_info_string[] = {'X', '\0'}; - ByteArray endpoint_info{std::string(kEndPointName)}; +TEST(WifiLanServiceInfoTest, ConstructionFailsWithShortServiceNameLength) { + char wifi_lan_service_info_name[] = {'X', '\0'}; + ByteArray wifi_lan_service_info_bytes{wifi_lan_service_info_name}; - ByteArray wifi_lan_service_info_bytes{wifi_lan_service_info_string}; - WifiLanServiceInfo wifi_lan_service_info{ - Base64Utils::Encode(wifi_lan_service_info_bytes), - Base64Utils::Encode(endpoint_info)}; + NsdServiceInfo nsd_service_info; + nsd_service_info.SetServiceInfoName( + Base64Utils::Encode(wifi_lan_service_info_bytes)); + + WifiLanServiceInfo wifi_lan_service_info{nsd_service_info}; EXPECT_FALSE(wifi_lan_service_info.IsValid()); } diff --git a/cpp/core/listeners.h b/cpp/core/listeners.h index c49b3831..d941477a 100644 --- a/cpp/core/listeners.h +++ b/cpp/core/listeners.h @@ -17,6 +17,7 @@ #include "core/payload.h" #include "core/status.h" #include "platform/base/byte_array.h" +#include "platform/base/byte_utils.h" #include "platform/base/listeners.h" namespace location { @@ -36,6 +37,10 @@ struct ResultCallback { }; struct ConnectionResponseInfo { + std::string GetAuthenticationDigits() { + return ByteUtils::ToFourDigitString(raw_authentication_token); + } + ByteArray remote_endpoint_info; std::string authentication_token; ByteArray raw_authentication_token; diff --git a/cpp/platform/api/wifi_lan.h b/cpp/platform/api/wifi_lan.h index 4361b715..94cfa9cd 100644 --- a/cpp/platform/api/wifi_lan.h +++ b/cpp/platform/api/wifi_lan.h @@ -6,6 +6,7 @@ #include "platform/base/byte_array.h" #include "platform/base/input_stream.h" #include "platform/base/listeners.h" +#include "platform/base/nsd_service_info.h" #include "platform/base/output_stream.h" #include "absl/strings/string_view.h" @@ -13,23 +14,17 @@ namespace location { namespace nearby { namespace api { -// Opaque wrapper over a WifiLan service which contains packed -// |WifiLanServiceInfo| string name. +// Opaque wrapper over a WifiLan service which contains |NsdServiceInfo|. class WifiLanService { public: virtual ~WifiLanService() = default; - // Returns the packed string of |WifiLanServiceInfo|. Note that the packed - // string would not include TXTRecord, which inheritor should save it in - // another store. - virtual std::string GetServiceName() const = 0; - - // Returns the packed string of endpoint info with named key. - virtual std::string GetTxtRecord(const std::string& key) const = 0; - - // Returns the local device's as a pair. - // IP address is in byte sequence, in network order. - virtual std::pair GetServiceAddress() const = 0; + // Returns the |NsdServiceInfo| which contains the packed string of + // |WifiLanServiceInfo| and the endpoint info with named key in a TXTRecord + // map. + // The details refer to + // https://developer.android.com/reference/android/net/nsd/NsdServiceInfo.html. + virtual NsdServiceInfo GetServiceInfo() const = 0; }; class WifiLanSocket { @@ -63,10 +58,8 @@ class WifiLanMedium { public: virtual ~WifiLanMedium() = default; - virtual bool StartAdvertising( - const std::string& service_id, - const std::string& wifi_lan_service_info_name, - const std::string& endpoint_info_name) = 0; + virtual bool StartAdvertising(const std::string& service_id, + const NsdServiceInfo& nsd_service_info) = 0; virtual bool StopAdvertising(const std::string& service_id) = 0; // Callback that is invoked when a discovered service is found or lost. @@ -106,10 +99,10 @@ class WifiLanMedium { // On success, returns a new WifiLanSocket. // On error, returns nullptr. virtual std::unique_ptr Connect( - WifiLanService& service, const std::string& service_id) = 0; + WifiLanService& wifi_lan_service, const std::string& service_id) = 0; - virtual WifiLanService* FindRemoteService(const std::string& ip_address, - int port) = 0; + virtual WifiLanService* GetRemoteService(const std::string& ip_address, + int port) = 0; virtual std::pair GetServiceAddress( const std::string& service_id) = 0; diff --git a/cpp/platform/base/BUILD b/cpp/platform/base/BUILD index bc7a65c2..c0ba327e 100644 --- a/cpp/platform/base/BUILD +++ b/cpp/platform/base/BUILD @@ -15,6 +15,7 @@ cc_library( "exception.h", "input_stream.h", "listeners.h", + "nsd_service_info.h", "output_stream.h", "payload_id.h", "prng.h", @@ -29,6 +30,7 @@ cc_library( "//platform/api:__subpackages__", ], deps = [ + "//absl/container:flat_hash_map", "//absl/meta:type_traits", "//absl/strings", "//absl/strings:str_format", @@ -41,11 +43,13 @@ cc_library( srcs = [ "base_input_stream.cc", "base_pipe.cc", + "byte_utils.cc", ], hdrs = [ "base_input_stream.h", "base_mutex_lock.h", "base_pipe.h", + "byte_utils.h", ], visibility = [ "//googlemac/iPhone/Shared/Nearby/Connections:__subpackages__", @@ -57,6 +61,7 @@ cc_library( ":base", "//platform/api:types", "//absl/base:core_headers", + "//absl/strings:str_format", ], ) @@ -112,6 +117,18 @@ cc_test( ], ) +cc_test( + name = "platform_util_test", + srcs = [ + "byte_utils_test.cc", + ], + deps = [ + ":base", + ":util", + "//testing/base/public:gunit_main", + ], +) + cc_with_non_compile_test( name = "exception_test", srcs = [ diff --git a/cpp/platform/base/byte_utils.cc b/cpp/platform/base/byte_utils.cc new file mode 100644 index 00000000..1b2a3d85 --- /dev/null +++ b/cpp/platform/base/byte_utils.cc @@ -0,0 +1,25 @@ +#include "platform/base/byte_utils.h" + +#include + +#include "platform/base/base_input_stream.h" +#include "absl/strings/str_format.h" + +namespace location { +namespace nearby { + +std::string ByteUtils::ToFourDigitString(ByteArray& bytes) { + int multiplier = 1; + int hashCode = 0; + + BaseInputStream base_input_stream{bytes}; + while (base_input_stream.IsAvailable(1)) { + auto byte = static_cast(base_input_stream.ReadUint8()); + hashCode = (hashCode + byte * multiplier) % kHashBasePrime; + multiplier = multiplier * kHashBaseMultiplier % kHashBasePrime; + } + return absl::StrFormat("%04d", abs(hashCode)); +} + +} // namespace nearby +} // namespace location diff --git a/cpp/platform/base/byte_utils.h b/cpp/platform/base/byte_utils.h new file mode 100644 index 00000000..6770e1fd --- /dev/null +++ b/cpp/platform/base/byte_utils.h @@ -0,0 +1,24 @@ +#ifndef PLATFORM_BASE_BYTE_UTILS_H_ +#define PLATFORM_BASE_BYTE_UTILS_H_ + +#include "platform/base/byte_array.h" + +namespace location { +namespace nearby { + +class ByteUtils { + public: + static std::string ToFourDigitString(ByteArray& bytes); + + private: + // The biggest prime number under 10000, used as a mod base to trim integers + // into 4 digits. + static constexpr int kHashBasePrime = 9973; + // The hash multiplier. + static constexpr int kHashBaseMultiplier = 31; +}; + +} // namespace nearby +} // namespace location + +#endif // PLATFORM_BASE_BYTE_UTILS_H_ diff --git a/cpp/platform/base/byte_utils_test.cc b/cpp/platform/base/byte_utils_test.cc new file mode 100644 index 00000000..ed35052c --- /dev/null +++ b/cpp/platform/base/byte_utils_test.cc @@ -0,0 +1,30 @@ +#include "platform/base/byte_utils.h" + +#include "platform/base/byte_array.h" +#include "gtest/gtest.h" + +namespace location { +namespace nearby { + +constexpr absl::string_view kFooBytes{"rawABCDE"}; +constexpr absl::string_view kFooFourDigitsToken{"0392"}; +constexpr absl::string_view kEmptyFourDigitsToken{"0000"}; + +TEST(ByteUtilsTest, ToFourDigitStringCorrect) { + ByteArray bytes{std::string(kFooBytes)}; + + auto four_digit_string = ByteUtils::ToFourDigitString(bytes); + + EXPECT_EQ(std::string(kFooFourDigitsToken), four_digit_string); +} + +TEST(ByteUtilsTest, TestEmptyByteArrayCorrect) { + ByteArray bytes; + + auto four_digit_string = ByteUtils::ToFourDigitString(bytes); + + EXPECT_EQ(std::string(kEmptyFourDigitsToken), four_digit_string); +} + +} // namespace nearby +} // namespace location diff --git a/cpp/platform/base/medium_environment.cc b/cpp/platform/base/medium_environment.cc index 7d70af45..7076edc7 100644 --- a/cpp/platform/base/medium_environment.cc +++ b/cpp/platform/base/medium_environment.cc @@ -201,28 +201,32 @@ void MediumEnvironment::OnBlePeripheralStateChanged( } void MediumEnvironment::OnWifiLanServiceStateChanged( - WifiLanMediumContext& info, api::WifiLanService& service, + WifiLanMediumContext& info, api::WifiLanService& wifi_lan_service, const std::string& service_id, bool enabled) { if (!enabled_) return; - NEARBY_LOG(INFO, - "G3 OnWifiLanServiceStateChanged [service impl=%p]; context=%p; " - "service_id=%s; notify=%d", - &service, &info, service_id.c_str(), enable_notifications_.load()); + NEARBY_LOG( + INFO, + "G3 OnWifiLanServiceStateChanged [wifi_lan_service impl=%p]; context=%p; " + "service_id=%s; notify=%d", + &wifi_lan_service, &info, service_id.c_str(), + enable_notifications_.load()); if (!enable_notifications_) return; - RunOnMediumEnvironmentThread([&info, enabled, &service, service_id]() { - NEARBY_LOG(INFO, - "G3 [Run] OnWifiLanServiceStateChanged [service impl=%p]; " - "context=%p; service_id=%s; enabled=%d", - &service, &info, service_id.c_str(), enabled); + RunOnMediumEnvironmentThread([&info, enabled, &wifi_lan_service, + service_id]() { + NEARBY_LOG( + INFO, + "G3 [Run] OnWifiLanServiceStateChanged [wifi_lan_service impl=%p]; " + "context=%p; service_id=%s; enabled=%d", + &wifi_lan_service, &info, service_id.c_str(), enabled); auto service_id_context = info.services.find(service_id); if (service_id_context == info.services.end()) return; if (enabled) { service_id_context->second.discovery_callback.service_discovered_cb( - service, service_id); + wifi_lan_service, service_id); } else { - service_id_context->second.discovery_callback.service_lost_cb(service, - service_id); + service_id_context->second.discovery_callback.service_lost_cb( + wifi_lan_service, service_id); } }); } @@ -477,10 +481,10 @@ void MediumEnvironment::RegisterWifiLanMedium(api::WifiLanMedium& medium) { } void MediumEnvironment::UpdateWifiLanMediumForAdvertising( - api::WifiLanMedium& medium, api::WifiLanService& service, + api::WifiLanMedium& medium, api::WifiLanService& wifi_lan_service, const std::string& service_id, bool enabled) { if (!enabled_) return; - RunOnMediumEnvironmentThread([this, &medium, &service, service_id, + RunOnMediumEnvironmentThread([this, &medium, &wifi_lan_service, service_id, enabled]() { auto item = wifi_lan_mediums_.find(&medium); if (item == wifi_lan_mediums_.end()) { @@ -490,7 +494,7 @@ void MediumEnvironment::UpdateWifiLanMediumForAdvertising( return; } auto& context = item->second; - context.wifi_lan_service = &service; + context.wifi_lan_service = &wifi_lan_service; auto service_id_context = context.services.find(service_id); if (service_id_context == context.services.end()) { WifiLanServiceIdContext id_context{ @@ -500,17 +504,19 @@ void MediumEnvironment::UpdateWifiLanMediumForAdvertising( } else { service_id_context->second.advertising = enabled; } - NEARBY_LOG(INFO, - "Update WifiLan medium for advertising: this=%p; medium=%p; " - "service_id=%s; name=%s; enabled=%d", - this, &medium, service_id.c_str(), - service.GetServiceName().c_str(), enabled); + NEARBY_LOG( + INFO, + "Update WifiLan medium for advertising: this=%p; medium=%p; " + "service_id=%s; wifi_lan_service=%p, service_info_name=%s; enabled=%d", + this, &medium, service_id.c_str(), &wifi_lan_service, + wifi_lan_service.GetServiceInfo().GetServiceInfoName().c_str(), + enabled); for (auto& medium_info : wifi_lan_mediums_) { auto& local_medium = medium_info.first; auto& info = medium_info.second; // Do not send notification to the same medium. if (local_medium == &medium) continue; - OnWifiLanServiceStateChanged(info, service, service_id, enabled); + OnWifiLanServiceStateChanged(info, wifi_lan_service, service_id, enabled); } }); } @@ -620,25 +626,26 @@ void MediumEnvironment::CallWifiLanAcceptedConnectionCallback( }); } -api::WifiLanService* MediumEnvironment::FindWifiLanService( +api::WifiLanService* MediumEnvironment::GetWifiLanService( const std::string& ip_address, int port) { - api::WifiLanService* remote_service = nullptr; + api::WifiLanService* remote_wifi_lan_service = nullptr; CountDownLatch latch(1); RunOnMediumEnvironmentThread( - [this, &remote_service, &ip_address, port, &latch]() { + [this, &remote_wifi_lan_service, &ip_address, port, &latch]() { for (auto& item : wifi_lan_mediums_) { - auto* service = item.second.wifi_lan_service; - if (!service) continue; - auto addr = remote_service->GetServiceAddress(); + auto* wifi_lan_service = item.second.wifi_lan_service; + if (!wifi_lan_service) continue; + auto addr = + remote_wifi_lan_service->GetServiceInfo().GetServiceAddress(); if (addr.first == ip_address && addr.second == port) { - remote_service = service; + remote_wifi_lan_service = wifi_lan_service; break; } } latch.CountDown(); }); latch.Await(); - return remote_service; + return remote_wifi_lan_service; } } // namespace nearby diff --git a/cpp/platform/base/medium_environment.h b/cpp/platform/base/medium_environment.h index 5642ab63..48614563 100644 --- a/cpp/platform/base/medium_environment.h +++ b/cpp/platform/base/medium_environment.h @@ -8,6 +8,7 @@ #include "platform/api/webrtc.h" #include "platform/base/byte_array.h" #include "platform/base/listeners.h" +#include "platform/base/nsd_service_info.h" #include "platform/public/single_thread_executor.h" #include "absl/container/flat_hash_map.h" #include "absl/strings/string_view.h" @@ -179,7 +180,7 @@ class MediumEnvironment { // Updates advertising info to indicate the current medium is exposing // advertising event. void UpdateWifiLanMediumForAdvertising(api::WifiLanMedium& medium, - api::WifiLanService& service, + api::WifiLanService& wifi_lan_service, const std::string& service_id, bool enabled); @@ -212,8 +213,8 @@ class MediumEnvironment { const std::string& service_id); // Returns WiFi LAN service matching IP address and port, or nullptr. - api::WifiLanService* FindWifiLanService(const std::string& ip_address, - int port); + api::WifiLanService* GetWifiLanService(const std::string& ip_address, + int port); private: struct BluetoothMediumContext { @@ -261,7 +262,7 @@ class MediumEnvironment { bool fast_advertisement, bool enabled); void OnWifiLanServiceStateChanged(WifiLanMediumContext& info, - api::WifiLanService& service, + api::WifiLanService& wifi_lan_service, const std::string& service_id, bool enabled); diff --git a/cpp/platform/base/nsd_service_info.h b/cpp/platform/base/nsd_service_info.h new file mode 100644 index 00000000..9b1f7695 --- /dev/null +++ b/cpp/platform/base/nsd_service_info.h @@ -0,0 +1,67 @@ +#ifndef PLATFORM_BASE_NSD_SERVICE_INFO_H_ +#define PLATFORM_BASE_NSD_SERVICE_INFO_H_ + +#include + +#include "absl/container/flat_hash_map.h" + +namespace location { +namespace nearby { + +// https://developer.android.com/reference/android/net/nsd/NsdServiceInfo.html. +class NsdServiceInfo { + public: + NsdServiceInfo() = default; + NsdServiceInfo(const NsdServiceInfo&) = default; + NsdServiceInfo& operator=(const NsdServiceInfo&) = default; + NsdServiceInfo(NsdServiceInfo&&) = default; + NsdServiceInfo& operator=(NsdServiceInfo&&) = default; + ~NsdServiceInfo() = default; + + // Returns the packed string of |WifiLanServiceInfo|. + std::string GetServiceInfoName() const { return service_info_name_; } + + // Sets the packed string of |WifiLanServiceInfo|. + void SetServiceInfoName(std::string service_info_name) { + service_info_name_ = std::move(service_info_name); + } + + // Gets the TXTRecord value of the specified TXTRecord key assigned. + std::string GetTxtRecord(const std::string& txt_record_key) const { + if (txt_records_.empty()) return {}; + auto record = txt_records_.find(txt_record_key); + if (record == txt_records_.end()) return {}; + return record->second; + } + + // Adds the TXTRecord with a pair of key and value. + void SetTxtRecord(const std::string& txt_record_key, + const std::string& txt_record_value) { + txt_records_.emplace(txt_record_key, txt_record_value); + } + + // Returns the advertising device's as a pair. + // IP address is in byte sequence, in network order. + std::pair GetServiceAddress() const { + return std::make_pair(ip_address_, port_); + } + + // Sets the ip address and port of the local device. + void SetServiceAddress(const std::string& ip_address, int port) { + ip_address_ = ip_address; + port_ = port; + } + + bool IsValid() const { return !service_info_name_.empty(); } + + private: + std::string service_info_name_; + absl::flat_hash_map txt_records_; + std::string ip_address_; + int port_; +}; + +} // namespace nearby +} // namespace location + +#endif // PLATFORM_BASE_NSD_SERVICE_INFO_H_ diff --git a/cpp/platform/impl/g3/wifi_lan.cc b/cpp/platform/impl/g3/wifi_lan.cc index 3c9bf266..9da564ab 100644 --- a/cpp/platform/impl/g3/wifi_lan.cc +++ b/cpp/platform/impl/g3/wifi_lan.cc @@ -7,6 +7,7 @@ #include "platform/api/wifi_lan.h" #include "platform/base/logging.h" #include "platform/base/medium_environment.h" +#include "platform/base/nsd_service_info.h" #include "platform/base/prng.h" #include "absl/synchronization/mutex.h" @@ -58,7 +59,7 @@ Exception WifiLanSocket::Close() { WifiLanService* WifiLanSocket::GetRemoteWifiLanService() { absl::MutexLock lock(&mutex_); - return service_; + return wifi_lan_service_; } void WifiLanSocket::DoClose() { @@ -87,7 +88,7 @@ OutputStream& WifiLanSocket::GetLocalOutputStream() { } std::unique_ptr WifiLanServerSocket::Accept( - WifiLanService* service) { + WifiLanService* wifi_lan_service) { absl::MutexLock lock(&mutex_); if (closed_) return {}; while (pending_sockets_.empty()) { @@ -98,7 +99,7 @@ std::unique_ptr WifiLanServerSocket::Accept( auto* remote_socket = pending_sockets_.extract(pending_sockets_.begin()).value(); CHECK(remote_socket); - auto local_socket = std::make_unique(service); + auto local_socket = std::make_unique(wifi_lan_service); local_socket->Connect(*remote_socket); remote_socket->Connect(*local_socket); cond_.SignalAll(); @@ -156,22 +157,13 @@ Exception WifiLanServerSocket::DoClose() { } WifiLanMedium::WifiLanMedium() { - service_.SetMedium(this); - std::string ip_address; - ip_address.resize(4); - uint32_t raw_ip_addr = Prng().NextUint32(); - uint16_t port = Prng().NextUint32(); - ip_address[0] = static_cast(raw_ip_addr >> 24); - ip_address[1] = static_cast(raw_ip_addr >> 16); - ip_address[2] = static_cast(raw_ip_addr >> 8); - ip_address[3] = static_cast(raw_ip_addr >> 0); - service_.SetServiceAddress(ip_address, port); + wifi_lan_service_.SetMedium(this); auto& env = MediumEnvironment::Instance(); env.RegisterWifiLanMedium(*this); } WifiLanMedium::~WifiLanMedium() { - service_.SetMedium(nullptr); + wifi_lan_service_.SetMedium(nullptr); auto& env = MediumEnvironment::Instance(); env.UnregisterWifiLanMedium(*this); @@ -191,15 +183,17 @@ WifiLanMedium::~WifiLanMedium() { } bool WifiLanMedium::StartAdvertising(const std::string& service_id, - const std::string& service_info_name, - const std::string& endpoint_info_name) { + const NsdServiceInfo& nsd_service_info) { NEARBY_LOG(INFO, - "G3 WifiLan StartAdvertising: service_id=%s, service_info_name=%s", - service_id.c_str(), service_info_name.c_str()); + "G3 WifiLan StartAdvertising: service_id=%s, nsd_service_info=%p, " + "service_info_name=%s", + service_id.c_str(), &nsd_service_info, + nsd_service_info.GetServiceInfoName().c_str()); auto& env = MediumEnvironment::Instance(); - service_.SetServiceName(service_info_name); - service_.SetTxtRecord("n", endpoint_info_name); - env.UpdateWifiLanMediumForAdvertising(*this, service_, service_id, true); + NsdServiceInfo local_nsd_service_info{nsd_service_info}; + SetWifiLanService(nsd_service_info); + env.UpdateWifiLanMediumForAdvertising(*this, wifi_lan_service_, service_id, + true); absl::MutexLock lock(&mutex_); if (server_socket_ != nullptr) server_socket_.release(); @@ -209,7 +203,7 @@ bool WifiLanMedium::StartAdvertising(const std::string& service_id, accept_loops_runner_.Execute([&env, this, service_id]() mutable { if (!accept_loops_runner_.InShutdown()) { while (true) { - auto client_socket = server_socket_->Accept(&service_); + auto client_socket = server_socket_->Accept(&wifi_lan_service_); if (client_socket == nullptr) break; env.CallWifiLanAcceptedConnectionCallback( *this, *(client_socket.release()), service_id); @@ -236,7 +230,8 @@ bool WifiLanMedium::StopAdvertising(const std::string& service_id) { } auto& env = MediumEnvironment::Instance(); - env.UpdateWifiLanMediumForAdvertising(*this, service_, service_id, false); + env.UpdateWifiLanMediumForAdvertising(*this, wifi_lan_service_, service_id, + false); accept_loops_runner_.Shutdown(); if (server_socket_ == nullptr) { NEARBY_LOGS(ERROR) << "G3 WifiLan StopAdvertising: failed to find WifiLan " @@ -308,39 +303,46 @@ bool WifiLanMedium::StopAcceptingConnections(const std::string& service_id) { } std::unique_ptr WifiLanMedium::Connect( - api::WifiLanService& remote_service, const std::string& service_id) { - NEARBY_LOG(INFO, - "G3 WifiLan Connect: medium=%p, service=%p, service_info_name=%s, " - "service_id=%s", - this, &service_, remote_service.GetServiceName().c_str(), - service_id.c_str()); + api::WifiLanService& remote_wifi_lan_service, + const std::string& service_id) { + NEARBY_LOG( + INFO, + "G3 WifiLan Connect: medium=%p, wifi_lan_service=%p, " + "service_info_name=%s, service_id=%s", + this, &wifi_lan_service_, + remote_wifi_lan_service.GetServiceInfo().GetServiceInfoName().c_str(), + service_id.c_str()); // First, find an instance of remote medium, that exposed this service. - auto* medium = static_cast(remote_service).GetMedium(); + auto* remote_medium = + static_cast(remote_wifi_lan_service).GetMedium(); - if (!medium) return {}; // Can't find medium. Bail out. + if (!remote_medium) return {}; // Can't find medium. Bail out. WifiLanServerSocket* remote_server_socket = nullptr; - NEARBY_LOG(INFO, - "G3 WifiLan Connect [peer]: medium=%p, service=%p, " - "service_info_name=%s, service_id=%s", - medium, &remote_service, remote_service.GetServiceName().c_str(), - service_id.c_str()); + NEARBY_LOG( + INFO, + "G3 WifiLan Connect [peer]: remote_wifi_lan_service=%p, " + "remote_service_info_name=%s, service_id=%s", + &remote_wifi_lan_service, + remote_wifi_lan_service.GetServiceInfo().GetServiceInfoName().c_str(), + service_id.c_str()); // Then, find our server socket context in this medium. { - absl::MutexLock medium_lock(&medium->mutex_); - remote_server_socket = medium->server_socket_.get(); + absl::MutexLock medium_lock(&remote_medium->mutex_); + remote_server_socket = remote_medium->server_socket_.get(); if (remote_server_socket == nullptr) { - NEARBY_LOG(ERROR, - "G3 WifiLan Connect: Failed to find WifiLan Server socket: " - "service_id=%s", - service_id.c_str()); + NEARBY_LOGS(ERROR) + << "G3 WifiLan Connect: Failed to find remote WifiLan Server socket: " + "service_id=" + << service_id; // Fall through for server socket not found. return {}; } } - WifiLanService service = static_cast(remote_service); - auto socket = std::make_unique(&service); + WifiLanService wifi_lan_service = + static_cast(remote_wifi_lan_service); + auto socket = std::make_unique(&wifi_lan_service); // Finally, Request to connect to this socket. if (!remote_server_socket->Connect(*socket)) { NEARBY_LOG(ERROR, @@ -354,17 +356,38 @@ std::unique_ptr WifiLanMedium::Connect( return socket; } -api::WifiLanService* WifiLanMedium::FindRemoteService( +api::WifiLanService* WifiLanMedium::GetRemoteService( const std::string& ip_address, int port) { auto& env = MediumEnvironment::Instance(); - return env.FindWifiLanService(ip_address, port); + return env.GetWifiLanService(ip_address, port); } std::pair WifiLanMedium::GetServiceAddress( const std::string& service_id) { NEARBY_LOGS(INFO) << "G3 WifiLan GetServiceAddress: service_id=" << service_id; - return service_.GetServiceAddress(); + return wifi_lan_service_.GetServiceInfo().GetServiceAddress(); +} + +void WifiLanMedium::SetWifiLanService(const NsdServiceInfo& nsd_service_info) { + NsdServiceInfo local_nsd_service_info{nsd_service_info}; + auto service_address = GetFakeServiceAddress(); + local_nsd_service_info.SetServiceAddress(service_address.first, + service_address.second); + wifi_lan_service_.SetServiceInfo(local_nsd_service_info); +} + +std::pair WifiLanMedium::GetFakeServiceAddress() const { + std::string ip_address; + ip_address.resize(4); + uint32_t raw_ip_addr = Prng().NextUint32(); + uint16_t port = Prng().NextUint32(); + ip_address[0] = static_cast(raw_ip_addr >> 24); + ip_address[1] = static_cast(raw_ip_addr >> 16); + ip_address[2] = static_cast(raw_ip_addr >> 8); + ip_address[3] = static_cast(raw_ip_addr >> 0); + + return std::make_pair(ip_address, port); } } // namespace g3 diff --git a/cpp/platform/impl/g3/wifi_lan.h b/cpp/platform/impl/g3/wifi_lan.h index 7fd4af0c..8a541c6e 100644 --- a/cpp/platform/impl/g3/wifi_lan.h +++ b/cpp/platform/impl/g3/wifi_lan.h @@ -8,6 +8,7 @@ #include "platform/api/wifi_lan.h" #include "platform/base/byte_array.h" #include "platform/base/input_stream.h" +#include "platform/base/nsd_service_info.h" #include "platform/base/output_stream.h" #include "platform/impl/g3/multi_thread_executor.h" #include "platform/impl/g3/pipe.h" @@ -21,39 +22,18 @@ namespace g3 { class WifiLanMedium; -// Opaque wrapper over a WifiLan service which contains packed -// |WifiLanServiceInfo| string name. +// Opaque wrapper over a WifiLan service which contains |NsdServiceInfo|. class WifiLanService : public api::WifiLanService { public: - explicit WifiLanService(std::string service_info_name) - : service_info_name_(std::move(service_info_name)) {} + WifiLanService() = default; + explicit WifiLanService(NsdServiceInfo nsd_service_info) + : nsd_service_info_(std::move(nsd_service_info)) {} ~WifiLanService() override = default; - std::string GetServiceName() const override { return service_info_name_; } + NsdServiceInfo GetServiceInfo() const override { return nsd_service_info_; } - void SetServiceName(std::string service_info_name) { - service_info_name_ = std::move(service_info_name); - } - - std::string GetTxtRecord(const std::string& txt_record_key) const override { - if (txt_records_.empty()) return {}; - auto record = txt_records_.find(txt_record_key); - if (record == txt_records_.end()) return {}; - return record->second; - } - - void SetTxtRecord(const std::string& txt_record_key, - const std::string& txt_record_value) { - txt_records_.emplace(txt_record_key, txt_record_value); - } - - std::pair GetServiceAddress() const override { - return std::make_pair(ip_address_, port_); - } - - void SetServiceAddress(const std::string& ip_address, int port) { - ip_address_ = ip_address; - port_ = port; + void SetServiceInfo(NsdServiceInfo nsd_service_info) { + nsd_service_info_ = std::move(nsd_service_info); } WifiLanMedium* GetMedium() { return medium_; } @@ -61,17 +41,15 @@ class WifiLanService : public api::WifiLanService { void SetMedium(WifiLanMedium* medium) { medium_ = medium; } private: - std::string service_info_name_; - absl::flat_hash_map txt_records_; + NsdServiceInfo nsd_service_info_; WifiLanMedium* medium_ = nullptr; - std::string ip_address_; - int port_; }; class WifiLanSocket : public api::WifiLanSocket { public: WifiLanSocket() = default; - explicit WifiLanSocket(WifiLanService* service) : service_(service) {} + explicit WifiLanSocket(WifiLanService* wifi_lan_service) + : wifi_lan_service_(wifi_lan_service) {} ~WifiLanSocket() override; // Connect to another WifiLanSocket, to form a functional low-level channel. @@ -124,7 +102,7 @@ class WifiLanSocket : public api::WifiLanSocket { std::shared_ptr output_{new Pipe}; std::shared_ptr input_; mutable absl::Mutex mutex_; - WifiLanService* service_; + WifiLanService* wifi_lan_service_; WifiLanSocket* remote_socket_ ABSL_GUARDED_BY(mutex_) = nullptr; bool closed_ ABSL_GUARDED_BY(mutex_) = false; }; @@ -182,8 +160,7 @@ class WifiLanMedium : public api::WifiLanMedium { ~WifiLanMedium() override; bool StartAdvertising(const std::string& service_id, - const std::string& service_info_name, - const std::string& endpoint_info_name) override + const NsdServiceInfo& nsd_service_info) override ABSL_LOCKS_EXCLUDED(mutex_); bool StopAdvertising(const std::string& service_id) override ABSL_LOCKS_EXCLUDED(mutex_); @@ -212,11 +189,11 @@ class WifiLanMedium : public api::WifiLanMedium { // On success, returns a new WifiLanSocket. // On error, returns nullptr. std::unique_ptr Connect( - api::WifiLanService& remote_service, + api::WifiLanService& remote_wifi_lan_service, const std::string& service_id) override ABSL_LOCKS_EXCLUDED(mutex_); - api::WifiLanService* FindRemoteService(const std::string& ip_address, - int port) override; + api::WifiLanService* GetRemoteService(const std::string& ip_address, + int port) override; std::pair GetServiceAddress( const std::string& service_id) override ABSL_LOCKS_EXCLUDED(mutex_); @@ -238,8 +215,11 @@ class WifiLanMedium : public api::WifiLanMedium { std::string service_id; }; + void SetWifiLanService(const NsdServiceInfo& nsd_service_info); + std::pair GetFakeServiceAddress() const; + absl::Mutex mutex_; - WifiLanService service_{"unknown G3 WifiLan service"}; + WifiLanService wifi_lan_service_; // A thread pool dedicated to running all the accept loops from // StartAdvertising(). diff --git a/cpp/platform/public/wifi_lan.cc b/cpp/platform/public/wifi_lan.cc index 62814e02..00f3b00b 100644 --- a/cpp/platform/public/wifi_lan.cc +++ b/cpp/platform/public/wifi_lan.cc @@ -7,10 +7,8 @@ namespace location { namespace nearby { bool WifiLanMedium::StartAdvertising(const std::string& service_id, - const std::string& service_info_name, - const std::string& endpoint_info_name) { - return impl_->StartAdvertising(service_id, service_info_name, - endpoint_info_name); + const NsdServiceInfo& nsd_service_info) { + return impl_->StartAdvertising(service_id, nsd_service_info); } bool WifiLanMedium::StopAdvertising(const std::string& service_id) { @@ -28,40 +26,46 @@ bool WifiLanMedium::StartDiscovery(const std::string& service_id, service_id, { .service_discovered_cb = - [this](api::WifiLanService& service, + [this](api::WifiLanService& wifi_lan_service, const std::string& service_id) { MutexLock lock(&mutex_); auto pair = services_.emplace( - &service, absl::make_unique()); + &wifi_lan_service, + absl::make_unique()); auto& context = *pair.first->second; if (!pair.second) { NEARBY_LOG(INFO, "Discovering (again) service=%p, impl=%p, " "service_info_name=%s", - &context.service, &service, - service.GetServiceName().c_str()); + &context.wifi_lan_service, &wifi_lan_service, + wifi_lan_service.GetServiceInfo() + .GetServiceInfoName() + .c_str()); } else { - context.service = WifiLanService(&service); - NEARBY_LOG(INFO, - "Discovering service=%p, impl=%p, " - "service_info_name=%s", - &context.service, &service, - service.GetServiceName().c_str()); + context.wifi_lan_service = WifiLanService(&wifi_lan_service); + NEARBY_LOG( + INFO, + "Discovering wifi_lan_service=%p, service_info_name=%s", + &wifi_lan_service, + wifi_lan_service.GetServiceInfo() + .GetServiceInfoName() + .c_str()); } discovered_service_callback_.service_discovered_cb( - context.service, service_id); + context.wifi_lan_service, service_id); }, .service_lost_cb = - [this](api::WifiLanService& service, + [this](api::WifiLanService& wifi_lan_service, const std::string& service_id) { MutexLock lock(&mutex_); if (services_.empty()) return; - auto context = services_.find(&service); + auto context = services_.find(&wifi_lan_service); if (context == services_.end()) return; - NEARBY_LOG(INFO, "Removing service=%p, impl=%p", - &(context->second->service), &service); + NEARBY_LOG(INFO, "Removing wifi_lan_service=%p, impl=%p", + &(context->second->wifi_lan_service), + &wifi_lan_service); discovered_service_callback_.service_lost_cb( - context->second->service, service_id); + context->second->wifi_lan_service, service_id); }, }); } @@ -117,18 +121,19 @@ bool WifiLanMedium::StopAcceptingConnections(const std::string& service_id) { return impl_->StopAcceptingConnections(service_id); } -WifiLanSocket WifiLanMedium::Connect(WifiLanService& service, +WifiLanSocket WifiLanMedium::Connect(WifiLanService& wifi_lan_service, const std::string& service_id) { NEARBY_LOG( INFO, "WifiLanMedium::Connect: service=%p [impl=%p, service_info_name=%s]", - &service, &service.GetImpl(), service.GetServiceName().c_str()); - return WifiLanSocket(impl_->Connect(service.GetImpl(), service_id)); + &wifi_lan_service, &wifi_lan_service.GetImpl(), + wifi_lan_service.GetServiceInfo().GetServiceInfoName().c_str()); + return WifiLanSocket(impl_->Connect(wifi_lan_service.GetImpl(), service_id)); } -WifiLanService WifiLanMedium::FindRemoteService(const std::string& ip_address, +WifiLanService WifiLanMedium::GetRemoteService(const std::string& ip_address, int port) { - return WifiLanService(impl_->FindRemoteService(ip_address, port)); + return WifiLanService(impl_->GetRemoteService(ip_address, port)); } std::pair WifiLanMedium::GetServiceAddress( diff --git a/cpp/platform/public/wifi_lan.h b/cpp/platform/public/wifi_lan.h index e0334526..8472267e 100644 --- a/cpp/platform/public/wifi_lan.h +++ b/cpp/platform/public/wifi_lan.h @@ -5,6 +5,7 @@ #include "platform/api/wifi_lan.h" #include "platform/base/byte_array.h" #include "platform/base/input_stream.h" +#include "platform/base/nsd_service_info.h" #include "platform/base/output_stream.h" #include "platform/public/mutex.h" #include "absl/container/flat_hash_map.h" @@ -12,8 +13,7 @@ namespace location { namespace nearby { -// Opaque wrapper over a WifiLan service which contains packed -// |WifiLanServiceInfo| string name and the TXT Record. +// Opaque wrapper over a WifiLan service which contains |NsdServiceInfo|. class WifiLanService final { public: WifiLanService() = default; @@ -22,12 +22,7 @@ class WifiLanService final { explicit WifiLanService(api::WifiLanService* service) : impl_(service) {} ~WifiLanService() = default; - std::string GetServiceName() const { return impl_->GetServiceName(); } - - std::string GetTxtRecord(const std::string& key) const { - return impl_->GetTxtRecord(key); - } - + NsdServiceInfo GetServiceInfo() const { return impl_->GetServiceInfo(); } api::WifiLanService& GetImpl() { return *impl_; } bool IsValid() const { return impl_ != nullptr; } @@ -105,7 +100,7 @@ class WifiLanMedium final { }; struct ServiceDiscoveryInfo { - WifiLanService service; + WifiLanService wifi_lan_service; }; struct AcceptedConnectionCallback { @@ -121,8 +116,7 @@ class WifiLanMedium final { ~WifiLanMedium() = default; bool StartAdvertising(const std::string& service_id, - const std::string& service_info_name, - const std::string& endpoint_info_name); + const NsdServiceInfo& nsd_service_info); bool StopAdvertising(const std::string& service_id); // Returns true once the WifiLan discovery has been initiated. @@ -142,13 +136,14 @@ class WifiLanMedium final { // Returns a new WifiLanSocket. On Success, WifiLanSocket::IsValid() // returns true. - WifiLanSocket Connect(WifiLanService& service, const std::string& service_id); + WifiLanSocket Connect(WifiLanService& wifi_lan_service, + const std::string& service_id); bool IsValid() const { return impl_ != nullptr; } api::WifiLanMedium& GetImpl() { return *impl_; } - WifiLanService FindRemoteService(const std::string& ip_address, int port); + WifiLanService GetRemoteService(const std::string& ip_address, int port); std::pair GetServiceAddress(const std::string& service_id); diff --git a/cpp/platform/public/wifi_lan_test.cc b/cpp/platform/public/wifi_lan_test.cc index 11796882..081f023e 100644 --- a/cpp/platform/public/wifi_lan_test.cc +++ b/cpp/platform/public/wifi_lan_test.cc @@ -16,6 +16,7 @@ namespace { constexpr absl::string_view kServiceID{"com.google.location.nearby.apps.test"}; constexpr absl::string_view kServiceInfoName{"Simulated service info name"}; constexpr absl::string_view kEndpointName{"Simulated endpoint name"}; +constexpr absl::string_view kEndpointInfoKey{"n"}; class WifiLanMediumTest : public ::testing::Test { protected: @@ -50,7 +51,11 @@ TEST_F(WifiLanMediumTest, CanStartAdvertising) { std::string endpoint_info_name{kEndpointName}; CountDownLatch found_latch(1); - wifi_a.StartAdvertising(service_id, service_info_name, endpoint_info_name); + NsdServiceInfo nsd_service_info; + nsd_service_info.SetServiceInfoName(service_info_name); + nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey), + endpoint_info_name); + wifi_a.StartAdvertising(service_id, nsd_service_info); EXPECT_TRUE(wifi_b.StartDiscovery( service_id, DiscoveredServiceCallback{ @@ -89,8 +94,12 @@ TEST_F(WifiLanMediumTest, CanStartDiscovery) { lost_latch.CountDown(); }, }); - EXPECT_TRUE(wifi_b.StartAdvertising(service_id, service_info_name, - endpoint_info_name)); + + NsdServiceInfo nsd_service_info; + nsd_service_info.SetServiceInfoName(service_info_name); + nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey), + endpoint_info_name); + EXPECT_TRUE(wifi_b.StartAdvertising(service_id, nsd_service_info)); EXPECT_TRUE(found_latch.Await(absl::Milliseconds(1000)).result()); EXPECT_TRUE(wifi_b.StopAdvertising(service_id)); EXPECT_TRUE(lost_latch.Await(absl::Milliseconds(1000)).result()); @@ -121,8 +130,13 @@ TEST_F(WifiLanMediumTest, CanStopDiscovery) { lost_latch.CountDown(); }, }); - EXPECT_TRUE(wifi_b.StartAdvertising(service_id, service_info_name, - endpoint_info_name)); + + NsdServiceInfo nsd_service_info; + nsd_service_info.SetServiceInfoName(service_info_name); + nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey), + endpoint_info_name); + + EXPECT_TRUE(wifi_b.StartAdvertising(service_id, nsd_service_info)); EXPECT_TRUE(found_latch.Await(absl::Milliseconds(1000)).result()); EXPECT_TRUE(wifi_a.StopDiscovery(service_id)); EXPECT_TRUE(wifi_b.StopAdvertising(service_id)); @@ -147,13 +161,20 @@ TEST_F(WifiLanMediumTest, CanStartAcceptingConnectionsAndConnect) { .service_discovered_cb = [&found_latch, &discovered_service]( WifiLanService& service, const std::string& service_id) { - NEARBY_LOG(INFO, "Service discovered: %s, %p", - service.GetServiceName().c_str(), &service); + NEARBY_LOG( + INFO, "Service discovered: %s, %p", + service.GetServiceInfo().GetServiceInfoName().c_str(), + &service); discovered_service = &service; found_latch.CountDown(); }, }); - wifi_b.StartAdvertising(service_id, service_info_name, endpoint_info_name); + + NsdServiceInfo nsd_service_info; + nsd_service_info.SetServiceInfoName(service_info_name); + nsd_service_info.SetTxtRecord(std::string(kEndpointInfoKey), + endpoint_info_name); + wifi_b.StartAdvertising(service_id, nsd_service_info); wifi_b.StartAcceptingConnections( service_id, AcceptedConnectionCallback{ diff --git a/proto/BUILD b/proto/BUILD index 6b1483d9..fce5a879 100644 --- a/proto/BUILD +++ b/proto/BUILD @@ -40,21 +40,6 @@ java_proto_library( deps = [":discovery_enums_proto"], ) -proto_library( - name = "error_code_enums_proto", - srcs = ["error_code_enums.proto"], - cc_api_version = 2, - compatible_with = ["//buildenv/target:appengine"], - deps = [ - "//logs/proto/logs_annotations", - ], -) - -java_lite_proto_library( - name = "error_code_enums_java_proto_lite", - deps = [":error_code_enums_proto"], -) - proto_library( name = "connections_enums_proto", srcs = ["connections_enums.proto"], diff --git a/proto/connections/offline_wire_formats.proto b/proto/connections/offline_wire_formats.proto index a3cd2f8b..ef8350d5 100644 --- a/proto/connections/offline_wire_formats.proto +++ b/proto/connections/offline_wire_formats.proto @@ -262,6 +262,8 @@ message MediumMetadata { optional bool supports_5_ghz = 1; // WiFi Lan BSSID optional string bssid = 2; + // IP address + optional bytes ip_address = 3; } // LocationHint is used to specify a location as well as format. diff --git a/proto/connections_enums.proto b/proto/connections_enums.proto index c9259767..599219f8 100644 --- a/proto/connections_enums.proto +++ b/proto/connections_enums.proto @@ -44,6 +44,9 @@ enum EventType { // Corresponds to the end of a StrategySession. STOP_STRATEGY_SESSION = 5; + + // Logs when the error happen. + ERROR_CODE = 6; } // The strategy used for a session of Nearby.Connections. diff --git a/proto/error_code_enums.proto b/proto/error_code_enums.proto deleted file mode 100644 index 302c30d1..00000000 --- a/proto/error_code_enums.proto +++ /dev/null @@ -1,434 +0,0 @@ -syntax = "proto2"; - -package location.nearby.proto; - -import "logs/proto/logs_annotations/logs_annotations.proto"; - -option optimize_for = LITE_RUNTIME; -option (logs_proto.file_not_used_for_logging_except_enums) = true; -option java_api_version = 2; -option java_package = "com.google.location.nearby.proto"; -option java_outer_classname = "ErrorCodeEnums"; -option objc_class_prefix = "GNCP"; - -// The type of the error. -// It help to sort error codes to different types to analyze and also impact the -// logcat print it as Warning or Severe. -enum ErrorType { - UNKNOWN_TYPE = 0; - - // The error should not happen on production, it's like the input is null or - // not invalid or it's a unexpected API call. For example, start advertising - // with empty service ID or start advertising with the same service ID twice. - DEVELOPING = 1; - - // It’s about the device's capabilities, some devices may not support the - // feature Nearby used. E.g. The device not support BLE advertising - DEVICE = 2; - - // The failure return from the system or library API we used to communicate - // with the medium. E.g. get null OS objects or call the API but get a - // negative return value which indicates that the system does not allow to do - // that now. - SYSTEM = 3; - - // The network related failure. E.g. get an EOF exception while reading pipe - // or fail to create connection. - NETWORK = 4; - - // This may not be a failure, it can be the things we are interested in, like - // to count how many BLE advertisements the device received in a specified - // period and how many different advertisements in it, it can help us to know - // the user under a clean or dirty environment. - OTHERS = 5; -} - -// The event which the error occurs on. -enum Event { - UNKNOWN_EVENT = 0; - START_ADVERTISING = 1; - STOP_ADVERTISING = 2; - START_LISTENING_INCOMING_CONNECTION = 3; - STOP_LISTENING_INCOMING_CONNECTION = 4; - START_DISCOVERING = 5; - STOP_DISCOVERING = 6; - CONNECT = 7; - DISCONNECT = 8; - ACCEPT_CONNECTION = 9; - REJECT_CONNECTION = 10; - SEND_PAYLOAD = 11; - CANCEL_PAYLOAD = 12; - RECEIVE_PAYLOAD = 13; -} - -// The error to identify the common failure for all mediums. The range between 0 -// and 30. -enum CommonError { - UNKNOWN_ERROR = 0; - - // The common error for all mediums, the range between 0 and 30. - - // Developing error, the input with invalid format or empty. - INVALID_PARAMETER = 1; - // Device error, the medium not available on this device. - MEDIUM_NOT_AVAILABLE = 2; - // System error, the medium in the unexpected state, e.g. we have check the - // medium is on, after then it suddently off and cause Nearby - // Connection failed. - UNEXPECTED_MEDIUM_STATE = 3; - // System error, the medim not available because the resource ran out. e.g. - // the Wi-Fi Direct initialized cause Wi-Fi Aware not available, or BLE - // connections hit the maximan number, or Wi-Fi Hotstop already created. - OUT_OF_RESOURCE = 4; - // Others error, the error happens when user cancel the flow, it's not a - // real failure. - FLOW_CANCELED = 5; - // Developing error, an unexpect call that the medium not ready, need to do - // something before this call. e.g. call WifiAwareImpli#connectToSocket but - // never join network before this call. - UNEXPECTED_CALL = 6; - - // Reserved 7 to 30 -} - -// The error for event START_ADVERTISING. The range between 31 and 99. -enum StartAdvertisingError { - reserved 37, 39; - - // Developing error, not allow to advertising fast pair model id and sharing - // fast advertisement at the same time, they are both use fast - // advertisement, and only allow 1 fast advertisement at the same time. - MULTIPLE_FAST_ADVERTISEMENT_NOT_ALLOWED = 31; - // System error, there's already someone advertising fast advertisement, not - // allow to start another one. - FAST_ADVERTISEMENT_ALREADY_ADVERTISED = 32; - // Developing error, this service ID already requested, should not request - // it again without stop advertising. - DUPLICATE_ADVERTISING_REQUESTED = 33; - // System error, failed to start GATT server - START_GATT_SERVER_FAILED = 34; - // System error, all advertising slot ran out, can't available for new - // regular advertisement. - BLE_MAX_GATT_ADVERTISEMENT_SLOT_REACHED = 35; - // System error, failed to start advertising for legacy advertisements on BLE - START_LEGACY_ADVERTISING_FAILED = 36; - // System error, failed to start advertising for extended advertisements on - // BLE - START_EXTENDED_ADVERTISING_FAILED = 38; - // System error, there's already someone advertising on Bluetooth, not allow - // to start another one. - BLUETOOTH_ALREADY_ADVERTISED = 40; - // System error, failed to modify the Bluetooth name. - MODIFY_BLUETOOTH_NAME_FAILED = 41; - // System error, failed to persist the original Bluetooth name into shared - // preference. - PERSIST_ORIGINAL_BLUETOOTH_NAME_FAILED = 42; - // System error, failed to start advertising. - START_ADVERTISING_FAILED = 43; - - // Developing error, not allow to advertising on Wi-Fi Lan(TDLS) without - // accetpting connections. The connection may comes in very quickly, so need - // to accetpt connections before advertising. - SHOULD_ACCEPT_CONNECTIONS_BEFORE_ADVERTISING_ON_WIFI_LAN = 44; - // System error, failed to acquire WifiAwareSession - ACQUIRE_WIFI_AWARE_SESSION_FAILED = 45; - - // Next ID :46 -} - -// The error for event STOP_ADVERTISING. The range between 31 and 99. -enum StopAdvertisingError { - // System error, failed to stop advertising. - STOP_ADVERTISING_FAILED = 31; - // System error, failed to modify the Bluetooth name. - RESTORE_BLUETOOTH_NAME_FAILED = 32; - // System error, failed to stop advertising for BLE legacy advertisements. - STOP_LEGACY_ADVERTISING_FAILED = 33; - // System error, failed to stop advertising for BLE extended advertisements. - STOP_EXTENDED_ADVERTISING_FAILED = 34; - - // Next ID :35 -} - -// The error for event START_DISCOVERING. The range between 31 and 99. -enum StartDiscoveringError { - // Developing error, this service ID already requested, should not request it - // again without stop discovering. - DUPLICATE_DISCOVERING_REQUESTED = 31; - // System error, failed to start discovering for legacy advertisements on BLE - START_LEGACY_DISCOVERING_FAILED = 32; - // System error, failed to start discovering for extended advertisements on - // BLE - START_EXTENDED_DISCOVERING_FAILED = 33; - // System error, failed to start discovering. - START_DISCOVERING_FAILED = 34; - // Network error, invalid remote target info, discover the nearby devices but - // the information not valid. - INVALID_TARGET_INFO = 35; - // Network error, failed to fetch the advertisement from the remote devices. - FETCH_ADVERTISEMENT_FAILED = 36; - // Network error, failed to fetch the advertisement via GATT from the remote - // devices. - GATT_FETCH_ADVERTISEMENT_FAILED = 37; - // Network error, failed to fetch the advertisement via L2CAP from the remote - // devices. - L2CAP_FETCH_ADVERTISEMENT_FAILED = 38; - // System error, the medium not available when trying to fetch advertisements. - // e.g. fetch advertisements but BT disabled unexpectedly. - NOT_AVAILABLE_TO_FETCH_ADVERTISEMENT = 39; - // System error, failed to acquire WifiAwareSession - ACQUIRE_WIFI_AWARE_SESSION_FOR_DISCOVERING_FAILED = 40; - - // Next ID :41 -} - -// The error for event STOP_DISCOVERING. The range between 31 and 99. -enum StopDiscoveringError { - // System error, failed to stop discovering. - STOP_DISCOVERING_FAILED = 31; - // System error, failed to stop discovering for BLE legacy scanning. - STOP_LEGACY_DISCOVERING_FAILED = 32; - // System error, failed to stop discovering for BLE extended scanning. - STOP_EXTENDED_DISCOVERING_FAILED = 33; - - // Next ID :34 -} - -// The error for event START_LISTENING_INCOMING_CONNECTION. The range between 31 -// and 99. -enum StartListeningIncomingConnectionError { - // Developing error, this service ID already requested, should not request it - // again without stop accepting. - DUPLICATE_ACCEPTING_CONNECTION_REQUESTED = 31; - // System error, failed to open a GATT server for listening incoming GATT - // connection. - OPEN_GATT_SERVER_FAILED = 32; - // System error, failed to accept the incoming GATT connection - ACCEPT_GATT_CONNECTION_FAILED = 33; - // System error, failed to accept the incoming L2CAP connection - ACCEPT_L2CAP_CONNECTION_FAILED = 34; - // Network error, wait the GATT connection ready after the connection - // established but never. - CREATE_GATT_SERVER_SOCKET_NOT_READY = 35; - // System error, failed to accept the incoming connection - ACCEPT_CONNECTION_FAILED = 36; - // System error, failed to create a server socket for listening incoming - // connection. - CREATE_SERVER_SOCKET_FAILED = 37; - - // Next ID :36 -} - -// The error for event STOP_LISTENING_INCOMING_CONNECTION. The range between 31 -// and 99. -enum StopListeningIncomingConnectionError { - // System error, failed to stop accepting the incoming connection - STOP_ACCEPTING_CONNECTION_FAILED = 31; - - // Next ID :32 -} - -// The error for event CONNECT. The range between 31 and 99. -enum ConnectError { - // Network error, failed to connect to remote device because we lost the - // target without MAC address to connect to. e.g. BLE cache MAC address in - // medium, it may lost when just try to connect. - UNEXPECT_TARGET_LOST = 31; - // System error, failed to establish connection on GATT - ESTABLISH_GATT_CONNECTION_FAILED = 32; - // System error, failed to establish connection on L2CAP - ESTABLISH_L2CAP_CONNECTION_FAILED = 33; - // Developing error, the MAC address not valid for connecting - INVALID_MAC_ADDRESS = 34; - // Others error, unexpected interrupt when sleep before connect GATT for - // waiting GATT server ready. Should not hapepen, it may be the process be - // killed. - SLEEP_BEFORE_CONNECT_GATT_INTERRUPTED = 35; - // Others error, unexpected interrupt when sleep after GATT connect to wait - // GATT connection ready to transfer data. Should not hapepen, it may be - // the process be killed. - SLEEP_AFTER_GATT_CONNECTED_INTERRUPTED = 36; - // Network error, failed to configure the GATT connection priority, it may - // failed when the connection still wait for the status update from network or - // just a RemoteException. - REQUEST_GATT_CONNECTION_PRIORITY_FAILED = 37; - // Network error, failed to change connection for data transferring on L2CAP - // connection. - L2CAP_SWITCH_TO_DATA_TRANSFERRING_FAILED = 38; - // Network error, failed to change connection for data transferring on GATT - // connection. - GATT_SWITCH_TO_DATA_TRANSFERRING_FAILED = 39; - // System error, failed to establish connection - ESTABLISH_CONNECTION_FAILED = 40; - // Developing error, this connection already established, should not request - // it again. - DUPLICATE_CONNECTION_REQUESTED = 41; - // Network error, the connection lost. - CONNECTION_LOST = 42; - // Network error, failed to connect to the network. e.g. an aware network, - // hotspot or a direct network. - CONNECT_TO_NETWORK_FAILED = 43; - - // Next ID :44 -} - -// The error for event DISCONNECT. The range between 31 and 99. -enum DisconnectError { - // System error, failed to disconnect the network. - DISCONNECT_NETWORK_FAILED = 31; - - // Next ID :32 -} - -enum Description { - reserved 28, 29; - - UNKNOWN = 0; - NULL_SERVICE_ID = 1; - NULL_ADVERTISEMENT_BYTES = 2; - CONNECTIONS_FEATURE_DISABLED = 3; - STALE_SDK_VERSION = 4; - FEATURE_BLUETOOTH_NOT_SUPPORTED = 5; - FEATURE_BLUETOOTH_LE_NOT_SUPPORTED = 6; - NULL_BLUETOOTH_MANAGER = 7; - NULL_BLUETOOTH_ADAPTER = 8; - INVALID_FAST_PAIR_MODEL_ID = 9; - INVALID_FAST_ADVERTISEMENT_DATA = 10; - INVALID_ADVERTISEMENT_HEADER_DATA = 11; - INVALID_REGULAR_ADVERTISEMENT_DATA = 12; - NULL_BLUETOOTH_LE_ADVERTISER_COMPAT = 13; - ADVERTISE_FAILED_ALREADY_STARTED = 14; - ADVERTISE_FAILED_DATA_TOO_LARGE = 15; - ADVERTISE_FAILED_FEATURE_UNSUPPORTED = 16; - ADVERTISE_FAILED_INTERNAL_ERROR = 17; - ADVERTISE_FAILED_TOO_MANY_ADVERTISERS = 18; - INTERRUPTED_EXCEPTION = 19; - EXECUTION_EXCEPTION = 20; - NULL_BLUETOOTH_DEVICE_NAME = 21; - SET_SCAN_MODE_FAILED = 22; - INVOKE_API_FAILED = 23; - TIMEOUT = 24; - NULL_NFC_TAG = 25; - FEATURE_NFC_NOT_SUPPORTED = 26; - FEATURE_NFC_HOST_CARD_EMULATION_NOT_SUPPORTED = 27; - MULTICAST_NOT_SUPPORTED = 30; - NSD_NOT_ENABLED = 31; - INVALID_PORT_NUMBER = 32; - NULL_SERVICE_NAME = 33; - NULL_SERVICE_TYPE = 34; - WITHOUT_CONNECTED_WIFI_NETWORK = 35; - FEATURE_WIFI_AWARE_NOT_SUPPORTED = 36; - NULL_CONNECTIVITY_MANAGER = 37; - NULL_WIFI_AWARE_MANAGER = 38; - STALE_ANDROID_VERSION = 39; - NULL_SERVICE_INFO = 40; - NULL_WORK_SOURCE = 41; - NULL_CALLBACK = 42; - NULL_BLUETOOTH_LE_SCANNER_COMPAT = 43; - EMPTY_WORK_SOURCE_CACHE = 44; - SCAN_FAILED_ALREADY_STARTED = 45; - SCAN_FAILED_APPLICATION_REGISTRATION_FAILED = 46; - SCAN_FAILED_INTERNAL_ERROR = 47; - SCAN_FAILED_FEATURE_UNSUPPORTED = 48; - SCAN_FAILED_BLUETOOTH_DISABLED = 49; - SCAN_FILTERS_NOT_ALLOWED_FOR_LOCATION = 50; - BLUETOOTH_SCAN_REJUVENATE_FAILED = 51; - NULL_BLE_PERIPHERAL = 52; - NULL_BLUETOOTH_GATT = 53; - UNEXPECTED_BLUETOOTH_STATE = 54; - REMOTE_EXCEPTION = 55; - INVALID_BLUETOOTH_SOCKET_STATE_BEFORE_CONNECT = 56; - BLUETOOTH_SOCKET_CLOSED_AFTER_CONNECTED = 57; - INVALID_BLUETOOTH_CHANNEL = 58; - NULL_BLUETOOTH_DEVICE = 59; - NULL_BLUETOOTH_PROXY = 60; - INVALID_PACKET_LENGTH = 61; - INVALID_PACKET_BYTES = 62; - UNEXPECTED_EOF_EXCEPTION = 63; - SOCKET_CLOSED_OR_TIMEOUT = 64; - INVALID_IPV4_ADDRESS = 65; - INVALID_IPV6_ADDRESS = 66; - NULL_ADDRESS = 67; - INVALID_VERSION = 68; - SET_CONNECTION_PRIORITY_FAILED = 69; - SET_CONNECTION_PRIORITY_INTERRUPTED = 70; - UNKNOWN_IO_EXCEPTION = 71; - READ_CHARACTERISTIC_FAILED = 72; - WIFI_HOTSPOT_ENABLED = 73; - AWARE_UNAVAILABLE = 74; - IN_BLACK_LIST = 75; - FEATURE_WIFI_NOT_SUPPORTED = 76; - NULL_WIFI_MANAGER = 77; - SOCKET_CLOSED = 78; - SOCKET_ALREADY_CONNECTED = 79; - NFC_TECH_NOT_SUPPORTED = 80; - NFC_SERVICE_DIED = 81; - BIND_NFC_SERVICE_FAILED = 82; - NFC_CREATE_SOCKET_FAILED = 83; - NULL_WIFI_AWARE_PEER = 84; - NETWORK_ALREADY_JOINED = 85; - JOIN_AWARE_NETWORK_CANCELLED = 86; - NETWORK_UNAVAILABLE = 87; - WITHOUT_ACTIVE_AWARE_NETWORK = 88; - WITHOUT_JOINED_AWARE_NETWORK = 89; - CONNET_TO_SOCKET_CANCELLED = 90; - NULL_SSID = 91; - NULL_PASSWORD = 92; - FEATURE_WIFI_DIRECT_NOT_SUPPORTED = 93; - NULL_WIFI_P2P_MANAGER = 94; - P2P_GROUP_FORMED = 95; - ACQUIRE_P2P_CHANNEL_FAILED = 96; - P2P_UNSUPPORTED = 97; - INTERNAL_ERROR = 98; - BUSY = 99; - REFLECTION_ERROR = 100; - NETWORK_ERROR_EHOSTUNREACH = 101; - NETWORK_ERROR_ENETUNREACH = 102; - ADD_NETWORK_FAILED = 103; - UPDATE_NETWORK_FAILED = 104; - ALREADY_IN_PROGRESS = 105; - INVALID_ARGS = 106; - NOT_AUTHORIZED = 107; - INVALID_NETWORK_ID = 108; - WIFI_MANAGER_ENABLE_NETWORK_FAILED = 109; - WIFI_MANAGER_RECONNECT_FAILED = 110; - WITHOUT_ACTIVE_NETWORK = 111; - WEBRTC_CONNECTION_FLOW_EXIST = 112; - NULL_DROID_GUARD_RESULT = 113; - TACHYON_SIGNALING_MESSENGER_EXIST = 114; - TACHYON_ALREADY_START_RECEIVE_MESSAGE = 115; - TACHYON_RECEIVE_MESSAGE_FAILED = 116; - TACHYON_RECEIVE_MESSAGE_INTERRUPTED = 117; - TACHYON_RECEIVE_MESSAGE_EXECUTION_EXCEPTION = 118; - TACHYON_RECEIVE_MESSAGE_TIMEOUT = 119; - TACHYON_RECEIVE_MESSAGE_AUTH_EXCEPTION = 120; - TACHYON_RECEIVE_MESSAGE_STATUS_EXCEPTION = 121; - TACHYON_SEND_MESSAGE_AUTH_EXCEPTION = 122; - TACHYON_SEND_MESSAGE_STATUS_EXCEPTION = 123; - TACHYON_GET_ICE_SERVER_AUTH_EXCEPTION = 124; - TACHYON_GET_ICE_SERVER_STATUS_EXCEPTION = 125; - EMPTY_TACHYON_ICE_SERVER = 126; - POTENTIAL_WEBRTC_LIB_LOADING_FAILURE = 127; - UNEXPECTED_GATT_DESCRIPTOR = 128; - FAIL_TO_RECEIVE_L2CAP_PACKET = 129; - WITHOUT_PSM_VALUE = 130; - SOCKET_BIND_LISTEN_FAILED = 131; - UNEXPECTED_PACKET_CONTENT = 132; - UNREGISTER_NSD_MANAGER_FAILED = 133; - PUBLISH_EMPTY_ADVERTISEMENT_FAILED = 134; - BLUETOOTH_SOCKET_NOT_IN_LISTENING_STATE = 135; - INVALID_BLUETOOTH_SOCKET_SIGNAL_SIZE = 136; - INVALID_BLUETOOTH_SOCKET_SIGNAL_STATUS = 137; - GET_ADDRESS_FAILED = 138; - NULL_LOCAL_ADDRESS = 139; - IS_LOOPBACK_ADDRESS = 140; - SOCKET_NOT_BOUND = 141; - INVALID_REMOTE_ADDRESS = 142; - SOCKET_ALREADY_BOUND = 143; - HOTSPOT_NOT_STARTED = 144; - WEBRTC_ALREADY_INITIALIZED = 145; - INVALID_WEBRTC_STATE = 146; - NULL_DATA_CHANNEL = 147; - CREATE_OFFER_FAILED = 148; - CLOSE_SERVER_SOCKET_FAILED = 149; -}