diff --git a/examples/zinx_decoder/bili/router/bili0x10router.go b/examples/zinx_decoder/bili/router/bili0x10router.go index c8db6d54..6dd0149e 100644 --- a/examples/zinx_decoder/bili/router/bili0x10router.go +++ b/examples/zinx_decoder/bili/router/bili0x10router.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/hex" "github.com/aceld/zinx/examples/zinx_decoder/bili/utils" + "github.com/aceld/zinx/examples/zinx_decoder/decode" "github.com/aceld/zinx/ziface" "github.com/aceld/zinx/zlog" "github.com/aceld/zinx/znet" @@ -18,8 +19,8 @@ func (this *Data0x10Router) Handle(request ziface.IRequest) { _response := request.GetResponse() if _response != nil { switch _response.(type) { - case BiliData: - _data := _response.(BiliData) + case decode.HtlvCrcData: + _data := _response.(decode.HtlvCrcData) //zlog.Ins().DebugF("Data0x10Router %v \n", _data) buffer := pack10(_data) request.GetConnection().Send(buffer) @@ -29,13 +30,13 @@ func (this *Data0x10Router) Handle(request ziface.IRequest) { // 头码 功能码 数据长度 Body CRC // A2 10 0E 0102030405060708091011121314 050B -func pack10(_data BiliData) []byte { +func pack10(_data decode.HtlvCrcData) []byte { buffer := bytes.NewBuffer([]byte{}) buffer.WriteByte(0xA1) - buffer.WriteByte(_data.funcode) + buffer.WriteByte(_data.Funcode) buffer.WriteByte(0x1E) //3~9:唯一设备码 将IMEI码转换为16进制 - buffer.Write(_data.body[:7]) + buffer.Write(_data.Body[:7]) //10~14:园区代码 后台根据幼儿园生成的唯一代码 buffer.Write([]byte{10, 11, 12, 13, 14}) //15~18:时间戳 实际当前北京时间的时间戳,转换为16进制 diff --git a/examples/zinx_decoder/bili/router/bili0x13router.go b/examples/zinx_decoder/bili/router/bili0x13router.go index f4a31ccc..c616a0a3 100644 --- a/examples/zinx_decoder/bili/router/bili0x13router.go +++ b/examples/zinx_decoder/bili/router/bili0x13router.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "github.com/aceld/zinx/examples/zinx_decoder/bili/utils" + "github.com/aceld/zinx/examples/zinx_decoder/decode" "github.com/aceld/zinx/ziface" "github.com/aceld/zinx/znet" ) @@ -17,8 +18,8 @@ func (this *Data0x13Router) Handle(request ziface.IRequest) { _response := request.GetResponse() if _response != nil { switch _response.(type) { - case BiliData: - _data := _response.(BiliData) + case decode.HtlvCrcData: + _data := _response.(decode.HtlvCrcData) fmt.Println("Data0x13Router", _data) buffer := pack13(_data) request.GetConnection().Send(buffer) @@ -28,13 +29,13 @@ func (this *Data0x13Router) Handle(request ziface.IRequest) { // 头码 功能码 数据长度 Body CRC // A2 10 0E 0102030405060708091011121314 050B -func pack13(_data BiliData) []byte { +func pack13(_data decode.HtlvCrcData) []byte { buffer := bytes.NewBuffer([]byte{}) buffer.WriteByte(0xA1) - buffer.WriteByte(_data.funcode) + buffer.WriteByte(_data.Funcode) buffer.WriteByte(0x0E) //3~9:3~6:用户卡号 用户IC卡卡号 - buffer.Write(_data.body[:4]) + buffer.Write(_data.Body[:4]) //7:卡状态: 0x00-未绑定(如服务器未查询到该IC卡时) //0x01-已绑定 //0x02-解除绑定(如服务器查询到该IC卡解除绑定时下发) diff --git a/examples/zinx_decoder/bili/router/bili0x14router.go b/examples/zinx_decoder/bili/router/bili0x14router.go index d43f033e..22b1fc70 100644 --- a/examples/zinx_decoder/bili/router/bili0x14router.go +++ b/examples/zinx_decoder/bili/router/bili0x14router.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "github.com/aceld/zinx/examples/zinx_decoder/bili/utils" + "github.com/aceld/zinx/examples/zinx_decoder/decode" "github.com/aceld/zinx/ziface" "github.com/aceld/zinx/znet" ) @@ -17,8 +18,8 @@ func (this *Data0x14Router) Handle(request ziface.IRequest) { _response := request.GetResponse() if _response != nil { switch _response.(type) { - case BiliData: - _data := _response.(BiliData) + case decode.HtlvCrcData: + _data := _response.(decode.HtlvCrcData) fmt.Println("Data0x14Router", _data) buffer := pack14(_data) request.GetConnection().Send(buffer) @@ -28,9 +29,9 @@ func (this *Data0x14Router) Handle(request ziface.IRequest) { // 头码 功能码 数据长度 Body CRC // A2 10 0E 0102030405060708091011121314 050B -func pack14(_data BiliData) []byte { - _data.data[0] = 0xA1 - buffer := bytes.NewBuffer(_data.data[:len(_data.data)-2]) +func pack14(_data decode.HtlvCrcData) []byte { + _data.Data[0] = 0xA1 + buffer := bytes.NewBuffer(_data.Data[:len(_data.Data)-2]) crc := utils.GetCrC(buffer.Bytes()) buffer.Write(crc) return buffer.Bytes() diff --git a/examples/zinx_decoder/bili/router/bili0x15router.go b/examples/zinx_decoder/bili/router/bili0x15router.go index 3fa3a393..4926356f 100644 --- a/examples/zinx_decoder/bili/router/bili0x15router.go +++ b/examples/zinx_decoder/bili/router/bili0x15router.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "github.com/aceld/zinx/examples/zinx_decoder/bili/utils" + "github.com/aceld/zinx/examples/zinx_decoder/decode" "github.com/aceld/zinx/ziface" "github.com/aceld/zinx/znet" ) @@ -17,8 +18,8 @@ func (this *Data0x15Router) Handle(request ziface.IRequest) { _response := request.GetResponse() if _response != nil { switch _response.(type) { - case BiliData: - _data := _response.(BiliData) + case decode.HtlvCrcData: + _data := _response.(decode.HtlvCrcData) fmt.Println("Data0x15Router", _data) buffer := pack15(_data) request.GetConnection().Send(buffer) @@ -28,13 +29,13 @@ func (this *Data0x15Router) Handle(request ziface.IRequest) { // 头码 功能码 数据长度 Body CRC // A2 10 0E 0102030405060708091011121314 050B -func pack15(_data BiliData) []byte { +func pack15(_data decode.HtlvCrcData) []byte { buffer := bytes.NewBuffer([]byte{}) buffer.WriteByte(0xA1) - buffer.WriteByte(_data.funcode) + buffer.WriteByte(_data.Funcode) buffer.WriteByte(0x26) //3~9:设备代码 将IMEI码转换为16进制 - buffer.Write(_data.body[:7]) + buffer.Write(_data.Body[:7]) //10:机型代码 A8(即热式幼儿园机) buffer.WriteByte(0xA8) //11:主机状态1 Bit0:0-待机中,1-运行中 diff --git a/examples/zinx_decoder/bili/router/bili0x16router.go b/examples/zinx_decoder/bili/router/bili0x16router.go index 62f87627..e3d5fda1 100644 --- a/examples/zinx_decoder/bili/router/bili0x16router.go +++ b/examples/zinx_decoder/bili/router/bili0x16router.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "github.com/aceld/zinx/examples/zinx_decoder/bili/utils" + "github.com/aceld/zinx/examples/zinx_decoder/decode" "github.com/aceld/zinx/ziface" "github.com/aceld/zinx/znet" ) @@ -17,8 +18,8 @@ func (this *Data0x16Router) Handle(request ziface.IRequest) { _response := request.GetResponse() if _response != nil { switch _response.(type) { - case BiliData: - _data := _response.(BiliData) + case decode.HtlvCrcData: + _data := _response.(decode.HtlvCrcData) fmt.Println("Data0x16Router", _data) buffer := pack16(_data) request.GetConnection().Send(buffer) @@ -28,9 +29,9 @@ func (this *Data0x16Router) Handle(request ziface.IRequest) { // 头码 功能码 数据长度 Body CRC // A2 10 0E 0102030405060708091011121314 050B -func pack16(_data BiliData) []byte { - _data.data[0] = 0xA1 - buffer := bytes.NewBuffer(_data.data[:len(_data.data)-2]) +func pack16(_data decode.HtlvCrcData) []byte { + _data.Data[0] = 0xA1 + buffer := bytes.NewBuffer(_data.Data[:len(_data.Data)-2]) crc := utils.GetCrC(buffer.Bytes()) buffer.Write(crc) return buffer.Bytes() diff --git a/examples/zinx_decoder/bili/router/biliinterceptor.go b/examples/zinx_decoder/bili/router/biliinterceptor.go deleted file mode 100644 index 7ae135a1..00000000 --- a/examples/zinx_decoder/bili/router/biliinterceptor.go +++ /dev/null @@ -1,83 +0,0 @@ -// HTLV+CRC,H头码,T功能码,L数据长度,V数据内容 -// +------+-------+---------+--------+--------+ -// | 头码 | 功能码 | 数据长度 | 数据内容 | CRC校验 | -// | 1字节 | 1字节 | 1字节 | N字节 | 2字节 | -// +------+-------+---------+--------+--------+ - -// 头码 功能码 数据长度 Body CRC -// A2 10 0E 0102030405060708091011121314 050B -// -// -// 说明: -// 1.数据长度len是14(0E),这里的len仅仅指Body长度; -// -// -// lengthFieldOffset = 2 (len的索引下标是2,下标从0开始) 长度字段的偏差 -// lengthFieldLength = 1 (len是1个byte) 长度字段占的字节数 -// lengthAdjustment = 2 (len只表示Body长度,程序只会读取len个字节就结束,但是CRC还有2byte没读呢,所以为2) -// initialBytesToStrip = 0 (这个0表示完整的协议内容,如果不想要A2,那么这里就是1) 从解码帧中第一次去除的字节数 -// maxFrameLength = 255 + 4(起始码、功能码、CRC) (len是1个byte,所以最大长度是无符号1个byte的最大值) - -package router - -import ( - "encoding/hex" - "fmt" - "github.com/aceld/zinx/examples/zinx_decoder/bili/utils" - "github.com/aceld/zinx/zcode" - "github.com/aceld/zinx/ziface" - "github.com/aceld/zinx/zlog" - "math" -) - -const HEADER_SIZE = 5 - -type BiliData struct { - data []byte //数据内容 - head byte //头码 - funcode byte //功能码 - length byte //数据长度 - body []byte //数据内容 - crc []byte //CRC校验 -} - -type HtlvcrcInterceptor struct { -} - -func (this *HtlvcrcInterceptor) GetDecoder() ziface.Interceptor { - return zcode.NewLengthFieldFrameInterceptor(math.MaxUint8+4, 2, 1, 2, 0) -} - -func (this *HtlvcrcInterceptor) Intercept(chain ziface.Chain) ziface.Response { - request := chain.Request() - if request != nil { - switch request.(type) { - case ziface.IRequest: - iRequest := request.(ziface.IRequest) - iMessage := iRequest.GetMessage() - if iMessage != nil { - data := iMessage.GetData() - zlog.Ins().DebugF("HtlvcrcInterceptor %s \n", hex.EncodeToString(data)) - datasize := len(data) - htlvData := BiliData{ - data: data, - } - if datasize >= HEADER_SIZE { - htlvData.head = data[0] - htlvData.funcode = data[1] - htlvData.length = data[2] - htlvData.body = data[3 : datasize-2] - htlvData.crc = data[datasize-2 : datasize] - if !utils.CheckCRC(data[:datasize-2], htlvData.crc) { - fmt.Println("crc校验失败", hex.EncodeToString(data), hex.EncodeToString(htlvData.crc)) - return nil - } - iMessage.SetMsgID(uint32(htlvData.funcode)) - iRequest.SetResponse(htlvData) - //zlog.Ins().DebugF("2htlvData %s \n", hex.EncodeToString(htlvData.data)) - } - } - } - } - return chain.Proceed(chain.Request()) -} diff --git a/examples/zinx_decoder/decode/htlvcrcdecoder.go b/examples/zinx_decoder/decode/htlvcrcdecoder.go index 45480809..f6af5779 100644 --- a/examples/zinx_decoder/decode/htlvcrcdecoder.go +++ b/examples/zinx_decoder/decode/htlvcrcdecoder.go @@ -21,18 +21,21 @@ package decode import ( + "encoding/hex" "fmt" + "github.com/aceld/zinx/examples/zinx_decoder/bili/utils" "github.com/aceld/zinx/ziface" ) const HEADER_SIZE = 5 type HtlvCrcData struct { - head byte //头码 - funcode byte //功能码 - length byte //数据长度 - data []byte //数据内容 - crc []byte //CRC校验 + Data []byte //数据内容 + Head byte //头码 + Funcode byte //功能码 + Length byte //数据长度 + Body []byte //数据内容 + Crc []byte //CRC校验 } type HtlvCrcDecoder struct { @@ -49,16 +52,22 @@ func (this *HtlvCrcDecoder) Intercept(chain ziface.Chain) ziface.Response { data := iMessage.GetData() fmt.Println("1htlvData", data) datasize := len(data) - htlvData := HtlvCrcData{} + htlvData := HtlvCrcData{ + Data: data, + } if datasize >= HEADER_SIZE { - htlvData.head = data[0] - htlvData.funcode = data[1] - htlvData.length = data[2] - htlvData.data = data[3 : 3+htlvData.length] - htlvData.crc = data[htlvData.length+3 : datasize] - iMessage.SetMsgID(uint32(htlvData.funcode)) //funcode作为msgID + htlvData.Head = data[0] + htlvData.Funcode = data[1] + htlvData.Length = data[2] + htlvData.Body = data[3 : datasize-2] + htlvData.Crc = data[datasize-2 : datasize] + if !utils.CheckCRC(data[:datasize-2], htlvData.Crc) { + fmt.Println("crc校验失败", hex.EncodeToString(data), hex.EncodeToString(htlvData.Crc)) + return nil + } + iMessage.SetMsgID(uint32(htlvData.Funcode)) iRequest.SetResponse(htlvData) - fmt.Println("2htlvData", htlvData) + //zlog.Ins().DebugF("2htlvData %s \n", hex.EncodeToString(htlvData.data)) } } }