#!/bin/bash
AUTHKEYFILE=/etc/lgtv.authkey
SESSKEYFILE=/tmp/lgtv.sesskey
AUTHKEY=`cat $AUTHKEYFILE`
SESSKEY=`cat $SESSKEYFILE`
TVADDR=10.0.0.92
#echo "Authkey:$AUTHKEY"
#echo "Sesskey:$SESSKEY"
if [ "$AUTHKEY" == "" ]; then
#todo: request data
echo "No data in '$AUTHKEYFILE', please enter the sync number"
exit 1
fi
function formatxml(){
echo "$1"|sed s/'>'/'>|'/g | sed s/'<'/'|<'/g | tr -s '|' | tr '|' '\n'
}
function formatxml2(){
echo "$1"|sed s/'><'/'>|<'/g | sed s/'<\/'/'|<\/'/g | tr -s '|' | tr '|>' '\n=' |tr -d '<'
}
function formatxml3(){
formatxml2 "$1" | grep -v '^/' | grep -v '^?' | grep -v '=$' | grep -v '^$' | grep -v 'ROAPError=200'
}
function doauthreq(){
echo "Sending authorization..."
RES=`curl -s -H "content-type: application/atom+xml" http://$TVADDR:8080/roap/api/auth \
-d 'AuthKeyReq'`
formatxml3 "$RES"
SESSKEY="`formatxml3 "$RES" | grep '^session=' | cut -d '=' -f 2`"
echo "Session key: '$SESSKEY'"
echo "$SESSKEY" > $SESSKEYFILE
}
function doauth(){
echo "Sending authorization..."
RES=`curl -s -H "content-type: application/atom+xml" http://$TVADDR:8080/roap/api/auth \
-d 'AuthReq'$AUTHKEY''`
formatxml3 "$RES"
SESSKEY="`formatxml3 "$RES" | grep '^session=' | cut -d '=' -f 2`"
echo "Session key: '$SESSKEY'"
echo "$SESSKEY" > $SESSKEYFILE
}
function keyraw(){
echo "Keycode:$1"
echo -e -n "Sending...\r"
curl -s -H "content-type: application/atom+xml" http://$TVADDR:8080/roap/api/command -d "491244606HandleKeyInput$1"
}
#unauthorized:
#401Unauthorized
#authorization:
#200OK491244606
function key(){
#$1 = command to send
RES="`keyraw "$1"`"
formatxml3 "$RES"
if [ "`echo "$RES"|grep Unauthorized`" != "" ]; then
echo "UNAUTHORIZED!"
doauth
RES="`keyraw "$1"`"
formatxml3 "$RES"
fi
sleep 0.3
}
if [ "$AUTHKEY" == "" ]; then
#todo: request data
echo "Requesting pairing key from the TV..."
doauthreq
echo
echo "No data in '$AUTHKEYFILE', please put there the sync number shown on screen, without spaces."
echo "Use e.g. this command (replace 123456 with your key):"
echo " echo 123456 > $AUTHKEYFILE"
exit 1
fi
KEY=""
function remotekey(){
case "$1" in
# authorization communication
##Authorization:);;
authreq) doauthreq;;
auth) doauth;;
# individual keys
##Power:);;
off) key 1;;
##Digits:);;
0) key 2;;
1) key 3;;
2) key 4;;
3) key 5;;
4) key 6;;
5) key 7;;
6) key 8;;
7) key 9;;
8) key 10;;
9) key 11;;
##Cursor:);;
u) key 12;;
up) key 12;;
d) key 13;;
down) key 13;;
l) key 14;;
left) key 14;;
r) key 15;;
right) key 15;;
#16,17,18,19 - error: 400Bad Request
ok) key 20;;
home) key 21;;
setting) key 22;;
back) key 23;;
##Volume:);;
v+) key 24;;
v-) key 25;;
m) key 26;;
mute) key 26;;
c+) key 27;;
p+) key 27;;
c-) key 28;;
p-) key 28;;
v--) key 25;key 25;;
v---) key 25;key 25;key 25;;
v----) key 25;key 25;key 25;key 25;;
v++) key 24;key 24;;
v+++) key 24;key 24;key 24;;
v++++) key 24;key 24;key 24;key 24;;
p--) key 28;key 28;;
p---) key 28;key 28;key 28;;
p----) key 28;key 28;key 28;key 28;;
p++) key 27;key 27;;
p+++) key 27;key 27;key 27;;
p++++) key 27;key 27;key 27;key 27;;
##Other:);;
blue) key 29;;
green) key 30;;
red) key 31;;
yellow) key 32;;
play) key 33;;
pause) key 34;;
stop) key 35;;
ffwd) key 36;;
rew) key 37;;
skip) key 38;;
skipb) key 39;;
rec) key 40;;
reclst) key 41;;
repeat) key 42;;
livetv) key 43;;
epg) key 44;;
guide) key 44;;
info) key 45;;
aspect) key 46;;
extinput) key 47;;
pip) key 48;;
subtit) key 49;;
proglist) key 50;;
teletext) key 51;;
mark) key 52;;
#53,54,55... - bad request
#100..200 - bad request
install) key 207;;
instart) key 251;;
254) echo "POWER_ONLY suspended for now";;
ez_adj) key 255;;
3d) key 400;;
3dlr) key 401;;
dash) key 402;;
last) key 403;;
prev) key 403;;
prevch) key 403;;
fav) key 404;;
qmenu) key 405;;
textopt) key 406;;
audiodesc) key 407;;
home) key 408;;
netcast) key 408;;
energysave) key 409;;
av) key 410;;
simplink) key 411;;
esc) key 412;;
exit) key 412;;
reservprg) key 413;;
pipup) key 414;;
pipdown) key 415;;
prisec) key 416;;
myapps) key 417;;
# 419 - DTV 0, mute
# 420-470 - shows (X) window with a X character, apparently some lock/unlock input?
# 471.. - bad request
*) key $1
esac
}
function doremotekeyset(){
echo "Keys: $@"
while [ "$1" ]; do
remotekey "$1"
shift
done
}
function docommand()
{
case "$1" in
# television channels, presets
##TV program list:);;
ct1) doremotekeyset 1 ok;;
ct2) doremotekeyset 3 ok;;
nova) doremotekeyset 2 ok;;
prima) doremotekeyset 4 ok;;
cool) doremotekeyset 5 ok;;
barr) doremotekeyset 6 ok;;
cinema) doremotekeyset 7 ok;;
zoom) doremotekeyset 8 ok;;
ct24) doremotekeyset 9 ok;;
svet) doremotekeyset 1 0 ok;;
love) doremotekeyset 1 1 ok;;
kbar) doremotekeyset 1 2 ok;;
ctart) doremotekeyset 1 3 ok;;
fanda) doremotekeyset 1 4 ok;;
w:*) echo "sleep for ${1:2}...";sleep ${1:2};;
*) remotekey "$1"
esac
}
function commandlist(){
echo -n "Available commands:"
cat "$0" | grep ')'|grep '\;\;'|cut -d ')' -f 1 \
|while read x; do
if [ "${x:0:2}" == "##" ]; then echo;echo;echo ${x:2};echo -n " "
else
echo -n $x" ";
fi
done
echo
echo
}
function help(){
echo "Usage: $0 [...]"
commandlist
}
if [ "$1" == "" ]; then
commandlist
exit 0
fi
while [ "$1" ]; do
docommand "$1"
shift
done