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