sensor: check NULL if memory allocation is failed
[platform/core/system/sensord.git] / src / sensor / linear_accel / linear_accel_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 "linear_accel_sensor.h"
21
22 #include <sensor_log.h>
23 #include <sensor_types.h>
24 #include <fusion_util.h>
25
26 #define NAME_SENSOR "http://tizen.org/sensor/general/linear_acceleration/tizen_default"
27 #define NAME_VENDOR "tizen.org"
28
29 #define SRC_ID_ACC   0x1
30 #define SRC_STR_ACC  "http://tizen.org/sensor/general/accelerometer"
31
32 #define SRC_ID_GRAVITY  0x2
33 #define SRC_STR_GRAVITY "http://tizen.org/sensor/general/gravity"
34
35 #define GRAVITY 9.80665
36
37 static sensor_info2_t sensor_info = {
38         id: 0x1,
39         type: LINEAR_ACCEL_SENSOR,
40         uri: NAME_SENSOR,
41         vendor: NAME_VENDOR,
42         min_range: -19.6,
43         max_range: 19.6,
44         resolution: 0.01,
45         min_interval: 5,
46         max_batch_count: 0,
47         wakeup_supported: false,
48         privilege:"",
49 };
50
51 static required_sensor_s required_sensors[] = {
52         {SRC_ID_ACC,     SRC_STR_ACC},
53         {SRC_ID_GRAVITY, SRC_STR_GRAVITY},
54 };
55
56 linear_accel_sensor::linear_accel_sensor()
57 : m_x(0)
58 , m_y(0)
59 , m_z(0)
60 , m_gx(0)
61 , m_gy(0)
62 , m_gz(0)
63 , m_accuracy(0)
64 , m_time(0)
65 {
66 }
67
68 linear_accel_sensor::~linear_accel_sensor()
69 {
70 }
71
72 int linear_accel_sensor::get_sensor_info(const sensor_info2_t **info)
73 {
74         *info = &sensor_info;
75         return OP_SUCCESS;
76 }
77
78 int linear_accel_sensor::get_required_sensors(const required_sensor_s **sensors)
79 {
80         *sensors = required_sensors;
81         return 2;
82 }
83
84 int linear_accel_sensor::update(uint32_t id, sensor_data_t *data, int len)
85 {
86         if (id == SRC_ID_GRAVITY) {
87                 m_gx = data->values[0];
88                 m_gy = data->values[1];
89                 m_gz = data->values[2];
90         } else if (id == SRC_ID_ACC) {
91                 m_accuracy = data->accuracy;
92                 m_time = data->timestamp;
93                 m_x = data->values[0] - m_gx;
94                 m_y = data->values[1] - m_gy;
95                 m_z = data->values[2] - m_gz;
96
97                 return OP_SUCCESS;
98         }
99
100         return OP_ERROR; /* skip */
101 }
102
103 int linear_accel_sensor::get_data(sensor_data_t **data, int *length)
104 {
105         sensor_data_t *sensor_data;
106         sensor_data = (sensor_data_t *)malloc(sizeof(sensor_data_t));
107         retvm_if(!sensor_data, -ENOMEM, "Failed to allocate memory");
108
109         sensor_data->accuracy = SENSOR_ACCURACY_GOOD;
110         sensor_data->timestamp = m_time;
111         sensor_data->value_count = 3;
112         sensor_data->values[0] = m_x;
113         sensor_data->values[1] = m_y;
114         sensor_data->values[2] = m_z;
115
116         *data = sensor_data;
117         *length = sizeof(sensor_data_t);
118
119         return 0;
120 }