Skip to content

Commit

Permalink
Add x.org.iotivity.version to schema.Platform
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Nov 6, 2023
1 parent be15c59 commit 0977084
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
18 changes: 18 additions & 0 deletions schema/platform/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
// https://github.com/openconnectivityfoundation/core/blob/master/swagger2.0/oic.wk.p.swagger.json
package platform

import "fmt"

const (
ResourceType = "oic.wk.p"
ResourceURI = "/oic/p"
Expand All @@ -34,4 +36,20 @@ type Platform struct {
ModelNumber string `json:"mnmo,omitempty"`
ManufacturersDefinedInformation string `json:"vid,omitempty"`
PlatformVersion string `json:"mnpv,omitempty"`

// custom properties
Version uint32 `json:"x.org.iotivity.version,omitempty"`
}

func (p Platform) GetVersion() (uint8, uint8, uint8, uint8) {
major := uint8((p.Version >> 24) & 0xFF)
minor := uint8((p.Version >> 16) & 0xFF)
patch := uint8((p.Version >> 8) & 0xFF)
bugfix := uint8(p.Version & 0xFF)
return major, minor, patch, bugfix
}

func (p Platform) GetVersionString() string {
major, minor, patch, bugfix := p.GetVersion()
return fmt.Sprintf("%d.%d.%d.%d", major, minor, patch, bugfix)
}
95 changes: 95 additions & 0 deletions schema/platform/platform_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// ************************************************************************
// Copyright (C) 2022 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 platform_test

import (
"testing"

"github.com/plgd-dev/device/v2/schema/platform"
"github.com/stretchr/testify/require"
)

func TestPlatformGetVersion(t *testing.T) {
type args struct {
version uint32
}
tests := []struct {
name string
args args
wantMajor uint8
wantMinor uint8
wantPatch uint8
wantBugfix uint8
}{
{
name: "Valid Version",
args: args{version: 0x01020304},
wantMajor: 0x01,
wantMinor: 0x02,
wantPatch: 0x03,
wantBugfix: 0x04,
},
{
name: "Zero Version",
args: args{version: 0},
wantMajor: 0,
wantMinor: 0,
wantPatch: 0,
wantBugfix: 0,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := platform.Platform{Version: tt.args.version}
major, minor, patch, bugfix := p.GetVersion()
require.Equal(t, tt.wantMajor, major)
require.Equal(t, tt.wantMinor, minor)
require.Equal(t, tt.wantPatch, patch)
require.Equal(t, tt.wantBugfix, bugfix)
})
}
}

func TestPlatformGetVersionString(t *testing.T) {
type args struct {
version uint32
}
tests := []struct {
name string
args args
expected string
}{
{
name: "Valid Version",
args: args{version: 0x0202050B},
expected: "2.2.5.11",
},
{
name: "Zero Version",
args: args{version: 0},
expected: "0.0.0.0",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
p := platform.Platform{Version: test.args.version}
require.Equal(t, test.expected, p.GetVersionString())
})
}
}

0 comments on commit 0977084

Please sign in to comment.