2024-11-03 15:46:14 +00:00
|
|
|
package vbantxt
|
2022-11-05 18:58:36 +00:00
|
|
|
|
2024-11-03 15:46:14 +00:00
|
|
|
import (
|
2024-11-07 19:34:44 +00:00
|
|
|
"bytes"
|
2024-11-03 15:46:14 +00:00
|
|
|
"encoding/binary"
|
2022-11-05 18:58:36 +00:00
|
|
|
|
2024-11-03 15:46:14 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
2022-11-05 18:58:36 +00:00
|
|
|
|
2024-11-03 15:46:14 +00:00
|
|
|
const (
|
|
|
|
vbanProtocolTxt = 0x40
|
|
|
|
streamNameSz = 16
|
|
|
|
headerSz = 4 + 1 + 1 + 1 + 1 + 16 + 4
|
|
|
|
)
|
|
|
|
|
|
|
|
var BpsOpts = []int{0, 110, 150, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 31250,
|
|
|
|
38400, 57600, 115200, 128000, 230400, 250000, 256000, 460800, 921600,
|
|
|
|
1000000, 1500000, 2000000, 3000000}
|
|
|
|
|
|
|
|
type packet struct {
|
2024-11-07 19:34:44 +00:00
|
|
|
streamname []byte
|
2022-11-05 18:58:36 +00:00
|
|
|
bpsIndex int
|
|
|
|
channel int
|
|
|
|
framecounter []byte
|
2024-11-07 19:36:56 +00:00
|
|
|
hbuf *bytes.Buffer
|
2022-11-05 18:58:36 +00:00
|
|
|
}
|
|
|
|
|
2024-11-03 15:46:14 +00:00
|
|
|
// newPacket returns a packet struct with default values, framecounter at 0.
|
|
|
|
func newPacket(streamname string) packet {
|
2024-11-07 19:34:44 +00:00
|
|
|
streamnameBuf := make([]byte, streamNameSz)
|
|
|
|
copy(streamnameBuf, streamname)
|
|
|
|
|
2024-11-03 15:46:14 +00:00
|
|
|
return packet{
|
2024-11-07 19:34:44 +00:00
|
|
|
streamname: streamnameBuf,
|
2024-11-03 15:46:14 +00:00
|
|
|
bpsIndex: 0,
|
|
|
|
channel: 0,
|
|
|
|
framecounter: make([]byte, 4),
|
2024-11-07 19:36:56 +00:00
|
|
|
hbuf: bytes.NewBuffer(make([]byte, headerSz)),
|
2022-11-05 18:58:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sr defines the samplerate for the request
|
2024-11-03 15:46:14 +00:00
|
|
|
func (p *packet) sr() byte {
|
|
|
|
return byte(vbanProtocolTxt + p.bpsIndex)
|
2022-11-05 18:58:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// nbc defines the channel of the request
|
2024-11-03 15:46:14 +00:00
|
|
|
func (p *packet) nbc() byte {
|
|
|
|
return byte(p.channel)
|
2022-11-05 18:58:36 +00:00
|
|
|
}
|
|
|
|
|
2024-11-03 15:46:14 +00:00
|
|
|
// header returns a fully formed packet header
|
|
|
|
func (p *packet) header() []byte {
|
2024-11-07 19:34:44 +00:00
|
|
|
p.hbuf.Reset()
|
|
|
|
p.hbuf.WriteString("VBAN")
|
|
|
|
p.hbuf.WriteByte(p.sr())
|
|
|
|
p.hbuf.WriteByte(byte(0))
|
|
|
|
p.hbuf.WriteByte(p.nbc())
|
|
|
|
p.hbuf.WriteByte(byte(0x10))
|
|
|
|
p.hbuf.Write(p.streamname)
|
|
|
|
p.hbuf.Write(p.framecounter)
|
|
|
|
return p.hbuf.Bytes()
|
2022-11-05 18:58:36 +00:00
|
|
|
}
|
2024-11-03 15:46:14 +00:00
|
|
|
|
|
|
|
// bumpFrameCounter increments the frame counter by 1
|
|
|
|
func (p *packet) bumpFrameCounter() {
|
|
|
|
x := binary.LittleEndian.Uint32(p.framecounter)
|
|
|
|
binary.LittleEndian.PutUint32(p.framecounter, x+1)
|
|
|
|
|
|
|
|
log.Tracef("framecounter: %d", x)
|
|
|
|
}
|