From 14e4da8a3115889789a21c9d991a6067b3beef0f Mon Sep 17 00:00:00 2001 From: Markus Ressel Date: Mon, 16 May 2022 23:53:51 +0200 Subject: [PATCH 1/2] fix hwmon device index mapping --- cmd/curve.go | 5 ++-- cmd/detect.go | 25 ++++++++++++++++---- cmd/fan/fan.go | 5 ++-- internal/backend.go | 6 ++--- internal/hwmon/hwmon.go | 51 ++++++++++++++++++++++------------------- 5 files changed, 54 insertions(+), 38 deletions(-) diff --git a/cmd/curve.go b/cmd/curve.go index 341afd4..9b4b345 100644 --- a/cmd/curve.go +++ b/cmd/curve.go @@ -41,9 +41,8 @@ var curveCmd = &cobra.Command{ ui.Fatal("Failed to match platform regex of %s (%s) against controller platform %s", config.ID, config.HwMon.Platform, controller.Platform) } if matched { - index := config.HwMon.Index - 1 - if len(controller.Fans) > index { - fan := controller.Fans[index] + fan, exists := controller.Fans[config.HwMon.Index] + if exists { config.HwMon.PwmOutput = fan.PwmOutput config.HwMon.RpmInput = fan.RpmInput break diff --git a/cmd/detect.go b/cmd/detect.go index 86f918f..f03dbad 100644 --- a/cmd/detect.go +++ b/cmd/detect.go @@ -10,6 +10,7 @@ import ( "github.com/spf13/cobra" "github.com/tomlazar/table" "path/filepath" + "sort" "strconv" ) @@ -39,17 +40,24 @@ var detectCmd = &cobra.Command{ continue } - fanList := controller.Fans - sensorList := controller.Sensors + fanMap := controller.Fans + sensorMap := controller.Sensors - if len(fanList) <= 0 && len(sensorList) <= 0 { + if len(fanMap) <= 0 && len(sensorMap) <= 0 { continue } ui.Printfln("> %s", controller.Name) + fanMapKeys := make([]int, 0, len(fanMap)) + for k := range fanMap { + fanMapKeys = append(fanMapKeys, k) + } + sort.Ints(fanMapKeys) + var fanRows [][]string - for _, fan := range fanList { + for _, index := range fanMapKeys { + fan := fanMap[index] pwmText := "N/A" if pwm, err := fan.GetPwm(); err == nil { pwmText = strconv.Itoa(pwm) @@ -72,8 +80,15 @@ var detectCmd = &cobra.Command{ Rows: fanRows, } + sensorMapKeys := make([]int, 0, len(sensorMap)) + for k := range sensorMap { + sensorMapKeys = append(sensorMapKeys, k) + } + sort.Ints(sensorMapKeys) + var sensorRows [][]string - for _, sensor := range sensorList { + for _, index := range sensorMapKeys { + sensor := sensorMap[index] value, err := sensor.GetValue() valueText := "N/A" if err == nil { diff --git a/cmd/fan/fan.go b/cmd/fan/fan.go index 22b070b..b787dc9 100644 --- a/cmd/fan/fan.go +++ b/cmd/fan/fan.go @@ -50,9 +50,8 @@ func getFan(id string) (fans.Fan, error) { return nil, errors.New(fmt.Sprintf("Failed to match platform regex of %s (%s) against controller platform %s", config.ID, config.HwMon.Platform, controller.Platform)) } if matched { - index := config.HwMon.Index - 1 - if len(controller.Fans) > index { - fan := controller.Fans[index] + fan, exists := controller.Fans[config.HwMon.Index] + if exists { config.HwMon.PwmOutput = fan.PwmOutput config.HwMon.RpmInput = fan.RpmInput break diff --git a/internal/backend.go b/internal/backend.go index 29667fc..ddc041f 100644 --- a/internal/backend.go +++ b/internal/backend.go @@ -253,9 +253,9 @@ func initializeFans(controllers []*hwmon.HwMonController) map[configuration.FanC } if matched { found = true - index := config.HwMon.Index - 1 - if len(c.Fans) > index { - fan := c.Fans[index] + + fan, exists := c.Fans[config.HwMon.Index] + if exists { config.HwMon.PwmOutput = fan.PwmOutput config.HwMon.RpmInput = fan.RpmInput } diff --git a/internal/hwmon/hwmon.go b/internal/hwmon/hwmon.go index 74c2acd..e02bc8e 100644 --- a/internal/hwmon/hwmon.go +++ b/internal/hwmon/hwmon.go @@ -30,8 +30,10 @@ type HwMonController struct { Platform string Path string - Fans []*fans.HwMonFan - Sensors []*sensors.HwmonSensor + // Fans maps from HwMon index -> HwMonFan instance + Fans map[int]*fans.HwMonFan + // Sensors maps from HwMon index -> HwmonSensor instance + Sensors map[int]*sensors.HwmonSensor } func GetChips() []*HwMonController { @@ -52,10 +54,10 @@ func GetChips() []*HwMonController { platform = identifier } - fansList := GetFans(chip) - sensorsList := GetTempSensors(chip) + fanMap := GetFans(chip) + sensorMap := GetTempSensors(chip) - if len(fansList) <= 0 && len(sensorsList) <= 0 { + if len(fanMap) <= 0 && len(sensorMap) <= 0 { continue } @@ -65,8 +67,8 @@ func GetChips() []*HwMonController { Modalias: modalias, Platform: platform, Path: chip.Path, - Fans: fansList, - Sensors: sensorsList, + Fans: fanMap, + Sensors: sensorMap, } list = append(list, c) } @@ -96,9 +98,10 @@ func getDeviceType(devicePath string) string { return strings.TrimSpace(string(content)) } -func GetTempSensors(chip gosensors.Chip) []*sensors.HwmonSensor { - var sensorList []*sensors.HwmonSensor +func GetTempSensors(chip gosensors.Chip) map[int]*sensors.HwmonSensor { + result := map[int]*sensors.HwmonSensor{} + currentOutputIndex := 0 features := chip.GetFeatures() for j := 0; j < len(features); j++ { feature := features[j] @@ -110,6 +113,8 @@ func GetTempSensors(chip gosensors.Chip) []*sensors.HwmonSensor { subfeatures := feature.GetSubFeatures() if containsSubFeature(subfeatures, gosensors.SubFeatureTypeTempInput) { + currentOutputIndex++ + inputSubFeature := getSubFeature(subfeatures, gosensors.SubFeatureTypeTempInput) sensorInputPath := path.Join(chip.Path, inputSubFeature.Name) @@ -127,24 +132,22 @@ func GetTempSensors(chip gosensors.Chip) []*sensors.HwmonSensor { label := getLabel(chip.Path, inputSubFeature.Name) - sensorList = append( - sensorList, - &sensors.HwmonSensor{ - Label: label, - Index: len(sensorList) + 1, - Input: sensorInputPath, - Max: max, - Min: min, - MovingAvg: inputSubFeature.GetValue(), - }) + result[currentOutputIndex] = &sensors.HwmonSensor{ + Label: label, + Index: currentOutputIndex, + Input: sensorInputPath, + Max: max, + Min: min, + MovingAvg: inputSubFeature.GetValue(), + } } } - return sensorList + return result } -func GetFans(chip gosensors.Chip) []*fans.HwMonFan { - var fanList []*fans.HwMonFan +func GetFans(chip gosensors.Chip) map[int]*fans.HwMonFan { + var result = map[int]*fans.HwMonFan{} currentOutputIndex := 0 features := chip.GetFeatures() @@ -210,11 +213,11 @@ func GetFans(chip gosensors.Chip) []*fans.HwMonFan { MaxPwm: max, } - fanList = append(fanList, fan) + result[currentOutputIndex] = fan } } - return fanList + return result } func getSubFeature(subfeatures []gosensors.SubFeature, input gosensors.SubFeatureType) *gosensors.SubFeature { From 69a52ade0eaf8bb9e1b2ee79e8a0c3e7545fe78a Mon Sep 17 00:00:00 2001 From: Markus Ressel Date: Mon, 16 May 2022 23:56:59 +0200 Subject: [PATCH 2/2] fix hwmon device index mapping --- cmd/sensor/sensor.go | 5 ++--- internal/backend.go | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/cmd/sensor/sensor.go b/cmd/sensor/sensor.go index 5abb372..1e72741 100644 --- a/cmd/sensor/sensor.go +++ b/cmd/sensor/sensor.go @@ -67,9 +67,8 @@ func getSensor(id string) (sensors.Sensor, error) { return nil, errors.New(fmt.Sprintf("Failed to match platform regex of %s (%s) against controller platform %s", config.ID, config.HwMon.Platform, controller.Platform)) } if matched { - index := config.HwMon.Index - 1 - if len(controller.Sensors) > index { - sensor := controller.Sensors[index] + sensor, exists := controller.Sensors[config.HwMon.Index] + if exists { if len(sensor.Input) <= 0 { return nil, errors.New(fmt.Sprintf("Unable to find temp input for sensor %s", id)) } diff --git a/internal/backend.go b/internal/backend.go index ddc041f..798be10 100644 --- a/internal/backend.go +++ b/internal/backend.go @@ -196,7 +196,7 @@ func initializeSensors(controllers []*hwmon.HwMonController) { } if matched { found = true - config.HwMon.TempInput = c.Sensors[config.HwMon.Index-1].Input + config.HwMon.TempInput = c.Sensors[config.HwMon.Index].Input } } if !found {