sensor_handler::sensor_handler(const sensor_info &info)
: m_info(info)
+, m_last_data(NULL)
+, m_last_data_size(0)
{
const char *priv = sensor::utils::get_privilege(m_info.get_uri());
m_info.set_privilege(priv);
if (msg->ref_count() == 0)
msg->unref();
+ set_cache(data, len);
+
return OP_SUCCESS;
}
{
return m_observers.size();
}
+
+void sensor_handler::set_cache(sensor_data_t *data, int size)
+{
+ if (m_last_data == NULL) {
+ m_last_data = (sensor_data_t*)malloc(size);
+ retm_if(m_last_data == NULL, "Memory allocation failed");
+ }
+
+ m_last_data_size = size;
+ memcpy(m_last_data, data, size);
+}
+
+int sensor_handler::get_cache(sensor_data_t **data, int *len)
+{
+ retv_if(m_last_data == NULL, -ENODATA);
+
+ *data = (sensor_data_t *)malloc(m_last_data_size);
+ retvm_if(*data == NULL, -ENOMEM, "Memory allocation failed");
+
+ memcpy(*data, m_last_data, m_last_data_size);
+ *len = m_last_data_size;
+
+ return 0;
+}
virtual int flush(sensor_observer *ob) = 0;
virtual int get_data(sensor_data_t **data, int *len) = 0;
+ void set_cache(sensor_data_t *data, int size);
+ int get_cache(sensor_data_t **data, int *len);
+
protected:
sensor_info m_info;
private:
std::list<sensor_observer *> m_observers;
+
+ sensor_data_t *m_last_data;
+ int m_last_data_size;
};
}
sensor_handler *sensor = m_manager->get_sensor(m_uri);
retv_if(!sensor, -EINVAL);
- /* TODO : caching the last data & retry logic if there is no data */
- return sensor->get_data(data, len);
+ return sensor->get_cache(data, len);
}
std::string sensor_listener_proxy::get_required_privileges(void)