| 1 |
/* |
| 2 |
Copyright 2013-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 systemimage is a mimimal wrapper for the system-image dbus API. |
| 18 |
package systemimage |
| 19 |
|
| 20 |
import ( |
| 21 |
"strconv" |
| 22 |
"strings" |
| 23 |
|
| 24 |
"gitlab.com/ubports/development/core/lomiri-push-service/bus" |
| 25 |
"gitlab.com/ubports/development/core/lomiri-push-service/logger" |
| 26 |
) |
| 27 |
|
| 28 |
// system-image service lives on a well-known bus.Address |
| 29 |
var BusAddress bus.Address = bus.Address{ |
| 30 |
Interface: "com.ubports.SystemImage", |
| 31 |
Path: "/Service", |
| 32 |
Name: "com.ubports.SystemImage", |
| 33 |
} |
| 34 |
|
| 35 |
// InfoResult holds the result of the system-image service Info method. |
| 36 |
type InfoResult struct { |
| 37 |
BuildNumber int32 |
| 38 |
Device string |
| 39 |
Channel string |
| 40 |
// xxx channel_target missing |
| 41 |
LastUpdate string |
| 42 |
VersionDetail map[string]string |
| 43 |
Raw map[string]string |
| 44 |
} |
| 45 |
|
| 46 |
// A SystemImage exposes the a subset of system-image service. |
| 47 |
type SystemImage interface { |
| 48 |
Information() (*InfoResult, error) |
| 49 |
} |
| 50 |
|
| 51 |
type systemImage struct { |
| 52 |
endp bus.Endpoint |
| 53 |
log logger.Logger |
| 54 |
} |
| 55 |
|
| 56 |
// New builds a new system-image service wrapper that uses the provided bus.Endpoint |
| 57 |
func New(endp bus.Endpoint, log logger.Logger) SystemImage { |
| 58 |
return &systemImage{endp, log} |
| 59 |
} |
| 60 |
|
| 61 |
var _ SystemImage = &systemImage{} // ensures it conforms |
| 62 |
|
| 63 |
func (si *systemImage) Information() (*InfoResult, error) { |
| 64 |
si.log.Debugf("invoking Information") |
| 65 |
m := map[string]string{} |
| 66 |
err := si.endp.Call("Information", bus.Args(), &m) |
| 67 |
|
| 68 |
if err != nil { |
| 69 |
si.log.Errorf("Information failed: %v", err) |
| 70 |
return nil, err |
| 71 |
} |
| 72 |
|
| 73 |
res := &InfoResult{} |
| 74 |
|
| 75 |
// Try parsing the build number if it exist. |
| 76 |
if bn := m["current_build_number"]; len(bn) > 0 { |
| 77 |
bn, err := strconv.ParseInt(bn, 10, 32) |
| 78 |
if err == nil { |
| 79 |
res.BuildNumber = int32(bn) |
| 80 |
} else { |
| 81 |
res.BuildNumber = -1 |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
res.Device = m["device_name"] |
| 86 |
res.Channel = m["channel_name"] |
| 87 |
res.LastUpdate = m["last_update_date"] |
| 88 |
res.VersionDetail = map[string]string{} |
| 89 |
|
| 90 |
// Split version detail key=value,key2=value2 into a string map |
| 91 |
// Note that even if |
| 92 |
vals := strings.Split(m["version_detail"], ",") |
| 93 |
for _, val := range vals { |
| 94 |
pairs := strings.Split(val, "=") |
| 95 |
if len(pairs) != 2 { |
| 96 |
continue |
| 97 |
} |
| 98 |
res.VersionDetail[pairs[0]] = pairs[1] |
| 99 |
} |
| 100 |
res.Raw = m |
| 101 |
|
| 102 |
return res, err |
| 103 |
} |