Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix hwmon device index mapping #124

Merged
merged 2 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions cmd/curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 20 additions & 5 deletions cmd/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/spf13/cobra"
"github.com/tomlazar/table"
"path/filepath"
"sort"
"strconv"
)

Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions cmd/fan/fan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions cmd/sensor/sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
8 changes: 4 additions & 4 deletions internal/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
51 changes: 27 additions & 24 deletions internal/hwmon/hwmon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

Expand All @@ -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)
}
Expand Down Expand Up @@ -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]
Expand All @@ -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)

Expand All @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down