kontron / python-ipmi

A pure python IPMI library
GNU Lesser General Public License v2.1
187 stars 74 forks source link

Getting SDR repository should work when there is an unknown record #99

Closed EmilioPeJu closed 2 years ago

EmilioPeJu commented 2 years ago

When iterating the SDR repository, if it contains an unknown record, an exception is raised and we can't continue iterating over the remaining records. A possible solution would be having a default unknown sensor record class for dealing with that case, for example:

     SdrManagementControllerDeviceLocator,
-                SDR_TYPE_OEM_SENSOR_RECORD:
-                    SdrOEMSensorRecord,
-            }[sdr_type]
-        except KeyError:
-            raise DecodingError('Unsupported SDR type(0x%02x)' % sdr_type)
+        cls = {
+            SDR_TYPE_FULL_SENSOR_RECORD:
+                SdrFullSensorRecord,
+            SDR_TYPE_COMPACT_SENSOR_RECORD:
+                SdrCompactSensorRecord,
+            SDR_TYPE_EVENT_ONLY_SENSOR_RECORD:
+                SdrEventOnlySensorRecord,
+            SDR_TYPE_FRU_DEVICE_LOCATOR_RECORD:
+                SdrFruDeviceLocator,
+            SDR_TYPE_MANAGEMENT_CONTROLLER_DEVICE_LOCATOR_RECORD:
+                SdrManagementControllerDeviceLocator,
+            SDR_TYPE_OEM_SENSOR_RECORD:
+                SdrOEMSensorRecord,
+        }.get(sdr_type, SdrUnknownSensorRecord)

         return cls(data, next_id)

@@ -627,3 +624,20 @@ class SdrOEMSensorRecord(SdrCommon):

         # record key bytes
         self._common_record_key(buffer.pop_slice(3))
+
+
+# Any SDR type not known or not implemented
+class SdrUnknownSensorRecord(SdrCommon):
+    def __init__(self, data=None, next_id=None):
+        super(SdrUnknownSensorRecord, self).__init__(data, next_id)
+        if data:
+            self._from_data(data)
+
+    def __str__(self):
+        return 'Not supported yet.'
+
+    def _from_data(self, data):
+        buffer = ByteBuffer(data[5:])
+
+        # record key bytes
+        self._common_record_key(buffer.pop_slice(3))
hthiery commented 2 years ago

Seems to be a good idea. Pleas make a PR and add a test for that.

EmilioPeJu commented 2 years ago

Done! I simplified the new class a bit, given we cannot assume an unknown record will have the record key part