2023-05-24 07:00:54 +00:00
|
|
|
# -*- coding: UTF-8 -*-
|
|
|
|
"""
|
|
|
|
|
|
|
|
@author: DJ2LS
|
|
|
|
|
|
|
|
HF mesh networking prototype and testing module
|
|
|
|
|
|
|
|
import time
|
|
|
|
MeshParam.routing_table = [['AA1AA', 'direct', 0, 1.0, 25, time.time(), ], ['AA1AA', 'AA2BB', 1, 3.1, 10, time.time(), ],
|
|
|
|
['AA3CC', 'AA2BB', 5, -4.5, -3, time.time(), ]]
|
|
|
|
|
|
|
|
print(MeshParam.routing_table)
|
|
|
|
print("---------------------------------")
|
|
|
|
|
2023-05-27 17:52:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TODO: SIGNALLING FOR ACK/NACK:
|
|
|
|
- mesh-signalling burst is datac13
|
|
|
|
- mesh-signalling frame contains [message id, status]
|
|
|
|
- create a list for signalling frames, contains [message id, message-status, attempts, state, timestamp]
|
|
|
|
- on "IRS", send ACK/NACK 10 times on receiving beacon?
|
|
|
|
- on "ROUTER", receive ACK/NACK, and store it in table, also send it 10 times
|
|
|
|
- if sent 10 times, set ACK/NACK state to "done"
|
|
|
|
- if done already in list, don't reset retry counter
|
|
|
|
- delete ACK/NACK if "done" and timestamp older than 1day
|
|
|
|
|
|
|
|
|
2023-05-24 07:00:54 +00:00
|
|
|
"""
|
|
|
|
# pylint: disable=invalid-name, line-too-long, c-extension-no-member
|
|
|
|
# pylint: disable=import-outside-toplevel, attribute-defined-outside-init
|
|
|
|
|
2023-05-26 12:25:48 +00:00
|
|
|
from static import TNC, MeshParam, FRAME_TYPE, Station, ModemParam
|
|
|
|
from codec2 import FREEDV_MODE
|
|
|
|
import numpy as np
|
2023-05-24 07:00:54 +00:00
|
|
|
import time
|
|
|
|
import threading
|
2023-05-26 12:25:48 +00:00
|
|
|
import modem
|
|
|
|
import helpers
|
2023-05-27 17:52:09 +00:00
|
|
|
import structlog
|
|
|
|
|
2023-05-26 12:25:48 +00:00
|
|
|
from queues import MESH_RECEIVED_QUEUE
|
2023-05-24 07:00:54 +00:00
|
|
|
|
|
|
|
class MeshRouter():
|
2023-05-26 12:25:48 +00:00
|
|
|
def __init__(self):
|
2023-05-27 17:52:09 +00:00
|
|
|
|
2023-05-27 19:00:46 +00:00
|
|
|
self.log = structlog.get_logger("RF")
|
2023-05-27 17:52:09 +00:00
|
|
|
|
|
|
|
|
2023-05-26 12:25:48 +00:00
|
|
|
self.mesh_broadcasting_thread = threading.Thread(
|
|
|
|
target=self.broadcast_routing_table, name="worker thread receive", daemon=True
|
|
|
|
)
|
|
|
|
self.mesh_broadcasting_thread.start()
|
|
|
|
|
|
|
|
self.mesh_rx_dispatcher_thread = threading.Thread(
|
|
|
|
target=self.mesh_rx_dispatcher, name="worker thread receive", daemon=True
|
|
|
|
)
|
|
|
|
self.mesh_rx_dispatcher_thread.start()
|
|
|
|
|
2023-05-24 07:00:54 +00:00
|
|
|
|
|
|
|
def get_from_heard_stations(self):
|
|
|
|
"""
|
|
|
|
get data from heard stations
|
|
|
|
heard stations format:
|
|
|
|
[dxcallsign,dxgrid,int(time.time()),datatype,snr,offset,frequency]
|
|
|
|
|
|
|
|
TNC.heard_stations.append(
|
|
|
|
[
|
|
|
|
dxcallsign,
|
|
|
|
dxgrid,
|
|
|
|
int(time.time()),
|
|
|
|
datatype,
|
|
|
|
snr,
|
|
|
|
offset,
|
|
|
|
frequency,
|
|
|
|
]
|
|
|
|
)
|
|
|
|
"""
|
2023-05-28 07:46:46 +00:00
|
|
|
dxcallsign_position = 0
|
|
|
|
dxgrid_position = 1
|
|
|
|
timestamp_position = 2
|
|
|
|
type_position = 3
|
|
|
|
snr_position = 4
|
|
|
|
offset_position = 5
|
|
|
|
frequency_position = 6
|
2023-05-24 07:00:54 +00:00
|
|
|
|
2023-05-28 07:23:01 +00:00
|
|
|
try:
|
|
|
|
for item in TNC.heard_stations:
|
2023-05-28 07:46:46 +00:00
|
|
|
print("-----------")
|
|
|
|
print(item)
|
|
|
|
print(item[snr_position])
|
2023-05-28 07:23:01 +00:00
|
|
|
try:
|
2023-05-28 07:46:46 +00:00
|
|
|
print(item[snr_position])
|
|
|
|
snr = bytes(item[snr_position], "utf-8").split(b"/")
|
2023-05-28 07:23:01 +00:00
|
|
|
snr = int(float(snr[0]))
|
2023-05-28 07:24:52 +00:00
|
|
|
except Exception as err:
|
2023-05-28 07:46:46 +00:00
|
|
|
snr = int(float(item[snr_position]))
|
2023-05-28 07:23:01 +00:00
|
|
|
|
2023-05-28 07:46:46 +00:00
|
|
|
new_router = [helpers.get_crc_24(item[dxcallsign_position]), helpers.get_crc_24(b'direct'), 0, snr, snr, item[timestamp_position]]
|
2023-05-28 07:23:01 +00:00
|
|
|
self.add_router_to_routing_table(new_router)
|
|
|
|
except Exception as e:
|
|
|
|
self.log.warning("[MESH] error fetching data from heard station list", e=e)
|
2023-05-24 07:00:54 +00:00
|
|
|
|
|
|
|
def add_router_to_routing_table(self, new_router):
|
2023-05-28 07:30:54 +00:00
|
|
|
try:
|
|
|
|
# destination callsign # router callsign # hops # rx snr # route quality # timestamp
|
|
|
|
for _, item in enumerate(MeshParam.routing_table):
|
|
|
|
# update routing entry if exists
|
|
|
|
if new_router[0] in item[0] and new_router[1] in item[1]:
|
|
|
|
print(f"UPDATE {MeshParam.routing_table[_]} >>> {new_router}")
|
|
|
|
MeshParam.routing_table[_] = new_router
|
|
|
|
|
|
|
|
# add new routing entry if not exists
|
|
|
|
if new_router not in MeshParam.routing_table:
|
|
|
|
print(f"INSERT {new_router} >>> ROUTING TABLE")
|
|
|
|
MeshParam.routing_table.append(new_router)
|
|
|
|
except Exception as e:
|
|
|
|
self.log.warning("[MESH] error adding data to routing table", e=e, router=new_router)
|
2023-05-26 12:25:48 +00:00
|
|
|
|
2023-05-28 14:06:07 +00:00
|
|
|
def broadcast_routing_table(self, interval=40):
|
2023-05-26 12:25:48 +00:00
|
|
|
# enable receiving for datac4 if broadcasting
|
|
|
|
modem.RECEIVE_DATAC4 = True
|
|
|
|
|
|
|
|
while True:
|
2023-05-27 10:56:33 +00:00
|
|
|
try:
|
2023-05-28 07:23:01 +00:00
|
|
|
|
2023-05-27 10:56:33 +00:00
|
|
|
# wait some time until sending routing table
|
|
|
|
threading.Event().wait(interval)
|
|
|
|
|
|
|
|
# before we are transmitting, let us update our routing table
|
|
|
|
self.get_from_heard_stations()
|
|
|
|
|
|
|
|
#[b'DJ2LS-0', 'direct', 0, 9.6, 9.6, 1684912305]
|
|
|
|
mesh_broadcast_frame_header = bytearray(4)
|
|
|
|
mesh_broadcast_frame_header[:1] = bytes([FRAME_TYPE.MESH_BROADCAST.value])
|
|
|
|
mesh_broadcast_frame_header[1:4] = helpers.get_crc_24(Station.mycallsign)
|
|
|
|
|
|
|
|
# callsign(6), router(6), hops(1), path_score(1) == 14 ==> 14 28 42 ==> 3 mesh routing entries
|
|
|
|
# callsign_crc(3), router_crc(3), hops(1), path_score(1) == 8 --> 6
|
|
|
|
# callsign_crc(3), hops(1), path_score(1) == 5 --> 10
|
|
|
|
|
|
|
|
# Create a new bytearray with a fixed length of 50
|
|
|
|
result = bytearray(50)
|
|
|
|
|
|
|
|
# Iterate over the route subarrays and add the selected entries to the result bytearray
|
|
|
|
index = 0
|
|
|
|
for route_id, route in enumerate(MeshParam.routing_table):
|
2023-05-28 07:23:01 +00:00
|
|
|
# the value 5 is the length of crc24 + hops + score
|
|
|
|
|
2023-05-27 10:56:33 +00:00
|
|
|
dxcall = MeshParam.routing_table[route_id][0]
|
|
|
|
# router = MeshParam.routing_table[i][1]
|
|
|
|
hops = MeshParam.routing_table[route_id][2]
|
|
|
|
# snr = MeshParam.routing_table[i][3]
|
|
|
|
route_score = np.clip(MeshParam.routing_table[route_id][4], 0, 254)
|
|
|
|
# timestamp = MeshParam.routing_table[i][5]
|
|
|
|
result[index:index + 5] = dxcall + bytes([hops]) + bytes([route_score])
|
2023-05-28 07:23:01 +00:00
|
|
|
index += 5
|
2023-05-27 10:56:33 +00:00
|
|
|
|
|
|
|
print(len(result))
|
|
|
|
# Split the result bytearray into a list of fixed-length bytearrays
|
|
|
|
split_result = [result[i:i + 50] for i in range(0, len(result), 50)]
|
|
|
|
print(len(split_result))
|
|
|
|
frame_list = []
|
|
|
|
for _ in split_result:
|
|
|
|
# make sure payload is always 50
|
|
|
|
_[len(_):] = bytes(50 - len(_))
|
|
|
|
#print(len(_))
|
|
|
|
frame_list.append(mesh_broadcast_frame_header + _)
|
|
|
|
|
|
|
|
print(frame_list)
|
|
|
|
TNC.transmitting = True
|
|
|
|
c2_mode = FREEDV_MODE.datac4.value
|
|
|
|
modem.MODEM_TRANSMIT_QUEUE.put([c2_mode, 1, 0, frame_list])
|
|
|
|
|
|
|
|
# Wait while transmitting
|
|
|
|
while TNC.transmitting:
|
|
|
|
threading.Event().wait(0.01)
|
|
|
|
except Exception as e:
|
2023-05-28 07:23:01 +00:00
|
|
|
self.log.warning("[MESH] broadcasting routing table", e=e)
|
2023-05-26 12:25:48 +00:00
|
|
|
|
|
|
|
def mesh_rx_dispatcher(self):
|
|
|
|
while True:
|
|
|
|
data_in = MESH_RECEIVED_QUEUE.get()
|
|
|
|
if int.from_bytes(data_in[:1], "big") in [FRAME_TYPE.MESH_BROADCAST.value]:
|
2023-05-26 13:36:05 +00:00
|
|
|
self.received_routing_table(data_in[:-2])
|
2023-05-26 12:25:48 +00:00
|
|
|
else:
|
|
|
|
print("wrong mesh data received")
|
|
|
|
print(data_in)
|
|
|
|
def received_routing_table(self, data_in):
|
2023-05-27 10:56:33 +00:00
|
|
|
try:
|
|
|
|
print("data received........")
|
|
|
|
print(data_in)
|
|
|
|
|
|
|
|
router = data_in[1:4] # Extract the first 4 bytes (header)
|
|
|
|
payload = data_in[4:] # Extract the payload (excluding the header)
|
|
|
|
|
|
|
|
print("Router:", router) # Output the header bytes
|
|
|
|
|
|
|
|
for i in range(0, len(payload)-1, 5):
|
|
|
|
callsign_checksum = payload[i:i + 3] # First 3 bytes of the information (callsign_checksum)
|
2023-05-27 11:47:04 +00:00
|
|
|
hops = int.from_bytes(payload[i+3:i + 4], "big") # Fourth byte of the information (hops)
|
|
|
|
score = int.from_bytes(payload[i+4:i + 5], "big") # Fifth byte of the information (score)
|
2023-05-27 10:56:33 +00:00
|
|
|
timestamp = int(time.time())
|
|
|
|
snr = int(ModemParam.snr)
|
|
|
|
|
|
|
|
# use case 1: add new router to table only if callsign not empty
|
|
|
|
_use_case1 = callsign_checksum.startswith(b'\x00')
|
|
|
|
|
|
|
|
# use case 2: add new router to table only if not own callsign
|
|
|
|
_use_case2 = callsign_checksum not in [helpers.get_crc_24(Station.mycallsign)]
|
|
|
|
|
2023-05-27 12:23:46 +00:00
|
|
|
# use case 3: increment hop if router not direct
|
2023-05-27 10:56:33 +00:00
|
|
|
if router not in [helpers.get_crc_24(b'direct')] and hops == 0:
|
|
|
|
hops += 1
|
|
|
|
|
2023-05-27 12:23:46 +00:00
|
|
|
# use case 4: if callsign is directly available skip route for only keeping shortest way in db
|
|
|
|
_use_case4 = False
|
|
|
|
for _, call in enumerate(MeshParam.routing_table):
|
2023-05-27 12:26:42 +00:00
|
|
|
# check if callsign already in routing table and is direct connection
|
|
|
|
if callsign_checksum in [MeshParam.routing_table[_][0]] and MeshParam.routing_table[_][1] in [helpers.get_crc_24(b'direct')]:
|
2023-05-27 12:23:46 +00:00
|
|
|
_use_case4 = True
|
2023-05-27 11:47:04 +00:00
|
|
|
|
|
|
|
# use case N: calculate score
|
2023-05-27 10:56:33 +00:00
|
|
|
# TODO...
|
|
|
|
|
|
|
|
if not _use_case1 \
|
2023-05-27 12:23:46 +00:00
|
|
|
and _use_case2\
|
|
|
|
and not _use_case4:
|
2023-05-27 10:56:33 +00:00
|
|
|
|
2023-05-28 07:23:01 +00:00
|
|
|
print("Callsign Checksum:", callsign_checksum)
|
|
|
|
print("Hops:", hops)
|
|
|
|
print("Score:", score)
|
|
|
|
|
2023-05-27 10:56:33 +00:00
|
|
|
new_router = [callsign_checksum, router, hops, snr, score, timestamp]
|
|
|
|
print(new_router)
|
|
|
|
self.add_router_to_routing_table(new_router)
|
|
|
|
|
|
|
|
print("-------------------------")
|
|
|
|
for _, item in enumerate(MeshParam.routing_table):
|
|
|
|
print(MeshParam.routing_table[_])
|
|
|
|
print("-------------------------")
|
|
|
|
except Exception as e:
|
|
|
|
self.log.warning("[MESH] error processing received routing broadcast", e=e)
|