Merge lp:~pedronis/ubuntu-push/flight-mode-n-wireless-tracking into lp:ubuntu-push/automatic

Proposed by Samuele Pedroni
Status: Merged
Approved by: Samuele Pedroni
Approved revision: 390
Merged at revision: 388
Proposed branch: lp:~pedronis/ubuntu-push/flight-mode-n-wireless-tracking
Merge into: lp:ubuntu-push/automatic
Diff against target: 491 lines (+389/-14)
5 files modified
bus/networkmanager/networkmanager.go (+65/-12)
bus/networkmanager/networkmanager_test.go (+109/-1)
bus/urfkill/urfkill.go (+94/-0)
bus/urfkill/urfkill.go_test.go (+121/-0)
client/client.go (+0/-1)
To merge this branch: bzr merge lp:~pedronis/ubuntu-push/flight-mode-n-wireless-tracking
Reviewer Review Type Date Requested Status
John Lenton (community) Approve
Review via email: mp+254616@code.launchpad.net

Commit message

URfkill dbus api to get and warch flight mode state, get and watch WirelessEnabled state from NetworkManager

Description of the change

URfkill dbus api to get and warch flight mode state, get and watch WirelessEnabled state from NetworkManager

To post a comment you must log in.
390. By Samuele Pedroni

Merged ubuntu-push into flight-mode-n-wireless-tracking.

Revision history for this message
John Lenton (chipaca) wrote :

Good.

Man, we need to change our dbus implementation already.

