Skip to content

Commit 5c33011

Browse files
committed
Add ID 7 to protection decoder (warning messages)
1 parent dc83da7 commit 5c33011

9 files changed

Lines changed: 123 additions & 62 deletions

File tree

backend/cmd/main.go

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"fmt"
99
"log"
1010
"net"
11+
"net/http"
12+
_ "net/http/pprof"
1113
"os"
1214
"os/signal"
1315
"path"
@@ -59,6 +61,8 @@ var traceLevel = flag.String("trace", "info", "set the trace level (\"fatal\", \
5961
var traceFile = flag.String("log", "trace.json", "set the trace log file")
6062
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
6163
var enableSNTP = flag.Bool("sntp", false, "enables a simple SNTP server on port 123")
64+
var networkDevice = flag.Int("dev", -1, "index of the network device to use, overrides device prompt")
65+
var blockprofile = flag.Int("blockprofile", 0, "number of block profiles to include")
6266

6367
func main() {
6468
flag.Parse()
@@ -79,6 +83,7 @@ func main() {
7983
pprof.StartCPUProfile(f)
8084
defer pprof.StopCPUProfile()
8185
}
86+
runtime.SetBlockProfileRate(*blockprofile)
8287
config := getConfig("./config.toml")
8388

8489
file, err := excel.Download(excel.DownloadConfig(config.Excel.Download))
@@ -102,10 +107,19 @@ func main() {
102107
trace.Fatal().Err(err).Msg("creating podData")
103108
}
104109

105-
dev, err := selectDev()
106-
if err != nil {
107-
trace.Fatal().Err(err).Msg("Error selecting device")
108-
panic(err)
110+
var dev pcap.Interface
111+
if *networkDevice != -1 {
112+
devs, err := pcap.FindAllDevs()
113+
if err != nil {
114+
trace.Fatal().Err(err).Msg("Getting devices")
115+
}
116+
117+
dev = devs[*networkDevice]
118+
} else {
119+
dev, err = selectDev()
120+
if err != nil {
121+
trace.Fatal().Err(err).Msg("Error selecting device")
122+
}
109123
}
110124

111125
vehicleOrders, err := vehicle_models.NewVehicleOrders(podData.Boards, config.Excel.Parse.Global.BLCUAddressKey)
@@ -276,6 +290,8 @@ func main() {
276290
go httpServer.ListenAndServe()
277291
}
278292

293+
go http.ListenAndServe("127.0.0.1:4040", nil)
294+
279295
// <--- SNTP --->
280296
if *enableSNTP {
281297
sntpAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", info.Addresses.Backend, info.Ports.SNTP))
@@ -480,13 +496,13 @@ func getTransportDecEnc(info info.Info, podData pod_data.PodData) (*presentation
480496
decoder.SetPacketDecoder(abstraction.PacketId(info.MessageIds.RemoveStateOrder), stateOrdersDecoder)
481497

482498
protectionDecoder := protection.NewDecoder(binary.LittleEndian)
483-
protectionDecoder.SetSeverity(1000, protection.Fault).SetSeverity(2000, protection.Warning).SetSeverity(3000, protection.Ok)
484-
protectionDecoder.SetSeverity(1111, protection.Fault).SetSeverity(2111, protection.Warning).SetSeverity(3111, protection.Ok)
485-
protectionDecoder.SetSeverity(1222, protection.Fault).SetSeverity(2222, protection.Warning).SetSeverity(3222, protection.Ok)
486-
protectionDecoder.SetSeverity(1333, protection.Fault)
487-
protectionDecoder.SetSeverity(1444, protection.Fault)
488-
protectionDecoder.SetSeverity(1555, protection.Fault).SetSeverity(2555, protection.Warning)
489-
protectionDecoder.SetSeverity(1666, protection.Fault).SetSeverity(2666, protection.Warning).SetSeverity(3666, protection.Ok)
499+
protectionDecoder.SetSeverity(1000, protection.FaultSeverity).SetSeverity(2000, protection.WarningSeverity).SetSeverity(3000, protection.OkSeverity)
500+
protectionDecoder.SetSeverity(1111, protection.FaultSeverity).SetSeverity(2111, protection.WarningSeverity).SetSeverity(3111, protection.OkSeverity)
501+
protectionDecoder.SetSeverity(1222, protection.FaultSeverity).SetSeverity(2222, protection.WarningSeverity).SetSeverity(3222, protection.OkSeverity)
502+
protectionDecoder.SetSeverity(1333, protection.FaultSeverity)
503+
protectionDecoder.SetSeverity(1444, protection.FaultSeverity)
504+
protectionDecoder.SetSeverity(1555, protection.FaultSeverity).SetSeverity(2555, protection.WarningSeverity)
505+
protectionDecoder.SetSeverity(1666, protection.FaultSeverity).SetSeverity(2666, protection.WarningSeverity).SetSeverity(3666, protection.OkSeverity)
490506
decoder.SetPacketDecoder(1000, protectionDecoder)
491507
decoder.SetPacketDecoder(1111, protectionDecoder)
492508
decoder.SetPacketDecoder(1222, protectionDecoder)

backend/pkg/broker/topics/message/message_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ package message_test
22

33
import (
44
"encoding/json"
5+
"os"
6+
"testing"
7+
"time"
8+
59
"github.com/HyperloopUPV-H8/h9-backend/pkg/abstraction"
610
"github.com/HyperloopUPV-H8/h9-backend/pkg/broker"
711
data "github.com/HyperloopUPV-H8/h9-backend/pkg/broker/topics/message"
@@ -10,9 +14,6 @@ import (
1014
"github.com/HyperloopUPV-H8/h9-backend/pkg/websocket"
1115
ws "github.com/gorilla/websocket"
1216
"github.com/rs/zerolog"
13-
"os"
14-
"testing"
15-
"time"
1617
)
1718

1819
func TestMessageTopic_Push(t *testing.T) {
@@ -82,7 +83,7 @@ func TestMessageTopic_ClientMessage(t *testing.T) {
8283
messageTopic := data.NewUpdateTopic(map[abstraction.BoardId]string{})
8384
messageTopic.SetAPI(api)
8485

85-
packet := protection.NewPacket(0, protection.Ok)
86+
packet := protection.NewPacket(0, protection.OkSeverity)
8687
payload := data.Push(packet, 0)
8788
payloadBytes, _ := json.Marshal(payload)
8889
messageTopic.ClientMessage(websocket.ClientId{0}, &websocket.Message{

backend/pkg/logger/logger_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ package logger_test
33
import (
44
"encoding/csv"
55
"fmt"
6+
"os"
7+
"path"
8+
"testing"
9+
"time"
10+
611
"github.com/HyperloopUPV-H8/h9-backend/pkg/abstraction"
712
"github.com/HyperloopUPV-H8/h9-backend/pkg/logger"
813
"github.com/HyperloopUPV-H8/h9-backend/pkg/logger/data"
@@ -13,10 +18,6 @@ import (
1318
protectionPacketer "github.com/HyperloopUPV-H8/h9-backend/pkg/transport/packet/protection"
1419
statePacketer "github.com/HyperloopUPV-H8/h9-backend/pkg/transport/packet/state"
1520
"github.com/rs/zerolog"
16-
"os"
17-
"path"
18-
"testing"
19-
"time"
2021
)
2122

2223
func TestLogger(t *testing.T) {
@@ -115,7 +116,7 @@ func TestLogger(t *testing.T) {
115116
}
116117

117118
// Protection
118-
protectionPacket := protectionPacketer.NewPacket(0, protectionPacketer.Ok)
119+
protectionPacket := protectionPacketer.NewPacket(0, protectionPacketer.OkSeverity)
119120
protectionPacket.Timestamp = &protectionPacketer.Timestamp{
120121
Counter: 0,
121122
Second: 0,

backend/pkg/transport/packet/protection/decoder.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var decodingMatrix = map[Type]map[Kind]func(io.Reader, binary.ByteOrder) (Data,
1717
NotEqualsKind: decodeNotEquals[int32],
1818
ErrorHandlerKind: decodeErrorHandler,
1919
TimeAccumulationKind: decodeTimeAccumulation[int32],
20+
7: decodeErrorHandler,
2021
},
2122
FloatType: {
2223
BelowKind: decodeBelow[float32],
@@ -26,6 +27,7 @@ var decodingMatrix = map[Type]map[Kind]func(io.Reader, binary.ByteOrder) (Data,
2627
NotEqualsKind: decodeNotEquals[float32],
2728
ErrorHandlerKind: decodeErrorHandler,
2829
TimeAccumulationKind: decodeTimeAccumulation[float32],
30+
7: decodeErrorHandler,
2931
},
3032
DoubleType: {
3133
BelowKind: decodeBelow[float64],
@@ -35,6 +37,7 @@ var decodingMatrix = map[Type]map[Kind]func(io.Reader, binary.ByteOrder) (Data,
3537
NotEqualsKind: decodeNotEquals[float64],
3638
ErrorHandlerKind: decodeErrorHandler,
3739
TimeAccumulationKind: decodeTimeAccumulation[float64],
40+
7: decodeErrorHandler,
3841
},
3942
CharType: {
4043
BelowKind: decodeBelow[byte],
@@ -44,6 +47,7 @@ var decodingMatrix = map[Type]map[Kind]func(io.Reader, binary.ByteOrder) (Data,
4447
NotEqualsKind: decodeNotEquals[byte],
4548
ErrorHandlerKind: decodeErrorHandler,
4649
TimeAccumulationKind: decodeTimeAccumulation[byte],
50+
7: decodeErrorHandler,
4751
},
4852
BoolType: {
4953
BelowKind: decodeBelow[bool],
@@ -53,6 +57,7 @@ var decodingMatrix = map[Type]map[Kind]func(io.Reader, binary.ByteOrder) (Data,
5357
NotEqualsKind: decodeNotEquals[bool],
5458
ErrorHandlerKind: decodeErrorHandler,
5559
TimeAccumulationKind: decodeTimeAccumulation[bool],
60+
7: decodeErrorHandler,
5661
},
5762
ShortType: {
5863
BelowKind: decodeBelow[int16],
@@ -62,6 +67,7 @@ var decodingMatrix = map[Type]map[Kind]func(io.Reader, binary.ByteOrder) (Data,
6267
NotEqualsKind: decodeNotEquals[int16],
6368
ErrorHandlerKind: decodeErrorHandler,
6469
TimeAccumulationKind: decodeTimeAccumulation[int16],
70+
7: decodeErrorHandler,
6571
},
6672
LongType: {
6773
BelowKind: decodeBelow[int64],
@@ -71,6 +77,7 @@ var decodingMatrix = map[Type]map[Kind]func(io.Reader, binary.ByteOrder) (Data,
7177
NotEqualsKind: decodeNotEquals[int64],
7278
ErrorHandlerKind: decodeErrorHandler,
7379
TimeAccumulationKind: decodeTimeAccumulation[int64],
80+
7: decodeErrorHandler,
7481
},
7582
Uint8Type: {
7683
BelowKind: decodeBelow[uint8],
@@ -80,6 +87,7 @@ var decodingMatrix = map[Type]map[Kind]func(io.Reader, binary.ByteOrder) (Data,
8087
NotEqualsKind: decodeNotEquals[uint8],
8188
ErrorHandlerKind: decodeErrorHandler,
8289
TimeAccumulationKind: decodeTimeAccumulation[uint8],
90+
7: decodeErrorHandler,
8391
},
8492
Uint16Type: {
8593
BelowKind: decodeBelow[uint16],
@@ -89,6 +97,7 @@ var decodingMatrix = map[Type]map[Kind]func(io.Reader, binary.ByteOrder) (Data,
8997
NotEqualsKind: decodeNotEquals[uint16],
9098
ErrorHandlerKind: decodeErrorHandler,
9199
TimeAccumulationKind: decodeTimeAccumulation[uint16],
100+
7: decodeErrorHandler,
92101
},
93102
Uint32Type: {
94103
BelowKind: decodeBelow[uint32],
@@ -98,6 +107,7 @@ var decodingMatrix = map[Type]map[Kind]func(io.Reader, binary.ByteOrder) (Data,
98107
NotEqualsKind: decodeNotEquals[uint32],
99108
ErrorHandlerKind: decodeErrorHandler,
100109
TimeAccumulationKind: decodeTimeAccumulation[uint32],
110+
7: decodeErrorHandler,
101111
},
102112
Uint64Type: {
103113
BelowKind: decodeBelow[uint64],
@@ -107,6 +117,7 @@ var decodingMatrix = map[Type]map[Kind]func(io.Reader, binary.ByteOrder) (Data,
107117
NotEqualsKind: decodeNotEquals[uint64],
108118
ErrorHandlerKind: decodeErrorHandler,
109119
TimeAccumulationKind: decodeTimeAccumulation[uint64],
120+
7: decodeErrorHandler,
110121
},
111122
Int8Type: {
112123
BelowKind: decodeBelow[int8],
@@ -116,6 +127,7 @@ var decodingMatrix = map[Type]map[Kind]func(io.Reader, binary.ByteOrder) (Data,
116127
NotEqualsKind: decodeNotEquals[int8],
117128
ErrorHandlerKind: decodeErrorHandler,
118129
TimeAccumulationKind: decodeTimeAccumulation[int8],
130+
7: decodeErrorHandler,
119131
},
120132
}
121133

backend/pkg/transport/packet/protection/kind.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,26 @@ func (TimeAccumulation[T]) Name() string {
189189
func (t TimeAccumulation[T]) MarshalJSON() ([]byte, error) {
190190
return []byte(fmt.Sprintf(`{"value":%v,"bound":%v,"timelimit":%v}`, t.Value, t.Threshold, t.Time)), nil
191191
}
192+
193+
type Warning struct {
194+
Message string
195+
}
196+
197+
func decodeWarning(reader io.Reader, endianness binary.ByteOrder) (Data, error) {
198+
packet := new(Warning)
199+
var err error
200+
packet.Message, err = readCString(reader)
201+
return packet, err
202+
}
203+
204+
func (Warning) Kind() Kind {
205+
return WarningKind
206+
}
207+
208+
func (Warning) Name() string {
209+
return "WARNING"
210+
}
211+
212+
func (e Warning) MarshalJSON() ([]byte, error) {
213+
return []byte(fmt.Sprintf(`"%s"`, e.Message)), nil
214+
}

backend/pkg/transport/packet/protection/packet.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ func (packet *Packet) Severity() Severity {
7272
type Severity string
7373

7474
const (
75-
Fault Severity = "fault"
76-
Warning Severity = "warning"
77-
Ok Severity = "ok"
75+
FaultSeverity Severity = "fault"
76+
WarningSeverity Severity = "warning"
77+
OkSeverity Severity = "ok"
7878
)
7979

8080
// Type is the type of the data that is being tracked by a protection packet.
@@ -109,6 +109,7 @@ const (
109109
NotEqualsKind // [NotEquals]
110110
ErrorHandlerKind // [ErrorHandler]
111111
TimeAccumulationKind // [TimeAccumulation]
112+
WarningKind // [Warning]
112113
)
113114

114115
// Data is a common interface to all the possible protection kinds.

backend/pkg/transport/presentation/decoder_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func TestDecoder(t *testing.T) {
133133
)),
134134
output: []abstraction.Packet{
135135
func() abstraction.Packet {
136-
p := protection.NewPacket(5, protection.Warning)
136+
p := protection.NewPacket(5, protection.WarningSeverity)
137137
p.Type = protection.Uint8Type
138138
p.Kind = protection.OutOfBoundsKind
139139
p.Name = "out of bounds"
@@ -154,7 +154,7 @@ func TestDecoder(t *testing.T) {
154154
return p
155155
}(),
156156
func() abstraction.Packet {
157-
p := protection.NewPacket(6, protection.Fault)
157+
p := protection.NewPacket(6, protection.FaultSeverity)
158158
p.Type = protection.FloatType
159159
p.Kind = protection.AboveKind
160160
p.Name = "upper bound"
@@ -174,7 +174,7 @@ func TestDecoder(t *testing.T) {
174174
return p
175175
}(),
176176
func() abstraction.Packet {
177-
p := protection.NewPacket(5, protection.Warning)
177+
p := protection.NewPacket(5, protection.WarningSeverity)
178178
p.Type = protection.IntType
179179
p.Kind = protection.BelowKind
180180
p.Name = "lower bound"
@@ -194,7 +194,7 @@ func TestDecoder(t *testing.T) {
194194
return p
195195
}(),
196196
func() abstraction.Packet {
197-
p := protection.NewPacket(6, protection.Fault)
197+
p := protection.NewPacket(6, protection.FaultSeverity)
198198
p.Type = protection.BoolType
199199
p.Kind = protection.EqualsKind
200200
p.Name = "equals"
@@ -214,7 +214,7 @@ func TestDecoder(t *testing.T) {
214214
return p
215215
}(),
216216
func() abstraction.Packet {
217-
p := protection.NewPacket(5, protection.Warning)
217+
p := protection.NewPacket(5, protection.WarningSeverity)
218218
p.Type = protection.CharType
219219
p.Kind = protection.NotEqualsKind
220220
p.Name = "not equals"
@@ -234,7 +234,7 @@ func TestDecoder(t *testing.T) {
234234
return p
235235
}(),
236236
func() abstraction.Packet {
237-
p := protection.NewPacket(6, protection.Fault)
237+
p := protection.NewPacket(6, protection.FaultSeverity)
238238
p.Type = protection.DoubleType
239239
p.Kind = protection.TimeAccumulationKind
240240
p.Name = "time accumulation"
@@ -256,7 +256,7 @@ func TestDecoder(t *testing.T) {
256256
return p
257257
}(),
258258
func() abstraction.Packet {
259-
p := protection.NewPacket(5, protection.Warning)
259+
p := protection.NewPacket(5, protection.WarningSeverity)
260260
p.Type = 0
261261
p.Kind = protection.ErrorHandlerKind
262262
p.Name = "error handler"
@@ -668,8 +668,8 @@ func getDecoder(endianness binary.ByteOrder) *presentation.Decoder {
668668
decoder.SetPacketDecoder(4, ordersDecoder)
669669

670670
protectionDecoder := protection.NewDecoder(endianness)
671-
protectionDecoder.SetSeverity(5, protection.Warning)
672-
protectionDecoder.SetSeverity(6, protection.Fault)
671+
protectionDecoder.SetSeverity(5, protection.WarningSeverity)
672+
protectionDecoder.SetSeverity(6, protection.FaultSeverity)
673673
decoder.SetPacketDecoder(5, protectionDecoder)
674674
decoder.SetPacketDecoder(6, protectionDecoder)
675675

0 commit comments

Comments
 (0)