Integrate internal fixes
[platform/core/system/sensord.git] / src / server / sensor_handler.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 "sensor_handler.h"
21
22 #include <message.h>
23 #include <sensor_log.h>
24 #include <sensor_utils.h>
25 #include <sensor_types_private.h>
26
27 using namespace sensor;
28
29 sensor_handler::sensor_handler(const sensor_info &info)
30 : m_info(info)
31 , m_last_data(NULL)
32 , m_last_data_size(0)
33 {
34         const char *priv = sensor::utils::get_privilege(m_info.get_uri());
35         m_info.set_privilege(priv);
36
37         sensor_type_t type = sensor::utils::get_type(m_info.get_uri());
38         m_info.set_type(type);
39
40         /* TODO: temporary walkaround for sensors that require multiple privileges */
41         switch (m_info.get_type()) {
42         case EXTERNAL_EXERCISE_SENSOR:
43         case EXERCISE_STANDALONE_SENSOR:
44                 m_info.add_privilege(PRIVILEGE_LOCATION_URI);
45                 break;
46         case GPS_CTRL_SENSOR:
47                 m_info.add_privilege(PRIVILEGE_PLATFORM_URI);
48                 break;
49         default:
50                 break;
51         }
52 }
53
54 bool sensor_handler::has_observer(sensor_observer *ob)
55 {
56         for (auto it = m_observers.begin(); it != m_observers.end(); ++it) {
57                 if ((*it) == ob)
58                         return true;
59         }
60
61         return false;
62 }
63
64 bool sensor_handler::add_observer(sensor_observer *ob)
65 {
66         retv_if(has_observer(ob), false);
67
68         m_observers.push_back(ob);
69         return true;
70 }
71
72 void sensor_handler::remove_observer(sensor_observer *ob)
73 {
74         m_observers.remove(ob);
75 }
76
77 int sensor_handler::notify(const char *uri, sensor_data_t *data, int len)
78 {
79         if (observer_count() == 0)
80                 return OP_ERROR;
81
82         ipc::message *msg;
83
84         msg = new(std::nothrow) ipc::message((char *)data, len);
85         retvm_if(!msg, OP_ERROR, "Failed to allocate memory");
86
87         for (auto it = m_observers.begin(); it != m_observers.end(); ++it)
88                 (*it)->update(uri, msg);
89
90         set_cache(data, len);
91
92         if (msg->ref_count() == 0)
93                 msg->unref();
94
95         return OP_SUCCESS;
96 }
97
98 uint32_t sensor_handler::observer_count(void)
99 {
100         return m_observers.size();
101 }
102
103 void sensor_handler::set_cache(sensor_data_t *data, int size)
104 {
105         if (m_last_data == NULL) {
106                 m_last_data = (sensor_data_t*)malloc(size);
107                 retm_if(m_last_data == NULL, "Memory allocation failed");
108         }
109
110         m_last_data_size = size;
111         memcpy(m_last_data, data, size);
112 }
113
114 int sensor_handler::get_cache(sensor_data_t **data, int *len)
115 {
116         retv_if(m_last_data == NULL, -ENODATA);
117
118         *data = (sensor_data_t *)malloc(m_last_data_size);
119         retvm_if(*data == NULL, -ENOMEM, "Memory allocation failed");
120
121         memcpy(*data, m_last_data, m_last_data_size);
122         *len = m_last_data_size;
123
124         return 0;
125 }
126
127 int sensor_handler::delete_batch_latency(sensor_observer *ob)
128 {
129         return 0;
130 }