-
Notifications
You must be signed in to change notification settings - Fork 4
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
Reactivate connection on powersave change, misc cleanup. #45
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,10 @@ const ( | |
ConnCheckFilepath = "/etc/NetworkManager/conf.d/80-viam.conf" | ||
ConnCheckContents = "[connectivity]\nuri=http://packages.viam.com/check_network_status.txt\ninterval=300\n" | ||
|
||
wifiPowerSaveFilepath = "/etc/NetworkManager/conf.d/81-viam-wifi-powersave.conf" | ||
wifiPowerSaveContentsDefault = "# This file intentionally left blank.\n" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If not set, we basically want to not set anything. Explicitly setting 0 or 1 might still override another file in conf.d that a user put in place. |
||
wifiPowerSaveContentsDisable = "[connection]\n# Explicitly disable\nwifi.powersave = 2\n" | ||
wifiPowerSaveContentsEnable = "[connection]\n# Explicitly enable\nwifi.powersave = 3\n" | ||
NetworkTypeWifi = "wifi" | ||
NetworkTypeWired = "wired" | ||
NetworkTypeHotspot = "hotspot" | ||
|
@@ -366,8 +370,8 @@ 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"` | ||
// If set, will explicitly enable or disable power save for all wifi connections managed by NetworkManager. | ||
WifiPowerSave *bool `json:"wifi_power_save"` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Double negatives are... weird. Changed to a postive. True == on == powersave. Let me know if that doesn't sound okay though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good call |
||
} | ||
|
||
// Timeout allows parsing golang-style durations (1h20m30s) OR seconds-as-float from/to json. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ func (w *Provisioning) startGRPC() error { | |
bind := PortalBindAddr + ":4772" | ||
lis, err := net.Listen("tcp", bind) | ||
if err != nil { | ||
return errw.Wrapf(err, "error listening on: %s", bind) | ||
return errw.Wrapf(err, "listening on: %s", bind) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A bunch of these scattered around. Errors no need to say the word "error" as it's implicit in the fact that it IS an error. Saw one in the new code, and cleaned up all my own uses of this elsewhere to match. |
||
} | ||
|
||
w.grpcServer = grpc.NewServer(grpc.WaitForHandlers(true)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -175,13 +175,13 @@ func (w *Provisioning) StartProvisioning(ctx context.Context, inputChan chan<- u | |
return err | ||
} | ||
if err := w.activateConnection(ctx, w.Config().HotspotInterface, w.Config().hotspotSSID); err != nil { | ||
return errw.Wrap(err, "error starting provisioning mode hotspot") | ||
return errw.Wrap(err, "starting provisioning mode hotspot") | ||
} | ||
|
||
// start portal with ssid list and known connections | ||
if err := w.startPortal(inputChan); err != nil { | ||
err = errors.Join(err, w.deactivateConnection(w.Config().HotspotInterface, w.Config().hotspotSSID)) | ||
return errw.Wrap(err, "could not start web/grpc portal") | ||
return errw.Wrap(err, "starting web/grpc portal") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another type of minor cleanup/standardization... Wrapped errors should provide context, and nothing more, as the error itself says what went wrong. |
||
} | ||
|
||
w.connState.setProvisioning(true) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,14 +21,6 @@ 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) | ||
} | ||
|
@@ -160,7 +152,7 @@ func (w *Provisioning) init(ctx context.Context) error { | |
w.updateHotspotSSID(w.cfg) | ||
|
||
if err := w.writeDNSMasq(); err != nil { | ||
return errw.Wrap(err, "error writing dnsmasq configuration") | ||
return errw.Wrap(err, "writing dnsmasq configuration") | ||
} | ||
|
||
if err := w.testConnCheck(); err != nil { | ||
|
@@ -222,8 +214,8 @@ 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")) | ||
if err := w.writeWifiPowerSave(ctx); err != nil { | ||
w.logger.Error(errw.Wrap(err, "applying wifi power save configuration")) | ||
} | ||
|
||
w.processAdditionalnetworks(ctx) | ||
|
@@ -353,7 +345,7 @@ func (w *Provisioning) processAdditionalnetworks(ctx context.Context) { | |
for _, network := range w.cfg.Networks { | ||
_, err := w.addOrUpdateConnection(network) | ||
if err != nil { | ||
w.logger.Error(errw.Wrapf(err, "error adding network %s", network.SSID)) | ||
w.logger.Error(errw.Wrapf(err, "adding network %s", network.SSID)) | ||
continue | ||
} | ||
if network.Interface != "" { | ||
|
@@ -372,27 +364,34 @@ func (w *Provisioning) updateHotspotSSID(cfg *Config) { | |
} | ||
} | ||
|
||
func (w *Provisioning) writeWifiPowerSave() error { | ||
// must be run inside dataMu lock. | ||
func (w *Provisioning) writeWifiPowerSave(ctx context.Context) error { | ||
contents := wifiPowerSaveContentsDefault | ||
|
||
if w.cfg.DisableWifiPowerSave != nil && *w.cfg.DisableWifiPowerSave { | ||
contents = wifiPowerSaveContentsDisable | ||
} | ||
|
||
if w.cfg.DisableWifiPowerSave != nil && !*w.cfg.DisableWifiPowerSave { | ||
contents = wifiPowerSaveContentsEnable | ||
if w.cfg.WifiPowerSave != nil { | ||
if *w.cfg.WifiPowerSave { | ||
contents = wifiPowerSaveContentsEnable | ||
} else { | ||
contents = wifiPowerSaveContentsDisable | ||
} | ||
} | ||
|
||
isNew, err := agent.WriteFileIfNew(wifiPowerSaveFilepath, []byte(contents)) | ||
if err != nil { | ||
return errw.Wrap(err, "error writing wifi-powersave.conf") | ||
return errw.Wrap(err, "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 errw.Wrap(err, "reloading NetworkManager after wifi-powersave.conf update") | ||
} | ||
|
||
ssid := w.netState.ActiveSSID(w.cfg.HotspotInterface) | ||
if w.connState.getConnected() && ssid != "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i guess we're checking with only do this if we're not in hotspot mode which is a good catch |
||
if err := w.activateConnection(ctx, w.cfg.HotspotInterface, ssid); err != nil { | ||
return errw.Wrapf(err, "reactivating %s to enforce powersave setting", ssid) | ||
} | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These belonged here with similar definitions for the other files.
Also, changed filename to avoid potential collisions with user-managed/written files.