Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cantool/generate: handle mux bool types correctly + skip overflow values #52

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions internal/generate/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"go/format"
"go/types"
"log"
"path"
"regexp"
"strings"
Expand Down Expand Up @@ -128,6 +129,15 @@ func slugifyString(s string) string {
return nonAlphaNumericRegexp.ReplaceAllString(s, "")
}

func countBits(v int) int {
out := 0
for v != 0 {
out += 1
v = v >> 1
}
return out
}

func SignalCustomType(f *File, m *descriptor.Message, s *descriptor.Signal) {
f.P("// ", signalType(m, s), " models the ", s.Name, " signal of the ", m.Name, " message.")
f.P("type ", signalType(m, s), " ", signalPrimitiveType(s))
Expand All @@ -136,6 +146,10 @@ func SignalCustomType(f *File, m *descriptor.Message, s *descriptor.Signal) {
f.P("const (")
for _, vd := range s.ValueDescriptions {
desc := slugifyString(vd.Description)
if countBits(vd.Value) > int(s.Length) {
log.Printf("warning: %s = %d overflows len %d", desc, vd.Value, s.Length)
continue
}
switch {
case s.Length == 1 && vd.Value == 1:
f.P(signalType(m, s), "_", desc, " ", signalType(m, s), " = true")
Expand Down Expand Up @@ -163,6 +177,10 @@ func SignalCustomType(f *File, m *descriptor.Message, s *descriptor.Signal) {
} else {
f.P("switch v {")
for _, vd := range s.ValueDescriptions {
if countBits(vd.Value) > int(s.Length) {
log.Printf("warning: %s = %d overflows len %d", vd.Description, vd.Value, s.Length)
continue
}
f.P("case ", vd.Value, ":")
f.P(`return "`, vd.Description, `"`)
}
Expand Down Expand Up @@ -402,7 +420,15 @@ func MarshalFrame(f *File, m *descriptor.Message) {
if !s.IsMultiplexed {
continue
}
f.P("if m.", signalField(mux), " == ", s.MultiplexerValue, " {")
var v interface{} = s.MultiplexerValue
if mux.Length == 1 {
if s.MultiplexerValue == 0 {
v = "false"
} else {
v = "true"
}
}
f.P("if m.", signalField(mux), " == ", v, " {")
f.P(
"md.", s.Name, ".Marshal", signalSuperType(s), "(&f.Data, ", signalPrimitiveSuperType(s),
"(m.", signalField(s), "))",
Expand Down Expand Up @@ -467,7 +493,15 @@ func UnmarshalFrame(f *File, m *descriptor.Message) {
if !s.IsMultiplexed {
continue
}
f.P("if m.", signalField(mux), " == ", s.MultiplexerValue, " {")
var v interface{} = s.MultiplexerValue
if mux.Length == 1 {
if s.MultiplexerValue == 0 {
v = "false"
} else {
v = "true"
}
}
f.P("if m.", signalField(mux), " == ", v, " {")
f.P("m.", signalField(s), " = ", signalType(m, s), "(md.", s.Name, ".Unmarshal", signalSuperType(s), "(f.Data))")
f.P("}")
}
Expand Down