added DetectAdapters() method, that returns all device information for detected adapt...
authorLars Op den Kamp <lars@opdenkamp.eu>
Sun, 24 Feb 2013 11:40:26 +0000 (12:40 +0100)
committerLars Op den Kamp <lars@opdenkamp.eu>
Sun, 24 Feb 2013 12:09:24 +0000 (13:09 +0100)
12 files changed:
include/cec.h
include/cecc.h
include/cectypes.h
src/lib/CECProcessor.cpp
src/lib/LibCEC.cpp
src/lib/LibCEC.h
src/lib/LibCECC.cpp
src/lib/adapter/AdapterFactory.cpp
src/lib/adapter/AdapterFactory.h
src/lib/adapter/Pulse-Eight/USBCECAdapterDetection.cpp
src/lib/adapter/Pulse-Eight/USBCECAdapterDetection.h
src/testclient/main.cpp

index 02450331d43c0e365ab32eed44a6e5bbc62e1ca1..8570220e1800220829ee084792f7ef4dc352fab4 100644 (file)
@@ -72,6 +72,7 @@ namespace CEC
     virtual void Close(void) = 0;
 
     /*!
+     * @deprecated Use DetectAdapters() instead
      * @brief Try to find all connected CEC adapters.
      * @param deviceList The vector to store device descriptors in.
      * @param iBufSize The size of the deviceList buffer.
@@ -446,6 +447,16 @@ namespace CEC
      * @return The current audio status, or cec_audio_status if unknown.
      */
     virtual uint8_t AudioStatus(void) = 0;
+
+    /*!
+     * @brief Try to find all connected CEC adapters.
+     * @param deviceList The vector to store device descriptors in.
+     * @param iBufSize The size of the deviceList buffer.
+     * @param strDevicePath Optional device path. Only adds device descriptors that match the given device path.
+     * @return The number of devices that were found, or -1 when an error occured.
+     */
+    virtual int8_t DetectAdapters(cec_adapter_descriptor *deviceList, uint8_t iBufSize, const char *strDevicePath = NULL) = 0;
+
   };
 };
 
index 074a348383b05d1815ff070a38cd524be15c489b..878c5ac71c4df86c6c2d7afabe6b3f081fea23a3 100644 (file)
@@ -284,6 +284,12 @@ extern DECLSPEC uint8_t cec_audio_unmute(void);
 
 extern DECLSPEC uint8_t cec_audio_get_status(void);
 
+#ifdef __cplusplus
+extern DECLSPEC int8_t cec_detect_adapters(CEC::cec_adapter_descriptor *deviceList, uint8_t iBufSize, const char *strDevicePath);
+#else
+extern DECLSPEC int8_t cec_detect_adapters(cec_adapter_descriptor *deviceList, uint8_t iBufSize, const char *strDevicePath);
+#endif
+
 #ifdef __cplusplus
 };
 #endif
index 7b39f841b57a274b707521e1bc9f122e2d91d182..002ff970de8cfe14ceb8852712061ecbb36e90d7 100644 (file)
@@ -883,6 +883,18 @@ typedef struct cec_adapter
   char comm[1024]; /**< the name of the com port */
 } cec_adapter;
 
