Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions base_station_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
# The system will recommend disconnecting after missing TIMEOUT PINGs.
# Exact timing depends on CHECK_FOR_TIMEOUTS_INTERVAL variable in base_station_tick()
"""
from constants import TIMEOUT, bUEs
from constants import TIMEOUT


# Internal imports
Expand All @@ -63,9 +63,14 @@ def __init__(self, yaml_str):
self.stdout_history = deque()

self.ota = Ota(
self.yaml_data["OTA_PORT"], self.yaml_data["OTA_BAUDRATE"], self.yaml_data["OTA_ID"], self.stdout_history
self.yaml_data["OTA_PORT"], self.yaml_data["OTA_BAUDRATE"], self.stdout_history
)
logger.info(f"[DEBUG] OTA ID is set to: {self.ota.id}")

# Fetch the Reyax ID from the OTA module
time.sleep(0.1)
self.reyax_id = self.ota.fetch_id()

logger.info(f"[DEBUG] OTA ID is set to: {self.reyax_id}")

self.EXIT = False

Expand Down Expand Up @@ -179,12 +184,17 @@ def message_listener(self):
if message_body.startswith("REQ"):
logger.debug("Sending a CON")
current_timestamp = int(time.time())
bue_name = message_body[4:]
_, payload = message_body.split(":", 1) # Split on first colon
bue_name, bue_id_check = payload.split(",", 1) # Split hostname and bUE_id

if int(bue_id_check) != bue_id:
logger.error(f"message_listener: Mismatched bUE ID in REQ message. Expected {bue_id}, got {bue_id_check}")
continue # Skip to the next message

self.ota.send_ota_message(bue_id, f"CON:{self.ota.id}:{current_timestamp}")
self.ota.send_ota_message(bue_id, f"CON:{self.reyax_id}:{current_timestamp}")
self.bue_timeout_tracker[bue_name] = TIMEOUT
if not bue_id in self.connected_bues:
logger.bind(bue_id=bue_id).info(f"Received a request signal from {bue_id}")
logger.bind(bue_id=bue_id).info(f"Received a request signal from {bue_id}:{bue_name}")
self.connected_bues[bue_id] = bue_name
else:
logger.error(f"Got a connection request from {bue_id} but it is already listed as connected")
Expand Down
14 changes: 11 additions & 3 deletions bue_main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Standard library imports
import os
import queue
import sys
import select
Expand Down Expand Up @@ -54,14 +55,21 @@ def __init__(self, yaml_str = "bue_config.yaml"):
try:
self.ota = Ota(
self.yaml_data["OTA_PORT"],
self.yaml_data["OTA_BAUDRATE"],
self.yaml_data["OTA_ID"],
self.yaml_data["OTA_BAUDRATE"]
)
break
except Exception as e:
logger.error(f"Failed to initialize OTA module: {e}")
time.sleep(2)

# Fetch the Reyax ID from the OTA module
time.sleep(0.1)
self.reyax_id = self.ota.fetch_id()
logger.info(f"__init__: OTA module initialized with Reyax ID {self.reyax_id}")

# Fetch the device hostname
self.hostname = os.uname().nodename

# Build the state machine - states
self.cur_st, self.nxt_st = State.INIT, State.INIT
logger.info(f"__init__: Initializing current state to {self.cur_st.name}")
Expand Down Expand Up @@ -240,7 +248,7 @@ def ota_connect_req(self):
return

# If flag not set, send another REQ message
self.ota_outgoing_queue.put((BROADCAST_OTA_ID, "REQ"))
self.ota_outgoing_queue.put((BROADCAST_OTA_ID, f"REQ:{self.hostname},{self.reyax_id}"))


def ota_idle_ping(self):
Expand Down
22 changes: 19 additions & 3 deletions ota.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


class Ota:
def __init__(self, port, baudrate, id, stdout_history=None):
def __init__(self, port, baudrate, stdout_history=None):

# Serial port configuration
while True:
Expand All @@ -27,8 +27,6 @@ def __init__(self, port, baudrate, id, stdout_history=None):
print(f"Failed to open serial port: {e}")
time.sleep(2)

self.id = id

self.stdout_history = stdout_history

# Initialize CRC8 calculator
Expand Down Expand Up @@ -155,6 +153,24 @@ def get_new_messages(self):
except queue.Empty:
pass
return messages

def fetch_id(self):
"""
Fetch the device ID from the Reyax module.
"""
try:
self.ser.write(b'AT+ADDRESS=?\r\n')
time.sleep(0.1) # Wait for response
response = self.ser.readlines()
for line in response:
decoded_line = line.decode('utf-8').strip()
if decoded_line.startswith('+ADDRESS='):
addr = decoded_line.split('=')[1]
self.id = int(addr)
return self.id
except Exception as e:
print(f"Failed to fetch ID: {e}")
return None

def __del__(self):
try:
Expand Down
4 changes: 2 additions & 2 deletions setup/message_dict.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ In the example for each of the messages, assume the bUE has an OTA id of 5, and

REQ:
Direction: bUE -> base
Meaning: The bUE is requesting that it join the OTA network and shares its name. It is broadcast over the ota, so it sends to address 0
Meaning: The bUE is requesting that it join the OTA network and shares its hostname and Reyax Id. It is broadcast over the ota, so it sends to address 0
Body: None
Example: (bue_main.py) self.ota.send_ota_message(0, "REQ:Perry")
Example: (bue_main.py) self.ota.send_ota_message(0, "REQ:<hostname>,<bUE_id>")
Response: The base station will respond with a CON message

CON:
Expand Down
Loading