cec: added GetActiveDevices()/cec_get_active_devices(), IsActiveDevice()/cec_is_activ...
[deb_libcec.git] / src / testclient / main.cpp
index 9952a8c77e514c59cbc27d017ef61ee458017045..dbb62e70804825094853973f43425cc44d54139b 100644 (file)
 using namespace CEC;
 using namespace std;
 
-#define CEC_TEST_CLIENT_VERSION 8
+#define CEC_TEST_CLIENT_VERSION 1
 
 #include <cecloader.h>
 
 int        g_cecLogLevel = CEC_LOG_ALL;
-int        g_iLogicalAddress = CECDEVICE_PLAYBACKDEVICE1;
 ofstream   g_logOutput;
 bool       g_bShortLog = false;
 CStdString g_strPort;
@@ -106,7 +105,7 @@ bool GetWord(string& data, string& word)
   return true;
 }
 
-void flush_log(ICECAdapter *cecParser)
+void FlushLog(ICECAdapter *cecParser)
 {
   cec_log_message message;
   while (cecParser && cecParser->GetNextLogMessage(&message))
@@ -150,7 +149,7 @@ void flush_log(ICECAdapter *cecParser)
   }
 }
 
-void list_devices(ICECAdapter *parser)
+void ListDevices(ICECAdapter *parser)
 {
   cec_adapter *devices = new cec_adapter[10];
   uint8_t iDevicesFound = parser->FindAdapters(devices, 10, NULL);
@@ -172,7 +171,7 @@ void list_devices(ICECAdapter *parser)
   }
 }
 
-void show_help(const char* strExec)
+void ShowHelpCommandLine(const char* strExec)
 {
   cout << endl <<
       strExec << " {-h|--help|-l|--list-devices|[COM PORT]}" << endl <<
@@ -180,11 +179,14 @@ void show_help(const char* strExec)
       "parameters:" << endl <<
       "  -h --help                   Shows this help text" << endl <<
       "  -l --list-devices           List all devices on this system" << endl <<
-      "  -la --logical-address {a}   The logical address to use." << endl <<
+      "  -t --type {p|r|t|a}         The device type to use. More than one is possible." << endl <<
+      "  -p --physical {hex-address} The physical address to use for the primary logical device." << endl <<
       "  -f --log-file {file}        Writes all libCEC log message to a file" << endl <<
       "  -sf --short-log-file {file} Writes all libCEC log message without timestamps" << endl <<
       "                              and log levels to a file." << endl <<
       "  -d --log-level {level}      Sets the log level. See cectypes.h for values." << endl <<
+      "  -s --single-command         Execute a single command and exit. Does not power" << endl <<
+      "                              on devices on startup and power them off on exit." << endl <<
       "  [COM PORT]                  The com port to connect to. If no COM" << endl <<
       "                              port is given, the client tries to connect to the" << endl <<
       "                              first device that is detected." << endl <<
@@ -193,37 +195,47 @@ void show_help(const char* strExec)
       "available commands" << endl;
 }
 
