Server/CecClient.cs
changeset 167 d2295c186ce1
parent 161 7b19bea5d73a
child 200 663c1ef0de59
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Server/CecClient.cs	Sat Sep 26 16:35:27 2015 +0200
     1.3 @@ -0,0 +1,385 @@
     1.4 +/*
     1.5 + * This file is part of the libCEC(R) library.
     1.6 + *
     1.7 + * libCEC(R) is Copyright (C) 2011-2013 Pulse-Eight Limited.    All rights reserved.
     1.8 + * libCEC(R) is an original work, containing original code.
     1.9 + *
    1.10 + * libCEC(R) is a trademark of Pulse-Eight Limited.
    1.11 + *
    1.12 + * This program is dual-licensed; you can redistribute it and/or modify
    1.13 + * it under the terms of the GNU General Public License as published by
    1.14 + * the Free Software Foundation; either version 2 of the License, or
    1.15 + * (at your option) any later version.
    1.16 + *
    1.17 + * This program is distributed in the hope that it will be useful,
    1.18 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.19 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.    See the
    1.20 + * GNU General Public License for more details.
    1.21 + *
    1.22 + * You should have received a copy of the GNU General Public License
    1.23 + * along with this program; if not, write to the Free Software
    1.24 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    1.25 + *
    1.26 + *
    1.27 + * Alternatively, you can license this library under a commercial license,
    1.28 + * please contact Pulse-Eight Licensing for more information.
    1.29 + *
    1.30 + * For more information contact:
    1.31 + * Pulse-Eight Licensing             <license@pulse-eight.com>
    1.32 + *         http://www.pulse-eight.com/
    1.33 + *         http://www.pulse-eight.net/
    1.34 + */
    1.35 +
    1.36 +using System;
    1.37 +using System.Text;
    1.38 +using CecSharp;
    1.39 +
    1.40 +namespace Cec
    1.41 +{
    1.42 +    class Client : CecCallbackMethods
    1.43 +    {
    1.44 +        /// <summary>
    1.45 +        /// 
    1.46 +        /// </summary>
    1.47 +        /// <param name="aDeviceName"></param>
    1.48 +        /// <param name="aHdmiPort"></param>
    1.49 +        public Client(string aDeviceName, byte aHdmiPort)
    1.50 +        {
    1.51 +            Config = new LibCECConfiguration();
    1.52 +            Config.DeviceTypes.Types[0] = CecDeviceType.Tv;
    1.53 +            Config.DeviceName = aDeviceName;
    1.54 +            Config.HDMIPort = aHdmiPort;
    1.55 +            //Config.ClientVersion = LibCECConfiguration.CurrentVersion;
    1.56 +            Config.SetCallbacks(this);
    1.57 +            LogLevel = (int)CecLogLevel.All;
    1.58 +
    1.59 +            iLib = new LibCecSharp(Config);
    1.60 +            iLib.InitVideoStandalone();
    1.61 +
    1.62 +            //Console.WriteLine("CEC Parser created - libCEC version " + Lib.VersionToString(Config.ServerVersion));
    1.63 +            Console.WriteLine("CEC Parser created - libCEC version " + Config.ServerVersion);
    1.64 +        }
    1.65 +
    1.66 +        public override int ReceiveCommand(CecCommand command)
    1.67 +        {
    1.68 +            return 1;
    1.69 +        }
    1.70 +
    1.71 +        public override int ReceiveKeypress(CecKeypress key)
    1.72 +        {
    1.73 +            return 1;
    1.74 +        }
    1.75 +
    1.76 +        public override int ReceiveLogMessage(CecLogMessage message)
    1.77 +        {
    1.78 +            if (((int)message.Level & LogLevel) == (int)message.Level)
    1.79 +            {
    1.80 +                string strLevel = "";
    1.81 +                switch (message.Level)
    1.82 +                {
    1.83 +                    case CecLogLevel.Error:
    1.84 +                        strLevel = "ERROR:     ";
    1.85 +                        break;
    1.86 +                    case CecLogLevel.Warning:
    1.87 +                        strLevel = "WARNING: ";
    1.88 +                        break;
    1.89 +                    case CecLogLevel.Notice:
    1.90 +                        strLevel = "NOTICE:    ";
    1.91 +                        break;
    1.92 +                    case CecLogLevel.Traffic:
    1.93 +                        strLevel = "TRAFFIC: ";
    1.94 +                        break;
    1.95 +                    case CecLogLevel.Debug:
    1.96 +                        strLevel = "DEBUG:     ";
    1.97 +                        break;
    1.98 +                    default:
    1.99 +                        break;
   1.100 +                }
   1.101 +                string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message);
   1.102 +                Console.WriteLine(strLog);
   1.103 +            }
   1.104 +            return 1;
   1.105 +        }
   1.106 +
   1.107 +        /// <summary>
   1.108 +        /// 
   1.109 +        /// </summary>
   1.110 +        /// <param name="timeout"></param>
   1.111 +        /// <returns></returns>
   1.112 +        public bool Connect(int timeout)
   1.113 +        {
   1.114 +            CecAdapter[] adapters = iLib.FindAdapters(string.Empty);
   1.115 +            if (adapters.Length > 0)
   1.116 +                return Connect(adapters[0].ComPort, timeout);
   1.117 +            else
   1.118 +            {
   1.119 +                Console.WriteLine("Did not find any CEC adapters");
   1.120 +                return false;
   1.121 +            }
   1.122 +        }
   1.123 +
   1.124 +        public bool Connect(string port, int timeout)
   1.125 +        {
   1.126 +            return iLib.Open(port, timeout);
   1.127 +        }
   1.128 +
   1.129 +        public void Close()
   1.130 +        {
   1.131 +            iLib.Close();
   1.132 +        }
   1.133 +
   1.134 +        public void ListDevices()
   1.135 +        {
   1.136 +            int iAdapter = 0;
   1.137 +            foreach (CecAdapter adapter in iLib.FindAdapters(string.Empty))
   1.138 +            {
   1.139 +                Console.WriteLine("Adapter:    " + iAdapter++);
   1.140 +                Console.WriteLine("Path:         " + adapter.Path);
   1.141 +                Console.WriteLine("Com port: " + adapter.ComPort);
   1.142 +            }
   1.143 +        }
   1.144 +
   1.145 +        void ShowConsoleHelp()
   1.146 +        {
   1.147 +            Console.WriteLine(
   1.148 +                "================================================================================" + Environment.NewLine +
   1.149 +                "Available commands:" + Environment.NewLine +
   1.150 +                Environment.NewLine +
   1.151 +                "[tx] {bytes}                            transfer bytes over the CEC line." + Environment.NewLine +
   1.152 +                "[txn] {bytes}                         transfer bytes but don't wait for transmission ACK." + Environment.NewLine +
   1.153 +                "[on] {address}                        power on the device with the given logical address." + Environment.NewLine +
   1.154 +                "[standby] {address}             put the device with the given address in standby mode." + Environment.NewLine +
   1.155 +                "[la] {logical_address}        change the logical address of the CEC adapter." + Environment.NewLine +
   1.156 +                "[pa] {physical_address}     change the physical address of the CEC adapter." + Environment.NewLine +
   1.157 +                "[osd] {addr} {string}         set OSD message on the specified device." + Environment.NewLine +
   1.158 +                "[ver] {addr}                            get the CEC version of the specified device." + Environment.NewLine +
   1.159 +                "[ven] {addr}                            get the vendor ID of the specified device." + Environment.NewLine +
   1.160 +                "[lang] {addr}                         get the menu language of the specified device." + Environment.NewLine +
   1.161 +                "[pow] {addr}                            get the power status of the specified device." + Environment.NewLine +
   1.162 +                "[poll] {addr}                         poll the specified device." + Environment.NewLine +
   1.163 +                "[scan]                                        scan the CEC bus and display device info" + Environment.NewLine +
   1.164 +                "[mon] {1|0}                             enable or disable CEC bus monitoring." + Environment.NewLine +
   1.165 +                "[log] {1 - 31}                        change the log level. see cectypes.h for values." + Environment.NewLine +
   1.166 +                "[ping]                                        send a ping command to the CEC adapter." + Environment.NewLine +
   1.167 +                "[bl]                                            to let the adapter enter the bootloader, to upgrade" + Environment.NewLine +
   1.168 +                "                                                    the flash rom." + Environment.NewLine +
   1.169 +                "[r]                                             reconnect to the CEC adapter." + Environment.NewLine +
   1.170 +                "[h] or [help]                         show this help." + Environment.NewLine +
   1.171 +                "[q] or [quit]                         to quit the CEC test client and switch off all" + Environment.NewLine +
   1.172 +                "                                                    connected CEC devices." + Environment.NewLine +
   1.173 +                "================================================================================");
   1.174 +        }
   1.175 +
   1.176 +        public void MainLoop()
   1.177 +        {
   1.178 +            bool bContinue = true;
   1.179 +            string command;
   1.180 +            while (bContinue)
   1.181 +            {
   1.182 +                Console.WriteLine("waiting for input");
   1.183 +
   1.184 +                command = Console.ReadLine();
   1.185 +                if (command != null && command.Length == 0)
   1.186 +                    continue;
   1.187 +                string[] splitCommand = command.Split(' ');
   1.188 +                if (splitCommand[0] == "tx" || splitCommand[0] == "txn")
   1.189 +                {
   1.190 +                    CecCommand bytes = new CecCommand();
   1.191 +                    for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
   1.192 +                    {
   1.193 +                        bytes.PushBack(byte.Parse(splitCommand[iPtr], System.Globalization.NumberStyles.HexNumber));
   1.194 +                    }
   1.195 +
   1.196 +                    if (command == "txn")
   1.197 +                        bytes.TransmitTimeout = 0;
   1.198 +
   1.199 +                    iLib.Transmit(bytes);
   1.200 +                }
   1.201 +                else if (splitCommand[0] == "on")
   1.202 +                {
   1.203 +                    if (splitCommand.Length > 1)
   1.204 +                        iLib.PowerOnDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
   1.205 +                    else
   1.206 +                        iLib.PowerOnDevices(CecLogicalAddress.Broadcast);
   1.207 +                }
   1.208 +                else if (splitCommand[0] == "standby")
   1.209 +                {
   1.210 +                    if (splitCommand.Length > 1)
   1.211 +                        iLib.StandbyDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
   1.212 +                    else
   1.213 +                        iLib.StandbyDevices(CecLogicalAddress.Broadcast);
   1.214 +                }
   1.215 +                else if (splitCommand[0] == "poll")
   1.216 +                {
   1.217 +                    bool bSent = false;
   1.218 +                    if (splitCommand.Length > 1)
   1.219 +                        bSent = iLib.PollDevice((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
   1.220 +                    else
   1.221 +                        bSent = iLib.PollDevice(CecLogicalAddress.Broadcast);
   1.222 +                    if (bSent)
   1.223 +                        Console.WriteLine("POLL message sent");
   1.224 +                    else
   1.225 +                        Console.WriteLine("POLL message not sent");
   1.226 +                }
   1.227 +                else if (splitCommand[0] == "la")
   1.228 +                {
   1.229 +                    if (splitCommand.Length > 1)
   1.230 +                        iLib.SetLogicalAddress((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
   1.231 +                }
   1.232 +                else if (splitCommand[0] == "pa")
   1.233 +                {
   1.234 +                    if (splitCommand.Length > 1)
   1.235 +                        iLib.SetPhysicalAddress(ushort.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
   1.236 +                }
   1.237 +                else if (splitCommand[0] == "osd")
   1.238 +                {
   1.239 +                    if (splitCommand.Length > 2)
   1.240 +                    {
   1.241 +                        StringBuilder osdString = new StringBuilder();
   1.242 +                        for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
   1.243 +                        {
   1.244 +                            osdString.Append(splitCommand[iPtr]);
   1.245 +                            if (iPtr != splitCommand.Length - 1)
   1.246 +                                osdString.Append(" ");
   1.247 +                        }
   1.248 +                        iLib.SetOSDString((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber), CecDisplayControl.DisplayForDefaultTime, osdString.ToString());
   1.249 +                    }
   1.250 +                }
   1.251 +                else if (splitCommand[0] == "ping")
   1.252 +                {
   1.253 +                    iLib.PingAdapter();
   1.254 +                }
   1.255 +                else if (splitCommand[0] == "mon")
   1.256 +                {
   1.257 +                    bool enable = splitCommand.Length > 1 ? splitCommand[1] == "1" : false;
   1.258 +                    iLib.SwitchMonitoring(enable);
   1.259 +                }
   1.260 +                else if (splitCommand[0] == "bl")
   1.261 +                {
   1.262 +                    iLib.StartBootloader();
   1.263 +                }
   1.264 +                else if (splitCommand[0] == "lang")
   1.265 +                {
   1.266 +                    if (splitCommand.Length > 1)
   1.267 +                    {
   1.268 +                        string language = iLib.GetDeviceMenuLanguage((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
   1.269 +                        Console.WriteLine("Menu language: " + language);
   1.270 +                    }
   1.271 +                }
   1.272 +                else if (splitCommand[0] == "ven")
   1.273 +                {
   1.274 +                    if (splitCommand.Length > 1)
   1.275 +                    {
   1.276 +                        CecVendorId vendor = iLib.GetDeviceVendorId((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
   1.277 +                        Console.WriteLine("Vendor ID: " + iLib.ToString(vendor));
   1.278 +                    }
   1.279 +                }
   1.280 +                else if (splitCommand[0] == "ver")
   1.281 +                {
   1.282 +                    if (splitCommand.Length > 1)
   1.283 +                    {
   1.284 +                        CecVersion version = iLib.GetDeviceCecVersion((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
   1.285 +                        Console.WriteLine("CEC version: " + iLib.ToString(version));
   1.286 +                    }
   1.287 +                }
   1.288 +                else if (splitCommand[0] == "pow")
   1.289 +                {
   1.290 +                    if (splitCommand.Length > 1)
   1.291 +                    {
   1.292 +                        CecPowerStatus power = iLib.GetDevicePowerStatus((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
   1.293 +                        Console.WriteLine("power status: " + iLib.ToString(power));
   1.294 +                    }
   1.295 +                }
   1.296 +                else if (splitCommand[0] == "r")
   1.297 +                {
   1.298 +                    Console.WriteLine("closing the connection");
   1.299 +                    iLib.Close();
   1.300 +
   1.301 +                    Console.WriteLine("opening a new connection");
   1.302 +                    Connect(10000);
   1.303 +
   1.304 +                    Console.WriteLine("setting active source");
   1.305 +                    iLib.SetActiveSource(CecDeviceType.PlaybackDevice);
   1.306 +                }
   1.307 +                else if (splitCommand[0] == "scan")
   1.308 +                {
   1.309 +                    StringBuilder output = new StringBuilder();
   1.310 +                    output.AppendLine("CEC bus information");
   1.311 +                    output.AppendLine("===================");
   1.312 +                    CecLogicalAddresses addresses = iLib.GetActiveDevices();
   1.313 +                    for (int iPtr = 0; iPtr < addresses.Addresses.Length; iPtr++)
   1.314 +                    {
   1.315 +                        CecLogicalAddress address = (CecLogicalAddress)iPtr;
   1.316 +                        if (!addresses.IsSet(address))
   1.317 +                            continue;
   1.318 +
   1.319 +                        CecVendorId iVendorId = iLib.GetDeviceVendorId(address);
   1.320 +                        bool bActive = iLib.IsActiveDevice(address);
   1.321 +                        ushort iPhysicalAddress = iLib.GetDevicePhysicalAddress(address);
   1.322 +                        string strAddr = "todo: fixme"; //Lib.PhysicalAddressToString(iPhysicalAddress);
   1.323 +                        CecVersion iCecVersion = iLib.GetDeviceCecVersion(address);
   1.324 +                        CecPowerStatus power = iLib.GetDevicePowerStatus(address);
   1.325 +                        string osdName = iLib.GetDeviceOSDName(address);
   1.326 +                        string lang = iLib.GetDeviceMenuLanguage(address);
   1.327 +
   1.328 +                        output.AppendLine("device #" + iPtr + ": " + iLib.ToString(address));
   1.329 +                        output.AppendLine("address:             " + strAddr);
   1.330 +                        output.AppendLine("active source: " + (bActive ? "yes" : "no"));
   1.331 +                        output.AppendLine("vendor:                " + iLib.ToString(iVendorId));
   1.332 +                        output.AppendLine("osd string:        " + osdName);
   1.333 +                        output.AppendLine("CEC version:     " + iLib.ToString(iCecVersion));
   1.334 +                        output.AppendLine("power status:    " + iLib.ToString(power));
   1.335 +                        if (!string.IsNullOrEmpty(lang))
   1.336 +                            output.AppendLine("language:            " + lang);
   1.337 +                        output.AppendLine("");
   1.338 +                    }
   1.339 +                    Console.WriteLine(output.ToString());
   1.340 +                }
   1.341 +                else if (splitCommand[0] == "h" || splitCommand[0] == "help")
   1.342 +                    ShowConsoleHelp();
   1.343 +                else if (splitCommand[0] == "q" || splitCommand[0] == "quit")
   1.344 +                    bContinue = false;
   1.345 +                else if (splitCommand[0] == "log" && splitCommand.Length > 1)
   1.346 +                    LogLevel = int.Parse(splitCommand[1]);                
   1.347 +            }
   1.348 +        }
   1.349 +
   1.350 +        /// TODO: remove that
   1.351 +        static void Main(string[] args)
   1.352 +        {
   1.353 +            Client p = new Client("CEC",2);
   1.354 +            if (p.Connect(10000))
   1.355 +            {
   1.356 +                p.MainLoop();
   1.357 +            }
   1.358 +            else
   1.359 +            {
   1.360 +                Console.WriteLine("Could not open a connection to the CEC adapter");
   1.361 +            }
   1.362 +        }
   1.363 +
   1.364 +        /// <summary>
   1.365 +        /// Provide direct access to CEC library
   1.366 +        /// </summary>
   1.367 +        public LibCecSharp Lib
   1.368 +        {
   1.369 +            get
   1.370 +            {
   1.371 +                return iLib;
   1.372 +            }
   1.373 +        }
   1.374 +
   1.375 +        /// <summary>
   1.376 +        /// 
   1.377 +        /// </summary>
   1.378 +        private int LogLevel;
   1.379 +        /// <summary>
   1.380 +        /// 
   1.381 +        /// </summary>
   1.382 +        private LibCecSharp iLib;
   1.383 +        /// <summary>
   1.384 +        /// 
   1.385 +        /// </summary>
   1.386 +        private LibCECConfiguration Config;
   1.387 +    }
   1.388 +}