442c282afacedfd0843193d97499780e2a843400
[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 public bool Connect(int timeout)
97 {
98 CecAdapter[] adapters = Lib.FindAdapters(string.Empty);
99 if (adapters.Length > 0)
100 return Connect(adapters[0].ComPort, timeout);
101 else
102 {
103 Console.WriteLine("Did not find any CEC adapters");
104 return false;
105 }
106 }
107
108 public bool Connect(string port, int timeout)
109 {
110 return Lib.Open(port, timeout);
111 }
112
113 public void Close()
114 {
115 Lib.Close();
116 }
117
118 public void ListDevices()
119 {
120 int iAdapter = 0;
121 foreach (CecAdapter adapter in Lib.FindAdapters(string.Empty))
122 {
123 Console.WriteLine("Adapter: " + iAdapter++);
124 Console.WriteLine("Path: " + adapter.Path);
125 Console.WriteLine("Com port: " + adapter.ComPort);
126 }
127 }
128
129 void ShowConsoleHelp()
130 {
131 Console.WriteLine(
132 "================================================================================" + Environment.NewLine +
133 "Available commands:" + Environment.NewLine +
134 Environment.NewLine +
135 "[tx] {bytes} transfer bytes over the CEC line." + Environment.NewLine +
136 "[txn] {bytes} transfer bytes but don't wait for transmission ACK." + Environment.NewLine +
137 "[on] {address} power on the device with the given logical address." + Environment.NewLine +
138 "[standby] {address} put the device with the given address in standby mode." + Environment.NewLine +
139 "[la] {logical_address} change the logical address of the CEC adapter." + Environment.NewLine +
140 "[pa] {physical_address} change the physical address of the CEC adapter." + Environment.NewLine +
141 "[osd] {addr} {string} set OSD message on the specified device." + Environment.NewLine +
142 "[ver] {addr} get the CEC version of the specified device." + Environment.NewLine +
143 "[ven] {addr} get the vendor ID of the specified device." + Environment.NewLine +
144 "[lang] {addr} get the menu language of the specified device." + Environment.NewLine +
145 "[pow] {addr} get the power status of the specified device." + Environment.NewLine +
146 "[poll] {addr} poll the specified device." + Environment.NewLine +
147 "[scan] scan the CEC bus and display device info" + Environment.NewLine +
148 "[mon] {1|0} enable or disable CEC bus monitoring." + Environment.NewLine +
149 "[log] {1 - 31} change the log level. see cectypes.h for values." + Environment.NewLine +
150 "[ping] send a ping command to the CEC adapter." + Environment.NewLine +
151 "[bl] to let the adapter enter the bootloader, to upgrade" + Environment.NewLine +
152 " the flash rom." + Environment.NewLine +
153 "[r] reconnect to the CEC adapter." + Environment.NewLine +
154 "[h] or [help] show this help." + Environment.NewLine +
155 "[q] or [quit] to quit the CEC test client and switch off all" + Environment.NewLine +
156 " connected CEC devices." + Environment.NewLine +
157 "================================================================================");
158 }
159
160 public void MainLoop()
161 {
162 bool bContinue = true;
163 string command;
164 while (bContinue)
165 {
166 Console.WriteLine("waiting for input");
167
168 command = Console.ReadLine();
169 if (command != null && command.Length == 0)
170 continue;
171 string[] splitCommand = command.Split(' ');
172 if (splitCommand[0] == "tx" || splitCommand[0] == "txn")
173 {
174 CecCommand bytes = new CecCommand();
175 for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
176 {
177 bytes.PushBack(byte.Parse(splitCommand[iPtr], System.Globalization.NumberStyles.HexNumber));
178 }
179
180 if (command == "txn")
181 bytes.TransmitTimeout = 0;
182
183 Lib.Transmit(bytes);
184 }
185 else if (splitCommand[0] == "on")
186 {
187 if (splitCommand.Length > 1)
188 Lib.PowerOnDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
189 else
190 Lib.PowerOnDevices(CecLogicalAddress.Broadcast);
191 }
192 else if (splitCommand[0] == "standby")
193 {
194 if (splitCommand.Length > 1)
195 Lib.StandbyDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
196 else
197 Lib.StandbyDevices(CecLogicalAddress.Broadcast);
198 }
199 else if (splitCommand[0] == "poll")
200 {
201 bool bSent = false;
202 if (splitCommand.Length > 1)
203 bSent = Lib.PollDevice((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
204 else
205 bSent = Lib.PollDevice(CecLogicalAddress.Broadcast);
206 if (bSent)
207 Console.WriteLine("POLL message sent");
208 else
209 Console.WriteLine("POLL message not sent");
210 }
211 else if (splitCommand[0] == "la")
212 {
213 if (splitCommand.Length > 1)
214 Lib.SetLogicalAddress((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
215 }
216 else if (splitCommand[0] == "pa")
217 {
218 if (splitCommand.Length > 1)
219 Lib.SetPhysicalAddress(ushort.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
220 }
221 else if (splitCommand[0] == "osd")
222 {
223 if (splitCommand.Length > 2)
224 {
225 StringBuilder osdString = new StringBuilder();
226 for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
227 {
228 osdString.Append(splitCommand[iPtr]);
229 if (iPtr != splitCommand.Length - 1)
230 osdString.Append(" ");
231 }
232 Lib.SetOSDString((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber), CecDisplayControl.DisplayForDefaultTime, osdString.ToString());
233 }
234 }
235 else if (splitCommand[0] == "ping")
236 {
237 Lib.PingAdapter();
238 }
239 else if (splitCommand[0] == "mon")
240 {
241 bool enable = splitCommand.Length > 1 ? splitCommand[1] == "1" : false;
242 Lib.SwitchMonitoring(enable);
243 }
244 else if (splitCommand[0] == "bl")
245 {
246 Lib.StartBootloader();
247 }
248 else if (splitCommand[0] == "lang")
249 {
250 if (splitCommand.Length > 1)
251 {
252 string language = Lib.GetDeviceMenuLanguage((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
253 Console.WriteLine("Menu language: " + language);
254 }
255 }
256 else if (splitCommand[0] == "ven")
257 {
258 if (splitCommand.Length > 1)
259 {
260 CecVendorId vendor = Lib.GetDeviceVendorId((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
261 Console.WriteLine("Vendor ID: " + Lib.ToString(vendor));
262 }
263 }
264 else if (splitCommand[0] == "ver")
265 {
266 if (splitCommand.Length > 1)
267 {
268 CecVersion version = Lib.GetDeviceCecVersion((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
269 Console.WriteLine("CEC version: " + Lib.ToString(version));
270 }
271 }
272 else if (splitCommand[0] == "pow")
273 {
274 if (splitCommand.Length > 1)
275 {
276 CecPowerStatus power = Lib.GetDevicePowerStatus((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
277 Console.WriteLine("power status: " + Lib.ToString(power));
278 }
279 }
280 else if (splitCommand[0] == "r")
281 {
282 Console.WriteLine("closing the connection");
283 Lib.Close();
284
285 Console.WriteLine("opening a new connection");
286 Connect(10000);
287
288 Console.WriteLine("setting active source");
289 Lib.SetActiveSource(CecDeviceType.PlaybackDevice);
290 }
291 else if (splitCommand[0] == "scan")
292 {
293 Console.WriteLine("CEC bus information");
294 Console.WriteLine("===================");
295 CecLogicalAddresses addresses = Lib.GetActiveDevices();
296 for (int iPtr = 0; iPtr < addresses.Addresses.Length; iPtr++)
297 {
298 CecLogicalAddress address = (CecLogicalAddress)iPtr;
299 if (!addresses.IsSet(address))
300 continue;
301
302 CecVendorId iVendorId = Lib.GetDeviceVendorId(address);
303 bool bActive = Lib.IsActiveDevice(address);
304 ushort iPhysicalAddress = Lib.GetDevicePhysicalAddress(address);
305 string strAddr = string.Format("{0,4:X}", iPhysicalAddress);
306 CecVersion iCecVersion = Lib.GetDeviceCecVersion(address);
307 CecPowerStatus power = Lib.GetDevicePowerStatus(address);
308 string osdName = Lib.GetDeviceOSDName(address);
309 string lang = Lib.GetDeviceMenuLanguage(address);
310
311 StringBuilder output = new StringBuilder();
312 output.AppendLine("device #" + iPtr + ": " + Lib.ToString(address));
313 output.AppendLine("address: " + strAddr);
314 output.AppendLine("active source: " + (bActive ? "yes" : "no"));
315 output.AppendLine("vendor: " + Lib.ToString(iVendorId));
316 output.AppendLine("osd string: " + osdName);
317 output.AppendLine("CEC version: " + Lib.ToString(iCecVersion));
318 output.AppendLine("power status: " + Lib.ToString(power));
319 if (!string.IsNullOrEmpty(lang))
320 output.AppendLine("language: " + lang);
321
322 Console.WriteLine(output.ToString());
323 }
324 }
325 else if (splitCommand[0] == "h" || splitCommand[0] == "help")
326 ShowConsoleHelp();
327 else if (splitCommand[0] == "q" || splitCommand[0] == "quit")
328 bContinue = false;
329 else if (splitCommand[0] == "log" && splitCommand.Length > 1)
330 LogLevel = int.Parse(splitCommand[1]);
331 }
332 }
333
334 static void Main(string[] args)
335 {
336 CecSharpClient p = new CecSharpClient();
337 if (p.Connect(10000))
338 {
339 p.MainLoop();
340 }
341 else
342 {
343 Console.WriteLine("Could not open a connection to the CEC adapter");
344 }
345 }
346
347 private int LogLevel;
348 private LibCecSharp Lib;
349 private LibCECConfiguration Config;
350 }
351 }