Merge pull request #10 from phommel/master

Don't Panic ™ : Refactoring of execption handling from panic to error values
This commit is contained in:
Tom Wambold 2019-06-26 10:08:15 -04:00 committed by GitHub
commit 7f3cfc8b74
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 453 additions and 243 deletions

View file

@ -24,36 +24,36 @@ type AccessPoint interface {
GetPath() dbus.ObjectPath GetPath() dbus.ObjectPath
// GetFlags gets flags describing the capabilities of the access point. // GetFlags gets flags describing the capabilities of the access point.
GetFlags() uint32 GetFlags() (uint32, error)
// GetWPAFlags gets flags describing the access point's capabilities // GetWPAFlags gets flags describing the access point's capabilities
// according to WPA (Wifi Protected Access). // according to WPA (Wifi Protected Access).
GetWPAFlags() uint32 GetWPAFlags() (uint32, error)
// GetRSNFlags gets flags describing the access point's capabilities // GetRSNFlags gets flags describing the access point's capabilities
// according to the RSN (Robust Secure Network) protocol. // according to the RSN (Robust Secure Network) protocol.
GetRSNFlags() uint32 GetRSNFlags() (uint32, error)
// GetSSID returns the Service Set Identifier identifying the access point. // GetSSID returns the Service Set Identifier identifying the access point.
GetSSID() string GetSSID() (string, error)
// GetFrequency gets the radio channel frequency in use by the access point, // GetFrequency gets the radio channel frequency in use by the access point,
// in MHz. // in MHz.
GetFrequency() uint32 GetFrequency() (uint32, error)
// GetHWAddress gets the hardware address (BSSID) of the access point. // GetHWAddress gets the hardware address (BSSID) of the access point.
GetHWAddress() string GetHWAddress() (string, error)
// GetMode describes the operating mode of the access point. // GetMode describes the operating mode of the access point.
GetMode() Nm80211Mode GetMode() (Nm80211Mode, error)
// GetMaxBitrate gets the maximum bitrate this access point is capable of, in // GetMaxBitrate gets the maximum bitrate this access point is capable of, in
// kilobits/second (Kb/s). // kilobits/second (Kb/s).
GetMaxBitrate() uint32 GetMaxBitrate() (uint32, error)
// GetStrength gets the current signal quality of the access point, in // GetStrength gets the current signal quality of the access point, in
// percent. // percent.
GetStrength() uint8 GetStrength() (uint8, error)
MarshalJSON() ([]byte, error) MarshalJSON() ([]byte, error)
} }
@ -71,52 +71,97 @@ func (a *accessPoint) GetPath() dbus.ObjectPath {
return a.obj.Path() return a.obj.Path()
} }
func (a *accessPoint) GetFlags() uint32 { func (a *accessPoint) GetFlags() (uint32, error) {
return a.getUint32Property(AccessPointPropertyFlags) return a.getUint32Property(AccessPointPropertyFlags)
} }
func (a *accessPoint) GetWPAFlags() uint32 { func (a *accessPoint) GetWPAFlags() (uint32, error) {
return a.getUint32Property(AccessPointPropertyWPAFlags) return a.getUint32Property(AccessPointPropertyWPAFlags)
} }
func (a *accessPoint) GetRSNFlags() uint32 { func (a *accessPoint) GetRSNFlags() (uint32, error) {
return a.getUint32Property(AccessPointPropertyRSNFlags) return a.getUint32Property(AccessPointPropertyRSNFlags)
} }
func (a *accessPoint) GetSSID() string { func (a *accessPoint) GetSSID() (string, error) {
return string(a.getSliceByteProperty(AccessPointPropertySSID)) r, err := a.getSliceByteProperty(AccessPointPropertySSID)
if err != nil {
return "", err
}
return string(r), nil
} }
func (a *accessPoint) GetFrequency() uint32 { func (a *accessPoint) GetFrequency() (uint32, error) {
return a.getUint32Property(AccessPointPropertyFrequency) return a.getUint32Property(AccessPointPropertyFrequency)
} }
func (a *accessPoint) GetHWAddress() string { func (a *accessPoint) GetHWAddress() (string, error) {
return a.getStringProperty(AccessPointPropertyHWAddress) return a.getStringProperty(AccessPointPropertyHWAddress)
} }
func (a *accessPoint) GetMode() Nm80211Mode { func (a *accessPoint) GetMode() (Nm80211Mode, error) {
return Nm80211Mode(a.getUint32Property(AccessPointPropertyMode)) r, err := a.getUint32Property(AccessPointPropertyMode)
if err != nil {
return Nm80211ModeUnknown, err
}
return Nm80211Mode(r), nil
} }
func (a *accessPoint) GetMaxBitrate() uint32 { func (a *accessPoint) GetMaxBitrate() (uint32, error) {
return a.getUint32Property(AccessPointPropertyMaxBitrate) return a.getUint32Property(AccessPointPropertyMaxBitrate)
} }
func (a *accessPoint) GetStrength() uint8 { func (a *accessPoint) GetStrength() (uint8, error) {
return a.getUint8Property(AccessPointPropertyStrength) return a.getUint8Property(AccessPointPropertyStrength)
} }
func (a *accessPoint) MarshalJSON() ([]byte, error) { func (a *accessPoint) MarshalJSON() ([]byte, error) {
Flags, err := a.GetFlags()
if err != nil {
return nil, err
}
WPAFlags, err := a.GetWPAFlags()
if err != nil {
return nil, err
}
RSNFlags, err := a.GetRSNFlags()
if err != nil {
return nil, err
}
SSID, err := a.GetSSID()
if err != nil {
return nil, err
}
Frequency, err := a.GetFrequency()
if err != nil {
return nil, err
}
HWAddress, err := a.GetHWAddress()
if err != nil {
return nil, err
}
Mode, err := a.GetMode()
if err != nil {
return nil, err
}
MaxBitrate, err := a.GetMaxBitrate()
if err != nil {
return nil, err
}
Strength, err := a.GetStrength()
if err != nil {
return nil, err
}
return json.Marshal(map[string]interface{}{ return json.Marshal(map[string]interface{}{
"Flags": a.GetFlags(), "Flags": Flags,
"WPAFlags": a.GetWPAFlags(), "WPAFlags": WPAFlags,
"RSNFlags": a.GetRSNFlags(), "RSNFlags": RSNFlags,
"SSID": a.GetSSID(), "SSID": SSID,
"Frequency": a.GetFrequency(), "Frequency": Frequency,
"HWAddress": a.GetHWAddress(), "HWAddress": HWAddress,
"Mode": a.GetMode().String(), "Mode": Mode.String(),
"MaxBitrate": a.GetMaxBitrate(), "MaxBitrate": MaxBitrate,
"Strength": a.GetStrength(), "Strength": Strength,
}) })
} }

View file

@ -24,43 +24,43 @@ const (
type ActiveConnection interface { type ActiveConnection interface {
// GetConnection gets connection object of the connection. // GetConnection gets connection object of the connection.
GetConnection() Connection GetConnection() (Connection, error)
// GetSpecificObject gets a specific object associated with the active connection. // GetSpecificObject gets a specific object associated with the active connection.
GetSpecificObject() AccessPoint GetSpecificObject() (AccessPoint, error)
// GetID gets the ID of the connection. // GetID gets the ID of the connection.
GetID() string GetID() (string, error)
// GetUUID gets the UUID of the connection. // GetUUID gets the UUID of the connection.
GetUUID() string GetUUID() (string, error)
// GetType gets the type of the connection. // GetType gets the type of the connection.
GetType() string GetType() (string, error)
// GetDevices gets array of device objects which are part of this active connection. // GetDevices gets array of device objects which are part of this active connection.
GetDevices() []Device GetDevices() ([]Device, error)
// GetState gets the state of the connection. // GetState gets the state of the connection.
GetState() uint32 GetState() (uint32, error)
// GetStateFlags gets the state flags of the connection. // GetStateFlags gets the state flags of the connection.
GetStateFlags() uint32 GetStateFlags() (uint32, error)
// GetDefault gets the default IPv4 flag of the connection. // GetDefault gets the default IPv4 flag of the connection.
GetDefault() bool GetDefault() (bool, error)
// GetIP4Config gets the IP4Config of the connection. // GetIP4Config gets the IP4Config of the connection.
GetIP4Config() IP4Config GetIP4Config() (IP4Config, error)
// GetDHCP4Config gets the DHCP4Config of the connection. // GetDHCP4Config gets the DHCP4Config of the connection.
GetDHCP4Config() DHCP4Config GetDHCP4Config() (DHCP4Config, error)
// GetVPN gets the VPN flag of the connection. // GetVPN gets the VPN flag of the connection.
GetVPN() bool GetVPN() (bool, error)
// GetMaster gets the master device of the connection. // GetMaster gets the master device of the connection.
GetMaster() Device GetMaster() (Device, error)
} }
func NewActiveConnection(objectPath dbus.ObjectPath) (ActiveConnection, error) { func NewActiveConnection(objectPath dbus.ObjectPath) (ActiveConnection, error) {
@ -72,90 +72,113 @@ type activeConnection struct {
dbusBase dbusBase
} }
func (a *activeConnection) GetConnection() Connection { func (a *activeConnection) GetConnection() (Connection, error) {
path := a.getObjectProperty(ActiveConnectionProperyConnection) path, err := a.getObjectProperty(ActiveConnectionProperyConnection)
if err != nil {
return nil, err
}
con, err := NewConnection(path) con, err := NewConnection(path)
if err != nil { if err != nil {
panic(err) return nil, err
} }
return con return con, nil
} }
func (a *activeConnection) GetSpecificObject() AccessPoint { func (a *activeConnection) GetSpecificObject() (AccessPoint, error) {
path := a.getObjectProperty(ActiveConnectionProperySpecificObject) path, err := a.getObjectProperty(ActiveConnectionProperySpecificObject)
if err != nil {
return nil, err
}
ap, err := NewAccessPoint(path) ap, err := NewAccessPoint(path)
if err != nil { if err != nil {
panic(err) return nil, err
} }
return ap return ap, nil
} }
func (a *activeConnection) GetID() string { func (a *activeConnection) GetID() (string, error) {
return a.getStringProperty(ActiveConnectionProperyID) return a.getStringProperty(ActiveConnectionProperyID)
} }
func (a *activeConnection) GetUUID() string { func (a *activeConnection) GetUUID() (string, error) {
return a.getStringProperty(ActiveConnectionProperyUUID) return a.getStringProperty(ActiveConnectionProperyUUID)
} }
func (a *activeConnection) GetType() string { func (a *activeConnection) GetType() (string, error) {
return a.getStringProperty(ActiveConnectionProperyType) return a.getStringProperty(ActiveConnectionProperyType)
} }
func (a *activeConnection) GetDevices() []Device { func (a *activeConnection) GetDevices() ([]Device, error) {
paths := a.getSliceObjectProperty(ActiveConnectionProperyDevices) paths, err := a.getSliceObjectProperty(ActiveConnectionProperyDevices)
if err != nil {
return nil, err
}
devices := make([]Device, len(paths)) devices := make([]Device, len(paths))
var err error
for i, path := range paths { for i, path := range paths {
devices[i], err = DeviceFactory(path) devices[i], err = DeviceFactory(path)
if err != nil { if err != nil {
panic(err) return nil, err
} }
} }
return devices return devices, nil
} }
func (a *activeConnection) GetState() uint32 { func (a *activeConnection) GetState() (uint32, error) {
return a.getUint32Property(ActiveConnectionProperyState) return a.getUint32Property(ActiveConnectionProperyState)
} }
func (a *activeConnection) GetStateFlags() uint32 { func (a *activeConnection) GetStateFlags() (uint32, error) {
return a.getUint32Property(ActiveConnectionProperyStateFlags) return a.getUint32Property(ActiveConnectionProperyStateFlags)
} }
func (a *activeConnection) GetDefault() bool { func (a *activeConnection) GetDefault() (bool, error) {
b := a.getProperty(ActiveConnectionProperyDefault) b, err := a.getProperty(ActiveConnectionProperyDefault)
return b.(bool) if err != nil {
return false, err
}
return b.(bool), nil
} }
func (a *activeConnection) GetIP4Config() IP4Config { func (a *activeConnection) GetIP4Config() (IP4Config, error) {
path := a.getObjectProperty(ActiveConnectionProperyIP4Config) path, err := a.getObjectProperty(ActiveConnectionProperyIP4Config)
if err != nil {
return nil, err
}
r, err := NewIP4Config(path) r, err := NewIP4Config(path)
if err != nil { if err != nil {
panic(err) return nil, err
} }
return r return r, nil
} }
func (a *activeConnection) GetDHCP4Config() DHCP4Config { func (a *activeConnection) GetDHCP4Config() (DHCP4Config, error) {
path := a.getObjectProperty(ActiveConnectionProperyDHCP4Config) path, err := a.getObjectProperty(ActiveConnectionProperyDHCP4Config)
if err != nil {
return nil, err
}
r, err := NewDHCP4Config(path) r, err := NewDHCP4Config(path)
if err != nil { if err != nil {
panic(err) return nil, err
} }
return r return r, nil
} }
func (a *activeConnection) GetVPN() bool { func (a *activeConnection) GetVPN() (bool, error) {
ret := a.getProperty(ActiveConnectionProperyVPN) ret, err := a.getProperty(ActiveConnectionProperyVPN)
return ret.(bool) if err != nil {
return false, err
}
return ret.(bool), nil
} }
func (a *activeConnection) GetMaster() Device { func (a *activeConnection) GetMaster() (Device, error) {
path := a.getObjectProperty(ActiveConnectionProperyMaster) path, err := a.getObjectProperty(ActiveConnectionProperyMaster)
if err != nil {
return nil, err
}
r, err := DeviceFactory(path) r, err := DeviceFactory(path)
if err != nil { if err != nil {
panic(err) return nil, err
} }
return r return r, nil
} }

View file

@ -16,7 +16,7 @@ type DHCP4Options map[string]interface{}
type DHCP4Config interface { type DHCP4Config interface {
// GetOptions gets options map of configuration returned by the IPv4 DHCP server. // GetOptions gets options map of configuration returned by the IPv4 DHCP server.
GetOptions() DHCP4Options GetOptions() (DHCP4Options, error)
MarshalJSON() ([]byte, error) MarshalJSON() ([]byte, error)
} }
@ -30,19 +30,26 @@ type dhcp4Config struct {
dbusBase dbusBase
} }
func (c *dhcp4Config) GetOptions() DHCP4Options { func (c *dhcp4Config) GetOptions() (DHCP4Options, error) {
options := c.getMapStringVariantProperty(DHCP4ConfigPropertyOptions) options, err := c.getMapStringVariantProperty(DHCP4ConfigPropertyOptions)
if err != nil {
return nil, err
}
rv := make(DHCP4Options) rv := make(DHCP4Options)
for k, v := range options { for k, v := range options {
rv[k] = v.Value() rv[k] = v.Value()
} }
return rv return rv, nil
} }
func (c *dhcp4Config) MarshalJSON() ([]byte, error) { func (c *dhcp4Config) MarshalJSON() ([]byte, error) {
Options, err := c.GetOptions()
if err != nil {
return nil, err
}
return json.Marshal(map[string]interface{}{ return json.Marshal(map[string]interface{}{
"Options": c.GetOptions(), "Options": Options,
}) })
} }

132
Device.go
View file

@ -2,6 +2,7 @@ package gonetworkmanager
import ( import (
"encoding/json" "encoding/json"
"errors"
"github.com/godbus/dbus" "github.com/godbus/dbus"
) )
@ -24,7 +25,11 @@ func DeviceFactory(objectPath dbus.ObjectPath) (Device, error) {
return nil, err return nil, err
} }
switch d.GetDeviceType() { dt, err := d.GetDeviceType()
if err != nil {
return nil, err
}
switch dt {
case NmDeviceTypeWifi: case NmDeviceTypeWifi:
return NewWirelessDevice(objectPath) return NewWirelessDevice(objectPath)
} }
@ -37,31 +42,31 @@ type Device interface {
// GetInterface gets the name of the device's control (and often data) // GetInterface gets the name of the device's control (and often data)
// interface. // interface.
GetInterface() string GetInterface() (string, error)
// GetIpInterface gets the IP interface name of the device. // GetIpInterface gets the IP interface name of the device.
GetIpInterface() string GetIpInterface() (string, error)
// GetState gets the current state of the device. // GetState gets the current state of the device.
GetState() NmDeviceState GetState() (NmDeviceState, error)
// GetIP4Config gets the Ip4Config object describing the configuration of the // GetIP4Config gets the Ip4Config object describing the configuration of the
// device. Only valid when the device is in the NM_DEVICE_STATE_ACTIVATED // device. Only valid when the device is in the NM_DEVICE_STATE_ACTIVATED
// state. // state.
GetIP4Config() IP4Config GetIP4Config() (IP4Config, error)
// GetDHCP4Config gets the Dhcp4Config object describing the configuration of the // GetDHCP4Config gets the Dhcp4Config object describing the configuration of the
// device. Only valid when the device is in the NM_DEVICE_STATE_ACTIVATED // device. Only valid when the device is in the NM_DEVICE_STATE_ACTIVATED
// state. // state.
GetDHCP4Config() DHCP4Config GetDHCP4Config() (DHCP4Config, error)
// GetDeviceType gets the general type of the network device; ie Ethernet, // GetDeviceType gets the general type of the network device; ie Ethernet,
// WiFi, etc. // WiFi, etc.
GetDeviceType() NmDeviceType GetDeviceType() (NmDeviceType, error)
// GetAvailableConnections gets an array of object paths of every configured // GetAvailableConnections gets an array of object paths of every configured
// connection that is currently 'available' through this device. // connection that is currently 'available' through this device.
GetAvailableConnections() []Connection GetAvailableConnections() ([]Connection, error)
MarshalJSON() ([]byte, error) MarshalJSON() ([]byte, error)
} }
@ -79,77 +84,126 @@ func (d *device) GetPath() dbus.ObjectPath {
return d.obj.Path() return d.obj.Path()
} }
func (d *device) GetInterface() string { func (d *device) GetInterface() (string, error) {
return d.getStringProperty(DevicePropertyInterface) return d.getStringProperty(DevicePropertyInterface)
} }
func (d *device) GetIpInterface() string { func (d *device) GetIpInterface() (string, error) {
return d.getStringProperty(DevicePropertyIpInterface) return d.getStringProperty(DevicePropertyIpInterface)
} }
func (d *device) GetState() NmDeviceState { func (d *device) GetState() (NmDeviceState, error) {
return NmDeviceState(d.getUint32Property(DevicePropertyState)) r, err := d.getUint32Property(DevicePropertyState)
if err != nil {
return NmDeviceStateFailed, err
}
return NmDeviceState(r), nil
} }
func (d *device) GetIP4Config() IP4Config { func (d *device) GetIP4Config() (IP4Config, error) {
path := d.getObjectProperty(DevicePropertyIP4Config) path, err := d.getObjectProperty(DevicePropertyIP4Config)
if err != nil {
return nil, err
}
if path == "/" { if path == "/" {
return nil return nil, errors.New("device path was empty")
} }
cfg, err := NewIP4Config(path) cfg, err := NewIP4Config(path)
if err != nil { if err != nil {
panic(err) return nil, err
} }
return cfg return cfg, nil
} }
func (d *device) GetDHCP4Config() DHCP4Config { func (d *device) GetDHCP4Config() (DHCP4Config, error) {
path := d.getObjectProperty(DevicePropertyDhcp4Config) path, err := d.getObjectProperty(DevicePropertyDhcp4Config)
if err != nil {
return nil, err
}
if path == "/" { if path == "/" {
return nil return nil, errors.New("device path was empty")
} }
cfg, err := NewDHCP4Config(path) cfg, err := NewDHCP4Config(path)
if err != nil { if err != nil {
panic(err) return nil, err
} }
return cfg return cfg, nil
} }
func (d *device) GetDeviceType() NmDeviceType { func (d *device) GetDeviceType() (NmDeviceType, error) {
return NmDeviceType(d.getUint32Property(DevicePropertyDeviceType)) r, err := d.getUint32Property(DevicePropertyDeviceType)
if err != nil {
return NmDeviceTypeUnknown, err
}
return NmDeviceType(r), nil
} }
func (d *device) GetAvailableConnections() []Connection { func (d *device) GetAvailableConnections() ([]Connection, error) {
connPaths := d.getSliceObjectProperty(DevicePropertyAvailableConnections) connPaths, err := d.getSliceObjectProperty(DevicePropertyAvailableConnections)
if err != nil {
return nil, err
}
conns := make([]Connection, len(connPaths)) conns := make([]Connection, len(connPaths))
var err error
for i, path := range connPaths { for i, path := range connPaths {
conns[i], err = NewConnection(path) conns[i], err = NewConnection(path)
if err != nil { if err != nil {
panic(err) return nil, err
} }
} }
return conns return conns, nil
}
func (d *device) marshalMap() (map[string]interface{}, error) {
Interface, err := d.GetInterface()
if err != nil {
return nil, err
}
IPinterface, err := d.GetIpInterface()
if err != nil {
return nil, err
}
State, err := d.GetState()
if err != nil {
return nil, err
}
IP4Config, err := d.GetIP4Config()
if err != nil {
return nil, err
}
DHCP4Config, err := d.GetDHCP4Config()
if err != nil {
return nil, err
}
DeviceType, err := d.GetDeviceType()
if err != nil {
return nil, err
}
AvailableConnections, err := d.GetAvailableConnections()
if err != nil {
return nil, err
} }
func (d *device) marshalMap() map[string]interface{} {
return map[string]interface{}{ return map[string]interface{}{
"Interface": d.GetInterface(), "Interface": Interface,
"IP interface": d.GetIpInterface(), "IP interface": IPinterface,
"State": d.GetState().String(), "State": State.String(),
"IP4Config": d.GetIP4Config(), "IP4Config": IP4Config,
"DHCP4Config": d.GetDHCP4Config(), "DHCP4Config": DHCP4Config,
"DeviceType": d.GetDeviceType().String(), "DeviceType": DeviceType.String(),
"AvailableConnections": d.GetAvailableConnections(), "AvailableConnections": AvailableConnections,
} }, nil
} }
func (d *device) MarshalJSON() ([]byte, error) { func (d *device) MarshalJSON() ([]byte, error) {
return json.Marshal(d.marshalMap()) m, err := d.marshalMap()
if err != nil {
return nil, err
}
return json.Marshal(m)
} }

