Logs with time stamp.
CEC logs level configurable from UI.
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/
36 using System.Threading;
40 class Client : CecCallbackMethods
43 /// Provide direct access to CEC library
45 public LibCecSharp Lib
56 public int LogLevel = (int)CecLogLevel.Notice;
61 public bool FilterOutPollLogs = true;
66 private LibCecSharp iLib;
70 private LibCECConfiguration Config;
75 private bool iConnected;
80 /// <param name="aDeviceName"></param>
81 /// <param name="aHdmiPort"></param>
82 public Client(string aDeviceName, byte aHdmiPort, CecDeviceType aDeviceType = CecDeviceType.PlaybackDevice)
84 Config = new LibCECConfiguration();
85 Config.DeviceTypes.Types[0] = aDeviceType;
86 Config.DeviceName = aDeviceName;
87 Config.HDMIPort = aHdmiPort;
88 //Config.ClientVersion = LibCECConfiguration.CurrentVersion;
89 Config.SetCallbacks(this);
91 iLib = new LibCecSharp(Config);
92 iLib.InitVideoStandalone();
94 //Console.WriteLine("CEC Parser created - libCEC version " + Lib.VersionToString(Config.ServerVersion));
95 Console.WriteLine("CEC Parser created - libCEC version " + Config.ServerVersion);
102 /// <param name="alert"></param>
103 /// <param name="data"></param>
104 /// <returns></returns>
105 public override int ReceiveAlert(CecAlert alert, CecParameter data)
107 string log = "CEC alert: " + alert.ToString();
108 if (data != null && data.Type == CecParameterType.ParameterTypeString)
110 log += " " + data.Data;
113 Console.WriteLine(log);
124 /// <param name="newVal"></param>
125 /// <returns></returns>
126 public override int ReceiveMenuStateChange(CecMenuState newVal)
128 Console.WriteLine("CEC menu state changed to: " + iLib.ToString(newVal));
135 /// <param name="logicalAddress"></param>
136 /// <param name="activated"></param>
137 public override void SourceActivated(CecLogicalAddress logicalAddress, bool activated)
139 Console.WriteLine("CEC source activated: " + iLib.ToString(logicalAddress) + "/" + activated.ToString() );
143 public override int ReceiveCommand(CecCommand command)
145 Console.WriteLine(string.Format("CEC command Src:{0} Dst:{1} Ack: {2} Eom: {3} OpcodeSet: {4} Opcode: {5} Timeout: {6}",
146 iLib.ToString(command.Initiator),
147 iLib.ToString(command.Destination),
148 command.Ack.ToString(),
149 command.Eom.ToString(),
150 command.OpcodeSet.ToString(),
151 iLib.ToString(command.Opcode),
152 command.TransmitTimeout.ToString()
157 public override int ReceiveKeypress(CecKeypress key)
159 Console.WriteLine(string.Format("CEC keypress: {0} Duration:{1} Empty: {2}",
160 key.Keycode.ToString(), key.Duration.ToString(), key.Empty.ToString()));
164 public override int ReceiveLogMessage(CecLogMessage message)
166 if (((int)message.Level & LogLevel) == (int)message.Level)
168 string strLevel = "";
169 switch (message.Level)
171 case CecLogLevel.Error:
172 strLevel = "ERROR: ";
174 case CecLogLevel.Warning:
175 strLevel = "WARNING: ";
177 case CecLogLevel.Notice:
178 strLevel = "NOTICE: ";
180 case CecLogLevel.Traffic:
181 strLevel = "TRAFFIC: ";
183 case CecLogLevel.Debug:
184 strLevel = "DEBUG: ";
185 if (message.Message.IndexOf("POLL") != -1 && FilterOutPollLogs)
187 //We have an option to prevent spamming with poll debug logs
194 string strLog = string.Format("{0} {1} {2}", strLevel, message.Time, message.Message);
195 Console.WriteLine(strLog);
204 /// <param name="timeout"></param>
205 /// <returns></returns>
206 public bool Connect(int timeout)
209 CecAdapter[] adapters = iLib.FindAdapters(string.Empty);
210 if (adapters.Length > 0)
212 Connect(adapters[0].ComPort, timeout);
216 Console.WriteLine("CEC did not find any adapters");
222 public bool Connect(string port, int timeout)
225 iConnected = iLib.Open(port, timeout);
241 public void TestSendKey()
243 //SetupMenu: opens option menu
244 //RootMenu: opens Android home menu
248 //Philips TopMenu = 16
249 //Philips PopupMenu = 17
251 //bool res = iLib.SendKeypress(CecLogicalAddress.Tv, CecUserControlCode.DisplayInformation, true);
252 //Thread.Sleep(3000); //Wait few seconds for menu to open
253 //res = iLib.SendKeypress(CecLogicalAddress.Tv, CecUserControlCode.SetupMenu, true);
255 for (int i = 0; i < 256; i++)
258 //res = iLib.SendKeyRelease(CecLogicalAddress.Tv, true);
260 switch ((CecUserControlCode)i)
262 case CecUserControlCode.Power:
263 case CecUserControlCode.PowerOffFunction:
264 case CecUserControlCode.PowerOnFunction:
265 case CecUserControlCode.PowerToggleFunction:
266 case CecUserControlCode.ElectronicProgramGuide:
267 case CecUserControlCode.InputSelect:
268 case CecUserControlCode.RootMenu:
273 Console.WriteLine(i.ToString());
275 iLib.SendKeypress(CecLogicalAddress.Tv, (CecUserControlCode)i, true);
285 for (int i=0;i<7;i++)
288 //res = iLib.SendKeyRelease(CecLogicalAddress.Tv, true);
290 //res = iLib.SendKeypress(CecLogicalAddress.Tv, CecUserControlCode.Down, true);
294 //res = iLib.SendKeypress(CecLogicalAddress.Tv, CecUserControlCode.Select, true);
296 //res = iLib.SendKeypress(CecLogicalAddress.Tv, CecUserControlCode.Select, true);
297 //res = iLib.SendKeyRelease(CecLogicalAddress.Tv, true);
305 Console.WriteLine("CEC bus information");
306 Console.WriteLine("===================");
307 CecLogicalAddresses addresses = Lib.GetActiveDevices();
308 for (int iPtr = 0; iPtr < addresses.Addresses.Length; iPtr++)
310 CecLogicalAddress address = (CecLogicalAddress) iPtr;
311 if (!addresses.IsSet(address))
314 CecVendorId iVendorId = Lib.GetDeviceVendorId(address);
315 bool bActive = Lib.IsActiveDevice(address);
316 ushort iPhysicalAddress = Lib.GetDevicePhysicalAddress(address);
317 string strAddr = Lib.PhysicalAddressToString(iPhysicalAddress);
318 CecVersion iCecVersion = Lib.GetDeviceCecVersion(address);
319 CecPowerStatus power = Lib.GetDevicePowerStatus(address);
320 string osdName = Lib.GetDeviceOSDName(address);
321 string lang = Lib.GetDeviceMenuLanguage(address);
323 Console.WriteLine("device #" + iPtr + ": " + Lib.ToString(address));
324 Console.WriteLine("address: " + strAddr);
325 Console.WriteLine("active source: " + (bActive ? "yes" : "no"));
326 Console.WriteLine("vendor: " + Lib.ToString(iVendorId));
327 Console.WriteLine("osd string: " + osdName);
328 Console.WriteLine("CEC version: " + Lib.ToString(iCecVersion));
329 Console.WriteLine("power status: " + Lib.ToString(power));
330 if (!string.IsNullOrEmpty(lang))
331 Console.WriteLine("language: " + lang);
332 Console.WriteLine("");
336 public void ListAdapters()
339 foreach (CecAdapter adapter in iLib.FindAdapters(string.Empty))
341 Console.WriteLine("Adapter: " + iAdapter++);
342 Console.WriteLine("Path: " + adapter.Path);
343 Console.WriteLine("Com port: " + adapter.ComPort);
347 void ShowConsoleHelp()
350 "================================================================================" + Environment.NewLine +
351 "Available commands:" + Environment.NewLine +
352 Environment.NewLine +
353 "[tx] {bytes} transfer bytes over the CEC line." + Environment.NewLine +
354 "[txn] {bytes} transfer bytes but don't wait for transmission ACK." + Environment.NewLine +
355 "[on] {address} power on the device with the given logical address." + Environment.NewLine +
356 "[standby] {address} put the device with the given address in standby mode." + Environment.NewLine +
357 "[la] {logical_address} change the logical address of the CEC adapter." + Environment.NewLine +
358 "[pa] {physical_address} change the physical address of the CEC adapter." + Environment.NewLine +
359 "[osd] {addr} {string} set OSD message on the specified device." + Environment.NewLine +
360 "[ver] {addr} get the CEC version of the specified device." + Environment.NewLine +
361 "[ven] {addr} get the vendor ID of the specified device." + Environment.NewLine +
362 "[lang] {addr} get the menu language of the specified device." + Environment.NewLine +
363 "[pow] {addr} get the power status of the specified device." + Environment.NewLine +
364 "[poll] {addr} poll the specified device." + Environment.NewLine +
365 "[scan] scan the CEC bus and display device info" + Environment.NewLine +
366 "[mon] {1|0} enable or disable CEC bus monitoring." + Environment.NewLine +
367 "[log] {1 - 31} change the log level. see cectypes.h for values." + Environment.NewLine +
368 "[ping] send a ping command to the CEC adapter." + Environment.NewLine +
369 "[bl] to let the adapter enter the bootloader, to upgrade" + Environment.NewLine +
370 " the flash rom." + Environment.NewLine +
371 "[r] reconnect to the CEC adapter." + Environment.NewLine +
372 "[h] or [help] show this help." + Environment.NewLine +
373 "[q] or [quit] to quit the CEC test client and switch off all" + Environment.NewLine +
374 " connected CEC devices." + Environment.NewLine +
375 "================================================================================");
378 public void MainLoop()
380 bool bContinue = true;
384 Console.WriteLine("waiting for input");
386 command = Console.ReadLine();
387 if (command != null && command.Length == 0)
389 string[] splitCommand = command.Split(' ');
390 if (splitCommand[0] == "tx" || splitCommand[0] == "txn")
392 CecCommand bytes = new CecCommand();
393 for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
395 bytes.PushBack(byte.Parse(splitCommand[iPtr], System.Globalization.NumberStyles.HexNumber));
398 if (command == "txn")
399 bytes.TransmitTimeout = 0;
401 iLib.Transmit(bytes);
403 else if (splitCommand[0] == "on")
405 if (splitCommand.Length > 1)
406 iLib.PowerOnDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
408 iLib.PowerOnDevices(CecLogicalAddress.Broadcast);
410 else if (splitCommand[0] == "standby")
412 if (splitCommand.Length > 1)
413 iLib.StandbyDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
415 iLib.StandbyDevices(CecLogicalAddress.Broadcast);
417 else if (splitCommand[0] == "poll")
420 if (splitCommand.Length > 1)
421 bSent = iLib.PollDevice((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
423 bSent = iLib.PollDevice(CecLogicalAddress.Broadcast);
425 Console.WriteLine("POLL message sent");
427 Console.WriteLine("POLL message not sent");
429 else if (splitCommand[0] == "la")
431 if (splitCommand.Length > 1)
432 iLib.SetLogicalAddress((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
434 else if (splitCommand[0] == "pa")
436 if (splitCommand.Length > 1)
437 iLib.SetPhysicalAddress(ushort.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
439 else if (splitCommand[0] == "osd")
441 if (splitCommand.Length > 2)
443 StringBuilder osdString = new StringBuilder();
444 for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
446 osdString.Append(splitCommand[iPtr]);
447 if (iPtr != splitCommand.Length - 1)
448 osdString.Append(" ");
450 iLib.SetOSDString((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber), CecDisplayControl.DisplayForDefaultTime, osdString.ToString());
453 else if (splitCommand[0] == "ping")
457 else if (splitCommand[0] == "mon")
459 bool enable = splitCommand.Length > 1 ? splitCommand[1] == "1" : false;
460 iLib.SwitchMonitoring(enable);
462 else if (splitCommand[0] == "bl")
464 iLib.StartBootloader();
466 else if (splitCommand[0] == "lang")
468 if (splitCommand.Length > 1)
470 string language = iLib.GetDeviceMenuLanguage((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
471 Console.WriteLine("Menu language: " + language);
474 else if (splitCommand[0] == "ven")
476 if (splitCommand.Length > 1)
478 CecVendorId vendor = iLib.GetDeviceVendorId((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
479 Console.WriteLine("Vendor ID: " + iLib.ToString(vendor));
482 else if (splitCommand[0] == "ver")
484 if (splitCommand.Length > 1)
486 CecVersion version = iLib.GetDeviceCecVersion((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
487 Console.WriteLine("CEC version: " + iLib.ToString(version));
490 else if (splitCommand[0] == "pow")
492 if (splitCommand.Length > 1)
494 CecPowerStatus power = iLib.GetDevicePowerStatus((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
495 Console.WriteLine("power status: " + iLib.ToString(power));
498 else if (splitCommand[0] == "r")
500 Console.WriteLine("closing the connection");
503 Console.WriteLine("opening a new connection");
506 Console.WriteLine("setting active source");
507 iLib.SetActiveSource(CecDeviceType.PlaybackDevice);
509 else if (splitCommand[0] == "scan")
511 StringBuilder output = new StringBuilder();
512 output.AppendLine("CEC bus information");
513 output.AppendLine("===================");
514 CecLogicalAddresses addresses = iLib.GetActiveDevices();
515 for (int iPtr = 0; iPtr < addresses.Addresses.Length; iPtr++)
517 CecLogicalAddress address = (CecLogicalAddress)iPtr;
518 if (!addresses.IsSet(address))
521 CecVendorId iVendorId = iLib.GetDeviceVendorId(address);
522 bool bActive = iLib.IsActiveDevice(address);
523 ushort iPhysicalAddress = iLib.GetDevicePhysicalAddress(address);
524 string strAddr = "todo: fixme"; //Lib.PhysicalAddressToString(iPhysicalAddress);
525 CecVersion iCecVersion = iLib.GetDeviceCecVersion(address);
526 CecPowerStatus power = iLib.GetDevicePowerStatus(address);
527 string osdName = iLib.GetDeviceOSDName(address);
528 string lang = iLib.GetDeviceMenuLanguage(address);
530 output.AppendLine("device #" + iPtr + ": " + iLib.ToString(address));
531 output.AppendLine("address: " + strAddr);
532 output.AppendLine("active source: " + (bActive ? "yes" : "no"));
533 output.AppendLine("vendor: " + iLib.ToString(iVendorId));
534 output.AppendLine("osd string: " + osdName);
535 output.AppendLine("CEC version: " + iLib.ToString(iCecVersion));
536 output.AppendLine("power status: " + iLib.ToString(power));
537 if (!string.IsNullOrEmpty(lang))
538 output.AppendLine("language: " + lang);
539 output.AppendLine("");
541 Console.WriteLine(output.ToString());
543 else if (splitCommand[0] == "h" || splitCommand[0] == "help")
545 else if (splitCommand[0] == "q" || splitCommand[0] == "quit")
547 else if (splitCommand[0] == "log" && splitCommand.Length > 1)
548 LogLevel = int.Parse(splitCommand[1]);