2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
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.
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.
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.
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
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/
33 #include "AdapterDetection.h"
34 #include "platform/os-dependent.h"
35 #include "util/StdString.h"
37 #if defined(__APPLE__)
39 #include <sys/param.h>
40 #include <IOKit/IOKitLib.h>
41 #include <IOKit/IOMessage.h>
42 #include <IOKit/IOCFPlugIn.h>
43 #include <IOKit/usb/IOUSBLib.h>
44 #include <IOKit/serial/IOSerialKeys.h>
45 #include <CoreFoundation/CoreFoundation.h>
47 #elif !defined(__WINDOWS__)
54 // the virtual COM port only shows up when requesting devices with the raw device guid!
55 static GUID USB_RAW_GUID
= { 0xA5DCBF10, 0x6530, 0x11D2, { 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED } };
58 #define CEC_VID 0x2548
59 #define CEC_PID 0x1001
64 #if !defined(__WINDOWS__)
65 bool TranslateComPort(CStdString
&strString
)
67 CStdString
strTmp(strString
);
69 int iSlash
= strTmp
.Find('/');
72 strTmp
= strTmp
.Left(iSlash
);
74 strString
.Format("%s/%s:1.0/tty", strString
.c_str(), strTmp
.c_str());
81 bool FindComPort(CStdString
&strLocation
)
83 CStdString strPort
= strLocation
;
84 bool bReturn(!strPort
.IsEmpty());
85 CStdString
strConfigLocation(strLocation
);
86 if (TranslateComPort(strConfigLocation
))
89 struct dirent
*dirent
;
90 if((dir
= opendir(strConfigLocation
.c_str())) == NULL
)
93 while ((dirent
= readdir(dir
)) != NULL
)
95 if(strcmp((char*)dirent
->d_name
, "." ) != 0 && strcmp((char*)dirent
->d_name
, ".." ) != 0)
97 strPort
.Format("/dev/%s", dirent
->d_name
);
98 if (!strPort
.IsEmpty())
100 strLocation
= strPort
;
113 uint8_t CAdapterDetection::FindAdapters(cec_adapter
*deviceList
, uint8_t iBufSize
, const char *strDevicePath
/* = NULL */)
117 #if defined(__APPLE__)
118 kern_return_t kresult
;
119 char bsdPath
[MAXPATHLEN
] = {0};
120 io_iterator_t serialPortIterator
;
122 CFMutableDictionaryRef classesToMatch
= IOServiceMatching(kIOSerialBSDServiceValue
);
125 CFDictionarySetValue(classesToMatch
, CFSTR(kIOSerialBSDTypeKey
), CFSTR(kIOSerialBSDModemType
));
126 kresult
= IOServiceGetMatchingServices(kIOMasterPortDefault
, classesToMatch
, &serialPortIterator
);
127 if (kresult
== KERN_SUCCESS
)
129 io_object_t serialService
;
130 while ((serialService
= IOIteratorNext(serialPortIterator
)))
132 int iVendor
= 0, iProduct
= 0;
133 CFTypeRef bsdPathAsCFString
;
135 // fetch the device path.
136 bsdPathAsCFString
= IORegistryEntryCreateCFProperty(serialService
,
137 CFSTR(kIOCalloutDeviceKey
), kCFAllocatorDefault
, 0);
138 if (bsdPathAsCFString
)
140 // convert the path from a CFString to a C (NUL-terminated) string.
141 CFStringGetCString((CFStringRef
)bsdPathAsCFString
, bsdPath
, MAXPATHLEN
- 1, kCFStringEncodingUTF8
);
142 CFRelease(bsdPathAsCFString
);
144 // now walk up the hierarchy until we find the entry with vendor/product IDs
145 io_registry_entry_t parent
;
146 CFTypeRef vendorIdAsCFNumber
= NULL
;
147 CFTypeRef productIdAsCFNumber
= NULL
;
148 kern_return_t kresult
= IORegistryEntryGetParentEntry(serialService
, kIOServicePlane
, &parent
);
149 while (kresult
== KERN_SUCCESS
)
151 vendorIdAsCFNumber
= IORegistryEntrySearchCFProperty(parent
,
152 kIOServicePlane
, CFSTR(kUSBVendorID
), kCFAllocatorDefault
, 0);
153 productIdAsCFNumber
= IORegistryEntrySearchCFProperty(parent
,
154 kIOServicePlane
, CFSTR(kUSBProductID
), kCFAllocatorDefault
, 0);
155 if (vendorIdAsCFNumber
&& productIdAsCFNumber
)
157 CFNumberGetValue((CFNumberRef
)vendorIdAsCFNumber
, kCFNumberIntType
, &iVendor
);
158 CFRelease(vendorIdAsCFNumber
);
159 CFNumberGetValue((CFNumberRef
)productIdAsCFNumber
, kCFNumberIntType
, &iProduct
);
160 CFRelease(productIdAsCFNumber
);
161 IOObjectRelease(parent
);
164 io_registry_entry_t oldparent
= parent
;
165 kresult
= IORegistryEntryGetParentEntry(parent
, kIOServicePlane
, &parent
);
166 IOObjectRelease(oldparent
);
168 if (strlen(bsdPath
) && iVendor
== CEC_VID
&& iProduct
== CEC_PID
)
170 if (!strDevicePath
|| !strcmp(bsdPath
, strDevicePath
))
172 // on darwin, the device path is the same as the comm path.
173 snprintf(deviceList
[iFound
].path
, sizeof(deviceList
[iFound
].path
), "%s", bsdPath
);
174 snprintf(deviceList
[iFound
++].comm
, sizeof(deviceList
[iFound
].path
), "%s", bsdPath
);
178 IOObjectRelease(serialService
);
181 IOObjectRelease(serialPortIterator
);
184 #elif !defined(__WINDOWS__)
186 if (!(udev
= udev_new()))
189 struct udev_enumerate
*enumerate
;
190 struct udev_list_entry
*devices
, *dev_list_entry
;
191 struct udev_device
*dev
, *pdev
;
192 enumerate
= udev_enumerate_new(udev
);
193 udev_enumerate_scan_devices(enumerate
);
194 devices
= udev_enumerate_get_list_entry(enumerate
);
195 udev_list_entry_foreach(dev_list_entry
, devices
)
198 strPath
= udev_list_entry_get_name(dev_list_entry
);
200 dev
= udev_device_new_from_syspath(udev
, strPath
);
204 pdev
= udev_device_get_parent(udev_device_get_parent(dev
));
205 if (!pdev
|| !udev_device_get_sysattr_value(pdev
,"idVendor") || !udev_device_get_sysattr_value(pdev
, "idProduct"))
207 udev_device_unref(dev
);
211 int iVendor
, iProduct
;
212 sscanf(udev_device_get_sysattr_value(pdev
, "idVendor"), "%x", &iVendor
);
213 sscanf(udev_device_get_sysattr_value(pdev
, "idProduct"), "%x", &iProduct
);
214 if (iVendor
== CEC_VID
&& iProduct
== CEC_PID
)
216 CStdString
strPath(udev_device_get_syspath(pdev
));
217 if (!strDevicePath
|| !strcmp(strPath
.c_str(), strDevicePath
))
219 CStdString
strComm(strPath
);
220 if (FindComPort(strComm
))
222 snprintf(deviceList
[iFound
].path
, sizeof(deviceList
[iFound
].path
), "%s", strPath
.c_str());
223 snprintf(deviceList
[iFound
++].comm
, sizeof(deviceList
[iFound
].path
), "%s", strComm
.c_str());
227 udev_device_unref(dev
);
230 udev_enumerate_unref(enumerate
);
234 DWORD required
= 0, iMemberIndex
= 0;
237 SP_DEVICE_INTERFACE_DATA deviceInterfaceData
;
238 deviceInterfaceData
.cbSize
= sizeof(SP_DEVICE_INTERFACE_DATA
);
240 SP_DEVINFO_DATA devInfoData
;
241 devInfoData
.cbSize
= sizeof(SP_DEVINFO_DATA
);
243 if ((hDevHandle
= SetupDiGetClassDevs(&USB_RAW_GUID
, 0, 0, DIGCF_PRESENT
| DIGCF_DEVICEINTERFACE
)) == INVALID_HANDLE_VALUE
)
247 TCHAR
*buffer
= NULL
;
248 PSP_DEVICE_INTERFACE_DETAIL_DATA devicedetailData
;
249 while(bResult
&& iFound
< iBufSize
)
251 bResult
= SetupDiEnumDeviceInfo(hDevHandle
, iMemberIndex
, &devInfoData
);
254 bResult
= SetupDiEnumDeviceInterfaces(hDevHandle
, 0, &USB_RAW_GUID
, iMemberIndex
, &deviceInterfaceData
);
258 SetupDiDestroyDeviceInfoList(hDevHandle
);
265 BOOL bDetailResult
= false;
267 // As per MSDN, Get the required buffer size. Call SetupDiGetDeviceInterfaceDetail with a
268 // NULL DeviceInterfaceDetailData pointer, a DeviceInterfaceDetailDataSize of zero,
269 // and a valid RequiredSize variable. In response to such a call, this function returns
270 // the required buffer size at RequiredSize and fails with GetLastError returning
271 // ERROR_INSUFFICIENT_BUFFER.
272 // Allocate an appropriately sized buffer and call the function again to get the interface details.
274 SetupDiGetDeviceInterfaceDetail(hDevHandle
, &deviceInterfaceData
, NULL
, 0, &required
, NULL
);
276 buffer
= new TCHAR
[required
];
277 devicedetailData
= (PSP_DEVICE_INTERFACE_DETAIL_DATA
) buffer
;
278 devicedetailData
->cbSize
= sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA
);
279 nBufferSize
= required
;
282 bDetailResult
= SetupDiGetDeviceInterfaceDetail(hDevHandle
, &deviceInterfaceData
, devicedetailData
, nBufferSize
, &required
, NULL
);
286 if (strDevicePath
&& strcmp(strDevicePath
, devicedetailData
->DevicePath
) != 0)
289 CStdString strVendorId
;
290 CStdString strProductId
;
291 CStdString
strTmp(devicedetailData
->DevicePath
);
292 strVendorId
.assign(strTmp
.substr(strTmp
.Find("vid_") + 4, 4));
293 strProductId
.assign(strTmp
.substr(strTmp
.Find("pid_") + 4, 4));
294 if (strTmp
.Find("&mi_") >= 0 && strTmp
.Find("&mi_00") < 0)
297 int iVendor
, iProduct
;
298 sscanf(strVendorId
, "%x", &iVendor
);
299 sscanf(strProductId
, "%x", &iProduct
);
300 if (iVendor
!= CEC_VID
|| iProduct
!= CEC_PID
)
303 HKEY hDeviceKey
= SetupDiOpenDevRegKey(hDevHandle
, &devInfoData
, DICS_FLAG_GLOBAL
, 0, DIREG_DEV
, KEY_QUERY_VALUE
);
307 TCHAR strPortName
[256];
308 strPortName
[0] = _T('\0');
309 DWORD dwSize
= sizeof(strPortName
);
312 /* search the registry */
313 if ((RegQueryValueEx(hDeviceKey
, _T("PortName"), NULL
, &dwType
, reinterpret_cast<LPBYTE
>(strPortName
), &dwSize
) == ERROR_SUCCESS
) && (dwType
== REG_SZ
))
315 if (_tcslen(strPortName
) > 3 && _tcsnicmp(strPortName
, _T("COM"), 3) == 0 &&
316 _ttoi(&(strPortName
[3])) > 0)
318 snprintf(deviceList
[iFound
].path
, sizeof(deviceList
[iFound
].path
), "%s", devicedetailData
->DevicePath
);
319 snprintf(deviceList
[iFound
++].comm
, sizeof(deviceList
[iFound
].path
), "%s", strPortName
);
323 RegCloseKey(hDeviceKey
);