diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b02729..af41a75 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ## Unreleased ### Added -1. Added _interval_ parameter to _set-listener_ to set auto-send interval. +1. Added auto-send _interval_ parameter to _get-listener_ and _set-listener_ API functions. ## [0.8.9](https://github.com/uhppoted/uhppote-core/releases/tag/v0.8.9) - 2024-09-06 diff --git a/README.md b/README.md index 88cc269..bbf1d72 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,7 @@ Sets the IPv4 address, subnet mask and gateway address for a controller. #### `GetListener` -Retrieves the IPv4 address of the host configured to receive events from the controller. +Retrieves the IPv4 configured address:port to which events are sent from the controller, along with the auto-send interval. #### `SetListener` diff --git a/TODO.md b/TODO.md index ded27c9..8055d13 100644 --- a/TODO.md +++ b/TODO.md @@ -1,7 +1,8 @@ # TODO -- [x] set-event-listener: add listen interval (cf. https://github.com/uhppoted/uhppote-core/issues/21) +- [x] event listener: add listen auto-send interval (cf. https://github.com/uhppoted/uhppote-core/issues/21) - [x] set-event-listener + - [x] get-event-listener - [x] CHANGELOG - [x] README diff --git a/messages/get_listener.go b/messages/get_listener.go index a18feda..d1a1ff2 100644 --- a/messages/get_listener.go +++ b/messages/get_listener.go @@ -14,4 +14,5 @@ type GetListenerResponse struct { MsgType types.MsgType `uhppote:"value:0x92"` SerialNumber types.SerialNumber `uhppote:"offset:4"` AddrPort netip.AddrPort `uhppote:"offset:8"` + Interval uint8 `uhppote:"offset:14"` } diff --git a/messages/get_listener_test.go b/messages/get_listener_test.go index 56528e4..d727e97 100644 --- a/messages/get_listener_test.go +++ b/messages/get_listener_test.go @@ -59,7 +59,7 @@ func TestFactoryUnmarshalGetListenerRequest(t *testing.T) { func TestUnmarshalGetListenerResponse(t *testing.T) { message := []byte{ - 0x17, 0x92, 0x00, 0x00, 0x2d, 0x55, 0x39, 0x19, 0xc0, 0xa8, 0x00, 0xe1, 0x92, 0x26, 0x00, 0x00, + 0x17, 0x92, 0x00, 0x00, 0x2d, 0x55, 0x39, 0x19, 0xc0, 0xa8, 0x00, 0xe1, 0x92, 0x26, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -83,11 +83,15 @@ func TestUnmarshalGetListenerResponse(t *testing.T) { if reply.AddrPort != netip.MustParseAddrPort("192.168.0.225:9874") { t.Errorf("Incorrect IPv4 address:port - expected:'%v', got:'%v'", netip.MustParseAddrPort("192.168.0.225:9874"), reply.AddrPort) } + + if reply.Interval != 13 { + t.Errorf("Incorrect auto-send interval - expected:%v, got:%v", 13, reply.Interval) + } } func TestFactoryUnmarshalGetListenerResponse(t *testing.T) { message := []byte{ - 0x17, 0x92, 0x00, 0x00, 0x2d, 0x55, 0x39, 0x19, 0xc0, 0xa8, 0x00, 0xe1, 0x92, 0x26, 0x00, 0x00, + 0x17, 0x92, 0x00, 0x00, 0x2d, 0x55, 0x39, 0x19, 0xc0, 0xa8, 0x00, 0xe1, 0x92, 0x26, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -97,6 +101,7 @@ func TestFactoryUnmarshalGetListenerResponse(t *testing.T) { MsgType: 0x92, SerialNumber: 423187757, AddrPort: netip.MustParseAddrPort("192.168.0.225:9874"), + Interval: 13, } response, err := UnmarshalResponse(message) diff --git a/uhppote/get_listener.go b/uhppote/get_listener.go index afa76f5..f2b1fdb 100644 --- a/uhppote/get_listener.go +++ b/uhppote/get_listener.go @@ -8,9 +8,13 @@ import ( "github.com/uhppoted/uhppote-core/types" ) -func (u *uhppote) GetListener(serialNumber uint32) (netip.AddrPort, error) { +// Retrieves the configured events listener address:port and auto-send interval from +// the controller. +// +// Returns an error if something the controller did not respohd. +func (u *uhppote) GetListener(serialNumber uint32) (netip.AddrPort, uint8, error) { if serialNumber == 0 { - return netip.AddrPort{}, fmt.Errorf("invalid device ID (%v)", serialNumber) + return netip.AddrPort{}, 0, fmt.Errorf("invalid device ID (%v)", serialNumber) } request := messages.GetListenerRequest{ @@ -18,8 +22,8 @@ func (u *uhppote) GetListener(serialNumber uint32) (netip.AddrPort, error) { } if reply, err := sendto[messages.GetListenerResponse](u, serialNumber, request); err != nil { - return netip.AddrPort{}, err + return netip.AddrPort{}, 0, err } else { - return reply.AddrPort, nil + return reply.AddrPort, reply.Interval, nil } } diff --git a/uhppote/get_listener_test.go b/uhppote/get_listener_test.go index b43584e..706006f 100644 --- a/uhppote/get_listener_test.go +++ b/uhppote/get_listener_test.go @@ -7,7 +7,7 @@ import ( func TestGetListenerWithInvalidDeviceID(t *testing.T) { u := uhppote{} - _, err := u.GetListener(0) + _, _, err := u.GetListener(0) if err == nil { t.Fatalf("Expected 'Invalid device ID' error, got %v", err) } diff --git a/uhppote/iuhppote.go b/uhppote/iuhppote.go index c1735be..d4b7c23 100644 --- a/uhppote/iuhppote.go +++ b/uhppote/iuhppote.go @@ -14,7 +14,12 @@ type IUHPPOTE interface { GetDevice(deviceID uint32) (*types.Device, error) SetAddress(deviceID uint32, address, mask, gateway net.IP) (*types.Result, error) - GetListener(deviceID uint32) (netip.AddrPort, error) + + // Retrieves the configured events listener address:port and auto-send interval from + // the controller. + // + // Returns an error if something the controller did not respohd. + GetListener(deviceID uint32) (netip.AddrPort, uint8, error) // Sets the controller event listener address:port and auto-send interval. //