2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011 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/
34 using System.Collections.Generic;
38 namespace CecSharpClient
40 class CecSharpClient : CecCallbackMethods
42 public CecSharpClient()
44 CecDeviceTypeList types = new CecDeviceTypeList();
45 types.Types[0] = CecDeviceType.RecordingDevice;
47 Lib = new LibCecSharp("CEC Tester", types);
48 LogLevel = (int) CecLogLevel.All;
50 Console.WriteLine("CEC Parser created - libcec version " + Lib.GetLibVersionMajor() + "." + Lib.GetLibVersionMinor());
53 public override int ReceiveCommand(CecCommand command)
58 public override int ReceiveKeypress(CecKeypress key)
63 public override int ReceiveLogMessage(CecLogMessage message)
65 if (((int)message.Level & LogLevel) == (int)message.Level)
68 switch (message.Level)
70 case CecLogLevel.Error:
73 case CecLogLevel.Warning:
74 strLevel = "WARNING: ";
76 case CecLogLevel.Notice:
77 strLevel = "NOTICE: ";
79 case CecLogLevel.Traffic:
80 strLevel = "TRAFFIC: ";
82 case CecLogLevel.Debug:
88 string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message);
89 Console.WriteLine(strLog);
96 CecLogMessage message = Lib.GetNextLogMessage();
97 bool bGotMessage = !message.Empty;
100 if (((int)message.Level & LogLevel) == (int)message.Level)
102 string strLevel = "";
103 switch (message.Level)
105 case CecLogLevel.Error:
106 strLevel = "ERROR: ";
108 case CecLogLevel.Warning:
109 strLevel = "WARNING: ";
111 case CecLogLevel.Notice:
112 strLevel = "NOTICE: ";
114 case CecLogLevel.Traffic:
115 strLevel = "TRAFFIC: ";
117 case CecLogLevel.Debug:
118 strLevel = "DEBUG: ";
123 string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message);
124 Console.WriteLine(strLog);
127 message = Lib.GetNextLogMessage();
128 bGotMessage = !message.Empty;
132 public bool Connect(int timeout)
134 CecAdapter[] adapters = Lib.FindAdapters(string.Empty);
135 if (adapters.Length > 0)
136 return Connect(adapters[0].ComPort, timeout);
139 Console.WriteLine("Did not find any CEC adapters");
144 public bool Connect(string port, int timeout)
146 return Lib.Open(port, timeout);
154 public void ListDevices()
157 foreach (CecAdapter adapter in Lib.FindAdapters(string.Empty))
159 Console.WriteLine("Adapter: " + iAdapter++);
160 Console.WriteLine("Path: " + adapter.Path);
161 Console.WriteLine("Com port: " + adapter.ComPort);
165 void ShowConsoleHelp()
168 "================================================================================" + System.Environment.NewLine +
169 "Available commands:" + System.Environment.NewLine +
170 System.Environment.NewLine +
171 "[tx] {bytes} transfer bytes over the CEC line." + System.Environment.NewLine +
172 "[txn] {bytes} transfer bytes but don't wait for transmission ACK." + System.Environment.NewLine +
173 "[on] {address} power on the device with the given logical address." + System.Environment.NewLine +
174 "[standby] {address} put the device with the given address in standby mode." + System.Environment.NewLine +
175 "[la] {logical_address} change the logical address of the CEC adapter." + System.Environment.NewLine +
176 "[pa] {physical_address} change the physical address of the CEC adapter." + System.Environment.NewLine +
177 "[osd] {addr} {string} set OSD message on the specified device." + System.Environment.NewLine +
178 "[ver] {addr} get the CEC version of the specified device." + System.Environment.NewLine +
179 "[ven] {addr} get the vendor ID of the specified device." + System.Environment.NewLine +
180 "[lang] {addr} get the menu language of the specified device." + System.Environment.NewLine +
181 "[pow] {addr} get the power status of the specified device." + System.Environment.NewLine +
182 "[poll] {addr} poll the specified device." + System.Environment.NewLine +
183 "[scan] scan the CEC bus and display device info" + System.Environment.NewLine +
184 "[mon] {1|0} enable or disable CEC bus monitoring." + System.Environment.NewLine +
185 "[log] {1 - 31} change the log level. see cectypes.h for values." + System.Environment.NewLine +
186 "[ping] send a ping command to the CEC adapter." + System.Environment.NewLine +
187 "[bl] to let the adapter enter the bootloader, to upgrade" + System.Environment.NewLine +
188 " the flash rom." + System.Environment.NewLine +
189 "[r] reconnect to the CEC adapter." + System.Environment.NewLine +
190 "[h] or [help] show this help." + System.Environment.NewLine +
191 "[q] or [quit] to quit the CEC test client and switch off all" + System.Environment.NewLine +
192 " connected CEC devices." + System.Environment.NewLine +
193 "================================================================================");
196 public void MainLoop()
198 Lib.EnableCallbacks(this);
200 Lib.PowerOnDevices(CecLogicalAddress.Tv);
201 Lib.SetActiveSource(CecDeviceType.PlaybackDevice);
203 bool bContinue = true;
207 Console.WriteLine("waiting for input");
209 command = Console.ReadLine();
210 if (command.Length == 0)
212 string[] splitCommand = command.Split(' ');
213 if (splitCommand[0] == "tx" || splitCommand[0] == "txn")
215 CecCommand bytes = new CecCommand();
216 for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
218 bytes.PushBack(byte.Parse(splitCommand[iPtr], System.Globalization.NumberStyles.HexNumber));
221 if (command == "txn")
222 bytes.TransmitTimeout = 0;
226 else if (splitCommand[0] == "on")
228 if (splitCommand.Length > 1)
229 Lib.PowerOnDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
231 Lib.PowerOnDevices(CecLogicalAddress.Broadcast);
233 else if (splitCommand[0] == "standby")
235 if (splitCommand.Length > 1)
236 Lib.StandbyDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
238 Lib.StandbyDevices(CecLogicalAddress.Broadcast);
240 else if (splitCommand[0] == "poll")
243 if (splitCommand.Length > 1)
244 bSent = Lib.PollDevice((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
246 bSent = Lib.PollDevice(CecLogicalAddress.Broadcast);
248 Console.WriteLine("POLL message sent");
250 Console.WriteLine("POLL message not sent");
252 else if (splitCommand[0] == "la")
254 if (splitCommand.Length > 1)
255 Lib.SetLogicalAddress((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
257 else if (splitCommand[0] == "pa")
259 if (splitCommand.Length > 1)
260 Lib.SetPhysicalAddress(short.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
262 else if (splitCommand[0] == "osd")
264 if (splitCommand.Length > 2)
266 StringBuilder osdString = new StringBuilder();
267 for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
269 osdString.Append(splitCommand[iPtr]);
270 if (iPtr != splitCommand.Length - 1)
271 osdString.Append(" ");
273 Lib.SetOSDString((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber), CecDisplayControl.DisplayForDefaultTime, osdString.ToString());
276 else if (splitCommand[0] == "ping")
280 else if (splitCommand[0] == "mon")
282 bool enable = splitCommand.Length > 1 ? splitCommand[1] == "1" : false;
283 Lib.SwitchMonitoring(enable);
285 else if (splitCommand[0] == "bl")
287 Lib.StartBootloader();
289 else if (splitCommand[0] == "lang")
291 if (splitCommand.Length > 1)
293 string language = Lib.GetDeviceMenuLanguage((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
294 Console.WriteLine("Menu language: " + language);
297 else if (splitCommand[0] == "ven")
299 if (splitCommand.Length > 1)
301 CecVendorId vendor = Lib.GetDeviceVendorId((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
302 Console.WriteLine("Vendor ID: " + Lib.ToString(vendor));
305 else if (splitCommand[0] == "ver")
307 if (splitCommand.Length > 1)
309 CecVersion version = Lib.GetDeviceCecVersion((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
310 Console.WriteLine("CEC version: " + Lib.ToString(version));
313 else if (splitCommand[0] == "pow")
315 if (splitCommand.Length > 1)
317 CecPowerStatus power = Lib.GetDevicePowerStatus((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
318 Console.WriteLine("power status: " + Lib.ToString(power));
321 else if (splitCommand[0] == "r")
323 Console.WriteLine("closing the connection");
326 Console.WriteLine("opening a new connection");
329 Console.WriteLine("setting active source");
330 Lib.SetActiveSource(CecDeviceType.PlaybackDevice);
332 else if (splitCommand[0] == "scan")
334 Console.WriteLine("CEC bus information");
335 Console.WriteLine("===================");
336 CecLogicalAddresses addresses = Lib.GetActiveDevices();
337 for (int iPtr = 0; iPtr < addresses.Addresses.Count(); iPtr++)
339 CecLogicalAddress address = (CecLogicalAddress)iPtr;
340 if (!addresses.IsSet(address))
343 CecVendorId iVendorId = Lib.GetDeviceVendorId(address);
344 bool bActive = Lib.IsActiveDevice(address);
345 ushort iPhysicalAddress = Lib.GetDevicePhysicalAddress(address);
346 string strAddr = string.Format("{0,4:X}", iPhysicalAddress);
347 CecVersion iCecVersion = Lib.GetDeviceCecVersion(address);
348 CecPowerStatus power = Lib.GetDevicePowerStatus(address);
349 string osdName = Lib.GetDeviceOSDName(address);
350 string lang = Lib.GetDeviceMenuLanguage(address);
352 StringBuilder output = new StringBuilder();
353 output.AppendLine("device #" + iPtr + ": " + Lib.ToString(address));
354 output.AppendLine("address: " + strAddr);
355 output.AppendLine("active source: " + (bActive ? "yes" : "no"));
356 output.AppendLine("vendor: " + Lib.ToString(iVendorId));
357 output.AppendLine("osd string: " + osdName);
358 output.AppendLine("CEC version: " + Lib.ToString(iCecVersion));
359 output.AppendLine("power status: " + Lib.ToString(power));
360 if (!string.IsNullOrEmpty(lang))
361 output.AppendLine("language: " + lang);
363 Console.WriteLine(output.ToString());
366 else if (splitCommand[0] == "h" || splitCommand[0] == "help")
368 else if (splitCommand[0] == "q" || splitCommand[0] == "quit")
370 else if (splitCommand[0] == "log" && splitCommand.Length > 1)
371 LogLevel = int.Parse(splitCommand[1]);
375 static void Main(string[] args)
377 CecSharpClient p = new CecSharpClient();
378 if (p.Connect(10000))
384 Console.WriteLine("Could not open a connection to the CEC adapter");
388 private int LogLevel;
389 private LibCecSharp Lib;