mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-15 07:06:11 -04:00
nearby: snapshot of cl/313536507
Signed-off-by: Alexey Polyudov <apolyudov@google.com> Change-Id: I8936b527079074d5c3af5531b9245063767cc4a7
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
cc_library(
|
||||
name = "sample",
|
||||
name = "sample_platform",
|
||||
srcs = [
|
||||
"sample_wifi_medium.cc",
|
||||
"sample_wifi_medium.h",
|
||||
"atomic_reference_impl.h",
|
||||
"sample_platform.cc",
|
||||
"settable_future_impl.h",
|
||||
],
|
||||
hdrs = ["sample_platform.h"],
|
||||
visibility = [
|
||||
"//googlemac/iPhone/Shared/Nearby/Connections:__subpackages__",
|
||||
"//core:__subpackages__",
|
||||
@@ -14,7 +14,9 @@ cc_library(
|
||||
"//platform:types",
|
||||
"//platform:utils",
|
||||
"//platform/api",
|
||||
"//platform/impl/shared/sample:sample_wifi_medium",
|
||||
"//platform/port:string",
|
||||
"//absl/time",
|
||||
"//absl/types:any",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef PLATFORM_IMPL_SAMPLE_ATOMIC_REFERENCE_IMPL_H_
|
||||
#define PLATFORM_IMPL_SAMPLE_ATOMIC_REFERENCE_IMPL_H_
|
||||
|
||||
#include "platform/api/atomic_reference_def.h"
|
||||
#include "absl/types/any.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace platform {
|
||||
|
||||
// Provide implementation for absl::any.
|
||||
class AtomicReferenceImpl : public AtomicReference<absl::any> {
|
||||
public:
|
||||
explicit AtomicReferenceImpl(absl::any initial_value) {}
|
||||
~AtomicReferenceImpl() override = default;
|
||||
|
||||
absl::any get() override { return {}; }
|
||||
void set(absl::any value) override {}
|
||||
};
|
||||
|
||||
} // namespace platform
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
#endif // PLATFORM_IMPL_SAMPLE_ATOMIC_REFERENCE_IMPL_H_
|
||||
@@ -0,0 +1,125 @@
|
||||
#include <cstdint>
|
||||
|
||||
#include "platform/api/atomic_boolean.h"
|
||||
#include "platform/api/atomic_reference_def.h"
|
||||
#include "platform/api/ble.h"
|
||||
#include "platform/api/ble_v2.h"
|
||||
#include "platform/api/bluetooth_adapter.h"
|
||||
#include "platform/api/bluetooth_classic.h"
|
||||
#include "platform/api/condition_variable.h"
|
||||
#include "platform/api/count_down_latch.h"
|
||||
#include "platform/api/hash_utils.h"
|
||||
#include "platform/api/lock.h"
|
||||
#include "platform/api/platform.h"
|
||||
#include "platform/api/server_sync.h"
|
||||
#include "platform/api/settable_future_def.h"
|
||||
#include "platform/api/submittable_executor_def.h"
|
||||
#include "platform/api/system_clock.h"
|
||||
#include "platform/api/thread_utils.h"
|
||||
#include "platform/api/wifi.h"
|
||||
#include "platform/cancelable.h"
|
||||
#include "platform/impl/sample/atomic_reference_impl.h"
|
||||
#include "platform/impl/sample/settable_future_impl.h"
|
||||
#include "platform/impl/shared/sample/sample_wifi_medium.h"
|
||||
#include "platform/port/string.h"
|
||||
#include "platform/ptr.h"
|
||||
#include "platform/runnable.h"
|
||||
#include "absl/types/any.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace platform {
|
||||
|
||||
Ptr<ScheduledExecutor> ImplementationPlatform::createScheduledExecutor() {
|
||||
return Ptr<ScheduledExecutor>{};
|
||||
}
|
||||
|
||||
Ptr<SubmittableExecutor> ImplementationPlatform::createSingleThreadExecutor() {
|
||||
return Ptr<SubmittableExecutor>{};
|
||||
}
|
||||
|
||||
Ptr<SubmittableExecutor> ImplementationPlatform::createMultiThreadExecutor(
|
||||
int max_concurrency) {
|
||||
return Ptr<SubmittableExecutor>{};
|
||||
}
|
||||
|
||||
Ptr<AtomicReference<absl::any>>
|
||||
ImplementationPlatform::createAtomicReferenceAny(absl::any initial_value) {
|
||||
return Ptr<AtomicReference<absl::any>>(
|
||||
new AtomicReferenceImpl(initial_value));
|
||||
}
|
||||
|
||||
Ptr<SettableFuture<absl::any>>
|
||||
ImplementationPlatform::createSettableFutureAny() {
|
||||
return Ptr<SettableFuture<absl::any>>(new SettableFutureImpl{});
|
||||
}
|
||||
|
||||
Ptr<BluetoothAdapter> ImplementationPlatform::createBluetoothAdapter() {
|
||||
return Ptr<BluetoothAdapter>{};
|
||||
}
|
||||
|
||||
Ptr<WifiMedium> ImplementationPlatform::createWifiMedium() {
|
||||
return Ptr<WifiMedium>();
|
||||
}
|
||||
|
||||
Ptr<CountDownLatch> ImplementationPlatform::createCountDownLatch(
|
||||
std::int32_t count) {
|
||||
return Ptr<CountDownLatch>{};
|
||||
}
|
||||
|
||||
Ptr<ThreadUtils> ImplementationPlatform::createThreadUtils() {
|
||||
return Ptr<ThreadUtils>{};
|
||||
}
|
||||
|
||||
Ptr<SystemClock> ImplementationPlatform::createSystemClock() {
|
||||
return Ptr<SystemClock>{};
|
||||
}
|
||||
|
||||
Ptr<AtomicBoolean> ImplementationPlatform::createAtomicBoolean(
|
||||
bool initial_value) {
|
||||
return Ptr<AtomicBoolean>{};
|
||||
}
|
||||
|
||||
Ptr<BluetoothClassicMedium>
|
||||
ImplementationPlatform::createBluetoothClassicMedium() {
|
||||
return Ptr<BluetoothClassicMedium>();
|
||||
}
|
||||
|
||||
Ptr<BLEMedium> ImplementationPlatform::createBLEMedium() {
|
||||
return Ptr<BLEMedium>();
|
||||
}
|
||||
|
||||
Ptr<BLEMediumV2> ImplementationPlatform::createBLEMediumV2() {
|
||||
return Ptr<BLEMediumV2>();
|
||||
}
|
||||
|
||||
Ptr<ServerSyncMedium> ImplementationPlatform::createServerSyncMedium() {
|
||||
return Ptr<ServerSyncMedium>{};
|
||||
}
|
||||
|
||||
Ptr<WebRtcSignalingMessenger>
|
||||
ImplementationPlatform::createWebRtcSignalingMessenger(
|
||||
const std::string& self_id) {
|
||||
return Ptr<WebRtcSignalingMessenger>{};
|
||||
}
|
||||
|
||||
Ptr<Lock> ImplementationPlatform::createLock() { return Ptr<Lock>{}; }
|
||||
|
||||
Ptr<ConditionVariable> ImplementationPlatform::createConditionVariable(
|
||||
Ptr<Lock> lock) {
|
||||
return Ptr<ConditionVariable>{};
|
||||
}
|
||||
|
||||
Ptr<HashUtils> ImplementationPlatform::createHashUtils() {
|
||||
return Ptr<HashUtils>{};
|
||||
}
|
||||
|
||||
std::string ImplementationPlatform::getDeviceId() { return "sample"; }
|
||||
|
||||
std::string ImplementationPlatform::getPayloadPath(int64_t payload_id) {
|
||||
return "/tmp/sample-" + std::to_string(payload_id);
|
||||
}
|
||||
|
||||
} // namespace platform
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
@@ -1,141 +0,0 @@
|
||||
#ifndef PLATFORM_IMPL_SAMPLE_SAMPLE_PLATFORM_H_
|
||||
#define PLATFORM_IMPL_SAMPLE_SAMPLE_PLATFORM_H_
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "platform/api/atomic_boolean.h"
|
||||
#include "platform/api/atomic_reference.h"
|
||||
#include "platform/api/ble.h"
|
||||
#include "platform/api/ble_v2.h"
|
||||
#include "platform/api/bluetooth_adapter.h"
|
||||
#include "platform/api/bluetooth_classic.h"
|
||||
#include "platform/api/condition_variable.h"
|
||||
#include "platform/api/count_down_latch.h"
|
||||
#include "platform/api/hash_utils.h"
|
||||
#include "platform/api/lock.h"
|
||||
#include "platform/api/multi_thread_executor.h"
|
||||
#include "platform/api/settable_future.h"
|
||||
#include "platform/api/single_thread_executor.h"
|
||||
#include "platform/api/system_clock.h"
|
||||
#include "platform/api/thread_utils.h"
|
||||
#include "platform/api/wifi.h"
|
||||
#include "platform/cancelable.h"
|
||||
#include "platform/impl/sample/sample_wifi_medium.h"
|
||||
#include "platform/port/string.h"
|
||||
#include "platform/ptr.h"
|
||||
#include "platform/runnable.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace sample {
|
||||
|
||||
// The SamplePlatform class below shows an example of the factory functions
|
||||
// and typedefs.
|
||||
class SamplePlatform {
|
||||
public:
|
||||
class SampleSubmittableExecutor
|
||||
: public SubmittableExecutor<SampleSubmittableExecutor> {
|
||||
public:
|
||||
template <typename T>
|
||||
Ptr<Future<T> > submit(Ptr<Callable<T> > callable) {
|
||||
return Ptr<Future<T> >();
|
||||
}
|
||||
};
|
||||
|
||||
class SampleSingleThreadExecutor
|
||||
: public SingleThreadExecutor<SampleSubmittableExecutor> {
|
||||
public:
|
||||
void execute(Ptr<Runnable> runnable) override {}
|
||||
void shutdown() override {}
|
||||
};
|
||||
|
||||
class SampleMultiThreadExecutor
|
||||
: public MultiThreadExecutor<SampleSubmittableExecutor> {
|
||||
public:
|
||||
void execute(Ptr<Runnable> runnable) override {}
|
||||
void shutdown() override {}
|
||||
};
|
||||
|
||||
class SampleScheduledExecutor {
|
||||
public:
|
||||
Ptr<Cancelable> schedule(Ptr<Runnable> runnable,
|
||||
std::int64_t delay_millis) {
|
||||
return Ptr<Cancelable>();
|
||||
}
|
||||
void shutdown() {}
|
||||
};
|
||||
|
||||
typedef SampleSingleThreadExecutor SingleThreadExecutorType;
|
||||
static Ptr<SingleThreadExecutorType> createSingleThreadExecutor() {
|
||||
return MakePtr(new SingleThreadExecutorType());
|
||||
}
|
||||
|
||||
typedef SampleMultiThreadExecutor MultiThreadExecutorType;
|
||||
static Ptr<MultiThreadExecutorType> createMultiThreadExecutor(
|
||||
std::int32_t max_concurrency) {
|
||||
return MakePtr(new MultiThreadExecutorType());
|
||||
}
|
||||
|
||||
typedef SampleScheduledExecutor ScheduledExecutorType;
|
||||
static Ptr<ScheduledExecutorType> createScheduledExecutor() {
|
||||
return MakePtr(new ScheduledExecutorType());
|
||||
}
|
||||
|
||||
static Ptr<BluetoothAdapter> createBluetoothAdapter() {
|
||||
return Ptr<BluetoothAdapter>();
|
||||
}
|
||||
|
||||
static Ptr<WifiMedium> createWifiMedium() {
|
||||
return MakePtr(new SampleWifiMedium());
|
||||
}
|
||||
|
||||
static Ptr<CountDownLatch> createCountDownLatch(std::int32_t count) {
|
||||
return Ptr<CountDownLatch>();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static Ptr<SettableFuture<T> > createSettableFuture() {
|
||||
return Ptr<SettableFuture<T> >();
|
||||
}
|
||||
|
||||
static Ptr<ThreadUtils> createThreadUtils() { return Ptr<ThreadUtils>(); }
|
||||
|
||||
static Ptr<SystemClock> createSystemClock() { return Ptr<SystemClock>(); }
|
||||
|
||||
static Ptr<AtomicBoolean> createAtomicBoolean(bool initial_value) {
|
||||
return Ptr<AtomicBoolean>();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static Ptr<AtomicReference<T> > createAtomicReference(T initial_value) {
|
||||
return Ptr<AtomicReference<T> >();
|
||||
}
|
||||
|
||||
static Ptr<BluetoothClassicMedium> createBluetoothClassicMedium() {
|
||||
return Ptr<BluetoothClassicMedium>();
|
||||
}
|
||||
|
||||
static Ptr<BLEMedium> createBLEMedium() { return Ptr<BLEMedium>(); }
|
||||
|
||||
static Ptr<BLEMediumV2> createBLEMediumV2() { return Ptr<BLEMediumV2>(); }
|
||||
|
||||
static Ptr<Lock> createLock() { return Ptr<Lock>(); }
|
||||
|
||||
static Ptr<ConditionVariable> createConditionVariable(Ptr<Lock> lock) {
|
||||
return Ptr<ConditionVariable>();
|
||||
}
|
||||
|
||||
static Ptr<HashUtils> createHashUtils() { return Ptr<HashUtils>(); }
|
||||
|
||||
static std::string getDeviceId() { return ""; }
|
||||
|
||||
static std::string getPayloadPath(int64_t payload_id) {
|
||||
return "/tmp/" + std::to_string(payload_id);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace sample
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
#endif // PLATFORM_IMPL_SAMPLE_SAMPLE_PLATFORM_H_
|
||||
@@ -1,110 +0,0 @@
|
||||
#include "platform/impl/sample/sample_wifi_medium.h"
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "platform/prng.h"
|
||||
#include "absl/time/clock.h"
|
||||
#include "absl/time/time.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace sample {
|
||||
|
||||
namespace {
|
||||
|
||||
const char* kOpenSSID = "__OPEN__";
|
||||
const char* kWpaPskSSID = "__WPA_PSK__";
|
||||
const char* kWepSSID = "__WEP__";
|
||||
const char* kNoInternetConnectivitySSID = "__NO_INTERNET_CONNECTIVITY__";
|
||||
const char* kConnectionFailureSSID = "__CONNECTION_FAILURE__";
|
||||
const char* kAuthFailureSSID = "__AUTH_FAILURE__";
|
||||
|
||||
std::uint32_t boundedUInt32(std::uint32_t upper_limit) {
|
||||
return Prng().nextUInt32() % (upper_limit + 1);
|
||||
}
|
||||
|
||||
void randomSleep(std::uint32_t upper_limit_millis) {
|
||||
absl::SleepFor(absl::Milliseconds(boundedUInt32(upper_limit_millis)));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::vector<SampleWifiScanResult> SampleWifiMedium::canned_scan_results_;
|
||||
|
||||
SampleWifiMedium::SampleWifiMedium() : current_ssid_() {
|
||||
// One-time initialization of our static canned_scan_results_.
|
||||
if (canned_scan_results_.empty()) {
|
||||
canned_scan_results_.push_back(
|
||||
SampleWifiScanResult(kOpenSSID, 1, 2401, WifiAuthType::OPEN));
|
||||
canned_scan_results_.push_back(
|
||||
SampleWifiScanResult(kWpaPskSSID, 2, 5002, WifiAuthType::WPA_PSK));
|
||||
canned_scan_results_.push_back(
|
||||
SampleWifiScanResult(kWepSSID, 3, 2403, WifiAuthType::WEP));
|
||||
canned_scan_results_.push_back(SampleWifiScanResult(
|
||||
kNoInternetConnectivitySSID, 4, 5004, WifiAuthType::OPEN));
|
||||
canned_scan_results_.push_back(SampleWifiScanResult(
|
||||
kConnectionFailureSSID, 5, 2405, WifiAuthType::OPEN));
|
||||
canned_scan_results_.push_back(
|
||||
SampleWifiScanResult(kAuthFailureSSID, 6, 5006, WifiAuthType::OPEN));
|
||||
}
|
||||
}
|
||||
|
||||
SampleWifiMedium::~SampleWifiMedium() {}
|
||||
|
||||
bool SampleWifiMedium::scan(
|
||||
Ptr<WifiMedium::ScanResultCallback> scan_result_callback) {
|
||||
// Sleep for up to 10 seconds, to simulate performing an actual Wifi scan.
|
||||
randomSleep(10 * 1000);
|
||||
|
||||
// Construct the response.
|
||||
std::vector<ConstPtr<WifiScanResult> > scan_results;
|
||||
for (std::vector<SampleWifiScanResult>::const_iterator it =
|
||||
canned_scan_results_.begin();
|
||||
it != canned_scan_results_.end(); it++) {
|
||||
scan_results.push_back(ConstPtr<WifiScanResult>(
|
||||
new SampleWifiScanResult(it->getSSID(), it->getSignalStrengthDbm(),
|
||||
it->getFrequencyMhz(), it->getAuthType())));
|
||||
}
|
||||
|
||||
// And report it back.
|
||||
scan_result_callback->onScanResults(scan_results);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
WifiConnectionStatus::Value SampleWifiMedium::connectToNetwork(
|
||||
const std::string& ssid, const std::string& password,
|
||||
WifiAuthType::Value auth_type) {
|
||||
// Sleep for up to 10 seconds, to simulate actually connecting to the SSID.
|
||||
randomSleep(10 * 1000);
|
||||
|
||||
if (kConnectionFailureSSID == ssid) {
|
||||
return WifiConnectionStatus::CONNECTION_FAILURE;
|
||||
}
|
||||
|
||||
if (kAuthFailureSSID == ssid) {
|
||||
return WifiConnectionStatus::AUTH_FAILURE;
|
||||
}
|
||||
|
||||
return WifiConnectionStatus::CONNECTED;
|
||||
}
|
||||
|
||||
bool SampleWifiMedium::verifyInternetConnectivity() {
|
||||
if (current_ssid_.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Sleep for up to 5 seconds, to simulate actually verifying internet
|
||||
// connectivity.
|
||||
randomSleep(5 * 1000);
|
||||
|
||||
return current_ssid_ != kNoInternetConnectivitySSID;
|
||||
}
|
||||
|
||||
std::string SampleWifiMedium::getIPAddress() {
|
||||
return current_ssid_.empty() ? "" : "1.2.3.4";
|
||||
}
|
||||
|
||||
} // namespace sample
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
@@ -1,59 +0,0 @@
|
||||
#ifndef PLATFORM_IMPL_SAMPLE_SAMPLE_WIFI_MEDIUM_H_
|
||||
#define PLATFORM_IMPL_SAMPLE_SAMPLE_WIFI_MEDIUM_H_
|
||||
|
||||
#include "platform/api/wifi.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace sample {
|
||||
|
||||
class SampleWifiScanResult : public WifiScanResult {
|
||||
public:
|
||||
SampleWifiScanResult(const std::string& ssid,
|
||||
std::int32_t signal_strength_dbm,
|
||||
std::int32_t frequency_mhz,
|
||||
WifiAuthType::Value auth_type)
|
||||
: ssid_(ssid),
|
||||
signal_strength_dbm_(signal_strength_dbm),
|
||||
frequency_mhz_(frequency_mhz),
|
||||
auth_type_(auth_type) {}
|
||||
~SampleWifiScanResult() override {}
|
||||
|
||||
std::string getSSID() const override { return ssid_; }
|
||||
std::int32_t getSignalStrengthDbm() const override {
|
||||
return signal_strength_dbm_;
|
||||
}
|
||||
std::int32_t getFrequencyMhz() const override { return frequency_mhz_; }
|
||||
WifiAuthType::Value getAuthType() const override { return auth_type_; }
|
||||
|
||||
private:
|
||||
const std::string ssid_;
|
||||
const std::int32_t signal_strength_dbm_;
|
||||
const std::int32_t frequency_mhz_;
|
||||
const WifiAuthType::Value auth_type_;
|
||||
};
|
||||
|
||||
class SampleWifiMedium : public WifiMedium {
|
||||
public:
|
||||
SampleWifiMedium();
|
||||
~SampleWifiMedium() override;
|
||||
|
||||
bool scan(Ptr<ScanResultCallback> scan_result_callback) override;
|
||||
WifiConnectionStatus::Value connectToNetwork(
|
||||
const std::string& ssid, const std::string& password,
|
||||
WifiAuthType::Value auth_type) override;
|
||||
bool verifyInternetConnectivity() override;
|
||||
std::string getIPAddress() override;
|
||||
|
||||
private:
|
||||
static std::vector<SampleWifiScanResult> canned_scan_results_;
|
||||
|
||||
// The SSID this Wifi stack is currently connected to; empty string if none.
|
||||
std::string current_ssid_;
|
||||
};
|
||||
|
||||
} // namespace sample
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
#endif // PLATFORM_IMPL_SAMPLE_SAMPLE_WIFI_MEDIUM_H_
|
||||
@@ -0,0 +1,35 @@
|
||||
#ifndef PLATFORM_IMPL_SAMPLE_SETTABLE_FUTURE_IMPL_H_
|
||||
#define PLATFORM_IMPL_SAMPLE_SETTABLE_FUTURE_IMPL_H_
|
||||
|
||||
#include "platform/api/settable_future_def.h"
|
||||
#include "absl/types/any.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace platform {
|
||||
|
||||
class SettableFutureImpl : public SettableFuture<absl::any> {
|
||||
public:
|
||||
explicit SettableFutureImpl() = default;
|
||||
~SettableFutureImpl() override = default;
|
||||
|
||||
bool set(absl::any value) override { return true; }
|
||||
|
||||
bool setException(Exception exception) override { return true; }
|
||||
|
||||
void addListener(Ptr<Runnable> runnable, Executor* executor) override {}
|
||||
|
||||
ExceptionOr<std::any> get() override {
|
||||
return ExceptionOr<std::any>{Exception{Exception::kFailed}};
|
||||
}
|
||||
|
||||
ExceptionOr<std::any> get(std::int64_t timeout_ms) override {
|
||||
return ExceptionOr<std::any>{Exception{Exception::kFailed}};
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace platform
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
#endif // PLATFORM_IMPL_SAMPLE_SETTABLE_FUTURE_IMPL_H_
|
||||
Reference in New Issue
Block a user