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