build: Convert project to meson due to ipmid dependencies

Cleanedup all build warnings and updated the header to point to the
latest ipmid.

Tested:
Build BMC image with witherspoon and was fine.

Change-Id: Ib00d9ef44b176aec5d2a92a24c6f26b7794051c8
Signed-off-by: Willy Tu <wltu@google.com>
diff --git a/Makefile.am b/Makefile.am
deleted file mode 100644
index 7365e0c..0000000
--- a/Makefile.am
+++ /dev/null
@@ -1,18 +0,0 @@
-# SPDX-License-Identifier: Apache-2.0
-# Copyright (C) 2018 IBM Corp.
-
-libhiomapdir = ${libdir}/ipmid-providers
-libhiomap_LTLIBRARIES = libhiomap.la
-
-libhiomap_la_SOURCES = hiomap.cpp
-
-libhiomap_la_LDFLAGS = $(SYSTEMD_LIBS)\
-                       $(SDBUSPLUS_LIBS) \
-                       $(PHOSPHOR_LOGGING_LIBS) \
-                       -version-info 0:0:0 -shared
-
-libhiomap_la_CXXFLAGS = $(SYSTEMD_CFLAGS) \
-                        $(SDBUSPLUS_CFLAGS) \
-                        $(PHOSPHOR_LOGGING_CFLAGS) \
-                        $(AM_CXXFLAGS) \
-                        -DBOOST_COROUTINES_NO_DEPRECATION_WARNING
diff --git a/bootstrap.sh b/bootstrap.sh
deleted file mode 100755
index d5b6ea4..0000000
--- a/bootstrap.sh
+++ /dev/null
@@ -1,24 +0,0 @@
-#!/bin/sh
-# SPDX-License-Identifier: Apache-2.0
-# Copyright (C) 2018 IBM Corp.
-
-set -e
-set -x
-
-AUTOCONF_FILES="Makefile.in aclocal.m4 ar-lib autom4te.cache compile \
-        config.guess config.h.in config.sub configure depcomp install-sh \
-        ltmain.sh missing *libtool test-driver"
-
-case $1 in
-    clean)
-        test -f Makefile && make maintainer-clean
-        for file in ${AUTOCONF_FILES}; do
-            find . -name "$file" -print0 | xargs -0 -r rm -rf
-        done
-        exit 0
-        ;;
-esac
-
-autoreconf -vfi
-# shellcheck disable=SC2016
-echo 'Run "./configure ${CONFIGURE_FLAGS} && make"'
diff --git a/configure.ac b/configure.ac
deleted file mode 100644
index 2ba83c1..0000000
--- a/configure.ac
+++ /dev/null
@@ -1,64 +0,0 @@
-# SPDX-License-Identifier: Apache-2.0
-# Copyright (C) 2018 IBM Corp.
-
-# Initialization
-AC_PREREQ([2.69])
-AC_INIT([openpower-host-ipmi-flash], [1.0], [])
-AC_CONFIG_HEADERS([config.h])
-AM_INIT_AUTOMAKE([subdir-objects -Wall -Werror foreign dist-xz])
-AM_SILENT_RULES([yes])
-AC_LANG([C++])
-
-# Checks for programs.
-AC_PROG_CXX
-AM_PROG_AR
-AC_PROG_INSTALL
-AC_PROG_MAKE_SET
-
-# Checks for libraries.
-PKG_CHECK_MODULES([SYSTEMD], [libsystemd >= 221])
-
-# Checks for header files.
-AC_CHECK_HEADER(systemd/sd-bus.h, ,[AC_MSG_ERROR([Could not find systemd/sd-bus.h...systemd development package required])])
-AC_CHECK_HEADER(host-ipmid/ipmid-api.h, ,[AC_MSG_ERROR([Could not find host-ipmid/ipmid-api.h...host-ipmid package required])])
-PKG_CHECK_MODULES([SDBUSPLUS], [sdbusplus])
-PKG_CHECK_MODULES([PHOSPHOR_LOGGING], [phosphor-logging])
-
-# Checks for typedefs, structures, and compiler characteristics.
-AX_CXX_COMPILE_STDCXX([20],[noext])
-AX_APPEND_COMPILE_FLAGS([-Wall -Werror], [CXXFLAGS])
-
-# Checks for library functions.
-LT_INIT([disable-static shared])
-
-# Check/set gtest specific functions.
-AX_PTHREAD([GTEST_CPPFLAGS="-DGTEST_HAS_PTHREAD=1"],[GTEST_CPPFLAGS="-GTEST_HAS_PTHREAD=0"])
-AC_SUBST(GTEST_CPPFLAGS)
-
-AC_ARG_ENABLE([oe-sdk],
-    AS_HELP_STRING([--enable-oe-sdk], [Link testcases absolutely against OE SDK so they can be ran within it.])
-)
-AC_ARG_VAR(OECORE_TARGET_SYSROOT,
-    [Path to the OE SDK SYSROOT])
-AS_IF([test "x$enable_oe_sdk" == "xyes"],
-    AS_IF([test "x$OECORE_TARGET_SYSROOT" == "x"],
-          AC_MSG_ERROR([OECORE_TARGET_SYSROOT must be set with --enable-oe-sdk])
-    )
-    AC_MSG_NOTICE([Enabling OE-SDK at $OECORE_TARGET_SYSROOT])
-    [
-        testcase_flags="-Wl,-rpath,\${OECORE_TARGET_SYSROOT}/lib"
-        testcase_flags="${testcase_flags} -Wl,-rpath,\${OECORE_TARGET_SYSROOT}/usr/lib"
-        testcase_flags="${testcase_flags} -Wl,-dynamic-linker,`find \${OECORE_TARGET_SYSROOT}/lib/ld-*.so | sort -r -n | head -n1`"
-    ]
-    AC_SUBST([OESDK_TESTCASE_FLAGS], [$testcase_flags])
-)
-
-# Service dbus object manager
-AC_ARG_VAR(HIOMAPD_OBJ_PATH, [The Control Host D-Bus Object Manager])
-AS_IF([test "x$HIOMAPD_OBJ_PATH" == "x"],
-      [CONTROL_HOST_OBJ_MGR="/xyz/openbmc_project/hiomapd"])
-AC_DEFINE_UNQUOTED([HIOMAPD_OBJ_PATH], ["$HIOMAPD_OBJ_PATH"], [The Control Host D-Bus Object Manager])
-
-# Create configured output.
-AC_CONFIG_FILES([Makefile])
-AC_OUTPUT
diff --git a/hiomap.cpp b/hiomap.cpp
index 2e31248..9253243 100644
--- a/hiomap.cpp
+++ b/hiomap.cpp
@@ -1,12 +1,10 @@
 // SPDX-License-Identifier: Apache-2.0
 // Copyright (C) 2018 IBM Corp.
 