Thank you for nuking FOO, I always forgot and it's annoying.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'bus/networkmanager/networkmanager.go'
2--- bus/networkmanager/networkmanager.go 2015-02-26 19:36:57 +0000
3+++ bus/networkmanager/networkmanager.go 2015-03-30 20:47:18 +0000
4@@ -1,5 +1,5 @@
5 /*
6- Copyright 2013-2014 Canonical Ltd.
7+ Copyright 2013-2015 Canonical Ltd.
8
9 This program is free software: you can redistribute it and/or modify it
10 under the terms of the GNU General Public License version 3, as published
11@@ -14,9 +14,10 @@
12 with this program. If not, see <http://www.gnu.org/licenses/>.
13 */
14
15-// Package networkmanager wraps a couple of NetworkManager's DBus API points:
16-// the org.freedesktop.NetworkManager.state call, and listening for the
17-// StateChange signal.
18+// Package networkmanager wraps a couple of NetworkManager's DBus API
19+// points: the org.freedesktop.NetworkManager.state call, and
20+// listening for the StateChange signal, similarly for the primary
21+// connection and wireless enabled state.
22 package networkmanager
23
24 import (
25@@ -47,8 +48,14 @@
26 // primary connection.
27 GetPrimaryConnection() string
28 // WatchPrimaryConnection listens for changes of NetworkManager's
29- // Primary Connection, and sends it out over the channel returned.
30+ // Primary Connection, and sends them out over the channel returned.
31 WatchPrimaryConnection() (<-chan string, bus.Cancellable, error)
32+ // GetWirelessEnabled fetches and returns NetworkManager's
33+ // wireless state.
34+ GetWirelessEnabled() bool
35+ // WatchWirelessEnabled listens for changes of NetworkManager's
36+ // wireless state, and sends them out over the channel returned.
37+ WatchWirelessEnabled() (<-chan bool, bus.Cancellable, error)
38 }
39
40 type networkManager struct {
41@@ -71,7 +78,7 @@
42 func (nm *networkManager) GetState() State {
43 s, err := nm.bus.GetProperty("state")
44 if err != nil {
45- nm.log.Errorf("failed gettting current state: %s", err)
46+ nm.log.Errorf("failed getting current state: %s", err)
47 nm.log.Debugf("defaulting state to Unknown")
48 return Unknown
49 }
50@@ -108,16 +115,16 @@
51 }
52
53 func (nm *networkManager) GetPrimaryConnection() string {
54- s, err := nm.bus.GetProperty("PrimaryConnection")
55+ got, err := nm.bus.GetProperty("PrimaryConnection")
56 if err != nil {
57- nm.log.Errorf("failed gettting current primary connection: %s", err)
58- nm.log.Debugf("defaulting primary connection to empty")
59+ nm.log.Errorf("failed getting current PrimaryConnection: %s", err)
60+ nm.log.Debugf("defaulting PrimaryConnection to empty")
61 return ""
62 }
63
64- v, ok := s.(dbus.ObjectPath)
65+ v, ok := got.(dbus.ObjectPath)
66 if !ok {
67- nm.log.Errorf("got weird PrimaryConnection: %#v", s)
68+ nm.log.Errorf("got weird PrimaryConnection: %#v", got)
69 return ""
70 }
71
72@@ -142,7 +149,7 @@
73 nm.log.Errorf("got weird PrimaryConnection via PropertiesChanged: %#v", v)
74 return
75 }
76- nm.log.Debugf("got primary connection: %s", con)
77+ nm.log.Debugf("got PrimaryConnection change: %s", con)
78 ch <- string(con)
79 }, func() { close(ch) })
80 if err != nil {
81@@ -152,3 +159,49 @@
82
83 return ch, w, nil
84 }
85+
86+func (nm *networkManager) GetWirelessEnabled() bool {
87+ got, err := nm.bus.GetProperty("WirelessEnabled")
88+ if err != nil {
89+ nm.log.Errorf("failed getting WirelessEnabled: %s", err)
90+ nm.log.Debugf("defaulting WirelessEnabled to true")
91+ return true
92+ }
93+
94+ v, ok := got.(bool)
95+ if !ok {
96+ nm.log.Errorf("got weird WirelessEnabled: %#v", got)
97+ return true
98+ }
99+
100+ return v
101+}
102+
103+func (nm *networkManager) WatchWirelessEnabled() (<-chan bool, bus.Cancellable, error) {
104+ ch := make(chan bool)
105+ w, err := nm.bus.WatchSignal("PropertiesChanged",
106+ func(ppsi ...interface{}) {
107+ pps, ok := ppsi[0].(map[string]dbus.Variant)
108+ if !ok {
109+ nm.log.Errorf("got weird PropertiesChanged: %#v", ppsi[0])
110+ return
111+ }
112+ v, ok := pps["WirelessEnabled"]
113+ if !ok {
114+ return
115+ }
116+ en, ok := v.Value.(bool)
117+ if !ok {
118+ nm.log.Errorf("got weird WirelessEnabled via PropertiesChanged: %#v", v)
119+ return
120+ }
121+ nm.log.Debugf("got WirelessEnabled change: %v", en)
122+ ch <- en
123+ }, func() { close(ch) })
124+ if err != nil {
125+ nm.log.Debugf("failed to set up the watch: %s", err)
126+ return nil, nil, err
127+ }
128+
129+ return ch, w, nil
130+}
131
132=== modified file 'bus/networkmanager/networkmanager_test.go'
133--- bus/networkmanager/networkmanager_test.go 2015-02-26 19:36:57 +0000
134+++ bus/networkmanager/networkmanager_test.go 2015-03-30 20:47:18 +0000
135@@ -1,5 +1,5 @@
136 /*
137- Copyright 2013-2014 Canonical Ltd.
138+ Copyright 2013-2015 Canonical Ltd.
139
140 This program is free software: you can redistribute it and/or modify it
141 under the terms of the GNU General Public License version 3, as published
142@@ -232,3 +232,111 @@
143 c.Check(ok, Equals, true)
144 c.Check(v, Equals, "42")
145 }
146+
147+// GetWirelessEnabled returns the right state when everything works
148+func (s *NMSuite) TestGetWirelessEnabled(c *C) {
149+ nm := New(testingbus.NewTestingEndpoint(nil, condition.Work(true), false), s.log)
150+ en := nm.GetWirelessEnabled()
151+ c.Check(en, Equals, false)
152+}
153+
154+// GetWirelessEnabled returns the right state when dbus fails
155+func (s *NMSuite) TestGetWirelessEnabledFail(c *C) {
156+ nm := New(testingbus.NewTestingEndpoint(nil, condition.Work(false)), s.log)
157+ en := nm.GetWirelessEnabled()
158+ c.Check(en, Equals, true)
159+}
160+
161+// GetWirelessEnabled returns the right state when dbus works but delivers rubbish values
162+func (s *NMSuite) TestGetWirelessEnabledRubbishValues(c *C) {
163+ nm := New(testingbus.NewTestingEndpoint(nil, condition.Work(true), "broken"), s.log)
164+ en := nm.GetWirelessEnabled()
165+ c.Check(en, Equals, true)
166+}
167+
168+// GetWirelessEnabled returns the right state when dbus works but delivers a rubbish structure
169+func (s *NMSuite) TestGetWirelessEnabledRubbishStructure(c *C) {
170+ nm := New(testingbus.NewMultiValuedTestingEndpoint(nil, condition.Work(true), []interface{}{}), s.log)
171+ en := nm.GetWirelessEnabled()
172+ c.Check(en, Equals, true)
173+}
174+
175+func mkWirelessEnMap(en bool) map[string]dbus.Variant {
176+ m := make(map[string]dbus.Variant)
177+ m["WirelessEnabled"] = dbus.Variant{en}
178+ return m
179+}
180+
181+// WatchWirelessEnabled sends a stream of wireless enabled states over the channel
182+func (s *NMSuite) TestWatchWirelessEnabled(c *C) {
183+ tc := testingbus.NewTestingEndpoint(nil, condition.Work(true),
184+ mkWirelessEnMap(true),
185+ mkWirelessEnMap(false),
186+ mkWirelessEnMap(true),
187+ )
188+ nm := New(tc, s.log)
189+ ch, w, err := nm.WatchWirelessEnabled()
190+ c.Assert(err, IsNil)
191+ defer w.Cancel()
192+ l := []bool{<-ch, <-ch, <-ch}
193+ c.Check(l, DeepEquals, []bool{true, false, true})
194+}
195+
196+// WatchWirelessEnabled returns on error if the dbus call fails
197+func (s *NMSuite) TestWatchWirelessEnabledFails(c *C) {
198+ nm := New(testingbus.NewTestingEndpoint(nil, condition.Work(false)), s.log)
199+ _, _, err := nm.WatchWirelessEnabled()
200+ c.Check(err, NotNil)
201+}
202+
203+// WatchWirelessEnabled calls close on its channel when the watch bails
204+func (s *NMSuite) TestWatchWirelessEnabledClosesOnWatchBail(c *C) {
205+ tc := testingbus.NewTestingEndpoint(nil, condition.Work(true))
206+ nm := New(tc, s.log)
207+ ch, w, err := nm.WatchWirelessEnabled()
208+ c.Assert(err, IsNil)
209+ defer w.Cancel()
210+ _, ok := <-ch
211+ c.Check(ok, Equals, false)
212+}
213+
214+// WatchWirelessEnabled survives rubbish values
215+func (s *NMSuite) TestWatchWirelessEnabledSurvivesRubbishValues(c *C) {
216+ tc := testingbus.NewTestingEndpoint(nil, condition.Work(true), "gorp")
217+ nm := New(tc, s.log)
218+ ch, w, err := nm.WatchWirelessEnabled()
219+ c.Assert(err, IsNil)
220+ defer w.Cancel()
221+ _, ok := <-ch
222+ c.Check(ok, Equals, false)
223+}
224+
225+// WatchWirelessEnabled ignores non-WirelessEnabled PropertyChanged
226+func (s *NMSuite) TestWatchWirelessEnabledIgnoresIrrelephant(c *C) {
227+ tc := testingbus.NewTestingEndpoint(nil, condition.Work(true),
228+ map[string]dbus.Variant{"foo": dbus.Variant{}},
229+ map[string]dbus.Variant{"WirelessEnabled": dbus.Variant{true}},
230+ )
231+ nm := New(tc, s.log)
232+ ch, w, err := nm.WatchWirelessEnabled()
233+ c.Assert(err, IsNil)
234+ defer w.Cancel()
235+ v, ok := <-ch
236+ c.Check(ok, Equals, true)
237+ c.Check(v, Equals, true)
238+}
239+
240+// WatchWirelessEnabled ignores rubbish WirelessEnabled
241+func (s *NMSuite) TestWatchWirelessEnabledIgnoresRubbishValues(c *C) {
242+ tc := testingbus.NewTestingEndpoint(nil, condition.Work(true),
243+ map[string]dbus.Variant{"WirelessEnabled": dbus.Variant{-12}},
244+ map[string]dbus.Variant{"WirelessEnabled": dbus.Variant{false}},
245+ )
246+ nm := New(tc, s.log)
247+ ch, w, err := nm.WatchWirelessEnabled()
248+ c.Assert(err, IsNil)
249+ defer w.Cancel()
250+ v, ok := <-ch
251+ c.Check(ok, Equals, true)
252+ c.Check(v, Equals, false)
253+}
254
255=== added directory 'bus/urfkill'
256=== added file 'bus/urfkill/urfkill.go'
257--- bus/urfkill/urfkill.go 1970-01-01 00:00:00 +0000
258+++ bus/urfkill/urfkill.go 2015-03-30 20:47:18 +0000
259@@ -0,0 +1,94 @@
260+/*
261+ Copyright 2015 Canonical Ltd.
262+
263+ This program is free software: you can redistribute it and/or modify it
264+ under the terms of the GNU General Public License version 3, as published
265+ by the Free Software Foundation.
266+
267+ This program is distributed in the hope that it will be useful, but
268+ WITHOUT ANY WARRANTY; without even the implied warranties of
269+ MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
270+ PURPOSE. See the GNU General Public License for more details.
271+
272+ You should have received a copy of the GNU General Public License along
273+ with this program. If not, see <http://www.gnu.org/licenses/>.
274+*/
275+
276+// Package urfkill wraps a couple of URfkill's DBus API points to
277+// watch for flight mode state changes.
278+package urfkill
279+
280+import (
281+ //"launchpad.net/go-dbus/v1"
282+
283+ "launchpad.net/ubuntu-push/bus"
284+ "launchpad.net/ubuntu-push/logger"
285+)
286+
287+// URfkill lives on a well-knwon bus.Address
288+var BusAddress bus.Address = bus.Address{
289+ Interface: "org.freedesktop.URfkill",
290+ Path: "/org/freedesktop/URfkill",
291+ Name: "org.freedesktop.URfkill",
292+}
293+
294+/*****************************************************************
295+ * URfkill (and its implementation)
296+ */
297+
298+type URfkill interface {
299+ // IsFlightMode returns flight mode state.
300+ IsFlightMode() bool
301+ // WatchFlightMode listens for changes to URfkill's flight
302+ // mode state, and sends them out over the channel returned.
303+ WatchFlightMode() (<-chan bool, bus.Cancellable, error)
304+}
305+
306+type uRfkill struct {
307+ bus bus.Endpoint
308+ log logger.Logger
309+}
310+
311+// New returns a new URfkill that'll use the provided bus.Endpoint
312+func New(endp bus.Endpoint, log logger.Logger) URfkill {
313+ return &uRfkill{endp, log}
314+}
315+
316+// ensure uRfkill implements URfkill
317+var _ URfkill = &uRfkill{}
318+
319+/*
320+ public methods
321+*/
322+
323+func (ur *uRfkill) IsFlightMode() bool {
324+ var res bool
325+ err := ur.bus.Call("IsFlightMode", bus.Args(), &res)
326+ if err != nil {
327+ ur.log.Errorf("failed getting flight-mode state: %s", err)
328+ ur.log.Debugf("defaulting flight-mode state to false")
329+ return false
330+ }
331+ return res
332+}
333+
334+func (ur *uRfkill) WatchFlightMode() (<-chan bool, bus.Cancellable, error) {
335+ ch := make(chan bool)
336+ w, err := ur.bus.WatchSignal("FlightModeChanged",
337+ func(ns ...interface{}) {
338+ stbool, ok := ns[0].(bool)
339+ if !ok {
340+ ur.log.Errorf("got weird flight-mode state: %#v", ns[0])
341+ return
342+ }
343+ ur.log.Debugf("got flight-mode change: %v", stbool)
344+ ch <- stbool
345+ },
346+ func() { close(ch) })
347+ if err != nil {
348+ ur.log.Debugf("Failed to set up the watch: %s", err)
349+ return nil, nil, err
350+ }
351+
352+ return ch, w, nil
353+}
354
355=== added file 'bus/urfkill/urfkill.go_test.go'
356--- bus/urfkill/urfkill.go_test.go 1970-01-01 00:00:00 +0000
357+++ bus/urfkill/urfkill.go_test.go 2015-03-30 20:47:18 +0000
358@@ -0,0 +1,121 @@
359+/*
360+ Copyright 2015 Canonical Ltd.
361+
362+ This program is free software: you can redistribute it and/or modify it
363+ under the terms of the GNU General Public License version 3, as published
364+ by the Free Software Foundation.
365+
366+ This program is distributed in the hope that it will be useful, but
367+ WITHOUT ANY WARRANTY; without even the implied warranties of
368+ MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
369+ PURPOSE. See the GNU General Public License for more details.
370+
371+ You should have received a copy of the GNU General Public License along
372+ with this program. If not, see <http://www.gnu.org/licenses/>.
373+*/
374+
375+package urfkill
376+
377+import (
378+ "testing"
379+
380+ //"launchpad.net/go-dbus/v1"
381+ . "launchpad.net/gocheck"
382+
383+ testingbus "launchpad.net/ubuntu-push/bus/testing"
384+ "launchpad.net/ubuntu-push/logger"
385+ helpers "launchpad.net/ubuntu-push/testing"
386+ "launchpad.net/ubuntu-push/testing/condition"
387+)
388+
389+// hook up gocheck
390+func Test(t *testing.T) { TestingT(t) }
391+
392+type URSuite struct {
393+ log logger.Logger
394+}
395+
396+var _ = Suite(&URSuite{})
397+
398+func (s *URSuite) SetUpTest(c *C) {
399+ s.log = helpers.NewTestLogger(c, "debug")
400+}
401+
402+func (s *URSuite) TestNew(c *C) {
403+ ur := New(testingbus.NewTestingEndpoint(nil, condition.Work(true)), s.log)
404+ c.Check(ur, NotNil)
405+}
406+
407+// IsFlightMode returns the right state when everything works
408+func (s *URSuite) TestIsFlightMode(c *C) {
409+ endp := testingbus.NewTestingEndpoint(nil, condition.Work(true), true)
410+ ur := New(endp, s.log)
411+ state := ur.IsFlightMode()
412+ c.Check(state, Equals, true)
413+ callArgs := testingbus.GetCallArgs(endp)
414+ c.Assert(callArgs, HasLen, 1)
415+ c.Assert(callArgs[0].Member, Equals, "IsFlightMode")
416+ c.Assert(callArgs[0].Args, HasLen, 0)
417+}
418+
419+// IsFlightMode returns the right state when dbus fails
420+func (s *URSuite) TestIsFlightModeFail(c *C) {
421+ ur := New(testingbus.NewTestingEndpoint(nil, condition.Work(false)), s.log)
422+ state := ur.IsFlightMode()
423+ c.Check(state, Equals, false)
424+}
425+
426+// IsFlightMode returns the right state when dbus works but delivers
427+// rubbish values
428+func (s *URSuite) TestIsFlightModeRubbishValues(c *C) {
429+ ur := New(testingbus.NewTestingEndpoint(nil, condition.Work(true), "broken"), s.log)
430+ state := ur.IsFlightMode()
431+ c.Check(state, Equals, false)
432+}
433+
434+// IsFlightMode returns the right state when dbus works but delivers a rubbish structure
435+func (s *URSuite) TestIsFlightModeRubbishStructure(c *C) {
436+ ur := New(testingbus.NewMultiValuedTestingEndpoint(nil, condition.Work(true), []interface{}{}), s.log)
437+ state := ur.IsFlightMode()
438+ c.Check(state, Equals, false)
439+}
440+
441+// WatchFightMode sends a stream of states over the channel
442+func (s *URSuite) TestWatchFlightMode(c *C) {
443+ tc := testingbus.NewTestingEndpoint(nil, condition.Work(true), false, true, false)
444+ ur := New(tc, s.log)
445+ ch, w, err := ur.WatchFlightMode()
446+ c.Assert(err, IsNil)
447+ defer w.Cancel()
448+ l := []bool{<-ch, <-ch, <-ch}
449+ c.Check(l, DeepEquals, []bool{false, true, false})
450+}
451+
452+// WatchFlightMode returns on error if the dbus call fails
453+func (s *URSuite) TestWatchFlightModeFails(c *C) {
454+ ur := New(testingbus.NewTestingEndpoint(nil, condition.Work(false)), s.log)
455+ _, _, err := ur.WatchFlightMode()
456+ c.Check(err, NotNil)
457+}
458+
459+// WatchFlightMode calls close on its channel when the watch bails
460+func (s *URSuite) TestWatchFlightModeClosesOnWatchBail(c *C) {
461+ tc := testingbus.NewTestingEndpoint(nil, condition.Work(true))
462+ ur := New(tc, s.log)
463+ ch, w, err := ur.WatchFlightMode()
464+ c.Assert(err, IsNil)
465+ defer w.Cancel()
466+ _, ok := <-ch
467+ c.Check(ok, Equals, false)
468+}
469+
470+// WatchFlightMode survives rubbish values
471+func (s *URSuite) TestWatchFlightModeSurvivesRubbishValues(c *C) {
472+ tc := testingbus.NewTestingEndpoint(nil, condition.Work(true), "gorp")
473+ ur := New(tc, s.log)
474+ ch, w, err := ur.WatchFlightMode()
475+ c.Assert(err, IsNil)
476+ defer w.Cancel()
477+ _, ok := <-ch
478+ c.Check(ok, Equals, false)
479+}
480
481=== modified file 'client/client.go'
482--- client/client.go 2015-03-30 19:21:49 +0000
483+++ client/client.go 2015-03-30 20:47:18 +0000
484@@ -287,7 +287,6 @@
485
486 // takeTheBus starts the connection(s) to D-Bus and sets up associated event channels
487 func (client *PushClient) takeTheBus() error {
488- fmt.Println("FOO")
489 cs := connectivity.New(client.connectivityEndp,
490 client.config.ConnectivityConfig, client.log)
491 go cs.Track(client.connCh)

Subscribers

People subscribed via source and target branches