libcec v0.5 (WIP)
[deb_libcec.git] / src / testclient / main.cpp
index deabed74e54d81594857696a7e13b35cdb101486..bec5c4d1655dd597ba03b39ad396df66c6f408ba 100644 (file)
  */
 
 #include "../../include/CECExports.h"
-#include "../lib/util/threads.h"
-#include "../lib/util/misc.h"
+#include "../lib/platform/threads.h"
 #include "../lib/util/StdString.h"
 #include <cstdio>
 #include <fcntl.h>
 #include <iostream>
 #include <string>
+#include <sstream>
 
 using namespace CEC;
 using namespace std;
 
-#define CEC_TEST_CLIENT_VERSION 2
+#define CEC_TEST_CLIENT_VERSION 5
 
-void flush_log(ICECDevice *cecParser)
+
+inline bool HexStrToInt(const std::string& data, uint8_t& value)
+{
+  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
+bool GetWord(string& data, string& word)
+{
+  stringstream datastream(data);
+  string end;
+
+  datastream >> word;
+  if (datastream.fail())
+  {
+    data.clear();
+    return false;
+  }
+
+  size_t pos = data.find(word) + word.length();
+
+  if (pos >= data.length())
+  {
+    data.clear();
+    return true;
+  }
+
+  data = data.substr(pos);
+
+  datastream.clear();
+  datastream.str(data);
+
+  datastream >> end;
+  if (datastream.fail())
+    data.clear();
+
+  return true;
+}
+
+void flush_log(ICECAdapter *cecParser)
 {
   cec_log_message message;
   while (cecParser && cecParser->GetNextLogMessage(&message))
@@ -52,41 +105,38 @@ void flush_log(ICECDevice *cecParser)
     switch (message.level)
     {
     case CEC_LOG_ERROR:
-      cout << "ERROR:   " << message.message.c_str() << endl;
+      cout << "ERROR:   " << message.message << endl;
       break;
     case CEC_LOG_WARNING:
-      cout << "WARNING: " << message.message.c_str() << endl;
+      cout << "WARNING: " << message.message << endl;
       break;
     case CEC_LOG_NOTICE:
-      cout << "NOTICE:  " << message.message.c_str() << endl;
+      cout << "NOTICE:  " << message.message << endl;
       break;
     case CEC_LOG_DEBUG:
-      cout << "DEBUG:   " << message.message.c_str() << endl;
+      cout << "DEBUG:   " << message.message << endl;
       break;
     }
   }
 }
 
-void list_devices(ICECDevice *parser)
+void list_devices(ICECAdapter *parser)
 {
-  cout << "Found devices: ";
-  vector<cec_device> devices;
-  int iDevicesFound = parser->FindDevices(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;
     }
   }
@@ -114,9 +164,6 @@ 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 <<
-  "am {ackmack}              change the ackmask of the CEC adapter." << endl <<
-  "[am 10]                   ackmask 0x10 (logical address 4)" << endl <<
-  endl <<
   "la {logical_address}      change the logical address of the CEC adapter." << endl <<
   "[la 4]                    logical address 4" << endl <<
   endl <<
@@ -129,7 +176,7 @@ void show_console_help(void)
 
 int main (int argc, char *argv[])
 {
-  ICECDevice *parser = LoadLibCec("CEC Tester");
+  ICECAdapter *parser = LoadLibCec("CEC Tester");
   if (!parser && parser->GetMinVersion() > CEC_TEST_CLIENT_VERSION)
   {
     cout << "Unable to create parser. Is libcec.dll present?" << endl;
@@ -150,8 +197,8 @@ int main (int argc, char *argv[])
   if (argc < 2)
   {
     cout << "no serial port given. trying autodetect: ";
-    vector<cec_device> devices;
-    int iDevicesFound = parser->FindDevices(devices);
+    cec_adapter devices[10];
+    uint8_t iDevicesFound = parser->FindAdapters(devices, 10, NULL);
     if (iDevicesFound <= 0)
     {
       cout << "FAILED" << endl;
@@ -190,7 +237,6 @@ int main (int argc, char *argv[])
   }
 
   cout << "cec device opened" << endl;
-  usleep(CEC_SETTLE_DOWN_TIME);
 
   parser->PowerOnDevices(CECDEVICE_TV);
   flush_log(parser);
@@ -216,22 +262,15 @@ int main (int argc, char *argv[])
         if (command == "tx")
         {
           string strvalue;
-          int    ivalue;
-          vector<uint8_t> bytes;
+          uint8_t ivalue;
+          cec_frame bytes;
+          bytes.clear();
+
           while (GetWord(input, strvalue) && HexStrToInt(strvalue, ivalue))
           bytes.push_back(ivalue);
 
           parser->Transmit(bytes);
         }
-        else if (command == "am")
-        {
-          string strvalue;
-          int    ackmask;
-          if (GetWord(input, strvalue) && HexStrToInt(strvalue, ackmask))
-          {
-            parser->SetAckMask(ackmask);
-          }
-        }
         else if (command == "la")
         {
           string strvalue;
@@ -242,12 +281,25 @@ int main (int argc, char *argv[])
         }
         else if (command == "ping")
         {
-          parser->Ping();
+          parser->PingAdapter();
         }
         else if (command == "bl")
         {
           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();
@@ -263,7 +315,8 @@ int main (int argc, char *argv[])
     CCondition::Sleep(50);
   }
 
-  parser->PowerOffDevices(CECDEVICE_TV);
+  parser->StandbyDevices(CECDEVICE_BROADCAST);
+  parser->Close();
   flush_log(parser);
   UnloadLibCec(parser);
   return 0;