Use generated occ to sensor ID map

Change-Id: I948cc33ef05c2c49353277f4d5df958012a9801f
Signed-off-by: Vishwanatha Subbanna <vishwa@linux.vnet.ibm.com>
diff --git a/Makefile.am b/Makefile.am
index 361af89..548e7ff 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -19,10 +19,19 @@
 	occ_finder.cpp
 
 BUILT_SOURCES =  org/open_power/OCC/Device/error.hpp \
-                 org/open_power/OCC/Device/error.cpp
+                 org/open_power/OCC/Device/error.cpp \
+                 occ_sensor.hpp
 
 CLEANFILES = ${BUILT_SOURCES}
 
+REQ_MAKO_FILE ?= ${top_srcdir}/occ_sensor.mako.hpp
+REQ_PY_SCRIPT ?= ${top_srcdir}/sensor_gen.py
+
+EXTRA_DIST = $(REQ_MAKO_FILE) $(REQ_PY_SCRIPT)
+
+occ_sensor.hpp: ${REQ_PY_SCRIPT} ${REQ_MAKO_FILE}
+	$(AM_V_GEN) ${PYTHON} ${REQ_PY_SCRIPT} -i ${YAML_PATH} > $@
+
 openpower_occ_control_LDFLAGS = \
 	$(SDBUSPLUS_LIBS) \
 	$(PHOSPHOR_LOGGING_LIBS) \
diff --git a/configure.ac b/configure.ac
index 06497f1..3091e67 100644
--- a/configure.ac
+++ b/configure.ac
@@ -105,6 +105,11 @@
 AS_IF([test "x$CPU_SUBPATH" == "x"], [CPU_SUBPATH="/xyz/openbmc_project/inventory/system/chassis/motherboard"])
 AC_DEFINE_UNQUOTED([CPU_SUBPATH], ["$CPU_SUBPATH"], [The subpath containing CPU objects])
 
+# Handling configuration files
+AC_ARG_VAR(YAML_PATH, [The path to the yaml config files.])
+AS_IF([test "x$YAML_PATH" == "x"], [YAML_PATH="$srcdir/example"])
+AC_DEFINE_UNQUOTED([YAML_PATH], ["$YAML_PATH"], [The path to the yaml config files])
+
 AC_CONFIG_HEADERS([config.h])
 AC_CONFIG_FILES([Makefile test/Makefile])
 AC_OUTPUT
diff --git a/example/occ_sensor.yaml b/example/occ_sensor.yaml
new file mode 100644
index 0000000..748a804
--- /dev/null
+++ b/example/occ_sensor.yaml
@@ -0,0 +1,4 @@
+- Instance: 0
+  SensorID: 0x08
+- Instance: 1
+  SensorID: 0x09
diff --git a/occ_sensor.mako.hpp b/occ_sensor.mako.hpp
new file mode 100755
index 0000000..b5fac53
--- /dev/null
+++ b/occ_sensor.mako.hpp
@@ -0,0 +1,31 @@
+## This file is a template.  The comment below is emitted
+## into the rendered file; feel free to edit this file.
+// WARNING: Generated header. Do not edit!
+
+
+#pragma once
+
+#include <map>
+
+namespace open_power
+{
+namespace occ
+{
+
+using instanceID = int;
+using sensorID = uint8_t;
+const std::map<instanceID, sensorID> Status::sensorMap = {
+\
+% for occ in occDict:
+<%
+    instance = occ.get("Instance")
+    id = occ.get("SensorID")
+%>\
+\
+    { ${instance}, ${id} },\
+
+% endfor
+};
+
+} // namespace occ
+} // namespace open_power
diff --git a/occ_status.cpp b/occ_status.cpp
index 83825c5..08b1ef3 100644
--- a/occ_status.cpp
+++ b/occ_status.cpp
@@ -1,4 +1,5 @@
 #include "occ_status.hpp"
+#include "occ_sensor.hpp"
 namespace open_power
 {
 namespace occ
diff --git a/occ_status.hpp b/occ_status.hpp
index f426048..d30d642 100644
--- a/occ_status.hpp
+++ b/occ_status.hpp
@@ -13,6 +13,12 @@
 namespace Base = sdbusplus::org::open_power::OCC::server;
 using Interface = sdbusplus::server::object::object<Base::Status>;
 
+// OCC status instance. Ex. for "occ0", the instance is 0
+using instanceID = int;
+
+// IPMI sensor ID for a given OCC instance
+using sensorID = uint8_t;
+
 /** @class Status
  *  @brief Implementation of OCC Active Status
  */
@@ -35,8 +41,9 @@
         Status(sdbusplus::bus::bus& bus, EventPtr& event, const char* path)
             : Interface(bus, path),
               path(path),
+              instance(((this->path.back() - '0'))),
               device(event,
-                     name + std::to_string((this->path.back() - '0') + 1),
+                     name + std::to_string(instance + 1),
                      std::bind(&Status::deviceErrorHandler, this))
         {
             // Nothing to do here
@@ -63,6 +70,12 @@
         /** @brief occ name prefix */
         std::string name = OCC_NAME;
 
+        /** @brief OCC instance number. Ex, 0,1, etc */
+        int instance;
+
+        /** @brief OCC instance to Sensor ID mapping */
+        static const std::map<instanceID, sensorID> sensorMap;
+
         /** @brief OCC device object to do bind and unbind */
         Device device;
 
diff --git a/sensor_gen.py b/sensor_gen.py
new file mode 100755
index 0000000..bedf4f8
--- /dev/null
+++ b/sensor_gen.py
@@ -0,0 +1,43 @@
+#!/usr/bin/env python
+
+import os
+import yaml
+import argparse
+from mako.template import Template
+import contextlib
+
+if __name__ == '__main__':
+    script_dir = os.path.dirname(os.path.realpath(__file__))
+    parser = argparse.ArgumentParser()
+    parser.add_argument(
+        "-f", "--filename",
+        default='occ_sensor.yaml',
+        help="Input File Name")
+    parser.add_argument(
+        "-i", "--input-dir",
+        dest='inputdir',
+        default=script_dir,
+        help="Input directory")
+
+    args = parser.parse_args()
+
+    # Default to the one that is in the current.
+    yaml_dir = script_dir
+    yaml_file = os.path.join(yaml_dir, 'occ_sensor.yaml')
+
+    if args.inputdir:
+        yaml_dir = args.inputdir
+
+    if args.filename:
+        yaml_file = os.path.join(yaml_dir, args.filename)
+
+    with open(yaml_file, 'r') as fd:
+        ifile = yaml.safe_load(fd)
+
+        # Render the mako template
+        template = os.path.join(script_dir, 'occ_sensor.mako.hpp')
+        t = Template(filename=template)
+        with open('occ_sensor.hpp', 'w') as fd:
+            fd.write(
+                t.render(
+                    occDict=ifile))