Skip to content

Commit

Permalink
Add disabale wifi power save option
Browse files Browse the repository at this point in the history
  • Loading branch information
ale7714 authored and Otterverse committed Nov 20, 2024
1 parent 29a0da3 commit 5e7f009
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
3 changes: 3 additions & 0 deletions subsystems/provisioning/definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,9 @@ type Config struct {

// Computed from HotspotPrefix and Manufacturer
hotspotSSID string

// When true, it will disable power save for all wifi connections managed by NetworkManager.
DisableWifiPowerSave *bool `json:"disable_wifi_power_save"`
}

// Timeout allows parsing golang-style durations (1h20m30s) OR seconds-as-float from/to json.
Expand Down
39 changes: 39 additions & 0 deletions subsystems/provisioning/provisioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ import (
"google.golang.org/grpc"
)

const (
wifiPowerSaveFilepath = "/etc/NetworkManager/conf.d/wifi-powersave.conf"

wifiPowerSaveContentsDefault = "[connection]\n# Do not modify existing setting\nwifi.powersave = 1"
wifiPowerSaveContentsDisable = "[connection]\n# Explicitly disable\nwifi.powersave = 2"
wifiPowerSaveContentsEnable = "[connection]\n# Explicitly enable\nwifi.powersave = 3"
)

func init() {
registry.Register(SubsysName, NewProvisioning)
}
Expand Down Expand Up @@ -230,6 +238,10 @@ func (w *Provisioning) Start(ctx context.Context) error {
}
}

if err := w.writeWifiPowerSave(); err != nil {
w.logger.Error(errw.Wrap(err, "error applying wifi power save configuration"))
}

w.processAdditionalnetworks(ctx)

if err := w.checkOnline(true); err != nil {
Expand Down Expand Up @@ -375,3 +387,30 @@ func (w *Provisioning) updateHotspotSSID(cfg *Config) {
cfg.hotspotSSID = cfg.hotspotSSID[:32]
}
}

func (w *Provisioning) writeWifiPowerSave() error {
contents := wifiPowerSaveContentsDefault

if w.cfg.DisableWifiPowerSave != nil && *w.cfg.DisableWifiPowerSave {
contents = wifiPowerSaveContentsDisable
}

if w.cfg.DisableWifiPowerSave != nil && !*w.cfg.DisableWifiPowerSave {
contents = wifiPowerSaveContentsEnable
}

isNew, err := agent.WriteFileIfNew(wifiPowerSaveFilepath, []byte(contents))
if err != nil {
return errw.Wrap(err, "error writing wifi-powersave.conf")
}

if isNew {
w.logger.Infof("Updated %s to: %q", wifiPowerSaveFilepath, contents)
// Reload NetworkManager to apply changes
if err := w.nm.Reload(0); err != nil {
return errw.Wrap(err, "error reloading NetworkManager after wifi-powersave.conf update")
}
}

return nil
}

0 comments on commit 5e7f009

Please sign in to comment.