Tizen 2.0 Release
[framework/osp/uix.git] / src / FUixSensor_SensorUtil.cpp
1 // 
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd. 
4 // 
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file     FUixSensor_SensorUtil.cpp
20  * @brief    This is the implementation file for the %_SensorUtil class.
21  *
22  * This files contains implementation of the %_SensorUtil class.
23  */
24
25 // Includes
26 #include <FBaseSysLog.h>
27
28 #include <FApp_AppInfo.h>
29
30 #include "FUixSensor_SensorUtil.h"
31 #include "FUixSensor_ISensorCore.h"
32
33 using Tizen::App::_AppInfo;
34
35 namespace Tizen { namespace Uix { namespace Sensor
36 {
37 bool
38 _SensorUtil::IsSensorTypeValid(SensorType sensorType)
39 {
40         return (sensorType > SENSOR_TYPE_NONE) && (sensorType < SENSOR_TYPE_MAX);
41 }
42
43 result
44 _SensorUtil::CheckSensorTypeValidation(SensorType sensorType)
45 {
46         if (_AppInfo::GetApiVersion() == Tizen::Base::_API_VERSION_2_0 && _AppInfo::IsOspCompat())
47         {
48                 if (!IsSensorTypeValid(sensorType))
49                 {
50                         SetLastResult(E_DEVICE_UNAVAILABLE);
51                         return E_DEVICE_UNAVAILABLE;
52                 }
53
54                 if (!_ISensorCore::IsSupported(sensorType))
55                 {
56                         SetLastResult(E_DEVICE_UNAVAILABLE);
57                         return E_DEVICE_UNAVAILABLE;
58                 }
59         }
60         else
61         {
62                 SysTryReturn(NID_UIX, IsSensorTypeValid(sensorType), E_INVALID_ARG, E_INVALID_ARG, "Invalid SensorType [sensorType:%d]", sensorType);
63
64                 bool isSupported = _ISensorCore::IsSupported(sensorType);
65                 result r = GetLastResult();
66
67                 SysTryReturn(NID_UIX, !IsFailed(r), E_OPERATION_FAILED, E_OPERATION_FAILED, "The operation has failed [sensorType:%d]", sensorType);
68                 SysTryReturn(NID_UIX, isSupported, E_UNSUPPORTED_OPERATION, E_UNSUPPORTED_OPERATION, "Unsupported SensorType [sensorType:%d]", sensorType);
69         }
70
71         return E_SUCCESS;
72 }
73
74 result
75 _SensorUtil::CheckIntervalValidation(SensorType sensorType, long interval)
76 {
77         long min = 0L;
78         long max = 0L;
79         result r = _ISensorCore::GetIntervalRange(sensorType, min, max);
80         if (IsFailed(r))
81         {
82                 return r;
83         }
84
85         SysTryReturn(NID_UIX, (min <= interval) && (max >= interval), E_INVALID_ARG, E_INVALID_ARG, "Invalid interval [sensorType:%d, min:%ld, max:%ld, interval:%ld]", sensorType, min, max, interval);
86
87         return E_SUCCESS;
88 }
89
90 double
91 _SensorUtil::Atan2(double y, double x)
92 {
93         // reference: http://en.wikipedia.org/wiki/Atan2#Other_implementations.2Fcode_for_atan2
94         if (x > 0.0)
95         {
96                 return Tizen::Base::Utility::Math::Atan(y / x);
97         }
98
99         if ((y >= 0.0) && (x < 0.0))
100         {
101                 return Tizen::Base::Utility::Math::Atan(y / x) + PI;
102         }
103
104         if ((y < 0.0) && (x < 0.0))
105         {
106                 return Tizen::Base::Utility::Math::Atan(y / x) - PI;
107         }
108
109         if ((y > 0.0) && IsEquals(x, 0.0))
110         {
111                 return PI / 2;
112         }
113
114         if ((y < 0.0) && IsEquals(x, 0.0))
115         {
116                 return -PI / 2;
117         }
118
119         // Undefined case
120         return 0.0;
121 }
122
123 long
124 _SensorUtil::CutTimestamp(long long timestamp)
125 {
126         return (static_cast<unsigned long long>(timestamp) << BIT_FOR_TIMESTAMP_CUTTING) >> BIT_FOR_TIMESTAMP_CUTTING;
127 }
128
129 bool
130 _SensorUtil::IsObjectDetected(float distance)
131 {
132         return _ISensorCore::IsObjectDetected(distance);
133 }
134 } } }// Tizen::Uix::Sensor