Server/CecClient.cs
author Stephane Lenclud
Sun, 17 Jul 2016 19:35:29 +0200
changeset 203 ce63ebb51da4
parent 202 8784c59c784e
child 206 33be8cb90c57
permissions -rw-r--r--
Published V0.9.1.0
Fixing libcec to prevent turning back on TV after standby.
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@202
    36
using System.Threading;
Stephane@161
    37
Stephane@161
    38
namespace Cec
Stephane@161
    39
{
Stephane@161
    40
    class Client : CecCallbackMethods
Stephane@161
    41
    {
StephaneLenclud@167
    42
        /// <summary>
StephaneLenclud@167
    43
        /// 
StephaneLenclud@167
    44
        /// </summary>
StephaneLenclud@167
    45
        /// <param name="aDeviceName"></param>
StephaneLenclud@167
    46
        /// <param name="aHdmiPort"></param>
Stephane@203
    47
        public Client(string aDeviceName, byte aHdmiPort, CecDeviceType aDeviceType = CecDeviceType.PlaybackDevice, CecLogLevel aLogLevel = CecLogLevel.Warning)
Stephane@161
    48
        {
Stephane@161
    49
            Config = new LibCECConfiguration();
Stephane@203
    50
            Config.DeviceTypes.Types[0] = aDeviceType;
StephaneLenclud@167
    51
            Config.DeviceName = aDeviceName;
StephaneLenclud@167
    52
            Config.HDMIPort = aHdmiPort;
Stephane@161
    53
            //Config.ClientVersion = LibCECConfiguration.CurrentVersion;
Stephane@161
    54
            Config.SetCallbacks(this);
Stephane@202
    55
            LogLevel = (int)aLogLevel;
Stephane@161
    56
StephaneLenclud@167
    57
            iLib = new LibCecSharp(Config);
StephaneLenclud@167
    58
            iLib.InitVideoStandalone();
Stephane@161
    59
Stephane@161
    60
            //Console.WriteLine("CEC Parser created - libCEC version " + Lib.VersionToString(Config.ServerVersion));
Stephane@161
    61
            Console.WriteLine("CEC Parser created - libCEC version " + Config.ServerVersion);
Stephane@161
    62
        }
Stephane@161
    63
Stephane@200
    64
StephaneLenclud@201
    65
        /// <summary>
StephaneLenclud@201
    66
        /// 
StephaneLenclud@201
    67
        /// </summary>
StephaneLenclud@201
    68
        /// <param name="alert"></param>
StephaneLenclud@201
    69
        /// <param name="data"></param>
StephaneLenclud@201
    70
        /// <returns></returns>
Stephane@202
    71
        public override int ReceiveAlert(CecAlert alert, CecParameter data)
Stephane@200
    72
        {
StephaneLenclud@201
    73
            string log = "CEC alert: " + alert.ToString();
StephaneLenclud@201
    74
            if (data != null && data.Type == CecParameterType.ParameterTypeString)
StephaneLenclud@201
    75
            {
StephaneLenclud@201
    76
                log += " " + data.Data;
StephaneLenclud@201
    77
            }
StephaneLenclud@201
    78
StephaneLenclud@201
    79
            Console.WriteLine(log);
StephaneLenclud@201
    80
StephaneLenclud@201
    81
            Close();
StephaneLenclud@201
    82
            //Try reconnect
StephaneLenclud@201
    83
            Connect(1000);
Stephane@200
    84
            return 1;
Stephane@200
    85
        }
Stephane@200
    86
StephaneLenclud@201
    87
        /// <summary>
StephaneLenclud@201
    88
        /// 
StephaneLenclud@201
    89
        /// </summary>
StephaneLenclud@201
    90
        /// <param name="newVal"></param>
StephaneLenclud@201
    91
        /// <returns></returns>
Stephane@202
    92
        public override int ReceiveMenuStateChange(CecMenuState newVal)
Stephane@200
    93
        {
StephaneLenclud@201
    94
            Console.WriteLine("CEC menu state changed to: " + iLib.ToString(newVal));
Stephane@200
    95
            return 1;
Stephane@200
    96
        }
Stephane@200
    97
StephaneLenclud@201
    98
        /// <summary>
StephaneLenclud@201
    99
        /// 
StephaneLenclud@201
   100
        /// </summary>
StephaneLenclud@201
   101
        /// <param name="logicalAddress"></param>
StephaneLenclud@201
   102
        /// <param name="activated"></param>
Stephane@202
   103
        public override void SourceActivated(CecLogicalAddress logicalAddress, bool activated)
Stephane@200
   104
        {
StephaneLenclud@201
   105
            Console.WriteLine("CEC source activated: " + iLib.ToString(logicalAddress) + "/" + activated.ToString() );
Stephane@200
   106
            return;
Stephane@200
   107
        }
Stephane@200
   108
Stephane@161
   109
        public override int ReceiveCommand(CecCommand command)
Stephane@161
   110
        {
StephaneLenclud@201
   111
            Console.WriteLine(string.Format("CEC command Src:{0} Dst:{1} Ack: {2} Eom: {3} OpcodeSet: {4} Opcode: {5} Timeout: {6}",
StephaneLenclud@201
   112
                iLib.ToString(command.Initiator),
StephaneLenclud@201
   113
                iLib.ToString(command.Destination),
StephaneLenclud@201
   114
                command.Ack.ToString(),
StephaneLenclud@201
   115
                command.Eom.ToString(),
StephaneLenclud@201
   116
                command.OpcodeSet.ToString(),
StephaneLenclud@201
   117
                iLib.ToString(command.Opcode),
StephaneLenclud@201
   118
                command.TransmitTimeout.ToString()
StephaneLenclud@201
   119
                ));
Stephane@161
   120
            return 1;
Stephane@161
   121
        }
Stephane@161
   122
Stephane@161
   123
        public override int ReceiveKeypress(CecKeypress key)
Stephane@161
   124
        {
StephaneLenclud@201
   125
            Console.WriteLine(string.Format("CEC keypress: {0} Duration:{1} Empty: {2}",
StephaneLenclud@201
   126
                key.Keycode.ToString(), key.Duration.ToString(), key.Empty.ToString()));
Stephane@161
   127
            return 1;
Stephane@161
   128
        }
Stephane@161
   129
Stephane@161
   130
        public override int ReceiveLogMessage(CecLogMessage message)
Stephane@161
   131
        {
Stephane@161
   132
            if (((int)message.Level & LogLevel) == (int)message.Level)
Stephane@161
   133
            {
Stephane@161
   134
                string strLevel = "";
Stephane@161
   135
                switch (message.Level)
Stephane@161
   136
                {
Stephane@161
   137
                    case CecLogLevel.Error:
Stephane@202
   138
                        strLevel = "ERROR: ";
Stephane@161
   139
                        break;
Stephane@161
   140
                    case CecLogLevel.Warning:
Stephane@161
   141
                        strLevel = "WARNING: ";
Stephane@161
   142
                        break;
Stephane@161
   143
                    case CecLogLevel.Notice:
Stephane@202
   144
                        strLevel = "NOTICE: ";
Stephane@161
   145
                        break;
Stephane@161
   146
                    case CecLogLevel.Traffic:
Stephane@161
   147
                        strLevel = "TRAFFIC: ";
Stephane@161
   148
                        break;
Stephane@161
   149
                    case CecLogLevel.Debug:
Stephane@202
   150
                        strLevel = "DEBUG: ";
Stephane@161
   151
                        break;
Stephane@161
   152
                    default:
Stephane@161
   153
                        break;
Stephane@161
   154
                }
Stephane@202
   155
                string strLog = string.Format("{0} {1} {2}", strLevel, message.Time, message.Message);
Stephane@161
   156
                Console.WriteLine(strLog);
StephaneLenclud@201
   157
                
Stephane@161
   158
            }
Stephane@161
   159
            return 1;
Stephane@161
   160
        }
Stephane@161
   161
Stephane@161
   162
        /// <summary>
Stephane@161
   163
        /// 
Stephane@161
   164
        /// </summary>
Stephane@161
   165
        /// <param name="timeout"></param>
Stephane@161
   166
        /// <returns></returns>
Stephane@161
   167
        public bool Connect(int timeout)
Stephane@161
   168
        {
StephaneLenclud@201
   169
            Close();         
StephaneLenclud@167
   170
            CecAdapter[] adapters = iLib.FindAdapters(string.Empty);
Stephane@161
   171
            if (adapters.Length > 0)
StephaneLenclud@201
   172
            {
StephaneLenclud@201
   173
                Connect(adapters[0].ComPort, timeout);                
StephaneLenclud@201
   174
            }                
Stephane@161
   175
            else
Stephane@161
   176
            {
StephaneLenclud@201
   177
                Console.WriteLine("CEC did not find any adapters");
Stephane@161
   178
            }
StephaneLenclud@201
   179
StephaneLenclud@201
   180
            return iConnected;
Stephane@161
   181
        }
Stephane@161
   182
Stephane@161
   183
        public bool Connect(string port, int timeout)
Stephane@161
   184
        {
StephaneLenclud@201
   185
            Close();
StephaneLenclud@201
   186
            iConnected = iLib.Open(port, timeout);
StephaneLenclud@201
   187
            if (iConnected)
StephaneLenclud@201
   188
            {
StephaneLenclud@201
   189
                Scan();
Stephane@202
   190
                //TestSendKey();
StephaneLenclud@201
   191
            }
StephaneLenclud@201
   192
            return iConnected;
Stephane@161
   193
        }
Stephane@161
   194
Stephane@161
   195
        public void Close()
StephaneLenclud@201
   196
        {            
StephaneLenclud@167
   197
            iLib.Close();
StephaneLenclud@201
   198
            iConnected = false;
Stephane@161
   199
        }
Stephane@161
   200
Stephane@202
   201
Stephane@202
   202
        public void TestSendKey()
Stephane@202
   203
        {
Stephane@202
   204
            //SetupMenu: opens option menu
Stephane@202
   205
            //RootMenu: opens Android home menu
Stephane@202
   206
            //ContentsMenu: nop
Stephane@202
   207
            //FavoriteMenu: nop
Stephane@202
   208
Stephane@202
   209
            //Philips TopMenu = 16
Stephane@202
   210
            //Philips PopupMenu = 17
Stephane@202
   211
Stephane@202
   212
            //bool res = iLib.SendKeypress(CecLogicalAddress.Tv, CecUserControlCode.DisplayInformation, true);
Stephane@202
   213
            //Thread.Sleep(3000); //Wait few seconds for menu to open
Stephane@202
   214
                                //res = iLib.SendKeypress(CecLogicalAddress.Tv, CecUserControlCode.SetupMenu, true);
Stephane@202
   215
Stephane@202
   216
            for (int i = 0; i < 256; i++)
Stephane@202
   217
            {
Stephane@202
   218
                //Thread.Sleep(100);
Stephane@202
   219
                //res = iLib.SendKeyRelease(CecLogicalAddress.Tv, true);
Stephane@202
   220
                //Thread.Sleep(100);
Stephane@202
   221
                switch ((CecUserControlCode)i)
Stephane@202
   222
                {
Stephane@202
   223
                    case CecUserControlCode.Power:
Stephane@202
   224
                    case CecUserControlCode.PowerOffFunction:
Stephane@202
   225
                    case CecUserControlCode.PowerOnFunction:
Stephane@202
   226
                    case CecUserControlCode.PowerToggleFunction:
Stephane@202
   227
                    case CecUserControlCode.ElectronicProgramGuide:
Stephane@202
   228
                    case CecUserControlCode.InputSelect:
Stephane@202
   229
                    case CecUserControlCode.RootMenu:
Stephane@202
   230
Stephane@202
   231
                        break;
Stephane@202
   232
Stephane@202
   233
                    default:
Stephane@202
   234
                        Console.WriteLine(i.ToString());
Stephane@202
   235
                        Thread.Sleep(1000);
Stephane@202
   236
                        iLib.SendKeypress(CecLogicalAddress.Tv, (CecUserControlCode)i, true);
Stephane@202
   237
                        
Stephane@202
   238
                        break;
Stephane@202
   239
Stephane@202
   240
                }
Stephane@202
   241
                
Stephane@202
   242
                //
Stephane@202
   243
            }
Stephane@202
   244
Stephane@202
   245
Stephane@202
   246
            for (int i=0;i<7;i++)
Stephane@202
   247
            {
Stephane@202
   248
                //Thread.Sleep(100);
Stephane@202
   249
                //res = iLib.SendKeyRelease(CecLogicalAddress.Tv, true);
Stephane@202
   250
                //Thread.Sleep(100);
Stephane@202
   251
                //res = iLib.SendKeypress(CecLogicalAddress.Tv, CecUserControlCode.Down, true);
Stephane@202
   252
                //
Stephane@202
   253
            }
Stephane@202
   254
Stephane@202
   255
            //res = iLib.SendKeypress(CecLogicalAddress.Tv, CecUserControlCode.Select, true);
Stephane@202
   256
Stephane@202
   257
            //res = iLib.SendKeypress(CecLogicalAddress.Tv, CecUserControlCode.Select, true);
Stephane@202
   258
            //res = iLib.SendKeyRelease(CecLogicalAddress.Tv, true);
Stephane@202
   259
        }
Stephane@202
   260
StephaneLenclud@201
   261
        /// <summary>
StephaneLenclud@201
   262
        /// 
StephaneLenclud@201
   263
        /// </summary>
StephaneLenclud@201
   264
        public void Scan()
StephaneLenclud@201
   265
        {
StephaneLenclud@201
   266
            Console.WriteLine("CEC bus information");
StephaneLenclud@201
   267
            Console.WriteLine("===================");
StephaneLenclud@201
   268
            CecLogicalAddresses addresses = Lib.GetActiveDevices();
StephaneLenclud@201
   269
            for (int iPtr = 0; iPtr < addresses.Addresses.Length; iPtr++)
StephaneLenclud@201
   270
            {
StephaneLenclud@201
   271
                CecLogicalAddress address = (CecLogicalAddress) iPtr;
StephaneLenclud@201
   272
                if (!addresses.IsSet(address))
StephaneLenclud@201
   273
                    continue;
StephaneLenclud@201
   274
StephaneLenclud@201
   275
                CecVendorId iVendorId = Lib.GetDeviceVendorId(address);
StephaneLenclud@201
   276
                bool bActive = Lib.IsActiveDevice(address);
StephaneLenclud@201
   277
                ushort iPhysicalAddress = Lib.GetDevicePhysicalAddress(address);
StephaneLenclud@201
   278
                string strAddr = Lib.PhysicalAddressToString(iPhysicalAddress);
StephaneLenclud@201
   279
                CecVersion iCecVersion = Lib.GetDeviceCecVersion(address);
StephaneLenclud@201
   280
                CecPowerStatus power = Lib.GetDevicePowerStatus(address);
StephaneLenclud@201
   281
                string osdName = Lib.GetDeviceOSDName(address);
StephaneLenclud@201
   282
                string lang = Lib.GetDeviceMenuLanguage(address);
StephaneLenclud@201
   283
StephaneLenclud@201
   284
                Console.WriteLine("device #" + iPtr + ": " + Lib.ToString(address));
StephaneLenclud@201
   285
                Console.WriteLine("address:       " + strAddr);
StephaneLenclud@201
   286
                Console.WriteLine("active source: " + (bActive ? "yes" : "no"));
StephaneLenclud@201
   287
                Console.WriteLine("vendor:        " + Lib.ToString(iVendorId));
StephaneLenclud@201
   288
                Console.WriteLine("osd string:    " + osdName);
StephaneLenclud@201
   289
                Console.WriteLine("CEC version:   " + Lib.ToString(iCecVersion));
StephaneLenclud@201
   290
                Console.WriteLine("power status:  " + Lib.ToString(power));
StephaneLenclud@201
   291
                if (!string.IsNullOrEmpty(lang))
StephaneLenclud@201
   292
                    Console.WriteLine("language:      " + lang);
StephaneLenclud@201
   293
                Console.WriteLine("");
StephaneLenclud@201
   294
            }
StephaneLenclud@201
   295
        }
StephaneLenclud@201
   296
StephaneLenclud@201
   297
        public void ListAdapters()
Stephane@161
   298
        {
Stephane@161
   299
            int iAdapter = 0;
StephaneLenclud@167
   300
            foreach (CecAdapter adapter in iLib.FindAdapters(string.Empty))
Stephane@161
   301
            {
Stephane@161
   302
                Console.WriteLine("Adapter:    " + iAdapter++);
Stephane@161
   303
                Console.WriteLine("Path:         " + adapter.Path);
Stephane@161
   304
                Console.WriteLine("Com port: " + adapter.ComPort);
Stephane@161
   305
            }
Stephane@161
   306
        }
Stephane@161
   307
Stephane@161
   308
        void ShowConsoleHelp()
Stephane@161
   309
        {
Stephane@161
   310
            Console.WriteLine(
Stephane@161
   311
                "================================================================================" + Environment.NewLine +
Stephane@161
   312
                "Available commands:" + Environment.NewLine +
Stephane@161
   313
                Environment.NewLine +
Stephane@161
   314
                "[tx] {bytes}                            transfer bytes over the CEC line." + Environment.NewLine +
Stephane@161
   315
                "[txn] {bytes}                         transfer bytes but don't wait for transmission ACK." + Environment.NewLine +
Stephane@161
   316
                "[on] {address}                        power on the device with the given logical address." + Environment.NewLine +
Stephane@161
   317
                "[standby] {address}             put the device with the given address in standby mode." + Environment.NewLine +
Stephane@161
   318
                "[la] {logical_address}        change the logical address of the CEC adapter." + Environment.NewLine +
Stephane@161
   319
                "[pa] {physical_address}     change the physical address of the CEC adapter." + Environment.NewLine +
Stephane@161
   320
                "[osd] {addr} {string}         set OSD message on the specified device." + Environment.NewLine +
Stephane@161
   321
                "[ver] {addr}                            get the CEC version of the specified device." + Environment.NewLine +
Stephane@161
   322
                "[ven] {addr}                            get the vendor ID of the specified device." + Environment.NewLine +
Stephane@161
   323
                "[lang] {addr}                         get the menu language of the specified device." + Environment.NewLine +
Stephane@161
   324
                "[pow] {addr}                            get the power status of the specified device." + Environment.NewLine +
Stephane@161
   325
                "[poll] {addr}                         poll the specified device." + Environment.NewLine +
Stephane@161
   326
                "[scan]                                        scan the CEC bus and display device info" + Environment.NewLine +
Stephane@161
   327
                "[mon] {1|0}                             enable or disable CEC bus monitoring." + Environment.NewLine +
Stephane@161
   328
                "[log] {1 - 31}                        change the log level. see cectypes.h for values." + Environment.NewLine +
Stephane@161
   329
                "[ping]                                        send a ping command to the CEC adapter." + Environment.NewLine +
Stephane@161
   330
                "[bl]                                            to let the adapter enter the bootloader, to upgrade" + Environment.NewLine +
Stephane@161
   331
                "                                                    the flash rom." + Environment.NewLine +
Stephane@161
   332
                "[r]                                             reconnect to the CEC adapter." + Environment.NewLine +
Stephane@161
   333
                "[h] or [help]                         show this help." + Environment.NewLine +
Stephane@161
   334
                "[q] or [quit]                         to quit the CEC test client and switch off all" + Environment.NewLine +
Stephane@161
   335
                "                                                    connected CEC devices." + Environment.NewLine +
Stephane@161
   336
                "================================================================================");
Stephane@161
   337
        }
Stephane@161
   338
Stephane@161
   339
        public void MainLoop()
Stephane@161
   340
        {
Stephane@161
   341
            bool bContinue = true;
Stephane@161
   342
            string command;
Stephane@161
   343
            while (bContinue)
Stephane@161
   344
            {
Stephane@161
   345
                Console.WriteLine("waiting for input");
Stephane@161
   346
Stephane@161
   347
                command = Console.ReadLine();
Stephane@161
   348
                if (command != null && command.Length == 0)
Stephane@161
   349
                    continue;
Stephane@161
   350
                string[] splitCommand = command.Split(' ');
Stephane@161
   351
                if (splitCommand[0] == "tx" || splitCommand[0] == "txn")
Stephane@161
   352
                {
Stephane@161
   353
                    CecCommand bytes = new CecCommand();
Stephane@161
   354
                    for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
Stephane@161
   355
                    {
Stephane@161
   356
                        bytes.PushBack(byte.Parse(splitCommand[iPtr], System.Globalization.NumberStyles.HexNumber));
Stephane@161
   357
                    }
Stephane@161
   358
Stephane@161
   359
                    if (command == "txn")
Stephane@161
   360
                        bytes.TransmitTimeout = 0;
Stephane@161
   361
StephaneLenclud@167
   362
                    iLib.Transmit(bytes);
Stephane@161
   363
                }
Stephane@161
   364
                else if (splitCommand[0] == "on")
Stephane@161
   365
                {
Stephane@161
   366
                    if (splitCommand.Length > 1)
StephaneLenclud@167
   367
                        iLib.PowerOnDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
Stephane@161
   368
                    else
StephaneLenclud@167
   369
                        iLib.PowerOnDevices(CecLogicalAddress.Broadcast);
Stephane@161
   370
                }
Stephane@161
   371
                else if (splitCommand[0] == "standby")
Stephane@161
   372
                {
Stephane@161
   373
                    if (splitCommand.Length > 1)
StephaneLenclud@167
   374
                        iLib.StandbyDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
Stephane@161
   375
                    else
StephaneLenclud@167
   376
                        iLib.StandbyDevices(CecLogicalAddress.Broadcast);
Stephane@161
   377
                }
Stephane@161
   378
                else if (splitCommand[0] == "poll")
Stephane@161
   379
                {
Stephane@161
   380
                    bool bSent = false;
Stephane@161
   381
                    if (splitCommand.Length > 1)
StephaneLenclud@167
   382
                        bSent = iLib.PollDevice((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
Stephane@161
   383
                    else
StephaneLenclud@167
   384
                        bSent = iLib.PollDevice(CecLogicalAddress.Broadcast);
Stephane@161
   385
                    if (bSent)
Stephane@161
   386
                        Console.WriteLine("POLL message sent");
Stephane@161
   387
                    else
Stephane@161
   388
                        Console.WriteLine("POLL message not sent");
Stephane@161
   389
                }
Stephane@161
   390
                else if (splitCommand[0] == "la")
Stephane@161
   391
                {
Stephane@161
   392
                    if (splitCommand.Length > 1)
StephaneLenclud@167
   393
                        iLib.SetLogicalAddress((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
Stephane@161
   394
                }
Stephane@161
   395
                else if (splitCommand[0] == "pa")
Stephane@161
   396
                {
Stephane@161
   397
                    if (splitCommand.Length > 1)
StephaneLenclud@167
   398
                        iLib.SetPhysicalAddress(ushort.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
Stephane@161
   399
                }
Stephane@161
   400
                else if (splitCommand[0] == "osd")
Stephane@161
   401
                {
Stephane@161
   402
                    if (splitCommand.Length > 2)
Stephane@161
   403
                    {
Stephane@161
   404
                        StringBuilder osdString = new StringBuilder();
Stephane@161
   405
                        for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
Stephane@161
   406
                        {
Stephane@161
   407
                            osdString.Append(splitCommand[iPtr]);
Stephane@161
   408
                            if (iPtr != splitCommand.Length - 1)
Stephane@161
   409
                                osdString.Append(" ");
Stephane@161
   410
                        }
StephaneLenclud@167
   411
                        iLib.SetOSDString((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber), CecDisplayControl.DisplayForDefaultTime, osdString.ToString());
Stephane@161
   412
                    }
Stephane@161
   413
                }
Stephane@161
   414
                else if (splitCommand[0] == "ping")
Stephane@161
   415
                {
StephaneLenclud@167
   416
                    iLib.PingAdapter();
Stephane@161
   417
                }
Stephane@161
   418
                else if (splitCommand[0] == "mon")
Stephane@161
   419
                {
Stephane@161
   420
                    bool enable = splitCommand.Length > 1 ? splitCommand[1] == "1" : false;
StephaneLenclud@167
   421
                    iLib.SwitchMonitoring(enable);
Stephane@161
   422
                }
Stephane@161
   423
                else if (splitCommand[0] == "bl")
Stephane@161
   424
                {
StephaneLenclud@167
   425
                    iLib.StartBootloader();
Stephane@161
   426
                }
Stephane@161
   427
                else if (splitCommand[0] == "lang")
Stephane@161
   428
                {
Stephane@161
   429
                    if (splitCommand.Length > 1)
Stephane@161
   430
                    {
StephaneLenclud@167
   431
                        string language = iLib.GetDeviceMenuLanguage((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
Stephane@161
   432
                        Console.WriteLine("Menu language: " + language);
Stephane@161
   433
                    }
Stephane@161
   434
                }
Stephane@161
   435
                else if (splitCommand[0] == "ven")
Stephane@161
   436
                {
Stephane@161
   437
                    if (splitCommand.Length > 1)
Stephane@161
   438
                    {
StephaneLenclud@167
   439
                        CecVendorId vendor = iLib.GetDeviceVendorId((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
StephaneLenclud@167
   440
                        Console.WriteLine("Vendor ID: " + iLib.ToString(vendor));
Stephane@161
   441
                    }
Stephane@161
   442
                }
Stephane@161
   443
                else if (splitCommand[0] == "ver")
Stephane@161
   444
                {
Stephane@161
   445
                    if (splitCommand.Length > 1)
Stephane@161
   446
                    {
StephaneLenclud@167
   447
                        CecVersion version = iLib.GetDeviceCecVersion((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
StephaneLenclud@167
   448
                        Console.WriteLine("CEC version: " + iLib.ToString(version));
Stephane@161
   449
                    }
Stephane@161
   450
                }
Stephane@161
   451
                else if (splitCommand[0] == "pow")
Stephane@161
   452
                {
Stephane@161
   453
                    if (splitCommand.Length > 1)
Stephane@161
   454
                    {
StephaneLenclud@167
   455
                        CecPowerStatus power = iLib.GetDevicePowerStatus((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
StephaneLenclud@167
   456
                        Console.WriteLine("power status: " + iLib.ToString(power));
Stephane@161
   457
                    }
Stephane@161
   458
                }
Stephane@161
   459
                else if (splitCommand[0] == "r")
Stephane@161
   460
                {
Stephane@161
   461
                    Console.WriteLine("closing the connection");
StephaneLenclud@167
   462
                    iLib.Close();
Stephane@161
   463
Stephane@161
   464
                    Console.WriteLine("opening a new connection");
Stephane@161
   465
                    Connect(10000);
Stephane@161
   466
Stephane@161
   467
                    Console.WriteLine("setting active source");
StephaneLenclud@167
   468
                    iLib.SetActiveSource(CecDeviceType.PlaybackDevice);
Stephane@161
   469
                }
Stephane@161
   470
                else if (splitCommand[0] == "scan")
Stephane@161
   471
                {
Stephane@161
   472
                    StringBuilder output = new StringBuilder();
Stephane@161
   473
                    output.AppendLine("CEC bus information");
Stephane@161
   474
                    output.AppendLine("===================");
StephaneLenclud@167
   475
                    CecLogicalAddresses addresses = iLib.GetActiveDevices();
Stephane@161
   476
                    for (int iPtr = 0; iPtr < addresses.Addresses.Length; iPtr++)
Stephane@161
   477
                    {
Stephane@161
   478
                        CecLogicalAddress address = (CecLogicalAddress)iPtr;
Stephane@161
   479
                        if (!addresses.IsSet(address))
Stephane@161
   480
                            continue;
Stephane@161
   481
StephaneLenclud@167
   482
                        CecVendorId iVendorId = iLib.GetDeviceVendorId(address);
StephaneLenclud@167
   483
                        bool bActive = iLib.IsActiveDevice(address);
StephaneLenclud@167
   484
                        ushort iPhysicalAddress = iLib.GetDevicePhysicalAddress(address);
Stephane@161
   485
                        string strAddr = "todo: fixme"; //Lib.PhysicalAddressToString(iPhysicalAddress);
StephaneLenclud@167
   486
                        CecVersion iCecVersion = iLib.GetDeviceCecVersion(address);
StephaneLenclud@167
   487
                        CecPowerStatus power = iLib.GetDevicePowerStatus(address);
StephaneLenclud@167
   488
                        string osdName = iLib.GetDeviceOSDName(address);
StephaneLenclud@167
   489
                        string lang = iLib.GetDeviceMenuLanguage(address);
Stephane@161
   490
StephaneLenclud@167
   491
                        output.AppendLine("device #" + iPtr + ": " + iLib.ToString(address));
Stephane@161
   492
                        output.AppendLine("address:             " + strAddr);
Stephane@161
   493
                        output.AppendLine("active source: " + (bActive ? "yes" : "no"));
StephaneLenclud@167
   494
                        output.AppendLine("vendor:                " + iLib.ToString(iVendorId));
Stephane@161
   495
                        output.AppendLine("osd string:        " + osdName);
StephaneLenclud@167
   496
                        output.AppendLine("CEC version:     " + iLib.ToString(iCecVersion));
StephaneLenclud@167
   497
                        output.AppendLine("power status:    " + iLib.ToString(power));
Stephane@161
   498
                        if (!string.IsNullOrEmpty(lang))
Stephane@161
   499
                            output.AppendLine("language:            " + lang);
Stephane@161
   500
                        output.AppendLine("");
Stephane@161
   501
                    }
Stephane@161
   502
                    Console.WriteLine(output.ToString());
Stephane@161
   503
                }
Stephane@161
   504
                else if (splitCommand[0] == "h" || splitCommand[0] == "help")
Stephane@161
   505
                    ShowConsoleHelp();
Stephane@161
   506
                else if (splitCommand[0] == "q" || splitCommand[0] == "quit")
Stephane@161
   507
                    bContinue = false;
Stephane@161
   508
                else if (splitCommand[0] == "log" && splitCommand.Length > 1)
Stephane@161
   509
                    LogLevel = int.Parse(splitCommand[1]);                
Stephane@161
   510
            }
Stephane@161
   511
        }
Stephane@203
   512
       
Stephane@161
   513
        /// <summary>
StephaneLenclud@167
   514
        /// Provide direct access to CEC library
Stephane@161
   515
        /// </summary>
StephaneLenclud@167
   516
        public LibCecSharp Lib
Stephane@161
   517
        {
StephaneLenclud@167
   518
            get
StephaneLenclud@167
   519
            {
StephaneLenclud@167
   520
                return iLib;
StephaneLenclud@167
   521
            }
Stephane@161
   522
        }
Stephane@161
   523
Stephane@161
   524
        /// <summary>
Stephane@161
   525
        /// 
Stephane@161
   526
        /// </summary>
Stephane@161
   527
        private int LogLevel;
StephaneLenclud@167
   528
        /// <summary>
StephaneLenclud@167
   529
        /// 
StephaneLenclud@167
   530
        /// </summary>
StephaneLenclud@167
   531
        private LibCecSharp iLib;
StephaneLenclud@167
   532
        /// <summary>
StephaneLenclud@167
   533
        /// 
StephaneLenclud@167
   534
        /// </summary>
Stephane@161
   535
        private LibCECConfiguration Config;
StephaneLenclud@201
   536
StephaneLenclud@201
   537
        /// <summary>
StephaneLenclud@201
   538
        /// 
StephaneLenclud@201
   539
        /// </summary>
StephaneLenclud@201
   540
        private bool iConnected;
Stephane@161
   541
    }
Stephane@161
   542
}