kodi
PeripheralBus.h
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 PERIPHERALS
21 {
22 class CPeripheral;
23 class CPeripherals;
24 
36 class CPeripheralBus : protected CThread
37 {
38 public:
39  CPeripheralBus(const std::string& threadname, CPeripherals& manager, PeripheralBusType type);
40  ~CPeripheralBus(void) override { Clear(); }
41 
45  PeripheralBusType Type(void) const { return m_type; }
46 
51  bool NeedsPolling(void) const
52  {
53  std::unique_lock<CCriticalSection> lock(m_critSection);
54  return m_bNeedsPolling;
55  }
56 
60  virtual bool InitializeProperties(CPeripheral& peripheral);
61 
67  virtual PeripheralPtr GetPeripheral(const std::string& strLocation) const;
68 
74  virtual bool HasPeripheral(const std::string& strLocation) const;
75 
81  virtual bool SupportsFeature(PeripheralFeature feature) const { return false; }
82 
89  virtual unsigned int GetPeripheralsWithFeature(PeripheralVector& results,
90  const PeripheralFeature feature) const;
91 
92  virtual unsigned int GetNumberOfPeripherals() const;
93  virtual unsigned int GetNumberOfPeripheralsWithId(const int iVendorId,
94  const int iProductId) const;
95 
100  virtual void GetFeatures(std::vector<PeripheralFeature>& features) const;
101 
107  virtual bool HasFeature(const PeripheralFeature feature) const;
108 
113  virtual void OnDeviceAdded(const std::string& strLocation);
114 
119  virtual void OnDeviceChanged(const std::string& strLocation);
120 
125  virtual void OnDeviceRemoved(const std::string& strLocation);
126 
130  virtual void Initialise(void);
131 
135  virtual void Clear(void);
136 
140  virtual void TriggerDeviceScan(void);
141 
147  virtual void GetDirectory(const std::string& strPath, CFileItemList& items) const;
148 
154  virtual PeripheralPtr GetByPath(const std::string& strPath) const;
155 
160  virtual void Register(const PeripheralPtr& peripheral);
161 
162  virtual bool FindComPort(std::string& strLocation) { return false; }
163 
167  virtual void ProcessEvents(void) {}
168 
173  virtual void EnableButtonMapping() {}
174 
179  virtual void PowerOff(const std::string& strLocation) {}
180 
181 protected:
182  void Process(void) override;
183  virtual bool ScanForDevices(void);
184  virtual void UnregisterRemovedDevices(const PeripheralScanResults& results);
185  virtual void RegisterNewDevices(const PeripheralScanResults& results);
186 
193  virtual bool PerformDeviceScan(PeripheralScanResults& results) = 0;
194 
195  PeripheralVector m_peripherals;
196  std::chrono::milliseconds m_iRescanTime;
198  true;
200  CPeripherals& m_manager;
201  const PeripheralBusType m_type;
202  mutable CCriticalSection m_critSection;
203  CEvent m_triggerEvent;
204 };
205 using PeripheralBusPtr = std::shared_ptr<CPeripheralBus>;
206 using PeripheralBusVector = std::vector<PeripheralBusPtr>;
207 } // namespace PERIPHERALS
Definition: PeripheralTypes.h:356
This is an Event class built from a ConditionVariable.
Definition: Event.h:35
virtual void Initialise(void)
Initialise this bus and start a polling thread if this bus needs polling.
Definition: PeripheralBus.cpp:230
bool NeedsPolling(void) const
Definition: PeripheralBus.h:51
virtual void OnDeviceChanged(const std::string &strLocation)
Callback method for when a device has been changed. Will perform a device scan.
Definition: PeripheralBus.cpp:50
Definition: Thread.h:44
virtual bool HasPeripheral(const std::string &strLocation) const
Check whether a peripheral is present at the given location.
Definition: PeripheralBus.cpp:290
virtual void Clear(void)
Stop the polling thread and clear all known devices on this bus.
Definition: PeripheralBus.cpp:60
virtual void TriggerDeviceScan(void)
Scan for devices.
Definition: PeripheralBus.cpp:275
virtual void PowerOff(const std::string &strLocation)
Power off the specified device.
Definition: PeripheralBus.h:179
virtual PeripheralPtr GetByPath(const std::string &strPath) const
Get the instance of a peripheral given it&#39;s path.
Definition: PeripheralBus.cpp:330
Definition: RetroPlayerInput.h:15
Represents a list of files.
Definition: FileItem.h:702
virtual void OnDeviceAdded(const std::string &strLocation)
Callback method for when a device has been added. Will perform a device scan.
Definition: PeripheralBus.cpp:45
virtual bool HasFeature(const PeripheralFeature feature) const
Check whether there is at least one device present with the given feature.
Definition: PeripheralBus.cpp:141
PeripheralBusType Type(void) const
Definition: PeripheralBus.h:45
virtual bool SupportsFeature(PeripheralFeature feature) const
Check if the bus supports the given feature.
Definition: PeripheralBus.h:81
bool m_bNeedsPolling
Definition: PeripheralBus.h:197
virtual bool PerformDeviceScan(PeripheralScanResults &results)=0
Scan for devices on this bus and add them to the results list. This will have to be implemented for e...
virtual PeripheralPtr GetPeripheral(const std::string &strLocation) const
Get the instance of the peripheral at the given location.
Definition: PeripheralBus.cpp:163
virtual unsigned int GetPeripheralsWithFeature(PeripheralVector &results, const PeripheralFeature feature) const
Get all peripheral instances that have the given feature.
Definition: PeripheralBus.cpp:178
virtual void ProcessEvents(void)
Poll for events.
Definition: PeripheralBus.h:167
virtual bool InitializeProperties(CPeripheral &peripheral)
Initialize the properties of a peripheral with a known location.
Definition: PeripheralBus.cpp:40
virtual void Register(const PeripheralPtr &peripheral)
Register a new peripheral on this bus.
Definition: PeripheralBus.cpp:248
virtual void EnableButtonMapping()
Initialize button mapping.
Definition: PeripheralBus.h:173
virtual void OnDeviceRemoved(const std::string &strLocation)
Callback method for when a device has been removed. Will perform a device scan.
Definition: PeripheralBus.cpp:55
Definition: PeripheralBus.h:36
virtual void GetFeatures(std::vector< PeripheralFeature > &features) const
Get all features that are supported by devices on this bus.
Definition: PeripheralBus.cpp:156
Definition: Peripheral.h:71
virtual void GetDirectory(const std::string &strPath, CFileItemList &items) const
Get all fileitems for a path.
Definition: PeripheralBus.cpp:295
Definition: Peripherals.h:56