/*
 * dct-channel
 * Copyright (c) 2003 Jim Paris <jim@jtan.com>
 *
 * This is free software; you can redistribute it and/or modify it and
 * it is provided under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation; see COPYING.
 */

dct-channel - switch channel on a DCT-2000-series cable box connected
to the serial port via a straight-through cable and print the new
channel, or just print the channel if no arguments are passed.

Tested on a Motorola DCT-2244 from Comcast.

Attempts to be smart about changing the channel.  It will check and
make sure we're not already on the channel, to avoid pulling up the
OSD unnecessarily.  It will also ensure that the box actually
switches, attempting to exit menus and even power the box on if it
doesn't appear to switch.

To use this from MythTV, just go into the MythTV card setup and point
the external changer program to the "channel" binary.  I also
recommend the "-v" option.

The following command-line options may be useful if you have
a problem:

-v	  Explain what's happening.  Very useful.
-vv	  Explain, plus dump packets and extra debug.
--force   Try to keep going after communication errors
--blind   Blindly change the channel.  This means that the code
	  just sends a fixed sequence to the device and never
	  checks for any response.  It assumes that the channel
	  change succeeded.  This should be identical behavior
	  to the old Python channel-changing scripts.
--nopower Never send KEY_POWER.  Use this if your box seems
	  to turn off unexpectedly.
-t 5	  Scale all timeouts by 5x.  Note that you can't go too
	  high with the timeouts when using blind mode, because
	  the box will stop waiting for keypresses if they come
	  too slowly.
 
Known bugs:

If the box is off and on the correct channel, it will remain off.
I don't know how to tell if the box is on or off except by
noticing that it doesn't respond to a channel change request. 
If this is really a problem, using this program to change to 
a separate but valid channel and then back should ensure that
the box turns on.

If a channel doesn't exist, the box will not switch to it,
so this code will wrongly assume the box is off and send
the power button, therefore turning it off.

Packet and sequence number handling needs to be better.  See TODO.
