| #include "tlbmc/hal/sysfs/hwmon.h" |
| |
| #include <fstream> |
| #include <string_view> |
| |
| #include "absl/status/status.h" |
| #include "absl/strings/str_cat.h" |
| #include "hal_common_config.pb.h" |
| |
| namespace milotic_tlbmc { |
| |
| absl::Status HwmonSysfs::NewDevice(const HalCommonConfig& config, |
| std::string_view driver_name) const { |
| if (IsDevicePresent(config)) { |
| return absl::AlreadyExistsError( |
| absl::StrCat("Device ", config, " already exists")); |
| } |
| |
| boost::filesystem::path path = GetBusPath(config.bus()) / "new_device"; |
| |
| std::ofstream file(path.string()); |
| if (!file.good()) { |
| return absl::InternalError(absl::StrCat("Failed to open ", path.string())); |
| } |
| |
| file << driver_name << " " << config.address() << "\n"; |
| file.flush(); |
| |
| if (!file.good()) { |
| return absl::InternalError( |
| absl::StrCat("Failed to write to ", path.string())); |
| } |
| |
| return absl::OkStatus(); |
| } |
| |
| absl::Status HwmonSysfs::DeleteDevice(const HalCommonConfig& config) const { |
| // No `IsDevicePresent` check on this like in `NewDevice`, since it |
| // might be used to clean up after a device instantiation that was only |
| // partially successful |
| |
| boost::filesystem::path path = GetBusPath(config.bus()) / "delete_device"; |
| std::ofstream file(path.string()); |
| if (!file.good()) { |
| return absl::InternalError(absl::StrCat("Failed to open ", path.string())); |
| } |
| file << config.address() << "\n"; |
| file.flush(); |
| if (!file.good()) { |
| return absl::InternalError( |
| absl::StrCat("Failed to write to ", path.string())); |
| } |
| return absl::OkStatus(); |
| } |
| |
| } // namespace milotic_tlbmc |