| 1 |
/* |
| 2 |
Copyright 2014 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 powerd is an interface to powerd via dbus. |
| 18 |
package powerd |
| 19 |
|
| 20 |
import ( |
| 21 |
"errors" |
| 22 |
"time" |
| 23 |
|
| 24 |
"gitlab.com/ubports/development/core/lomiri-push-service/bus" |
| 25 |
"gitlab.com/ubports/development/core/lomiri-push-service/logger" |
| 26 |
) |
| 27 |
|
| 28 |
// powerd lives on a well-known bus.Address |
| 29 |
var BusAddress bus.Address = bus.Address{ |
| 30 |
Interface: "com.lomiri.Repowerd", |
| 31 |
Path: "/com/lomiri/Repowerd", |
| 32 |
Name: "com.lomiri.Repowerd", |
| 33 |
} |
| 34 |
|
| 35 |
// Powerd exposes a subset of powerd |
| 36 |
type Powerd interface { |
| 37 |
RequestWakeup(name string, wakeupTime time.Time) (string, error) |
| 38 |
ClearWakeup(cookie string) error |
| 39 |
WatchWakeups() (<-chan bool, error) |
| 40 |
RequestWakelock(name string) (string, error) |
| 41 |
ClearWakelock(cookie string) error |
| 42 |
} |
| 43 |
|
| 44 |
type powerd struct { |
| 45 |
endp bus.Endpoint |
| 46 |
log logger.Logger |
| 47 |
} |
| 48 |
|
| 49 |
var ( |
| 50 |
ErrUnconfigured = errors.New("unconfigured.") |
| 51 |
) |
| 52 |
|
| 53 |
// New builds a new Powerd that uses the provided bus.Endpoint |
| 54 |
func New(endp bus.Endpoint, log logger.Logger) Powerd { |
| 55 |
return &powerd{endp, log} |
| 56 |
} |
| 57 |
|
| 58 |
func (p *powerd) RequestWakeup(name string, wakeupTime time.Time) (string, error) { |
| 59 |
if p.endp == nil { |
| 60 |
return "", ErrUnconfigured |
| 61 |
} |
| 62 |
var res string |
| 63 |
err := p.endp.Call("requestWakeup", bus.Args(name, uint64(wakeupTime.Unix())), &res) |
| 64 |
return res, err |
| 65 |
} |
| 66 |
|
| 67 |
func (p *powerd) ClearWakeup(cookie string) error { |
| 68 |
if p.endp == nil { |
| 69 |
return ErrUnconfigured |
| 70 |
} |
| 71 |
return p.endp.Call("clearWakeup", bus.Args(cookie)) |
| 72 |
} |
| 73 |
|
| 74 |
func (p *powerd) WatchWakeups() (<-chan bool, error) { |
| 75 |
if p.endp == nil { |
| 76 |
return nil, ErrUnconfigured |
| 77 |
} |
| 78 |
ch := make(chan bool) |
| 79 |
p.endp.WatchSignal("Wakeup", func(...interface{}) { |
| 80 |
ch <- true |
| 81 |
}, func() { close(ch) }) |
| 82 |
return ch, nil |
| 83 |
} |
| 84 |
|
| 85 |
func (p *powerd) RequestWakelock(name string) (string, error) { |
| 86 |
// wakelocks are documented on https://wiki.ubuntu.com/powerd#API |
| 87 |
// (requestSysState with state=1) |
| 88 |
if p.endp == nil { |
| 89 |
return "", ErrUnconfigured |
| 90 |
} |
| 91 |
var res string |
| 92 |
err := p.endp.Call("requestSysState", bus.Args(name, int32(1)), &res) |
| 93 |
return res, err |
| 94 |
} |
| 95 |
|
| 96 |
func (p *powerd) ClearWakelock(cookie string) error { |
| 97 |
if p.endp == nil { |
| 98 |
return ErrUnconfigured |
| 99 |
} |
| 100 |
return p.endp.Call("clearSysState", bus.Args(cookie)) |
| 101 |
} |