From 097708433371415ed2ab408d7479d60487b92747 Mon Sep 17 00:00:00 2001 From: Daniel Adam Date: Mon, 6 Nov 2023 10:14:48 +0100 Subject: [PATCH] Add x.org.iotivity.version to schema.Platform --- schema/platform/platform.go | 18 ++++++ schema/platform/platform_test.go | 95 ++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 schema/platform/platform_test.go diff --git a/schema/platform/platform.go b/schema/platform/platform.go index 2ec93ed4..84f3098a 100644 --- a/schema/platform/platform.go +++ b/schema/platform/platform.go @@ -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" @@ -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) } diff --git a/schema/platform/platform_test.go b/schema/platform/platform_test.go new file mode 100644 index 00000000..d0ed2247 --- /dev/null +++ b/schema/platform/platform_test.go @@ -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()) + }) + } +}