Add compact sensor data record and fix sdr count

Description
- Commit added compact sensor data record structure
- Total sdr count from get sdr list and get device sdr
info mismatch
- Fixed by added condtion to check fru data record,
type12 record and sdrcount increamented
- Also added condition for compact sensor data record count
- get device sdr info command, keep reading record in while. As
"getSensorDataRecord" handler function adds record in end, everytime
record vector keeps growing. So get device info command keep reading the
same record and end up with wrong sdr count
- Changed code to add record in begin everytime

Tested
Successfully built and tested in reference platform. SDR count from
both get sdr list and get device sdr info matches.

$ ipmitool raw 0x04 0x20 0x01
e6 81 7d 01 00 00
$ ipmitool sdr elist all | wc -l
230

$  ipmitool raw 0x04 0x20 0x00
c8 81 7d 01 00 00
$ ipmitool sensor list | wc -l
200

Change-Id: Ifd76a53485de8d92af955e2bb4d4268b3e891110
Signed-off-by: selvaganapathi <selvaganapathim@ami.com>
diff --git a/dbus-sdr/sensorcommands.cpp b/dbus-sdr/sensorcommands.cpp
index 6b3dc8c..c57a1a2 100644
--- a/dbus-sdr/sensorcommands.cpp
+++ b/dbus-sdr/sensorcommands.cpp
@@ -1968,6 +1968,7 @@
             ->getIpmiEntityRecords();
     size_t entityCount = entityRecords.size();
 
+    recordData.clear();
     size_t lastRecord = getNumberOfSensors() + fruCount +
                         ipmi::storage::type12Count + entityCount - 1;
     if (recordID == lastRecordIndex)
@@ -2020,8 +2021,9 @@
             }
             data.header.record_id_msb = recordID >> 8;
             data.header.record_id_lsb = recordID & 0xFF;
-            recordData.insert(recordData.end(), (uint8_t*)&data,
-                              ((uint8_t*)&data) + sizeof(data));
+            recordData.insert(recordData.end(),
+                              reinterpret_cast<uint8_t*>(&data),
+                              reinterpret_cast<uint8_t*>(&data) + sizeof(data));
         }
 
         return 0;
@@ -2100,8 +2102,8 @@
             return GENERAL_ERROR;
         }
 
-        recordData.insert(recordData.end(), (uint8_t*)&record,
-                          ((uint8_t*)&record) + sizeof(record));
+        recordData.insert(recordData.end(), reinterpret_cast<uint8_t*>(&record),
+                          reinterpret_cast<uint8_t*>(&record) + sizeof(record));
 
         return 0;
     }
@@ -2125,8 +2127,8 @@
             constructStaticSensorSdr(ctx, sensorNum, recordID, sensor, record);
         }
 
-        recordData.insert(recordData.end(), (uint8_t*)&record,
-                          ((uint8_t*)&record) + sizeof(record));
+        recordData.insert(recordData.end(), reinterpret_cast<uint8_t*>(&record),
+                          reinterpret_cast<uint8_t*>(&record) + sizeof(record));
 
         return 0;
     }
@@ -2149,8 +2151,8 @@
         {
             return GENERAL_ERROR;
         }
-        recordData.insert(recordData.end(), (uint8_t*)&record,
-                          ((uint8_t*)&record) + sizeof(record));
+        recordData.insert(recordData.end(), reinterpret_cast<uint8_t*>(&record),
+                          reinterpret_cast<uint8_t*>(&record) + sizeof(record));
     }
 
     return 0;
@@ -2196,7 +2198,12 @@
             get_sdr::SensorDataRecordHeader* hdr =
                 reinterpret_cast<get_sdr::SensorDataRecordHeader*>(
                     record.data());
-            if (hdr && hdr->record_type == get_sdr::SENSOR_DATA_FULL_RECORD)
+            if (!hdr)
+            {
+                continue;
+            }
+
+            if (hdr->record_type == get_sdr::SENSOR_DATA_FULL_RECORD)
             {
                 get_sdr::SensorDataFullRecord* recordData =
                     reinterpret_cast<get_sdr::SensorDataFullRecord*>(
@@ -2206,7 +2213,22 @@
                     sdrCount++;
                 }
             }
+            else if (hdr->record_type == get_sdr::SENSOR_DATA_COMPACT_RECORD)
+            {
+                get_sdr::SensorDataCompactRecord* recordData =
+                    reinterpret_cast<get_sdr::SensorDataCompactRecord*>(
+                        record.data());
+                if (ctx->lun == recordData->key.owner_lun)
+                {
+                    sdrCount++;
+                }
+            }
+            else if (hdr->record_type == get_sdr::SENSOR_DATA_FRU_RECORD)
+            {
+                sdrCount++;
+            }
         }
+        sdrCount += ipmi::storage::type12Count;
     }
     else if (count.value_or(0) == getSensorCount)
     {
diff --git a/sensorhandler.hpp b/sensorhandler.hpp
index 66c9bbb..43ed82f 100644
--- a/sensorhandler.hpp
+++ b/sensorhandler.hpp
@@ -189,6 +189,7 @@
 enum SensorDataRecordType
 {
     SENSOR_DATA_FULL_RECORD = 0x1,
+    SENSOR_DATA_COMPACT_RECORD = 0x2,
     SENSOR_DATA_EVENT_RECORD = 0x3,
     SENSOR_DATA_FRU_RECORD = 0x11,
     SENSOR_DATA_ENTITY_RECORD = 0x8,
@@ -340,6 +341,33 @@
     char id_string[FULL_RECORD_ID_STR_MAX_LENGTH];
 } __attribute__((packed));
 
+/** @struct SensorDataCompactRecord
+ *
+ *  Compact Sensor Record(body) - SDR Type 2
+ */
+struct SensorDataCompactRecordBody
+{
+    uint8_t entity_id;
+    uint8_t entity_instance;
+    uint8_t sensor_initialization;
+    uint8_t sensor_capabilities; // no macro support
+    uint8_t sensor_type;
+    uint8_t event_reading_type;
+    uint8_t supported_assertions[2];          // no macro support
+    uint8_t supported_deassertions[2];        // no macro support
+    uint8_t discrete_reading_setting_mask[2]; // no macro support
+    uint8_t sensor_units_1;
+    uint8_t sensor_units_2_base;
+    uint8_t sensor_units_3_modifier;
+    uint8_t record_sharing[2];
+    uint8_t positive_threshold_hysteresis;
+    uint8_t negative_threshold_hysteresis;
+    uint8_t reserved[3];
+    uint8_t oem_reserved;
+    uint8_t id_string_info;
+    char id_string[FULL_RECORD_ID_STR_MAX_LENGTH];
+} __attribute__((packed));
+
 /** @struct SensorDataEventRecord
  *
  *  Event Only Sensor Record(body) - SDR Type 3
@@ -634,6 +662,17 @@
     SensorDataFullRecordBody body;
 } __attribute__((packed));
 
+/** @struct SensorDataComapactRecord
+ *
+ *  Compact Sensor Record - SDR Type 2
+ */
+struct SensorDataCompactRecord
+{
+    SensorDataRecordHeader header;
+    SensorDataRecordKey key;
+    SensorDataCompactRecordBody body;
+} __attribute__((packed));
+
 /** @struct SensorDataEventRecord
  *
  *  Event Only Sensor Record - SDR Type 3