diff --git a/cpp/core/internal/base_pcp_handler.cc b/cpp/core/internal/base_pcp_handler.cc index 356c0dfe..6568a91c 100644 --- a/cpp/core/internal/base_pcp_handler.cc +++ b/cpp/core/internal/base_pcp_handler.cc @@ -65,6 +65,8 @@ Status BasePcpHandler::StartAdvertising(ClientProxy* client, const ConnectionOptions& options, const ConnectionRequestInfo& info) { Future response; + NEARBY_LOG(INFO, "StartAdvertising with supported mediums: %s", + GetStringValueOfSupportedMediums(options).c_str()); ConnectionOptions advertising_options = options.CompatibleOptions(); RunOnPcpHandlerThread([this, client, &service_id, &info, &advertising_options, &response]() { @@ -77,6 +79,12 @@ Status BasePcpHandler::StartAdvertising(ClientProxy* client, } // Now that we've succeeded, mark the client as advertising. + // Save the advertising options for local reference in later process like + // upgrading bandwidth. + // TODO(hais): saving advertising_options_ in clientProxy instead of here + // as java implementation does. + std::vector supported_mediums = + advertising_options.GetMediums(); advertising_options_ = advertising_options; advertising_listener_ = info.listener; client->StartedAdvertising(service_id, GetStrategy(), info.listener, @@ -93,18 +101,33 @@ void BasePcpHandler::StopAdvertising(ClientProxy* client) { RunOnPcpHandlerThread([this, client, &latch]() { StopAdvertisingImpl(client); client->StoppedAdvertising(); - advertising_options_.Clear(); + // advertising_options_ is purposefully not cleared here. latch.CountDown(); }); WaitForLatch("StopAdvertising", &latch); } +std::string BasePcpHandler::GetStringValueOfSupportedMediums( + const ConnectionOptions& options) const { + std::ostringstream result; + result << "{ "; + if (options.allowed.bluetooth) result << "bluetooth "; + if (options.allowed.ble) result << "ble "; + if (options.allowed.web_rtc) result << "webrtc "; + if (options.allowed.wifi_lan) result << "wifilan "; + result << "}"; + return result.str(); +} + Status BasePcpHandler::StartDiscovery(ClientProxy* client, const std::string& service_id, const ConnectionOptions& options, const DiscoveryListener& listener) { Future response; ConnectionOptions discovery_options = options.CompatibleOptions(); + + NEARBY_LOG(INFO, "StartDiscovery with supported mediums: %s", + GetStringValueOfSupportedMediums(options).c_str()); RunOnPcpHandlerThread( [this, client, service_id, discovery_options, &listener, &response]() { // Ask the implementation to attempt to start discovery. @@ -131,7 +154,7 @@ void BasePcpHandler::StopDiscovery(ClientProxy* client) { RunOnPcpHandlerThread([this, client, &latch]() { StopDiscoveryImpl(client); client->StoppedDiscovery(); - discovery_options_.Clear(); + // discovery_options_ is purposefully not cleared here. latch.CountDown(); }); @@ -342,7 +365,9 @@ Status BasePcpHandler::RequestConnection(ClientProxy* client, ConnectImplResult connect_impl_result; for (auto connect_endpoint : discovered_endpoints) { - if (!MediumSupported(connect_endpoint->medium, options)) continue; + if (!MediumSupportedByClientOptions(connect_endpoint->medium, + discovery_options_)) + continue; connect_impl_result = ConnectImpl(client, connect_endpoint); if (connect_impl_result.status.Ok()) { channel = std::move(connect_impl_result.endpoint_channel); @@ -366,7 +391,7 @@ Status BasePcpHandler::RequestConnection(ClientProxy* client, // endpoint about ourselves. Exception write_exception = WriteConnectionRequestFrame( channel.get(), client->GetLocalEndpointId(), info.endpoint_info, nonce, - GetSupportedConnectionMediumsByPriority(options)); + GetSupportedConnectionMediumsByPriority(discovery_options_)); if (!write_exception.Ok()) { NEARBY_LOG(INFO, "Failed to send connection request: id=%s", endpoint_id.c_str()); @@ -416,21 +441,25 @@ Status BasePcpHandler::RequestConnection(ClientProxy* client, return status; } -bool BasePcpHandler::MediumSupported(const proto::connections::Medium& medium, - const ConnectionOptions& options) const { - for (auto supported_medium : - options.allowed.GetMediums(/* is supported = */true)) { - if (medium == supported_medium) return true; +bool BasePcpHandler::MediumSupportedByClientOptions( + const proto::connections::Medium& medium, + const ConnectionOptions& client_options) const { + for (auto supported_medium : client_options.GetMediums()) { + if (medium == supported_medium) { + return true; + } } return false; } +// Get ordered supported connection medium based on local advertising/discovery +// option. local_option is either advertising_options_ or discovery_options_. std::vector BasePcpHandler::GetSupportedConnectionMediumsByPriority( - const ConnectionOptions& options) { + const ConnectionOptions& local_option) { std::vector supported_mediums_by_priority; for (auto medium_by_priority : GetConnectionMediumsByPriority()) { - if (MediumSupported(medium_by_priority, options)) { + if (MediumSupportedByClientOptions(medium_by_priority, local_option)) { supported_mediums_by_priority.push_back(medium_by_priority); } } @@ -980,21 +1009,13 @@ void BasePcpHandler::ProcessTieBreakLoss( void BasePcpHandler::InitiateBandwidthUpgrade( ClientProxy* client, const std::string& endpoint_id, - const std::vector& supported_mediums) { - // When we successfully connect to a remote endpoint and a bandwidth upgrade - // medium has not yet been decided, we'll pick the highest bandwidth medium - // supported by both us and the remote endpoint. Once we pick a medium, all - // future connections will use it too. eg. If we chose Wifi LAN, we'll attempt - // to upgrade the 2nd, 3rd, etc remote endpoints with Wifi LAN even if they're - // on a different network (or had a better medium). This is a quick and easy - // way to prevent mediums, like Wifi Hotspot, from interfering with active - // connections (although it's suboptimal for bandwidth throughput). When all - // endpoints disconnect, we reset the bandwidth upgrade medium. - Medium bwu_medium = bwu_medium_.Get(); - if (bwu_medium == Medium::UNKNOWN_MEDIUM) { - bwu_medium = ChooseBestUpgradeMedium(supported_mediums); - bwu_medium_.Set(bwu_medium); - } + const std::vector& their_supported_mediums) { + // There was comments for re-using the same highest medium for upgrading. But + // given that end users may change data options all the time, it makes more + // sense to dynamically select the proper medium for upgrading. + // TODO(hais): when we add more mediums like Wifi Hotspot, we need to prevent + // upgrading interfering with active connections. + Medium bwu_medium = ChooseBestUpgradeMedium(their_supported_mediums); if (AutoUpgradeBandwidth() && bwu_medium != Medium::UNKNOWN_MEDIUM) { bwu_manager_->InitiateBwuForEndpoint(client, endpoint_id, bwu_medium); @@ -1013,7 +1034,7 @@ proto::connections::Medium BasePcpHandler::ChooseBestUpgradeMedium( // Otherwise, pick the best medium we support. std::vector my_mediums = - GetConnectionMediumsByPriority(); + GetSupportedConnectionMediumsByPriority(advertising_options_); for (const auto& my_medium : my_mediums) { for (const auto& their_medium : their_mediums) { if (my_medium == their_medium) { diff --git a/cpp/core/internal/base_pcp_handler.h b/cpp/core/internal/base_pcp_handler.h index 8dfea1a1..abfc3f4c 100644 --- a/cpp/core/internal/base_pcp_handler.h +++ b/cpp/core/internal/base_pcp_handler.h @@ -152,7 +152,6 @@ class BasePcpHandler : public PcpHandler, Pcp GetPcp() const override { return pcp_; } Strategy GetStrategy() const override { return strategy_; } - Medium GetBwuMedium() const { return bwu_medium_.Get(); } void DisconnectFromEndpointManager(); protected: @@ -464,13 +463,15 @@ class BasePcpHandler : public PcpHandler, void WaitForLatch(const std::string& method_name, CountDownLatch* latch); Status WaitForResult(const std::string& method_name, std::int64_t client_id, Future* future); - bool MediumSupported(const proto::connections::Medium& medium, - const ConnectionOptions& options) const; + bool MediumSupportedByClientOptions( + const proto::connections::Medium& medium, + const ConnectionOptions& client_options) const; std::vector GetSupportedConnectionMediumsByPriority( - const ConnectionOptions& options); + const ConnectionOptions& local_option); + std::string GetStringValueOfSupportedMediums( + const ConnectionOptions& options) const; - AtomicReference bwu_medium_{Medium::UNKNOWN_MEDIUM}; ScheduledExecutor alarm_executor_; SingleThreadExecutor serial_executor_; diff --git a/proto/sharing_enums.proto b/proto/sharing_enums.proto index 6abc80c7..dda6fe56 100644 --- a/proto/sharing_enums.proto +++ b/proto/sharing_enums.proto @@ -144,6 +144,12 @@ enum EventType { // Receiver taps quick settings tile. TAP_QUICK_SETTINGS_TILE = 39; + + // Receiver Installation of APKs status. + INSTALL_APK = 40; + + // Receiver verification of APKs status. + VERIFY_APK = 41; } // Event category to differentiate whether this comes from sender or receiver, @@ -321,3 +327,27 @@ enum ActivityName { CONTACT_SELECT_ACTIVITY = 5; CONSENTS_ACTIVITY = 6; } + +enum ApkSource { + UNKNOWN_APK_SOURCE = 0; + + FILE_BROSWER = 1; + LAUNCHER = 2; +} + +// The Installation status of APK. +enum InstallAPKStatus { + UNKNOWN_INSTALL_APK_STATUS = 0; + + FAIL_INSTALLATION = 1; + SUCCESS_INSTALLATION = 2; +} + +// The verification status of APK. +enum VerifyAPKStatus { + UNKNOWN_VERIFY_APK_STATUS = 0; + + NOT_INSTALLABLE = 1; + INSTALLABLE = 2; + ALREADY_INSTALLED = 3; +}