cec: fix power on of LG devices
[deb_libcec.git] / src / CecSharpTester / CecSharpClient.cs
1 /*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
6 *
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
8 *
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.
13 *
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.
18 *
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.
22 *
23 *
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
26 *
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/
31 */
32
33 using System;
34 using System.Collections.Generic;
35 using System.Linq;
36 using System.Text;
37
38 namespace CecSharpClient
39 {
40 class CecSharpClient
41 {
42 public CecSharpClient()
43 {
44 CecDeviceTypeList types = new CecDeviceTypeList();
45 types.Types[0] = CecDeviceType.PlaybackDevice;
46
47 Lib = new LibCecSharp("CEC Tester", types);
48 LogLevel = (int) CecLogLevel.All;
49
50 Console.WriteLine("CEC Parser created - libcec version " + Lib.GetLibVersionMajor() + "." + Lib.GetLibVersionMinor());
51 }
52
53 void FlushLog()
54 {
55 CecLogMessage message = Lib.GetNextLogMessage();
56 bool bGotMessage = !message.Empty;
57 while (bGotMessage)
58 {
59 if (((int)message.Level & LogLevel) == (int)message.Level)
60 {
61 string strLevel = "";
62 switch (message.Level)
63 {
64 case CecLogLevel.Error:
65 strLevel = "ERROR: ";
66 break;
67 case CecLogLevel.Warning:
68 strLevel = "WARNING: ";
69 break;
70 case CecLogLevel.Notice:
71 strLevel = "NOTICE: ";
72 break;
73 case CecLogLevel.Traffic:
74 strLevel = "TRAFFIC: ";
75 break;
76 case CecLogLevel.Debug:
77 strLevel = "DEBUG: ";
78 break;
79 default:
80 break;
81 }
82 string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message);
83 Console.WriteLine(strLog);
84 }
85
86 message = Lib.GetNextLogMessage();
87 bGotMessage = !message.Empty;
88 }
89 }
90
91 public bool Connect(int timeout)
92 {
93 CecAdapter[] adapters = Lib.FindAdapters(string.Empty);
94 if (adapters.Length > 0)
95 return Connect(adapters[0].ComPort, timeout);
96 else
97 {
98 Console.WriteLine("Did not find any CEC adapters");
99 return false;
100 }
101 }
102
103 public bool Connect(string port, int timeout)
104 {
105 return Lib.Open(port, timeout);
106 }
107
108 public void Close()
109 {
110 Lib.Close();
111 }
112
113 public void ListDevices()
114 {
115 int iAdapter = 0;
116 foreach (CecAdapter adapter in Lib.FindAdapters(string.Empty))
117 {
118 Console.WriteLine("Adapter: " + iAdapter++);
119 Console.WriteLine("Path: " + adapter.Path);
120 Console.WriteLine("Com port: " + adapter.ComPort);
121 }
122 }
123
124 void ShowConsoleHelp()
125 {
126 Console.WriteLine(
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 "================================================================================");
175 }
176
177 public void MainLoop()
178 {
179 Lib.PowerOnDevices(CecLogicalAddress.Tv);
180 FlushLog();
181
182 Lib.SetActiveSource(CecDeviceType.PlaybackDevice);
183 FlushLog();
184
185 bool bContinue = true;
186 string command;
187 while (bContinue)
188 {
189 Console.WriteLine("waiting for input");
190
191 command = Console.ReadLine();
192 if (command.Length == 0)
193 continue;
194 string[] splitCommand = command.Split(' ');
195 if (splitCommand[0] == "tx" || splitCommand[0] == "txn")
196 {
197 CecCommand bytes = new CecCommand();
198 for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
199 {
200 bytes.PushBack(byte.Parse(splitCommand[iPtr], System.Globalization.NumberStyles.HexNumber));
201 }
202
203 if (command == "txn")
204 bytes.TransmitTimeout = 0;
205
206 Lib.Transmit(bytes);
207 }
208 else if (splitCommand[0] == "on")
209 {
210 if (splitCommand.Length > 1)
211 Lib.PowerOnDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
212 else
213 Lib.PowerOnDevices(CecLogicalAddress.Broadcast);
214 }
215 else if (splitCommand[0] == "standby")
216 {
217 if (splitCommand.Length > 1)
218 Lib.StandbyDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
219 else
220 Lib.StandbyDevices(CecLogicalAddress.Broadcast);
221 }
222 else if (splitCommand[0] == "poll")
223 {
224 bool bSent = false;
225 if (splitCommand.Length > 1)
226 bSent = Lib.PollDevice((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
227 else
228 bSent = Lib.PollDevice(CecLogicalAddress.Broadcast);
229 if (bSent)
230 Console.WriteLine("POLL message sent");
231 else
232 Console.WriteLine("POLL message not sent");
233 }
234 else if (splitCommand[0] == "la")
235 {
236 if (splitCommand.Length > 1)
237 Lib.SetLogicalAddress((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
238 }
239 else if (splitCommand[0] == "pa")
240 {
241 if (splitCommand.Length > 1)
242 Lib.SetPhysicalAddress(short.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
243 }
244 else if (splitCommand[0] == "osd")
245 {
246 if (splitCommand.Length > 2)
247 {
248 StringBuilder osdString = new StringBuilder();
249 for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
250 {
251 osdString.Append(splitCommand[iPtr]);
252 if (iPtr != splitCommand.Length - 1)
253 osdString.Append(" ");
254 }
255 Lib.SetOSDString((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber), CecDisplayControl.DisplayForDefaultTime, osdString.ToString());
256 }
257 }
258 else if (splitCommand[0] == "ping")
259 {
260 Lib.PingAdapter();
261 }
262 else if (splitCommand[0] == "mon")
263 {
264 bool enable = splitCommand.Length > 1 ? splitCommand[1] == "1" : false;
265 Lib.SwitchMonitoring(enable);
266 }
267 else if (splitCommand[0] == "bl")
268 {
269 Lib.StartBootloader();
270 }
271 else if (splitCommand[0] == "lang")
272 {
273 if (splitCommand.Length > 1)
274 {
275 string language = Lib.GetDeviceMenuLanguage((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
276 Console.WriteLine("Menu language: " + language);
277 }
278 }
279 else if (splitCommand[0] == "ven")
280 {
281 if (splitCommand.Length > 1)
282 {
283 ulong vendor = Lib.GetDeviceVendorId((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
284 Console.WriteLine("Vendor ID: " + vendor);
285 }
286 }
287 else if (splitCommand[0] == "ver")
288 {
289 if (splitCommand.Length > 1)
290 {
291 CecVersion version = Lib.GetDeviceCecVersion((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
292 switch (version)
293 {
294 case CecVersion.V1_2:
295 Console.WriteLine("CEC version 1.2");
296 break;
297 case CecVersion.V1_2A:
298 Console.WriteLine("CEC version 1.2a");
299 break;
300 case CecVersion.V1_3:
301 Console.WriteLine("CEC version 1.3");
302 break;
303 case CecVersion.V1_3A:
304 Console.WriteLine("CEC version 1.3a");
305 break;
306 case CecVersion.V1_4:
307 Console.WriteLine("CEC version 1.4");
308 break;
309 default:
310 Console.WriteLine("unknown CEC version");
311 break;
312 }
313 }
314 }
315 else if (splitCommand[0] == "pow")
316 {
317 if (splitCommand.Length > 1)
318 {
319 CecPowerStatus power = Lib.GetDevicePowerStatus((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
320 switch (power)
321 {
322 case CecPowerStatus.On:
323 Console.WriteLine("powered on");
324 break;
325 case CecPowerStatus.InTransitionOnToStandby:
326 Console.WriteLine("on -> standby");
327 break;
328 case CecPowerStatus.InTransitionStandbyToOn:
329 Console.WriteLine("standby -> on");
330 break;
331 case CecPowerStatus.Standby:
332 Console.WriteLine("standby");
333 break;
334 default:
335 Console.WriteLine("unknown power status");
336 break;
337 }
338 }
339 }
340 else if (splitCommand[0] == "r")
341 {
342 Console.WriteLine("closing the connection");
343 Lib.Close();
344 FlushLog();
345
346 Console.WriteLine("opening a new connection");
347 Connect(10000);
348 FlushLog();
349
350 Console.WriteLine("setting active source");
351 Lib.SetActiveSource(CecDeviceType.PlaybackDevice);
352 }
353 else if (splitCommand[0] == "h" || splitCommand[0] == "help")
354 ShowConsoleHelp();
355 else if (splitCommand[0] == "q" || splitCommand[0] == "quit")
356 bContinue = false;
357 else if (splitCommand[0] == "log" && splitCommand.Length > 1)
358 LogLevel = int.Parse(splitCommand[1]);
359
360 FlushLog();
361 }
362 }
363
364 static void Main(string[] args)
365 {
366 CecSharpClient p = new CecSharpClient();
367 if (p.Connect(10000))
368 {
369 p.MainLoop();
370 }
371 else
372 {
373 Console.WriteLine("Could not open a connection to the CEC adapter");
374 }
375 p.FlushLog();
376 }
377
378 private int LogLevel;
379 private LibCecSharp Lib;
380 }
381 }