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