Skip to content

Commit 8d393b9

Browse files
committed
harden live compare host detection
1 parent 9b14b8f commit 8d393b9

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

scripts/run_live_network_compare.sh

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ MQTT_PORT="${MQTT_PORT:-1883}"
2121
MQTT_TOPIC="${MQTT_TOPIC:-zerokernel/live/telemetry}"
2222
SSID="${SSID:-sayaganteng}"
2323
PASSWORD="${PASSWORD:-sayaganteng}"
24+
HTTP_HOST_OVERRIDE="${HTTP_HOST:-}"
25+
MQTT_HOST_OVERRIDE="${MQTT_HOST:-}"
26+
HOST_IP_OVERRIDE="${HOST_IP:-}"
2427
HTTP_CAPTURE_SECONDS="${HTTP_CAPTURE_SECONDS:-14}"
2528
CAPTURE_START_DELAY="${CAPTURE_START_DELAY:-2}"
2629
MQTT_CAPTURE_COUNT="${MQTT_CAPTURE_COUNT:-50}"
@@ -57,7 +60,20 @@ case "${BOARD}" in
5760
;;
5861
esac
5962

60-
HOST_IP="$(ip route get 1.1.1.1 2>/dev/null | awk '/src/ {print $7; exit}')"
63+
ACTIVE_WIFI_DEVICE="$(nmcli -t -f DEVICE,TYPE,STATE,CONNECTION device status 2>/dev/null | awk -F: '$2=="wifi" && $3=="connected" {print $1; exit}')"
64+
ACTIVE_WIFI_CONNECTION="$(nmcli -t -f DEVICE,TYPE,STATE,CONNECTION device status 2>/dev/null | awk -F: '$2=="wifi" && $3=="connected" {print $4; exit}')"
65+
ACTIVE_WIFI_IP=""
66+
if [[ -n "${ACTIVE_WIFI_DEVICE}" ]]; then
67+
ACTIVE_WIFI_IP="$(ip -o -4 addr show dev "${ACTIVE_WIFI_DEVICE}" 2>/dev/null | awk '{print $4}' | cut -d/ -f1 | head -n1)"
68+
fi
69+
70+
HOST_IP="${HOST_IP_OVERRIDE}"
71+
if [[ -z "${HOST_IP}" ]]; then
72+
HOST_IP="${ACTIVE_WIFI_IP}"
73+
fi
74+
if [[ -z "${HOST_IP}" ]]; then
75+
HOST_IP="$(ip route get 1.1.1.1 2>/dev/null | awk '/src/ {print $7; exit}')"
76+
fi
6177
if [[ -z "${HOST_IP}" ]]; then
6278
HOST_IP="$(hostname -I | awk '{print $1}')"
6379
fi
@@ -66,6 +82,15 @@ if [[ -z "${HOST_IP}" ]]; then
6682
exit 1
6783
fi
6884

85+
HTTP_HOST_VALUE="${HTTP_HOST_OVERRIDE:-${HOST_IP}}"
86+
MQTT_HOST_VALUE="${MQTT_HOST_OVERRIDE:-${HOST_IP}}"
87+
88+
if [[ -z "${HTTP_HOST_OVERRIDE}" && -z "${MQTT_HOST_OVERRIDE}" && -n "${ACTIVE_WIFI_CONNECTION}" && "${ACTIVE_WIFI_CONNECTION}" != "${SSID}" ]]; then
89+
echo "warning: host Wi-Fi is '${ACTIVE_WIFI_CONNECTION}' while board target SSID is '${SSID}'." >&2
90+
echo "warning: local HTTP/MQTT endpoints on ${HOST_IP} may not be reachable from the board." >&2
91+
echo "warning: set HOST_IP/HTTP_HOST/MQTT_HOST explicitly or join the laptop to the target SSID for true end-to-end validation." >&2
92+
fi
93+
6994
write_secrets() {
7095
local target="$1"
7196
cat >"${target}" <<EOF
@@ -74,10 +99,10 @@ write_secrets() {
7499
75100
static const char* kWiFiSsid = "${SSID}";
76101
static const char* kWiFiPassword = "${PASSWORD}";
77-
static const char* kHttpHost = "${HOST_IP}";
102+
static const char* kHttpHost = "${HTTP_HOST_VALUE}";
78103
static const uint16_t kHttpPort = ${HTTP_PORT};
79104
static const char* kHttpPath = "/api/data";
80-
static const char* kMqttHost = "${HOST_IP}";
105+
static const char* kMqttHost = "${MQTT_HOST_VALUE}";
81106
static const uint16_t kMqttPort = ${MQTT_PORT};
82107
static const char* kMqttTopic = "${MQTT_TOPIC}";
83108
@@ -186,7 +211,7 @@ raise SystemExit(1)
186211
PY
187212
}
188213

189-
echo "Using host IP ${HOST_IP} for HTTP and MQTT"
214+
echo "Using host targets HTTP=${HTTP_HOST_VALUE}:${HTTP_PORT} MQTT=${MQTT_HOST_VALUE}:${MQTT_PORT}"
190215

191216
echo "Running live baseline on ${BOARD} (${PORT})"
192217
compile_and_upload "${ROOT_DIR}/examples/LiveNetworkBaseline" "${BASELINE_BUILD_LOG}"

0 commit comments

Comments
 (0)