gotelem/skylab/skylab.go

102 lines
2 KiB
Go
Raw Normal View History

2023-05-14 21:19:57 +00:00
package skylab
import (
"encoding/binary"
2023-05-16 15:48:30 +00:00
"encoding/json"
2023-05-16 11:24:38 +00:00
"math"
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 {
binary.BigEndian.Uint32(b)
} else {
binary.LittleEndian.Uint32(b)
}
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 {
MarshalPacket() ([]byte, error)
UnmarshalPacket(p []byte) error
2023-05-18 22:47:14 +00:00
CANId() (uint32, error)
2023-05-16 11:24:38 +00:00
Size() uint
}
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.
func CanSend(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
// ---- JSON encoding business ----
type JSONPacket struct {
Id uint32
Data json.RawMessage
}
func ToJson(p Packet) (*JSONPacket, error) {
d, err := json.Marshal(p)
if err != nil {
return nil, err
}
2023-05-18 22:47:14 +00:00
id, err := p.CANId()
2023-05-17 02:58:20 +00:00
if err != nil {
return nil, err
2023-05-16 15:48:30 +00:00
}
2023-05-17 02:58:20 +00:00
jp := &JSONPacket{Id: id, Data: d}
2023-05-16 15:48:30 +00:00
return jp, nil
}
// we need to be able to parse the JSON as well.
2023-05-17 02:58:20 +00:00
// this is done using the generator since we can use the switch/case thing
// since it's the fastest