Updating libcec to 6d68d21243aa92862592435e8396b4280ea46c3f.
2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011-2013 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
9 * This program is dual-licensed; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
27 * For more information contact:
28 * Pulse-Eight Licensing <license@pulse-eight.com>
29 * http://www.pulse-eight.com/
30 * http://www.pulse-eight.net/
39 class Client : CecCallbackMethods
44 /// <param name="aDeviceName"></param>
45 /// <param name="aHdmiPort"></param>
46 public Client(string aDeviceName, byte aHdmiPort)
48 Config = new LibCECConfiguration();
49 Config.DeviceTypes.Types[0] = CecDeviceType.Tv;
50 Config.DeviceName = aDeviceName;
51 Config.HDMIPort = aHdmiPort;
52 //Config.ClientVersion = LibCECConfiguration.CurrentVersion;
53 Config.SetCallbacks(this);
54 LogLevel = (int)CecLogLevel.All;
56 iLib = new LibCecSharp(Config);
57 iLib.InitVideoStandalone();
59 //Console.WriteLine("CEC Parser created - libCEC version " + Lib.VersionToString(Config.ServerVersion));
60 Console.WriteLine("CEC Parser created - libCEC version " + Config.ServerVersion);
65 public virtual int ReceiveAlert(CecAlert alert, CecParameter data)
70 public virtual int ReceiveMenuStateChange(CecMenuState newVal)
75 public virtual void SourceActivated(CecLogicalAddress logicalAddress, bool activated)
80 public override int ReceiveCommand(CecCommand command)
85 public override int ReceiveKeypress(CecKeypress key)
90 public override int ReceiveLogMessage(CecLogMessage message)
92 if (((int)message.Level & LogLevel) == (int)message.Level)
95 switch (message.Level)
97 case CecLogLevel.Error:
100 case CecLogLevel.Warning:
101 strLevel = "WARNING: ";
103 case CecLogLevel.Notice:
104 strLevel = "NOTICE: ";
106 case CecLogLevel.Traffic:
107 strLevel = "TRAFFIC: ";
109 case CecLogLevel.Debug:
110 strLevel = "DEBUG: ";
115 string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message);
116 Console.WriteLine(strLog);
124 /// <param name="timeout"></param>
125 /// <returns></returns>
126 public bool Connect(int timeout)
128 CecAdapter[] adapters = iLib.FindAdapters(string.Empty);
129 if (adapters.Length > 0)
130 return Connect(adapters[0].ComPort, timeout);
133 Console.WriteLine("Did not find any CEC adapters");
138 public bool Connect(string port, int timeout)
140 return iLib.Open(port, timeout);
148 public void ListDevices()
151 foreach (CecAdapter adapter in iLib.FindAdapters(string.Empty))
153 Console.WriteLine("Adapter: " + iAdapter++);
154 Console.WriteLine("Path: " + adapter.Path);
155 Console.WriteLine("Com port: " + adapter.ComPort);
159 void ShowConsoleHelp()
162 "================================================================================" + Environment.NewLine +
163 "Available commands:" + Environment.NewLine +
164 Environment.NewLine +
165 "[tx] {bytes} transfer bytes over the CEC line." + Environment.NewLine +
166 "[txn] {bytes} transfer bytes but don't wait for transmission ACK." + Environment.NewLine +
167 "[on] {address} power on the device with the given logical address." + Environment.NewLine +
168 "[standby] {address} put the device with the given address in standby mode." + Environment.NewLine +
169 "[la] {logical_address} change the logical address of the CEC adapter." + Environment.NewLine +
170 "[pa] {physical_address} change the physical address of the CEC adapter." + Environment.NewLine +
171 "[osd] {addr} {string} set OSD message on the specified device." + Environment.NewLine +
172 "[ver] {addr} get the CEC version of the specified device." + Environment.NewLine +
173 "[ven] {addr} get the vendor ID of the specified device." + Environment.NewLine +
174 "[lang] {addr} get the menu language of the specified device." + Environment.NewLine +
175 "[pow] {addr} get the power status of the specified device." + Environment.NewLine +
176 "[poll] {addr} poll the specified device." + Environment.NewLine +
177 "[scan] scan the CEC bus and display device info" + Environment.NewLine +
178 "[mon] {1|0} enable or disable CEC bus monitoring." + Environment.NewLine +
179 "[log] {1 - 31} change the log level. see cectypes.h for values." + Environment.NewLine +
180 "[ping] send a ping command to the CEC adapter." + Environment.NewLine +
181 "[bl] to let the adapter enter the bootloader, to upgrade" + Environment.NewLine +
182 " the flash rom." + Environment.NewLine +
183 "[r] reconnect to the CEC adapter." + Environment.NewLine +
184 "[h] or [help] show this help." + Environment.NewLine +
185 "[q] or [quit] to quit the CEC test client and switch off all" + Environment.NewLine +
186 " connected CEC devices." + Environment.NewLine +
187 "================================================================================");
190 public void MainLoop()
192 bool bContinue = true;
196 Console.WriteLine("waiting for input");
198 command = Console.ReadLine();
199 if (command != null && command.Length == 0)
201 string[] splitCommand = command.Split(' ');
202 if (splitCommand[0] == "tx" || splitCommand[0] == "txn")
204 CecCommand bytes = new CecCommand();
205 for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
207 bytes.PushBack(byte.Parse(splitCommand[iPtr], System.Globalization.NumberStyles.HexNumber));
210 if (command == "txn")
211 bytes.TransmitTimeout = 0;
213 iLib.Transmit(bytes);
215 else if (splitCommand[0] == "on")
217 if (splitCommand.Length > 1)
218 iLib.PowerOnDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
220 iLib.PowerOnDevices(CecLogicalAddress.Broadcast);
222 else if (splitCommand[0] == "standby")
224 if (splitCommand.Length > 1)
225 iLib.StandbyDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
227 iLib.StandbyDevices(CecLogicalAddress.Broadcast);
229 else if (splitCommand[0] == "poll")
232 if (splitCommand.Length > 1)
233 bSent = iLib.PollDevice((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
235 bSent = iLib.PollDevice(CecLogicalAddress.Broadcast);
237 Console.WriteLine("POLL message sent");
239 Console.WriteLine("POLL message not sent");
241 else if (splitCommand[0] == "la")
243 if (splitCommand.Length > 1)
244 iLib.SetLogicalAddress((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
246 else if (splitCommand[0] == "pa")
248 if (splitCommand.Length > 1)
249 iLib.SetPhysicalAddress(ushort.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
251 else if (splitCommand[0] == "osd")
253 if (splitCommand.Length > 2)
255 StringBuilder osdString = new StringBuilder();
256 for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
258 osdString.Append(splitCommand[iPtr]);
259 if (iPtr != splitCommand.Length - 1)
260 osdString.Append(" ");
262 iLib.SetOSDString((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber), CecDisplayControl.DisplayForDefaultTime, osdString.ToString());
265 else if (splitCommand[0] == "ping")
269 else if (splitCommand[0] == "mon")
271 bool enable = splitCommand.Length > 1 ? splitCommand[1] == "1" : false;
272 iLib.SwitchMonitoring(enable);
274 else if (splitCommand[0] == "bl")
276 iLib.StartBootloader();
278 else if (splitCommand[0] == "lang")
280 if (splitCommand.Length > 1)
282 string language = iLib.GetDeviceMenuLanguage((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
283 Console.WriteLine("Menu language: " + language);
286 else if (splitCommand[0] == "ven")
288 if (splitCommand.Length > 1)
290 CecVendorId vendor = iLib.GetDeviceVendorId((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
291 Console.WriteLine("Vendor ID: " + iLib.ToString(vendor));
294 else if (splitCommand[0] == "ver")
296 if (splitCommand.Length > 1)
298 CecVersion version = iLib.GetDeviceCecVersion((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
299 Console.WriteLine("CEC version: " + iLib.ToString(version));
302 else if (splitCommand[0] == "pow")
304 if (splitCommand.Length > 1)
306 CecPowerStatus power = iLib.GetDevicePowerStatus((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
307 Console.WriteLine("power status: " + iLib.ToString(power));
310 else if (splitCommand[0] == "r")
312 Console.WriteLine("closing the connection");
315 Console.WriteLine("opening a new connection");
318 Console.WriteLine("setting active source");
319 iLib.SetActiveSource(CecDeviceType.PlaybackDevice);
321 else if (splitCommand[0] == "scan")
323 StringBuilder output = new StringBuilder();
324 output.AppendLine("CEC bus information");
325 output.AppendLine("===================");
326 CecLogicalAddresses addresses = iLib.GetActiveDevices();
327 for (int iPtr = 0; iPtr < addresses.Addresses.Length; iPtr++)
329 CecLogicalAddress address = (CecLogicalAddress)iPtr;
330 if (!addresses.IsSet(address))
333 CecVendorId iVendorId = iLib.GetDeviceVendorId(address);
334 bool bActive = iLib.IsActiveDevice(address);
335 ushort iPhysicalAddress = iLib.GetDevicePhysicalAddress(address);
336 string strAddr = "todo: fixme"; //Lib.PhysicalAddressToString(iPhysicalAddress);
337 CecVersion iCecVersion = iLib.GetDeviceCecVersion(address);
338 CecPowerStatus power = iLib.GetDevicePowerStatus(address);
339 string osdName = iLib.GetDeviceOSDName(address);
340 string lang = iLib.GetDeviceMenuLanguage(address);
342 output.AppendLine("device #" + iPtr + ": " + iLib.ToString(address));
343 output.AppendLine("address: " + strAddr);
344 output.AppendLine("active source: " + (bActive ? "yes" : "no"));
345 output.AppendLine("vendor: " + iLib.ToString(iVendorId));
346 output.AppendLine("osd string: " + osdName);
347 output.AppendLine("CEC version: " + iLib.ToString(iCecVersion));
348 output.AppendLine("power status: " + iLib.ToString(power));
349 if (!string.IsNullOrEmpty(lang))
350 output.AppendLine("language: " + lang);
351 output.AppendLine("");
353 Console.WriteLine(output.ToString());
355 else if (splitCommand[0] == "h" || splitCommand[0] == "help")
357 else if (splitCommand[0] == "q" || splitCommand[0] == "quit")
359 else if (splitCommand[0] == "log" && splitCommand.Length > 1)
360 LogLevel = int.Parse(splitCommand[1]);
364 /// TODO: remove that
365 static void Main(string[] args)
367 Client p = new Client("CEC",2);
368 if (p.Connect(10000))
374 Console.WriteLine("Could not open a connection to the CEC adapter");
379 /// Provide direct access to CEC library
381 public LibCecSharp Lib
392 private int LogLevel;
396 private LibCecSharp iLib;
400 private LibCECConfiguration Config;