Skip to content

Commit

Permalink
bridge: add maintenance resource
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Jan 22, 2024
1 parent 6ce95a6 commit 7368672
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 7 deletions.
2 changes: 1 addition & 1 deletion bridge/device/cloud/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ func (c *Manager) run() {
func (c *Manager) connect(ctx context.Context) error {
funcs := []func(ctx context.Context) error{
c.signUp,
c.refreshToken,
// c.refreshToken,
c.signIn,
c.publishResources,
}
Expand Down
8 changes: 8 additions & 0 deletions bridge/device/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ import (
"github.com/plgd-dev/device/v2/bridge/resources"
cloudResource "github.com/plgd-dev/device/v2/bridge/resources/cloud"
resourcesDevice "github.com/plgd-dev/device/v2/bridge/resources/device"
"github.com/plgd-dev/device/v2/bridge/resources/maintenance"
"github.com/plgd-dev/device/v2/schema"
cloudSchema "github.com/plgd-dev/device/v2/schema/cloud"
plgdDevice "github.com/plgd-dev/device/v2/schema/device"
maintenanceSchema "github.com/plgd-dev/device/v2/schema/maintenance"
"github.com/plgd-dev/go-coap/v3/message"
"github.com/plgd-dev/go-coap/v3/message/codes"
"github.com/plgd-dev/go-coap/v3/message/pool"
Expand Down Expand Up @@ -123,6 +125,12 @@ func New(cfg Config, onDeviceUpdated func(d *Device), additionalProperties resou
}
d.AddResource(resourcesDevice.New(plgdDevice.ResourceURI, d, additionalProperties))

d.AddResource(maintenance.New(maintenanceSchema.ResourceURI, func() {
if d.cloudManager != nil {
d.cloudManager.Unregister()
}
}))

if cfg.Cloud.Enabled {
d.cloudManager = cloud.New(d.cfg.ID, func() {
d.onDeviceUpdated(d)
Expand Down
69 changes: 69 additions & 0 deletions bridge/resources/maintenance/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/****************************************************************************
*
* Copyright (c) 2024 plgd.dev s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License"),
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*
****************************************************************************/

package maintenance

import (
"github.com/plgd-dev/device/v2/bridge/net"
"github.com/plgd-dev/device/v2/bridge/resources"
"github.com/plgd-dev/device/v2/pkg/codec/cbor"
"github.com/plgd-dev/device/v2/schema/interfaces"
"github.com/plgd-dev/device/v2/schema/maintenance"
"github.com/plgd-dev/go-coap/v3/message/codes"
"github.com/plgd-dev/go-coap/v3/message/pool"
)

type OnFactoryReset func()

type Resource struct {
*resources.Resource
onFactoryReset OnFactoryReset
}

func (r *Resource) Get(request *net.Request) (*pool.Message, error) {
return resources.CreateResponseContent(request.Context(), maintenance.Maintenance{
FactoryReset: false,
}, codes.Content)
}

func (r *Resource) Post(request *net.Request) (*pool.Message, error) {
var upd maintenance.MaintenanceUpdateRequest
err := cbor.ReadFrom(request.Body(), &upd)
if err != nil {
return resources.CreateResponseBadRequest(request.Context(), err)
}
if upd.FactoryReset {
r.onFactoryReset()
}
return resources.CreateResponseContent(request.Context(), maintenance.Maintenance{
FactoryReset: false,
}, codes.Changed)
}

func New(uri string, onFactoryReset OnFactoryReset) *Resource {
r := &Resource{
onFactoryReset: onFactoryReset,
}
r.Resource = resources.NewResource(uri,
r.Get,
r.Post,
[]string{maintenance.ResourceType},
[]string{interfaces.OC_IF_BASELINE, interfaces.OC_IF_RW},
)
return r
}
87 changes: 87 additions & 0 deletions bridge/resources/maintenance/resource_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/****************************************************************************
*
* Copyright (c) 2024 plgd.dev s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License"),
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*
****************************************************************************/

package maintenance_test

import (
"bytes"
"context"
"testing"

"github.com/plgd-dev/device/v2/bridge/net"
"github.com/plgd-dev/device/v2/bridge/resources/maintenance"
"github.com/plgd-dev/device/v2/pkg/codec/cbor"
maintenanceSchema "github.com/plgd-dev/device/v2/schema/maintenance"
"github.com/plgd-dev/go-coap/v3/message"
"github.com/plgd-dev/go-coap/v3/message/codes"
"github.com/plgd-dev/go-coap/v3/message/pool"
"github.com/stretchr/testify/require"
)

func TestMaintenanceGet(t *testing.T) {
mnt := maintenance.New(maintenanceSchema.ResourceURI, func() {})
require.NotNil(t, mnt)

req := pool.NewMessage(context.Background())
req.SetContentFormat(message.AppOcfCbor)
resp, err := mnt.Get(&net.Request{
Message: req,
})
require.NoError(t, err)

var mntData maintenanceSchema.Maintenance
err = cbor.ReadFrom(resp.Body(), &mntData)
require.NoError(t, err)
require.False(t, mntData.FactoryReset)
}

func TestMaintenancePost(t *testing.T) {
invoked := false
mnt := maintenance.New(maintenanceSchema.ResourceURI, func() {
invoked = true
})
require.NotNil(t, mnt)

reqInvalid := pool.NewMessage(context.Background())
reqInvalid.SetContentFormat(message.TextPlain)
reqInvalid.SetBody(bytes.NewReader([]byte("")))
resp, err := mnt.Post(&net.Request{
Message: reqInvalid,
})
require.NoError(t, err)
require.Equal(t, codes.BadRequest, resp.Code())
require.False(t, invoked)

d, err := cbor.Encode(maintenanceSchema.MaintenanceUpdateRequest{
FactoryReset: true,
})
require.NoError(t, err)
req := pool.NewMessage(context.Background())
req.SetContentFormat(message.AppOcfCbor)
req.SetBody(bytes.NewReader(d))
resp, err = mnt.Post(&net.Request{
Message: req,
})
require.NoError(t, err)
require.Equal(t, codes.Changed, resp.Code())
require.True(t, invoked)
var mntData maintenanceSchema.Maintenance
err = cbor.ReadFrom(resp.Body(), &mntData)
require.NoError(t, err)
require.False(t, mntData.FactoryReset)
}
10 changes: 7 additions & 3 deletions client/core/maintenance.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,29 @@ import (
"fmt"
"net/http"

"github.com/plgd-dev/device/v2/pkg/net/coap"
"github.com/plgd-dev/device/v2/schema"
"github.com/plgd-dev/device/v2/schema/maintenance"
)

func (d *Device) Reboot(
ctx context.Context,
links schema.ResourceLinks,
options ...coap.OptionFunc,
) error {
return d.updateMaintenanceResource(ctx, links, maintenance.MaintenanceUpdateRequest{
Reboot: true,
})
}, options...)
}

func (d *Device) FactoryReset(
ctx context.Context,
links schema.ResourceLinks,
options ...coap.OptionFunc,
) error {
err := d.updateMaintenanceResource(ctx, links, maintenance.MaintenanceUpdateRequest{
FactoryReset: true,
})
}, options...)
if connectionWasClosed(ctx, err) {
// connection was closed by disown so we don't report error just log it.
d.cfg.Logger.Debug(err.Error())
Expand All @@ -53,13 +56,14 @@ func (d *Device) updateMaintenanceResource(
ctx context.Context,
links schema.ResourceLinks,
req maintenance.MaintenanceUpdateRequest,
options ...coap.OptionFunc,
) (ret error) {
links = links.GetResourceLinks(maintenance.ResourceType)
if len(links) == 0 {
return MakeUnavailable(fmt.Errorf("cannot find '%v' in %+v", maintenance.ResourceType, links))
}
var resp maintenance.Maintenance
err := d.UpdateResource(ctx, links[0], req, &resp)
err := d.UpdateResource(ctx, links[0], req, &resp, options...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion client/disownDevice.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (c *Client) DisownDevice(ctx context.Context, deviceID string, opts ...Comm

ok := d.IsSecured()
if !ok {
return d.FactoryReset(ctx, links)
return d.FactoryReset(ctx, links, cfg.opts...)
}

return d.Disown(ctx, links)
Expand Down
4 changes: 2 additions & 2 deletions client/maitenance.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (c *Client) FactoryReset(ctx context.Context, deviceID string, opts ...Comm
return err
}
defer c.removeTemporaryDeviceFromCache(ctx, d)
return d.FactoryReset(ctx, links)
return d.FactoryReset(ctx, links, cfg.opts...)
}

// Reboot reboots the device.
Expand All @@ -39,5 +39,5 @@ func (c *Client) Reboot(ctx context.Context, deviceID string, opts ...CommonComm
return err
}
defer c.removeTemporaryDeviceFromCache(ctx, d)
return d.Reboot(ctx, links)
return d.Reboot(ctx, links, cfg.opts...)
}

0 comments on commit 7368672

Please sign in to comment.