#!/bin/sh

show_help() {
  echo "AXE debug script, commands:"
  echo "  tuner [0x<mask>]  : enable tuner (fe) debug"
  echo "  i2c               : enable i2c debug"
  echo "  off               : all debug off"
  echo "  reset             : reset all tuners and kill (restart) minisatip"
  echo "  dmxts"
  echo "  pti [dmesg|pid|util|pdev|vdev] [num0-3]"
}

case "$1" in
tuner)
  val1="$2"
  test -z "$val1" && val1="0xffffffff"
  echo "Enabling tuner debug $val1"
  echo "deb $val1" > /proc/bus/nim_sockets
  ;;
i2c)
  if test -z "$2"; then
    echo "Enabling i2c debug"
    echo "i2c_dbg_trans 1" > /proc/bus/ivo_i2c
  fi
  ;;
off)
  echo "Disabling kernel driver debug"
  echo "deb 0" > /proc/bus/nim_sockets
  echo "i2c_dbg_trans 0" > /proc/bus/ivo_i2c
  ;;
reset)
  echo "Reset all tuners"
  echo "i2c_dbg_trans 1" > /proc/bus/ivo_i2c
  echo "res" > /proc/bus/nim_sockets
  sleep 10
  echo "i2c_dbg_trans 0" > /proc/bus/ivo_i2c
  killall -9 minisatip
  ;;
dmxts)
  noex="AAAAAAAAAABBBBBBBBB"
  while [ 1 -eq 1 ]; do grep -v $noex /proc/driver/axedmx-ts ; sleep 0.2; done
  ;;
pti)
  cmd="$2"
  test -z "$cmd" && cmd=vdev
  num="$3"
  test -z "$num" && num=0
  noex="AAAAAAAAAABBBBBBBBB"
  d="/proc/STAPI/stpti/PTI$num"
  case "$cmd" in
  dmesg) grep -v $noex $d/PrintBuffer ;;
  pid) grep -v $noex $d/PIDTable ;;
  util) while [ 1 -eq 1 ]; do grep -v $noex $d/UtilisationTP ; sleep 0.2; done ;;
  pdev) while [ 1 -eq 1 ]; do grep -v $noex $d/pDevice ; sleep 0.2 ; done ;;
  vdev) while [ 1 -eq 1 ]; do grep -v $noex $d/vDeviceInfo ; sleep 0.2 ; done ;;
  esac
  ;;
demod)
  for a in 0xd0:f2:f3:Input_1 0xd0:f4:f5:Input_2 \
           0xd2:f2:f3:Input_3 0xd2:f4:f5:Input_4; do
    d=$(echo $a | cut -d ':' -f 1)
    a1=$(echo $a | cut -d ':' -f 2)
    a2=$(echo $a | cut -d ':' -f 3)
    n=$(echo $a | cut -d ':' -f 4)
    echo
    echo ">>> $n"
    echo "********************************************************"
    echo
    axehelper i2c_reg_read --decode $d 0x${a1}12 2
    axehelper i2c_reg_read --decode $d 0x${a1}16 1
    axehelper i2c_reg_read --decode $d 0x${a2}69 2
    axehelper i2c_reg_read --decode $d 0x${a2}70 1
    axehelper i2c_reg_read --decode $d 0x${a2}80 5
    axehelper i2c_reg_read --decode $d 0x${a2}99 3
    axehelper i2c_reg_read --decode $d 0x${a2}9d 3
    axehelper i2c_reg_read --decode $d 0x${a2}a4 1
    axehelper i2c_reg_read --decode $d 0x${a2}ad 3
  done
  ;;
gain)
  val1="$2"
  test -z "$val1" && val1="3"
  echo "Tuner gain set to 0x5${val1}"
  axehelper i2c_reg_write 0xc0 0x01 0x5${val1}
  axehelper i2c_reg_write 0xc0 0x0b 0x5${val1}
  axehelper i2c_reg_write 0xc6 0x01 0x5${val1}
  axehelper i2c_reg_write 0xc6 0x0b 0x5${val1}
  ;;
tdump)
  echo "Tuner 1"
  axehelper i2c_reg_read 0xc0 0 10
  axehelper i2c_reg_read 0xc0 10 15
  echo "Tuner 2"
  axehelper i2c_reg_read 0xc6 0 10
  axehelper i2c_reg_read 0xc6 10 15
  ;;
gain2)
  val1="$2"
  test -z "$val1" && val1="9"
  echo "Demod gain set to 0x4${val1}"
  axehelper i2c_reg_write 0xd0 0xf2e8 0x4${val1}
  axehelper i2c_reg_write 0xd0 0xf4e8 0x4${val1}
  axehelper i2c_reg_write 0xd2 0xf2e8 0x4${val1}
  axehelper i2c_reg_write 0xd2 0xf4e8 0x4${val1}
  ;;
agc2)
  val1="$2"
  test -z "$val1" && val1="38"
  echo "Demod gain set to 0x${val1}"
  axehelper i2c_reg_write 0xd0 0xf22d 0x${val1}
  axehelper i2c_reg_write 0xd0 0xf42d 0x${val1}
  axehelper i2c_reg_write 0xd2 0xf22d 0x${val1}
  axehelper i2c_reg_write 0xd2 0xf42d 0x${val1}
  ;;
*)
  show_help
esac