github.com/blueinnovationsgroup/can-go@v0.0.0-20230518195432-d0567cda0028/pkg/dbc/analysis/passes/multiplexedsignals/analyzer.go (about)

     1  package multiplexedsignals
     2  
     3  import (
     4  	"github.com/blueinnovationsgroup/can-go/pkg/dbc"
     5  	"github.com/blueinnovationsgroup/can-go/pkg/dbc/analysis"
     6  )
     7  
     8  func Analyzer() *analysis.Analyzer {
     9  	return &analysis.Analyzer{
    10  		Name: "multiplexedsignals",
    11  		Doc:  "check that multiplexed signals are valid",
    12  		Run:  run,
    13  	}
    14  }
    15  
    16  func run(pass *analysis.Pass) error {
    17  	for _, def := range pass.File.Defs {
    18  		message, ok := def.(*dbc.MessageDef)
    19  		if !ok {
    20  			continue
    21  		}
    22  		// locate multiplexer switch
    23  		var multiplexerSwitch *dbc.SignalDef
    24  		for i := range message.Signals {
    25  			if !message.Signals[i].IsMultiplexerSwitch {
    26  				continue
    27  			}
    28  			if multiplexerSwitch != nil {
    29  				pass.Reportf(message.Signals[i].Pos, "more than one multiplexer switch")
    30  				continue
    31  			}
    32  			multiplexerSwitch = &message.Signals[i]
    33  			if multiplexerSwitch.IsSigned {
    34  				pass.Reportf(message.Signals[i].Pos, "signed multiplexer switch")
    35  				continue
    36  			}
    37  			if multiplexerSwitch.IsMultiplexed {
    38  				pass.Reportf(message.Signals[i].Pos, "can't be multiplexer and multiplexed")
    39  				continue
    40  			}
    41  		}
    42  		for i := range message.Signals {
    43  			signal := &message.Signals[i]
    44  			if !signal.IsMultiplexed {
    45  				continue
    46  			}
    47  			if multiplexerSwitch == nil {
    48  				pass.Reportf(message.Signals[i].Pos, "no multiplexer switch for multiplexed signal")
    49  				continue
    50  			}
    51  			multiplexerSwitchMaxValue := uint64((1 << multiplexerSwitch.Size) - 1)
    52  			if signal.MultiplexerSwitch > multiplexerSwitchMaxValue {
    53  				pass.Reportf(signal.Pos, "multiplexer switch exceeds max value: %v", multiplexerSwitchMaxValue)
    54  				continue
    55  			}
    56  		}
    57  	}
    58  	return nil
    59  }