+typedef struct cec_adapter_descriptor
+{
+  char             strComPath[1024]; /**< the path to the com port */
+  char             strComName[1024]; /**< the name of the com port */
+  uint16_t         iVendorId;
+  uint16_t         iProductId;
+  uint16_t         iFirmwareVersion;
+  uint16_t         iPhysicalAddress;
+  uint32_t         iFirmwareBuildDate;
+  cec_adapter_type adapterType;
+} cec_adapter_descriptor;
+
 typedef struct cec_datapacket
 {
   uint8_t data[100]; /**< the actual data */
index 707b8eb2d6e767a13c527d62b4d23ac21fa803ea..3be1c36cdb52c3996ce092302dd157797b6772dc 100644 (file)
@@ -623,6 +623,8 @@ bool CCECProcessor::GetDeviceInformation(const char *strPort, libcec_configurati
   config->iFirmwareBuildDate = m_communication->GetFirmwareBuildDate();
   config->adapterType        = m_communication->GetAdapterType();
 
+  Close();
+
   return true;
 }
 
index a27ad9dbc45530e02a38ed3a3b408b64db6f9955..fa2195374124c53e486535f2bc8ff4df9c1564d3 100644 (file)
@@ -559,3 +559,18 @@ uint8_t CLibCEC::AudioStatus(void)
 {
   return m_client ? m_client->AudioStatus() : (uint8_t)CEC_AUDIO_VOLUME_STATUS_UNKNOWN;
 }
+
+int8_t CLibCEC::DetectAdapters(cec_adapter_descriptor *deviceList, uint8_t iBufSize, const char *strDevicePath /* = NULL */)
+{
+  int8_t iAdaptersFound = CAdapterFactory(this).DetectAdapters(deviceList, iBufSize, strDevicePath);
+  for (int8_t iPtr = 0; iPtr < iAdaptersFound; iPtr++)
+  {
+    libcec_configuration config;
+    GetDeviceInformation(deviceList[iPtr].strComName, &config);
+    deviceList[iPtr].iFirmwareVersion   = config.iFirmwareVersion;
+    deviceList[iPtr].iPhysicalAddress   = config.iPhysicalAddress;
+    deviceList[iPtr].iFirmwareBuildDate = config.iFirmwareBuildDate;
+    deviceList[iPtr].adapterType        = config.adapterType;
+  }
+  return iAdaptersFound;
+}
index f65b8186b9e843d2917fa95e98be62b891a1b2a5..2f3ba51f346c0b34d7f698e432cd740df0fc5212 100644 (file)
@@ -52,6 +52,7 @@ namespace CEC
       void Close(void);
       bool EnableCallbacks(void *cbParam, ICECCallbacks *callbacks);
       int8_t FindAdapters(cec_adapter *deviceList, uint8_t iBufSize, const char *strDevicePath = NULL);
+      int8_t DetectAdapters(cec_adapter_descriptor *deviceList, uint8_t iBufSize, const char *strDevicePath = NULL);
       bool PingAdapter(void);
       bool StartBootloader(void);
 
index 433bef7bf8e76216e7aade46c77c5c6feafb543e..05e324a65873b5d250ad28e7a9685188914a1694 100644 (file)
@@ -408,4 +408,11 @@ uint8_t cec_audio_get_status(void)
   return cec_parser ? cec_parser->AudioStatus() : (uint8_t)CEC_AUDIO_VOLUME_STATUS_UNKNOWN;
 }
 
+int8_t cec_detect_adapters(cec_adapter_descriptor *deviceList, uint8_t iBufSize, const char *strDevicePath /* = NULL */)
+{
+  if (cec_parser)
+    return cec_parser->DetectAdapters(deviceList, iBufSize, strDevicePath);
+  return -1;
+}
+
 //@}
index 02b6200e1616674964f34af5459cc9955ffe6a22..1596cfd1aeebb07d414e8eb74eba9e692555b628 100644 (file)
@@ -56,6 +56,18 @@ using namespace std;
 using namespace CEC;
 
 int8_t CAdapterFactory::FindAdapters(cec_adapter *deviceList, uint8_t iBufSize, const char *strDevicePath /* = NULL */)
