Skip to content

Commit

Permalink
RSDK-9814 Increase packet buffer size to 10 (#24)
Browse files Browse the repository at this point in the history
* add warning

* edits

* move comment

* move to default case

* fix test

* pr review

* add nil check
  • Loading branch information
oliviamiller authored Feb 3, 2025
1 parent 98a9bdd commit b77b66a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
5 changes: 2 additions & 3 deletions gateway/gateway.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
#include <stdio.h>
#include <unistd.h>

#define MAX_RX_PKT 8

#define RADIO_0_FREQ 902700000
#define RADIO_1_FREQ 903700000
int MAX_RX_PKT = 10;

// the IF chain frequencies allow the gateway to read on multiple frequency channels.
// subtracting main frequenecy - intermediate frequency will give that channel's freq.
Expand Down Expand Up @@ -109,7 +108,7 @@ struct lgw_pkt_rx_s* createRxPacketArray() {
}

int receive(struct lgw_pkt_rx_s* packet) {
return lgw_receive(1, packet);
return lgw_receive(MAX_RX_PKT, packet);
}

int send(struct lgw_pkt_tx_s* packet) {
Expand Down
40 changes: 26 additions & 14 deletions gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strings"
"sync"
"time"
"unsafe"

"go.viam.com/rdk/components/board"
"go.viam.com/rdk/components/sensor"
Expand Down Expand Up @@ -269,6 +270,8 @@ func (g *gateway) captureCOutputToLogs(ctx context.Context, scanner *bufio.Scann
g.logger.Error("gateway hardware is misconfigured: unplug the board and wait a few minutes before trying again")
case strings.Contains(line, "ERROR"):
g.logger.Error(line)
case strings.Contains(line, "WARNING"):
g.logger.Warn(line)
default:
g.logger.Debug(line)
}
Expand All @@ -279,14 +282,17 @@ func (g *gateway) captureCOutputToLogs(ctx context.Context, scanner *bufio.Scann

func (g *gateway) receivePackets(ctx context.Context) {
// receive the radio packets
packet := C.createRxPacketArray()
p := C.createRxPacketArray()
defer C.free(unsafe.Pointer(p))
for {
select {
case <-ctx.Done():

return
default:
}
numPackets := int(C.receive(packet))
numPackets := int(C.receive(p))

switch numPackets {
case 0:
// no packet received, wait 10 ms to receive again.
Expand All @@ -295,19 +301,25 @@ func (g *gateway) receivePackets(ctx context.Context) {
return
case <-time.After(10 * time.Millisecond):
}
case 1:
// received a LORA packet
var payload []byte
if packet.size == 0 {
continue
}
// Convert packet to go byte array
for i := range int(packet.size) {
payload = append(payload, byte(packet.payload[i]))
}
g.handlePacket(ctx, payload)
default:
case -1:
g.logger.Errorf("error receiving lora packet")
default:
// convert from c array to go slice
packets := unsafe.Slice((*C.struct_lgw_pkt_rx_s)(unsafe.Pointer(p)), int(C.MAX_RX_PKT))
for i := range numPackets {
// received a LORA packet
var payload []byte
if packets[i].size == 0 {
continue
}
// Convert packet to go byte array
for j := range int(packets[i].size) {
payload = append(payload, byte(packets[i].payload[j]))
}
if payload != nil {
g.handlePacket(ctx, payload)
}
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions gateway/gateway.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ int stopGateway();
int setUpGateway(int com_path);
void disableBuffering();
void redirectToPipe(int fd);
extern const int MAX_RX_PKT;

0 comments on commit b77b66a

Please sign in to comment.