xbmc
PeripheralBus.h
1 /*
2  * Copyright (C) 2005-2018 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 
34 class CPeripheralBus : protected CThread
35 {
36 public:
37  CPeripheralBus(const std::string& threadname, CPeripherals& manager, PeripheralBusType type);
38  ~CPeripheralBus(void) override { Clear(); }
39 
43  PeripheralBusType Type(void) const { return m_type; }
44 
49  bool NeedsPolling(void) const
50  {
51  std::unique_lock<CCriticalSection> lock(m_critSection);
52  return m_bNeedsPolling;
53  }
54 
58  virtual bool InitializeProperties(CPeripheral& peripheral);
59 
65  virtual PeripheralPtr GetPeripheral(const std::string& strLocation) const;
66 
72  virtual bool HasPeripheral(const std::string& strLocation) const;
73 
79  virtual bool SupportsFeature(PeripheralFeature feature) const { return false; }
80 
87  virtual unsigned int GetPeripheralsWithFeature(PeripheralVector& results,
88  const PeripheralFeature feature) const;
89 
90  virtual unsigned int GetNumberOfPeripherals() const;
91  virtual unsigned int GetNumberOfPeripheralsWithId(const int iVendorId,
92  const int iProductId) const;
93 
98  virtual void GetFeatures(std::vector<PeripheralFeature>& features) const;
99 
105  virtual bool HasFeature(const PeripheralFeature feature) const;
106 
111  virtual void OnDeviceAdded(const std::string& strLocation);
112 
117  virtual void OnDeviceChanged(const std::string& strLocation);
118 
123  virtual void OnDeviceRemoved(const std::string& strLocation);
124 
128  virtual void Initialise(void);
129 
133  virtual void Clear(void);
134 
138  virtual void TriggerDeviceScan(void);
139 
145  virtual void GetDirectory(const std::string& strPath, CFileItemList& items) const;
146 
152  virtual PeripheralPtr GetByPath(const std::string& strPath) const;
153 
158  virtual void Register(const PeripheralPtr& peripheral);
159 
160  virtual bool FindComPort(std::string& strLocation) { return false; }
161 
165  virtual void ProcessEvents(void) {}
166 
171  virtual void EnableButtonMapping() {}
172 
177  virtual void PowerOff(const std::string& strLocation) {}
178 
179 protected:
180  void Process(void) override;
181  virtual bool ScanForDevices(void);
182  virtual void UnregisterRemovedDevices(const PeripheralScanResults& results);
183  virtual void RegisterNewDevices(const PeripheralScanResults& results);
184 
191  virtual bool PerformDeviceScan(PeripheralScanResults& results) = 0;
192 
193  PeripheralVector m_peripherals;
194  std::chrono::milliseconds m_iRescanTime;
197  CPeripherals& m_manager;
198  const PeripheralBusType m_type;
199  mutable CCriticalSection m_critSection;
200  CEvent m_triggerEvent;
201 };
202 using PeripheralBusPtr = std::shared_ptr<CPeripheralBus>;
203 using PeripheralBusVector = std::vector<PeripheralBusPtr>;
204 } // namespace PERIPHERALS
Definition: PeripheralTypes.h:348
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:231
bool NeedsPolling(void) const
Definition: PeripheralBus.h:49
virtual void OnDeviceChanged(const std::string &strLocation)
Callback method for when a device has been changed. Will perform a device scan.
Definition: PeripheralBus.cpp:51
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:291
virtual void Clear(void)
Stop the polling thread and clear all known devices on this bus.
Definition: PeripheralBus.cpp:61
virtual void TriggerDeviceScan(void)
Scan for devices.
Definition: PeripheralBus.cpp:276
virtual void PowerOff(const std::string &strLocation)
Power off the specified device.
Definition: PeripheralBus.h:177
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:713
virtual void OnDeviceAdded(const std::string &strLocation)
Callback method for when a device has been added. Will perform a device scan.
Definition: PeripheralBus.cpp:46
virtual bool HasFeature(const PeripheralFeature feature) const
Check whether there is at least one device present with the given feature.
Definition: PeripheralBus.cpp:142
PeripheralBusType Type(void) const
Definition: PeripheralBus.h:43
virtual bool SupportsFeature(PeripheralFeature feature) const
Check if the bus supports the given feature.
Definition: PeripheralBus.h:79
bool m_bNeedsPolling
Definition: PeripheralBus.h:195
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:164
virtual unsigned int GetPeripheralsWithFeature(PeripheralVector &results, const PeripheralFeature feature) const
Get all peripheral instances that have the given feature.
Definition: PeripheralBus.cpp:179
virtual void ProcessEvents(void)
Poll for events.
Definition: PeripheralBus.h:165
virtual bool InitializeProperties(CPeripheral &peripheral)
Initialize the properties of a peripheral with a known location.
Definition: PeripheralBus.cpp:41
virtual void Register(const PeripheralPtr &peripheral)
Register a new peripheral on this bus.
Definition: PeripheralBus.cpp:249
virtual void EnableButtonMapping()
Initialize button mapping.
Definition: PeripheralBus.h:171
virtual void OnDeviceRemoved(const std::string &strLocation)
Callback method for when a device has been removed. Will perform a device scan.
Definition: PeripheralBus.cpp:56
Definition: PeripheralBus.h:34
virtual void GetFeatures(std::vector< PeripheralFeature > &features) const
Get all features that are supported by devices on this bus.
Definition: PeripheralBus.cpp:157
Definition: Peripheral.h:62
virtual void GetDirectory(const std::string &strPath, CFileItemList &items) const
Get all fileitems for a path.
Definition: PeripheralBus.cpp:296
Definition: Peripherals.h:49