serdevtry 1.5 KB

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