sensord: check NULL if memory allocation is failed
[platform/core/system/sensord.git] / src / sensor / gesture / face_down_sensor.cpp
1 /*
2  * sensord
3  *
4  * Copyright (c) 2016 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 <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <errno.h>
24 #include <math.h>
25 #include <time.h>
26 #include <sys/types.h>
27 #include <dlfcn.h>
28
29 #include <sensor_log.h>
30 #include <sensor_types.h>
31
32 #include <sensor_common.h>
33 #include <virtual_sensor.h>
34 #include <face_down_sensor.h>
35 #include <sensor_loader.h>
36 #include <fusion_util.h>
37 #include <face_down_alg_impl.h>
38
39 #define SENSOR_NAME "SENSOR_FACE_DOWN"
40
41 #define SENSOR_FREQUENCY 50
42
43 face_down_sensor::face_down_sensor()
44 : m_gravity_sensor(NULL)
45 , m_alg(NULL)
46 , m_time(0)
47 , m_state(false)
48 , m_interval(SENSOR_INTERVAL_NORMAL)
49 {
50 }
51
52 face_down_sensor::~face_down_sensor()
53 {
54         _I("%s is destroyed!", SENSOR_NAME);
55 }
56
57 bool face_down_sensor::init(void)
58 {
59         m_gravity_sensor = sensor_loader::get_instance().get_sensor(GRAVITY_SENSOR);
60
61         if (!m_gravity_sensor) {
62                 _W("cannot load gravity sensor sensor[%s]", SENSOR_NAME);
63                 return false;
64         }
65
66         m_alg = get_alg();
67         if (!m_alg)
68                 return false;
69
70         _I("%s is created!", SENSOR_NAME);
71         return true;
72 }
73
74 sensor_type_t face_down_sensor::get_type(void)
75 {
76         return GESTURE_FACE_DOWN_SENSOR;
77 }
78
79 unsigned int face_down_sensor::get_event_type(void)
80 {
81         return CONVERT_TYPE_EVENT(GESTURE_FACE_DOWN_SENSOR);
82 }
83
84 const char *face_down_sensor::get_name(void)
85 {
86         return SENSOR_NAME;
87 }
88
89 bool face_down_sensor::get_sensor_info(sensor_info & info)
90 {
91         info.set_type(get_type());
92         info.set_id(get_id());
93         info.set_privilege(SENSOR_PRIVILEGE_PUBLIC);
94         info.set_name(get_name());
95         info.set_vendor("Samsung Electronics");
96         info.set_min_range(0);
97         info.set_max_range(1);
98         info.set_resolution(1);
99         info.set_min_interval(1);
100         info.set_fifo_count(0);
101         info.set_max_batch_count(0);
102         info.set_supported_event(get_event_type());
103         info.set_wakeup_supported(false);
104
105         return true;
106 }
107
108 void face_down_sensor::synthesize(const sensor_event_t & event)
109 {
110         if (event.event_type != GRAVITY_EVENT_RAW_DATA_REPORT_ON_TIME)
111                 return;
112
113         m_time = event.data->timestamp;
114         m_alg->push_event(event);
115         m_state = m_alg->get_face_down();
116         if (!m_state)
117                 return;
118
119         sensor_event_t *face_down_event;
120         sensor_data_t *face_down_data;
121         int data_length;
122
123         face_down_event = (sensor_event_t *)malloc(sizeof(sensor_event_t));
124         retm_if(!face_down_event, "Failed to allocate memory");
125
126         get_data(&face_down_data, &data_length);
127         face_down_event->sensor_id = get_id();
128         face_down_event->event_type = FACE_DOWN_RAW_DATA_EVENT;
129         face_down_event->data_length = data_length;
130         face_down_event->data = face_down_data;
131
132         push(face_down_event);
133
134         _I("[face_down_sensor] : True");
135 }
136
137 int face_down_sensor::get_data(sensor_data_t ** data, int *length)
138 {
139         sensor_data_t *sensor_data;
140         sensor_data = (sensor_data_t *)malloc(sizeof(sensor_data_t));
141         retvm_if(!sensor_data, -ENOMEM, "Failed to allocate memory");
142
143         sensor_data->accuracy = SENSOR_ACCURACY_GOOD;
144         sensor_data->timestamp = m_time;
145         sensor_data->value_count = 1;
146         sensor_data->values[0] = m_state;
147
148         *data = sensor_data;
149         *length = sizeof(sensor_data_t);
150
151         return 0;
152 }
153
154 bool face_down_sensor::set_interval(unsigned long interval)
155 {
156         m_interval = interval;
157         return true;
158 }
159
160 bool face_down_sensor::set_batch_latency(unsigned long latency)
161 {
162         return false;
163 }
164
165 bool face_down_sensor::on_start(void)
166 {
167         if (m_gravity_sensor)
168                 m_gravity_sensor->start();
169
170         m_time = 0;
171         m_state = false;
172         return activate();
173 }
174
175 bool face_down_sensor::on_stop(void)
176 {
177         if (m_gravity_sensor)
178                 m_gravity_sensor->stop();
179
180         m_time = 0;
181         m_state = false;
182
183         return deactivate();
184 }
185
186 bool face_down_sensor::add_interval(int client_id, unsigned int interval, bool is_processor)
187 {
188         m_gravity_sensor->add_interval(client_id, interval, true);
189         return sensor_base::add_interval(client_id, interval, is_processor);
190 }
191
192 bool face_down_sensor::delete_interval(int client_id, bool is_processor)
193 {
194         m_gravity_sensor->delete_interval(client_id, true);
195         return sensor_base::delete_interval(client_id, is_processor);
196 }
197
198 face_down_alg_impl *face_down_sensor::get_alg(void)
199 {
200         face_down_alg_impl *alg = new(std::nothrow) face_down_alg_impl();
201         retvm_if(!alg, NULL, "Failed to allocate memory");
202
203         return alg;
204 }