mirror of
https://github.com/onyx-and-iris/vbantxt.git
synced 2026-04-07 01:33:30 +00:00
reorganise some of the internals of the package.
functional options added.
This commit is contained in:
70
packet.go
70
packet.go
@@ -1,50 +1,72 @@
|
||||
package main
|
||||
package vbantxt
|
||||
|
||||
var r *requestHeader
|
||||
import (
|
||||
"encoding/binary"
|
||||
|
||||
const VBAN_PROTOCOL_TXT = 0x40
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// requestHeader represents a single request header
|
||||
type requestHeader struct {
|
||||
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 {
|
||||
name string
|
||||
bpsIndex int
|
||||
channel int
|
||||
framecounter []byte
|
||||
}
|
||||
|
||||
// newRequestHeader returns a pointer to a requestHeader struct as a singleton
|
||||
func newRequestHeader(streamname string, bpsI, channel int) *requestHeader {
|
||||
if r != nil {
|
||||
return r
|
||||
// newPacket returns a packet struct with default values, framecounter at 0.
|
||||
func newPacket(streamname string) packet {
|
||||
return packet{
|
||||
name: streamname,
|
||||
bpsIndex: 0,
|
||||
channel: 0,
|
||||
framecounter: make([]byte, 4),
|
||||
}
|
||||
return &requestHeader{streamname, bpsI, channel, make([]byte, 4)}
|
||||
}
|
||||
|
||||
// sr defines the samplerate for the request
|
||||
func (r *requestHeader) sr() byte {
|
||||
return byte(VBAN_PROTOCOL_TXT + r.bpsIndex)
|
||||
func (p *packet) sr() byte {
|
||||
return byte(vbanProtocolTxt + p.bpsIndex)
|
||||
}
|
||||
|
||||
// nbc defines the channel of the request
|
||||
func (r *requestHeader) nbc() byte {
|
||||
return byte(r.channel)
|
||||
func (p *packet) nbc() byte {
|
||||
return byte(p.channel)
|
||||
}
|
||||
|
||||
// streamname defines the stream name of the text request
|
||||
func (r *requestHeader) streamname() []byte {
|
||||
b := make([]byte, 16)
|
||||
copy(b, r.name)
|
||||
func (p *packet) streamname() []byte {
|
||||
b := make([]byte, streamNameSz)
|
||||
copy(b, p.name)
|
||||
return b
|
||||
}
|
||||
|
||||
// header returns a fully formed text request packet header
|
||||
func (t *requestHeader) header() []byte {
|
||||
h := []byte("VBAN")
|
||||
h = append(h, t.sr())
|
||||
// header returns a fully formed packet header
|
||||
func (p *packet) header() []byte {
|
||||
h := make([]byte, 0, headerSz)
|
||||
h = append(h, []byte("VBAN")...)
|
||||
h = append(h, p.sr())
|
||||
h = append(h, byte(0))
|
||||
h = append(h, t.nbc())
|
||||
h = append(h, p.nbc())
|
||||
h = append(h, byte(0x10))
|
||||
h = append(h, t.streamname()...)
|
||||
h = append(h, t.framecounter...)
|
||||
h = append(h, p.streamname()...)
|
||||
h = append(h, p.framecounter...)
|
||||
return h
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user