main: Improve support for existing and new driver semantics

The current Nuvoton driver will emit multiple post codes per read, but is buggy
and can send part of a post code if more than one is read at a time.
This means our current code will put the driver into a confused state if
we are using 4 byte post codes and the driver happens to have only
process 7 bytes of 8 bytes worth of codes in its internal fifo. This
leaves one byte in the fifo which cannot be read again until another 4
bytes are ready. Limiting our read size will prevent this issue as a
workaround until the new driver code can be submitted.

A driver re-work is in progress that will limit the output to a single
post code, and will only write the number of bytes needed for the
io_write transaction on the other end of the bus. Our change trivially
supports this by populating the post code as zero before writing out the
little endian value so that the upper bits are zero if a short post code
is emitted by the driver. Eventually we can get rid of the bytes runtime
flag altogether and rely on the driver returning a single post code per
read.

Change-Id: Ia2e6dc0761a6dd8ddb7cf1194a4230686a7a3d6d
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/main.cpp b/main.cpp
index e31a178..f3815c1 100644
--- a/main.cpp
+++ b/main.cpp
@@ -16,13 +16,13 @@
 
 #include "lpcsnoop/snoop.hpp"
 
+#include <endian.h>
 #include <fcntl.h>
 #include <getopt.h>
 #include <sys/epoll.h>
 #include <systemd/sd-event.h>
 #include <unistd.h>
 
-#include <array>
 #include <cstdint>
 #include <exception>
 #include <iostream>
@@ -35,13 +35,6 @@
 static const char* snoopFilename = "/dev/aspeed-lpc-snoop0";
 static size_t codeSize = 1; /* Size of each POST code in bytes */
 
-/*
- * 256 bytes is a nice amount.  It's improbable we'd need this many, but its
- * gives us leg room in the event the driver poll doesn't return in a timely
- * fashion.  So, mostly arbitrarily chosen.
- */
-static constexpr size_t BUFFER_SIZE = 256;
-
 static void usage(const char* name)
 {
     fprintf(stderr,
@@ -52,20 +45,6 @@
             name, codeSize, snoopFilename);
 }
 
-static uint64_t assembleBytes(std::array<uint8_t, BUFFER_SIZE> buf, int start,
-                              int size)
-{
-    uint64_t result = 0;
-
-    for (int i = start + size - 1; i >= start; i--)
-    {
-        result <<= 8;
-        result |= buf[i];
-    }
-
-    return result;
-}
-
 /*
  * Callback handling IO event from the POST code fd. i.e. there is new
  * POST code available to read.
@@ -73,31 +52,17 @@
 void PostCodeEventHandler(sdeventplus::source::IO& s, int postFd,
                           uint32_t revents, PostReporter* reporter)
 {
-    std::array<uint8_t, BUFFER_SIZE> buffer;
-    int readb;
-
-    readb = read(postFd, buffer.data(), buffer.size());
+    uint64_t code = 0;
+    ssize_t readb = read(postFd, &code, codeSize);
     if (readb < 0)
     {
         /* Read failure. */
         s.get_event().exit(1);
         return;
     }
-
-    if (readb % codeSize != 0)
+    if (readb > 0)
     {
-        fprintf(stderr,
-                "Warning: read size %d not a multiple of "
-                "POST code length %zu. Some codes may be "
-                "corrupt or missing\n",
-                readb, codeSize);
-        readb -= (readb % codeSize);
-    }
-
-    /* Broadcast the values read. */
-    for (int i = 0; i < readb; i += codeSize)
-    {
-        reporter->value(assembleBytes(buffer, i, codeSize));
+        reporter->value(le64toh(code));
     }
 }