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