cec: fixed typo in GetStatus()
[deb_libcec.git] / src / CecSharpTester / CecSharpClient.cs
CommitLineData
61f3c2ad
LOK
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
33using System;
34using System.Collections.Generic;
61f3c2ad 35using System.Text;
4ef3b314 36using CecSharp;
61f3c2ad
LOK
37
38namespace CecSharpClient
39{
dd4ace4f 40 class CecSharpClient : CecCallbackMethods
61f3c2ad
LOK
41 {
42 public CecSharpClient()
43 {
7f7414d5
LOK
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;
61f3c2ad 50
7f7414d5 51 Lib = new LibCecSharp(Config);
61f3c2ad 52
3efda01a 53 Console.WriteLine("CEC Parser created - libcec version " + Lib.ToString(Config.ServerVersion));
61f3c2ad
LOK
54 }
55
dd4ace4f
LOK
56 public override int ReceiveCommand(CecCommand command)
57 {
58 return 1;
59 }
60
61 public override int ReceiveKeypress(CecKeypress key)
62 {
63 return 1;
64 }
65
66 public override int ReceiveLogMessage(CecLogMessage message)
67 {
68 if (((int)message.Level & LogLevel) == (int)message.Level)
69 {
70 string strLevel = "";
71 switch (message.Level)
72 {
73 case CecLogLevel.Error:
74 strLevel = "ERROR: ";
75 break;
76 case CecLogLevel.Warning:
77 strLevel = "WARNING: ";
78 break;
79 case CecLogLevel.Notice:
80 strLevel = "NOTICE: ";
81 break;
82 case CecLogLevel.Traffic:
83 strLevel = "TRAFFIC: ";
84 break;
85 case CecLogLevel.Debug:
86 strLevel = "DEBUG: ";
87 break;
88 default:
89 break;
90 }
91 string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message);
92 Console.WriteLine(strLog);
93 }
94 return 1;
95 }
96
61f3c2ad
LOK
97 void FlushLog()
98 {
99 CecLogMessage message = Lib.GetNextLogMessage();
100 bool bGotMessage = !message.Empty;
101 while (bGotMessage)
102 {
103 if (((int)message.Level & LogLevel) == (int)message.Level)
104 {
105 string strLevel = "";
106 switch (message.Level)
107 {
108 case CecLogLevel.Error:
109 strLevel = "ERROR: ";
110 break;
111 case CecLogLevel.Warning:
112 strLevel = "WARNING: ";
113 break;
114 case CecLogLevel.Notice:
115 strLevel = "NOTICE: ";
116 break;
117 case CecLogLevel.Traffic:
118 strLevel = "TRAFFIC: ";
119 break;
120 case CecLogLevel.Debug:
121 strLevel = "DEBUG: ";
122 break;
123 default:
124 break;
125 }
126 string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message);
127 Console.WriteLine(strLog);
128 }
129
130 message = Lib.GetNextLogMessage();
131 bGotMessage = !message.Empty;
132 }
133 }
134
135 public bool Connect(int timeout)
136 {
137 CecAdapter[] adapters = Lib.FindAdapters(string.Empty);
138 if (adapters.Length > 0)
139 return Connect(adapters[0].ComPort, timeout);
140 else
141 {
142 Console.WriteLine("Did not find any CEC adapters");
143 return false;
144 }
145 }
146
147 public bool Connect(string port, int timeout)
148 {
149 return Lib.Open(port, timeout);
150 }
151
152 public void Close()
153 {
154 Lib.Close();
155 }
156
157 public void ListDevices()
158 {
159 int iAdapter = 0;
160 foreach (CecAdapter adapter in Lib.FindAdapters(string.Empty))
161 {
162 Console.WriteLine("Adapter: " + iAdapter++);
163 Console.WriteLine("Path: " + adapter.Path);
164 Console.WriteLine("Com port: " + adapter.ComPort);
165 }
166 }
167
168 void ShowConsoleHelp()
169 {
170 Console.WriteLine(
171 "================================================================================" + System.Environment.NewLine +
172 "Available commands:" + System.Environment.NewLine +
173 System.Environment.NewLine +
8d4c47d9
LOK
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 +
61f3c2ad
LOK
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 +
61f3c2ad
LOK
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 "================================================================================");
197 }
198
199 public void MainLoop()
200 {
61f3c2ad
LOK
201 bool bContinue = true;
202 string command;
203 while (bContinue)
204 {
205 Console.WriteLine("waiting for input");
206
207 command = Console.ReadLine();
208 if (command.Length == 0)
209 continue;
210 string[] splitCommand = command.Split(' ');
211 if (splitCommand[0] == "tx" || splitCommand[0] == "txn")
212 {
213 CecCommand bytes = new CecCommand();
214 for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
215 {
216 bytes.PushBack(byte.Parse(splitCommand[iPtr], System.Globalization.NumberStyles.HexNumber));
217 }
218
219 if (command == "txn")
220 bytes.TransmitTimeout = 0;
221
222 Lib.Transmit(bytes);
223 }
224 else if (splitCommand[0] == "on")
225 {
226 if (splitCommand.Length > 1)
227 Lib.PowerOnDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
228 else
229 Lib.PowerOnDevices(CecLogicalAddress.Broadcast);
230 }
231 else if (splitCommand[0] == "standby")
232 {
233 if (splitCommand.Length > 1)
234 Lib.StandbyDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
235 else
236 Lib.StandbyDevices(CecLogicalAddress.Broadcast);
237 }
238 else if (splitCommand[0] == "poll")
239 {
240 bool bSent = false;
241 if (splitCommand.Length > 1)
242 bSent = Lib.PollDevice((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
243 else
244 bSent = Lib.PollDevice(CecLogicalAddress.Broadcast);
245 if (bSent)
246 Console.WriteLine("POLL message sent");
247 else
248 Console.WriteLine("POLL message not sent");
249 }
250 else if (splitCommand[0] == "la")
251 {
252 if (splitCommand.Length > 1)
253 Lib.SetLogicalAddress((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
254 }
255 else if (splitCommand[0] == "pa")
256 {
257 if (splitCommand.Length > 1)
006b76b9 258 Lib.SetPhysicalAddress(ushort.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
61f3c2ad
LOK
259 }
260 else if (splitCommand[0] == "osd")
261 {
262 if (splitCommand.Length > 2)
263 {
264 StringBuilder osdString = new StringBuilder();
265 for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
266 {
267 osdString.Append(splitCommand[iPtr]);
268 if (iPtr != splitCommand.Length - 1)
269 osdString.Append(" ");
270 }
271 Lib.SetOSDString((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber), CecDisplayControl.DisplayForDefaultTime, osdString.ToString());
272 }
273 }
274 else if (splitCommand[0] == "ping")
275 {
276 Lib.PingAdapter();
277 }
278 else if (splitCommand[0] == "mon")
279 {
280 bool enable = splitCommand.Length > 1 ? splitCommand[1] == "1" : false;
281 Lib.SwitchMonitoring(enable);
282 }
283 else if (splitCommand[0] == "bl")
284 {
285 Lib.StartBootloader();
286 }
287 else if (splitCommand[0] == "lang")
288 {
289 if (splitCommand.Length > 1)
290 {
291 string language = Lib.GetDeviceMenuLanguage((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
292 Console.WriteLine("Menu language: " + language);
293 }
294 }
295 else if (splitCommand[0] == "ven")
296 {
297 if (splitCommand.Length > 1)
298 {
8d4c47d9
LOK
299 CecVendorId vendor = Lib.GetDeviceVendorId((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
300 Console.WriteLine("Vendor ID: " + Lib.ToString(vendor));
61f3c2ad
LOK
301 }
302 }
303 else if (splitCommand[0] == "ver")
304 {
305 if (splitCommand.Length > 1)
306 {
307 CecVersion version = Lib.GetDeviceCecVersion((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
8d4c47d9 308 Console.WriteLine("CEC version: " + Lib.ToString(version));
61f3c2ad
LOK
309 }
310 }
311 else if (splitCommand[0] == "pow")
312 {
313 if (splitCommand.Length > 1)
314 {
315 CecPowerStatus power = Lib.GetDevicePowerStatus((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
8d4c47d9 316 Console.WriteLine("power status: " + Lib.ToString(power));
61f3c2ad
LOK
317 }
318 }
319 else if (splitCommand[0] == "r")
320 {
321 Console.WriteLine("closing the connection");
322 Lib.Close();
61f3c2ad
LOK
323
324 Console.WriteLine("opening a new connection");
325 Connect(10000);
61f3c2ad
LOK
326
327 Console.WriteLine("setting active source");
328 Lib.SetActiveSource(CecDeviceType.PlaybackDevice);
329 }
8d4c47d9
LOK
330 else if (splitCommand[0] == "scan")
331 {
332 Console.WriteLine("CEC bus information");
333 Console.WriteLine("===================");
334 CecLogicalAddresses addresses = Lib.GetActiveDevices();
4d760b54 335 for (int iPtr = 0; iPtr < addresses.Addresses.Length; iPtr++)
8d4c47d9
LOK
336 {
337 CecLogicalAddress address = (CecLogicalAddress)iPtr;
338 if (!addresses.IsSet(address))
339 continue;
340
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);
349
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);
360
361 Console.WriteLine(output.ToString());
362 }
363 }
61f3c2ad
LOK
364 else if (splitCommand[0] == "h" || splitCommand[0] == "help")
365 ShowConsoleHelp();
366 else if (splitCommand[0] == "q" || splitCommand[0] == "quit")
367 bContinue = false;
368 else if (splitCommand[0] == "log" && splitCommand.Length > 1)
8d4c47d9 369 LogLevel = int.Parse(splitCommand[1]);
61f3c2ad
LOK
370 }
371 }
372
373 static void Main(string[] args)
374 {
375 CecSharpClient p = new CecSharpClient();
376 if (p.Connect(10000))
377 {
378 p.MainLoop();
379 }
380 else
381 {
382 Console.WriteLine("Could not open a connection to the CEC adapter");
383 }
61f3c2ad
LOK
384 }
385
7f7414d5
LOK
386 private int LogLevel;
387 private LibCecSharp Lib;
388 private LibCECConfiguration Config;
61f3c2ad
LOK
389 }
390}