| 1 | /* |
| 2 | * This file is part of the libCEC(R) library. |
| 3 | * |
| 4 | * libCEC(R) is Copyright (C) 2011-2012 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 "../env.h" |
| 34 | #include "../include/cec.h" |
| 35 | |
| 36 | #include <cstdio> |
| 37 | #include <fcntl.h> |
| 38 | #include <iostream> |
| 39 | #include <fstream> |
| 40 | #include <string> |
| 41 | #include <sstream> |
| 42 | #include <signal.h> |
| 43 | #include "../lib/platform/threads/mutex.h" |
| 44 | #include "../lib/platform/util/timeutils.h" |
| 45 | #include "../lib/implementations/CECCommandHandler.h" |
| 46 | #include "../lib/platform/util/StdString.h" |
| 47 | |
| 48 | using namespace CEC; |
| 49 | using namespace std; |
| 50 | using namespace PLATFORM; |
| 51 | |
| 52 | #include <cecloader.h> |
| 53 | |
| 54 | CMutex g_outputMutex; |
| 55 | |
| 56 | CEvent g_responseEvent; |
| 57 | cec_opcode g_lastCommand = CEC_OPCODE_NONE; |
| 58 | |
| 59 | CEvent g_keyEvent; |
| 60 | cec_user_control_code g_lastKey = CEC_USER_CONTROL_CODE_UNKNOWN; |
| 61 | |
| 62 | ICECCallbacks g_callbacks; |
| 63 | libcec_configuration g_config; |
| 64 | ICECAdapter * g_parser; |
| 65 | |
| 66 | inline void PrintToStdOut(const char *strFormat, ...) |
| 67 | { |
| 68 | CStdString strLog; |
| 69 | |
| 70 | va_list argList; |
| 71 | va_start(argList, strFormat); |
| 72 | strLog.FormatV(strFormat, argList); |
| 73 | va_end(argList); |
| 74 | |
| 75 | CLockObject lock(g_outputMutex); |
| 76 | cout << strLog << endl; |
| 77 | } |
| 78 | |
| 79 | //get the first word (separated by whitespace) from string data and place that in word |
| 80 | //then remove that word from string data |
| 81 | bool GetWord(string& data, string& word) |
| 82 | { |
| 83 | stringstream datastream(data); |
| 84 | string end; |
| 85 | |
| 86 | datastream >> word; |
| 87 | if (datastream.fail()) |
| 88 | { |
| 89 | data.clear(); |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | size_t pos = data.find(word) + word.length(); |
| 94 | |
| 95 | if (pos >= data.length()) |
| 96 | { |
| 97 | data.clear(); |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | data = data.substr(pos); |
| 102 | |
| 103 | datastream.clear(); |
| 104 | datastream.str(data); |
| 105 | |
| 106 | datastream >> end; |
| 107 | if (datastream.fail()) |
| 108 | data.clear(); |
| 109 | |
| 110 | return true; |
| 111 | } |
| 112 | |
| 113 | int CecLogMessage(void *UNUSED(cbParam), const cec_log_message &message) |
| 114 | { |
| 115 | switch (message.level) |
| 116 | { |
| 117 | case CEC_LOG_ERROR: |
| 118 | PrintToStdOut("ERROR:\t%s", message.message); |
| 119 | break; |
| 120 | case CEC_LOG_WARNING: |
| 121 | PrintToStdOut("ERROR:\t%s", message.message); |
| 122 | break; |
| 123 | default: |
| 124 | break; |
| 125 | } |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | int CecKeyPress(void *UNUSED(cbParam), const cec_keypress &key) |
| 131 | { |
| 132 | g_lastKey = key.keycode; |
| 133 | g_keyEvent.Signal(); |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | int CecCommand(void *UNUSED(cbParam), const cec_command &command) |
| 138 | { |
| 139 | g_lastCommand = command.opcode; |
| 140 | g_responseEvent.Signal(); |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | bool ProcessConsoleCommand(string &input) |
| 145 | { |
| 146 | if (!input.empty()) |
| 147 | { |
| 148 | string command; |
| 149 | if (GetWord(input, command)) |
| 150 | { |
| 151 | if (command == "q" || command == "quit") |
| 152 | return false; |
| 153 | } |
| 154 | } |
| 155 | return true; |
| 156 | } |
| 157 | |
| 158 | bool OpenConnection(cec_device_type type = CEC_DEVICE_TYPE_RECORDING_DEVICE) |
| 159 | { |
| 160 | g_config.Clear(); |
| 161 | snprintf(g_config.strDeviceName, 13, "CEC-config"); |
| 162 | g_config.callbackParam = NULL; |
| 163 | g_config.clientVersion = (uint32_t)CEC_CLIENT_VERSION_1_9_0; |
| 164 | g_callbacks.CBCecLogMessage = &CecLogMessage; |
| 165 | g_callbacks.CBCecKeyPress = &CecKeyPress; |
| 166 | g_callbacks.CBCecCommand = &CecCommand; |
| 167 | g_config.callbacks = &g_callbacks; |
| 168 | |
| 169 | g_config.deviceTypes.add(type); |
| 170 | |
| 171 | g_parser = LibCecInitialise(&g_config); |
| 172 | if (!g_parser) |
| 173 | return false; |
| 174 | |
| 175 | // init video on targets that need this |
| 176 | g_parser->InitVideoStandalone(); |
| 177 | |
| 178 | CStdString strPort; |
| 179 | cec_adapter devices[10]; |
| 180 | uint8_t iDevicesFound = g_parser->FindAdapters(devices, 10, NULL); |
| 181 | if (iDevicesFound <= 0) |
| 182 | { |
| 183 | PrintToStdOut("autodetect FAILED"); |
| 184 | UnloadLibCec(g_parser); |
| 185 | return false; |
| 186 | } |
| 187 | else |
| 188 | { |
| 189 | strPort = devices[0].comm; |
| 190 | } |
| 191 | |
| 192 | PrintToStdOut("opening a connection to the CEC adapter..."); |
| 193 | if (!g_parser->Open(strPort.c_str())) |
| 194 | { |
| 195 | PrintToStdOut("unable to open the device on port %s", strPort.c_str()); |
| 196 | UnloadLibCec(g_parser); |
| 197 | return false; |
| 198 | } |
| 199 | |
| 200 | g_parser->GetCurrentConfiguration(&g_config); |
| 201 | PrintToStdOut("CEC Parser created - libCEC version %s", g_parser->ToString((cec_server_version)g_config.serverVersion)); |
| 202 | |
| 203 | return true; |
| 204 | } |
| 205 | |
| 206 | int8_t FindPhysicalAddressPortNumber(void) |
| 207 | { |
| 208 | PrintToStdOut("Enter the HDMI port number to which you connected your CEC adapter, followed by <enter>. Valid ports are in the range 1-15. Anything else will cancel this wizard."); |
| 209 | string input; |
| 210 | getline(cin, input); |
| 211 | cin.clear(); |
| 212 | if (input.empty()) |
| 213 | return -1; |
| 214 | |
| 215 | int hdmiport = atoi(input.c_str()); |
| 216 | return (hdmiport < 1 || hdmiport > 15) ? -1 : (int8_t)hdmiport; |
| 217 | } |
| 218 | |
| 219 | cec_logical_address FindPhysicalAddressBaseDevice(void) |
| 220 | { |
| 221 | PrintToStdOut("Press 1 if your CEC adapter is connected to your TV or\nPress 2 if it's connected to an AVR, followed by <enter>. Anything else will cancel this wizard."); |
| 222 | |
| 223 | string input; |
| 224 | getline(cin, input); |
| 225 | cin.clear(); |
| 226 | if (input.empty() || (input != "1" && input != "2")) |
| 227 | { |
| 228 | PrintToStdOut("Exiting..."); |
| 229 | return CECDEVICE_UNKNOWN; |
| 230 | } |
| 231 | return (input == "2") ? |
| 232 | CECDEVICE_AUDIOSYSTEM : |
| 233 | CECDEVICE_TV; |
| 234 | } |
| 235 | |
| 236 | uint16_t FindPhysicalAddress(void) |
| 237 | { |
| 238 | PrintToStdOut("=== Physical Address Configuration ===\n"); |
| 239 | uint16_t iAddress(CEC_INVALID_PHYSICAL_ADDRESS); |
| 240 | |
| 241 | PrintToStdOut("Do you want to let libCEC try to autodetect the address (y/n)?"); |
| 242 | string input; |
| 243 | getline(cin, input); |
| 244 | cin.clear(); |
| 245 | if (input == "y" || input == "Y") |
| 246 | { |
| 247 | cec_logical_address baseDevice = FindPhysicalAddressBaseDevice(); |
| 248 | if (baseDevice == CECDEVICE_UNKNOWN) |
| 249 | return iAddress; |
| 250 | |
| 251 | int8_t iPortNumber = FindPhysicalAddressPortNumber(); |
| 252 | if (iPortNumber == -1) |
| 253 | return iAddress; |
| 254 | |
| 255 | PrintToStdOut("Trying to detect the physical address..."); |
| 256 | if (!g_parser->SetHDMIPort(baseDevice, iPortNumber)) |
| 257 | PrintToStdOut("Failed. Please enter the address manually, or restart this wizard and use different settings."); |
| 258 | else |
| 259 | { |
| 260 | g_parser->GetCurrentConfiguration(&g_config); |
| 261 | iAddress = g_parser->GetDevicePhysicalAddress(g_config.logicalAddresses.primary); |
| 262 | if (iAddress == 0 || iAddress == CEC_INVALID_PHYSICAL_ADDRESS) |
| 263 | PrintToStdOut("Failed. Please enter the address manually, or restart this wizard and use different settings."); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | if (iAddress == 0 || iAddress == CEC_INVALID_PHYSICAL_ADDRESS) |
| 268 | { |
| 269 | PrintToStdOut("Please enter the physical address (0001 - FFFE), followed by <enter>."); |
| 270 | getline(cin, input); |
| 271 | cin.clear(); |
| 272 | |
| 273 | int iAddressTmp; |
| 274 | if (sscanf(input.c_str(), "%x", &iAddressTmp) == 1) |
| 275 | { |
| 276 | if (iAddressTmp <= CEC_PHYSICAL_ADDRESS_TV || iAddressTmp > CEC_MAX_PHYSICAL_ADDRESS) |
| 277 | iAddressTmp = CEC_INVALID_PHYSICAL_ADDRESS; |
| 278 | iAddress = (uint16_t)iAddressTmp; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | if (iAddress != 0) |
| 283 | { |
| 284 | g_parser->SetPhysicalAddress(iAddress); |
| 285 | g_parser->SetActiveSource(g_config.deviceTypes[0]); |
| 286 | } |
| 287 | |
| 288 | return iAddress; |
| 289 | } |
| 290 | |
| 291 | bool PowerOnTV(uint64_t iTimeout = 60000) |
| 292 | { |
| 293 | cec_power_status currentTvPower(CEC_POWER_STATUS_UNKNOWN); |
| 294 | uint64_t iNow = GetTimeMs(); |
| 295 | uint64_t iTarget = iNow + iTimeout; |
| 296 | |
| 297 | currentTvPower = g_parser->GetDevicePowerStatus(CECDEVICE_TV); |
| 298 | if (currentTvPower != CEC_POWER_STATUS_ON) |
| 299 | { |
| 300 | PrintToStdOut("Sending 'power on' command to the TV\n=== Please wait ==="); |
| 301 | g_parser->PowerOnDevices(CECDEVICE_TV); |
| 302 | while (iTarget > iNow) |
| 303 | { |
| 304 | g_responseEvent.Wait((uint32_t)(iTarget - iNow)); |
| 305 | if (g_lastCommand == CEC_OPCODE_REQUEST_ACTIVE_SOURCE) |
| 306 | break; |
| 307 | iNow = GetTimeMs(); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | currentTvPower = g_parser->GetDevicePowerStatus(CECDEVICE_TV); |
| 312 | |
| 313 | if (currentTvPower != CEC_POWER_STATUS_ON) |
| 314 | PrintToStdOut("Failed to power on the TV, or the TV does not respond properly"); |
| 315 | |
| 316 | return currentTvPower == CEC_POWER_STATUS_ON; |
| 317 | } |
| 318 | |
| 319 | void sighandler(int iSignal) |
| 320 | { |
| 321 | PrintToStdOut("signal caught: %d - exiting", iSignal); |
| 322 | |
| 323 | g_parser->Close(); |
| 324 | UnloadLibCec(g_parser); |
| 325 | |
| 326 | exit(1); |
| 327 | } |
| 328 | |
| 329 | int main (int UNUSED(argc), char *UNUSED(argv[])) |
| 330 | { |
| 331 | if (signal(SIGINT, sighandler) == SIG_ERR) |
| 332 | { |
| 333 | PrintToStdOut("can't register sighandler"); |
| 334 | return -1; |
| 335 | } |
| 336 | |
| 337 | g_callbacks.Clear(); |
| 338 | g_config.Clear(); |
| 339 | PrintToStdOut("=== USB-CEC Adapter Configuration ===\n"); |
| 340 | if (!OpenConnection()) |
| 341 | return 1; |
| 342 | |
| 343 | if (!PowerOnTV()) |
| 344 | return 1; |
| 345 | |
| 346 | bool bAddressOk(false); |
| 347 | while (!bAddressOk) |
| 348 | { |
| 349 | uint16_t iAddress = FindPhysicalAddress(); |
| 350 | |
| 351 | PrintToStdOut("Physical address: %4X", iAddress); |
| 352 | PrintToStdOut("Is this correct (y/n)?"); |
| 353 | string input; |
| 354 | getline(cin, input); |
| 355 | cin.clear(); |
| 356 | bAddressOk = (input == "y" || input == "Y"); |
| 357 | } |
| 358 | |
| 359 | g_parser->GetCurrentConfiguration(&g_config); |
| 360 | |
| 361 | { |
| 362 | cec_menu_language lang; |
| 363 | if (g_parser->GetDeviceMenuLanguage(CECDEVICE_TV, &lang)) |
| 364 | { |
| 365 | PrintToStdOut("TV menu language: %s", lang.language); |
| 366 | PrintToStdOut("Do you want the application to use the menu language of the TV (y/n)?"); |
| 367 | string input; |
| 368 | getline(cin, input); |
| 369 | cin.clear(); |
| 370 | g_config.bUseTVMenuLanguage = (input == "y" || input == "Y") ? 1 : 0; |
| 371 | } |
| 372 | else |
| 373 | { |
| 374 | PrintToStdOut("The TV did not respond properly to the menu language request."); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | { |
| 379 | PrintToStdOut("Do you want to make the CEC adapter the active source when starting the application (y/n)?"); |
| 380 | string input; |
| 381 | getline(cin, input); |
| 382 | cin.clear(); |
| 383 | g_config.bActivateSource = (input == "y" || input == "Y") ? 1 : 0; |
| 384 | } |
| 385 | |
| 386 | { |
| 387 | PrintToStdOut("Do you want to power on the TV when starting the application (y/n)?"); |
| 388 | string input; |
| 389 | getline(cin, input); |
| 390 | cin.clear(); |
| 391 | if (input == "y" || input == "Y") |
| 392 | g_config.wakeDevices.Set(CECDEVICE_TV); |
| 393 | } |
| 394 | |
| 395 | { |
| 396 | PrintToStdOut("Do you want to power off CEC devices when closing the application (y/n)?"); |
| 397 | string input; |
| 398 | getline(cin, input); |
| 399 | cin.clear(); |
| 400 | if (input == "y" || input == "Y") |
| 401 | g_config.powerOffDevices.Set(CECDEVICE_TV); |
| 402 | } |
| 403 | |
| 404 | { |
| 405 | PrintToStdOut("Do you want to power off CEC devices when the screensaver is activated (y/n)?"); |
| 406 | string input; |
| 407 | getline(cin, input); |
| 408 | cin.clear(); |
| 409 | g_config.bPowerOffScreensaver = (input == "y" || input == "Y") ? 1 : 0; |
| 410 | } |
| 411 | |
| 412 | { |
| 413 | PrintToStdOut("Do you want to put the PC in standby when the TV is put in standby mode (y/n)?"); |
| 414 | string input; |
| 415 | getline(cin, input); |
| 416 | cin.clear(); |
| 417 | g_config.bPowerOffOnStandby = (input == "y" || input == "Y") ? 1 : 0; |
| 418 | } |
| 419 | |
| 420 | { |
| 421 | PrintToStdOut("Do you want to send an inactive source message when stopping the application (y/n)?"); |
| 422 | string input; |
| 423 | getline(cin, input); |
| 424 | cin.clear(); |
| 425 | g_config.bSendInactiveSource = (input == "y" || input == "Y") ? 1 : 0; |
| 426 | } |
| 427 | |
| 428 | PrintToStdOut("\n\n=== USB-CEC Adapter Configuration Summary ==="); |
| 429 | PrintToStdOut("HDMI port number: %d", g_config.iHDMIPort); |
| 430 | PrintToStdOut("Connected to HDMI device: %X", (uint8_t)g_config.baseDevice); |
| 431 | PrintToStdOut("Physical address: %4X", g_config.iPhysicalAddress); |
| 432 | PrintToStdOut("Use the TV's language setting: %s", g_config.bUseTVMenuLanguage ? "yes" : "no"); |
| 433 | PrintToStdOut("Make the adapter the active source when starting XBMC: %s", g_config.bActivateSource ? "yes" : "no"); |
| 434 | PrintToStdOut("Power on the TV when starting XBMC: %s", g_config.wakeDevices.IsSet(CECDEVICE_BROADCAST) ? "yes" : "no"); |
| 435 | PrintToStdOut("Power off devices when stopping XBMC: %s", g_config.powerOffDevices.IsSet(CECDEVICE_BROADCAST) ? "yes" : "no"); |
| 436 | PrintToStdOut("Put devices in standby mode when activating screensaver: %s", g_config.bPowerOffScreensaver ? "yes" : "no"); |
| 437 | PrintToStdOut("Put this PC in standby mode when the TV is switched off: %s", g_config.bPowerOffOnStandby ? "yes" : "no"); |
| 438 | PrintToStdOut("Send an inactive source message when stopping XBMC: %s\n\n", g_config.bSendInactiveSource ? "yes" : "no"); |
| 439 | |
| 440 | PrintToStdOut("Storing settings ..."); |
| 441 | if (g_parser->PersistConfiguration(&g_config)) |
| 442 | PrintToStdOut("Settings stored."); |
| 443 | else |
| 444 | PrintToStdOut("The settings could not be stored"); |
| 445 | |
| 446 | ofstream configOutput; |
| 447 | configOutput.open("usb_2548_1001.xml"); |
| 448 | if (configOutput.is_open()) |
| 449 | { |
| 450 | CStdString strWakeDevices; |
| 451 | for (uint8_t iPtr = 0; iPtr < 16; iPtr++) |
| 452 | if (g_config.wakeDevices[iPtr]) |
| 453 | strWakeDevices.AppendFormat(" %d", iPtr); |
| 454 | CStdString strStandbyDevices; |
| 455 | for (uint8_t iPtr = 0; iPtr < 16; iPtr++) |
| 456 | if (g_config.powerOffDevices[iPtr]) |
| 457 | strStandbyDevices.AppendFormat(" %d", iPtr); |
| 458 | |
| 459 | configOutput << |
| 460 | "<settings>\n" << |
| 461 | "\t<setting id=\"enabled\" value=\"1\" />\n" << |
| 462 | "\t<setting id=\"activate_source\" value=\"" << (int)g_config.bActivateSource << "\" />\n" << |
| 463 | "\t<setting id=\"wake_devices\" value=\"" << strWakeDevices.c_str() << "\" />\n" << |
| 464 | "\t<setting id=\"standby_devices\" value=\"" << strStandbyDevices.c_str() << "\" />\n" << |
| 465 | "\t<setting id=\"cec_standby_screensaver\" value=\"" << (int)g_config.bPowerOffScreensaver << "\" />\n" << |
| 466 | "\t<setting id=\"standby_pc_on_tv_standby\" value=\"" << (int)g_config.bPowerOffOnStandby << "\" />\n" << |
| 467 | "\t<setting id=\"use_tv_menu_language\" value=\"" << (int)g_config.bUseTVMenuLanguage << "\" />\n" << |
| 468 | "\t<setting id=\"physical_address\" value=\"" << hex << g_config.iPhysicalAddress << "\" />\n" << |
| 469 | "\t<setting id=\"cec_hdmi_port\" value=\"" << g_config.iHDMIPort << "\" />\n" << |
| 470 | "\t<setting id=\"connected_device\" value=\"" << (int)g_config.baseDevice << "\" />\n" << |
| 471 | "\t<setting id=\"port\" value=\"\" />\n" << |
| 472 | "\t<setting id=\"send_inactive_source\" value=\"" << (int)g_config.bSendInactiveSource << "\" />\n" << |
| 473 | "</settings>"; |
| 474 | configOutput.close(); |
| 475 | |
| 476 | PrintToStdOut("The configuration has been stored in 'usb_2548_1001.xml'. Copy this file to ~/.userdata/peripheral_data to use it in XBMC"); |
| 477 | } |
| 478 | |
| 479 | g_parser->StandbyDevices(); |
| 480 | g_parser->Close(); |
| 481 | UnloadLibCec(g_parser); |
| 482 | |
| 483 | PrintToStdOut("Press enter to close this wizard."); |
| 484 | string input; |
| 485 | getline(cin, input); |
| 486 | return 0; |
| 487 | } |