@@ -23,6 +23,7 @@ import (
23
23
"crypto/tls"
24
24
"crypto/x509"
25
25
"fmt"
26
+ "reflect"
26
27
"sync"
27
28
"time"
28
29
@@ -31,6 +32,7 @@ import (
31
32
"github.com/plgd-dev/device/v2/bridge/resources"
32
33
"github.com/plgd-dev/device/v2/bridge/resources/discovery"
33
34
"github.com/plgd-dev/device/v2/pkg/codec/cbor"
35
+ "github.com/plgd-dev/device/v2/pkg/eventloop"
34
36
"github.com/plgd-dev/device/v2/pkg/log"
35
37
"github.com/plgd-dev/device/v2/pkg/net/coap"
36
38
ocfCloud "github.com/plgd-dev/device/v2/pkg/ocf/cloud"
@@ -91,9 +93,10 @@ type Manager struct {
91
93
resourcesPublished bool
92
94
done chan struct {}
93
95
trigger chan bool
96
+ loop * eventloop.Loop
94
97
}
95
98
96
- func New (cfg Config , deviceID uuid.UUID , save func (), handler net.RequestHandler , getLinks GetLinksFilteredBy , caPool CAPoolGetter , opts ... Option ) (* Manager , error ) {
99
+ func New (cfg Config , deviceID uuid.UUID , save func (), handler net.RequestHandler , getLinks GetLinksFilteredBy , caPool CAPoolGetter , loop * eventloop. Loop , opts ... Option ) (* Manager , error ) {
97
100
if ! caPool .IsValid () {
98
101
return nil , fmt .Errorf ("invalid ca pool" )
99
102
}
@@ -123,6 +126,7 @@ func New(cfg Config, deviceID uuid.UUID, save func(), handler net.RequestHandler
123
126
getCertificates : o .getCertificates ,
124
127
removeCloudCAs : o .removeCloudCAs ,
125
128
logger : o .logger ,
129
+ loop : loop ,
126
130
}
127
131
c .private .cfg .ProvisioningStatus = cloud .ProvisioningStatus_UNINITIALIZED
128
132
c .importConfig (cfg )
@@ -162,11 +166,55 @@ func (c *Manager) importConfig(cfg Config) {
162
166
})
163
167
}
164
168
169
+ func (c * Manager ) handleTrigger (value reflect.Value , closed bool ) {
170
+ if closed {
171
+ return
172
+ }
173
+ ctx := context .Background ()
174
+ wantToReset := value .Bool ()
175
+ if wantToReset {
176
+ c .resetCredentials (ctx , true )
177
+ }
178
+ if c .getCloudConfiguration ().URL == "" {
179
+ return
180
+ }
181
+ if err := c .connect (ctx ); err != nil {
182
+ c .logger .Errorf ("cannot connect to cloud: %w" , err )
183
+ } else {
184
+ c .setProvisioningStatus (cloud .ProvisioningStatus_REGISTERED )
185
+ }
186
+ }
187
+
188
+ func (c * Manager ) handleTimer (_ reflect.Value , closed bool ) {
189
+ if closed {
190
+ return
191
+ }
192
+ if c .getCloudConfiguration ().URL == "" {
193
+ return
194
+ }
195
+ if err := c .connect (context .Background ()); err != nil {
196
+ c .logger .Errorf ("cannot connect to cloud: %w" , err )
197
+ } else {
198
+ c .setProvisioningStatus (cloud .ProvisioningStatus_REGISTERED )
199
+ }
200
+ }
201
+
165
202
func (c * Manager ) Init () {
166
203
if c .private .cfg .URL != "" {
167
204
c .triggerRunner (false )
168
205
}
169
- go c .run ()
206
+ t := time .NewTicker (time .Second * 10 )
207
+ handlers := []eventloop.Handler {
208
+ eventloop .NewReadHandler (reflect .ValueOf (c .trigger ), c .handleTrigger ),
209
+ eventloop .NewReadHandler (reflect .ValueOf (t .C ), c .handleTimer ),
210
+ eventloop .NewReadHandler (reflect .ValueOf (c .done ), func (_ reflect.Value , _ bool ) {
211
+ _ = c .close ()
212
+ // cleanup resources
213
+ c .loop .RemoveByChannels (reflect .ValueOf (c .done ), reflect .ValueOf (t .C ), reflect .ValueOf (c .trigger ))
214
+ t .Stop ()
215
+ }),
216
+ }
217
+ c .loop .Add (handlers ... )
170
218
}
171
219
172
220
func (c * Manager ) resetCredentials (ctx context.Context , signOff bool ) {
@@ -371,6 +419,7 @@ func (c *Manager) dial(ctx context.Context) error {
371
419
}
372
420
tlsConfig := & tls.Config {
373
421
InsecureSkipVerify : true , //nolint:gosec
422
+ MinVersion : tls .VersionTLS12 ,
374
423
Certificates : c .getCertificates (c .deviceID .String ()),
375
424
VerifyPeerCertificate : coap .NewVerifyPeerCertificate (caPool , func (cert * x509.Certificate ) error {
376
425
cloudID , errP := uuid .Parse (c .getCloudConfiguration ().CloudID )
@@ -426,34 +475,6 @@ func patchDeviceLink(links schema.ResourceLinks) schema.ResourceLinks {
426
475
return links
427
476
}
428
477
429
- func (c * Manager ) run () {
430
- ctx := context .Background ()
431
- defer func () {
432
- if err := c .close (); err != nil {
433
- c .logger .Warnf ("cannot close connection: %w" , err )
434
- }
435
- }()
436
- t := time .NewTicker (time .Second * 10 )
437
- for {
438
- select {
439
- case <- c .done :
440
- return
441
- case wantToReset := <- c .trigger :
442
- if wantToReset {
443
- c .resetCredentials (ctx , true )
444
- }
445
- case <- t .C :
446
- }
447
- if c .getCloudConfiguration ().URL != "" {
448
- if err := c .connect (ctx ); err != nil {
449
- c .logger .Errorf ("cannot connect to cloud: %w" , err )
450
- } else {
451
- c .setProvisioningStatus (cloud .ProvisioningStatus_REGISTERED )
452
- }
453
- }
454
- }
455
- }
456
-
457
478
func (c * Manager ) connect (ctx context.Context ) error {
458
479
var funcs []func (ctx context.Context ) error
459
480
if c .isCredsExpiring () {
0 commit comments