From ba0310894bffdb2f8bc2072a4965e347c97e489b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Ros=C3=A9n?= Date: Mon, 13 Dec 2021 20:16:36 +0100 Subject: [PATCH 1/5] fix(SubscribeState): add the path to the recieved chan type also remove the check for the signal to be the current path since this will block us from receiving updates to the actual object before it only signaled on when we deactivated, since the activation happens on another device. --- ActiveConnection.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ActiveConnection.go b/ActiveConnection.go index 3f6e3dc..b06cc9b 100644 --- a/ActiveConnection.go +++ b/ActiveConnection.go @@ -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)), } From bfc40dcb211fc61654e97efb6883c4a4ac048970 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Ros=C3=A9n?= Date: Mon, 13 Dec 2021 20:20:07 +0100 Subject: [PATCH 2/5] fix(PrimaryConnection): use ActiveConnection type Instead of returning a `Connection` it should return an `ActiveConnection` --- NetworkManager.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/NetworkManager.go b/NetworkManager.go index 190004c..053c1a3 100644 --- a/NetworkManager.go +++ b/NetworkManager.go @@ -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) { From 0080a5c9dbfaef78db03e99a0412bf8850f104a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Ros=C3=A9n?= Date: Mon, 13 Dec 2021 20:21:50 +0100 Subject: [PATCH 3/5] feat(AccessPoint): add LastSeen property The property field can be seen as available here: https://developer-old.gnome.org/NetworkManager/stable/gdbus-org.freedesktop.NetworkManager.AccessPoint.html#gdbus-property-org-freedesktop-NetworkManager-AccessPoint.LastSeen Its also showing, in the `Body` of the recieved dBus signal on the `Subscribe()` method. --- AccessPoint.go | 14 ++++++++++++++ utils.go | 15 ++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/AccessPoint.go b/AccessPoint.go index bad167e..849009b 100644 --- a/AccessPoint.go +++ b/AccessPoint.go @@ -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, }) } diff --git a/utils.go b/utils.go index 1a2d3dc..4e1f01b 100644 --- a/utils.go +++ b/utils.go @@ -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 { From 19203e6d79341ea7deca08c61133384b981a65b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Ros=C3=A9n?= Date: Mon, 13 Dec 2021 20:23:38 +0100 Subject: [PATCH 4/5] fix(DeviceWireless): remove duplicated fields --- DeviceWireless.go | 1 - 1 file changed, 1 deletion(-) diff --git a/DeviceWireless.go b/DeviceWireless.go index 187406d..3d3e405 100644 --- a/DeviceWireless.go +++ b/DeviceWireless.go @@ -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() From a1ddfeaa9c6eae49351ad1c767870e945db28cfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Ros=C3=A9n?= Date: Mon, 13 Dec 2021 20:24:41 +0100 Subject: [PATCH 5/5] fix(examples): move examples to their own subfolders --- .github/workflows/go.yml | 2 +- examples/{ => devices}/devices.go | 0 examples/{ => static_connect}/static_connect.go | 0 3 files changed, 1 insertion(+), 1 deletion(-) rename examples/{ => devices}/devices.go (100%) rename examples/{ => static_connect}/static_connect.go (100%) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 3a8bd00..32595f1 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -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 diff --git a/examples/devices.go b/examples/devices/devices.go similarity index 100% rename from examples/devices.go rename to examples/devices/devices.go diff --git a/examples/static_connect.go b/examples/static_connect/static_connect.go similarity index 100% rename from examples/static_connect.go rename to examples/static_connect/static_connect.go