Better CEC architecture.
     2  * This file is part of the libCEC(R) library.
 
     4  * libCEC(R) is Copyright (C) 2011-2013 Pulse-Eight Limited.    All rights reserved.
 
     5  * libCEC(R) is an original work, containing original code.
 
     7  * libCEC(R) is a trademark of Pulse-Eight Limited.
 
     9  * This program is dual-licensed; you can redistribute it and/or modify
 
    10  * it under the terms of the GNU General Public License as published by
 
    11  * the Free Software Foundation; either version 2 of the License, or
 
    12  * (at your option) any later version.
 
    14  * This program is distributed in the hope that it will be useful,
 
    15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.    See the
 
    17  * GNU General Public License for more details.
 
    19  * You should have received a copy of the GNU General Public License
 
    20  * along with this program; if not, write to the Free Software
 
    21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
    24  * Alternatively, you can license this library under a commercial license,
 
    25  * please contact Pulse-Eight Licensing for more information.
 
    27  * For more information contact:
 
    28  * Pulse-Eight Licensing             <license@pulse-eight.com>
 
    29  *         http://www.pulse-eight.com/
 
    30  *         http://www.pulse-eight.net/
 
    39     class Client : CecCallbackMethods
 
    44         /// <param name="aDeviceName"></param>
 
    45         /// <param name="aHdmiPort"></param>
 
    46         public Client(string aDeviceName, byte aHdmiPort)
 
    48             Config = new LibCECConfiguration();
 
    49             Config.DeviceTypes.Types[0] = CecDeviceType.Tv;
 
    50             Config.DeviceName = aDeviceName;
 
    51             Config.HDMIPort = aHdmiPort;
 
    52             //Config.ClientVersion = LibCECConfiguration.CurrentVersion;
 
    53             Config.SetCallbacks(this);
 
    54             LogLevel = (int)CecLogLevel.All;
 
    56             iLib = new LibCecSharp(Config);
 
    57             iLib.InitVideoStandalone();
 
    59             //Console.WriteLine("CEC Parser created - libCEC version " + Lib.VersionToString(Config.ServerVersion));
 
    60             Console.WriteLine("CEC Parser created - libCEC version " + Config.ServerVersion);
 
    63         public override int ReceiveCommand(CecCommand command)
 
    68         public override int ReceiveKeypress(CecKeypress key)
 
    73         public override int ReceiveLogMessage(CecLogMessage message)
 
    75             if (((int)message.Level & LogLevel) == (int)message.Level)
 
    78                 switch (message.Level)
 
    80                     case CecLogLevel.Error:
 
    83                     case CecLogLevel.Warning:
 
    84                         strLevel = "WARNING: ";
 
    86                     case CecLogLevel.Notice:
 
    87                         strLevel = "NOTICE:    ";
 
    89                     case CecLogLevel.Traffic:
 
    90                         strLevel = "TRAFFIC: ";
 
    92                     case CecLogLevel.Debug:
 
    98                 string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message);
 
    99                 Console.WriteLine(strLog);
 
   107         /// <param name="timeout"></param>
 
   108         /// <returns></returns>
 
   109         public bool Connect(int timeout)
 
   111             CecAdapter[] adapters = iLib.FindAdapters(string.Empty);
 
   112             if (adapters.Length > 0)
 
   113                 return Connect(adapters[0].ComPort, timeout);
 
   116                 Console.WriteLine("Did not find any CEC adapters");
 
   121         public bool Connect(string port, int timeout)
 
   123             return iLib.Open(port, timeout);
 
   131         public void ListDevices()
 
   134             foreach (CecAdapter adapter in iLib.FindAdapters(string.Empty))
 
   136                 Console.WriteLine("Adapter:    " + iAdapter++);
 
   137                 Console.WriteLine("Path:         " + adapter.Path);
 
   138                 Console.WriteLine("Com port: " + adapter.ComPort);
 
   142         void ShowConsoleHelp()
 
   145                 "================================================================================" + Environment.NewLine +
 
   146                 "Available commands:" + Environment.NewLine +
 
   147                 Environment.NewLine +
 
   148                 "[tx] {bytes}                            transfer bytes over the CEC line." + Environment.NewLine +
 
   149                 "[txn] {bytes}                         transfer bytes but don't wait for transmission ACK." + Environment.NewLine +
 
   150                 "[on] {address}                        power on the device with the given logical address." + Environment.NewLine +
 
   151                 "[standby] {address}             put the device with the given address in standby mode." + Environment.NewLine +
 
   152                 "[la] {logical_address}        change the logical address of the CEC adapter." + Environment.NewLine +
 
   153                 "[pa] {physical_address}     change the physical address of the CEC adapter." + Environment.NewLine +
 
   154                 "[osd] {addr} {string}         set OSD message on the specified device." + Environment.NewLine +
 
   155                 "[ver] {addr}                            get the CEC version of the specified device." + Environment.NewLine +
 
   156                 "[ven] {addr}                            get the vendor ID of the specified device." + Environment.NewLine +
 
   157                 "[lang] {addr}                         get the menu language of the specified device." + Environment.NewLine +
 
   158                 "[pow] {addr}                            get the power status of the specified device." + Environment.NewLine +
 
   159                 "[poll] {addr}                         poll the specified device." + Environment.NewLine +
 
   160                 "[scan]                                        scan the CEC bus and display device info" + Environment.NewLine +
 
   161                 "[mon] {1|0}                             enable or disable CEC bus monitoring." + Environment.NewLine +
 
   162                 "[log] {1 - 31}                        change the log level. see cectypes.h for values." + Environment.NewLine +
 
   163                 "[ping]                                        send a ping command to the CEC adapter." + Environment.NewLine +
 
   164                 "[bl]                                            to let the adapter enter the bootloader, to upgrade" + Environment.NewLine +
 
   165                 "                                                    the flash rom." + Environment.NewLine +
 
   166                 "[r]                                             reconnect to the CEC adapter." + Environment.NewLine +
 
   167                 "[h] or [help]                         show this help." + Environment.NewLine +
 
   168                 "[q] or [quit]                         to quit the CEC test client and switch off all" + Environment.NewLine +
 
   169                 "                                                    connected CEC devices." + Environment.NewLine +
 
   170                 "================================================================================");
 
   173         public void MainLoop()
 
   175             bool bContinue = true;
 
   179                 Console.WriteLine("waiting for input");
 
   181                 command = Console.ReadLine();
 
   182                 if (command != null && command.Length == 0)
 
   184                 string[] splitCommand = command.Split(' ');
 
   185                 if (splitCommand[0] == "tx" || splitCommand[0] == "txn")
 
   187                     CecCommand bytes = new CecCommand();
 
   188                     for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
 
   190                         bytes.PushBack(byte.Parse(splitCommand[iPtr], System.Globalization.NumberStyles.HexNumber));
 
   193                     if (command == "txn")
 
   194                         bytes.TransmitTimeout = 0;
 
   196                     iLib.Transmit(bytes);
 
   198                 else if (splitCommand[0] == "on")
 
   200                     if (splitCommand.Length > 1)
 
   201                         iLib.PowerOnDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
 
   203                         iLib.PowerOnDevices(CecLogicalAddress.Broadcast);
 
   205                 else if (splitCommand[0] == "standby")
 
   207                     if (splitCommand.Length > 1)
 
   208                         iLib.StandbyDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
 
   210                         iLib.StandbyDevices(CecLogicalAddress.Broadcast);
 
   212                 else if (splitCommand[0] == "poll")
 
   215                     if (splitCommand.Length > 1)
 
   216                         bSent = iLib.PollDevice((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
 
   218                         bSent = iLib.PollDevice(CecLogicalAddress.Broadcast);
 
   220                         Console.WriteLine("POLL message sent");
 
   222                         Console.WriteLine("POLL message not sent");
 
   224                 else if (splitCommand[0] == "la")
 
   226                     if (splitCommand.Length > 1)
 
   227                         iLib.SetLogicalAddress((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
 
   229                 else if (splitCommand[0] == "pa")
 
   231                     if (splitCommand.Length > 1)
 
   232                         iLib.SetPhysicalAddress(ushort.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
 
   234                 else if (splitCommand[0] == "osd")
 
   236                     if (splitCommand.Length > 2)
 
   238                         StringBuilder osdString = new StringBuilder();
 
   239                         for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
 
   241                             osdString.Append(splitCommand[iPtr]);
 
   242                             if (iPtr != splitCommand.Length - 1)
 
   243                                 osdString.Append(" ");
 
   245                         iLib.SetOSDString((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber), CecDisplayControl.DisplayForDefaultTime, osdString.ToString());
 
   248                 else if (splitCommand[0] == "ping")
 
   252                 else if (splitCommand[0] == "mon")
 
   254                     bool enable = splitCommand.Length > 1 ? splitCommand[1] == "1" : false;
 
   255                     iLib.SwitchMonitoring(enable);
 
   257                 else if (splitCommand[0] == "bl")
 
   259                     iLib.StartBootloader();
 
   261                 else if (splitCommand[0] == "lang")
 
   263                     if (splitCommand.Length > 1)
 
   265                         string language = iLib.GetDeviceMenuLanguage((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
 
   266                         Console.WriteLine("Menu language: " + language);
 
   269                 else if (splitCommand[0] == "ven")
 
   271                     if (splitCommand.Length > 1)
 
   273                         CecVendorId vendor = iLib.GetDeviceVendorId((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
 
   274                         Console.WriteLine("Vendor ID: " + iLib.ToString(vendor));
 
   277                 else if (splitCommand[0] == "ver")
 
   279                     if (splitCommand.Length > 1)
 
   281                         CecVersion version = iLib.GetDeviceCecVersion((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
 
   282                         Console.WriteLine("CEC version: " + iLib.ToString(version));
 
   285                 else if (splitCommand[0] == "pow")
 
   287                     if (splitCommand.Length > 1)
 
   289                         CecPowerStatus power = iLib.GetDevicePowerStatus((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
 
   290                         Console.WriteLine("power status: " + iLib.ToString(power));
 
   293                 else if (splitCommand[0] == "r")
 
   295                     Console.WriteLine("closing the connection");
 
   298                     Console.WriteLine("opening a new connection");
 
   301                     Console.WriteLine("setting active source");
 
   302                     iLib.SetActiveSource(CecDeviceType.PlaybackDevice);
 
   304                 else if (splitCommand[0] == "scan")
 
   306                     StringBuilder output = new StringBuilder();
 
   307                     output.AppendLine("CEC bus information");
 
   308                     output.AppendLine("===================");
 
   309                     CecLogicalAddresses addresses = iLib.GetActiveDevices();
 
   310                     for (int iPtr = 0; iPtr < addresses.Addresses.Length; iPtr++)
 
   312                         CecLogicalAddress address = (CecLogicalAddress)iPtr;
 
   313                         if (!addresses.IsSet(address))
 
   316                         CecVendorId iVendorId = iLib.GetDeviceVendorId(address);
 
   317                         bool bActive = iLib.IsActiveDevice(address);
 
   318                         ushort iPhysicalAddress = iLib.GetDevicePhysicalAddress(address);
 
   319                         string strAddr = "todo: fixme"; //Lib.PhysicalAddressToString(iPhysicalAddress);
 
   320                         CecVersion iCecVersion = iLib.GetDeviceCecVersion(address);
 
   321                         CecPowerStatus power = iLib.GetDevicePowerStatus(address);
 
   322                         string osdName = iLib.GetDeviceOSDName(address);
 
   323                         string lang = iLib.GetDeviceMenuLanguage(address);
 
   325                         output.AppendLine("device #" + iPtr + ": " + iLib.ToString(address));
 
   326                         output.AppendLine("address:             " + strAddr);
 
   327                         output.AppendLine("active source: " + (bActive ? "yes" : "no"));
 
   328                         output.AppendLine("vendor:                " + iLib.ToString(iVendorId));
 
   329                         output.AppendLine("osd string:        " + osdName);
 
   330                         output.AppendLine("CEC version:     " + iLib.ToString(iCecVersion));
 
   331                         output.AppendLine("power status:    " + iLib.ToString(power));
 
   332                         if (!string.IsNullOrEmpty(lang))
 
   333                             output.AppendLine("language:            " + lang);
 
   334                         output.AppendLine("");
 
   336                     Console.WriteLine(output.ToString());
 
   338                 else if (splitCommand[0] == "h" || splitCommand[0] == "help")
 
   340                 else if (splitCommand[0] == "q" || splitCommand[0] == "quit")
 
   342                 else if (splitCommand[0] == "log" && splitCommand.Length > 1)
 
   343                     LogLevel = int.Parse(splitCommand[1]);                
 
   347         /// TODO: remove that
 
   348         static void Main(string[] args)
 
   350             Client p = new Client("CEC",2);
 
   351             if (p.Connect(10000))
 
   357                 Console.WriteLine("Could not open a connection to the CEC adapter");
 
   362         /// Provide direct access to CEC library
 
   364         public LibCecSharp Lib
 
   375         private int LogLevel;
 
   379         private LibCecSharp iLib;
 
   383         private LibCECConfiguration Config;