Merge pull request #26 from felixmr1/master

General fixes and improvements
This commit is contained in:
Christian Müller 2022-01-17 08:35:05 +01:00 committed by GitHub
commit 4b20a2d51a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 37 additions and 9 deletions

View file

@ -23,4 +23,4 @@ jobs:
uses: actions/checkout@v2
- name: Build
run: go build -v examples/devices.go
run: go build -v examples/devices/devices.go

View file

@ -57,6 +57,11 @@ type AccessPoint interface {
// percent.
GetPropertyStrength() (uint8, error)
// GetPropertyLastSeen
// The timestamp (in CLOCK_BOOTTIME seconds) for the last time the access point was found in scan results.
// A value of -1 means the access point has never been found in scan results.
GetPropertyLastSeen() (int32, error)
MarshalJSON() ([]byte, error)
}
@ -117,6 +122,10 @@ func (a *accessPoint) GetPropertyStrength() (uint8, error) {
return a.getUint8Property(AccessPointPropertyStrength)
}
func (a *accessPoint) GetPropertyLastSeen() (int32, error) {
return a.getInt32Property(AccessPointPropertyLastSeen)
}
func (a *accessPoint) MarshalJSON() ([]byte, error) {
Flags, err := a.GetPropertyFlags()
if err != nil {
@ -154,6 +163,10 @@ func (a *accessPoint) MarshalJSON() ([]byte, error) {
if err != nil {
return nil, err
}
LastSeen, err := a.GetPropertyLastSeen()
if err != nil {
return nil, err
}
return json.Marshal(map[string]interface{}{
"Flags": Flags,
@ -165,5 +178,6 @@ func (a *accessPoint) MarshalJSON() ([]byte, error) {
"Mode": Mode.String(),
"MaxBitrate": MaxBitrate,
"Strength": Strength,
"LastSeen": LastSeen,
})
}

View file

@ -2,6 +2,7 @@ package gonetworkmanager
import (
"fmt"
"github.com/godbus/dbus/v5"
)
@ -220,6 +221,7 @@ func (a *activeConnection) GetPropertyMaster() (Device, error) {
}
type StateChange struct {
Path dbus.ObjectPath
State NmActiveConnectionState
Reason NmActiveConnectionStateReason
}
@ -243,17 +245,17 @@ func (a *activeConnection) SubscribeState(receiver chan StateChange, exit chan s
for {
select {
case signal, ok := <-channel:
if !ok {
err = fmt.Errorf("connection closed for %s", ActiveConnectionSignalStateChanged)
return
}
if signal.Path != a.GetPath() || signal.Name != ActiveConnectionInterface+"."+ActiveConnectionSignalStateChanged {
if signal.Name != ActiveConnectionInterface+"."+ActiveConnectionSignalStateChanged {
continue
}
stateChange := StateChange{
Path: signal.Path,
State: NmActiveConnectionState(signal.Body[0].(uint32)),
Reason: NmActiveConnectionStateReason(signal.Body[1].(uint32)),
}

View file

@ -186,7 +186,6 @@ func (d *deviceWireless) MarshalJSON() ([]byte, error) {
m["PermHwAddress"], _ = d.GetPropertyPermHwAddress()
m["Mode"], _ = d.GetPropertyMode()
m["Bitrate"], _ = d.GetPropertyBitrate()
m["AccessPoints"], _ = d.GetPropertyAccessPoints()
m["ActiveAccessPoint"], _ = d.GetPropertyActiveAccessPoint()
m["WirelessCapabilities"], _ = d.GetPropertyWirelessCapabilities()
m["LastScan"], _ = d.GetPropertyLastScan()

View file

@ -172,7 +172,7 @@ type NetworkManager interface {
GetPropertyActiveConnections() ([]ActiveConnection, error)
// The object path of the "primary" active connection being used to access the network. In particular, if there is no VPN active, or the VPN does not have the default route, then this indicates the connection that has the default route. If there is a VPN active with the default route, then this indicates the connection that contains the route to the VPN endpoint.
GetPropertyPrimaryConnection() (Connection, error)
GetPropertyPrimaryConnection() (ActiveConnection, error)
// The connection type of the "primary" active connection being used to access the network. This is the same as the Type property on the object indicated by PrimaryConnection.
GetPropertyPrimaryConnectionType() (string, error)
@ -543,14 +543,14 @@ func (nm *networkManager) GetPropertyActiveConnections() ([]ActiveConnection, er
return ac, nil
}
func (nm *networkManager) GetPropertyPrimaryConnection() (Connection, error) {
connectionPath, err := nm.getObjectProperty(NetworkManagerPropertyPrimaryConnection)
func (nm *networkManager) GetPropertyPrimaryConnection() (ActiveConnection, error) {
activeConnectionPath, err := nm.getObjectProperty(NetworkManagerPropertyPrimaryConnection)
if err != nil {
return nil, err
}
return NewConnection(connectionPath)
return NewActiveConnection(activeConnectionPath)
}
func (nm *networkManager) GetPropertyPrimaryConnectionType() (string, error) {

View file

@ -58,7 +58,7 @@ func (d *dbusBase) getProperty(iface string) (interface{}, error) {
return variant.Value(), err
}
func (d *dbusBase) setProperty(iface string, value interface{}) (error) {
func (d *dbusBase) setProperty(iface string, value interface{}) error {
err := d.obj.SetProperty(iface, dbus.MakeVariant(value))
return err
}
@ -180,6 +180,19 @@ func (d *dbusBase) getUint32Property(iface string) (value uint32, err error) {
return
}
func (d *dbusBase) getInt32Property(iface string) (value int32, err error) {
prop, err := d.getProperty(iface)
if err != nil {
return
}
value, ok := prop.(int32)
if !ok {
err = makeErrVariantType(iface)
return
}
return
}
func (d *dbusBase) getInt64Property(iface string) (value int64, err error) {
prop, err := d.getProperty(iface)
if err != nil {