@@ -23,6 +23,7 @@ import (
2323 "crypto/tls"
2424 "crypto/x509"
2525 "fmt"
26+ "reflect"
2627 "sync"
2728 "time"
2829
@@ -31,6 +32,7 @@ import (
3132 "github.com/plgd-dev/device/v2/bridge/resources"
3233 "github.com/plgd-dev/device/v2/bridge/resources/discovery"
3334 "github.com/plgd-dev/device/v2/pkg/codec/cbor"
35+ "github.com/plgd-dev/device/v2/pkg/eventloop"
3436 "github.com/plgd-dev/device/v2/pkg/log"
3537 "github.com/plgd-dev/device/v2/pkg/net/coap"
3638 ocfCloud "github.com/plgd-dev/device/v2/pkg/ocf/cloud"
@@ -91,9 +93,10 @@ type Manager struct {
9193 resourcesPublished bool
9294 done chan struct {}
9395 trigger chan bool
96+ loop * eventloop.Loop
9497}
9598
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 ) {
97100 if ! caPool .IsValid () {
98101 return nil , fmt .Errorf ("invalid ca pool" )
99102 }
@@ -123,6 +126,7 @@ func New(cfg Config, deviceID uuid.UUID, save func(), handler net.RequestHandler
123126 getCertificates : o .getCertificates ,
124127 removeCloudCAs : o .removeCloudCAs ,
125128 logger : o .logger ,
129+ loop : loop ,
126130 }
127131 c .private .cfg .ProvisioningStatus = cloud .ProvisioningStatus_UNINITIALIZED
128132 c .importConfig (cfg )
@@ -162,11 +166,55 @@ func (c *Manager) importConfig(cfg Config) {
162166 })
163167}
164168
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+
165202func (c * Manager ) Init () {
166203 if c .private .cfg .URL != "" {
167204 c .triggerRunner (false )
168205 }
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 ... )
170218}
171219
172220func (c * Manager ) resetCredentials (ctx context.Context , signOff bool ) {
@@ -371,6 +419,7 @@ func (c *Manager) dial(ctx context.Context) error {
371419 }
372420 tlsConfig := & tls.Config {
373421 InsecureSkipVerify : true , //nolint:gosec
422+ MinVersion : tls .VersionTLS12 ,
374423 Certificates : c .getCertificates (c .deviceID .String ()),
375424 VerifyPeerCertificate : coap .NewVerifyPeerCertificate (caPool , func (cert * x509.Certificate ) error {
376425 cloudID , errP := uuid .Parse (c .getCloudConfiguration ().CloudID )
@@ -426,34 +475,6 @@ func patchDeviceLink(links schema.ResourceLinks) schema.ResourceLinks {
426475 return links
427476}
428477
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-
457478func (c * Manager ) connect (ctx context.Context ) error {
458479 var funcs []func (ctx context.Context ) error
459480 if c .isCredsExpiring () {
0 commit comments