2023-05-17 05:01:40 +00:00
|
|
|
{{ define "packet" }}
|
|
|
|
{{- $structName := camelCase .Name true}}
|
|
|
|
|
|
|
|
{{- /* generate any bitfield structs */ -}}
|
|
|
|
{{range .Data -}}
|
|
|
|
{{ if .Bits -}}
|
|
|
|
{{- $bfname := (printf "%s%s" $structName (camelCase .Name true)) }}
|
|
|
|
type {{$bfname}} struct {
|
|
|
|
{{- range $el := .Bits}}
|
|
|
|
{{camelCase $el.Name true}} bool `json:"{{$el.Name}}"`
|
|
|
|
{{- end}}
|
|
|
|
}
|
|
|
|
|
2023-05-29 00:39:03 +00:00
|
|
|
func (p *{{$bfname}}) MarshalByte() byte {
|
2023-05-17 05:01:40 +00:00
|
|
|
var b byte
|
|
|
|
{{- range $idx, $el := .Bits}}
|
|
|
|
{{- $bitName := camelCase $el.Name true}}
|
|
|
|
if p.{{$bitName}} {
|
|
|
|
b |= 1 << {{$idx}}
|
|
|
|
}
|
|
|
|
{{- end}}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2023-05-29 00:39:03 +00:00
|
|
|
func (p *{{$bfname}}) UnmarshalByte(b byte) {
|
2023-05-17 05:01:40 +00:00
|
|
|
{{- range $idx, $el := .Bits}}
|
|
|
|
{{- $bitName := camelCase $el.Name true }}
|
|
|
|
p.{{$bitName}} = (b & (1 << {{ $idx }})) != 0
|
|
|
|
{{- end}}
|
|
|
|
}
|
|
|
|
{{end}}
|
|
|
|
{{- end}}
|
|
|
|
|
|
|
|
// {{$structName}} is {{.Description}}
|
|
|
|
type {{$structName}} struct {
|
|
|
|
{{- range .Data}}
|
2023-05-18 22:47:14 +00:00
|
|
|
{{- if .Units }}
|
|
|
|
// {{.Conversion}} {{.Units}}
|
|
|
|
{{- end }}
|
2023-05-17 05:01:40 +00:00
|
|
|
{{ .ToStructMember $structName }} `json:"{{.Name}}"`
|
|
|
|
{{- end}}
|
|
|
|
{{- if .Repeat }}
|
|
|
|
// Idx is the packet index. The accepted range is 0-{{.Repeat}}
|
2023-05-22 13:41:37 +00:00
|
|
|
Idx uint32 `json:"idx"`
|
2023-05-17 05:01:40 +00:00
|
|
|
{{- end }}
|
|
|
|
}
|
|
|
|
|
2023-05-18 22:47:14 +00:00
|
|
|
func (p *{{$structName}}) CANId() (uint32, error) {
|
2023-05-17 05:01:40 +00:00
|
|
|
{{- if .Repeat }}
|
|
|
|
if p.Idx >= {{.Repeat}} {
|
2023-06-28 01:39:57 +00:00
|
|
|
return 0, &UnknownIdError{ {{ printf "0x%X" .Id }} }
|
2023-05-17 05:01:40 +00:00
|
|
|
}
|
|
|
|
return {{ printf "0x%X" .Id }} + p.Idx, nil
|
|
|
|
{{- else }}
|
|
|
|
return {{ printf "0x%X" .Id }}, nil
|
|
|
|
{{- end }}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *{{$structName}}) Size() uint {
|
|
|
|
return {{.CalcSize}}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *{{$structName}}) MarshalPacket() ([]byte, error) {
|
|
|
|
b := make([]byte, {{ .CalcSize }})
|
|
|
|
{{.MakeMarshal}}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *{{$structName}}) UnmarshalPacket(b []byte) error {
|
|
|
|
{{.MakeUnmarshal}}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *{{$structName}}) String() string {
|
2023-05-22 17:16:48 +00:00
|
|
|
return "{{ .Name }}"
|
2023-05-17 05:01:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{{ end }}
|
|
|
|
|
|
|
|
{{- /* begin actual file template */ -}}
|
|
|
|
|
|
|
|
// generated by gen_skylab.go at {{ Time }} DO NOT EDIT!
|
|
|
|
|
|
|
|
package skylab
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"encoding/binary"
|
|
|
|
"encoding/json"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SkylabId uint32
|
|
|
|
|
|
|
|
const (
|
|
|
|
{{- range .Packets }}
|
|
|
|
{{camelCase .Name true}}Id SkylabId = {{.Id | printf "0x%X"}}
|
|
|
|
{{- end}}
|
|
|
|
)
|
|
|
|
|
|
|
|
// list of every packet ID. can be used for O(1) checks.
|
|
|
|
var idMap = map[uint32]bool{
|
|
|
|
{{ range $p := .Packets -}}
|
|
|
|
{{ if $p.Repeat }}
|
|
|
|
{{ range $idx := Nx (int $p.Id) $p.Repeat $p.Offset -}}
|
|
|
|
{{ $idx | printf "0x%X"}}: true,
|
|
|
|
{{ end }}
|
|
|
|
{{- else }}
|
|
|
|
{{ $p.Id | printf "0x%X" }}: true,
|
|
|
|
{{- end}}
|
|
|
|
{{- end}}
|
|
|
|
}
|
|
|
|
|
2023-05-22 13:41:37 +00:00
|
|
|
// FromCanFrame creates a Packet from a given CAN ID and data payload.
|
|
|
|
// If the CAN ID is unknown, it will return an error.
|
2023-05-17 05:01:40 +00:00
|
|
|
func FromCanFrame(id uint32, data []byte) (Packet, error) {
|
|
|
|
if !idMap[id] {
|
2023-06-28 01:39:57 +00:00
|
|
|
return nil, &UnknownIdError{ id }
|
2023-05-17 05:01:40 +00:00
|
|
|
}
|
|
|
|
switch id {
|
|
|
|
{{- range $p := .Packets }}
|
|
|
|
{{- if $p.Repeat }}
|
|
|
|
case {{ Nx (int $p.Id) $p.Repeat $p.Offset | mapf "0x%X" | strJoin ", " -}}:
|
2023-05-20 19:54:10 +00:00
|
|
|
var res = &{{camelCase $p.Name true}}{}
|
2023-05-17 05:01:40 +00:00
|
|
|
res.UnmarshalPacket(data)
|
|
|
|
res.Idx = id - {{$p.Id | printf "0x%X" }}
|
|
|
|
return res, nil
|
|
|
|
{{- else }}
|
|
|
|
case {{ $p.Id | printf "0x%X" }}:
|
2023-05-20 19:54:10 +00:00
|
|
|
var res = &{{camelCase $p.Name true}}{}
|
2023-05-17 05:01:40 +00:00
|
|
|
res.UnmarshalPacket(data)
|
|
|
|
return res, nil
|
|
|
|
{{- end}}
|
|
|
|
{{- end}}
|
|
|
|
}
|
|
|
|
|
2023-05-29 00:39:03 +00:00
|
|
|
return nil, errors.New("failed to match Id, something is really wrong")
|
2023-05-17 05:01:40 +00:00
|
|
|
}
|
|
|
|
|
2023-05-29 00:39:03 +00:00
|
|
|
|
|
|
|
func FromJson (id uint32, raw []byte) (Packet, error) {
|
|
|
|
if !idMap[id] {
|
|
|
|
return nil, errors.New("unknown id")
|
2023-05-17 05:01:40 +00:00
|
|
|
}
|
2023-05-29 00:39:03 +00:00
|
|
|
switch id {
|
2023-05-17 05:01:40 +00:00
|
|
|
{{- range $p := .Packets }}
|
|
|
|
{{- if $p.Repeat }}
|
|
|
|
case {{ Nx (int $p.Id) $p.Repeat $p.Offset | mapf "0x%X" | strJoin ", " -}}:
|
2023-05-19 21:57:05 +00:00
|
|
|
var res = &{{camelCase $p.Name true}}{}
|
2023-05-29 00:39:03 +00:00
|
|
|
err := json.Unmarshal(raw, res)
|
|
|
|
res.Idx = id - {{ $p.Id | printf "0x%X" }}
|
2023-05-17 05:01:40 +00:00
|
|
|
return res, err
|
|
|
|
{{- else }}
|
|
|
|
case {{ $p.Id | printf "0x%X" }}:
|
2023-05-19 21:57:05 +00:00
|
|
|
var res = &{{camelCase $p.Name true}}{}
|
2023-05-29 00:39:03 +00:00
|
|
|
err := json.Unmarshal(raw, res)
|
2023-05-17 05:01:40 +00:00
|
|
|
return res, err
|
|
|
|
{{- end }}
|
|
|
|
{{- end }}
|
|
|
|
}
|
|
|
|
|
2023-05-29 00:39:03 +00:00
|
|
|
return nil, errors.New("failed to match id")
|
2023-05-17 05:01:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{{range .Packets -}}
|
|
|
|
{{template "packet" .}}
|
|
|
|
{{- end}}
|
|
|
|
|
2023-06-23 21:20:28 +00:00
|
|
|
// The json representation that was used to generate this data.
|
|
|
|
// can be used to share the parsing data for i.e dynamic python gui.
|
2023-06-28 01:39:57 +00:00
|
|
|
const SkylabDefinitions = `{{json . | printf "%s" }}`
|