|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | + |
| 4 | +# Sample pddf_fanutil file |
| 5 | +# All the supported FAN SysFS aattributes are |
| 6 | +#- fan<idx>_present |
| 7 | +#- fan<idx>_direction |
| 8 | +#- fan<idx>_input |
| 9 | +#- fan<idx>_pwm |
| 10 | +#- fan<idx>_fault |
| 11 | +# where idx is in the range [1-12] |
| 12 | +# |
| 13 | + |
| 14 | + |
| 15 | +import os.path |
| 16 | +import sys |
| 17 | +sys.path.append('/usr/share/sonic/platform/plugins') |
| 18 | +import pddfparse |
| 19 | +import json |
| 20 | + |
| 21 | +try: |
| 22 | + from sonic_fan.fan_base import FanBase |
| 23 | +except ImportError as e: |
| 24 | + raise ImportError (str(e) + "- required module not found") |
| 25 | + |
| 26 | +class FanUtil(FanBase): |
| 27 | + """PDDF generic FAN util class""" |
| 28 | + |
| 29 | + def __init__(self): |
| 30 | + FanBase.__init__(self) |
| 31 | + global pddf_obj |
| 32 | + global plugin_data |
| 33 | + with open(os.path.join(os.path.dirname(os.path.realpath(__file__)) + '/../pddf/pd-plugin.json')) as pd: |
| 34 | + plugin_data = json.load(pd) |
| 35 | + |
| 36 | + pddf_obj = pddfparse.PddfParse() |
| 37 | + self.platform = pddf_obj.get_platform() |
| 38 | + |
| 39 | + self.num_fans = (self.platform['num_fantrays'] * self.platform['num_fans_pertray'] ) |
| 40 | + |
| 41 | + def get_num_fans(self): |
| 42 | + return self.num_fans |
| 43 | + |
| 44 | + def get_presence(self, idx): |
| 45 | + # 1 based fan index |
| 46 | + if idx<1 or idx>self.num_fans: |
| 47 | + print "Invalid fan index %d\n"%idx |
| 48 | + return False |
| 49 | + |
| 50 | + attr_name = "fan"+ str(idx) +"_present" |
| 51 | + output = pddf_obj.get_attr_name_output("FAN-CTRL", attr_name) |
| 52 | + if not output: |
| 53 | + return False |
| 54 | + |
| 55 | + mode = output['mode'] |
| 56 | + presence = output['status'].rstrip() |
| 57 | + |
| 58 | + vmap = plugin_data['FAN']['present'][mode]['valmap'] |
| 59 | + |
| 60 | + if presence in vmap: |
| 61 | + status = vmap[presence] |
| 62 | + else: |
| 63 | + status = False |
| 64 | + |
| 65 | + return status |
| 66 | + |
| 67 | + def get_status(self, idx): |
| 68 | + # 1 based fan index |
| 69 | + if idx<1 or idx>self.num_fans: |
| 70 | + print "Invalid fan index %d\n"%idx |
| 71 | + return False |
| 72 | + |
| 73 | + speed = self.get_speed(idx) |
| 74 | + status = True if (speed != 0) else False |
| 75 | + return status |
| 76 | + |
| 77 | + def get_direction(self, idx): |
| 78 | + # 1 based fan index |
| 79 | + if idx<1 or idx>self.num_fans: |
| 80 | + print "Invalid fan index %d\n"%idx |
| 81 | + return None |
| 82 | + |
| 83 | + attr = "fan" + str(idx) + "_direction" |
| 84 | + output = pddf_obj.get_attr_name_output("FAN-CTRL", attr) |
| 85 | + if not output: |
| 86 | + return None |
| 87 | + |
| 88 | + mode = output['mode'] |
| 89 | + val = output['status'] |
| 90 | + |
| 91 | + val = val.rstrip() |
| 92 | + vmap = plugin_data['FAN']['direction'][mode]['valmap'] |
| 93 | + |
| 94 | + |
| 95 | + if val in vmap: |
| 96 | + direction = vmap[val] |
| 97 | + else: |
| 98 | + direction = val |
| 99 | + |
| 100 | + return direction |
| 101 | + |
| 102 | + def get_directions(self): |
| 103 | + num_fan = self.get_num_fan(); |
| 104 | + |
| 105 | + for i in range(1, num_fan+1): |
| 106 | + attr = "fan" + str(i) + "_direction" |
| 107 | + output = pddf_obj.get_attr_name_output("FAN-CTRL", attr) |
| 108 | + if not output: |
| 109 | + return None |
| 110 | + |
| 111 | + mode = output['mode'] |
| 112 | + val = output['status'] |
| 113 | + |
| 114 | + val = val.rstrip() |
| 115 | + vmap = plugin_data['FAN']['direction'][mode]['valmap'] |
| 116 | + |
| 117 | + direction = vmap[str(val)] |
| 118 | + |
| 119 | + print "FAN-%d direction is %s"%(i, direction) |
| 120 | + |
| 121 | + return 0 |
| 122 | + |
| 123 | + def get_speed(self, idx): |
| 124 | + # 1 based fan index |
| 125 | + if idx<1 or idx>self.num_fans: |
| 126 | + print "Invalid fan index %d\n"%idx |
| 127 | + return 0 |
| 128 | + |
| 129 | + attr = "fan" + str(idx) + "_input" |
| 130 | + output = pddf_obj.get_attr_name_output("FAN-CTRL", attr) |
| 131 | + if not output: |
| 132 | + return 0 |
| 133 | + |
| 134 | + #mode = output['mode'] |
| 135 | + val = output['status'].rstrip() |
| 136 | + |
| 137 | + if val.isalpha(): |
| 138 | + return 0 |
| 139 | + else: |
| 140 | + rpm_speed = int(float(val)) |
| 141 | + |
| 142 | + return rpm_speed |
| 143 | + |
| 144 | + def get_speeds(self): |
| 145 | + num_fan = self.get_num_fan(); |
| 146 | + ret = "FAN_INDEX\t\tRPM\n" |
| 147 | + |
| 148 | + for i in range(1, num_fan+1): |
| 149 | + attr1 = "fan" + str(i) + "_input" |
| 150 | + output = pddf_obj.get_attr_name_output("FAN-CTRL", attr1) |
| 151 | + if not output: |
| 152 | + return "" |
| 153 | + |
| 154 | + #mode = output['mode'] |
| 155 | + val = output['status'].rstrip() |
| 156 | + |
| 157 | + if val.isalpha(): |
| 158 | + frpm = 0 |
| 159 | + else: |
| 160 | + frpm = int(val) |
| 161 | + |
| 162 | + ret += "FAN-%d\t\t\t%d\n"%(i, frpm) |
| 163 | + |
| 164 | + return ret |
| 165 | + |
| 166 | + def set_speed(self, val): |
| 167 | + if val<0 or val>100: |
| 168 | + print "Error: Invalid speed %d. Please provide a valid speed percentage"%val |
| 169 | + return False |
| 170 | + |
| 171 | + num_fan = self.num_fans |
| 172 | + if 'duty_cycle_to_pwm' not in plugin_data['FAN']: |
| 173 | + print "Setting fan speed is not allowed !" |
| 174 | + return False |
| 175 | + else: |
| 176 | + duty_cycle_to_pwm = eval(plugin_data['FAN']['duty_cycle_to_pwm']) |
| 177 | + pwm = duty_cycle_to_pwm(val) |
| 178 | + print "New Speed: %d%% - PWM value to be set is %d\n"%(val,pwm) |
| 179 | + |
| 180 | + for i in range(1, num_fan+1): |
| 181 | + attr = "fan" + str(i) + "_pwm" |
| 182 | + node = pddf_obj.get_path("FAN-CTRL", attr) |
| 183 | + if node is None: |
| 184 | + return False |
| 185 | + try: |
| 186 | + with open(node, 'w') as f: |
| 187 | + f.write(str(pwm)) |
| 188 | + except IOError: |
| 189 | + return False |
| 190 | + |
| 191 | + return True |
| 192 | + |
| 193 | + def dump_sysfs(self): |
| 194 | + return pddf_obj.cli_dump_dsysfs('fan') |
| 195 | + |
| 196 | + def get_change_event(self): |
| 197 | + """ |
| 198 | + TODO: This function need to be implemented |
| 199 | + when decide to support monitoring FAN(fand) |
| 200 | + on this platform. |
| 201 | + """ |
| 202 | + raise NotImplementedError |
0 commit comments