blob: 9cf64f2d65a5607c714df91e0b07ae31e40c01c3 [file]
#include "tlbmc/hal/sysfs/iio.h"
#include <fstream>
#include <string>
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/ascii.h"
#include "absl/strings/str_cat.h"
#include "boost/filesystem.hpp" // NOLINT: boost::filesystem is commonly used in BMC
#include "sensor.pb.h"
namespace milotic_tlbmc {
absl::StatusOr<boost::filesystem::path> IioSysfs::GetIioDeviceDirectory(
const std::string& root_iio_device_name) const {
boost::filesystem::path path(config_.iio_sysfs_path);
boost::system::error_code ec;
if (!boost::filesystem::exists(path, ec) || ec ||
!boost::filesystem::is_directory(path, ec) || ec) {
return absl::NotFoundError(
absl::StrCat("IIO sysfs path not found: ", path.string()));
}
boost::filesystem::directory_iterator it(path, ec);
if (ec) {
return absl::UnavailableError(
absl::StrCat("Could not iterate directory: ", path.string()));
}
for (const auto& entry : it) {
if (!boost::filesystem::is_directory(entry.path(), ec) || ec) {
continue;
}
boost::filesystem::path name_file_path = entry.path() / "name";
if (!boost::filesystem::exists(name_file_path, ec) || ec) {
continue;
}
std::ifstream name_file(name_file_path.string());
if (!name_file.is_open()) {
continue;
}
std::string content;
std::getline(name_file, content);
absl::StripTrailingAsciiWhitespace(&content);
if (content == root_iio_device_name) {
return boost::filesystem::path(entry.path());
}
}
return absl::NotFoundError(absl::StrCat("Device ", root_iio_device_name,
" not found in ", path.string()));
}
absl::StatusOr<boost::filesystem::path> IioSysfs::GetSensorPath(
const std::string& device_name, const std::string& channel_name) const {
absl::StatusOr<boost::filesystem::path> device_path =
GetIioDeviceDirectory(device_name);
if (!device_path.ok()) {
return device_path.status();
}
boost::filesystem::path sensor_file_path = device_path.value() / channel_name;
boost::system::error_code ec;
if (!boost::filesystem::exists(sensor_file_path, ec) || ec) {
return absl::NotFoundError(
absl::StrCat("Sensor file not found: ", sensor_file_path.string()));
}
return sensor_file_path;
}
} // namespace milotic_tlbmc