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