2023-06-30 16:51:06 +00:00
|
|
|
// Package skylab provides CAN packet encoding and decoding information based off
|
|
|
|
// of skylab.yaml. It can convert packets to/from CAN raw bytes and JSON objects.
|
2023-05-14 21:19:57 +00:00
|
|
|
package skylab
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2023-05-16 15:48:30 +00:00
|
|
|
"encoding/json"
|
2023-06-28 01:39:57 +00:00
|
|
|
"fmt"
|
2023-05-16 11:24:38 +00:00
|
|
|
"math"
|
2023-06-30 04:59:16 +00:00
|
|
|
"time"
|
2023-05-29 18:33:49 +00:00
|
|
|
|
|
|
|
// this is needed so that we can run make_skylab.go
|
|
|
|
// without this, the yaml library will be removed
|
|
|
|
// when we run `go mod tidy`
|
|
|
|
_ "gopkg.in/yaml.v3"
|
2023-05-14 21:19:57 +00:00
|
|
|
)
|
|
|
|
|
2023-05-16 11:24:38 +00:00
|
|
|
/*
|
|
|
|
This file provides helpers used for serializing and deserializing skylab packets.
|
|
|
|
It contains common code and interfaces.
|
|
|
|
*/
|
|
|
|
|
2023-05-16 15:48:30 +00:00
|
|
|
// float32ToBytes is an internal function used to encode a float value to bytes
|
2023-05-14 21:19:57 +00:00
|
|
|
func float32ToBytes(b []byte, f float32, bigEndian bool) {
|
|
|
|
bits := math.Float32bits(f)
|
|
|
|
if bigEndian {
|
|
|
|
binary.BigEndian.PutUint32(b, bits)
|
|
|
|
} else {
|
|
|
|
binary.LittleEndian.PutUint32(b, bits)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-16 15:48:30 +00:00
|
|
|
// float32FromBytes is an internal function used to decode float value from bytes
|
2023-05-14 21:19:57 +00:00
|
|
|
func float32FromBytes(b []byte, bigEndian bool) (f float32) {
|
|
|
|
var bits uint32
|
|
|
|
if bigEndian {
|
2023-05-20 19:53:20 +00:00
|
|
|
bits = binary.BigEndian.Uint32(b)
|
2023-05-14 21:19:57 +00:00
|
|
|
} else {
|
2023-05-20 19:53:20 +00:00
|
|
|
bits = binary.LittleEndian.Uint32(b)
|
2023-05-14 21:19:57 +00:00
|
|
|
}
|
|
|
|
return math.Float32frombits(bits)
|
|
|
|
}
|
|
|
|
|
2023-05-16 11:24:38 +00:00
|
|
|
// Packet is any Skylab-generated packet.
|
2023-05-14 21:19:57 +00:00
|
|
|
type Packet interface {
|
2023-06-30 12:18:15 +00:00
|
|
|
Marshaler
|
|
|
|
Unmarshaler
|
|
|
|
Ider
|
|
|
|
Sizer
|
|
|
|
fmt.Stringer // to get the name
|
2023-05-16 11:24:38 +00:00
|
|
|
}
|
|
|
|
|
2023-05-16 15:48:30 +00:00
|
|
|
// Marshaler is a packet that can be marshalled into bytes.
|
2023-05-16 11:24:38 +00:00
|
|
|
type Marshaler interface {
|
|
|
|
MarshalPacket() ([]byte, error)
|
|
|
|
}
|
2023-05-16 15:48:30 +00:00
|
|
|
|
|
|
|
// Unmarshaler is a packet that can be unmarshalled from bytes.
|
2023-05-16 11:24:38 +00:00
|
|
|
type Unmarshaler interface {
|
|
|
|
UnmarshalPacket(p []byte) error
|
|
|
|
}
|
|
|
|
|
2023-05-16 15:48:30 +00:00
|
|
|
// Ider is a packet that can get its ID, based on the index of the packet, if any.
|
2023-05-16 11:24:38 +00:00
|
|
|
type Ider interface {
|
2023-05-18 22:47:14 +00:00
|
|
|
CANId() (uint32, error)
|
2023-05-16 11:24:38 +00:00
|
|
|
}
|
|
|
|
|
2023-05-16 15:48:30 +00:00
|
|
|
// Sizer allows for fast allocation.
|
2023-05-16 11:24:38 +00:00
|
|
|
type Sizer interface {
|
|
|
|
Size() uint
|
|
|
|
}
|
|
|
|
|
2023-05-17 02:58:20 +00:00
|
|
|
// CanSend takes a packet and makes CAN framing data.
|
2023-05-29 00:39:03 +00:00
|
|
|
func ToCanFrame(p Packet) (id uint32, data []byte, err error) {
|
2023-05-16 11:24:38 +00:00
|
|
|
|
2023-05-18 22:47:14 +00:00
|
|
|
id, err = p.CANId()
|
2023-05-17 02:58:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
data, err = p.MarshalPacket()
|
|
|
|
return
|
2023-05-14 21:19:57 +00:00
|
|
|
}
|
2023-05-16 15:48:30 +00:00
|
|
|
|
2023-05-29 00:39:03 +00:00
|
|
|
// ---- other wire encoding business ----
|
2023-05-16 15:48:30 +00:00
|
|
|
|
2023-05-29 00:39:03 +00:00
|
|
|
// internal structure for partially decoding json object.
|
2023-06-30 04:59:16 +00:00
|
|
|
type RawJsonEvent struct {
|
2023-09-28 22:23:29 +00:00
|
|
|
Timestamp int64 `json:"ts" db:"ts"`
|
2023-06-30 04:59:16 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
Data json.RawMessage `json:"data"`
|
2023-05-16 15:48:30 +00:00
|
|
|
}
|
|
|
|
|
2024-02-12 15:45:23 +00:00
|
|
|
// BusEvent is a timestamped Skylab packet - it contains
|
2023-05-29 00:39:03 +00:00
|
|
|
type BusEvent struct {
|
2023-06-30 04:59:16 +00:00
|
|
|
Timestamp time.Time `json:"ts"`
|
2024-02-12 15:45:23 +00:00
|
|
|
Name string `json:"id"`
|
2023-06-30 04:59:16 +00:00
|
|
|
Data Packet `json:"data"`
|
2023-05-29 00:39:03 +00:00
|
|
|
}
|
2023-05-16 15:48:30 +00:00
|
|
|
|
2023-09-28 22:23:29 +00:00
|
|
|
func (e BusEvent) MarshalJSON() (b []byte, err error) {
|
2023-05-29 00:39:03 +00:00
|
|
|
// create the underlying raw event
|
2023-06-30 04:59:16 +00:00
|
|
|
j := &RawJsonEvent{
|
2023-07-03 18:51:15 +00:00
|
|
|
Timestamp: e.Timestamp.UnixMilli(),
|
2024-02-12 15:45:23 +00:00
|
|
|
Name: e.Name,
|
2023-05-29 00:39:03 +00:00
|
|
|
}
|
|
|
|
// now we use the magic Packet -> map[string]interface{} function
|
2024-02-12 15:45:23 +00:00
|
|
|
// FIXME: this uses reflection and isn't good for the economy
|
2023-05-29 00:39:03 +00:00
|
|
|
j.Data, err = json.Marshal(e.Data)
|
2023-05-16 15:48:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-29 00:39:03 +00:00
|
|
|
return json.Marshal(j)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *BusEvent) UnmarshalJSON(b []byte) error {
|
2023-06-30 04:59:16 +00:00
|
|
|
j := &RawJsonEvent{}
|
2023-05-29 00:39:03 +00:00
|
|
|
|
2023-06-30 04:59:16 +00:00
|
|
|
err := json.Unmarshal(b, j)
|
2023-05-29 00:39:03 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-03 18:51:15 +00:00
|
|
|
e.Timestamp = time.UnixMilli(j.Timestamp)
|
2024-02-12 15:45:23 +00:00
|
|
|
e.Name = j.Name
|
|
|
|
e.Data, err = FromJson(j.Name, j.Data)
|
2023-05-29 00:39:03 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-20 19:53:20 +00:00
|
|
|
// we need to be able to parse the JSON as well. this is done using the
|
|
|
|
// generator since we can use the switch/case thing since it's the fastest
|
2023-06-28 01:39:57 +00:00
|
|
|
|
|
|
|
type UnknownIdError struct {
|
2023-06-30 12:18:15 +00:00
|
|
|
id uint32
|
2023-06-28 01:39:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *UnknownIdError) Error() string {
|
|
|
|
return fmt.Sprintf("unknown id: %x", e.id)
|
|
|
|
}
|