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 Config = new LibCECConfiguration();
45 Config.DeviceTypes.Types[0] = CecDeviceType.RecordingDevice;
46 Config.DeviceName = "CEC Tester";
47 Config.ClientVersion = CecClientVersion.Version1_5_0;
48 Config.SetCallbacks(this);
49 LogLevel = (int)CecLogLevel.All;
51 Lib = new LibCecSharp(Config);
53 Console.WriteLine("CEC Parser created - libcec version " + Lib.GetLibVersionMajor() + "." + Lib.GetLibVersionMinor());
56 public override int ReceiveCommand(CecCommand command)
61 public override int ReceiveKeypress(CecKeypress key)
66 public override int ReceiveLogMessage(CecLogMessage message)
68 if (((int)message.Level & LogLevel) == (int)message.Level)
71 switch (message.Level)
73 case CecLogLevel.Error:
76 case CecLogLevel.Warning:
77 strLevel = "WARNING: ";
79 case CecLogLevel.Notice:
80 strLevel = "NOTICE: ";
82 case CecLogLevel.Traffic:
83 strLevel = "TRAFFIC: ";
85 case CecLogLevel.Debug:
91 string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message);
92 Console.WriteLine(strLog);
99 CecLogMessage message = Lib.GetNextLogMessage();
100 bool bGotMessage = !message.Empty;
103 if (((int)message.Level & LogLevel) == (int)message.Level)
105 string strLevel = "";
106 switch (message.Level)
108 case CecLogLevel.Error:
109 strLevel = "ERROR: ";
111 case CecLogLevel.Warning:
112 strLevel = "WARNING: ";
114 case CecLogLevel.Notice:
115 strLevel = "NOTICE: ";
117 case CecLogLevel.Traffic:
118 strLevel = "TRAFFIC: ";
120 case CecLogLevel.Debug:
121 strLevel = "DEBUG: ";
126 string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message);
127 Console.WriteLine(strLog);
130 message = Lib.GetNextLogMessage();
131 bGotMessage = !message.Empty;
135 public bool Connect(int timeout)
137 CecAdapter[] adapters = Lib.FindAdapters(string.Empty);
138 if (adapters.Length > 0)
139 return Connect(adapters[0].ComPort, timeout);
142 Console.WriteLine("Did not find any CEC adapters");
147 public bool Connect(string port, int timeout)
149 return Lib.Open(port, timeout);
157 public void ListDevices()
160 foreach (CecAdapter adapter in Lib.FindAdapters(string.Empty))
162 Console.WriteLine("Adapter: " + iAdapter++);
163 Console.WriteLine("Path: " + adapter.Path);
164 Console.WriteLine("Com port: " + adapter.ComPort);
168 void ShowConsoleHelp()
171 "================================================================================" + System.Environment.NewLine +
172 "Available commands:" + System.Environment.NewLine +
173 System.Environment.NewLine +
174 "[tx] {bytes} transfer bytes over the CEC line." + System.Environment.NewLine +
175 "[txn] {bytes} transfer bytes but don't wait for transmission ACK." + System.Environment.NewLine +
176 "[on] {address} power on the device with the given logical address." + System.Environment.NewLine +
177 "[standby] {address} put the device with the given address in standby mode." + System.Environment.NewLine +
178 "[la] {logical_address} change the logical address of the CEC adapter." + System.Environment.NewLine +
179 "[pa] {physical_address} change the physical address of the CEC adapter." + System.Environment.NewLine +
180 "[osd] {addr} {string} set OSD message on the specified device." + System.Environment.NewLine +
181 "[ver] {addr} get the CEC version of the specified device." + System.Environment.NewLine +
182 "[ven] {addr} get the vendor ID of the specified device." + System.Environment.NewLine +
183 "[lang] {addr} get the menu language of the specified device." + System.Environment.NewLine +
184 "[pow] {addr} get the power status of the specified device." + System.Environment.NewLine +
185 "[poll] {addr} poll the specified device." + System.Environment.NewLine +
186 "[scan] scan the CEC bus and display device info" + System.Environment.NewLine +
187 "[mon] {1|0} enable or disable CEC bus monitoring." + System.Environment.NewLine +
188 "[log] {1 - 31} change the log level. see cectypes.h for values." + System.Environment.NewLine +
189 "[ping] send a ping command to the CEC adapter." + System.Environment.NewLine +
190 "[bl] to let the adapter enter the bootloader, to upgrade" + System.Environment.NewLine +
191 " the flash rom." + System.Environment.NewLine +
192 "[r] reconnect to the CEC adapter." + System.Environment.NewLine +
193 "[h] or [help] show this help." + System.Environment.NewLine +
194 "[q] or [quit] to quit the CEC test client and switch off all" + System.Environment.NewLine +
195 " connected CEC devices." + System.Environment.NewLine +
196 "================================================================================");
199 public void MainLoop()
201 Lib.PowerOnDevices(CecLogicalAddress.Tv);
202 Lib.SetActiveSource(CecDeviceType.PlaybackDevice);
204 bool bContinue = true;
208 Console.WriteLine("waiting for input");
210 command = Console.ReadLine();
211 if (command.Length == 0)
213 string[] splitCommand = command.Split(' ');
214 if (splitCommand[0] == "tx" || splitCommand[0] == "txn")
216 CecCommand bytes = new CecCommand();
217 for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
219 bytes.PushBack(byte.Parse(splitCommand[iPtr], System.Globalization.NumberStyles.HexNumber));
222 if (command == "txn")
223 bytes.TransmitTimeout = 0;
227 else if (splitCommand[0] == "on")
229 if (splitCommand.Length > 1)
230 Lib.PowerOnDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
232 Lib.PowerOnDevices(CecLogicalAddress.Broadcast);
234 else if (splitCommand[0] == "standby")
236 if (splitCommand.Length > 1)
237 Lib.StandbyDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
239 Lib.StandbyDevices(CecLogicalAddress.Broadcast);
241 else if (splitCommand[0] == "poll")
244 if (splitCommand.Length > 1)
245 bSent = Lib.PollDevice((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
247 bSent = Lib.PollDevice(CecLogicalAddress.Broadcast);
249 Console.WriteLine("POLL message sent");
251 Console.WriteLine("POLL message not sent");
253 else if (splitCommand[0] == "la")
255 if (splitCommand.Length > 1)
256 Lib.SetLogicalAddress((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
258 else if (splitCommand[0] == "pa")
260 if (splitCommand.Length > 1)
261 Lib.SetPhysicalAddress(ushort.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
263 else if (splitCommand[0] == "osd")
265 if (splitCommand.Length > 2)
267 StringBuilder osdString = new StringBuilder();
268 for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
270 osdString.Append(splitCommand[iPtr]);
271 if (iPtr != splitCommand.Length - 1)
272 osdString.Append(" ");
274 Lib.SetOSDString((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber), CecDisplayControl.DisplayForDefaultTime, osdString.ToString());
277 else if (splitCommand[0] == "ping")
281 else if (splitCommand[0] == "mon")
283 bool enable = splitCommand.Length > 1 ? splitCommand[1] == "1" : false;
284 Lib.SwitchMonitoring(enable);
286 else if (splitCommand[0] == "bl")
288 Lib.StartBootloader();
290 else if (splitCommand[0] == "lang")
292 if (splitCommand.Length > 1)
294 string language = Lib.GetDeviceMenuLanguage((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
295 Console.WriteLine("Menu language: " + language);
298 else if (splitCommand[0] == "ven")
300 if (splitCommand.Length > 1)
302 CecVendorId vendor = Lib.GetDeviceVendorId((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
303 Console.WriteLine("Vendor ID: " + Lib.ToString(vendor));
306 else if (splitCommand[0] == "ver")
308 if (splitCommand.Length > 1)
310 CecVersion version = Lib.GetDeviceCecVersion((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
311 Console.WriteLine("CEC version: " + Lib.ToString(version));
314 else if (splitCommand[0] == "pow")
316 if (splitCommand.Length > 1)
318 CecPowerStatus power = Lib.GetDevicePowerStatus((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
319 Console.WriteLine("power status: " + Lib.ToString(power));
322 else if (splitCommand[0] == "r")
324 Console.WriteLine("closing the connection");
327 Console.WriteLine("opening a new connection");
330 Console.WriteLine("setting active source");
331 Lib.SetActiveSource(CecDeviceType.PlaybackDevice);
333 else if (splitCommand[0] == "scan")
335 Console.WriteLine("CEC bus information");
336 Console.WriteLine("===================");
337 CecLogicalAddresses addresses = Lib.GetActiveDevices();
338 for (int iPtr = 0; iPtr < addresses.Addresses.Length; iPtr++)
340 CecLogicalAddress address = (CecLogicalAddress)iPtr;
341 if (!addresses.IsSet(address))
344 CecVendorId iVendorId = Lib.GetDeviceVendorId(address);
345 bool bActive = Lib.IsActiveDevice(address);
346 ushort iPhysicalAddress = Lib.GetDevicePhysicalAddress(address);
347 string strAddr = string.Format("{0,4:X}", iPhysicalAddress);
348 CecVersion iCecVersion = Lib.GetDeviceCecVersion(address);
349 CecPowerStatus power = Lib.GetDevicePowerStatus(address);
350 string osdName = Lib.GetDeviceOSDName(address);
351 string lang = Lib.GetDeviceMenuLanguage(address);
353 StringBuilder output = new StringBuilder();
354 output.AppendLine("device #" + iPtr + ": " + Lib.ToString(address));
355 output.AppendLine("address: " + strAddr);
356 output.AppendLine("active source: " + (bActive ? "yes" : "no"));
357 output.AppendLine("vendor: " + Lib.ToString(iVendorId));
358 output.AppendLine("osd string: " + osdName);
359 output.AppendLine("CEC version: " + Lib.ToString(iCecVersion));
360 output.AppendLine("power status: " + Lib.ToString(power));
361 if (!string.IsNullOrEmpty(lang))
362 output.AppendLine("language: " + lang);
364 Console.WriteLine(output.ToString());
367 else if (splitCommand[0] == "h" || splitCommand[0] == "help")
369 else if (splitCommand[0] == "q" || splitCommand[0] == "quit")
371 else if (splitCommand[0] == "log" && splitCommand.Length > 1)
372 LogLevel = int.Parse(splitCommand[1]);
376 static void Main(string[] args)
378 CecSharpClient p = new CecSharpClient();
379 if (p.Connect(10000))
385 Console.WriteLine("Could not open a connection to the CEC adapter");
389 private int LogLevel;
390 private LibCecSharp Lib;
391 private LibCECConfiguration Config;