nemora-postd: import from gBMC

This is the POST code portion of nemorad.

Signed-off-by: Nan Zhou <nanzhoumails@gmail.com>
Google-Bug-Id: 179618653
Change-Id: Icf68fe8adf62c646238cf8235918a13effa857f8
diff --git a/.gitignore b/.gitignore
index 4849188..8be3ff7 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,4 +4,5 @@
 !/**/subprojects/kcsbridge/
 !/**/subprojects/metrics-ipmi-blobs/
 !/**/subprojects/ncsid/
+!/**/subprojects/nemora-postd/
 /**/subprojects/*.wrap
diff --git a/meson.build b/meson.build
index 12ae455..534d61f 100644
--- a/meson.build
+++ b/meson.build
@@ -38,3 +38,4 @@
 subproject('kcsbridge')
 subproject('ncsid', default_options: 'tests=' + tests_str)
 subproject('metrics-ipmi-blobs', default_options: 'tests=' + tests_str)
+subproject('nemora-postd')
diff --git a/nemora-postd b/nemora-postd
new file mode 120000
index 0000000..e8171d5
--- /dev/null
+++ b/nemora-postd
@@ -0,0 +1 @@
+subprojects/nemora-postd
\ No newline at end of file
diff --git a/subprojects/nemora-postd/MAINTAINERS b/subprojects/nemora-postd/MAINTAINERS
new file mode 100644
index 0000000..a8f2c52
--- /dev/null
+++ b/subprojects/nemora-postd/MAINTAINERS
@@ -0,0 +1,45 @@
+How to use this list:
+    Find the most specific section entry (described below) that matches where
+    your change lives and add the reviewers (R) and maintainers (M) as
+    reviewers. You can use the same method to track down who knows a particular
+    code base best.
+
+    Your change/query may span multiple entries; that is okay.
+
+    If you do not find an entry that describes your request at all, someone
+    forgot to update this list; please at least file an issue or send an email
+    to a maintainer, but preferably you should just update this document.
+
+Description of section entries:
+
+    Section entries are structured according to the following scheme:
+
+    X:  NAME <EMAIL_USERNAME@DOMAIN> <IRC_USERNAME!>
+    X:  ...
+    .
+    .
+    .
+
+    Where REPO_NAME is the name of the repository within the OpenBMC GitHub
+    organization; FILE_PATH is a file path within the repository, possibly with
+    wildcards; X is a tag of one of the following types:
+
+    M:  Denotes maintainer; has fields NAME <EMAIL_USERNAME@DOMAIN> <IRC_USERNAME!>;
+        if omitted from an entry, assume one of the maintainers from the
+        MAINTAINERS entry.
+    R:  Denotes reviewer; has fields NAME <EMAIL_USERNAME@DOMAIN> <IRC_USERNAME!>;
+        these people are to be added as reviewers for a change matching the repo
+        path.
+    F:  Denotes forked from an external repository; has fields URL.
+
+    Line comments are to be denoted "# SOME COMMENT" (typical shell style
+    comment); it is important to follow the correct syntax and semantics as we
+    may want to use automated tools with this file in the future.
+
+    A change cannot be added to an OpenBMC repository without a MAINTAINER's
+    approval; thus, a MAINTAINER should always be listed as a reviewer.
+
+START OF MAINTAINERS LIST
+-------------------------
+
+M:  Nan Zhou <nanzhoumails@gmails.com> <nanzhou!>
diff --git a/subprojects/nemora-postd/OWNERS b/subprojects/nemora-postd/OWNERS
new file mode 100644
index 0000000..1dca102
--- /dev/null
+++ b/subprojects/nemora-postd/OWNERS
@@ -0,0 +1 @@
+nanzhoumails@gmail.com
diff --git a/subprojects/nemora-postd/README.md b/subprojects/nemora-postd/README.md
new file mode 100644
index 0000000..f0e6792
--- /dev/null
+++ b/subprojects/nemora-postd/README.md
@@ -0,0 +1,38 @@
+## Nemora-postd
+
+Nemora-postd is a daemon running on the BMC to stream host POST codes.
+
+## Tests
+
+The following instruction is for manual testing, but the Robot Framework test can follow the same logic.
+
+### Prerequisites
+Install the latest version of [Protocol Buffers](https://github.com/protocolbuffers/protobuf/releases/tag/v3.13.0), and the latest version of [Linux / UNIX TCP Port Forwarder](http://www.dest-unreach.org/socat/).
+
+### Functional Tests
+1. On BMC, stop the existing Nemora-postd;
+```bash
+systemctl stop nemora-postd@eth0
+```
+2. On any machine, start a testing server at IP_SERVER listening to upcoming UDP datagrams;
+```
+DECODE_CMD="protoc --decode=platforms.nemora.proto.EventSeries event_message.proto"
+exec socat udp-recvfrom:3960,fork exec:"$DECODE_CMD",fdout=stdout
+```
+3. On BMC, start a new nemora session which sends POST codes to the testing server;
+```
+nemora-postd eth0 --udp4 $IP_SERVER
+```
+4. On BMC, manually change the DBus property via `busctl`;
+```
+busctl set-property xyz.openbmc_project.State.Boot.Raw /xyz/openbmc_project/state/boot/raw0 xyz.openbmc_project.State.Boot.Raw Value '('tay')' 10000004 3 1 2 3
+```
+5. The testing server should receive the following packet in about 20 seconds.
+```bash
+magic: 9876039085048616960
+mac: ...
+sent_time_us: ...
+postcodes: 0x989684
+postcodes_protocol: NATIVE_32_BIT
+```
+Note that, `magic` and `postcodes_protocol` are fixed. `postcodes` should be what you set via `busctl`.
diff --git a/subprojects/nemora-postd/event_message.proto b/subprojects/nemora-postd/event_message.proto
new file mode 100644
index 0000000..0b3f9eb
--- /dev/null
+++ b/subprojects/nemora-postd/event_message.proto
@@ -0,0 +1,42 @@
+// Copyright 2021 Google LLC
+//
+// 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.
+
+syntax = "proto2";
+
+// Note: it is important that the application is resilient to 
+// incorrectly encoded protobuf's.
+
+message EventSeries {
+  enum PostCodeProtocol {
+    PROTOCOL_UNSPECIFIED = 0;
+    EIGHT_BIT_WITH_EXTENDED_CODES = 1;
+    NATIVE_32_BIT = 2;
+  }
+  // A fixed integer to confirm the type of this protobuf: 0x890ebd38ec325800
+  optional fixed64 magic = 1;
+  // Firmware version of the device reporting this data.
+  optional int32 fw_version = 2;
+  // Primary MAC address of the server this data is from (in network order).
+  optional bytes mac = 3;
+  // Time message was sent in microseconds.
+  optional int64 sent_time_us = 4;
+  // Zero or more POST codes. Some codes may be missed because the host can
+  // send out codes faster than the EC handles them. While standard values are
+  // only 8-bits, non-standard values above 255 may be used.
+  repeated int32 postcodes = 5;
+  reserved 6 to 13;
+  optional PostCodeProtocol postcodes_protocol = 14;
+}
+
+package platforms.nemora.proto;
diff --git a/subprojects/nemora-postd/meson.build b/subprojects/nemora-postd/meson.build
new file mode 100644
index 0000000..f949663
--- /dev/null
+++ b/subprojects/nemora-postd/meson.build
@@ -0,0 +1,62 @@
+# Copyright 2021 Google LLC
+#
+# 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.
+
+project(
+  'nemora-postd',
+  'cpp',
+  version: '0.1',
+  default_options: [
+    'warning_level=3',
+    'werror=true',
+    'cpp_std=c++17'
+  ],
+)
+
+protobuf = dependency('protobuf')
+phosphor_dbus_interfaces = dependency('phosphor-dbus-interfaces')
+sdbusplus = dependency('sdbusplus')
+threads = dependency('threads')
+logging = dependency('phosphor-logging')
+fmt = declare_dependency(compile_args: '-DFMT_STRING_ALIAS=1', dependencies: dependency('fmt'))
+
+protoc = find_program('protoc', native: true)
+
+proto = custom_target(
+  'event_message_proto',
+  command: [
+    find_program('protoc', native: true),
+    '--proto_path=@CURRENT_SOURCE_DIR@',
+    '--cpp_out=@OUTDIR@',
+    '@INPUT@'
+  ],
+  output: [
+    'event_message.pb.cc',
+    'event_message.pb.h',
+  ],
+  input: 'event_message.proto')
+
+executable(
+  'nemora-postd',
+  'nemorad.cpp',
+  'src/host_manager.cpp',
+  'src/nemora.cpp',
+  'src/socket_manager.cpp',
+  'src/serializer.cpp',
+  proto,
+  include_directories: include_directories('.'),
+  dependencies: [protobuf, phosphor_dbus_interfaces, sdbusplus, threads, logging, fmt],
+  install: true
+)
+
+
diff --git a/subprojects/nemora-postd/nemorad.cpp b/subprojects/nemora-postd/nemorad.cpp
new file mode 100644
index 0000000..5df3b48
--- /dev/null
+++ b/subprojects/nemora-postd/nemorad.cpp
@@ -0,0 +1,119 @@
+// Copyright 2021 Google LLC
+//
+// 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 "src/default_addresses.h"
+
+#include "src/host_manager.hpp"
+#include "src/nemora.hpp"
+
+#include <arpa/inet.h>
+#include <fmt/format.h>
+
+#include <CLI/CLI.hpp>
+#include <phosphor-logging/log.hpp>
+
+#include <csignal>
+#include <cstdint>
+#include <iostream>
+#include <regex>
+#include <string>
+#include <thread>
+#include <unordered_map>
+#include <vector>
+
+using fmt::format;
+using phosphor::logging::level;
+using phosphor::logging::log;
+
+namespace
+{
+volatile std::sig_atomic_t gSignalStatus;
+}
+
+void signal_handler(int signal)
+{
+    gSignalStatus = signal;
+}
+
+void NemoraUdpPoll(Nemora* nemora)
+{
+    while (!gSignalStatus)
+    {
+        nemora->UdpPoll();
+    }
+}
+
+int main(int argc, char* argv[])
+{
+    // Init arg parser
+    CLI::App app("gBMC-side Nemora implementation (POST-code only)");
+
+    std::string udp_address_v4_str;
+    auto* ipv4_option = app.add_option(
+        "--udp4", udp_address_v4_str,
+        "Target IPv4 address for UDP communication, i.e., POST streaming.",
+        true);
+
+    std::string udp_address_v6_str;
+    auto* ipv6_option = app.add_option(
+        "--udp6", udp_address_v6_str,
+        "Target IPv6 address for UDP communication, i.e., POST streaming.",
+        true);
+
+    // interface is last, and required.
+    std::string iface_name;
+    app.add_option("interface", iface_name,
+                   "Network interface for TCP communication. Ex: eth0")
+        ->required();
+
+    CLI11_PARSE(app, argc, argv);
+
+    in_addr udp_address_v4;
+    udp_address_v4.s_addr = htonl(DEFAULT_ADDRESSES_TARGET_IP);
+    if (*ipv4_option &&
+        !inet_pton(AF_INET, udp_address_v4_str.c_str(), &udp_address_v4))
+    {
+        std::cerr << "Invalid IPv4 address supplied: " << udp_address_v4_str
+                  << std::endl;
+    }
+
+    // The value from default_addresses.h is designed for LWIP which EC uses,
+    // and needs to be translated to network byte order.
+    in6_addr udp_address_v6;
+    uint32_t default_addr_6[4] = DEFAULT_ADDRESSES_TARGET_IP6;
+
+    auto htonl_inplace = [](uint32_t& i) { i = htonl(i); };
+    std::for_each(std::begin(default_addr_6), std::end(default_addr_6),
+                  htonl_inplace);
+    std::memcpy(udp_address_v6.s6_addr, default_addr_6, sizeof(default_addr_6));
+
+    if (*ipv6_option &&
+        !inet_pton(AF_INET6, udp_address_v6_str.c_str(), &udp_address_v6))
+    {
+        std::cerr << "Invalid IPv6 address supplied: " << udp_address_v6_str
+                  << std::endl;
+    }
+
+    log<level::INFO>("Start Nemora...");
+    Nemora nemora(iface_name, udp_address_v4, udp_address_v6);
+
+    // Install a signal handler
+    std::signal(SIGINT, signal_handler);
+
+    std::thread udp(NemoraUdpPoll, &nemora);
+
+    udp.join();
+
+    return EXIT_SUCCESS;
+}
diff --git a/subprojects/nemora-postd/src/default_addresses.h b/subprojects/nemora-postd/src/default_addresses.h
new file mode 100644
index 0000000..2e1321e
--- /dev/null
+++ b/subprojects/nemora-postd/src/default_addresses.h
@@ -0,0 +1,88 @@
+/*
+ * Copyright 2021 Google LLC
+ *
+ * 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.
+ */
+
+#ifndef PLATFORMS_NEMORA_PORTABLE_DEFAULT_ADDRESSES_H_
+#define PLATFORMS_NEMORA_PORTABLE_DEFAULT_ADDRESSES_H_
+//
+// Nemora dedicated port. Filtered by NIC.
+#define DEFAULT_ADDRESSES_RX_PORT 3959
+
+// NOTE: All the IPv4 addresses used in this file will be represented in the
+//       CPU order and therefore must *not* be used to initialize LWIP
+//       ip_addr types, unless the HTONL macro is used.
+//
+// Example: Given Nemora UDP collector VIP 172.20.0.197, the
+//   DEFAULT_ADDRESSES_TARGET_IP macro expands to the 32-bit number 0xAC1408C5
+//  (to help the reader: 172 is 0xAC), but with our little endian CPU that
+//  32-bit number is represented in memory as:
+//  0xC5 @ offset 0, 0x08 @ offset 1, 0x14 @ offset 2, 0xAC @ offset 3
+//  Since LWIP uses network order, a correct initialization requires:
+//  ip_addr collector = { .addr = HTONL(DEFAULT_ADDRESSES_TARGET_IP) };
+//
+#ifdef USE_LAB_UDP_DEST
+// Currently targets the lab installer fdcorp1.mtv
+#define DEFAULT_ADDRESSES_TARGET_IP ((172 << 24) | (18 << 16) | (107 << 8) | 1)
+#define DEFAULT_ADDRESSES_TARGET_PORT 50201
+#else
+// DEFAULT : Point to production Nemora collector (via anycast VIP).
+#define DEFAULT_ADDRESSES_TARGET_IP ((172 << 24) | (20 << 16) | (0 << 8) | 197)
+#define DEFAULT_ADDRESSES_TARGET_PORT 3960
+#endif
+
+// 2001:4860:f802::c5
+#define DEFAULT_ADDRESSES_TARGET_IP6                                           \
+    {                                                                          \
+        0x20014860, 0xf8020000, 0, 0xc5                                        \
+    }
+
+#ifdef NETWORK_UNITTEST
+#define DEFAULT_ADDRESSES_GATEWAY ((172 << 24) | (23 << 16) | (130 << 8) | 190)
+#define DEFAULT_ADDRESSES_NETMASK ((255 << 24) | (255 << 16) | (255 << 8) | 192)
+#define DEFAULT_ADDRESSES_LOCAL_IP ((172 << 24) | (23 << 16) | (130 << 8) | 141)
+#define DEFAULT_ADDRESSES_MAC                                                  \
+    {                                                                          \
+        0x00, 0x1a, 0x11, 0x30, 0xc9, 0x6f                                     \
+    }
+#define DEFAULT_ADDRESSES_GATEWAY6                                             \
+    {                                                                          \
+        0, 0, 0, 0                                                             \
+    }
+#define DEFAULT_ADDRESSES_GATEWAY6_MAC                                         \
+    {                                                                          \
+        0, 0, 0, 0, 0, 0                                                       \
+    }
+#else
+#define DEFAULT_ADDRESSES_GATEWAY 0
+#define DEFAULT_ADDRESSES_NETMASK 0
+#define DEFAULT_ADDRESSES_LOCAL_IP 0
+#define DEFAULT_ADDRESSES_MAC                                                  \
+    {                                                                          \
+        0, 0, 0, 0, 0, 0                                                       \
+    }
+// fe80::1 -- as of 2016-10-13 this is guaranteed to be the GW in prod.
+#define DEFAULT_ADDRESSES_GATEWAY6                                             \
+    {                                                                          \
+        0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1                   \
+    }
+// 02:32:00:00:00:00 -- as of 2016-10-13 this is guaranteed to be the
+// GW MAC addr in prod.
+#define DEFAULT_ADDRESSES_GATEWAY6_MAC                                         \
+    {                                                                          \
+        0x02, 0x32, 0, 0, 0, 0                                                 \
+    }
+#endif
+
+#endif // PLATFORMS_NEMORA_PORTABLE_DEFAULT_ADDRESSES_H_
diff --git a/subprojects/nemora-postd/src/host_manager.cpp b/subprojects/nemora-postd/src/host_manager.cpp
new file mode 100644
index 0000000..b27ac21
--- /dev/null
+++ b/subprojects/nemora-postd/src/host_manager.cpp
@@ -0,0 +1,111 @@
+// Copyright 2021 Google LLC
+//
+// 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 "host_manager.hpp"
+
+#include <fmt/format.h>
+
+#include <phosphor-logging/log.hpp>
+#include <sdbusplus/bus.hpp>
+#include <sdbusplus/message.hpp>
+
+#include <functional>
+#include <iostream>
+#include <variant>
+
+using fmt::format;
+using phosphor::logging::level;
+using phosphor::logging::log;
+
+HostManager::HostManager() :
+    postcodes_(), bus_(sdbusplus::bus::new_default()),
+    signal_(bus_, HostManager::GetMatch().c_str(),
+            [this](auto& m) -> void { this->DbusHandleSignal(m); }),
+    post_poller_enabled_(true)
+{
+    // Spin off thread to listen on bus_
+    auto post_poller_thread = std::mem_fn(&HostManager::PostPollerThread);
+    post_poller_ = std::make_unique<std::thread>(post_poller_thread, this);
+}
+
+int HostManager::DbusHandleSignal(sdbusplus::message::message& msg)
+{
+    log<level::INFO>("Property Changed!");
+    std::string msgSensor, busName{POSTCODE_BUSNAME};
+    std::map<std::string,
+             std::variant<std::tuple<uint64_t, std::vector<uint8_t>>>>
+        msgData;
+    msg.read(msgSensor, msgData);
+
+    if (msgSensor == busName)
+    {
+        auto valPropMap = msgData.find("Value");
+        if (valPropMap != msgData.end())
+        {
+            uint64_t rawValue =
+                std::get<uint64_t>(std::get<0>(valPropMap->second));
+
+            PushPostcode(rawValue);
+        }
+    }
+
+    return 0;
+}
+
+void HostManager::PushPostcode(uint64_t postcode)
+{
+    // Get lock
+    std::lock_guard<std::mutex> lock(postcodes_lock_);
+    // Add postcode to queue
+    postcodes_.push_back(postcode);
+}
+
+std::vector<uint64_t> HostManager::DrainPostcodes()
+{
+    // Get lock
+    std::lock_guard<std::mutex> lock(postcodes_lock_);
+
+    auto count = postcodes_.size();
+    if (count > 0)
+    {
+        std::string msg = format("Draining Postcodes. Count: {}.", count);
+        log<level::ERR>(msg.c_str());
+    }
+
+    // Drain the queue into a list
+    // TODO: maximum # postcodes?
+    std::vector<uint64_t> result(postcodes_);
+    postcodes_.clear();
+
+    return result;
+}
+
+std::string HostManager::GetMatch()
+{
+    std::string obj{POSTCODE_OBJECTPATH};
+    return std::string("type='signal',"
+                       "interface='org.freedesktop.DBus.Properties',"
+                       "member='PropertiesChanged',"
+                       "path='" +
+                       obj + "'");
+}
+
+void HostManager::PostPollerThread()
+{
+    while (post_poller_enabled_)
+    {
+        bus_.process_discard();
+        bus_.wait();
+    }
+}
diff --git a/subprojects/nemora-postd/src/host_manager.hpp b/subprojects/nemora-postd/src/host_manager.hpp
new file mode 100644
index 0000000..6ed2a33
--- /dev/null
+++ b/subprojects/nemora-postd/src/host_manager.hpp
@@ -0,0 +1,78 @@
+// Copyright 2021 Google LLC
+//
+// 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.
+
+#pragma once
+#include "nemora_types.hpp"
+
+#include <sdbusplus/bus.hpp>
+#include <sdbusplus/message.hpp>
+#include <sdbusplus/server.hpp>
+
+#include <mutex>
+#include <queue>
+#include <string>
+#include <unordered_map>
+#include <vector>
+
+#define POSTCODE_OBJECTPATH "/xyz/openbmc_project/state/boot/raw0"
+#define POSTCODE_BUSNAME "xyz.openbmc_project.State.Boot.Raw"
+
+class HostManager
+{
+  public:
+    HostManager();
+    ~HostManager() = default;
+
+    /**
+     * Callback for POST code DBus listener
+     * - msg: out-parameter for message received over DBus from callback
+     *
+     * - returns: error code or 0 for success
+     */
+    int DbusHandleSignal(sdbusplus::message::message& msg);
+
+    /**
+     * Helper to construct match string for callback registration for POST
+     * listener
+     * - returns: match string for use in registering callback
+     */
+    static std::string GetMatch();
+
+    /**
+     * Copies contents of POSTcode vector away to allow for sending via UDP
+     * - returns: vector filled with current state of postcodes_
+     */
+    std::vector<uint64_t> DrainPostcodes();
+
+    /**
+     * Add POST code to vector, thread-safely
+     * - postcode: POST code to add, typically only 8 or 16 bits wide
+     */
+    void PushPostcode(uint64_t postcode);
+
+  private:
+    /**
+     * Business logic of thread listening to DBus for POST codes
+     */
+    void PostPollerThread();
+
+    // It's important that postcodes_ be initialized before post_poller_!
+    std::vector<uint64_t> postcodes_;
+    std::mutex postcodes_lock_;
+
+    sdbusplus::bus::bus bus_;
+    sdbusplus::server::match::match signal_;
+    std::unique_ptr<std::thread> post_poller_;
+    bool post_poller_enabled_;
+};
diff --git a/subprojects/nemora-postd/src/nemora.cpp b/subprojects/nemora-postd/src/nemora.cpp
new file mode 100644
index 0000000..c5cff87
--- /dev/null
+++ b/subprojects/nemora-postd/src/nemora.cpp
@@ -0,0 +1,149 @@
+// Copyright 2021 Google LLC
+//
+// 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 "nemora.hpp"
+
+#include "default_addresses.h"
+
+#include <netinet/in.h>
+#include <time.h>
+#include <unistd.h>
+
+#include <phosphor-logging/log.hpp>
+#include <sdbusplus/bus.hpp>
+#include <sdbusplus/exception.hpp>
+
+#include <cstdint>
+#include <cstring>
+#include <functional>
+#include <iostream>
+#include <variant>
+
+using phosphor::logging::level;
+using phosphor::logging::log;
+using sdbusplus::exception::SdBusError;
+
+constexpr auto MAC_FORMAT = "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx";
+constexpr auto MAC_INTERFACE = "xyz.openbmc_project.Network.MACAddress";
+constexpr auto NETWORK_INTERFACE = "xyz.openbmc_project.Network";
+constexpr auto PROP_INTERFACE = "org.freedesktop.DBus.Properties";
+constexpr auto IFACE_ROOT = "/xyz/openbmc_project/network/";
+
+bool Nemora::ParseMac(const std::string& mac_addr, MacAddr* mac)
+{
+    int ret =
+        sscanf(mac_addr.c_str(), MAC_FORMAT, mac->octet, mac->octet + 1,
+               mac->octet + 2, mac->octet + 3, mac->octet + 4, mac->octet + 5);
+    return (ret == MAC_ADDR_SIZE);
+}
+
+bool Nemora::GetMacAddr(MacAddr* mac, const std::string& iface_path)
+{
+    if (mac == nullptr)
+    {
+        log<level::ERR>("Nemora::GetMacAddr MAC Address is nullptr");
+        return false;
+    }
+    auto dbus = sdbusplus::bus::new_default();
+    sdbusplus::message::message reply;
+
+    try
+    {
+        auto networkd_call = dbus.new_method_call(
+            NETWORK_INTERFACE, iface_path.c_str(), PROP_INTERFACE, "Get");
+        networkd_call.append(MAC_INTERFACE, "MACAddress");
+
+        reply = dbus.call(networkd_call);
+    }
+    catch (const SdBusError& e)
+    {
+        log<level::ERR>(
+            "Nemora::GetMacAddr failed to call Network D-Bus interface");
+        return false;
+    }
+
+    std::variant<std::string> result;
+    reply.read(result);
+    auto mac_addr = std::get<std::string>(result);
+    if (!ParseMac(mac_addr, mac))
+    {
+        log<level::ERR>("Nemora::GetMacAddr Failed to parse MAC Address");
+        return false;
+    }
+    return true;
+}
+
+void Nemora::InitEventData()
+{
+    event_data_.type = NemoraDatagramType::NemoraEvent;
+
+    // UDP IPv4 addr for POST
+    event_data_.destination.sin_family = AF_INET;
+    event_data_.destination.sin_port = htons(DEFAULT_ADDRESSES_TARGET_PORT);
+
+    // UDP IPv6 addr for POST
+    event_data_.destination6.sin6_family = AF_INET6;
+    event_data_.destination6.sin6_port = htons(DEFAULT_ADDRESSES_TARGET_PORT);
+}
+
+Nemora::Nemora()
+{
+    InitEventData();
+}
+
+Nemora::Nemora(const std::string& iface_name, const in_addr ipv4,
+               const in6_addr ipv6) :
+    socketManager_(),
+    hostManager_(), iface_path_{std::string(IFACE_ROOT) + iface_name}
+{
+    InitEventData();
+    event_data_.destination.sin_addr = ipv4;
+    event_data_.destination6.sin6_addr = ipv6;
+}
+
+void Nemora::UdpPoll()
+{
+    auto postcodes = hostManager_.DrainPostcodes();
+
+    // Don't bother updating if there is no POST code
+    // EC supports a flag EC_NEMORA_UDP_CONFIG_MASK_PERIODIC to send
+    // periodic updates, which is non-POR for gBMC for now.
+    bool shouldBroadcast = !postcodes.empty();
+
+    UpdateEventData(std::move(postcodes));
+
+    log<level::INFO>("UpdateEventData gets called.");
+
+    if (shouldBroadcast)
+    {
+        log<level::INFO>("Should broadcast");
+        std::lock_guard<std::mutex> lock(event_data_mutex_);
+        socketManager_.SendDatagram(static_cast<NemoraDatagram*>(&event_data_));
+    }
+
+    sleep(20);
+}
+
+void Nemora::UpdateEventData(std::vector<uint64_t>&& postcodes)
+{
+    MacAddr mac;
+    GetMacAddr(&mac, iface_path_);
+
+    std::lock_guard<std::mutex> lock(event_data_mutex_);
+
+    memcpy(event_data_.mac, mac.octet, sizeof(MacAddr));
+
+    event_data_.postcodes = std::move(postcodes);
+    event_data_.sent_time_s = time(0);
+}
diff --git a/subprojects/nemora-postd/src/nemora.hpp b/subprojects/nemora-postd/src/nemora.hpp
new file mode 100644
index 0000000..fe5799e
--- /dev/null
+++ b/subprojects/nemora-postd/src/nemora.hpp
@@ -0,0 +1,92 @@
+// Copyright 2021 Google LLC
+//
+// 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.
+
+#pragma once
+
+#include "host_manager.hpp"
+#include "socket_manager.hpp"
+
+#include <cstdint>
+#include <mutex>
+#include <string>
+#include <thread>
+
+class Nemora
+{
+  public:
+    /**
+     * Constructs a Nemora object.
+     * - iface_name: The networking interface to use (eg. eth0)
+     * - ipv4: Target IPv4 address for UDP communication, i.e., POST streaming.
+     * - ipv6: Target IPv6 address for UDP communication, i.e., POST streaming.
+     */
+    Nemora(const std::string& iface_name, const in_addr ipv4,
+           const in6_addr ipv6);
+
+    /**
+     * Construct uninitialized Nemora object
+     */
+    Nemora();
+
+    /**
+     * Cancels polling threads and destructs Nemora object.
+     */
+    ~Nemora() = default;
+
+    /**
+     * Loops collecting the current state of event_data_ and sending via UDP.
+     */
+    void UdpPoll();
+
+  private:
+    /**
+     * Initialize event_data_ with default values.
+     * This is used by constructors.
+     */
+    void InitEventData();
+
+    /**
+     * Fetches MAC from host
+     * - mac: out-parameter for host mac address
+     * - iface_path: DBus path to network interface, typically
+     *   IFACE_ROOT + iface_path_.
+     *
+     * - returns: true if address was populated correctly, false if error
+     */
+    bool GetMacAddr(MacAddr* mac, const std::string& iface_path);
+
+    /**
+     * Converts from string to struct
+     * - mac_addr: string of format MAC_FORMAT
+     * - mac: out-parameter with MAC from mac_addr populated. must be allocated
+     * by caller
+     *
+     * - returns: true if mac_addr was correct format, false otherwise
+     */
+    bool ParseMac(const std::string& mac_addr, MacAddr* mac);
+
+    /**
+     * Update event_data_ from host.
+     * - postcodes: list of postcodes polled.
+     *   Forced to bind to temporary to avoid copying.
+     */
+    void UpdateEventData(std::vector<uint64_t>&& postcodes);
+
+    NemoraEvent event_data_ = {};
+    std::mutex event_data_mutex_;
+
+    SocketManager socketManager_;
+    HostManager hostManager_;
+    const std::string iface_path_;
+};
diff --git a/subprojects/nemora-postd/src/nemora_types.hpp b/subprojects/nemora-postd/src/nemora_types.hpp
new file mode 100644
index 0000000..6dde67d
--- /dev/null
+++ b/subprojects/nemora-postd/src/nemora_types.hpp
@@ -0,0 +1,59 @@
+// Copyright 2021 Google LLC
+//
+// 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.
+
+#pragma once
+
+#include <arpa/inet.h>
+#include <sys/socket.h>
+
+#include <cstdint>
+#include <cstdio>
+#include <cstring>
+#include <memory>
+#include <vector>
+
+#define MAC_ADDR_SIZE 6
+
+struct MacAddr
+{
+    std::uint8_t octet[MAC_ADDR_SIZE]; // network order
+};
+
+enum class NemoraDatagramType
+{
+    NemoraEvent,
+};
+
+/**
+ * Encompasses all valid outbound UDP messages
+ */
+struct NemoraDatagram
+{
+    // destination
+    sockaddr_in destination;
+    sockaddr_in6 destination6;
+    // type
+    NemoraDatagramType type;
+    std::vector<uint8_t> payload;
+};
+
+/**
+ * Event information as broadcast to System Health Data Collector
+ */
+struct NemoraEvent : NemoraDatagram
+{
+    std::uint8_t mac[MAC_ADDR_SIZE];
+    std::uint64_t sent_time_s;
+    std::vector<uint64_t> postcodes;
+};
diff --git a/subprojects/nemora-postd/src/serializer.cpp b/subprojects/nemora-postd/src/serializer.cpp
new file mode 100644
index 0000000..52008b6
--- /dev/null
+++ b/subprojects/nemora-postd/src/serializer.cpp
@@ -0,0 +1,69 @@
+// Copyright 2021 Google LLC
+//
+// 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 "serializer.hpp"
+
+#include "event_message.pb.h"
+
+#include <fmt/format.h>
+
+#include <phosphor-logging/log.hpp>
+
+using fmt::format;
+using phosphor::logging::level;
+using phosphor::logging::log;
+
+std::string Serializer::Serialize(const NemoraDatagram* dgram)
+{
+    std::string result;
+    switch (dgram->type)
+    {
+        case NemoraDatagramType::NemoraEvent:
+            result = SerializeEvent(static_cast<const NemoraEvent*>(dgram));
+            break;
+        default:
+            log<level::ERR>(
+                format("Type with ID {} not supported by "
+                       "Serializer::Serialize(const NemoraDatagram*)",
+                       static_cast<int>(dgram->type))
+                    .c_str());
+    }
+
+    return result;
+}
+
+std::string Serializer::SerializeEvent(const NemoraEvent* event)
+{
+    std::string result;
+    platforms::nemora::proto::EventSeries pb;
+
+    pb.set_magic(NEMORA_EVENT_PB_MAGIC);
+
+    const char* p_arr = reinterpret_cast<const char*>(event->mac);
+    pb.set_mac(p_arr, MAC_ADDR_SIZE);
+
+    pb.set_sent_time_us(event->sent_time_s * 1000000);
+
+    for (auto postcode : event->postcodes)
+    {
+        pb.add_postcodes(postcode);
+    }
+
+    pb.set_postcodes_protocol(
+        platforms::nemora::proto::EventSeries::NATIVE_32_BIT);
+
+    log<level::INFO>(format("NemoraEvent {}", pb.DebugString()).c_str());
+    pb.SerializeToString(&result);
+    return result;
+}
diff --git a/subprojects/nemora-postd/src/serializer.hpp b/subprojects/nemora-postd/src/serializer.hpp
new file mode 100644
index 0000000..2dedc85
--- /dev/null
+++ b/subprojects/nemora-postd/src/serializer.hpp
@@ -0,0 +1,38 @@
+// Copyright 2021 Google LLC
+//
+// 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.
+
+#pragma once
+#include "nemora_types.hpp"
+
+#include <arpa/inet.h>
+
+#include <cstddef>
+#include <cstdint>
+#include <memory>
+#include <string>
+
+using std::int32_t;
+using std::size_t;
+using std::uint64_t;
+
+class Serializer
+{
+  public:
+    static std::string Serialize(const NemoraDatagram* dgram);
+
+  private:
+    static std::string SerializeEvent(const NemoraEvent* event);
+
+    static constexpr uint64_t NEMORA_EVENT_PB_MAGIC = 0x890ebd38ec325800;
+};
diff --git a/subprojects/nemora-postd/src/socket_manager.cpp b/subprojects/nemora-postd/src/socket_manager.cpp
new file mode 100644
index 0000000..13f3269
--- /dev/null
+++ b/subprojects/nemora-postd/src/socket_manager.cpp
@@ -0,0 +1,97 @@
+// Copyright 2021 Google LLC
+//
+// 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 "socket_manager.hpp"
+
+#include "serializer.hpp"
+
+#include <errno.h>
+#include <fmt/format.h>
+#include <sys/socket.h>
+#include <unistd.h>
+
+#include <phosphor-logging/log.hpp>
+
+#include <cstring>
+
+using fmt::format;
+using phosphor::logging::level;
+using phosphor::logging::log;
+
+SocketManager::~SocketManager()
+{
+    std::lock_guard<std::mutex> lock(open_sockets_lock_);
+    for (auto fd : open_sockets_)
+    {
+        close(fd);
+    }
+}
+
+void SocketManager::SendDatagram(const NemoraDatagram* bcast)
+{
+    std::string serialized = Serializer::Serialize(bcast);
+
+    // Create socket
+    auto fd = socket(AF_INET6, SOCK_DGRAM, 0);
+    if (fd < 0)
+    {
+        log<level::ERR>("SocketManager::SendDatagram: Couldn't open socket");
+    }
+    TrackSocket(fd);
+
+    // Because we aren't sure whether the v6 or v4 target IP will be present,
+    // we send UDP packets to both. This puts us at feature parity with EC.
+
+    // Send serialized data (v6)
+    auto addr6 = reinterpret_cast<const sockaddr*>(&bcast->destination6);
+    auto err = sendto(fd, serialized.c_str(), serialized.length(), 0, addr6,
+                      sizeof(bcast->destination6));
+    if (err < 0)
+    {
+        log<level::ERR>(format("SocketManager::SendDatagram: Couldn't sendto "
+                               "socket (IPv6): {}",
+                               std::strerror(errno))
+                            .c_str());
+    }
+
+    // Send serialized data (v4)
+    auto addr4 = reinterpret_cast<const sockaddr*>(&bcast->destination);
+    err = sendto(fd, serialized.c_str(), serialized.length(), 0, addr4,
+                 sizeof(bcast->destination));
+    if (err < 0)
+    {
+        log<level::ERR>(format("SocketManager::SendDatagram: Couldn't sendto "
+                               "socket (IPv4): {}",
+                               std::strerror(errno))
+                            .c_str());
+    }
+
+    CloseSocketSafely(fd);
+}
+
+void SocketManager::CloseSocketSafely(int fd)
+{
+    std::lock_guard<std::mutex> lock(open_sockets_lock_);
+    if (open_sockets_.find(fd) != open_sockets_.end())
+    {
+        close(fd);
+        open_sockets_.erase(fd);
+    }
+}
+
+void SocketManager::TrackSocket(int fd)
+{
+    std::lock_guard<std::mutex> lock(open_sockets_lock_);
+    open_sockets_.insert(fd);
+}
diff --git a/subprojects/nemora-postd/src/socket_manager.hpp b/subprojects/nemora-postd/src/socket_manager.hpp
new file mode 100644
index 0000000..6435cf9
--- /dev/null
+++ b/subprojects/nemora-postd/src/socket_manager.hpp
@@ -0,0 +1,49 @@
+// Copyright 2021 Google LLC
+//
+// 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.
+
+#pragma once
+#include "nemora_types.hpp"
+#include "serializer.hpp"
+
+#include <memory>
+#include <mutex>
+#include <string>
+#include <unordered_set>
+
+class SocketManager
+{
+  public:
+    SocketManager() = default;
+    ~SocketManager();
+
+    /**
+     * Sends a UDP packet to the address named in bcast object.
+     */
+    void SendDatagram(const NemoraDatagram* bcast);
+
+    /**
+     * Checks content of open_sockets_ and closes the socket if it is contained
+     * in the list. Closing a socket which is already closed causes problems.
+     */
+    void CloseSocketSafely(int fd);
+
+  private:
+    /**
+     * Adds a socket fd to open_sockets_ to allow tracking of which sockets are
+     * open or not.  Closing a socket which is already closed causes problems.
+     */
+    void TrackSocket(int fd);
+    std::unordered_set<int> open_sockets_;
+    std::mutex open_sockets_lock_;
+};
diff --git a/subprojects/nemora-postd/subprojects b/subprojects/nemora-postd/subprojects
new file mode 120000
index 0000000..a96aa0e
--- /dev/null
+++ b/subprojects/nemora-postd/subprojects
@@ -0,0 +1 @@
+..
\ No newline at end of file