Debian Codegraph @

xbmc/peripherals/bus/PeripheralBus.cpp

raw
1 /*
2 * Copyright (C) 2005-2024 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9 #include "PeripheralBus.h"
10
11 #include "FileItem.h"
12 #include "guilib/LocalizeStrings.h"
13 #include "peripherals/Peripherals.h"
14 #include "peripherals/devices/Peripheral.h"
15 #include "utils/StringUtils.h"
16 #include "utils/Variant.h"
17 #include "utils/log.h"
18
19 #include <mutex>
20
21 using namespace PERIPHERALS;
22 using namespace std::chrono_literals;
23
24 namespace
25 {
26 constexpr auto PERIPHERAL_DEFAULT_RESCAN_INTERVAL = 5000ms;
27 }
28
29 CPeripheralBus::CPeripheralBus(const std::string& threadname,
30 CPeripherals& manager,
31 PeripheralBusType type)
32 : CThread(threadname.c_str()),
33 m_iRescanTime(PERIPHERAL_DEFAULT_RESCAN_INTERVAL),
34 m_manager(manager),
35 m_type(type),
36 m_triggerEvent(true)
37 {
38 }
39
40 bool CPeripheralBus::InitializeProperties(CPeripheral& peripheral)
41 {
42 return true;
43 }
44
45 void CPeripheralBus::OnDeviceAdded(const std::string& strLocation)
46 {
47 ScanForDevices();
48 }
49
50 void CPeripheralBus::OnDeviceChanged(const std::string& strLocation)
51 {
52 ScanForDevices();
53 }
54
55 void CPeripheralBus::OnDeviceRemoved(const std::string& strLocation)
56 {
57 ScanForDevices();
58 }
59
60 void CPeripheralBus::Clear(void)
61 {
62 if (m_bNeedsPolling)
63 {
64 StopThread(false);
65 m_triggerEvent.Set();
66 StopThread(true);
67 }
68
69 std::unique_lock<CCriticalSection> lock(m_critSection);
70
71 m_peripherals.clear();
72 }
73
74 void CPeripheralBus::UnregisterRemovedDevices(const PeripheralScanResults& results)
75 {
76 PeripheralVector removedPeripherals;
77
78 {
79 std::unique_lock<CCriticalSection> lock(m_critSection);
80 for (int iDevicePtr = (int)m_peripherals.size() - 1; iDevicePtr >= 0; iDevicePtr--)
81 {
82 const PeripheralPtr& peripheral = m_peripherals.at(iDevicePtr);
83 PeripheralScanResult updatedDevice(m_type);
84 if (!results.GetDeviceOnLocation(peripheral->Location(), &updatedDevice) ||
85 *peripheral != updatedDevice)
86 {
87 /* device removed */
88 removedPeripherals.push_back(peripheral);
89 m_peripherals.erase(m_peripherals.begin() + iDevicePtr);
90 }
91 }
92 }
93
94 for (auto& peripheral : removedPeripherals)
95 {
96 std::vector<PeripheralFeature> features;
97 peripheral->GetFeatures(features);
98 bool peripheralHasFeatures =
99 features.size() > 1 || (features.size() == 1 && features.at(0) != FEATURE_UNKNOWN);
100 if (peripheral->Type() != PERIPHERAL_UNKNOWN || peripheralHasFeatures)
101 {
102 CLog::Log(LOGINFO, "{} - device removed from {}/{}: {} ({}:{})", __FUNCTION__,
103 PeripheralTypeTranslator::TypeToString(peripheral->Type()), peripheral->Location(),
104 peripheral->DeviceName(), peripheral->VendorIdAsString(),
105 peripheral->ProductIdAsString());
106 peripheral->OnDeviceRemoved();
107 }
108
109 m_manager.OnDeviceDeleted(*this, *peripheral);
110 }
111 }
112
113 void CPeripheralBus::RegisterNewDevices(const PeripheralScanResults& results)
114 {
115 for (unsigned int iResultPtr = 0; iResultPtr < results.m_results.size(); iResultPtr++)
116 {
117 const PeripheralScanResult& result = results.m_results.at(iResultPtr);
118 if (!HasPeripheral(result.m_strLocation))
119 m_manager.CreatePeripheral(*this, result);
120 }
121 }
122
123 bool CPeripheralBus::ScanForDevices(void)
124 {
125 bool bReturn(false);
126
127 PeripheralScanResults results;
128 if (PerformDeviceScan(results))
129 {
130 UnregisterRemovedDevices(results);
131 RegisterNewDevices(results);
132
133 m_manager.NotifyObservers(ObservableMessagePeripheralsChanged);
134
135 bReturn = true;
136 }
137
138 return bReturn;
139 }
140
141 bool CPeripheralBus::HasFeature(const PeripheralFeature feature) const
142 {
143 bool bReturn(false);
144 std::unique_lock<CCriticalSection> lock(m_critSection);
145 for (unsigned int iPeripheralPtr = 0; iPeripheralPtr < m_peripherals.size(); iPeripheralPtr++)
146 {
147 if (m_peripherals.at(iPeripheralPtr)->HasFeature(feature))
148 {
149 bReturn = true;
150 break;
151 }
152 }
153 return bReturn;
154 }
155
156 void CPeripheralBus::GetFeatures(std::vector<PeripheralFeature>& features) const
157 {
158 std::unique_lock<CCriticalSection> lock(m_critSection);
159 for (unsigned int iPeripheralPtr = 0; iPeripheralPtr < m_peripherals.size(); iPeripheralPtr++)
160 m_peripherals.at(iPeripheralPtr)->GetFeatures(features);
161 }
162
163 PeripheralPtr CPeripheralBus::GetPeripheral(const std::string& strLocation) const
164 {
165 PeripheralPtr result;
166 std::unique_lock<CCriticalSection> lock(m_critSection);
167 for (auto& peripheral : m_peripherals)
168 {
169 if (peripheral->Location() == strLocation)
170 {
171 result = peripheral;
172 break;
173 }
174 }
175 return result;
176 }
177
178 unsigned int CPeripheralBus::GetPeripheralsWithFeature(PeripheralVector& results,
179 const PeripheralFeature feature) const
180 {
181 unsigned int iReturn = 0;
182 std::unique_lock<CCriticalSection> lock(m_critSection);
183 for (auto& peripheral : m_peripherals)
184 {
185 if (peripheral->HasFeature(feature))
186 {
187 results.push_back(peripheral);
188 ++iReturn;
189 }
190 }
191
192 return iReturn;
193 }
194
195 unsigned int CPeripheralBus::GetNumberOfPeripheralsWithId(const int iVendorId,
196 const int iProductId) const
197 {
198 unsigned int iReturn = 0;
199 std::unique_lock<CCriticalSection> lock(m_critSection);
200 for (const auto& peripheral : m_peripherals)
201 {
202 if (peripheral->VendorId() == iVendorId && peripheral->ProductId() == iProductId)
203 iReturn++;
204 }
205
206 return iReturn;
207 }
208
209 void CPeripheralBus::Process(void)
210 {
211 while (!m_bStop)
212 {
213 m_triggerEvent.Reset();
214
215 if (!ScanForDevices())
216 break;
217
218 // depending on bus implementation
219 // needsPolling can be set properly
220 // only after initial scan.
221 // if this is the case, bail out.
222 if (!m_bNeedsPolling)
223 break;
224
225 if (!m_bStop)
226 m_triggerEvent.Wait(m_iRescanTime);
227 }
228 }
229
230 void CPeripheralBus::Initialise(void)
231 {
232 bool bNeedsPolling = false;
233
234 if (!IsRunning())
235 {
236 std::unique_lock<CCriticalSection> lock(m_critSection);
237 bNeedsPolling = m_bNeedsPolling;
238 }
239
240 if (bNeedsPolling)
241 {
242 m_triggerEvent.Reset();
243 Create();
244 SetPriority(ThreadPriority::BELOW_NORMAL);
245 }
246 }
247
248 void CPeripheralBus::Register(const PeripheralPtr& peripheral)
249 {
250 if (!peripheral)
251 return;
252
253 bool bPeripheralAdded = false;
254
255 {
256 std::unique_lock<CCriticalSection> lock(m_critSection);
257 if (!HasPeripheral(peripheral->Location()))
258 {
259 m_peripherals.push_back(peripheral);
260 bPeripheralAdded = true;
261 }
262 }
263
264 if (bPeripheralAdded)
265 {
266 CLog::Log(LOGINFO, "{} - new {} device registered on {}->{}: {} ({}:{})", __FUNCTION__,
267 PeripheralTypeTranslator::TypeToString(peripheral->Type()),
268 PeripheralTypeTranslator::BusTypeToString(m_type), peripheral->Location(),
269 peripheral->DeviceName(), peripheral->VendorIdAsString(),
270 peripheral->ProductIdAsString());
271 m_manager.OnDeviceAdded(*this, *peripheral);
272 }
273 }
274
275 void CPeripheralBus::TriggerDeviceScan(void)
276 {
277 bool bNeedsPolling;
278
279 {
280 std::unique_lock<CCriticalSection> lock(m_critSection);
281 bNeedsPolling = m_bNeedsPolling;
282 }
283
284 if (bNeedsPolling)
285 m_triggerEvent.Set();
286 else
287 ScanForDevices();
288 }
289
290 bool CPeripheralBus::HasPeripheral(const std::string& strLocation) const
291 {
292 return (GetPeripheral(strLocation) != NULL);
293 }
294
295 void CPeripheralBus::GetDirectory(const std::string& strPath, CFileItemList& items) const
296 {
297 std::unique_lock<CCriticalSection> lock(m_critSection);
298 for (const auto& peripheral : m_peripherals)
299 {
300 if (peripheral->IsHidden())
301 continue;
302
303 CFileItemPtr peripheralFile(new CFileItem(peripheral->DeviceName()));
304 peripheralFile->SetPath(peripheral->FileLocation());
305 peripheralFile->SetProperty("vendor", peripheral->VendorIdAsString());
306 peripheralFile->SetProperty("product", peripheral->ProductIdAsString());
307 peripheralFile->SetProperty(
308 "bus", PeripheralTypeTranslator::BusTypeToString(peripheral->GetBusType()));
309 peripheralFile->SetProperty("location", peripheral->Location());
310 peripheralFile->SetProperty("class",
311 PeripheralTypeTranslator::TypeToString(peripheral->Type()));
312
313 std::string strVersion(peripheral->GetVersionInfo());
314
315 std::string strDetails;
316
317 if (peripheral->GetBusType() == PERIPHERAL_BUS_CEC && !peripheral->GetSettingBool("enabled"))
318 strDetails =
319 StringUtils::Format("{}: {}", g_localizeStrings.Get(126), g_localizeStrings.Get(13106));
320
321 if (strDetails.empty())
322 {
323 std::string strVersion(peripheral->GetVersionInfo());
324 if (!strVersion.empty())
325 strDetails = StringUtils::Format("{} {}", g_localizeStrings.Get(24051), strVersion);
326 }
327
328 peripheralFile->SetProperty("version", strVersion);
329 peripheralFile->SetLabel2(strDetails);
330 peripheralFile->SetArt("icon", peripheral->GetIcon());
331
332 items.Add(peripheralFile);
333 }
334 }
335
336 PeripheralPtr CPeripheralBus::GetByPath(const std::string& strPath) const
337 {
338 PeripheralPtr result;
339
340 std::unique_lock<CCriticalSection> lock(m_critSection);
341 for (auto& peripheral : m_peripherals)
342 {
343 if (StringUtils::EqualsNoCase(strPath, peripheral->FileLocation()))
344 {
345 result = peripheral;
346 break;
347 }
348 }
349
350 return result;
351 }
352
353 unsigned int CPeripheralBus::GetNumberOfPeripherals() const
354 {
355 std::unique_lock<CCriticalSection> lock(m_critSection);
356 return static_cast<unsigned int>(m_peripherals.size());
357 }