Skip to content

Commit

Permalink
Update ody-connect.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
jr1221 committed Apr 14, 2024
1 parent c4f1283 commit 5f06b3f
Showing 1 changed file with 43 additions and 27 deletions.
70 changes: 43 additions & 27 deletions dev/scripts/ody-connect.sh
Original file line number Diff line number Diff line change
@@ -1,34 +1,50 @@
#!/bin/bash

# this script has to be sourced
# this script has to be sourced!!!

if [[ -z "${USBIP_PORT}" || -z "${PROJECT_NAME}" ]]
then
echo "This project doesnt use USB/IP"
return 1
fi
ody_connect_main() {
if [[ -z "${USBIP_PORT}" || -z "${PROJECT_NAME}" ]]
then
echo "This project doesnt use USB/IP"
return 1
fi

if ls /dev/tty* | grep -q "$PROJECT_NAME" && [[ ! $(wc -l <(usbip port) | head -c 1) -eq 2 ]]
then
echo "Found /dev/tty$PROJECT_NAME, setting it to be used by container"
DEBUG_PORT="/dev/tty$PROJECT_NAME"
return 0
else
echo "Didn't find existing tty USB/IP connected"
rm "/dev/tty$PROJECT_NAME"
fi
# check if symlink exits and can be resolved to the actual device
if [ -L "/dev/tty$PROJECT_NAME" ] && [ -e "/dev/tty$PROJECT_NAME" ]
then
echo "Found /dev/tty$PROJECT_NAME, setting it to be used by container"
DEBUG_PORT="/dev/tty$PROJECT_NAME"
return 0
else
echo "Didn't find existing tty USB/IP connected"
rm "/dev/tty$PROJECT_NAME"
fi


PREV_LIST_OF_TARGETS=$(ls /dev/tty*)
# do not allow inherit of prev targets
local prev_targets=$(ls /dev/tty*)

if usbip attach -r 192.168.100.12 -b "$USBIP_PORT"
then
echo "Successfully attached, linking to /dev/tty$PROJECT_NAME"
# find the new target in the dev fs and symlink it to project name
ln -s /dev/tty$(diff -n <(echo "$PREV_LIST_OF_TARGETS") <(ls /dev/tty*) | tail -n 1) /dev/tty"$PROJECT_NAME"
DEBUG_PORT="/dev/tty$PROJECT_NAME"
else
echo "Failed to connect, please see below if any USB devices are available"
usbip list -r 192.168.100.12
return 1
fi
if usbip attach -r 192.168.100.12 -b "$USBIP_PORT"
then
echo "Successfully attached, linking to /dev/tty$PROJECT_NAME"

# add a sleep time due to a race condition of the population of dev
sleep 5s

# find the new target in the dev fs and symlink it to project name by diffing it
ln -s $(diff -n <(echo "$prev_targets") <(ls /dev/tty*) | tail -n 1) "/dev/tty$PROJECT_NAME"

# set new debug port variable (inherited)
DEBUG_PORT="/dev/tty$PROJECT_NAME"

echo "Done!"
else
echo "Failed to connect, please see below if any USB devices are available"
usbip list -r 192.168.100.12
return 1
fi
}

# run kmain then remove it from env
ody_connect_main
unset -f ody_connect_main

0 comments on commit 5f06b3f

Please sign in to comment.