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