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())
70 /// Property: Gets the name of the sensor.
72 /// <since_tizen> 3 </since_tizen>
73 /// <value> The name of the sensor. </value>
78 Log.Info(Globals.LogTag, "Getting the sensor name");
84 /// Property: Gets the vendor.
86 /// <since_tizen> 3 </since_tizen>
87 /// <value> The vendor name of the sensor. </value>
92 Log.Info(Globals.LogTag, "Getting the sensor vendor name");
98 /// Property: Gets the minimum value of the range of the sensor data.
100 /// <since_tizen> 3 </since_tizen>
101 /// <value> The lower bound of the range of the sensor reading. </value>
102 public float MinValue
106 Log.Info(Globals.LogTag, "Getting the min value of the sensor");
112 /// Property: Gets the maximum value of the range of the sensor data.
114 /// <since_tizen> 3 </since_tizen>
115 /// <value> The upper bound of the range of the sensor reading. </value>
116 public float MaxValue
120 Log.Info(Globals.LogTag, "Getting the max value of the sensor");
126 /// Property: Gets the resolution.
128 /// <since_tizen> 3 </since_tizen>
129 /// <value> The resolution. </value>
130 public float Resolution
134 Log.Info(Globals.LogTag, "Getting the resolution of the sensor");
140 /// Property: Gets the minimum interval.
142 /// <since_tizen> 3 </since_tizen>
143 /// <value> The minimum update interval. </value>
144 public int MinInterval
148 Log.Info(Globals.LogTag, "Getting the min interval for the sensor");
154 /// Property: Gets the FIFO count.
156 /// <since_tizen> 3 </since_tizen>
157 /// <value> The size of the hardware FIFO. </value>
162 Log.Info(Globals.LogTag, "Getting the fifo count of the sensor");
168 /// Property: Gets the maximum batch count.
170 /// <since_tizen> 3 </since_tizen>
171 /// <value> The maximum batch count. </value>
172 public int MaxBatchCount
176 Log.Info(Globals.LogTag, "Getting the max batch count of the sensor");
177 return _maxBatchCount;
182 /// Sets the interval of the sensor for the sensor data event.
183 /// Callbacks will be called at the frequency of this interval.
185 /// <since_tizen> 3 </since_tizen>
186 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
187 /// <value> The interval of the sensor. </value>
192 Log.Info(Globals.LogTag, "Setting the interval of the sensor");
198 Log.Info(Globals.LogTag, "Getting the interval of the sensor");
204 /// Sets the maximum batch latency for the sensor corresponding to the sensor data event.
206 /// <since_tizen> 3 </since_tizen>
207 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
208 /// <value> The maximum batch latency. </value>
209 public uint MaxBatchLatency
213 Log.Info(Globals.LogTag, "Setting the max batch latency of the sensor");
214 _maxBatchLatency = value;
215 SetMaxBatchLatency();
219 Log.Info(Globals.LogTag, "Getting the max batch latency of the sensor");
220 return _maxBatchLatency;
225 /// Sets the pause policy of the sensor.
227 /// <since_tizen> 3 </since_tizen>
228 /// <value>The pause policy.</value>
229 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
230 /// <value> The pause policy. </value>
231 public SensorPausePolicy PausePolicy
235 Log.Info(Globals.LogTag, "Setting the pause policy of the sensor");
236 _pausePolicy = value;
237 SetAttribute(SensorAttribute.PausePolicy, (int)_pausePolicy);
241 Log.Info(Globals.LogTag, "Getting the pause policy of the sensor");
247 /// Gets or sets the time span.
249 /// <since_tizen> 3 </since_tizen>
250 /// <value> The time span. </value>
251 public TimeSpan TimeSpan
255 Log.Info(Globals.LogTag, "Setting the timespan of the sensor values");
260 Log.Info(Globals.LogTag, "Getting the timespan of the sensor values");
266 /// Indicates whether this sensor is sensing.
268 /// <since_tizen> 3 </since_tizen>
269 /// <value><c>true</c> if this sensor is sensing; otherwise <c>false</c>.</value>
270 public bool IsSensing
274 Log.Info(Globals.LogTag, "Checking if the sensor is started");
279 internal IntPtr ListenerHandle
283 return _listenerHandle;
287 internal static bool CheckIfSupported(SensorType type, String key)
289 bool isSupported = false;
290 bool error = SystemInfo.TryGetValue(key, out isSupported);
292 if (!error || !isSupported)
294 Log.Error(Globals.LogTag, "Error checking if sensor is supported(systeminfo)");
298 int ret = Interop.SensorManager.SensorIsSupported(type, out isSupported);
299 if (ret != (int)SensorError.None)
301 Log.Error(Globals.LogTag, "Error checking if sensor is supported");
309 /// Starts the sensor.
310 /// After this, event handlers will start receiving events.
312 /// <since_tizen> 3 </since_tizen>
313 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
316 Log.Info(Globals.LogTag, "Starting the sensor");
317 if (CheckListenerHandle())
319 int error = Interop.SensorListener.StartListener(_listenerHandle);
320 if (error != (int)SensorError.None)
322 Log.Error(Globals.LogTag, "Error starting sensor");
323 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to Start Sensor Listener");
327 Log.Info(Globals.LogTag, "Sensor started");
332 /// Stops the sensor.
333 /// After this, event handlers will stop receiving events.
335 /// <since_tizen> 3 </since_tizen>
336 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
339 Log.Info(Globals.LogTag, "Stopping the sensor");
342 int error = Interop.SensorListener.StopListener(_listenerHandle);
343 if (error != (int)SensorError.None)
345 Log.Error(Globals.LogTag, "Error stopping the sensor");
346 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to Stop Sensor Listener");
350 Log.Info(Globals.LogTag, "Sensor stopped");
354 public void Dispose()
357 GC.SuppressFinalize(this);
360 protected virtual void Dispose(bool disposing)
369 internal void SetAttribute(SensorAttribute attribute, int option)
371 if (CheckListenerHandle())
373 int error = Interop.SensorListener.SetAttribute(_listenerHandle, attribute, option);
374 if (error != (int)SensorError.None)
376 Log.Error(Globals.LogTag, "Error setting sensor pause policy");
377 throw SensorErrorFactory.CheckAndThrowException(error, "Setting Sensor.PausePolicy Failed");
382 private void GetHandleList(SensorType type, uint index)
387 int error = Interop.SensorManager.GetSensorList(type, out list, out count);
388 if (error != (int)SensorError.None)
390 Log.Error(Globals.LogTag, "Error getting sensor list");
391 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.GetSensorList Failed");
393 sensorList = Interop.IntPtrToIntPtrArray(list, count);
394 _sensorHandle = sensorList[index];
395 Interop.Libc.Free(list);
398 private void GetProperty()
400 int error = (int)SensorError.None;
402 error = Interop.Sensor.GetName(_sensorHandle, out _name);
403 if (error != (int)SensorError.None)
405 Log.Error(Globals.LogTag, "Error getting sensor name");
406 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.Name Failed");
409 error = Interop.Sensor.GetVendor(_sensorHandle, out _vendor);
410 if (error != (int)SensorError.None)
412 Log.Error(Globals.LogTag, "Error getting sensor vendor name");
413 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.Vendor Failed");
416 error = Interop.Sensor.GetMinRange(_sensorHandle, out _minValue);
417 if (error != (int)SensorError.None)
419 Log.Error(Globals.LogTag, "Error getting sensor min value");
420 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.MinValue Failed");
423 error = Interop.Sensor.GetMaxRange(_sensorHandle, out _maxValue);
424 if (error != (int)SensorError.None)
426 Log.Error(Globals.LogTag, "Error getting sensor max value");
427 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.MaxValue Failed");
430 error = Interop.Sensor.GetResolution(_sensorHandle, out _resolution);
431 if (error != (int)SensorError.None)
433 Log.Error(Globals.LogTag, "Error getting sensor resolution");
434 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.Resolution Failed");
437 error = Interop.Sensor.GetMinInterval(_sensorHandle, out _minInterval);
438 if (error != (int)SensorError.None)
440 Log.Error(Globals.LogTag, "Error getting sensor min interval");
441 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.MinInterval Failed");
444 error = Interop.Sensor.GetFifoCount(_sensorHandle, out _fifoCount);
445 if (error != (int)SensorError.None)
447 Log.Error(Globals.LogTag, "Error getting sensor fifo count");
448 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.FifoCount Failed");
451 error = Interop.Sensor.GetMaxBatchCount(_sensorHandle, out _maxBatchCount);
452 if (error != (int)SensorError.None)
454 Log.Error(Globals.LogTag, "Error getting sensor max batch count");
455 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.MaxBatchCount Failed");
459 private void CreateListener()
461 int error = Interop.SensorListener.CreateListener(_sensorHandle, out _listenerHandle);
462 if (error != (int)SensorError.None)
464 Log.Error(Globals.LogTag, "Error cerating sensor listener handle");
465 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.CreateListener Failed");
469 private void SetInterval()
471 if (CheckListenerHandle())
475 int error = Interop.SensorListener.SetInterval(_listenerHandle, _interval);
476 if (error != (int)SensorError.None)
478 Log.Error(Globals.LogTag, "Error setting sensor interval");
479 throw SensorErrorFactory.CheckAndThrowException(error, "Setting Sensor.SetInterval Failed");
485 private void SetMaxBatchLatency()
487 if (CheckListenerHandle())
489 int error = Interop.SensorListener.SetMaxBatchLatency(_listenerHandle, _maxBatchLatency);
490 if (error != (int)SensorError.None)
492 Log.Error(Globals.LogTag, "Error setting max batch latency");
493 throw SensorErrorFactory.CheckAndThrowException(error, "Setting Sensor.MaxBatchLatency Failed");
498 private bool CheckListenerHandle()
501 if (_listenerHandle != IntPtr.Zero)
507 Log.Error(Globals.LogTag, "Sensor listener handle is null");
508 throw new ArgumentException("Invalid Parameter: Sensor is null");
513 private bool CheckSensorHandle()
516 if (_sensorHandle != IntPtr.Zero)
522 Log.Error(Globals.LogTag, "Sensor handle is null");
523 throw new ArgumentException("Invalid Parameter: Sensor is null");
528 private void DestroyHandles()
530 Interop.SensorListener.DestroyListener(_listenerHandle);