2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 namespace Tizen.Sensor
22 internal static class Globals
24 internal const string LogTag = "Tizen.Sensor";
28 /// The Sensor class is used for storing the hardware information about a particular sensor.
30 public abstract class Sensor : IDisposable
33 private string _vendor;
34 private float _minValue;
35 private float _maxValue;
36 private float _resolution;
37 private int _minInterval;
38 private int _fifoCount;
39 private int _maxBatchCount;
40 private bool _isSensing = false;
41 private bool _disposed = false;
42 private TimeSpan _timeSpan;
43 private uint _interval = 0;
44 private uint _maxBatchLatency = 0;
45 private SensorPausePolicy _pausePolicy = SensorPausePolicy.None;
46 private IntPtr _sensorHandle = IntPtr.Zero;
47 private IntPtr _listenerHandle = IntPtr.Zero;
49 internal abstract SensorType GetSensorType();
50 internal abstract void EventListenStart();
51 internal abstract void EventListenStop();
53 internal Sensor(uint index)
55 SensorType type = GetSensorType();
56 GetHandleList(type, index);
57 if (CheckSensorHandle())
65 /// Destroy the Sensor object.
73 /// Property: Gets the name of the sensor.
75 /// <since_tizen> 3 </since_tizen>
76 /// <value> The name of the sensor. </value>
81 Log.Info(Globals.LogTag, "Getting the sensor name");
87 /// Property: Gets the vendor.
89 /// <since_tizen> 3 </since_tizen>
90 /// <value> The vendor name of the sensor. </value>
95 Log.Info(Globals.LogTag, "Getting the sensor vendor name");
101 /// Property: Gets the minimum value of the range of the sensor data.
103 /// <since_tizen> 3 </since_tizen>
104 /// <value> The lower bound of the range of the sensor reading. </value>
105 public float MinValue
109 Log.Info(Globals.LogTag, "Getting the min value of the sensor");
115 /// Property: Gets the maximum value of the range of the sensor data.
117 /// <since_tizen> 3 </since_tizen>
118 /// <value> The upper bound of the range of the sensor reading. </value>
119 public float MaxValue
123 Log.Info(Globals.LogTag, "Getting the max value of the sensor");
129 /// Property: Gets the resolution.
131 /// <since_tizen> 3 </since_tizen>
132 /// <value> The resolution. </value>
133 public float Resolution
137 Log.Info(Globals.LogTag, "Getting the resolution of the sensor");
143 /// Property: Gets the minimum interval.
145 /// <since_tizen> 3 </since_tizen>
146 /// <value> The minimum update interval. </value>
147 public int MinInterval
151 Log.Info(Globals.LogTag, "Getting the min interval for the sensor");
157 /// Property: Gets the FIFO count.
159 /// <since_tizen> 3 </since_tizen>
160 /// <value> The size of the hardware FIFO. </value>
165 Log.Info(Globals.LogTag, "Getting the fifo count of the sensor");
171 /// Property: Gets the maximum batch count.
173 /// <since_tizen> 3 </since_tizen>
174 /// <value> The maximum batch count. </value>
175 public int MaxBatchCount
179 Log.Info(Globals.LogTag, "Getting the max batch count of the sensor");
180 return _maxBatchCount;
185 /// Sets the interval of the sensor for the sensor data event.
186 /// Callbacks will be called at the frequency of this interval.
188 /// <since_tizen> 3 </since_tizen>
189 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
190 /// <value> The interval of the sensor. </value>
195 Log.Info(Globals.LogTag, "Setting the interval of the sensor");
201 Log.Info(Globals.LogTag, "Getting the interval of the sensor");
207 /// Sets the maximum batch latency for the sensor corresponding to the sensor data event.
209 /// <since_tizen> 3 </since_tizen>
210 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
211 /// <value> The maximum batch latency. </value>
212 public uint MaxBatchLatency
216 Log.Info(Globals.LogTag, "Setting the max batch latency of the sensor");
217 _maxBatchLatency = value;
218 SetMaxBatchLatency();
222 Log.Info(Globals.LogTag, "Getting the max batch latency of the sensor");
223 return _maxBatchLatency;
228 /// Sets the pause policy of the sensor.
230 /// <since_tizen> 3 </since_tizen>
231 /// <value>The pause policy.</value>
232 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
233 /// <value> The pause policy. </value>
234 public SensorPausePolicy PausePolicy
238 Log.Info(Globals.LogTag, "Setting the pause policy of the sensor");
239 _pausePolicy = value;
240 SetAttribute(SensorAttribute.PausePolicy, (int)_pausePolicy);
244 Log.Info(Globals.LogTag, "Getting the pause policy of the sensor");
250 /// Gets or sets the time span.
252 /// <since_tizen> 3 </since_tizen>
253 /// <value> The time span. </value>
254 public TimeSpan TimeSpan
258 Log.Info(Globals.LogTag, "Setting the timespan of the sensor values");
263 Log.Info(Globals.LogTag, "Getting the timespan of the sensor values");
269 /// Indicates whether this sensor is sensing.
271 /// <since_tizen> 3 </since_tizen>
272 /// <value><c>true</c> if this sensor is sensing; otherwise <c>false</c>.</value>
273 public bool IsSensing
277 Log.Info(Globals.LogTag, "Checking if the sensor is started");
282 internal IntPtr ListenerHandle
286 return _listenerHandle;
290 internal static bool CheckIfSupported(SensorType type, String key)
292 bool isSupported = false;
293 bool error = Information.TryGetValue(key, out isSupported);
295 if (!error || !isSupported)
297 Log.Error(Globals.LogTag, "Error checking if sensor is supported(systeminfo)");
301 int ret = Interop.SensorManager.SensorIsSupported(type, out isSupported);
302 if (ret != (int)SensorError.None)
304 Log.Error(Globals.LogTag, "Error checking if sensor is supported");
312 /// Starts the sensor.
313 /// After this, event handlers will start receiving events.
315 /// <since_tizen> 3 </since_tizen>
316 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
319 Log.Info(Globals.LogTag, "Starting the sensor");
320 if (CheckListenerHandle())
322 int error = Interop.SensorListener.StartListener(_listenerHandle);
323 if (error != (int)SensorError.None)
325 Log.Error(Globals.LogTag, "Error starting sensor");
326 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to Start Sensor Listener");
330 Log.Info(Globals.LogTag, "Sensor started");
335 /// Stops the sensor.
336 /// After this, event handlers will stop receiving events.
338 /// <since_tizen> 3 </since_tizen>
339 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
342 Log.Info(Globals.LogTag, "Stopping the sensor");
345 int error = Interop.SensorListener.StopListener(_listenerHandle);
346 if (error != (int)SensorError.None)
348 Log.Error(Globals.LogTag, "Error stopping the sensor");
349 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to Stop Sensor Listener");
353 Log.Info(Globals.LogTag, "Sensor stopped");
358 /// Destroy the current object.
360 public void Dispose()
363 GC.SuppressFinalize(this);
367 /// Releases all resources currently used by this instance.
369 /// <param name="disposing">
370 /// true if managed resources should be disposed
371 /// otherwise, false.
373 protected virtual void Dispose(bool disposing)
382 internal void SetAttribute(SensorAttribute attribute, int option)
384 if (CheckListenerHandle())
386 int error = Interop.SensorListener.SetAttribute(_listenerHandle, attribute, option);
387 if (error != (int)SensorError.None)
389 Log.Error(Globals.LogTag, "Error setting sensor pause policy");
390 throw SensorErrorFactory.CheckAndThrowException(error, "Setting Sensor.PausePolicy Failed");
395 private void GetHandleList(SensorType type, uint index)
400 int error = Interop.SensorManager.GetSensorList(type, out list, out count);
401 if (error != (int)SensorError.None)
403 Log.Error(Globals.LogTag, "Error getting sensor list");
404 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.GetSensorList Failed");
406 sensorList = Interop.IntPtrToIntPtrArray(list, count);
407 _sensorHandle = sensorList[index];
408 Interop.Libc.Free(list);
411 private void GetProperty()
413 int error = (int)SensorError.None;
415 error = Interop.Sensor.GetName(_sensorHandle, out _name);
416 if (error != (int)SensorError.None)
418 Log.Error(Globals.LogTag, "Error getting sensor name");
419 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.Name Failed");
422 error = Interop.Sensor.GetVendor(_sensorHandle, out _vendor);
423 if (error != (int)SensorError.None)
425 Log.Error(Globals.LogTag, "Error getting sensor vendor name");
426 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.Vendor Failed");
429 error = Interop.Sensor.GetMinRange(_sensorHandle, out _minValue);
430 if (error != (int)SensorError.None)
432 Log.Error(Globals.LogTag, "Error getting sensor min value");
433 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.MinValue Failed");
436 error = Interop.Sensor.GetMaxRange(_sensorHandle, out _maxValue);
437 if (error != (int)SensorError.None)
439 Log.Error(Globals.LogTag, "Error getting sensor max value");
440 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.MaxValue Failed");
443 error = Interop.Sensor.GetResolution(_sensorHandle, out _resolution);
444 if (error != (int)SensorError.None)
446 Log.Error(Globals.LogTag, "Error getting sensor resolution");
447 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.Resolution Failed");
450 error = Interop.Sensor.GetMinInterval(_sensorHandle, out _minInterval);
451 if (error != (int)SensorError.None)
453 Log.Error(Globals.LogTag, "Error getting sensor min interval");
454 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.MinInterval Failed");
457 error = Interop.Sensor.GetFifoCount(_sensorHandle, out _fifoCount);
458 if (error != (int)SensorError.None)
460 Log.Error(Globals.LogTag, "Error getting sensor fifo count");
461 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.FifoCount Failed");
464 error = Interop.Sensor.GetMaxBatchCount(_sensorHandle, out _maxBatchCount);
465 if (error != (int)SensorError.None)
467 Log.Error(Globals.LogTag, "Error getting sensor max batch count");
468 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.MaxBatchCount Failed");
472 private void CreateListener()
474 int error = Interop.SensorListener.CreateListener(_sensorHandle, out _listenerHandle);
475 if (error != (int)SensorError.None)
477 Log.Error(Globals.LogTag, "Error cerating sensor listener handle");
478 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.CreateListener Failed");
482 private void SetInterval()
484 if (CheckListenerHandle())
488 int error = Interop.SensorListener.SetInterval(_listenerHandle, _interval);
489 if (error != (int)SensorError.None)
491 Log.Error(Globals.LogTag, "Error setting sensor interval");
492 throw SensorErrorFactory.CheckAndThrowException(error, "Setting Sensor.SetInterval Failed");
498 private void SetMaxBatchLatency()
500 if (CheckListenerHandle())
502 int error = Interop.SensorListener.SetMaxBatchLatency(_listenerHandle, _maxBatchLatency);
503 if (error != (int)SensorError.None)
505 Log.Error(Globals.LogTag, "Error setting max batch latency");
506 throw SensorErrorFactory.CheckAndThrowException(error, "Setting Sensor.MaxBatchLatency Failed");
511 private bool CheckListenerHandle()
514 if (_listenerHandle != IntPtr.Zero)
520 Log.Error(Globals.LogTag, "Sensor listener handle is null");
521 throw new ArgumentException("Invalid Parameter: Sensor is null");
526 private bool CheckSensorHandle()
529 if (_sensorHandle != IntPtr.Zero)
535 Log.Error(Globals.LogTag, "Sensor handle is null");
536 throw new ArgumentException("Invalid Parameter: Sensor is null");
541 private void DestroyHandles()
543 Interop.SensorListener.DestroyListener(_listenerHandle);