From a4abd1a14764031ff60980ff2dc01a89bdcbf2c3 Mon Sep 17 00:00:00 2001 From: Koki Shinjo Date: Tue, 25 Jul 2023 11:55:12 +0900 Subject: [PATCH 1/6] Add network script and service --- .../scripts/deploy-scripts-and-services.sh | 15 +++ .../scripts/update-network-connection.sh | 117 ++++++++++++++++++ .../services/jsk-spot-utils-network.service | 12 ++ 3 files changed, 144 insertions(+) create mode 100755 jsk_spot_robot/jsk_spot_startup/scripts/deploy-scripts-and-services.sh create mode 100755 jsk_spot_robot/jsk_spot_startup/scripts/update-network-connection.sh create mode 100644 jsk_spot_robot/jsk_spot_startup/services/jsk-spot-utils-network.service diff --git a/jsk_spot_robot/jsk_spot_startup/scripts/deploy-scripts-and-services.sh b/jsk_spot_robot/jsk_spot_startup/scripts/deploy-scripts-and-services.sh new file mode 100755 index 0000000000..0d702f4cac --- /dev/null +++ b/jsk_spot_robot/jsk_spot_startup/scripts/deploy-scripts-and-services.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +PACKAGE_PATH=$(rospack find jsk_spot_startup) +SERVICE_SOURCE_PATH=$PACKAGE_PATH/services +SERVICE_DESTINATION_PATH=/etc/systemd/system + +# install systemd unit files +cd $SERVICE_SOURCE_PATH +for service_file in $(ls ./*); +do + sudo cp $service_file $SERVICE_DESTINATION_PATH/$service_file + sudo chmod 644 $SERVICE_DESTINATION_PATH/$service_file + sudo chown root:root $SERVICE_DESTINATION_PATH/$service_file +done +sudo systemctl daemon-reload diff --git a/jsk_spot_robot/jsk_spot_startup/scripts/update-network-connection.sh b/jsk_spot_robot/jsk_spot_startup/scripts/update-network-connection.sh new file mode 100755 index 0000000000..2e51b59c38 --- /dev/null +++ b/jsk_spot_robot/jsk_spot_startup/scripts/update-network-connection.sh @@ -0,0 +1,117 @@ +#!/bin/bash + +IF_ETH=enxa0cec875af37 +IF_LTE=enxf8b7975c750a +IF_WIFI=wlxc006c31b198c +WIFI_PROFILE=sanshiro + +function existIF() { + interface_name=$1 + for DEV in `find /sys/devices -name net | grep -v virtual` + do + if [ `ls $DEV/` = $interface_name ]; then + echo 0 + return 0 + fi + done + echo 1 + return 1 +} + +function connectETH() { + sudo ifmetric $IF_ETH 100 + sudo ifmetric $IF_LTE 101 + sudo ifmetric $IF_WIFI 101 +} + +function restartWIFI() { + sudo nmcli connection down $WIFI_PROFILE + sudo ip l set $IF_WIFI down + sudo ip l set $IF_WIFI up + sudo nmcli connection up $WIFI_PROFILE +} + +function connectWIFI() { + sudo ifmetric $IF_WIFI 100 + sudo ifmetric $IF_LTE 101 + sudo ifmetric $IF_ETH 102 +} + +function connectLTE() { + sudo ifmetric $IF_LTE 100 + sudo ifmetric $IF_WIFI 101 + sudo ifmetric $IF_ETH 102 +} + +function updateConnection() { + + echo "Connection updating." + + # Reconnect WIFI + if [ $(existIF $IF_WIFI) = 0 ]; then + ping -c 1 -W 1 1.1.1.1 -I $IF_WIFI + if [ $? = 0 ]; then + echo "wifi connected" + else + echo "wifi not connected. trying to connect $WIFI_PROFILE" + restartWIFI + fi + else + echo "wifi device not found. skipped wifi reconnection." + fi + + # Check internet connection + ping -c 1 -W 1 1.1.1.1 + if [ $? = 0 ]; then + echo "Network connected" + return 0 + else + echo "Network disconnected. Trying to reconnect." + fi + + # connect with Ethernet if available + if [ $(existIF $IF_ETH) = 0 ]; then + ping -c 1 -W 1 1.1.1.1 -I $IF_ETH + if [ $? = 0 ]; then + echo "Ethernet is online. switched to ethernet." + connectETH + return 0 + else + echo "Ethernet is offline." + fi + else + echo "No ethernet device is found." + fi + + # connect with Wi-Fi if available + if [ $(existIF $IF_WIFI) = 0 ]; then + ping -c 1 -W 1 1.1.1.1 -I $IF_WIFI + if [ $? = 0 ]; then + echo "Wifi is online. switched to wifi." + connectWIFI + return 0 + else + echo "Wifi is offline." + fi + else + echo "No wifi device is found." + fi + + # connect with LTE if available + if [ $(existIF $IF_LTE) = 0 ]; then + echo "Connection type switched to lte" + connectLTE + return 1 + else + echo "No lte device is found" + fi + + echo "No network device found." + return 1 +} + +while : +do + sleep 1 + updateConnection +done diff --git a/jsk_spot_robot/jsk_spot_startup/services/jsk-spot-utils-network.service b/jsk_spot_robot/jsk_spot_startup/services/jsk-spot-utils-network.service new file mode 100644 index 0000000000..9d554fec65 --- /dev/null +++ b/jsk_spot_robot/jsk_spot_startup/services/jsk-spot-utils-network.service @@ -0,0 +1,12 @@ +[Unit] +Description=JSK Network Service + +[Service] +Type=simple +ExecStart=/bin/bash -c ". /opt/ros/melodic/setup.bash && . /home/spot/spot_driver_ws/devel/setup.bash && rosrun jsk_spot_startup update-network-connection.sh" +ExecStop=/bin/kill -WINCH ${MAINPID} +KillMode=control-group +KillSignal=SIGTERM + +[Install] +WantedBy=multi-user.target From a944b741b3d2d48f392501624381327cf97c6316 Mon Sep 17 00:00:00 2001 From: Koki Shinjo Date: Thu, 3 Aug 2023 15:58:07 +0900 Subject: [PATCH 2/6] [jsk_spot_startup] update network script --- .../jsk_spot_startup/scripts/update-network-connection.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jsk_spot_robot/jsk_spot_startup/scripts/update-network-connection.sh b/jsk_spot_robot/jsk_spot_startup/scripts/update-network-connection.sh index 2e51b59c38..f4b5ab056d 100755 --- a/jsk_spot_robot/jsk_spot_startup/scripts/update-network-connection.sh +++ b/jsk_spot_robot/jsk_spot_startup/scripts/update-network-connection.sh @@ -2,7 +2,7 @@ IF_ETH=enxa0cec875af37 IF_LTE=enxf8b7975c750a -IF_WIFI=wlxc006c31b198c +IF_WIFI=wlxc006c31b1a80 WIFI_PROFILE=sanshiro function existIF() { From ba034084261036f0e2807dbf327206f5f3ec1873 Mon Sep 17 00:00:00 2001 From: Koki Shinjo Date: Mon, 7 Aug 2023 20:18:35 +0900 Subject: [PATCH 3/6] [jsk_spot_startup] update service for expriment --- .../jsk_spot_startup/services/jsk-spot-utils-network.service | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jsk_spot_robot/jsk_spot_startup/services/jsk-spot-utils-network.service b/jsk_spot_robot/jsk_spot_startup/services/jsk-spot-utils-network.service index 9d554fec65..8137664641 100644 --- a/jsk_spot_robot/jsk_spot_startup/services/jsk-spot-utils-network.service +++ b/jsk_spot_robot/jsk_spot_startup/services/jsk-spot-utils-network.service @@ -3,7 +3,7 @@ Description=JSK Network Service [Service] Type=simple -ExecStart=/bin/bash -c ". /opt/ros/melodic/setup.bash && . /home/spot/spot_driver_ws/devel/setup.bash && rosrun jsk_spot_startup update-network-connection.sh" +ExecStart=/bin/bash -c ". /opt/ros/melodic/setup.bash && . /home/sktometometo/ros/ws_main_extend/devel/setup.bash && rosrun jsk_spot_startup update-network-connection.sh" ExecStop=/bin/kill -WINCH ${MAINPID} KillMode=control-group KillSignal=SIGTERM From 6575f197894a4b80c82640870afd51bad62ef026 Mon Sep 17 00:00:00 2001 From: Koki Shinjo Date: Sun, 8 Oct 2023 12:41:56 +0900 Subject: [PATCH 4/6] update --- .../scripts/update-network-connection.sh | 101 ++++++++++-------- 1 file changed, 58 insertions(+), 43 deletions(-) diff --git a/jsk_spot_robot/jsk_spot_startup/scripts/update-network-connection.sh b/jsk_spot_robot/jsk_spot_startup/scripts/update-network-connection.sh index f4b5ab056d..d42e58661d 100755 --- a/jsk_spot_robot/jsk_spot_startup/scripts/update-network-connection.sh +++ b/jsk_spot_robot/jsk_spot_startup/scripts/update-network-connection.sh @@ -1,9 +1,10 @@ #!/bin/bash IF_ETH=enxa0cec875af37 -IF_LTE=enxf8b7975c750a IF_WIFI=wlxc006c31b1a80 +IF_LTE=enxf8b7975c750a WIFI_PROFILE=sanshiro +CURRENT_CONNECTION= function existIF() { interface_name=$1 @@ -20,21 +21,21 @@ function existIF() { function connectETH() { sudo ifmetric $IF_ETH 100 - sudo ifmetric $IF_LTE 101 sudo ifmetric $IF_WIFI 101 + sudo ifmetric $IF_LTE 101 } function restartWIFI() { sudo nmcli connection down $WIFI_PROFILE - sudo ip l set $IF_WIFI down - sudo ip l set $IF_WIFI up + #sudo ip l set $IF_WIFI down + #sudo ip l set $IF_WIFI up sudo nmcli connection up $WIFI_PROFILE } function connectWIFI() { sudo ifmetric $IF_WIFI 100 - sudo ifmetric $IF_LTE 101 sudo ifmetric $IF_ETH 102 + sudo ifmetric $IF_LTE 101 } function connectLTE() { @@ -43,75 +44,89 @@ function connectLTE() { sudo ifmetric $IF_ETH 102 } -function updateConnection() { - - echo "Connection updating." - - # Reconnect WIFI - if [ $(existIF $IF_WIFI) = 0 ]; then - ping -c 1 -W 1 1.1.1.1 -I $IF_WIFI - if [ $? = 0 ]; then - echo "wifi connected" - else - echo "wifi not connected. trying to connect $WIFI_PROFILE" - restartWIFI - fi - else - echo "wifi device not found. skipped wifi reconnection." - fi - - # Check internet connection - ping -c 1 -W 1 1.1.1.1 - if [ $? = 0 ]; then - echo "Network connected" - return 0 - else - echo "Network disconnected. Trying to reconnect." - fi - - # connect with Ethernet if available +function updateConnectionToETH() { if [ $(existIF $IF_ETH) = 0 ]; then - ping -c 1 -W 1 1.1.1.1 -I $IF_ETH + ping -c 1 -W 1 1.1.1.1 -I $IF_ETH > /dev/null 2>&1 if [ $? = 0 ]; then echo "Ethernet is online. switched to ethernet." + CURRENT_CONNECTION="ETH" connectETH - return 0 else - echo "Ethernet is offline." + echo "Ethernet is offline.: $?" fi else echo "No ethernet device is found." fi +} - # connect with Wi-Fi if available +function updateConnectionToWiFi() { if [ $(existIF $IF_WIFI) = 0 ]; then - ping -c 1 -W 1 1.1.1.1 -I $IF_WIFI + ping -c 1 -W 1 1.1.1.1 -I $IF_WIFI > /dev/null 2>&1 if [ $? = 0 ]; then echo "Wifi is online. switched to wifi." + CURRENT_CONNECTION="WiFi" connectWIFI - return 0 else - echo "Wifi is offline." + echo "Wifi is offline.: $?" fi else echo "No wifi device is found." fi +} - # connect with LTE if available +function updateConnectionToLTE() { if [ $(existIF $IF_LTE) = 0 ]; then echo "Connection type switched to lte" + CURRENT_CONNECTION="LTE" connectLTE - return 1 else echo "No lte device is found" fi echo "No network device found." - return 1 } +function updateConnection() { + + echo "Connection updating." + + # Reconnect WIFI + if [ $(existIF $IF_WIFI) = 0 ]; then + ping -c 1 -W 1 133.11.216.240 -I $IF_WIFI > /dev/null 2>&1 + if [ $? = 0 ]; then + echo "wifi connected" + else + echo "wifi not connected. trying to connect $WIFI_PROFILE" + restartWIFI + updateConnectionToWiFi + fi + else + echo "wifi device not found. skipped wifi reconnection." + fi + + # Check internet connection + ping -c 1 -W 1 1.1.1.1 > /dev/null 2>&1 + if [ $? = 0 ]; then + echo "Network connected" + return 0 + else + echo "Network disconnected. Trying to reconnect." + fi + + # First, try ethernet to connect with Ethernet if available + updateConnectionToETH + + # Second, try wifi to connect with Wi-Fi if available + updateConnectionToWiFi + + # connect with LTE if available + updateConnectionToLTE +} + +connectETH +CURRENT_CONNECTION="ETH" while : do - sleep 1 + sleep 5 updateConnection done From 9b6094174bbc73128d81d3993c298ca42c8ddfb2 Mon Sep 17 00:00:00 2001 From: Koki Shinjo Date: Sun, 8 Oct 2023 12:56:29 +0900 Subject: [PATCH 5/6] [jsk_spot_starutp] update update-network-connection.sh --- .../scripts/update-network-connection.sh | 57 +++++++++++++------ 1 file changed, 39 insertions(+), 18 deletions(-) diff --git a/jsk_spot_robot/jsk_spot_startup/scripts/update-network-connection.sh b/jsk_spot_robot/jsk_spot_startup/scripts/update-network-connection.sh index d42e58661d..a41443f0a8 100755 --- a/jsk_spot_robot/jsk_spot_startup/scripts/update-network-connection.sh +++ b/jsk_spot_robot/jsk_spot_startup/scripts/update-network-connection.sh @@ -19,12 +19,6 @@ function existIF() { return 1 } -function connectETH() { - sudo ifmetric $IF_ETH 100 - sudo ifmetric $IF_WIFI 101 - sudo ifmetric $IF_LTE 101 -} - function restartWIFI() { sudo nmcli connection down $WIFI_PROFILE #sudo ip l set $IF_WIFI down @@ -32,25 +26,36 @@ function restartWIFI() { sudo nmcli connection up $WIFI_PROFILE } -function connectWIFI() { +function updateRouteToETH() { + sudo ifmetric $IF_ETH 100 + sudo ifmetric $IF_WIFI 101 + sudo ifmetric $IF_LTE 101 +} + +function updateRouteToWIFI() { sudo ifmetric $IF_WIFI 100 sudo ifmetric $IF_ETH 102 sudo ifmetric $IF_LTE 101 } -function connectLTE() { +function updateRouteToLTE() { sudo ifmetric $IF_LTE 100 sudo ifmetric $IF_WIFI 101 sudo ifmetric $IF_ETH 102 } +# get default route interface name which has most priority +function get_default_route() { + ip route | grep default | awk '{print $5}' | head -n 1 +} + function updateConnectionToETH() { if [ $(existIF $IF_ETH) = 0 ]; then ping -c 1 -W 1 1.1.1.1 -I $IF_ETH > /dev/null 2>&1 if [ $? = 0 ]; then echo "Ethernet is online. switched to ethernet." - CURRENT_CONNECTION="ETH" - connectETH + CURRENT_CONNECTION=$IF_ETH + updateRouteToETH else echo "Ethernet is offline.: $?" fi @@ -64,8 +69,8 @@ function updateConnectionToWiFi() { ping -c 1 -W 1 1.1.1.1 -I $IF_WIFI > /dev/null 2>&1 if [ $? = 0 ]; then echo "Wifi is online. switched to wifi." - CURRENT_CONNECTION="WiFi" - connectWIFI + CURRENT_CONNECTION=$IF_WIFI + updateRouteToWIFI else echo "Wifi is offline.: $?" fi @@ -77,8 +82,8 @@ function updateConnectionToWiFi() { function updateConnectionToLTE() { if [ $(existIF $IF_LTE) = 0 ]; then echo "Connection type switched to lte" - CURRENT_CONNECTION="LTE" - connectLTE + CURRENT_CONNECTION=$IF_LTE + updateRouteToLTE else echo "No lte device is found" fi @@ -87,12 +92,28 @@ function updateConnectionToLTE() { } function updateConnection() { - echo "Connection updating." + # Check default route is current connection + default_route=$(get_default_route) + if [ $default_route = $CURRENT_CONNECTION ]; then + echo "Current connection is default route. No need to update." + else + echo "Current connection is not default route. Trying to update." + if [ $CURRENT_CONNECTION = $IF_ETH ]; then + updateConnectionToETH + elif [ $CURRENT_CONNECTION = $IF_WIFI ]; then + updateConnectionToWiFi + elif [ $CURRENT_CONNECTION = $IF_LTE ]; then + updateConnectionToLTE + else + echo "Unknown current connection: $CURRENT_CONNECTION" + fi + if + # Reconnect WIFI if [ $(existIF $IF_WIFI) = 0 ]; then - ping -c 1 -W 1 133.11.216.240 -I $IF_WIFI > /dev/null 2>&1 + ping -c 1 -W 1 1.1.1.1 -I $IF_WIFI > /dev/null 2>&1 if [ $? = 0 ]; then echo "wifi connected" else @@ -123,8 +144,8 @@ function updateConnection() { updateConnectionToLTE } -connectETH -CURRENT_CONNECTION="ETH" +updateRouteToETH +CURRENT_CONNECTION=$IF_ETH while : do sleep 5 From a06c88d49ac408d56d0756d5617c42bc8eaf6ae5 Mon Sep 17 00:00:00 2001 From: Koki Shinjo Date: Sun, 8 Oct 2023 12:57:30 +0900 Subject: [PATCH 6/6] [jsk_spot_startup] update network script --- .../scripts/update-network-connection.sh | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/jsk_spot_robot/jsk_spot_startup/scripts/update-network-connection.sh b/jsk_spot_robot/jsk_spot_startup/scripts/update-network-connection.sh index a41443f0a8..c5edb7f505 100755 --- a/jsk_spot_robot/jsk_spot_startup/scripts/update-network-connection.sh +++ b/jsk_spot_robot/jsk_spot_startup/scripts/update-network-connection.sh @@ -97,9 +97,9 @@ function updateConnection() { # Check default route is current connection default_route=$(get_default_route) if [ $default_route = $CURRENT_CONNECTION ]; then - echo "Current connection is default route. No need to update." + echo "Current connection $CURRENT_CONNECTION is default route. No need to update." else - echo "Current connection is not default route. Trying to update." + echo "Current connection $CURRENT_CONNECTION is not default route. Trying to update." if [ $CURRENT_CONNECTION = $IF_ETH ]; then updateConnectionToETH elif [ $CURRENT_CONNECTION = $IF_WIFI ]; then @@ -109,13 +109,18 @@ function updateConnection() { else echo "Unknown current connection: $CURRENT_CONNECTION" fi - if + fi # Reconnect WIFI if [ $(existIF $IF_WIFI) = 0 ]; then ping -c 1 -W 1 1.1.1.1 -I $IF_WIFI > /dev/null 2>&1 if [ $? = 0 ]; then - echo "wifi connected" + if [ $CURRENT_CONNECTION = $IF_WIFI ]; then + echo "wifi connected" + else + echo "wifi connected. But current connection $CURRENT_CONNECTION is not wifi $IF_WIFI. So update" + updateConnectionToWiFi + fi else echo "wifi not connected. trying to connect $WIFI_PROFILE" restartWIFI @@ -144,7 +149,6 @@ function updateConnection() { updateConnectionToLTE } -updateRouteToETH CURRENT_CONNECTION=$IF_ETH while : do