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

upgrade to github.com/plgd-dev/go-coap/v3 #278

Merged
merged 15 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from 14 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
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Set up Go 1.18
uses: actions/setup-go@v3
with:
go-version: "^1.18" # The Go version to download (if necessary) and use.
go-version: "1.18" # The Go version to download (if necessary) and use.

- run: go version

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/staticAnalysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Set up Go 1.18
uses: actions/setup-go@v3
with:
go-version: "^1.18" # The Go version to download (if necessary) and use.
go-version: "1.18" # The Go version to download (if necessary) and use.

- run: go version

Expand Down
7 changes: 7 additions & 0 deletions .github/workflows/test-for-fork.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ jobs:
# Checks-out repository under $GITHUB_WORKSPACE
- uses: actions/checkout@v3

- name: Set up Go 1.18
uses: actions/setup-go@v3
with:
go-version: "1.18" # The Go version to download (if necessary) and use.

- run: go version

- name: Setup kernel for multicast messages, increase read buffer to 8MB
run: sudo sysctl -w net.core.rmem_max=8388608

Expand Down
7 changes: 7 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ jobs:
if: ${{ !matrix.coverage }}
uses: actions/checkout@v3

- name: Set up Go 1.18
uses: actions/setup-go@v3
with:
go-version: "1.18" # The Go version to download (if necessary) and use.

- run: go version

- name: Setup kernel for multicast messages, increase read buffer to 8MB
run: sudo sysctl -w net.core.rmem_max=8388608

Expand Down
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
"IDENTITY_CRT": "${workspaceFolder}/.tmp/pki_certs/identitycrt.pem",
"IDENTITY_KEY": "${workspaceFolder}/.tmp/pki_certs/identitykey.pem",
},
"files.watcherExclude": {
"**/plgd-dev/device/v2/**": true
},
"go.testTimeout": "600s",
}
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ env: clean certificates
unit-test:
mkdir -p $(TMP_PATH)
go test -race -v ./schema/... -covermode=atomic -coverprofile=$(TMP_PATH)/schema.coverage.txt
go test -race -v ./pkg/... -covermode=atomic -coverprofile=$(TMP_PATH)/pkg.coverage.txt

test: env build-testcontainer
docker run \
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

# Device

The **client** enables interacting with devices in a local network:
- Listing devices
The **client** enables interaction with devices in a local network:

- Listing devices
- Retrieving and updating resources
- Secure ownership transfer via coaps+tcp and coaps
- Onboard and offboard device
Expand Down
86 changes: 53 additions & 33 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ import (
"time"

"github.com/pion/dtls/v2"
"github.com/plgd-dev/device/client/core"
"github.com/plgd-dev/device/client/core/otm"
"github.com/plgd-dev/device/pkg/net/coap"
"github.com/plgd-dev/go-coap/v2/net/blockwise"
kitSync "github.com/plgd-dev/kit/v2/sync"
"github.com/plgd-dev/device/v2/client/core"
"github.com/plgd-dev/device/v2/client/core/otm"
"github.com/plgd-dev/device/v2/pkg/net/coap"
"github.com/plgd-dev/go-coap/v3/net/blockwise"
"github.com/plgd-dev/go-coap/v3/options"
coapSync "github.com/plgd-dev/go-coap/v3/pkg/sync"
"github.com/plgd-dev/go-coap/v3/tcp"
tcpClient "github.com/plgd-dev/go-coap/v3/tcp/client"
"github.com/plgd-dev/go-coap/v3/udp"
udpClient "github.com/plgd-dev/go-coap/v3/udp/client"
)

