updated copyright messages for 2013
[deb_libcec.git] / src / CecSharpTester / CecSharpClient.cs
index 0cd6aebd7dab9f14f305be169490aae5374f8b31..e3d22f6948a889a5bf3abe1d6c1548e9a7514998 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the libCEC(R) library.
  *
- * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited.  All rights reserved.
+ * libCEC(R) is Copyright (C) 2011-2013 Pulse-Eight Limited.  All rights reserved.
  * libCEC(R) is an original work, containing original code.
  *
  * libCEC(R) is a trademark of Pulse-Eight Limited.
  */
 
 using System;
-using System.Collections.Generic;
-using System.Linq;
 using System.Text;
+using CecSharp;
 
 namespace CecSharpClient
 {
-  class CecSharpClient
+  class CecSharpClient : CecCallbackMethods
   {
     public CecSharpClient()
     {
-      CecDeviceTypeList types = new CecDeviceTypeList();
-      types.Types[0] = CecDeviceType.PlaybackDevice;
+      Config = new LibCECConfiguration();
+      Config.DeviceTypes.Types[0] = CecDeviceType.RecordingDevice;
+      Config.DeviceName = "CEC Tester";
+      Config.ClientVersion = CecClientVersion.Version2_1_0;
+      Config.SetCallbacks(this);
+      LogLevel = (int)CecLogLevel.All;
 
-      Lib = new LibCecSharp("CEC Tester", types);
-      LogLevel = (int) CecLogLevel.All;
+      Lib = new LibCecSharp(Config);
+      Lib.InitVideoStandalone();
 
-      Console.WriteLine("CEC Parser created - libcec version " + Lib.GetLibVersionMajor() + "." + Lib.GetLibVersionMinor());
+      Console.WriteLine("CEC Parser created - libCEC version " + Lib.ToString(Config.ServerVersion));
     }
 
-    void FlushLog()
+    public override int ReceiveCommand(CecCommand command)
     {
-      CecLogMessage message = Lib.GetNextLogMessage();
-      bool bGotMessage = !message.Empty;
-      while (bGotMessage)
+      return 1;
+    }
+
+    public override int ReceiveKeypress(CecKeypress key)
+    {
+      return 1;
+    }
+
+    public override int ReceiveLogMessage(CecLogMessage message)
+    {
+      if (((int)message.Level & LogLevel) == (int)message.Level)
       {
-        if (((int)message.Level & LogLevel) == (int)message.Level)
+        string strLevel = "";
+        switch (message.Level)
         {
-          string strLevel = "";
-          switch (message.Level)
-          {
-            case CecLogLevel.Error:
-              strLevel = "ERROR:   ";
-              break;
-            case CecLogLevel.Warning:
-              strLevel = "WARNING: ";
-              break;
-            case CecLogLevel.Notice:
-              strLevel = "NOTICE:  ";
-              break;
-            case CecLogLevel.Traffic:
-              strLevel = "TRAFFIC: ";
-              break;
-            case CecLogLevel.Debug:
-              strLevel = "DEBUG:   ";
-              break;
-            default:
-              break;
-          }
-          string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message);
-          Console.WriteLine(strLog);
+          case CecLogLevel.Error:
+            strLevel = "ERROR:   ";
+            break;
+          case CecLogLevel.Warning:
+            strLevel = "WARNING: ";
+            break;
+          case CecLogLevel.Notice:
+            strLevel = "NOTICE:  ";
+            break;
+          case CecLogLevel.Traffic:
+            strLevel = "TRAFFIC: ";
+            break;
+          case CecLogLevel.Debug:
+            strLevel = "DEBUG:   ";
+            break;
+          default:
+            break;
         }
-
-        message = Lib.GetNextLogMessage();
-        bGotMessage = !message.Empty;
+        string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message);
+        Console.WriteLine(strLog);
       }
+      return 1;
     }
 
