diff --git a/.env b/.env new file mode 100644 index 0000000..c9326dc --- /dev/null +++ b/.env @@ -0,0 +1,9 @@ +# Configuration for xbnet0 +XBEE0_PORT=/dev/ttyUSB1 +XBEE0_NET_IP=192.168.10.1 +PING0_TARGET=192.168.10.2 + +# Configuration for xbnet1 +XBEE1_PORT=/dev/ttyUSB2 +XBEE1_NET_IP=192.168.10.2 +PING1_TARGET=192.168.10.1 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..4a5c3a1 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,28 @@ +# Use an official Rust image as the base +FROM rust:latest + +# Install dependencies +RUN apt-get update && apt-get install -y \ + libudev-dev \ + iproute2 \ + iputils-ping \ + && rm -rf /var/lib/apt/lists/* + +# Clone the xbnet repository +RUN git clone https://github.com/jgoerzen/xbnet.git /usr/src/xbnet + +# Build xbnet +WORKDIR /usr/src/xbnet +RUN cargo build --release + +# Copy the built binary to /usr/local/bin +RUN cp target/release/xbnet /usr/local/bin/xbnet + +# Create the entrypoint script +RUN echo '#!/bin/bash\n\ +set -e\n\ +exec "$@"' > /entrypoint.sh \ + && chmod +x /entrypoint.sh + +ENTRYPOINT ["/entrypoint.sh"] +CMD ["bash"] diff --git a/README.md b/README.md index 4c11213..90063d3 100644 --- a/README.md +++ b/README.md @@ -2,17 +2,17 @@ - The README.md will be fully updated once the repo is in a stable state -# TODO: -- Need to automate rust project (xbnet) +## TODO +- Need to automate Rust project (xbnet) - Configure (looks good) - Build - Deploy - - On ubuntu for testing + - On Ubuntu for testing - On Linux for Tegra for TX2 - On Jetson Orin - Working on setup_host_device.sh - Issues with bridging interfaces - Need to add setup_end_device.sh -- Need dokcerize service -- Possibly may need to bridge docker xbnet network will local wifi interface as well +- Need to dockerize service +- Possibly may need to bridge docker xbnet network with local wifi interface as well - Make a submodule of xbnet, or copy over raw files, depending on future decisions \ No newline at end of file diff --git a/docker-compose-run-multiple.yml b/docker-compose-run-multiple.yml new file mode 100644 index 0000000..c1e7f6f --- /dev/null +++ b/docker-compose-run-multiple.yml @@ -0,0 +1,38 @@ +version: '3.8' + +services: + xbnet0: + build: . + container_name: xbnet0 + privileged: true + env_file: + - .env + devices: + - "${XBEE0_PORT}:${XBEE0_PORT}" + command: > + bash -c " + set -x; + xbnet ${XBEE0_PORT} tun; + ip addr add ${XBEE0_NET_IP}/24 dev xbnet0; + ip link set dev xbnet0 up; + ping -c 4 ${PING0_TARGET}; + sleep infinity + " + + xbnet1: + build: . + container_name: xbnet1 + privileged: true + env_file: + - .env + devices: + - "${XBEE1_PORT}:${XBEE1_PORT}" + command: > + bash -c " + set -x; + xbnet ${XBEE1_PORT} tun; + ip addr add ${XBEE1_NET_IP}/24 dev xbnet1; + ip link set dev xbnet1 up; + ping -c 4 ${PING1_TARGET}; + sleep infinity + " diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..573f3db --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,19 @@ +version: '3.8' + +services: + xbee-node: + build: . + container_name: xbee_node + privileged: true + env_file: + - .env + devices: + - "${XBEE0_PORT}:${XBEE0_PORT}" + command: > + bash -c " + xbnet ${XBEE0_PORT} tun; + ip addr add ${XBEE0_NET_IP}/24 dev xbnet0; + ip link set dev xbnet0 up; + ping -c 4 ${PING0_TARGET}; + sleep infinity + " diff --git a/test/test-run-cleanup.sh b/test/test-run-cleanup.sh new file mode 100644 index 0000000..382bfed --- /dev/null +++ b/test/test-run-cleanup.sh @@ -0,0 +1,56 @@ +#!/bin/bash + +# Function to display usage instructions +usage() { + echo "Usage: $0 -p xbee_port" + echo " -p xbee_port The serial port where the XBee is connected (e.g., /dev/ttyUSB0)" + exit 1 +} + +# Parse command-line arguments +while getopts "p:" opt; do + case "$opt" in + p) xbee_port=$OPTARG ;; + *) usage ;; + esac +done + +# Check if required arguments are provided +if [ -z "$xbee_port" ]; then + usage +fi + +# Find the xbnet interface created by xbnet +xbnet_interface=$(ip link show | grep -o 'xbnet[0-9]*') + +# Remove the IP address and bring down the network interface +if [ -n "$xbnet_interface" ]; then + echo "Removing IP address from $xbnet_interface..." + sudo ip addr flush dev $xbnet_interface + + echo "Bringing down the network interface $xbnet_interface..." + sudo ip link set dev $xbnet_interface down + + echo "Removing the network interface $xbnet_interface..." + sudo ip link delete $xbnet_interface +else + echo "No xbnet interface found." +fi + +# Remove the xbnet binary +if [ -f /usr/local/bin/xbnet ]; then + echo "Removing xbnet binary..." + sudo rm /usr/local/bin/xbnet +else + echo "xbnet binary not found." +fi + +# Remove the cloned xbnet repository +if [ -d /usr/src/xbnet ]; then + echo "Removing the cloned xbnet repository..." + sudo rm -rf /usr/src/xbnet +else + echo "xbnet repository not found." +fi + +echo "Cleanup complete." diff --git a/test/test-run.sh b/test/test-run.sh new file mode 100644 index 0000000..03e5485 --- /dev/null +++ b/test/test-run.sh @@ -0,0 +1,58 @@ +#!/bin/bash +# +# Usage: eg. ./test-run.sh -p /dev/ttyUSB0 -s 192.168.10.1 -d 192.168.10.2 +# + +# Function to display usage instructions +usage() { + echo "Usage: $0 -p xbee_port -s xbee_net_src_ip -d xbee_net_dst_ip" + echo " -p xbee_port The serial port where the XBee is connected (e.g., /dev/ttyUSB0)" + echo " -s xbee_net_src_ip The source IP address for the XBee network (e.g., 192.168.10.1)" + echo " -d xbee_net_dst_ip The destination IP address to ping (e.g., 192.168.10.2)" + exit 1 +} + +# Parse command-line arguments +while getopts "p:s:d:" opt; do + case "$opt" in + p) xbee_port=$OPTARG ;; + s) xbee_net_src_ip=$OPTARG ;; + d) xbee_net_dst_ip=$OPTARG ;; + *) usage ;; + esac +done + +# Check if all required arguments are provided +if [ -z "$xbee_port" ] || [ -z "$xbee_net_src_ip" ] || [ -z "$xbee_net_dst_ip" ]; then + usage +fi + +# Update package list and install necessary packages +echo "Installing necessary packages..." +sudo apt-get update +sudo apt-get install -y git build-essential libudev-dev iproute2 iputils-ping cargo + +# Clone the xbnet repository +echo "Cloning xbnet repository..." +git clone https://github.com/jgoerzen/xbnet.git /usr/src/xbnet + +# Build xbnet +echo "Building xbnet..." +cd /usr/src/xbnet +cargo build --release + +# Copy the built binary to /usr/local/bin +echo "Installing xbnet..." +sudo cp target/release/xbnet /usr/local/bin/xbnet + +# Configure the XBee network interface +echo "Configuring XBee network interface..." +sudo xbnet $xbee_port tun +sudo ip addr add $xbee_net_src_ip/24 dev xbnet0 +sudo ip link set dev xbnet0 up + +# Ping the destination IP to verify connectivity +echo "Pinging destination IP $xbee_net_dst_ip..." +ping -c 4 $xbee_net_dst_ip + +echo "Setup complete. The XBee network interface is configured and tested."