gotelem/skylab/templates/golang.go.tmpl

168 lines
3.6 KiB
Cheetah
Raw Normal View History

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}}
}
func (p *{{$bfname}}) Marshal() byte {
var b byte
{{- range $idx, $el := .Bits}}
{{- $bitName := camelCase $el.Name true}}
if p.{{$bitName}} {
b |= 1 << {{$idx}}
}
{{- end}}
return b
}
func (p *{{$bfname}}) Unmarshal(b byte) {
{{- 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}}
Idx uint32
{{- 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}} {
return 0, errors.New("invalid packet index")
}
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 {
return ""
}
{{ 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}}
}
func FromCanFrame(id uint32, data []byte) (Packet, error) {
if !idMap[id] {
return nil, errors.New("Unknown Id")
}
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}}
}
return nil, errors.New("failed to match Id, something is really wrong!")
}
func FromJson (raw []byte) (Packet, error) {
// attempt to parse the JSON to a JSONPacket
jp := &JSONPacket{}
err := json.Unmarshal(raw, jp)
if err != nil {
return nil, err
}
switch jp.Id {
{{- 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-17 05:01:40 +00:00
err := json.Unmarshal(jp.Data, res)
res.Idx = jp.Id - {{ $p.Id | printf "0x%X" }}
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-17 05:01:40 +00:00
err := json.Unmarshal(jp.Data, res)
return res, err
{{- end }}
{{- end }}
}
return nil, errors.New("aaa")
}
{{range .Packets -}}
{{template "packet" .}}
{{- end}}