sensor-cache: Use new get function

The get function for sensor-cache will use the sdbusplus message to get
the sensor data instead of making DBus calls, where the sdbusplus
message could be a signal callback, or the getAllProperty's reply message.

So the get function's prototype is changed.

Tested: Verify the build is OK when enable or disable the sensor-cache
        option.

Signed-off-by: Lei YU <yulei.sh@bytedance.com>
Change-Id: Ife2f55d87ce2b0941a4efdb599e46d80d827c2ff
diff --git a/include/ipmid/types.hpp b/include/ipmid/types.hpp
index 9c59ea0..3332f81 100644
--- a/include/ipmid/types.hpp
+++ b/include/ipmid/types.hpp
@@ -172,7 +172,13 @@
     SensorUnits1 sensorUnits1;
     Unit unit;
     std::function<uint8_t(SetSensorReadingReq&, const Info&)> updateFunc;
+#ifndef FEATURE_SENSORS_CACHE
     std::function<GetSensorResponse(const Info&)> getFunc;
+#else
+    std::function<std::optional<GetSensorResponse>(
+        uint8_t, const Info&, sdbusplus::message::message&)>
+        getFunc;
+#endif
     Mutability mutability;
     SensorName sensorName;
     std::function<SensorName(const Info&)> sensorNameFunc;
diff --git a/sensordatahandler.cpp b/sensordatahandler.cpp
index 4665f38..3f3b04b 100644
--- a/sensordatahandler.cpp
+++ b/sensordatahandler.cpp
@@ -158,6 +158,7 @@
     return response;
 }
 
+#ifndef FEATURE_SENSORS_CACHE
 GetSensorResponse assertion(const Info& sensorInfo)
 {
     return mapDbusToAssertion(sensorInfo, sensorInfo.sensorPath,
@@ -197,6 +198,22 @@
 
     return response;
 }
+#else
+std::optional<GetSensorResponse> assertion(uint8_t id, const Info& sensorInfo,
+                                           sdbusplus::message::message& msg)
+{
+    // TODO
+    return {};
+}
+
+std::optional<GetSensorResponse> eventdata2(uint8_t id, const Info& sensorInfo,
+                                            sdbusplus::message::message& msg)
+{
+    // TODO
+    return {};
+}
+
+#endif // FEATURE_SENSORS_CACHE
 
 } // namespace get
 
diff --git a/sensordatahandler.hpp b/sensordatahandler.hpp
index 4ab0e87..91654af 100644
--- a/sensordatahandler.hpp
+++ b/sensordatahandler.hpp
@@ -140,6 +140,7 @@
                                      const InstancePath& path,
                                      const DbusInterface& interface);
 
+#ifndef FEATURE_SENSORS_CACHE
 /**
  *  @brief Map the Dbus info to sensor's assertion status in the Get sensor
  *         reading command response.
@@ -331,6 +332,77 @@
     return response;
 }
 
+#else
+
+using SensorCacheMap =
+    std::map<uint8_t, std::optional<ipmi::sensor::GetSensorResponse>>;
+extern SensorCacheMap sensorCacheMap;
+
+/**
+ *  @brief Map the Dbus info to sensor's assertion status in the Get sensor
+ *         reading command response.
+ *
+ *  @param[in] id - The sensor id
+ *  @param[in] sensorInfo - Dbus info related to sensor.
+ *  @param[in] msg - Dbus message from match callback.
+ *
+ *  @return Response for get sensor reading command.
+ */
+std::optional<GetSensorResponse> assertion(uint8_t id, const Info& sensorInfo,
+                                           sdbusplus::message::message& msg);
+
+/**
+ *  @brief Maps the Dbus info to the reading field in the Get sensor reading
+ *         command response.
+ *
+ *  @param[in] id - The sensor id
+ *  @param[in] sensorInfo - Dbus info related to sensor.
+ *  @param[in] msg - Dbus message from match callback.
+ *
+ *  @return Response for get sensor reading command.
+ */
+std::optional<GetSensorResponse> eventdata2(uint8_t id, const Info& sensorInfo,
+                                            sdbusplus::message::message& msg);
+
+/**
+ *  @brief readingAssertion is a case where the entire assertion state field
+ *         serves as the sensor value.
+ *
+ *  @tparam T - type of the dbus property related to sensor.
+ *  @param[in] id - The sensor id
+ *  @param[in] sensorInfo - Dbus info related to sensor.
+ *  @param[in] msg - Dbus message from match callback.
+ *
+ *  @return Response for get sensor reading command.
+ */
+template <typename T>
+std::optional<GetSensorResponse>
+    readingAssertion(uint8_t id, const Info& sensorInfo,
+                     sdbusplus::message::message& msg)
+{
+    // TODO
+    return {};
+}
+
+/** @brief Get sensor reading from the dbus message from match
+ *
+ *  @tparam T - type of the dbus property related to sensor.
+ *  @param[in] id - The sensor id
+ *  @param[in] sensorInfo - Dbus info related to sensor.
+ *  @param[in] msg - Dbus message from match callback.
+ *
+ *  @return Response for get sensor reading command.
+ */
+template <typename T>
+std::optional<GetSensorResponse> readingData(uint8_t id, const Info& sensorInfo,
+                                             sdbusplus::message::message& msg)
+{
+    // TODO
+    return {};
+}
+
+#endif // FEATURE_SENSORS_CACHE
+
 } // namespace get
 
 namespace set
diff --git a/sensorhandler.cpp b/sensorhandler.cpp
index dc632d0..34565a1 100644
--- a/sensorhandler.cpp
+++ b/sensorhandler.cpp
@@ -92,6 +92,10 @@
 std::map<uint8_t, std::unique_ptr<sdbusplus::bus::match::match>>
     sensorUpdatedMatches __attribute__((init_priority(101)));
 
+using SensorCacheMap =
+    std::map<uint8_t, std::optional<ipmi::sensor::GetSensorResponse>>;
+SensorCacheMap sensorCacheMap __attribute__((init_priority(101)));
+
 void initSensorMatches()
 {
     using namespace sdbusplus::bus::match::rules;
@@ -483,6 +487,10 @@
 
     try
     {
+#ifdef FEATURE_SENSORS_CACHE
+        // TODO
+        return ipmi::responseSuccess();
+#else
         ipmi::sensor::GetSensorResponse getResponse =
             iter->second.getFunc(iter->second);
 
@@ -492,6 +500,7 @@
                                      getResponse.allEventMessagesEnabled,
                                      getResponse.thresholdLevelsStates,
                                      getResponse.discreteReadingSensorStates);
+#endif
     }
 #ifdef UPDATE_FUNCTIONAL_ON_FAIL
     catch (const SensorFunctionalError& e)