54 lines
1.0 KiB
Go
54 lines
1.0 KiB
Go
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 */
|
|
if f.Metadata == METADATA {
|
|
return errors.New("frame is already initialized")
|
|
}
|
|
f.Metadata = int8(buff[23])
|
|
if f.Metadata == METADATA {
|
|
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
|
|
}
|