serdevtry 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/bin/sh
  2. # Copyright (C) 2014 Intel Corporation
  3. #
  4. # Released under the MIT license (see COPYING.MIT)
  5. if [ "$1" = "" -o "$1" = "--help" ] ; then
  6. echo "Usage: $0 <serial terminal command>"
  7. echo
  8. echo "Simple script to handle maintaining a terminal for serial devices that"
  9. echo "disappear when a device is powered down or reset, such as the USB"
  10. echo "serial console on the original BeagleBone (white version)."
  11. echo
  12. echo "e.g. $0 picocom -b 115200 /dev/ttyUSB0"
  13. echo
  14. exit
  15. fi
  16. args="$@"
  17. DEVICE=""
  18. while [ "$1" != "" ]; do
  19. case "$1" in
  20. /dev/*)
  21. DEVICE=$1
  22. break;;
  23. esac
  24. shift
  25. done
  26. if [ "$DEVICE" != "" ] ; then
  27. while true; do
  28. if [ ! -e $DEVICE ] ; then
  29. echo "serdevtry: waiting for $DEVICE to exist..."
  30. while [ ! -e $DEVICE ]; do
  31. sleep 0.1
  32. done
  33. fi
  34. if [ ! -w $DEVICE ] ; then
  35. # Sometimes (presumably because of a race with udev) we get to
  36. # the device before its permissions have been set up
  37. RETRYNUM=0
  38. while [ ! -w $DEVICE ]; do
  39. if [ "$RETRYNUM" = "2" ] ; then
  40. echo "Device $DEVICE exists but is not writable!"
  41. exit 1
  42. fi
  43. RETRYNUM=$((RETRYNUM+1))
  44. sleep 0.1
  45. done
  46. fi
  47. $args
  48. if [ -e $DEVICE ] ; then
  49. break
  50. fi
  51. done
  52. else
  53. echo "Unable to determine device node from command: $args"
  54. exit 1
  55. fi