renamed: CECExports.h => cec.h, CECExportsC.h => cecc.h, CECLoader.h => cecloader...
[deb_libcec.git] / src / testclient / main.cpp
index f2f2bf3e0a9678338fc8b347b38d6afc853b0006..817d26797cf7c139cf2977d611083574fadb3b7a 100644 (file)
  *     http://www.pulse-eight.net/
  */
 
-#include "../../include/CECExports.h"
-#include "../lib/platform/threads.h"
-#include "../lib/util/StdString.h"
+#include <cec.h>
+
 #include <cstdio>
 #include <fcntl.h>
 #include <iostream>
 #include <string>
 #include <sstream>
+#include "../lib/platform/threads.h"
+#include "../lib/util/StdString.h"
 
 using namespace CEC;
 using namespace std;
 
-#define CEC_TEST_CLIENT_VERSION 3
+#define CEC_TEST_CLIENT_VERSION 7
 
+#include <cecloader.h>
 
-inline bool HexStrToInt(const std::string& data, int& value)
+inline bool HexStrToInt(const std::string& data, uint8_t& value)
 {
-  return sscanf(data.c_str(), "%x", &value) == 1;
-}
+  int iTmp(0);
+  if (sscanf(data.c_str(), "%x", &iTmp) == 1)
+  {
+    if (iTmp > 256)
+      value = 255;
+         else if (iTmp < 0)
+      value = 0;
+    else
+      value = (uint8_t) iTmp;
 
+    return true;
+  }
+
+  return false;
+}
 
 //get the first word (separated by whitespace) from string data and place that in word
 //then remove that word from string data
@@ -93,41 +107,42 @@ void flush_log(ICECAdapter *cecParser)
     switch (message.level)
     {
     case CEC_LOG_ERROR:
-      cout << "ERROR:   " << message.message.c_str() << endl;
+      cout << "ERROR:   ";
       break;
     case CEC_LOG_WARNING:
-      cout << "WARNING: " << message.message.c_str() << endl;
+      cout << "WARNING: ";
       break;
     case CEC_LOG_NOTICE:
-      cout << "NOTICE:  " << message.message.c_str() << endl;
+      cout << "NOTICE:  ";
       break;
     case CEC_LOG_DEBUG:
-      cout << "DEBUG:   " << message.message.c_str() << endl;
+      cout << "DEBUG:   ";
       break;
     }
