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