nearbyconnections : Define interfaces of /medium, /public(wrapper), /g3 WifiLanV2.

PiperOrigin-RevId: 405537829
This commit is contained in:
edwinwu
2021-10-25 18:35:42 -07:00
committed by Copybara-Service
parent b733dde4e7
commit d0da545834
18 changed files with 1154 additions and 40 deletions
+2
View File
@@ -44,6 +44,7 @@ cc_library(
"webrtc_endpoint_channel.cc",
"wifi_lan_bwu_handler.cc",
"wifi_lan_endpoint_channel.cc",
"wifi_lan_endpoint_channel_v2.cc",
"wifi_lan_service_info.cc",
],
hdrs = [
@@ -81,6 +82,7 @@ cc_library(
"webrtc_endpoint_channel.h",
"wifi_lan_bwu_handler.h",
"wifi_lan_endpoint_channel.h",
"wifi_lan_endpoint_channel_v2.h",
"wifi_lan_service_info.h",
],
compatible_with = ["//buildenv/target:non_prod"],
+2
View File
@@ -24,6 +24,7 @@ cc_library(
"uuid.cc",
"webrtc.cc",
"wifi_lan.cc",
"wifi_lan_v2.cc",
],
hdrs = [
"ble.h",
@@ -35,6 +36,7 @@ cc_library(
"uuid.h",
"webrtc.h",
"wifi_lan.h",
"wifi_lan_v2.h",
],
compatible_with = ["//buildenv/target:non_prod"],
visibility = [
+2
View File
@@ -26,6 +26,8 @@ Ble& Mediums::GetBle() { return ble_; }
WifiLan& Mediums::GetWifiLan() { return wifi_lan_; }
WifiLanV2& Mediums::GetWifiLanV2() { return wifi_lan_v2_; }
mediums::WebRtc& Mediums::GetWebRtc() { return webrtc_; }
} // namespace connections
+5
View File
@@ -20,6 +20,7 @@
#include "core/internal/mediums/bluetooth_radio.h"
#include "core/internal/mediums/webrtc.h"
#include "core/internal/mediums/wifi_lan.h"
#include "core/internal/mediums/wifi_lan_v2.h"
namespace location {
namespace nearby {
@@ -43,6 +44,9 @@ class Mediums {
// Returns a handle to the Wifi-Lan medium.
WifiLan& GetWifiLan();
// Returns a handle to the Wifi-Lan medium.
WifiLanV2& GetWifiLanV2();
// Returns a handle to the WebRtc medium.
mediums::WebRtc& GetWebRtc();
@@ -59,6 +63,7 @@ class Mediums {
BluetoothClassic bluetooth_classic_{bluetooth_radio_};
Ble ble_{bluetooth_radio_};
WifiLan wifi_lan_;
WifiLanV2 wifi_lan_v2_;
mediums::WebRtc webrtc_;
};
+142
View File
@@ -0,0 +1,142 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "core/internal/mediums/wifi_lan_v2.h"
#include <memory>
#include <string>
#include <utility>
#include "absl/strings/str_format.h"
#include "core/internal/mediums/utils.h"
#include "platform/public/logging.h"
#include "platform/public/mutex_lock.h"
namespace location {
namespace nearby {
namespace connections {
WifiLanV2::~WifiLanV2() {
// All the AcceptLoopRunnable objects in here should already have gotten an
// opportunity to shut themselves down cleanly in the calls to
// StopAcceptingConnections() above.
accept_loops_runner_.Shutdown();
}
bool WifiLanV2::IsAvailable() const {
MutexLock lock(&mutex_);
return IsAvailableLocked();
}
bool WifiLanV2::IsAvailableLocked() const { return medium_.IsValid(); }
bool WifiLanV2::StartAdvertising(const std::string& service_id,
NsdServiceInfo& nsd_service_info) {
return false;
}
bool WifiLanV2::StopAdvertising(const std::string& service_id) { return false; }
bool WifiLanV2::IsAdvertising(const std::string& service_id) {
MutexLock lock(&mutex_);
return IsAdvertisingLocked(service_id);
}
bool WifiLanV2::IsAdvertisingLocked(const std::string& service_id) {
return advertising_info_.Existed(service_id);
}
bool WifiLanV2::StartDiscovery(const std::string& service_id,
DiscoveredServiceCallback callback) {
MutexLock lock(&mutex_);
return false;
}
bool WifiLanV2::StopDiscovery(const std::string& service_id) {
MutexLock lock(&mutex_);
return false;
}
bool WifiLanV2::IsDiscovering(const std::string& service_id) {
MutexLock lock(&mutex_);
return IsDiscoveringLocked(service_id);
}
bool WifiLanV2::IsDiscoveringLocked(const std::string& service_id) {
return discovering_info_.Existed(service_id);
}
bool WifiLanV2::StartAcceptingConnections(const std::string& service_id,
AcceptedConnectionCallback callback) {
MutexLock lock(&mutex_);
return false;
}
bool WifiLanV2::StopAcceptingConnections(const std::string& service_id) {
MutexLock lock(&mutex_);
return false;
}
bool WifiLanV2::IsAcceptingConnections(const std::string& service_id) {
MutexLock lock(&mutex_);
return IsAcceptingConnectionsLocked(service_id);
}
bool WifiLanV2::IsAcceptingConnectionsLocked(const std::string& service_id) {
return server_sockets_.find(service_id) != server_sockets_.end();
}
WifiLanSocketV2 WifiLanV2::Connect(const std::string& service_id,
NsdServiceInfo& service_info,
CancellationFlag* cancellation_flag) {
MutexLock lock(&mutex_);
return {};
}
WifiLanSocketV2 WifiLanV2::Connect(const std::string& service_id,
const std::string& ip_address, int port,
CancellationFlag* cancellation_flag) {
MutexLock lock(&mutex_);
return {};
}
std::pair<std::string, int> WifiLanV2::GetCredentials(
const std::string& service_id) {
MutexLock lock(&mutex_);
const auto& it = server_sockets_.find(service_id);
if (it == server_sockets_.end()) {
return std::pair<std::string, int>();
}
return std::pair<std::string, int>(it->second.GetIpAddress(),
it->second.GetPort());
}
std::string WifiLanV2::GenerateServiceType(const std::string& service_id) {
std::string service_id_hash_string;
const ByteArray service_id_hash = Utils::Sha256Hash(
service_id, NsdServiceInfo::kTypeFromServiceIdHashLength);
for (auto byte : std::string(service_id_hash)) {
absl::StrAppend(&service_id_hash_string, absl::StrFormat("%02X", byte));
}
return absl::StrFormat(NsdServiceInfo::kNsdTypeFormat,
service_id_hash_string);
}
} // namespace connections
} // namespace nearby
} // namespace location
+190
View File
@@ -0,0 +1,190 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef CORE_INTERNAL_MEDIUMS_WIFI_LAN_V2_H_
#define CORE_INTERNAL_MEDIUMS_WIFI_LAN_V2_H_
#include <cstdint>
#include <string>
#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
#include "platform/base/byte_array.h"
#include "platform/base/cancellation_flag.h"
#include "platform/base/nsd_service_info.h"
#include "platform/public/multi_thread_executor.h"
#include "platform/public/mutex.h"
#include "platform/public/wifi_lan_v2.h"
namespace location {
namespace nearby {
namespace connections {
class WifiLanV2 {
public:
using DiscoveredServiceCallback = WifiLanMediumV2::DiscoveredServiceCallback;
// Callback that is invoked when a new connection is accepted.
struct AcceptedConnectionCallback {
std::function<void(WifiLanSocketV2 socket)> accepted_cb =
DefaultCallback<WifiLanSocketV2>();
};
WifiLanV2() = default;
~WifiLanV2();
// 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 in NsdServiceInfo and
// then enables WifiLan advertising.
// Returns true, if NsdServiceInfo is successfully set, and false otherwise.
bool StartAdvertising(const std::string& service_id,
NsdServiceInfo& nsd_service_info)
ABSL_LOCKS_EXCLUDED(mutex_);
// Disables WifiLan advertising.
// Returns false if no successful call StartAdvertising() was previously
// made, otherwise returns true.
bool StopAdvertising(const std::string& service_id)
ABSL_LOCKS_EXCLUDED(mutex_);
bool IsAdvertising(const std::string& service_id) ABSL_LOCKS_EXCLUDED(mutex_);
// Enables WifiLan discovery. Will report any discoverable services
// through a callback.
// Returns true, if discovery was enabled, false otherwise.
bool StartDiscovery(const std::string& service_id,
DiscoveredServiceCallback callback)
ABSL_LOCKS_EXCLUDED(mutex_);
// Disables WifiLan discovery.
bool StopDiscovery(const std::string& service_id) ABSL_LOCKS_EXCLUDED(mutex_);
bool IsDiscovering(const std::string& service_id) ABSL_LOCKS_EXCLUDED(mutex_);
// Starts a worker thread, creates a WifiLan socket, associates it with a
// service id.
bool StartAcceptingConnections(const std::string& service_id,
AcceptedConnectionCallback callback)
ABSL_LOCKS_EXCLUDED(mutex_);
// Closes socket corresponding to a service id.
bool StopAcceptingConnections(const std::string& service_id)
ABSL_LOCKS_EXCLUDED(mutex_);
bool IsAcceptingConnections(const std::string& service_id)
ABSL_LOCKS_EXCLUDED(mutex_);
// Establishes connection to WifiLan service that was might be started on
// another service with StartAcceptingConnections() using the same service_id.
// Blocks until connection is established, or server-side is terminated.
// Returns socket instance. On success, WifiLanSocket.IsValid() return true.
WifiLanSocketV2 Connect(const std::string& service_id,
NsdServiceInfo& service_info,
CancellationFlag* cancellation_flag)
ABSL_LOCKS_EXCLUDED(mutex_);
// Establishes connection to WifiLan service by ip address and port for
// bandwidth upgradation.
// Returns socket instance. On success, WifiLanSocket.IsValid() return true.
WifiLanSocketV2 Connect(const std::string& service_id,
const std::string& ip_address, int port,
CancellationFlag* cancellation_flag)
ABSL_LOCKS_EXCLUDED(mutex_);
std::pair<std::string, int> GetCredentials(const std::string& service_id)
ABSL_LOCKS_EXCLUDED(mutex_);
private:
struct AdvertisingInfo {
bool Empty() const { return nsd_service_infos.empty(); }
void Clear() { nsd_service_infos.clear(); }
void Add(const std::string& service_id,
const NsdServiceInfo& nsd_service_info) {
nsd_service_infos.insert({service_id, nsd_service_info});
}
void Remove(const std::string& service_id) {
nsd_service_infos.erase(service_id);
}
bool Existed(const std::string& service_id) const {
return nsd_service_infos.contains(service_id);
}
NsdServiceInfo* GetServiceInfo(const std::string& service_id) {
const auto& it = nsd_service_infos.find(service_id);
if (it == nsd_service_infos.end()) {
return nullptr;
}
return &it->second;
}
absl::flat_hash_map<std::string, NsdServiceInfo> nsd_service_infos;
};
struct DiscoveringInfo {
bool Empty() const { return service_ids.empty(); }
void Clear() { service_ids.clear(); }
void Add(const std::string& service_id) { service_ids.emplace(service_id); }
void Remove(const std::string& service_id) {
service_ids.erase(service_id);
}
bool Existed(const std::string& service_id) const {
return service_ids.contains(service_id);
}
absl::flat_hash_set<std::string> service_ids;
};
static constexpr int kMaxConcurrentAcceptLoops = 5;
// Same as IsAvailable(), but must be called with mutex_ held.
bool IsAvailableLocked() const ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
// Same as IsAdvertising(), but must be called with mutex_ held.
bool IsAdvertisingLocked(const std::string& service_id)
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
// Same as IsDiscovering(), but must be called with mutex_ held.
bool IsDiscoveringLocked(const std::string& service_id)
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
// Same as IsAcceptingConnections(), but must be called with mutex_ held.
bool IsAcceptingConnectionsLocked(const std::string& service_id)
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
// Generates mDNS type.
std::string GenerateServiceType(const std::string& service_id);
mutable Mutex mutex_;
WifiLanMediumV2 medium_ ABSL_GUARDED_BY(mutex_);
AdvertisingInfo advertising_info_ ABSL_GUARDED_BY(mutex_);
DiscoveringInfo discovering_info_ ABSL_GUARDED_BY(mutex_);
// A thread pool dedicated to running all the accept loops from
// StartAcceptingConnections().
MultiThreadExecutor accept_loops_runner_{kMaxConcurrentAcceptLoops};
// A map of service_id -> ServerSocket. If map is non-empty, we
// are currently listening for incoming connections.
// WifiLanServerSocket instances are used from accept_loops_runner_,
// and thus require pointer stability.
absl::flat_hash_map<std::string, WifiLanServerSocketV2> server_sockets_
ABSL_GUARDED_BY(mutex_);
};
} // namespace connections
} // namespace nearby
} // namespace location
#endif // CORE_INTERNAL_MEDIUMS_WIFI_LAN_V2_H_
@@ -0,0 +1,47 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "core/internal/wifi_lan_endpoint_channel_v2.h"
#include <string>
#include "platform/public/logging.h"
#include "platform/public/wifi_lan_v2.h"
namespace location {
namespace nearby {
namespace connections {
WifiLanEndpointChannelV2::WifiLanEndpointChannelV2(
const std::string& channel_name, WifiLanSocketV2 socket)
: BaseEndpointChannel(channel_name, &socket.GetInputStream(),
&socket.GetOutputStream()),
socket_(std::move(socket)) {}
proto::connections::Medium WifiLanEndpointChannelV2::GetMedium() const {
return proto::connections::Medium::WIFI_LAN;
}
void WifiLanEndpointChannelV2::CloseImpl() {
auto status = socket_.Close();
if (!status.Ok()) {
NEARBY_LOGS(INFO)
<< "Failed to close underlying socket for WifiLanEndpointChannel "
<< GetName() << " : exception = " << status.value;
}
}
} // namespace connections
} // namespace nearby
} // namespace location
@@ -0,0 +1,43 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef CORE_INTERNAL_WIFI_LAN_ENDPOINT_CHANNEL_V2_H_
#define CORE_INTERNAL_WIFI_LAN_ENDPOINT_CHANNEL_V2_H_
#include "core/internal/base_endpoint_channel.h"
#include "platform/public/wifi_lan_v2.h"
namespace location {
namespace nearby {
namespace connections {
class WifiLanEndpointChannelV2 final : public BaseEndpointChannel {
public:
// Creates both outgoing and incoming WifiLan channels.
WifiLanEndpointChannelV2(const std::string& channel_name,
WifiLanSocketV2 socket);
proto::connections::Medium GetMedium() const override;
private:
void CloseImpl() override;
WifiLanSocketV2 socket_;
};
} // namespace connections
} // namespace nearby
} // namespace location
#endif // CORE_INTERNAL_WIFI_LAN_ENDPOINT_CHANNEL_V2_H_