2 * Copyright (C) 2014 Intel Corporation.
10 #include <sys/socket.h>
11 #include <utils/Log.h>
12 #include <hardware/sensors.h>
14 #include "enumeration.h"
16 #include "transform.h"
17 #include "calibration.h"
19 /* Currently active sensors count, per device */
20 static int poll_sensors_per_dev[MAX_DEVICES]; /* poll-mode sensors */
21 static int trig_sensors_per_dev[MAX_DEVICES]; /* trigger, event based */
23 static int device_fd[MAX_DEVICES]; /* fd on the /dev/iio:deviceX file */
25 static int poll_fd; /* epoll instance covering all enabled sensors */
27 static int active_poll_sensors; /* Number of enabled poll-mode sensors */
29 /* We use pthread condition variables to get worker threads out of sleep */
30 static pthread_cond_t thread_release_cond [MAX_SENSORS];
31 static pthread_mutex_t thread_release_mutex [MAX_SENSORS];
34 * We associate tags to each of our poll set entries. These tags have the
36 * - a iio device number if the fd is a iio character device fd
37 * - THREAD_REPORT_TAG_BASE + sensor handle if the fd is the receiving end of a
38 * pipe used by a sysfs data acquisition thread
40 #define THREAD_REPORT_TAG_BASE 0x00010000
43 static int enable_buffer(int dev_num, int enabled)
45 char sysfs_path[PATH_MAX];
47 sprintf(sysfs_path, ENABLE_PATH, dev_num);
49 /* Low level, non-multiplexed, enable/disable routine */
50 return sysfs_write_int(sysfs_path, enabled);
54 static int setup_trigger(int dev_num, const char* trigger_val)
56 char sysfs_path[PATH_MAX];
58 sprintf(sysfs_path, TRIGGER_PATH, dev_num);
60 return sysfs_write_str(sysfs_path, trigger_val);
64 void build_sensor_report_maps(int dev_num)
67 * Read sysfs files from a iio device's scan_element directory, and
68 * build a couple of tables from that data. These tables will tell, for
69 * each sensor, where to gather relevant data in a device report, i.e.
70 * the structure that we read from the /dev/iio:deviceX file in order to
71 * sensor report, itself being the data that we return to Android when a
72 * sensor poll completes. The mapping should be straightforward in the
73 * case where we have a single sensor active per iio device but, this is
74 * not the general case. In general several sensors can be handled
75 * through a single iio device, and the _en, _index and _type syfs
76 * entries all concur to paint a picture of what the structure of the
86 char spec_buf[MAX_TYPE_SPEC_LEN];
87 struct datum_info_t* ch_info;
89 char sysfs_path[PATH_MAX];
92 int channel_size_from_index[MAX_SENSORS * MAX_CHANNELS] = { 0 };
93 int sensor_handle_from_index[MAX_SENSORS * MAX_CHANNELS] = { 0 };
94 int channel_number_from_index[MAX_SENSORS * MAX_CHANNELS] = { 0 };
98 /* For each sensor that is linked to this device */
99 for (s=0; s<sensor_count; s++) {
100 if (sensor_info[s].dev_num != dev_num)
103 i = sensor_info[s].catalog_index;
105 /* Read channel details through sysfs attributes */
106 for (c=0; c<sensor_info[s].num_channels; c++) {
108 /* Read _type file */
109 sprintf(sysfs_path, CHANNEL_PATH "%s",
110 sensor_info[s].dev_num,
111 sensor_catalog[i].channel[c].type_path);
113 n = sysfs_read_str(sysfs_path, spec_buf,
117 ALOGW( "Failed to read type: %s\n",
122 ch_spec = sensor_info[s].channel[c].type_spec;
124 memcpy(ch_spec, spec_buf, sizeof(spec_buf));
126 ch_info = &sensor_info[s].channel[c].type_info;
128 size = decode_type_spec(ch_spec, ch_info);
130 /* Read _index file */
131 sprintf(sysfs_path, CHANNEL_PATH "%s",
132 sensor_info[s].dev_num,
133 sensor_catalog[i].channel[c].index_path);
135 n = sysfs_read_int(sysfs_path, &ch_index);
138 ALOGW( "Failed to read index: %s\n",
143 if (ch_index >= MAX_SENSORS) {
144 ALOGE("Index out of bounds!: %s\n", sysfs_path);
148 /* Record what this index is about */
150 sensor_handle_from_index [ch_index] = s;
151 channel_number_from_index[ch_index] = c;
152 channel_size_from_index [ch_index] = size;
157 /* Stop sampling - if we are recovering from hal restart */
158 enable_buffer(dev_num, 0);
159 setup_trigger(dev_num, "\n");
161 /* Turn on channels we're aware of */
162 for (c=0;c<sensor_info[s].num_channels; c++) {
163 sprintf(sysfs_path, CHANNEL_PATH "%s",
164 sensor_info[s].dev_num,
165 sensor_catalog[i].channel[c].en_path);
166 sysfs_write_int(sysfs_path, 1);
170 ALOGI("Found %d channels on iio device %d\n", known_channels, dev_num);
173 * Now that we know which channels are defined, their sizes and their
174 * ordering, update channels offsets within device report. Note: there
175 * is a possibility that several sensors share the same index, with
176 * their data fields being isolated by masking and shifting as specified
177 * through the real bits and shift values in type attributes. This case
178 * is not currently supported. Also, the code below assumes no hole in
179 * the sequence of indices, so it is dependent on discovery of all
183 for (i=0; i<MAX_SENSORS * MAX_CHANNELS; i++) {
184 s = sensor_handle_from_index[i];
185 c = channel_number_from_index[i];
186 size = channel_size_from_index[i];
191 ALOGI("S%d C%d : offset %d, size %d, type %s\n",
192 s, c, offset, size, sensor_info[s].channel[c].type_spec);
194 sensor_info[s].channel[c].offset = offset;
195 sensor_info[s].channel[c].size = size;
202 int adjust_counters (int s, int enabled)
205 * Adjust counters based on sensor enable action. Return values are:
206 * -1 if there's an inconsistency: abort action in this case
207 * 0 if the operation was completed and we're all set
208 * 1 if we toggled the state of the sensor and there's work left
211 int dev_num = sensor_info[s].dev_num;
212 int catalog_index = sensor_info[s].catalog_index;
213 int sensor_type = sensor_catalog[catalog_index].type;
215 /* Refcount per sensor, in terms of enable count */
217 ALOGI("Enabling sensor %d (iio device %d: %s)\n",
218 s, dev_num, sensor_info[s].friendly_name);
220 sensor_info[s].enable_count++;
222 if (sensor_info[s].enable_count > 1)
223 return 0; /* The sensor was, and remains, in use */
225 switch (sensor_type) {
226 case SENSOR_TYPE_MAGNETIC_FIELD:
227 compass_read_data(&sensor_info[s]);
230 case SENSOR_TYPE_GYROSCOPE:
231 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
232 gyro_cal_init(&sensor_info[s]);
236 if (sensor_info[s].enable_count == 0)
237 return -1; /* Spurious disable call */
239 ALOGI("Disabling sensor %d (iio device %d: %s)\n", s, dev_num,
240 sensor_info[s].friendly_name);
242 sensor_info[s].enable_count--;
244 if (sensor_info[s].enable_count > 0)
245 return 0; /* The sensor was, and remains, in use */
247 /* Sensor disabled, lower report available flag */
248 sensor_info[s].report_pending = 0;
250 if (sensor_type == SENSOR_TYPE_MAGNETIC_FIELD)
251 compass_store_data(&sensor_info[s]);
255 /* If uncalibrated type and pair is already active don't adjust counters */
256 if (sensor_type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED &&
257 sensor_info[sensor_info[s].pair_idx].enable_count != 0)
260 /* We changed the state of a sensor - adjust per iio device counters */
262 /* If this is a regular event-driven sensor */
263 if (sensor_info[s].num_channels) {
266 trig_sensors_per_dev[dev_num]++;
268 trig_sensors_per_dev[dev_num]--;
274 active_poll_sensors++;
275 poll_sensors_per_dev[dev_num]++;
279 active_poll_sensors--;
280 poll_sensors_per_dev[dev_num]--;
285 static int get_field_count (int s)
287 int catalog_index = sensor_info[s].catalog_index;
288 int sensor_type = sensor_catalog[catalog_index].type;
290 switch (sensor_type) {
291 case SENSOR_TYPE_ACCELEROMETER: /* m/s^2 */
292 case SENSOR_TYPE_MAGNETIC_FIELD: /* micro-tesla */
293 case SENSOR_TYPE_ORIENTATION: /* degrees */
294 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
295 case SENSOR_TYPE_GYROSCOPE: /* radians/s */
298 case SENSOR_TYPE_LIGHT: /* SI lux units */
299 case SENSOR_TYPE_AMBIENT_TEMPERATURE: /* °C */
300 case SENSOR_TYPE_TEMPERATURE: /* °C */
301 case SENSOR_TYPE_PROXIMITY: /* centimeters */
302 case SENSOR_TYPE_PRESSURE: /* hecto-pascal */
303 case SENSOR_TYPE_RELATIVE_HUMIDITY: /* percent */
306 case SENSOR_TYPE_ROTATION_VECTOR:
310 ALOGE("Unknown sensor type!\n");
311 return 0; /* Drop sample */
316 static void time_add(struct timespec *out, struct timespec *in, int64_t ns)
318 int64_t target_ts = 1000000000LL * in->tv_sec + in->tv_nsec + ns;
320 out->tv_sec = target_ts / 1000000000;
321 out->tv_nsec = target_ts % 1000000000;
325 static void* acquisition_routine (void* param)
328 * Data acquisition routine run in a dedicated thread, covering a single
329 * sensor. This loop will periodically retrieve sampling data through
330 * sysfs, then package it as a sample and transfer it to our master poll
331 * loop through a report fd. Checks for a cancellation signal quite
332 * frequently, as the thread may be disposed of at any time. Note that
333 * Bionic does not provide pthread_cancel / pthread_testcancel...
339 struct sensors_event_t data = {0};
343 struct timespec entry_time;
344 struct timespec target_time;
347 ALOGV("Entering data acquisition thread for sensor %d\n", s);
349 if (s < 0 || s >= sensor_count) {
350 ALOGE("Invalid sensor handle!\n");
354 if (!sensor_info[s].sampling_rate) {
355 ALOGE("Zero rate in acquisition routine for sensor %d\n", s);
359 num_fields = get_field_count(s);
362 * Each condition variable is associated to a mutex that has to be
363 * locked by the thread that's waiting on it. We use these condition
364 * variables to get the acquisition threads out of sleep quickly after
365 * the sampling rate is adjusted, or the sensor is disabled.
367 pthread_mutex_lock(&thread_release_mutex[s]);
370 /* Pinpoint the moment we start sampling */
371 clock_gettime(CLOCK_REALTIME, &entry_time);
373 ALOGV("Acquiring sample data for sensor %d through sysfs\n", s);
375 /* Read values through sysfs */
376 for (c=0; c<num_fields; c++) {
377 data.data[c] = acquire_immediate_value(s, c);
379 /* Check and honor termination requests */
380 if (sensor_info[s].thread_data_fd[1] == -1)
383 ALOGV("\tfield %d: %f\n", c, data.data[c]);
387 /* If the sample looks good */
388 if (sensor_info[s].ops.finalize(s, &data)) {
390 /* Pipe it for transmission to poll loop */
391 ret = write( sensor_info[s].thread_data_fd[1],
393 num_fields * sizeof(float));
396 /* Check and honor termination requests */
397 if (sensor_info[s].thread_data_fd[1] == -1)
401 period = 1000000000LL / sensor_info[s].sampling_rate;
403 time_add(&target_time, &entry_time, period);
406 * Wait until the sampling time elapses, or a rate change is
407 * signaled, or a thread exit is requested.
409 ret = pthread_cond_timedwait( &thread_release_cond[s],
410 &thread_release_mutex[s],
413 /* Check and honor termination requests */
414 if (sensor_info[s].thread_data_fd[1] == -1)
419 ALOGV("Acquisition thread for S%d exiting\n", s);
420 pthread_mutex_unlock(&thread_release_mutex[s]);
426 static void start_acquisition_thread (int s)
428 int incoming_data_fd;
431 struct epoll_event ev = {0};
433 ALOGV("Initializing acquisition context for sensor %d\n", s);
435 /* Create condition variable and mutex for quick thread release */
436 ret = pthread_cond_init(&thread_release_cond[s], NULL);
437 ret = pthread_mutex_init(&thread_release_mutex[s], NULL);
439 /* Create a pipe for inter thread communication */
440 ret = pipe(sensor_info[s].thread_data_fd);
442 incoming_data_fd = sensor_info[s].thread_data_fd[0];
445 ev.data.u32 = THREAD_REPORT_TAG_BASE + s;
447 /* Add incoming side of pipe to our poll set, with a suitable tag */
448 ret = epoll_ctl(poll_fd, EPOLL_CTL_ADD, incoming_data_fd , &ev);
450 /* Create and start worker thread */
451 ret = pthread_create( &sensor_info[s].acquisition_thread,
458 static void stop_acquisition_thread (int s)
460 int incoming_data_fd = sensor_info[s].thread_data_fd[0];
461 int outgoing_data_fd = sensor_info[s].thread_data_fd[1];
463 ALOGV("Tearing down acquisition context for sensor %d\n", s);
465 /* Delete the incoming side of the pipe from our poll set */
466 epoll_ctl(poll_fd, EPOLL_CTL_DEL, incoming_data_fd, NULL);
468 /* Mark the pipe ends as invalid ; that's a cheap exit flag */
469 sensor_info[s].thread_data_fd[0] = -1;
470 sensor_info[s].thread_data_fd[1] = -1;
472 /* Close both sides of our pipe */
473 close(incoming_data_fd);
474 close(outgoing_data_fd);
476 /* Stop acquisition thread and clean up thread handle */
477 pthread_cond_signal(&thread_release_cond[s]);
478 pthread_join(sensor_info[s].acquisition_thread, NULL);
480 /* Clean up our sensor descriptor */
481 sensor_info[s].acquisition_thread = -1;
483 /* Delete condition variable and mutex */
484 pthread_cond_destroy(&thread_release_cond[s]);
485 pthread_mutex_destroy(&thread_release_mutex[s]);
489 int sensor_activate(int s, int enabled)
491 char device_name[PATH_MAX];
492 char trigger_name[MAX_NAME_SIZE + 16];
494 struct epoll_event ev = {0};
497 int dev_num = sensor_info[s].dev_num;
498 int i = sensor_info[s].catalog_index;
499 int is_poll_sensor = !sensor_info[s].num_channels;
501 /* If we want to activate gyro calibrated and gyro uncalibrated is activated
502 * Deactivate gyro uncalibrated - Uncalibrated releases handler
503 * Activate gyro calibrated - Calibrated has handler
504 * Reactivate gyro uncalibrated - Uncalibrated gets data from calibrated */
506 /* If we want to deactivate gyro calibrated and gyro uncalibrated is active
507 * Deactivate gyro uncalibrated - Uncalibrated no longer gets data from handler
508 * Deactivate gyro calibrated - Calibrated releases handler
509 * Reactivate gyro uncalibrated - Uncalibrated has handler */
511 if (sensor_catalog[sensor_info[s].catalog_index].type == SENSOR_TYPE_GYROSCOPE &&
512 sensor_info[s].pair_idx && sensor_info[sensor_info[s].pair_idx].enable_count != 0) {
514 sensor_activate(sensor_info[s].pair_idx, 0);
515 ret = sensor_activate(s, enabled);
516 sensor_activate(sensor_info[s].pair_idx, 1);
520 ret = adjust_counters(s, enabled);
522 /* If the operation was neutral in terms of state, we're done */
527 if (!is_poll_sensor) {
530 enable_buffer(dev_num, 0);
531 setup_trigger(dev_num, "\n");
533 /* If there's at least one sensor enabled on this iio device */
534 if (trig_sensors_per_dev[dev_num]) {
535 sprintf(trigger_name, "%s-dev%d",
536 sensor_info[s].internal_name, dev_num);
539 setup_trigger(dev_num, trigger_name);
540 enable_buffer(dev_num, 1);
545 * Make sure we have a fd on the character device ; conversely, close
546 * the fd if no one is using associated sensors anymore. The assumption
547 * here is that the underlying driver will power on the relevant
548 * hardware block while someone holds a fd on the device.
550 dev_fd = device_fd[dev_num];
554 stop_acquisition_thread(s);
556 if (dev_fd != -1 && !poll_sensors_per_dev[dev_num] &&
557 !trig_sensors_per_dev[dev_num]) {
559 * Stop watching this fd. This should be a no-op
560 * in case this fd was not in the poll set.
562 epoll_ctl(poll_fd, EPOLL_CTL_DEL, dev_fd, NULL);
565 device_fd[dev_num] = -1;
571 /* First enabled sensor on this iio device */
572 sprintf(device_name, DEV_FILE_PATH, dev_num);
573 dev_fd = open(device_name, O_RDONLY | O_NONBLOCK);
575 device_fd[dev_num] = dev_fd;
578 ALOGE("Could not open fd on %s (%s)\n",
579 device_name, strerror(errno));
580 adjust_counters(s, 0);
584 ALOGV("Opened %s: fd=%d\n", device_name, dev_fd);
586 if (!is_poll_sensor) {
588 /* Add this iio device fd to the set of watched fds */
590 ev.data.u32 = dev_num;
592 ret = epoll_ctl(poll_fd, EPOLL_CTL_ADD, dev_fd, &ev);
595 ALOGE( "Failed adding %d to poll set (%s)\n",
596 dev_fd, strerror(errno));
600 /* Note: poll-mode fds are not readable */
604 /* Ensure that on-change sensors send at least one event after enable */
605 sensor_info[s].prev_val = -1;
608 start_acquisition_thread(s);
614 static int integrate_device_report(int dev_num)
618 unsigned char buf[MAX_SENSOR_REPORT_SIZE] = { 0 };
620 unsigned char *target;
621 unsigned char *source;
625 /* There's an incoming report on the specified iio device char dev fd */
627 if (dev_num < 0 || dev_num >= MAX_DEVICES) {
628 ALOGE("Event reported on unexpected iio device %d\n", dev_num);
632 if (device_fd[dev_num] == -1) {
633 ALOGE("Ignoring stale report on iio device %d\n", dev_num);
637 ts = get_timestamp();
639 len = read(device_fd[dev_num], buf, MAX_SENSOR_REPORT_SIZE);
642 ALOGE("Could not read report from iio device %d (%s)\n",
643 dev_num, strerror(errno));
647 ALOGV("Read %d bytes from iio device %d\n", len, dev_num);
649 for (s=0; s<MAX_SENSORS; s++)
650 if (sensor_info[s].dev_num == dev_num &&
651 sensor_info[s].enable_count) {
655 /* Copy data from device to sensor report buffer */
656 for (c=0; c<sensor_info[s].num_channels; c++) {
658 target = sensor_info[s].report_buffer +
661 source = buf + sensor_info[s].channel[c].offset;
663 size = sensor_info[s].channel[c].size;
665 memcpy(target, source, size);
670 ALOGV("Sensor %d report available (%d bytes)\n", s,
673 sensor_info[s].report_ts = ts;
674 sensor_info[s].report_pending = 1;
681 static int propagate_sensor_report(int s, struct sensors_event_t *data)
683 /* There's a sensor report pending for this sensor ; transmit it */
685 int catalog_index = sensor_info[s].catalog_index;
686 int sensor_type = sensor_catalog[catalog_index].type;
687 int num_fields = get_field_count(s);
689 unsigned char* current_sample;
691 /* If there's nothing to return... we're done */
696 /* Only return uncalibrated event if also gyro active */
697 if (sensor_type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED &&
698 sensor_info[sensor_info[s].pair_idx].enable_count != 0)
701 memset(data, 0, sizeof(sensors_event_t));
703 data->version = sizeof(sensors_event_t);
705 data->type = sensor_type;
706 data->timestamp = sensor_info[s].report_ts;
708 ALOGV("Sample on sensor %d (type %d):\n", s, sensor_type);
710 current_sample = sensor_info[s].report_buffer;
712 /* If this is a poll sensor */
713 if (!sensor_info[s].num_channels) {
714 /* Use the data provided by the acquisition thread */
715 ALOGV("Reporting data from worker thread for S%d\n", s);
716 memcpy(data->data, current_sample, num_fields * sizeof(float));
720 /* Convert the data into the expected Android-level format */
721 for (c=0; c<num_fields; c++) {
723 data->data[c] = sensor_info[s].ops.transform
724 (s, c, current_sample);
726 ALOGV("\tfield %d: %f\n", c, data->data[c]);
727 current_sample += sensor_info[s].channel[c].size;
731 * The finalize routine, in addition to its late sample processing duty,
732 * has the final say on whether or not the sample gets sent to Android.
734 return sensor_info[s].ops.finalize(s, data);
738 static void integrate_thread_report (uint32_t tag)
740 int s = tag - THREAD_REPORT_TAG_BASE;
744 expected_len = get_field_count(s) * sizeof(float);
746 len = read(sensor_info[s].thread_data_fd[0],
747 sensor_info[s].report_buffer,
750 if (len == expected_len) {
751 sensor_info[s].report_ts = get_timestamp();
752 sensor_info[s].report_pending = 1;
757 int sensor_poll(struct sensors_event_t* data, int count)
762 struct epoll_event ev[MAX_DEVICES];
767 /* Get one or more events from our collection of sensors */
769 return_available_sensor_reports:
773 /* Check our sensor collection for available reports */
774 for (s=0; s<sensor_count && returned_events < count; s++)
775 if (sensor_info[s].report_pending) {
778 sensor_info[s].report_pending = 0;
780 /* Report this event if it looks OK */
781 event_count = propagate_sensor_report(s, &data[returned_events]);
783 /* Duplicate only if both cal & uncal are active */
784 if (sensor_catalog[sensor_info[s].catalog_index].type == SENSOR_TYPE_GYROSCOPE &&
785 sensor_info[s].pair_idx && sensor_info[sensor_info[s].pair_idx].enable_count != 0) {
786 struct gyro_cal* gyro_data = (struct gyro_cal*) sensor_info[s].cal_data;
788 memcpy(&data[returned_events + event_count], &data[returned_events],
789 sizeof(struct sensors_event_t) * event_count);
790 for (i = 0; i < event_count; i++) {
791 data[returned_events + i].type = SENSOR_TYPE_GYROSCOPE_UNCALIBRATED;
792 data[returned_events + i].sensor = sensor_info[s].pair_idx;
794 data[returned_events + i].data[0] = data[returned_events + i].data[0] + gyro_data->bias[0];
795 data[returned_events + i].data[1] = data[returned_events + i].data[1] + gyro_data->bias[1];
796 data[returned_events + i].data[2] = data[returned_events + i].data[2] + gyro_data->bias[2];
798 data[returned_events + i].uncalibrated_gyro.bias[0] = gyro_data->bias[0];
799 data[returned_events + i].uncalibrated_gyro.bias[1] = gyro_data->bias[1];
800 data[returned_events + i].uncalibrated_gyro.bias[2] = gyro_data->bias[2];
804 sensor_info[sensor_info[s].pair_idx].report_pending = 0;
805 returned_events += event_count;
807 * If the sample was deemed invalid or unreportable,
808 * e.g. had the same value as the previously reported
809 * value for a 'on change' sensor, silently drop it.
814 return returned_events;
818 ALOGV("Awaiting sensor data\n");
820 nfds = epoll_wait(poll_fd, ev, MAX_DEVICES, -1);
823 ALOGI("epoll_wait returned -1 (%s)\n", strerror(errno));
827 ALOGV("%d fds signalled\n", nfds);
829 /* For each of the signalled sources */
830 for (i=0; i<nfds; i++)
831 if (ev[i].events == EPOLLIN)
832 switch (ev[i].data.u32) {
833 case 0 ... MAX_DEVICES-1:
834 /* Read report from iio char dev fd */
835 integrate_device_report(ev[i].data.u32);
838 case THREAD_REPORT_TAG_BASE ...
839 THREAD_REPORT_TAG_BASE + MAX_SENSORS-1:
840 /* Get report from acquisition thread */
841 integrate_thread_report(ev[i].data.u32);
845 ALOGW("Unexpected event source!\n");
849 goto return_available_sensor_reports;
853 int sensor_set_delay(int s, int64_t ns)
855 /* Set the rate at which a specific sensor should report events */
857 /* See Android sensors.h for indication on sensor trigger modes */
859 char sysfs_path[PATH_MAX];
860 char avail_sysfs_path[PATH_MAX];
861 int dev_num = sensor_info[s].dev_num;
862 int i = sensor_info[s].catalog_index;
863 const char *prefix = sensor_catalog[i].tag;
864 float new_sampling_rate; /* Granted sampling rate after arbitration */
865 float cur_sampling_rate; /* Currently used sampling rate */
866 int per_sensor_sampling_rate;
867 int per_device_sampling_rate;
868 float max_supported_rate = 0;
875 ALOGE("Rejecting zero delay request on sensor %d\n", s);
879 new_sampling_rate = 1000000000LL/ns;
882 * Artificially limit ourselves to 1 Hz or higher. This is mostly to
883 * avoid setting up the stage for divisions by zero.
885 if (new_sampling_rate < 1)
886 new_sampling_rate = 1;
888 sensor_info[s].sampling_rate = new_sampling_rate;
890 /* If we're dealing with a poll-mode sensor */
891 if (!sensor_info[s].num_channels) {
892 /* Interrupt current sleep so the new sampling gets used */
893 pthread_cond_signal(&thread_release_cond[s]);
897 sprintf(sysfs_path, SENSOR_SAMPLING_PATH, dev_num, prefix);
899 if (sysfs_read_float(sysfs_path, &cur_sampling_rate) != -1) {
900 per_sensor_sampling_rate = 1;
901 per_device_sampling_rate = 0;
903 per_sensor_sampling_rate = 0;
905 sprintf(sysfs_path, DEVICE_SAMPLING_PATH, dev_num);
907 if (sysfs_read_float(sysfs_path, &cur_sampling_rate) != -1)
908 per_device_sampling_rate = 1;
910 per_device_sampling_rate = 0;
913 if (!per_sensor_sampling_rate && !per_device_sampling_rate) {
914 ALOGE("No way to adjust sampling rate on sensor %d\n", s);
918 /* Coordinate with others active sensors on the same device, if any */
919 if (per_device_sampling_rate)
920 for (n=0; n<sensor_count; n++)
921 if (n != s && sensor_info[n].dev_num == dev_num &&
922 sensor_info[n].num_channels &&
923 sensor_info[n].enable_count &&
924 sensor_info[n].sampling_rate > new_sampling_rate)
925 new_sampling_rate= sensor_info[n].sampling_rate;
927 /* Check if we have contraints on allowed sampling rates */
929 sprintf(avail_sysfs_path, DEVICE_AVAIL_FREQ_PATH, dev_num);
931 if (sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf)) > 0){
934 /* Decode allowed sampling rates string, ex: "10 20 50 100" */
936 /* While we're not at the end of the string */
937 while (*cursor && cursor[0]) {
939 /* Decode a single value */
940 sr = strtod(cursor, NULL);
942 if (sr > max_supported_rate)
943 max_supported_rate = sr;
945 /* If this matches the selected rate, we're happy */
946 if (new_sampling_rate == sr)
950 * If we reached a higher value than the desired rate,
951 * adjust selected rate so it matches the first higher
952 * available one and stop parsing - this makes the
953 * assumption that rates are sorted by increasing value
954 * in the allowed frequencies string.
956 if (sr > new_sampling_rate) {
957 new_sampling_rate = sr;
962 while (cursor[0] && !isspace(cursor[0]))
966 while (cursor[0] && isspace(cursor[0]))
972 if (max_supported_rate &&
973 new_sampling_rate > max_supported_rate) {
974 new_sampling_rate = max_supported_rate;
978 /* If the desired rate is already active we're all set */
979 if (new_sampling_rate == cur_sampling_rate)
982 ALOGI("Sensor %d sampling rate set to %g\n", s, new_sampling_rate);
984 if (trig_sensors_per_dev[dev_num])
985 enable_buffer(dev_num, 0);
987 sysfs_write_float(sysfs_path, new_sampling_rate);
989 if (trig_sensors_per_dev[dev_num])
990 enable_buffer(dev_num, 1);
996 int allocate_control_data (void)
999 struct epoll_event ev = {0};
1001 for (i=0; i<MAX_DEVICES; i++)
1004 poll_fd = epoll_create(MAX_DEVICES);
1006 if (poll_fd == -1) {
1007 ALOGE("Can't create epoll instance for iio sensors!\n");
1015 void delete_control_data (void)