OSDN Git Service

Enable proximity sensor detection
[android-x86/hardware-intel-libsensors.git] / control.c
1 /*
2  * Copyright (C) 2014 Intel Corporation.
3  */
4
5 #include <stdlib.h>
6 #include <ctype.h>
7 #include <fcntl.h>
8 #include <sys/epoll.h>
9 #include <sys/socket.h>
10 #include <utils/Log.h>
11 #include <hardware/sensors.h>
12 #include "control.h"
13 #include "enumeration.h"
14 #include "utils.h"
15 #include "transform.h"
16 #include "calibration.h"
17
18 /* Currently active sensors count, per device */
19 static int poll_sensors_per_dev[MAX_DEVICES];   /* poll-mode sensors */
20 static int trig_sensors_per_dev[MAX_DEVICES];   /* trigger, event based */
21
22 static int device_fd[MAX_DEVICES];   /* fd on the /dev/iio:deviceX file */
23
24 static int poll_fd; /* epoll instance covering all enabled sensors */
25
26 static int poll_socket_pair[2]; /* used to unblock the poll loop */
27
28 /* Timestamp for the moment when we last exited a poll operation */
29 static int64_t last_poll_exit_ts;
30
31 static int active_poll_sensors; /* Number of enabled poll-mode sensors */
32
33 #define INVALID_DEV_NUM ((uint32_t) -1)
34
35
36 static int enable_buffer(int dev_num, int enabled)
37 {
38         char sysfs_path[PATH_MAX];
39
40         sprintf(sysfs_path, ENABLE_PATH, dev_num);
41
42         /* Low level, non-multiplexed, enable/disable routine */
43         return sysfs_write_int(sysfs_path, enabled);
44 }
45
46
47 static int setup_trigger(int dev_num, const char* trigger_val)
48 {
49         char sysfs_path[PATH_MAX];
50
51         sprintf(sysfs_path, TRIGGER_PATH, dev_num);
52
53         return sysfs_write_str(sysfs_path, trigger_val);
54 }
55
56
57 void build_sensor_report_maps(int dev_num)
58 {
59         /*
60          * Read sysfs files from a iio device's scan_element directory, and
61          * build a couple of tables from that data. These tables will tell, for
62          * each sensor, where to gather relevant data in a device report, i.e.
63          * the structure that we read from the /dev/iio:deviceX file in order to
64          * sensor report, itself being the data that we return to Android when a
65          * sensor poll completes. The mapping should be straightforward in the
66          * case where we have a single sensor active per iio device but, this is
67          * not the general case. In general several sensors can be handled
68          * through a single iio device, and the _en, _index and _type syfs
69          * entries all concur to paint a picture of what the structure of the
70          * device report is.
71          */
72
73         int s;
74         int c;
75         int n;
76         int i;
77         int ch_index;
78         char* ch_spec;
79         char spec_buf[MAX_TYPE_SPEC_LEN];
80         struct datum_info_t* ch_info;
81         int size;
82         char sysfs_path[PATH_MAX];
83         int known_channels;
84         int offset;
85         int channel_size_from_index[MAX_SENSORS * MAX_CHANNELS] = { 0 };
86         int sensor_handle_from_index[MAX_SENSORS * MAX_CHANNELS] = { 0 };
87         int channel_number_from_index[MAX_SENSORS * MAX_CHANNELS] = { 0 };
88
89         known_channels = 0;
90
91         /* For each sensor that is linked to this device */
92         for (s=0; s<sensor_count; s++) {
93                 if (sensor_info[s].dev_num != dev_num)
94                         continue;
95
96                 i = sensor_info[s].catalog_index;
97
98                 /* Read channel details through sysfs attributes */
99                 for (c=0; c<sensor_info[s].num_channels; c++) {
100
101                         /* Read _type file */
102                         sprintf(sysfs_path, CHANNEL_PATH "%s",
103                                 sensor_info[s].dev_num,
104                                 sensor_catalog[i].channel[c].type_path);
105
106                         n = sysfs_read_str(sysfs_path, spec_buf, 
107                                                 sizeof(spec_buf));
108
109                         if (n == -1) {
110                                         ALOGW(  "Failed to read type: %s\n",
111                                         sysfs_path);
112                                         continue;
113                                 }
114
115                         ch_spec = sensor_info[s].channel[c].type_spec;
116
117                         memcpy(ch_spec, spec_buf, sizeof(spec_buf));
118
119                         ch_info = &sensor_info[s].channel[c].type_info;
120
121                         size = decode_type_spec(ch_spec, ch_info);
122
123                         /* Read _index file */
124                         sprintf(sysfs_path, CHANNEL_PATH "%s",
125                                 sensor_info[s].dev_num,
126                                 sensor_catalog[i].channel[c].index_path);
127
128                         n = sysfs_read_int(sysfs_path, &ch_index);
129
130                         if (n == -1) {
131                                         ALOGW(  "Failed to read index: %s\n",
132                                                 sysfs_path);
133                                         continue;
134                                 }
135
136                         if (ch_index >= MAX_SENSORS) {
137                                 ALOGE("Index out of bounds!: %s\n", sysfs_path);
138                                 continue;
139                         }
140
141                         /* Record what this index is about */
142
143                         sensor_handle_from_index [ch_index] = s;
144                         channel_number_from_index[ch_index] = c;
145                         channel_size_from_index  [ch_index] = size;
146
147                         known_channels++;
148                 }
149
150                 /* Stop sampling - if we are recovering from hal restart */
151                 enable_buffer(dev_num, 0);
152                 setup_trigger(dev_num, "\n");
153
154                 /* Turn on channels we're aware of */
155                 for (c=0;c<sensor_info[s].num_channels; c++) {
156                         sprintf(sysfs_path, CHANNEL_PATH "%s",
157                                 sensor_info[s].dev_num,
158                                 sensor_catalog[i].channel[c].en_path);
159                         sysfs_write_int(sysfs_path, 1);
160                 }
161         }
162
163         ALOGI("Found %d channels on iio device %d\n", known_channels, dev_num);
164
165         /*
166          * Now that we know which channels are defined, their sizes and their
167          * ordering, update channels offsets within device report. Note: there
168          * is a possibility that several sensors share the same index, with
169          * their data fields being isolated by masking and shifting as specified
170          * through the real bits and shift values in type attributes. This case
171          * is not currently supported. Also, the code below assumes no hole in
172          * the sequence of indices, so it is dependent on discovery of all
173          * sensors.
174          */
175          offset = 0;
176          for (i=0; i<MAX_SENSORS * MAX_CHANNELS; i++) {
177                 s =     sensor_handle_from_index[i];
178                 c =     channel_number_from_index[i];
179                 size =  channel_size_from_index[i];
180
181                 if (!size)
182                         continue;
183
184                 ALOGI("S%d C%d : offset %d, size %d, type %s\n",
185                       s, c, offset, size, sensor_info[s].channel[c].type_spec);
186
187                 sensor_info[s].channel[c].offset        = offset;
188                 sensor_info[s].channel[c].size          = size;
189
190                 offset += size;
191          }
192 }
193
194
195 int adjust_counters (int s, int enabled)
196 {
197         /*
198          * Adjust counters based on sensor enable action. Return values are:
199          * -1 if there's an inconsistency: abort action in this case
200          *  0 if the operation was completed and we're all set
201          *  1 if we toggled the state of the sensor and there's work left
202          */
203
204         int dev_num = sensor_info[s].dev_num;
205         int catalog_index = sensor_info[s].catalog_index;
206         int sensor_type = sensor_catalog[catalog_index].type;
207
208         /* Refcount per sensor, in terms of enable count */
209         if (enabled) {
210                 ALOGI("Enabling sensor %d (iio device %d: %s)\n",
211                         s, dev_num, sensor_info[s].friendly_name);
212
213                 sensor_info[s].enable_count++;
214
215                 if (sensor_info[s].enable_count != 1) {
216                         return 0; /* The sensor was, and remains, in use */
217                 } else {
218                         if (sensor_type == SENSOR_TYPE_MAGNETIC_FIELD)
219                                 compass_read_data(&sensor_info[s]);
220
221                         if (sensor_type == SENSOR_TYPE_GYROSCOPE)
222                                 gyro_cal_init(&sensor_info[s]);
223                 }
224         } else {
225                 if (sensor_info[s].enable_count == 0)
226                         return -1; /* Spurious disable call */
227
228                 ALOGI("Disabling sensor %d (iio device %d: %s)\n", s, dev_num,
229                       sensor_info[s].friendly_name);
230
231                 if (sensor_type == SENSOR_TYPE_MAGNETIC_FIELD)
232                         compass_store_data(&sensor_info[s]);
233
234                 sensor_info[s].enable_count--;
235
236                 if (sensor_info[s].enable_count > 0)
237                         return 0; /* The sensor was, and remains, in use */
238
239                 /* Sensor disabled, lower report available flag */
240                 sensor_info[s].report_pending = 0;
241         }
242
243         /* We changed the state of a sensor - adjust per iio device counters */
244
245         /* If this is a regular event-driven sensor */
246         if (sensor_info[s].num_channels) {
247
248                         if (enabled)
249                                 trig_sensors_per_dev[dev_num]++;
250                         else
251                                 trig_sensors_per_dev[dev_num]--;
252
253                         return 1;
254                 }
255
256         if (enabled) {
257                 active_poll_sensors++;
258                 poll_sensors_per_dev[dev_num]++;
259                 return 1;
260         }
261
262         active_poll_sensors--;
263         poll_sensors_per_dev[dev_num]--;
264         return 1;
265 }
266
267
268 int sensor_activate(int s, int enabled)
269 {
270         char device_name[PATH_MAX];
271         char trigger_name[MAX_NAME_SIZE + 16];
272         int c;
273         struct epoll_event ev = {0};
274         int dev_fd;
275         int ret;
276         int dev_num = sensor_info[s].dev_num;
277         int i = sensor_info[s].catalog_index;
278         int is_poll_sensor = !sensor_info[s].num_channels;
279
280         ret = adjust_counters(s, enabled);
281
282         /* If the operation was neutral in terms of state, we're done */
283         if (ret <= 0)
284                 return ret;
285
286         if (!is_poll_sensor) {
287
288                 /* Stop sampling */
289                 enable_buffer(dev_num, 0);
290                 setup_trigger(dev_num, "\n");
291
292                 /* If there's at least one sensor enabled on this iio device */
293                 if (trig_sensors_per_dev[dev_num]) {
294                         sprintf(trigger_name, "%s-dev%d",
295                                         sensor_info[s].internal_name, dev_num);
296
297                         /* Start sampling */
298                         setup_trigger(dev_num, trigger_name);
299                         enable_buffer(dev_num, 1);
300                 }
301         }
302
303         /*
304          * Make sure we have a fd on the character device ; conversely, close
305          * the fd if no one is using associated sensors anymore. The assumption
306          * here is that the underlying driver will power on the relevant
307          * hardware block while someone holds a fd on the device.
308          */
309         dev_fd = device_fd[dev_num];
310
311         if (!enabled) {
312                 if (dev_fd != -1 && !poll_sensors_per_dev[dev_num] &&
313                         !trig_sensors_per_dev[dev_num]) {
314                                 /*
315                                  * Stop watching this fd. This should be a no-op
316                                  * in case this fd was not in the poll set.
317                                  */
318                                 epoll_ctl(poll_fd, EPOLL_CTL_DEL, dev_fd, NULL);
319
320                                 close(dev_fd);
321                                 device_fd[dev_num] = -1;
322                         }
323                 return 0;
324         }
325
326         if (dev_fd == -1) {
327                 /* First enabled sensor on this iio device */
328                 sprintf(device_name, DEV_FILE_PATH, dev_num);
329                 dev_fd = open(device_name, O_RDONLY | O_NONBLOCK);
330
331                 device_fd[dev_num] = dev_fd;
332
333                 if (dev_fd == -1) {
334                         ALOGE("Could not open fd on %s (%s)\n",
335                               device_name, strerror(errno));
336                         adjust_counters(s, 0);
337                         return -1;
338                 }
339
340                 ALOGV("Opened %s: fd=%d\n", device_name, dev_fd);
341
342                 if (!is_poll_sensor) {
343
344                         /* Add this iio device fd to the set of watched fds */
345                         ev.events = EPOLLIN;
346                         ev.data.u32 = dev_num;
347
348                         ret = epoll_ctl(poll_fd, EPOLL_CTL_ADD, dev_fd, &ev);
349
350                         if (ret == -1) {
351                                 ALOGE(  "Failed adding %d to poll set (%s)\n",
352                                         dev_fd, strerror(errno));
353                                 return -1;
354                         }
355
356                         /* Note: poll-mode fds are not readable */
357                 }
358         }
359
360         /* Release the polling loop so an updated timeout gets used */
361         write(poll_socket_pair[1], "", 1);
362
363         return 0;
364 }
365
366
367 static int integrate_device_report(int dev_num)
368 {
369         int len;
370         int s,c;
371         unsigned char buf[MAX_SENSOR_REPORT_SIZE] = { 0 };
372         int sr_offset;
373         unsigned char *target;
374         unsigned char *source;
375         int size;
376         int ts;
377
378         /* There's an incoming report on the specified fd */
379
380         if (dev_num < 0 || dev_num >= MAX_DEVICES) {
381                 ALOGE("Event reported on unexpected iio device %d\n", dev_num);
382                 return -1;
383         }
384
385         if (device_fd[dev_num] == -1) {
386                 ALOGE("Ignoring stale report on iio device %d\n", dev_num);
387                 return -1;
388         }
389
390         ts = get_timestamp();
391
392         len = read(device_fd[dev_num], buf, MAX_SENSOR_REPORT_SIZE);
393
394         if (len == -1) {
395                 ALOGE("Could not read report from iio device %d (%s)\n",
396                       dev_num, strerror(errno));
397                 return -1;
398         }
399
400         ALOGV("Read %d bytes from iio device %d\n", len, dev_num);
401
402         for (s=0; s<MAX_SENSORS; s++)
403                 if (sensor_info[s].dev_num == dev_num &&
404                     sensor_info[s].enable_count) {
405
406                         sr_offset = 0;
407
408                         /* Copy data from device to sensor report buffer */
409                         for (c=0; c<sensor_info[s].num_channels; c++) {
410
411                                 target = sensor_info[s].report_buffer +
412                                         sr_offset;
413
414                                 source = buf + sensor_info[s].channel[c].offset;
415
416                                 size = sensor_info[s].channel[c].size;
417
418                                 memcpy(target, source, size);
419
420                                 sr_offset += size;
421                         }
422
423                         ALOGV("Sensor %d report available (%d bytes)\n", s,
424                               sr_offset);
425
426                         sensor_info[s].report_ts = ts;
427                         sensor_info[s].report_pending = 1;
428                 }
429
430         return 0;
431 }
432
433
434 static int propagate_sensor_report(int s, struct sensors_event_t* data)
435 {
436         /* There's a sensor report pending for this sensor ; transmit it */
437
438         int catalog_index = sensor_info[s].catalog_index;
439         int sensor_type = sensor_catalog[catalog_index].type;
440         int num_fields;
441         int c;
442         unsigned char* current_sample;
443         int64_t current_ts = get_timestamp();
444         int64_t delta;
445
446         memset(data, 0, sizeof(sensors_event_t));
447
448         data->version = sizeof(sensors_event_t);
449         data->sensor = s;
450         data->type = sensor_type;
451
452         if (sensor_info[s].report_ts)
453                 data->timestamp = sensor_info[s].report_ts;
454         else
455                 data->timestamp = current_ts;
456
457         switch (sensor_type) {
458                 case SENSOR_TYPE_ACCELEROMETER:         /* m/s^2        */
459                 case SENSOR_TYPE_MAGNETIC_FIELD:        /* micro-tesla  */
460                 case SENSOR_TYPE_ORIENTATION:           /* degrees      */
461                 case SENSOR_TYPE_GYROSCOPE:             /* radians/s    */
462                         num_fields = 3;
463                         break;
464
465                 case SENSOR_TYPE_LIGHT:                 /* SI lux units */
466                 case SENSOR_TYPE_AMBIENT_TEMPERATURE:   /* Â°C          */
467                 case SENSOR_TYPE_TEMPERATURE:           /* Â°C          */
468                 case SENSOR_TYPE_PROXIMITY:             /* centimeters  */
469                 case SENSOR_TYPE_PRESSURE:              /* hecto-pascal */
470                 case SENSOR_TYPE_RELATIVE_HUMIDITY:     /* percent */
471                         num_fields = 1;
472                         break;
473
474                 case SENSOR_TYPE_ROTATION_VECTOR:
475                         num_fields = 4;
476                         break;
477
478                 case SENSOR_TYPE_DEVICE_PRIVATE_BASE:   /* Hidden for now */
479                         return 0;                       /* Drop sample */
480
481                 default:
482                         ALOGE("Unknown sensor type!\n");
483                         return 0;                       /* Drop sample */
484         }
485
486         ALOGV("Sample on sensor %d (type %d):\n", s, sensor_type);
487
488         /* Take note of current time counter value for rate control purposes */
489         sensor_info[s].last_integration_ts = current_ts;
490
491         /* If we're dealing with a poll-mode sensor */
492         if (!sensor_info[s].num_channels) {
493
494                 /* Read values through sysfs rather than from a report buffer */
495                 for (c=0; c<num_fields; c++) {
496
497                         data->data[c] = acquire_immediate_value(s, c);
498
499                         ALOGV("\tfield %d: %f\n", c, data->data[c]);
500                 }
501
502                 /* Control acquisition time and flag anything beyond 100 ms */
503                 delta = get_timestamp() - current_ts;
504
505                 if (delta > 100 * 1000 * 1000) {
506                         ALOGI("Sensor %d (%s) sampling time: %d ms\n", s,
507                         sensor_info[s].friendly_name, (int) (delta / 1000000));
508                 }
509
510                 return sensor_info[s].ops.finalize(s, data);
511         }
512
513         /* Convert the data into the expected Android-level format */
514
515         current_sample = sensor_info[s].report_buffer;
516
517         for (c=0; c<num_fields; c++) {
518
519                 data->data[c] = sensor_info[s].ops.transform
520                                                         (s, c, current_sample);
521
522                 ALOGV("\tfield %d: %f\n", c, data->data[c]);
523                 current_sample += sensor_info[s].channel[c].size;
524         }
525
526         /*
527          * The finalize routine, in addition to its late sample processing duty,
528          * has the final say on whether or not the sample gets sent to Android.
529          */
530         return sensor_info[s].ops.finalize(s, data);
531 }
532
533
534 static int get_poll_time (void)
535 {
536         int64_t target_ts;
537         int64_t lowest_target_ts;
538         int64_t current_ts;
539         int s;
540
541         if (!active_poll_sensors)
542                 return -1;      /* Infinite wait */
543
544         /* Check if we should schedule a poll-mode sensor event delivery */
545
546         lowest_target_ts = INT64_MAX;
547
548         for (s=0; s<sensor_count; s++)
549                 if (sensor_info[s].enable_count &&
550                     sensor_info[s].sampling_rate > 0 &&
551                     !sensor_info[s].num_channels) {
552                                 target_ts = sensor_info[s].last_integration_ts +
553                                       1000000000LL/sensor_info[s].sampling_rate;
554
555                                 if (target_ts < lowest_target_ts)
556                                         lowest_target_ts = target_ts;
557                         }
558
559         if (lowest_target_ts == INT64_MAX)
560                 return -1;
561
562         current_ts = get_timestamp();
563
564         if (lowest_target_ts <= current_ts)
565                 return 0;
566
567         return (lowest_target_ts - current_ts)/1000000; /* ms */
568 }
569
570
571 static void acknowledge_release (void)
572 {
573         /* A write to our socket circuit was performed to release epoll */
574         char buf;
575         read(poll_socket_pair[0], &buf, 1);
576 }
577
578
579 int sensor_poll(struct sensors_event_t* data, int count)
580 {
581         int s;
582         int i;
583         int nfds;
584         int delta;
585         struct epoll_event ev[MAX_DEVICES];
586         int64_t target_ts;
587
588         /* Get one or more events from our collection of sensors */
589
590 return_first_available_sensor_report:
591
592         /* If there's at least one available report */
593         for (s=0; s<sensor_count; s++)
594                 if (sensor_info[s].report_pending) {
595
596                         /* Lower flag */
597                         sensor_info[s].report_pending = 0;
598
599                         if (propagate_sensor_report(s, data)) {
600                                 /* Return that up */
601                                 ALOGV("Report on sensor %d\n", s);
602                                 return 1;
603                         }
604
605                         /*
606                          * If the sample was deemed invalid or unreportable,
607                          * e.g. had the same value as the previously reported
608                          * value for a 'on change' sensor, silently drop it
609                          */
610                 }
611 await_event:
612
613         ALOGV("Awaiting sensor data\n");
614
615         nfds = epoll_wait(poll_fd, ev, MAX_DEVICES, get_poll_time());
616
617         last_poll_exit_ts = get_timestamp();
618
619         if (nfds == -1) {
620                 ALOGI("epoll_wait returned -1 (%s)\n", strerror(errno));
621                 goto await_event;
622         }
623
624         ALOGV("%d fds signalled\n", nfds);
625
626         /* For each of the devices for which a report is available */
627         for (i=0; i<nfds; i++)
628                 if (ev[i].events == EPOLLIN) {
629                         if (ev[i].data.u32 == INVALID_DEV_NUM) {
630                                 acknowledge_release();
631                                 goto await_event;
632                         } else
633                                 /* Read report */
634                                 integrate_device_report(ev[i].data.u32);
635                 }
636
637         /* Check poll-mode sensors and fire up an event if it's time to do so */
638         if (active_poll_sensors)
639                 for (s=0; s<sensor_count; s++)
640                         if (sensor_info[s].enable_count &&
641                             !sensor_info[s].num_channels &&
642                             sensor_info[s].sampling_rate > 0) {
643                                 target_ts = sensor_info[s].last_integration_ts +
644                                       1000000000LL/sensor_info[s].sampling_rate;
645
646                                 if (last_poll_exit_ts >= target_ts)
647                                         sensor_info[s].report_pending = 1;
648                         }
649
650         goto return_first_available_sensor_report;
651 }
652
653
654 int sensor_set_delay(int s, int64_t ns)
655 {
656         /* Set the rate at which a specific sensor should report events */
657
658         /* See Android sensors.h for indication on sensor trigger modes */
659
660         char sysfs_path[PATH_MAX];
661         char avail_sysfs_path[PATH_MAX];
662         int dev_num             =       sensor_info[s].dev_num;
663         int i                   =       sensor_info[s].catalog_index;
664         const char *prefix      =       sensor_catalog[i].tag;
665         float new_sampling_rate; /* Granted sampling rate after arbitration   */
666         float cur_sampling_rate; /* Currently used sampling rate              */
667         float req_sampling_rate; /* Requested ; may be different from granted */
668         int per_sensor_sampling_rate;
669         int per_device_sampling_rate;
670         int max_supported_rate = 0;
671         char freqs_buf[100];
672         char* cursor;
673         int n;
674         int sr;
675
676         if (!ns) {
677                 ALOGE("Rejecting zero delay request on sensor %d\n", s);
678                 return -EINVAL;
679         }
680
681         new_sampling_rate = req_sampling_rate = 1000000000L/ns;
682
683         if (new_sampling_rate < 1) {
684                 ALOGI("Sub-HZ sampling rate requested on on sensor %d\n", s);
685                 new_sampling_rate = 1;
686         }
687
688         sensor_info[s].sampling_rate = new_sampling_rate;
689
690         /* If we're dealing with a poll-mode sensor, release poll and return */
691         if (!sensor_info[s].num_channels)
692                 goto exit;
693
694         sprintf(sysfs_path, SENSOR_SAMPLING_PATH, dev_num, prefix);
695
696         if (sysfs_read_float(sysfs_path, &cur_sampling_rate) != -1) {
697                 per_sensor_sampling_rate = 1;
698                 per_device_sampling_rate = 0;
699         } else {
700                 per_sensor_sampling_rate = 0;
701
702                 sprintf(sysfs_path, DEVICE_SAMPLING_PATH, dev_num);
703
704                 if (sysfs_read_float(sysfs_path, &cur_sampling_rate) != -1)
705                         per_device_sampling_rate = 1;
706                 else
707                         per_device_sampling_rate = 0;
708         }
709
710         if (!per_sensor_sampling_rate && !per_device_sampling_rate) {
711                 ALOGE("No way to adjust sampling rate on sensor %d\n", s);
712                 return -ENOSYS;
713         }
714
715         /* Coordinate with others active sensors on the same device, if any */
716         if (per_device_sampling_rate)
717                 for (n=0; n<sensor_count; n++)
718                         if (n != s && sensor_info[n].dev_num == dev_num &&
719                             sensor_info[n].num_channels &&
720                             sensor_info[n].enable_count &&
721                             sensor_info[n].sampling_rate > new_sampling_rate)
722                                 new_sampling_rate= sensor_info[n].sampling_rate;
723
724         /* Check if we have contraints on allowed sampling rates */
725
726         sprintf(avail_sysfs_path, DEVICE_AVAIL_FREQ_PATH, dev_num);
727
728         if (sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf)) > 0){
729                 cursor = freqs_buf;
730
731                 /* Decode allowed sampling rates string, ex: "10 20 50 100" */
732
733                 /* While we're not at the end of the string */
734                 while (*cursor && cursor[0]) {
735
736                         /* Decode a single value */
737                         sr = strtod(cursor, NULL);
738
739                         /* Cap sampling rate to CAP_SENSOR_MAX_FREQUENCY*/
740                         if (sr > CAP_SENSOR_MAX_FREQUENCY)
741                                  break;
742
743                         if (sr > max_supported_rate)
744                                 max_supported_rate = sr;
745
746                         /* If this matches the selected rate, we're happy */
747                         if (new_sampling_rate == sr)
748                                 break;
749
750                         /*
751                          * If we reached a higher value than the desired rate,
752                          * adjust selected rate so it matches the first higher
753                          * available one and stop parsing - this makes the
754                          * assumption that rates are sorted by increasing value
755                          * in the allowed frequencies string.
756                          */
757                         if (sr > new_sampling_rate) {
758                                 ALOGI(
759                         "Increasing sampling rate on sensor %d from %g to %g\n",
760                                 s, (double) req_sampling_rate, (double) sr);
761
762                                 new_sampling_rate = sr;
763                                 break;
764                         }
765
766                         /* Skip digits */
767                         while (cursor[0] && !isspace(cursor[0]))
768                                 cursor++;
769
770                         /* Skip spaces */
771                         while (cursor[0] && isspace(cursor[0]))
772                                         cursor++;
773                 }
774         }
775
776
777         if (max_supported_rate &&
778                 new_sampling_rate > max_supported_rate) {
779                 new_sampling_rate = max_supported_rate;
780                 ALOGI(  "Can't support %g sampling rate, lowering to %g\n",
781                         (double) req_sampling_rate, (double) new_sampling_rate);
782         }
783
784
785         /* If the desired rate is already active we're all set */
786         if (new_sampling_rate == cur_sampling_rate)
787                 return 0;
788
789         ALOGI("Sensor %d sampling rate switched to %g\n", s, new_sampling_rate);
790
791         if (trig_sensors_per_dev[dev_num])
792                 enable_buffer(dev_num, 0);
793
794         sysfs_write_float(sysfs_path, new_sampling_rate);
795
796         if (trig_sensors_per_dev[dev_num])
797                 enable_buffer(dev_num, 1);
798
799 exit:
800         /* Release the polling loop so an updated timeout value gets used */
801         write(poll_socket_pair[1], "", 1);
802
803         return 0;
804 }
805
806
807
808 int allocate_control_data (void)
809 {
810         int i;
811         struct epoll_event ev = {0};
812
813         for (i=0; i<MAX_DEVICES; i++)
814                 device_fd[i] = -1;
815
816         poll_fd = epoll_create(MAX_DEVICES);
817
818         if (poll_fd == -1) {
819                 ALOGE("Can't create epoll instance for iio sensors!\n");
820                 return -1;
821         }
822
823         /* Create and add "unblocking" fd to the set of watched fds */
824
825         if (socketpair(AF_UNIX, SOCK_STREAM, 0, poll_socket_pair) == -1) {
826                 ALOGE("Can't create socket pair for iio sensors!\n");
827                 close(poll_fd);
828                 return -1;
829         }
830
831         ev.events = EPOLLIN;
832         ev.data.u32 = INVALID_DEV_NUM;
833
834         epoll_ctl(poll_fd, EPOLL_CTL_ADD, poll_socket_pair[0], &ev);
835
836         return poll_fd;
837 }
838
839
840 void delete_control_data (void)
841 {
842 }