blob: a71b103d25d4d9e03182c98da103e9eae51b639b [file] [log] [blame]
Alexander Hansend6a95742025-01-06 15:06:25 +01001#include "utils.hpp"
2
3#include <libpldm/firmware_update.h>
4#include <libpldm/pldm_types.h>
5
6#include <string>
7
8namespace pldm
9{
10namespace utils
11{
12
13 std::expected<std::string, std::string>
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