+
+    CStdString strMessageTmp;
+    strMessageTmp.Format("[%16lld]\t%s", message.time, message.message);
+    cout << strMessageTmp.c_str() << endl;
   }
 }
 
 void list_devices(ICECAdapter *parser)
 {
-  cout << "Found devices: ";
-  vector<cec_adapter> devices;
-  int iDevicesFound = parser->FindAdapters(devices);
+  cec_adapter *devices = new cec_adapter[10];
+  uint8_t iDevicesFound = parser->FindAdapters(devices, 10, NULL);
   if (iDevicesFound <= 0)
   {
-#ifdef __WINDOWS__
-    cout << "Not supported yet, sorry!" << endl;
-#else
-    cout << "NONE" << endl;
-#endif
+    cout << "Found devices: NONE" << endl;
   }
   else
   {
-    cout << devices.size() << endl;
-    for (unsigned int iDevicePtr = 0; iDevicePtr < devices.size(); iDevicePtr++)
+    CStdString strLog;
+    strLog.Format("Found devices: %d", iDevicesFound);
+    cout << strLog.c_str() << endl;
+    for (unsigned int iDevicePtr = 0; iDevicePtr < iDevicesFound; iDevicePtr++)
     {
       CStdString strDevice;
-      strDevice.Format("device:        %d\npath:          %s\ncom port:      %s", iDevicePtr, devices[iDevicePtr].path.c_str(), devices[0].comm.c_str());
+      strDevice.Format("device:        %d\npath:          %s\ncom port:      %s", iDevicePtr + 1, devices[iDevicePtr].path, devices[iDevicePtr].comm);
       cout << endl << strDevice.c_str() << endl;
     }
   }
@@ -155,22 +170,35 @@ void show_console_help(void)
   "tx {bytes}                transfer bytes over the CEC line." << endl <<
   "[tx 40 00 FF 11 22 33]    sends bytes 0x40 0x00 0xFF 0x11 0x22 0x33" << endl <<
   endl <<
+  "on {address}              power on the device with the given logical address." << endl <<
+  "[on 5]                    power on a connected audio system" << endl <<
+  endl <<
+  "standby {address}         put the device with the given address in standby mode." << endl <<
+  "[standby 0]               powers off the TV" << endl <<
+  endl <<
   "la {logical_address}      change the logical address of the CEC adapter." << endl <<
   "[la 4]                    logical address 4" << endl <<
   endl <<
   "[ping]                    send a ping command to the CEC adapter." << endl <<
-  "[bl]                      to let the adapter enter the bootloader, to upgrade the flash rom." << endl <<
+  "[bl]                      to let the adapter enter the bootloader, to upgrade" << endl <<
+  "                          the flash rom." << endl <<
+  "[r]                       reconnect to the CEC adapter." << endl <<
   "[h] or [help]             show this help." << endl <<
-  "[q] or [quit]             to quit the CEC test client and switch off all connected CEC devices." << endl <<
+  "[q] or [quit]             to quit the CEC test client and switch off all" << endl <<
+  "                          connected CEC devices." << endl <<
   "================================================================================" << endl;
 }
 
 int main (int argc, char *argv[])
 {
-  ICECAdapter *parser = LoadLibCec("CEC Tester");
-  if (!parser && parser->GetMinVersion() > CEC_TEST_CLIENT_VERSION)
+  ICECAdapter *parser = LoadLibCec("CECTester");
+  if (!parser || parser->GetMinVersion() > CEC_TEST_CLIENT_VERSION)
   {
-    cout << "Unable to create parser. Is libcec.dll present?" << endl;
+#ifdef __WINDOWS__
+    cout << "Cannot load libcec.dll" << endl;
+#else
+    cout << "Cannot load libcec.so" << endl;
+#endif
     return 1;
   }
   CStdString strLog;
@@ -188,8 +216,8 @@ int main (int argc, char *argv[])
   if (argc < 2)
   {
     cout << "no serial port given. trying autodetect: ";
-    vector<cec_adapter> devices;
-    int iDevicesFound = parser->FindAdapters(devices);
+    cec_adapter devices[10];
+    uint8_t iDevicesFound = parser->FindAdapters(devices, 10, NULL);
     if (iDevicesFound <= 0)
     {
       cout << "FAILED" << endl;
@@ -228,7 +256,6 @@ int main (int argc, char *argv[])
   }
 
   cout << "cec device opened" << endl;
-  usleep(CEC_SETTLE_DOWN_TIME);
 
   parser->PowerOnDevices(CECDEVICE_TV);
   flush_log(parser);
@@ -254,13 +281,41 @@ int main (int argc, char *argv[])
         if (command == "tx")
         {
           string strvalue;
-          int    ivalue;
-          vector<uint8_t> bytes;
+          uint8_t ivalue;
+          cec_command bytes;
+          bytes.clear();
+
           while (GetWord(input, strvalue) && HexStrToInt(strvalue, ivalue))
           bytes.push_back(ivalue);
 
           parser->Transmit(bytes);
         }
+        else if (command == "on")
+        {
+          string strValue;
+          uint8_t iValue = 0;
+          if (GetWord(input, strValue) && HexStrToInt(strValue, iValue) && iValue <= 0xF)
+          {
+            parser->PowerOnDevices((cec_logical_address) iValue);
+          }
+          else
+          {
+            cout << "invalid destination" << endl;
+          }
+        }
+        else if (command == "standby")
+        {
+          string strValue;
+          uint8_t iValue = 0;
+          if (GetWord(input, strValue) && HexStrToInt(strValue, iValue) && iValue <= 0xF)
+          {
+            parser->StandbyDevices((cec_logical_address) iValue);
+          }
+          else
+          {
+            cout << "invalid destination" << endl;
+          }
+        }
         else if (command == "la")
         {
           string strvalue;
@@ -277,6 +332,19 @@ int main (int argc, char *argv[])
         {
           parser->StartBootloader();
         }
+        else if (command == "r")
+        {
+          cout << "closing the connection" << endl;
+          parser->Close();
+          flush_log(parser);
+
+          cout << "opening a new connection" << endl;
+          parser->Open(strPort.c_str());
+          flush_log(parser);
+
+          cout << "setting active view" << endl;
+          parser->SetActiveView();
+        }
         else if (command == "h" || command == "help")
         {
           show_console_help();