-#include "config.h"
-
 #include "hiomap.hpp"
 
 #include <endian.h>
-#include <host-ipmid/ipmid-api.h>
+#include <ipmid/api.h>
 #include <signal.h>
 #include <string.h>
 #include <systemd/sd-bus.h>
@@ -16,9 +14,9 @@
 #include <cstring>
 #include <fstream>
 #include <functional>
-#include <host-ipmid/ipmid-host-cmd-utils.hpp>
-#include <host-ipmid/ipmid-host-cmd.hpp>
 #include <iostream>
+#include <ipmid-host/cmd-utils.hpp>
+#include <ipmid-host/cmd.hpp>
 #include <ipmid/api.hpp>
 #include <map>
 #include <phosphor-logging/log.hpp>
@@ -27,6 +25,7 @@
 #include <sdbusplus/exception.hpp>
 #include <string>
 #include <tuple>
+#include <unordered_map>
 #include <utility>
 
 /*
@@ -283,10 +282,9 @@
     std::memcpy(buf, &t, sizeof(t));
 }
 
-typedef ipmi_ret_t (*hiomap_command)(ipmi_request_t req, ipmi_response_t resp,
-                                     ipmi_data_len_t data_len,
-                                     ipmi_context_t context);
-
+using hiomap_command =
+    std::function<ipmi_ret_t(ipmi_request_t req, ipmi_response_t resp,
+                             ipmi_data_len_t data_len, ipmi_context_t context)>;
 struct errno_cc_entry
 {
     int err;
@@ -383,7 +381,8 @@
     return 0;
 }
 
-static int hiomap_protocol_reset_response(IpmiCmdData cmd, bool status)
+static int hiomap_protocol_reset_response([[maybe_unused]] IpmiCmdData cmd,
+                                          [[maybe_unused]] bool status)
 {
     // If this is running in signal context, ipmid will shutdown
     // the event queue as the last signal handler
@@ -432,7 +431,8 @@
     return match;
 }
 
-static ipmi_ret_t hiomap_reset(ipmi_request_t request, ipmi_response_t response,
+static ipmi_ret_t hiomap_reset([[maybe_unused]] ipmi_request_t request,
+                               [[maybe_unused]] ipmi_response_t response,
                                ipmi_data_len_t data_len, ipmi_context_t context)
 {
     struct hiomap* ctx = static_cast<struct hiomap*>(context);
@@ -496,7 +496,7 @@
     return IPMI_CC_OK;
 }
 
-static ipmi_ret_t hiomap_get_flash_info(ipmi_request_t request,
+static ipmi_ret_t hiomap_get_flash_info([[maybe_unused]] ipmi_request_t request,
                                         ipmi_response_t response,
                                         ipmi_data_len_t data_len,
                                         ipmi_context_t context)
@@ -589,7 +589,7 @@
 }
 
 static ipmi_ret_t hiomap_close_window(ipmi_request_t request,
-                                      ipmi_response_t response,
+                                      [[maybe_unused]] ipmi_response_t response,
                                       ipmi_data_len_t data_len,
                                       ipmi_context_t context)
 {
@@ -620,7 +620,7 @@
 }
 
 static ipmi_ret_t hiomap_mark_dirty(ipmi_request_t request,
-                                    ipmi_response_t response,
+                                    [[maybe_unused]] ipmi_response_t response,
                                     ipmi_data_len_t data_len,
                                     ipmi_context_t context)
 {
@@ -652,7 +652,8 @@
     return IPMI_CC_OK;
 }
 
-static ipmi_ret_t hiomap_flush(ipmi_request_t request, ipmi_response_t response,
+static ipmi_ret_t hiomap_flush([[maybe_unused]] ipmi_request_t request,
+                               [[maybe_unused]] ipmi_response_t response,
                                ipmi_data_len_t data_len, ipmi_context_t context)
 {
     struct hiomap* ctx = static_cast<struct hiomap*>(context);
@@ -675,7 +676,8 @@
     return IPMI_CC_OK;
 }
 
-static ipmi_ret_t hiomap_ack(ipmi_request_t request, ipmi_response_t response,
+static ipmi_ret_t hiomap_ack(ipmi_request_t request,
+                             [[maybe_unused]] ipmi_response_t response,
                              ipmi_data_len_t data_len, ipmi_context_t context)
 {
     struct hiomap* ctx = static_cast<struct hiomap*>(context);
@@ -705,7 +707,8 @@
     return IPMI_CC_OK;
 }
 
-static ipmi_ret_t hiomap_erase(ipmi_request_t request, ipmi_response_t response,
+static ipmi_ret_t hiomap_erase(ipmi_request_t request,
+                               [[maybe_unused]] ipmi_response_t response,
                                ipmi_data_len_t data_len, ipmi_context_t context)
 {
     struct hiomap* ctx = static_cast<struct hiomap*>(context);
@@ -747,25 +750,26 @@
 #define HIOMAP_C_ACK 9
 #define HIOMAP_C_ERASE 10
 
-static const hiomap_command hiomap_commands[] = {
-    [0] = NULL, /* Invalid command ID */
-    [HIOMAP_C_RESET] = hiomap_reset,
-    [HIOMAP_C_GET_INFO] = hiomap_get_info,
-    [HIOMAP_C_GET_FLASH_INFO] = hiomap_get_flash_info,
-    [HIOMAP_C_CREATE_READ_WINDOW] = hiomap_create_read_window,
-    [HIOMAP_C_CLOSE_WINDOW] = hiomap_close_window,
-    [HIOMAP_C_CREATE_WRITE_WINDOW] = hiomap_create_write_window,
-    [HIOMAP_C_MARK_DIRTY] = hiomap_mark_dirty,
-    [HIOMAP_C_FLUSH] = hiomap_flush,
-    [HIOMAP_C_ACK] = hiomap_ack,
-    [HIOMAP_C_ERASE] = hiomap_erase,
+static const std::unordered_map<uint8_t, hiomap_command> hiomap_commands = {
+    {0, nullptr}, /* Invalid command ID */
+    {HIOMAP_C_RESET, hiomap_reset},
+    {HIOMAP_C_GET_INFO, hiomap_get_info},
+    {HIOMAP_C_GET_FLASH_INFO, hiomap_get_flash_info},
+    {HIOMAP_C_CREATE_READ_WINDOW, hiomap_create_read_window},
+    {HIOMAP_C_CLOSE_WINDOW, hiomap_close_window},
+    {HIOMAP_C_CREATE_WRITE_WINDOW, hiomap_create_write_window},
+    {HIOMAP_C_MARK_DIRTY, hiomap_mark_dirty},
+    {HIOMAP_C_FLUSH, hiomap_flush},
+    {HIOMAP_C_ACK, hiomap_ack},
+    {HIOMAP_C_ERASE, hiomap_erase},
 };
 
 /* FIXME: Define this in the "right" place, wherever that is */
 /* FIXME: Double evaluation */
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
 
