/
discover.go
173 lines (156 loc) · 4.8 KB
/
discover.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
package core
import (
"context"
"fmt"
"sync"
"time"
"github.com/plgd-dev/device/pkg/net/coap"
"github.com/plgd-dev/go-coap/v2/message"
"github.com/plgd-dev/go-coap/v2/net"
"github.com/plgd-dev/go-coap/v2/net/blockwise"
"github.com/plgd-dev/go-coap/v2/udp"
"github.com/plgd-dev/go-coap/v2/udp/client"
udpMessage "github.com/plgd-dev/go-coap/v2/udp/message"
"github.com/plgd-dev/go-coap/v2/udp/message/pool"
)
// See the section 10.4 on the line 2482 of the Core specification:
// https://openconnectivity.org/specs/OCF_Core_Specification_v2.0.0.pdf
// https://iotivity.org/documentation/linux/programmers-guide
var (
DiscoveryAddressUDP4 = []string{"224.0.1.187:5683"}
DiscoveryAddressUDP6 = []string{"[ff02::158]:5683", "[ff03::158]:5683", "[ff05::158]:5683"}
)
type DiscoveryHandler = func(conn *client.ClientConn, req *pool.Message)
type DiscoveryClient struct {
mcastaddr string
msgID uint16
l *net.UDPConn
server *udp.Server
wg sync.WaitGroup
opts []net.MulticastOption
}
func newDiscoveryClient(network, mcastaddr string, msgID uint16, timeout time.Duration, errors func(error), opts []net.MulticastOption) (*DiscoveryClient, error) {
l, err := net.NewListenUDP(network, "", net.WithErrors(errors))
if err != nil {
return nil, err
}
s := udp.NewServer(udp.WithErrors(errors), udp.WithBlockwise(true, blockwise.SZX1024, timeout), udp.WithMessagePool(pool.New(0, 0)))
c := &DiscoveryClient{
mcastaddr: mcastaddr,
msgID: msgID,
server: s,
l: l,
opts: opts,
}
c.wg.Add(1)
go func() {
defer c.wg.Done()
err := s.Serve(l)
if err != nil {
errors(err)
}
}()
return c, nil
}
func (d *DiscoveryClient) PublishMsgWithContext(req *pool.Message, discoveryHandler DiscoveryHandler) error {
req.SetMessageID(d.msgID)
req.SetType(udpMessage.NonConfirmable)
return d.server.DiscoveryRequest(req, d.mcastaddr, discoveryHandler, d.opts...)
}
func (d *DiscoveryClient) Close() error {
d.server.Stop()
err := d.l.Close()
d.wg.Wait()
return err
}
// DialDiscoveryAddresses connects to discovery endpoints.
func DialDiscoveryAddresses(ctx context.Context, cfg DiscoveryConfiguration, errors func(error)) ([]*DiscoveryClient, error) {
v, ok := ctx.Deadline()
if !ok {
return nil, fmt.Errorf("context has not set deadline")
}
timeout := time.Until(v)
var out []*DiscoveryClient
// We need to separate messageIDs for upd4 and udp6, because if any docker container has isolated network
// iotivity-lite gets error EINVAL(22) for sendmsg with UDP6 for some interfaces. If it happens, the device is
// not discovered and msgid is cached so all other multicast messages from another interfaces are dropped for deduplication.
msgIDudp4 := udpMessage.GetMID()
msgIDudp6 := msgIDudp4 + ^uint16(0)/2
for _, address := range cfg.MulticastAddressUDP4 {
c, err := newDiscoveryClient("udp4", address, msgIDudp4, timeout, errors, cfg.MulticastOptions)
if err != nil {
errors(err)
continue
}
out = append(out, c)
}
for _, address := range cfg.MulticastAddressUDP6 {
c, err := newDiscoveryClient("udp6", address, msgIDudp6, timeout, errors, cfg.MulticastOptions)
if err != nil {
errors(err)
continue
}
out = append(out, c)
}
return out, nil
}
// Discover discovers devices using a CoAP multicast request via UDP.
// It waits for device responses until the context is canceled.
// Device resources can be queried in DiscoveryHandler.
// An empty typeFilter queries all resource types.
// Note: Iotivity 1.3 which responds with BadRequest if more than 1 resource type is queried.
func Discover(
ctx context.Context,
conn []*DiscoveryClient,
href string,
handler DiscoveryHandler,
options ...coap.OptionFunc,
) error {
var wg sync.WaitGroup
defer wg.Wait()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
errors := make(chan error)
runDiscovery := runDiscovery(&wg, href, handler, errors, options...)
for _, c := range conn {
runDiscovery(ctx, c)
}
select {
case err := <-errors:
return err
case <-ctx.Done():
return nil
}
}
func runDiscovery(
wg *sync.WaitGroup,
href string,
handler DiscoveryHandler,
errors chan<- error,
options ...coap.OptionFunc,
) func(ctx context.Context, conn *DiscoveryClient) {
return func(ctx context.Context, conn *DiscoveryClient) {
wg.Add(1)
go func() {
defer wg.Done()
opts := make(message.Options, 0, 4)
for _, o := range options {
opts = o(opts)
}
req, err := client.NewGetRequest(ctx, pool.New(0, 0), href, opts...)
if err != nil {
errors <- MakeInternal(fmt.Errorf("device discovery request creation failed: %w", err))
return
}
err = conn.PublishMsgWithContext(req, handler)
if err != nil {
select {
case errors <- MakeInternal(fmt.Errorf("device discovery multicast request failed: %w", err)):
case <-ctx.Done():
}
return
}
<-ctx.Done()
}()
}
}