| 1 |
/* |
| 2 |
Copyright 2013-2015 Canonical Ltd. |
| 3 |
|
| 4 |
This program is free software: you can redistribute it and/or modify it |
| 5 |
under the terms of the GNU General Public License version 3, as published |
| 6 |
by the Free Software Foundation. |
| 7 |
|
| 8 |
This program is distributed in the hope that it will be useful, but |
| 9 |
WITHOUT ANY WARRANTY; without even the implied warranties of |
| 10 |
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 11 |
PURPOSE. See the GNU General Public License for more details. |
| 12 |
|
| 13 |
You should have received a copy of the GNU General Public License along |
| 14 |
with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
// Package networkmanager wraps a couple of NetworkManager's DBus API |
| 18 |
// points: the org.freedesktop.NetworkManager.state call, and |
| 19 |
// listening for the StateChange signal, similarly for the primary |
| 20 |
// connection and wireless enabled state. |
| 21 |
package networkmanager |
| 22 |
|
| 23 |
import ( |
| 24 |
"github.com/godbus/dbus/v5" |
| 25 |
|
| 26 |
"gitlab.com/ubports/development/core/lomiri-push-service/bus" |
| 27 |
"gitlab.com/ubports/development/core/lomiri-push-service/logger" |
| 28 |
) |
| 29 |
|
| 30 |
// NetworkManager lives on a well-knwon bus.Address |
| 31 |
var BusAddress bus.Address = bus.Address{ |
| 32 |
Interface: "org.freedesktop.NetworkManager", |
| 33 |
Path: "/org/freedesktop/NetworkManager", |
| 34 |
Name: "org.freedesktop.NetworkManager", |
| 35 |
} |
| 36 |
|
| 37 |
/***************************************************************** |
| 38 |
* NetworkManager (and its implementation) |
| 39 |
*/ |
| 40 |
|
| 41 |
type NetworkManager interface { |
| 42 |
// GetState fetches and returns NetworkManager's current state |
| 43 |
GetState() State |
| 44 |
// WatchState listens for changes to NetworkManager's state, and sends |
| 45 |
// them out over the channel returned. |
| 46 |
WatchState() (<-chan State, bus.Cancellable, error) |
| 47 |
// GetPrimaryConnection fetches and returns NetworkManager's current |
| 48 |
// primary connection. |
| 49 |
GetPrimaryConnection() string |
| 50 |
// WatchPrimaryConnection listens for changes of NetworkManager's |
| 51 |
// Primary Connection, and sends them out over the channel returned. |
| 52 |
WatchPrimaryConnection() (<-chan string, bus.Cancellable, error) |
| 53 |
// GetWirelessEnabled fetches and returns NetworkManager's |
| 54 |
// wireless state. |
| 55 |
GetWirelessEnabled() bool |
| 56 |
// WatchWirelessEnabled listens for changes of NetworkManager's |
| 57 |
// wireless state, and sends them out over the channel returned. |
| 58 |
WatchWirelessEnabled() (<-chan bool, bus.Cancellable, error) |
| 59 |
} |
| 60 |
|
| 61 |
type networkManager struct { |
| 62 |
bus bus.Endpoint |
| 63 |
log logger.Logger |
| 64 |
} |
| 65 |
|
| 66 |
// New returns a new NetworkManager that'll use the provided bus.Endpoint |
| 67 |
func New(endp bus.Endpoint, log logger.Logger) NetworkManager { |
| 68 |
return &networkManager{endp, log} |
| 69 |
} |
| 70 |
|
| 71 |
// ensure networkManager implements NetworkManager |
| 72 |
var _ NetworkManager = &networkManager{} |
| 73 |
|
| 74 |
/* |
| 75 |
public methods |
| 76 |
*/ |
| 77 |
|
| 78 |
func (nm *networkManager) GetState() State { |
| 79 |
s, err := nm.bus.GetProperty("State") |
| 80 |
if err != nil { |
| 81 |
nm.log.Errorf("failed getting current state: %s", err) |
| 82 |
nm.log.Debugf("defaulting state to Unknown") |
| 83 |
return Unknown |
| 84 |
} |
| 85 |
|
| 86 |
v, ok := s.(uint32) |
| 87 |
if !ok { |
| 88 |
nm.log.Errorf("got weird state: %#v", s) |
| 89 |
return Unknown |
| 90 |
} |
| 91 |
|
| 92 |
return State(v) |
| 93 |
} |
| 94 |
|
| 95 |
func (nm *networkManager) WatchState() (<-chan State, bus.Cancellable, error) { |
| 96 |
ch := make(chan State) |
| 97 |
w, err := nm.bus.WatchSignal("StateChanged", |
| 98 |
func(ns ...interface{}) { |
| 99 |
stint, ok := ns[0].(uint32) |
| 100 |
if !ok { |
| 101 |
nm.log.Errorf("got weird state: %#v", ns[0]) |
| 102 |
return |
| 103 |
} |
| 104 |
st := State(stint) |
| 105 |
nm.log.Debugf("got state: %s", st) |
| 106 |
ch <- State(stint) |
| 107 |
}, |
| 108 |
func() { close(ch) }) |
| 109 |
if err != nil { |
| 110 |
nm.log.Debugf("Failed to set up the watch: %s", err) |
| 111 |
return nil, nil, err |
| 112 |
} |
| 113 |
|
| 114 |
return ch, w, nil |
| 115 |
} |
| 116 |
|
| 117 |
func (nm *networkManager) GetPrimaryConnection() string { |
| 118 |
got, err := nm.bus.GetProperty("PrimaryConnection") |
| 119 |
if err != nil { |
| 120 |
nm.log.Errorf("failed getting current PrimaryConnection: %s", err) |
| 121 |
nm.log.Debugf("defaulting PrimaryConnection to empty") |
| 122 |
return "" |
| 123 |
} |
| 124 |
|
| 125 |
v, ok := got.(dbus.ObjectPath) |
| 126 |
if !ok { |
| 127 |
nm.log.Errorf("got weird PrimaryConnection: %#v", got) |
| 128 |
return "" |
| 129 |
} |
| 130 |
|
| 131 |
return string(v) |
| 132 |
} |
| 133 |
|
| 134 |
func (nm *networkManager) WatchPrimaryConnection() (<-chan string, bus.Cancellable, error) { |
| 135 |
ch := make(chan string) |
| 136 |
w, err := nm.bus.WatchProperties( |
| 137 |
func(changed map[string]dbus.Variant, invalidated []string) { |
| 138 |
v, ok := changed["PrimaryConnection"] |
| 139 |
if !ok { |
| 140 |
return |
| 141 |
} |
| 142 |
con, ok := v.Value().(dbus.ObjectPath) |
| 143 |
if !ok { |
| 144 |
nm.log.Errorf("got weird PrimaryConnection via PropertiesChanged: %#v", v) |
| 145 |
return |
| 146 |
} |
| 147 |
nm.log.Debugf("got PrimaryConnection change: %s", con) |
| 148 |
ch <- string(con) |
| 149 |
}, func() { close(ch) }) |
| 150 |
if err != nil { |
| 151 |
nm.log.Debugf("failed to set up the watch: %s", err) |
| 152 |
return nil, nil, err |
| 153 |
} |
| 154 |
|
| 155 |
return ch, w, nil |
| 156 |
} |
| 157 |
|
| 158 |
func (nm *networkManager) GetWirelessEnabled() bool { |
| 159 |
got, err := nm.bus.GetProperty("WirelessEnabled") |
| 160 |
if err != nil { |
| 161 |
nm.log.Errorf("failed getting WirelessEnabled: %s", err) |
| 162 |
nm.log.Debugf("defaulting WirelessEnabled to true") |
| 163 |
return true |
| 164 |
} |
| 165 |
|
| 166 |
v, ok := got.(bool) |
| 167 |
if !ok { |
| 168 |
nm.log.Errorf("got weird WirelessEnabled: %#v", got) |
| 169 |
return true |
| 170 |
} |
| 171 |
|
| 172 |
return v |
| 173 |
} |
| 174 |
|
| 175 |
func (nm *networkManager) WatchWirelessEnabled() (<-chan bool, bus.Cancellable, error) { |
| 176 |
ch := make(chan bool) |
| 177 |
w, err := nm.bus.WatchSignal("PropertiesChanged", |
| 178 |
func(ppsi ...interface{}) { |
| 179 |
pps, ok := ppsi[0].(map[string]dbus.Variant) |
| 180 |
if !ok { |
| 181 |
nm.log.Errorf("got weird PropertiesChanged: %#v", ppsi[0]) |
| 182 |
return |
| 183 |
} |
| 184 |
v, ok := pps["WirelessEnabled"] |
| 185 |
if !ok { |
| 186 |
return |
| 187 |
} |
| 188 |
en, ok := v.Value().(bool) |
| 189 |
if !ok { |
| 190 |
nm.log.Errorf("got weird WirelessEnabled via PropertiesChanged: %#v", v) |
| 191 |
return |
| 192 |
} |
| 193 |
nm.log.Debugf("got WirelessEnabled change: %v", en) |
| 194 |
ch <- en |
| 195 |
}, func() { close(ch) }) |
| 196 |
if err != nil { |
| 197 |
nm.log.Debugf("failed to set up the watch: %s", err) |
| 198 |
return nil, nil, err |
| 199 |
} |
| 200 |
|
| 201 |
return ch, w, nil |
| 202 |
} |