Skip to content

Commit 7700003

Browse files
AnoopKamathmssonicbld
authored andcommitted
Wait for specified duration during LPmode on/off (#503)
* wait for specified duration during LPmode on/off * Add Macro and remove redundant sleep * Update test_cmis.py * Address review comments * Indentation issue
1 parent 8fb25f1 commit 7700003

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

sonic_platform_base/sonic_xcvr/api/public/cmis.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,21 @@ def get_lpmode(self):
10661066
return True
10671067
return False
10681068

1069+
def wait_time_condition(self, condition_func, expected_state, duration_ms, delay_retry):
1070+
'''
1071+
This function will wait and retry based on
1072+
condition function state and delay provided
1073+
'''
1074+
start_time = time.time()
1075+
duration = duration_ms / 1000
1076+
# Loop until the duration has elapsed
1077+
while (time.time() - start_time) < duration:
1078+
if condition_func() == expected_state:
1079+
return True
1080+
# Sleep for a delay_retry interval before the next check
1081+
time.sleep(delay_retry)
1082+
return condition_func() == expected_state
1083+
10691084
def set_lpmode(self, lpmode):
10701085
'''
10711086
This function sets the module to low power state.
@@ -1077,23 +1092,21 @@ def set_lpmode(self, lpmode):
10771092
if self.is_flat_memory() or not self.get_lpmode_support():
10781093
return False
10791094

1095+
DELAY_RETRY = 0.1
10801096
lpmode_val = self.xcvr_eeprom.read(consts.MODULE_LEVEL_CONTROL)
10811097
if lpmode_val is not None:
10821098
if lpmode is True:
10831099
# Force module transition to LowPwr under SW control
10841100
lpmode_val = lpmode_val | (1 << CmisApi.LowPwrRequestSW)
10851101
self.xcvr_eeprom.write(consts.MODULE_LEVEL_CONTROL, lpmode_val)
1086-
time.sleep(0.1)
1087-
return self.get_lpmode()
1102+
return self.wait_time_condition(self.get_lpmode, True, self.get_module_pwr_down_duration(), DELAY_RETRY)
10881103
else:
10891104
# Force transition from LowPwr to HighPower state under SW control.
10901105
# This will transition LowPwrS signal to False. (see Table 6-12 CMIS v5.0)
10911106
lpmode_val = lpmode_val & ~(1 << CmisApi.LowPwrRequestSW)
10921107
lpmode_val = lpmode_val & ~(1 << CmisApi.LowPwrAllowRequestHW)
10931108
self.xcvr_eeprom.write(consts.MODULE_LEVEL_CONTROL, lpmode_val)
1094-
time.sleep(1)
1095-
mstate = self.get_module_state()
1096-
return True if mstate == 'ModuleReady' else False
1109+
return self.wait_time_condition(self.get_module_state, 'ModuleReady', self.get_module_pwr_up_duration(), DELAY_RETRY)
10971110
return False
10981111

10991112
def get_loopback_capability(self):

tests/sonic_xcvr/test_cmis.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,7 @@ def test_reset(self):
10121012
assert kall[0] == (consts.MODULE_LEVEL_CONTROL, 0x8)
10131013

10141014
@pytest.mark.parametrize("lpmode", [
1015-
([True], [False])
1015+
True, False
10161016
])
10171017
def test_set_low_power(self, lpmode):
10181018
self.api.xcvr_eeprom.read = MagicMock()
@@ -1024,6 +1024,8 @@ def test_set_low_power(self, lpmode):
10241024
self.api.get_lpmode_support = MagicMock()
10251025
self.api.get_lpmode_support.return_value = True
10261026
self.api.get_module_state = MagicMock()
1027+
self.api.get_lpmode = MagicMock()
1028+
self.api.get_lpmode.return_value = True
10271029
self.api.get_module_state.return_value = "ModuleReady"
10281030
self.api.set_lpmode(lpmode)
10291031

0 commit comments

Comments
 (0)