| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- package zigbee
- import (
- "encoding/binary"
- "github.com/valyala/bytebufferpool"
- )
- type PackSendDataAck struct {
- M1Flag uint16 //固定为F1F1
- M1Leng uint16 //包体1长度
- M1Cmd uint8 //1字节
- Result uint8 //0接收成功,1接收失败,2地址编码错误
- M1Crc uint16 //整包CRC
- }
- func (o *PackSendDataAck) SetData(result uint8) {
- o.Result = result
- }
- func (o *PackSendDataAck) EnCode() (*bytebufferpool.ByteBuffer, error) {
- buff := bytebufferpool.Get()
- buff.Write([]byte{0xF1, 0xF1}) //M1Flag uint16 //固定为F1F1
- tmp := make([]byte, 2)
- binary.BigEndian.PutUint16(tmp, 1)
- buff.Write(tmp) //M1Leng uint16 //包体1长度
- buff.WriteByte(CmdSendDataAck) //M1Cmd uint8 //1字节
- buff.WriteByte(o.Result)
- crc16 := CRC16(buff.B)
- tmp2 := make([]byte, 2)
- binary.BigEndian.PutUint16(tmp2, crc16)
- buff.Write(tmp2) //外层CRC16
- return buff, nil
- }
- func (o *PackSendDataAck) DeCode(Data []byte) error {
- if len(Data) < 7 {
- return ErrorProtocol
- }
- if Data[0] != 0xF1 || Data[1] != 0xF1 {
- return ErrorProtocol
- }
- o.M1Flag = binary.BigEndian.Uint16(Data[0:2])
- o.M1Leng = binary.BigEndian.Uint16(Data[2:4])
- o.M1Cmd = Data[4]
- o.M1Crc = binary.BigEndian.Uint16(Data[o.M1Leng+5 : o.M1Leng+7])
- //校验CRC1
- crc := CRC16(Data[0 : o.M1Leng+5])
- if o.M1Crc != crc {
- return ErrorInvalidCrc1
- }
- return nil
- }
|