-
Notifications
You must be signed in to change notification settings - Fork 301
/
schema.go
330 lines (300 loc) · 10.3 KB
/
schema.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// Copyright © 2020 The Things Network Foundation, The Things Industries B.V.
//
// 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 remote
import (
"google.golang.org/protobuf/types/known/wrapperspb"
"go.thethings.network/lorawan-stack/v3/pkg/ttnpb"
)
// Vendor is an end device vendor.
type Vendor struct {
ID string `yaml:"id"`
Name string `yaml:"name"`
VendorID uint32 `yaml:"vendorID"`
Draft bool `yaml:"draft,omitempty"`
Email string `yaml:"email"`
Website string `yaml:"website"`
PEN uint32 `yaml:"pen"`
OUIs []string `yaml:"ouis"`
Logo string `yaml:"logo"`
}
// ToPB creates a ttnpb.EndDeviceBrand protocol buffer from a Vendor.
func (v Vendor) ToPB(paths ...string) (*ttnpb.EndDeviceBrand, error) {
pb := &ttnpb.EndDeviceBrand{
BrandId: v.ID,
Name: v.Name,
LoraAllianceVendorId: v.VendorID,
Email: v.Email,
Website: v.Website,
Logo: v.Logo,
PrivateEnterpriseNumber: v.PEN,
OrganizationUniqueIdentifiers: v.OUIs,
}
res := &ttnpb.EndDeviceBrand{}
if err := res.SetFields(pb, paths...); err != nil {
return nil, err
}
return res, nil
}
// VendorsIndex is the format for the vendor/index.yaml file.
type VendorsIndex struct {
Vendors []Vendor `yaml:"vendors"`
}
// VendorEndDevicesIndex is the format of the `vendor/<vendor-id>/index.yaml` file.
type VendorEndDevicesIndex struct {
EndDevices []string `yaml:"endDevices"`
}
// EndDeviceModel is the format of the `vendor/<vendor-id>/<model-id>.yaml` file.
type EndDeviceModel struct {
Name string `yaml:"name"`
Description string `yaml:"description"`
HardwareVersions []struct {
Version string `yaml:"version"`
Numeric uint32 `yaml:"numeric"`
PartNumber string `yaml:"partNumber"`
} `yaml:"hardwareVersions"`
FirmwareVersions []struct {
Version string `yaml:"version"`
Numeric uint32 `yaml:"numeric"`
HardwareVersions []string `yaml:"hardwareVersions"`
Profiles map[string]struct {
VendorID string `yaml:"vendorID"`
ID string `yaml:"id"`
Codec string `yaml:"codec"`
LoRaWANCertified bool `yaml:"lorawanCertified"`
} `yaml:"profiles"`
} `yaml:"firmwareVersions"`
Sensors []string `yaml:"sensors"`
Dimensions *struct {
Width float32 `yaml:"width"`
Height float32 `yaml:"height"`
Diameter float32 `yaml:"diameter"`
Length float32 `yaml:"length"`
} `yaml:"dimensions"`
Weight float32 `yaml:"weight"`
Battery *struct {
Replaceable bool `yaml:"replaceable"`
Type string `yaml:"type"`
} `yaml:"battery"`
OperatingConditions *struct {
Temperature *struct {
Min float32 `yaml:"min"`
Max float32 `yaml:"max"`
} `yaml:"temperature"`
RelativeHumidity *struct {
Min float32 `yaml:"min"`
Max float32 `yaml:"max"`
} `yaml:"relativeHumidity"`
} `yaml:"operatingConditions"`
IPCode string `yaml:"ipCode"`
KeyProvisioning []ttnpb.KeyProvisioning `yaml:"keyProvisioning"`
KeySecurity ttnpb.KeySecurity `yaml:"keySecurity"`
Photos *struct {
Main string `yaml:"main"`
Other []string `yaml:"other"`
} `yaml:"photos"`
Videos *struct {
Main string `yaml:"main"`
Other []string `yaml:"other"`
} `yaml:"videos"`
ProductURL string `yaml:"productURL"`
DataSheetURL string `yaml:"dataSheetURL"`
ResellerURLs []struct {
Name string `yaml:"name"`
Region []string `yaml:"region"`
URL string `yaml:"url"`
} `yaml:"resellerURLs"`
Compliances *struct {
Safety []struct {
Body string `yaml:"body"`
Norm string `yaml:"norm"`
Standard string `yaml:"standard"`
Version string `yaml:"version"`
} `yaml:"safety"`
RadioEquipment []struct {
Body string `yaml:"body"`
Norm string `yaml:"norm"`
Standard string `yaml:"standard"`
Version string `yaml:"version"`
} `yaml:"radioEquipment"`
} `yaml:"compliances"`
AdditionalRadios []string `yaml:"additionalRadios"`
}
// ToPB converts an EndDefinitionDefinition to a Protocol Buffer.
func (d EndDeviceModel) ToPB(brandID, modelID string, paths ...string) (*ttnpb.EndDeviceModel, error) {
pb := &ttnpb.EndDeviceModel{
BrandId: brandID,
ModelId: modelID,
Name: d.Name,
Description: d.Description,
FirmwareVersions: make([]*ttnpb.EndDeviceModel_FirmwareVersion, 0, len(d.FirmwareVersions)),
Sensors: d.Sensors,
IpCode: d.IPCode,
KeyProvisioning: d.KeyProvisioning,
KeySecurity: d.KeySecurity,
ProductUrl: d.ProductURL,
DatasheetUrl: d.DataSheetURL,
AdditionalRadios: d.AdditionalRadios,
}
if hwVersions := d.HardwareVersions; hwVersions != nil {
pb.HardwareVersions = make([]*ttnpb.EndDeviceModel_HardwareVersion, 0, len(hwVersions))
for _, ver := range hwVersions {
pb.HardwareVersions = append(pb.HardwareVersions, &ttnpb.EndDeviceModel_HardwareVersion{
Version: ver.Version,
Numeric: ver.Numeric,
PartNumber: ver.PartNumber,
})
}
}
for _, ver := range d.FirmwareVersions {
pbver := &ttnpb.EndDeviceModel_FirmwareVersion{
Version: ver.Version,
Numeric: ver.Numeric,
SupportedHardwareVersions: ver.HardwareVersions,
}
pbver.Profiles = make(map[string]*ttnpb.EndDeviceModel_FirmwareVersion_Profile, len(ver.Profiles))
for region, profile := range ver.Profiles {
pbver.Profiles[regionToBandID[region]] = &ttnpb.EndDeviceModel_FirmwareVersion_Profile{
VendorId: profile.VendorID,
ProfileId: profile.ID,
LorawanCertified: profile.LoRaWANCertified,
CodecId: profile.Codec,
}
}
pb.FirmwareVersions = append(pb.FirmwareVersions, pbver)
}
if dim := d.Dimensions; dim != nil {
pb.Dimensions = &ttnpb.EndDeviceModel_Dimensions{}
if w := d.Dimensions.Width; w > 0 {
pb.Dimensions.Width = &wrapperspb.FloatValue{Value: w}
}
if h := d.Dimensions.Height; h > 0 {
pb.Dimensions.Height = &wrapperspb.FloatValue{Value: h}
}
if d := d.Dimensions.Diameter; d > 0 {
pb.Dimensions.Diameter = &wrapperspb.FloatValue{Value: d}
}
if l := d.Dimensions.Length; l > 0 {
pb.Dimensions.Length = &wrapperspb.FloatValue{Value: l}
}
}
if w := d.Weight; w > 0 {
pb.Weight = &wrapperspb.FloatValue{Value: w}
}
if battery := d.Battery; battery != nil {
pb.Battery = &ttnpb.EndDeviceModel_Battery{
Replaceable: &wrapperspb.BoolValue{Value: d.Battery.Replaceable},
Type: d.Battery.Type,
}
}
if oc := d.OperatingConditions; oc != nil {
pb.OperatingConditions = &ttnpb.EndDeviceModel_OperatingConditions{}
if rh := oc.RelativeHumidity; rh != nil {
pb.OperatingConditions.RelativeHumidity = &ttnpb.EndDeviceModel_OperatingConditions_Limits{
Min: &wrapperspb.FloatValue{Value: rh.Min},
Max: &wrapperspb.FloatValue{Value: rh.Max},
}
}
if temp := oc.Temperature; temp != nil {
pb.OperatingConditions.Temperature = &ttnpb.EndDeviceModel_OperatingConditions_Limits{
Min: &wrapperspb.FloatValue{Value: temp.Min},
Max: &wrapperspb.FloatValue{Value: temp.Max},
}
}
}
if p := d.Photos; p != nil {
pb.Photos = &ttnpb.EndDeviceModel_Photos{
Main: p.Main,
Other: p.Other,
}
}
if v := d.Videos; v != nil {
pb.Videos = &ttnpb.EndDeviceModel_Videos{
Main: v.Main,
Other: v.Other,
}
}
if rs := d.ResellerURLs; len(rs) > 0 {
pb.Resellers = make([]*ttnpb.EndDeviceModel_Reseller, 0, len(rs))
for _, reseller := range rs {
pb.Resellers = append(pb.Resellers, &ttnpb.EndDeviceModel_Reseller{
Name: reseller.Name,
Url: reseller.URL,
Region: reseller.Region,
})
}
}
if cs := d.Compliances; cs != nil {
pb.Compliances = &ttnpb.EndDeviceModel_Compliances{
Safety: make([]*ttnpb.EndDeviceModel_Compliances_Compliance, 0, len(cs.Safety)),
RadioEquipment: make([]*ttnpb.EndDeviceModel_Compliances_Compliance, 0, len(cs.RadioEquipment)),
}
for _, c := range cs.Safety {
pb.Compliances.Safety = append(pb.Compliances.Safety, &ttnpb.EndDeviceModel_Compliances_Compliance{
Version: c.Version,
Body: c.Body,
Standard: c.Standard,
Norm: c.Norm,
})
}
for _, c := range cs.RadioEquipment {
pb.Compliances.RadioEquipment = append(pb.Compliances.RadioEquipment, &ttnpb.EndDeviceModel_Compliances_Compliance{
Version: c.Version,
Body: c.Body,
Standard: c.Standard,
Norm: c.Norm,
})
}
}
res := &ttnpb.EndDeviceModel{}
if err := res.SetFields(pb, paths...); err != nil {
return nil, err
}
return res, nil
}
type EncodedCodecData struct {
FPort uint32 `yaml:"fPort"`
Bytes []byte `yaml:"bytes"`
Warnings []string `yaml:"warnings"`
Errors []string `yaml:"errors"`
}
type DecodedCodecData struct {
Data map[string]any `yaml:"data"`
Warnings []string `yaml:"warnings"`
Errors []string `yaml:"errors"`
}
type DecoderCodecExample struct {
Description string `yaml:"description"`
Input EncodedCodecData `yaml:"input"`
Output DecodedCodecData `yaml:"output"`
}
type EncoderCodecExample struct {
Description string `yaml:"description"`
Input DecodedCodecData `yaml:"input"`
Output EncodedCodecData `yaml:"output"`
}
type EndDeviceEncoderCodec struct {
FileName string `yaml:"fileName"`
Examples []EncoderCodecExample `yaml:"examples"`
}
type EndDeviceDecoderCodec struct {
FileName string `yaml:"fileName"`
Examples []DecoderCodecExample `yaml:"examples"`
}
// EndDeviceCodecs is the format of the `vendor/<vendor>/<codec-id>.yaml` files.
type EndDeviceCodecs struct {
CodecID string
UplinkDecoder EndDeviceDecoderCodec `yaml:"uplinkDecoder"`
DownlinkDecoder EndDeviceDecoderCodec `yaml:"downlinkDecoder"`
DownlinkEncoder EndDeviceEncoderCodec `yaml:"downlinkEncoder"`
}