blob: ce3d788a8a59167effcb72977c4d812f5df7b5a2 [file] [log] [blame]
Martin Roth1b18f0f2019-03-07 21:59:531#!/bin/bash
2# Copyright 2019 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6VERSION="0.01"
7
8#Flags & Parameters
9SHOW_USAGE="" # Show the usage for the tool
10VERBOSE="" # Show verbose output
11
12# Servo USB Device ID values
13GOOGLE_VENDOR_ID="18d1"
14SERVO_V2_DEVICE_ID="5002"
15SERVO_V4_DEVICE_ID="501b"
16SERVO_MICRO_DEVICE_ID="501a"
17SUZY_Q_CABLE_DEVICE_ID="501f"
18SUZY_Q_DEVICE_ID="5014" # Actually CR50, but report as Suzy Q-connected to CR50
19
20# Text STYLE variables
21RED='\033[38;5;9m'
22GREEN='\033[38;5;2m'
23ORANGE_RED="\033[38;5;202m"
24C_DODGERBLUE2="\033[38;5;27m"
25NC='\033[0m' # No Color
26
27_printf_color() {
28 printf "${1}%s" "$2"
29}
30
31_echo_color() {
32 printf "${1}%s${NC}\n" "$2"
33}
34
35_echo_error() {
36 (_echo_color >&2 "${RED}" "$*")
37}
38
39_echo_debug() {
40 if [ -n "${VERBOSE}" ]; then
41 (_echo_color >&2 "${ORANGE_RED}" "$*")
42 fi
43}
44
45# Checks that a program is installed. Prints an optional message on failure.
46_checkutil() {
47 local util="$1"
48 local printstring="$2"
49
50 if ! command -v "${util}" >/dev/null 2>&1; then
51 _echo_error "Error: ${util} not found in path."
52 if [ -n "$printstring" ]; then
53 _echo_error "$printstring"
54 fi
55 exit 1
56 fi
57}
58
59_show_dev() {
60 local servo_type="$1"
61 local portnum="$2"
62 local serialnum="$3"
63 local fw_version="$4"
64
65 if [ -n "${portnum}" ]; then
66 _printf_color "${GREEN}" " ${servo_type} running servod."
67 else
68 _printf_color "${C_DODGERBLUE2}" " ${servo_type} -"
69 fi
70
71 if [ -n "${fw_version}" ]; then
72 _printf_color "" " Firmware Version: ${fw_version}"
73 fi
74
75 if [ -n "${portnum}" ]; then
76 _printf_color "" " --port ${portnum}"
77 fi
78
79 if [ -n "${serialnum}" ]; then
80 _printf_color "" " --serialname ${serialnum}"
81 fi
82
83 _echo_color "${NC}" ""
84}
85
86get_servo_info() {
87 local lsusb_output
88 local servod_running
89
90 # Verify that lsusb is installed
91 _checkutil lsusb "Please install with your package manager before continuing." || exit 127
92
93 # Get USB attached devices
94 lsusb_output="$(lsusb | grep "ID ${GOOGLE_VENDOR_ID}:")"
95 servod_running="$(pgrep -a servod)"
96 _echo_debug "Debug Output:"
97 _echo_debug "${lsusb_output}"
98 _echo_debug "${servod_running}"
99
100 servo_v2_attached="$(echo -n "${lsusb_output}" | grep ":${SERVO_V2_DEVICE_ID}" | grep -c "^")"
101 servo_micro_attached="$(echo -n "${lsusb_output}" | grep ":${SERVO_MICRO_DEVICE_ID}" | grep -c "^")"
102 servo_v4_attached="$(echo -n "${lsusb_output}" | grep ":${SERVO_V4_DEVICE_ID}" | grep -c "^")"
103 suzy_q_attached="$(echo -n "${lsusb_output}" | grep ":${SUZY_Q_DEVICE_ID}" | grep -c "^")"
104 suzy_q_cables="$(echo -n "${lsusb_output}" | grep ":${SUZY_Q_CABLE_DEVICE_ID}" | grep -c "^")"
105 suzy_q_detached=$((suzy_q_cables - suzy_q_attached))
106 SERVOS_ATTACHED=$((servo_v2_attached + servo_micro_attached + servo_v4_attached + suzy_q_attached))
107
108 if [ "${suzy_q_detached}" -ne 0 ]; then
109 _echo_error "Detected ${suzy_q_detached} SuzyQables not attached to a device."
110 fi
111 if [ "${SERVOS_ATTACHED}" -eq 0 ]; then
112 _echo_error "No servos detected"
113 exit 0
114 else
115 echo "Found ${SERVOS_ATTACHED} servo devices."
116 fi
117 echo
118
119 # Loop through all the servo types and see if any are detected.
120 # If any are, see if they match the current port or serialnumber (serialname?)
121 for servo_vals in "servo_v2:${SERVO_V2_DEVICE_ID}" "servo_micro:${SERVO_MICRO_DEVICE_ID}" \
122 "suzy-q:${SUZY_Q_DEVICE_ID}" "servo_v4:${SERVO_V4_DEVICE_ID}"; do
123 local servo_device_id=${servo_vals#*:}
124 local servo_type=${servo_vals%:*}
125
126 # Move on to next servo type if none of this type are detected.
127 if [ "$(echo -n "${lsusb_output}" | grep ":${servo_device_id}" | grep -c "^")" -eq 0 ]; then
128 continue
129 fi
130
131 echo "Detected ${servo_type}. (Vendor/Device = ${GOOGLE_VENDOR_ID}:${servo_device_id})"
132 # Loop through all detected devices of this type
133 for dev in $(echo "${lsusb_output}" | grep ":${servo_device_id}" | sed 's/://' | awk '{ print $2 ":" $4 }'); do
134 local portnum
135 local serialnum
136 local firmware_version
137 local devinfo
138
139 devinfo="$(lsusb -v -s "${dev}")"
140 serialnum="$(echo "${devinfo}" | grep iSerial | awk '{ print $3 }')"
141 portnum="$(echo -n "${servod_running}" | grep "${serialnum}" | head -n 1 | sed 's/.*--port.//' | cut -f1 -d ' ')"
142 firmware_version="$(echo "${devinfo}" | grep iConfiguration | awk '{ print $3 }')"
143 _show_dev "${servo_type}" "${portnum}" "${serialnum}" "${firmware_version}"
144 _echo_debug "${devinfo}"
145 done
146 echo
147 done
148
149 if [ "$(printf "%s" "${servod_running}" | wc -l)" -ne 0 ]; then
150 echo
151 echo "Running servod instances"
152 echo "${servod_running}"
153 fi
154 echo
155}
156
157myversion() {
158 echo "servoinfo version ${VERSION}"
159}
160
161usage() {
162 echo "${0} [options]"
163 echo
164 echo "Options:"
165 echo " V | version - Print version and exit"
166 echo " h | help - Print help and exit"
167 echo " d | debug - Set verbose output"
168 echo
169}
170
171################################################################################
172# Get arguments
173args=$(getopt -l version,help,debug -o Vhd -- "$@")
174getopt_ret=$?
175eval set -- "${args}"
176
177if [ ${getopt_ret} != 0 ]; then
178 SHOW_USAGE=1
179fi
180
181while true; do
182 case "$1" in
183 -V | --version)
184 shift
185 myversion
186 exit 0
187 ;;
188 -h | --help)
189 shift
190 SHOW_USAGE=1
191 ;;
192
193 -d | --debug)
194 shift
195 VERBOSE=1
196 ;;
197 --)
198 shift
199 break
200 ;;
201 *) break ;;
202 esac
203done
204
205if [ "${SHOW_USAGE}" ]; then
206 usage
207 exit 0
208fi
209
210get_servo_info