LibCecSharp: more cosmetics
[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.Text;
35 using CecSharp;
36
37 namespace CecSharpClient
38 {
39 class CecSharpClient : CecCallbackMethods
40 {
41 public CecSharpClient()
42 {
43 Config = new LibCECConfiguration();
44 Config.DeviceTypes.Types[0] = CecDeviceType.RecordingDevice;
45 Config.DeviceName = "CEC Tester";
46 Config.ClientVersion = CecClientVersion.Version1_5_0;
47 Config.SetCallbacks(this);
48 LogLevel = (int)CecLogLevel.All;
49
50 Lib = new LibCecSharp(Config);
51
52 Console.WriteLine("CEC Parser created - libcec version " + Lib.ToString(Config.ServerVersion));
53 }
54
55 public override int ReceiveCommand(CecCommand command)
56 {
57 return 1;
58 }
59
60 public override int ReceiveKeypress(CecKeypress key)
61 {
62 return 1;
63 }
64
65 public override int ReceiveLogMessage(CecLogMessage message)
66 {
67 if (((int)message.Level & LogLevel) == (int)message.Level)
68 {
69 string strLevel = "";
70 switch (message.Level)
71 {
72 case CecLogLevel.Error:
73 strLevel = "ERROR: ";
74 break;
75 case CecLogLevel.Warning:
76 strLevel = "WARNING: ";
77 break;
78 case CecLogLevel.Notice:
79 strLevel = "NOTICE: ";
80 break;
81 case CecLogLevel.Traffic:
82 strLevel = "TRAFFIC: ";
83 break;
84 case CecLogLevel.Debug:
85 strLevel = "DEBUG: ";
86 break;
87 default:
88 break;
89 }
90 string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message);
91 Console.WriteLine(strLog);
92 }
93 return 1;
94 }
95
96 void FlushLog()
97 {
98 CecLogMessage message = Lib.GetNextLogMessage();
99 bool bGotMessage = !message.Empty;
100 while (bGotMessage)
101 {
102 if (((int)message.Level & LogLevel) == (int)message.Level)
103 {
104 string strLevel = "";
105 switch (message.Level)
106 {
107 case CecLogLevel.Error:
108 strLevel = "ERROR: ";
109 break;
110 case CecLogLevel.Warning:
111 strLevel = "WARNING: ";
112 break;
113 case CecLogLevel.Notice:
114 strLevel = "NOTICE: ";
115 break;
116 case CecLogLevel.Traffic:
117 strLevel = "TRAFFIC: ";
118 break;
119 case CecLogLevel.Debug:
120 strLevel = "DEBUG: ";
121 break;
122 default:
123 break;
124 }
125 string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message);
126 Console.WriteLine(strLog);
127 }
128
129 message = Lib.GetNextLogMessage();
130 bGotMessage = !message.Empty;
131 }
132 }
133
134 public bool Connect(int timeout)
135 {
136 CecAdapter[] adapters = Lib.FindAdapters(string.Empty);
137 if (adapters.Length > 0)
138 return Connect(adapters[0].ComPort, timeout);
139 else
140 {
141 Console.WriteLine("Did not find any CEC adapters");
142 return false;
143 }
144 }
145
146 public bool Connect(string port, int timeout)
147 {
148 return Lib.Open(port, timeout);
149 }
150
151 public void Close()
152 {
153 Lib.Close();
154 }
155
156 public void ListDevices()
157 {
158 int iAdapter = 0;
159 foreach (CecAdapter adapter in Lib.FindAdapters(string.Empty))
160 {
161 Console.WriteLine("Adapter: " + iAdapter++);
162 Console.WriteLine("Path: " + adapter.Path);
163 Console.WriteLine("Com port: " + adapter.ComPort);
164 }
165 }
166
167 void ShowConsoleHelp()
168 {
169 Console.WriteLine(
170 "================================================================================" + Environment.NewLine +
171 "Available commands:" + Environment.NewLine +
172 Environment.NewLine +
173 "[tx] {bytes} transfer bytes over the CEC line." + Environment.NewLine +
174 "[txn] {bytes} transfer bytes but don't wait for transmission ACK." + Environment.NewLine +
175 "[on] {address} power on the device with the given logical address." + Environment.NewLine +
176 "[standby] {address} put the device with the given address in standby mode." + Environment.NewLine +
177 "[la] {logical_address} change the logical address of the CEC adapter." + Environment.NewLine +
178 "[pa] {physical_address} change the physical address of the CEC adapter." + Environment.NewLine +
179 "[osd] {addr} {string} set OSD message on the specified device." + Environment.NewLine +
180 "[ver] {addr} get the CEC version of the specified device." + Environment.NewLine +
181 "[ven] {addr} get the vendor ID of the specified device." + Environment.NewLine +
182 "[lang] {addr} get the menu language of the specified device." + Environment.NewLine +
183 "[pow] {addr} get the power status of the specified device." + Environment.NewLine +
184 "[poll] {addr} poll the specified device." + Environment.NewLine +
185 "[scan] scan the CEC bus and display device info" + Environment.NewLine +
186 "[mon] {1|0} enable or disable CEC bus monitoring." + Environment.NewLine +
187 "[log] {1 - 31} change the log level. see cectypes.h for values." + Environment.NewLine +
188 "[ping] send a ping command to the CEC adapter." + Environment.NewLine +
189 "[bl] to let the adapter enter the bootloader, to upgrade" + Environment.NewLine +
190 " the flash rom." + Environment.NewLine +
191 "[r] reconnect to the CEC adapter." + Environment.NewLine +
192 "[h] or [help] show this help." + Environment.NewLine +
193 "[q] or [quit] to quit the CEC test client and switch off all" + Environment.NewLine +
194 " connected CEC devices." + Environment.NewLine +
195 "================================================================================");
196 }
197
198 public void MainLoop()
199 {
200 bool bContinue = true;
201 string command;
202 while (bContinue)
203 {
204 Console.WriteLine("waiting for input");
205
206 command = Console.ReadLine();
207 if (command != null && command.Length == 0)
208 continue;
209 string[] splitCommand = command.Split(' ');
210 if (splitCommand[0] == "tx" || splitCommand[0] == "txn")
211 {
212 CecCommand bytes = new CecCommand();
213 for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
214 {
215 bytes.PushBack(byte.Parse(splitCommand[iPtr], System.Globalization.NumberStyles.HexNumber));
216 }
217
218 if (command == "txn")
219 bytes.TransmitTimeout = 0;
220
221 Lib.Transmit(bytes);
222 }
223 else if (splitCommand[0] == "on")
224 {
225 if (splitCommand.Length > 1)
226 Lib.PowerOnDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
227 else
228 Lib.PowerOnDevices(CecLogicalAddress.Broadcast);
229 }
230 else if (splitCommand[0] == "standby")
231 {
232 if (splitCommand.Length > 1)
233 Lib.StandbyDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
234 else
235 Lib.StandbyDevices(CecLogicalAddress.Broadcast);
236 }
237 else if (splitCommand[0] == "poll")
238 {
239 bool bSent = false;
240 if (splitCommand.Length > 1)
241 bSent = Lib.PollDevice((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
242 else
243 bSent = Lib.PollDevice(CecLogicalAddress.Broadcast);
244 if (bSent)
245 Console.WriteLine("POLL message sent");
246 else
247 Console.WriteLine("POLL message not sent");
248 }
249 else if (splitCommand[0] == "la")
250 {
251 if (splitCommand.Length > 1)
252 Lib.SetLogicalAddress((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
253 }
254 else if (splitCommand[0] == "pa")
255 {
256 if (splitCommand.Length > 1)
257 Lib.SetPhysicalAddress(ushort.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
258 }
259 else if (splitCommand[0] == "osd")
260 {
261 if (splitCommand.Length > 2)
262 {
263 StringBuilder osdString = new StringBuilder();
264 for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
265 {
266 osdString.Append(splitCommand[iPtr]);
267 if (iPtr != splitCommand.Length - 1)
268 osdString.Append(" ");
269 }
270 Lib.SetOSDString((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber), CecDisplayControl.DisplayForDefaultTime, osdString.ToString());
271 }
272 }
273 else if (splitCommand[0] == "ping")
274 {
275 Lib.PingAdapter();
276 }
277 else if (splitCommand[0] == "mon")
278 {
279 bool enable = splitCommand.Length > 1 ? splitCommand[1] == "1" : false;
280 Lib.SwitchMonitoring(enable);
281 }
282 else if (splitCommand[0] == "bl")
283 {
284 Lib.StartBootloader();
285 }
286 else if (splitCommand[0] == "lang")
287 {
288 if (splitCommand.Length > 1)
289 {
290 string language = Lib.GetDeviceMenuLanguage((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
291 Console.WriteLine("Menu language: " + language);
292 }
293 }
294 else if (splitCommand[0] == "ven")
295 {
296 if (splitCommand.Length > 1)
297 {
298 CecVendorId vendor = Lib.GetDeviceVendorId((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
299 Console.WriteLine("Vendor ID: " + Lib.ToString(vendor));
300 }
301 }
302 else if (splitCommand[0] == "ver")
303 {
304 if (splitCommand.Length > 1)
305 {
306 CecVersion version = Lib.GetDeviceCecVersion((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
307 Console.WriteLine("CEC version: " + Lib.ToString(version));
308 }
309 }
310 else if (splitCommand[0] == "pow")
311 {
312 if (splitCommand.Length > 1)
313 {
314 CecPowerStatus power = Lib.GetDevicePowerStatus((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
315 Console.WriteLine("power status: " + Lib.ToString(power));
316 }
317 }
318 else if (splitCommand[0] == "r")
319 {
320 Console.WriteLine("closing the connection");
321 Lib.Close();
322
323 Console.WriteLine("opening a new connection");
324 Connect(10000);
325
326 Console.WriteLine("setting active source");
327 Lib.SetActiveSource(CecDeviceType.PlaybackDevice);
328 }
329 else if (splitCommand[0] == "scan")
330 {
331 Console.WriteLine("CEC bus information");
332 Console.WriteLine("===================");
333 CecLogicalAddresses addresses = Lib.GetActiveDevices();
334 for (int iPtr = 0; iPtr < addresses.Addresses.Length; iPtr++)
335 {
336 CecLogicalAddress address = (CecLogicalAddress)iPtr;
337 if (!addresses.IsSet(address))
338 continue;
339
340 CecVendorId iVendorId = Lib.GetDeviceVendorId(address);
341 bool bActive = Lib.IsActiveDevice(address);
342 ushort iPhysicalAddress = Lib.GetDevicePhysicalAddress(address);
343 string strAddr = string.Format("{0,4:X}", iPhysicalAddress);
344 CecVersion iCecVersion = Lib.GetDeviceCecVersion(address);
345 CecPowerStatus power = Lib.GetDevicePowerStatus(address);
346 string osdName = Lib.GetDeviceOSDName(address);
347 string lang = Lib.GetDeviceMenuLanguage(address);
348
349 StringBuilder output = new StringBuilder();
350 output.AppendLine("device #" + iPtr + ": " + Lib.ToString(address));
351 output.AppendLine("address: " + strAddr);
352 output.AppendLine("active source: " + (bActive ? "yes" : "no"));
353 output.AppendLine("vendor: " + Lib.ToString(iVendorId));
354 output.AppendLine("osd string: " + osdName);
355 output.AppendLine("CEC version: " + Lib.ToString(iCecVersion));
356 output.AppendLine("power status: " + Lib.ToString(power));
357 if (!string.IsNullOrEmpty(lang))
358 output.AppendLine("language: " + lang);
359
360 Console.WriteLine(output.ToString());
361 }
362 }
363 else if (splitCommand[0] == "h" || splitCommand[0] == "help")
364 ShowConsoleHelp();
365 else if (splitCommand[0] == "q" || splitCommand[0] == "quit")
366 bContinue = false;
367 else if (splitCommand[0] == "log" && splitCommand.Length > 1)
368 LogLevel = int.Parse(splitCommand[1]);
369 }
370 }
371
372 static void Main(string[] args)
373 {
374 CecSharpClient p = new CecSharpClient();
375 if (p.Connect(10000))
376 {
377 p.MainLoop();
378 }
379 else
380 {
381 Console.WriteLine("Could not open a connection to the CEC adapter");
382 }
383 }
384
385 private int LogLevel;
386 private LibCecSharp Lib;
387 private LibCECConfiguration Config;
388 }
389 }