public class DeviceManager
extends java.lang.Object
DeviceManager
class provides methods for opening and registering
devices that can then be handled as Device
instances. A Device
instance
of a particular type can be opened using its platform-specific numerical ID or name as well as
its properties or using an ad-hoc configuration (in which case, its hardware addressing
information must be explicitly provided).
A Device
instance may be identified by a numerical ID. This ID is unrelated to the
hardware number (hardware addressing information) that may be used to identify a device such as a
GPIO pin number or an I2C slave device address. A device ID typically corresponds to a
registered configuration for a device. The numerical ID of a device must be
greater than or equal to 0
and must be unique. Yet the same device may be
directly and indirectly mapped through several IDs; each ID may correspond to a different
configuration, representation or abstraction for the same underlying device hardware
resource. Device
instance opened with an ad-hoc configuration - that is: not
through one of its registered configurations - is not assigned a numerical ID nor a name. Its
numerical ID and name are both undefined and set respectively to
UNDEFINED_ID
and null
.
Devices may be opened in either
exclusive or shared mode. By default,
devices are opened in exclusive mode. Whether a device can be opened in
shared mode depends on the underlying device hardware as well as on the underlying
device driver. It also depends on whether the provided Device
implementation is a
dedicated, virtualized or shared abstraction of the underlying
device resource. Device
implementation (or driver) may throw a
InvalidDeviceConfigException
if the device is already open and the
requested adhoc configuration is incompatible with the current configuration of the device
device. Device.tryLock
and
Device.unlock
. Device locks are held on a per Device
instance basis.
When the same device is open twice in shared access mode by the same application,
locking one of the Device
instances will prevent the other from being accessed/used.
Opening a Device
instance of a specific type with a registered configuration is
subject to permission checks (see DeviceMgmtPermission.OPEN
). Device
instance of a specific type with an ad-hoc configuration is subject
to permission checks specific for that type (for example see
GPIOPinPermission.OPEN
). This permission check
should be implemented by the DeviceProvider.open
method. DeviceMgmtPermission.REGISTER
and DeviceMgmtPermission.UNREGISTER
).
Modifier and Type | Field and Description |
---|---|
static int |
EXCLUSIVE
Exclusive access mode.
|
static int |
SHARED
Shared access mode.
|
static int |
UNSPECIFIED_ID
Unspecified device numerical ID - requesting the allocation of a free ID.
|
Constructor and Description |
---|
DeviceManager() |
Modifier and Type | Method and Description |
---|---|
static <P extends Device<? super P>> |
addRegistrationListener(RegistrationListener<P> listener,
java.lang.Class<P> intf)
Adds the specified registration listener to receive notification of registration and
unregistration of devices of the specified type.
|
static <P extends Device<? super P>> |
list()
List all platform- and user-registered devices.
|
static <P extends Device<? super P>> |
list(java.lang.Class<P> intf)
List all platform- and user-registered devices of the designated type.
|
static <P extends Device<? super P>> |
open(java.lang.Class<P> intf,
DeviceConfig<? super P> config)
Opens a
Device instance of the specified type with the specified hardware
addressing information and configuration. |
static <P extends Device<? super P>> |
open(java.lang.Class<P> intf,
DeviceConfig<? super P> config,
int mode)
Opens a
Device instance of the specified type with the specified hardware
addressing information and configuration. |
static <P extends Device<? super P>> |
open(DeviceConfig<? super P> config)
Opens a
Device instance with the specified hardware addressing information and
configuration. |
static <P extends Device<? super P>> |
open(DeviceConfig<? super P> config,
int mode)
Opens a
Device instance with the specified hardware addressing information and
configuration. |
static <P extends Device<? super P>> |
open(int id)
Looks up and opens a
Device instance for the provided numerical ID. |
static <P extends Device<? super P>> |
open(int id,
java.lang.Class<P> intf)
Looks up and opens a
Device instance for the provided numerical ID and type. |
static <P extends Device<? super P>> |
open(int id,
java.lang.Class<P> intf,
int mode)
Looks up and opens a
Device instance for the provided numerical ID and type. |
static <P extends Device<? super P>> |
open(int id,
int mode)
Looks up and opens a
Device instance for the provided numerical ID. |
static <P extends Device<? super P>> |
open(java.lang.String name,
java.lang.Class<P> intf,
int mode,
java.lang.String... properties)
Looks up and opens a
Device instance for the specified name, type and/or
properties. |
static <P extends Device<? super P>> |
open(java.lang.String name,
java.lang.Class<P> intf,
java.lang.String... properties)
Looks up and opens a
Device instance for the specified name, type and/or
properties. |
static <P extends Device<? super P>> |
register(int id,
java.lang.Class<P> intf,
DeviceConfig<? super P> config,
java.lang.String name,
java.lang.String... properties)
Registers under the specified ID (and optionally name and properties) a new device
supporting the provided configuration.
|
static <P extends Device<? super P>> |
removeRegistrationListener(RegistrationListener<P> listener,
java.lang.Class<P> intf)
Removes the specified registration listener so that it no longer receives notification of
registration and unregistration of devices of the specified type.
|
static void |
unregister(int id)
Unregisters the device associated with the specified ID.
|
public static final int EXCLUSIVE
public static final int SHARED
public static final int UNSPECIFIED_ID
register
,
Constant Field Valuespublic static <P extends Device<? super P>> java.util.Iterator<DeviceDescriptor<P>> list()
P
- the type of devices listed.public static <P extends Device<? super P>> java.util.Iterator<DeviceDescriptor<P>> list(java.lang.Class<P> intf)
P
- the type of devices to list.intf
- the interface (sub-interface of Device
) of the device to be
registered.java.lang.NullPointerException
- if intf
is null
.public static <P extends Device<? super P>> P open(java.lang.Class<P> intf, DeviceConfig<? super P> config) throws java.io.IOException, InvalidDeviceConfigException, UnsupportedDeviceTypeException, DeviceNotFoundException, UnavailableDeviceException
Device
instance of the specified type with the specified hardware
addressing information and configuration. The specified device type and specified
configuration type must be compatible. An instance of the first available
matching Device
is returned. If a matching Device
is already open
(therefore not available) the next matching Device
is considered.
The device is opened in exclusive access mode.
Note that the returned Device
instance's ID and name are undefined.
A new instance is returned upon each call.P
- the type of the device to open.intf
- the interface (sub-interface of Device
) of the device being looked
up.config
- the device configuration (which includes hardware addressing information as
well as configuration parameters).Device
instance for the specified configuration.UnsupportedDeviceTypeException
- if the designated device type is not supported.InvalidDeviceConfigException
- if the provided device configuration (as defined by the configuration
parameters) is not valid/supported.DeviceNotFoundException
- if the device designated by the hardware addressing information is not found.UnavailableDeviceException
- if the designated device is not currently available - such as if it is
already open with exclusive access.java.io.IOException
- if any other I/O error occurred.java.lang.SecurityException
- if the caller has no permission to access the designated device (see
DevicePermission.OPEN
).java.lang.NullPointerException
- if intf
or config
is null
.java.lang.ClassCastException
- if the device configuration type specified by config
is not applicable to the device type specified by intf
.public static <P extends Device<? super P>> P open(java.lang.Class<P> intf, DeviceConfig<? super P> config, int mode) throws java.io.IOException, InvalidDeviceConfigException, UnsupportedDeviceTypeException, DeviceNotFoundException, UnavailableDeviceException, UnsupportedAccessModeException
Device
instance of the specified type with the specified hardware
addressing information and configuration. The specified device type and specified
configuration type must be compatible. An instance of the first available matching Device
is returned. If a matching Device
is already open in a mode that is not
compatible with the requested mode the next matching Device
is considered.
The device is opened in the designated access mode. A device may be
opened in shared mode if supported by the underlying driver and hardware and if it is not
already opened in exclusive mode. A device may be opened in exclusive mode if
supported by the underlying driver and hardware and if it is not already opened.
Note that the returned Device
instance's ID and name are undefined.
A new instance is returned upon each call.P
- the type of the device to open.intf
- the interface (sub-interface of Device
) of the device being looked
up.config
- the device configuration (which includes hardware addressing information as
well as configuration parameters).mode
- the access mode, one of: EXCLUSIVE
or SHARED
.Device
instance for the specified configuration.UnsupportedDeviceTypeException
- if the designated device type is not supported.InvalidDeviceConfigException
- if the provided device configuration (as defined by the configuration
parameters) is not valid/supported or when opened in shared mode, if the provided
device configuration is incompatible with the currently open configuration of
the device.DeviceNotFoundException
- if the device designated by the hardware addressing information is not found.UnavailableDeviceException
- if the designated device is not currently available - such as if it is
already open with exclusive access.UnsupportedAccessModeException
- if the requested access mode is not supported.java.io.IOException
- if any other I/O error occurred.java.lang.SecurityException
- if the caller has no permission to access the designated device (see
DevicePermission.OPEN
).java.lang.NullPointerException
- if intf
or config
is null
.java.lang.ClassCastException
- if the device configuration type specified by config
is not applicable to the device type specified by intf
.public static <P extends Device<? super P>> P open(int id) throws java.io.IOException, DeviceNotFoundException, UnavailableDeviceException
Device
instance for the provided numerical ID.
The device is opened in exclusive access mode.
A new instance is returned upon each call.P
- the type of the device to open.id
- the numerical device id.Device
instance for the given numerical ID.DeviceNotFoundException
- if the designated device is not found.UnavailableDeviceException
- if the designated device is not currently available - such as if it is
already open with exclusive access.java.io.IOException
- if any other I/O error occurred.java.lang.SecurityException
- if the caller has no permission to access the designated device (see
DeviceMgmtPermission.OPEN
).java.lang.IllegalArgumentException
- if id
is less than 0
.public static <P extends Device<? super P>> P open(int id, java.lang.Class<P> intf) throws java.io.IOException, UnsupportedDeviceTypeException, DeviceNotFoundException, UnavailableDeviceException
Device
instance for the provided numerical ID and type.
The device is opened in exclusive access mode.
A new instance is returned upon each call.P
- the type of the device to open.id
- the numerical device id.intf
- the interface (sub-interface of Device
) of the device being looked
up.Device
instance for the given numerical ID.DeviceNotFoundException
- if the designated device is not found.UnsupportedDeviceTypeException
- if the designated device type is not supported.UnavailableDeviceException
- if the designated device is not currently available - such as if it is
already open with exclusive access.java.io.IOException
- if any other I/O error occurred.java.lang.SecurityException
- if the caller has no permission to access the designated device (see
DeviceMgmtPermission.OPEN
).java.lang.IllegalArgumentException
- if id
is less than 0
.java.lang.NullPointerException
- if intf
is null
.public static <P extends Device<? super P>> P open(int id, java.lang.Class<P> intf, int mode) throws java.io.IOException, UnsupportedDeviceTypeException, DeviceNotFoundException, UnavailableDeviceException, UnsupportedAccessModeException
Device
instance for the provided numerical ID and type.
The device is opened in the designated access mode. A device may be
opened in shared mode if supported by the underlying driver and hardware and if it is not
already opened in exclusive mode. A device may be opened in exclusive mode if
supported by the underlying driver and hardware and if it is not already opened.
A new instance is returned upon each call.P
- the type of the device to open.id
- the numerical device id.intf
- the interface (sub-interface of Device
) of the device being looked
up.mode
- the access mode, one of: EXCLUSIVE
or SHARED
.Device
instance for the given numerical ID.UnsupportedDeviceTypeException
- if the designated device type is not supported.DeviceNotFoundException
- if the designated device is not found.UnsupportedDeviceTypeException
- if the designated device type is not supported.UnavailableDeviceException
- if the designated device is not currently available - such as when it is
already open in an access mode incompatible with the requested access mode.UnsupportedAccessModeException
- if the requested access mode is not supported.java.io.IOException
- if any other I/O error occurred.java.lang.SecurityException
- if the caller has no permission to access the designated device (see
DeviceMgmtPermission.OPEN
).java.lang.IllegalArgumentException
- if id
is less than 0
.java.lang.NullPointerException
- if intf
is null
.public static <P extends Device<? super P>> P open(int id, int mode) throws java.io.IOException, DeviceNotFoundException, UnavailableDeviceException, UnsupportedAccessModeException
Device
instance for the provided numerical ID.
The device is opened in the designated access mode. A device may be
opened in shared mode if supported by the underlying driver and hardware and if it is not
already opened in exclusive mode. A device may be opened in exclusive mode if
supported by the underlying driver and hardware and if it is not already opened.
A new instance is returned upon each call.P
- the type of the device to open.id
- the numerical device id.mode
- the access mode, one of: EXCLUSIVE
or SHARED
.Device
instance for the given numerical ID.DeviceNotFoundException
- if the designated device is not found.UnavailableDeviceException
- if the designated device is not currently available - such as when it is
already open in an access mode incompatible with the requested access mode.UnsupportedAccessModeException
- if the requested access mode is not supported.java.io.IOException
- if any other I/O error occurred.java.lang.SecurityException
- if the caller has no permission to access the designated device (see
DeviceMgmtPermission.OPEN
).java.lang.IllegalArgumentException
- if id
is less than 0
.public static <P extends Device<? super P>> P open(DeviceConfig<? super P> config) throws java.io.IOException, InvalidDeviceConfigException, UnsupportedDeviceTypeException, DeviceNotFoundException, UnavailableDeviceException
Device
instance with the specified hardware addressing information and
configuration. The type of the device is inferred from the configuration type. An
instance of the first available matching Device
. If a matching
Device
is already open (therefore not available) the next matching
Device
is considered.
The device is opened in exclusive access mode.
Note that the returned Device
instance's ID and name are undefined.
A new instance is returned upon each call.P
- the type of the device to open.config
- the device configuration (which includes hardware addressing information as
well as configuration parameters).Device
instance for the specified configuration.UnsupportedDeviceTypeException
- if the designated device type is not supported.InvalidDeviceConfigException
- if the provided device configuration (as defined by the configuration
parameters) is not valid/supported.DeviceNotFoundException
- if the device designated by the hardware addressing information is not found.UnavailableDeviceException
- if the designated device is not currently available - such as if it is
already open with exclusive access.java.io.IOException
- if any other I/O error occurred.java.lang.SecurityException
- if the caller has no permission to access the designated device (see
DevicePermission.OPEN
).java.lang.NullPointerException
- if config
is null
.public static <P extends Device<? super P>> P open(DeviceConfig<? super P> config, int mode) throws java.io.IOException, InvalidDeviceConfigException, UnsupportedDeviceTypeException, DeviceNotFoundException, UnavailableDeviceException, UnsupportedAccessModeException
Device
instance with the specified hardware addressing information and
configuration. The type of the device is inferred from the configuration type. An
instance of the first available matching Device
is returned. If a
matching Device
is already open in a mode that is not compatible with the
requested mode the next matching Device
is considered.
The device is opened in the designated access mode. A device may be
opened in shared mode if supported by the underlying driver and hardware and if it is not
already opened in exclusive mode. A device may be opened in exclusive mode if
supported by the underlying driver and hardware and if it is not already opened.
Note that the returned Device
instance's ID and name are undefined.
A new instance is returned upon each call.config
- the device configuration (which includes hardware addressing information as
well as configuration parameters).mode
- the access mode, one of: EXCLUSIVE
or SHARED
.Device
instance for the specified configuration.UnsupportedDeviceTypeException
- if the designated device type is not supported.InvalidDeviceConfigException
- if the provided device configuration (as defined by the configuration
parameters) is not valid/supported or when opened in shared mode, if the provided
device configuration is incompatible with the currently open configuration of
the device.DeviceNotFoundException
- if the device designated by the hardware addressing information is not found.UnavailableDeviceException
- if the designated device is not currently available - such as if it is
already open with exclusive access.UnsupportedAccessModeException
- if the requested access mode is not supported.java.io.IOException
- if any other I/O error occurred.java.lang.SecurityException
- if the caller has no permission to access the designated device (see
DevicePermission.OPEN
).java.lang.NullPointerException
- if config
is null
.public static <P extends Device<? super P>> P open(java.lang.String name, java.lang.Class<P> intf, int mode, java.lang.String... properties) throws java.io.IOException, UnsupportedDeviceTypeException, DeviceNotFoundException, UnavailableDeviceException, UnsupportedAccessModeException
Device
instance for the specified name, type and/or
properties. An instance of the first available matching Device
is
returned. If a matching Device
is already open in a mode that is not compatible
with the requested mode the next matching Device
is considered.
The device is opened in the designated access mode. A device may be
opened in shared mode if supported by the underlying driver and hardware and if it is not
already opened in exclusive mode. A device may be opened in exclusive mode if
supported by the underlying driver and hardware and if it is not already opened.
A new instance is returned upon each call.
Property-based lookup only uses exact (case-insensitive) matching and does not perform any
semantic interpretation.P
- the type of the device to open.name
- the device name; may be null
.intf
- the interface (sub-interface of Device
) of the device being looked
up.mode
- the access mode, one of: EXCLUSIVE
or SHARED
.properties
- the list of required properties; may be null
.Device
instance for the given name and required properties.UnsupportedDeviceTypeException
- if the designated device type is not supported.DeviceNotFoundException
- if the designated device is not found.UnavailableDeviceException
- if the designated device is not currently available - such as when it is
already open in an access mode incompatible with the requested access mode.UnsupportedAccessModeException
- if the requested access mode is not supported.java.io.IOException
- if any other I/O error occurred.java.lang.SecurityException
- if the caller has no permission to access the designated device (see
DeviceMgmtPermission.OPEN
).java.lang.IllegalArgumentException
- if both name
is null
and properties
is empty.java.lang.NullPointerException
- if intf
is null
.public static <P extends Device<? super P>> P open(java.lang.String name, java.lang.Class<P> intf, java.lang.String... properties) throws java.io.IOException, UnsupportedDeviceTypeException, DeviceNotFoundException, UnavailableDeviceException
Device
instance for the specified name, type and/or
properties. An instance of the first available matching Device
is
returned. If a matching Device
is already open (therefore not available)
the next matching Device
is considered.
The device is opened in exclusive access mode.
A new instance is returned upon each call.
Property-based lookup only uses exact (case-insensitive) matching and does not perform any
semantic interpretation.P
- the type of the device to open.name
- the device name; may be null
.intf
- the interface (sub-interface of Device
) of the device being looked
up.properties
- the list of required properties; may be null
.Device
instance for the given name and required properties.UnsupportedDeviceTypeException
- if the designated device type is not supported.DeviceNotFoundException
- if the designated device is not found.UnavailableDeviceException
- if the designated device is not currently available - such as if it is
already open with exclusive access.java.io.IOException
- if any other I/O error occurred.java.lang.SecurityException
- if the caller has no permission to access the designated device (see
DeviceMgmtPermission.OPEN
).java.lang.IllegalArgumentException
- if both name
is null
and properties
is empty.java.lang.NullPointerException
- if intf
is null
.public static <P extends Device<? super P>> int register(int id, java.lang.Class<P> intf, DeviceConfig<? super P> config, java.lang.String name, java.lang.String... properties) throws java.io.IOException, UnsupportedDeviceTypeException, InvalidDeviceConfigException, DeviceNotFoundException, DeviceAlreadyExistsException
RegistrationListener
instances registered for the type of the registered device
are notified.
An implementation of the DeviceManager
must guarantee that an application
registering a device is the first one to get notified (in the event it has registered a
RegistrationListener
for that type of devices).
The designated device may be probed to check if the provided configuration is valid.
Prior to registering a new device of a certain type the
DeviceMgmtPermission
is checked with the action
DeviceMgmtPermission.REGISTER
. GPIOPin
is to be registered the DeviceMgmtPermission
is checked with a target
name composed of the requested device name and ID and with the action
DeviceMgmtPermission#REGISTER DeviceMgmtPermission.REGISTER
.
The following is an example of how this method may be used to register a new UART with Modem
control lines: Note that this method cannot be used to register a device of a custom type. If an attempt is made to register a custom implementation ofDeviceManager.register(10, // the device ID ModemUART.class, // the device type/interface new UARTConfig(0, 2400, UARTConfig.DATABITS_8, UARTConfig.PARITY_EVEN, UARTConfig.STOPBITS_1, UARTConfig.FLOWCONTROL_NONE), // the device configuration "MODEM", // the device name "com.foobar.modem.xxx=true", "com.foobar.modem.yyy=true" // the device capabilities );
DeviceConfig
a UnsupportedDeviceTypeException
will be thrown.P
- the type of the device to be registered.id
- the device ID; if id
is equal to UNSPECIFIED_ID
a free ID
will be allocated.intf
- the interface (sub-interface of Device
) of the device to be
registered.config
- the device configuration.name
- the name of the device to be registered; may be null
.properties
- the list of properties/capabilities of the device to be registered; may be
null
.InvalidDeviceConfigException
- if the provided device configuration (as defined by the configuration
parameters) is not valid/supported.UnsupportedDeviceTypeException
- if the designated device type is not supported.DeviceNotFoundException
- if the device designated by the hardware addressing information is not found.DeviceAlreadyExistsException
- if id
is already assigned to a device.java.io.IOException
- if any other I/O error occurred.java.lang.NullPointerException
- if intf
or config
is null
.java.lang.IllegalArgumentException
- if id
is less than 0
and is not equal to UNSPECIFIED_ID
.java.lang.UnsupportedOperationException
- if registering a new device is not supported - such as may be the case
on a platform supporting only a closed device topology.java.lang.SecurityException
- if the caller does not have the required permission (see
DeviceMgmtPermission.REGISTER
).java.lang.ClassCastException
- if the device configuration type specified by config
is not applicable to the device type specified by intf
.public static void unregister(int id)
RegistrationListener
instances registered for the type of the
device that has been unregistered are notified.
Some devices are registered by the underlying platform and cannot be unregistered.
Unregistration of a device has no side effect on its currently open
Device
instances. These Device
instances especially retain the
device ID that was assigned to them at the time they were open.
This method returns silently if the provided ID does not correspond to a registered
device.id
- the ID of the device to unregister.java.lang.IllegalArgumentException
- if id
is less than 0
or if id
corresponds to a device
device registered by the platform.java.lang.SecurityException
- if the caller does not have the required permission (see
DeviceMgmtPermission.UNREGISTER
).public static <P extends Device<? super P>> void addRegistrationListener(RegistrationListener<P> listener, java.lang.Class<P> intf)
P
- the type of the device to be listened for.listener
- the registration listener.intf
- the interface (sub-interface of Device
) of the devices to be
listened for.java.lang.NullPointerException
- if listener
or intf
is null
.public static <P extends Device<? super P>> void removeRegistrationListener(RegistrationListener<P> listener, java.lang.Class<P> intf)
P
- the type of the device listened for.listener
- the registration listener.intf
- the interface (sub-interface of Device
) of the devices listened
for.java.lang.NullPointerException
- if listener
or intf
is null
.Copyright © 2012, 2014, Oracle and/or its affiliates. All rights reserved.
Legal Notices