summaryrefslogtreecommitdiff
path: root/scripts/contrib/serdevtry
blob: 74bd7b7161f3a7f829c6491d7608a48df50fe0b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/bin/sh

# Copyright (C) 2014 Intel Corporation
#
# Released under the MIT license (see COPYING.MIT)

if [ "$1" = "" -o "$1" = "--help" ] ; then
    echo "Usage: $0 <serial terminal command>"
    echo
    echo "Simple script to handle maintaining a terminal for serial devices that"
    echo "disappear when a device is powered down or reset, such as the USB"
    echo "serial console on the original BeagleBone (white version)."
    echo
    echo "e.g. $0 picocom -b 115200 /dev/ttyUSB0"
    echo
    exit
fi

args="$@"
DEVICE=""
while [ "$1" != "" ]; do
    case "$1" in
        /dev/*)
            DEVICE=$1
            break;;
    esac
    shift
done

if [ "$DEVICE" != "" ] ; then
    while true; do
        if [ ! -e $DEVICE ] ; then
            echo "serdevtry: waiting for $DEVICE to exist..."
            while [ ! -e $DEVICE ]; do
                sleep 0.1
            done
        fi
        if [ ! -w $DEVICE ] ; then
            # Sometimes (presumably because of a race with udev) we get to
            # the device before its permissions have been set up
            RETRYNUM=0
            while [ ! -w $DEVICE ]; do
                if [ "$RETRYNUM" = "2" ] ; then
                    echo "Device $DEVICE exists but is not writable!"
                    exit 1
                fi
                RETRYNUM=$((RETRYNUM+1))
                sleep 0.1
            done
        fi
        $args
        if [ -e $DEVICE ] ; then
            break
        fi
    done
else
    echo "Unable to determine device node from command: $args"
    exit 1
fi