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 gyro_cal_init(&sensor_info[s]);
235 if (sensor_info[s].enable_count == 0)
236 return -1; /* Spurious disable call */
238 ALOGI("Disabling sensor %d (iio device %d: %s)\n", s, dev_num,
239 sensor_info[s].friendly_name);
241 sensor_info[s].enable_count--;
243 if (sensor_info[s].enable_count > 0)
244 return 0; /* The sensor was, and remains, in use */
246 /* Sensor disabled, lower report available flag */
247 sensor_info[s].report_pending = 0;
249 if (sensor_type == SENSOR_TYPE_MAGNETIC_FIELD)
250 compass_store_data(&sensor_info[s]);
253 /* We changed the state of a sensor - adjust per iio device counters */
255 /* If this is a regular event-driven sensor */
256 if (sensor_info[s].num_channels) {
259 trig_sensors_per_dev[dev_num]++;
261 trig_sensors_per_dev[dev_num]--;
267 active_poll_sensors++;
268 poll_sensors_per_dev[dev_num]++;
272 active_poll_sensors--;
273 poll_sensors_per_dev[dev_num]--;
278 static int get_field_count (int s)
280 int catalog_index = sensor_info[s].catalog_index;
281 int sensor_type = sensor_catalog[catalog_index].type;
283 switch (sensor_type) {
284 case SENSOR_TYPE_ACCELEROMETER: /* m/s^2 */
285 case SENSOR_TYPE_MAGNETIC_FIELD: /* micro-tesla */
286 case SENSOR_TYPE_ORIENTATION: /* degrees */
287 case SENSOR_TYPE_GYROSCOPE: /* radians/s */
290 case SENSOR_TYPE_LIGHT: /* SI lux units */
291 case SENSOR_TYPE_AMBIENT_TEMPERATURE: /* °C */
292 case SENSOR_TYPE_TEMPERATURE: /* °C */
293 case SENSOR_TYPE_PROXIMITY: /* centimeters */
294 case SENSOR_TYPE_PRESSURE: /* hecto-pascal */
295 case SENSOR_TYPE_RELATIVE_HUMIDITY: /* percent */
298 case SENSOR_TYPE_ROTATION_VECTOR:
302 ALOGE("Unknown sensor type!\n");
303 return 0; /* Drop sample */
308 static void time_add(struct timespec *out, struct timespec *in, int64_t ns)
310 int64_t target_ts = 1000000000LL * in->tv_sec + in->tv_nsec + ns;
312 out->tv_sec = target_ts / 1000000000;
313 out->tv_nsec = target_ts % 1000000000;
317 static void* acquisition_routine (void* param)
320 * Data acquisition routine run in a dedicated thread, covering a single
321 * sensor. This loop will periodically retrieve sampling data through
322 * sysfs, then package it as a sample and transfer it to our master poll
323 * loop through a report fd. Checks for a cancellation signal quite
324 * frequently, as the thread may be disposed of at any time. Note that
325 * Bionic does not provide pthread_cancel / pthread_testcancel...
331 struct sensors_event_t data = {0};
335 struct timespec entry_time;
336 struct timespec target_time;
339 ALOGV("Entering data acquisition thread for sensor %d\n", s);
341 if (s < 0 || s >= sensor_count) {
342 ALOGE("Invalid sensor handle!\n");
346 if (!sensor_info[s].sampling_rate) {
347 ALOGE("Zero rate in acquisition routine for sensor %d\n", s);
351 num_fields = get_field_count(s);
354 * Each condition variable is associated to a mutex that has to be
355 * locked by the thread that's waiting on it. We use these condition
356 * variables to get the acquisition threads out of sleep quickly after
357 * the sampling rate is adjusted, or the sensor is disabled.
359 pthread_mutex_lock(&thread_release_mutex[s]);
362 /* Pinpoint the moment we start sampling */
363 clock_gettime(CLOCK_REALTIME, &entry_time);
365 ALOGV("Acquiring sample data for sensor %d through sysfs\n", s);
367 /* Read values through sysfs */
368 for (c=0; c<num_fields; c++) {
369 data.data[c] = acquire_immediate_value(s, c);
371 /* Check and honor termination requests */
372 if (sensor_info[s].thread_data_fd[1] == -1)
375 ALOGV("\tfield %d: %f\n", c, data.data[c]);
379 /* If the sample looks good */
380 if (sensor_info[s].ops.finalize(s, &data)) {
382 /* Pipe it for transmission to poll loop */
383 ret = write( sensor_info[s].thread_data_fd[1],
385 num_fields * sizeof(float));
388 /* Check and honor termination requests */
389 if (sensor_info[s].thread_data_fd[1] == -1)
393 period = 1000000000LL / sensor_info[s].sampling_rate;
395 time_add(&target_time, &entry_time, period);
398 * Wait until the sampling time elapses, or a rate change is
399 * signaled, or a thread exit is requested.
401 ret = pthread_cond_timedwait( &thread_release_cond[s],
402 &thread_release_mutex[s],
405 /* Check and honor termination requests */
406 if (sensor_info[s].thread_data_fd[1] == -1)
411 ALOGV("Acquisition thread for S%d exiting\n", s);
412 pthread_mutex_unlock(&thread_release_mutex[s]);
418 static void start_acquisition_thread (int s)
420 int incoming_data_fd;
423 struct epoll_event ev = {0};
425 ALOGV("Initializing acquisition context for sensor %d\n", s);
427 /* Create condition variable and mutex for quick thread release */
428 ret = pthread_cond_init(&thread_release_cond[s], NULL);
429 ret = pthread_mutex_init(&thread_release_mutex[s], NULL);
431 /* Create a pipe for inter thread communication */
432 ret = pipe(sensor_info[s].thread_data_fd);
434 incoming_data_fd = sensor_info[s].thread_data_fd[0];
437 ev.data.u32 = THREAD_REPORT_TAG_BASE + s;
439 /* Add incoming side of pipe to our poll set, with a suitable tag */
440 ret = epoll_ctl(poll_fd, EPOLL_CTL_ADD, incoming_data_fd , &ev);
442 /* Create and start worker thread */
443 ret = pthread_create( &sensor_info[s].acquisition_thread,
450 static void stop_acquisition_thread (int s)
452 int incoming_data_fd = sensor_info[s].thread_data_fd[0];
453 int outgoing_data_fd = sensor_info[s].thread_data_fd[1];
455 ALOGV("Tearing down acquisition context for sensor %d\n", s);
457 /* Delete the incoming side of the pipe from our poll set */
458 epoll_ctl(poll_fd, EPOLL_CTL_DEL, incoming_data_fd, NULL);
460 /* Mark the pipe ends as invalid ; that's a cheap exit flag */
461 sensor_info[s].thread_data_fd[0] = -1;
462 sensor_info[s].thread_data_fd[1] = -1;
464 /* Close both sides of our pipe */
465 close(incoming_data_fd);
466 close(outgoing_data_fd);
468 /* Stop acquisition thread and clean up thread handle */
469 pthread_cond_signal(&thread_release_cond[s]);
470 pthread_join(sensor_info[s].acquisition_thread, NULL);
472 /* Clean up our sensor descriptor */
473 sensor_info[s].acquisition_thread = -1;
475 /* Delete condition variable and mutex */
476 pthread_cond_destroy(&thread_release_cond[s]);
477 pthread_mutex_destroy(&thread_release_mutex[s]);
481 int sensor_activate(int s, int enabled)
483 char device_name[PATH_MAX];
484 char trigger_name[MAX_NAME_SIZE + 16];
486 struct epoll_event ev = {0};
489 int dev_num = sensor_info[s].dev_num;
490 int i = sensor_info[s].catalog_index;
491 int is_poll_sensor = !sensor_info[s].num_channels;
493 ret = adjust_counters(s, enabled);
495 /* If the operation was neutral in terms of state, we're done */
499 if (!is_poll_sensor) {
502 enable_buffer(dev_num, 0);
503 setup_trigger(dev_num, "\n");
505 /* If there's at least one sensor enabled on this iio device */
506 if (trig_sensors_per_dev[dev_num]) {
507 sprintf(trigger_name, "%s-dev%d",
508 sensor_info[s].internal_name, dev_num);
511 setup_trigger(dev_num, trigger_name);
512 enable_buffer(dev_num, 1);
517 * Make sure we have a fd on the character device ; conversely, close
518 * the fd if no one is using associated sensors anymore. The assumption
519 * here is that the underlying driver will power on the relevant
520 * hardware block while someone holds a fd on the device.
522 dev_fd = device_fd[dev_num];
526 stop_acquisition_thread(s);
528 if (dev_fd != -1 && !poll_sensors_per_dev[dev_num] &&
529 !trig_sensors_per_dev[dev_num]) {
531 * Stop watching this fd. This should be a no-op
532 * in case this fd was not in the poll set.
534 epoll_ctl(poll_fd, EPOLL_CTL_DEL, dev_fd, NULL);
537 device_fd[dev_num] = -1;
543 /* First enabled sensor on this iio device */
544 sprintf(device_name, DEV_FILE_PATH, dev_num);
545 dev_fd = open(device_name, O_RDONLY | O_NONBLOCK);
547 device_fd[dev_num] = dev_fd;
550 ALOGE("Could not open fd on %s (%s)\n",
551 device_name, strerror(errno));
552 adjust_counters(s, 0);
556 ALOGV("Opened %s: fd=%d\n", device_name, dev_fd);
558 if (!is_poll_sensor) {
560 /* Add this iio device fd to the set of watched fds */
562 ev.data.u32 = dev_num;
564 ret = epoll_ctl(poll_fd, EPOLL_CTL_ADD, dev_fd, &ev);
567 ALOGE( "Failed adding %d to poll set (%s)\n",
568 dev_fd, strerror(errno));
572 /* Note: poll-mode fds are not readable */
576 /* Ensure that on-change sensors send at least one event after enable */
577 sensor_info[s].prev_val = -1;
580 start_acquisition_thread(s);
586 static int integrate_device_report(int dev_num)
590 unsigned char buf[MAX_SENSOR_REPORT_SIZE] = { 0 };
592 unsigned char *target;
593 unsigned char *source;
597 /* There's an incoming report on the specified iio device char dev fd */
599 if (dev_num < 0 || dev_num >= MAX_DEVICES) {
600 ALOGE("Event reported on unexpected iio device %d\n", dev_num);
604 if (device_fd[dev_num] == -1) {
605 ALOGE("Ignoring stale report on iio device %d\n", dev_num);
609 ts = get_timestamp();
611 len = read(device_fd[dev_num], buf, MAX_SENSOR_REPORT_SIZE);
614 ALOGE("Could not read report from iio device %d (%s)\n",
615 dev_num, strerror(errno));
619 ALOGV("Read %d bytes from iio device %d\n", len, dev_num);
621 for (s=0; s<MAX_SENSORS; s++)
622 if (sensor_info[s].dev_num == dev_num &&
623 sensor_info[s].enable_count) {
627 /* Copy data from device to sensor report buffer */
628 for (c=0; c<sensor_info[s].num_channels; c++) {
630 target = sensor_info[s].report_buffer +
633 source = buf + sensor_info[s].channel[c].offset;
635 size = sensor_info[s].channel[c].size;
637 memcpy(target, source, size);
642 ALOGV("Sensor %d report available (%d bytes)\n", s,
645 sensor_info[s].report_ts = ts;
646 sensor_info[s].report_pending = 1;
653 static int propagate_sensor_report(int s, struct sensors_event_t *data)
655 /* There's a sensor report pending for this sensor ; transmit it */
657 int catalog_index = sensor_info[s].catalog_index;
658 int sensor_type = sensor_catalog[catalog_index].type;
659 int num_fields = get_field_count(s);
661 unsigned char* current_sample;
663 /* If there's nothing to return... we're done */
667 memset(data, 0, sizeof(sensors_event_t));
669 data->version = sizeof(sensors_event_t);
671 data->type = sensor_type;
672 data->timestamp = sensor_info[s].report_ts;
674 ALOGV("Sample on sensor %d (type %d):\n", s, sensor_type);
676 current_sample = sensor_info[s].report_buffer;
678 /* If this is a poll sensor */
679 if (!sensor_info[s].num_channels) {
680 /* Use the data provided by the acquisition thread */
681 ALOGV("Reporting data from worker thread for S%d\n", s);
682 memcpy(data->data, current_sample, num_fields * sizeof(float));
686 /* Convert the data into the expected Android-level format */
687 for (c=0; c<num_fields; c++) {
689 data->data[c] = sensor_info[s].ops.transform
690 (s, c, current_sample);
692 ALOGV("\tfield %d: %f\n", c, data->data[c]);
693 current_sample += sensor_info[s].channel[c].size;
697 * The finalize routine, in addition to its late sample processing duty,
698 * has the final say on whether or not the sample gets sent to Android.
700 return sensor_info[s].ops.finalize(s, data);
704 static void integrate_thread_report (uint32_t tag)
706 int s = tag - THREAD_REPORT_TAG_BASE;
710 expected_len = get_field_count(s) * sizeof(float);
712 len = read(sensor_info[s].thread_data_fd[0],
713 sensor_info[s].report_buffer,
716 if (len == expected_len) {
717 sensor_info[s].report_ts = get_timestamp();
718 sensor_info[s].report_pending = 1;
723 int sensor_poll(struct sensors_event_t* data, int count)
728 struct epoll_event ev[MAX_DEVICES];
732 /* Get one or more events from our collection of sensors */
734 return_available_sensor_reports:
738 /* Check our sensor collection for available reports */
739 for (s=0; s<sensor_count && returned_events<count; s++)
740 if (sensor_info[s].report_pending) {
743 sensor_info[s].report_pending = 0;
745 /* Report this event if it looks OK */
747 propagate_sensor_report(s, &data[returned_events]);
750 * If the sample was deemed invalid or unreportable,
751 * e.g. had the same value as the previously reported
752 * value for a 'on change' sensor, silently drop it.
757 return returned_events;
761 ALOGV("Awaiting sensor data\n");
763 nfds = epoll_wait(poll_fd, ev, MAX_DEVICES, -1);
766 ALOGI("epoll_wait returned -1 (%s)\n", strerror(errno));
770 ALOGV("%d fds signalled\n", nfds);
772 /* For each of the signalled sources */
773 for (i=0; i<nfds; i++)
774 if (ev[i].events == EPOLLIN)
775 switch (ev[i].data.u32) {
776 case 0 ... MAX_DEVICES-1:
777 /* Read report from iio char dev fd */
778 integrate_device_report(ev[i].data.u32);
781 case THREAD_REPORT_TAG_BASE ...
782 THREAD_REPORT_TAG_BASE + MAX_SENSORS-1:
783 /* Get report from acquisition thread */
784 integrate_thread_report(ev[i].data.u32);
788 ALOGW("Unexpected event source!\n");
792 goto return_available_sensor_reports;
796 int sensor_set_delay(int s, int64_t ns)
798 /* Set the rate at which a specific sensor should report events */
800 /* See Android sensors.h for indication on sensor trigger modes */
802 char sysfs_path[PATH_MAX];
803 char avail_sysfs_path[PATH_MAX];
804 int dev_num = sensor_info[s].dev_num;
805 int i = sensor_info[s].catalog_index;
806 const char *prefix = sensor_catalog[i].tag;
807 float new_sampling_rate; /* Granted sampling rate after arbitration */
808 float cur_sampling_rate; /* Currently used sampling rate */
809 int per_sensor_sampling_rate;
810 int per_device_sampling_rate;
811 float max_supported_rate = 0;
818 ALOGE("Rejecting zero delay request on sensor %d\n", s);
822 new_sampling_rate = 1000000000LL/ns;
825 * Artificially limit ourselves to 1 Hz or higher. This is mostly to
826 * avoid setting up the stage for divisions by zero.
828 if (new_sampling_rate < 1)
829 new_sampling_rate = 1;
831 sensor_info[s].sampling_rate = new_sampling_rate;
833 /* If we're dealing with a poll-mode sensor */
834 if (!sensor_info[s].num_channels) {
835 /* Interrupt current sleep so the new sampling gets used */
836 pthread_cond_signal(&thread_release_cond[s]);
840 sprintf(sysfs_path, SENSOR_SAMPLING_PATH, dev_num, prefix);
842 if (sysfs_read_float(sysfs_path, &cur_sampling_rate) != -1) {
843 per_sensor_sampling_rate = 1;
844 per_device_sampling_rate = 0;
846 per_sensor_sampling_rate = 0;
848 sprintf(sysfs_path, DEVICE_SAMPLING_PATH, dev_num);
850 if (sysfs_read_float(sysfs_path, &cur_sampling_rate) != -1)
851 per_device_sampling_rate = 1;
853 per_device_sampling_rate = 0;
856 if (!per_sensor_sampling_rate && !per_device_sampling_rate) {
857 ALOGE("No way to adjust sampling rate on sensor %d\n", s);
861 /* Coordinate with others active sensors on the same device, if any */
862 if (per_device_sampling_rate)
863 for (n=0; n<sensor_count; n++)
864 if (n != s && sensor_info[n].dev_num == dev_num &&
865 sensor_info[n].num_channels &&
866 sensor_info[n].enable_count &&
867 sensor_info[n].sampling_rate > new_sampling_rate)
868 new_sampling_rate= sensor_info[n].sampling_rate;
870 /* Check if we have contraints on allowed sampling rates */
872 sprintf(avail_sysfs_path, DEVICE_AVAIL_FREQ_PATH, dev_num);
874 if (sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf)) > 0){
877 /* Decode allowed sampling rates string, ex: "10 20 50 100" */
879 /* While we're not at the end of the string */
880 while (*cursor && cursor[0]) {
882 /* Decode a single value */
883 sr = strtod(cursor, NULL);
885 if (sr > max_supported_rate)
886 max_supported_rate = sr;
888 /* If this matches the selected rate, we're happy */
889 if (new_sampling_rate == sr)
893 * If we reached a higher value than the desired rate,
894 * adjust selected rate so it matches the first higher
895 * available one and stop parsing - this makes the
896 * assumption that rates are sorted by increasing value
897 * in the allowed frequencies string.
899 if (sr > new_sampling_rate) {
900 new_sampling_rate = sr;
905 while (cursor[0] && !isspace(cursor[0]))
909 while (cursor[0] && isspace(cursor[0]))
915 if (max_supported_rate &&
916 new_sampling_rate > max_supported_rate) {
917 new_sampling_rate = max_supported_rate;
921 /* If the desired rate is already active we're all set */
922 if (new_sampling_rate == cur_sampling_rate)
925 ALOGI("Sensor %d sampling rate set to %g\n", s, new_sampling_rate);
927 if (trig_sensors_per_dev[dev_num])
928 enable_buffer(dev_num, 0);
930 sysfs_write_float(sysfs_path, new_sampling_rate);
932 if (trig_sensors_per_dev[dev_num])
933 enable_buffer(dev_num, 1);
939 int allocate_control_data (void)
942 struct epoll_event ev = {0};
944 for (i=0; i<MAX_DEVICES; i++)
947 poll_fd = epoll_create(MAX_DEVICES);
950 ALOGE("Can't create epoll instance for iio sensors!\n");
958 void delete_control_data (void)