Change primary postcode interface to byte array

Change primary code to std::vector<uint8_t> to meet the changes in
phosphor-dbus-interface.

[1] https://gerrit.openbmc.org/c/openbmc/phosphor-dbus-interfaces/+/74633

Change-Id: Ib5822973a1dbc6aa8c52f4e81b56091dc33b5f08
Signed-off-by: Cosmo Chou <cosmo.chou@quantatw.com>
Signed-off-by: Potin Lai <potin.lai@quantatw.com>
diff --git a/7seg.cpp b/7seg.cpp
index 501c3f5..a9247d2 100644
--- a/7seg.cpp
+++ b/7seg.cpp
@@ -25,14 +25,14 @@
 
 static void DisplayDbusValue(FILE* f, postcode_t postcodes)
 {
-    auto postcode = std::get<primary_post_code_t>(postcodes);
+    const auto& postcode = std::get<0>(postcodes);
     // Uses cstdio instead of streams because the device file has
     // very strict requirements about the data format and streaming
     // abstractions tend to muck it up.
-    if (f)
+    if (f && !postcode.empty())
     {
-        int rc = std::fprintf(f, "%d%02x\n", (postcode > 0xff),
-                              static_cast<uint8_t>(postcode & 0xff));
+        int rc =
+            std::fprintf(f, "%d%02x\n", postcode.size() > 1, postcode.back());
         if (rc < 0)
         {
             std::fprintf(stderr, "failed to write 7seg value: rc=%d\n", rc);
diff --git a/example.cpp b/example.cpp
index b3549d0..95cc7a4 100644
--- a/example.cpp
+++ b/example.cpp
@@ -25,8 +25,14 @@
 static void printPostcode(FILE*, postcode_t postcode)
 {
     /* Print output to verify the example program is receiving values. */
-    std::printf("recv: 0x%" PRIx64 "\n",
-                std::get<primary_post_code_t>(postcode));
+    auto code = std::get<0>(postcode);
+    std::string hexCode;
+    hexCode.reserve(2 * code.size());
+    for (const auto& byte : code)
+    {
+        hexCode += std::format("{:02x}", byte);
+    }
+    std::printf("recv: 0x%s\n", hexCode.c_str());
 }
 
 /*
diff --git a/ipmisnoop/ipmisnoop.cpp b/ipmisnoop/ipmisnoop.cpp
index 56d06d9..474fc34 100644
--- a/ipmisnoop/ipmisnoop.cpp
+++ b/ipmisnoop/ipmisnoop.cpp
@@ -113,12 +113,12 @@
 
                 if (posVal > minPositionVal && posVal < maxPositionVal)
                 {
-                    std::tuple<uint64_t, secondary_post_code_t> postcodes =
-                        reporters[posVal - 1]->value();
-                    uint64_t postcode = std::get<uint64_t>(postcodes);
+                    std::tuple<primary_post_code_t, secondary_post_code_t>
+                        postcodes = reporters[posVal - 1]->value();
+                    auto postcode = std::get<0>(postcodes);
 
                     // write postcode into seven segment display
-                    if (postCodeDisplay(postcode) < 0)
+                    if (postCodeDisplay(postcode[0]) < 0)
                     {
                         fprintf(stderr, "Error in display the postcode\n");
                     }
diff --git a/ipmisnoop/ipmisnoop.hpp b/ipmisnoop/ipmisnoop.hpp
index d8319ac..3f52275 100644
--- a/ipmisnoop/ipmisnoop.hpp
+++ b/ipmisnoop/ipmisnoop.hpp
@@ -19,7 +19,6 @@
 const std::string ipmiSnoopObject = "/xyz/openbmc_project/state/boot/raw";
 
 const int hostParseIdx = 3;
-const int maxPostcode = 255;
 const int maxPosition = 4;
 
 extern bool sevenSegmentLedEnabled;
@@ -54,7 +53,7 @@
             sdbusplus::bus::match::rules::propertiesChanged(objPath, rawIface),
 
             [this, &bus](sdbusplus::message_t& msg) {
-                using primarycode_t = uint64_t;
+                using primarycode_t = std::vector<uint8_t>;
                 using secondarycode_t = std::vector<uint8_t>;
                 using postcode_t = std::tuple<primarycode_t, secondarycode_t>;
 
@@ -95,18 +94,15 @@
                     std::cerr << "Value property is not found " << std::endl;
                     return;
                 }
-                uint64_t postcode =
+                auto postcode =
                     std::get<0>(std::get<postcode_t>(valPropMap->second));
 
-                if (postcode <= maxPostcode)
+                if (postcode.size() == 1)
                 {
                     if (position == hostNum)
                     {
-                        uint8_t postcode_8bit =
-                            static_cast<uint8_t>(postcode & 0x0000FF);
-
                         // write postcode into seven segment display
-                        if (postCodeDisplay(postcode_8bit) < 0)
+                        if (postCodeDisplay(postcode[0]) < 0)
                         {
                             fprintf(stderr, "Error in display the postcode\n");
                         }
diff --git a/lpcsnoop/snoop.hpp b/lpcsnoop/snoop.hpp
index 9d11ac3..2d43251 100644
--- a/lpcsnoop/snoop.hpp
+++ b/lpcsnoop/snoop.hpp
@@ -13,7 +13,7 @@
 using ServerObject = typename sdbusplus::server::object_t<T...>;
 using PostInterface = sdbusplus::xyz::openbmc_project::State::Boot::server::Raw;
 using PostObject = ServerObject<PostInterface>;
-using primary_post_code_t = uint64_t;
+using primary_post_code_t = std::vector<uint8_t>;
 using secondary_post_code_t = std::vector<uint8_t>;
 using postcode_t = std::tuple<primary_post_code_t, secondary_post_code_t>;
 
diff --git a/main.cpp b/main.cpp
index e544a11..f4353d7 100644
--- a/main.cpp
+++ b/main.cpp
@@ -44,7 +44,7 @@
 
 static size_t codeSize = 1; /* Size of each POST code in bytes */
 static bool verbose = false;
-static std::function<bool(uint64_t&, ssize_t)> procPostCode;
+static std::function<bool(std::vector<uint8_t>&, ssize_t)> procPostCode;
 
 static void usage(const char* name)
 {
@@ -134,7 +134,7 @@
  * aspeedPCCBuffer contains enough PCC codes, the postcode will be assigned as
  * 0xDDCCBBAA.
  */
-bool aspeedPCC(uint64_t& code, ssize_t readb)
+bool aspeedPCC(std::vector<uint8_t>& code, ssize_t readb)
 {
     // Size of data coming from the PCC hardware
     constexpr size_t pccSize = sizeof(uint16_t);
@@ -148,7 +148,7 @@
     constexpr uint16_t pccPostCodeMask = 0x00FF;
     constexpr uint8_t byteShift = 8;
 
-    uint16_t* codePtr = reinterpret_cast<uint16_t*>(&code);
+    uint16_t* codePtr = reinterpret_cast<uint16_t*>(code.data());
 
     for (size_t i = 0; i < (readb / pccSize); i++)
     {
@@ -180,11 +180,10 @@
     }
 
     // Remove the prefix bytes and combine the partial postcodes together.
-    code = 0;
-    for (size_t i = 0; i < fullPostPCCCount; i++)
+    code.clear();
+    for (size_t i = fullPostPCCCount; i > 0; --i)
     {
-        code |= static_cast<uint64_t>(aspeedPCCBuffer[i] & pccPostCodeMask)
-                << (byteShift * i);
+        code.push_back(aspeedPCCBuffer[i - 1] & pccPostCodeMask);
     }
     aspeedPCCBuffer.erase(aspeedPCCBuffer.begin(),
                           aspeedPCCBuffer.begin() + fullPostPCCCount);
@@ -199,30 +198,37 @@
 void PostCodeEventHandler(PostReporter* reporter, sdeventplus::source::IO& s,
                           int postFd, uint32_t)
 {
-    uint64_t code = 0;
+    std::vector<uint8_t> code(codeSize, 0);
     ssize_t readb;
 
-    while ((readb = read(postFd, &code, codeSize)) > 0)
+    while ((readb = read(postFd, code.data(), codeSize)) > 0)
     {
         if (procPostCode && procPostCode(code, readb) == false)
         {
             return;
         }
 
-        code = le64toh(code);
         if (verbose)
         {
-            fprintf(stderr, "Code: 0x%" PRIx64 "\n", code);
+            fprintf(stderr, "Code: 0x");
+            for (const auto& byte : code)
+            {
+                fprintf(stderr, "%02x", byte);
+            }
+            fprintf(stderr, "\n");
         }
         // HACK: Always send property changed signal even for the same code
         // since we are single threaded, external users will never see the
         // first value.
-        reporter->value(std::make_tuple(~code, secondary_post_code_t{}), true);
+        code[0] = ~code[0];
+        reporter->value(std::make_tuple(code, secondary_post_code_t{}), true);
+        code[0] = ~code[0];
         reporter->value(std::make_tuple(code, secondary_post_code_t{}));
 
         // read depends on old data being cleared since it doesn't always read
         // the full code size
-        code = 0;
+        code.resize(codeSize);
+        std::fill(code.begin(), code.end(), 0);
 
         if (rateLimit(*reporter, s))
         {
diff --git a/test/post_reporter_test.cpp b/test/post_reporter_test.cpp
index 170e90f..4a4f695 100644
--- a/test/post_reporter_test.cpp
+++ b/test/post_reporter_test.cpp
@@ -53,54 +53,54 @@
     PostReporter testReporter(bus, snoopObject, true);
 }
 
-TEST_F(PostReporterTest, ValueReadsDefaultToZero)
+TEST_F(PostReporterTest, ValueReadsDefaultToEmpty)
 {
     PostReporter testReporter(bus, snoopObject, true);
-    EXPECT_EQ(0, std::get<primary_post_code_t>(testReporter.value()));
+    EXPECT_TRUE(std::get<0>(testReporter.value()).empty());
 }
 
 TEST_F(PostReporterTest, SetValueToPositiveValueWorks)
 {
     PostReporter testReporter(bus, snoopObject, true);
+    primary_post_code_t primaryCode = {122, 126, 127};
     secondary_post_code_t secondaryCode = {123, 124, 125};
-    testReporter.value(std::make_tuple(65537, secondaryCode));
-    EXPECT_EQ(65537, std::get<primary_post_code_t>(testReporter.value()));
-    EXPECT_EQ(secondaryCode,
-              std::get<secondary_post_code_t>(testReporter.value()));
+    testReporter.value(std::make_tuple(primaryCode, secondaryCode));
+    EXPECT_EQ(primaryCode, std::get<0>(testReporter.value()));
+    EXPECT_EQ(secondaryCode, std::get<1>(testReporter.value()));
 }
 
 TEST_F(PostReporterTest, SetValueMultipleTimesWorks)
 {
     PostReporter testReporter(bus, snoopObject, true);
+    primary_post_code_t primaryCode = {20, 21, 0, 123};
     secondary_post_code_t secondaryCode = {10, 40, 0, 245, 56};
-    testReporter.value(std::make_tuple(123, secondaryCode));
-    EXPECT_EQ(123, std::get<primary_post_code_t>(testReporter.value()));
-    EXPECT_EQ(secondaryCode,
-              std::get<secondary_post_code_t>(testReporter.value()));
+    testReporter.value(std::make_tuple(primaryCode, secondaryCode));
+    EXPECT_EQ(primaryCode, std::get<0>(testReporter.value()));
+    EXPECT_EQ(secondaryCode, std::get<1>(testReporter.value()));
 
+    primaryCode = {44, 45};
     secondaryCode = {0, 0, 0, 0, 0};
-    testReporter.value(std::make_tuple(45, secondaryCode));
-    EXPECT_EQ(45, std::get<primary_post_code_t>(testReporter.value()));
-    EXPECT_EQ(secondaryCode,
-              std::get<secondary_post_code_t>(testReporter.value()));
+    testReporter.value(std::make_tuple(primaryCode, secondaryCode));
+    EXPECT_EQ(primaryCode, std::get<0>(testReporter.value()));
+    EXPECT_EQ(secondaryCode, std::get<1>(testReporter.value()));
 
+    primaryCode = {0};
     secondaryCode = {23, 200, 0, 45, 2};
-    testReporter.value(std::make_tuple(0, secondaryCode));
-    EXPECT_EQ(0, std::get<primary_post_code_t>(testReporter.value()));
-    EXPECT_EQ(secondaryCode,
-              std::get<secondary_post_code_t>(testReporter.value()));
+    testReporter.value(std::make_tuple(primaryCode, secondaryCode));
+    EXPECT_EQ(primaryCode, std::get<0>(testReporter.value()));
+    EXPECT_EQ(secondaryCode, std::get<1>(testReporter.value()));
 
+    primaryCode = {46};
     secondaryCode = {10, 40, 0, 35, 78};
-    testReporter.value(std::make_tuple(46, secondaryCode));
-    EXPECT_EQ(46, std::get<primary_post_code_t>(testReporter.value()));
-    EXPECT_EQ(secondaryCode,
-              std::get<secondary_post_code_t>(testReporter.value()));
+    testReporter.value(std::make_tuple(primaryCode, secondaryCode));
+    EXPECT_EQ(primaryCode, std::get<0>(testReporter.value()));
+    EXPECT_EQ(secondaryCode, std::get<1>(testReporter.value()));
 
+    primaryCode = {46};
     secondaryCode = {10, 40, 0, 35, 78};
-    testReporter.value(std::make_tuple(46, secondaryCode));
-    EXPECT_EQ(46, std::get<primary_post_code_t>(testReporter.value()));
-    EXPECT_EQ(secondaryCode,
-              std::get<secondary_post_code_t>(testReporter.value()));
+    testReporter.value(std::make_tuple(primaryCode, secondaryCode));
+    EXPECT_EQ(primaryCode, std::get<0>(testReporter.value()));
+    EXPECT_EQ(secondaryCode, std::get<1>(testReporter.value()));
 }
 
 } // namespace