diff --git a/src/network_systems/projects/remote_transceiver/test/test_remote_transceiver.cpp b/src/network_systems/projects/remote_transceiver/test/test_remote_transceiver.cpp index 1625f50b7..8854db2c9 100644 --- a/src/network_systems/projects/remote_transceiver/test/test_remote_transceiver.cpp +++ b/src/network_systems/projects/remote_transceiver/test/test_remote_transceiver.cpp @@ -218,7 +218,8 @@ TEST_F(TestRemoteTransceiver, rockblockWebServerExample) curl = curl_easy_init(); if (curl != nullptr) { - curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:8100/?data=B&imei=300434065264590&username=myuser"); + curl_easy_setopt( + curl, CURLOPT_URL, "http://localhost:8100/?data=thisistestdata&ec=B&imei=300434065264590&username=myuser"); curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST"); diff --git a/src/network_systems/scripts/README.md b/src/network_systems/scripts/README.md index 8b902ed33..cff511a66 100644 --- a/src/network_systems/scripts/README.md +++ b/src/network_systems/scripts/README.md @@ -28,8 +28,13 @@ This web server is a virtual implementation of the Rockblock Web Servers used to [here](https://docs.groundcontrol.com/iot/rockblock/web-services/sending-mt-message). This virtual server follows this specification with one notable difference, it will return the specific error code that -is contained in the `data` section of the request sent to it. +is contained in the `ec` section of the request sent to it. -So a request to this url: will return -`FAILED,11, No RockBLOCK with this IMEI found on your account` since `data=B` and B is hex for 11 which is the +So a request to this url: will +return `FAILED,11, No RockBLOCK with this IMEI found on your account` since `ec=B` and B is hex for 11 which is the corresponding error code as mentioned above. + +Continuing on, the data parameter now implements the functionality for any form of data to be handled by the server. +This data will be written to `TEMP_FILE_PATH` so that any tests can properly verify that the rockblock web server will +have received the correct data. So as above, if all goes well, `thisistestdata` should be written to the file located +at `TEMP_FILE_PATH`. diff --git a/src/network_systems/scripts/rockblock_web_server.py b/src/network_systems/scripts/rockblock_web_server.py index f1dfd63da..a8825ccda 100644 --- a/src/network_systems/scripts/rockblock_web_server.py +++ b/src/network_systems/scripts/rockblock_web_server.py @@ -1,5 +1,6 @@ import http.server import logging +import os import random import signal import socketserver @@ -24,6 +25,8 @@ logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s") logger = logging.getLogger(__name__) +TEMP_FILE_PATH = "/tmp/remote_transceiver/downstream_test.log" + # Custom HTTP request handler class MyHandler(http.server.BaseHTTPRequestHandler): @@ -31,22 +34,23 @@ class MyHandler(http.server.BaseHTTPRequestHandler): def do_POST(self): logger.debug("Received POST request.") # Extract data parameter from the request - data = self.get_data() + data, error_code = self.get_data() logger.debug(f"Extracted data: {data}") - # Check if data is not empty if data: try: - # Decode the hex data - error_code = int(data, 16) - logger.debug(f"Decoded error code: {error_code}") - except ValueError: - # If decoding fails, return error code 99 - logger.error("Failed to decode data. Using error code 99.") - error_code = 99 - else: - # Default error code if data is empty - logger.warning("No data found. Using error code 99.") + os.makedirs(os.path.dirname(TEMP_FILE_PATH), exist_ok=True) + with open(TEMP_FILE_PATH, "w") as f: + f.write(data) + logger.info(f"Data written to {TEMP_FILE_PATH}") + logger.info(f"Data written is: {data}") + except Exception as e: + logger.error(f"Failed to write data to file: {e}") + + try: + error_code = int(error_code, 16) if error_code else 99 + except ValueError: + logger.error("Failed to decode ec parameter. Using error code 99.") error_code = 99 # Handle error codes and special case for error_code 0 @@ -75,7 +79,9 @@ def get_data(self): """Extracts the data parameter from the URL query string (e.g., ?data=48656C6C6F)""" query = parse.urlsplit(self.path).query params = dict(parse.parse_qsl(query)) - return params.get("data", "") + data = params.get("data", "") + error_code = params.get("ec", "") + return data, error_code # Set up the server