Modify eeprom.py to support cache
This commit is contained in:
parent
d2b2118326
commit
0fcffa20a3
@ -2,13 +2,87 @@
|
||||
|
||||
try:
|
||||
from sonic_platform_pddf_base.pddf_eeprom import PddfEeprom
|
||||
import os
|
||||
except ImportError as e:
|
||||
raise ImportError(str(e) + "- required module not found")
|
||||
|
||||
CACHE_ROOT = '/var/cache/sonic/decode-syseeprom'
|
||||
CACHE_FILE = 'syseeprom_cache'
|
||||
|
||||
class Eeprom(PddfEeprom):
|
||||
_TLV_INFO_MAX_LEN = 256
|
||||
pddf_obj = {}
|
||||
plugin_data = {}
|
||||
|
||||
def __init__(self, pddf_data=None, pddf_plugin_data=None):
|
||||
PddfEeprom.__init__(self, pddf_data, pddf_plugin_data)
|
||||
#PddfEeprom.__init__(self, pddf_data, pddf_plugin_data)
|
||||
if not pddf_data or not pddf_plugin_data:
|
||||
raise ValueError('PDDF JSON data error')
|
||||
|
||||
self.pddf_obj = pddf_data
|
||||
self.plugin_data = pddf_plugin_data
|
||||
|
||||
# system EEPROM always has device name EEPROM1
|
||||
self.eeprom_path = self.pddf_obj.get_path("EEPROM1", "eeprom")
|
||||
if self.eeprom_path is None:
|
||||
return
|
||||
|
||||
super(PddfEeprom, self).__init__(self.eeprom_path, 0, '', True)
|
||||
self.eeprom_tlv_dict = dict()
|
||||
|
||||
# Create the cache directory if not created
|
||||
if not os.path.exists(CACHE_ROOT):
|
||||
try:
|
||||
os.makedirs(CACHE_ROOT)
|
||||
except Exception as e:
|
||||
print("Error in creating Eeprom cache directory - {}".format(str(e)))
|
||||
|
||||
# Assign cache_name in eeprom_base.py
|
||||
try:
|
||||
self.set_cache_name(os.path.join(CACHE_ROOT, CACHE_FILE))
|
||||
except:
|
||||
pass
|
||||
|
||||
try:
|
||||
self.eeprom_data = self.read_eeprom()
|
||||
except Exception as e:
|
||||
self.eeprom_data = "N/A"
|
||||
raise RuntimeError("Eeprom is not Programmed - Error: {}".format(str(e)))
|
||||
else:
|
||||
eeprom = self.eeprom_data
|
||||
|
||||
try:
|
||||
self.update_cache(eeprom)
|
||||
except:
|
||||
pass
|
||||
|
||||
if not self.is_valid_tlvinfo_header(eeprom):
|
||||
return
|
||||
|
||||
total_length = ((eeprom[9]) << 8) | (eeprom[10])
|
||||
tlv_index = self._TLV_INFO_HDR_LEN
|
||||
tlv_end = self._TLV_INFO_HDR_LEN + total_length
|
||||
|
||||
while (tlv_index + 2) < len(eeprom) and tlv_index < tlv_end:
|
||||
if not self.is_valid_tlv(eeprom[tlv_index:]):
|
||||
break
|
||||
|
||||
tlv = eeprom[tlv_index:tlv_index + 2
|
||||
+ (eeprom[tlv_index + 1])]
|
||||
code = "0x%02X" % ((tlv[0]))
|
||||
|
||||
if (tlv[0]) == self._TLV_CODE_VENDOR_EXT:
|
||||
value = str(((tlv[2]) << 24) | ((tlv[3]) << 16) |
|
||||
((tlv[4]) << 8) | (tlv[5]))
|
||||
value += str(tlv[6:6 + (tlv[1])])
|
||||
else:
|
||||
name, value = self.decoder(None, tlv)
|
||||
|
||||
self.eeprom_tlv_dict[code] = value
|
||||
if (eeprom[tlv_index]) == self._TLV_CODE_CRC_32:
|
||||
break
|
||||
|
||||
tlv_index += (eeprom[tlv_index+1]) + 2
|
||||
|
||||
|
||||
# Provide the functions/variables below for which implementation is to be overwritten
|
||||
|
Reference in New Issue
Block a user