1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
|
package wsjtx
import (
"encoding/binary"
"errors"
"fmt"
"math"
"reflect"
"time"
"github.com/leemcloughlin/jdn"
)
type parser struct {
buffer []byte
length int
cursor int
}
var ParseError = errors.New("parse error")
var notEnoughBytes = fmt.Errorf("%w: fewer bytes than expected, maybe an older version of WSJTX", ParseError)
// Parse messages following the interface laid out in
// https://sourceforge.net/p/wsjt/wsjtx/ci/master/tree/Network/NetworkMessage.hpp. This only parses
// "Out" or "In/Out" message types and does not include "In" types because they will never be
// received by WSJT-X.
func parseMessage(buffer []byte, length int) (interface{}, error) {
p := parser{buffer: buffer, length: length, cursor: 0}
m, err := p.parseUint32()
if err != nil {
return nil, ParseError
}
if m != magic {
return nil, fmt.Errorf("%w: packet is not speaking the WSJT-X protocol", ParseError)
}
sch, err := p.parseUint32()
if err != nil {
return nil, ParseError
}
if sch != schema {
return nil, fmt.Errorf("%w: got a schema version I wasn't expecting: %d", ParseError, sch)
}
messageType, _ := p.parseUint32()
switch messageType {
case heartbeatNum:
heartbeat, err := p.parseHeartbeat()
if err != nil {
return heartbeat, err
}
err = p.checkParse(heartbeat)
return heartbeat, err
case statusNum:
status, err := p.parseStatus()
if err != nil {
return status, err
}
err = p.checkParse(status)
return status, err
case decodeNum:
decode, err := p.parseDecode()
if err != nil {
return decode, err
}
err = p.checkParse(decode)
return decode, err
case clearNum:
clear, err := p.parseClear()
if err != nil {
return clear, err
}
err = p.checkParse(clear)
return clear, err
case qsoLoggedNum:
qsoLogged, err := p.parseQsoLogged()
if err != nil {
return qsoLogged, err
}
err = p.checkParse(qsoLogged)
return qsoLogged, err
case closeNum:
closeMsg, err := p.parseClose()
if err != nil {
return closeMsg, err
}
err = p.checkParse(closeMsg)
return closeMsg, err
case wsprDecodeNum:
wspr, err := p.parseWsprDecode()
if err != nil {
return wspr, err
}
err = p.checkParse(wspr)
return wspr, err
case loggedAdifNum:
loggedAdif, err := p.parseLoggedAdif()
if err != nil {
return loggedAdif, err
}
err = p.checkParse(loggedAdif)
return loggedAdif, err
}
return nil, fmt.Errorf("%w: unknown message type %d", ParseError, messageType)
}
// Quick sanity check that we parsed all of the message bytes
func (p *parser) checkParse(message interface{}) error {
if p.cursor != p.length {
return fmt.Errorf("%w %s: there were %d bytes left over",
ParseError, reflect.TypeOf(message).Name(), p.length-p.cursor)
}
return nil
}
func (p *parser) parseHeartbeat() (HeartbeatMessage, error) {
var err error
heartbeatMessage := HeartbeatMessage{}
heartbeatMessage.Id, err = p.parseUtf8()
heartbeatMessage.MaxSchema, err = p.parseUint32()
heartbeatMessage.Version, err = p.parseUtf8()
heartbeatMessage.Revision, err = p.parseUtf8()
return heartbeatMessage, err
}
func (p *parser) parseStatus() (StatusMessage, error) {
var err error
statusMessage := StatusMessage{}
statusMessage.Id, err = p.parseUtf8()
statusMessage.DialFrequency, err = p.parseUint64()
statusMessage.Mode, err = p.parseUtf8()
statusMessage.DxCall, err = p.parseUtf8()
statusMessage.Report, err = p.parseUtf8()
statusMessage.TxMode, err = p.parseUtf8()
statusMessage.TxEnabled, err = p.parseBool()
statusMessage.Transmitting, err = p.parseBool()
statusMessage.Decoding, err = p.parseBool()
statusMessage.RxDF, err = p.parseUint32()
statusMessage.TxDF, err = p.parseUint32()
statusMessage.DeCall, err = p.parseUtf8()
statusMessage.DeGrid, err = p.parseUtf8()
statusMessage.DxGrid, err = p.parseUtf8()
statusMessage.TxWatchdog, err = p.parseBool()
statusMessage.SubMode, err = p.parseUtf8()
statusMessage.FastMode, err = p.parseBool()
statusMessage.SpecialOperationMode, err = p.parseUint8()
statusMessage.FrequencyTolerance, err = p.parseUint32()
statusMessage.TRPeriod, err = p.parseUint32()
statusMessage.ConfigurationName, err = p.parseUtf8()
statusMessage.TxMessage, err = p.parseUtf8()
return statusMessage, err
}
func (p *parser) parseDecode() (DecodeMessage, error) {
var err error
decodeMessage := DecodeMessage{}
decodeMessage.Id, err = p.parseUtf8()
decodeMessage.New, err = p.parseBool()
decodeMessage.Time, err = p.parseUint32()
decodeMessage.Snr, err = p.parseInt32()
decodeMessage.DeltaTimeSec, err = p.parseFloat64()
decodeMessage.DeltaFrequencyHz, err = p.parseUint32()
decodeMessage.Mode, err = p.parseUtf8()
decodeMessage.Message, err = p.parseUtf8()
decodeMessage.LowConfidence, err = p.parseBool()
decodeMessage.OffAir, err = p.parseBool()
return decodeMessage, err
}
func (p *parser) parseClear() (ClearMessage, error) {
var err error
clearMessage := ClearMessage{}
clearMessage.Id, err = p.parseUtf8()
return clearMessage, err
}
func (p *parser) parseQsoLogged() (QsoLoggedMessage, error) {
var err error
qsoLoggedMessage := QsoLoggedMessage{}
qsoLoggedMessage.Id, err = p.parseUtf8()
qsoLoggedMessage.DateTimeOff, err = p.parseQDateTime()
qsoLoggedMessage.DxCall, err = p.parseUtf8()
qsoLoggedMessage.DxGrid, err = p.parseUtf8()
qsoLoggedMessage.TxFrequency, err = p.parseUint64()
qsoLoggedMessage.Mode, err = p.parseUtf8()
qsoLoggedMessage.ReportSent, err = p.parseUtf8()
qsoLoggedMessage.ReportReceived, err = p.parseUtf8()
qsoLoggedMessage.TxPower, err = p.parseUtf8()
qsoLoggedMessage.Comments, err = p.parseUtf8()
qsoLoggedMessage.Name, err = p.parseUtf8()
qsoLoggedMessage.DateTimeOn, err = p.parseQDateTime()
qsoLoggedMessage.OperatorCall, err = p.parseUtf8()
qsoLoggedMessage.MyCall, err = p.parseUtf8()
qsoLoggedMessage.MyGrid, err = p.parseUtf8()
qsoLoggedMessage.ExchangeSent, err = p.parseUtf8()
qsoLoggedMessage.ExchangeReceived, err = p.parseUtf8()
qsoLoggedMessage.ADIFPropagationMode, err = p.parseUtf8()
return qsoLoggedMessage, err
}
func (p *parser) parseClose() (CloseMessage, error) {
var err error
closeMessage := CloseMessage{}
closeMessage.Id, err = p.parseUtf8()
return closeMessage, err
}
func (p *parser) parseWsprDecode() (WSPRDecodeMessage, error) {
var err error
wsprDecodeMessage := WSPRDecodeMessage{}
wsprDecodeMessage.Id, err = p.parseUtf8()
wsprDecodeMessage.New, err = p.parseBool()
wsprDecodeMessage.Time, err = p.parseUint32()
wsprDecodeMessage.Snr, err = p.parseInt32()
wsprDecodeMessage.DeltaTime, err = p.parseFloat64()
wsprDecodeMessage.Frequency, err = p.parseUint64()
wsprDecodeMessage.Drift, err = p.parseInt32()
wsprDecodeMessage.Callsign, err = p.parseUtf8()
wsprDecodeMessage.Grid, err = p.parseUtf8()
wsprDecodeMessage.Power, err = p.parseInt32()
wsprDecodeMessage.OffAir, err = p.parseBool()
return wsprDecodeMessage, err
}
func (p *parser) parseLoggedAdif() (LoggedAdifMessage, error) {
var err error
loggedAdifMessage := LoggedAdifMessage{}
loggedAdifMessage.Id, err = p.parseUtf8()
loggedAdifMessage.Adif, err = p.parseUtf8()
return loggedAdifMessage, err
}
func (p *parser) parseUint8() (uint8, error) {
if len(p.buffer) < p.cursor {
return 0, notEnoughBytes
}
value := p.buffer[p.cursor]
p.cursor += 1
return value, nil
}
func (p *parser) parseUtf8() (string, error) {
strlen, err := p.parseUint32()
if err != nil {
return "", err
}
if strlen == uint32(qDataStreamNull) {
// this is a sentinel value meaning "null" in QDataStream, but Golang can't have nil strings
strlen = 0
}
end := p.cursor + int(strlen)
value := string(p.buffer[p.cursor:end])
p.cursor += int(strlen)
return value, nil
}
func (p *parser) parseUint32() (uint32, error) {
end := p.cursor + 4
if len(p.buffer) < end {
return 0, notEnoughBytes
}
value := binary.BigEndian.Uint32(p.buffer[p.cursor:end])
p.cursor += 4
return value, nil
}
func (p *parser) parseInt32() (int32, error) {
end := p.cursor + 4
if len(p.buffer) < end {
return 0, notEnoughBytes
}
value := int32(binary.BigEndian.Uint32(p.buffer[p.cursor:end]))
p.cursor += 4
return value, nil
}
func (p *parser) parseUint64() (uint64, error) {
end := p.cursor + 8
if len(p.buffer) < end {
return 0, notEnoughBytes
}
value := binary.BigEndian.Uint64(p.buffer[p.cursor:end])
p.cursor += 8
return value, nil
}
func (p *parser) parseFloat64() (float64, error) {
end := p.cursor + 8
if len(p.buffer) < end {
return 0, notEnoughBytes
}
bits := binary.BigEndian.Uint64(p.buffer[p.cursor:end])
value := math.Float64frombits(bits)
p.cursor += 8
return value, nil
}
func (p *parser) parseBool() (bool, error) {
if len(p.buffer) < p.cursor {
return false, notEnoughBytes
}
value := p.buffer[p.cursor] != 0
p.cursor += 1
return value, nil
}
func (p *parser) parseQDateTime() (time.Time, error) {
julianDay, err := p.parseUint64()
year, month, day := jdn.FromNumber(int(julianDay))
msMid, err := p.parseUint32()
msSinceMidnight := int(msMid)
hour := msSinceMidnight / 3600000
msSinceMidnight -= hour * 3600000
minute := msSinceMidnight / 60000
msSinceMidnight -= minute * 60000
second := msSinceMidnight / 1000
timespec, err := p.parseUint8()
var value time.Time
switch timespec {
case 0:
// local
value = time.Date(year, month, day, hour, minute, second, 0, time.Local)
case 1:
// UTC
value = time.Date(year, month, day, hour, minute, second, 0, time.UTC)
default:
return value, fmt.Errorf("got a timespec I wasn't expecting: %d", timespec)
}
return value, err
}
|