usrpserv/usrp/proto.go

54 lines
1.0 KiB
Go
Raw Normal View History

2024-05-24 23:07:40 +01:00
package usrp
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"time"
)
func NewFrame() Frame {
var frame Frame
frame.Time = time.Now()
return frame
}
func (f *Frame) Init(buff []byte, n int) error {
if err := IsValid(buff); err != nil {
return err
}
/* Can't initialize existing frame */
2024-05-25 16:20:53 +01:00
if f.Metadata == METADATA {
2024-05-24 23:07:40 +01:00
return errors.New("frame is already initialized")
}
f.Metadata = int8(buff[23])
2024-05-25 16:20:53 +01:00
if f.Metadata == METADATA {
2024-05-24 23:07:40 +01:00
fmt.Println("meta data")
callLen := buff[33]
f.Callsign = string(buff[46 : 46+callLen])
}
f.Seq = binary.LittleEndian.Uint32(buff[4:9])
return nil
}
func IsValid(buff []byte) error {
if bytes.Compare(buff[0:4], []byte{'U', 'S', 'R', 'P'}) != 0 {
return errors.New("not a valid USRP packet")
}
return nil
}
func (f *Frame) Parse(buff []byte, n int) error {
if err := IsValid(buff); err != nil {
return err
}
f.Seq = binary.BigEndian.Uint32(buff[4:9])
if len(buff[32:n]) > 320 {
return errors.New("too many bytes")
}
f.PTT = int8(buff[15])
copy(f.PCM.Data[:], buff[32:])
return nil
}