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.
20 #include <client_common.h>
22 #include <sys/types.h>
29 #define FILL_LOG_ELEMENT(ID, TYPE, CNT, PRINT_PER_CNT) {ID, TYPE, {#TYPE, CNT, PRINT_PER_CNT} }
31 log_element g_log_elements[] = {
32 FILL_LOG_ELEMENT(LOG_ID_SENSOR_TYPE, UNKNOWN_SENSOR, 0, 1),
33 FILL_LOG_ELEMENT(LOG_ID_SENSOR_TYPE, ACCELEROMETER_SENSOR, 0, 1),
34 FILL_LOG_ELEMENT(LOG_ID_SENSOR_TYPE, GEOMAGNETIC_SENSOR, 0, 1),
35 FILL_LOG_ELEMENT(LOG_ID_SENSOR_TYPE, LIGHT_SENSOR, 0, 1),
36 FILL_LOG_ELEMENT(LOG_ID_SENSOR_TYPE, PROXIMITY_SENSOR, 0, 1),
37 FILL_LOG_ELEMENT(LOG_ID_SENSOR_TYPE, GYROSCOPE_SENSOR, 0, 1),
38 FILL_LOG_ELEMENT(LOG_ID_SENSOR_TYPE, MOTION_SENSOR, 0, 1),
39 FILL_LOG_ELEMENT(LOG_ID_SENSOR_TYPE, GRAVITY_SENSOR, 0, 1),
40 FILL_LOG_ELEMENT(LOG_ID_SENSOR_TYPE, LINEAR_ACCEL_SENSOR, 0, 1),
41 FILL_LOG_ELEMENT(LOG_ID_SENSOR_TYPE, ORIENTATION_SENSOR, 0, 1),
43 FILL_LOG_ELEMENT(LOG_ID_EVENT, ACCELEROMETER_EVENT_ROTATION_CHECK, 0, 1),
44 FILL_LOG_ELEMENT(LOG_ID_EVENT, ACCELEROMETER_EVENT_CALIBRATION_NEEDED, 0, 1),
45 FILL_LOG_ELEMENT(LOG_ID_EVENT, ACCELEROMETER_EVENT_SET_WAKEUP, 0, 1),
46 FILL_LOG_ELEMENT(LOG_ID_EVENT, GEOMAGNETIC_EVENT_CALIBRATION_NEEDED, 0, 1),
47 FILL_LOG_ELEMENT(LOG_ID_EVENT, PROXIMITY_EVENT_CHANGE_STATE, 0, 1),
48 FILL_LOG_ELEMENT(LOG_ID_EVENT, LIGHT_EVENT_CHANGE_LEVEL, 0, 1),
49 FILL_LOG_ELEMENT(LOG_ID_EVENT, MOTION_ENGINE_EVENT_SNAP, 0, 1),
50 FILL_LOG_ELEMENT(LOG_ID_EVENT, MOTION_ENGINE_EVENT_SHAKE, 0, 1),
51 FILL_LOG_ELEMENT(LOG_ID_EVENT, MOTION_ENGINE_EVENT_DOUBLETAP, 0, 1),
52 FILL_LOG_ELEMENT(LOG_ID_EVENT, MOTION_ENGINE_EVENT_DIRECT_CALL, 0, 1),
54 FILL_LOG_ELEMENT(LOG_ID_EVENT, ACCELEROMETER_EVENT_RAW_DATA_REPORT_ON_TIME, 0, 10),
55 FILL_LOG_ELEMENT(LOG_ID_EVENT, GEOMAGNETIC_EVENT_RAW_DATA_REPORT_ON_TIME, 0, 10),
56 FILL_LOG_ELEMENT(LOG_ID_EVENT, PROXIMITY_EVENT_STATE_REPORT_ON_TIME, 0, 10),
57 FILL_LOG_ELEMENT(LOG_ID_EVENT, GYROSCOPE_EVENT_RAW_DATA_REPORT_ON_TIME, 0, 10),
58 FILL_LOG_ELEMENT(LOG_ID_EVENT, LIGHT_EVENT_LEVEL_DATA_REPORT_ON_TIME, 0, 10),
59 FILL_LOG_ELEMENT(LOG_ID_EVENT, LIGHT_EVENT_LUX_DATA_REPORT_ON_TIME, 0, 10),
60 FILL_LOG_ELEMENT(LOG_ID_EVENT, GEOMAGNETIC_EVENT_ATTITUDE_DATA_REPORT_ON_TIME, 0, 10),
61 FILL_LOG_ELEMENT(LOG_ID_EVENT, ACCELEROMETER_EVENT_ORIENTATION_DATA_REPORT_ON_TIME, 0, 10),
62 FILL_LOG_ELEMENT(LOG_ID_EVENT, PROXIMITY_EVENT_DISTANCE_DATA_REPORT_ON_TIME, 0, 10),
63 FILL_LOG_ELEMENT(LOG_ID_EVENT, GRAVITY_EVENT_RAW_DATA_REPORT_ON_TIME, 0, 10),
64 FILL_LOG_ELEMENT(LOG_ID_EVENT, LINEAR_ACCEL_EVENT_RAW_DATA_REPORT_ON_TIME, 0, 10),
65 FILL_LOG_ELEMENT(LOG_ID_EVENT, ORIENTATION_EVENT_RAW_DATA_REPORT_ON_TIME, 0, 10),
67 FILL_LOG_ELEMENT(LOG_ID_DATA, ACCELEROMETER_BASE_DATA_SET, 0, 25),
68 FILL_LOG_ELEMENT(LOG_ID_DATA, ACCELEROMETER_ORIENTATION_DATA_SET, 0, 25),
69 FILL_LOG_ELEMENT(LOG_ID_DATA, ACCELEROMETER_ROTATION_DATA_SET, 0, 25),
70 FILL_LOG_ELEMENT(LOG_ID_DATA, GYRO_BASE_DATA_SET, 0, 25),
71 FILL_LOG_ELEMENT(LOG_ID_DATA, PROXIMITY_BASE_DATA_SET, 0, 25),
72 FILL_LOG_ELEMENT(LOG_ID_DATA, PROXIMITY_DISTANCE_DATA_SET, 0, 25),
73 FILL_LOG_ELEMENT(LOG_ID_DATA, GEOMAGNETIC_BASE_DATA_SET, 0, 25),
74 FILL_LOG_ELEMENT(LOG_ID_DATA, GEOMAGNETIC_RAW_DATA_SET, 0, 25),
75 FILL_LOG_ELEMENT(LOG_ID_DATA, GEOMAGNETIC_ATTITUDE_DATA_SET, 0, 25),
76 FILL_LOG_ELEMENT(LOG_ID_DATA, LIGHT_BASE_DATA_SET, 0, 25),
77 FILL_LOG_ELEMENT(LOG_ID_DATA, LIGHT_LUX_DATA_SET, 0, 25),
78 FILL_LOG_ELEMENT(LOG_ID_DATA, GRAVITY_BASE_DATA_SET, 0, 25),
79 FILL_LOG_ELEMENT(LOG_ID_DATA, LINEAR_ACCEL_BASE_DATA_SET, 0, 25),
80 FILL_LOG_ELEMENT(LOG_ID_DATA, ORIENTATION_BASE_DATA_SET, 0, 25),
82 FILL_LOG_ELEMENT(LOG_ID_ROTATE, ROTATION_EVENT_0, 0, 1),
83 FILL_LOG_ELEMENT(LOG_ID_ROTATE, ROTATION_EVENT_90, 0, 1),
84 FILL_LOG_ELEMENT(LOG_ID_ROTATE, ROTATION_EVENT_180, 0, 1),
85 FILL_LOG_ELEMENT(LOG_ID_ROTATE, ROTATION_EVENT_270, 0, 1),
88 typedef map<unsigned int, log_attr *> log_map;
89 log_map g_log_maps[LOG_ID_END];
91 extern void init_client(void);
92 static void init_log_maps(void);
104 static void init_log_maps(void)
107 cnt = sizeof(g_log_elements) / sizeof(g_log_elements[0]);
109 for (int i = 0; i < cnt; ++i) {
110 g_log_maps[g_log_elements[i].id][g_log_elements[i].type] = &g_log_elements[i].log_attr;
114 const char *get_log_element_name(log_id id, unsigned int type)
116 const char *p_unknown = "UNKNOWN";
117 log_map::iterator iter;
118 iter = g_log_maps[id].find(type);
120 if (iter == g_log_maps[id].end()) {
121 INFO("Unknown type value: 0x%x", type);
125 return iter->second->name;
128 const char *get_sensor_name(sensor_type_t sensor_type)
130 return get_log_element_name(LOG_ID_SENSOR_TYPE, sensor_type);
133 const char *get_event_name(unsigned int event_type)
135 return get_log_element_name(LOG_ID_EVENT, event_type);
138 const char *get_data_name(unsigned int data_id)
140 return get_log_element_name(LOG_ID_DATA, data_id);
143 const char *get_rotate_name(unsigned int rotate_type)
145 return get_log_element_name(LOG_ID_ROTATE, rotate_type);
148 bool is_one_shot_event(unsigned int event_type)
150 switch (event_type) {
151 case ACCELEROMETER_EVENT_SET_WAKEUP:
159 bool is_ontime_event(unsigned int event_type)
161 switch (event_type ) {
162 case ACCELEROMETER_EVENT_RAW_DATA_REPORT_ON_TIME:
163 case ACCELEROMETER_EVENT_ORIENTATION_DATA_REPORT_ON_TIME:
164 case GEOMAGNETIC_EVENT_RAW_DATA_REPORT_ON_TIME:
165 case GEOMAGNETIC_EVENT_ATTITUDE_DATA_REPORT_ON_TIME:
166 case GYROSCOPE_EVENT_RAW_DATA_REPORT_ON_TIME:
167 case LIGHT_EVENT_LEVEL_DATA_REPORT_ON_TIME:
168 case LIGHT_EVENT_LUX_DATA_REPORT_ON_TIME:
169 case PROXIMITY_EVENT_STATE_REPORT_ON_TIME:
170 case PROXIMITY_EVENT_DISTANCE_DATA_REPORT_ON_TIME:
171 case GRAVITY_EVENT_RAW_DATA_REPORT_ON_TIME:
172 case LINEAR_ACCEL_EVENT_RAW_DATA_REPORT_ON_TIME:
173 case ORIENTATION_EVENT_RAW_DATA_REPORT_ON_TIME:
181 bool is_panning_event(unsigned int event_type)
186 bool is_single_state_event(unsigned int event_type)
188 switch (event_type) {
189 case ACCELEROMETER_EVENT_SET_WAKEUP:
190 case ACCELEROMETER_EVENT_ROTATION_CHECK:
191 case GEOMAGNETIC_EVENT_CALIBRATION_NEEDED:
192 case LIGHT_EVENT_CHANGE_LEVEL:
193 case PROXIMITY_EVENT_CHANGE_STATE:
194 case MOTION_ENGINE_EVENT_SNAP:
195 case MOTION_ENGINE_EVENT_SHAKE:
196 case MOTION_ENGINE_EVENT_DOUBLETAP:
197 case MOTION_ENGINE_EVENT_DIRECT_CALL:
205 unsigned int get_calibration_event_type(unsigned int event_type)
207 sensor_type_t sensor;
208 sensor = (sensor_type_t)(event_type >> SENSOR_TYPE_SHIFT);
211 case GEOMAGNETIC_SENSOR:
212 return GEOMAGNETIC_EVENT_CALIBRATION_NEEDED;
213 case ORIENTATION_SENSOR:
214 return ORIENTATION_EVENT_CALIBRATION_NEEDED;
220 unsigned long long get_timestamp(void)
223 clock_gettime(CLOCK_MONOTONIC, &t);
224 return ((unsigned long long)(t.tv_sec) * NS_TO_SEC + t.tv_nsec) / MS_TO_SEC;
227 void sensor_event_to_data(sensor_event_t &event, sensor_data_t &data)
229 data.data_accuracy = event.data.data_accuracy;
230 data.data_unit_idx = event.data.data_unit_idx;
231 data.timestamp = event.data.timestamp;
232 data.values_num = event.data.values_num;
233 memcpy(data.values, event.data.values, sizeof(event.data.values));
236 void sensorhub_event_to_hub_data(sensorhub_event_t &event, sensorhub_data_t &data)
238 data.version = event.data.version;
239 data.sensorhub = event.data.sensorhub;
240 data.type = event.data.type;
241 data.hub_data_size = event.data.hub_data_size;
242 data.timestamp = event.data.timestamp;
243 memcpy(data.hub_data, event.data.hub_data, event.data.hub_data_size);
244 memcpy(data.data, event.data.data, sizeof(event.data.data));
247 void print_event_occurrence_log(csensor_handle_info &sensor_handle_info, creg_event_info &event_info,
248 sensor_event_data_t &sensor_event_data)
251 log_map::iterator iter;
252 iter = g_log_maps[LOG_ID_EVENT].find(event_info.m_event_type);
254 if (iter == g_log_maps[LOG_ID_EVENT].end()) {
255 ERR("wrong event_type: 0x%x, handle %d", event_info.m_event_type, sensor_handle_info.m_handle);
259 log_attr = iter->second;
262 if ((log_attr->cnt != 1) && ((log_attr->cnt % log_attr->print_per_cnt) != 0)) {
266 INFO("%s receives %s with %s[%d][state: %d, option: %d count: %d]", get_client_name(), log_attr->name,
267 get_sensor_name(sensor_handle_info.m_sensor_type), sensor_handle_info.m_handle, sensor_handle_info.m_sensor_state,
268 sensor_handle_info.m_sensor_option, log_attr->cnt);
270 if (event_info.m_event_type == ACCELEROMETER_EVENT_ROTATION_CHECK) {
271 INFO("%s", get_rotate_name(*(unsigned int *)(sensor_event_data.event_data)));
274 INFO("0x%x(cb_event_type = %s, &cb_data, client_data = 0x%x)", event_info.m_event_callback,
275 log_attr->name, event_info.m_cb_data);