Implemented missing implementation files

I missed some implementation when I was looking over them. These are
them.
This commit is contained in:
Timothy Hutchins
2023-08-16 21:37:52 -05:00
parent bfe5db2229
commit 1ff995400f
4 changed files with 282 additions and 0 deletions
@@ -0,0 +1,88 @@
// 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 PLATFORM_IMPL_LINUX_SERVER_SYNC_H_
#define PLATFORM_IMPL_LINUX_SERVER_SYNC_H_
#include "internal/platform/implementation/server_sync.h"
namespace nearby {
namespace linux {
// Abstraction that represents a Nearby endpoint exchanging data through
// ServerSync Medium.
class ServerSyncDevice : public api::ServerSyncDevice {
public:
// TODO(b/184975123): replace with real implementation.
~ServerSyncDevice() override = default;
// TODO(b/184975123): replace with real implementation.
std::string GetName() const override { return "Un-implemented"; }
// TODO(b/184975123): replace with real implementation.
std::string GetGuid() const override { return "Un-implemented"; }
// TODO(b/184975123): replace with real implementation.
std::string GetOwnGuid() const override { return "Un-implemented"; }
};
// Container of operations that can be performed over the Chrome Sync medium.
class ServerSyncMedium : public api::ServerSyncMedium {
public:
// TODO(b/184975123): replace with real implementation.
~ServerSyncMedium() override = default;
// TODO(b/184975123): replace with real implementation.
bool StartAdvertising(absl::string_view service_id,
absl::string_view endpoint_id,
const ByteArray& endpoint_info) override {
return false;
}
// TODO(b/184975123): replace with real implementation.
void StopAdvertising(absl::string_view service_id) override {}
class DiscoveredDeviceCallback
: public api::ServerSyncMedium::DiscoveredDeviceCallback {
public:
// TODO(b/184975123): replace with real implementation.
~DiscoveredDeviceCallback() override = default;
// Called on a new ServerSyncDevice discovery.
// TODO(b/184975123): replace with real implementation.
void OnDeviceDiscovered(api::ServerSyncDevice* device,
absl::string_view service_id,
absl::string_view endpoint_id,
const ByteArray& endpoint_info) override {}
// Called when ServerSyncDevice is no longer reachable.
// TODO(b/184975123): replace with real implementation.
void OnDeviceLost(api::ServerSyncDevice* device,
absl::string_view service_id) override {}
};
// Returns true once the Chrome Sync scan has been initiated.
// TODO(b/184975123): replace with real implementation.
bool StartDiscovery(absl::string_view service_id,
const api::ServerSyncMedium::DiscoveredDeviceCallback&
discovered_device_callback) override {
return false;
}
// Returns true once Chrome Sync scan for service_id is well and truly
// stopped; after this returns, there must be no more invocations of the
// DiscoveredDeviceCallback passed in to startScanning() for service_id.
// TODO(b/184975123): replace with real implementation.
void StopDiscovery(absl::string_view service_id) override {}
};
} // namespace linux
} // namespace nearby
#endif // PLATFORM_IMPL_LINUX_SERVER_SYNC_H_
@@ -0,0 +1,41 @@
// Copyright 2021 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 PLATFORM_IMPL_LINUX_SYSTEM_CLOCK_H_
#define PLATFORM_IMPL_LINUX_SYSTEM_CLOCK_H_
#include "internal/platform/implementation/system_clock.h"
namespace nearby {
// Initialize global system state.
void SystemClock::Init() { }
// Returns current absolute time. It is guaranteed to be monotonic.
absl::Time SystemClock::ElapsedRealtime() {
return absl::FromUnixNanos(
std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::steady_clock::now().time_since_epoch())
.count());
}
// Pauses current thread for the specified duration.
Exception SystemClock::Sleep(absl::Duration duration) {
absl::SleepFor(duration);
return {Exception::kSuccess};
}
} // namespace nearby
#endif // PLATFORM_IMPL_LINUX_SYSTEM_CLOCK_H_
@@ -0,0 +1,68 @@
// Copyright 2021 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 "internal/platform/implementation/timer.h"
#include <chrono> // NOLINT
// NOLINT
#include <memory>
#include <thread> // NOLINT
#include "gtest/gtest.h"
#include "internal/platform/implementation/platform.h"
namespace nearby {
namespace linux {
namespace {
TEST(Timer, TestCreateTimer) {
int count = 0;
std::unique_ptr<nearby::api::Timer> timer =
nearby::api::ImplementationPlatform::CreateTimer();
ASSERT_TRUE(timer != nullptr);
EXPECT_FALSE(timer->Create(-100, 0, [&]() { ++count; }));
EXPECT_TRUE(timer->Stop());
}
// This test case cannot run on Google3
TEST(Timer, DISABLED_TestRepeatTimer) {
int count = 0;
std::unique_ptr<nearby::api::Timer> timer =
nearby::api::ImplementationPlatform::CreateTimer();
ASSERT_TRUE(timer != nullptr);
EXPECT_TRUE(timer->Create(300, 300, [&]() { ++count; }));
std::this_thread::sleep_for(std::chrono::seconds(1));
EXPECT_TRUE(timer->Stop());
EXPECT_EQ(count, 3);
}
TEST(Timer, DISABLED_TestFireNow) {
int count = 0;
auto timer = nearby::api::ImplementationPlatform::CreateTimer();
EXPECT_TRUE(timer != nullptr);
EXPECT_TRUE(timer->Create(3000, 3000, [&]() { ++count; }));
EXPECT_TRUE(timer->FireNow());
EXPECT_TRUE(timer->Stop());
EXPECT_EQ(count, 1);
}
} // namespace
} // namespace linux
} // namespace nearby
@@ -0,0 +1,85 @@
// 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 "internal/platform/implementation/linux/utils.h"
#include <string>
#include "gtest/gtest.h"
namespace nearby {
namespace linux {
TEST(UtilsTests, MacAddressToString) {
// Arrange
const uint64_t input = 0x000034363bc70c71;
std::string expected = "34:36:3B:C7:0C:71";
// Act
std::string result = uint64_to_mac_address_string(input);
// Assert
EXPECT_EQ(result, expected);
}
TEST(UtilsTests, StringToMacAddress) {
// Arrange
std::string input = "34:36:3B:C7:8C:71";
const uint64_t expected = 0x000034363bc78c71;
// Act
uint64_t result = mac_address_string_to_uint64(input);
// Assert
EXPECT_EQ(result, expected);
}
constexpr absl::string_view kIpDotdecimal{"192.168.1.37"};
constexpr char kIp4Bytes[] = {(char)192, (char)168, (char)1, (char)37};
TEST(UtilsTests, Ip4BytesToDotdecimal) {
std::string result =
ipaddr_4bytes_to_dotdecimal_string(absl::string_view(kIp4Bytes));
EXPECT_EQ(result, kIpDotdecimal);
}
TEST(UtilsTests, IpDotdecimalTo4Bytes) {
std::string result =
ipaddr_dotdecimal_to_4bytes_string(std::string(kIpDotdecimal));
EXPECT_EQ(result, std::string(kIp4Bytes, 4));
}
/*
TEST(UtilsTests, ConvertBetweenWinrtGuidAndNearbyUuidSuccessfully) {
Uuid uuid(0x123e4567e89b12d3, 0xa456426614174000);
winrt::guid guid("{123e4567-e89b-12d3-a456-426614174000}");
EXPECT_EQ(uuid, winrt_guid_to_nearby_uuid(guid));
EXPECT_EQ(nearby_uuid_to_winrt_guid(uuid), guid);
EXPECT_TRUE(is_nearby_uuid_equal_to_winrt_guid(uuid, guid));
}
TEST(UtilsTests, CompareWinrtGuidAndNearbyUuidSuccessfully) {
Uuid uuid(0x123e4567e89b12d3, 0xa456426614174000);
winrt::guid guid("123e4567-e89b-12d3-a456-426614074000");
EXPECT_NE(uuid, winrt_guid_to_nearby_uuid(guid));
}
*/
} // namespace linux
} // namespace nearby