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.PlaybackDevice;
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 "[tx 40 00 FF 11 22 33] sends bytes 0x40 0x00 0xFF 0x11 0x22 0x33" + System.Environment.NewLine +
133 System.Environment.NewLine +
134 "on {address} power on the device with the given logical address." + System.Environment.NewLine +
135 "[on 5] power on a connected audio system" + System.Environment.NewLine +
136 System.Environment.NewLine +
137 "standby {address} put the device with the given address in standby mode." + System.Environment.NewLine +
138 "[standby 0] powers off the TV" + System.Environment.NewLine +
139 System.Environment.NewLine +
140 "la {logical_address} change the logical address of the CEC adapter." + System.Environment.NewLine +
141 "[la 4] logical address 4" + System.Environment.NewLine +
142 System.Environment.NewLine +
143 "pa {physical_address} change the physical address of the CEC adapter." + System.Environment.NewLine +
144 "[pa 1000] physical address 1.0.0.0" + System.Environment.NewLine +
145 System.Environment.NewLine +
146 "osd {addr} {string} set OSD message on the specified device." + System.Environment.NewLine +
147 "[osd 0 Test Message] displays 'Test Message' on the TV" + System.Environment.NewLine +
148 System.Environment.NewLine +
149 "ver {addr} get the CEC version of the specified device." + System.Environment.NewLine +
150 "[ver 0] get the CEC version of the TV" + System.Environment.NewLine +
151 System.Environment.NewLine +
152 "ven {addr} get the vendor ID of the specified device." + System.Environment.NewLine +
153 "[ven 0] get the vendor ID of the TV" + System.Environment.NewLine +
154 System.Environment.NewLine +
155 "lang {addr} get the menu language of the specified device." + System.Environment.NewLine +
156 "[lang 0] get the menu language of the TV" + System.Environment.NewLine +
157 System.Environment.NewLine +
158 "pow {addr} get the power status of the specified device." + System.Environment.NewLine +
159 "[pow 0] get the power status of the TV" + System.Environment.NewLine +
160 System.Environment.NewLine +
161 "poll {addr} poll the specified device." + System.Environment.NewLine +
162 "[poll 0] sends a poll message to the TV" + System.Environment.NewLine +
163 System.Environment.NewLine +
164 "[mon] {1|0} enable or disable CEC bus monitoring." + System.Environment.NewLine +
165 "[log] {1 - 31} change the log level. see cectypes.h for values." + System.Environment.NewLine +
166 System.Environment.NewLine +
167 "[ping] send a ping command to the CEC adapter." + System.Environment.NewLine +
168 "[bl] to let the adapter enter the bootloader, to upgrade" + System.Environment.NewLine +
169 " the flash rom." + System.Environment.NewLine +
170 "[r] reconnect to the CEC adapter." + System.Environment.NewLine +
171 "[h] or [help] show this help." + System.Environment.NewLine +
172 "[q] or [quit] to quit the CEC test client and switch off all" + System.Environment.NewLine +
173 " connected CEC devices." + System.Environment.NewLine +
174 "================================================================================");
177 public void MainLoop()
179 Lib.PowerOnDevices(CecLogicalAddress.Tv);
182 Lib.SetActiveSource(CecDeviceType.PlaybackDevice);
185 bool bContinue = true;
189 Console.WriteLine("waiting for input");
191 command = Console.ReadLine();
192 if (command.Length == 0)
194 string[] splitCommand = command.Split(' ');
195 if (splitCommand[0] == "tx" || splitCommand[0] == "txn")
197 CecCommand bytes = new CecCommand();
198 for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
200 bytes.PushBack(byte.Parse(splitCommand[iPtr], System.Globalization.NumberStyles.HexNumber));
203 if (command == "txn")
204 bytes.TransmitTimeout = 0;
208 else if (splitCommand[0] == "on")
210 if (splitCommand.Length > 1)
211 Lib.PowerOnDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
213 Lib.PowerOnDevices(CecLogicalAddress.Broadcast);
215 else if (splitCommand[0] == "standby")
217 if (splitCommand.Length > 1)
218 Lib.StandbyDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
220 Lib.StandbyDevices(CecLogicalAddress.Broadcast);
222 else if (splitCommand[0] == "poll")
225 if (splitCommand.Length > 1)
226 bSent = Lib.PollDevice((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
228 bSent = Lib.PollDevice(CecLogicalAddress.Broadcast);
230 Console.WriteLine("POLL message sent");
232 Console.WriteLine("POLL message not sent");
234 else if (splitCommand[0] == "la")
236 if (splitCommand.Length > 1)
237 Lib.SetLogicalAddress((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
239 else if (splitCommand[0] == "pa")
241 if (splitCommand.Length > 1)
242 Lib.SetPhysicalAddress(short.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
244 else if (splitCommand[0] == "osd")
246 if (splitCommand.Length > 2)
248 StringBuilder osdString = new StringBuilder();
249 for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
251 osdString.Append(splitCommand[iPtr]);
252 if (iPtr != splitCommand.Length - 1)
253 osdString.Append(" ");
255 Lib.SetOSDString((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber), CecDisplayControl.DisplayForDefaultTime, osdString.ToString());
258 else if (splitCommand[0] == "ping")
262 else if (splitCommand[0] == "mon")
264 bool enable = splitCommand.Length > 1 ? splitCommand[1] == "1" : false;
265 Lib.SwitchMonitoring(enable);
267 else if (splitCommand[0] == "bl")
269 Lib.StartBootloader();
271 else if (splitCommand[0] == "lang")
273 if (splitCommand.Length > 1)
275 string language = Lib.GetDeviceMenuLanguage((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
276 Console.WriteLine("Menu language: " + language);
279 else if (splitCommand[0] == "ven")
281 if (splitCommand.Length > 1)
283 ulong vendor = Lib.GetDeviceVendorId((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
284 Console.WriteLine("Vendor ID: " + vendor);
287 else if (splitCommand[0] == "ver")
289 if (splitCommand.Length > 1)
291 CecVersion version = Lib.GetDeviceCecVersion((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
294 case CecVersion.V1_2:
295 Console.WriteLine("CEC version 1.2");
297 case CecVersion.V1_2A:
298 Console.WriteLine("CEC version 1.2a");
300 case CecVersion.V1_3:
301 Console.WriteLine("CEC version 1.3");
303 case CecVersion.V1_3A:
304 Console.WriteLine("CEC version 1.3a");
306 case CecVersion.V1_4:
307 Console.WriteLine("CEC version 1.4");
310 Console.WriteLine("unknown CEC version");
315 else if (splitCommand[0] == "pow")
317 if (splitCommand.Length > 1)
319 CecPowerStatus power = Lib.GetDevicePowerStatus((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
322 case CecPowerStatus.On:
323 Console.WriteLine("powered on");
325 case CecPowerStatus.InTransitionOnToStandby:
326 Console.WriteLine("on -> standby");
328 case CecPowerStatus.InTransitionStandbyToOn:
329 Console.WriteLine("standby -> on");
331 case CecPowerStatus.Standby:
332 Console.WriteLine("standby");
335 Console.WriteLine("unknown power status");
340 else if (splitCommand[0] == "r")
342 Console.WriteLine("closing the connection");
346 Console.WriteLine("opening a new connection");
350 Console.WriteLine("setting active source");
351 Lib.SetActiveSource(CecDeviceType.PlaybackDevice);
353 else if (splitCommand[0] == "h" || splitCommand[0] == "help")
355 else if (splitCommand[0] == "q" || splitCommand[0] == "quit")
357 else if (splitCommand[0] == "log" && splitCommand.Length > 1)
358 LogLevel = int.Parse(splitCommand[1]);
364 static void Main(string[] args)
366 CecSharpClient p = new CecSharpClient();
367 if (p.Connect(10000))
373 Console.WriteLine("Could not open a connection to the CEC adapter");
378 private int LogLevel;
379 private LibCecSharp Lib;