2 // Copyright (c) 2015 Intel Corporation
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
19 #include <utils/Log.h>
20 #include <cutils/properties.h>
21 #include <hardware/sensors.h>
22 #include "calibration.h"
24 #include "description.h"
25 #include "transform.h"
27 #include "filtering.h"
28 #include "enumeration.h"
30 #define GYRO_MIN_SAMPLES 5 /* Drop first few gyro samples after enable */
33 /*----------------------------------------------------------------------------*/
36 /* Macros related to Intel Sensor Hub */
38 #define GRAVITY 9.80665
42 #define NUMOFACCDATA 8.0
44 /* Conversion of acceleration data to SI units (m/s^2) */
45 #define CONVERT_A (GRAVITY_EARTH / LSG / NUMOFACCDATA)
46 #define CONVERT_A_X(x) ((float(x) / 1000) * (GRAVITY * -1.0))
47 #define CONVERT_A_Y(x) ((float(x) / 1000) * (GRAVITY * 1.0))
48 #define CONVERT_A_Z(x) ((float(x) / 1000) * (GRAVITY * 1.0))
50 /* Conversion of magnetic data to uT units */
51 #define CONVERT_M (1.0 / 6.6)
52 #define CONVERT_M_X (-CONVERT_M)
53 #define CONVERT_M_Y (-CONVERT_M)
54 #define CONVERT_M_Z (CONVERT_M)
56 /* Conversion of orientation data to degree units */
57 #define CONVERT_O (1.0 / 64)
58 #define CONVERT_O_A (CONVERT_O)
59 #define CONVERT_O_P (CONVERT_O)
60 #define CONVERT_O_R (-CONVERT_O)
62 /* Conversion of gyro data to SI units (radian/sec) */
63 #define CONVERT_GYRO (2000.0 / 32767 * M_PI / 180)
64 #define CONVERT_GYRO_X (-CONVERT_GYRO)
65 #define CONVERT_GYRO_Y (-CONVERT_GYRO)
66 #define CONVERT_GYRO_Z (CONVERT_GYRO)
68 #define BIT(x) (1 << (x))
70 #define PROXIMITY_THRESHOLD 1
72 inline unsigned int set_bit_range (int start, int end)
75 unsigned int value = 0;
77 for (i = start; i < end; ++i)
83 inline float convert_from_vtf_format (int size, int exponent, unsigned int value)
90 value = value & set_bit_range(0, size * 8);
92 if (value & BIT(size*8-1)) {
93 value = ((1LL << (size * 8)) - value);
100 exponent = abs(exponent);
101 for (i = 0; i < exponent; ++i)
102 divider = divider * 10;
104 return mul * sample/divider;
107 return mul * sample * pow(10.0, exponent);
110 /* Platform sensor orientation */
111 #define DEF_ORIENT_ACCEL_X -1
112 #define DEF_ORIENT_ACCEL_Y -1
113 #define DEF_ORIENT_ACCEL_Z -1
115 #define DEF_ORIENT_GYRO_X 1
116 #define DEF_ORIENT_GYRO_Y 1
117 #define DEF_ORIENT_GYRO_Z 1
120 #define CONVERT_FROM_VTF16(s,d,x) convert_from_vtf_format(s,d,x)
121 #define CONVERT_A_G_VTF16E14_X(s,d,x) (DEF_ORIENT_ACCEL_X * convert_from_vtf_format(s,d,x) * GRAVITY)
122 #define CONVERT_A_G_VTF16E14_Y(s,d,x) (DEF_ORIENT_ACCEL_Y * convert_from_vtf_format(s,d,x) * GRAVITY)
123 #define CONVERT_A_G_VTF16E14_Z(s,d,x) (DEF_ORIENT_ACCEL_Z * convert_from_vtf_format(s,d,x) * GRAVITY)
125 /* Degree/sec to radian/sec */
126 #define CONVERT_G_D_VTF16E14_X(s,d,x) (DEF_ORIENT_GYRO_X * convert_from_vtf_format(s,d,x) * M_PI / 180)
127 #define CONVERT_G_D_VTF16E14_Y(s,d,x) (DEF_ORIENT_GYRO_Y * convert_from_vtf_format(s,d,x) * M_PI / 180)
128 #define CONVERT_G_D_VTF16E14_Z(s,d,x) (DEF_ORIENT_GYRO_Z * convert_from_vtf_format(s,d,x) * M_PI / 180)
130 /* Milli gauss to micro tesla */
131 #define CONVERT_M_MG_VTF16E14_X(s,d,x) (convert_from_vtf_format(s,d,x) / 10)
132 #define CONVERT_M_MG_VTF16E14_Y(s,d,x) (convert_from_vtf_format(s,d,x) / 10)
133 #define CONVERT_M_MG_VTF16E14_Z(s,d,x) (convert_from_vtf_format(s,d,x) / 10)
136 static int64_t sample_as_int64 (unsigned char* sample, datum_info_t* type)
140 int zeroed_bits = type->storagebits - type->realbits;
146 if (type->endianness == 'b')
147 for (i=0; i<type->storagebits/8; i++)
148 u64 = (u64 << 8) | sample[i];
150 for (i=type->storagebits/8 - 1; i>=0; i--)
151 u64 = (u64 << 8) | sample[i];
153 u64 = (u64 >> type->shift) & (~0ULL >> zeroed_bits);
155 if (type->sign == 'u')
156 return (int64_t) u64; /* We don't handle unsigned 64 bits int */
160 switch (type->realbits) {
165 return (int64_t) (int8_t) u64;
168 return (int64_t) (int16_t) u64;
171 return (int64_t) (int32_t) u64;
174 return (int64_t) u64;
177 sign_mask = 1 << (type->realbits-1);
178 value_mask = sign_mask - 1;
181 return - ((~u64 & value_mask) + 1); /* Negative value: return 2-complement */
183 return (int64_t) u64; /* Positive value */
188 static void reorder_fields (float* data, unsigned char map[MAX_CHANNELS])
191 float temp[MAX_CHANNELS];
193 for (i=0; i<MAX_CHANNELS; i++)
194 temp[i] = data[map[i]];
196 for (i=0; i<MAX_CHANNELS; i++)
200 static void mount_correction (float* data, float mm[9])
206 temp[i] = data[0] * mm[i * 3] + data[1] * mm[i * 3 + 1] + data[2] * mm[i * 3 + 2];
212 static void clamp_gyro_readings_to_zero (int s, sensors_event_t* data)
221 /* If we're calibrated, don't filter out as much */
222 if (sensor[s].cal_level > 0)
223 near_zero = 0.02; /* rad/s */
227 /* If motion on all axes is small enough */
228 if (fabs(x) < near_zero && fabs(y) < near_zero && fabs(z) < near_zero) {
231 * Report that we're not moving at all... but not exactly zero as composite sensors (orientation, rotation vector) don't
232 * seem to react very well to it.
235 data->data[0] *= 0.000001;
236 data->data[1] *= 0.000001;
237 data->data[2] *= 0.000001;
242 static void process_event_gyro_uncal (int s, int i, sensors_event_t* data)
244 gyro_cal_t* gyro_data;
246 if (sensor[s].type == SENSOR_TYPE_GYROSCOPE) {
247 gyro_data = (gyro_cal_t*) sensor[s].cal_data;
249 memcpy(&sensor[i].sample, data, sizeof(sensors_event_t));
251 sensor[i].sample.type = SENSOR_TYPE_GYROSCOPE_UNCALIBRATED;
252 sensor[i].sample.sensor = s;
254 sensor[i].sample.data[0] = data->data[0] + gyro_data->bias_x;
255 sensor[i].sample.data[1] = data->data[1] + gyro_data->bias_y;
256 sensor[i].sample.data[2] = data->data[2] + gyro_data->bias_z;
258 sensor[i].sample.uncalibrated_gyro.bias[0] = gyro_data->bias_x;
259 sensor[i].sample.uncalibrated_gyro.bias[1] = gyro_data->bias_y;
260 sensor[i].sample.uncalibrated_gyro.bias[2] = gyro_data->bias_z;
262 sensor[i].report_pending = 1;
266 static void process_event_magn_uncal (int s, int i, sensors_event_t* data)
268 compass_cal_t* magn_data;
270 if (sensor[s].type == SENSOR_TYPE_MAGNETIC_FIELD) {
271 magn_data = (compass_cal_t*) sensor[s].cal_data;
273 memcpy(&sensor[i].sample, data, sizeof(sensors_event_t));
275 sensor[i].sample.type = SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED;
276 sensor[i].sample.sensor = s;
278 sensor[i].sample.data[0] = data->data[0] + magn_data->offset[0][0];
279 sensor[i].sample.data[1] = data->data[1] + magn_data->offset[1][0];
280 sensor[i].sample.data[2] = data->data[2] + magn_data->offset[2][0];
282 sensor[i].sample.uncalibrated_magnetic.bias[0] = magn_data->offset[0][0];
283 sensor[i].sample.uncalibrated_magnetic.bias[1] = magn_data->offset[1][0];
284 sensor[i].sample.uncalibrated_magnetic.bias[2] = magn_data->offset[2][0];
286 sensor[i].report_pending = 1;
290 static void process_event (int s, sensors_event_t* data)
293 * This gets the real event (post process - calibration, filtering & co.) and makes it into a virtual one.
294 * The specific processing function for each sensor will populate the necessary fields and set up the report pending flag.
299 /* Go through out virtual sensors and check if we can use this event */
300 for (i = 0; i < sensor_count; i++)
301 switch (sensor[i].type) {
302 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
303 process_event_gyro_uncal(s, i, data);
305 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
306 process_event_magn_uncal(s, i, data);
314 static int finalize_sample_default (int s, sensors_event_t* data)
316 /* Swap fields if we have a custom channel ordering on this sensor */
317 if (sensor[s].quirks & QUIRK_FIELD_ORDERING)
318 reorder_fields(data->data, sensor[s].order);
319 if (sensor[s].quirks & QUIRK_MOUNTING_MATRIX)
320 mount_correction(data->data, sensor[s].mounting_matrix);
322 sensor[s].event_count++;
324 switch (sensor[s].type) {
325 case SENSOR_TYPE_ACCELEROMETER:
326 /* Always consider the accelerometer accurate */
327 data->acceleration.status = SENSOR_STATUS_ACCURACY_HIGH;
328 if (sensor[s].quirks & QUIRK_BIASED)
329 calibrate_accel(s, data);
333 case SENSOR_TYPE_MAGNETIC_FIELD:
334 calibrate_compass (s, data);
338 case SENSOR_TYPE_GYROSCOPE:
340 /* Report medium accuracy by default ; higher accuracy levels will be reported once, and if, we achieve calibration. */
341 data->gyro.status = SENSOR_STATUS_ACCURACY_MEDIUM;
344 * We're only trying to calibrate data from continuously firing gyroscope drivers, as motion based ones use
345 * movement thresholds that may lead us to incorrectly estimate bias.
347 if (sensor[s].selected_trigger !=
348 sensor[s].motion_trigger_name)
349 calibrate_gyro(s, data);
352 * For noisy sensors drop a few samples to make sure we have at least GYRO_MIN_SAMPLES events in the
353 * filtering queue. This improves mean and std dev.
355 if (sensor[s].filter_type) {
356 if (sensor[s].selected_trigger !=
357 sensor[s].motion_trigger_name &&
358 sensor[s].event_count < GYRO_MIN_SAMPLES)
364 /* Clamp near zero moves to (0,0,0) if appropriate */
365 clamp_gyro_readings_to_zero(s, data);
368 case SENSOR_TYPE_PROXIMITY:
370 * See iio spec for in_proximity* - depending on the device
371 * this value is either in meters either unit-less and cannot
372 * be translated to SI units. Where the translation is not possible
373 * lower values indicate something is close and higher ones indicate distance.
375 if (data->data[0] > PROXIMITY_THRESHOLD)
376 data->data[0] = PROXIMITY_THRESHOLD;
378 /* ... fall through ... */
379 case SENSOR_TYPE_LIGHT:
380 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
381 case SENSOR_TYPE_TEMPERATURE:
382 case SENSOR_TYPE_INTERNAL_ILLUMINANCE:
383 case SENSOR_TYPE_INTERNAL_INTENSITY:
384 /* Only keep two decimals for these readings */
385 data->data[0] = 0.01 * ((int) (data->data[0] * 100));
387 /* These are on change sensors ; drop the sample if it has the same value as the previously reported one. */
388 if (data->data[0] == sensor[s].prev_val.data)
391 sensor[s].prev_val.data = data->data[0];
393 case SENSOR_TYPE_STEP_COUNTER:
394 if (data->u64.step_counter == sensor[s].prev_val.data64)
396 sensor[s].prev_val.data64 = data->u64.data[0];
401 /* If there are active virtual sensors depending on this one - process the event */
402 if (sensor[s].ref_count)
403 process_event(s, data);
406 return 1; /* Return sample to Android */
410 static float transform_sample_default (int s, int c, unsigned char* sample_data)
412 datum_info_t* sample_type = &sensor[s].channel[c].type_info;
413 int64_t s64 = sample_as_int64(sample_data, sample_type);
414 float scale = sensor[s].scale ? sensor[s].scale : sensor[s].channel[c].scale;
416 /* In case correction has been requested using properties, apply it */
417 float correction = sensor[s].channel[c].opt_scale;
419 /* Correlated with "acquire_immediate_value" method */
420 if (sensor[s].type == SENSOR_TYPE_MAGNETIC_FIELD)
421 return CONVERT_GAUSS_TO_MICROTESLA((sensor[s].offset + s64) * scale) * correction;
423 /* Apply default scaling rules */
424 return (sensor[s].offset + s64) * scale * correction;
428 static int finalize_sample_ISH (int s, sensors_event_t* data)
430 float pitch, roll, yaw;
432 /* Swap fields if we have a custom channel ordering on this sensor */
433 if (sensor[s].quirks & QUIRK_FIELD_ORDERING)
434 reorder_fields(data->data, sensor[s].order);
436 if (sensor[s].type == SENSOR_TYPE_ORIENTATION) {
438 pitch = data->data[0];
439 roll = data->data[1];
442 data->data[0] = 360.0 - yaw;
443 data->data[1] = -pitch;
444 data->data[2] = -roll;
447 /* Add this event to our global records, for filtering purposes */
448 record_sample(s, data);
450 return 1; /* Return sample to Android */
454 static float transform_sample_ISH (int s, int c, unsigned char* sample_data)
456 datum_info_t* sample_type = &sensor[s].channel[c].type_info;
457 int val = (int) sample_as_int64(sample_data, sample_type);
459 int data_bytes = (sample_type->realbits)/8;
460 int exponent = sensor[s].offset;
462 /* In case correction has been requested using properties, apply it */
463 correction = sensor[s].channel[c].opt_scale;
465 switch (sensor_desc[s].type) {
466 case SENSOR_TYPE_ACCELEROMETER:
469 return correction * CONVERT_A_G_VTF16E14_X(data_bytes, exponent, val);
472 return correction * CONVERT_A_G_VTF16E14_Y(data_bytes, exponent, val);
475 return correction * CONVERT_A_G_VTF16E14_Z(data_bytes, exponent, val);
479 case SENSOR_TYPE_GYROSCOPE:
482 return correction * CONVERT_G_D_VTF16E14_X(data_bytes, exponent, val);
485 return correction * CONVERT_G_D_VTF16E14_Y(data_bytes, exponent, val);
488 return correction * CONVERT_G_D_VTF16E14_Z(data_bytes, exponent, val);
492 case SENSOR_TYPE_MAGNETIC_FIELD:
495 return correction * CONVERT_M_MG_VTF16E14_X(data_bytes, exponent, val);
498 return correction * CONVERT_M_MG_VTF16E14_Y(data_bytes, exponent, val);
501 return correction * CONVERT_M_MG_VTF16E14_Z(data_bytes, exponent, val);
505 case SENSOR_TYPE_LIGHT:
508 case SENSOR_TYPE_ORIENTATION:
509 return correction * convert_from_vtf_format(data_bytes, exponent, val);
511 case SENSOR_TYPE_ROTATION_VECTOR:
512 return correction * convert_from_vtf_format(data_bytes, exponent, val);
519 void select_transform (int s)
521 char prop_name[PROP_NAME_MAX];
522 char prop_val[PROP_VALUE_MAX];
523 int i = sensor[s].catalog_index;
524 const char *prefix = sensor_catalog[i].tag;
526 sprintf(prop_name, PROP_BASE, prefix, "transform");
528 if (property_get(prop_name, prop_val, ""))
529 if (!strcmp(prop_val, "ISH")) {
530 ALOGI( "Using Intel Sensor Hub semantics on %s\n", sensor[s].friendly_name);
532 sensor[s].ops.transform = transform_sample_ISH;
533 sensor[s].ops.finalize = finalize_sample_ISH;
537 sensor[s].ops.transform = transform_sample_default;
538 sensor[s].ops.finalize = finalize_sample_default;
542 float acquire_immediate_float_value (int s, int c)
544 char sysfs_path[PATH_MAX];
547 int dev_num = sensor[s].dev_num;
548 int i = sensor[s].catalog_index;
549 const char* raw_path = sensor_catalog[i].channel[c].raw_path;
550 const char* input_path = sensor_catalog[i].channel[c].input_path;
551 float scale = sensor[s].scale ? sensor[s].scale : sensor[s].channel[c].scale;
552 float offset = sensor[s].offset;
555 /* In case correction has been requested using properties, apply it */
556 correction = sensor[s].channel[c].opt_scale;
558 /* Acquire a sample value for sensor s / channel c through sysfs */
560 if (sensor[s].channel[c].input_path_present) {
561 sprintf(sysfs_path, BASE_PATH "%s", dev_num, input_path);
562 ret = sysfs_read_float(sysfs_path, &val);
565 if (sensor[s].type == SENSOR_TYPE_MAGNETIC_FIELD)
566 return CONVERT_GAUSS_TO_MICROTESLA (val * correction);
567 return val * correction;
571 if (!sensor[s].channel[c].raw_path_present)
574 sprintf(sysfs_path, BASE_PATH "%s", dev_num, raw_path);
575 ret = sysfs_read_float(sysfs_path, &val);
581 * There is no transform ops defined yet for raw sysfs values.
582 * Use this function to perform transformation as well.
584 if (sensor[s].type == SENSOR_TYPE_MAGNETIC_FIELD)
585 return CONVERT_GAUSS_TO_MICROTESLA ((val + offset) * scale) * correction;
587 return (val + offset) * scale * correction;
590 uint64_t acquire_immediate_uint64_value (int s, int c)
592 char sysfs_path[PATH_MAX];
595 int dev_num = sensor[s].dev_num;
596 int i = sensor[s].catalog_index;
597 const char* raw_path = sensor_catalog[i].channel[c].raw_path;
598 const char* input_path = sensor_catalog[i].channel[c].input_path;
599 float scale = sensor[s].scale ? sensor[s].scale : sensor[s].channel[c].scale;
600 float offset = sensor[s].offset;
603 /* In case correction has been requested using properties, apply it */
604 correction = sensor[s].channel[c].opt_scale;
606 /* Acquire a sample value for sensor s / channel c through sysfs */
608 if (sensor[s].channel[c].input_path_present) {
609 sprintf(sysfs_path, BASE_PATH "%s", dev_num, input_path);
610 ret = sysfs_read_uint64(sysfs_path, &val);
613 return val * correction;
616 if (!sensor[s].channel[c].raw_path_present)
619 sprintf(sysfs_path, BASE_PATH "%s", dev_num, raw_path);
620 ret = sysfs_read_uint64(sysfs_path, &val);
625 return (val + offset) * scale * correction;