-    public bool Connect(int timeout)
+      public bool Connect(int timeout)
     {
       CecAdapter[] adapters = Lib.FindAdapters(string.Empty);
       if (adapters.Length > 0)
@@ -124,64 +130,36 @@ namespace CecSharpClient
     void ShowConsoleHelp()
     {
       Console.WriteLine(
-        "================================================================================" + System.Environment.NewLine +
-        "Available commands:" + System.Environment.NewLine +
-        System.Environment.NewLine +
-        "tx {bytes}                transfer bytes over the CEC line." + System.Environment.NewLine +
-        "txn {bytes}               transfer bytes but don't wait for transmission ACK." + System.Environment.NewLine +
-        "[tx 40 00 FF 11 22 33]    sends bytes 0x40 0x00 0xFF 0x11 0x22 0x33" + System.Environment.NewLine +
-        System.Environment.NewLine +
-        "on {address}              power on the device with the given logical address." + System.Environment.NewLine +
-        "[on 5]                    power on a connected audio system" + System.Environment.NewLine +
-        System.Environment.NewLine +
-        "standby {address}         put the device with the given address in standby mode." + System.Environment.NewLine +
-        "[standby 0]               powers off the TV" + System.Environment.NewLine +
-        System.Environment.NewLine +
-        "la {logical_address}      change the logical address of the CEC adapter." + System.Environment.NewLine +
-        "[la 4]                    logical address 4" + System.Environment.NewLine +
-        System.Environment.NewLine +
-        "pa {physical_address}     change the physical address of the CEC adapter." + System.Environment.NewLine +
-        "[pa 1000]                 physical address 1.0.0.0" + System.Environment.NewLine +
-        System.Environment.NewLine +
-        "osd {addr} {string}       set OSD message on the specified device." + System.Environment.NewLine +
-        "[osd 0 Test Message]      displays 'Test Message' on the TV" + System.Environment.NewLine +
-        System.Environment.NewLine +
-        "ver {addr}                get the CEC version of the specified device." + System.Environment.NewLine +
-        "[ver 0]                   get the CEC version of the TV" + System.Environment.NewLine +
-        System.Environment.NewLine +
-        "ven {addr}                get the vendor ID of the specified device." + System.Environment.NewLine +
-        "[ven 0]                   get the vendor ID of the TV" + System.Environment.NewLine +
-        System.Environment.NewLine +
-        "lang {addr}               get the menu language of the specified device." + System.Environment.NewLine +
-        "[lang 0]                  get the menu language of the TV" + System.Environment.NewLine +
-        System.Environment.NewLine +
-        "pow {addr}                get the power status of the specified device." + System.Environment.NewLine +
-        "[pow 0]                   get the power status of the TV" + System.Environment.NewLine +
-        System.Environment.NewLine +
-        "poll {addr}               poll the specified device." + System.Environment.NewLine +
-        "[poll 0]                  sends a poll message to the TV" + System.Environment.NewLine +
-        System.Environment.NewLine +
-        "[mon] {1|0}               enable or disable CEC bus monitoring." + System.Environment.NewLine +
-        "[log] {1 - 31}            change the log level. see cectypes.h for values." + System.Environment.NewLine +
-        System.Environment.NewLine +
-        "[ping]                    send a ping command to the CEC adapter." + System.Environment.NewLine +
-        "[bl]                      to let the adapter enter the bootloader, to upgrade" + System.Environment.NewLine +
-        "                          the flash rom." + System.Environment.NewLine +
-        "[r]                       reconnect to the CEC adapter." + System.Environment.NewLine +
-        "[h] or [help]             show this help." + System.Environment.NewLine +
-        "[q] or [quit]             to quit the CEC test client and switch off all" + System.Environment.NewLine +
-        "                          connected CEC devices." + System.Environment.NewLine +
+        "================================================================================" + Environment.NewLine +
+        "Available commands:" + Environment.NewLine +
+        Environment.NewLine +
+        "[tx] {bytes}              transfer bytes over the CEC line." + Environment.NewLine +
+        "[txn] {bytes}             transfer bytes but don't wait for transmission ACK." + Environment.NewLine +
+        "[on] {address}            power on the device with the given logical address." + Environment.NewLine +
+        "[standby] {address}       put the device with the given address in standby mode." + Environment.NewLine +
+        "[la] {logical_address}    change the logical address of the CEC adapter." + Environment.NewLine +
+        "[pa] {physical_address}   change the physical address of the CEC adapter." + Environment.NewLine +
+        "[osd] {addr} {string}     set OSD message on the specified device." + Environment.NewLine +
+        "[ver] {addr}              get the CEC version of the specified device." + Environment.NewLine +
+        "[ven] {addr}              get the vendor ID of the specified device." + Environment.NewLine +
+        "[lang] {addr}             get the menu language of the specified device." + Environment.NewLine +
+        "[pow] {addr}              get the power status of the specified device." + Environment.NewLine +
+        "[poll] {addr}             poll the specified device." + Environment.NewLine +
+        "[scan]                    scan the CEC bus and display device info" + Environment.NewLine +
+        "[mon] {1|0}               enable or disable CEC bus monitoring." + Environment.NewLine +
+        "[log] {1 - 31}            change the log level. see cectypes.h for values." + Environment.NewLine +
+        "[ping]                    send a ping command to the CEC adapter." + Environment.NewLine +
+        "[bl]                      to let the adapter enter the bootloader, to upgrade" + Environment.NewLine +
+        "                          the flash rom." + Environment.NewLine +
+        "[r]                       reconnect to the CEC adapter." + Environment.NewLine +
+        "[h] or [help]             show this help." + Environment.NewLine +
+        "[q] or [quit]             to quit the CEC test client and switch off all" + Environment.NewLine +
+        "                          connected CEC devices." + Environment.NewLine +
         "================================================================================");
     }
 
     public void MainLoop()
     {
-      Lib.PowerOnDevices(CecLogicalAddress.Tv);
-      FlushLog();
-
-      Lib.SetActiveSource(CecDeviceType.PlaybackDevice);
-      FlushLog();
-
       bool bContinue = true;
       string command;
       while (bContinue)
@@ -189,7 +167,7 @@ namespace CecSharpClient
         Console.WriteLine("waiting for input");
 
         command = Console.ReadLine();
-        if (command.Length == 0)
+        if (command != null && command.Length == 0)
           continue;
         string[] splitCommand = command.Split(' ');
         if (splitCommand[0] == "tx" || splitCommand[0] == "txn")
@@ -239,7 +217,7 @@ namespace CecSharpClient
         else if (splitCommand[0] == "pa")
         {
           if (splitCommand.Length > 1)
-            Lib.SetPhysicalAddress(short.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
+            Lib.SetPhysicalAddress(ushort.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
         }
         else if (splitCommand[0] == "osd")
         {
@@ -280,8 +258,8 @@ namespace CecSharpClient
         {
           if (splitCommand.Length > 1)
           {
-            ulong vendor = Lib.GetDeviceVendorId((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
-            Console.WriteLine("Vendor ID: " + vendor);
+            CecVendorId vendor = Lib.GetDeviceVendorId((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
+            Console.WriteLine("Vendor ID: " + Lib.ToString(vendor));
           }
         }
         else if (splitCommand[0] == "ver")
@@ -289,27 +267,7 @@ namespace CecSharpClient
           if (splitCommand.Length > 1)
           {
             CecVersion version = Lib.GetDeviceCecVersion((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
-            switch (version)
-            {
-              case CecVersion.V1_2:
-                Console.WriteLine("CEC version 1.2");
-                break;
-              case CecVersion.V1_2A:
-                Console.WriteLine("CEC version 1.2a");
-                break;
-              case CecVersion.V1_3:
-                Console.WriteLine("CEC version 1.3");
-                break;
-              case CecVersion.V1_3A:
-                Console.WriteLine("CEC version 1.3a");
-                break;
-              case CecVersion.V1_4:
-                Console.WriteLine("CEC version 1.4");
-                break;
-              default:
-                Console.WriteLine("unknown CEC version");
-                break;
-            }
+            Console.WriteLine("CEC version: " + Lib.ToString(version));
           }
         }
         else if (splitCommand[0] == "pow")
@@ -317,47 +275,60 @@ namespace CecSharpClient
           if (splitCommand.Length > 1)
           {
             CecPowerStatus power = Lib.GetDevicePowerStatus((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
-            switch (power)
-            {
-              case CecPowerStatus.On:
-                Console.WriteLine("powered on");
-                break;
-              case CecPowerStatus.InTransitionOnToStandby:
-                Console.WriteLine("on -> standby");
-                break;
-              case CecPowerStatus.InTransitionStandbyToOn:
-                Console.WriteLine("standby -> on");
-                break;
-              case CecPowerStatus.Standby:
-                Console.WriteLine("standby");
-                break;
-              default:
-                Console.WriteLine("unknown power status");
-                break;
-            }
+            Console.WriteLine("power status: " + Lib.ToString(power));
           }
         }
         else if (splitCommand[0] == "r")
         {
           Console.WriteLine("closing the connection");
           Lib.Close();
-          FlushLog();
 
           Console.WriteLine("opening a new connection");
           Connect(10000);
-          FlushLog();
 
           Console.WriteLine("setting active source");
           Lib.SetActiveSource(CecDeviceType.PlaybackDevice);
         }
+        else if (splitCommand[0] == "scan")
+        {
+          Console.WriteLine("CEC bus information");
+          Console.WriteLine("===================");
+          CecLogicalAddresses addresses = Lib.GetActiveDevices();
+          for (int iPtr = 0; iPtr < addresses.Addresses.Length; iPtr++)
+          {
+            CecLogicalAddress address = (CecLogicalAddress)iPtr;
+            if (!addresses.IsSet(address))
+              continue;
+
+            CecVendorId iVendorId = Lib.GetDeviceVendorId(address);
+            bool bActive = Lib.IsActiveDevice(address);
+            ushort iPhysicalAddress = Lib.GetDevicePhysicalAddress(address);
+            string strAddr = string.Format("{0,4:X}", iPhysicalAddress);
+            CecVersion iCecVersion = Lib.GetDeviceCecVersion(address);
+            CecPowerStatus power = Lib.GetDevicePowerStatus(address);
+            string osdName = Lib.GetDeviceOSDName(address);
+            string lang = Lib.GetDeviceMenuLanguage(address);
+
+            StringBuilder output = new StringBuilder();
+            output.AppendLine("device #" + iPtr + ": " + Lib.ToString(address));
+            output.AppendLine("address:       " + strAddr);
+            output.AppendLine("active source: " + (bActive ? "yes" : "no"));
+            output.AppendLine("vendor:        " + Lib.ToString(iVendorId));
+            output.AppendLine("osd string:    " + osdName);
+            output.AppendLine("CEC version:   " + Lib.ToString(iCecVersion));
+            output.AppendLine("power status:  " + Lib.ToString(power));
+            if (!string.IsNullOrEmpty(lang))
+              output.AppendLine("language:      " + lang);
+
+            Console.WriteLine(output.ToString());
+          }
+        }
         else if (splitCommand[0] == "h" || splitCommand[0] == "help")
           ShowConsoleHelp();
         else if (splitCommand[0] == "q" || splitCommand[0] == "quit")
           bContinue = false;
         else if (splitCommand[0] == "log" && splitCommand.Length > 1)
-          LogLevel = int.Parse(splitCommand[1]);
-
-        FlushLog();
+          LogLevel = int.Parse(splitCommand[1]);        
       }
     }
 
@@ -372,10 +343,10 @@ namespace CecSharpClient
       {
         Console.WriteLine("Could not open a connection to the CEC adapter");
       }
-      p.FlushLog();
     }
 
-    private int         LogLevel;
-    private LibCecSharp Lib;
+    private int                 LogLevel;
+    private LibCecSharp         Lib;
+    private LibCECConfiguration Config;
   }
 }