View file

@ -32,20 +32,20 @@ type IP4Config interface {
// GetAddresses gets an array of tuples of IPv4 address/prefix/gateway. All 3 // GetAddresses gets an array of tuples of IPv4 address/prefix/gateway. All 3
// elements of each tuple are in network byte order. Essentially: [(addr, // elements of each tuple are in network byte order. Essentially: [(addr,
// prefix, gateway), (addr, prefix, gateway), ...] // prefix, gateway), (addr, prefix, gateway), ...]
GetAddresses() []IP4Address GetAddresses() ([]IP4Address, error)
// GetRoutes gets tuples of IPv4 route/prefix/next-hop/metric. All 4 elements // GetRoutes gets tuples of IPv4 route/prefix/next-hop/metric. All 4 elements
// of each tuple are in network byte order. 'route' and 'next hop' are IPv4 // of each tuple are in network byte order. 'route' and 'next hop' are IPv4
// addresses, while prefix and metric are simple unsigned integers. // addresses, while prefix and metric are simple unsigned integers.
// Essentially: [(route, prefix, next-hop, metric), (route, prefix, next-hop, // Essentially: [(route, prefix, next-hop, metric), (route, prefix, next-hop,
// metric), ...] // metric), ...]
GetRoutes() []IP4Route GetRoutes() ([]IP4Route, error)
// GetNameservers gets the nameservers in use. // GetNameservers gets the nameservers in use.
GetNameservers() []string GetNameservers() ([]string, error)
// GetDomains gets a list of domains this address belongs to. // GetDomains gets a list of domains this address belongs to.
GetDomains() []string GetDomains() ([]string, error)
MarshalJSON() ([]byte, error) MarshalJSON() ([]byte, error)
} }
@ -59,8 +59,11 @@ type ip4Config struct {
dbusBase dbusBase
} }
func (c *ip4Config) GetAddresses() []IP4Address { func (c *ip4Config) GetAddresses() ([]IP4Address, error) {
addresses := c.getSliceSliceUint32Property(IP4ConfigPropertyAddresses) addresses, err := c.getSliceSliceUint32Property(IP4ConfigPropertyAddresses)
if err != nil {
return nil, err
}
ret := make([]IP4Address, len(addresses)) ret := make([]IP4Address, len(addresses))
for i, parts := range addresses { for i, parts := range addresses {
@ -71,11 +74,14 @@ func (c *ip4Config) GetAddresses() []IP4Address {
} }
} }
return ret return ret, nil
} }
func (c *ip4Config) GetRoutes() []IP4Route { func (c *ip4Config) GetRoutes() ([]IP4Route, error) {
routes := c.getSliceSliceUint32Property(IP4ConfigPropertyRoutes) routes, err := c.getSliceSliceUint32Property(IP4ConfigPropertyRoutes)
if err != nil {
return nil, err
}
ret := make([]IP4Route, len(routes)) ret := make([]IP4Route, len(routes))
for i, parts := range routes { for i, parts := range routes {
@ -87,29 +93,49 @@ func (c *ip4Config) GetRoutes() []IP4Route {
} }
} }
return ret return ret, nil
} }
func (c *ip4Config) GetNameservers() []string { func (c *ip4Config) GetNameservers() ([]string, error) {
nameservers := c.getSliceUint32Property(IP4ConfigPropertyNameservers) nameservers, err := c.getSliceUint32Property(IP4ConfigPropertyNameservers)
if err != nil {
return nil, err
}
ret := make([]string, len(nameservers)) ret := make([]string, len(nameservers))
for i, ns := range nameservers { for i, ns := range nameservers {
ret[i] = ip4ToString(ns) ret[i] = ip4ToString(ns)
} }
return ret return ret, nil
} }
func (c *ip4Config) GetDomains() []string { func (c *ip4Config) GetDomains() ([]string, error) {
return c.getSliceStringProperty(IP4ConfigPropertyDomains) return c.getSliceStringProperty(IP4ConfigPropertyDomains)
} }
func (c *ip4Config) MarshalJSON() ([]byte, error) { func (c *ip4Config) MarshalJSON() ([]byte, error) {
Addresses, err := c.GetAddresses()
if err != nil {
return nil, err
}
Routes, err := c.GetRoutes()
if err != nil {
return nil, err
}
Nameservers, err := c.GetNameservers()
if err != nil {
return nil, err
}
Domains, err := c.GetDomains()
if err != nil {
return nil, err
}
return json.Marshal(map[string]interface{}{ return json.Marshal(map[string]interface{}{
"Addresses": c.GetAddresses(), "Addresses": Addresses,
"Routes": c.GetRoutes(), "Routes": Routes,
"Nameservers": c.GetNameservers(), "Nameservers": Nameservers,
"Domains": c.GetDomains(), "Domains": Domains,
}) })
} }

