Skip to content

Commit

Permalink
[device/as5835-54t] Implement get_transceiver_change_event() in sfput…
Browse files Browse the repository at this point in the history
…il.py (#3485)

Signed-off-by: brandon_chuang <[email protected]>
  • Loading branch information
brandonchuang authored and jleveque committed Sep 23, 2019
1 parent 622f3f5 commit a97b15e
Showing 1 changed file with 89 additions and 7 deletions.
96 changes: 89 additions & 7 deletions device/accton/x86_64-accton_as5835_54t-r0/plugins/sfputil.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
except ImportError as e:
raise ImportError("%s - required module not found" % str(e))

SFP_STATUS_INSERTED = '1'
SFP_STATUS_REMOVED = '0'

class SfpUtil(SfpUtilBase):
"""Platform-specific SfpUtil class"""
Expand Down Expand Up @@ -216,10 +218,90 @@ def reset(self, port_num):

return True

def get_transceiver_change_event(self):
"""
TODO: This function need to be implemented
when decide to support monitoring SFP(Xcvrd)
on this platform.
"""
raise NotImplementedError
@property
def _get_presence_bitmap(self):

bitmap = ""
try:
reg_file = open("/sys/bus/i2c/devices/3-0062/module_present_all")
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, {}

0 comments on commit a97b15e

Please sign in to comment.