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/
37 namespace CecSharpClient
39 class CecSharpClient : CecCallbackMethods
41 public CecSharpClient()
43 Config = new LibCECConfiguration();
44 Config.DeviceTypes.Types[0] = CecDeviceType.RecordingDevice;
45 Config.DeviceName = "CEC Tester";
46 Config.ClientVersion = CecClientVersion.CurrentVersion;
47 Config.SetCallbacks(this);
48 LogLevel = (int)CecLogLevel.All;
50 Lib = new LibCecSharp(Config);
51 Lib.InitVideoStandalone();
53 Console.WriteLine("CEC Parser created - libCEC version " + Lib.ToString(Config.ServerVersion));
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);
97 public bool Connect(int timeout)
99 CecAdapter[] adapters = Lib.FindAdapters(string.Empty);
100 if (adapters.Length > 0)
101 return Connect(adapters[0].ComPort, timeout);
104 Console.WriteLine("Did not find any CEC adapters");
109 public bool Connect(string port, int timeout)
111 return Lib.Open(port, timeout);
119 public void ListDevices()
122 foreach (CecAdapter adapter in Lib.FindAdapters(string.Empty))
124 Console.WriteLine("Adapter: " + iAdapter++);
125 Console.WriteLine("Path: " + adapter.Path);
126 Console.WriteLine("Com port: " + adapter.ComPort);
130 void ShowConsoleHelp()
133 "================================================================================" + Environment.NewLine +
134 "Available commands:" + Environment.NewLine +
135 Environment.NewLine +
136 "[tx] {bytes} transfer bytes over the CEC line." + Environment.NewLine +
137 "[txn] {bytes} transfer bytes but don't wait for transmission ACK." + Environment.NewLine +
138 "[on] {address} power on the device with the given logical address." + Environment.NewLine +
139 "[standby] {address} put the device with the given address in standby mode." + Environment.NewLine +
140 "[la] {logical_address} change the logical address of the CEC adapter." + Environment.NewLine +
141 "[pa] {physical_address} change the physical address of the CEC adapter." + Environment.NewLine +
142 "[osd] {addr} {string} set OSD message on the specified device." + Environment.NewLine +
143 "[ver] {addr} get the CEC version of the specified device." + Environment.NewLine +
144 "[ven] {addr} get the vendor ID of the specified device." + Environment.NewLine +
145 "[lang] {addr} get the menu language of the specified device." + Environment.NewLine +
146 "[pow] {addr} get the power status of the specified device." + Environment.NewLine +
147 "[poll] {addr} poll the specified device." + Environment.NewLine +
148 "[scan] scan the CEC bus and display device info" + Environment.NewLine +
149 "[mon] {1|0} enable or disable CEC bus monitoring." + Environment.NewLine +
150 "[log] {1 - 31} change the log level. see cectypes.h for values." + Environment.NewLine +
151 "[ping] send a ping command to the CEC adapter." + Environment.NewLine +
152 "[bl] to let the adapter enter the bootloader, to upgrade" + Environment.NewLine +
153 " the flash rom." + Environment.NewLine +
154 "[r] reconnect to the CEC adapter." + Environment.NewLine +
155 "[h] or [help] show this help." + Environment.NewLine +
156 "[q] or [quit] to quit the CEC test client and switch off all" + Environment.NewLine +
157 " connected CEC devices." + Environment.NewLine +
158 "================================================================================");
161 public void MainLoop()
163 bool bContinue = true;
167 Console.WriteLine("waiting for input");
169 command = Console.ReadLine();
170 if (command != null && command.Length == 0)
172 string[] splitCommand = command.Split(' ');
173 if (splitCommand[0] == "tx" || splitCommand[0] == "txn")
175 CecCommand bytes = new CecCommand();
176 for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
178 bytes.PushBack(byte.Parse(splitCommand[iPtr], System.Globalization.NumberStyles.HexNumber));
181 if (command == "txn")
182 bytes.TransmitTimeout = 0;
186 else if (splitCommand[0] == "on")
188 if (splitCommand.Length > 1)
189 Lib.PowerOnDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
191 Lib.PowerOnDevices(CecLogicalAddress.Broadcast);
193 else if (splitCommand[0] == "standby")
195 if (splitCommand.Length > 1)
196 Lib.StandbyDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
198 Lib.StandbyDevices(CecLogicalAddress.Broadcast);
200 else if (splitCommand[0] == "poll")
203 if (splitCommand.Length > 1)
204 bSent = Lib.PollDevice((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
206 bSent = Lib.PollDevice(CecLogicalAddress.Broadcast);
208 Console.WriteLine("POLL message sent");
210 Console.WriteLine("POLL message not sent");
212 else if (splitCommand[0] == "la")
214 if (splitCommand.Length > 1)
215 Lib.SetLogicalAddress((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
217 else if (splitCommand[0] == "pa")
219 if (splitCommand.Length > 1)
220 Lib.SetPhysicalAddress(ushort.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
222 else if (splitCommand[0] == "osd")
224 if (splitCommand.Length > 2)
226 StringBuilder osdString = new StringBuilder();
227 for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
229 osdString.Append(splitCommand[iPtr]);
230 if (iPtr != splitCommand.Length - 1)
231 osdString.Append(" ");
233 Lib.SetOSDString((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber), CecDisplayControl.DisplayForDefaultTime, osdString.ToString());
236 else if (splitCommand[0] == "ping")
240 else if (splitCommand[0] == "mon")
242 bool enable = splitCommand.Length > 1 ? splitCommand[1] == "1" : false;
243 Lib.SwitchMonitoring(enable);
245 else if (splitCommand[0] == "bl")
247 Lib.StartBootloader();
249 else if (splitCommand[0] == "lang")
251 if (splitCommand.Length > 1)
253 string language = Lib.GetDeviceMenuLanguage((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
254 Console.WriteLine("Menu language: " + language);
257 else if (splitCommand[0] == "ven")
259 if (splitCommand.Length > 1)
261 CecVendorId vendor = Lib.GetDeviceVendorId((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
262 Console.WriteLine("Vendor ID: " + Lib.ToString(vendor));
265 else if (splitCommand[0] == "ver")
267 if (splitCommand.Length > 1)
269 CecVersion version = Lib.GetDeviceCecVersion((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
270 Console.WriteLine("CEC version: " + Lib.ToString(version));
273 else if (splitCommand[0] == "pow")
275 if (splitCommand.Length > 1)
277 CecPowerStatus power = Lib.GetDevicePowerStatus((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
278 Console.WriteLine("power status: " + Lib.ToString(power));
281 else if (splitCommand[0] == "r")
283 Console.WriteLine("closing the connection");
286 Console.WriteLine("opening a new connection");
289 Console.WriteLine("setting active source");
290 Lib.SetActiveSource(CecDeviceType.PlaybackDevice);
292 else if (splitCommand[0] == "scan")
294 Console.WriteLine("CEC bus information");
295 Console.WriteLine("===================");
296 CecLogicalAddresses addresses = Lib.GetActiveDevices();
297 for (int iPtr = 0; iPtr < addresses.Addresses.Length; iPtr++)
299 CecLogicalAddress address = (CecLogicalAddress)iPtr;
300 if (!addresses.IsSet(address))
303 CecVendorId iVendorId = Lib.GetDeviceVendorId(address);
304 bool bActive = Lib.IsActiveDevice(address);
305 ushort iPhysicalAddress = Lib.GetDevicePhysicalAddress(address);
306 string strAddr = string.Format("{0,4:X}", iPhysicalAddress);
307 CecVersion iCecVersion = Lib.GetDeviceCecVersion(address);
308 CecPowerStatus power = Lib.GetDevicePowerStatus(address);
309 string osdName = Lib.GetDeviceOSDName(address);
310 string lang = Lib.GetDeviceMenuLanguage(address);
312 StringBuilder output = new StringBuilder();
313 output.AppendLine("device #" + iPtr + ": " + Lib.ToString(address));
314 output.AppendLine("address: " + strAddr);
315 output.AppendLine("active source: " + (bActive ? "yes" : "no"));
316 output.AppendLine("vendor: " + Lib.ToString(iVendorId));
317 output.AppendLine("osd string: " + osdName);
318 output.AppendLine("CEC version: " + Lib.ToString(iCecVersion));
319 output.AppendLine("power status: " + Lib.ToString(power));
320 if (!string.IsNullOrEmpty(lang))
321 output.AppendLine("language: " + lang);
323 Console.WriteLine(output.ToString());
326 else if (splitCommand[0] == "h" || splitCommand[0] == "help")
328 else if (splitCommand[0] == "q" || splitCommand[0] == "quit")
330 else if (splitCommand[0] == "log" && splitCommand.Length > 1)
331 LogLevel = int.Parse(splitCommand[1]);
335 static void Main(string[] args)
337 CecSharpClient p = new CecSharpClient();
338 if (p.Connect(10000))
344 Console.WriteLine("Could not open a connection to the CEC adapter");
348 private int LogLevel;
349 private LibCecSharp Lib;
350 private LibCECConfiguration Config;