|
11 | 11 | except ImportError as e:
|
12 | 12 | raise ImportError("%s - required module not found" % str(e))
|
13 | 13 |
|
| 14 | +SFP_STATUS_INSERTED = '1' |
| 15 | +SFP_STATUS_REMOVED = '0' |
14 | 16 |
|
15 | 17 | class SfpUtil(SfpUtilBase):
|
16 | 18 | """Platform-specific SfpUtil class"""
|
@@ -216,10 +218,90 @@ def reset(self, port_num):
|
216 | 218 |
|
217 | 219 | return True
|
218 | 220 |
|
219 |
| - def get_transceiver_change_event(self): |
220 |
| - """ |
221 |
| - TODO: This function need to be implemented |
222 |
| - when decide to support monitoring SFP(Xcvrd) |
223 |
| - on this platform. |
224 |
| - """ |
225 |
| - raise NotImplementedError |
| 221 | + @property |
| 222 | + def _get_presence_bitmap(self): |
| 223 | + |
| 224 | + bitmap = "" |
| 225 | + try: |
| 226 | + reg_file = open("/sys/bus/i2c/devices/3-0062/module_present_all") |
| 227 | + except IOError as e: |
| 228 | + print "Error: unable to open file: %s" % str(e) |
| 229 | + return False |
| 230 | + bitmap += reg_file.readline().rstrip() + " " |
| 231 | + reg_file.close() |
| 232 | + |
| 233 | + rev = bitmap.split(" ") |
| 234 | + rev.pop() # Remove the last useless character |
| 235 | + |
| 236 | + # Save port 49-54 into buffer |
| 237 | + tmp = rev.pop() |
| 238 | + |
| 239 | + # Insert port 1-48 |
| 240 | + for i in range (0, 6): |
| 241 | + rev.append(hex(0)[2:]) |
| 242 | + rev[i] = rev[i].zfill(2) |
| 243 | + |
| 244 | + # Expand port 49-54 |
| 245 | + for i in range (0, 6): |
| 246 | + val = (int(tmp,16) >> i) & 0x1 |
| 247 | + rev.append(hex(val)[2:]) |
| 248 | + |
| 249 | + rev = "".join(rev[::-1]) |
| 250 | + return int(rev,16) |
| 251 | + |
| 252 | + data = {'valid':0, 'present':0} |
| 253 | + def get_transceiver_change_event(self, timeout=0): |
| 254 | + |
| 255 | + start_time = time.time() |
| 256 | + port_dict = {} |
| 257 | + port = 0 |
| 258 | + blocking = False |
| 259 | + |
| 260 | + if timeout == 0: |
| 261 | + blocking = True |
| 262 | + elif timeout > 0: |
| 263 | + timeout = timeout / float(1000) # Convert to secs |
| 264 | + else: |
| 265 | + print "get_transceiver_change_event:Invalid timeout value", timeout |
| 266 | + return False, {} |
| 267 | + |
| 268 | + end_time = start_time + timeout |
| 269 | + if start_time > end_time: |
| 270 | + print 'get_transceiver_change_event:' \ |
| 271 | + 'time wrap / invalid timeout value', timeout |
| 272 | + |
| 273 | + return False, {} # Time wrap or possibly incorrect timeout |
| 274 | + |
| 275 | + while timeout >= 0: |
| 276 | + # Check for OIR events and return updated port_dict |
| 277 | + |
| 278 | + reg_value = self._get_presence_bitmap |
| 279 | + changed_ports = self.data['present'] ^ reg_value |
| 280 | + if changed_ports: |
| 281 | + for port in range (self.port_start, self.port_end+1): |
| 282 | + # Mask off the bit corresponding to our port |
| 283 | + mask = (1 << (port - 1)) |
| 284 | + if changed_ports & mask: |
| 285 | + |
| 286 | + if (reg_value & mask) == 0: |
| 287 | + port_dict[port] = SFP_STATUS_REMOVED |
| 288 | + else: |
| 289 | + port_dict[port] = SFP_STATUS_INSERTED |
| 290 | + |
| 291 | + # Update cache |
| 292 | + self.data['present'] = reg_value |
| 293 | + self.data['valid'] = 1 |
| 294 | + return True, port_dict |
| 295 | + |
| 296 | + if blocking: |
| 297 | + time.sleep(1) |
| 298 | + else: |
| 299 | + timeout = end_time - time.time() |
| 300 | + if timeout >= 1: |
| 301 | + time.sleep(1) # We poll at 1 second granularity |
| 302 | + else: |
| 303 | + if timeout > 0: |
| 304 | + time.sleep(timeout) |
| 305 | + return True, {} |
| 306 | + print "get_transceiver_change_event: Should not reach here." |
| 307 | + return False, {} |
0 commit comments