Skip to content

Commit

Permalink
Parsers register themselves like in database/sql.
Browse files Browse the repository at this point in the history
  • Loading branch information
bemasher committed Jan 2, 2016
1 parent 206c0dc commit 99be3e7
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 22 deletions.
6 changes: 5 additions & 1 deletion idm/idm.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ import (
"github.com/bemasher/rtlamr/parse"
)

func init() {
parse.Register("idm", NewParser)
}

func NewPacketConfig(symbolLength int) (cfg decode.PacketConfig) {
cfg.CenterFreq = 912600155
cfg.DataRate = 32768
Expand All @@ -51,7 +55,7 @@ func (p *Parser) Cfg() *decode.PacketConfig {
return &p.Decoder.Cfg
}

func NewParser(symbolLength, decimation int) (p *Parser) {
func NewParser(symbolLength, decimation int) (p parse.Parser) {
return &Parser{
decode.NewDecoder(NewPacketConfig(symbolLength), decimation),
crc.NewCRC("CCITT", 0xFFFF, 0x1021, 0x1D0F),
Expand Down
32 changes: 32 additions & 0 deletions parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package parse
import (
"fmt"
"strconv"
"sync"
"time"

"github.com/bemasher/rtlamr/decode"
Expand All @@ -14,6 +15,37 @@ const (
TimeFormat = "2006-01-02T15:04:05.000"
)

var (
parserMutex sync.Mutex
parsers = make(map[string]NewParserFunc)
)

type NewParserFunc func(symbolLength, decimation int) Parser

func Register(name string, parserFn NewParserFunc) {
parserMutex.Lock()
defer parserMutex.Unlock()

if parserFn == nil {
panic("parser: new parser func is nil")
}
if _, dup := parsers[name]; dup {
panic(fmt.Sprintf("parser: parser already registered (%s)", name))
}
parsers[name] = parserFn
}

func NewParser(name string, symbolLength, decimation int) (Parser, error) {
parserMutex.Lock()
defer parserMutex.Unlock()

if parserFn, exists := parsers[name]; exists {
return parserFn(symbolLength, decimation), nil
} else {
return nil, fmt.Errorf("invalid message type: %q\n", name)
}
}

type Data struct {
Bits string
Bytes []byte
Expand Down
10 changes: 7 additions & 3 deletions r900/r900.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ const (
PayloadSymbols = 42
)

func init() {
parse.Register("r900", NewParser)
}

func NewPacketConfig(symbolLength int) (cfg decode.PacketConfig) {
cfg.CenterFreq = 912380000
cfg.DataRate = 32768
Expand All @@ -52,8 +56,8 @@ type Parser struct {
quantized []byte
}

func NewParser(symbolLength, decimation int) (p *Parser) {
p = new(Parser)
func NewParser(symbolLength, decimation int) parse.Parser {
p := new(Parser)

p.Decoder = decode.NewDecoder(NewPacketConfig(symbolLength), decimation)

Expand All @@ -64,7 +68,7 @@ func NewParser(symbolLength, decimation int) (p *Parser) {
p.filtered = make([][3]float64, p.Decoder.DecCfg.BufferLength)
p.quantized = make([]byte, p.Decoder.DecCfg.BufferLength)

return
return p
}

func (p Parser) Dec() decode.Decoder {
Expand Down
24 changes: 8 additions & 16 deletions recv.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ import (
"os"
"os/signal"
"runtime/pprof"
"strings"
"time"

"github.com/bemasher/rtlamr/idm"
"github.com/bemasher/rtlamr/parse"
"github.com/bemasher/rtlamr/r900"
"github.com/bemasher/rtlamr/scm"
"github.com/bemasher/rtlamr/scmplus"
"github.com/bemasher/rtltcp"

_ "github.com/bemasher/rtlamr/idm"
_ "github.com/bemasher/rtlamr/r900"
_ "github.com/bemasher/rtlamr/scm"
_ "github.com/bemasher/rtlamr/scmplus"
)

var rcvr Receiver
Expand All @@ -45,17 +45,9 @@ type Receiver struct {
}

func (rcvr *Receiver) NewReceiver() {
switch strings.ToLower(*msgType) {
case "scm":
rcvr.p = scm.NewParser(*symbolLength, *decimation)
case "idm":
rcvr.p = idm.NewParser(*symbolLength, *decimation)
case "r900":
rcvr.p = r900.NewParser(*symbolLength, *decimation)
case "scm+":
rcvr.p = scmplus.NewParser(*symbolLength, *decimation)
default:
log.Fatalf("Invalid message type: %q\n", *msgType)
var err error
if rcvr.p, err = parse.NewParser(*msgType, *symbolLength, *decimation); err != nil {
log.Fatal(err)
}

// Connect to rtl_tcp server.
Expand Down
6 changes: 5 additions & 1 deletion scm/scm.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ import (
"github.com/bemasher/rtlamr/parse"
)

func init() {
parse.Register("scm", NewParser)
}

func NewPacketConfig(symbolLength int) (cfg decode.PacketConfig) {
cfg.CenterFreq = 912600155
cfg.DataRate = 32768
Expand All @@ -42,7 +46,7 @@ type Parser struct {
crc.CRC
}

func NewParser(symbolLength, decimation int) (p *Parser) {
func NewParser(symbolLength, decimation int) (p parse.Parser) {
return &Parser{
decode.NewDecoder(NewPacketConfig(symbolLength), decimation),
crc.NewCRC("BCH", 0, 0x6F63, 0),
Expand Down
6 changes: 5 additions & 1 deletion scmplus/scmplus.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ import (
"github.com/bemasher/rtlamr/parse"
)

func init() {
parse.Register("scm+", NewParser)
}

func NewPacketConfig(symbolLength int) (cfg decode.PacketConfig) {
cfg.CenterFreq = 912600155
cfg.DataRate = 32768
Expand All @@ -51,7 +55,7 @@ func (p *Parser) Cfg() *decode.PacketConfig {
return &p.Decoder.Cfg
}

func NewParser(symbolLength, decimation int) (p *Parser) {
func NewParser(symbolLength, decimation int) (p parse.Parser) {
return &Parser{
decode.NewDecoder(NewPacketConfig(symbolLength), decimation),
crc.NewCRC("CCITT", 0xFFFF, 0x1021, 0x1D0F),
Expand Down

0 comments on commit 99be3e7

Please sign in to comment.