| 1 |
/* |
| 2 |
Copyright 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 urfkill wraps a couple of URfkill's DBus API points to |
| 18 |
// watch for flight mode state changes. |
| 19 |
package urfkill |
| 20 |
|
| 21 |
import ( |
| 22 |
"github.com/godbus/dbus/v5" |
| 23 |
|
| 24 |
"gitlab.com/ubports/development/core/lomiri-push-service/bus" |
| 25 |
"gitlab.com/ubports/development/core/lomiri-push-service/logger" |
| 26 |
) |
| 27 |
|
| 28 |
// URfkill lives on a well-knwon bus.Address |
| 29 |
var BusAddress bus.Address = bus.Address{ |
| 30 |
Interface: "org.freedesktop.URfkill", |
| 31 |
Path: "/org/freedesktop/URfkill", |
| 32 |
Name: "org.freedesktop.URfkill", |
| 33 |
} |
| 34 |
|
| 35 |
// URfkill lives on a well-knwon bus.Address |
| 36 |
var WLANKillswitchBusAddress bus.Address = bus.Address{ |
| 37 |
Interface: "org.freedesktop.URfkill.Killswitch", |
| 38 |
Path: "/org/freedesktop/URfkill/WLAN", |
| 39 |
Name: "org.freedesktop.URfkill", |
| 40 |
} |
| 41 |
|
| 42 |
/***************************************************************** |
| 43 |
* URfkill (and its implementation) |
| 44 |
*/ |
| 45 |
|
| 46 |
type KillswitchState int32 |
| 47 |
|
| 48 |
const ( |
| 49 |
KillswitchStateUnblocked KillswitchState = iota |
| 50 |
KillswitchStateSoftBlocked |
| 51 |
KillswitchStateHardBlocked |
| 52 |
KillswitchStateMax |
| 53 |
) |
| 54 |
|
| 55 |
func (ks KillswitchState) IsValid() bool { |
| 56 |
return ks >= 0 && ks < KillswitchStateMax |
| 57 |
} |
| 58 |
|
| 59 |
type URfkill interface { |
| 60 |
// IsFlightMode returns flight mode state. |
| 61 |
IsFlightMode() bool |
| 62 |
// WatchFlightMode listens for changes to URfkill's flight |
| 63 |
// mode state, and sends them out over the channel returned. |
| 64 |
WatchFlightMode() (<-chan bool, bus.Cancellable, error) |
| 65 |
// GetWLANKillswitchState fetches and returns URfkill's |
| 66 |
// WLAN killswitch state. |
| 67 |
GetWLANKillswitchState() KillswitchState |
| 68 |
// WatchWLANKillswitchState listens for changes of URfkill's |
| 69 |
// WLAN killswtich state, and sends them out over the channel returned. |
| 70 |
WatchWLANKillswitchState() (<-chan KillswitchState, bus.Cancellable, error) |
| 71 |
} |
| 72 |
|
| 73 |
type uRfkill struct { |
| 74 |
bus bus.Endpoint |
| 75 |
wlanKillswitch bus.Endpoint |
| 76 |
log logger.Logger |
| 77 |
} |
| 78 |
|
| 79 |
// New returns a new URfkill that'll use the provided bus.Endpoints |
| 80 |
// for BusAddress and WLANKillswitchBusAddress |
| 81 |
func New(endp bus.Endpoint, wlanKillswitch bus.Endpoint, log logger.Logger) URfkill { |
| 82 |
return &uRfkill{endp, wlanKillswitch, log} |
| 83 |
} |
| 84 |
|
| 85 |
// ensure uRfkill implements URfkill |
| 86 |
var _ URfkill = &uRfkill{} |
| 87 |
|
| 88 |
/* |
| 89 |
public methods |
| 90 |
*/ |
| 91 |
|
| 92 |
func (ur *uRfkill) IsFlightMode() bool { |
| 93 |
var res bool |
| 94 |
err := ur.bus.Call("IsFlightMode", bus.Args(), &res) |
| 95 |
if err != nil { |
| 96 |
ur.log.Errorf("failed getting flight-mode state: %s", err) |
| 97 |
ur.log.Debugf("defaulting flight-mode state to false") |
| 98 |
return false |
| 99 |
} |
| 100 |
return res |
| 101 |
} |
| 102 |
|
| 103 |
func (ur *uRfkill) WatchFlightMode() (<-chan bool, bus.Cancellable, error) { |
| 104 |
ch := make(chan bool) |
| 105 |
w, err := ur.bus.WatchSignal("FlightModeChanged", |
| 106 |
func(ns ...interface{}) { |
| 107 |
stbool, ok := ns[0].(bool) |
| 108 |
if !ok { |
| 109 |
ur.log.Errorf("got weird flight-mode state: %#v", ns[0]) |
| 110 |
return |
| 111 |
} |
| 112 |
ur.log.Debugf("got flight-mode change: %v", stbool) |
| 113 |
ch <- stbool |
| 114 |
}, |
| 115 |
func() { close(ch) }) |
| 116 |
if err != nil { |
| 117 |
ur.log.Debugf("Failed to set up the watch: %s", err) |
| 118 |
return nil, nil, err |
| 119 |
} |
| 120 |
|
| 121 |
return ch, w, nil |
| 122 |
} |
| 123 |
|
| 124 |
func (ur *uRfkill) GetWLANKillswitchState() KillswitchState { |
| 125 |
got, err := ur.wlanKillswitch.GetProperty("state") |
| 126 |
if err != nil { |
| 127 |
ur.log.Errorf("failed getting WLANKillswitchState: %s", err) |
| 128 |
ur.log.Debugf("defaulting WLANKillswitchState to true") |
| 129 |
return KillswitchStateUnblocked |
| 130 |
} |
| 131 |
|
| 132 |
v, ok := got.(int32) |
| 133 |
if !ok { |
| 134 |
ur.log.Errorf("got weird WLANKillswitchState: %#v", got) |
| 135 |
return KillswitchStateUnblocked |
| 136 |
} |
| 137 |
|
| 138 |
return KillswitchState(v) |
| 139 |
} |
| 140 |
|
| 141 |
func (ur *uRfkill) WatchWLANKillswitchState() (<-chan KillswitchState, bus.Cancellable, error) { |
| 142 |
ch := make(chan KillswitchState) |
| 143 |
w, err := ur.wlanKillswitch.WatchProperties( |
| 144 |
func(changed map[string]dbus.Variant, invalidated []string) { |
| 145 |
v, ok := changed["state"] |
| 146 |
if !ok { |
| 147 |
return |
| 148 |
} |
| 149 |
var st KillswitchState |
| 150 |
if v.Store(&st) != nil || !st.IsValid() { |
| 151 |
ur.log.Errorf("got weird WLANKillswitchState via PropertiesChanged: %#v", v) |
| 152 |
return |
| 153 |
} |
| 154 |
ur.log.Debugf("got WLANKillswitchState change: %v", st) |
| 155 |
ch <- st |
| 156 |
}, func() { close(ch) }) |
| 157 |
if err != nil { |
| 158 |
ur.log.Debugf("failed to set up the watch: %s", err) |
| 159 |
return nil, nil, err |
| 160 |
} |
| 161 |
|
| 162 |
return ch, w, nil |
| 163 |
} |