| #include "biossettings_callback.h" | |
| #include <fstream> | |
| #include <sstream> | |
| #include <string> | |
| #include "absl/log/log.h" | |
| #include "grpcblob_defs.h" | |
| #include "grpcpp/server_context.h" | |
| #include "grpcpp/support/status.h" | |
| #include "google/protobuf/text_format.h" | |
| namespace blobs { | |
| // persistent path | |
| constexpr const char* biosSettingsConfigFile = | |
| "/var/google/bios-settings.txtpb"; | |
| // Read and Write the simple message should be quick enough, we can use sync | |
| // implementation. | |
| grpc::Status BiosSettingsServiceImpl::GetBiosSettings( | |
| [[maybe_unused]] grpc::ServerContext* context, | |
| [[maybe_unused]] const google::protobuf::Empty* request, | |
| phosphor::bios::BiosSettings* reply) { | |
| std::string filename{biosSettingsConfigFile}; | |
| if ((instance + 1) != numInstances) { | |
| // Not last instance, assume we are multi-host system, append | |
| // "_instance" suffix | |
| filename += "_" + std::to_string(instance); | |
| } | |
| std::ifstream ifs(filename); | |
| std::string errorMessage; | |
| if (!ifs.is_open()) { | |
| errorMessage = "Failed to open " + filename; | |
| LOG(ERROR) << errorMessage; | |
| return {grpc::StatusCode::NOT_FOUND, errorMessage}; | |
| } | |
| std::stringstream buffer; | |
| buffer << ifs.rdbuf(); | |
| std::string content = buffer.str(); | |
| if (content.empty()) { | |
| errorMessage = "Empty " + filename; | |
| LOG(ERROR) << errorMessage; | |
| return {grpc::StatusCode::INTERNAL, errorMessage}; | |
| } | |
| google::protobuf::TextFormat::Parser parser; | |
| if (!parser.ParseFromString(content, reply)) { | |
| errorMessage = "Failed to parse " + filename; | |
| LOG(ERROR) << errorMessage; | |
| return {grpc::StatusCode::INTERNAL, errorMessage}; | |
| } | |
| LOG(INFO) << "Received GetBiosSettings gRPC"; | |
| return grpc::Status::OK; | |
| } | |
| grpc::Status BiosSettingsServiceImpl::SetBiosSettings( | |
| [[maybe_unused]] grpc::ServerContext* context, | |
| const phosphor::bios::BiosSettings* request, | |
| [[maybe_unused]] google::protobuf::Empty* reply) { | |
| std::string filename{biosSettingsConfigFile}; | |
| if ((instance + 1) != numInstances) { | |
| // Not last instance, assume we are multi-host system, append | |
| // "_instance" suffix | |
| filename += "_" + std::to_string(instance); | |
| } | |
| phosphor::bios::BiosSettings settings; | |
| settings.CopyFrom(*request); | |
| std::string errorMessage; | |
| std::string text; | |
| google::protobuf::TextFormat::Printer printer; | |
| if (!printer.PrintToString(settings, &text)) { | |
| errorMessage = "Failed to transform BiosSettings message into text format"; | |
| LOG(ERROR) << errorMessage; | |
| return {grpc::StatusCode::INVALID_ARGUMENT, errorMessage}; | |
| } | |
| std::ofstream out(filename); | |
| if (!out) { | |
| errorMessage = "Failed to open " + filename; | |
| LOG(ERROR) << errorMessage; | |
| return {grpc::StatusCode::INTERNAL, errorMessage}; | |
| } | |
| out << text; | |
| out.close(); | |
| if (!out) { | |
| errorMessage = "Failed to write " + filename; | |
| LOG(ERROR) << errorMessage; | |
| return {grpc::StatusCode::INTERNAL, errorMessage}; | |
| } | |
| LOG(INFO) << "Received SetBiosSettingsStatus gRPC"; | |
| return grpc::Status::OK; | |
| } | |
| } // namespace blobs |