-static ipmi_ret_t hiomap_dispatch(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
+static ipmi_ret_t hiomap_dispatch([[maybe_unused]] ipmi_netfn_t netfn,
+                                  [[maybe_unused]] ipmi_cmd_t cmd,
                                   ipmi_request_t request,
                                   ipmi_response_t response,
                                   ipmi_data_len_t data_len,
@@ -783,7 +787,7 @@
     uint8_t* ipmi_resp = (uint8_t*)response;
     uint8_t hiomap_cmd = ipmi_req[0];
 
-    if (hiomap_cmd == 0 || hiomap_cmd > ARRAY_SIZE(hiomap_commands) - 1)
+    if (hiomap_cmd == 0 || hiomap_cmd > hiomap_commands.size() - 1)
     {
         *data_len = 0;
         return IPMI_CC_PARM_OUT_OF_RANGE;
@@ -804,8 +808,13 @@
     size_t flash_len = *data_len - 2;
     uint8_t* flash_resp = ipmi_resp + 2;
 
-    ipmi_ret_t cc =
-        hiomap_commands[hiomap_cmd](flash_req, flash_resp, &flash_len, context);
+    auto command = hiomap_commands.find(hiomap_cmd);
+    if (command == hiomap_commands.end())
+    {
+        *data_len = 0;
+        return IPMI_CC_INVALID;
+    }
+    ipmi_ret_t cc = command->second(flash_req, flash_resp, &flash_len, context);
     if (cc != IPMI_CC_OK)
     {
         *data_len = 0;
@@ -848,7 +857,7 @@
         new bus::match::match(std::move(hiomap_match_properties(ctx)));
 
     std::function<SignalResponse(int)> shutdownHandler =
-        [ctx](int signalNumber) {
+        [ctx]([[maybe_unused]] int signalNumber) {
             hiomap_protocol_reset(ctx);
             return sigtermResponse;
         };
diff --git a/meson.build b/meson.build
new file mode 100644
index 0000000..5106d21
--- /dev/null
+++ b/meson.build
@@ -0,0 +1,46 @@
+project(
+  'openpower-host-ipmi-flash',
+  'cpp',
+  version: '0.1',
+  meson_version: '>=0.57.0',
+  default_options: [
+    'werror=true',
+    'warning_level=3',
+    'cpp_std=c++20',
+    'tests=' + (meson.is_subproject() ? 'disabled' : 'auto'),
+  ])
+
+# Dependencies
+phosphor_logging_dep = dependency('phosphor-logging')
+systemd = dependency('systemd')
+sdbusplus_dep = dependency('sdbusplus', required : false, include_type: 'system')
+ipmid_dep = dependency('libipmid')
+
+# Project Arguments
+cpp = meson.get_compiler('cpp')
+add_project_arguments(
+  cpp.get_supported_arguments([
+    '-DBOOST_COROUTINES_NO_DEPRECATION_WARNING',
+  ]),
+  language : 'cpp')
+
+root_inc = include_directories('.')
+
+hiomapdir_pre = declare_dependency(
+  include_directories: root_inc,
+  dependencies: [
+    phosphor_logging_dep,
+    systemd,
+    sdbusplus_dep,
+    ipmid_dep,
+  ])
+
+library(
+  'hiomap',
+  'hiomap.cpp',
+  implicit_include_directories: false,
+  dependencies: hiomapdir_pre,
+  version: meson.project_version(),
+  override_options: ['b_lundef=false'],
+  install: true,
+  install_dir: get_option('libdir') / 'ipmid-providers')
diff --git a/meson_options.txt b/meson_options.txt
new file mode 100644
index 0000000..b975de8
--- /dev/null
+++ b/meson_options.txt
@@ -0,0 +1,6 @@
+option('tests', type: 'feature', description: 'Build tests')
+option(
+  'hiomapd-obj-path',
+  type: 'string',
+  value: '/xyz/openbmc_project/hiomapd',
+  description: 'The Control Host D-Bus Object Manager')
diff --git a/subprojects/phosphor-logging.wrap b/subprojects/phosphor-logging.wrap
new file mode 100644
index 0000000..6876a6e
--- /dev/null
+++ b/subprojects/phosphor-logging.wrap
@@ -0,0 +1,6 @@
+[wrap-git]
+url = https://github.com/openbmc/phosphor-logging
+revision = HEAD
+
+[provide]
+phosphor-logging = phosphor_logging_dep
diff --git a/subprojects/sdbusplus.wrap b/subprojects/sdbusplus.wrap
new file mode 100644
index 0000000..42cfcee
--- /dev/null
+++ b/subprojects/sdbusplus.wrap
@@ -0,0 +1,6 @@
+[wrap-git]
+url = https://github.com/openbmc/sdbusplus
+revision = HEAD
+
+[provide]
+sdbusplus = sdbusplus_dep