-void show_console_help(void)
+ICECAdapter *CreateParser(cec_device_type_list typeList)
+{
+  ICECAdapter *parser = LibCecInit("CECTester", typeList);
+  if (!parser || parser->GetMinLibVersion() > CEC_TEST_CLIENT_VERSION)
+  {
+  #ifdef __WINDOWS__
+    cout << "Cannot load libcec.dll" << endl;
+  #else
+    cout << "Cannot load libcec.so" << endl;
+  #endif
+    return NULL;
+  }
+
+  CStdString strLog;
+  strLog.Format("CEC Parser created - libcec version %d.%d", parser->GetLibVersionMajor(), parser->GetLibVersionMinor());
+  cout << strLog.c_str() << endl;
+
+  return parser;
+}
+
+void ShowHelpConsole(void)
 {
   cout << endl <<
   "================================================================================" << endl <<
   "Available commands:" << endl <<
   endl <<
   "tx {bytes}                transfer bytes over the CEC line." << endl <<
-  "txn {bytes}               transfer bytes and don't wait for an ACK reply." << endl <<
-  "[tx 40 00 FF 11 22 33]    sends bytes 0x40 0x00 0xFF 0x11 0x22 0x33" << endl <<
-  endl <<
+  "txn {bytes}               transfer bytes but don't wait for transmission ACK." << 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 <<
   "pa {physical_address}     change the physical address of the CEC adapter." << endl <<
-  "[pa 10 00]                physical address 1.0.0.0" << endl <<
-  endl <<
   "osd {addr} {string}       set OSD message on the specified device." << endl <<
-  "[osd 0 Test Message]      displays 'Test Message' on the TV" << endl <<
-  endl <<
   "ver {addr}                get the CEC version of the specified device." << endl <<
-  "[ver 0]                   get the CEC version of the TV" << endl <<
-  endl <<
-  "lang {addr                get the menu language of the specified device." << endl <<
-  "[lang 0]                  get the menu language of the TV" << endl <<
-  endl <<
+  "ven {addr}                get the vendor ID of the specified device." << endl <<
+  "lang {addr}               get the menu language of the specified device." << endl <<
+  "pow {addr}                get the power status of the specified device." << endl <<
+  "poll {addr}               poll the specified device." << endl <<
+  "lad                       lists active devices on the bus" << endl <<
+  "ad {addr}                 checks whether the specified device is active." << endl <<
+  "at {type}                 checks whether the specified device type is active." << endl <<
   "[mon] {1|0}               enable or disable CEC bus monitoring." << endl <<
   "[log] {1 - 31}            change the log level. see cectypes.h for values." << endl <<
   "[ping]                    send a ping command to the CEC adapter." << endl <<
@@ -238,28 +250,12 @@ void show_console_help(void)
 
 int main (int argc, char *argv[])
 {
-  ICECAdapter *parser = LoadLibCec("CECTester");
-  if (!parser || parser->GetMinVersion() > CEC_TEST_CLIENT_VERSION)
-  {
-#ifdef __WINDOWS__
-    cout << "Cannot load libcec.dll" << endl;
-#else
-    cout << "Cannot load libcec.so" << endl;
-#endif
-    return 1;
-  }
-  CStdString strLog;
-  strLog.Format("CEC Parser created - libcec version %d", parser->GetLibVersion());
-  cout << strLog.c_str() << endl;
-
-  //make stdin non-blocking
-#ifndef __WINDOWS__
-  int flags = fcntl(0, F_GETFL, 0);
-  flags |= O_NONBLOCK;
-  fcntl(0, F_SETFL, flags);
-#endif
+  int16_t iPhysicalAddress = -1;
+  cec_device_type_list typeList;
+  typeList.clear();
 
   int iArgPtr = 1;
+  bool bSingleCommand(false);
   while (iArgPtr < argc)
   {
     if (argc >= iArgPtr + 1)
@@ -290,7 +286,8 @@ int main (int argc, char *argv[])
           if (iNewLevel >= CEC_LOG_ERROR && iNewLevel <= CEC_LOG_ALL)
           {
             g_cecLogLevel = iNewLevel;
-            cout << "log level set to " << argv[iArgPtr + 1] << endl;
+            if (!bSingleCommand)
+              cout << "log level set to " << argv[iArgPtr + 1] << endl;
           }
           else
           {
@@ -304,43 +301,79 @@ int main (int argc, char *argv[])
           ++iArgPtr;
         }
       }
-      else if (!strcmp(argv[iArgPtr], "-la") ||
-               !strcmp(argv[iArgPtr], "--logical-address"))
+      else if (!strcmp(argv[iArgPtr], "-t") ||
+               !strcmp(argv[iArgPtr], "--type"))
       {
         if (argc >= iArgPtr + 2)
         {
-          int iNewAddress = atoi(argv[iArgPtr + 1]);
-          if (iNewAddress >= 0 && iNewAddress <= 15)
+          if (!strcmp(argv[iArgPtr + 1], "p"))
+          {
+            if (!bSingleCommand)
+              cout << "== using device type 'playback device'" << endl;
+            typeList.add(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
+          }
+          else if (!strcmp(argv[iArgPtr + 1], "r"))
+          {
+            if (!bSingleCommand)
+              cout << "== using device type 'recording device'" << endl;
+            typeList.add(CEC_DEVICE_TYPE_RECORDING_DEVICE);
+          }
+          else if (!strcmp(argv[iArgPtr + 1], "t"))
           {
-            g_iLogicalAddress = iNewAddress;
-            cout << "logical address set to " << argv[iArgPtr + 1] << endl;
+            if (!bSingleCommand)
+              cout << "== using device type 'tuner'" << endl;
+            typeList.add(CEC_DEVICE_TYPE_TUNER);
+          }
+          else if (!strcmp(argv[iArgPtr + 1], "a"))
+          {
+            if (!bSingleCommand)
+              cout << "== using device type 'audio system'" << endl;
+            typeList.add(CEC_DEVICE_TYPE_AUDIO_SYSTEM);
           }
           else
           {
-            cout << "== skipped logical-address parameter: invalid address '" << argv[iArgPtr + 1] << "' ==" << endl;
+            cout << "== skipped invalid device type '" << argv[iArgPtr + 1] << "'" << endl;
           }
-          iArgPtr += 2;
-        }
-        else
-        {
-          cout << "== skipped logical-address parameter: no address given ==" << endl;
           ++iArgPtr;
         }
+        ++iArgPtr;
       }
       else if (!strcmp(argv[iArgPtr], "--list-devices") ||
                !strcmp(argv[iArgPtr], "-l"))
       {
-        list_devices(parser);
-        UnloadLibCec(parser);
+        ICECAdapter *parser = CreateParser(typeList);
+        if (parser)
+        {
+          ListDevices(parser);
+          UnloadLibCec(parser);
+        }
         return 0;
       }
+      else if (!strcmp(argv[iArgPtr], "--single-command") ||
+          !strcmp(argv[iArgPtr], "-s"))
+      {
+        bSingleCommand = true;
+        ++iArgPtr;
+      }
       else if (!strcmp(argv[iArgPtr], "--help") ||
                !strcmp(argv[iArgPtr], "-h"))
       {
-        show_help(argv[0]);
-        UnloadLibCec(parser);
+        ShowHelpCommandLine(argv[0]);
         return 0;
       }
+      else if (!strcmp(argv[iArgPtr], "-p") ||
+               !strcmp(argv[iArgPtr], "--physical"))
+      {
+        if (argc >= iArgPtr + 2)
+        {
+          if (sscanf(argv[iArgPtr + 1], "%x", &iPhysicalAddress))
+            cout << "using physical address '" << std::hex << iPhysicalAddress << "'" << endl;
+          else
+            cout << "== skipped physical address parameter: invalid physical address '" << argv[iArgPtr + 1] << "' ==" << endl;
+          ++iArgPtr;
+        }
+        ++iArgPtr;
+      }
       else
       {
         g_strPort = argv[iArgPtr++];
@@ -348,48 +381,97 @@ int main (int argc, char *argv[])
     }
   }
 
+  if (typeList.IsEmpty())
+  {
+    if (!bSingleCommand)
+      cout << "No device type given. Using 'recording device'" << endl;
+    typeList.add(CEC_DEVICE_TYPE_RECORDING_DEVICE);
+  }
+
+  ICECAdapter *parser = LibCecInit("CECTester", typeList);
+  if (!parser || parser->GetMinLibVersion() > CEC_TEST_CLIENT_VERSION)
+  {
+#ifdef __WINDOWS__
+    cout << "Cannot load libcec.dll" << endl;
+#else
+    cout << "Cannot load libcec.so" << endl;
+#endif
+    return 1;
+  }
+
+  if (!bSingleCommand)
+  {
+    CStdString strLog;
+    strLog.Format("CEC Parser created - libcec version %d.%d", parser->GetLibVersionMajor(), parser->GetLibVersionMinor());
+    cout << strLog.c_str() << endl;
+
+    //make stdin non-blocking
+  #ifndef __WINDOWS__
+    int flags = fcntl(0, F_GETFL, 0);
+    flags |= O_NONBLOCK;
+    fcntl(0, F_SETFL, flags);
+  #endif
+  }
+
   if (g_strPort.IsEmpty())
   {
-    cout << "no serial port given. trying autodetect: ";
+    if (!bSingleCommand)
+      cout << "no serial port given. trying autodetect: ";
     cec_adapter devices[10];
     uint8_t iDevicesFound = parser->FindAdapters(devices, 10, NULL);
     if (iDevicesFound <= 0)
     {
+      if (bSingleCommand)
+        cout << "autodetect ";
       cout << "FAILED" << endl;
       UnloadLibCec(parser);
       return 1;
     }
     else
     {
-      cout << endl << " path:     " << devices[0].path << endl <<
-          " com port: " << devices[0].comm << endl << endl;
+      if (!bSingleCommand)
+      {
+        cout << endl << " path:     " << devices[0].path << endl <<
+            " com port: " << devices[0].comm << endl << endl;
+      }
       g_strPort = devices[0].comm;
     }
   }
 
-  parser->SetLogicalAddress((cec_logical_address) g_iLogicalAddress);
-
   if (!parser->Open(g_strPort.c_str()))
   {
     cout << "unable to open the device on port " << g_strPort << endl;
-    flush_log(parser);
+    FlushLog(parser);
     UnloadLibCec(parser);
     return 1;
   }
 
-  cout << "cec device opened" << endl;
+  if (!bSingleCommand)
+  {
+    cout << "cec device opened" << endl;
 
-  parser->PowerOnDevices(CECDEVICE_TV);
-  flush_log(parser);
+    if (-1 != iPhysicalAddress)
+    {
+      parser->SetPhysicalAddress(iPhysicalAddress);
+      FlushLog(parser);
+    }
 
-  parser->SetActiveView();
-  flush_log(parser);
+    parser->PowerOnDevices(CECDEVICE_TV);
+    FlushLog(parser);
+
+    parser->SetActiveSource();
+    FlushLog(parser);
+
+    cout << "waiting for input" << endl;
+  }
 
   bool bContinue(true);
-  cout << "waiting for input" << endl;
   while (bContinue)
   {
-    flush_log(parser);
+    if (bSingleCommand)
+      bContinue = false;
+
+    FlushLog(parser);
 
     /* just ignore the command buffer and clear it */
     cec_command dummy;
@@ -409,13 +491,13 @@ int main (int argc, char *argv[])
           string strvalue;
           uint8_t ivalue;
           cec_command bytes;
-          bytes.clear();
+          bytes.Clear();
 
           while (GetWord(input, strvalue) && HexStrToInt(strvalue, ivalue))
-            bytes.push_back(ivalue);
+            bytes.PushBack(ivalue);
 
           if (command == "txn")
-            bytes.ack_timeout = 0;
+            bytes.transmit_timeout = 0;
 
           parser->Transmit(bytes);
         }
@@ -445,6 +527,22 @@ int main (int argc, char *argv[])
             cout << "invalid destination" << endl;
           }
         }
