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
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());
55 CecLogMessage message = Lib.GetNextLogMessage();
56 bool bGotMessage = !message.Empty;
59 if (((int)message.Level & LogLevel) == (int)message.Level)
62 switch (message.Level)
64 case CecLogLevel.Error:
67 case CecLogLevel.Warning:
68 strLevel = "WARNING: ";
70 case CecLogLevel.Notice:
71 strLevel = "NOTICE: ";
73 case CecLogLevel.Traffic:
74 strLevel = "TRAFFIC: ";
76 case CecLogLevel.Debug:
82 string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message);
83 Console.WriteLine(strLog);
86 message = Lib.GetNextLogMessage();
87 bGotMessage = !message.Empty;
91 public bool Connect(int timeout)
93 CecAdapter[] adapters = Lib.FindAdapters(string.Empty);
94 if (adapters.Length > 0)
95 return Connect(adapters[0].ComPort, timeout);
98 Console.WriteLine("Did not find any CEC adapters");
103 public bool Connect(string port, int timeout)
105 return Lib.Open(port, timeout);
113 public void ListDevices()
116 foreach (CecAdapter adapter in Lib.FindAdapters(string.Empty))
118 Console.WriteLine("Adapter: " + iAdapter++);
119 Console.WriteLine("Path: " + adapter.Path);
120 Console.WriteLine("Com port: " + adapter.ComPort);
124 void ShowConsoleHelp()
127 "================================================================================" + System.Environment.NewLine +
128 "Available commands:" + System.Environment.NewLine +
129 System.Environment.NewLine +
130 "[tx] {bytes} transfer bytes over the CEC line." + System.Environment.NewLine +
131 "[txn] {bytes} transfer bytes but don't wait for transmission ACK." + System.Environment.NewLine +
132 "[on] {address} power on the device with the given logical address." + System.Environment.NewLine +
133 "[standby] {address} put the device with the given address in standby mode." + System.Environment.NewLine +
134 "[la] {logical_address} change the logical address of the CEC adapter." + System.Environment.NewLine +
135 "[pa] {physical_address} change the physical address of the CEC adapter." + System.Environment.NewLine +
136 "[osd] {addr} {string} set OSD message on the specified device." + System.Environment.NewLine +
137 "[ver] {addr} get the CEC version of the specified device." + System.Environment.NewLine +
138 "[ven] {addr} get the vendor ID of the specified device." + System.Environment.NewLine +
139 "[lang] {addr} get the menu language of the specified device." + System.Environment.NewLine +
140 "[pow] {addr} get the power status of the specified device." + System.Environment.NewLine +
141 "[poll] {addr} poll the specified device." + System.Environment.NewLine +
142 "[scan] scan the CEC bus and display device info" + System.Environment.NewLine +
143 "[mon] {1|0} enable or disable CEC bus monitoring." + System.Environment.NewLine +
144 "[log] {1 - 31} change the log level. see cectypes.h for values." + System.Environment.NewLine +
145 "[ping] send a ping command to the CEC adapter." + System.Environment.NewLine +
146 "[bl] to let the adapter enter the bootloader, to upgrade" + System.Environment.NewLine +
147 " the flash rom." + System.Environment.NewLine +
148 "[r] reconnect to the CEC adapter." + System.Environment.NewLine +
149 "[h] or [help] show this help." + System.Environment.NewLine +
150 "[q] or [quit] to quit the CEC test client and switch off all" + System.Environment.NewLine +
151 " connected CEC devices." + System.Environment.NewLine +
152 "================================================================================");
155 public void MainLoop()
157 Lib.PowerOnDevices(CecLogicalAddress.Tv);
160 Lib.SetActiveSource(CecDeviceType.PlaybackDevice);
163 bool bContinue = true;
168 Console.WriteLine("waiting for input");
170 command = Console.ReadLine();
171 if (command.Length == 0)
173 string[] splitCommand = command.Split(' ');
174 if (splitCommand[0] == "tx" || splitCommand[0] == "txn")
176 CecCommand bytes = new CecCommand();
177 for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
179 bytes.PushBack(byte.Parse(splitCommand[iPtr], System.Globalization.NumberStyles.HexNumber));
182 if (command == "txn")
183 bytes.TransmitTimeout = 0;
187 else if (splitCommand[0] == "on")
189 if (splitCommand.Length > 1)
190 Lib.PowerOnDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
192 Lib.PowerOnDevices(CecLogicalAddress.Broadcast);
194 else if (splitCommand[0] == "standby")
196 if (splitCommand.Length > 1)
197 Lib.StandbyDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
199 Lib.StandbyDevices(CecLogicalAddress.Broadcast);
201 else if (splitCommand[0] == "poll")
204 if (splitCommand.Length > 1)
205 bSent = Lib.PollDevice((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
207 bSent = Lib.PollDevice(CecLogicalAddress.Broadcast);
209 Console.WriteLine("POLL message sent");
211 Console.WriteLine("POLL message not sent");
213 else if (splitCommand[0] == "la")
215 if (splitCommand.Length > 1)
216 Lib.SetLogicalAddress((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
218 else if (splitCommand[0] == "pa")
220 if (splitCommand.Length > 1)
221 Lib.SetPhysicalAddress(short.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
223 else if (splitCommand[0] == "osd")
225 if (splitCommand.Length > 2)
227 StringBuilder osdString = new StringBuilder();
228 for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
230 osdString.Append(splitCommand[iPtr]);
231 if (iPtr != splitCommand.Length - 1)
232 osdString.Append(" ");
234 Lib.SetOSDString((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber), CecDisplayControl.DisplayForDefaultTime, osdString.ToString());
237 else if (splitCommand[0] == "ping")
241 else if (splitCommand[0] == "mon")
243 bool enable = splitCommand.Length > 1 ? splitCommand[1] == "1" : false;
244 Lib.SwitchMonitoring(enable);
246 else if (splitCommand[0] == "bl")
248 Lib.StartBootloader();
250 else if (splitCommand[0] == "lang")
252 if (splitCommand.Length > 1)
254 string language = Lib.GetDeviceMenuLanguage((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
255 Console.WriteLine("Menu language: " + language);
258 else if (splitCommand[0] == "ven")
260 if (splitCommand.Length > 1)
262 CecVendorId vendor = Lib.GetDeviceVendorId((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
263 Console.WriteLine("Vendor ID: " + Lib.ToString(vendor));
266 else if (splitCommand[0] == "ver")
268 if (splitCommand.Length > 1)
270 CecVersion version = Lib.GetDeviceCecVersion((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
271 Console.WriteLine("CEC version: " + Lib.ToString(version));
274 else if (splitCommand[0] == "pow")
276 if (splitCommand.Length > 1)
278 CecPowerStatus power = Lib.GetDevicePowerStatus((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
279 Console.WriteLine("power status: " + Lib.ToString(power));
282 else if (splitCommand[0] == "r")
284 Console.WriteLine("closing the connection");
288 Console.WriteLine("opening a new connection");
292 Console.WriteLine("setting active source");
293 Lib.SetActiveSource(CecDeviceType.PlaybackDevice);
295 else if (splitCommand[0] == "scan")
297 Console.WriteLine("CEC bus information");
298 Console.WriteLine("===================");
299 CecLogicalAddresses addresses = Lib.GetActiveDevices();
300 for (int iPtr = 0; iPtr < addresses.Addresses.Count(); iPtr++)
302 CecLogicalAddress address = (CecLogicalAddress)iPtr;
303 if (!addresses.IsSet(address))
306 CecVendorId iVendorId = Lib.GetDeviceVendorId(address);
307 bool bActive = Lib.IsActiveDevice(address);
308 ushort iPhysicalAddress = Lib.GetDevicePhysicalAddress(address);
309 string strAddr = string.Format("{0,4:X}", iPhysicalAddress);
310 CecVersion iCecVersion = Lib.GetDeviceCecVersion(address);
311 CecPowerStatus power = Lib.GetDevicePowerStatus(address);
312 string osdName = Lib.GetDeviceOSDName(address);
313 string lang = Lib.GetDeviceMenuLanguage(address);
315 StringBuilder output = new StringBuilder();
316 output.AppendLine("device #" + iPtr + ": " + Lib.ToString(address));
317 output.AppendLine("address: " + strAddr);
318 output.AppendLine("active source: " + (bActive ? "yes" : "no"));
319 output.AppendLine("vendor: " + Lib.ToString(iVendorId));
320 output.AppendLine("osd string: " + osdName);
321 output.AppendLine("CEC version: " + Lib.ToString(iCecVersion));
322 output.AppendLine("power status: " + Lib.ToString(power));
323 if (!string.IsNullOrEmpty(lang))
324 output.AppendLine("language: " + lang);
326 Console.WriteLine(output.ToString());
329 else if (splitCommand[0] == "h" || splitCommand[0] == "help")
331 else if (splitCommand[0] == "q" || splitCommand[0] == "quit")
333 else if (splitCommand[0] == "log" && splitCommand.Length > 1)
334 LogLevel = int.Parse(splitCommand[1]);
338 static void Main(string[] args)
340 CecSharpClient p = new CecSharpClient();
341 if (p.Connect(10000))
347 Console.WriteLine("Could not open a connection to the CEC adapter");
352 private int LogLevel;
353 private LibCecSharp Lib;