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),
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, PROXIMITY_EVENT_STATE_REPORT_ON_TIME, 0, 10),
50 FILL_LOG_ELEMENT(LOG_ID_EVENT, PROXIMITY_EVENT_DISTANCE_DATA_REPORT_ON_TIME, 0, 10),
51 FILL_LOG_ELEMENT(LOG_ID_EVENT, CONTEXT_EVENT_REPORT, 0, 1),
52 FILL_LOG_ELEMENT(LOG_ID_EVENT, AUTO_ROTATION_EVENT_CHANGE_STATE, 0, 1),
53 FILL_LOG_ELEMENT(LOG_ID_EVENT, ACCELEROMETER_EVENT_RAW_DATA_REPORT_ON_TIME, 0, 10),
54 FILL_LOG_ELEMENT(LOG_ID_EVENT, GYROSCOPE_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, PRESSURE_EVENT_RAW_DATA_REPORT_ON_TIME, 0, 10),
57 FILL_LOG_ELEMENT(LOG_ID_EVENT, LIGHT_EVENT_LEVEL_DATA_REPORT_ON_TIME, 0, 10),
58 FILL_LOG_ELEMENT(LOG_ID_EVENT, LIGHT_EVENT_LUX_DATA_REPORT_ON_TIME, 0, 10),
59 FILL_LOG_ELEMENT(LOG_ID_EVENT, GRAVITY_EVENT_RAW_DATA_REPORT_ON_TIME, 0, 10),
60 FILL_LOG_ELEMENT(LOG_ID_EVENT, LINEAR_ACCEL_EVENT_RAW_DATA_REPORT_ON_TIME, 0, 10),
61 FILL_LOG_ELEMENT(LOG_ID_EVENT, ORIENTATION_EVENT_RAW_DATA_REPORT_ON_TIME, 0, 10),
62 FILL_LOG_ELEMENT(LOG_ID_EVENT, PRESSURE_EVENT_RAW_DATA_REPORT_ON_TIME, 0, 10),
63 FILL_LOG_ELEMENT(LOG_ID_EVENT, TEMPERATURE_EVENT_RAW_DATA_REPORT_ON_TIME, 0, 10),
64 FILL_LOG_ELEMENT(LOG_ID_EVENT, ROTATION_VECTOR_EVENT_RAW_DATA_REPORT_ON_TIME, 0, 10),
65 FILL_LOG_ELEMENT(LOG_ID_EVENT, GEOMAGNETIC_RV_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, GYRO_BASE_DATA_SET, 0, 25),
69 FILL_LOG_ELEMENT(LOG_ID_DATA, PROXIMITY_BASE_DATA_SET, 0, 25),
70 FILL_LOG_ELEMENT(LOG_ID_DATA, PROXIMITY_DISTANCE_DATA_SET, 0, 25),
71 FILL_LOG_ELEMENT(LOG_ID_DATA, PRESSURE_BASE_DATA_SET, 0, 25),
72 FILL_LOG_ELEMENT(LOG_ID_DATA, GEOMAGNETIC_BASE_DATA_SET, 0, 25),
73 FILL_LOG_ELEMENT(LOG_ID_DATA, LIGHT_BASE_DATA_SET, 0, 25),
74 FILL_LOG_ELEMENT(LOG_ID_DATA, LIGHT_LUX_DATA_SET, 0, 25),
75 FILL_LOG_ELEMENT(LOG_ID_DATA, CONTEXT_BASE_DATA_SET, 0, 25),
76 FILL_LOG_ELEMENT(LOG_ID_DATA, AUTO_ROTATION_BASE_DATA_SET, 0, 25),
77 FILL_LOG_ELEMENT(LOG_ID_DATA, GRAVITY_BASE_DATA_SET, 0, 25),
78 FILL_LOG_ELEMENT(LOG_ID_DATA, LINEAR_ACCEL_BASE_DATA_SET, 0, 25),
79 FILL_LOG_ELEMENT(LOG_ID_DATA, ORIENTATION_BASE_DATA_SET, 0, 25),
80 FILL_LOG_ELEMENT(LOG_ID_DATA, PRESSURE_BASE_DATA_SET, 0, 25),
81 FILL_LOG_ELEMENT(LOG_ID_DATA, TEMPERATURE_BASE_DATA_SET, 0, 25),
84 typedef unordered_map<unsigned int, log_attr* > log_map;
85 log_map g_log_maps[LOG_ID_END];
87 extern void init_client(void);
88 static void init_log_maps(void);
101 static void init_log_maps(void)
105 cnt = sizeof(g_log_elements) / sizeof(g_log_elements[0]);
107 for (int i = 0; i < cnt; ++i) {
108 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";
118 auto 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_id_t sensor_id)
130 const int SENSOR_TYPE_MASK = 0x0000FFFF;
132 sensor_type_t sensor_type = (sensor_type_t) (sensor_id & SENSOR_TYPE_MASK);
134 return get_log_element_name(LOG_ID_SENSOR_TYPE, sensor_type);
137 const char* get_event_name(unsigned int event_type)
139 return get_log_element_name(LOG_ID_EVENT, event_type);
143 const char* get_data_name(unsigned int data_id)
145 return get_log_element_name(LOG_ID_DATA, data_id);
148 bool is_one_shot_event(unsigned int event_type)
153 bool is_ontime_event(unsigned int event_type)
155 switch (event_type ) {
156 case ACCELEROMETER_EVENT_RAW_DATA_REPORT_ON_TIME:
157 case PROXIMITY_EVENT_STATE_REPORT_ON_TIME:
158 case GYROSCOPE_EVENT_RAW_DATA_REPORT_ON_TIME:
159 case LIGHT_EVENT_LEVEL_DATA_REPORT_ON_TIME:
160 case GEOMAGNETIC_EVENT_RAW_DATA_REPORT_ON_TIME:
161 case LIGHT_EVENT_LUX_DATA_REPORT_ON_TIME:
162 case PROXIMITY_EVENT_DISTANCE_DATA_REPORT_ON_TIME:
163 case GRAVITY_EVENT_RAW_DATA_REPORT_ON_TIME:
164 case LINEAR_ACCEL_EVENT_RAW_DATA_REPORT_ON_TIME:
165 case ORIENTATION_EVENT_RAW_DATA_REPORT_ON_TIME:
166 case PRESSURE_EVENT_RAW_DATA_REPORT_ON_TIME:
174 bool is_panning_event(unsigned int event_type)
179 bool is_single_state_event(unsigned int event_type)
181 switch (event_type) {
182 case GEOMAGNETIC_EVENT_CALIBRATION_NEEDED:
183 case LIGHT_EVENT_CHANGE_LEVEL:
184 case PROXIMITY_EVENT_CHANGE_STATE:
185 case AUTO_ROTATION_EVENT_CHANGE_STATE:
193 unsigned int get_calibration_event_type(unsigned int event_type)
195 sensor_type_t sensor;
197 sensor = (sensor_type_t)(event_type >> SENSOR_TYPE_SHIFT);
200 case GEOMAGNETIC_SENSOR:
201 return GEOMAGNETIC_EVENT_CALIBRATION_NEEDED;
202 case ORIENTATION_SENSOR:
203 return ORIENTATION_EVENT_CALIBRATION_NEEDED;
209 unsigned long long get_timestamp(void)
212 clock_gettime(CLOCK_MONOTONIC, &t);
213 return ((unsigned long long)(t.tv_sec)*1000000000LL + t.tv_nsec) / 1000;
216 void print_event_occurrence_log(csensor_handle_info &sensor_handle_info, const creg_event_info *event_info)
220 auto iter = g_log_maps[LOG_ID_EVENT].find(event_info->type);
222 if (iter == g_log_maps[LOG_ID_EVENT].end())
225 log_attr = iter->second;
229 if ((log_attr->cnt != 1) && ((log_attr->cnt % log_attr->print_per_cnt) != 0)) {
233 INFO("%s receives %s with %s[%d][state: %d, option: %d count: %d]", get_client_name(), log_attr->name,
234 get_sensor_name(sensor_handle_info.m_sensor_id), sensor_handle_info.m_handle, sensor_handle_info.m_sensor_state,
235 sensor_handle_info.m_sensor_option, log_attr->cnt);
237 INFO("0x%x(cb_event_type = %s, &user_data, client_data = 0x%x)\n", event_info->m_cb,
238 log_attr->name, event_info->m_user_data);
242 * To prevent user mistakenly freeing sensor_info using sensor_t
244 static const int SENSOR_TO_SENSOR_INFO = 4;
245 static const int SENSOR_INFO_TO_SENSOR = -SENSOR_TO_SENSOR_INFO;
247 sensor_info *sensor_to_sensor_info(sensor_t sensor)
252 sensor_info* info = (sensor_info *)((char *)sensor + SENSOR_TO_SENSOR_INFO);
257 sensor_t sensor_info_to_sensor(const sensor_info *info)
262 sensor_t sensor = (sensor_t)((char *)info + SENSOR_INFO_TO_SENSOR);