Simulate UDP Packet to Gateway Bridge with Python

Hi,

I’m trying, to simulate UDP packet to Gateway bridge with python, and I plan after having passed this stage, to change manually the payload. I followed precisely the documentation below :
Lora-net/packet_forwarder

how encode the byte :

I modified the file. .toml configuration of the gateway bridge:

  marshaler="json"

i try to see the example test in the link below :

https://github.com/brocaar/chirpstack-gateway-bridge/blob/master/internal/backend/semtechudp/backend_test.go

My python script :

import socket
import time
import json
import binascii

UDP_IP_ADDRESS = "127.0.0.1"
UDP_PORT_NO = 1700

PAYLOAD = '{"rxpk":[{"time":"2013-03-31T16:21:17.532038Z","tmst":3316387610,"chan":0,"rfch":0,"freq":863.00981,"stat":1,"modu":"LORA","datr":"SF10BW125","codr":"4/7","rssi":-38,"lsnr":5.5,"size":32,"data":"ysgRl452xNLep9S1NTIg2lomKDxUgn3DJ7DE+b00Ass"}]}'.encode("utf-8")
MAC = "ABCDEF1010101010"
hex_mac =bytes.fromhex(MAC)

frame =  b'\2'       # 0 for protocole version
frame += b'\7'      # byte 1 random token
frame += b'\5'      # byte 2 random token
frame += b'\0'      # push data identifier
frame += hex_mac
frame += PAYLOAD
print(frame)

while True:	    
    clientSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    clientSock.sendto(frame, (UDP_IP_ADDRESS, UDP_PORT_NO))
    time.sleep(5)

unfortunately when i run the script, the gateway brige gives me a base 64 format error, see below :

      Jan 15 11:51:40 B827EBFFFEF65A01 chirpstack-gateway-bridge[3821]: time="2020-01-15T11:51:40Z" level=error msg="backend/semtechudp: could not handle packet" addr="127.0.0.1:50002" data_base64="AgcFAKvN7xAQEBAQeyJyeHBrIjpbeyJ0aW1lIjoiMjAxMy0wMy0zMVQxNjoyMToxNy41MzIwMzhaIiwidG1zdCI6MzMxNjM4NzYxMCwiY2hhbiI6MCwicmZjaCI6MCwiZnJlcSI6ODYzLjAwOTgxLCJzdGF0IjoxLCJtb2R1IjoiTE9SQSIsImRhdHIiOiJTRjEwQlcxMjUiLCJjb2RyIjoiNC83IiwicnNzaSI6LTM4LCJsc25yIjo1LjUsInNpemUiOjMyLCJkYXRhIjoieXNnUmw0NTJ4TkxlcDlTMU5USWcybG9tS0R4VWduM0RKN0RFK2IwMEFzcyJ9XX0=" error="illegal base64 data at input byte 40"

Can some one help me to make the good udp packet format !! thanks.

Here’s what I use, where data is a dictionary

def _push_data(self, data):
	token = os.urandom(2)
	packet = binascii.unhexlify('02') + token + binascii.unhexlify('00') + binascii.unhexlify(self.mac) + data
	try:
		self.sock.sendto(packet, self.server_ip)
	except Exception as ex:
		logger.exception('Failed to push uplink packet to server: {}', ex)
1 Like

Hi @gustavobsch !

Thanks for reply, i had try you solution, but i have the same error, can please let me show your variable " data ", it is a string ?
in my case it’s :

      PAYLOAD = '{"rxpk":[{"time":"2013-03-31T16:21:17.532038Z","tmst":3316387610,"chan":0,"rfch":0,"freq":863.00981,"stat":1,"modu":"LORA","datr":"SF10BW125","codr":"4/7","rssi":-38,"lsnr":5.5,"size":32,"data":"ysgRl452xNLep9S1NTIg2lomKDxUgn3DJ7DE+b00Ass"}]}'.encode("utf-8")

Thanks

Hi,

Data is not a String nor a Dictionary, Data is base64 data type. What error are you getting?

@gustavobsch, i resolve the bug ! i had mistake on my payload, thanks

hello,can you share the bug-free python code?
i want konw more about simulate things,thanks