Skip to content

Commit 844aab3

Browse files
committed
Avoid (un)marshal from/to PHYPayload.
1 parent ac78236 commit 844aab3

File tree

14 files changed

+134
-367
lines changed

14 files changed

+134
-367
lines changed

backend/mqttpubsub/backend.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@ import (
77
"time"
88

99
log "github.com/Sirupsen/logrus"
10-
"github.com/brocaar/loraserver/models"
10+
"github.com/brocaar/loraserver/api/gw"
1111
"github.com/brocaar/lorawan"
1212
"github.com/eclipse/paho.mqtt.golang"
1313
)
1414

1515
// Backend implements a MQTT pub-sub backend.
1616
type Backend struct {
1717
conn mqtt.Client
18-
txPacketChan chan models.TXPacket
18+
txPacketChan chan gw.TXPacketBytes
1919
gateways map[lorawan.EUI64]struct{}
2020
mutex sync.RWMutex
2121
}
2222

2323
// NewBackend creates a new Backend.
2424
func NewBackend(server, username, password string) (*Backend, error) {
2525
b := Backend{
26-
txPacketChan: make(chan models.TXPacket),
26+
txPacketChan: make(chan gw.TXPacketBytes),
2727
gateways: make(map[lorawan.EUI64]struct{}),
2828
}
2929

@@ -48,12 +48,12 @@ func (b *Backend) Close() {
4848
b.conn.Disconnect(250) // wait 250 milisec to complete pending actions
4949
}
5050

51-
// TXPacketChan returns the TXPacket channel.
52-
func (b *Backend) TXPacketChan() chan models.TXPacket {
51+
// TXPacketChan returns the TXPacketBytes channel.
52+
func (b *Backend) TXPacketChan() chan gw.TXPacketBytes {
5353
return b.txPacketChan
5454
}
5555

56-
// SubscribeGatewayTX subscribes the backend to the gateway TXPacket
56+
// SubscribeGatewayTX subscribes the backend to the gateway TXPacketBytes
5757
// topic (packets the gateway needs to transmit).
5858
func (b *Backend) SubscribeGatewayTX(mac lorawan.EUI64) error {
5959
defer b.mutex.Unlock()
@@ -68,7 +68,7 @@ func (b *Backend) SubscribeGatewayTX(mac lorawan.EUI64) error {
6868
return nil
6969
}
7070

71-
// UnSubscribeGatewayTX unsubscribes the backend from the gateway TXPacket
71+
// UnSubscribeGatewayTX unsubscribes the backend from the gateway TXPacketBytes
7272
// topic.
7373
func (b *Backend) UnSubscribeGatewayTX(mac lorawan.EUI64) error {
7474
defer b.mutex.Unlock()
@@ -84,13 +84,13 @@ func (b *Backend) UnSubscribeGatewayTX(mac lorawan.EUI64) error {
8484
}
8585

8686
// PublishGatewayRX publishes a RX packet to the MQTT broker.
87-
func (b *Backend) PublishGatewayRX(mac lorawan.EUI64, rxPacket models.RXPacket) error {
87+
func (b *Backend) PublishGatewayRX(mac lorawan.EUI64, rxPacket gw.RXPacketBytes) error {
8888
topic := fmt.Sprintf("gateway/%s/rx", mac.String())
8989
return b.publish(topic, rxPacket)
9090
}
9191

9292
// PublishGatewayStats publishes a GatewayStatsPacket to the MQTT broker.
93-
func (b *Backend) PublishGatewayStats(mac lorawan.EUI64, stats models.GatewayStatsPacket) error {
93+
func (b *Backend) PublishGatewayStats(mac lorawan.EUI64, stats gw.GatewayStatsPacket) error {
9494
topic := fmt.Sprintf("gateway/%s/stats", mac.String())
9595
return b.publish(topic, stats)
9696
}
@@ -109,7 +109,7 @@ func (b *Backend) publish(topic string, v interface{}) error {
109109

110110
func (b *Backend) txPacketHandler(c mqtt.Client, msg mqtt.Message) {
111111
log.WithField("topic", msg.Topic()).Info("backend: packet received")
112-
var txPacket models.TXPacket
112+
var txPacket gw.TXPacketBytes
113113
if err := json.Unmarshal(msg.Payload(), &txPacket); err != nil {
114114
log.Errorf("backend: decode tx packet error: %s", err)
115115
return

backend/mqttpubsub/backend_test.go

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import (
55
"testing"
66
"time"
77

8-
"github.com/brocaar/loraserver/models"
9-
"github.com/brocaar/lorawan"
8+
"github.com/brocaar/loraserver/api/gw"
109
"github.com/eclipse/paho.mqtt.golang"
1110
. "github.com/smartystreets/goconvey/convey"
1211
)
@@ -28,9 +27,9 @@ func TestBackend(t *testing.T) {
2827
defer backend.Close()
2928

3029
Convey("Given the MQTT client is subscribed to RX packets", func() {
31-
rxPacketChan := make(chan models.RXPacket)
30+
rxPacketChan := make(chan gw.RXPacketBytes)
3231
token := c.Subscribe("gateway/+/rx", 0, func(c mqtt.Client, msg mqtt.Message) {
33-
var rxPacket models.RXPacket
32+
var rxPacket gw.RXPacketBytes
3433
if err := json.Unmarshal(msg.Payload(), &rxPacket); err != nil {
3534
t.Fatal(err)
3635
}
@@ -40,18 +39,12 @@ func TestBackend(t *testing.T) {
4039
So(token.Error(), ShouldBeNil)
4140

4241
Convey("When publishing a RXPacket", func() {
43-
rxPacket := models.RXPacket{
44-
RXInfo: models.RXInfo{
42+
rxPacket := gw.RXPacketBytes{
43+
RXInfo: gw.RXInfo{
4544
MAC: [8]byte{1, 2, 3, 4, 5, 6, 7, 8},
4645
Time: time.Now().UTC(),
4746
},
48-
PHYPayload: lorawan.PHYPayload{
49-
MHDR: lorawan.MHDR{
50-
MType: lorawan.UnconfirmedDataUp,
51-
Major: lorawan.LoRaWANR1,
52-
},
53-
MACPayload: &lorawan.MACPayload{},
54-
},
47+
PHYPayload: []byte{1, 2, 3, 4},
5548
}
5649

5750
err := backend.PublishGatewayRX([8]byte{1, 2, 3, 4, 5, 6, 7, 8}, rxPacket)
@@ -69,17 +62,11 @@ func TestBackend(t *testing.T) {
6962
So(err, ShouldBeNil)
7063

7164
Convey("When publishing a TXPacket from the MQTT client", func() {
72-
txPacket := models.TXPacket{
73-
TXInfo: models.TXInfo{
65+
txPacket := gw.TXPacketBytes{
66+
TXInfo: gw.TXInfo{
7467
MAC: [8]byte{1, 2, 3, 4, 5, 6, 7, 8},
7568
},
76-
PHYPayload: lorawan.PHYPayload{
77-
MHDR: lorawan.MHDR{
78-
MType: lorawan.UnconfirmedDataUp,
79-
Major: lorawan.LoRaWANR1,
80-
},
81-
MACPayload: &lorawan.MACPayload{},
82-
},
69+
PHYPayload: []byte{1, 2, 3, 4},
8370
}
8471
b, err := json.Marshal(txPacket)
8572
So(err, ShouldBeNil)

gateway/backend.go

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"sync"
1111
"time"
1212

13-
"github.com/brocaar/loraserver/models"
13+
"github.com/brocaar/loraserver/api/gw"
1414
"github.com/brocaar/lorawan"
1515
"github.com/brocaar/lorawan/band"
1616

@@ -81,8 +81,8 @@ func (c *gateways) cleanup() error {
8181
// Backend implements a Semtech gateway backend.
8282
type Backend struct {
8383
conn *net.UDPConn
84-
rxChan chan models.RXPacket
85-
statsChan chan models.GatewayStatsPacket
84+
rxChan chan gw.RXPacketBytes
85+
statsChan chan gw.GatewayStatsPacket
8686
udpSendChan chan udpPacket
8787
closed bool
8888
gateways gateways
@@ -103,8 +103,8 @@ func NewBackend(bind string, onNew func(lorawan.EUI64) error, onDelete func(lora
103103

104104
b := &Backend{
105105
conn: conn,
106-
rxChan: make(chan models.RXPacket),
107-
statsChan: make(chan models.GatewayStatsPacket),
106+
rxChan: make(chan gw.RXPacketBytes),
107+
statsChan: make(chan gw.GatewayStatsPacket),
108108
udpSendChan: make(chan udpPacket),
109109
gateways: gateways{
110110
gateways: make(map[lorawan.EUI64]gateway),
@@ -157,17 +157,17 @@ func (b *Backend) Close() error {
157157
}
158158

159159
// RXPacketChan returns the channel containing the received RX packets.
160-
func (b *Backend) RXPacketChan() chan models.RXPacket {
160+
func (b *Backend) RXPacketChan() chan gw.RXPacketBytes {
161161
return b.rxChan
162162
}
163163

164164
// StatsChan returns the channel containg the received gateway stats.
165-
func (b *Backend) StatsChan() chan models.GatewayStatsPacket {
165+
func (b *Backend) StatsChan() chan gw.GatewayStatsPacket {
166166
return b.statsChan
167167
}
168168

169169
// Send sends the given packet to the gateway.
170-
func (b *Backend) Send(txPacket models.TXPacket) error {
170+
func (b *Backend) Send(txPacket gw.TXPacketBytes) error {
171171
gw, err := b.gateways.get(txPacket.TXInfo.MAC)
172172
if err != nil {
173173
return err
@@ -383,9 +383,9 @@ func (b *Backend) handleTXACK(addr *net.UDPAddr, data []byte) error {
383383
}
384384

385385
// newGatewayStatsPacket from Stat transforms a Semtech Stat packet into a
386-
// models.GatewayStatsPacket.
387-
func newGatewayStatsPacket(mac lorawan.EUI64, stat Stat) models.GatewayStatsPacket {
388-
return models.GatewayStatsPacket{
386+
// gw.GatewayStatsPacket.
387+
func newGatewayStatsPacket(mac lorawan.EUI64, stat Stat) gw.GatewayStatsPacket {
388+
return gw.GatewayStatsPacket{
389389
Time: time.Time(stat.Time),
390390
MAC: mac,
391391
Latitude: stat.Lati,
@@ -396,21 +396,21 @@ func newGatewayStatsPacket(mac lorawan.EUI64, stat Stat) models.GatewayStatsPack
396396
}
397397
}
398398

399-
// newRXPacketFromRXPK transforms a Semtech packet into a models.RXPacket.
400-
func newRXPacketFromRXPK(mac lorawan.EUI64, rxpk RXPK) (models.RXPacket, error) {
401-
var phy lorawan.PHYPayload
402-
if err := phy.UnmarshalText([]byte(rxpk.Data)); err != nil {
403-
return models.RXPacket{}, fmt.Errorf("gateway: could not unmarshal PHYPayload: %s", err)
399+
// newRXPacketFromRXPK transforms a Semtech packet into a gw.RXPacketBytes.
400+
func newRXPacketFromRXPK(mac lorawan.EUI64, rxpk RXPK) (gw.RXPacketBytes, error) {
401+
dataRate, err := newDataRateFromDatR(rxpk.DatR)
402+
if err != nil {
403+
return gw.RXPacketBytes{}, fmt.Errorf("gateway: could not get DataRate from DatR: %s", err)
404404
}
405405

406-
dataRate, err := newDataRateFromDatR(rxpk.DatR)
406+
b, err := base64.StdEncoding.DecodeString(rxpk.Data)
407407
if err != nil {
408-
return models.RXPacket{}, fmt.Errorf("gateway: could not get DataRate from DatR: %s", err)
408+
return gw.RXPacketBytes{}, fmt.Errorf("gateway: could not base64 decode data: %s", err)
409409
}
410410

411-
rxPacket := models.RXPacket{
412-
PHYPayload: phy,
413-
RXInfo: models.RXInfo{
411+
rxPacket := gw.RXPacketBytes{
412+
PHYPayload: b,
413+
RXInfo: gw.RXInfo{
414414
MAC: mac,
415415
Time: time.Time(rxpk.Time),
416416
Timestamp: rxpk.Tmst,
@@ -428,14 +428,9 @@ func newRXPacketFromRXPK(mac lorawan.EUI64, rxpk RXPK) (models.RXPacket, error)
428428
return rxPacket, nil
429429
}
430430

431-
// newTXPKFromTXPacket transforms a models.TXPacket into a Semtech
431+
// newTXPKFromTXPacket transforms a gw.TXPacketBytes into a Semtech
432432
// compatible packet.
433-
func newTXPKFromTXPacket(txPacket models.TXPacket) (TXPK, error) {
434-
b, err := txPacket.PHYPayload.MarshalBinary()
435-
if err != nil {
436-
return TXPK{}, err
437-
}
438-
433+
func newTXPKFromTXPacket(txPacket gw.TXPacketBytes) (TXPK, error) {
439434
txpk := TXPK{
440435
Imme: txPacket.TXInfo.Immediately,
441436
Tmst: txPacket.TXInfo.Timestamp,
@@ -444,8 +439,8 @@ func newTXPKFromTXPacket(txPacket models.TXPacket) (TXPK, error) {
444439
Modu: string(txPacket.TXInfo.DataRate.Modulation),
445440
DatR: newDatRfromDataRate(txPacket.TXInfo.DataRate),
446441
CodR: txPacket.TXInfo.CodeRate,
447-
Size: uint16(len(b)),
448-
Data: base64.RawStdEncoding.EncodeToString(b),
442+
Size: uint16(len(txPacket.PHYPayload)),
443+
Data: base64.StdEncoding.EncodeToString(txPacket.PHYPayload),
449444
}
450445

451446
// TODO: do testing with FSK modulation

gateway/backend_test.go

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"testing"
77
"time"
88

9-
"github.com/brocaar/loraserver/models"
9+
"github.com/brocaar/loraserver/api/gw"
1010
"github.com/brocaar/lorawan"
1111
"github.com/brocaar/lorawan/band"
1212
. "github.com/smartystreets/goconvey/convey"
@@ -140,19 +140,8 @@ func TestBackend(t *testing.T) {
140140
})
141141

142142
Convey("Given a TXPacket", func() {
143-
var nwkSKey lorawan.AES128Key
144-
145-
phy := lorawan.PHYPayload{
146-
MHDR: lorawan.MHDR{
147-
MType: lorawan.UnconfirmedDataDown,
148-
Major: lorawan.LoRaWANR1,
149-
},
150-
MACPayload: &lorawan.MACPayload{},
151-
}
152-
So(phy.SetMIC(nwkSKey), ShouldBeNil)
153-
154-
txPacket := models.TXPacket{
155-
TXInfo: models.TXInfo{
143+
txPacket := gw.TXPacketBytes{
144+
TXInfo: gw.TXInfo{
156145
MAC: [8]byte{1, 2, 3, 4, 5, 6, 7, 8},
157146
Immediately: true,
158147
Timestamp: 12345,
@@ -165,7 +154,7 @@ func TestBackend(t *testing.T) {
165154
},
166155
CodeRate: "4/5",
167156
},
168-
PHYPayload: phy,
157+
PHYPayload: []byte{1, 2, 3, 4},
169158
}
170159

171160
Convey("When sending the TXPacket and the gateway is not known to the backend", func() {
@@ -207,9 +196,6 @@ func TestBackend(t *testing.T) {
207196
var pullResp PullRespPacket
208197
So(pullResp.UnmarshalBinary(buf[:i]), ShouldBeNil)
209198

210-
str, err := phy.MarshalText()
211-
So(err, ShouldBeNil)
212-
213199
So(pullResp, ShouldResemble, PullRespPacket{
214200
ProtocolVersion: p.ProtocolVersion,
215201
Payload: PullRespPayload{
@@ -223,8 +209,8 @@ func TestBackend(t *testing.T) {
223209
LoRa: "SF12BW250",
224210
},
225211
CodR: "4/5",
226-
Size: uint16(len(b)),
227-
Data: string(str),
212+
Size: uint16(len([]byte{1, 2, 3, 4})),
213+
Data: base64.StdEncoding.EncodeToString([]byte{1, 2, 3, 4}),
228214
IPol: true,
229215
},
230216
},
@@ -253,9 +239,9 @@ func TestNewGatewayStatPacket(t *testing.T) {
253239
mac := [8]byte{1, 2, 3, 4, 5, 6, 7, 8}
254240

255241
Convey("When calling newGatewayStatsPacket", func() {
256-
gw := newGatewayStatsPacket(mac, stat)
242+
gwStats := newGatewayStatsPacket(mac, stat)
257243
Convey("Then all fields are set correctly", func() {
258-
So(gw, ShouldResemble, models.GatewayStatsPacket{
244+
So(gwStats, ShouldResemble, gw.GatewayStatsPacket{
259245
Time: now,
260246
MAC: mac,
261247
Latitude: 1.234,
@@ -286,7 +272,7 @@ func TestNewRXPacketFromRXPK(t *testing.T) {
286272
RSSI: -51,
287273
LSNR: 7,
288274
Size: 16,
289-
Data: "QAEBAQGAAAABVfdjR6YrSw==",
275+
Data: base64.StdEncoding.EncodeToString([]byte{1, 2, 3, 4}),
290276
}
291277
mac := [8]byte{1, 2, 3, 4, 5, 6, 7, 8}
292278

@@ -295,15 +281,9 @@ func TestNewRXPacketFromRXPK(t *testing.T) {
295281
So(err, ShouldBeNil)
296282

297283
Convey("Then all fields are set correctly", func() {
298-
b, err := base64.StdEncoding.DecodeString(rxpk.Data)
299-
So(err, ShouldBeNil)
300-
301-
var phy lorawan.PHYPayload
302-
So(phy.UnmarshalBinary(b), ShouldBeNil)
303-
304-
So(rxPacket.PHYPayload, ShouldResemble, phy)
284+
So(rxPacket.PHYPayload, ShouldResemble, []byte{1, 2, 3, 4})
305285

306-
So(rxPacket.RXInfo, ShouldResemble, models.RXInfo{
286+
So(rxPacket.RXInfo, ShouldResemble, gw.RXInfo{
307287
MAC: mac,
308288
Time: now,
309289
Timestamp: 708016819,

0 commit comments

Comments
 (0)