forked from TheThingsNetwork/lorawan-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.go
More file actions
155 lines (144 loc) · 4.38 KB
/
javascript.go
File metadata and controls
155 lines (144 loc) · 4.38 KB
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
// Copyright © 2019 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 javascript contains the Javascript payload formatter message processors.
package javascript
import (
"context"
"fmt"
"reflect"
"runtime/trace"
"go.thethings.network/lorawan-stack/v3/pkg/errors"
"go.thethings.network/lorawan-stack/v3/pkg/gogoproto"
"go.thethings.network/lorawan-stack/v3/pkg/messageprocessors"
"go.thethings.network/lorawan-stack/v3/pkg/scripting"
js "go.thethings.network/lorawan-stack/v3/pkg/scripting/javascript"
"go.thethings.network/lorawan-stack/v3/pkg/ttnpb"
)
type host struct {
engine scripting.Engine
}
// New creates and returns a new Javascript payload encoder and decoder.
func New() messageprocessors.PayloadEncodeDecoder {
return &host{
engine: js.New(scripting.DefaultOptions),
}
}
func (h *host) createEnvironment(ids ttnpb.EndDeviceIdentifiers, version *ttnpb.EndDeviceVersionIdentifiers) map[string]interface{} {
env := make(map[string]interface{})
if ids.DevEUI != nil {
env["dev_eui"] = ids.DevEUI.String()
}
if version != nil {
env["brand"] = version.BrandID
env["model"] = version.ModelID
env["hardware_version"] = version.HardwareVersion
env["firmware_version"] = version.FirmwareVersion
}
return env
}
var (
errInput = errors.DefineInvalidArgument("input", "invalid input")
errOutput = errors.Define("output", "invalid output")
errOutputType = errors.Define("output_type", "invalid output of type `{type}`")
errOutputRange = errors.Define("output_range", "output value `{value}` does not fall between `{low}` and `{high}`")
)
// Encode encodes the message's DecodedPayload to FRMPayload using the given script.
func (h *host) Encode(ctx context.Context, ids ttnpb.EndDeviceIdentifiers, version *ttnpb.EndDeviceVersionIdentifiers, msg *ttnpb.ApplicationDownlink, script string) error {
defer trace.StartRegion(ctx, "encode message").End()
decoded := msg.DecodedPayload
if decoded == nil {
return nil
}
m, err := gogoproto.Map(decoded)
if err != nil {
return errInput.WithCause(err)
}
env := h.createEnvironment(ids, version)
env["payload"] = m
env["f_port"] = msg.FPort
script = fmt.Sprintf(`
%s
Encoder(env.payload, env.f_port)
`, script)
value, err := h.engine.Run(ctx, script, env)
if err != nil {
return err
}
if value == nil || reflect.TypeOf(value).Kind() != reflect.Slice {
return errOutputType.New()
}
slice := reflect.ValueOf(value)
frmPayload := make([]byte, slice.Len())
for i := 0; i < slice.Len(); i++ {
val := slice.Index(i).Interface()
var b int64
switch i := val.(type) {
case int:
b = int64(i)
case int8:
b = int64(i)
case int16:
b = int64(i)
case int32:
b = int64(i)
case int64:
b = i
case uint8:
b = int64(i)
case uint16:
b = int64(i)
case uint32:
b = int64(i)
case uint64:
b = int64(i)
default:
return errOutputType.WithAttributes("type", fmt.Sprintf("%T", i))
}
if b < 0x00 || b > 0xFF {
return errOutputRange.WithAttributes(
"value", b,
"low", 0x00,
"high", 0xFF,
)
}
frmPayload[i] = byte(b)
}
msg.FRMPayload = frmPayload
return nil
}
// Decode decodes the message's FRMPayload to DecodedPayload using the given script.
func (h *host) Decode(ctx context.Context, ids ttnpb.EndDeviceIdentifiers, version *ttnpb.EndDeviceVersionIdentifiers, msg *ttnpb.ApplicationUplink, script string) error {
defer trace.StartRegion(ctx, "decode message").End()
env := h.createEnvironment(ids, version)
env["payload"] = msg.FRMPayload
env["f_port"] = msg.FPort
script = fmt.Sprintf(`
%s
Decoder(env.payload, env.f_port)
`, script)
value, err := h.engine.Run(ctx, script, env)
if err != nil {
return err
}
m, ok := value.(map[string]interface{})
if !ok {
return errOutput.New()
}
s, err := gogoproto.Struct(m)
if err != nil {
return errOutput.WithCause(err)
}
msg.DecodedPayload = s
return nil
}