Commit | Line | Data |
---|---|---|
abbca718 LOK |
1 | /* |
2 | * This file is part of the libCEC(R) library. | |
3 | * | |
4 | * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved. | |
5 | * libCEC(R) is an original work, containing original code. | |
6 | * | |
7 | * libCEC(R) is a trademark of Pulse-Eight Limited. | |
8 | * | |
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. | |
13 | * | |
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. | |
18 | * | |
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. | |
22 | * | |
23 | * | |
24 | * Alternatively, you can license this library under a commercial license, | |
25 | * please contact Pulse-Eight Licensing for more information. | |
26 | * | |
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/ | |
31 | */ | |
32 | ||
33 | #include "CECDetect.h" | |
34 | #include "libPlatform/os-dependent.h" | |
35 | #include "util/StdString.h" | |
36 | #if !defined(__WINDOWS__) | |
37 | #include <dirent.h> | |
38 | #include <libudev.h> | |
39 | #include <poll.h> | |
40 | #endif | |
41 | #include <string.h> | |
42 | ||
43 | #define CEC_VID 0x2548 | |
44 | #define CEC_PID 0x1001 | |
45 | ||
46 | using namespace CEC; | |
47 | using namespace std; | |
48 | ||
49 | #if !defined(__WINDOWS__) | |
50 | bool TranslateComPort(CStdString &strString) | |
51 | { | |
52 | CStdString strTmp(strString); | |
53 | strTmp.MakeReverse(); | |
54 | int iSlash = strTmp.Find('/'); | |
55 | if (iSlash >= 0) | |
56 | { | |
57 | strTmp = strTmp.Left(iSlash); | |
58 | strTmp.MakeReverse(); | |
59 | strString.Format("%s/%s:1.0/tty", strString.c_str(), strTmp.c_str()); | |
60 | return true; | |
61 | } | |
62 | ||
63 | return false; | |
64 | } | |
65 | ||
66 | bool FindComPort(CStdString &strLocation) | |
67 | { | |
68 | CStdString strPort = strLocation; | |
69 | bool bReturn(!strPort.IsEmpty()); | |
70 | CStdString strConfigLocation(strLocation); | |
71 | if (TranslateComPort(strConfigLocation)) | |
72 | { | |
73 | DIR *dir; | |
74 | struct dirent *dirent; | |
75 | if((dir = opendir(strConfigLocation.c_str())) == NULL) | |
76 | return bReturn; | |
77 | ||
78 | while ((dirent = readdir(dir)) != NULL) | |
79 | { | |
80 | if(strcmp((char*)dirent->d_name, "." ) != 0 && strcmp((char*)dirent->d_name, ".." ) != 0) | |
81 | { | |
82 | strPort.Format("/dev/%s", dirent->d_name); | |
83 | if (!strPort.IsEmpty()) | |
84 | { | |
85 | strLocation = strPort; | |
86 | bReturn = true; | |
87 | break; | |
88 | } | |
89 | } | |
90 | } | |
91 | closedir(dir); | |
92 | } | |
93 | ||
94 | return bReturn; | |
95 | } | |
96 | #endif | |
97 | ||
98 | int CCECDetect::FindDevices(vector<cec_device> &deviceList, const char *strDevicePath /* = NULL */) | |
99 | { | |
100 | int iFound(0); | |
101 | ||
102 | #if !defined(__WINDOWS__) | |
103 | struct udev *udev; | |
104 | if (!(udev = udev_new())) | |
105 | return -1; | |
106 | ||
107 | struct udev_enumerate *enumerate; | |
108 | struct udev_list_entry *devices, *dev_list_entry; | |
109 | struct udev_device *dev; | |
110 | enumerate = udev_enumerate_new(udev); | |
111 | udev_enumerate_scan_devices(enumerate); | |
112 | devices = udev_enumerate_get_list_entry(enumerate); | |
113 | udev_list_entry_foreach(dev_list_entry, devices) | |
114 | { | |
115 | const char *strPath; | |
116 | strPath = udev_list_entry_get_name(dev_list_entry); | |
117 | ||
118 | dev = udev_device_new_from_syspath(udev, strPath); | |
119 | if (!dev) | |
120 | continue; | |
121 | ||
122 | dev = udev_device_get_parent(udev_device_get_parent(dev)); | |
123 | if (!dev) | |
124 | continue; | |
125 | if (!udev_device_get_sysattr_value(dev,"idVendor") || !udev_device_get_sysattr_value(dev, "idProduct")) | |
126 | { | |
127 | udev_device_unref(dev); | |
128 | continue; | |
129 | } | |
130 | ||
131 | int iVendor, iProduct; | |
132 | sscanf(udev_device_get_sysattr_value(dev, "idVendor"), "%x", &iVendor); | |
133 | sscanf(udev_device_get_sysattr_value(dev, "idProduct"), "%x", &iProduct); | |
134 | if (iVendor == CEC_VID && iProduct == CEC_PID) | |
135 | { | |
136 | CStdString strPath(udev_device_get_syspath(dev)); | |
137 | if (strDevicePath && strcmp(strPath.c_str(), strDevicePath)) | |
138 | continue; | |
139 | ||
140 | CStdString strComm(strPath); | |
141 | if (FindComPort(strComm)) | |
142 | { | |
143 | cec_device foundDev; | |
144 | foundDev.path = strPath; | |
145 | foundDev.comm = strComm; | |
146 | deviceList.push_back(foundDev); | |
147 | ++iFound; | |
148 | } | |
149 | } | |
150 | udev_device_unref(dev); | |
151 | } | |
152 | ||
153 | udev_enumerate_unref(enumerate); | |
154 | udev_unref(udev); | |
155 | #endif | |
156 | ||
157 | return iFound; | |
158 | } |