2024-08-09 16:21:47 -03:00
|
|
|
#!/bin/bash
|
|
|
|
|
2024-08-21 10:48:40 -03:00
|
|
|
# entrypoint.sh
|
|
|
|
#
|
2024-08-22 13:17:24 -03:00
|
|
|
# This script is responsible for setting up and maintaining an XBee network interface within a Docker container.
|
2024-08-26 16:57:24 -03:00
|
|
|
# It checks if a device is connected to the specified port, creates a TAP network interface, and monitors the connection.
|
|
|
|
# If the device is disconnected, it cleans up and retries the connection process.
|
2024-08-21 10:48:40 -03:00
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
# - This script is intended to be run as the entrypoint for a Docker container.
|
|
|
|
# - Ensure that the script has executable permissions (`chmod +x entrypoint.sh`) before running it.
|
|
|
|
|
2024-08-26 16:57:24 -03:00
|
|
|
echo "Starting entrypoint.sh"
|
2024-08-16 13:14:54 -03:00
|
|
|
|
2024-08-26 16:57:24 -03:00
|
|
|
loop() {
|
|
|
|
while true; do
|
|
|
|
# Check if the XBee device is connected
|
|
|
|
check_device_port
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
echo "No XBee device found at $XBEE_PORT. Waiting for device connection..."
|
|
|
|
cleanup
|
|
|
|
continue
|
|
|
|
fi
|
2024-08-16 13:14:54 -03:00
|
|
|
|
2024-08-28 10:36:39 -03:00
|
|
|
sleep 0.5
|
|
|
|
|
2024-08-26 16:57:24 -03:00
|
|
|
# Check if the network interface is up
|
|
|
|
check_network_state
|
2024-08-28 13:43:03 -03:00
|
|
|
if [ $? -ne 0 ]; then
|
2024-08-26 16:57:24 -03:00
|
|
|
echo "Network interface $XBEE_NET_IFACE_NAME not found. Creating TAP interface..."
|
|
|
|
create_tap_interface
|
|
|
|
fi
|
2024-08-16 13:14:54 -03:00
|
|
|
|
2024-08-28 14:19:36 -03:00
|
|
|
sleep 0.5
|
|
|
|
|
2024-08-26 16:57:24 -03:00
|
|
|
# Log messages sent and received over xbnet
|
2024-08-28 14:19:36 -03:00
|
|
|
log_xbnet_messages
|
|
|
|
|
|
|
|
sleep 0.5
|
2024-08-26 16:57:24 -03:00
|
|
|
done
|
2024-08-16 13:14:54 -03:00
|
|
|
}
|
|
|
|
|
2024-08-26 16:57:24 -03:00
|
|
|
|
2024-08-16 13:14:54 -03:00
|
|
|
# Function to check if the XBee device is connected
|
2024-08-26 16:57:24 -03:00
|
|
|
check_device_port() {
|
2024-08-21 10:48:40 -03:00
|
|
|
if [ -e "$XBEE_PORT" ]; then
|
2024-08-16 13:14:54 -03:00
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2024-08-26 16:57:24 -03:00
|
|
|
# Function to check if the network interface is up
|
|
|
|
check_network_state() {
|
|
|
|
if ip link show $XBEE_NET_IFACE_NAME > /dev/null 2>&1; then
|
2024-08-16 13:14:54 -03:00
|
|
|
return 0
|
2024-08-26 16:57:24 -03:00
|
|
|
else
|
|
|
|
return 1
|
2024-08-16 13:14:54 -03:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2024-08-26 16:57:24 -03:00
|
|
|
# Function to create a TAP network interface
|
|
|
|
create_tap_interface() {
|
|
|
|
# Start xbnet and run it in the background
|
|
|
|
xbnet -d --serial-speed ${XBEE_BAUDRATE} ${XBEE_PORT} tap --iface-name ${XBEE_NET_IFACE_NAME} &
|
2024-08-16 13:14:54 -03:00
|
|
|
|
2024-08-26 16:57:24 -03:00
|
|
|
# Wait until the interface is created
|
|
|
|
while ! check_network_state; do
|
|
|
|
sleep 1
|
2024-08-16 13:14:54 -03:00
|
|
|
done
|
|
|
|
|
2024-08-28 14:19:36 -03:00
|
|
|
# Create and bring up xbnet interface
|
2024-08-28 13:43:03 -03:00
|
|
|
ip addr add $XBEE_NET_SRC_IP/24 dev $XBEE_NET_IFACE_NAME
|
|
|
|
ip link set $XBEE_NET_IFACE_NAME up
|
2024-08-28 10:36:39 -03:00
|
|
|
|
2024-08-28 14:19:36 -03:00
|
|
|
echo "-------------------------------------------"
|
|
|
|
echo "********** Tap interface created **********"
|
|
|
|
echo "-------------------------------------------"
|
2024-08-28 10:36:39 -03:00
|
|
|
echo "Name: ${XBEE_NET_IFACE_NAME}"
|
2024-08-28 13:43:03 -03:00
|
|
|
echo "Gateway: ${DEFAULT_GATEWAY}"
|
2024-08-28 10:36:39 -03:00
|
|
|
echo "IP: ${XBEE_NET_SRC_IP}"
|
|
|
|
echo "Device port: ${XBEE_PORT}"
|
|
|
|
echo "Device baudrate: ${XBEE_BAUDRATE}"
|
2024-08-28 14:19:36 -03:00
|
|
|
echo "-------------------------------------------"
|
2024-08-26 16:57:24 -03:00
|
|
|
return 0
|
2024-08-16 13:14:54 -03:00
|
|
|
}
|
|
|
|
|
2024-08-28 14:19:36 -03:00
|
|
|
# Function to log messages sent and received over xbnet using netstat
|
|
|
|
log_xbnet_messages() {
|
|
|
|
echo "Monitoring messages on $XBEE_NET_IFACE_NAME..."
|
|
|
|
|
|
|
|
# Continuously monitor the interface for packet statistics
|
|
|
|
while true; do
|
|
|
|
RX_PACKETS_BEFORE=$(cat /sys/class/net/$XBEE_NET_IFACE_NAME/statistics/rx_packets)
|
|
|
|
TX_PACKETS_BEFORE=$(cat /sys/class/net/$XBEE_NET_IFACE_NAME/statistics/tx_packets)
|
|
|
|
|
|
|
|
sleep 1
|
|
|
|
|
|
|
|
RX_PACKETS_AFTER=$(cat /sys/class/net/$XBEE_NET_IFACE_NAME/statistics/rx_packets)
|
|
|
|
TX_PACKETS_AFTER=$(cat /sys/class/net/$XBEE_NET_IFACE_NAME/statistics/tx_packets)
|
|
|
|
|
|
|
|
RX_DIFF=$((RX_PACKETS_AFTER - RX_PACKETS_BEFORE))
|
|
|
|
TX_DIFF=$((TX_PACKETS_AFTER - TX_PACKETS_BEFORE))
|
|
|
|
|
|
|
|
if [[ $RX_DIFF -gt 0 ]]; then
|
|
|
|
echo "$(date +'%Y-%m-%d %H:%M:%S') - $RX_DIFF packets received on $XBEE_NET_IFACE_NAME"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ $TX_DIFF -gt 0 ]]; then
|
|
|
|
echo "$(date +'%Y-%m-%d %H:%M:%S') - $TX_DIFF packets sent on $XBEE_NET_IFACE_NAME"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2024-08-26 16:57:24 -03:00
|
|
|
# Function to clean up resources and exit the script
|
|
|
|
cleanup() {
|
|
|
|
echo "Cleaning up resources..."
|
|
|
|
pkill -f "xbnet -d --serial-speed $XBEE_BAUDRATE $XBEE_PORT tap"
|
2024-08-16 13:14:54 -03:00
|
|
|
|
2024-08-26 16:57:24 -03:00
|
|
|
if ip link show $XBEE_NET_IFACE_NAME > /dev/null 2>&1; then
|
|
|
|
ip link set $XBEE_NET_IFACE_NAME down
|
|
|
|
ip link delete $XBEE_NET_IFACE_NAME
|
|
|
|
fi
|
2024-08-16 13:14:54 -03:00
|
|
|
}
|
|
|
|
|
2024-08-26 16:57:24 -03:00
|
|
|
# Trap signals to clean up properly
|
|
|
|
trap cleanup EXIT
|
|
|
|
|
|
|
|
# Start the loop to monitor the network and device
|
|
|
|
loop
|