48 lines
1.2 KiB
Bash
48 lines
1.2 KiB
Bash
#!/bin/sh
|
|
|
|
# The application supplies its own PID. Refuse broad or malformed targets.
|
|
TARGET_PID="$1"
|
|
LASER_DEVICE="${2:-/dev/ttyS1}"
|
|
LASER_BAUD="${3:-9600}"
|
|
|
|
turn_off_laser() {
|
|
if [ ! -c "$LASER_DEVICE" ]; then
|
|
echo "[CHARGE] laser serial device not found: $LASER_DEVICE" >&2
|
|
return 1
|
|
fi
|
|
|
|
stty -F "$LASER_DEVICE" "$LASER_BAUD" raw -echo 2>/dev/null || return 1
|
|
printf '\252\000\001\276\000\001\000\000\300' > "$LASER_DEVICE"
|
|
}
|
|
|
|
case "$TARGET_PID" in
|
|
''|*[!0-9]*)
|
|
echo "[CHARGE] invalid application pid: $TARGET_PID" >&2
|
|
exit 2
|
|
;;
|
|
esac
|
|
|
|
if [ "$TARGET_PID" -le 1 ]; then
|
|
echo "[CHARGE] refusing to terminate pid: $TARGET_PID" >&2
|
|
exit 2
|
|
fi
|
|
|
|
# First request laser-off while the application still owns the initialized UART.
|
|
turn_off_laser || true
|
|
|
|
kill -TERM "$TARGET_PID" 2>/dev/null || true
|
|
|
|
# Wait up to two seconds for a graceful exit, then force termination.
|
|
WAIT_COUNT=0
|
|
while kill -0 "$TARGET_PID" 2>/dev/null && [ "$WAIT_COUNT" -lt 20 ]; do
|
|
sleep 0.1
|
|
WAIT_COUNT=$((WAIT_COUNT + 1))
|
|
done
|
|
if kill -0 "$TARGET_PID" 2>/dev/null; then
|
|
kill -KILL "$TARGET_PID" 2>/dev/null || true
|
|
sleep 0.1
|
|
fi
|
|
|
|
# Send laser-off again after the application releases the UART.
|
|
turn_off_laser || true
|