Skip to content

Commit

Permalink
Separate I2CAddr from I2CExplorer
Browse files Browse the repository at this point in the history
Summary:
Problem:
I2CExplore can't include Utils because it'll cause circular dependency as below:
I2CExplorer -> ConfigValidator -> Utils -> I2CExplorer

To avoid this, move I2CAddr as a separate library, the graph more or less becomes:

I2CAddr -> ConfigValidator -> Utils -> I2CExplorer

Reviewed By: somasun

Differential Revision: D62396774

fbshipit-source-id: d54f56cd0080b868b4568ad1cf90d315737c0f52
  • Loading branch information
Justin Kim authored and facebook-github-bot committed Sep 24, 2024
1 parent a9496b0 commit 158a52f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 39 deletions.
30 changes: 23 additions & 7 deletions fboss/platform/platform_manager/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ cpp_library(
"ConfigValidator.cpp",
],
exported_deps = [
":i2c_explorer",
":i2c_misc",
":platform_manager_config-cpp2-types",
"//folly/logging:logging",
],
Expand All @@ -61,13 +61,14 @@ cpp_library(
"I2cExplorer.cpp",
],
exported_deps = [
"fbcode//fboss/lib/i2c:i2c_ctrl",
"fbcode//fboss/platform/helpers:platform_utils",
"fbcode//folly:file_util",
"fbcode//folly:string",
"fbcode//folly/logging:logging",
"fbsource//third-party/fmt:fmt",
":i2c_misc",
":platform_manager_config-cpp2-types",
"//fboss/lib/i2c:i2c_ctrl",
"//fboss/platform/helpers:platform_utils",
"//folly:file_util",
"//folly:string",
"//folly/logging:logging",
],
exported_external_deps = [
"re2",
Expand Down Expand Up @@ -159,7 +160,9 @@ cpp_library(

cpp_library(
name = "utils",
srcs = ["Utils.cpp"],
srcs = [
"Utils.cpp",
],
exported_deps = [
":config_validator",
":platform_manager_config-cpp2-types",
Expand All @@ -176,6 +179,19 @@ cpp_library(
],
)

cpp_library(
name = "i2c_misc",
headers = [
"I2cAddr.h",
],
exported_deps = [
"fbsource//third-party/fmt:fmt",
],
exported_external_deps = [
"re2",
],
)

cpp_library(
name = "data_store",
srcs = ["DataStore.cpp"],
Expand Down
3 changes: 1 addition & 2 deletions fboss/platform/platform_manager/ConfigValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
#include <folly/logging/xlog.h>
#include <re2/re2.h>

#include "fboss/platform/platform_manager/I2cExplorer.h"
#include "fboss/platform/platform_manager/gen-cpp2/platform_manager_config_constants.h"
#include "fboss/platform/platform_manager/I2cAddr.h"

namespace {
const re2::RE2 kRpmVersionRegex{"^[0-9]+\\.[0-9]+\\.[0-9]+\\-[0-9]+$"};
Expand Down
43 changes: 43 additions & 0 deletions fboss/platform/platform_manager/I2cAddr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary.

#pragma once

#include <string>

#include <fmt/format.h>
#include <re2/re2.h>

namespace facebook::fboss::platform::platform_manager {

inline const re2::RE2 kI2cAddrRe{"0x[0-9a-f]{2}"};

struct I2cAddr {
public:
explicit I2cAddr(uint16_t addr) : addr_(addr) {}
explicit I2cAddr(const std::string& addr)
: addr_(std::stoi(addr, nullptr, 16 /* base */)) {
if (!re2::RE2::FullMatch(addr, kI2cAddrRe)) {
throw std::invalid_argument("Invalid i2c addr: " + addr);
}
}
bool operator==(const I2cAddr& b) const {
return addr_ == b.addr_;
}
// Returns string in the format 0x0f
std::string hex2Str() const {
return fmt::format("{:#04x}", addr_);
}
// Returns string in the format 000f
std::string hex4Str() const {
return fmt::format("{:04x}", addr_);
}
// Returns integer
uint16_t raw() const {
return addr_;
}

private:
uint16_t addr_{0};
};

} // namespace facebook::fboss::platform::platform_manager
31 changes: 1 addition & 30 deletions fboss/platform/platform_manager/I2cExplorer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <map>
#include <memory>
#include <optional>
#include <stdexcept>
#include <vector>

#include <fmt/format.h>
Expand All @@ -15,39 +14,11 @@
#include <re2/re2.h>

#include "fboss/platform/helpers/PlatformUtils.h"
#include "fboss/platform/platform_manager/I2cAddr.h"
#include "fboss/platform/platform_manager/gen-cpp2/platform_manager_config_types.h"

namespace facebook::fboss::platform::platform_manager {

struct I2cAddr {
public:
explicit I2cAddr(uint16_t addr) : addr_(addr) {}
explicit I2cAddr(const std::string& addr)
: addr_(std::stoi(addr, nullptr, 16 /* base */)) {
if (!re2::RE2::FullMatch(addr, re2::RE2{"0x[0-9a-f]{2}"})) {
throw std::invalid_argument("Invalid i2c addr: " + addr);
}
}
bool operator==(const I2cAddr& b) const {
return addr_ == b.addr_;
}
// Returns string in the format 0x0f
std::string hex2Str() const {
return fmt::format("{:#04x}", addr_);
}
// Returns string in the format 000f
std::string hex4Str() const {
return fmt::format("{:04x}", addr_);
}
// Returns integer
uint16_t raw() const {
return addr_;
}

private:
uint16_t addr_{0};
};

class I2cExplorer {
public:
virtual ~I2cExplorer() = default;
Expand Down

0 comments on commit 158a52f

Please sign in to comment.