Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client/getDevices: fix filling doxm values for owned devices #249

Merged
merged 2 commits into from
Apr 12, 2022
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
14 changes: 8 additions & 6 deletions client/core/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
goNet "net"
"sync"

"github.com/pion/dtls/v2"
Expand Down Expand Up @@ -74,23 +76,23 @@ func (d *Device) popConnections() []*coap.ClientCloseHandler {

// Close closes open connections to the device.
func (d *Device) Close(ctx context.Context) error {
var errors []error
var errs []error
err := d.stopObservations(ctx)
if err != nil {
errors = append(errors, err)
errs = append(errs, err)
}

for _, conn := range d.popConnections() {
err = conn.Close()
if err != nil {
errors = append(errors, err)
if err != nil && !errors.Is(err, goNet.ErrClosed) {
errs = append(errs, err)
}
// wait for closing socket
<-conn.Done()
}

if len(errors) > 0 {
return MakeInternal(fmt.Errorf("cannot close device %v: %v", d.DeviceID(), errors))
if len(errs) > 0 {
return MakeInternal(fmt.Errorf("cannot close device %v: %v", d.DeviceID(), errs))
}
return nil
}
Expand Down
31 changes: 17 additions & 14 deletions client/getDevices.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,41 +43,44 @@ func (c *Client) GetDevices(
for _, o := range opts {
cfg = o.applyOnGetDevices(cfg)
}
var m sync.Mutex
resOwnerships := make(map[string]doxm.Doxm)
ownerships := func(d doxm.Doxm) {
m.Lock()
defer m.Unlock()
resOwnerships[d.DeviceID] = d
}

getDetails := func(ctx context.Context, d *core.Device, links schema.ResourceLinks) (interface{}, error) {
return cfg.getDetails(ctx, d, patchResourceLinksEndpoints(links, c.disableUDPEndpoints))
links = patchResourceLinksEndpoints(links, c.disableUDPEndpoints)
details, err := cfg.getDetails(ctx, d, links)
if err == nil && d.IsSecured() {
doxm, err := d.GetOwnership(ctx, links)
if err == nil {
ownerships(doxm)
}
}
return details, err
}

var m sync.Mutex
var res []DeviceDetails
devices := func(d DeviceDetails) {
m.Lock()
defer m.Unlock()
res = append(res, d)
}

resOwnerships := make(map[string]doxm.Doxm)
ownerships := func(d doxm.Doxm) {
m.Lock()
defer m.Unlock()
resOwnerships[d.DeviceID] = d
}

ctx, cancel := context.WithCancel(ctx)
defer cancel()

ownershipsHandler := newDiscoveryOwnershipsHandler(ctx, cfg.err, ownerships)
go c.client.GetOwnerships(ctx, cfg.discoveryConfiguration, core.DiscoverAllDevices, ownershipsHandler)

handler := newDiscoveryHandler(ctx, cfg.resourceTypes, cfg.err, devices, getDetails, c.deviceCache, c.disableUDPEndpoints)
if err := c.client.GetDevicesV2(ctx, cfg.discoveryConfiguration, handler); err != nil {
return nil, err
}

m.Lock()
defer m.Unlock()

ownerID, _ := c.client.GetSdkOwnerID()

return setOwnership(ownerID, mergeDevices(res), resOwnerships), nil
}

Expand Down
28 changes: 24 additions & 4 deletions client/getDevices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ func TestDeviceDiscovery(t *testing.T) {
secureDeviceID := test.MustFindDeviceByName(test.DevsimName)
c, err := NewTestSecureClient()
require.NoError(t, err)
defer func() {
err := c.Close(context.Background())
require.NoError(t, err)
}()

ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
devices, err := c.GetDevices(ctx)
require.NoError(t, err)
defer func() {
err := c.Close(context.Background())
require.NoError(t, err)
}()

d := devices[deviceID]
require.NotEmpty(t, d)
Expand All @@ -38,6 +38,26 @@ func TestDeviceDiscovery(t *testing.T) {
assert.Equal(t, test.DevsimName, d.Details.(*device.Device).Name)
require.NotNil(t, d.Ownership)
assert.Equal(t, d.Ownership.OwnerID, "00000000-0000-0000-0000-000000000000")

ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
secureDeviceID, err = c.OwnDevice(ctx, secureDeviceID, client.WithOTM(client.OTMType_JustWorks))
require.NoError(t, err)
devices, err = c.GetDevices(ctx)
require.NoError(t, err)

d = devices[secureDeviceID]
fmt.Println(d)
require.NotNil(t, d)
require.NotNil(t, d.Ownership)
sdkID, err := c.CoreClient().GetSdkOwnerID()
require.NoError(t, err)
assert.Equal(t, d.Ownership.OwnerID, sdkID)

ctx, cancel = context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
err = c.DisownDevice(ctx, secureDeviceID)
require.NoError(t, err)
}

func TestDeviceDiscoveryWithFilter(t *testing.T) {
Expand Down