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 /// <since_tizen> 3 </since_tizen>
31 public abstract class Sensor : IDisposable
34 private string _vendor;
35 private float _minValue;
36 private float _maxValue;
37 private float _resolution;
38 private int _minInterval;
39 private int _fifoCount;
40 private int _maxBatchCount;
41 private bool _isSensing = false;
42 private bool _disposed = false;
43 private TimeSpan _timeSpan;
44 private uint _interval = 0;
45 private uint _maxBatchLatency = 0;
46 private SensorPausePolicy _pausePolicy = SensorPausePolicy.None;
47 private IntPtr _sensorHandle = IntPtr.Zero;
48 private IntPtr _listenerHandle = IntPtr.Zero;
51 /// Read a sensor data synchronously.
53 internal abstract void ReadData();
54 internal abstract SensorType GetSensorType();
55 internal abstract void EventListenStart();
56 internal abstract void EventListenStop();
58 internal Sensor(uint index)
60 SensorType type = GetSensorType();
61 GetHandleList(type, index);
62 if (CheckSensorHandle())
70 /// Destroy the Sensor object.
78 /// Property: Gets the name of the sensor.
80 /// <since_tizen> 3 </since_tizen>
81 /// <value> The name of the sensor. </value>
86 Log.Info(Globals.LogTag, "Getting the sensor name");
92 /// Property: Gets the vendor.
94 /// <since_tizen> 3 </since_tizen>
95 /// <value> The vendor name of the sensor. </value>
100 Log.Info(Globals.LogTag, "Getting the sensor vendor name");
106 /// Property: Gets the minimum value of the range of the sensor data.
108 /// <since_tizen> 3 </since_tizen>
109 /// <value> The lower bound of the range of the sensor reading. </value>
110 public float MinValue
114 Log.Info(Globals.LogTag, "Getting the min value of the sensor");
120 /// Property: Gets the maximum value of the range of the sensor data.
122 /// <since_tizen> 3 </since_tizen>
123 /// <value> The upper bound of the range of the sensor reading. </value>
124 public float MaxValue
128 Log.Info(Globals.LogTag, "Getting the max value of the sensor");
134 /// Property: Gets the resolution.
136 /// <since_tizen> 3 </since_tizen>
137 /// <value> The resolution. </value>
138 public float Resolution
142 Log.Info(Globals.LogTag, "Getting the resolution of the sensor");
148 /// Property: Gets the minimum interval.
150 /// <since_tizen> 3 </since_tizen>
151 /// <value> The minimum update interval. </value>
152 public int MinInterval
156 Log.Info(Globals.LogTag, "Getting the min interval for the sensor");
162 /// Property: Gets the FIFO count.
164 /// <since_tizen> 3 </since_tizen>
165 /// <value> The size of the hardware FIFO. </value>
170 Log.Info(Globals.LogTag, "Getting the fifo count of the sensor");
176 /// Property: Gets the maximum batch count.
178 /// <since_tizen> 3 </since_tizen>
179 /// <value> The maximum batch count. </value>
180 public int MaxBatchCount
184 Log.Info(Globals.LogTag, "Getting the max batch count of the sensor");
185 return _maxBatchCount;
190 /// Sets the interval of the sensor for the sensor data event.
191 /// Callbacks will be called at the frequency of this interval.
193 /// <since_tizen> 3 </since_tizen>
194 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
195 /// <value> The interval of the sensor. </value>
200 Log.Info(Globals.LogTag, "Setting the interval of the sensor");
206 Log.Info(Globals.LogTag, "Getting the interval of the sensor");
212 /// Sets the maximum batch latency for the sensor corresponding to the sensor data event.
214 /// <since_tizen> 3 </since_tizen>
215 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
216 /// <value> The maximum batch latency. </value>
217 public uint MaxBatchLatency
221 Log.Info(Globals.LogTag, "Setting the max batch latency of the sensor");
222 _maxBatchLatency = value;
223 SetMaxBatchLatency();
227 Log.Info(Globals.LogTag, "Getting the max batch latency of the sensor");
228 return _maxBatchLatency;
233 /// Sets the pause policy of the sensor.
235 /// <since_tizen> 3 </since_tizen>
236 /// <value>The pause policy.</value>
237 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
238 /// <value> The pause policy. </value>
239 public SensorPausePolicy PausePolicy
243 Log.Info(Globals.LogTag, "Setting the pause policy of the sensor");
244 _pausePolicy = value;
245 SetAttribute(SensorAttribute.PausePolicy, (int)_pausePolicy);
249 Log.Info(Globals.LogTag, "Getting the pause policy of the sensor");
255 /// Gets or sets the time span.
257 /// <since_tizen> 3 </since_tizen>
258 /// <value> The time span. </value>
259 public TimeSpan TimeSpan
263 Log.Info(Globals.LogTag, "Setting the timespan of the sensor values");
268 Log.Info(Globals.LogTag, "Getting the timespan of the sensor values");
274 /// Indicates whether this sensor is sensing.
276 /// <since_tizen> 3 </since_tizen>
277 /// <value><c>true</c> if this sensor is sensing; otherwise <c>false</c>.</value>
278 public bool IsSensing
282 Log.Info(Globals.LogTag, "Checking if the sensor is started");
287 internal IntPtr ListenerHandle
291 return _listenerHandle;
295 internal static bool CheckIfSupported(SensorType type, String key)
297 bool isSupported = false;
298 bool error = Information.TryGetValue(key, out isSupported);
300 if (!error || !isSupported)
302 Log.Error(Globals.LogTag, "Error checking if sensor is supported(systeminfo)");
306 int ret = Interop.SensorManager.SensorIsSupported(type, out isSupported);
307 if (ret != (int)SensorError.None)
309 Log.Error(Globals.LogTag, "Error checking if sensor is supported");
317 /// Starts the sensor.
318 /// After this, event handlers will start receiving events.
320 /// <since_tizen> 3 </since_tizen>
321 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
324 Log.Info(Globals.LogTag, "Starting the sensor");
325 if (CheckListenerHandle())
327 int error = Interop.SensorListener.StartListener(_listenerHandle);
328 if (error != (int)SensorError.None)
330 Log.Error(Globals.LogTag, "Error starting sensor");
331 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to Start Sensor Listener");
336 Log.Info(Globals.LogTag, "Sensor started");
341 /// Stops the sensor.
342 /// After this, event handlers will stop receiving events.
344 /// <since_tizen> 3 </since_tizen>
345 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
348 Log.Info(Globals.LogTag, "Stopping the sensor");
351 int error = Interop.SensorListener.StopListener(_listenerHandle);
352 if (error != (int)SensorError.None)
354 Log.Error(Globals.LogTag, "Error stopping the sensor");
355 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to Stop Sensor Listener");
359 Log.Info(Globals.LogTag, "Sensor stopped");
364 /// Destroy the current object.
366 /// <since_tizen> 3 </since_tizen>
367 public void Dispose()
370 GC.SuppressFinalize(this);
374 /// Releases all resources currently used by this instance.
376 /// <param name="disposing">
377 /// true if managed resources should be disposed
378 /// otherwise, false.
380 /// <since_tizen> 3 </since_tizen>
381 protected virtual void Dispose(bool disposing)
390 internal void SetAttribute(SensorAttribute attribute, int option)
392 if (CheckListenerHandle())
394 int error = Interop.SensorListener.SetAttribute(_listenerHandle, attribute, option);
395 if (error != (int)SensorError.None)
397 Log.Error(Globals.LogTag, "Error setting sensor pause policy");
398 throw SensorErrorFactory.CheckAndThrowException(error, "Setting Sensor.PausePolicy Failed");
403 private void GetHandleList(SensorType type, uint index)
408 int error = Interop.SensorManager.GetSensorList(type, out list, out count);
409 if (error != (int)SensorError.None)
411 Log.Error(Globals.LogTag, "Error getting sensor list");
412 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.GetSensorList Failed");
414 sensorList = Interop.IntPtrToIntPtrArray(list, count);
415 _sensorHandle = sensorList[index];
416 Interop.Libc.Free(list);
419 private void GetProperty()
421 int error = (int)SensorError.None;
423 error = Interop.Sensor.GetName(_sensorHandle, out _name);
424 if (error != (int)SensorError.None)
426 Log.Error(Globals.LogTag, "Error getting sensor name");
427 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.Name Failed");
430 error = Interop.Sensor.GetVendor(_sensorHandle, out _vendor);
431 if (error != (int)SensorError.None)
433 Log.Error(Globals.LogTag, "Error getting sensor vendor name");
434 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.Vendor Failed");
437 error = Interop.Sensor.GetMinRange(_sensorHandle, out _minValue);
438 if (error != (int)SensorError.None)
440 Log.Error(Globals.LogTag, "Error getting sensor min value");
441 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.MinValue Failed");
444 error = Interop.Sensor.GetMaxRange(_sensorHandle, out _maxValue);
445 if (error != (int)SensorError.None)
447 Log.Error(Globals.LogTag, "Error getting sensor max value");
448 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.MaxValue Failed");
451 error = Interop.Sensor.GetResolution(_sensorHandle, out _resolution);
452 if (error != (int)SensorError.None)
454 Log.Error(Globals.LogTag, "Error getting sensor resolution");
455 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.Resolution Failed");
458 error = Interop.Sensor.GetMinInterval(_sensorHandle, out _minInterval);
459 if (error != (int)SensorError.None)
461 Log.Error(Globals.LogTag, "Error getting sensor min interval");
462 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.MinInterval Failed");
465 error = Interop.Sensor.GetFifoCount(_sensorHandle, out _fifoCount);
466 if (error != (int)SensorError.None)
468 Log.Error(Globals.LogTag, "Error getting sensor fifo count");
469 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.FifoCount Failed");
472 error = Interop.Sensor.GetMaxBatchCount(_sensorHandle, out _maxBatchCount);
473 if (error != (int)SensorError.None)
475 Log.Error(Globals.LogTag, "Error getting sensor max batch count");
476 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.MaxBatchCount Failed");
480 private void CreateListener()
482 int error = Interop.SensorListener.CreateListener(_sensorHandle, out _listenerHandle);
483 if (error != (int)SensorError.None)
485 Log.Error(Globals.LogTag, "Error cerating sensor listener handle");
486 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.CreateListener Failed");
490 private void SetInterval()
492 if (CheckListenerHandle())
496 int error = Interop.SensorListener.SetInterval(_listenerHandle, _interval);
497 if (error != (int)SensorError.None)
499 Log.Error(Globals.LogTag, "Error setting sensor interval");
500 throw SensorErrorFactory.CheckAndThrowException(error, "Setting Sensor.SetInterval Failed");
506 private void SetMaxBatchLatency()
508 if (CheckListenerHandle())
510 int error = Interop.SensorListener.SetMaxBatchLatency(_listenerHandle, _maxBatchLatency);
511 if (error != (int)SensorError.None)
513 Log.Error(Globals.LogTag, "Error setting max batch latency");
514 throw SensorErrorFactory.CheckAndThrowException(error, "Setting Sensor.MaxBatchLatency Failed");
519 private bool CheckListenerHandle()
522 if (_listenerHandle != IntPtr.Zero)
528 Log.Error(Globals.LogTag, "Sensor listener handle is null");
529 throw new ArgumentException("Invalid Parameter: Sensor is null");
534 private bool CheckSensorHandle()
537 if (_sensorHandle != IntPtr.Zero)
543 Log.Error(Globals.LogTag, "Sensor handle is null");
544 throw new ArgumentException("Invalid Parameter: Sensor is null");
549 private void DestroyHandles()
551 Interop.SensorListener.DestroyListener(_listenerHandle);