1 #include "utils.hpp" 2 3 #include <libpldm/firmware_update.h> 4 #include <libpldm/pldm_types.h> 5 6 #include <string> 7 8 namespace pldm 9 { 10 namespace utils 11 { 12 13 std::expected<std::string, std::string> toString(const uint8_t pldm_string_type,const struct variable_field & var)14 toString(const uint8_t pldm_string_type, 15 const struct variable_field &var) 16 { 17 if (var.ptr == nullptr || !var.length) { 18 return std::unexpected("null pointer"); 19 } 20 21 if (pldm_string_type != PLDM_STR_TYPE_ASCII) { 22 // unsupported string encoding 23 return std::unexpected("not an ascii string"); 24 } 25 26 std::string s(var.length, ' '); 27 for (size_t i = 0; i < var.length; i++) { 28 s[i] = static_cast<char>(var.ptr[i]); 29 } 30 31 return s; 32 } 33 34 } // namespace utils 35 } // namespace pldm 36