Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions local/core/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ func (c *Client) getDeviceConfiguration() deviceConfiguration {
}
}

func (c *Client) GetDiscoveryConfiguration() DiscoveryConfiguration {
return c.discoveryConfiguration
}

func NewClient(opts ...OptionFunc) *Client {
cfg := config{
errFunc: func(err error) {
Expand Down
12 changes: 3 additions & 9 deletions local/core/getDevice.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,13 @@ import (
"sync"

"github.com/plgd-dev/go-coap/v2/udp/client"
"github.com/plgd-dev/kit/net/coap"
"github.com/plgd-dev/sdk/schema"
)

// According to Device2Cloud spec the CoAPCloudConf Resource shall expose only secure Endpoints (e.g. CoAPS); see the ISO/IEC 30118-1:2018, clause 10.
// You have to be secure to talk to it so we try to load device links via secure endpoints if it is possible.
func patchDeviceLinks(ctx context.Context, d *Device, dlinks schema.ResourceLinks) (*Device, schema.ResourceLinks, error) {
isSecure, err := d.IsSecured(ctx, dlinks)
if err != nil {
defer d.Close(ctx)
return nil, nil, MakeFailedPrecondition(fmt.Errorf("cannot determine whether device %s is secured: %w", d.DeviceID(), err))
}
if !isSecure {
return d, dlinks, nil
}
dlink, err := GetResourceLink(dlinks, "/oic/d")
if err != nil {
defer d.Close(ctx)
Expand All @@ -46,7 +39,8 @@ func (c *Client) GetDevice(ctx context.Context, deviceID string) (*Device, schem
}()

h := newDeviceHandler(c.getDeviceConfiguration(), deviceID, cancel)
err := DiscoverDevices(findCtx, multicastConn, h)
// we want to just get "oic.wk.d" resource, because links will be get via unicast to /oic/res
err := DiscoverDevices(findCtx, multicastConn, h, coap.WithResourceType("oic.wk.d"))
if err != nil {
return nil, nil, MakeDataLoss(fmt.Errorf("could not get the device %s: %w", deviceID, err))
}
Expand Down
4 changes: 3 additions & 1 deletion local/core/getDevices.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/plgd-dev/go-coap/v2/udp/client"
"github.com/plgd-dev/kit/net/coap"
"github.com/plgd-dev/sdk/schema"
)

Expand All @@ -25,7 +26,8 @@ func (c *Client) GetDevices(ctx context.Context, handler DeviceHandler) error {
conn.Close()
}
}()
return DiscoverDevices(ctx, multicastConn, newDiscoveryHandler(c.getDeviceConfiguration(), handler))
// we want to just get "oic.wk.d" resource, because links will be get via unicast to /oic/res
return DiscoverDevices(ctx, multicastConn, newDiscoveryHandler(c.getDeviceConfiguration(), handler), coap.WithResourceType("oic.wk.d"))
}

func newDiscoveryHandler(
Expand Down
26 changes: 22 additions & 4 deletions local/observeDevices.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"sync"
"time"

"github.com/plgd-dev/go-coap/v2/udp/client"
"github.com/plgd-dev/kit/net/coap"
"github.com/gofrs/uuid"
"github.com/plgd-dev/sdk/local/core"
"github.com/plgd-dev/sdk/schema"
Expand Down Expand Up @@ -115,9 +117,13 @@ type listDeviceIds struct {
}

// Handle gets a device connection and is responsible for closing it.
func (o *listDeviceIds) Handle(ctx context.Context, device *core.Device, deviceLinks schema.ResourceLinks) {
defer device.Close(ctx)
o.devices.Store(device.DeviceID(), nil)
func (o *listDeviceIds) Handle(ctx context.Context, client *client.ClientConn, device schema.ResourceLinks) {
defer client.Close()
d, ok := device.GetResourceLink("/oic/d")
if !ok {
return
}
o.devices.Store(d.GetDeviceID(), nil)
}

// Error gets errors during discovery.
Expand All @@ -127,9 +133,21 @@ func (o *listDeviceIds) Error(err error) {
}
}

func (o *devicesObserver) discover(ctx context.Context, handler core.DiscoverDevicesHandler) error {
multicastConn := core.DialDiscoveryAddresses(ctx, o.c.CoreClient().GetDiscoveryConfiguration(), o.c.errors)
defer func() {
for _, conn := range multicastConn {
conn.Close()
}
}()
// we want to just get "oic.wk.d" resource, because links will be get via unicast to /oic/res
return core.DiscoverDevices(ctx, multicastConn, handler, coap.WithResourceType("oic.wk.d"))
}

func (o *devicesObserver) observe(ctx context.Context) (map[string]bool, error) {
newDevices := listDeviceIds{err: o.c.errors, devices: &sync.Map{}}
err := o.c.GetDevicesWithHandler(ctx, &newDevices)

err := o.discover(ctx, &newDevices)
if err != nil {
return nil, err
}
Expand Down