1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/Server/CecSharpClient.cs Fri Sep 25 12:28:47 2015 +0200
1.3 @@ -0,0 +1,376 @@
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 + public Client()
1.45 + {
1.46 + Config = new LibCECConfiguration();
1.47 + Config.DeviceTypes.Types[0] = CecDeviceType.Tv;
1.48 + Config.DeviceName = "CEC";
1.49 + Config.HDMIPort = 2;
1.50 + //Config.ClientVersion = LibCECConfiguration.CurrentVersion;
1.51 + Config.SetCallbacks(this);
1.52 + LogLevel = (int)CecLogLevel.All;
1.53 +
1.54 + Lib = new LibCecSharp(Config);
1.55 + Lib.InitVideoStandalone();
1.56 +
1.57 + //Console.WriteLine("CEC Parser created - libCEC version " + Lib.VersionToString(Config.ServerVersion));
1.58 + Console.WriteLine("CEC Parser created - libCEC version " + Config.ServerVersion);
1.59 + }
1.60 +
1.61 + public override int ReceiveCommand(CecCommand command)
1.62 + {
1.63 + return 1;
1.64 + }
1.65 +
1.66 + public override int ReceiveKeypress(CecKeypress key)
1.67 + {
1.68 + return 1;
1.69 + }
1.70 +
1.71 + public override int ReceiveLogMessage(CecLogMessage message)
1.72 + {
1.73 + if (((int)message.Level & LogLevel) == (int)message.Level)
1.74 + {
1.75 + string strLevel = "";
1.76 + switch (message.Level)
1.77 + {
1.78 + case CecLogLevel.Error:
1.79 + strLevel = "ERROR: ";
1.80 + break;
1.81 + case CecLogLevel.Warning:
1.82 + strLevel = "WARNING: ";
1.83 + break;
1.84 + case CecLogLevel.Notice:
1.85 + strLevel = "NOTICE: ";
1.86 + break;
1.87 + case CecLogLevel.Traffic:
1.88 + strLevel = "TRAFFIC: ";
1.89 + break;
1.90 + case CecLogLevel.Debug:
1.91 + strLevel = "DEBUG: ";
1.92 + break;
1.93 + default:
1.94 + break;
1.95 + }
1.96 + string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message);
1.97 + Console.WriteLine(strLog);
1.98 + }
1.99 + return 1;
1.100 + }
1.101 +
1.102 + /// <summary>
1.103 + ///
1.104 + /// </summary>
1.105 + /// <param name="timeout"></param>
1.106 + /// <returns></returns>
1.107 + public bool Connect(int timeout)
1.108 + {
1.109 + CecAdapter[] adapters = Lib.FindAdapters(string.Empty);
1.110 + if (adapters.Length > 0)
1.111 + return Connect(adapters[0].ComPort, timeout);
1.112 + else
1.113 + {
1.114 + Console.WriteLine("Did not find any CEC adapters");
1.115 + return false;
1.116 + }
1.117 + }
1.118 +
1.119 + public bool Connect(string port, int timeout)
1.120 + {
1.121 + return Lib.Open(port, timeout);
1.122 + }
1.123 +
1.124 + public void Close()
1.125 + {
1.126 + Lib.Close();
1.127 + }
1.128 +
1.129 + public void ListDevices()
1.130 + {
1.131 + int iAdapter = 0;
1.132 + foreach (CecAdapter adapter in Lib.FindAdapters(string.Empty))
1.133 + {
1.134 + Console.WriteLine("Adapter: " + iAdapter++);
1.135 + Console.WriteLine("Path: " + adapter.Path);
1.136 + Console.WriteLine("Com port: " + adapter.ComPort);
1.137 + }
1.138 + }
1.139 +
1.140 + void ShowConsoleHelp()
1.141 + {
1.142 + Console.WriteLine(
1.143 + "================================================================================" + Environment.NewLine +
1.144 + "Available commands:" + Environment.NewLine +
1.145 + Environment.NewLine +
1.146 + "[tx] {bytes} transfer bytes over the CEC line." + Environment.NewLine +
1.147 + "[txn] {bytes} transfer bytes but don't wait for transmission ACK." + Environment.NewLine +
1.148 + "[on] {address} power on the device with the given logical address." + Environment.NewLine +
1.149 + "[standby] {address} put the device with the given address in standby mode." + Environment.NewLine +
1.150 + "[la] {logical_address} change the logical address of the CEC adapter." + Environment.NewLine +
1.151 + "[pa] {physical_address} change the physical address of the CEC adapter." + Environment.NewLine +
1.152 + "[osd] {addr} {string} set OSD message on the specified device." + Environment.NewLine +
1.153 + "[ver] {addr} get the CEC version of the specified device." + Environment.NewLine +
1.154 + "[ven] {addr} get the vendor ID of the specified device." + Environment.NewLine +
1.155 + "[lang] {addr} get the menu language of the specified device." + Environment.NewLine +
1.156 + "[pow] {addr} get the power status of the specified device." + Environment.NewLine +
1.157 + "[poll] {addr} poll the specified device." + Environment.NewLine +
1.158 + "[scan] scan the CEC bus and display device info" + Environment.NewLine +
1.159 + "[mon] {1|0} enable or disable CEC bus monitoring." + Environment.NewLine +
1.160 + "[log] {1 - 31} change the log level. see cectypes.h for values." + Environment.NewLine +
1.161 + "[ping] send a ping command to the CEC adapter." + Environment.NewLine +
1.162 + "[bl] to let the adapter enter the bootloader, to upgrade" + Environment.NewLine +
1.163 + " the flash rom." + Environment.NewLine +
1.164 + "[r] reconnect to the CEC adapter." + Environment.NewLine +
1.165 + "[h] or [help] show this help." + Environment.NewLine +
1.166 + "[q] or [quit] to quit the CEC test client and switch off all" + Environment.NewLine +
1.167 + " connected CEC devices." + Environment.NewLine +
1.168 + "================================================================================");
1.169 + }
1.170 +
1.171 + public void MainLoop()
1.172 + {
1.173 + bool bContinue = true;
1.174 + string command;
1.175 + while (bContinue)
1.176 + {
1.177 + Console.WriteLine("waiting for input");
1.178 +
1.179 + command = Console.ReadLine();
1.180 + if (command != null && command.Length == 0)
1.181 + continue;
1.182 + string[] splitCommand = command.Split(' ');
1.183 + if (splitCommand[0] == "tx" || splitCommand[0] == "txn")
1.184 + {
1.185 + CecCommand bytes = new CecCommand();
1.186 + for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
1.187 + {
1.188 + bytes.PushBack(byte.Parse(splitCommand[iPtr], System.Globalization.NumberStyles.HexNumber));
1.189 + }
1.190 +
1.191 + if (command == "txn")
1.192 + bytes.TransmitTimeout = 0;
1.193 +
1.194 + Lib.Transmit(bytes);
1.195 + }
1.196 + else if (splitCommand[0] == "on")
1.197 + {
1.198 + if (splitCommand.Length > 1)
1.199 + Lib.PowerOnDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
1.200 + else
1.201 + Lib.PowerOnDevices(CecLogicalAddress.Broadcast);
1.202 + }
1.203 + else if (splitCommand[0] == "standby")
1.204 + {
1.205 + if (splitCommand.Length > 1)
1.206 + Lib.StandbyDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
1.207 + else
1.208 + Lib.StandbyDevices(CecLogicalAddress.Broadcast);
1.209 + }
1.210 + else if (splitCommand[0] == "poll")
1.211 + {
1.212 + bool bSent = false;
1.213 + if (splitCommand.Length > 1)
1.214 + bSent = Lib.PollDevice((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
1.215 + else
1.216 + bSent = Lib.PollDevice(CecLogicalAddress.Broadcast);
1.217 + if (bSent)
1.218 + Console.WriteLine("POLL message sent");
1.219 + else
1.220 + Console.WriteLine("POLL message not sent");
1.221 + }
1.222 + else if (splitCommand[0] == "la")
1.223 + {
1.224 + if (splitCommand.Length > 1)
1.225 + Lib.SetLogicalAddress((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
1.226 + }
1.227 + else if (splitCommand[0] == "pa")
1.228 + {
1.229 + if (splitCommand.Length > 1)
1.230 + Lib.SetPhysicalAddress(ushort.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
1.231 + }
1.232 + else if (splitCommand[0] == "osd")
1.233 + {
1.234 + if (splitCommand.Length > 2)
1.235 + {
1.236 + StringBuilder osdString = new StringBuilder();
1.237 + for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
1.238 + {
1.239 + osdString.Append(splitCommand[iPtr]);
1.240 + if (iPtr != splitCommand.Length - 1)
1.241 + osdString.Append(" ");
1.242 + }
1.243 + Lib.SetOSDString((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber), CecDisplayControl.DisplayForDefaultTime, osdString.ToString());
1.244 + }
1.245 + }
1.246 + else if (splitCommand[0] == "ping")
1.247 + {
1.248 + Lib.PingAdapter();
1.249 + }
1.250 + else if (splitCommand[0] == "mon")
1.251 + {
1.252 + bool enable = splitCommand.Length > 1 ? splitCommand[1] == "1" : false;
1.253 + Lib.SwitchMonitoring(enable);
1.254 + }
1.255 + else if (splitCommand[0] == "bl")
1.256 + {
1.257 + Lib.StartBootloader();
1.258 + }
1.259 + else if (splitCommand[0] == "lang")
1.260 + {
1.261 + if (splitCommand.Length > 1)
1.262 + {
1.263 + string language = Lib.GetDeviceMenuLanguage((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
1.264 + Console.WriteLine("Menu language: " + language);
1.265 + }
1.266 + }
1.267 + else if (splitCommand[0] == "ven")
1.268 + {
1.269 + if (splitCommand.Length > 1)
1.270 + {
1.271 + CecVendorId vendor = Lib.GetDeviceVendorId((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
1.272 + Console.WriteLine("Vendor ID: " + Lib.ToString(vendor));
1.273 + }
1.274 + }
1.275 + else if (splitCommand[0] == "ver")
1.276 + {
1.277 + if (splitCommand.Length > 1)
1.278 + {
1.279 + CecVersion version = Lib.GetDeviceCecVersion((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
1.280 + Console.WriteLine("CEC version: " + Lib.ToString(version));
1.281 + }
1.282 + }
1.283 + else if (splitCommand[0] == "pow")
1.284 + {
1.285 + if (splitCommand.Length > 1)
1.286 + {
1.287 + CecPowerStatus power = Lib.GetDevicePowerStatus((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
1.288 + Console.WriteLine("power status: " + Lib.ToString(power));
1.289 + }
1.290 + }
1.291 + else if (splitCommand[0] == "r")
1.292 + {
1.293 + Console.WriteLine("closing the connection");
1.294 + Lib.Close();
1.295 +
1.296 + Console.WriteLine("opening a new connection");
1.297 + Connect(10000);
1.298 +
1.299 + Console.WriteLine("setting active source");
1.300 + Lib.SetActiveSource(CecDeviceType.PlaybackDevice);
1.301 + }
1.302 + else if (splitCommand[0] == "scan")
1.303 + {
1.304 + StringBuilder output = new StringBuilder();
1.305 + output.AppendLine("CEC bus information");
1.306 + output.AppendLine("===================");
1.307 + CecLogicalAddresses addresses = Lib.GetActiveDevices();
1.308 + for (int iPtr = 0; iPtr < addresses.Addresses.Length; iPtr++)
1.309 + {
1.310 + CecLogicalAddress address = (CecLogicalAddress)iPtr;
1.311 + if (!addresses.IsSet(address))
1.312 + continue;
1.313 +
1.314 + CecVendorId iVendorId = Lib.GetDeviceVendorId(address);
1.315 + bool bActive = Lib.IsActiveDevice(address);
1.316 + ushort iPhysicalAddress = Lib.GetDevicePhysicalAddress(address);
1.317 + string strAddr = "todo: fixme"; //Lib.PhysicalAddressToString(iPhysicalAddress);
1.318 + CecVersion iCecVersion = Lib.GetDeviceCecVersion(address);
1.319 + CecPowerStatus power = Lib.GetDevicePowerStatus(address);
1.320 + string osdName = Lib.GetDeviceOSDName(address);
1.321 + string lang = Lib.GetDeviceMenuLanguage(address);
1.322 +
1.323 + output.AppendLine("device #" + iPtr + ": " + Lib.ToString(address));
1.324 + output.AppendLine("address: " + strAddr);
1.325 + output.AppendLine("active source: " + (bActive ? "yes" : "no"));
1.326 + output.AppendLine("vendor: " + Lib.ToString(iVendorId));
1.327 + output.AppendLine("osd string: " + osdName);
1.328 + output.AppendLine("CEC version: " + Lib.ToString(iCecVersion));
1.329 + output.AppendLine("power status: " + Lib.ToString(power));
1.330 + if (!string.IsNullOrEmpty(lang))
1.331 + output.AppendLine("language: " + lang);
1.332 + output.AppendLine("");
1.333 + }
1.334 + Console.WriteLine(output.ToString());
1.335 + }
1.336 + else if (splitCommand[0] == "h" || splitCommand[0] == "help")
1.337 + ShowConsoleHelp();
1.338 + else if (splitCommand[0] == "q" || splitCommand[0] == "quit")
1.339 + bContinue = false;
1.340 + else if (splitCommand[0] == "log" && splitCommand.Length > 1)
1.341 + LogLevel = int.Parse(splitCommand[1]);
1.342 + }
1.343 + }
1.344 +
1.345 + static void Main(string[] args)
1.346 + {
1.347 + Client p = new Client();
1.348 + if (p.Connect(10000))
1.349 + {
1.350 + p.MainLoop();
1.351 + }
1.352 + else
1.353 + {
1.354 + Console.WriteLine("Could not open a connection to the CEC adapter");
1.355 + }
1.356 + }
1.357 +
1.358 + /// <summary>
1.359 + ///
1.360 + /// </summary>
1.361 + public void PowerOnDevices(CecLogicalAddress aAddress)
1.362 + {
1.363 + Lib.PowerOnDevices(aAddress);
1.364 + }
1.365 +
1.366 + /// <summary>
1.367 + ///
1.368 + /// </summary>
1.369 + public void StandbyDevices(CecLogicalAddress aAddress)
1.370 + {
1.371 + Lib.StandbyDevices(aAddress);
1.372 + }
1.373 +
1.374 +
1.375 + private int LogLevel;
1.376 + private LibCecSharp Lib;
1.377 + private LibCECConfiguration Config;
1.378 + }
1.379 +}