Ocean
OceanUSBManager.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_SYSTEM_USB_ANDROID_OCEAN_USB_MANAGER_H
9 #define META_OCEAN_SYSTEM_USB_ANDROID_OCEAN_USB_MANAGER_H
10 
12 
13 #include "ocean/base/Lock.h"
15 #include "ocean/base/Singleton.h"
16 
18 
19 namespace Ocean
20 {
21 
22 namespace System
23 {
24 
25 namespace USB
26 {
27 
28 namespace Android
29 {
30 
31 /**
32  * This class implements the native version of the USB manager for Android.
33  * The class has a corresponding Java class which is used/necessary to access the manager from Java.
34  * @ingroup systemusbandroid
35  */
36 class OCEAN_SYSTEM_USB_ANDROID_EXPORT OceanUSBManager : public Singleton<OceanUSBManager>
37 {
38  friend class Singleton<OceanUSBManager>;
39 
40  public:
41 
42  /**
43  * This class holds the relevant information of a USB device.
44  * However, it does not hold the actual device object or any reference to the device.
45  */
47  {
48  public:
49 
50  /**
51  * Default constructor creating an invalid object.
52  */
53  DeviceDescriptor() = default;
54 
55  /**
56  * Returns the device information as a string.
57  * @return The string holding the device information, 'Invalid' if the object is invalid
58  */
59  std::string toString() const;
60 
61  /**
62  * Returns whether this object holds valid device information.
63  * @return True, if so
64  */
65  inline bool isValid() const;
66 
67  public:
68 
69  /// The device's name as defined by the system (may not be human readable).
70  std::string deviceName_;
71 
72  /// The product name of the device (human readable).
73  std::string productName_;
74 
75  /// The manufacturer name of the device (human readable), empty if unknown.
76  std::string manufacturerName_;
77 
78  /// The vendor id of the device.
79  unsigned int vendorId_ = 0u;
80 
81  /// The product id of the device.
82  unsigned int productId_ = 0u;
83 
84  /// The USB device class of the device.
85  unsigned int deviceClass_ = 0u;
86 
87  /// The USB device subclass of the device.
88  unsigned int deviceSubclass_ = 0u;
89 
90  /// The USB device protocol of the device.
91  unsigned int deviceProtocol_ = 0u;
92  };
93 
94  /**
95  * Definition of a vector holding device descriptor objects.
96  */
97  using DeviceDescriptors = std::vector<DeviceDescriptor>;
98 
99  /**
100  * Definition of a callback functio for permission events.
101  * @param deviceName The name of the device, must be valid
102  * @param granted True, if the permission was granted; False, if the permission was denied
103  */
104  using PermissionCallback = std::function<void(const std::string& deviceName, bool granted)>;
105 
106  /**
107  * Definition of a pair combining a unique id with a device name
108  */
109  using DevicePair = std::pair<uint32_t, std::string>;
110 
111  /**
112  * Definition of a scoped subscription object.
113  */
115 
116  protected:
117 
118  /**
119  * Definition of a pair combining a unique id with a permission callback.
120  */
121  using PermissionCallbackPair = std::pair<uint32_t, PermissionCallback>;
122 
123  /**
124  * Definition of a vector holding permission callback pairs.
125  */
126  using PermissionCallbackPairs = std::vector<PermissionCallbackPair>;
127 
128  /**
129  * Definition of an unordered map mapping device names to permission callback functions.
130  */
131  using PermissionCallbackMap = std::unordered_map<std::string, PermissionCallbackPairs>;
132 
133  public:
134 
135  /**
136  * Initializes the manager.
137  * This function should be called once before any other function is called, from the main thread.
138  * @param jniEnv The JNI environment, must be valid
139  * @return True, if succeeded
140  * @see isInitialized().
141  */
142  bool initialize(JNIEnv* jniEnv);
143 
144  /**
145  * Returns whether the manager is initialized.
146  * @return True, if so
147  * @see initialize().
148  */
150 
151  /**
152  * Enumerates all currently available USB devices.
153  * @param jniEnv The JNI environment, must be valid
154  * @param deviceDescriptors The resulting list of available USB devices, empty if no device is available
155  * @param deviceClass Optional USB class a device must have (can be an interface class) to be enumerated, -1 to enumerate all devices
156  * @return True, if succeeded
157  * @see isInitialized().
158  */
159  bool enumerateDevices(JNIEnv* jniEnv, DeviceDescriptors& deviceDescriptors, const unsigned int deviceClass = (unsigned int)(-1));
160 
161  /**
162  * Returns whether the application has permission to access the specified device.
163  * @param jniEnv The JNI environment, must be valid
164  * @param deviceName The name of the device, must be valid
165  * @param permissionGranted True, if the application has already been granted access to this device; False, if the application has not yet been granted access to this device
166  * @return True, if succeeded
167  * @see requestPermission().
168  */
169  bool hasPermission(JNIEnv* jniEnv, const std::string& deviceName, bool& permissionGranted);
170 
171  /**
172  * Requests permission to access a specified device.
173  * This function returns a subscription object which will keep the permission request active as long as the subscription object exists.<br>
174  * Once the provided permission callback function is called, the scoped permission object is meaningless and can be relased at any time.
175  * @param jniEnv The JNI environment, must be valid
176  * @param deviceName The name of the device, must be valid
177  * @param permissionCallback Optional callback function which is called when the permission request has been finished, nullptr to avoid getting informed automatically
178  * @return The resulting permission subscription object, invalid if the permission request could not be started
179  */
180  [[nodiscard]] ScopedPermissionSubscription requestPermission(JNIEnv* jniEnv, const std::string& deviceName, PermissionCallback permissionCallback = nullptr);
181 
182  /**
183  * Opens a specified device.
184  * On Android, a USB device cannot be opened/accessed without using the Java USB API.
185  * The Java API will return a file descriptor which can be used to access the device.
186  * @param jniEnv The JNI environment, must be valid
187  * @param deviceName The name of the device, must be valid
188  * @param fileDescriptor The resulting device's file descriptor
189  * @return True, if succeeded
190  */
191  bool openDevice(JNIEnv* jniEnv, const std::string& deviceName, int64_t& fileDescriptor);
192 
193  /**
194  * Closes a specified device.
195  * @param jniEnv The JNI environment, must be valid
196  * @param deviceName The name of the device, must be valid
197  * @return True, if succeeded
198  */
199  bool closeDevice(JNIEnv* jniEnv, const std::string& deviceName);
200 
201  /**
202  * Event functions for device permission events.
203  * Don't call this function manually, this function is called via the JNI bridge from Java.
204  * @param deviceName The name of the device, must be valid
205  * @param granted True, if the permission was granted; False, if the permission was denied
206  */
207  void onDevicePermission(const std::string& deviceName, bool granted);
208 
209  protected:
210 
211  /**
212  * Creates a default manager object which is not yet initialized.
213  * @see initialize(), isInitialized().
214  */
215  OceanUSBManager() = default;
216 
217  /**
218  * Releases a permission request which has been created before.
219  * @param devicePair The pair combining a unique id and the name of the device for which the request was created, must be valid
220  */
221  void releasePermissionRequest(const DevicePair& devicePair);
222 
223  protected:
224 
225  /// The JNI class object of the Java OceanUSBManager class, invalid if not yet initialized.
227 
228  /// The map mapping device names to permission callback functions.
230 
231  /// The counter for unique ids.
232  uint32_t uniqueRequestIdCounter_ = 0u;
233 
234  /// The manager's lock.
236 };
237 
239 {
240  return vendorId_ != 0u && productId_ != 0u;
241 }
242 
243 }
244 
245 }
246 
247 }
248 
249 }
250 
251 /**
252  * Native interface function to initialize the manager, this function should be called from the main thread.
253  * @param env Native interface environment, must be valid
254  * @param javaThis JNI object, must be valid
255  * @return True, if succeeded
256  */
257 extern "C" bool Java_com_meta_ocean_system_usb_android_OceanUSBManager_initialize(JNIEnv* env, jobject javaThis);
258 
259 /**
260  * Native interface function for device permission events.
261  * @param env Native interface environment, must be valid
262  * @param javaThis JNI object, must be valid
263  * @param deviceName The name of the device, must be valid
264  * @param granted True, if the permission was granted; False, if the permission was denied
265  */
266 extern "C" void Java_com_meta_ocean_system_usb_android_OceanUSBManager_onDevicePermission(JNIEnv* env, jobject javaThis, jstring deviceName, jboolean granted);
267 
268 #endif // META_OCEAN_SYSTEM_USB_ANDROID_OCEAN_USB_MANAGER_H
void Java_com_meta_ocean_system_usb_android_OceanUSBManager_onDevicePermission(JNIEnv *env, jobject javaThis, jstring deviceName, jboolean granted)
Native interface function for device permission events.
bool Java_com_meta_ocean_system_usb_android_OceanUSBManager_initialize(JNIEnv *env, jobject javaThis)
Native interface function to initialize the manager, this function should be called from the main thr...
This class implements a recursive lock object.
Definition: Lock.h:31
This template class is the base class for all singleton objects.
Definition: Singleton.h:71
This class holds the relevant information of a USB device.
Definition: OceanUSBManager.h:47
std::string toString() const
Returns the device information as a string.
std::string deviceName_
The device's name as defined by the system (may not be human readable).
Definition: OceanUSBManager.h:70
bool isValid() const
Returns whether this object holds valid device information.
Definition: OceanUSBManager.h:238
DeviceDescriptor()=default
Default constructor creating an invalid object.
std::string manufacturerName_
The manufacturer name of the device (human readable), empty if unknown.
Definition: OceanUSBManager.h:76
unsigned int vendorId_
The vendor id of the device.
Definition: OceanUSBManager.h:79
unsigned int productId_
The product id of the device.
Definition: OceanUSBManager.h:82
std::string productName_
The product name of the device (human readable).
Definition: OceanUSBManager.h:73
This class implements the native version of the USB manager for Android.
Definition: OceanUSBManager.h:37
OceanUSBManager()=default
Creates a default manager object which is not yet initialized.
std::pair< uint32_t, std::string > DevicePair
Definition of a pair combining a unique id with a device name.
Definition: OceanUSBManager.h:109
bool enumerateDevices(JNIEnv *jniEnv, DeviceDescriptors &deviceDescriptors, const unsigned int deviceClass=(unsigned int)(-1))
Enumerates all currently available USB devices.
Lock lock_
The manager's lock.
Definition: OceanUSBManager.h:235
bool initialize(JNIEnv *jniEnv)
Initializes the manager.
std::pair< uint32_t, PermissionCallback > PermissionCallbackPair
Definition of a pair combining a unique id with a permission callback.
Definition: OceanUSBManager.h:121
Platform::Android::ScopedJClass javaClassOceanUSBManager_
The JNI class object of the Java OceanUSBManager class, invalid if not yet initialized.
Definition: OceanUSBManager.h:226
bool openDevice(JNIEnv *jniEnv, const std::string &deviceName, int64_t &fileDescriptor)
Opens a specified device.
bool hasPermission(JNIEnv *jniEnv, const std::string &deviceName, bool &permissionGranted)
Returns whether the application has permission to access the specified device.
std::vector< DeviceDescriptor > DeviceDescriptors
Definition of a vector holding device descriptor objects.
Definition: OceanUSBManager.h:97
ScopedPermissionSubscription requestPermission(JNIEnv *jniEnv, const std::string &deviceName, PermissionCallback permissionCallback=nullptr)
Requests permission to access a specified device.
void onDevicePermission(const std::string &deviceName, bool granted)
Event functions for device permission events.
PermissionCallbackMap permissionCallbackMap_
The map mapping device names to permission callback functions.
Definition: OceanUSBManager.h:229
std::vector< PermissionCallbackPair > PermissionCallbackPairs
Definition of a vector holding permission callback pairs.
Definition: OceanUSBManager.h:126
std::unordered_map< std::string, PermissionCallbackPairs > PermissionCallbackMap
Definition of an unordered map mapping device names to permission callback functions.
Definition: OceanUSBManager.h:131
void releasePermissionRequest(const DevicePair &devicePair)
Releases a permission request which has been created before.
bool closeDevice(JNIEnv *jniEnv, const std::string &deviceName)
Closes a specified device.
std::function< void(const std::string &deviceName, bool granted)> PermissionCallback
Definition of a callback functio for permission events.
Definition: OceanUSBManager.h:104
bool isInitialized()
Returns whether the manager is initialized.
ScopedJNIObject< jclass > ScopedJClass
Definition of a scoped object encapsulating a jclass object.
Definition: ScopedJNIObject.h:24
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15