-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathCecSharpClient.cs
354 lines (332 loc) · 13.5 KB
/
CecSharpClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
* This file is part of the libCEC(R) library.
*
* libCEC(R) is Copyright (C) 2011-2020 Pulse-Eight Limited. All rights reserved.
* libCEC(R) is an original work, containing original code.
*
* libCEC(R) is a trademark of Pulse-Eight Limited.
*
* This program is dual-licensed; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
* Alternatively, you can license this library under a commercial license,
* please contact Pulse-Eight Licensing for more information.
*
* For more information contact:
* Pulse-Eight Licensing <[email protected]>
* http://www.pulse-eight.com/
* http://www.pulse-eight.net/
*
* Author: Lars Op den Kamp <[email protected]>
*
*/
using System;
using System.Text;
using CecSharp;
namespace CecSharpClient
{
class CecSharpClient : CecCallbackMethods
{
public CecSharpClient()
{
Config = new LibCECConfiguration();
Config.DeviceTypes.Types[0] = CecDeviceType.RecordingDevice;
Config.DeviceName = "CEC Tester";
Config.ClientVersion = LibCECConfiguration.CurrentVersion;
LogLevel = (int)CecLogLevel.All;
Lib = new LibCecSharp(this, Config);
Lib.InitVideoStandalone();
Console.WriteLine("CEC Parser created - libCEC version " + Lib.VersionToString(Config.ServerVersion));
}
public override int ReceiveCommand(CecCommand command)
{
return 1;
}
public override int ReceiveKeypress(CecKeypress key)
{
return 1;
}
public override int ReceiveLogMessage(CecLogMessage message)
{
if (((int)message.Level & LogLevel) == (int)message.Level)
{
string strLevel = "";
switch (message.Level)
{
case CecLogLevel.Error:
strLevel = "ERROR: ";
break;
case CecLogLevel.Warning:
strLevel = "WARNING: ";
break;
case CecLogLevel.Notice:
strLevel = "NOTICE: ";
break;
case CecLogLevel.Traffic:
strLevel = "TRAFFIC: ";
break;
case CecLogLevel.Debug:
strLevel = "DEBUG: ";
break;
default:
break;
}
string strLog = string.Format("{0} {1,16} {2}", strLevel, message.Time, message.Message);
Console.WriteLine(strLog);
}
return 1;
}
public bool Connect(int timeout)
{
CecAdapter[] adapters = Lib.FindAdapters(string.Empty);
if (adapters.Length > 0)
return Connect(adapters[0].ComPort, timeout);
else
{
Console.WriteLine("Did not find any CEC adapters");
return false;
}
}
public bool Connect(string port, int timeout)
{
return Lib.Open(port, timeout);
}
public void Close()
{
Lib.Close();
}
public void ListDevices()
{
int iAdapter = 0;
foreach (CecAdapter adapter in Lib.FindAdapters(string.Empty))
{
Console.WriteLine("Adapter: " + iAdapter++);
Console.WriteLine("Path: " + adapter.Path);
Console.WriteLine("Com port: " + adapter.ComPort);
}
}
void ShowConsoleHelp()
{
Console.WriteLine(
"================================================================================" + Environment.NewLine +
"Available commands:" + Environment.NewLine +
Environment.NewLine +
"[tx] {bytes} transfer bytes over the CEC line." + Environment.NewLine +
"[txn] {bytes} transfer bytes but don't wait for transmission ACK." + Environment.NewLine +
"[on] {address} power on the device with the given logical address." + Environment.NewLine +
"[standby] {address} put the device with the given address in standby mode." + Environment.NewLine +
"[la] {logical_address} change the logical address of the CEC adapter." + Environment.NewLine +
"[pa] {physical_address} change the physical address of the CEC adapter." + Environment.NewLine +
"[osd] {addr} {string} set OSD message on the specified device." + Environment.NewLine +
"[ver] {addr} get the CEC version of the specified device." + Environment.NewLine +
"[ven] {addr} get the vendor ID of the specified device." + Environment.NewLine +
"[lang] {addr} get the menu language of the specified device." + Environment.NewLine +
"[pow] {addr} get the power status of the specified device." + Environment.NewLine +
"[poll] {addr} poll the specified device." + Environment.NewLine +
"[scan] scan the CEC bus and display device info" + Environment.NewLine +
"[mon] {1|0} enable or disable CEC bus monitoring." + Environment.NewLine +
"[log] {1 - 31} change the log level. see cectypes.h for values." + Environment.NewLine +
"[ping] send a ping command to the CEC adapter." + Environment.NewLine +
"[bl] to let the adapter enter the bootloader, to upgrade" + Environment.NewLine +
" the flash rom." + Environment.NewLine +
"[r] reconnect to the CEC adapter." + Environment.NewLine +
"[h] or [help] show this help." + Environment.NewLine +
"[q] or [quit] to quit the CEC test client and switch off all" + Environment.NewLine +
" connected CEC devices." + Environment.NewLine +
"================================================================================");
}
public void MainLoop()
{
bool bContinue = true;
string command;
while (bContinue)
{
Console.WriteLine("waiting for input");
command = Console.ReadLine();
if (command != null && command.Length == 0)
continue;
string[] splitCommand = command.Split(' ');
if (splitCommand[0] == "tx" || splitCommand[0] == "txn")
{
CecCommand bytes = new CecCommand();
for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
{
bytes.PushBack(byte.Parse(splitCommand[iPtr], System.Globalization.NumberStyles.HexNumber));
}
if (command == "txn")
bytes.TransmitTimeout = 0;
Lib.Transmit(bytes);
}
else if (splitCommand[0] == "on")
{
if (splitCommand.Length > 1)
Lib.PowerOnDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
else
Lib.PowerOnDevices(CecLogicalAddress.Broadcast);
}
else if (splitCommand[0] == "standby")
{
if (splitCommand.Length > 1)
Lib.StandbyDevices((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
else
Lib.StandbyDevices(CecLogicalAddress.Broadcast);
}
else if (splitCommand[0] == "poll")
{
bool bSent = false;
if (splitCommand.Length > 1)
bSent = Lib.PollDevice((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
else
bSent = Lib.PollDevice(CecLogicalAddress.Broadcast);
if (bSent)
Console.WriteLine("POLL message sent");
else
Console.WriteLine("POLL message not sent");
}
else if (splitCommand[0] == "la")
{
if (splitCommand.Length > 1)
Lib.SetLogicalAddress((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
}
else if (splitCommand[0] == "pa")
{
if (splitCommand.Length > 1)
Lib.SetPhysicalAddress(ushort.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
}
else if (splitCommand[0] == "osd")
{
if (splitCommand.Length > 2)
{
StringBuilder osdString = new StringBuilder();
for (int iPtr = 1; iPtr < splitCommand.Length; iPtr++)
{
osdString.Append(splitCommand[iPtr]);
if (iPtr != splitCommand.Length - 1)
osdString.Append(" ");
}
Lib.SetOSDString((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber), CecDisplayControl.DisplayForDefaultTime, osdString.ToString());
}
}
else if (splitCommand[0] == "ping")
{
Lib.PingAdapter();
}
else if (splitCommand[0] == "mon")
{
bool enable = splitCommand.Length > 1 ? splitCommand[1] == "1" : false;
Lib.SwitchMonitoring(enable);
}
else if (splitCommand[0] == "bl")
{
Lib.StartBootloader();
}
else if (splitCommand[0] == "lang")
{
if (splitCommand.Length > 1)
{
string language = Lib.GetDeviceMenuLanguage((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
Console.WriteLine("Menu language: " + language);
}
}
else if (splitCommand[0] == "ven")
{
if (splitCommand.Length > 1)
{
CecVendorId vendor = Lib.GetDeviceVendorId((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
Console.WriteLine("Vendor ID: " + Lib.ToString(vendor));
}
}
else if (splitCommand[0] == "ver")
{
if (splitCommand.Length > 1)
{
CecVersion version = Lib.GetDeviceCecVersion((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
Console.WriteLine("CEC version: " + Lib.ToString(version));
}
}
else if (splitCommand[0] == "pow")
{
if (splitCommand.Length > 1)
{
CecPowerStatus power = Lib.GetDevicePowerStatus((CecLogicalAddress)byte.Parse(splitCommand[1], System.Globalization.NumberStyles.HexNumber));
Console.WriteLine("power status: " + Lib.ToString(power));
}
}
else if (splitCommand[0] == "r")
{
Console.WriteLine("closing the connection");
Lib.Close();
Console.WriteLine("opening a new connection");
Connect(10000);
Console.WriteLine("setting active source");
Lib.SetActiveSource(CecDeviceType.PlaybackDevice);
}
else if (splitCommand[0] == "scan")
{
StringBuilder output = new StringBuilder();
output.AppendLine("CEC bus information");
output.AppendLine("===================");
CecLogicalAddresses addresses = Lib.GetActiveDevices();
for (int iPtr = 0; iPtr < addresses.Addresses.Length; iPtr++)
{
CecLogicalAddress address = (CecLogicalAddress)iPtr;
if (!addresses.IsSet(address))
continue;
CecVendorId iVendorId = Lib.GetDeviceVendorId(address);
bool bActive = Lib.IsActiveDevice(address);
ushort iPhysicalAddress = Lib.GetDevicePhysicalAddress(address);
string strAddr = Lib.PhysicalAddressToString(iPhysicalAddress);
CecVersion iCecVersion = Lib.GetDeviceCecVersion(address);
CecPowerStatus power = Lib.GetDevicePowerStatus(address);
string osdName = Lib.GetDeviceOSDName(address);
string lang = Lib.GetDeviceMenuLanguage(address);
output.AppendLine("device #" + iPtr + ": " + Lib.ToString(address));
output.AppendLine("address: " + strAddr);
output.AppendLine("active source: " + (bActive ? "yes" : "no"));
output.AppendLine("vendor: " + Lib.ToString(iVendorId));
output.AppendLine("osd string: " + osdName);
output.AppendLine("CEC version: " + Lib.ToString(iCecVersion));
output.AppendLine("power status: " + Lib.ToString(power));
if (!string.IsNullOrEmpty(lang))
output.AppendLine("language: " + lang);
output.AppendLine("");
}
Console.WriteLine(output.ToString());
}
else if (splitCommand[0] == "h" || splitCommand[0] == "help")
ShowConsoleHelp();
else if (splitCommand[0] == "q" || splitCommand[0] == "quit")
bContinue = false;
else if (splitCommand[0] == "log" && splitCommand.Length > 1)
LogLevel = int.Parse(splitCommand[1]);
}
}
static void Main(string[] args)
{
CecSharpClient p = new CecSharpClient();
if (p.Connect(10000))
{
p.MainLoop();
}
else
{
Console.WriteLine("Could not open a connection to the CEC adapter");
}
}
private int LogLevel;
private LibCecSharp Lib;
private LibCECConfiguration Config;
}
}