Skip to content

Commit

Permalink
fan_service : Fan_service control logic to use either sysfs path or g…
Browse files Browse the repository at this point in the history
…pio, for detecting fan presence

Summary: [FBOSS/Platform] fan_service : Fan_service control logic to use either sysfs path or gpio, for detecting fan presence

Reviewed By: alandau, kimdo8736

Differential Revision: D62211922

fbshipit-source-id: d0fb8322da74a76ed47bf2e446096afeff88dddc
  • Loading branch information
Mike Choi authored and facebook-github-bot committed Sep 16, 2024
1 parent 7673a73 commit a091b41
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
3 changes: 3 additions & 0 deletions cmake/PlatformFanService.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ target_link_libraries(fan_service_lib
platform_name_lib
platform_utils
fan_service_config_types_cpp2
gpiod_line
sensor_service_cpp2
fan_service_cpp2
Folly::folly
Expand Down Expand Up @@ -72,6 +73,8 @@ add_executable(fan_service_sw_test
target_link_libraries(fan_service_sw_test
fan_service_lib
Folly::folly
${LIBGPIOD}
gpiod_line
${GTEST}
${LIBGMOCK_LIBRARIES}
)
Expand Down
4 changes: 4 additions & 0 deletions fboss/platform/fan_service/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ cpp_library(
"//fboss/fsdb/if:fsdb_model",
"//fboss/fsdb/if:fsdb_oper-cpp2-types",
"//fboss/lib:common_file_utils",
"//fboss/lib:gpiod_line",
"//fboss/platform/fan_service/if:fan_service-cpp2-services",
"//fboss/platform/fan_service/if:fan_service-cpp2-types",
"//fboss/platform/helpers:platform_utils",
Expand All @@ -104,6 +105,9 @@ cpp_library(
"//security/ca/lib:cert_path_picker",
"//thrift/lib/cpp2/protocol:protocol",
],
exported_external_deps = [
("libgpiod", None, "gpiod"),
],
)

custom_unittest(
Expand Down
40 changes: 31 additions & 9 deletions fboss/platform/fan_service/ControlLogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
#include "fboss/platform/fan_service/ControlLogic.h"

#include <folly/logging/xlog.h>
#include <gpiod.h>

#include "common/time/Time.h"
#include "fboss/lib/GpiodLine.h"
#include "fboss/platform/fan_service/SensorData.h"
#include "fboss/platform/fan_service/if/gen-cpp2/fan_service_config_constants.h"
#include "fboss/platform/fan_service/if/gen-cpp2/fan_service_config_types.h"
Expand Down Expand Up @@ -349,15 +351,35 @@ void ControlLogic::getOpticsUpdate() {
bool ControlLogic::isFanPresentInDevice(const Fan& fan) {
unsigned int readVal;
bool readSuccessful = false;
try {
readVal = static_cast<unsigned>(pBsp_->readSysfs(*fan.presenceSysfsPath()));
readSuccessful = true;
} catch (std::exception&) {
XLOG(ERR) << "Failed to read sysfs " << *fan.presenceSysfsPath();
}
auto fanPresent = (readSuccessful && readVal == *fan.fanPresentVal());
if (!fanPresent) {
XLOG(INFO) << fmt::format("{}: is absent in the host", *fan.fanName());
bool fanPresent = false;
if (fan.presenceSysfsPath()) {
try {
readVal =
static_cast<unsigned>(pBsp_->readSysfs(*fan.presenceSysfsPath()));
readSuccessful = true;
} catch (std::exception&) {
XLOG(ERR) << "Failed to read sysfs " << *fan.presenceSysfsPath();
}
fanPresent = (readSuccessful && readVal == *fan.fanPresentVal());
if (fanPresent) {
XLOG(INFO) << fmt::format(
"{}: is present in the host (through sysfs)", *fan.fanName());
} else {
XLOG(INFO) << fmt::format(
"{}: is absent in the host (through sysfs)", *fan.fanName());
}
} else if (fan.presenceGpio()) {
struct gpiod_chip* chip =
gpiod_chip_open(fan.presenceGpio()->path()->c_str());
GpiodLine line(chip, *fan.presenceGpio()->lineIndex(), "gpioline");
int16_t value = line.getValue();
gpiod_chip_close(chip);
if (value == *fan.presenceGpio()->desiredValue()) {
fanPresent = true;
} else {
XLOG(INFO) << fmt::format(
"{}: is absent in the host (through gpio)", *fan.fanName());
}
}
fb303::fbData->setCounter(
fmt::format(kFanAbsent, *fan.fanName()), !fanPresent);
Expand Down

0 comments on commit a091b41

Please sign in to comment.