sensorhandler: add get_sdr_info

Change-Id: I13c82b62cec1c2f505fa81786fdc60cb38b2a636
Signed-off-by: Emily Shaffer <emilyshaffer@google.com>
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
diff --git a/sensorhandler.cpp b/sensorhandler.cpp
index 71376d2..c1657da 100644
--- a/sensorhandler.cpp
+++ b/sensorhandler.cpp
@@ -664,6 +664,37 @@
     return rc;
 }
 
+ipmi_ret_t ipmi_sen_get_sdr_info(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
+                                 ipmi_request_t request,
+                                 ipmi_response_t response,
+                                 ipmi_data_len_t data_len,
+                                 ipmi_context_t context)
+{
+    auto resp = static_cast<get_sdr_info::GetSdrInfoResp*>(response);
+    if (request == nullptr ||
+        get_sdr_info::request::get_count(request) == false)
+    {
+        // Get Sensor Count
+        resp->count = sensors.size();
+    }
+    else
+    {
+        resp->count = 1;
+    }
+
+    // Multiple LUNs not supported.
+    namespace response = get_sdr_info::response;
+    response::set_lun_present(0, &(resp->luns_and_dynamic_population));
+    response::set_lun_not_present(1, &(resp->luns_and_dynamic_population));
+    response::set_lun_not_present(2, &(resp->luns_and_dynamic_population));
+    response::set_lun_not_present(3, &(resp->luns_and_dynamic_population));
+    response::set_static_population(&(resp->luns_and_dynamic_population));
+
+    *data_len = SDR_INFO_RESP_SIZE;
+
+    return IPMI_CC_OK;
+}
+
 
 void register_netfn_sen_functions()
 {
@@ -687,5 +718,9 @@
     ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_READING, NULL,
                            ipmi_sen_get_sensor_reading, PRIVILEGE_USER);
 
+    // <Get SDR Info>
+    printf("Registering NetFn:[0x%X], Cmd:[0x%x]\n",NETFUN_SENSOR, IPMI_CMD_GET_SDR_INFO);
+    ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SDR_INFO, NULL,
+                           ipmi_sen_get_sdr_info, PRIVILEGE_USER);
     return;
 }
diff --git a/sensorhandler.h b/sensorhandler.h
index cedb2e4..ef30779 100644
--- a/sensorhandler.h
+++ b/sensorhandler.h
@@ -6,9 +6,10 @@
 // IPMI commands for net functions.
 enum ipmi_netfn_sen_cmds
 {
+    IPMI_CMD_GET_SDR_INFO       = 0x20,
     IPMI_CMD_GET_SENSOR_READING = 0x2D,
-    IPMI_CMD_GET_SENSOR_TYPE = 0x2F,
-    IPMI_CMD_SET_SENSOR      = 0x30,
+    IPMI_CMD_GET_SENSOR_TYPE    = 0x2F,
+    IPMI_CMD_SET_SENSOR         = 0x30,
 };
 
 // Discrete sensor types.
@@ -53,4 +54,48 @@
     uint8_t eventData3;
 } __attribute__((packed));
 
+/**
+ * Get SDR Info
+ */
+
+namespace get_sdr_info
+{
+namespace request
+{
+// Note: for some reason the ipmi_request_t appears to be the
+// raw value for this call.
+inline bool get_count(void* req)
+{
+    return (bool)((uint64_t)(req) & 1);
+}
+} // namespace request
+
+namespace response
+{
+#define SDR_INFO_RESP_SIZE 2
+inline void set_lun_present(int lun, uint8_t* resp)
+{
+    *resp |= 1 << lun;
+}
+inline void set_lun_not_present(int lun, uint8_t* resp)
+{
+    *resp &= ~(1 << lun);
+}
+inline void set_dynamic_population(uint8_t* resp)
+{
+    *resp |= 1 << 7;
+}
+inline void set_static_population(uint8_t* resp)
+{
+    *resp &= ~(1 << 7);
+}
+} // namespace response
+
+struct GetSdrInfoResp
+{
+    uint8_t count;
+    uint8_t luns_and_dynamic_population;
+};
+
+} // namespace get_sdr_info
 #endif