#!/usr/bin/env bash set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # shellcheck disable=SC1091 source "${SCRIPT_DIR}/common.sh" STEP="5g-dial" run_dial() { local rc export TERM="${TERM:-xterm}" export LANG="${LANG:-C.UTF-8}" export LC_ALL="${LC_ALL:-C.UTF-8}" blitz_log "${STEP}" "dial-env" "start" "TERM=${TERM} LANG=${LANG} LC_ALL=${LC_ALL} interface=${BLITZ_5G_INTERFACE}" 0 if blitz_run "${STEP}" "dial" python3 rndis_dial.py --serial-port "${BLITZ_5G_SERIAL_PORT}" --interface "${BLITZ_5G_INTERFACE}"; then return 0 fi rc=$? blitz_log "${STEP}" "dial-retry" "start" "first dial attempt failed rc=${rc}; retrying after 3s" "${rc}" sleep 3 blitz_run "${STEP}" "dial-retry" python3 rndis_dial.py --serial-port "${BLITZ_5G_SERIAL_PORT}" --interface "${BLITZ_5G_INTERFACE}" } wait_for_serial() { local serial_port="$1" local timeout_sec="$2" local waited=0 while (( waited < timeout_sec )); do if [[ -e "${serial_port}" ]]; then blitz_log "${STEP}" "wait-serial" "success" "serial_port=${serial_port} waited_sec=${waited}" 0 return 0 fi if (( waited == 0 || waited % 5 == 0 )); then blitz_log "${STEP}" "wait-serial" "waiting" "serial_port=${serial_port} waited_sec=${waited}" 0 fi sleep 1 waited=$(( waited + 1 )) done blitz_log "${STEP}" "wait-serial" "failure" "serial_port=${serial_port} timeout_sec=${timeout_sec}" 1 return 1 } wait_for_route() { local target_ip="$1" local timeout_sec="$2" local expected_interface="${3:-}" local waited=0 local route_output while (( waited < timeout_sec )); do route_output="$(blitz_route_ready "${target_ip}" "${expected_interface}" || true)" if [[ -n "${route_output}" ]]; then blitz_log "${STEP}" "route-check" "success" "target_ip=${target_ip} interface=${expected_interface:-auto} route=${route_output}" 0 return 0 fi if (( waited == 0 || waited % 5 == 0 )); then blitz_log "${STEP}" "route-check" "waiting" "target_ip=${target_ip} interface=${expected_interface:-auto} waited_sec=${waited}" 0 fi sleep 1 waited=$(( waited + 1 )) done blitz_log "${STEP}" "route-check" "failure" "target_ip=${target_ip} interface=${expected_interface:-auto} timeout_sec=${timeout_sec}" 1 return 1 } blitz_load_boot_env blitz_require_root "${STEP}" blitz_require_command ip "${STEP}" blitz_require_command python3 "${STEP}" blitz_require_file "${BLITZ_5G_DIAL_DIR}/rndis_dial.py" "${STEP}" if [[ -z "${BLITZ_TIME_SERVER_IP}" ]]; then blitz_log "${STEP}" "precheck" "failure" "BLITZ_TIME_SERVER_IP is empty and no fallback could be derived" 1 exit 1 fi if [[ -z "${BLITZ_5G_INTERFACE:-}" ]]; then blitz_log "${STEP}" "precheck" "failure" "BLITZ_5G_INTERFACE must not be empty" 1 exit 1 fi route_output="$(blitz_route_ready "${BLITZ_TIME_SERVER_IP}" "${BLITZ_5G_INTERFACE}" || true)" if [[ -n "${route_output}" ]]; then blitz_log "${STEP}" "dial" "already_up" "target_ip=${BLITZ_TIME_SERVER_IP} interface=${BLITZ_5G_INTERFACE} route=${route_output}" 0 exit 0 fi wait_for_serial "${BLITZ_5G_SERIAL_PORT}" "${BLITZ_5G_SERIAL_WAIT_SEC}" pushd "${BLITZ_5G_DIAL_DIR}" >/dev/null run_dial popd >/dev/null wait_for_route "${BLITZ_TIME_SERVER_IP}" "${BLITZ_5G_ROUTE_WAIT_SEC}" "${BLITZ_5G_INTERFACE}" blitz_log "${STEP}" "complete" "success" "5G dial completed and route is ready on ${BLITZ_5G_INTERFACE}" 0