-
Notifications
You must be signed in to change notification settings - Fork 0
/
getDevices.go
300 lines (271 loc) · 8.41 KB
/
getDevices.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package client
import (
"context"
"fmt"
"sort"
"strings"
"sync"
"github.com/google/uuid"
"github.com/plgd-dev/device/client/core"
"github.com/plgd-dev/device/pkg/net/coap"
"github.com/plgd-dev/device/schema"
"github.com/plgd-dev/device/schema/device"
"github.com/plgd-dev/device/schema/doxm"
"github.com/plgd-dev/device/schema/interfaces"
kitStrings "github.com/plgd-dev/kit/v2/strings"
)
func getDetails(ctx context.Context, d *core.Device, links schema.ResourceLinks) (interface{}, error) {
link := links.GetResourceLinks(device.ResourceType)
if len(link) == 0 {
return nil, fmt.Errorf("cannot find device resource at links %+v", links)
}
var dev device.Device
err := d.GetResource(ctx, link[0], &dev, coap.WithInterface(interfaces.OC_IF_BASELINE))
if err != nil {
return nil, err
}
return &dev, nil
}
type ownership struct {
doxm *doxm.Doxm
status OwnershipStatus
}
// GetDevices discovers devices in the local mode.
// The deviceResourceType is applied on the client side, because len(deviceResourceType) > 1 does not work with Iotivity 1.3.
func (c *Client) GetDevices(
ctx context.Context,
opts ...GetDevicesOption,
) (map[string]DeviceDetails, error) {
cfg := getDevicesOptions{
err: c.errors,
getDetails: getDetails,
discoveryConfiguration: core.DefaultDiscoveryConfiguration(),
}
for _, o := range opts {
cfg = o.applyOnGetDevices(cfg)
}
var m sync.Mutex
resOwnerships := make(map[string]ownership)
ownerships := func(deviceID string, d ownership) {
m.Lock()
defer m.Unlock()
resOwnerships[deviceID] = d
}
getDetails := func(ctx context.Context, d *core.Device, links schema.ResourceLinks) (interface{}, error) {
links = patchResourceLinksEndpoints(links, c.disableUDPEndpoints)
details, err := cfg.getDetails(ctx, d, links)
if err == nil && d.IsSecured() {
doxm, ownErr := d.GetOwnership(ctx, links)
if ownErr == nil {
ownerships(d.DeviceID(), ownership{
doxm: &doxm,
status: OwnershipStatus_Unknown, // will be resolved later
})
} else if strings.Contains(ownErr.Error(), "x509: certificate signed by unknown authority") {
ownerships(d.DeviceID(), ownership{
status: OwnershipStatus_OwnedByOther,
})
}
}
return details, err
}
var res []DeviceDetails
devices := func(d DeviceDetails) {
m.Lock()
defer m.Unlock()
res = append(res, d)
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
handler := newDiscoveryHandler(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
}
// GetDevicesWithHandler discovers devices using a CoAP multicast request via UDP.
// Device resources can be queried in DeviceHandler using device.Client,
func (c *Client) GetDevicesWithHandler(ctx context.Context, handler core.DeviceHandlerV2, opts ...GetDevicesWithHandlerOption) error {
cfg := getDevicesWithHandlerOptions{
discoveryConfiguration: core.DefaultDiscoveryConfiguration(),
}
for _, o := range opts {
cfg = o.applyOnGetGetDevicesWithHandler(cfg)
}
return c.client.GetDevicesV2(ctx, cfg.discoveryConfiguration, handler)
}
// OwnershipStatus describes ownership status of the device
type OwnershipStatus string
const (
// OwnershipStatus_ReadyToBeOwned the device is ready to be owned.
OwnershipStatus_ReadyToBeOwned OwnershipStatus = "readytobeowned"
// OwnershipStatus_Owned the device is owned.
OwnershipStatus_Owned OwnershipStatus = "owned"
// OwnershipStatus_OwnedByOther the device is owned by another user.
OwnershipStatus_OwnedByOther OwnershipStatus = "ownedbyother"
// OwnershipStatus_Unknown the device is unsecure or cannot obtain his status.
OwnershipStatus_Unknown OwnershipStatus = "unknown"
)
// DeviceDetails describes a device.
type DeviceDetails struct {
// ID of the device
ID string
// IP used to find this device
FoundByIP string
// Details result of function which can be set via option WithGetDetails(), by default it is nil.
Details interface{}
// IsSecured is secured.
IsSecured bool
// Ownership describes ownership of the device, for unsecure device it is nil.
Ownership *doxm.Doxm
// Resources list of the device resources.
Resources []schema.ResourceLink
// Resources list of the device endpoints.
Endpoints []schema.Endpoint
// Ownership status
OwnershipStatus OwnershipStatus
}
func newDiscoveryHandler(
typeFilter []string,
errors func(error),
devices func(DeviceDetails),
getDetails GetDetailsFunc,
deviceCache *DeviceCache,
disableUDPEndpoints bool,
) *discoveryHandler {
return &discoveryHandler{typeFilter: typeFilter, errors: errors, devices: devices, getDetails: getDetails, deviceCache: deviceCache, disableUDPEndpoints: disableUDPEndpoints}
}
type detailsWasSet struct {
sync.Mutex
wasSet bool
}
type discoveryHandler struct {
typeFilter []string
errors func(error)
devices func(DeviceDetails)
getDetails GetDetailsFunc
deviceCache *DeviceCache
disableUDPEndpoints bool
getDetailsWasCalled sync.Map
}
func (h *discoveryHandler) Error(err error) { h.errors(err) }
func getDeviceDetails(ctx context.Context, dev *core.Device, links schema.ResourceLinks, getDetails GetDetailsFunc) (out DeviceDetails, _ error) {
link, ok := links.GetResourceLink(device.ResourceURI)
var eps []schema.Endpoint
if ok {
eps = link.GetEndpoints()
}
isSecured := dev.IsSecured()
var details interface{}
if getDetails != nil {
d, err := getDetails(ctx, dev, links)
if err != nil {
return DeviceDetails{}, err
}
details = d
}
return DeviceDetails{
ID: dev.DeviceID(),
FoundByIP: dev.FoundByIP(),
Details: details,
IsSecured: isSecured,
Resources: links,
Endpoints: eps,
OwnershipStatus: OwnershipStatus_Unknown,
}, nil
}
func (h *discoveryHandler) Handle(ctx context.Context, newdev *core.Device) {
dev, _ := h.deviceCache.UpdateOrStoreDeviceWithExpiration(newdev)
links, err := getLinksDevice(ctx, dev, h.disableUDPEndpoints)
if err != nil {
dev, ok := h.deviceCache.LoadAndDeleteDevice(dev.DeviceID())
if ok {
dev.Close(ctx)
}
return
}
deviceTypes := make(kitStrings.Set, len(dev.DeviceTypes()))
deviceTypes.Add(dev.DeviceTypes()...)
if !deviceTypes.HasOneOf(h.typeFilter...) {
return
}
devDetails, err := h.getDeviceDetails(ctx, dev, links)
if err != nil {
h.Error(err)
return
}
h.devices(devDetails)
}
func (h *discoveryHandler) getDeviceDetails(ctx context.Context, d *core.Device, links schema.ResourceLinks) (out DeviceDetails, _ error) {
getDetails := h.getDetails
v, _ := h.getDetailsWasCalled.LoadOrStore(d.DeviceID(), &detailsWasSet{})
m := v.(*detailsWasSet)
m.Lock()
defer m.Unlock()
if m.wasSet {
getDetails = nil
}
devDetails, err := getDeviceDetails(ctx, d, links, getDetails)
if err == nil {
m.wasSet = true
}
return devDetails, err
}
func mergeDevices(list []DeviceDetails) map[string]DeviceDetails {
m := make(map[string]DeviceDetails, len(list))
for _, i := range list {
d, ok := m[i.ID]
if !ok {
m[i.ID] = i
d = i
} else {
d.Endpoints = mergeEndpoints(d.Endpoints, i.Endpoints)
}
if i.Details != nil {
d.Details = i.Details
}
m[i.ID] = d
}
return m
}
func mergeEndpoints(a, b []schema.Endpoint) []schema.Endpoint {
eps := make([]schema.Endpoint, 0, len(a)+len(b))
eps = append(eps, a...)
eps = append(eps, b...)
sort.SliceStable(eps, func(i, j int) bool { return eps[i].URI < eps[j].URI })
sort.SliceStable(eps, func(i, j int) bool { return eps[i].Priority < eps[j].Priority })
out := make([]schema.Endpoint, 0, len(eps))
var last string
for _, e := range eps {
if last != e.URI {
out = append(out, e)
}
last = e.URI
}
return out
}
func setOwnership(ownerID string, devs map[string]DeviceDetails, owns map[string]ownership) map[string]DeviceDetails {
for deviceID, o := range owns {
d, ok := devs[deviceID]
if ok && d.Ownership == nil {
if o.doxm == nil {
d.OwnershipStatus = o.status
} else {
d.Ownership = o.doxm
switch o.doxm.OwnerID {
case uuid.Nil.String():
d.OwnershipStatus = OwnershipStatus_ReadyToBeOwned
case ownerID:
d.OwnershipStatus = OwnershipStatus_Owned
default:
d.OwnershipStatus = OwnershipStatus_OwnedByOther
}
}
devs[deviceID] = d
}
}
return devs
}