| 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 | #include <cec.h> |
| 34 | |
| 35 | #include <cstdio> |
| 36 | #include <fcntl.h> |
| 37 | #include <iostream> |
| 38 | #include <fstream> |
| 39 | #include <string> |
| 40 | #include <sstream> |
| 41 | #include "../lib/platform/threads.h" |
| 42 | #include "../lib/util/StdString.h" |
| 43 | #include "../lib/implementations/CECCommandHandler.h" |
| 44 | |
| 45 | using namespace CEC; |
| 46 | using namespace std; |
| 47 | |
| 48 | #define CEC_TEST_CLIENT_VERSION 1 |
| 49 | |
| 50 | #include <cecloader.h> |
| 51 | |
| 52 | int g_cecLogLevel = CEC_LOG_ALL; |
| 53 | ofstream g_logOutput; |
| 54 | bool g_bShortLog = false; |
| 55 | CStdString g_strPort; |
| 56 | |
| 57 | inline bool HexStrToInt(const std::string& data, uint8_t& value) |
| 58 | { |
| 59 | int iTmp(0); |
| 60 | if (sscanf(data.c_str(), "%x", &iTmp) == 1) |
| 61 | { |
| 62 | if (iTmp > 256) |
| 63 | value = 255; |
| 64 | else if (iTmp < 0) |
| 65 | value = 0; |
| 66 | else |
| 67 | value = (uint8_t) iTmp; |
| 68 | |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | //get the first word (separated by whitespace) from string data and place that in word |
| 76 | //then remove that word from string data |
| 77 | bool GetWord(string& data, string& word) |
| 78 | { |
| 79 | stringstream datastream(data); |
| 80 | string end; |
| 81 | |
| 82 | datastream >> word; |
| 83 | if (datastream.fail()) |
| 84 | { |
| 85 | data.clear(); |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | size_t pos = data.find(word) + word.length(); |
| 90 | |
| 91 | if (pos >= data.length()) |
| 92 | { |
| 93 | data.clear(); |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | data = data.substr(pos); |
| 98 | |
| 99 | datastream.clear(); |
| 100 | datastream.str(data); |
| 101 | |
| 102 | datastream >> end; |
| 103 | if (datastream.fail()) |
| 104 | data.clear(); |
| 105 | |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | void FlushLog(ICECAdapter *cecParser) |
| 110 | { |
| 111 | cec_log_message message; |
| 112 | while (cecParser && cecParser->GetNextLogMessage(&message)) |
| 113 | { |
| 114 | if ((message.level & g_cecLogLevel) == message.level) |
| 115 | { |
| 116 | CStdString strLevel; |
| 117 | switch (message.level) |
| 118 | { |
| 119 | case CEC_LOG_ERROR: |
| 120 | strLevel = "ERROR: "; |
| 121 | break; |
| 122 | case CEC_LOG_WARNING: |
| 123 | strLevel = "WARNING: "; |
| 124 | break; |
| 125 | case CEC_LOG_NOTICE: |
| 126 | strLevel = "NOTICE: "; |
| 127 | break; |
| 128 | case CEC_LOG_TRAFFIC: |
| 129 | strLevel = "TRAFFIC: "; |
| 130 | break; |
| 131 | case CEC_LOG_DEBUG: |
| 132 | strLevel = "DEBUG: "; |
| 133 | break; |
| 134 | default: |
| 135 | break; |
| 136 | } |
| 137 | |
| 138 | CStdString strFullLog; |
| 139 | strFullLog.Format("%s[%16lld]\t%s", strLevel.c_str(), message.time, message.message); |
| 140 | cout << strFullLog.c_str() << endl; |
| 141 | |
| 142 | if (g_logOutput.is_open()) |
| 143 | { |
| 144 | if (g_bShortLog) |
| 145 | g_logOutput << message.message << endl; |
| 146 | else |
| 147 | g_logOutput << strFullLog.c_str() << endl; |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | void ListDevices(ICECAdapter *parser) |
| 154 | { |
| 155 | cec_adapter *devices = new cec_adapter[10]; |
| 156 | uint8_t iDevicesFound = parser->FindAdapters(devices, 10, NULL); |
| 157 | if (iDevicesFound <= 0) |
| 158 | { |
| 159 | cout << "Found devices: NONE" << endl; |
| 160 | } |
| 161 | else |
| 162 | { |
| 163 | CStdString strLog; |
| 164 | strLog.Format("Found devices: %d", iDevicesFound); |
| 165 | cout << strLog.c_str() << endl; |
| 166 | for (unsigned int iDevicePtr = 0; iDevicePtr < iDevicesFound; iDevicePtr++) |
| 167 | { |
| 168 | CStdString strDevice; |
| 169 | strDevice.Format("device: %d\npath: %s\ncom port: %s", iDevicePtr + 1, devices[iDevicePtr].path, devices[iDevicePtr].comm); |
| 170 | cout << endl << strDevice.c_str() << endl; |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | void ShowHelpCommandLine(const char* strExec) |
| 176 | { |
| 177 | cout << endl << |
| 178 | strExec << " {-h|--help|-l|--list-devices|[COM PORT]}" << endl << |
| 179 | endl << |
| 180 | "parameters:" << endl << |
| 181 | " -h --help Shows this help text" << endl << |
| 182 | " -l --list-devices List all devices on this system" << endl << |
| 183 | " -t --type {p|r|t|a} The device type to use. More than one is possible." << endl << |
| 184 | " -p --port {int} The HDMI port to use as active source." << endl << |
| 185 | " -f --log-file {file} Writes all libCEC log message to a file" << endl << |
| 186 | " -sf --short-log-file {file} Writes all libCEC log message without timestamps" << endl << |
| 187 | " and log levels to a file." << endl << |
| 188 | " -d --log-level {level} Sets the log level. See cectypes.h for values." << endl << |
| 189 | " -s --single-command Execute a single command and exit. Does not power" << endl << |
| 190 | " on devices on startup and power them off on exit." << endl << |
| 191 | " [COM PORT] The com port to connect to. If no COM" << endl << |
| 192 | " port is given, the client tries to connect to the" << endl << |
| 193 | " first device that is detected." << endl << |
| 194 | endl << |
| 195 | "Type 'h' or 'help' and press enter after starting the client to display all " << endl << |
| 196 | "available commands" << endl; |
| 197 | } |
| 198 | |
| 199 | ICECAdapter *CreateParser(cec_device_type_list typeList) |
| 200 | { |
| 201 | ICECAdapter *parser = LibCecInit("CECTester", typeList); |
| 202 | if (!parser || parser->GetMinLibVersion() > CEC_TEST_CLIENT_VERSION) |
| 203 | { |
| 204 | #ifdef __WINDOWS__ |
| 205 | cout << "Cannot load libcec.dll" << endl; |
| 206 | #else |
| 207 | cout << "Cannot load libcec.so" << endl; |
| 208 | #endif |
| 209 | return NULL; |
| 210 | } |
| 211 | |
| 212 | CStdString strLog; |
| 213 | strLog.Format("CEC Parser created - libcec version %d.%d", parser->GetLibVersionMajor(), parser->GetLibVersionMinor()); |
| 214 | cout << strLog.c_str() << endl; |
| 215 | |
| 216 | return parser; |
| 217 | } |
| 218 | |
| 219 | void ShowHelpConsole(void) |
| 220 | { |
| 221 | cout << endl << |
| 222 | "================================================================================" << endl << |
| 223 | "Available commands:" << endl << |
| 224 | endl << |
| 225 | "[tx] {bytes} transfer bytes over the CEC line." << endl << |
| 226 | "[txn] {bytes} transfer bytes but don't wait for transmission ACK." << endl << |
| 227 | "[on] {address} power on the device with the given logical address." << endl << |
| 228 | "[standby] {address} put the device with the given address in standby mode." << endl << |
| 229 | "[la] {logical address} change the logical address of the CEC adapter." << endl << |
| 230 | "[p] {device} {port} change the HDMI port number of the CEC adapter." << endl << |
| 231 | "[pa] {physical address} change the physical address of the CEC adapter." << endl << |
| 232 | "[osd] {addr} {string} set OSD message on the specified device." << endl << |
| 233 | "[ver] {addr} get the CEC version of the specified device." << endl << |
| 234 | "[ven] {addr} get the vendor ID of the specified device." << endl << |
| 235 | "[lang] {addr} get the menu language of the specified device." << endl << |
| 236 | "[pow] {addr} get the power status of the specified device." << endl << |
| 237 | "[name] {addr} get the OSD name of the specified device." << endl << |
| 238 | "[poll] {addr} poll the specified device." << endl << |
| 239 | "[lad] lists active devices on the bus" << endl << |
| 240 | "[ad] {addr} checks whether the specified device is active." << endl << |
| 241 | "[at] {type} checks whether the specified device type is active." << endl << |
| 242 | "[volup] send a volume up command to the amp if present" << endl << |
| 243 | "[voldown] send a volume down command to the amp if present" << endl << |
| 244 | "[mute] send a mute/unmute command to the amp if present" << endl << |
| 245 | "[scan] scan the CEC bus and display device info" << endl << |
| 246 | "[mon] {1|0} enable or disable CEC bus monitoring." << endl << |
| 247 | "[log] {1 - 31} change the log level. see cectypes.h for values." << endl << |
| 248 | "[ping] send a ping command to the CEC adapter." << endl << |
| 249 | "[bl] to let the adapter enter the bootloader, to upgrade" << endl << |
| 250 | " the flash rom." << endl << |
| 251 | "[r] reconnect to the CEC adapter." << endl << |
| 252 | "[h] or [help] show this help." << endl << |
| 253 | "[q] or [quit] to quit the CEC test client and switch off all" << endl << |
| 254 | " connected CEC devices." << endl << |
| 255 | "================================================================================" << endl; |
| 256 | } |
| 257 | |
| 258 | int main (int argc, char *argv[]) |
| 259 | { |
| 260 | int8_t iHDMIPort(-1); |
| 261 | cec_device_type_list typeList; |
| 262 | typeList.clear(); |
| 263 | |
| 264 | int iArgPtr = 1; |
| 265 | bool bSingleCommand(false); |
| 266 | while (iArgPtr < argc) |
| 267 | { |
| 268 | if (argc >= iArgPtr + 1) |
| 269 | { |
| 270 | if (!strcmp(argv[iArgPtr], "-f") || |
| 271 | !strcmp(argv[iArgPtr], "--log-file") || |
| 272 | !strcmp(argv[iArgPtr], "-sf") || |
| 273 | !strcmp(argv[iArgPtr], "--short-log-file")) |
| 274 | { |
| 275 | if (argc >= iArgPtr + 2) |
| 276 | { |
| 277 | g_logOutput.open(argv[iArgPtr + 1]); |
| 278 | g_bShortLog = (!strcmp(argv[iArgPtr], "-sf") || !strcmp(argv[iArgPtr], "--short-log-file")); |
| 279 | iArgPtr += 2; |
| 280 | } |
| 281 | else |
| 282 | { |
| 283 | cout << "== skipped log-file parameter: no file given ==" << endl; |
| 284 | ++iArgPtr; |
| 285 | } |
| 286 | } |
| 287 | else if (!strcmp(argv[iArgPtr], "-d") || |
| 288 | !strcmp(argv[iArgPtr], "--log-level")) |
| 289 | { |
| 290 | if (argc >= iArgPtr + 2) |
| 291 | { |
| 292 | int iNewLevel = atoi(argv[iArgPtr + 1]); |
| 293 | if (iNewLevel >= CEC_LOG_ERROR && iNewLevel <= CEC_LOG_ALL) |
| 294 | { |
| 295 | g_cecLogLevel = iNewLevel; |
| 296 | if (!bSingleCommand) |
| 297 | cout << "log level set to " << argv[iArgPtr + 1] << endl; |
| 298 | } |
| 299 | else |
| 300 | { |
| 301 | cout << "== skipped log-level parameter: invalid level '" << argv[iArgPtr + 1] << "' ==" << endl; |
| 302 | } |
| 303 | iArgPtr += 2; |
| 304 | } |
| 305 | else |
| 306 | { |
| 307 | cout << "== skipped log-level parameter: no level given ==" << endl; |
| 308 | ++iArgPtr; |
| 309 | } |
| 310 | } |
| 311 | else if (!strcmp(argv[iArgPtr], "-t") || |
| 312 | !strcmp(argv[iArgPtr], "--type")) |
| 313 | { |
| 314 | if (argc >= iArgPtr + 2) |
| 315 | { |
| 316 | if (!strcmp(argv[iArgPtr + 1], "p")) |
| 317 | { |
| 318 | if (!bSingleCommand) |
| 319 | cout << "== using device type 'playback device'" << endl; |
| 320 | typeList.add(CEC_DEVICE_TYPE_PLAYBACK_DEVICE); |
| 321 | } |
| 322 | else if (!strcmp(argv[iArgPtr + 1], "r")) |
| 323 | { |
| 324 | if (!bSingleCommand) |
| 325 | cout << "== using device type 'recording device'" << endl; |
| 326 | typeList.add(CEC_DEVICE_TYPE_RECORDING_DEVICE); |
| 327 | } |
| 328 | else if (!strcmp(argv[iArgPtr + 1], "t")) |
| 329 | { |
| 330 | if (!bSingleCommand) |
| 331 | cout << "== using device type 'tuner'" << endl; |
| 332 | typeList.add(CEC_DEVICE_TYPE_TUNER); |
| 333 | } |
| 334 | else if (!strcmp(argv[iArgPtr + 1], "a")) |
| 335 | { |
| 336 | if (!bSingleCommand) |
| 337 | cout << "== using device type 'audio system'" << endl; |
| 338 | typeList.add(CEC_DEVICE_TYPE_AUDIO_SYSTEM); |
| 339 | } |
| 340 | else |
| 341 | { |
| 342 | cout << "== skipped invalid device type '" << argv[iArgPtr + 1] << "'" << endl; |
| 343 | } |
| 344 | ++iArgPtr; |
| 345 | } |
| 346 | ++iArgPtr; |
| 347 | } |
| 348 | else if (!strcmp(argv[iArgPtr], "--list-devices") || |
| 349 | !strcmp(argv[iArgPtr], "-l")) |
| 350 | { |
| 351 | ICECAdapter *parser = CreateParser(typeList); |
| 352 | if (parser) |
| 353 | { |
| 354 | ListDevices(parser); |
| 355 | UnloadLibCec(parser); |
| 356 | } |
| 357 | return 0; |
| 358 | } |
| 359 | else if (!strcmp(argv[iArgPtr], "--single-command") || |
| 360 | !strcmp(argv[iArgPtr], "-s")) |
| 361 | { |
| 362 | bSingleCommand = true; |
| 363 | ++iArgPtr; |
| 364 | } |
| 365 | else if (!strcmp(argv[iArgPtr], "--help") || |
| 366 | !strcmp(argv[iArgPtr], "-h")) |
| 367 | { |
| 368 | ShowHelpCommandLine(argv[0]); |
| 369 | return 0; |
| 370 | } |
| 371 | else if (!strcmp(argv[iArgPtr], "-p") || |
| 372 | !strcmp(argv[iArgPtr], "--port")) |
| 373 | { |
| 374 | if (argc >= iArgPtr + 2) |
| 375 | { |
| 376 | iHDMIPort = (int8_t)atoi(argv[iArgPtr + 1]); |
| 377 | cout << "using HDMI port '" << iHDMIPort << "'" << endl; |
| 378 | ++iArgPtr; |
| 379 | } |
| 380 | ++iArgPtr; |
| 381 | } |
| 382 | else |
| 383 | { |
| 384 | g_strPort = argv[iArgPtr++]; |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | if (typeList.IsEmpty()) |
| 390 | { |
| 391 | if (!bSingleCommand) |
| 392 | cout << "No device type given. Using 'recording device'" << endl; |
| 393 | typeList.add(CEC_DEVICE_TYPE_RECORDING_DEVICE); |
| 394 | } |
| 395 | |
| 396 | ICECAdapter *parser = LibCecInit("CECTester", typeList); |
| 397 | if (!parser || parser->GetMinLibVersion() > CEC_TEST_CLIENT_VERSION) |
| 398 | { |
| 399 | #ifdef __WINDOWS__ |
| 400 | cout << "Cannot load libcec.dll" << endl; |
| 401 | #else |
| 402 | cout << "Cannot load libcec.so" << endl; |
| 403 | #endif |
| 404 | return 1; |
| 405 | } |
| 406 | |
| 407 | if (!bSingleCommand) |
| 408 | { |
| 409 | CStdString strLog; |
| 410 | strLog.Format("CEC Parser created - libcec version %d.%d", parser->GetLibVersionMajor(), parser->GetLibVersionMinor()); |
| 411 | cout << strLog.c_str() << endl; |
| 412 | |
| 413 | //make stdin non-blocking |
| 414 | #ifndef __WINDOWS__ |
| 415 | int flags = fcntl(0, F_GETFL, 0); |
| 416 | flags |= O_NONBLOCK; |
| 417 | fcntl(0, F_SETFL, flags); |
| 418 | #endif |
| 419 | } |
| 420 | |
| 421 | if (g_strPort.IsEmpty()) |
| 422 | { |
| 423 | if (!bSingleCommand) |
| 424 | cout << "no serial port given. trying autodetect: "; |
| 425 | cec_adapter devices[10]; |
| 426 | uint8_t iDevicesFound = parser->FindAdapters(devices, 10, NULL); |
| 427 | if (iDevicesFound <= 0) |
| 428 | { |
| 429 | if (bSingleCommand) |
| 430 | cout << "autodetect "; |
| 431 | cout << "FAILED" << endl; |
| 432 | UnloadLibCec(parser); |
| 433 | return 1; |
| 434 | } |
| 435 | else |
| 436 | { |
| 437 | if (!bSingleCommand) |
| 438 | { |
| 439 | cout << endl << " path: " << devices[0].path << endl << |
| 440 | " com port: " << devices[0].comm << endl << endl; |
| 441 | } |
| 442 | g_strPort = devices[0].comm; |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | if (iHDMIPort > 0) |
| 447 | { |
| 448 | parser->SetHDMIPort((cec_logical_address)CEC_DEFAULT_BASE_DEVICE, (uint8_t)iHDMIPort); |
| 449 | FlushLog(parser); |
| 450 | } |
| 451 | |
| 452 | cout << "scanning the CEC bus..." << endl; |
| 453 | |
| 454 | if (!parser->Open(g_strPort.c_str())) |
| 455 | { |
| 456 | cout << "unable to open the device on port " << g_strPort << endl; |
| 457 | FlushLog(parser); |
| 458 | UnloadLibCec(parser); |
| 459 | return 1; |
| 460 | } |
| 461 | |
| 462 | if (!bSingleCommand) |
| 463 | { |
| 464 | cout << "cec device opened" << endl; |
| 465 | |
| 466 | parser->PowerOnDevices(CECDEVICE_TV); |
| 467 | FlushLog(parser); |
| 468 | |
| 469 | parser->SetActiveSource(); |
| 470 | FlushLog(parser); |
| 471 | |
| 472 | cout << "waiting for input" << endl; |
| 473 | } |
| 474 | |
| 475 | bool bContinue(true); |
| 476 | while (bContinue) |
| 477 | { |
| 478 | if (bSingleCommand) |
| 479 | bContinue = false; |
| 480 | |
| 481 | FlushLog(parser); |
| 482 | |
| 483 | /* just ignore the command buffer and clear it */ |
| 484 | cec_command dummy; |
| 485 | while (parser && parser->GetNextCommand(&dummy)) {} |
| 486 | |
| 487 | string input; |
| 488 | getline(cin, input); |
| 489 | cin.clear(); |
| 490 | |
| 491 | if (!input.empty()) |
| 492 | { |
| 493 | string command; |
| 494 | if (GetWord(input, command)) |
| 495 | { |
| 496 | if (command == "tx" || command == "txn") |
| 497 | { |
| 498 | string strvalue; |
| 499 | uint8_t ivalue; |
| 500 | cec_command bytes; |
| 501 | bytes.Clear(); |
| 502 | |
| 503 | while (GetWord(input, strvalue) && HexStrToInt(strvalue, ivalue)) |
| 504 | bytes.PushBack(ivalue); |
| 505 | |
| 506 | if (command == "txn") |
| 507 | bytes.transmit_timeout = 0; |
| 508 | |
| 509 | parser->Transmit(bytes); |
| 510 | } |
| 511 | else if (command == "on") |
| 512 | { |
| 513 | string strValue; |
| 514 | uint8_t iValue = 0; |
| 515 | if (GetWord(input, strValue) && HexStrToInt(strValue, iValue) && iValue <= 0xF) |
| 516 | { |
| 517 | parser->PowerOnDevices((cec_logical_address) iValue); |
| 518 | } |
| 519 | else |
| 520 | { |
| 521 | cout << "invalid destination" << endl; |
| 522 | } |
| 523 | } |
| 524 | else if (command == "standby") |
| 525 | { |
| 526 | string strValue; |
| 527 | uint8_t iValue = 0; |
| 528 | if (GetWord(input, strValue) && HexStrToInt(strValue, iValue) && iValue <= 0xF) |
| 529 | { |
| 530 | parser->StandbyDevices((cec_logical_address) iValue); |
| 531 | } |
| 532 | else |
| 533 | { |
| 534 | cout << "invalid destination" << endl; |
| 535 | } |
| 536 | } |
| 537 | else if (command == "poll") |
| 538 | { |
| 539 | string strValue; |
| 540 | uint8_t iValue = 0; |
| 541 | if (GetWord(input, strValue) && HexStrToInt(strValue, iValue) && iValue <= 0xF) |
| 542 | { |
| 543 | if (parser->PollDevice((cec_logical_address) iValue)) |
| 544 | cout << "POLL message sent" << endl; |
| 545 | else |
| 546 | cout << "POLL message not sent" << endl; |
| 547 | } |
| 548 | else |
| 549 | { |
| 550 | cout << "invalid destination" << endl; |
| 551 | } |
| 552 | } |
| 553 | else if (command == "la") |
| 554 | { |
| 555 | string strvalue; |
| 556 | if (GetWord(input, strvalue)) |
| 557 | { |
| 558 | parser->SetLogicalAddress((cec_logical_address) atoi(strvalue.c_str())); |
| 559 | } |
| 560 | } |
| 561 | else if (command == "p") |
| 562 | { |
| 563 | string strPort, strDevice; |
| 564 | if (GetWord(input, strDevice) && GetWord(input, strPort)) |
| 565 | { |
| 566 | parser->SetHDMIPort((cec_logical_address)atoi(strDevice.c_str()), (uint8_t)atoi(strPort.c_str())); |
| 567 | } |
| 568 | } |
| 569 | else if (command == "pa") |
| 570 | { |
| 571 | string strB1, strB2; |
| 572 | uint8_t iB1, iB2; |
| 573 | if (GetWord(input, strB1) && HexStrToInt(strB1, iB1) && |
| 574 | GetWord(input, strB2) && HexStrToInt(strB2, iB2)) |
| 575 | { |
| 576 | uint16_t iPhysicalAddress = ((uint16_t)iB1 << 8) + iB2; |
| 577 | parser->SetPhysicalAddress(iPhysicalAddress); |
| 578 | } |
| 579 | } |
| 580 | else if (command == "osd") |
| 581 | { |
| 582 | bool bFirstWord(false); |
| 583 | string strAddr, strMessage, strWord; |
| 584 | uint8_t iAddr; |
| 585 | if (GetWord(input, strAddr) && HexStrToInt(strAddr, iAddr) && iAddr < 0xF) |
| 586 | { |
| 587 | while (GetWord(input, strWord)) |
| 588 | { |
| 589 | if (bFirstWord) |
| 590 | { |
| 591 | bFirstWord = false; |
| 592 | strMessage.append(" "); |
| 593 | } |
| 594 | strMessage.append(strWord); |
| 595 | } |
| 596 | parser->SetOSDString((cec_logical_address) iAddr, CEC_DISPLAY_CONTROL_DISPLAY_FOR_DEFAULT_TIME, strMessage.c_str()); |
| 597 | } |
| 598 | } |
| 599 | else if (command == "ping") |
| 600 | { |
| 601 | parser->PingAdapter(); |
| 602 | } |
| 603 | else if (command == "volup") |
| 604 | { |
| 605 | CStdString strLog; |
| 606 | strLog.Format("volume up: %2X", parser->VolumeUp()); |
| 607 | cout << strLog.c_str() << endl; |
| 608 | } |
| 609 | else if (command == "voldown") |
| 610 | { |
| 611 | CStdString strLog; |
| 612 | strLog.Format("volume up: %2X", parser->VolumeDown()); |
| 613 | cout << strLog.c_str() << endl; |
| 614 | } |
| 615 | else if (command == "mute") |
| 616 | { |
| 617 | CStdString strLog; |
| 618 | strLog.Format("mute: %2X", parser->MuteAudio()); |
| 619 | cout << strLog.c_str() << endl; |
| 620 | } |
| 621 | else if (command == "mon") |
| 622 | { |
| 623 | CStdString strEnable; |
| 624 | if (GetWord(input, strEnable) && (strEnable.Equals("0") || strEnable.Equals("1"))) |
| 625 | { |
| 626 | parser->SwitchMonitoring(strEnable.Equals("1")); |
| 627 | } |
| 628 | } |
| 629 | else if (command == "bl") |
| 630 | { |
| 631 | parser->StartBootloader(); |
| 632 | } |
| 633 | else if (command == "lang") |
| 634 | { |
| 635 | CStdString strDev; |
| 636 | if (GetWord(input, strDev)) |
| 637 | { |
| 638 | int iDev = atoi(strDev); |
| 639 | if (iDev >= 0 && iDev < 15) |
| 640 | { |
| 641 | CStdString strLog; |
| 642 | cec_menu_language language; |
| 643 | if (parser->GetDeviceMenuLanguage((cec_logical_address) iDev, &language)) |
| 644 | strLog.Format("menu language '%s'", language.language); |
| 645 | else |
| 646 | strLog = "failed!"; |
| 647 | cout << strLog.c_str() << endl; |
| 648 | } |
| 649 | } |
| 650 | } |
| 651 | else if (command == "ven") |
| 652 | { |
| 653 | CStdString strDev; |
| 654 | if (GetWord(input, strDev)) |
| 655 | { |
| 656 | int iDev = atoi(strDev); |
| 657 | if (iDev >= 0 && iDev < 15) |
| 658 | { |
| 659 | uint64_t iVendor = parser->GetDeviceVendorId((cec_logical_address) iDev); |
| 660 | CStdString strLog; |
| 661 | strLog.Format("vendor id: %06x", iVendor); |
| 662 | cout << strLog.c_str() << endl; |
| 663 | } |
| 664 | } |
| 665 | } |
| 666 | else if (command == "ver") |
| 667 | { |
| 668 | CStdString strDev; |
| 669 | if (GetWord(input, strDev)) |
| 670 | { |
| 671 | int iDev = atoi(strDev); |
| 672 | if (iDev >= 0 && iDev < 15) |
| 673 | { |
| 674 | cec_version iVersion = parser->GetDeviceCecVersion((cec_logical_address) iDev); |
| 675 | switch (iVersion) |
| 676 | { |
| 677 | case CEC_VERSION_1_2: |
| 678 | cout << "CEC version 1.2" << endl; |
| 679 | break; |
| 680 | case CEC_VERSION_1_2A: |
| 681 | cout << "CEC version 1.2a" << endl; |
| 682 | break; |
| 683 | case CEC_VERSION_1_3: |
| 684 | cout << "CEC version 1.3" << endl; |
| 685 | break; |
| 686 | case CEC_VERSION_1_3A: |
| 687 | cout << "CEC version 1.3a" << endl; |
| 688 | break; |
| 689 | default: |
| 690 | cout << "unknown CEC version" << endl; |
| 691 | break; |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 | else if (command == "pow") |
| 697 | { |
| 698 | CStdString strDev; |
| 699 | if (GetWord(input, strDev)) |
| 700 | { |
| 701 | int iDev = atoi(strDev); |
| 702 | if (iDev >= 0 && iDev < 15) |
| 703 | { |
| 704 | cec_power_status iPower = parser->GetDevicePowerStatus((cec_logical_address) iDev); |
| 705 | switch (iPower) |
| 706 | { |
| 707 | case CEC_POWER_STATUS_ON: |
| 708 | cout << "powered on" << endl; |
| 709 | break; |
| 710 | case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY: |
| 711 | cout << "on -> standby" << endl; |
| 712 | break; |
| 713 | case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON: |
| 714 | cout << "standby -> on" << endl; |
| 715 | break; |
| 716 | case CEC_POWER_STATUS_STANDBY: |
| 717 | cout << "standby" << endl; |
| 718 | break; |
| 719 | default: |
| 720 | cout << "unknown power status" << endl; |
| 721 | break; |
| 722 | } |
| 723 | } |
| 724 | } |
| 725 | } |
| 726 | else if (command == "name") |
| 727 | { |
| 728 | CStdString strDev; |
| 729 | if (GetWord(input, strDev)) |
| 730 | { |
| 731 | int iDev = atoi(strDev); |
| 732 | if (iDev >= 0 && iDev < 15) |
| 733 | { |
| 734 | cec_osd_name name = parser->GetOSDName((cec_logical_address)iDev); |
| 735 | cout << "OSD name of device " << iDev << " is '" << name.name << "'" << endl; |
| 736 | } |
| 737 | } |
| 738 | } |
| 739 | else if (command == "lad") |
| 740 | { |
| 741 | cout << "listing active devices:" << endl; |
| 742 | cec_logical_addresses addresses = parser->GetActiveDevices(); |
| 743 | for (uint8_t iPtr = 0; iPtr < 16; iPtr++) |
| 744 | if (addresses[iPtr]) |
| 745 | cout << "logical address " << (int)iPtr << endl; |
| 746 | } |
| 747 | else if (command == "scan") |
| 748 | { |
| 749 | cout << "CEC bus information" << endl; |
| 750 | cout << "===================" << endl; |
| 751 | cec_logical_addresses addresses = parser->GetActiveDevices(); |
| 752 | for (uint8_t iPtr = 0; iPtr < 16; iPtr++) |
| 753 | if (addresses[iPtr]) |
| 754 | { |
| 755 | uint64_t iVendorId = parser->GetDeviceVendorId((cec_logical_address)iPtr); |
| 756 | cec_version iCecVersion = parser->GetDeviceCecVersion((cec_logical_address)iPtr); |
| 757 | cec_power_status power = parser->GetDevicePowerStatus((cec_logical_address)iPtr); |
| 758 | cec_osd_name osdName = parser->GetOSDName((cec_logical_address)iPtr); |
| 759 | cec_menu_language lang; |
| 760 | lang.device = CECDEVICE_UNKNOWN; |
| 761 | parser->GetDeviceMenuLanguage((cec_logical_address)iPtr, &lang); |
| 762 | |
| 763 | cout << "device #" << (int)iPtr << ": " << parser->ToString((cec_logical_address)iPtr) << endl; |
| 764 | cout << "vendor: " << parser->ToString((cec_vendor_id)iVendorId) << endl; |
| 765 | cout << "osd string: " << osdName.name << endl; |
| 766 | cout << "CEC version: " << parser->ToString(iCecVersion) << endl; |
| 767 | cout << "power status: " << parser->ToString(power) << endl; |
| 768 | if ((uint8_t)lang.device == iPtr) |
| 769 | cout << "language: " << lang.language << endl; |
| 770 | cout << endl; |
| 771 | } |
| 772 | } |
| 773 | else if (command == "ad") |
| 774 | { |
| 775 | CStdString strDev; |
| 776 | if (GetWord(input, strDev)) |
| 777 | { |
| 778 | int iDev = atoi(strDev); |
| 779 | if (iDev >= 0 && iDev < 15) |
| 780 | cout << "logical address " << iDev << " is " << (parser->IsActiveDevice((cec_logical_address)iDev) ? "active" : "not active") << endl; |
| 781 | } |
| 782 | } |
| 783 | else if (command == "at") |
| 784 | { |
| 785 | CStdString strType; |
| 786 | if (GetWord(input, strType)) |
| 787 | { |
| 788 | cec_device_type type = CEC_DEVICE_TYPE_TV; |
| 789 | if (strType.Equals("a")) |
| 790 | type = CEC_DEVICE_TYPE_AUDIO_SYSTEM; |
| 791 | else if (strType.Equals("p")) |
| 792 | type = CEC_DEVICE_TYPE_PLAYBACK_DEVICE; |
| 793 | else if (strType.Equals("r")) |
| 794 | type = CEC_DEVICE_TYPE_RECORDING_DEVICE; |
| 795 | else if (strType.Equals("t")) |
| 796 | type = CEC_DEVICE_TYPE_TUNER; |
| 797 | cout << "device " << type << " is " << (parser->IsActiveDeviceType(type) ? "active" : "not active") << endl; |
| 798 | } |
| 799 | } |
| 800 | else if (command == "r") |
| 801 | { |
| 802 | cout << "closing the connection" << endl; |
| 803 | parser->Close(); |
| 804 | FlushLog(parser); |
| 805 | |
| 806 | cout << "opening a new connection" << endl; |
| 807 | parser->Open(g_strPort.c_str()); |
| 808 | FlushLog(parser); |
| 809 | |
| 810 | cout << "setting active source" << endl; |
| 811 | parser->SetActiveSource(); |
| 812 | } |
| 813 | else if (command == "h" || command == "help") |
| 814 | { |
| 815 | ShowHelpConsole(); |
| 816 | } |
| 817 | else if (command == "q" || command == "quit") |
| 818 | { |
| 819 | bContinue = false; |
| 820 | } |
| 821 | else if (command == "log") |
| 822 | { |
| 823 | CStdString strLevel; |
| 824 | if (GetWord(input, strLevel)) |
| 825 | { |
| 826 | int iNewLevel = atoi(strLevel); |
| 827 | if (iNewLevel >= CEC_LOG_ERROR && iNewLevel <= CEC_LOG_ALL) |
| 828 | { |
| 829 | g_cecLogLevel = iNewLevel; |
| 830 | cout << "log level changed to " << strLevel.c_str() << endl; |
| 831 | } |
| 832 | } |
| 833 | } |
| 834 | } |
| 835 | if (bContinue) |
| 836 | cout << "waiting for input" << endl; |
| 837 | } |
| 838 | |
| 839 | if (bContinue) |
| 840 | CCondition::Sleep(50); |
| 841 | } |
| 842 | |
| 843 | if (!bSingleCommand) |
| 844 | parser->StandbyDevices(CECDEVICE_BROADCAST); |
| 845 | |
| 846 | parser->Close(); |
| 847 | FlushLog(parser); |
| 848 | UnloadLibCec(parser); |
| 849 | |
| 850 | if (g_logOutput.is_open()) |
| 851 | g_logOutput.close(); |
| 852 | |
| 853 | return 0; |
| 854 | } |