2023-11-20 16:55:07 +08:00
|
|
|
|
package message
|
|
|
|
|
|
2024-07-25 14:55:46 +08:00
|
|
|
|
import (
|
|
|
|
|
"encoding/binary"
|
|
|
|
|
"fmt"
|
|
|
|
|
"log/slog"
|
|
|
|
|
)
|
2023-11-20 16:55:07 +08:00
|
|
|
|
|
2024-07-25 14:55:46 +08:00
|
|
|
|
// 返回应答器数据帧(数据帧 + timeA+timeB+结束帧)
|
2024-08-15 14:54:16 +08:00
|
|
|
|
func CreateBtmAtpDataRspFramesData(statusRsp *BtmHeadFrame, msg []byte, msgPackError, haxBaliseData bool, msgTimeA uint32, msgTimeB uint32, tkTimeB uint32) ([]byte, bool) {
|
2024-07-25 14:55:46 +08:00
|
|
|
|
sn := statusRsp.CanId.ID4
|
|
|
|
|
//应答器整个是16个数据帧,其中报文占13帧,timeA,timeB 占2帧,最后是结束帧
|
2024-07-11 14:58:34 +08:00
|
|
|
|
//数据
|
2024-07-25 14:55:46 +08:00
|
|
|
|
dms := make([]*BtmDataMessageAtpFrame, 13)
|
2024-07-11 14:58:34 +08:00
|
|
|
|
for mr := 0x00; mr <= 0x0c; mr++ {
|
2024-07-25 14:55:46 +08:00
|
|
|
|
dms[mr] = CreateBtmDataMessageDataAtpFrame(sn, byte(mr))
|
2024-07-11 14:58:34 +08:00
|
|
|
|
dms[mr].Message = make([]byte, 8) //8字节数组,默认值0
|
|
|
|
|
//
|
|
|
|
|
if !msgPackError {
|
|
|
|
|
mi := mr * 8
|
|
|
|
|
if mi < len(msg) { //数据帧中有<=8个字节数据
|
|
|
|
|
if mi+7 < len(msg) {
|
|
|
|
|
dms[mr].Message = msg[mi : mi+8]
|
|
|
|
|
} else {
|
|
|
|
|
for i, d := range msg[mi:] {
|
|
|
|
|
dms[mr].Message[i] = d
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else { //BTM解包发生错误,则数据帧及CRC32A/B全填“0xFF”
|
|
|
|
|
for c := 0; c < 8; c++ {
|
|
|
|
|
dms[mr].Message[c] = 0xff
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-15 14:54:16 +08:00
|
|
|
|
dtA := createTimeA(sn, DATA_TIME_A_OFFSET, msgTimeA, msgPackError, msg, haxBaliseData)
|
|
|
|
|
dtB := createTimeA(sn, DATA_TIME_B_OFFSET, msgTimeB, msgPackError, msg, haxBaliseData)
|
2024-07-11 14:58:34 +08:00
|
|
|
|
dtACf := dtA.Encode()
|
|
|
|
|
dtBCf := dtB.Encode()
|
2024-07-25 14:55:46 +08:00
|
|
|
|
|
|
|
|
|
//结束帧
|
|
|
|
|
|
|
|
|
|
end := createDataAtpEnd(sn, tkTimeB, statusRsp.CanData, msg, dtACf.CanData, dtBCf.CanData)
|
|
|
|
|
|
2024-07-11 14:58:34 +08:00
|
|
|
|
endCf := end.Encode()
|
2024-07-25 14:55:46 +08:00
|
|
|
|
rt := make([]byte, 0) //17*12
|
|
|
|
|
for _, dmCf := range dms {
|
|
|
|
|
rt = append(rt, dmCf.Encode().Encode()...)
|
2024-07-11 14:58:34 +08:00
|
|
|
|
}
|
2024-07-25 14:55:46 +08:00
|
|
|
|
rt = append(rt, dtACf.Encode()...)
|
|
|
|
|
rt = append(rt, dtBCf.Encode()...)
|
|
|
|
|
rt = append(rt, endCf.Encode()...)
|
|
|
|
|
if len(rt) != 192 {
|
|
|
|
|
slog.Warn(fmt.Sprintf("len(rt)!=192 实际数据长度:%v", len(rt)))
|
2024-07-11 14:58:34 +08:00
|
|
|
|
return nil, false
|
|
|
|
|
}
|
|
|
|
|
return rt, true
|
|
|
|
|
}
|
2024-08-15 14:54:16 +08:00
|
|
|
|
|
2024-07-25 14:55:46 +08:00
|
|
|
|
func createDataAtpEnd(sn byte, time uint32, statusData, msg, dtAData, dtBData []byte) *BtmDataMessageAtpTimeAndEndFrame {
|
|
|
|
|
end := BtmDataMessageEndAtpFrame(sn)
|
|
|
|
|
end.Time = time
|
|
|
|
|
crc32cData := make([]byte, 0)
|
|
|
|
|
|
|
|
|
|
crc32cData = append(crc32cData, statusData...)
|
|
|
|
|
crc32cData = append(crc32cData, msg...)
|
|
|
|
|
crc32cData = append(crc32cData, dtAData...)
|
|
|
|
|
crc32cData = append(crc32cData, dtBData...)
|
|
|
|
|
crc32cData = binary.BigEndian.AppendUint32(crc32cData, end.Time)
|
|
|
|
|
end.Crc32 = Can_Crc32(crc32cData)
|
2024-08-15 14:54:16 +08:00
|
|
|
|
|
2024-07-25 14:55:46 +08:00
|
|
|
|
return end
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-15 14:54:16 +08:00
|
|
|
|
func createTimeA(sn, offset byte, time uint32, msgPackError bool, msg []byte, hasBaliseData bool) *BtmDataMessageAtpTimeAndEndFrame {
|
2024-07-25 14:55:46 +08:00
|
|
|
|
tf := CreateBtmDataMessageTimeAtpFrame(sn, offset)
|
|
|
|
|
tf.Time = time
|
|
|
|
|
if !msgPackError {
|
|
|
|
|
var crcData []byte
|
|
|
|
|
crcData = append(crcData, msg...)
|
|
|
|
|
crcData = binary.BigEndian.AppendUint32(crcData, time)
|
2024-08-15 14:54:16 +08:00
|
|
|
|
if !hasBaliseData {
|
|
|
|
|
tf.Crc32 = 0
|
2024-07-25 14:55:46 +08:00
|
|
|
|
} else {
|
2024-08-15 14:54:16 +08:00
|
|
|
|
if offset == DATA_TIME_A_OFFSET {
|
|
|
|
|
tf.Crc32 = Can_Crc32A(crcData) //CRC32A的校验范围是:报文+时间戳B
|
2024-07-25 14:55:46 +08:00
|
|
|
|
|
2024-08-15 14:54:16 +08:00
|
|
|
|
} else {
|
|
|
|
|
tf.Crc32 = Can_Crc32B(crcData) //CRC32B的校验范围是:报文+时间戳B
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-25 14:55:46 +08:00
|
|
|
|
} else { //BTM解包发生错误,则数据帧及CRC32A/B全填“0xFF”
|
|
|
|
|
tf.Crc32 = 0xff_ff_ff_ff
|
|
|
|
|
}
|
|
|
|
|
return tf
|
|
|
|
|
}
|
2024-07-11 14:58:34 +08:00
|
|
|
|
|
2024-04-18 11:14:05 +08:00
|
|
|
|
// CreateBtmRspFramesData BTM与ATP之间为双向通信,ATP(主)定时发送请求帧,BTM(从)在未接收到应答器报文时回复状态应答器帧和时间同步帧,在接收到应答器报文时回复所有帧
|
|
|
|
|
//
|
|
|
|
|
// 数据帧与状态应答帧同时发送给ATP
|
|
|
|
|
//
|
2023-11-20 16:55:07 +08:00
|
|
|
|
// 共17帧,17X12个字节,每个帧12字节
|
|
|
|
|
// msg - 应答器报文
|
|
|
|
|
// msgPackError - true BTM解包发生错误,则数据帧及CRC32A/B全填“0xFF”
|
2024-07-25 14:55:46 +08:00
|
|
|
|
func CreateBtmRspFramesData(statusRsp *BtmStatusRspFrame, msg []byte, msgPackError bool, msgTimeA uint32, msgTimeB uint32, tkTimeB uint32) ([]byte, bool) {
|
2024-04-18 11:14:05 +08:00
|
|
|
|
/* if len(msg) > 104 { //数据帧最多存储13*8个字节
|
2023-11-20 16:55:07 +08:00
|
|
|
|
return nil, false
|
2024-04-18 11:14:05 +08:00
|
|
|
|
}*/
|
2023-11-20 16:55:07 +08:00
|
|
|
|
//最近一次ATP查询请求序列号
|
|
|
|
|
sn := statusRsp.FId.ID4
|
|
|
|
|
//13个BtmDataMessageFrame [0x00,0x0c]
|
2024-04-02 18:20:10 +08:00
|
|
|
|
//数据
|
2023-11-20 16:55:07 +08:00
|
|
|
|
dms := make([]*BtmDataMessageFrame, 13)
|
|
|
|
|
for mr := 0x00; mr <= 0x0c; mr++ {
|
2024-07-25 14:55:46 +08:00
|
|
|
|
dms[mr] = NewBtmDataMessageFrame(sn, byte(mr))
|
2023-11-20 16:55:07 +08:00
|
|
|
|
dms[mr].Message = make([]byte, 8) //8字节数组,默认值0
|
|
|
|
|
//
|
|
|
|
|
if !msgPackError {
|
|
|
|
|
mi := mr * 8
|
|
|
|
|
if mi < len(msg) { //数据帧中有<=8个字节数据
|
|
|
|
|
if mi+7 < len(msg) {
|
|
|
|
|
dms[mr].Message = msg[mi : mi+8]
|
|
|
|
|
} else {
|
|
|
|
|
for i, d := range msg[mi:] {
|
|
|
|
|
dms[mr].Message[i] = d
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else { //BTM解包发生错误,则数据帧及CRC32A/B全填“0xFF”
|
|
|
|
|
for c := 0; c < 8; c++ {
|
|
|
|
|
dms[mr].Message[c] = 0xff
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//
|
2024-07-25 14:55:46 +08:00
|
|
|
|
dtA := NewBtmDataMessageTimeAFrame(sn)
|
2023-11-20 16:55:07 +08:00
|
|
|
|
dtA.TimeA = msgTimeA
|
|
|
|
|
if !msgPackError {
|
|
|
|
|
var crc32AData []byte
|
|
|
|
|
crc32AData = append(crc32AData, msg...)
|
|
|
|
|
crc32AData = append(crc32AData, canTimeToBytes(dtA.TimeA)...)
|
2024-01-22 13:10:10 +08:00
|
|
|
|
dtA.Crc32A = Can_Crc32(crc32AData) //CRC32A的校验范围是:报文+时间戳A
|
2023-11-20 16:55:07 +08:00
|
|
|
|
} else { //BTM解包发生错误,则数据帧及CRC32A/B全填“0xFF”
|
|
|
|
|
dtA.Crc32A = 0xff_ff_ff_ff
|
|
|
|
|
}
|
|
|
|
|
//
|
2024-07-25 14:55:46 +08:00
|
|
|
|
dtB := NewBtmDataMessageTimeBFrame(sn)
|
2024-04-18 11:14:05 +08:00
|
|
|
|
dtB.TimeB = msgTimeB
|
|
|
|
|
if !msgPackError {
|
|
|
|
|
var crc32BData []byte
|
|
|
|
|
crc32BData = append(crc32BData, msg...)
|
|
|
|
|
crc32BData = append(crc32BData, canTimeToBytes(dtB.TimeB)...)
|
|
|
|
|
dtB.Crc32B = Can_Crc32(crc32BData) //CRC32B的校验范围是:报文+时间戳B
|
|
|
|
|
} else { //BTM解包发生错误,则数据帧及CRC32A/B全填“0xFF”
|
|
|
|
|
dtB.Crc32B = 0xff_ff_ff_ff
|
2023-11-20 16:55:07 +08:00
|
|
|
|
}
|
|
|
|
|
//
|
2024-07-25 14:55:46 +08:00
|
|
|
|
end := NewBtmDataMessageEndFrame(sn)
|
2023-11-20 16:55:07 +08:00
|
|
|
|
end.TkB = tkTimeB
|
|
|
|
|
//
|
2023-11-21 17:20:03 +08:00
|
|
|
|
statusCf := statusRsp.Encode()
|
|
|
|
|
dmsCfs := make([]*CanetFrame, 0, 13)
|
2023-11-20 16:55:07 +08:00
|
|
|
|
for _, dm := range dms {
|
2023-11-21 17:20:03 +08:00
|
|
|
|
dmsCfs = append(dmsCfs, dm.Encode())
|
2023-11-20 16:55:07 +08:00
|
|
|
|
}
|
2023-11-21 17:20:03 +08:00
|
|
|
|
dtACf := dtA.Encode()
|
|
|
|
|
dtBCf := dtB.Encode()
|
|
|
|
|
//
|
|
|
|
|
crc32cData := make([]byte, 0, 132)
|
|
|
|
|
crc32cData = append(crc32cData, statusCf.CanData...)
|
|
|
|
|
for _, dmCf := range dmsCfs {
|
|
|
|
|
crc32cData = append(crc32cData, dmCf.CanData...)
|
2023-11-20 16:55:07 +08:00
|
|
|
|
}
|
2023-11-21 17:20:03 +08:00
|
|
|
|
crc32cData = append(crc32cData, dtACf.CanData...)
|
|
|
|
|
crc32cData = append(crc32cData, dtBCf.CanData...)
|
|
|
|
|
crc32cData = append(crc32cData, canTimeToBytes(end.TkB)...)
|
2023-11-20 16:55:07 +08:00
|
|
|
|
//
|
2024-01-22 13:10:10 +08:00
|
|
|
|
end.Crc32C = Can_Crc32(crc32cData)
|
2023-11-20 16:55:07 +08:00
|
|
|
|
//
|
2023-11-21 17:20:03 +08:00
|
|
|
|
endCf := end.Encode()
|
|
|
|
|
//
|
|
|
|
|
rt := make([]byte, 0, 221) //17*13
|
|
|
|
|
rt = append(rt, statusCf.Encode()...)
|
|
|
|
|
for _, dmCf := range dmsCfs {
|
|
|
|
|
rt = append(rt, dmCf.Encode()...)
|
2023-11-20 16:55:07 +08:00
|
|
|
|
}
|
2023-11-21 17:20:03 +08:00
|
|
|
|
rt = append(rt, dtACf.Encode()...)
|
|
|
|
|
rt = append(rt, dtBCf.Encode()...)
|
|
|
|
|
rt = append(rt, endCf.Encode()...)
|
|
|
|
|
if len(rt) != 221 {
|
|
|
|
|
slog.Warn("len(rt)!=221")
|
|
|
|
|
return nil, false
|
2023-11-20 16:55:07 +08:00
|
|
|
|
}
|
2024-07-25 14:55:46 +08:00
|
|
|
|
|
2023-11-21 17:20:03 +08:00
|
|
|
|
return rt, true
|
2023-11-20 16:55:07 +08:00
|
|
|
|
}
|