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.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);
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 bool bContinue = true;
205 Console.WriteLine("waiting for input");
207 command = Console.ReadLine();
208 if (command.Length == 0)
210 string[] splitCommand = command.Split(' ');
211 if (splitCommand[0] == "tx" || splitCommand[0] == "txn")
213 CecCommand bytes = new CecCommand();
214 for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
216 bytes.PushBack(byte.Parse(splitCommand[iPtr], System.Globalization.NumberStyles.HexNumber));
219 if (command == "txn")
220 bytes.TransmitTimeout = 0;
224 else if (splitCommand[0] == "on")
226 if (splitCommand.Length > 1)
227 Lib.PowerOnDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
229 Lib.PowerOnDevices(CecLogicalAddress.Broadcast);
231 else if (splitCommand[0] == "standby")
233 if (splitCommand.Length > 1)
234 Lib.StandbyDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
236 Lib.StandbyDevices(CecLogicalAddress.Broadcast);
238 else if (splitCommand[0] == "poll")
241 if (splitCommand.Length > 1)
242 bSent = Lib.PollDevice((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
244 bSent = Lib.PollDevice(CecLogicalAddress.Broadcast);
246 Console.WriteLine("POLL message sent");
248 Console.WriteLine("POLL message not sent");
250 else if (splitCommand[0] == "la")
252 if (splitCommand.Length > 1)
253 Lib.SetLogicalAddress((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
255 else if (splitCommand[0] == "pa")
257 if (splitCommand.Length > 1)
258 Lib.SetPhysicalAddress(ushort.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
260 else if (splitCommand[0] == "osd")
262 if (splitCommand.Length > 2)
264 StringBuilder osdString = new StringBuilder();
265 for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
267 osdString.Append(splitCommand[iPtr]);
268 if (iPtr != splitCommand.Length - 1)
269 osdString.Append(" ");
271 Lib.SetOSDString((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber), CecDisplayControl.DisplayForDefaultTime, osdString.ToString());
274 else if (splitCommand[0] == "ping")
278 else if (splitCommand[0] == "mon")
280 bool enable = splitCommand.Length > 1 ? splitCommand[1] == "1" : false;
281 Lib.SwitchMonitoring(enable);
283 else if (splitCommand[0] == "bl")
285 Lib.StartBootloader();
287 else if (splitCommand[0] == "lang")
289 if (splitCommand.Length > 1)
291 string language = Lib.GetDeviceMenuLanguage((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
292 Console.WriteLine("Menu language: " + language);
295 else if (splitCommand[0] == "ven")
297 if (splitCommand.Length > 1)
299 CecVendorId vendor = Lib.GetDeviceVendorId((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
300 Console.WriteLine("Vendor ID: " + Lib.ToString(vendor));
303 else if (splitCommand[0] == "ver")
305 if (splitCommand.Length > 1)
307 CecVersion version = Lib.GetDeviceCecVersion((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
308 Console.WriteLine("CEC version: " + Lib.ToString(version));
311 else if (splitCommand[0] == "pow")
313 if (splitCommand.Length > 1)
315 CecPowerStatus power = Lib.GetDevicePowerStatus((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
316 Console.WriteLine("power status: " + Lib.ToString(power));
319 else if (splitCommand[0] == "r")
321 Console.WriteLine("closing the connection");
324 Console.WriteLine("opening a new connection");
327 Console.WriteLine("setting active source");
328 Lib.SetActiveSource(CecDeviceType.PlaybackDevice);
330 else if (splitCommand[0] == "scan")
332 Console.WriteLine("CEC bus information");
333 Console.WriteLine("===================");
334 CecLogicalAddresses addresses = Lib.GetActiveDevices();
335 for (int iPtr = 0; iPtr < addresses.Addresses.Length; iPtr++)
337 CecLogicalAddress address = (CecLogicalAddress)iPtr;
338 if (!addresses.IsSet(address))
341 CecVendorId iVendorId = Lib.GetDeviceVendorId(address);
342 bool bActive = Lib.IsActiveDevice(address);
343 ushort iPhysicalAddress = Lib.GetDevicePhysicalAddress(address);
344 string strAddr = string.Format("{0,4:X}", iPhysicalAddress);
345 CecVersion iCecVersion = Lib.GetDeviceCecVersion(address);
346 CecPowerStatus power = Lib.GetDevicePowerStatus(address);
347 string osdName = Lib.GetDeviceOSDName(address);
348 string lang = Lib.GetDeviceMenuLanguage(address);
350 StringBuilder output = new StringBuilder();
351 output.AppendLine("device #" + iPtr + ": " + Lib.ToString(address));
352 output.AppendLine("address: " + strAddr);
353 output.AppendLine("active source: " + (bActive ? "yes" : "no"));
354 output.AppendLine("vendor: " + Lib.ToString(iVendorId));
355 output.AppendLine("osd string: " + osdName);
356 output.AppendLine("CEC version: " + Lib.ToString(iCecVersion));
357 output.AppendLine("power status: " + Lib.ToString(power));
358 if (!string.IsNullOrEmpty(lang))
359 output.AppendLine("language: " + lang);
361 Console.WriteLine(output.ToString());
364 else if (splitCommand[0] == "h" || splitCommand[0] == "help")
366 else if (splitCommand[0] == "q" || splitCommand[0] == "quit")
368 else if (splitCommand[0] == "log" && splitCommand.Length > 1)
369 LogLevel = int.Parse(splitCommand[1]);
373 static void Main(string[] args)
375 CecSharpClient p = new CecSharpClient();
376 if (p.Connect(10000))
382 Console.WriteLine("Could not open a connection to the CEC adapter");
386 private int LogLevel;
387 private LibCecSharp Lib;
388 private LibCECConfiguration Config;