e02cad346081a7da95f583602820c974fb95162c
[platform/core/system/sensord.git] / src / sensor / auto_rotation / auto_rotation_sensor.cpp
1 /*
2  * sensord
3  *
4  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include "auto_rotation_sensor.h"
21
22 #include <sensor_log.h>
23 #include <sensor_types.h>
24
25 #include "auto_rotation_alg_emul.h"
26
27 #define NAME_SENSOR "http://tizen.org/sensor/general/auto_rotation/tizen_default"
28 #define NAME_VENDOR "tizen.org"
29
30 #define SRC_ID_ACC  0x1
31 #define SRC_STR_ACC "http://tizen.org/sensor/general/accelerometer"
32
33 static sensor_info2_t sensor_info = {
34         id: 0x1,
35         type: AUTO_ROTATION_SENSOR,
36         uri: NAME_SENSOR,
37         vendor: NAME_VENDOR,
38         min_range: AUTO_ROTATION_DEGREE_UNKNOWN,
39         max_range: AUTO_ROTATION_DEGREE_270,
40         resolution: 1,
41         min_interval: 60,
42         max_batch_count: 0,
43         wakeup_supported: false,
44         privilege:"",
45 };
46
47 static required_sensor_s required_sensors[] = {
48         {SRC_ID_ACC, SRC_STR_ACC}
49 };
50
51 auto_rotation_sensor::auto_rotation_sensor()
52 : m_rotation(0)
53 , m_rotation_time(0)
54 , m_alg(NULL)
55 {
56         if (!init())
57                 throw OP_ERROR;
58 }
59
60 auto_rotation_sensor::~auto_rotation_sensor()
61 {
62         deinit();
63 }
64
65 bool auto_rotation_sensor::init(void)
66 {
67         m_alg = get_alg();
68         retvm_if(!m_alg, false, "Not supported");
69         retvm_if(!m_alg->open(), false, "Cannot open auto rotation algorithm");
70         return true;
71 }
72
73 void auto_rotation_sensor::deinit(void)
74 {
75         delete m_alg;
76 }
77
78 int auto_rotation_sensor::get_sensor_info(const sensor_info2_t **info)
79 {
80         *info = &sensor_info;
81         return OP_SUCCESS;
82 }
83
84 int auto_rotation_sensor::get_required_sensors(const required_sensor_s **sensors)
85 {
86         *sensors = required_sensors;
87         return 1;
88 }
89
90 int auto_rotation_sensor::update(uint32_t id, sensor_data_t *data, int len)
91 {
92         int rotation;
93         float acc[3];
94         acc[0] = data->values[0];
95         acc[1] = data->values[1];
96         acc[2] = data->values[2];
97
98         if (!m_alg->get_rotation(acc, data->timestamp, m_rotation, rotation))
99                 return OP_ERROR;
100
101         _D("Rotation: %d, ACC[0]: %f, ACC[1]: %f, ACC[2]: %f",
102                 rotation, data->values[0], data->values[1], data->values[2]);
103
104         m_rotation = rotation;
105         m_rotation_time = data->timestamp;
106
107         return OP_SUCCESS;
108 }
109
110 int auto_rotation_sensor::get_data(sensor_data_t **data, int *length)
111 {
112         sensor_data_t *sensor_data;
113         sensor_data = (sensor_data_t *)malloc(sizeof(sensor_data_t));
114
115         sensor_data->accuracy = SENSOR_ACCURACY_GOOD;
116         sensor_data->timestamp = m_rotation_time;
117         sensor_data->values[0] = m_rotation;
118         sensor_data->value_count = 1;
119
120         *data = sensor_data;
121         *length = sizeof(sensor_data_t);
122
123         return 1;
124 }
125
126 int auto_rotation_sensor::start(observer_h ob)
127 {
128         m_rotation = AUTO_ROTATION_DEGREE_UNKNOWN;
129
130         /* TODO: cache */
131
132         m_alg->start();
133
134         /* if OP_DEFAULT is returned,
135          * this function is not called anymore before stop() is called */
136         return OP_DEFAULT;
137 }
138
139 int auto_rotation_sensor::stop(observer_h ob)
140 {
141         m_alg->stop();
142
143         /* if OP_DEFAULT is returned,
144          * this function is not called anymore before start() is called */
145         return OP_DEFAULT;
146 }
147
148 int auto_rotation_sensor::set_interval(observer_h ob, int32_t &interval)
149 {
150         /* Fix internal */
151         interval = 60;
152         return OP_SUCCESS;
153 }
154
155 auto_rotation_alg *auto_rotation_sensor::get_alg(void)
156 {
157         auto_rotation_alg *alg = new(std::nothrow) auto_rotation_alg_emul();
158         retvm_if(!alg, NULL, "Failed to allocate memory");
159
160         return alg;
161 }