4 * Copyright (c) 2014-15 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.
23 #include <sensor_internal.h>
25 #include <sensor_common.h>
29 #define DEFAULT_EVENT_INTERVAL 100
31 static GMainLoop *mainloop;
35 printf("Usage : ./tc-common <Sensor_type> <event>(optional) <interval>(optional)\n\n");
37 printf("Sensor_type: ");
38 printf("[accelerometer] ");
39 printf("[gyroscope] ");
40 printf("[pressure] ");
41 printf("[temperature] ");
42 printf("[geomagnetic] ");
43 printf("[orientation] ");
45 printf("[linear_accel] ");
46 printf("[rotation_vector] ");
47 printf("[geomagnetic_rv] ");
50 printf("[RAW_DATA_REPORT_ON_TIME]\n");
52 printf("Sensor_type: ");
53 printf("[proximity]\n");
55 printf("[EVENT_CHANGE_STATE]\n");
57 printf("interval:\n");
58 printf("The time interval should be entered based on the sampling frequency supported by accelerometer driver on the device in ms.If no value for sensor is entered default value by the driver will be used.\n");
61 unsigned int get_event_driven(sensor_type_t sensor_type, char str[])
63 switch (sensor_type) {
64 case ACCELEROMETER_SENSOR:
65 if (strcmp(str, "RAW_DATA_REPORT_ON_TIME") == 0)
66 return ACCELEROMETER_RAW_DATA_EVENT;
68 case GYROSCOPE_SENSOR:
69 if (strcmp(str, "RAW_DATA_REPORT_ON_TIME") == 0)
70 return GYROSCOPE_RAW_DATA_EVENT;
73 if (strcmp(str, "RAW_DATA_REPORT_ON_TIME") == 0)
74 return PRESSURE_RAW_DATA_EVENT;
76 case GEOMAGNETIC_SENSOR:
77 if (strcmp(str, "RAW_DATA_REPORT_ON_TIME") == 0)
78 return GEOMAGNETIC_RAW_DATA_EVENT;
81 if (strcmp(str, "RAW_DATA_REPORT_ON_TIME") == 0)
82 return LIGHT_EVENT_LUX_DATA_REPORT_ON_TIME;
84 case TEMPERATURE_SENSOR:
85 if (strcmp(str, "RAW_DATA_REPORT_ON_TIME") == 0)
86 return TEMPERATURE_RAW_DATA_EVENT;
88 case PROXIMITY_SENSOR:
89 if (strcmp(str, "EVENT_CHANGE_STATE") == 0)
90 return PROXIMITY_CHANGE_STATE_EVENT;
92 case ORIENTATION_SENSOR:
93 if (strcmp(str, "RAW_DATA_REPORT_ON_TIME") == 0)
94 return ORIENTATION_EVENT_RAW_DATA_REPORT_ON_TIME;
97 if (strcmp(str, "RAW_DATA_REPORT_ON_TIME") == 0)
98 return GRAVITY_EVENT_RAW_DATA_REPORT_ON_TIME;
100 case LINEAR_ACCEL_SENSOR:
101 if (strcmp(str, "RAW_DATA_REPORT_ON_TIME") == 0)
102 return LINEAR_ACCEL_EVENT_RAW_DATA_REPORT_ON_TIME;
104 case ROTATION_VECTOR_SENSOR:
105 if (strcmp(str, "RAW_DATA_REPORT_ON_TIME") == 0)
106 return ROTATION_VECTOR_EVENT_RAW_DATA_REPORT_ON_TIME;
108 case GEOMAGNETIC_RV_SENSOR:
109 if (strcmp(str, "RAW_DATA_REPORT_ON_TIME") == 0)
110 return GEOMAGNETIC_RV_RAW_DATA_EVENT;
117 void callback(sensor_t sensor, unsigned int event_type, sensor_data_t *data, void *user_data)
119 sensor_type_t sensor_type = event_type >> 16;
121 switch (sensor_type) {
122 case ACCELEROMETER_SENSOR:
123 printf("Accelerometer [%lld] [%6.6f] [%6.6f] [%6.6f]\n\n", data->timestamp, data->values[0], data->values[1], data->values[2]);
125 case GYROSCOPE_SENSOR:
126 printf("Gyroscope [%lld] [%6.6f] [%6.6f] [%6.6f]\n\n", data->timestamp, data->values[0], data->values[1], data->values[2]);
128 case PRESSURE_SENSOR:
129 printf("Pressure [%lld] [%6.6f] [%6.6f] [%6.6f]\n\n", data->timestamp, data->values[0], data->values[1], data->values[2]);
131 case GEOMAGNETIC_SENSOR:
132 printf("Geomagnetic [%lld] [%6.6f] [%6.6f] [%6.6f]\n\n", data->timestamp, data->values[0], data->values[1], data->values[2]);
135 printf("Light [%lld] [%6.6f]\n\n", data->timestamp, data->values[0]);
137 case TEMPERATURE_SENSOR :
138 printf("Temperature [%lld] [%6.6f]\n\n", data->timestamp, data->values[0]);
140 case PROXIMITY_SENSOR:
141 printf("Proximity [%lld] [%6.6f]\n\n", data->timestamp, data->values[0]);
143 case ORIENTATION_SENSOR :
144 printf("Orientation [%lld] [%6.6f] [%6.6f] [%6.6f]\n\n", data->timestamp, data->values[0], data->values[1], data->values[2]);
147 printf("Gravity [%lld] [%6.6f] [%6.6f] [%6.6f]\n\n", data->timestamp, data->values[0], data->values[1], data->values[2]);
149 case LINEAR_ACCEL_SENSOR:
150 printf("Linear acceleration [%lld] [%6.6f] [%6.6f] [%6.6f]\n\n", data->timestamp, data->values[0], data->values[1], data->values[2]);
152 case ROTATION_VECTOR_SENSOR:
153 printf("Rotation vector [%lld] [%6.6f] [%6.6f] [%6.6f] [%6.6f]\n\n", data->timestamp, data->values[0], data->values[1], data->values[2], data->values[3]);
155 case GEOMAGNETIC_RV_SENSOR:
156 printf("Geomagnetic RV [%lld] [%6.6f] [%6.6f] [%6.6f] [%6.6f]\n\n", data->timestamp, data->values[0], data->values[1], data->values[2], data->values[3]);
163 int main(int argc, char **argv)
165 int result, handle, start_handle, stop_handle, interval;
168 bool EVENT_NOT_ENTERED = TRUE;
169 sensor_type_t sensor_type;
170 mainloop = g_main_loop_new(NULL, FALSE);
172 if (argc < 2 || argc > 4) {
173 printf("Wrong number of arguments\n");
178 if (strcmp(argv[1], "accelerometer") == 0) {
179 sensor_type = ACCELEROMETER_SENSOR;
180 event = ACCELEROMETER_RAW_DATA_EVENT;
182 else if (strcmp(argv[1], "gyroscope") == 0) {
183 sensor_type = GYROSCOPE_SENSOR;
184 event = GYROSCOPE_RAW_DATA_EVENT;
186 else if (strcmp(argv[1], "pressure") == 0) {
187 sensor_type = PRESSURE_SENSOR;
188 event = PRESSURE_RAW_DATA_EVENT;
190 else if (strcmp(argv[1], "temperature") == 0) {
191 sensor_type = TEMPERATURE_SENSOR;
192 event = TEMPERATURE_RAW_DATA_EVENT;
194 else if (strcmp(argv[1], "geomagnetic") == 0) {
195 sensor_type = GEOMAGNETIC_SENSOR;
196 event = GEOMAGNETIC_RAW_DATA_EVENT;
198 else if (strcmp(argv[1], "orientation") == 0) {
199 sensor_type = ORIENTATION_SENSOR;
200 event = ORIENTATION_EVENT_RAW_DATA_REPORT_ON_TIME;
202 else if (strcmp(argv[1], "gravity") == 0) {
203 sensor_type = GRAVITY_SENSOR;
204 event = GRAVITY_EVENT_RAW_DATA_REPORT_ON_TIME;
206 else if (strcmp(argv[1], "linear_accel") == 0) {
207 sensor_type = LINEAR_ACCEL_SENSOR;
208 event = LINEAR_ACCEL_EVENT_RAW_DATA_REPORT_ON_TIME;
210 else if (strcmp(argv[1], "rotation_vector") == 0) {
211 sensor_type = ROTATION_VECTOR_SENSOR;
212 event = ROTATION_VECTOR_EVENT_RAW_DATA_REPORT_ON_TIME;
214 else if (strcmp(argv[1], "geomagnetic_rv") == 0) {
215 sensor_type = GEOMAGNETIC_RV_SENSOR;
216 event = GEOMAGNETIC_RV_RAW_DATA_EVENT;
218 else if (strcmp(argv[1], "light") == 0) {
219 sensor_type = LIGHT_SENSOR;
220 event = LIGHT_EVENT_LUX_DATA_REPORT_ON_TIME;
222 else if (strcmp(argv[1], "proximity") == 0) {
223 sensor_type = PROXIMITY_SENSOR;
224 event = PROXIMITY_CHANGE_STATE_EVENT;
230 interval = DEFAULT_EVENT_INTERVAL;
233 event = get_event_driven(sensor_type, argv[2]);
240 EVENT_NOT_ENTERED = FALSE;
244 interval = strtol(argv[3], &end1, 10);
247 printf("Conversion error, non-convertible part: %s\n", end1);
252 if (argc == 3 && EVENT_NOT_ENTERED) {
253 interval = strtol(argv[2], &end2, 10);
256 printf("Conversion error, non-convertible part: %s\n", end2);
261 sensor_t sensor = sensord_get_sensor(sensor_type);
262 handle = sensord_connect(sensor);
264 result = sensord_register_event(handle, event, interval, 0, callback, NULL);
267 printf("Can't register %s\n", argv[1]);
271 start_handle = sensord_start(handle, 0);
273 if (start_handle < 0) {
274 printf("Error\n\n\n\n");
275 sensord_unregister_event(handle, event);
276 sensord_disconnect(handle);
280 g_main_loop_run(mainloop);
281 g_main_loop_unref(mainloop);
283 result = sensord_unregister_event(handle, event);
290 stop_handle = sensord_stop(handle);
292 if (stop_handle < 0) {
297 sensord_disconnect(handle);