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),
79 FILL_LOG_ELEMENT(LOG_ID_ROTATE, ROTATION_EVENT_0, 0, 1),
80 FILL_LOG_ELEMENT(LOG_ID_ROTATE, ROTATION_EVENT_90, 0, 1),
81 FILL_LOG_ELEMENT(LOG_ID_ROTATE, ROTATION_EVENT_180, 0, 1),
82 FILL_LOG_ELEMENT(LOG_ID_ROTATE, ROTATION_EVENT_270, 0, 1),
85 typedef map<unsigned int, log_attr *> log_map;
86 log_map g_log_maps[LOG_ID_END];
88 extern void init_client(void);
89 static void init_log_maps(void);
101 static void init_log_maps(void)
104 cnt = sizeof(g_log_elements) / sizeof(g_log_elements[0]);
106 for (int i = 0; i < cnt; ++i) {
107 g_log_maps[g_log_elements[i].id][g_log_elements[i].type] = &g_log_elements[i].log_attr;
111 const char *get_log_element_name(log_id id, unsigned int type)
113 const char *p_unknown = "UNKNOWN";
114 log_map::iterator iter;
115 iter = g_log_maps[id].find(type);
117 if (iter == g_log_maps[id].end()) {
118 INFO("Unknown type value: 0x%x", type);
122 return iter->second->name;
125 const char *get_sensor_name(sensor_type_t sensor_type)
127 return get_log_element_name(LOG_ID_SENSOR_TYPE, sensor_type);
130 const char *get_event_name(unsigned int event_type)
132 return get_log_element_name(LOG_ID_EVENT, event_type);
135 const char *get_data_name(unsigned int data_id)
137 return get_log_element_name(LOG_ID_DATA, data_id);
140 const char *get_rotate_name(unsigned int rotate_type)
142 return get_log_element_name(LOG_ID_ROTATE, rotate_type);
145 bool is_one_shot_event(unsigned int event_type)
147 switch (event_type) {
148 case ACCELEROMETER_EVENT_SET_WAKEUP:
156 bool is_ontime_event(unsigned int event_type)
158 switch (event_type ) {
159 case ACCELEROMETER_EVENT_RAW_DATA_REPORT_ON_TIME:
160 case ACCELEROMETER_EVENT_ORIENTATION_DATA_REPORT_ON_TIME:
161 case GEOMAGNETIC_EVENT_RAW_DATA_REPORT_ON_TIME:
162 case GEOMAGNETIC_EVENT_ATTITUDE_DATA_REPORT_ON_TIME:
163 case GYROSCOPE_EVENT_RAW_DATA_REPORT_ON_TIME:
164 case LIGHT_EVENT_LEVEL_DATA_REPORT_ON_TIME:
165 case LIGHT_EVENT_LUX_DATA_REPORT_ON_TIME:
166 case PROXIMITY_EVENT_STATE_REPORT_ON_TIME:
167 case PROXIMITY_EVENT_DISTANCE_DATA_REPORT_ON_TIME:
168 case GRAVITY_EVENT_RAW_DATA_REPORT_ON_TIME:
169 case LINEAR_ACCEL_EVENT_RAW_DATA_REPORT_ON_TIME:
170 case ORIENTATION_EVENT_RAW_DATA_REPORT_ON_TIME:
178 bool is_panning_event(unsigned int event_type)
183 bool is_single_state_event(unsigned int event_type)
185 switch (event_type) {
186 case ACCELEROMETER_EVENT_SET_WAKEUP:
187 case ACCELEROMETER_EVENT_ROTATION_CHECK:
188 case GEOMAGNETIC_EVENT_CALIBRATION_NEEDED:
189 case LIGHT_EVENT_CHANGE_LEVEL:
190 case PROXIMITY_EVENT_CHANGE_STATE:
191 case MOTION_ENGINE_EVENT_SNAP:
192 case MOTION_ENGINE_EVENT_SHAKE:
193 case MOTION_ENGINE_EVENT_DOUBLETAP:
194 case MOTION_ENGINE_EVENT_DIRECT_CALL:
202 unsigned int get_calibration_event_type(unsigned int event_type)
204 sensor_type_t sensor;
205 sensor = (sensor_type_t)(event_type >> SENSOR_TYPE_SHIFT);
208 case GEOMAGNETIC_SENSOR:
209 return GEOMAGNETIC_EVENT_CALIBRATION_NEEDED;
215 unsigned long long get_timestamp(void)
218 clock_gettime(CLOCK_MONOTONIC, &t);
219 return ((unsigned long long)(t.tv_sec) * NS_TO_SEC + t.tv_nsec) / MS_TO_SEC;
222 void sensor_event_to_data(sensor_event_t &event, sensor_data_t &data)
224 data.data_accuracy = event.data.data_accuracy;
225 data.data_unit_idx = event.data.data_unit_idx;
226 data.timestamp = event.data.timestamp;
227 data.values_num = event.data.values_num;
228 memcpy(data.values, event.data.values, sizeof(event.data.values));
231 void sensorhub_event_to_hub_data(sensorhub_event_t &event, sensorhub_data_t &data)
233 data.version = event.data.version;
234 data.sensorhub = event.data.sensorhub;
235 data.type = event.data.type;
236 data.hub_data_size = event.data.hub_data_size;
237 data.timestamp = event.data.timestamp;
238 memcpy(data.hub_data, event.data.hub_data, event.data.hub_data_size);
239 memcpy(data.data, event.data.data, sizeof(event.data.data));
242 void print_event_occurrence_log(csensor_handle_info &sensor_handle_info, creg_event_info &event_info,
243 sensor_event_data_t &sensor_event_data)
246 log_map::iterator iter;
247 iter = g_log_maps[LOG_ID_EVENT].find(event_info.m_event_type);
249 if (iter == g_log_maps[LOG_ID_EVENT].end()) {
250 ERR("wrong event_type: 0x%x, handle %d", event_info.m_event_type, sensor_handle_info.m_handle);
254 log_attr = iter->second;
257 if ((log_attr->cnt != 1) && ((log_attr->cnt % log_attr->print_per_cnt) != 0)) {
261 INFO("%s receives %s with %s[%d][state: %d, option: %d count: %d]", get_client_name(), log_attr->name,
262 get_sensor_name(sensor_handle_info.m_sensor_type), sensor_handle_info.m_handle, sensor_handle_info.m_sensor_state,
263 sensor_handle_info.m_sensor_option, log_attr->cnt);
265 if (event_info.m_event_type == ACCELEROMETER_EVENT_ROTATION_CHECK) {
266 INFO("%s", get_rotate_name(*(unsigned int *)(sensor_event_data.event_data)));
269 INFO("0x%x(cb_event_type = %s, &cb_data, client_data = 0x%x)", event_info.m_event_callback,
270 log_attr->name, event_info.m_cb_data);