2023-11-17 17:10:15 +08:00
|
|
|
|
package message
|
|
|
|
|
|
2023-11-27 09:14:20 +08:00
|
|
|
|
import (
|
2024-07-25 14:55:46 +08:00
|
|
|
|
"encoding/binary"
|
2023-11-27 09:14:20 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
2023-11-17 17:10:15 +08:00
|
|
|
|
// BtmTimeSyncCheckFrame BTM发往ATP的时间校验帧
|
|
|
|
|
type BtmTimeSyncCheckFrame struct {
|
|
|
|
|
//帧ID
|
|
|
|
|
FId CanFrameId
|
|
|
|
|
//T2时刻
|
|
|
|
|
//当收到某一流水号的ATP查询帧后,BTM将记录接收到该查询帧的时间值做为T2时刻。
|
|
|
|
|
T2 uint32
|
|
|
|
|
//T3时刻
|
|
|
|
|
//BTM将在T3时刻发送回复帧给ATP,并保持流水号与ATP查询帧一致。
|
2024-07-25 14:55:46 +08:00
|
|
|
|
T3 uint32
|
2023-11-17 17:10:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-25 14:55:46 +08:00
|
|
|
|
func NewBtmTimeSyncCheckFrame(sn byte) *BtmTimeSyncCheckFrame {
|
2023-11-17 17:10:15 +08:00
|
|
|
|
return &BtmTimeSyncCheckFrame{
|
2024-07-04 09:26:37 +08:00
|
|
|
|
FId: *NewCanFrameId(CAN_ADDR_RSP_ATP, CAN_ADDR_RSP_BTM, CAN_FRAME_TIME_SYNC_RSP, sn),
|
|
|
|
|
//FId: *NewCanFrameId(0x00, 0x00, 0x35, sn),
|
2024-07-25 14:55:46 +08:00
|
|
|
|
|
2023-11-17 17:10:15 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-27 09:14:20 +08:00
|
|
|
|
|
2023-11-21 17:20:03 +08:00
|
|
|
|
func (f *BtmTimeSyncCheckFrame) Decode(cf *CanetFrame) bool {
|
|
|
|
|
f.FId = cf.CanId
|
2023-11-17 17:10:15 +08:00
|
|
|
|
//
|
2023-11-21 17:20:03 +08:00
|
|
|
|
reader := NewCanBitsReader(cf.CanData, 8)
|
2023-11-17 17:10:15 +08:00
|
|
|
|
//T2
|
|
|
|
|
t21, t21Ok := reader.ReadByte()
|
|
|
|
|
if !t21Ok {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
t22, t22Ok := reader.ReadByte()
|
|
|
|
|
if !t22Ok {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
t23, t23Ok := reader.ReadByte()
|
|
|
|
|
if !t23Ok {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
t24, t24Ok := reader.ReadByte()
|
|
|
|
|
if !t24Ok {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
f.T2 = 0
|
|
|
|
|
f.T2 |= uint32(t21) << 24
|
|
|
|
|
f.T2 |= uint32(t22) << 16
|
|
|
|
|
f.T2 |= uint32(t23) << 8
|
|
|
|
|
f.T2 |= uint32(t24)
|
|
|
|
|
//T3
|
|
|
|
|
t31, t31Ok := reader.ReadByte()
|
|
|
|
|
if !t31Ok {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
t32, t32Ok := reader.ReadByte()
|
|
|
|
|
if !t32Ok {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
t33, t33Ok := reader.ReadByte()
|
|
|
|
|
if !t33Ok {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
t34, t34Ok := reader.ReadByte()
|
|
|
|
|
if !t34Ok {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
f.T3 = 0
|
|
|
|
|
f.T3 |= uint32(t31) << 24
|
|
|
|
|
f.T3 |= uint32(t32) << 16
|
|
|
|
|
f.T3 |= uint32(t33) << 8
|
|
|
|
|
f.T3 |= uint32(t34)
|
|
|
|
|
//
|
|
|
|
|
return true
|
|
|
|
|
}
|
2024-07-25 14:55:46 +08:00
|
|
|
|
|
|
|
|
|
func (f *BtmTimeSyncCheckFrame) EncodeBtmAtp() *BtmHeadFrame {
|
|
|
|
|
cf := &BtmHeadFrame{}
|
|
|
|
|
cf.CanId = f.FId
|
|
|
|
|
cf.CanLen = 8
|
|
|
|
|
data := make([]byte, 0)
|
|
|
|
|
data = binary.BigEndian.AppendUint32(data, f.T2)
|
|
|
|
|
data = binary.BigEndian.AppendUint32(data, f.T3)
|
|
|
|
|
cf.CanData = data
|
|
|
|
|
|
|
|
|
|
return cf
|
|
|
|
|
}
|
2023-11-21 17:20:03 +08:00
|
|
|
|
func (f *BtmTimeSyncCheckFrame) Encode() *CanetFrame {
|
|
|
|
|
cf := &CanetFrame{}
|
2024-07-25 14:55:46 +08:00
|
|
|
|
|
2023-11-21 17:20:03 +08:00
|
|
|
|
cf.CanId = f.FId
|
|
|
|
|
cf.CanLen = 8
|
|
|
|
|
cf.FF = true
|
|
|
|
|
cf.RTR = false
|
|
|
|
|
//
|
|
|
|
|
writer := NewCanBitsWriter(8)
|
2023-11-17 17:10:15 +08:00
|
|
|
|
//
|
|
|
|
|
timeByteMsk := uint32(0x00_00_00_ff)
|
|
|
|
|
//t2
|
|
|
|
|
t21 := byte((f.T2 >> 24) & timeByteMsk)
|
|
|
|
|
t22 := byte((f.T2 >> 16) & timeByteMsk)
|
|
|
|
|
t23 := byte((f.T2 >> 8) & timeByteMsk)
|
|
|
|
|
t24 := byte(f.T2 & timeByteMsk)
|
|
|
|
|
writer.AddByte(t21)
|
|
|
|
|
writer.AddByte(t22)
|
|
|
|
|
writer.AddByte(t23)
|
|
|
|
|
writer.AddByte(t24)
|
|
|
|
|
//t3
|
|
|
|
|
t31 := byte((f.T3 >> 24) & timeByteMsk)
|
|
|
|
|
t32 := byte((f.T3 >> 16) & timeByteMsk)
|
|
|
|
|
t33 := byte((f.T3 >> 8) & timeByteMsk)
|
|
|
|
|
t34 := byte(f.T3 & timeByteMsk)
|
|
|
|
|
writer.AddByte(t31)
|
|
|
|
|
writer.AddByte(t32)
|
|
|
|
|
writer.AddByte(t33)
|
|
|
|
|
writer.AddByte(t34)
|
|
|
|
|
//
|
2023-11-21 17:20:03 +08:00
|
|
|
|
cf.CanData = writer.(CanBusData).GetData()
|
|
|
|
|
return cf
|
2023-11-17 17:10:15 +08:00
|
|
|
|
}
|
2023-11-27 09:14:20 +08:00
|
|
|
|
func (f *BtmTimeSyncCheckFrame) String() string {
|
|
|
|
|
sb := strings.Builder{}
|
|
|
|
|
sb.WriteString(fmt.Sprintf("BtmTimeSyncCheckFrame 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("T2 = %d", f.T2))
|
|
|
|
|
sb.WriteString(fmt.Sprintf(",T3 = %d", f.T3))
|
|
|
|
|
return sb.String()
|
|
|
|
|
}
|