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.
19 #include <client_common.h>
21 #include <sys/types.h>
24 #include <unordered_map>
25 using std::unordered_map;
27 #define FILL_LOG_ELEMENT(ID, TYPE, CNT, PRINT_PER_CNT) {ID, TYPE, {#TYPE, CNT, PRINT_PER_CNT} }
29 log_element g_log_elements[] = {
30 FILL_LOG_ELEMENT(LOG_ID_SENSOR_TYPE, UNKNOWN_SENSOR, 0, 1),
31 FILL_LOG_ELEMENT(LOG_ID_SENSOR_TYPE, ACCELEROMETER_SENSOR, 0, 1),
32 FILL_LOG_ELEMENT(LOG_ID_SENSOR_TYPE, GEOMAGNETIC_SENSOR, 0, 1),
33 FILL_LOG_ELEMENT(LOG_ID_SENSOR_TYPE, LIGHT_SENSOR, 0, 1),
34 FILL_LOG_ELEMENT(LOG_ID_SENSOR_TYPE, PROXIMITY_SENSOR, 0, 1),
35 FILL_LOG_ELEMENT(LOG_ID_SENSOR_TYPE, GYROSCOPE_SENSOR, 0, 1),
36 FILL_LOG_ELEMENT(LOG_ID_SENSOR_TYPE, PRESSURE_SENSOR, 0, 1),
37 FILL_LOG_ELEMENT(LOG_ID_SENSOR_TYPE, CONTEXT_SENSOR, 0, 1),
38 FILL_LOG_ELEMENT(LOG_ID_SENSOR_TYPE, AUTO_ROTATION_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),
42 FILL_LOG_ELEMENT(LOG_ID_SENSOR_TYPE, TEMPERATURE_SENSOR, 0, 1),
43 FILL_LOG_ELEMENT(LOG_ID_SENSOR_TYPE, ROTATION_VECTOR_SENSOR, 0, 1),
44 FILL_LOG_ELEMENT(LOG_ID_SENSOR_TYPE, GEOMAGNETIC_RV_SENSOR, 0, 1),
45 FILL_LOG_ELEMENT(LOG_ID_SENSOR_TYPE, GAMING_RV_SENSOR, 0, 1),
46 FILL_LOG_ELEMENT(LOG_ID_SENSOR_TYPE, FUSION_SENSOR, 0, 1),
47 FILL_LOG_ELEMENT(LOG_ID_SENSOR_TYPE, TILT_SENSOR, 0, 1),
48 FILL_LOG_ELEMENT(LOG_ID_SENSOR_TYPE, GYROSCOPE_UNCAL_SENSOR, 0, 1),
49 FILL_LOG_ELEMENT(LOG_ID_SENSOR_TYPE, ULTRAVIOLET_SENSOR, 0, 1),
50 FILL_LOG_ELEMENT(LOG_ID_SENSOR_TYPE, BIO_LED_RED_SENSOR, 0, 1),
51 FILL_LOG_ELEMENT(LOG_ID_SENSOR_TYPE, GESTURE_WRIST_UP_SENSOR, 0, 1),
53 FILL_LOG_ELEMENT(LOG_ID_EVENT, PROXIMITY_CHANGE_STATE_EVENT, 0,1),
54 FILL_LOG_ELEMENT(LOG_ID_EVENT, LIGHT_CHANGE_LEVEL_EVENT, 0, 1),
55 FILL_LOG_ELEMENT(LOG_ID_EVENT, PROXIMITY_STATE_EVENT, 0, 10),
56 FILL_LOG_ELEMENT(LOG_ID_EVENT, PROXIMITY_DISTANCE_DATA_EVENT, 0, 10),
57 FILL_LOG_ELEMENT(LOG_ID_EVENT, AUTO_ROTATION_CHANGE_STATE_EVENT, 0, 1),
58 FILL_LOG_ELEMENT(LOG_ID_EVENT, ACCELEROMETER_RAW_DATA_EVENT, 0, 10),
59 FILL_LOG_ELEMENT(LOG_ID_EVENT, GYROSCOPE_RAW_DATA_EVENT, 0, 10),
60 FILL_LOG_ELEMENT(LOG_ID_EVENT, GEOMAGNETIC_RAW_DATA_EVENT, 0, 10),
61 FILL_LOG_ELEMENT(LOG_ID_EVENT, PRESSURE_RAW_DATA_EVENT, 0, 10),
62 FILL_LOG_ELEMENT(LOG_ID_EVENT, LIGHT_LEVEL_DATA_EVENT, 0, 10),
63 FILL_LOG_ELEMENT(LOG_ID_EVENT, LIGHT_LUX_DATA_EVENT, 0, 10),
64 FILL_LOG_ELEMENT(LOG_ID_EVENT, GRAVITY_RAW_DATA_EVENT, 0, 10),
65 FILL_LOG_ELEMENT(LOG_ID_EVENT, LINEAR_ACCEL_RAW_DATA_EVENT, 0, 10),
66 FILL_LOG_ELEMENT(LOG_ID_EVENT, ORIENTATION_RAW_DATA_EVENT, 0, 10),
67 FILL_LOG_ELEMENT(LOG_ID_EVENT, PRESSURE_RAW_DATA_EVENT, 0, 10),
68 FILL_LOG_ELEMENT(LOG_ID_EVENT, TEMPERATURE_RAW_DATA_EVENT, 0, 10),
69 FILL_LOG_ELEMENT(LOG_ID_EVENT, ROTATION_VECTOR_RAW_DATA_EVENT, 0, 10),
70 FILL_LOG_ELEMENT(LOG_ID_EVENT, GEOMAGNETIC_RV_RAW_DATA_EVENT, 0, 10),
71 FILL_LOG_ELEMENT(LOG_ID_EVENT, GAMING_RV_RAW_DATA_EVENT, 0, 10),
72 FILL_LOG_ELEMENT(LOG_ID_EVENT, FUSION_EVENT, 0, 10),
73 FILL_LOG_ELEMENT(LOG_ID_EVENT, TILT_RAW_DATA_EVENT, 0, 10),
74 FILL_LOG_ELEMENT(LOG_ID_EVENT, GYROSCOPE_UNCAL_RAW_DATA_EVENT, 0, 10),
75 FILL_LOG_ELEMENT(LOG_ID_EVENT, ULTRAVIOLET_RAW_DATA_EVENT, 0, 10),
76 FILL_LOG_ELEMENT(LOG_ID_EVENT, BIO_LED_RED_RAW_DATA_EVENT, 0, 10),
79 typedef unordered_map<unsigned int, log_attr* > log_map;
80 log_map g_log_maps[LOG_ID_END];
82 extern void init_client(void);
83 static void init_log_maps(void);
96 static void init_log_maps(void)
100 cnt = sizeof(g_log_elements) / sizeof(g_log_elements[0]);
102 for (int i = 0; i < cnt; ++i) {
103 g_log_maps[g_log_elements[i].id][g_log_elements[i].type] = &g_log_elements[i].log_attr;
109 const char* get_log_element_name(log_id id, unsigned int type)
111 const char* p_unknown = "UNKNOWN";
113 auto iter = g_log_maps[id].find(type);
115 if (iter == g_log_maps[id].end()) {
116 _I("Unknown type value: 0x%x", type);
120 return iter->second->name;
123 const char* get_sensor_name(sensor_id_t sensor_id)
125 sensor_type_t sensor_type = (sensor_type_t) (sensor_id >> SENSOR_TYPE_SHIFT);
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);
136 const char* get_data_name(unsigned int data_id)
138 return get_log_element_name(LOG_ID_DATA, data_id);
141 bool is_one_shot_event(unsigned int event_type)
146 bool is_ontime_event(unsigned int event_type)
148 switch (event_type ) {
149 case ACCELEROMETER_RAW_DATA_EVENT:
150 case PROXIMITY_STATE_EVENT:
151 case GYROSCOPE_RAW_DATA_EVENT:
152 case LIGHT_LEVEL_DATA_EVENT:
153 case GEOMAGNETIC_RAW_DATA_EVENT:
154 case LIGHT_LUX_DATA_EVENT:
155 case PROXIMITY_DISTANCE_DATA_EVENT:
156 case GRAVITY_RAW_DATA_EVENT:
157 case LINEAR_ACCEL_RAW_DATA_EVENT:
158 case ORIENTATION_RAW_DATA_EVENT:
159 case PRESSURE_RAW_DATA_EVENT:
167 bool is_panning_event(unsigned int event_type)
172 bool is_single_state_event(unsigned int event_type)
174 switch (event_type) {
175 case LIGHT_CHANGE_LEVEL_EVENT:
176 case PROXIMITY_CHANGE_STATE_EVENT:
177 case AUTO_ROTATION_CHANGE_STATE_EVENT:
185 unsigned int get_calibration_event_type(unsigned int event_type)
187 sensor_type_t sensor;
189 sensor = (sensor_type_t)(event_type >> EVENT_TYPE_SHIFT);
197 unsigned long long get_timestamp(void)
200 clock_gettime(CLOCK_MONOTONIC, &t);
201 return ((unsigned long long)(t.tv_sec)*1000000000LL + t.tv_nsec) / 1000;
204 void print_event_occurrence_log(sensor_handle_info &sensor_handle_info, const reg_event_info *event_info)
208 auto iter = g_log_maps[LOG_ID_EVENT].find(event_info->type);
210 if (iter == g_log_maps[LOG_ID_EVENT].end())
213 log_attr = iter->second;
217 if ((log_attr->cnt != 1) && ((log_attr->cnt % log_attr->print_per_cnt) != 0)) {
221 _I("%s receives %s with %s[%d][state: %d, option: %d count: %d]", get_client_name(), log_attr->name,
222 get_sensor_name(sensor_handle_info.m_sensor_id), sensor_handle_info.m_handle, sensor_handle_info.m_sensor_state,
223 sensor_handle_info.m_sensor_option, log_attr->cnt);
225 _I("0x%x(cb_event_type = %s, &user_data, client_data = 0x%x)\n", event_info->m_cb,
226 log_attr->name, event_info->m_user_data);
230 * To prevent user mistakenly freeing sensor_info using sensor_t
232 static const int SENSOR_TO_SENSOR_INFO = 4;
233 static const int SENSOR_INFO_TO_SENSOR = -SENSOR_TO_SENSOR_INFO;
235 sensor_info *sensor_to_sensor_info(sensor_t sensor)
240 sensor_info* info = (sensor_info *)((char *)sensor + SENSOR_TO_SENSOR_INFO);
245 sensor_t sensor_info_to_sensor(const sensor_info *info)
250 sensor_t sensor = (sensor_t)((char *)info + SENSOR_INFO_TO_SENSOR);