From: <pat...@us...> - 2007-01-29 13:17:15
|
Revision: 38 http://mp-plugins.svn.sourceforge.net/mp-plugins/?rev=38&view=rev Author: patrick_sf Date: 2007-01-29 05:17:12 -0800 (Mon, 29 Jan 2007) Log Message: ----------- Modified Paths: -------------- trunk/plugins/DirectTv/TVE 2 DirecTV Plugin/TunerPlugin/SerialInterface/SerialInterface.cs Modified: trunk/plugins/DirectTv/TVE 2 DirecTV Plugin/TunerPlugin/SerialInterface/SerialInterface.cs =================================================================== --- trunk/plugins/DirectTv/TVE 2 DirecTV Plugin/TunerPlugin/SerialInterface/SerialInterface.cs 2007-01-28 20:06:07 UTC (rev 37) +++ trunk/plugins/DirectTv/TVE 2 DirecTV Plugin/TunerPlugin/SerialInterface/SerialInterface.cs 2007-01-29 13:17:12 UTC (rev 38) @@ -22,6 +22,7 @@ public const byte ILLEGAL_CHARACTER_RECEIVED = 0xFB; public const byte BUFFER_UNDERFLOW = 0xFD; public const byte BUFFER_OVERFLOW = 0xFF; + public const byte NULL_BYTE = 0x00; } #endregion @@ -117,6 +118,8 @@ private bool _twowaydisable = false; //Hide the OSD on D10-1/200 boxes private bool _hideOSD = false; + // Allow tuning digital subchannels on HTL-HD boxes + private bool _allowSubchannels = false; #region defaults @@ -298,6 +301,12 @@ set { _powerOnBeforeTuning = value; } } + public bool AllowDigitalSubchannels + { + get { return _allowSubchannels; } + set { _allowSubchannels = value; } + } + #endregion #region Serial Interface Control @@ -354,10 +363,26 @@ bool ReadResponse(byte verify, out byte expect, out byte response) { expect = verify; - response = (byte)_serialPort.ReadByte(); - WriteDebug("DirecTV.SerialInterface.ReadResponse(): received: {0:x}", response); - return (response == expect); + response = 0x00; + + try + { + response = (byte)_serialPort.ReadByte(); + WriteDebug("DirecTV.SerialInterface.ReadResponse(): received: {0:x}", response); + return (response == expect); + } + catch (System.TimeoutException te) + { + WriteDebug("DirecTV.SerialInterface.ReadResponse(): SerialPortTimedOut"); + } + catch (Exception ex) + { + WriteDebug("DirecTV.SerialInterface.ReadResponse(): Exception: " + ex.ToString()); + } + + return false; } + void ReadPossibleTrailingData() { try @@ -368,10 +393,12 @@ } catch (InvalidOperationException) { } } + string ToString(byte data) { return String.Format("{0.x}", data); } + string ToString(byte[] data) { if (data == null) return String.Empty; @@ -391,11 +418,15 @@ { byte reponse; byte expect; + // Start with sending the command to the box byte[] cmdData = GetCommandData(cmd.command); - WriteDebug("DirecTV.SerialInterface.SendCommand(): send command: {0}, size {1}", ToString(cmdData), - cmdData.Length); + + WriteDebug("DirecTV.SerialInterface.SendCommand(): send command: {0}, size {1}", + ToString(cmdData), cmdData.Length); + _serialPort.Write(cmdData, 0, cmdData.Length); + // Check if command was recognised by the box if (TwoWayDisable == false) { @@ -411,10 +442,23 @@ // check if the data was correctly received by the box if (ReadResponse(Response.COMMAND_IS_BEING_PROCESSED, out expect, out reponse)) { + // it was, so now check if the command succeeded WriteDebug("DirecTV.SerialInterface.SendCommand(): data is being processed by box"); - if (ReadResponse(Response.COMMAND_COMPLETED_SUCCESSFULLY, out expect, out reponse)) + + bool okContinue = false; + ReadResponse(Response.NULL_BYTE, out expect, out reponse); + if (reponse == (byte)Response.NULL_BYTE) { + WriteDebug("DirecTV.SerialInterface.SendCommand(): Null Byte Recieved Will Now Check for Successful Completion"); + if (ReadResponse(Response.COMMAND_COMPLETED_SUCCESSFULLY, out expect, out reponse)) + okContinue = true; + } + else if (reponse == (byte)Response.COMMAND_COMPLETED_SUCCESSFULLY) + okContinue = true; + + if (okContinue) + { // command succeeded, check if we expect additional data if (cmd.bytesToReceive > 0) { @@ -451,7 +495,9 @@ return; } } + WriteDebug("DirecTV.SerialInterface.SendCommand(): command NOT accepted by box!"); + // If we get here, the command must somehow have failed // make sure all received data is read ReadPossibleTrailingData(); @@ -467,8 +513,8 @@ temp[0] = 0x00; _serialPort.Write(temp, 0, 1); receivedData = temp; + } } - } public void SendCommand(Command cmd) { @@ -517,11 +563,58 @@ return _keyMap.KEY_8; case '9': return _keyMap.KEY_9; + case '-': + return _keyMap.DASH; default: return 0x00; } } + void ParseMajorMinorChannel(string channel, out int channelMajor, out int channelMinor) + { + channelMajor = -1; + channelMinor = -1; + + // If the channel string contains a dash "-" + // or has a length of 6 assume Major-Minor Channel + if (channel.Contains("-")) + { + try + { + channelMajor = Int32.Parse(channel.Substring(0, channel.IndexOf("-"))); + + if (channel.Length > (channel.IndexOf("-") + 1)) + channelMinor = Int32.Parse(channel.Substring(channel.IndexOf("-") + 1, 1)); + } + catch + { + } + } + else if (channel.Length == 6) + { + try + { + // Assume first four Major and Last two minor + channelMajor = Int32.Parse(channel.Substring(0, 4)); + channelMinor = Int32.Parse(channel.Substring(4)); + } + catch + { + } + } + else + { + try + { + channelMajor = Int32.Parse(channel); + channelMinor = 0; + } + catch + { + } + } + } + void TuneWithSetChannel(int channel) { Command cmd = _commandSet.SET_CHANNEL_NUMBER.Clone(); @@ -548,6 +641,54 @@ SendCommand(cmd); } + void TuneWithSetChannel(string channel) + { + WriteDebug("DirecTV.SerialInterface.TuneWithSetChannel: Attempting to tune STRING Channel: {0}", channel); + Command cmd = _commandSet.SET_CHANNEL_NUMBER.Clone(); + + int chMajor = 0; + int chMinor = 0; + + ParseMajorMinorChannel(channel, out chMajor, out chMinor); + + if (chMajor < 0) + { + WriteDebug("DirecTV.SerialInterface.TuneWithSetChannel: Invalid Channel Received: {0} - Channel String: {1}", chMajor.ToString(), channel); + return; + } + + if (chMajor < 256) + { + cmd.dataToSend[0] = 0x00; + cmd.dataToSend[1] = (byte)chMajor; + } + else if (chMajor < 65536) + { + byte[] chdata = BitConverter.GetBytes(chMajor); + cmd.dataToSend[0] = chdata[1]; + cmd.dataToSend[1] = chdata[0]; + } + else + { + WriteDebug("DirecTV.SerialInterface.TuneWithSetChannel: Channel Value Too Large: {0} - Channel String: {1}", chMajor.ToString(), channel); + return; + } + + if (chMinor > 0 && cmd.bytesToSend > 2) + { + //byte[] chdata = BitConverter.GetBytes(chMinor); + cmd.dataToSend[2] = 0x00; // chdata[1]; + cmd.dataToSend[3] = (byte)chMinor; // chdata[0]; + } + else if (cmd.bytesToSend > 2) + { + for (int i = 2; i < cmd.bytesToSend; i++) + cmd.dataToSend[i] = 0xFF; + } + + SendCommand(cmd); + } + void TuneWithRemoteKeys(int channel) { WriteDebug("DirecTV: In tuning"); @@ -577,6 +718,42 @@ } } + void TuneWithRemoteKeys(string channel) + { + WriteDebug("DirecTV.SerialInterface.TuneWithRemoteKeys: Attempting to tune STRING Channel: {0}", channel); + for (int i = 0; i < channel.Length; i++) + { + if (GetRemoteKeyCode(channel[i]) == 0x00) + { + WriteDebug("DirecTV.SerialInterface.TuneWithRemoteKeys: Invalid Character Found in Channel: {0}", channel); + return; + } + } + + int chMajor = 0; + int chMinor = 0; + + ParseMajorMinorChannel(channel, out chMajor, out chMinor); + + if (chMajor > 65535 || chMajor < 0) + { + WriteDebug("DirecTV.SerialInterface.TuneWithRemoteKeys: Channel Value Invalid: {0} - Channel String: {1}", chMajor.ToString(), channel); + return; + } + + Command cmd = _commandSet.REMOTE_CONTROL_KEY.Clone(); + + GetKeyMapPadBytes(_box).CopyTo(cmd.dataToSend, 0); + + for (int i = 0; i < channel.Length; i++) + { + cmd.dataToSend[2] = GetRemoteKeyCode(channel[i]); + SendCommand(cmd); + } + cmd.dataToSend[2] = _keyMap.EXIT; + SendCommand(cmd); + } + #endregion #region Generic channel tuning interface @@ -604,6 +781,26 @@ } } + public void TuneToChannel(string channel) + { + if (_serialPort == null || !_serialPort.IsOpen) + OpenPort(); + if (PowerOnBeforeTuning) + { + WriteDebug("DirecTV.SerialInterface.TuneToChannel(): send power on command"); + PowerOn(); + } + if (UseSetChannelForTune) + { + WriteDebug("DirecTV.SerialInterface.TuneToChannel(): tuning to channel {0} with set channel command", channel); + TuneWithSetChannel(channel); + } + else + { + WriteDebug("DirecTV.SerialInterface.TuneToChannel(): tuning to channel {0} with remote keypresses", channel); + TuneWithRemoteKeys(channel); + } + } #endregion #region power handling This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |