4 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <sf_common.h>
23 #include <geo_sensor.h>
24 #include <sensor_plugin_loader.h>
26 #define SENSOR_NAME "GEOMAGNETIC_SENSOR"
28 geo_sensor::geo_sensor()
32 m_name = string(SENSOR_NAME);
34 register_supported_event(GEOMAGNETIC_EVENT_RAW_DATA_REPORT_ON_TIME);
35 register_supported_event(GEOMAGNETIC_EVENT_CALIBRATION_NEEDED);
37 physical_sensor::set_poller(geo_sensor::working, this);
40 geo_sensor::~geo_sensor()
42 INFO("geo_sensor is destroyed!\n");
45 bool geo_sensor::init()
47 m_sensor_hal = sensor_plugin_loader::get_instance().get_sensor_hal(GEOMAGNETIC_SENSOR);
50 ERR("cannot load sensor_hal[%s]", sensor_base::get_name());
54 sensor_properties_t properties;
56 if (m_sensor_hal->get_properties(properties) == false) {
57 ERR("sensor->get_properties() is failed!\n");
61 m_resolution = properties.resolution;
63 INFO("%s is created!\n", sensor_base::get_name());
68 sensor_type_t geo_sensor::get_type(void)
70 return GEOMAGNETIC_SENSOR;
73 bool geo_sensor::working(void *inst)
75 geo_sensor *sensor = (geo_sensor*)inst;
76 return sensor->process_event();;
79 bool geo_sensor::process_event(void)
83 if (!m_sensor_hal->is_data_ready(true))
86 m_sensor_hal->get_sensor_data(event.data);
88 AUTOLOCK(m_client_info_mutex);
91 if (get_client_cnt(GEOMAGNETIC_EVENT_RAW_DATA_REPORT_ON_TIME)) {
92 event.sensor_id = get_id();
93 event.event_type = GEOMAGNETIC_EVENT_RAW_DATA_REPORT_ON_TIME;
94 raw_to_base(event.data);
101 bool geo_sensor::on_start(void)
103 if (!m_sensor_hal->enable()) {
104 ERR("m_sensor_hal start fail\n");
111 bool geo_sensor::on_stop(void)
113 if (!m_sensor_hal->disable()) {
114 ERR("m_sensor_hal stop fail\n");
121 bool geo_sensor::get_properties(sensor_properties_t &properties)
123 return m_sensor_hal->get_properties(properties);
126 int geo_sensor::get_sensor_data(unsigned int type, sensor_data_t &data)
130 if (type != GEOMAGNETIC_BASE_DATA_SET)
133 state = m_sensor_hal->get_sensor_data(data);
136 ERR("m_sensor_hal get struct_data fail\n");
143 bool geo_sensor::set_interval(unsigned long interval)
147 INFO("Polling interval is set to %dms", interval);
149 return m_sensor_hal->set_interval(interval);
152 void geo_sensor::raw_to_base(sensor_data_t &data)
154 data.value_count = 3;
155 data.values[0] = data.values[0] * m_resolution;
156 data.values[1] = data.values[1] * m_resolution;
157 data.values[2] = data.values[2] * m_resolution;
160 extern "C" void *create(void)
165 inst = new geo_sensor();
167 ERR("geo_sensor class create fail , errno : %d , errstr : %s\n", err, strerror(err));
174 extern "C" void destroy(void *inst)
176 delete (geo_sensor*)inst;;