So....ich habe jetzt noch ein paar Stunden gegoogelt und probiert. Ich bin auf folgende Seite gestossen:
http://www.whizzy.org/2012/11/device-co ... ibcec-ftw/
Dort wurden zwei Scripte verwendet. Ein Server und ein Client. Der Server sorgt dafür, dass die Verbindung zum cec-client immer aufrecht gehalten wird. Der Client dient in dem Beispiel nur zum senden der Befehle.
Ich habe mich an den Scripten versucht. Ich habe noch eine zweite "named Pipe" hinzugefügt (die kannte ich bisher noch gar nicht) und bekomme beim Client auch eine Ausgabe mit "cat $CECFIFOBACK":
cecserver.sh
Code: Select all
#!/bin/bash
#
# Set up a fifo and connect cec-client to it
#
# By Will Cooke. http://www.whizzy.org
# It's a very hacky solution, but it seems to just about work.
#
# Version 1. Does the job. November 2012
# Modified by Bismosa 26.01.2015
CECLOG=/tmp/cec.log
CECDEV=/dev/ttyACM0
CECFIFO=/tmp/cec.fifo
CECFIFOBACK=/tmp/cecback.fifo
CECCLIENT="/usr/local/bin/cec-client " #-d 8 -p 1 -b 5 -t p -o MythTV -f $CECLOG $CECDEV"
log(){
echo "SERVER: $1" >> $CECLOG
}
stop(){
# Kill the right proceses
log "Begin shutting down..."
# Using this hacky grep so that we only match those tail processes looking
# at /dev/null rather than, say, syslog
declare -a TAILPIDS=(`ps aux | grep 'tailf /dev/null' | egrep -v grep | awk '{print $2}'`)
declare -a CATPIDS=(`ps aux | grep 'cat $CECFIFO' | egrep -v grep | awk '{print $2}'`)
if [ ${#TAILPIDS[@]} -gt 0 ]
then
# Found some old tail processes to kill
log "Found some tail processes..."
for i in "${TAILPIDS[@]}"
do
log "Killing $i"
kill $i
done
fi
if [ ${#CATPIDS[@]} -gt 0 ]
then
# Found some old cat processes to kill
# It's unlikely we will ever get in here, because the previous tail
# processes have been killed and so shut down this end of the pipe
# already.
log "Found some cat processes..."
for i in "${CATPIDS[@]}"
do
log "Killing $i"
kill $i
done
fi
log "Asking cec-client to stop if it's running..."
# Using signal 2, the same as a ctrl-c
killall -s 2 cec-client 2> $CECLOG
log "Trying to remove FIFO..."
rm $CECFIFO 2> $CECLOG
rm $CECFIFOBACK #2> $CECLOG
log "Done shutting down."
}
case "${1}" in
start|restart)
log "Starting server. Since only one server can run at a time, stopping first."
stop
log "Done stopping, now starting..."
log "Setting up FIFOs..."
# We use a FIFO to pass in CEC commands to the cec-client which comes
# with libcec.
mkfifo $CECFIFO
mkfifo $CECFIFOBACK
log "Open pipe for writing..."
# We use tailf /dev/null because it doesn't disconnect from stdin when
# put in the background and it doesn't cause any load when running.
tailf /dev/null > $CECFIFO &
echo "Opening pipe for reading and start cec-client..."
# Since we're writing to a log file anyway we don't need the output from
# cec-client. Put the whole thing in brackets to background the lot.
#(cat $CECFIFO | $CECCLIENT &) > /dev/null
(cat $CECFIFO | $CECCLIENT &) > $CECFIFOBACK &
log "Start up complete."
;;
stop)
stop
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
esac
exit 0
cecsimple.sh
Code: Select all
#!/bin/bash
#
# Execute some fairly simple CEC commands
# Can use the "server" if its already running for faster execution
# or will fall back to starting the cec-client in "single pass" mode.
#
# Another terrible hack from Will Cooke. http://www.whizzy.org
#
# Handy site: http://www.cec-o-matic.com/
#
# Version 1. Seems to work. Nov. 2012
# Modified by Bismosa 26.01.2015
CECLOG=/tmp/cec.log
CECDEV=/dev/ttyACM0
CECFIFO=/tmp/cec.fifo
CECCLIENT="/usr/local/bin/cec-client -s -d 8 -p 1 -b 5 -t p -o MythTV $CECDEV"
CECFIFOBACK=/tmp/cecback.fifo
log(){
echo "SIMPLE CLIENT: $1" >> $CECLOG
}
send_command(){
if [ $CECCLIENTAVAIL == true ]
then
# We've tested for the "server", and it seems to be running
# Basically we dont have to start cec-client from scratch which
# saves about 4 seconds, so things like volume control are a bit
# more responsive.
log "Using server to send CEC packets $1 ..."
echo $1 > $CECFIFO
while read line
do
echo $line
done <$CECFIFOBACK
#echo `cat $CECFIFOBACK`
else
# Server wasn't found to be running, so start cec-client
# just for this one command.
# Why is this here? Well, sometimes when you come out of suspend
# the cec-client, and so the "server" drops the connection to the
# USB CEC device and so quits. This means that we can always send
# commands regarless of the state of the server.
log "Using single run cec-client to send packets ( $1 ) ..."
echo $1 | $CECCLIENT
fi
}
# We should check if the server is running, because if it is we should use it.
CECCLIENTPID=`pidof cec-client`
if [ $? -lt 1 ]
then
# cec-client is /probably/ running ok
CECCLIENTAVAIL=true
log "Main server seems to be alive. We will use that instead."
else
CECCLIENTAVAIL=false
fi
# Here are the commands we know how to support
case "${1}" in
tvon)
# "on" is supported by cec-client, it's a kind of short cut to the
# hex codes. 0 is always the destination address of the TV.
send_command "on 0"
# Make sure something appears on the TV we've just switched on
xscreensaver-command -deactivate
;;
tvoff)
# "standby" is also supported by cec-client
send_command "standby 0"
;;
ampon)
#address 5 is the "audio system" in an HDMI network
send_command "on 5"
;;
ampoff)
# My Sony Amp doesn't support "standby" for some reason, so instead
# I poke it like this...
send_command "tx 45 44 6C"
# 45 means from 4 (me, the playback device) to 5(amp)
# 44 6C means "the user pressed the power off button, nap time"
;;
allon)
# address f is the broadcast address. Haven't actually tested this.
send_command "on f"
;;
alloff)
# same
send_command "standby f"
;;
activesrc)
# Me (4) to broadcast (f) -> I am now the active source, switch to me.
# 82 "switch to", 1100 = address 1.1.0.0 the first device on dev 1 (amp)
# 1.2.0.0 would be the 2nd sub device on device 1
# 2.1.0.0 would be the 1st sub device on device 2
send_command "tx 4F 82 11 00"
;;
mute)
# Me to TV -> user pressed mute
send_command "tx 40 44 43"
;;
volup)
# Me to TV -> user pressed vol up
send_command "tx 40 44 41"
;;
voldown)
# Me to TV -> user pressed vol down
send_command "tx 40 44 42"
;;
status)
# Me to TV -> user pressed vol down
send_command "pow 0"
;;
*)
echo $"Usage: $0 {tvon|tvoff|ampon|ampoff|allon|alloff|activesrc|mute|volup|voldown}"
exit 1
;;
esac
exit 0
Mein Problem ist jetzt, dass ich es noch nicht geschafft habe die Rückgabe vom Server in eine Variable zu bekommen. Beim lesen mit der Schleife bzw über cat bleibt die Ausführung des Scriptes hängen. Was kann ich tun, um nach der Ausgabe mein Script weiter laufen zu lassen? Wie kann ich das Ende der Pipe erkennen?
Ich denke das ist schon ein vielversprechender Ansatz...und geht genau in die Richtung die ich haben wollte
Gruß
Bismosa