type ApplicationCallback = interface {
Expand Down Expand Up @@ -57,48 +62,63 @@ func NewClientFromConfig(cfg *Config, app ApplicationCallback, errors func(error
if cfg.ObserverPollingIntervalSeconds > 0 {
observerPollingInterval = time.Second * time.Duration(cfg.ObserverPollingIntervalSeconds)
}
dialOpts := make([]coap.DialOptionFunc, 0, 5)
if cfg.KeepAliveConnectionTimeoutSeconds > 0 {
dialOpts = append(dialOpts, coap.WithKeepAlive(time.Second*time.Duration(cfg.KeepAliveConnectionTimeoutSeconds)))
} else {
dialOpts = append(dialOpts, coap.WithKeepAlive(time.Second*60))
}
if cfg.MaxMessageSize > 0 {
dialOpts = append(dialOpts, coap.WithMaxMessageSize(cfg.MaxMessageSize))
} else {
dialOpts = append(dialOpts, coap.WithMaxMessageSize(512*1024))
}
if cfg.DisablePeerTCPSignalMessageCSMs {
dialOpts = append(dialOpts, coap.WithDialDisablePeerTCPSignalMessageCSMs())
}

tcpDialOpts := make([]tcp.Option, 0, 5)
udpDialOpts := make([]udp.Option, 0, 5)

errFn := func(error) {
// ignore error
}
if errors != nil {
errFn = errors
}
dialOpts = append(dialOpts, coap.WithErrors(errFn))
tcpDialOpts = append(tcpDialOpts, options.WithErrors(errFn))

keepAliveConnectionTimeoutSeconds := time.Second * 60
if cfg.KeepAliveConnectionTimeoutSeconds > 0 {
keepAliveConnectionTimeoutSeconds = time.Second * time.Duration(cfg.KeepAliveConnectionTimeoutSeconds)
}
tcpDialOpts = append(tcpDialOpts, options.WithKeepAlive(3, keepAliveConnectionTimeoutSeconds/3, func(cc *tcpClient.Conn) {
errFn(fmt.Errorf("keepalive failed for tcp: %v", cc.RemoteAddr()))
cc.Close()
}))
udpDialOpts = append(udpDialOpts, options.WithKeepAlive(3, keepAliveConnectionTimeoutSeconds/3, func(cc *udpClient.Conn) {
errFn(fmt.Errorf("keepalive failed for udp: %v", cc.RemoteAddr()))
cc.Close()
}))

maxMessageSize := uint32(512 * 1024)
if cfg.MaxMessageSize > 0 {
maxMessageSize = cfg.MaxMessageSize
}
tcpDialOpts = append(tcpDialOpts, options.WithMaxMessageSize(maxMessageSize))
udpDialOpts = append(udpDialOpts, options.WithMaxMessageSize(maxMessageSize))

if cfg.DisablePeerTCPSignalMessageCSMs {
tcpDialOpts = append(tcpDialOpts, options.WithDisablePeerTCPSignalMessageCSMs())
}

defaultTransferDuration := time.Second * 15
if cfg.DefaultTransferDurationSeconds > 0 {
dialOpts = append(dialOpts, coap.WithBlockwise(true, blockwise.SZX1024, time.Second*time.Duration(cfg.DefaultTransferDurationSeconds)))
} else {
dialOpts = append(dialOpts, coap.WithBlockwise(true, blockwise.SZX1024, 15*time.Second))
defaultTransferDuration = time.Second * time.Duration(cfg.DefaultTransferDurationSeconds)
}
tcpDialOpts = append(tcpDialOpts, options.WithBlockwise(true, blockwise.SZX1024, defaultTransferDuration))
udpDialOpts = append(udpDialOpts, options.WithBlockwise(true, blockwise.SZX1024, defaultTransferDuration))

dialTLS := func(ctx context.Context, addr string, tlsCfg *tls.Config, opts ...coap.DialOptionFunc) (*coap.ClientCloseHandler, error) {
opts = append(opts, dialOpts...)
dialTLS := func(ctx context.Context, addr string, tlsCfg *tls.Config, opts ...tcp.Option) (*coap.ClientCloseHandler, error) {
opts = append(opts, tcpDialOpts...)
return coap.DialTCPSecure(ctx, addr, tlsCfg, opts...)
}
dialDTLS := func(ctx context.Context, addr string, dtlsCfg *dtls.Config, opts ...coap.DialOptionFunc) (*coap.ClientCloseHandler, error) {
opts = append(opts, dialOpts...)
dialDTLS := func(ctx context.Context, addr string, dtlsCfg *dtls.Config, opts ...udp.Option) (*coap.ClientCloseHandler, error) {
opts = append(opts, udpDialOpts...)
return coap.DialUDPSecure(ctx, addr, dtlsCfg, opts...)
}
dialTCP := func(ctx context.Context, addr string, opts ...coap.DialOptionFunc) (*coap.ClientCloseHandler, error) {
opts = append(opts, dialOpts...)
dialTCP := func(ctx context.Context, addr string, opts ...tcp.Option) (*coap.ClientCloseHandler, error) {
opts = append(opts, tcpDialOpts...)
return coap.DialTCP(ctx, addr, opts...)
}
dialUDP := func(ctx context.Context, addr string, opts ...coap.DialOptionFunc) (*coap.ClientCloseHandler, error) {
opts = append(opts, dialOpts...)
dialUDP := func(ctx context.Context, addr string, opts ...udp.Option) (*coap.ClientCloseHandler, error) {
opts = append(opts, udpDialOpts...)
return coap.DialUDP(ctx, addr, opts...)
}

Expand Down Expand Up @@ -149,7 +169,7 @@ func NewClient(
client: oc,
app: app,
deviceCache: NewRefDeviceCache(cacheExpiration, errors),
observeResourceCache: kitSync.NewMap(),
observeResourceCache: coapSync.NewMap[string, *observationsHandler](),
deviceOwner: deviceOwner,
subscriptions: make(map[string]subscription),
observerPollingInterval: observerPollingInterval,
Expand All @@ -175,7 +195,7 @@ type Client struct {

deviceCache *refDeviceCache

observeResourceCache *kitSync.Map
observeResourceCache *coapSync.Map[string, *observationsHandler]
observerPollingInterval time.Duration

deviceOwner DeviceOwner
Expand Down
4 changes: 2 additions & 2 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"testing"
"time"

"github.com/plgd-dev/device/client"
"github.com/plgd-dev/device/test"
"github.com/plgd-dev/device/v2/client"
"github.com/plgd-dev/device/v2/test"
"github.com/stretchr/testify/require"
)

Expand Down
16 changes: 9 additions & 7 deletions client/core/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
"net"

"github.com/pion/dtls/v2"
pkgError "github.com/plgd-dev/device/pkg/error"
"github.com/plgd-dev/device/pkg/net/coap"
coapNet "github.com/plgd-dev/go-coap/v2/net"
pkgError "github.com/plgd-dev/device/v2/pkg/error"
"github.com/plgd-dev/device/v2/pkg/net/coap"
coapNet "github.com/plgd-dev/go-coap/v3/net"
"github.com/plgd-dev/go-coap/v3/tcp"
"github.com/plgd-dev/go-coap/v3/udp"
"github.com/plgd-dev/kit/v2/log"
)

Expand Down Expand Up @@ -81,10 +83,10 @@ func WithErr(errFunc ErrFunc) OptionFunc {
}

type (
DialDTLS = func(ctx context.Context, addr string, dtlsCfg *dtls.Config, opts ...coap.DialOptionFunc) (*coap.ClientCloseHandler, error)
DialTLS = func(ctx context.Context, addr string, tlsCfg *tls.Config, opts ...coap.DialOptionFunc) (*coap.ClientCloseHandler, error)
DialUDP = func(ctx context.Context, addr string, opts ...coap.DialOptionFunc) (*coap.ClientCloseHandler, error)
DialTCP = func(ctx context.Context, addr string, opts ...coap.DialOptionFunc) (*coap.ClientCloseHandler, error)
DialDTLS = func(ctx context.Context, addr string, dtlsCfg *dtls.Config, opts ...udp.Option) (*coap.ClientCloseHandler, error)
DialTLS = func(ctx context.Context, addr string, tlsCfg *tls.Config, opts ...tcp.Option) (*coap.ClientCloseHandler, error)
DialUDP = func(ctx context.Context, addr string, opts ...udp.Option) (*coap.ClientCloseHandler, error)
DialTCP = func(ctx context.Context, addr string, opts ...tcp.Option) (*coap.ClientCloseHandler, error)
)

func WithDialDTLS(dial DialDTLS) OptionFunc {
Expand Down
26 changes: 14 additions & 12 deletions client/core/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ import (
"time"

"github.com/pion/dtls/v2"
"github.com/plgd-dev/device/client/core"
"github.com/plgd-dev/device/client/core/otm"
justworks "github.com/plgd-dev/device/client/core/otm/just-works"
"github.com/plgd-dev/device/client/core/otm/manufacturer"
pkgError "github.com/plgd-dev/device/pkg/error"
"github.com/plgd-dev/device/pkg/net/coap"
"github.com/plgd-dev/device/schema"
"github.com/plgd-dev/device/test"
"github.com/plgd-dev/device/v2/client/core"
"github.com/plgd-dev/device/v2/client/core/otm"
justworks "github.com/plgd-dev/device/v2/client/core/otm/just-works"
"github.com/plgd-dev/device/v2/client/core/otm/manufacturer"
pkgError "github.com/plgd-dev/device/v2/pkg/error"
"github.com/plgd-dev/device/v2/pkg/net/coap"
"github.com/plgd-dev/device/v2/schema"
"github.com/plgd-dev/device/v2/test"
"github.com/plgd-dev/go-coap/v3/tcp"
"github.com/plgd-dev/go-coap/v3/udp"
"github.com/plgd-dev/kit/v2/security"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -79,12 +81,12 @@ func NewTestSecureClientWithCert(cert tls.Certificate, disableDTLS, disableTCPTL

var manOpts []manufacturer.OptionFunc
if disableDTLS {
manOpts = append(manOpts, manufacturer.WithDialDTLS(func(ctx context.Context, addr string, dtlsCfg *dtls.Config, opts ...coap.DialOptionFunc) (*coap.ClientCloseHandler, error) {
manOpts = append(manOpts, manufacturer.WithDialDTLS(func(ctx context.Context, addr string, dtlsCfg *dtls.Config, opts ...udp.Option) (*coap.ClientCloseHandler, error) {
return nil, pkgError.NotSupported()
}))
}
if disableTCPTLS {
manOpts = append(manOpts, manufacturer.WithDialTLS(func(ctx context.Context, addr string, tlsCfg *tls.Config, opts ...coap.DialOptionFunc) (*coap.ClientCloseHandler, error) {
manOpts = append(manOpts, manufacturer.WithDialTLS(func(ctx context.Context, addr string, tlsCfg *tls.Config, opts ...tcp.Option) (*coap.ClientCloseHandler, error) {
return nil, pkgError.NotSupported()
}))
}
Expand All @@ -94,12 +96,12 @@ func NewTestSecureClientWithCert(cert tls.Certificate, disableDTLS, disableTCPTL

var opts []core.OptionFunc
if disableDTLS {
opts = append(opts, core.WithDialDTLS(func(ctx context.Context, addr string, dtlsCfg *dtls.Config, opts ...coap.DialOptionFunc) (*coap.ClientCloseHandler, error) {
opts = append(opts, core.WithDialDTLS(func(ctx context.Context, addr string, dtlsCfg *dtls.Config, opts ...udp.Option) (*coap.ClientCloseHandler, error) {
return nil, pkgError.NotSupported()
}))
}
if disableTCPTLS {
opts = append(opts, core.WithDialTLS(func(ctx context.Context, addr string, tlsCfg *tls.Config, opts ...coap.DialOptionFunc) (*coap.ClientCloseHandler, error) {
opts = append(opts, core.WithDialTLS(func(ctx context.Context, addr string, tlsCfg *tls.Config, opts ...tcp.Option) (*coap.ClientCloseHandler, error) {
return nil, pkgError.NotSupported()
}))
}
Expand Down
6 changes: 3 additions & 3 deletions client/core/deleteResource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"context"
"fmt"

"github.com/plgd-dev/device/pkg/net/coap"
"github.com/plgd-dev/device/schema"
"github.com/plgd-dev/kit/v2/codec/ocf"
"github.com/plgd-dev/device/v2/pkg/codec/ocf"
"github.com/plgd-dev/device/v2/pkg/net/coap"
"github.com/plgd-dev/device/v2/schema"
)

func (d *Device) DeleteResource(
Expand Down
4 changes: 2 additions & 2 deletions client/core/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"sync/atomic"

"github.com/pion/dtls/v2"
"github.com/plgd-dev/device/pkg/net/coap"
"github.com/plgd-dev/device/schema"
"github.com/plgd-dev/device/v2/pkg/net/coap"
"github.com/plgd-dev/device/v2/schema"
"github.com/plgd-dev/kit/v2/net"
"golang.org/x/sync/semaphore"
)
Expand Down
Loading