// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2017 Intel Corporation. All Rights Reserved.
namespace Intel.RealSense
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.InteropServices;
///
/// The device object represents a physical camera and provides the means to manipulate it.
///
public class Device : Base.RefCountedPooledObject
{
protected static Hashtable refCountTable = new Hashtable();
protected static readonly object tableLock = new object();
internal override void Initialize()
{
lock (tableLock)
{
if (refCountTable.Contains(Handle))
refCount = refCountTable[Handle] as Base.RefCount;
else
{
refCount = new Base.RefCount();
refCountTable[Handle] = refCount;
}
Retain();
}
Info = new InfoCollection(NativeMethods.rs2_supports_device_info, NativeMethods.rs2_get_device_info, Handle);
}
protected override void Dispose(bool disposing)
{
if (m_instance.IsInvalid)
{
return;
}
lock (tableLock)
{
IntPtr localHandle = Handle;
System.Diagnostics.Debug.Assert(refCountTable.Contains(localHandle));
base.Dispose(disposing);
if (refCount.count == 0)
{
refCountTable.Remove(localHandle);
}
}
}
internal Device(IntPtr ptr)
: base(ptr, null)
{
this.Initialize();
}
internal Device(IntPtr ptr, Base.Deleter deleter)
: base(ptr, deleter)
{
this.Initialize();
}
internal static T Create(IntPtr ptr)
where T : Device
{
return ObjectPool.Get(ptr);
}
internal static T Create(IntPtr ptr, Base.Deleter deleter)
where T : Device
{
var dev = ObjectPool.Get(ptr);
dev.Reset(ptr, deleter);
return dev;
}
///
/// Gets camera specific information, like versions of various internal components
///
public InfoCollection Info { get; private set; }
///
/// create a static snapshot of all connected devices at the time of the call
///
/// The list of sensors
public ReadOnlyCollection QuerySensors()
{
object error;
var ptr = NativeMethods.rs2_query_sensors(Handle, out error);
using (var sl = new SensorList(ptr))
{
var a = new Sensor[sl.Count];
sl.CopyTo(a, 0);
return Array.AsReadOnly(a);
}
}
///
/// Gets a static snapshot of all connected devices at the time of the call
///
/// The list of sensors
public ReadOnlyCollection Sensors => QuerySensors();
///
/// Send hardware reset request to the device. The actual reset is asynchronous.
/// Note: Invalidates all handles to this device.
///
public void HardwareReset()
{
object error;
NativeMethods.rs2_hardware_reset(Handle, out error);
}
/// Test if the given device can be extended to the requested extension.
/// The extension to which the device should be tested if it is extendable
/// Non-zero value iff the device can be extended to the given extension
public bool Is(Extension extension)
{
object error;
return NativeMethods.rs2_is_device_extendable_to(Handle, extension, out error) != 0;
}
public T As()
where T : Device
{
return Device.Create(Handle);
}
}
}