Skip to content

Commit 353860f

Browse files
committed
Fix missing gw mac field and add handleTXACK.
Currently the handleTXACK is only logging the ack. The ack will be published to a MQTT topic in a future release.
1 parent d43b03e commit 353860f

File tree

3 files changed

+53
-17
lines changed

3 files changed

+53
-17
lines changed

gateway/backend.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ func (b *Backend) handlePacket(addr *net.UDPAddr, data []byte) error {
248248
return b.handlePushData(addr, data)
249249
case PullData:
250250
return b.handlePullData(addr, data)
251+
case TXACK:
252+
return b.handleTXACK(addr, data)
251253
default:
252254
return fmt.Errorf("gateway: unknown packet type: %s", pt)
253255
}
@@ -346,6 +348,33 @@ func (b *Backend) handleRXPacket(addr *net.UDPAddr, mac lorawan.EUI64, rxpk RXPK
346348
return nil
347349
}
348350

351+
func (b *Backend) handleTXACK(addr *net.UDPAddr, data []byte) error {
352+
var p TXACKPacket
353+
if err := p.UnmarshalBinary(data); err != nil {
354+
return err
355+
}
356+
var errBool bool
357+
358+
logFields := log.Fields{
359+
"mac": p.GatewayMAC,
360+
"random_token": p.RandomToken,
361+
}
362+
if p.Payload != nil {
363+
if p.Payload.TXPKACK.Error != "NONE" {
364+
errBool = true
365+
}
366+
logFields["error"] = p.Payload.TXPKACK.Error
367+
}
368+
369+
if errBool {
370+
log.WithFields(logFields).Error("gateway: tx ack received")
371+
} else {
372+
log.WithFields(logFields).Info("gateway: tx ack received")
373+
}
374+
375+
return nil
376+
}
377+
349378
// newGatewayStatsPacket from Stat transforms a Semtech Stat packet into a
350379
// models.GatewayStatsPacket.
351380
func newGatewayStatsPacket(mac lorawan.EUI64, stat Stat) models.GatewayStatsPacket {

gateway/structs.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ func (p *PullRespPacket) UnmarshalBinary(data []byte) error {
216216
// gateway.
217217
type TXACKPacket struct {
218218
RandomToken uint16
219+
GatewayMAC lorawan.EUI64
219220
Payload *TXACKPayload
220221
}
221222

@@ -230,18 +231,19 @@ func (p TXACKPacket) MarshalBinary() ([]byte, error) {
230231
}
231232
}
232233

233-
out := make([]byte, 4, 4+len(pb))
234+
out := make([]byte, 4, len(pb)+12)
234235
out[0] = ProtocolVersion2
235236
binary.LittleEndian.PutUint16(out[1:3], p.RandomToken)
236237
out[3] = byte(TXACK)
238+
out = append(out, p.GatewayMAC[:]...)
237239
out = append(out, pb...)
238240
return out, nil
239241
}
240242