+        else if (command == "poll")
+        {
+          string strValue;
+          uint8_t iValue = 0;
+          if (GetWord(input, strValue) && HexStrToInt(strValue, iValue) && iValue <= 0xF)
+          {
+            if (parser->PollDevice((cec_logical_address) iValue))
+              cout << "POLL message sent" << endl;
+            else
+              cout << "POLL message not sent" << endl;
+          }
+          else
+          {
+            cout << "invalid destination" << endl;
+          }
+        }
         else if (command == "la")
         {
           string strvalue;
@@ -517,6 +615,21 @@ int main (int argc, char *argv[])
             }
           }
         }
+        else if (command == "ven")
+        {
+          CStdString strDev;
+          if (GetWord(input, strDev))
+          {
+            int iDev = atoi(strDev);
+            if (iDev >= 0 && iDev < 15)
+            {
+              uint64_t iVendor = parser->GetDeviceVendorId((cec_logical_address) iDev);
+              CStdString strLog;
+              strLog.Format("vendor id: %06x", iVendor);
+              cout << strLog.c_str() << endl;
+            }
+          }
+        }
         else if (command == "ver")
         {
           CStdString strDev;
@@ -547,22 +660,87 @@ int main (int argc, char *argv[])
             }
           }
         }
+        else if (command == "pow")
+        {
+          CStdString strDev;
+          if (GetWord(input, strDev))
+          {
+            int iDev = atoi(strDev);
+            if (iDev >= 0 && iDev < 15)
+            {
+              cec_power_status iPower = parser->GetDevicePowerStatus((cec_logical_address) iDev);
+              switch (iPower)
+              {
+              case CEC_POWER_STATUS_ON:
+                cout << "powered on" << endl;
+                break;
+              case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY:
+                cout << "on -> standby" << endl;
+                break;
+              case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON:
+                cout << "standby -> on" << endl;
+                break;
+              case CEC_POWER_STATUS_STANDBY:
+                cout << "standby" << endl;
+                break;
+              default:
+                cout << "unknown power status" << endl;
+                break;
+              }
+            }
+          }
+        }
+        else if (command == "lad")
+        {
+          cout << "listing active devices:" << endl;
+          cec_logical_addresses addresses = parser->GetActiveDevices();
+          for (unsigned iPtr = 0; iPtr < 16; iPtr++)
+            if (addresses[iPtr])
+              cout << "logical address " << iPtr << endl;
+        }
+        else if (command == "ad")
+        {
+          CStdString strDev;
+          if (GetWord(input, strDev))
+          {
+            int iDev = atoi(strDev);
+            if (iDev >= 0 && iDev < 15)
+              cout << "logical address " << iDev << " is " << (parser->IsActiveDevice((cec_logical_address)iDev) ? "active" : "not active") << endl;
+          }
+        }
+        else if (command == "at")
+        {
+          CStdString strType;
+          if (GetWord(input, strType))
+          {
+            cec_device_type type = CEC_DEVICE_TYPE_TV;
+            if (strType.Equals("a"))
+              type = CEC_DEVICE_TYPE_AUDIO_SYSTEM;
+            else if (strType.Equals("p"))
+              type = CEC_DEVICE_TYPE_PLAYBACK_DEVICE;
+            else if (strType.Equals("r"))
+              type = CEC_DEVICE_TYPE_RECORDING_DEVICE;
+            else if (strType.Equals("t"))
+              type = CEC_DEVICE_TYPE_TUNER;
+            cout << "device " << type << " is " << (parser->IsActiveDeviceType(type) ? "active" : "not active") << endl;
+          }
+        }
         else if (command == "r")
         {
           cout << "closing the connection" << endl;
           parser->Close();
-          flush_log(parser);
+          FlushLog(parser);
 
           cout << "opening a new connection" << endl;
           parser->Open(g_strPort.c_str());
-          flush_log(parser);
+          FlushLog(parser);
 
-          cout << "setting active view" << endl;
-          parser->SetActiveView();
+          cout << "setting active source" << endl;
+          parser->SetActiveSource();
         }
         else if (command == "h" || command == "help")
         {
-          show_console_help();
+          ShowHelpConsole();
         }
         else if (command == "q" || command == "quit")
         {
@@ -585,12 +763,16 @@ int main (int argc, char *argv[])
       if (bContinue)
         cout << "waiting for input" << endl;
     }
-    CCondition::Sleep(50);
+
+    if (bContinue)
+      CCondition::Sleep(50);
   }
 
-  parser->StandbyDevices(CECDEVICE_BROADCAST);
+  if (!bSingleCommand)
+    parser->StandbyDevices(CECDEVICE_BROADCAST);
+
   parser->Close();
-  flush_log(parser);
+  FlushLog(parser);
   UnloadLibCec(parser);
 
   if (g_logOutput.is_open())