55
66try :
77 import time
8+ import datetime
89 from sonic_sfp .sfputilbase import SfpUtilBase
910except ImportError as e :
1011 raise ImportError ("%s - required module not found" % str (e ))
@@ -20,6 +21,7 @@ class SfpUtil(SfpUtilBase):
2021 EEPROM_OFFSET = 20
2122
2223 _port_to_eeprom_mapping = {}
24+ port_dict = {}
2325
2426 @property
2527 def port_start (self ):
@@ -37,12 +39,33 @@ def qsfp_ports(self):
3739 def port_to_eeprom_mapping (self ):
3840 return self ._port_to_eeprom_mapping
3941
42+ @property
43+ def get_transceiver_status (self ):
44+
45+ try :
46+ reg_file = open ("/sys/devices/platform/dell-s6000-cpld.0/qsfp_modprs" )
47+
48+ except IOError as e :
49+ print "Error: unable to open file: %s" % str (e )
50+ return False
51+
52+ content = reg_file .readline ().rstrip ()
53+
54+ reg_file .close ()
55+
56+ return int (content , 16 )
57+
58+
4059 def __init__ (self ):
60+
4161 eeprom_path = "/sys/class/i2c-adapter/i2c-{0}/{0}-0050/eeprom"
4262
4363 for x in range (0 , self .port_end + 1 ):
4464 self ._port_to_eeprom_mapping [x ] = eeprom_path .format (x + self .EEPROM_OFFSET )
4565
66+ # Get Transceiver status
67+ self .modprs_register = self .get_transceiver_status
68+
4669 SfpUtilBase .__init__ (self )
4770
4871 def get_presence (self , port_num ):
@@ -174,10 +197,60 @@ def reset(self, port_num):
174197
175198 return True
176199
177- def get_transceiver_change_event (self ):
178- """
179- TODO: This function need to be implemented
180- when decide to support monitoring SFP(Xcvrd)
181- on this platform.
182- """
183- raise NotImplementedError
200+ def get_transceiver_change_event (self , timeout = 0 ):
201+
202+ start_time = time .time ()
203+ port_dict = {}
204+ port = self .port_start
205+ forever = False
206+
207+ if timeout == 0 :
208+ forever = True
209+ elif timeout > 0 :
210+ timeout = timeout / float (1000 ) # Convert to secs
211+ else :
212+ print "get_transceiver_change_event:Invalid timeout value" , timeout
213+ return False , {}
214+
215+ end_time = start_time + timeout
216+ if start_time > end_time :
217+ print 'get_transceiver_change_event:' \
218+ 'time wrap / invalid timeout value' , timeout
219+
220+ return False , {} # Time wrap or possibly incorrect timeout
221+
222+ while timeout >= 0 :
223+ # Check for OIR events and return updated port_dict
224+ reg_value = self .get_transceiver_status
225+ if reg_value != self .modprs_register :
226+ changed_ports = self .modprs_register ^ reg_value
227+ while port >= self .port_start and port <= self .port_end :
228+
229+ # Mask off the bit corresponding to our port
230+ mask = (1 << port )
231+
232+ if changed_ports & mask :
233+ # ModPrsL is active low
234+ if reg_value & mask == 0 :
235+ port_dict [port ] = '1'
236+ else :
237+ port_dict [port ] = '0'
238+
239+ port += 1
240+
241+ # Update reg value
242+ self .modprs_register = reg_value
243+ return True , port_dict
244+
245+ if forever :
246+ time .sleep (1 )
247+ else :
248+ timeout = end_time - time .time ()
249+ if timeout >= 1 :
250+ time .sleep (1 ) # We poll at 1 second granularity
251+ else :
252+ if timeout > 0 :
253+ time .sleep (timeout )
254+ return True , {}
255+ print "get_transceiver_change_event: Should not reach here."
256+ return False , {}
0 commit comments