241243
// UnmarshalBinary decodes the object from binary form.
242244
func (p *TXACKPacket) UnmarshalBinary(data []byte) error {
243-
if len(data) < 4 {
244-
return errors.New("gateway: at least 4 bytes of data are expected")
245+
if len(data) < 12 {
246+
return errors.New("gateway: at least 12 bytes of data are expected")
245247
}
246248
if data[3] != byte(TXACK) {
247249
return errors.New("gateway: identifier mismatch (TXACK expected)")
@@ -250,9 +252,12 @@ func (p *TXACKPacket) UnmarshalBinary(data []byte) error {
250252
return ErrInvalidProtocolVersion
251253
}
252254
p.RandomToken = binary.LittleEndian.Uint16(data[1:3])
253-
if len(data) > 4 {
255+
for i := 0; i < 8; i++ {
256+
p.GatewayMAC[i] = data[4+i]
257+
}
258+
if len(data) > 12 {
254259
p.Payload = &TXACKPayload{}
255-
return json.Unmarshal(data[4:], p.Payload)
260+
return json.Unmarshal(data[12:], p.Payload)
256261
}
257262
return nil
258263
}

gateway/structs_test.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -281,28 +281,30 @@ func TestPullRespPacket(t *testing.T) {
281281
func TestTXACKPacket(t *testing.T) {
282282
Convey("Given an empty TXACKPacket", t, func() {
283283
var p TXACKPacket
284-
Convey("Then MarshalBinary returns []byte{2, 0, 0, 5}", func() {
284+
Convey("Then MarshalBinary returns []byte{2, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0}", func() {
285285
b, err := p.MarshalBinary()
286286
So(err, ShouldBeNil)
287-
So(b, ShouldResemble, []byte{2, 0, 0, 5})
287+
So(b, ShouldResemble, []byte{2, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0})
288288
})
289289

290-
Convey("Given RandomToken=123", func() {
290+
Convey("Given RandomToken=123 and GatewayMAC=[]byte{8, 7, 6, 5, 4, 3, 2, 1}", func() {
291291
p.RandomToken = 123
292-
Convey("Then MarshalBinary returns []byte{2, 123, 0, 5}", func() {
292+
p.GatewayMAC = [8]byte{8, 7, 6, 5, 4, 3, 2, 1}
293+
Convey("Then MarshalBinary returns []byte{2, 123, 0, 5, 8, 7, 6, 5, 4, 3, 2, 1}", func() {
293294
b, err := p.MarshalBinary()
294295
So(err, ShouldBeNil)
295-
So(b[0:4], ShouldResemble, []byte{2, 123, 0, 5})
296+
So(b, ShouldResemble, []byte{2, 123, 0, 5, 8, 7, 6, 5, 4, 3, 2, 1})
296297
})
297298
})
298299

299-
Convey("Given the slice []byte{2, 123, 0, 5}", func() {
300-
b := []byte{2, 123, 0, 5}
300+
Convey("Given the slice []byte{2, 123, 0, 5, 8, 7, 6, 5, 4, 3, 2, 1}", func() {
301+
b := []byte{2, 123, 0, 5, 8, 7, 6, 5, 4, 3, 2, 1}
301302

302-
Convey("Then UnmarshalBinary return RandomToken=123", func() {
303+
Convey("Then UnmarshalBinary return RandomToken=123 and GatewayMAC=[8]byte{8, 7, 6, 5, 4, 3, 2, 1}", func() {
303304
err := p.UnmarshalBinary(b)
304305
So(err, ShouldBeNil)
305306
So(p.RandomToken, ShouldEqual, 123)
307+
So(p.GatewayMAC[:], ShouldResemble, []byte{8, 7, 6, 5, 4, 3, 2, 1})
306308
So(p.Payload, ShouldBeNil)
307309
})
308310
})
@@ -315,15 +317,15 @@ func TestTXACKPacket(t *testing.T) {
315317
},
316318
}
317319

318-
Convey("Then MarshalBinary returns []byte{2, 123, 0, 5, 123, 34, 116, 120, 112, 107, 95, 97, 99, 107, 34, 58, 123, 34, 101, 114, 114, 111, 114, 34, 58, 34, 67, 79, 76, 76, 73, 83, 73, 79, 78, 95, 66, 69, 65, 67, 79, 78, 34, 125, 125}", func() {
320+
Convey("Then MarshalBinary returns []byte{2, 123, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 123, 34, 116, 120, 112, 107, 95, 97, 99, 107, 34, 58, 123, 34, 101, 114, 114, 111, 114, 34, 58, 34, 67, 79, 76, 76, 73, 83, 73, 79, 78, 95, 66, 69, 65, 67, 79, 78, 34, 125, 125}", func() {
319321
b, err := p.MarshalBinary()
320322
So(err, ShouldBeNil)
321-
So(b, ShouldResemble, []byte{2, 123, 0, 5, 123, 34, 116, 120, 112, 107, 95, 97, 99, 107, 34, 58, 123, 34, 101, 114, 114, 111, 114, 34, 58, 34, 67, 79, 76, 76, 73, 83, 73, 79, 78, 95, 66, 69, 65, 67, 79, 78, 34, 125, 125})
323+
So(b, ShouldResemble, []byte{2, 123, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 123, 34, 116, 120, 112, 107, 95, 97, 99, 107, 34, 58, 123, 34, 101, 114, 114, 111, 114, 34, 58, 34, 67, 79, 76, 76, 73, 83, 73, 79, 78, 95, 66, 69, 65, 67, 79, 78, 34, 125, 125})
322324
})
323325
})
324326

325-
Convey("Given the slice []byte{2, 123, 0, 5, 123, 34, 116, 120, 112, 107, 95, 97, 99, 107, 34, 58, 123, 34, 101, 114, 114, 111, 114, 34, 58, 34, 67, 79, 76, 76, 73, 83, 73, 79, 78, 95, 66, 69, 65, 67, 79, 78, 34, 125, 125}", func() {
326-
b := []byte{2, 123, 0, 5, 123, 34, 116, 120, 112, 107, 95, 97, 99, 107, 34, 58, 123, 34, 101, 114, 114, 111, 114, 34, 58, 34, 67, 79, 76, 76, 73, 83, 73, 79, 78, 95, 66, 69, 65, 67, 79, 78, 34, 125, 125}
327+
Convey("Given the slice []byte{2, 123, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 123, 34, 116, 120, 112, 107, 95, 97, 99, 107, 34, 58, 123, 34, 101, 114, 114, 111, 114, 34, 58, 34, 67, 79, 76, 76, 73, 83, 73, 79, 78, 95, 66, 69, 65, 67, 79, 78, 34, 125, 125}", func() {
328+
b := []byte{2, 123, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 123, 34, 116, 120, 112, 107, 95, 97, 99, 107, 34, 58, 123, 34, 101, 114, 114, 111, 114, 34, 58, 34, 67, 79, 76, 76, 73, 83, 73, 79, 78, 95, 66, 69, 65, 67, 79, 78, 34, 125, 125}
327329
Convey("Then UnmarshalBinary returns RandomToken=123 and a payload with Error=COLLISION_BEACON", func() {
328330
err := p.UnmarshalBinary(b)
329331
So(err, ShouldBeNil)

0 commit comments

Comments
 (0)