View file

@ -20,18 +20,30 @@ const (
type NetworkManager interface { type NetworkManager interface {
// GetDevices gets the list of network devices. // GetDevices gets the list of network devices.
GetDevices() []Device GetDevices() ([]Device, error)
// GetState returns the overall networking state as determined by the // GetState returns the overall networking state as determined by the
// NetworkManager daemon, based on the state of network devices under it's // NetworkManager daemon, based on the state of network devices under it's
// management. // management.
GetState() NmState GetState() (NmState, error)
// GetActiveConnections returns the active connection of network devices. // GetActiveConnections returns the active connection of network devices.
GetActiveConnections() []ActiveConnection GetActiveConnections() ([]ActiveConnection, error)
// ActivateWirelessConnection requests activating access point to network device // ActivateWirelessConnection requests activating access point to network device
ActivateWirelessConnection(connection Connection, device Device, accessPoint AccessPoint) ActiveConnection ActivateWirelessConnection(connection Connection, device Device, accessPoint AccessPoint) (ActiveConnection, error)
// AddAndActivateWirelessConnection adds a new connection profile to the network device it has been
// passed. It then activates the connection to the passed access point. The first paramter contains
// additional information for the connection (most propably the credentials).
// Example contents for connection are:
// connection := make(map[string]map[string]interface{})
// connection["802-11-wireless"] = make(map[string]interface{})
// connection["802-11-wireless"]["security"] = "802-11-wireless-security"
// connection["802-11-wireless-security"] = make(map[string]interface{})
// connection["802-11-wireless-security"]["key-mgmt"] = "wpa-psk"
// connection["802-11-wireless-security"]["psk"] = password
AddAndActivateWirelessConnection(connection map[string]map[string]interface{}, device Device, accessPoint AccessPoint) (ac ActiveConnection, err error)
// AddAndActivateWirelessConnection adds a new connection profile to the network device it has been // AddAndActivateWirelessConnection adds a new connection profile to the network device it has been
// passed. It then activates the connection to the passed access point. The first paramter contains // passed. It then activates the connection to the passed access point. The first paramter contains
@ -62,46 +74,69 @@ type networkManager struct {
sigChan chan *dbus.Signal sigChan chan *dbus.Signal
} }
func (n *networkManager) GetDevices() []Device { func (n *networkManager) GetDevices() ([]Device, error) {
var devicePaths []dbus.ObjectPath var devicePaths []dbus.ObjectPath
n.call(&devicePaths, NetworkManagerGetDevices) err := n.call(&devicePaths, NetworkManagerGetDevices)
if err != nil {
return nil, err
}
devices := make([]Device, len(devicePaths)) devices := make([]Device, len(devicePaths))
var err error
for i, path := range devicePaths { for i, path := range devicePaths {
devices[i], err = DeviceFactory(path) devices[i], err = DeviceFactory(path)
if err != nil { if err != nil {
panic(err) return nil, err
} }
} }
return devices return devices, nil
} }
func (n *networkManager) GetState() NmState { func (n *networkManager) GetState() (NmState, error) {
return NmState(n.getUint32Property(NetworkManagerPropertyState)) r, err := n.getUint32Property(NetworkManagerPropertyState)
if err != nil {
return NmStateUnknown, err
}
return NmState(r), nil
} }
func (n *networkManager) GetActiveConnections() []ActiveConnection { func (n *networkManager) GetActiveConnections() ([]ActiveConnection, error) {
acPaths := n.getSliceObjectProperty(NetworkManagerPropertyActiveConnection) acPaths, err := n.getSliceObjectProperty(NetworkManagerPropertyActiveConnection)
if err != nil {
return nil, err
}
ac := make([]ActiveConnection, len(acPaths)) ac := make([]ActiveConnection, len(acPaths))
var err error
for i, path := range acPaths { for i, path := range acPaths {
ac[i], err = NewActiveConnection(path) ac[i], err = NewActiveConnection(path)
if err != nil { if err != nil {
panic(err) return nil, err
} }
} }
return ac return ac, nil
} }
func (n *networkManager) ActivateWirelessConnection(c Connection, d Device, ap AccessPoint) ActiveConnection { func (n *networkManager) ActivateWirelessConnection(c Connection, d Device, ap AccessPoint) (ActiveConnection, error) {
var opath dbus.ObjectPath var opath dbus.ObjectPath
n.call(&opath, NetworkManagerActivateConnection, c.GetPath(), d.GetPath(), ap.GetPath()) return nil, n.call(&opath, NetworkManagerActivateConnection, c.GetPath(), d.GetPath(), ap.GetPath())
return nil }
func (n *networkManager) AddAndActivateWirelessConnection(connection map[string]map[string]interface{}, d Device, ap AccessPoint) (ac ActiveConnection, err error) {
var opath1 dbus.ObjectPath
var opath2 dbus.ObjectPath
err = n.call2(&opath1, &opath2, NetworkManagerAddAndActivateConnection, connection, d.GetPath(), ap.GetPath())
if err != nil {
return
}
ac, err = NewActiveConnection(opath2)
if err != nil {
return
}
return
} }
func (n *networkManager) AddAndActivateWirelessConnection(connection map[string]map[string]interface{}, d Device, ap AccessPoint) (ac ActiveConnection, err error) { func (n *networkManager) AddAndActivateWirelessConnection(connection map[string]map[string]interface{}, d Device, ap AccessPoint) (ac ActiveConnection, err error) {
@ -138,8 +173,17 @@ func (n *networkManager) Unsubscribe() {
} }
func (n *networkManager) MarshalJSON() ([]byte, error) { func (n *networkManager) MarshalJSON() ([]byte, error) {
NetworkState, err := n.GetState()
if err != nil {
return nil, err
}
Devices, err := n.GetDevices()
if err != nil {
return nil, err
}
return json.Marshal(map[string]interface{}{ return json.Marshal(map[string]interface{}{
"NetworkState": n.GetState().String(), "NetworkState": NetworkState.String(),
"Devices": n.GetDevices(), "Devices": Devices,
}) })
} }

View file

@ -15,10 +15,10 @@ const (
type Settings interface { type Settings interface {
// ListConnections gets list the saved network connections known to NetworkManager // ListConnections gets list the saved network connections known to NetworkManager
ListConnections() []Connection ListConnections() ([]Connection, error)
// AddConnection call new connection and save it to disk. // AddConnection call new connection and save it to disk.
AddConnection(settings ConnectionSettings) Connection AddConnection(settings ConnectionSettings) (Connection, error)
} }
func NewSettings() (Settings, error) { func NewSettings() (Settings, error) {
@ -30,29 +30,34 @@ type settings struct {
dbusBase dbusBase
} }
func (s *settings) ListConnections() []Connection { func (s *settings) ListConnections() ([]Connection, error) {
var connectionPaths []dbus.ObjectPath var connectionPaths []dbus.ObjectPath
s.call(&connectionPaths, SettingsListConnections) err := s.call(&connectionPaths, SettingsListConnections)
if err != nil {
return nil, err
}
connections := make([]Connection, len(connectionPaths)) connections := make([]Connection, len(connectionPaths))
var err error
for i, path := range connectionPaths { for i, path := range connectionPaths {
connections[i], err = NewConnection(path) connections[i], err = NewConnection(path)
if err != nil { if err != nil {
panic(err) return nil, err
} }
} }
return connections return connections, nil
} }
func (s *settings) AddConnection(settings ConnectionSettings) Connection { func (s *settings) AddConnection(settings ConnectionSettings) (Connection, error) {
var path dbus.ObjectPath var path dbus.ObjectPath
s.call(&path, SettingsAddConnection, settings) err := s.call(&path, SettingsAddConnection, settings)
if err != nil {
return nil, err
}
con, err := NewConnection(path) con, err := NewConnection(path)
if err != nil { if err != nil {
panic(err) return nil, err
} }
return con return con, nil
} }

View file

@ -20,9 +20,9 @@ type WirelessDevice interface {
// Note that this list does not include access points which hide their SSID. // Note that this list does not include access points which hide their SSID.
// To retrieve a list of all access points (including hidden ones) use the // To retrieve a list of all access points (including hidden ones) use the
// GetAllAccessPoints() method. // GetAllAccessPoints() method.
GetAccessPoints() []AccessPoint GetAccessPoints() ([]AccessPoint, error)
RequestScan() RequestScan() error
} }
func NewWirelessDevice(objectPath dbus.ObjectPath) (WirelessDevice, error) { func NewWirelessDevice(objectPath dbus.ObjectPath) (WirelessDevice, error) {
@ -34,30 +34,39 @@ type wirelessDevice struct {
device device
} }
func (d *wirelessDevice) GetAccessPoints() []AccessPoint { func (d *wirelessDevice) GetAccessPoints() ([]AccessPoint, error) {
var apPaths []dbus.ObjectPath var apPaths []dbus.ObjectPath
d.call(&apPaths, WirelessDeviceGetAccessPoints) err := d.call(&apPaths, WirelessDeviceGetAccessPoints)
if err != nil {
return nil, err
}
aps := make([]AccessPoint, len(apPaths)) aps := make([]AccessPoint, len(apPaths))
var err error
for i, path := range apPaths { for i, path := range apPaths {
aps[i], err = NewAccessPoint(path) aps[i], err = NewAccessPoint(path)
if err != nil { if err != nil {
panic(err) return nil, err
} }
} }
return aps return aps, nil
} }
func (d *wirelessDevice) RequestScan() { func (d *wirelessDevice) RequestScan() error {
var options map[string]interface{} var options map[string]interface{}
d.obj.Call(WirelessDeviceRequestScan, 0, options).Store() return d.obj.Call(WirelessDeviceRequestScan, 0, options).Store()
} }
func (d *wirelessDevice) MarshalJSON() ([]byte, error) { func (d *wirelessDevice) MarshalJSON() ([]byte, error) {
m := d.device.marshalMap() m, err := d.device.marshalMap()
m["AccessPoints"] = d.GetAccessPoints() if err != nil {
return nil, err
}
aps, err := d.GetAccessPoints()
if err != nil {
return nil, err
}
m["AccessPoints"] = aps
return json.Marshal(m) return json.Marshal(m)
} }

117
utils.go
View file

@ -30,15 +30,12 @@ func (d *dbusBase) init(iface string, objectPath dbus.ObjectPath) error {
return nil return nil
} }
func (d *dbusBase) call(value interface{}, method string, args ...interface{}) { func (d *dbusBase) call(value interface{}, method string, args ...interface{}) error {
err := d.callError(value, method, args...) return d.obj.Call(method, 0, args...).Store(value)
if err != nil {
panic(err)
}
} }
func (d *dbusBase) callError(value interface{}, method string, args ...interface{}) error { func (d *dbusBase) call2(value1 interface{}, value2 interface{}, method string, args ...interface{}) error {
return d.obj.Call(method, 0, args...).Store(value) return d.obj.Call(method, 0, args...).Store(value1, value2)
} }
func (d *dbusBase) callError2(value1 interface{}, value2 interface{}, method string, args ...interface{}) error { func (d *dbusBase) callError2(value1 interface{}, value2 interface{}, method string, args ...interface{}) error {
@ -56,92 +53,92 @@ func (d *dbusBase) subscribeNamespace(namespace string) {
d.conn.BusObject().Call(dbusMethodAddMatch, 0, rule) d.conn.BusObject().Call(dbusMethodAddMatch, 0, rule)
} }
func (d *dbusBase) getProperty(iface string) interface{} { func (d *dbusBase) getProperty(iface string) (interface{}, error) {
variant, err := d.obj.GetProperty(iface) variant, err := d.obj.GetProperty(iface)
if err != nil { if err != nil {
panic(err) return nil, err
} }
return variant.Value() return variant.Value(), nil
} }
func (d *dbusBase) getObjectProperty(iface string) dbus.ObjectPath { func (d *dbusBase) getObjectProperty(iface string) (dbus.ObjectPath, error) {
value, ok := d.getProperty(iface).(dbus.ObjectPath) value, err := d.getProperty(iface)
if !ok { if err != nil {
panic(makeErrVariantType(iface)) return "", makeErrVariantType(iface)
} }
return value return value.(dbus.ObjectPath), nil
} }
func (d *dbusBase) getSliceObjectProperty(iface string) []dbus.ObjectPath { func (d *dbusBase) getSliceObjectProperty(iface string) ([]dbus.ObjectPath, error) {
value, ok := d.getProperty(iface).([]dbus.ObjectPath) value, err := d.getProperty(iface)
if !ok { if err != nil {
panic(makeErrVariantType(iface)) return nil, makeErrVariantType(iface)
} }
return value return value.([]dbus.ObjectPath), nil
} }
func (d *dbusBase) getStringProperty(iface string) string { func (d *dbusBase) getStringProperty(iface string) (string, error) {
value, ok := d.getProperty(iface).(string) value, err := d.getProperty(iface)
if !ok { if err != nil {
panic(makeErrVariantType(iface)) return "", makeErrVariantType(iface)
} }
return value return value.(string), nil
} }
func (d *dbusBase) getSliceStringProperty(iface string) []string { func (d *dbusBase) getSliceStringProperty(iface string) ([]string, error) {
value, ok := d.getProperty(iface).([]string) value, err := d.getProperty(iface)
if !ok { if err != nil {
panic(makeErrVariantType(iface)) return nil, makeErrVariantType(iface)
} }
return value return value.([]string), nil
} }
func (d *dbusBase) getMapStringVariantProperty(iface string) map[string]dbus.Variant { func (d *dbusBase) getMapStringVariantProperty(iface string) (map[string]dbus.Variant, error) {
value, ok := d.getProperty(iface).(map[string]dbus.Variant) value, err := d.getProperty(iface)
if !ok { if err != nil {
panic(makeErrVariantType(iface)) return nil, makeErrVariantType(iface)
} }
return value return value.(map[string]dbus.Variant), nil
} }
func (d *dbusBase) getUint8Property(iface string) uint8 { func (d *dbusBase) getUint8Property(iface string) (uint8, error) {
value, ok := d.getProperty(iface).(uint8) value, err := d.getProperty(iface)
if !ok { if err != nil {
panic(makeErrVariantType(iface)) return 0, makeErrVariantType(iface)
} }
return value return value.(uint8), nil
} }
func (d *dbusBase) getUint32Property(iface string) uint32 { func (d *dbusBase) getUint32Property(iface string) (uint32, error) {
value, ok := d.getProperty(iface).(uint32) value, err := d.getProperty(iface)
if !ok { if err != nil {
panic(makeErrVariantType(iface)) return 0, makeErrVariantType(iface)
} }
return value return value.(uint32), nil
} }
func (d *dbusBase) getSliceUint32Property(iface string) []uint32 { func (d *dbusBase) getSliceUint32Property(iface string) ([]uint32, error) {
value, ok := d.getProperty(iface).([]uint32) value, err := d.getProperty(iface)
if !ok { if err != nil {
panic(makeErrVariantType(iface)) return nil, makeErrVariantType(iface)
} }
return value return value.([]uint32), nil
} }
func (d *dbusBase) getSliceSliceUint32Property(iface string) [][]uint32 { func (d *dbusBase) getSliceSliceUint32Property(iface string) ([][]uint32, error) {
value, ok := d.getProperty(iface).([][]uint32) value, err := d.getProperty(iface)
if !ok { if err != nil {
panic(makeErrVariantType(iface)) return nil, makeErrVariantType(iface)
} }
return value return value.([][]uint32), nil
} }
func (d *dbusBase) getSliceByteProperty(iface string) []byte { func (d *dbusBase) getSliceByteProperty(iface string) ([]byte, error) {
value, ok := d.getProperty(iface).([]byte) value, err := d.getProperty(iface)
if !ok { if err != nil {
panic(makeErrVariantType(iface)) return nil, makeErrVariantType(iface)
} }
return value return value.([]byte), nil
} }
func makeErrVariantType(iface string) error { func makeErrVariantType(iface string) error {