2021-12-16 19:45:32 -05:00
|
|
|
package oscar
|
2021-11-16 16:44:29 -05:00
|
|
|
|
|
|
|
import (
|
2021-12-17 19:01:22 -05:00
|
|
|
"aim-oscar/util"
|
2021-11-16 16:44:29 -05:00
|
|
|
"bytes"
|
|
|
|
"encoding"
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ encoding.BinaryUnmarshaler = &FLAP{}
|
|
|
|
var _ encoding.BinaryMarshaler = &FLAP{}
|
|
|
|
|
|
|
|
type FLAPHeader struct {
|
|
|
|
Channel uint8
|
|
|
|
SequenceNumber uint16
|
|
|
|
DataLength uint16
|
|
|
|
}
|
|
|
|
|
|
|
|
type FLAP struct {
|
|
|
|
Header FLAPHeader
|
2021-12-16 17:41:17 -05:00
|
|
|
Data Buffer
|
2021-11-16 16:44:29 -05:00
|
|
|
}
|
|
|
|
|
2021-12-16 18:37:27 -05:00
|
|
|
func NewFLAP(channel uint8) *FLAP {
|
2021-11-16 16:44:29 -05:00
|
|
|
return &FLAP{
|
|
|
|
Header: FLAPHeader{
|
|
|
|
Channel: channel,
|
2021-12-16 18:37:27 -05:00
|
|
|
SequenceNumber: 0,
|
|
|
|
DataLength: 0,
|
2021-11-16 16:44:29 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-15 19:55:43 -05:00
|
|
|
func (f *FLAP) MarshalBinary() ([]byte, error) {
|
2021-12-16 17:41:17 -05:00
|
|
|
buf := Buffer{}
|
|
|
|
buf.WriteUint8(0x2a)
|
|
|
|
|
|
|
|
f.Header.DataLength = uint16(len(f.Data.Bytes()))
|
|
|
|
|
2021-12-15 19:55:43 -05:00
|
|
|
binary.Write(&buf, binary.BigEndian, f.Header)
|
2021-12-16 17:41:17 -05:00
|
|
|
n, err := buf.Write(f.Data.Bytes())
|
2021-12-15 19:55:43 -05:00
|
|
|
if n != int(f.Header.DataLength) {
|
|
|
|
return nil, fmt.Errorf("needed to write %d bytes to buffer but wrote %d", f.Header.DataLength, n)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2021-11-16 16:44:29 -05:00
|
|
|
func (f *FLAP) UnmarshalBinary(data []byte) error {
|
|
|
|
buf := bytes.NewBuffer(data)
|
|
|
|
start, err := buf.ReadByte()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if start != 0x2a {
|
|
|
|
return fmt.Errorf("FLAP missing 0x2a header")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = binary.Read(buf, binary.BigEndian, &f.Header); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-16 17:41:17 -05:00
|
|
|
f.Data.Write(buf.Bytes())
|
2021-11-16 16:44:29 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-15 19:55:43 -05:00
|
|
|
func (f *FLAP) Len() int {
|
|
|
|
return 6 + int(f.Header.DataLength)
|
|
|
|
}
|
2021-11-16 16:44:29 -05:00
|
|
|
|
2021-12-15 19:55:43 -05:00
|
|
|
func (f *FLAP) String() string {
|
2021-12-17 19:01:22 -05:00
|
|
|
return fmt.Sprintf("FLAP(CH:%d, SEQ:%d):\n%s", f.Header.Channel, f.Header.SequenceNumber, util.PrettyBytes(f.Data.Bytes()))
|
2021-11-16 16:44:29 -05:00
|
|
|
}
|