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