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