Server/CecClient.cs
author StephaneLenclud
Sat, 26 Sep 2015 16:35:27 +0200
changeset 167 d2295c186ce1
parent 161 Server/CecSharpClient.cs@7b19bea5d73a
child 200 663c1ef0de59
permissions -rw-r--r--
Better CEC architecture.
     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         public override int ReceiveCommand(CecCommand command)
    64         {
    65             return 1;
    66         }
    67 
    68         public override int ReceiveKeypress(CecKeypress key)
    69         {
    70             return 1;
    71         }
    72 
    73         public override int ReceiveLogMessage(CecLogMessage message)
    74         {
    75             if (((int)message.Level & LogLevel) == (int)message.Level)
    76             {
    77                 string strLevel = "";
    78                 switch (message.Level)
    79                 {
    80                     case CecLogLevel.Error:
    81                         strLevel = "ERROR:     ";
    82                         break;
    83                     case CecLogLevel.Warning:
    84                         strLevel = "WARNING: ";
    85                         break;
    86                     case CecLogLevel.Notice:
    87                         strLevel = "NOTICE:    ";
    88                         break;
    89                     case CecLogLevel.Traffic:
    90                         strLevel = "TRAFFIC: ";
    91                         break;
    92                     case CecLogLevel.Debug:
    93                         strLevel = "DEBUG:     ";
    94                         break;
    95                     default:
    96                         break;
    97                 }
    98                 string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message);
    99                 Console.WriteLine(strLog);
   100             }
   101             return 1;
   102         }
   103 
   104         /// <summary>
   105         /// 
   106         /// </summary>
   107         /// <param name="timeout"></param>
   108         /// <returns></returns>
   109         public bool Connect(int timeout)
   110         {
   111             CecAdapter[] adapters = iLib.FindAdapters(string.Empty);
   112             if (adapters.Length > 0)
   113                 return Connect(adapters[0].ComPort, timeout);
   114             else
   115             {
   116                 Console.WriteLine("Did not find any CEC adapters");
   117                 return false;
   118             }
   119         }
   120 
   121         public bool Connect(string port, int timeout)
   122         {
   123             return iLib.Open(port, timeout);
   124         }
   125 
   126         public void Close()
   127         {
   128             iLib.Close();
   129         }
   130 
   131         public void ListDevices()
   132         {
   133             int iAdapter = 0;
   134             foreach (CecAdapter adapter in iLib.FindAdapters(string.Empty))
   135             {
   136                 Console.WriteLine("Adapter:    " + iAdapter++);
   137                 Console.WriteLine("Path:         " + adapter.Path);
   138                 Console.WriteLine("Com port: " + adapter.ComPort);
   139             }
   140         }
   141 
   142         void ShowConsoleHelp()
   143         {
   144             Console.WriteLine(
   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                 "================================================================================");
   171         }
   172 
   173         public void MainLoop()
   174         {
   175             bool bContinue = true;
   176             string command;
   177             while (bContinue)
   178             {
   179                 Console.WriteLine("waiting for input");
   180 
   181                 command = Console.ReadLine();
   182                 if (command != null && command.Length == 0)
   183                     continue;
   184                 string[] splitCommand = command.Split(' ');
   185                 if (splitCommand[0] == "tx" || splitCommand[0] == "txn")
   186                 {
   187                     CecCommand bytes = new CecCommand();
   188                     for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
   189                     {
   190                         bytes.PushBack(byte.Parse(splitCommand[iPtr], System.Globalization.NumberStyles.HexNumber));
   191                     }
   192 
   193                     if (command == "txn")
   194                         bytes.TransmitTimeout = 0;
   195 
   196                     iLib.Transmit(bytes);
   197                 }
   198                 else if (splitCommand[0] == "on")
   199                 {
   200                     if (splitCommand.Length > 1)
   201                         iLib.PowerOnDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
   202                     else
   203                         iLib.PowerOnDevices(CecLogicalAddress.Broadcast);
   204                 }
   205                 else if (splitCommand[0] == "standby")
   206                 {
   207                     if (splitCommand.Length > 1)
   208                         iLib.StandbyDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
   209                     else
   210                         iLib.StandbyDevices(CecLogicalAddress.Broadcast);
   211                 }
   212                 else if (splitCommand[0] == "poll")
   213                 {
   214                     bool bSent = false;
   215                     if (splitCommand.Length > 1)
   216                         bSent = iLib.PollDevice((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
   217                     else
   218                         bSent = iLib.PollDevice(CecLogicalAddress.Broadcast);
   219                     if (bSent)
   220                         Console.WriteLine("POLL message sent");
   221                     else
   222                         Console.WriteLine("POLL message not sent");
   223                 }
   224                 else if (splitCommand[0] == "la")
   225                 {
   226                     if (splitCommand.Length > 1)
   227                         iLib.SetLogicalAddress((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
   228                 }
   229                 else if (splitCommand[0] == "pa")
   230                 {
   231                     if (splitCommand.Length > 1)
   232                         iLib.SetPhysicalAddress(ushort.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
   233                 }
   234                 else if (splitCommand[0] == "osd")
   235                 {
   236                     if (splitCommand.Length > 2)
   237                     {
   238                         StringBuilder osdString = new StringBuilder();
   239                         for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
   240                         {
   241                             osdString.Append(splitCommand[iPtr]);
   242                             if (iPtr != splitCommand.Length - 1)
   243                                 osdString.Append(" ");
   244                         }
   245                         iLib.SetOSDString((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber), CecDisplayControl.DisplayForDefaultTime, osdString.ToString());
   246                     }
   247                 }
   248                 else if (splitCommand[0] == "ping")
   249                 {
   250                     iLib.PingAdapter();
   251                 }
   252                 else if (splitCommand[0] == "mon")
   253                 {
   254                     bool enable = splitCommand.Length > 1 ? splitCommand[1] == "1" : false;
   255                     iLib.SwitchMonitoring(enable);
   256                 }
   257                 else if (splitCommand[0] == "bl")
   258                 {
   259                     iLib.StartBootloader();
   260                 }
   261                 else if (splitCommand[0] == "lang")
   262                 {
   263                     if (splitCommand.Length > 1)
   264                     {
   265                         string language = iLib.GetDeviceMenuLanguage((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
   266                         Console.WriteLine("Menu language: " + language);
   267                     }
   268                 }
   269                 else if (splitCommand[0] == "ven")
   270                 {
   271                     if (splitCommand.Length > 1)
   272                     {
   273                         CecVendorId vendor = iLib.GetDeviceVendorId((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
   274                         Console.WriteLine("Vendor ID: " + iLib.ToString(vendor));
   275                     }
   276                 }
   277                 else if (splitCommand[0] == "ver")
   278                 {
   279                     if (splitCommand.Length > 1)
   280                     {
   281                         CecVersion version = iLib.GetDeviceCecVersion((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
   282                         Console.WriteLine("CEC version: " + iLib.ToString(version));
   283                     }
   284                 }
   285                 else if (splitCommand[0] == "pow")
   286                 {
   287                     if (splitCommand.Length > 1)
   288                     {
   289                         CecPowerStatus power = iLib.GetDevicePowerStatus((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
   290                         Console.WriteLine("power status: " + iLib.ToString(power));
   291                     }
   292                 }
   293                 else if (splitCommand[0] == "r")
   294                 {
   295                     Console.WriteLine("closing the connection");
   296                     iLib.Close();
   297 
   298                     Console.WriteLine("opening a new connection");
   299                     Connect(10000);
   300 
   301                     Console.WriteLine("setting active source");
   302                     iLib.SetActiveSource(CecDeviceType.PlaybackDevice);
   303                 }
   304                 else if (splitCommand[0] == "scan")
   305                 {
   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++)
   311                     {
   312                         CecLogicalAddress address = (CecLogicalAddress)iPtr;
   313                         if (!addresses.IsSet(address))
   314                             continue;
   315 
   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);
   324 
   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("");
   335                     }
   336                     Console.WriteLine(output.ToString());
   337                 }
   338                 else if (splitCommand[0] == "h" || splitCommand[0] == "help")
   339                     ShowConsoleHelp();
   340                 else if (splitCommand[0] == "q" || splitCommand[0] == "quit")
   341                     bContinue = false;
   342                 else if (splitCommand[0] == "log" && splitCommand.Length > 1)
   343                     LogLevel = int.Parse(splitCommand[1]);                
   344             }
   345         }
   346 
   347         /// TODO: remove that
   348         static void Main(string[] args)
   349         {
   350             Client p = new Client("CEC",2);
   351             if (p.Connect(10000))
   352             {
   353                 p.MainLoop();
   354             }
   355             else
   356             {
   357                 Console.WriteLine("Could not open a connection to the CEC adapter");
   358             }
   359         }
   360 
   361         /// <summary>
   362         /// Provide direct access to CEC library
   363         /// </summary>
   364         public LibCecSharp Lib
   365         {
   366             get
   367             {
   368                 return iLib;
   369             }
   370         }
   371 
   372         /// <summary>
   373         /// 
   374         /// </summary>
   375         private int LogLevel;
   376         /// <summary>
   377         /// 
   378         /// </summary>
   379         private LibCecSharp iLib;
   380         /// <summary>
   381         /// 
   382         /// </summary>
   383         private LibCECConfiguration Config;
   384     }
   385 }