Debian Codegraph @

xbmc/peripherals/bus/PeripheralBus.h

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 #pragma once
10
11 #include "peripherals/PeripheralTypes.h"
12 #include "threads/Thread.h"
13
14 #include <memory>
15 #include <mutex>
16 #include <vector>
17
18 class CFileItemList;
19
20 namespace KODI
21 {
22 namespace JOYSTICK
23 {
24 class IButtonMap;
25 } // namespace JOYSTICK
26 } // namespace KODI
27
28 namespace PERIPHERALS
29 {
30 class CPeripheral;
31 class CPeripherals;
32
33 /*!
34 * \ingroup peripherals
35 *
36 * @class CPeripheralBus
37 * This represents a bus on the system. By default, this bus instance will scan for changes every 5
38 * seconds. If this bus only has to be updated after a notification sent by the system, set
39 * m_bNeedsPolling to false in the constructor, and implement the OnDeviceAdded(), OnDeviceChanged()
40 * and OnDeviceRemoved() methods.
41 *
42 * The PerformDeviceScan() method has to be implemented by each specific bus implementation.
43 */
44 class CPeripheralBus : protected CThread
45 {
46 public:
47 CPeripheralBus(const std::string& threadname, CPeripherals& manager, PeripheralBusType type);
48 ~CPeripheralBus(void) override { Clear(); }
49
50 /*!
51 * @return The bus type
52 */
53 PeripheralBusType Type(void) const { return m_type; }
54
55 /*!
56 * @return True if this bus needs to be polled for changes, false if this bus performs updates via
57 * callbacks
58 */
59 bool NeedsPolling(void) const
60 {
61 std::unique_lock<CCriticalSection> lock(m_critSection);
62 return m_bNeedsPolling;
63 }
64
65 /*!
66 * \brief Initialize the properties of a peripheral with a known location
67 */
68 virtual bool InitializeProperties(CPeripheral& peripheral);
69
70 /*!
71 * \brief Initialize a joystick buttonmap, if possible
72 */
73 virtual bool InitializeButtonMap(const CPeripheral& peripheral,
74 KODI::JOYSTICK::IButtonMap& buttonMap) const
75 {
76 return false;
77 }
78
79 /*!
80 * @brief Get the instance of the peripheral at the given location.
81 * @param strLocation The location.
82 * @return The peripheral or NULL if it wasn't found.
83 */
84 virtual PeripheralPtr GetPeripheral(const std::string& strLocation) const;
85
86 /*!
87 * @brief Check whether a peripheral is present at the given location.
88 * @param strLocation The location.
89 * @return True when a peripheral was found, false otherwise.
90 */
91 virtual bool HasPeripheral(const std::string& strLocation) const;
92
93 /*!
94 * @brief Check if the bus supports the given feature
95 * @param feature The feature to check for
96 * @return True if the bus supports the feature, false otherwise
97 */
98 virtual bool SupportsFeature(PeripheralFeature feature) const { return false; }
99
100 /*!
101 * @brief Get all peripheral instances that have the given feature.
102 * @param results The list of results.
103 * @param feature The feature to search for.
104 * @return The number of devices that have been found.
105 */
106 virtual unsigned int GetPeripheralsWithFeature(PeripheralVector& results,
107 const PeripheralFeature feature) const;
108
109 virtual unsigned int GetNumberOfPeripherals() const;
110 virtual unsigned int GetNumberOfPeripheralsWithId(const int iVendorId,
111 const int iProductId) const;
112
113 /*!
114 * @brief Get all features that are supported by devices on this bus.
115 * @param features All features.
116 */
117 virtual void GetFeatures(std::vector<PeripheralFeature>& features) const;
118
119 /*!
120 * @brief Check whether there is at least one device present with the given feature.
121 * @param feature The feature to check for.
122 * @return True when at least one device was found with this feature, false otherwise.
123 */
124 virtual bool HasFeature(const PeripheralFeature feature) const;
125
126 /*!
127 * @brief Callback method for when a device has been added. Will perform a device scan.
128 * @param strLocation The location of the device that has been added.
129 */
130 virtual void OnDeviceAdded(const std::string& strLocation);
131
132 /*!
133 * @brief Callback method for when a device has been changed. Will perform a device scan.
134 * @param strLocation The location of the device that has been changed.
135 */
136 virtual void OnDeviceChanged(const std::string& strLocation);
137
138 /*!
139 * @brief Callback method for when a device has been removed. Will perform a device scan.
140 * @param strLocation The location of the device that has been removed.
141 */
142 virtual void OnDeviceRemoved(const std::string& strLocation);
143
144 /*!
145 * @brief Initialise this bus and start a polling thread if this bus needs polling.
146 */
147 virtual void Initialise(void);
148
149 /*!
150 * @brief Stop the polling thread and clear all known devices on this bus.
151 */
152 virtual void Clear(void);
153
154 /*!
155 * @brief Scan for devices.
156 */
157 virtual void TriggerDeviceScan(void);
158
159 /*!
160 * @brief Get all fileitems for a path.
161 * @param strPath The path to the directory to get the items from.
162 * @param items The item list.
163 */
164 virtual void GetDirectory(const std::string& strPath, CFileItemList& items) const;
165
166 /*!
167 * @brief Get the instance of a peripheral given it's path.
168 * @param strPath The path to the peripheral.
169 * @return The peripheral or NULL if it wasn't found.
170 */
171 virtual PeripheralPtr GetByPath(const std::string& strPath) const;
172
173 /*!
174 * @brief Register a new peripheral on this bus.
175 * @param peripheral The peripheral to register.
176 */
177 virtual void Register(const PeripheralPtr& peripheral);
178
179 virtual bool FindComPort(std::string& strLocation) { return false; }
180
181 /*!
182 * \brief Poll for events
183 */
184 virtual void ProcessEvents(void) {}
185
186 /*!
187 * \brief Initialize button mapping
188 * \return True if button mapping is enabled for this bus
189 */
190 virtual void EnableButtonMapping() {}
191
192 /*!
193 * \brief Power off the specified device
194 * \param strLocation The device's location
195 */
196 virtual void PowerOff(const std::string& strLocation) {}
197
198 protected:
199 void Process(void) override;
200 virtual bool ScanForDevices(void);
201 virtual void UnregisterRemovedDevices(const PeripheralScanResults& results);
202 virtual void RegisterNewDevices(const PeripheralScanResults& results);
203
204 /*!
205 * @brief Scan for devices on this bus and add them to the results list. This will have to be
206 * implemented for each bus.
207 * @param results The result list.
208 * @return True when the scan was successful, false otherwise.
209 */
210 virtual bool PerformDeviceScan(PeripheralScanResults& results) = 0;
211
212 PeripheralVector m_peripherals;
213 std::chrono::milliseconds m_iRescanTime;
214 bool m_bNeedsPolling =
215 true; /*!< true when this bus needs to be polled for new devices, false when it
216 uses callbacks to notify this bus of changed */
217 CPeripherals& m_manager;
218 const PeripheralBusType m_type;
219 mutable CCriticalSection m_critSection;
220 CEvent m_triggerEvent;
221 };
222 using PeripheralBusPtr = std::shared_ptr<CPeripheralBus>;
223 using PeripheralBusVector = std::vector<PeripheralBusPtr>;
224 } // namespace PERIPHERALS