rts-sim-testing-service/third_party/message/can_btm_status_rsp.go
2024-07-25 14:55:46 +08:00

211 lines
5.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package message
import (
"encoding/binary"
"fmt"
"strings"
)
// BtmStatusRspFrame BTM发往ATP的状态应答帧
type BtmStatusRspFrame struct {
//帧ID
FId CanFrameId
//数据流水号0255变化每次加一
Dsn byte
//应答器计数(每过一个应答器加一,在同一个应答器内不变)
BaliseCounter byte
//报文计数器(每解出一个报文加一)
MessageCounter byte
//功率放大器状态true-1 turn on ;false-0 turn off
PowerAmplifierOn bool
//ATP查询帧CRC16校验结果,true-1校验失败false-0校验通过
AtpReqCrcCheckWrong bool
//功率放大器故障,true-1故障false-0正常
PowerAmplifierFailure bool
//天线故障,true-1故障false-0正常
AntennaFault bool
//BTM detailed code
DetailedCode byte
//有具体报文内容时TK A时刻填充为CD信号上升沿时刻报文全零时TK A时刻填充为CD信号下降沿时刻。
TkTimeA uint32
}
func NewBtmStatusRspFrame(sn byte) *BtmStatusRspFrame {
return &BtmStatusRspFrame{
FId: *NewCanFrameId(CAN_ADDR_RSP_ATP, CAN_ADDR_RSP_BTM, CAN_FRAME_STATUS_RSP, sn),
//FId: *NewCanFrameId(0x00, 0x00, 0x05, sn),
}
}
func (f *BtmStatusRspFrame) Decode(cf *CanetFrame) bool {
f.FId = cf.CanId
//
reader := NewCanBitsReader(cf.CanData, 8)
//数据流水号
if d, ok := reader.ReadByte(); ok {
f.Dsn = d
} else {
return false
}
//应答器计数
if d, ok := reader.ReadByte(); ok {
f.BaliseCounter = d
} else {
return false
}
//报文计数器
if d, ok := reader.ReadByte(); ok {
f.MessageCounter = d
} else {
return false
}
//功率放大器状态
if d, ok := reader.ReadBits(1); ok {
f.PowerAmplifierOn = 1 == d
} else {
return false
}
//ATP查询帧CRC16校验结果
if d, ok := reader.ReadBits(1); ok {
f.AtpReqCrcCheckWrong = 1 == d
} else {
return false
}
//功率放大器故障
if d, ok := reader.ReadBits(1); ok {
f.PowerAmplifierFailure = 1 == d
} else {
return false
}
//天线故障
if d, ok := reader.ReadBits(1); ok {
f.AntennaFault = 1 == d
} else {
return false
}
//BTM detailed code
if d, ok := reader.ReadBits(4); ok {
f.DetailedCode = d
} else {
return false
}
//tk time a
tk1, tk1Ok := reader.ReadByte()
if !tk1Ok {
return false
}
tk2, tk2Ok := reader.ReadByte()
if !tk2Ok {
return false
}
tk3, tk3Ok := reader.ReadByte()
if !tk3Ok {
return false
}
tk4, tk4Ok := reader.ReadByte()
if !tk4Ok {
return false
}
f.TkTimeA = 0
f.TkTimeA |= uint32(tk1) << 24
f.TkTimeA |= uint32(tk2) << 16
f.TkTimeA |= uint32(tk3) << 8
f.TkTimeA |= uint32(tk4)
//
return true
}
func (f *BtmStatusRspFrame) EncodeBtmAtp() *BtmHeadFrame {
cf := &BtmHeadFrame{}
cf.CanId = f.FId
cf.CanLen = 8
data := make([]byte, 0)
data = append(data, f.Dsn)
data = append(data, f.BaliseCounter)
data = append(data, f.MessageCounter)
var state byte = 0
if f.PowerAmplifierOn {
state = setBit(state, 7, 1)
}
if f.AtpReqCrcCheckWrong {
state = setBit(state, 6, 1)
}
if f.PowerAmplifierFailure {
state = setBit(state, 5, 1)
}
if f.AntennaFault {
state = setBit(state, 4, 1)
}
data = append(data, state)
data = binary.BigEndian.AppendUint32(data, f.TkTimeA)
cf.CanData = data
return cf
}
func (f *BtmStatusRspFrame) Encode() *CanetFrame {
cf := &CanetFrame{}
cf.CanId = f.FId
cf.CanLen = 8
cf.FF = true
cf.RTR = false
//
writer := NewCanBitsWriter(8)
writer.AddByte(f.Dsn)
writer.AddByte(f.BaliseCounter)
writer.AddByte(f.MessageCounter)
//
if f.PowerAmplifierOn {
writer.AddBits(1, 1)
} else {
writer.AddBits(0, 1)
}
//
if f.AtpReqCrcCheckWrong {
writer.AddBits(1, 1)
} else {
writer.AddBits(0, 1)
}
//
if f.PowerAmplifierFailure {
writer.AddBits(1, 1)
} else {
writer.AddBits(0, 1)
}
//
if f.AntennaFault {
writer.AddBits(1, 1)
} else {
writer.AddBits(0, 1)
}
//
writer.AddBits(f.DetailedCode, 4)
//tk time a
tkMsk := uint32(0x00_00_00_ff)
tk1 := byte((f.TkTimeA >> 24) & tkMsk)
tk2 := byte((f.TkTimeA >> 16) & tkMsk)
tk3 := byte((f.TkTimeA >> 8) & tkMsk)
tk4 := byte(f.TkTimeA & tkMsk)
writer.AddByte(tk1)
writer.AddByte(tk2)
writer.AddByte(tk3)
writer.AddByte(tk4)
//
cf.CanData = writer.(CanBusData).GetData()
return cf
}
func (f *BtmStatusRspFrame) String() string {
sb := strings.Builder{}
sb.WriteString(fmt.Sprintf("BtmStatusRspFrame ID1 = 0x%0x, ID2 = 0x%0x, ID3 = 0x%0x, ID4 = 0x%0x,",
f.FId.ID1, f.FId.ID2, f.FId.ID3, f.FId.ID4))
sb.WriteString(fmt.Sprintf("Dsn = %d", f.Dsn))
sb.WriteString(fmt.Sprintf(",BaliseCounter = %d", f.BaliseCounter))
sb.WriteString(fmt.Sprintf(",MessageCounter = %d", f.MessageCounter))
sb.WriteString(fmt.Sprintf(",PowerAmplifierOn = %t", f.PowerAmplifierOn))
sb.WriteString(fmt.Sprintf(",AtpReqCrcCheckWrong = %t", f.AtpReqCrcCheckWrong))
sb.WriteString(fmt.Sprintf(",PowerAmplifierFailure = %t", f.PowerAmplifierFailure))
sb.WriteString(fmt.Sprintf(",AntennaFault = %t", f.AntennaFault))
sb.WriteString(fmt.Sprintf(",DetailedCode = %d", f.DetailedCode))
sb.WriteString(fmt.Sprintf(",TkTimeA = %d", f.TkTimeA))
return sb.String()
}