From: Ankur Date: Tue, 24 May 2016 06:47:36 +0000 (+0530) Subject: Introduce Sensor implementation X-Git-Tag: submit/tizen/20161214.063015~34 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=60cac354cd93e627efdb95daf3fd4cddebd2026f;p=platform%2Fcore%2Fcsapi%2Fsensor.git Introduce Sensor implementation Patch-Set-3 Changes as per reviews/comments. Auto-implemented properties, dont throw exception in the error factory, protected/private tags, enums as per 3.0 Patch-Set-4 Changes as per reviews/comments Separate function for destroying handles, renaming of one function etc. Patch-Set-5: Minor Changes Patch-Set-6: Minor Changes Patch-Set-7: Changes as per review - removed exceptions from getters. Patch-Set-8: Removed Stopwatch Patch-Set-9: Minor Changes Patch-Set-10: Minor Changes as per reviews/comments signed-off-by:"Ankur Garg " Change-Id: I15ba1686d8954165781d4a8d4a63ff71b9a92d08 --- diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f6f9a65 --- /dev/null +++ b/Makefile @@ -0,0 +1,24 @@ +ASM_DIRS := Tizen.System.Sensor +ASM_DLLS := $(addsuffix .dll,$(ASM_DIRS)) + +FLAGS := + +ALL: $(ASM_DLLS) + +define make-dll +$(eval ASM = $(strip $1)) +$(eval SRC = $(shell find $(ASM) -path $(ASM)/obj -prune -o -name '*.cs' -print)) +$(eval PKG = $(shell echo $2 | tr ' ' ',')) +$(ASM).dll: $(SRC) + @echo "[BUILD] $$@" + @mcs /nologo /out:$$@ /t:library /keyfile:$(ASM)/$(ASM).snk $(addprefix /pkg:,$(PKG)) $(FLAGS) $(SRC) + @echo "[CHECK] $$@" + @RET=`mono-shlib-cop $$@`; \ + CNT=`echo $$$$RET | grep -e '^error:' | wc -l`; \ + if [ $$$$CNT -gt 0 ]; then echo $$$$RET; rm -f $$@ exit 1; fi +endef + +$(eval $(call make-dll, Tizen.System.Sensor, csapi-tizen)) + +clean: + @rm -f $(ASM_DLLS) diff --git a/Tizen.System.Sensor.sln b/Tizen.System.Sensor.sln new file mode 100644 index 0000000..7e415e3 --- /dev/null +++ b/Tizen.System.Sensor.sln @@ -0,0 +1,53 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tizen.System.Sensor", "Tizen.System.Sensor\Tizen.System.Sensor.csproj", "{CB655C6A-F73B-448E-913C-CA4DCBC5E401}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tizen.Internals", "..\tizen\Tizen.Internals\Tizen.Internals.csproj", "{B9AA1CB2-F72D-4A30-A33B-A20C850A38A0}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {B9AA1CB2-F72D-4A30-A33B-A20C850A38A0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B9AA1CB2-F72D-4A30-A33B-A20C850A38A0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B9AA1CB2-F72D-4A30-A33B-A20C850A38A0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B9AA1CB2-F72D-4A30-A33B-A20C850A38A0}.Release|Any CPU.Build.0 = Release|Any CPU + {CB655C6A-F73B-448E-913C-CA4DCBC5E401}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CB655C6A-F73B-448E-913C-CA4DCBC5E401}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CB655C6A-F73B-448E-913C-CA4DCBC5E401}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CB655C6A-F73B-448E-913C-CA4DCBC5E401}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(MonoDevelopProperties) = preSolution + StartupItem = Tizen.System.Sensor\Tizen.System.Sensor.csproj + Policies = $0 + $0.DotNetNamingPolicy = $1 + $1.DirectoryNamespaceAssociation = None + $1.ResourceNamePolicy = FileFormatDefault + $0.TextStylePolicy = $2 + $2.inheritsSet = null + $2.scope = text/x-csharp + $0.CSharpFormattingPolicy = $3 + $3.inheritsSet = Mono + $3.inheritsScope = text/x-csharp + $3.scope = text/x-csharp + $0.StandardHeader = $4 + $4.Text = + $4.IncludeInNewFiles = True + $0.TextStylePolicy = $5 + $5.EolMarker = Unix + $5.inheritsSet = VisualStudio + $5.inheritsScope = text/plain + $5.scope = text/plain + $0.TextStylePolicy = $6 + $6.inheritsSet = Mono + $6.inheritsScope = text/plain + $6.scope = application/xml + $0.XmlFormattingPolicy = $7 + $7.inheritsSet = Mono + $7.inheritsScope = application/xml + $7.scope = application/xml + EndGlobalSection +EndGlobal diff --git a/Tizen.System.Sensor.userprefs b/Tizen.System.Sensor.userprefs new file mode 100644 index 0000000..2a833c1 --- /dev/null +++ b/Tizen.System.Sensor.userprefs @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Tizen.System.Sensor/Interop/Interop.Libraries.cs b/Tizen.System.Sensor/Interop/Interop.Libraries.cs new file mode 100644 index 0000000..2b42537 --- /dev/null +++ b/Tizen.System.Sensor/Interop/Interop.Libraries.cs @@ -0,0 +1,16 @@ +// Copyright 2016 by Samsung Electronics, Inc., +// +// This software is the confidential and proprietary information +// of Samsung Electronics, Inc. ("Confidential Information"). You +// shall not disclose such Confidential Information and shall use +// it only in accordance with the terms of the license agreement +// you entered into with Samsung. + +internal static partial class Interop +{ + internal static partial class Libraries + { + public const string Sensor = "libcapi-system-sensor.so.0"; + public const string Libc = "libc.so.6"; + } +} \ No newline at end of file diff --git a/Tizen.System.Sensor/Interop/Interop.Sensor.cs b/Tizen.System.Sensor/Interop/Interop.Sensor.cs new file mode 100644 index 0000000..1777244 --- /dev/null +++ b/Tizen.System.Sensor/Interop/Interop.Sensor.cs @@ -0,0 +1,128 @@ +// Copyright 2016 by Samsung Electronics, Inc., +// +// This software is the confidential and proprietary information +// of Samsung Electronics, Inc. ("Confidential Information"). You +// shall not disclose such Confidential Information and shall use +// it only in accordance with the terms of the license agreement +// you entered into with Samsung. + +using System; +using System.Runtime.InteropServices; +using Tizen.System.Sensor; + +internal static partial class Interop +{ + internal static class Sensor + { + //Sensor Hardware CAPI + [DllImport(Libraries.Sensor, EntryPoint = "sensor_get_name")] + internal static extern int GetName(IntPtr sensorhandle, out String name); + + [DllImport(Libraries.Sensor, EntryPoint = "sensor_get_vendor")] + internal static extern int GetVendor(IntPtr sensorHandle, out String vendor); + + [DllImport(Libraries.Sensor, EntryPoint = "sensor_get_type")] + internal static extern int GetType(IntPtr sensorHandle, out SensorType type); + + [DllImport(Libraries.Sensor, EntryPoint = "sensor_get_min_range")] + internal static extern int GetMinRange(IntPtr sensorHandle, out float minRange); + + [DllImport(Libraries.Sensor, EntryPoint = "sensor_get_max_range")] + internal static extern int GetMaxRange(IntPtr sensorHandle, out float maxRange); + + [DllImport(Libraries.Sensor, EntryPoint = "sensor_get_resolution")] + internal static extern int GetResolution(IntPtr sensorHandle, out float resolution); + + [DllImport(Libraries.Sensor, EntryPoint = "sensor_get_min_interval")] + internal static extern int GetMinInterval(IntPtr sensorHandle, out int minInterval); + + [DllImport(Libraries.Sensor, EntryPoint = "sensor_get_fifo_count")] + internal static extern int GetFifoCount(IntPtr sensorHandle, out int fifoCount); + + [DllImport(Libraries.Sensor, EntryPoint = "sensor_get_max_batch_count")] + internal static extern int GetMaxBatchCount(IntPtr sensorHandle, out int maxBatchCount); + } + internal static class SensorListener + { + //Sensor Listener CAPI + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void SensorEventCallback(IntPtr sensorHandle, IntPtr eventData, IntPtr data); + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void SensorAccuracyCallback(IntPtr sensorHandle, ulong timestamp, SensorDataAccuracy accuracy, IntPtr data); + + [DllImport(Libraries.Sensor, EntryPoint = "sensor_create_listener")] + internal static extern int CreateListener(IntPtr sensorHandle, out IntPtr listenerHandle); + + [DllImport(Libraries.Sensor, EntryPoint = "sensor_destroy_listener")] + internal static extern int DestroyListener(IntPtr listenerHandle); + + [DllImport(Libraries.Sensor, EntryPoint = "sensor_listener_start")] + internal static extern int StartListener(IntPtr listenerHandle); + + [DllImport(Libraries.Sensor, EntryPoint = "sensor_listener_stop")] + internal static extern int StopListener(IntPtr listenerHandle); + + [DllImport(Libraries.Sensor, EntryPoint = "sensor_listener_set_event_cb")] + internal static extern int SetEventCallback(IntPtr listenerHandle, uint intervalMs, SensorEventCallback callback, IntPtr data); + + [DllImport(Libraries.Sensor, EntryPoint = "sensor_listener_unset_event_cb")] + internal static extern int UnsetEventCallback(IntPtr listernerHandle); + + [DllImport(Libraries.Sensor, EntryPoint = "sensor_listener_set_accuracy_cb")] + internal static extern int SetAccuracyCallback(IntPtr listenerHandle, uint intervalMs, SensorAccuracyCallback callback, IntPtr data); + + [DllImport(Libraries.Sensor, EntryPoint = "sensor_listener_unset_accuracy_cb")] + internal static extern int UnsetAccuracyCallback(IntPtr listernerHandle); + + [DllImport(Libraries.Sensor, EntryPoint = "sensor_listener_set_interval")] + internal static extern int SetInterval(IntPtr listenerHandle, uint intervalMs); + + [DllImport(Libraries.Sensor, EntryPoint = "sensor_listener_set_max_batch_latency")] + internal static extern int SetMaxBatchLatency(IntPtr listenerHandle, uint maxBatchLatency); + + [DllImport(Libraries.Sensor, EntryPoint = "sensor_listener_set_attribute_int")] + internal static extern int SetAttribute(IntPtr listenerHandle, SensorAttribute sensorAttribute, int option); + } + + internal static class SensorManager + { + //Sensor Manager CAPI + [DllImport(Libraries.Sensor, EntryPoint = "sensor_is_supported")] + internal static extern int SensorIsSupported(SensorType type, out bool supported); + + [DllImport(Libraries.Sensor, EntryPoint = "sensor_get_default_sensor")] + internal static extern int GetDefaultSensor(SensorType type, out IntPtr sensor); + + [DllImport(Libraries.Sensor, EntryPoint = "sensor_get_sensor_list")] + internal static extern int GetSensorList(SensorType type, out IntPtr list, out int sensor_count); + } + + internal static partial class Libc + { + [DllImport(Libraries.Libc, EntryPoint = "free", CallingConvention = CallingConvention.Cdecl)] + internal static extern int Free(IntPtr ptr); + } + + [StructLayout(LayoutKind.Sequential, Pack = 0)] + internal struct SensorEventStruct + { + internal SensorDataAccuracy accuracy; + internal UInt64 timestamp; + internal int value_count; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] + internal float[] values; + } + + internal static IntPtr[] IntPtrToIntPtrArray(IntPtr unmanagedArray, int size) + { + IntPtr[] managedArray = new IntPtr[size]; + Marshal.Copy(unmanagedArray, managedArray, 0, size); + return managedArray; + } + + internal static SensorEventStruct IntPtrToEventStruct(IntPtr unmanagedVariable) + { + SensorEventStruct outStruct = (SensorEventStruct)Marshal.PtrToStructure(unmanagedVariable, typeof(SensorEventStruct)); + return outStruct; + } +} \ No newline at end of file diff --git a/Tizen.System.Sensor/Properties/AssemblyInfo.cs b/Tizen.System.Sensor/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..32b31bb --- /dev/null +++ b/Tizen.System.Sensor/Properties/AssemblyInfo.cs @@ -0,0 +1,27 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. + +[assembly: AssemblyTitle("Tizen.System.Sensor")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Samsung Electronics")] +[assembly: AssemblyProduct("Tizen.System.Sensor")] +[assembly: AssemblyCopyright("Copyright (c) 2015 Samsung Electronics Co., Ltd")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. + +[assembly: AssemblyVersion("1.0.0.0")] + +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. + +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] + diff --git a/Tizen.System.Sensor/Sensor/Enumerations.cs b/Tizen.System.Sensor/Sensor/Enumerations.cs new file mode 100755 index 0000000..9de4515 --- /dev/null +++ b/Tizen.System.Sensor/Sensor/Enumerations.cs @@ -0,0 +1,151 @@ +// Copyright 2016 by Samsung Electronics, Inc., +// +// This software is the confidential and proprietary information +// of Samsung Electronics, Inc. ("Confidential Information"). You +// shall not disclose such Confidential Information and shall use +// it only in accordance with the terms of the license agreement +// you entered into with Samsung. + +using System; + +namespace Tizen.System.Sensor +{ + /// + /// The SensorType Enum defintion for all sensor types. + /// + public enum SensorType + { + /// + /// All sensors. This can be used to retrieve Sensor class object for all available sensors. + /// + All = -1, + /// + /// Accelerometer sensor. + /// + Accelerometer = 0, + /// + /// Gravity sensor. + /// + GravitySensor = 1, + /// + /// Linear Acceleration sensor. + /// + LinearAccelerationSensor = 2, + /// + /// Magnetometer sensor. + /// + Magnetometer = 3, + /// + /// Rotation Vector sensor. + /// + RotationVectorSensor = 4, + /// + /// Orientation sensor. + /// + OrientationSensor = 5, + /// + /// Gyroscope sensor. + /// + Gyroscope = 6, + /// + /// Light sensor. + /// + LightSensor = 7, + /// + /// Proximity sensor. + /// + ProximitySensor = 8, + /// + /// Pressure sensor. + /// + PressureSensor = 9, + /// + /// Ultraviolet sensor. + /// + UltravioletSensor = 10, + /// + /// Temperature sensor. + /// + TemperatureSensor = 11, + /// + /// Humidity sensor. + /// + HumiditySensor = 12, + /// + /// Hear rate monitor sensor. + /// + HeartRateMonitor = 13, + /// + /// Uncalibrated Gyroscope sensor. + /// + UncalibratedGyroscope = 17, + /// + /// Uncalibrated Geomagnetic sensor. + /// + UncalibratedMagnetometer = 18, + /// + /// Gyroscope-based rotation vector sensor. + /// + GyroscopeRotationVectorSensor = 19, + /// + /// Geomagnetic-based rotation vector sensor. + /// + MagnetometerRotationVectorSensor = 20 + } + + /// + /// SensorDataAccuracy Enum definition for all possible sensor data accuracy Values. + /// + public enum SensorDataAccuracy + { + /// + /// Undefined sensor data accuracy. + /// + Undefined = -1, + /// + /// Sensor data not accurate. + /// + Bad = 0, + /// + /// Moderately accurate sensor data. + /// + Normal = 1, + /// + /// Highly accurate sensor data. + /// + Good = 2, + /// + /// Very highly accurate sensor data. + /// + VeryGood = 3 + } + + /// + /// Sensor Option Enum definition for sensor option Values + /// + public enum SensorPausePolicy + { + /// + /// Does not receive data when the LCD is off and in the power save mode. + /// + None, + /// + /// Receives data when the LCD is off. + /// + DisplayOff, + /// + /// Receives data in the power save mode. + /// + PowerSaveMode, + /// + /// Receives data when the LCD is off and in the power save mode. + /// + All + } + + public enum SensorAttribute + { + AxisOrientation, + PausePolicy + } +} diff --git a/Tizen.System.Sensor/Sensor/EventArgs/AccelerometerChangedEventArgs.cs b/Tizen.System.Sensor/Sensor/EventArgs/AccelerometerChangedEventArgs.cs new file mode 100644 index 0000000..84d2cee --- /dev/null +++ b/Tizen.System.Sensor/Sensor/EventArgs/AccelerometerChangedEventArgs.cs @@ -0,0 +1,40 @@ +// Copyright 2016 by Samsung Electronics, Inc., +// +// This software is the confidential and proprietary information +// of Samsung Electronics, Inc. ("Confidential Information"). You +// shall not disclose such Confidential Information and shall use +// it only in accordance with the terms of the license agreement +// you entered into with Samsung. + +using System; + +namespace Tizen.System.Sensor +{ + /// + /// Accelerometer changed event arguments. Class for storing the data returned by accelerometer + /// + public class AccelerometerChangedEventArgs : EventArgs + { + internal AccelerometerChangedEventArgs(float[] values) + { + X = values[0]; + Y = values[1]; + Z = values[2]; + } + + /// + /// Gets the X component of the acceleration. + /// + public float X { get; private set; } + + /// + /// Gets the Y component of the acceleration. + /// + public float Y { get; private set; } + + /// + /// Gets the Z component of the acceleration. + /// + public float Z { get; private set; } + } +} \ No newline at end of file diff --git a/Tizen.System.Sensor/Sensor/EventArgs/SensorAccuracyChangedEventArgs.cs b/Tizen.System.Sensor/Sensor/EventArgs/SensorAccuracyChangedEventArgs.cs new file mode 100644 index 0000000..126b555 --- /dev/null +++ b/Tizen.System.Sensor/Sensor/EventArgs/SensorAccuracyChangedEventArgs.cs @@ -0,0 +1,34 @@ +// Copyright 2016 by Samsung Electronics, Inc., +// +// This software is the confidential and proprietary information +// of Samsung Electronics, Inc. ("Confidential Information"). You +// shall not disclose such Confidential Information and shall use +// it only in accordance with the terms of the license agreement +// you entered into with Samsung. + +using System; + +namespace Tizen.System.Sensor +{ + /// + /// Sensor accuracy changed event arguments Class. Contains the parameters to be returned through accuracy callback + /// + public class SensorAccuracyChangedEventArgs : EventArgs + { + internal SensorAccuracyChangedEventArgs(DateTime timestamp, SensorDataAccuracy accuracy) + { + TimeStamp = timestamp; + Accuracy = accuracy; + } + + /// + /// Gets the time stamp. + /// + public DateTime TimeStamp { get; private set; } + + /// + /// Gets the accuracy. + /// + public SensorDataAccuracy Accuracy { get; private set; } + } +} \ No newline at end of file diff --git a/Tizen.System.Sensor/Sensor/Plugins/Accelerometer.cs b/Tizen.System.Sensor/Sensor/Plugins/Accelerometer.cs new file mode 100644 index 0000000..a0f9d3c --- /dev/null +++ b/Tizen.System.Sensor/Sensor/Plugins/Accelerometer.cs @@ -0,0 +1,125 @@ +// Copyright 2016 by Samsung Electronics, Inc., +// +// This software is the confidential and proprietary information +// of Samsung Electronics, Inc. ("Confidential Information"). You +// shall not disclose such Confidential Information and shall use +// it only in accordance with the terms of the license agreement +// you entered into with Samsung. + +using System; + +namespace Tizen.System.Sensor +{ + /// + /// Accelerometer Sensor Class. Used for registering callbacks for accelerometer and getting accelerometer data + /// /// + public class Accelerometer : Sensor + { + private uint _defaultInterval = 0; + + /// + /// Gets the X component of the acceleration. + /// + public float X { get; private set; } + + /// + /// Gets the Y component of the acceleration. + /// + public float Y { get; private set; } + + /// + /// Gets the Z component of the acceleration. + /// + public float Z { get; private set; } + + /// + /// Returns true or false based on whether accelerometer sensor is supported by device. + /// + public static bool IsSupported + { + get + { + return CheckIfSupported(); + } + } + + /// + /// Returns the number of accelerometer sensors available on the device. + /// + public static int Count + { + get + { + return GetCount(); + } + } + + /// + /// Initializes a new instance of the class. + /// + /// + /// Index. Default value for this is 0. Index refers to a particular accelerometer sensor in case of multiple sensors + /// + public Accelerometer(int index = 0) : base(index) + { + Interval = _defaultInterval; + } + + protected override SensorType GetSensorType() + { + return SensorType.Accelerometer; + } + + /// + /// Event Handler for storing the callback functions for event corresponding to change in accelerometer sensor data. + /// + + public event EventHandler DataUpdated; + + private static bool CheckIfSupported() + { + bool isSupported; + int error = Interop.SensorManager.SensorIsSupported(SensorType.Accelerometer, out isSupported); + if (error != 0) + isSupported = false; + return isSupported; + } + + private static int GetCount() + { + IntPtr list; + int count; + int error = Interop.SensorManager.GetSensorList(SensorType.Accelerometer, out list, out count); + if (error != 0) + count = 0; + else + Interop.Libc.Free(list); + return count; + } + + protected override void EventListenStart() + { + int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, SensorEventCallback, IntPtr.Zero); + if (error != 0) + throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for accelerometer"); + } + + protected override void EventListenStop() + { + int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle); + if (error != 0) + throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for accelerometer"); + } + + private void SensorEventCallback(IntPtr sensorHandle, IntPtr sensorPtr, IntPtr data) + { + Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(sensorPtr); + Timestamp = sensorData.timestamp; + X = sensorData.values[0]; + Y = sensorData.values[1]; + Z = sensorData.values[2]; + + DataUpdated?.Invoke(this, new AccelerometerChangedEventArgs(sensorData.values)); + } + } +} \ No newline at end of file diff --git a/Tizen.System.Sensor/Sensor/Sensor.cs b/Tizen.System.Sensor/Sensor/Sensor.cs new file mode 100644 index 0000000..af841b9 --- /dev/null +++ b/Tizen.System.Sensor/Sensor/Sensor.cs @@ -0,0 +1,392 @@ +// Copyright 2016 by Samsung Electronics, Inc., +// +// This software is the confidential and proprietary information +// of Samsung Electronics, Inc. ("Confidential Information"). You +// shall not disclose such Confidential Information and shall use +// it only in accordance with the terms of the license agreement +// you entered into with Samsung. + +using System; + +namespace Tizen.System.Sensor +{ + /// + /// Sensor class for storing hardware information about a particular sensor + /// + public abstract class Sensor : IDisposable + { + private string _name; + private string _vendor; + private float _minValue; + private float _maxValue; + private float _resolution; + private int _minInterval; + private int _fifoCount; + private int _maxBatchCount; + private bool _sensing = false; + private bool _disposed = false; + private UInt64 _timestamp; + private uint _interval = 0; + private uint _maxBatchLatency = 0; + private SensorPausePolicy _pausePolicy = SensorPausePolicy.None; + private IntPtr _sensorHandle = IntPtr.Zero; + private IntPtr _listenerHandle = IntPtr.Zero; + + protected abstract SensorType GetSensorType(); + protected abstract void EventListenStart(); + protected abstract void EventListenStop(); + + internal Sensor(int index) + { + SensorType type = GetSensorType(); + GetHandleList(type, index); + if (CheckSensorHandle()) + { + CreateListener(); + GetProperty(); + } + } + + ~Sensor() + { + Dispose(false); + } + + /// + /// Property: For getting the name of the sensor + /// + public string Name + { + get + { + return _name; + } + } + + /// + /// Property: Gets the vendor. + /// + public string Vendor + { + get + { + return _vendor; + } + } + + /// + /// Property: Gets the minimum value of range of sensor data. + /// + public float MinValue + { + get + { + return _minValue; + } + } + + /// + /// Property: Gets the maximum value of range of sensor data. + /// + public float MaxValue + { + get + { + return _maxValue; + } + } + + /// + /// Property: Gets the resolution. + /// + public float Resolution + { + get + { + return _resolution; + } + } + + /// + /// Property: Gets the minimum interval. + /// + public int MinInterval + { + get + { + return _minInterval; + } + } + + /// + /// Property: Gets the fifo count. + /// + public int FifoCount + { + get + { + return _fifoCount; + } + } + + /// + /// Property: Gets the maximum batch count. + /// + public int MaxBatchCount + { + get + { + return _maxBatchCount; + } + } + + /// + /// Sets the interval of the sensor for sensor data event + /// Callbacks will be called at frequency of this interval + /// + public uint Interval + { + set + { + _interval = value; + SetInterval(); + } + get + { + return _interval; + } + } + + /// + /// Sets the max batch latency for the sensor corresponding to the sensor data event. + /// + public uint MaxBatchLatency + { + set + { + _maxBatchLatency = value; + SetMaxBatchLatency(); + } + get + { + return _maxBatchLatency; + } + } + + /// + /// Gets the attribute. + /// + /// + public SensorPausePolicy PausePolicy + { + set + { + _pausePolicy = value; + SetPausePolicy(); + } + get + { + return _pausePolicy; + } + } + + public UInt64 Timestamp + { + set + { + _timestamp = value; + } + get + { + return _timestamp; + } + } + + public bool Sensing + { + get + { + return _sensing; + } + } + + protected IntPtr ListenerHandle + { + get + { + return _listenerHandle; + } + } + + /// + /// Starts the sensor. + /// After this the event handlers will start receiving events. + /// + public void Start() + { + if (CheckListenerHandle()) + { + int error = Interop.SensorListener.StartListener(_listenerHandle); + if (error != 0) + throw SensorErrorFactory.CheckAndThrowException(error, "Unable to Start Sensor Listener"); + EventListenStart(); + _sensing = true; + } + } + + /// + /// Stop the sensor. + /// After this the event handlers will stop receiving the events + /// + public void Stop() + { + if (!_sensing) + { + int error = Interop.SensorListener.StopListener(_listenerHandle); + if (error != 0) + throw SensorErrorFactory.CheckAndThrowException(error, "Unable to Stop Sensor Listener"); + EventListenStop(); + _sensing = false; + } + else + { + throw new InvalidOperationException("Operation Failed: Sensor already stopped"); + } + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) + { + if (_disposed) + return; + + DestroyHandles(); + _disposed = true; + } + + private void GetHandleList(SensorType type, int index) + { + IntPtr list; + IntPtr[] sensorList; + int count; + int error = Interop.SensorManager.GetSensorList(type, out list, out count); + if (error != 0) + throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.GetSensorList Failed"); + sensorList = Interop.IntPtrToIntPtrArray(list, count); + _sensorHandle = sensorList[index]; + Interop.Libc.Free(list); + } + + private void GetProperty() + { + int error = (int)SensorErrorFactory.SensorError.None; + + error = Interop.Sensor.GetName(_sensorHandle, out _name); + if (error != 0) + throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.Name Failed"); + + error = Interop.Sensor.GetVendor(_sensorHandle, out _vendor); + if (error != 0) + throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.Vendor Failed"); + + error = Interop.Sensor.GetMinRange(_sensorHandle, out _minValue); + if (error != 0) + throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.MinValue Failed"); + + error = Interop.Sensor.GetMaxRange(_sensorHandle, out _maxValue); + if (error != 0) + throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.MaxValue Failed"); + + error = Interop.Sensor.GetResolution(_sensorHandle, out _resolution); + if (error != 0) + throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.Resolution Failed"); + + error = Interop.Sensor.GetMinInterval(_sensorHandle, out _minInterval); + if (error != 0) + throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.MinInterval Failed"); + + error = Interop.Sensor.GetFifoCount(_sensorHandle, out _fifoCount); + if (error != 0) + throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.FifoCount Failed"); + + error = Interop.Sensor.GetMaxBatchCount(_sensorHandle, out _maxBatchCount); + if (error != 0) + throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.MaxBatchCount Failed"); + } + + private void CreateListener() + { + int error = Interop.SensorListener.CreateListener(_sensorHandle, out _listenerHandle); + if (error != 0) + throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.CreateListener Failed"); + } + + private void SetInterval() + { + if (CheckListenerHandle()) + { + int error = Interop.SensorListener.SetInterval(_listenerHandle, _interval); + if (error != 0) + throw SensorErrorFactory.CheckAndThrowException(error, "Setting Sensor.PausePolicy Failed"); + } + } + + private void SetMaxBatchLatency() + { + if (CheckListenerHandle()) + { + int error = Interop.SensorListener.SetMaxBatchLatency(_listenerHandle, _maxBatchLatency); + if (error != 0) + throw SensorErrorFactory.CheckAndThrowException(error, "Setting Sensor.MaxBatchLatency Failed"); + } + } + + private void SetPausePolicy() + { + if (CheckListenerHandle()) + { + int error = Interop.SensorListener.SetAttribute(_listenerHandle, SensorAttribute.PausePolicy, (int)_pausePolicy); + if (error != 0) + throw SensorErrorFactory.CheckAndThrowException(error, "Setting Sensor.PausePolicy Failed"); + } + } + + private bool CheckListenerHandle() + { + bool result = false; + if (_listenerHandle != IntPtr.Zero) + { + result = true; + } + else + { + throw new ArgumentException("Invalid Parameter: Sensor is null"); + } + return result; + } + + private bool CheckSensorHandle() + { + bool result = false; + if (_sensorHandle != IntPtr.Zero) + { + result = true; + } + else + { + throw new ArgumentException("Invalid Parameter: Sensor is null"); + } + return result; + } + + private void DestroyHandles() + { + Interop.SensorListener.DestroyListener(_listenerHandle); + } + } +} diff --git a/Tizen.System.Sensor/Sensor/SensorErrorFactory.cs b/Tizen.System.Sensor/Sensor/SensorErrorFactory.cs new file mode 100644 index 0000000..620f42d --- /dev/null +++ b/Tizen.System.Sensor/Sensor/SensorErrorFactory.cs @@ -0,0 +1,54 @@ +// Copyright 2016 by Samsung Electronics, Inc., +// +// This software is the confidential and proprietary information +// of Samsung Electronics, Inc. ("Confidential Information"). You +// shall not disclose such Confidential Information and shall use +// it only in accordance with the terms of the license agreement +// you entered into with Samsung. + +using System; +using Tizen.Internals.Errors; + +namespace Tizen.System.Sensor +{ + internal static class SensorErrorFactory + { + internal enum SensorError + { + None = ErrorCode.None, + IOError = ErrorCode.IoError, + InvalidParameter = ErrorCode.InvalidParameter, + NotSupported = ErrorCode.NotSupported, + PermissionDenied = ErrorCode.PermissionDenied, + OutOfMemory = ErrorCode.OutOfMemory, + NotNeedCalibration = -0x02440000 | 0x03, + OperationFailed = -0x02440000 | 0x06 + } + + static internal Exception CheckAndThrowException(int error, string msg) + { + SensorError e = (SensorError)error; + switch (e) + { + case SensorError.None: + return null; + case SensorError.IOError: + return new InvalidOperationException("I/O Error: " + msg); + case SensorError.InvalidParameter: + return new ArgumentException("Invalid Parameter: " + msg); + case SensorError.NotSupported: + return new InvalidOperationException("Not Supported: " + msg); + case SensorError.PermissionDenied: + return new InvalidOperationException("Permission Denied: " + msg); + case SensorError.OutOfMemory: + return new InvalidOperationException("Out of Memory: " + msg); + case SensorError.NotNeedCalibration: + return new InvalidOperationException("Sensor doesn't need calibration: " + msg); + case SensorError.OperationFailed: + return new InvalidOperationException("Operation Failed: " + msg); + default: + return new InvalidOperationException("Unknown Error Code: " + msg); + } + } + } +} \ No newline at end of file diff --git a/Tizen.System.Sensor/Tizen.System.Sensor.csproj b/Tizen.System.Sensor/Tizen.System.Sensor.csproj new file mode 100644 index 0000000..4856c17 --- /dev/null +++ b/Tizen.System.Sensor/Tizen.System.Sensor.csproj @@ -0,0 +1,67 @@ + + + + Debug + AnyCPU + 10.0.0 + 2.0 + {CB655C6A-F73B-448E-913C-CA4DCBC5E401} + Library + Tizen.System.Sensor + Tizen.System.Sensor + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + false + + + none + false + bin\Release + prompt + 4 + false + + + + + + + + + + + + + + + + + + + {B9AA1CB2-F72D-4A30-A33B-A20C850A38A0} + Tizen.Internals + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Tizen.System.Sensor/Tizen.System.Sensor.pidb b/Tizen.System.Sensor/Tizen.System.Sensor.pidb new file mode 100644 index 0000000..78ae299 Binary files /dev/null and b/Tizen.System.Sensor/Tizen.System.Sensor.pidb differ diff --git a/Tizen.System.Sensor/Tizen.System.Sensor.snk b/Tizen.System.Sensor/Tizen.System.Sensor.snk new file mode 100644 index 0000000..e480db4 Binary files /dev/null and b/Tizen.System.Sensor/Tizen.System.Sensor.snk differ diff --git a/Tizen.System.Sensor/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache b/Tizen.System.Sensor/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache new file mode 100644 index 0000000..ece1079 Binary files /dev/null and b/Tizen.System.Sensor/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache differ diff --git a/packaging/csapi-system.sensor.manifest b/packaging/csapi-system.sensor.manifest new file mode 100755 index 0000000..75b0fa5 --- /dev/null +++ b/packaging/csapi-system.sensor.manifest @@ -0,0 +1,5 @@ + + + + + diff --git a/packaging/csapi-system.sensor.pc.in b/packaging/csapi-system.sensor.pc.in new file mode 100755 index 0000000..72354d8 --- /dev/null +++ b/packaging/csapi-system.sensor.pc.in @@ -0,0 +1,5 @@ +Name: csapi-system-sensor +Description: Tizen Sensor API for C# +Version: @version@ +Libs: -r:@dllpath@/@dllname@ +Requires: diff --git a/packaging/csapi-system.sensor.spec b/packaging/csapi-system.sensor.spec new file mode 100644 index 0000000..9c1bb71 --- /dev/null +++ b/packaging/csapi-system.sensor.spec @@ -0,0 +1,86 @@ +%define dllpath %{_libdir}/mono/tizen +%define dllname Tizen.System.Sensor.dll + +Name: csapi-system.sensor +Summary: Tizen Sensor API for C# +Version: 1.0.0 +Release: 1 +Group: Development/Libraries +License: Apache-2.0 +URL: https://www.tizen.org +Source0: %{name}-%{version}.tar.gz +Source1: %{name}.manifest +Source2: %{name}.pc.in + +# TODO: replace mono-compiler, mono-devel to mcs, mono-shlib-cop +BuildRequires: mono-compiler +BuildRequires: mono-devel +#BuildRequires: mcs +#BuildRequires: mono-shlib-cop +BuildRequires: pkgconfig(csapi-tizen) +BuildRequires: pkgconfig(glib-2.0) + +# TODO: replace mono-core to gacutil. +# mono-core should provide the symbol 'gacutil' +Requires(post): mono-core +Requires(postun): mono-core + +# P/Invoke Dependencies +BuildRequires: pkgconfig(capi-system-sensor) + +# P/Invoke Runtime Dependencies +# TODO: It should be removed after fix tizen-rpm-config +Requires: capi-system-sensor +# DLL Dependencies +#BuildRequires: ... + +%description +Tizen Sensor API for C# + +%package devel +Summary: Development package for %{name} +Group: Development/Libraries +Requires: %{name} = %{version}-%{release} + +%description devel +Development package for %{name} + +%prep +%setup -q + +cp %{SOURCE1} . + +%build +# build dll +mcs -target:library -out:%{dllname} -keyfile:Tizen.System.Sensor/Tizen.System.Sensor.snk -pkg:'csapi-tizen'\ + Tizen.System.Sensor/Properties/AssemblyInfo.cs \ + Tizen.System.Sensor/Sensor/Plugins/*.cs \ + Tizen.System.Sensor/Sensor/EventArgs/*.cs \ + Tizen.System.Sensor/Sensor/*.cs \ + Tizen.System.Sensor/Interop/*.cs +# check p/invoke +if [ -x %{dllname} ]; then + RET=`mono-shlib-cop %{dllname}`; \ + CNT=`echo $RET | grep -E "^error:" | wc -l`; \ + if [ $CNT -gt 0 ]; then exit 1; fi +fi + +%install +# copy dll +mkdir -p %{buildroot}%{dllpath} +install -p -m 644 %{dllname} %{buildroot}%{dllpath} + +# generate pkgconfig +mkdir -p %{buildroot}%{_libdir}/pkgconfig +sed -e "s#@version@#%{version}#g" \ + -e "s#@dllpath@#%{dllpath}#g" \ + -e "s#@dllname@#%{dllname}#g" \ + %{SOURCE2} > %{buildroot}%{_libdir}/pkgconfig/%{name}.pc +%post +gacutil -i %{dllpath}/%{dllname} + +%files +%{dllpath}/%{dllname} + +%files devel +%{_libdir}/pkgconfig/%{name}.pc