[device/as5835-54t] Implement get_transceiver_change_event() in sfputil.py (#3485)
Signed-off-by: brandon_chuang <brandon_chuang@edge-core.com>
This commit is contained in:
parent
622f3f5d1a
commit
a97b15e0ec
@ -11,6 +11,8 @@ try:
|
|||||||
except ImportError as e:
|
except ImportError as e:
|
||||||
raise ImportError("%s - required module not found" % str(e))
|
raise ImportError("%s - required module not found" % str(e))
|
||||||
|
|
||||||
|
SFP_STATUS_INSERTED = '1'
|
||||||
|
SFP_STATUS_REMOVED = '0'
|
||||||
|
|
||||||
class SfpUtil(SfpUtilBase):
|
class SfpUtil(SfpUtilBase):
|
||||||
"""Platform-specific SfpUtil class"""
|
"""Platform-specific SfpUtil class"""
|
||||||
@ -216,10 +218,90 @@ class SfpUtil(SfpUtilBase):
|
|||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def get_transceiver_change_event(self):
|
@property
|
||||||
"""
|
def _get_presence_bitmap(self):
|
||||||
TODO: This function need to be implemented
|
|
||||||
when decide to support monitoring SFP(Xcvrd)
|
bitmap = ""
|
||||||
on this platform.
|
try:
|
||||||
"""
|
reg_file = open("/sys/bus/i2c/devices/3-0062/module_present_all")
|
||||||
raise NotImplementedError
|
except IOError as e:
|
||||||
|
print "Error: unable to open file: %s" % str(e)
|
||||||
|
return False
|
||||||
|
bitmap += reg_file.readline().rstrip() + " "
|
||||||
|
reg_file.close()
|
||||||
|
|
||||||
|
rev = bitmap.split(" ")
|
||||||
|
rev.pop() # Remove the last useless character
|
||||||
|
|
||||||
|
# Save port 49-54 into buffer
|
||||||
|
tmp = rev.pop()
|
||||||
|
|
||||||
|
# Insert port 1-48
|
||||||
|
for i in range (0, 6):
|
||||||
|
rev.append(hex(0)[2:])
|
||||||
|
rev[i] = rev[i].zfill(2)
|
||||||
|
|
||||||
|
# Expand port 49-54
|
||||||
|
for i in range (0, 6):
|
||||||
|
val = (int(tmp,16) >> i) & 0x1
|
||||||
|
rev.append(hex(val)[2:])
|
||||||
|
|
||||||
|
rev = "".join(rev[::-1])
|
||||||
|
return int(rev,16)
|
||||||
|
|
||||||
|
data = {'valid':0, 'present':0}
|
||||||
|
def get_transceiver_change_event(self, timeout=0):
|
||||||
|
|
||||||
|
start_time = time.time()
|
||||||
|
port_dict = {}
|
||||||
|
port = 0
|
||||||
|
blocking = False
|
||||||
|
|
||||||
|
if timeout == 0:
|
||||||
|
blocking = True
|
||||||
|
elif timeout > 0:
|
||||||
|
timeout = timeout / float(1000) # Convert to secs
|
||||||
|
else:
|
||||||
|
print "get_transceiver_change_event:Invalid timeout value", timeout
|
||||||
|
return False, {}
|
||||||
|
|
||||||
|
end_time = start_time + timeout
|
||||||
|
if start_time > end_time:
|
||||||
|
print 'get_transceiver_change_event:' \
|
||||||
|
'time wrap / invalid timeout value', timeout
|
||||||
|
|
||||||
|
return False, {} # Time wrap or possibly incorrect timeout
|
||||||
|
|
||||||
|
while timeout >= 0:
|
||||||
|
# Check for OIR events and return updated port_dict
|
||||||
|
|
||||||
|
reg_value = self._get_presence_bitmap
|
||||||
|
changed_ports = self.data['present'] ^ reg_value
|
||||||
|
if changed_ports:
|
||||||
|
for port in range (self.port_start, self.port_end+1):
|
||||||
|
# Mask off the bit corresponding to our port
|
||||||
|
mask = (1 << (port - 1))
|
||||||
|
if changed_ports & mask:
|
||||||
|
|
||||||
|
if (reg_value & mask) == 0:
|
||||||
|
port_dict[port] = SFP_STATUS_REMOVED
|
||||||
|
else:
|
||||||
|
port_dict[port] = SFP_STATUS_INSERTED
|
||||||
|
|
||||||
|
# Update cache
|
||||||
|
self.data['present'] = reg_value
|
||||||
|
self.data['valid'] = 1
|
||||||
|
return True, port_dict
|
||||||
|
|
||||||
|
if blocking:
|
||||||
|
time.sleep(1)
|
||||||
|
else:
|
||||||
|
timeout = end_time - time.time()
|
||||||
|
if timeout >= 1:
|
||||||
|
time.sleep(1) # We poll at 1 second granularity
|
||||||
|
else:
|
||||||
|
if timeout > 0:
|
||||||
|
time.sleep(timeout)
|
||||||
|
return True, {}
|
||||||
|
print "get_transceiver_change_event: Should not reach here."
|
||||||
|
return False, {}
|
||||||
|
Reference in New Issue
Block a user