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/
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.Version1_5_0;
47 Config.SetCallbacks(this);
48 LogLevel = (int)CecLogLevel.All;
50 Lib = new LibCecSharp(Config);
52 Console.WriteLine("CEC Parser created - libcec version " + Lib.ToString(Config.ServerVersion));
55 public override int ReceiveCommand(CecCommand command)
60 public override int ReceiveKeypress(CecKeypress key)
65 public override int ReceiveLogMessage(CecLogMessage message)
67 if (((int)message.Level & LogLevel) == (int)message.Level)
70 switch (message.Level)
72 case CecLogLevel.Error:
75 case CecLogLevel.Warning:
76 strLevel = "WARNING: ";
78 case CecLogLevel.Notice:
79 strLevel = "NOTICE: ";
81 case CecLogLevel.Traffic:
82 strLevel = "TRAFFIC: ";
84 case CecLogLevel.Debug:
90 string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message);
91 Console.WriteLine(strLog);
96 public bool Connect(int timeout)
98 CecAdapter[] adapters = Lib.FindAdapters(string.Empty);
99 if (adapters.Length > 0)
100 return Connect(adapters[0].ComPort, timeout);
103 Console.WriteLine("Did not find any CEC adapters");
108 public bool Connect(string port, int timeout)
110 return Lib.Open(port, timeout);
118 public void ListDevices()
121 foreach (CecAdapter adapter in Lib.FindAdapters(string.Empty))
123 Console.WriteLine("Adapter: " + iAdapter++);
124 Console.WriteLine("Path: " + adapter.Path);
125 Console.WriteLine("Com port: " + adapter.ComPort);
129 void ShowConsoleHelp()
132 "================================================================================" + Environment.NewLine +
133 "Available commands:" + Environment.NewLine +
134 Environment.NewLine +
135 "[tx] {bytes} transfer bytes over the CEC line." + Environment.NewLine +
136 "[txn] {bytes} transfer bytes but don't wait for transmission ACK." + Environment.NewLine +
137 "[on] {address} power on the device with the given logical address." + Environment.NewLine +
138 "[standby] {address} put the device with the given address in standby mode." + Environment.NewLine +
139 "[la] {logical_address} change the logical address of the CEC adapter." + Environment.NewLine +
140 "[pa] {physical_address} change the physical address of the CEC adapter." + Environment.NewLine +
141 "[osd] {addr} {string} set OSD message on the specified device." + Environment.NewLine +
142 "[ver] {addr} get the CEC version of the specified device." + Environment.NewLine +
143 "[ven] {addr} get the vendor ID of the specified device." + Environment.NewLine +
144 "[lang] {addr} get the menu language of the specified device." + Environment.NewLine +
145 "[pow] {addr} get the power status of the specified device." + Environment.NewLine +
146 "[poll] {addr} poll the specified device." + Environment.NewLine +
147 "[scan] scan the CEC bus and display device info" + Environment.NewLine +
148 "[mon] {1|0} enable or disable CEC bus monitoring." + Environment.NewLine +
149 "[log] {1 - 31} change the log level. see cectypes.h for values." + Environment.NewLine +
150 "[ping] send a ping command to the CEC adapter." + Environment.NewLine +
151 "[bl] to let the adapter enter the bootloader, to upgrade" + Environment.NewLine +
152 " the flash rom." + Environment.NewLine +
153 "[r] reconnect to the CEC adapter." + Environment.NewLine +
154 "[h] or [help] show this help." + Environment.NewLine +
155 "[q] or [quit] to quit the CEC test client and switch off all" + Environment.NewLine +
156 " connected CEC devices." + Environment.NewLine +
157 "================================================================================");
160 public void MainLoop()
162 bool bContinue = true;
166 Console.WriteLine("waiting for input");
168 command = Console.ReadLine();
169 if (command != null && command.Length == 0)
171 string[] splitCommand = command.Split(' ');
172 if (splitCommand[0] == "tx" || splitCommand[0] == "txn")
174 CecCommand bytes = new CecCommand();
175 for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
177 bytes.PushBack(byte.Parse(splitCommand[iPtr], System.Globalization.NumberStyles.HexNumber));
180 if (command == "txn")
181 bytes.TransmitTimeout = 0;
185 else if (splitCommand[0] == "on")
187 if (splitCommand.Length > 1)
188 Lib.PowerOnDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
190 Lib.PowerOnDevices(CecLogicalAddress.Broadcast);
192 else if (splitCommand[0] == "standby")
194 if (splitCommand.Length > 1)
195 Lib.StandbyDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
197 Lib.StandbyDevices(CecLogicalAddress.Broadcast);
199 else if (splitCommand[0] == "poll")
202 if (splitCommand.Length > 1)
203 bSent = Lib.PollDevice((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
205 bSent = Lib.PollDevice(CecLogicalAddress.Broadcast);
207 Console.WriteLine("POLL message sent");
209 Console.WriteLine("POLL message not sent");
211 else if (splitCommand[0] == "la")
213 if (splitCommand.Length > 1)
214 Lib.SetLogicalAddress((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
216 else if (splitCommand[0] == "pa")
218 if (splitCommand.Length > 1)
219 Lib.SetPhysicalAddress(ushort.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
221 else if (splitCommand[0] == "osd")
223 if (splitCommand.Length > 2)
225 StringBuilder osdString = new StringBuilder();
226 for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
228 osdString.Append(splitCommand[iPtr]);
229 if (iPtr != splitCommand.Length - 1)
230 osdString.Append(" ");
232 Lib.SetOSDString((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber), CecDisplayControl.DisplayForDefaultTime, osdString.ToString());
235 else if (splitCommand[0] == "ping")
239 else if (splitCommand[0] == "mon")
241 bool enable = splitCommand.Length > 1 ? splitCommand[1] == "1" : false;
242 Lib.SwitchMonitoring(enable);
244 else if (splitCommand[0] == "bl")
246 Lib.StartBootloader();
248 else if (splitCommand[0] == "lang")
250 if (splitCommand.Length > 1)
252 string language = Lib.GetDeviceMenuLanguage((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
253 Console.WriteLine("Menu language: " + language);
256 else if (splitCommand[0] == "ven")
258 if (splitCommand.Length > 1)
260 CecVendorId vendor = Lib.GetDeviceVendorId((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
261 Console.WriteLine("Vendor ID: " + Lib.ToString(vendor));
264 else if (splitCommand[0] == "ver")
266 if (splitCommand.Length > 1)
268 CecVersion version = Lib.GetDeviceCecVersion((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
269 Console.WriteLine("CEC version: " + Lib.ToString(version));
272 else if (splitCommand[0] == "pow")
274 if (splitCommand.Length > 1)
276 CecPowerStatus power = Lib.GetDevicePowerStatus((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
277 Console.WriteLine("power status: " + Lib.ToString(power));
280 else if (splitCommand[0] == "r")
282 Console.WriteLine("closing the connection");
285 Console.WriteLine("opening a new connection");
288 Console.WriteLine("setting active source");
289 Lib.SetActiveSource(CecDeviceType.PlaybackDevice);
291 else if (splitCommand[0] == "scan")
293 Console.WriteLine("CEC bus information");
294 Console.WriteLine("===================");
295 CecLogicalAddresses addresses = Lib.GetActiveDevices();
296 for (int iPtr = 0; iPtr < addresses.Addresses.Length; iPtr++)
298 CecLogicalAddress address = (CecLogicalAddress)iPtr;
299 if (!addresses.IsSet(address))
302 CecVendorId iVendorId = Lib.GetDeviceVendorId(address);
303 bool bActive = Lib.IsActiveDevice(address);
304 ushort iPhysicalAddress = Lib.GetDevicePhysicalAddress(address);
305 string strAddr = string.Format("{0,4:X}", iPhysicalAddress);
306 CecVersion iCecVersion = Lib.GetDeviceCecVersion(address);
307 CecPowerStatus power = Lib.GetDevicePowerStatus(address);
308 string osdName = Lib.GetDeviceOSDName(address);
309 string lang = Lib.GetDeviceMenuLanguage(address);
311 StringBuilder output = new StringBuilder();
312 output.AppendLine("device #" + iPtr + ": " + Lib.ToString(address));
313 output.AppendLine("address: " + strAddr);
314 output.AppendLine("active source: " + (bActive ? "yes" : "no"));
315 output.AppendLine("vendor: " + Lib.ToString(iVendorId));
316 output.AppendLine("osd string: " + osdName);
317 output.AppendLine("CEC version: " + Lib.ToString(iCecVersion));
318 output.AppendLine("power status: " + Lib.ToString(power));
319 if (!string.IsNullOrEmpty(lang))
320 output.AppendLine("language: " + lang);
322 Console.WriteLine(output.ToString());
325 else if (splitCommand[0] == "h" || splitCommand[0] == "help")
327 else if (splitCommand[0] == "q" || splitCommand[0] == "quit")
329 else if (splitCommand[0] == "log" && splitCommand.Length > 1)
330 LogLevel = int.Parse(splitCommand[1]);
334 static void Main(string[] args)
336 CecSharpClient p = new CecSharpClient();
337 if (p.Connect(10000))
343 Console.WriteLine("Could not open a connection to the CEC adapter");
347 private int LogLevel;
348 private LibCecSharp Lib;
349 private LibCECConfiguration Config;