+{
+  cec_adapter_descriptor devices[iBufSize];
+  int8_t iReturn = DetectAdapters(devices, iBufSize, strDevicePath);
+  for (int8_t iPtr = 0; iPtr < iReturn; iPtr++)
+  {
+    strncpy(deviceList[iPtr].comm, devices[iPtr].strComName, sizeof(deviceList[iPtr].comm));
+    strncpy(deviceList[iPtr].path, devices[iPtr].strComPath, sizeof(deviceList[iPtr].path));
+  }
+  return iReturn;
+}
+
+int8_t CAdapterFactory::DetectAdapters(cec_adapter_descriptor *deviceList, uint8_t iBufSize, const char *strDevicePath /* = NULL */)
 {
   int8_t iAdaptersFound(0);
 
@@ -75,8 +87,11 @@ int8_t CAdapterFactory::FindAdapters(cec_adapter *deviceList, uint8_t iBufSize,
   if (iAdaptersFound < iBufSize && CRPiCECAdapterDetection::FindAdapter() &&
       (!strDevicePath || !strcmp(strDevicePath, CEC_RPI_VIRTUAL_COM)))
   {
-    snprintf(deviceList[iAdaptersFound].path, 1024, CEC_RPI_VIRTUAL_PATH);
-    snprintf(deviceList[iAdaptersFound++].comm, 1024, CEC_RPI_VIRTUAL_COM);
+    snprintf(deviceList[iAdaptersFound].strComPath, sizeof(deviceList[iAdaptersFound].strComPath), CEC_RPI_VIRTUAL_PATH);
+    snprintf(deviceList[iAdaptersFound].strComName, sizeof(deviceList[iAdaptersFound].strComName), CEC_RPI_VIRTUAL_COM);
+    deviceList[iAdaptersFound].iVendorId = RPI_ADAPTER_VID;
+    deviceList[iAdaptersFound].iProductId = RPI_ADAPTER_PID;
+    iAdaptersFound++;
   }
 #endif
 
@@ -84,8 +99,11 @@ int8_t CAdapterFactory::FindAdapters(cec_adapter *deviceList, uint8_t iBufSize,
   if (iAdaptersFound < iBufSize && CTDA995xCECAdapterDetection::FindAdapter() &&
       (!strDevicePath || !strcmp(strDevicePath, CEC_TDA995x_VIRTUAL_COM)))
   {
-    snprintf(deviceList[iAdaptersFound].path, 1024, CEC_TDA995x_PATH);
-    snprintf(deviceList[iAdaptersFound++].comm, 1024, CEC_TDA995x_VIRTUAL_COM);
+    snprintf(deviceList[iAdaptersFound].path, sizeof(deviceList[iAdaptersFound].strComPath), CEC_TDA995x_PATH);
+    snprintf(deviceList[iAdaptersFound].comm, sizeof(deviceList[iAdaptersFound].strComName), CEC_TDA995x_VIRTUAL_COM);
+    deviceList[iAdaptersFound].iVendorId = TDA995X_ADAPTER_VID;
+    deviceList[iAdaptersFound].iProductId = TDA995X_ADAPTER_PID;
+    iAdaptersFound++;
   }
 #endif
 
index d8a13602da346f6158ac42d5554d5d20277e8685..4cab3d19a5f71e54f7dfe0a8f868f004089bcc89 100644 (file)
@@ -47,6 +47,7 @@ namespace CEC
     virtual ~CAdapterFactory(void) {};
 
     int8_t FindAdapters(cec_adapter *deviceList, uint8_t iBufSize, const char *strDevicePath = NULL);
+    int8_t DetectAdapters(cec_adapter_descriptor *deviceList, uint8_t iBufSize, const char *strDevicePath = NULL);
     IAdapterCommunication *GetInstance(const char *strPort, uint16_t iBaudRate = CEC_SERIAL_DEFAULT_BAUDRATE);
 
     static void InitVideoStandalone(void);
index 433a3d325df954e29fdf698e998cd451f2f0b68c..1331d7c044a8ba1943aab3ad26c21c8b5c9e249e 100644 (file)
@@ -202,7 +202,7 @@ static bool FindComPortForComposite(const char* strLocation, char* strPortName,
 }
 #endif
 
-uint8_t CUSBCECAdapterDetection::FindAdapters(cec_adapter *deviceList, uint8_t iBufSize, const char *strDevicePath /* = NULL */)
+uint8_t CUSBCECAdapterDetection::FindAdapters(cec_adapter_descriptor *deviceList, uint8_t iBufSize, const char *strDevicePath /* = NULL */)
 {
   uint8_t iFound(0);
 
@@ -262,10 +262,12 @@ uint8_t CUSBCECAdapterDetection::FindAdapters(cec_adapter *deviceList, uint8_t i
             if (!strDevicePath || !strcmp(bsdPath, strDevicePath))
             {
               // on darwin, the device path is the same as the comm path.
-              if (iFound == 0 || strcmp(deviceList[iFound-1].comm, bsdPath))
+              if (iFound == 0 || strcmp(deviceList[iFound-1].strComName, bsdPath))
               {
-                snprintf(deviceList[iFound].path, sizeof(deviceList[iFound].path), "%s", bsdPath);
-                snprintf(deviceList[iFound].comm, sizeof(deviceList[iFound].path), "%s", bsdPath);
+                snprintf(deviceList[iFound].strComPath, sizeof(deviceList[iFound].strComPath), "%s", bsdPath);
+                snprintf(deviceList[iFound].strComName, sizeof(deviceList[iFound].strComName), "%s", bsdPath);
+                deviceList[iFound].iVendorId = iVendor;
+                deviceList[iFound].iProductId = iProduct;
                 iFound++;
               }
             }
@@ -312,10 +314,12 @@ uint8_t CUSBCECAdapterDetection::FindAdapters(cec_adapter *deviceList, uint8_t i
       if (!strDevicePath || !strcmp(strPath.c_str(), strDevicePath))
       {
         CStdString strComm(strPath);
-        if (FindComPort(strComm) && (iFound == 0 || strcmp(deviceList[iFound-1].comm, strComm.c_str())))
+        if (FindComPort(strComm) && (iFound == 0 || strcmp(deviceList[iFound-1].strComName, strComm.c_str())))
         {
-          snprintf(deviceList[iFound].path, sizeof(deviceList[iFound].path), "%s", strPath.c_str());
-          snprintf(deviceList[iFound].comm, sizeof(deviceList[iFound].path), "%s", strComm.c_str());
+          snprintf(deviceList[iFound].strComPath, sizeof(deviceList[iFound].strComPath), "%s", strPath.c_str());
+          snprintf(deviceList[iFound].strComName, sizeof(deviceList[iFound].strComName), "%s", strComm.c_str());
+          deviceList[iFound].iVendorId = iVendor;
+          deviceList[iFound].iProductId = iProduct;
           iFound++;
         }
       }
@@ -408,15 +412,19 @@ uint8_t CUSBCECAdapterDetection::FindAdapters(cec_adapter *deviceList, uint8_t i
       // the 1002 pid indicates a composite device, that needs special treatment
       char strId[512];
       CM_Get_Device_ID(devInfoData.DevInst, strId, 512, 0);
-      if (FindComPortForComposite(strId, deviceList[iFound].comm, sizeof(deviceList[iFound].comm)))
+      if (FindComPortForComposite(strId, deviceList[iFound].strComName, sizeof(deviceList[iFound].strComName)))
       {
-        snprintf(deviceList[iFound].path, sizeof(deviceList[iFound].path), "%s", devicedetailData->DevicePath);
+        snprintf(deviceList[iFound].strComPath, sizeof(deviceList[iFound].strComPath), "%s", devicedetailData->DevicePath);
+        deviceList[iFound].iVendorId = iVendor;
+        deviceList[iFound].iProductId = iProduct;
         iFound++;
       }
     }
-    else if (GetComPortFromHandle(hDevHandle, &devInfoData, deviceList[iFound].comm, sizeof(deviceList[iFound].comm)))
+    else if (GetComPortFromHandle(hDevHandle, &devInfoData, deviceList[iFound].strComName, sizeof(deviceList[iFound].strComName)))
     {
-      snprintf(deviceList[iFound].path, sizeof(deviceList[iFound].path), "%s", devicedetailData->DevicePath);
+      snprintf(deviceList[iFound].strComPath, sizeof(deviceList[iFound].strComPath), "%s", devicedetailData->DevicePath);
+      deviceList[iFound].iVendorId = iVendor;
+      deviceList[iFound].iProductId = iProduct;
       iFound++;
     }
   }
@@ -431,8 +439,10 @@ uint8_t CUSBCECAdapterDetection::FindAdapters(cec_adapter *deviceList, uint8_t i
       continue;
     if (!access(devicePath, 0))
     {
-      snprintf(deviceList[iFound].path, sizeof(deviceList[iFound].path), "%s", devicePath);
-      snprintf(deviceList[iFound].comm, sizeof(deviceList[iFound].path), "%s", devicePath);
+      snprintf(deviceList[iFound].strComPath, sizeof(deviceList[iFound].strComPath), "%s", devicePath);
+      snprintf(deviceList[iFound].strComName, sizeof(deviceList[iFound].strComName), "%s", devicePath);
+      deviceList[iFound].iVendorId = CEC_VID;
+      deviceList[iFound].iProductId = CEC_VID;
       iFound++;
     }
   }
index 37478b9632036770d6e480537c94252d074d9ad6..5640881a3f8de2cda6369990dd3e0a5c93956225 100644 (file)
@@ -40,7 +40,7 @@ namespace CEC
   class CUSBCECAdapterDetection
   {
   public:
-    static uint8_t FindAdapters(cec_adapter *deviceList, uint8_t iBufSize, const char *strDevicePath = NULL);
+    static uint8_t FindAdapters(cec_adapter_descriptor *deviceList, uint8_t iBufSize, const char *strDevicePath = NULL);
     static bool    CanAutodetect(void);
   };
 };
index c6082921bf1844c22b70f86f114f211bd3cf7c67..df565b52d90eec41ffa1b4d4d9b2cf5ffa233298 100644 (file)
@@ -197,45 +197,41 @@ int CecAlert(void *UNUSED(cbParam), const libcec_alert type, const libcec_parame
 
 void ListDevices(ICECAdapter *parser)
 {
-  cec_adapter *devices = new cec_adapter[10];
-  int8_t iDevicesFound = parser->FindAdapters(devices, 10, NULL);
+  cec_adapter_descriptor devices[10];
+  int8_t iDevicesFound = parser->DetectAdapters(devices, 10, NULL);
   if (iDevicesFound <= 0)
   {
     PrintToStdOut("Found devices: NONE");
   }
   else
   {
-    CStdString strDeviceInfo;
-    strDeviceInfo.Format("Found devices: %d\n\n", iDevicesFound);
+    PrintToStdOut("Found devices: %d\n", iDevicesFound);
 
     for (int8_t iDevicePtr = 0; iDevicePtr < iDevicesFound; iDevicePtr++)
     {
-      strDeviceInfo.AppendFormat("device:              %d\ncom port:            %s\n", iDevicePtr + 1, devices[iDevicePtr].comm);
-      libcec_configuration config;
-      config.Clear();
+      PrintToStdOut("device:              %d", iDevicePtr + 1);
+      PrintToStdOut("com port:            %s", devices[iDevicePtr].strComName);
+      PrintToStdOut("vendor id:           %04x", devices[iDevicePtr].iVendorId);
+      PrintToStdOut("product id:          %04x", devices[iDevicePtr].iProductId);
+      PrintToStdOut("firmware version:    %d", devices[iDevicePtr].iFirmwareVersion);
 
-      if (!parser->GetDeviceInformation(devices[iDevicePtr].comm, &config))
-        PrintToStdOut("WARNING: unable to open the device on port %s", devices[iDevicePtr].comm);
-      else
+      if (devices[iDevicePtr].iFirmwareBuildDate != CEC_FW_BUILD_UNKNOWN)
       {
-        strDeviceInfo.AppendFormat("firmware version:    %d\n", config.iFirmwareVersion);
-
-        if (config.iFirmwareBuildDate != CEC_FW_BUILD_UNKNOWN)
-        {
-          time_t buildTime = (time_t)config.iFirmwareBuildDate;
-          strDeviceInfo.AppendFormat("firmware build date: %s", asctime(gmtime(&buildTime)));
-          strDeviceInfo = strDeviceInfo.Left(strDeviceInfo.length() > 1 ? (unsigned)(strDeviceInfo.length() - 1) : 0); // strip \n added by asctime
-          strDeviceInfo.append(" +0000\n");
-        }
+        time_t buildTime = (time_t)devices[iDevicePtr].iFirmwareBuildDate;
+        CStdString strDeviceInfo;
+        strDeviceInfo.AppendFormat("firmware build date: %s", asctime(gmtime(&buildTime)));
+        strDeviceInfo = strDeviceInfo.Left(strDeviceInfo.length() > 1 ? (unsigned)(strDeviceInfo.length() - 1) : 0); // strip \n added by asctime
+        strDeviceInfo.append(" +0000");
+        PrintToStdOut(strDeviceInfo.c_str());
+      }
 
-        if (config.adapterType != ADAPTERTYPE_UNKNOWN)
-        {
-          strDeviceInfo.AppendFormat("type:                %s\n", parser->ToString(config.adapterType));
-        }
+      if (devices[iDevicePtr].adapterType != ADAPTERTYPE_UNKNOWN)
+      {
+        PrintToStdOut("type:                %s", parser->ToString(devices[iDevicePtr].adapterType));
       }
-      strDeviceInfo.append("\n");
+
+      PrintToStdOut("");
     }
-    PrintToStdOut(strDeviceInfo.c_str());
   }
 }