Implement post code manager

This depends on interfaces definition:
https://gerrit.openbmc-project.xyz/c/openbmc/phosphor-dbus-interfaces/+/20474

Implement method and properties defined in PostCode.interface.yaml
under phosphor-dbus-interfaces/xyz/openbmc_project/State/Boot
1. Method: std::vector<uint64_t> PostCode::getPostCodes(uint16_t index)
2. Properties: CurrentBootCycleIndex/MaxBootCycleNum

Test-By:
    Every cycle post codes is saved in "/var/lib/phosphor-post-code-manager"
    "1" file is saved all post codes for cycle 1
    "2" file is saved all post codes for cycle 2
    "CurrentBootCycleIndex" file is saved the current boot cycle number.
    root@wolfpass:/var/lib/phosphor-post-code-manager# ls
      1  2 CurrentBootCycleIndex

Change-Id: Ia89b9121983261fef5573092d890beb84626ceeb
Signed-off-by: Kuiying Wang <kuiying.wang@intel.com>
diff --git a/src/main.cpp b/src/main.cpp
new file mode 100644
index 0000000..fcd09d3
--- /dev/null
+++ b/src/main.cpp
@@ -0,0 +1,60 @@
+/*
+// Copyright (c) 2019 Intel Corporation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+*/
+#include "post_code.hpp"
+
+int main(int argc, char* argv[])
+{
+    int ret = 0;
+
+    phosphor::logging::log<phosphor::logging::level::INFO>(
+        "Start post code manager service...");
+
+    sd_event* event = nullptr;
+    ret = sd_event_default(&event);
+    if (ret < 0)
+    {
+        phosphor::logging::log<phosphor::logging::level::ERR>(
+            "Error creating a default sd_event handler");
+        return ret;
+    }
+    EventPtr eventP{event};
+    event = nullptr;
+
+    sdbusplus::bus::bus bus = sdbusplus::bus::new_default();
+    sdbusplus::server::manager_t m{bus, DBUS_OBJECT_NAME};
+
+    bus.request_name(DBUS_INTF_NAME);
+
+    PostCode postCode{bus, DBUS_OBJECT_NAME, eventP};
+
+    try
+    {
+        bus.attach_event(eventP.get(), SD_EVENT_PRIORITY_NORMAL);
+        ret = sd_event_loop(eventP.get());
+        if (ret < 0)
+        {
+            phosphor::logging::log<phosphor::logging::level::ERR>(
+                "Error occurred during the sd_event_loop",
+                phosphor::logging::entry("RET=%d", ret));
+        }
+    }
+    catch (std::exception& e)
+    {
+        phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
+        return -1;
+    }
+    return 0;
+}
diff --git a/src/post_code.cpp b/src/post_code.cpp
new file mode 100644
index 0000000..8a94911
--- /dev/null
+++ b/src/post_code.cpp
@@ -0,0 +1,114 @@
+/*
+// Copyright (c) 2019 Intel Corporation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+*/
+#include "post_code.hpp"
+std::vector<uint64_t> PostCode::getPostCodes(uint16_t index)
+{
+    std::vector<uint64_t> codes;
+
+    if (currentBootCycleIndex() == index)
+        return postCodes;
+    deserializePostCodes(fs::path(strPostCodeListPath + std::to_string(index)),
+                         codes);
+    return codes;
+}
+void PostCode::savePostCodes(uint64_t code)
+{
+    postCodes.push_back(code);
+    serialize(fs::path(PostCodeListPath));
+    return;
+}
+
+fs::path PostCode::serialize(const std::string& path)
+{
+    try
+    {
+        uint16_t index = currentBootCycleIndex();
+        fs::path fullPath(path + strCurrentBootCycleIndexName);
+        std::ofstream os(fullPath.c_str(), std::ios::binary);
+        cereal::JSONOutputArchive oarchive(os);
+        oarchive(index);
+
+        std::ofstream osPostCodes(
+            (path + std::to_string(currentBootCycleIndex())).c_str(),
+            std::ios::binary);
+        cereal::JSONOutputArchive oarchivePostCodes(osPostCodes);
+        oarchivePostCodes(postCodes);
+    }
+    catch (cereal::Exception& e)
+    {
+        phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
+        return "";
+    }
+    catch (const fs::filesystem_error& e)
+    {
+        phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
+        return "";
+    }
+    return path;
+}
+
+bool PostCode::deserialize(const fs::path& path, uint16_t& index)
+{
+    try
+    {
+        if (fs::exists(path))
+        {
+            std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
+            cereal::JSONInputArchive iarchive(is);
+            iarchive(index);
+            return true;
+        }
+        return false;
+    }
+    catch (cereal::Exception& e)
+    {
+        phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
+        return false;
+    }
+    catch (const fs::filesystem_error& e)
+    {
+        return false;
+    }
+
+    return false;
+}
+
+bool PostCode::deserializePostCodes(const fs::path& path,
+                                    std::vector<uint64_t>& codes)
+{
+    try
+    {
+        if (fs::exists(path))
+        {
+            std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
+            cereal::JSONInputArchive iarchive(is);
+            iarchive(codes);
+            return true;
+        }
+        return false;
+    }
+    catch (cereal::Exception& e)
+    {
+        phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
+        return false;
+    }
+    catch (const fs::filesystem_error& e)
+    {
+        return false;
+    }
+
+    return false;
+}