| 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 | |
| 44 | using namespace CEC; |
| 45 | using namespace std; |
| 46 | |
| 47 | #define CEC_TEST_CLIENT_VERSION 8 |
| 48 | |
| 49 | #include <cecloader.h> |
| 50 | |
| 51 | int g_cecLogLevel = CEC_LOG_ALL; |
| 52 | ofstream g_logOutput; |
| 53 | bool g_bShortLog = false; |
| 54 | CStdString g_strPort; |
| 55 | |
| 56 | inline bool HexStrToInt(const std::string& data, uint8_t& value) |
| 57 | { |
| 58 | int iTmp(0); |
| 59 | if (sscanf(data.c_str(), "%x", &iTmp) == 1) |
| 60 | { |
| 61 | if (iTmp > 256) |
| 62 | value = 255; |
| 63 | else if (iTmp < 0) |
| 64 | value = 0; |
| 65 | else |
| 66 | value = (uint8_t) iTmp; |
| 67 | |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | //get the first word (separated by whitespace) from string data and place that in word |
| 75 | //then remove that word from string data |
| 76 | bool GetWord(string& data, string& word) |
| 77 | { |
| 78 | stringstream datastream(data); |
| 79 | string end; |
| 80 | |
| 81 | datastream >> word; |
| 82 | if (datastream.fail()) |
| 83 | { |
| 84 | data.clear(); |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | size_t pos = data.find(word) + word.length(); |
| 89 | |
| 90 | if (pos >= data.length()) |
| 91 | { |
| 92 | data.clear(); |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | data = data.substr(pos); |
| 97 | |
| 98 | datastream.clear(); |
| 99 | datastream.str(data); |
| 100 | |
| 101 | datastream >> end; |
| 102 | if (datastream.fail()) |
| 103 | data.clear(); |
| 104 | |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | void flush_log(ICECAdapter *cecParser) |
| 109 | { |
| 110 | cec_log_message message; |
| 111 | while (cecParser && cecParser->GetNextLogMessage(&message)) |
| 112 | { |
| 113 | if ((message.level & g_cecLogLevel) == message.level) |
| 114 | { |
| 115 | CStdString strLevel; |
| 116 | switch (message.level) |
| 117 | { |
| 118 | case CEC_LOG_ERROR: |
| 119 | strLevel = "ERROR: "; |
| 120 | break; |
| 121 | case CEC_LOG_WARNING: |
| 122 | strLevel = "WARNING: "; |
| 123 | break; |
| 124 | case CEC_LOG_NOTICE: |
| 125 | strLevel = "NOTICE: "; |
| 126 | break; |
| 127 | case CEC_LOG_TRAFFIC: |
| 128 | strLevel = "TRAFFIC: "; |
| 129 | break; |
| 130 | case CEC_LOG_DEBUG: |
| 131 | strLevel = "DEBUG: "; |
| 132 | break; |
| 133 | default: |
| 134 | break; |
| 135 | } |
| 136 | |
| 137 | CStdString strFullLog; |
| 138 | strFullLog.Format("%s[%16lld]\t%s", strLevel.c_str(), message.time, message.message); |
| 139 | cout << strFullLog.c_str() << endl; |
| 140 | |
| 141 | if (g_logOutput.is_open()) |
| 142 | { |
| 143 | if (g_bShortLog) |
| 144 | g_logOutput << message.message << endl; |
| 145 | else |
| 146 | g_logOutput << strFullLog.c_str() << endl; |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | void list_devices(ICECAdapter *parser) |
| 153 | { |
| 154 | cec_adapter *devices = new cec_adapter[10]; |
| 155 | uint8_t iDevicesFound = parser->FindAdapters(devices, 10, NULL); |
| 156 | if (iDevicesFound <= 0) |
| 157 | { |
| 158 | cout << "Found devices: NONE" << endl; |
| 159 | } |
| 160 | else |
| 161 | { |
| 162 | CStdString strLog; |
| 163 | strLog.Format("Found devices: %d", iDevicesFound); |
| 164 | cout << strLog.c_str() << endl; |
| 165 | for (unsigned int iDevicePtr = 0; iDevicePtr < iDevicesFound; iDevicePtr++) |
| 166 | { |
| 167 | CStdString strDevice; |
| 168 | strDevice.Format("device: %d\npath: %s\ncom port: %s", iDevicePtr + 1, devices[iDevicePtr].path, devices[iDevicePtr].comm); |
| 169 | cout << endl << strDevice.c_str() << endl; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | void show_help(const char* strExec) |
| 175 | { |
| 176 | cout << endl << |
| 177 | strExec << " {-h|--help|-l|--list-devices|[COM PORT]}" << endl << |
| 178 | endl << |
| 179 | "parameters:" << endl << |
| 180 | " -h --help Shows this help text" << endl << |
| 181 | " -l --list-devices List all devices on this system" << endl << |
| 182 | " -f --log-file {file} Writes all libCEC log message to a file" << endl << |
| 183 | " -sf --short-log-file {file} Writes all libCEC log message without timestamps" << endl << |
| 184 | " and log levels to a file." << endl << |
| 185 | " [COM PORT] The com port to connect to. If no COM" << endl << |
| 186 | " port is given, the client tries to connect to the" << endl << |
| 187 | " first device that is detected." << endl << |
| 188 | endl << |
| 189 | "Type 'h' or 'help' and press enter after starting the client to display all " << endl << |
| 190 | "available commands" << endl; |
| 191 | } |
| 192 | |
| 193 | void show_console_help(void) |
| 194 | { |
| 195 | cout << endl << |
| 196 | "================================================================================" << endl << |
| 197 | "Available commands:" << endl << |
| 198 | endl << |
| 199 | "tx {bytes} transfer bytes over the CEC line." << endl << |
| 200 | "txn {bytes} transfer bytes and don't wait for an ACK reply." << endl << |
| 201 | "[tx 40 00 FF 11 22 33] sends bytes 0x40 0x00 0xFF 0x11 0x22 0x33" << endl << |
| 202 | endl << |
| 203 | "on {address} power on the device with the given logical address." << endl << |
| 204 | "[on 5] power on a connected audio system" << endl << |
| 205 | endl << |
| 206 | "standby {address} put the device with the given address in standby mode." << endl << |
| 207 | "[standby 0] powers off the TV" << endl << |
| 208 | endl << |
| 209 | "la {logical_address} change the logical address of the CEC adapter." << endl << |
| 210 | "[la 4] logical address 4" << endl << |
| 211 | endl << |
| 212 | "pa {physical_address} change the physical address of the CEC adapter." << endl << |
| 213 | "[pa 10 00] physical address 1.0.0.0" << endl << |
| 214 | endl << |
| 215 | "osd {addr} {string} set OSD message on the specified device." << endl << |
| 216 | "[osd 0 Test Message] displays 'Test Message' on the TV" << endl << |
| 217 | endl << |
| 218 | "[mon] {1|0} enable or disable CEC bus monitoring." << endl << |
| 219 | "[log] {1 - 31} change the log level. see cectypes.h for values." << endl << |
| 220 | "[ping] send a ping command to the CEC adapter." << endl << |
| 221 | "[bl] to let the adapter enter the bootloader, to upgrade" << endl << |
| 222 | " the flash rom." << endl << |
| 223 | "[r] reconnect to the CEC adapter." << endl << |
| 224 | "[h] or [help] show this help." << endl << |
| 225 | "[q] or [quit] to quit the CEC test client and switch off all" << endl << |
| 226 | " connected CEC devices." << endl << |
| 227 | "================================================================================" << endl; |
| 228 | } |
| 229 | |
| 230 | int main (int argc, char *argv[]) |
| 231 | { |
| 232 | ICECAdapter *parser = LoadLibCec("CECTester"); |
| 233 | if (!parser || parser->GetMinVersion() > CEC_TEST_CLIENT_VERSION) |
| 234 | { |
| 235 | #ifdef __WINDOWS__ |
| 236 | cout << "Cannot load libcec.dll" << endl; |
| 237 | #else |
| 238 | cout << "Cannot load libcec.so" << endl; |
| 239 | #endif |
| 240 | return 1; |
| 241 | } |
| 242 | CStdString strLog; |
| 243 | strLog.Format("CEC Parser created - libcec version %d", parser->GetLibVersion()); |
| 244 | cout << strLog.c_str() << endl; |
| 245 | |
| 246 | //make stdin non-blocking |
| 247 | #ifndef __WINDOWS__ |
| 248 | int flags = fcntl(0, F_GETFL, 0); |
| 249 | flags |= O_NONBLOCK; |
| 250 | fcntl(0, F_SETFL, flags); |
| 251 | #endif |
| 252 | |
| 253 | int iArgPtr = 1; |
| 254 | while (iArgPtr < argc) |
| 255 | { |
| 256 | if (argc >= iArgPtr + 1) |
| 257 | { |
| 258 | if (!strcmp(argv[iArgPtr], "-f") || |
| 259 | !strcmp(argv[iArgPtr], "--log-file") || |
| 260 | !strcmp(argv[iArgPtr], "-sf") || |
| 261 | !strcmp(argv[iArgPtr], "--short-log-file")) |
| 262 | { |
| 263 | if (argc >= iArgPtr + 2) |
| 264 | { |
| 265 | g_logOutput.open(argv[iArgPtr + 1]); |
| 266 | g_bShortLog = (!strcmp(argv[iArgPtr], "-sf") || !strcmp(argv[iArgPtr], "--short-log-file")); |
| 267 | iArgPtr += 2; |
| 268 | } |
| 269 | else |
| 270 | { |
| 271 | ++iArgPtr; |
| 272 | } |
| 273 | } |
| 274 | else if (!strcmp(argv[iArgPtr], "--list-devices") || |
| 275 | !strcmp(argv[iArgPtr], "-l")) |
| 276 | { |
| 277 | list_devices(parser); |
| 278 | UnloadLibCec(parser); |
| 279 | return 0; |
| 280 | } |
| 281 | else if (!strcmp(argv[iArgPtr], "--help") || |
| 282 | !strcmp(argv[iArgPtr], "-h")) |
| 283 | { |
| 284 | show_help(argv[0]); |
| 285 | UnloadLibCec(parser); |
| 286 | return 0; |
| 287 | } |
| 288 | else |
| 289 | { |
| 290 | g_strPort = argv[iArgPtr++]; |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | if (g_strPort.IsEmpty()) |
| 296 | { |
| 297 | cout << "no serial port given. trying autodetect: "; |
| 298 | cec_adapter devices[10]; |
| 299 | uint8_t iDevicesFound = parser->FindAdapters(devices, 10, NULL); |
| 300 | if (iDevicesFound <= 0) |
| 301 | { |
| 302 | cout << "FAILED" << endl; |
| 303 | UnloadLibCec(parser); |
| 304 | return 1; |
| 305 | } |
| 306 | else |
| 307 | { |
| 308 | cout << endl << " path: " << devices[0].path << endl << |
| 309 | " com port: " << devices[0].comm << endl << endl; |
| 310 | g_strPort = devices[0].comm; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | if (!parser->Open(g_strPort.c_str())) |
| 315 | { |
| 316 | cout << "unable to open the device on port " << g_strPort << endl; |
| 317 | flush_log(parser); |
| 318 | UnloadLibCec(parser); |
| 319 | return 1; |
| 320 | } |
| 321 | |
| 322 | cout << "cec device opened" << endl; |
| 323 | |
| 324 | parser->PowerOnDevices(CECDEVICE_TV); |
| 325 | flush_log(parser); |
| 326 | |
| 327 | parser->SetActiveView(); |
| 328 | flush_log(parser); |
| 329 | |
| 330 | bool bContinue(true); |
| 331 | cout << "waiting for input" << endl; |
| 332 | while (bContinue) |
| 333 | { |
| 334 | flush_log(parser); |
| 335 | |
| 336 | string input; |
| 337 | getline(cin, input); |
| 338 | cin.clear(); |
| 339 | |
| 340 | if (!input.empty()) |
| 341 | { |
| 342 | string command; |
| 343 | if (GetWord(input, command)) |
| 344 | { |
| 345 | if (command == "tx" || command == "txn") |
| 346 | { |
| 347 | string strvalue; |
| 348 | uint8_t ivalue; |
| 349 | cec_command bytes; |
| 350 | bytes.clear(); |
| 351 | |
| 352 | while (GetWord(input, strvalue) && HexStrToInt(strvalue, ivalue)) |
| 353 | bytes.push_back(ivalue); |
| 354 | |
| 355 | parser->Transmit(bytes, command == "tx"); |
| 356 | } |
| 357 | else if (command == "on") |
| 358 | { |
| 359 | string strValue; |
| 360 | uint8_t iValue = 0; |
| 361 | if (GetWord(input, strValue) && HexStrToInt(strValue, iValue) && iValue <= 0xF) |
| 362 | { |
| 363 | parser->PowerOnDevices((cec_logical_address) iValue); |
| 364 | } |
| 365 | else |
| 366 | { |
| 367 | cout << "invalid destination" << endl; |
| 368 | } |
| 369 | } |
| 370 | else if (command == "standby") |
| 371 | { |
| 372 | string strValue; |
| 373 | uint8_t iValue = 0; |
| 374 | if (GetWord(input, strValue) && HexStrToInt(strValue, iValue) && iValue <= 0xF) |
| 375 | { |
| 376 | parser->StandbyDevices((cec_logical_address) iValue); |
| 377 | } |
| 378 | else |
| 379 | { |
| 380 | cout << "invalid destination" << endl; |
| 381 | } |
| 382 | } |
| 383 | else if (command == "la") |
| 384 | { |
| 385 | string strvalue; |
| 386 | if (GetWord(input, strvalue)) |
| 387 | { |
| 388 | parser->SetLogicalAddress((cec_logical_address) atoi(strvalue.c_str())); |
| 389 | } |
| 390 | } |
| 391 | else if (command == "pa") |
| 392 | { |
| 393 | string strB1, strB2; |
| 394 | uint8_t iB1, iB2; |
| 395 | if (GetWord(input, strB1) && HexStrToInt(strB1, iB1) && |
| 396 | GetWord(input, strB2) && HexStrToInt(strB2, iB2)) |
| 397 | { |
| 398 | uint16_t iPhysicalAddress = ((uint16_t)iB1 << 8) + iB2; |
| 399 | parser->SetPhysicalAddress(iPhysicalAddress); |
| 400 | } |
| 401 | } |
| 402 | else if (command == "osd") |
| 403 | { |
| 404 | bool bFirstWord(false); |
| 405 | string strAddr, strMessage, strWord; |
| 406 | uint8_t iAddr; |
| 407 | if (GetWord(input, strAddr) && HexStrToInt(strAddr, iAddr) && iAddr < 0xF) |
| 408 | { |
| 409 | while (GetWord(input, strWord)) |
| 410 | { |
| 411 | if (bFirstWord) |
| 412 | { |
| 413 | bFirstWord = false; |
| 414 | strMessage.append(" "); |
| 415 | } |
| 416 | strMessage.append(strWord); |
| 417 | } |
| 418 | parser->SetOSDString((cec_logical_address) iAddr, CEC_DISPLAY_CONTROL_DISPLAY_FOR_DEFAULT_TIME, strMessage.c_str()); |
| 419 | } |
| 420 | } |
| 421 | else if (command == "ping") |
| 422 | { |
| 423 | parser->PingAdapter(); |
| 424 | } |
| 425 | else if (command == "mon") |
| 426 | { |
| 427 | CStdString strEnable; |
| 428 | if (GetWord(input, strEnable) && (strEnable.Equals("0") || strEnable.Equals("1"))) |
| 429 | { |
| 430 | parser->SwitchMonitoring(strEnable.Equals("1")); |
| 431 | } |
| 432 | } |
| 433 | else if (command == "bl") |
| 434 | { |
| 435 | parser->StartBootloader(); |
| 436 | } |
| 437 | else if (command == "r") |
| 438 | { |
| 439 | cout << "closing the connection" << endl; |
| 440 | parser->Close(); |
| 441 | flush_log(parser); |
| 442 | |
| 443 | cout << "opening a new connection" << endl; |
| 444 | parser->Open(g_strPort.c_str()); |
| 445 | flush_log(parser); |
| 446 | |
| 447 | cout << "setting active view" << endl; |
| 448 | parser->SetActiveView(); |
| 449 | } |
| 450 | else if (command == "h" || command == "help") |
| 451 | { |
| 452 | show_console_help(); |
| 453 | } |
| 454 | else if (command == "q" || command == "quit") |
| 455 | { |
| 456 | bContinue = false; |
| 457 | } |
| 458 | else if (command == "log") |
| 459 | { |
| 460 | CStdString strLevel; |
| 461 | if (GetWord(input, strLevel)) |
| 462 | { |
| 463 | int iNewLevel = atoi(strLevel); |
| 464 | if (iNewLevel >= CEC_LOG_ERROR && iNewLevel <= CEC_LOG_ALL) |
| 465 | { |
| 466 | g_cecLogLevel = iNewLevel; |
| 467 | cout << "log level changed to " << strLevel.c_str() << endl; |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | if (bContinue) |
| 473 | cout << "waiting for input" << endl; |
| 474 | } |
| 475 | CCondition::Sleep(50); |
| 476 | } |
| 477 | |
| 478 | parser->StandbyDevices(CECDEVICE_BROADCAST); |
| 479 | parser->Close(); |
| 480 | flush_log(parser); |
| 481 | UnloadLibCec(parser); |
| 482 | |
| 483 | if (g_logOutput.is_open()) |
| 484 | g_logOutput.close(); |
| 485 | |
| 486 | return 0; |
| 487 | } |