Server/CecClient.cs
author Stephane Lenclud
Thu, 14 Jul 2016 19:25:52 +0200
changeset 200 663c1ef0de59
parent 167 d2295c186ce1
child 201 6213f42f1983
permissions -rw-r--r--
Updating libcec to 6d68d21243aa92862592435e8396b4280ea46c3f.
     1 /*
     2  * This file is part of the libCEC(R) library.
     3  *
     4  * libCEC(R) is Copyright (C) 2011-2013 Pulse-Eight Limited.    All rights reserved.
     5  * libCEC(R) is an original work, containing original code.
     6  *
     7  * libCEC(R) is a trademark of Pulse-Eight Limited.
     8  *
     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.
    13  *
    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.
    18  *
    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.
    22  *
    23  *
    24  * Alternatively, you can license this library under a commercial license,
    25  * please contact Pulse-Eight Licensing for more information.
    26  *
    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/
    31  */
    32 
    33 using System;
    34 using System.Text;
    35 using CecSharp;
    36 
    37 namespace Cec
    38 {
    39     class Client : CecCallbackMethods
    40     {
    41         /// <summary>
    42         /// 
    43         /// </summary>
    44         /// <param name="aDeviceName"></param>
    45         /// <param name="aHdmiPort"></param>
    46         public Client(string aDeviceName, byte aHdmiPort)
    47         {
    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;
    55 
    56             iLib = new LibCecSharp(Config);
    57             iLib.InitVideoStandalone();
    58 
    59             //Console.WriteLine("CEC Parser created - libCEC version " + Lib.VersionToString(Config.ServerVersion));
    60             Console.WriteLine("CEC Parser created - libCEC version " + Config.ServerVersion);
    61         }
    62 
    63 
    64 
    65         public virtual int ReceiveAlert(CecAlert alert, CecParameter data)
    66         {
    67             return 1;
    68         }
    69 
    70         public virtual int ReceiveMenuStateChange(CecMenuState newVal)
    71         {
    72             return 1;
    73         }
    74 
    75         public virtual void SourceActivated(CecLogicalAddress logicalAddress, bool activated)
    76         {
    77             return;
    78         }
    79 
    80         public override int ReceiveCommand(CecCommand command)
    81         {
    82             return 1;
    83         }
    84 
    85         public override int ReceiveKeypress(CecKeypress key)
    86         {
    87             return 1;
    88         }
    89 
    90         public override int ReceiveLogMessage(CecLogMessage message)
    91         {
    92             if (((int)message.Level & LogLevel) == (int)message.Level)
    93             {
    94                 string strLevel = "";
    95                 switch (message.Level)
    96                 {
    97                     case CecLogLevel.Error:
    98                         strLevel = "ERROR:     ";
    99                         break;
   100                     case CecLogLevel.Warning:
   101                         strLevel = "WARNING: ";
   102                         break;
   103                     case CecLogLevel.Notice:
   104                         strLevel = "NOTICE:    ";
   105                         break;
   106                     case CecLogLevel.Traffic:
   107                         strLevel = "TRAFFIC: ";
   108                         break;
   109                     case CecLogLevel.Debug:
   110                         strLevel = "DEBUG:     ";
   111                         break;
   112                     default:
   113                         break;
   114                 }
   115                 string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message);
   116                 Console.WriteLine(strLog);
   117             }
   118             return 1;
   119         }
   120 
   121         /// <summary>
   122         /// 
   123         /// </summary>
   124         /// <param name="timeout"></param>
   125         /// <returns></returns>
   126         public bool Connect(int timeout)
   127         {
   128             CecAdapter[] adapters = iLib.FindAdapters(string.Empty);
   129             if (adapters.Length > 0)
   130                 return Connect(adapters[0].ComPort, timeout);
   131             else
   132             {
   133                 Console.WriteLine("Did not find any CEC adapters");
   134                 return false;
   135             }
   136         }
   137 
   138         public bool Connect(string port, int timeout)
   139         {
   140             return iLib.Open(port, timeout);
   141         }
   142 
   143         public void Close()
   144         {
   145             iLib.Close();
   146         }
   147 
   148         public void ListDevices()
   149         {
   150             int iAdapter = 0;
   151             foreach (CecAdapter adapter in iLib.FindAdapters(string.Empty))
   152             {
   153                 Console.WriteLine("Adapter:    " + iAdapter++);
   154                 Console.WriteLine("Path:         " + adapter.Path);
   155                 Console.WriteLine("Com port: " + adapter.ComPort);
   156             }
   157         }
   158 
   159         void ShowConsoleHelp()
   160         {
   161             Console.WriteLine(
   162                 "================================================================================" + Environment.NewLine +
   163                 "Available commands:" + Environment.NewLine +
   164                 Environment.NewLine +
   165                 "[tx] {bytes}                            transfer bytes over the CEC line." + Environment.NewLine +
   166                 "[txn] {bytes}                         transfer bytes but don't wait for transmission ACK." + Environment.NewLine +
   167                 "[on] {address}                        power on the device with the given logical address." + Environment.NewLine +
   168                 "[standby] {address}             put the device with the given address in standby mode." + Environment.NewLine +
   169                 "[la] {logical_address}        change the logical address of the CEC adapter." + Environment.NewLine +
   170                 "[pa] {physical_address}     change the physical address of the CEC adapter." + Environment.NewLine +
   171                 "[osd] {addr} {string}         set OSD message on the specified device." + Environment.NewLine +
   172                 "[ver] {addr}                            get the CEC version of the specified device." + Environment.NewLine +
   173                 "[ven] {addr}                            get the vendor ID of the specified device." + Environment.NewLine +
   174                 "[lang] {addr}                         get the menu language of the specified device." + Environment.NewLine +
   175                 "[pow] {addr}                            get the power status of the specified device." + Environment.NewLine +
   176                 "[poll] {addr}                         poll the specified device." + Environment.NewLine +
   177                 "[scan]                                        scan the CEC bus and display device info" + Environment.NewLine +
   178                 "[mon] {1|0}                             enable or disable CEC bus monitoring." + Environment.NewLine +
   179                 "[log] {1 - 31}                        change the log level. see cectypes.h for values." + Environment.NewLine +
   180                 "[ping]                                        send a ping command to the CEC adapter." + Environment.NewLine +
   181                 "[bl]                                            to let the adapter enter the bootloader, to upgrade" + Environment.NewLine +
   182                 "                                                    the flash rom." + Environment.NewLine +
   183                 "[r]                                             reconnect to the CEC adapter." + Environment.NewLine +
   184                 "[h] or [help]                         show this help." + Environment.NewLine +
   185                 "[q] or [quit]                         to quit the CEC test client and switch off all" + Environment.NewLine +
   186                 "                                                    connected CEC devices." + Environment.NewLine +
   187                 "================================================================================");
   188         }
   189 
   190         public void MainLoop()
   191         {
   192             bool bContinue = true;
   193             string command;
   194             while (bContinue)
   195             {
   196                 Console.WriteLine("waiting for input");
   197 
   198                 command = Console.ReadLine();
   199                 if (command != null && command.Length == 0)
   200                     continue;
   201                 string[] splitCommand = command.Split(' ');
   202                 if (splitCommand[0] == "tx" || splitCommand[0] == "txn")
   203                 {
   204                     CecCommand bytes = new CecCommand();
   205                     for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
   206                     {
   207                         bytes.PushBack(byte.Parse(splitCommand[iPtr], System.Globalization.NumberStyles.HexNumber));
   208                     }
   209 
   210                     if (command == "txn")
   211                         bytes.TransmitTimeout = 0;
   212 
   213                     iLib.Transmit(bytes);
   214                 }
   215                 else if (splitCommand[0] == "on")
   216                 {
   217                     if (splitCommand.Length > 1)
   218                         iLib.PowerOnDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
   219                     else
   220                         iLib.PowerOnDevices(CecLogicalAddress.Broadcast);
   221                 }
   222                 else if (splitCommand[0] == "standby")
   223                 {
   224                     if (splitCommand.Length > 1)
   225                         iLib.StandbyDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
   226                     else
   227                         iLib.StandbyDevices(CecLogicalAddress.Broadcast);
   228                 }
   229                 else if (splitCommand[0] == "poll")
   230                 {
   231                     bool bSent = false;
   232                     if (splitCommand.Length > 1)
   233                         bSent = iLib.PollDevice((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
   234                     else
   235                         bSent = iLib.PollDevice(CecLogicalAddress.Broadcast);
   236                     if (bSent)
   237                         Console.WriteLine("POLL message sent");
   238                     else
   239                         Console.WriteLine("POLL message not sent");
   240                 }
   241                 else if (splitCommand[0] == "la")
   242                 {
   243                     if (splitCommand.Length > 1)
   244                         iLib.SetLogicalAddress((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
   245                 }
   246                 else if (splitCommand[0] == "pa")
   247                 {
   248                     if (splitCommand.Length > 1)
   249                         iLib.SetPhysicalAddress(ushort.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
   250                 }
   251                 else if (splitCommand[0] == "osd")
   252                 {
   253                     if (splitCommand.Length > 2)
   254                     {
   255                         StringBuilder osdString = new StringBuilder();
   256                         for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
   257                         {
   258                             osdString.Append(splitCommand[iPtr]);
   259                             if (iPtr != splitCommand.Length - 1)
   260                                 osdString.Append(" ");
   261                         }
   262                         iLib.SetOSDString((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber), CecDisplayControl.DisplayForDefaultTime, osdString.ToString());
   263                     }
   264                 }
   265                 else if (splitCommand[0] == "ping")
   266                 {
   267                     iLib.PingAdapter();
   268                 }
   269                 else if (splitCommand[0] == "mon")
   270                 {
   271                     bool enable = splitCommand.Length > 1 ? splitCommand[1] == "1" : false;
   272                     iLib.SwitchMonitoring(enable);
   273                 }
   274                 else if (splitCommand[0] == "bl")
   275                 {
   276                     iLib.StartBootloader();
   277                 }
   278                 else if (splitCommand[0] == "lang")
   279                 {
   280                     if (splitCommand.Length > 1)
   281                     {
   282                         string language = iLib.GetDeviceMenuLanguage((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
   283                         Console.WriteLine("Menu language: " + language);
   284                     }
   285                 }
   286                 else if (splitCommand[0] == "ven")
   287                 {
   288                     if (splitCommand.Length > 1)
   289                     {
   290                         CecVendorId vendor = iLib.GetDeviceVendorId((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
   291                         Console.WriteLine("Vendor ID: " + iLib.ToString(vendor));
   292                     }
   293                 }
   294                 else if (splitCommand[0] == "ver")
   295                 {
   296                     if (splitCommand.Length > 1)
   297                     {
   298                         CecVersion version = iLib.GetDeviceCecVersion((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
   299                         Console.WriteLine("CEC version: " + iLib.ToString(version));
   300                     }
   301                 }
   302                 else if (splitCommand[0] == "pow")
   303                 {
   304                     if (splitCommand.Length > 1)
   305                     {
   306                         CecPowerStatus power = iLib.GetDevicePowerStatus((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
   307                         Console.WriteLine("power status: " + iLib.ToString(power));
   308                     }
   309                 }
   310                 else if (splitCommand[0] == "r")
   311                 {
   312                     Console.WriteLine("closing the connection");
   313                     iLib.Close();
   314 
   315                     Console.WriteLine("opening a new connection");
   316                     Connect(10000);
   317 
   318                     Console.WriteLine("setting active source");
   319                     iLib.SetActiveSource(CecDeviceType.PlaybackDevice);
   320                 }
   321                 else if (splitCommand[0] == "scan")
   322                 {
   323                     StringBuilder output = new StringBuilder();
   324                     output.AppendLine("CEC bus information");
   325                     output.AppendLine("===================");
   326                     CecLogicalAddresses addresses = iLib.GetActiveDevices();
   327                     for (int iPtr = 0; iPtr < addresses.Addresses.Length; iPtr++)
   328                     {
   329                         CecLogicalAddress address = (CecLogicalAddress)iPtr;
   330                         if (!addresses.IsSet(address))
   331                             continue;
   332 
   333                         CecVendorId iVendorId = iLib.GetDeviceVendorId(address);
   334                         bool bActive = iLib.IsActiveDevice(address);
   335                         ushort iPhysicalAddress = iLib.GetDevicePhysicalAddress(address);
   336                         string strAddr = "todo: fixme"; //Lib.PhysicalAddressToString(iPhysicalAddress);
   337                         CecVersion iCecVersion = iLib.GetDeviceCecVersion(address);
   338                         CecPowerStatus power = iLib.GetDevicePowerStatus(address);
   339                         string osdName = iLib.GetDeviceOSDName(address);
   340                         string lang = iLib.GetDeviceMenuLanguage(address);
   341 
   342                         output.AppendLine("device #" + iPtr + ": " + iLib.ToString(address));
   343                         output.AppendLine("address:             " + strAddr);
   344                         output.AppendLine("active source: " + (bActive ? "yes" : "no"));
   345                         output.AppendLine("vendor:                " + iLib.ToString(iVendorId));
   346                         output.AppendLine("osd string:        " + osdName);
   347                         output.AppendLine("CEC version:     " + iLib.ToString(iCecVersion));
   348                         output.AppendLine("power status:    " + iLib.ToString(power));
   349                         if (!string.IsNullOrEmpty(lang))
   350                             output.AppendLine("language:            " + lang);
   351                         output.AppendLine("");
   352                     }
   353                     Console.WriteLine(output.ToString());
   354                 }
   355                 else if (splitCommand[0] == "h" || splitCommand[0] == "help")
   356                     ShowConsoleHelp();
   357                 else if (splitCommand[0] == "q" || splitCommand[0] == "quit")
   358                     bContinue = false;
   359                 else if (splitCommand[0] == "log" && splitCommand.Length > 1)
   360                     LogLevel = int.Parse(splitCommand[1]);                
   361             }
   362         }
   363 
   364         /// TODO: remove that
   365         static void Main(string[] args)
   366         {
   367             Client p = new Client("CEC",2);
   368             if (p.Connect(10000))
   369             {
   370                 p.MainLoop();
   371             }
   372             else
   373             {
   374                 Console.WriteLine("Could not open a connection to the CEC adapter");
   375             }
   376         }
   377 
   378         /// <summary>
   379         /// Provide direct access to CEC library
   380         /// </summary>
   381         public LibCecSharp Lib
   382         {
   383             get
   384             {
   385                 return iLib;
   386             }
   387         }
   388 
   389         /// <summary>
   390         /// 
   391         /// </summary>
   392         private int LogLevel;
   393         /// <summary>
   394         /// 
   395         /// </summary>
   396         private LibCecSharp iLib;
   397         /// <summary>
   398         /// 
   399         /// </summary>
   400         private LibCECConfiguration Config;
   401     }
   402 }