OSDN Git Service

Magnetometer ts improvements
[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 <pthread.h>
9 #include <time.h>
10 #include <sys/epoll.h>
11 #include <sys/socket.h>
12 #include <utils/Log.h>
13 #include <hardware/sensors.h>
14 #include "control.h"
15 #include "enumeration.h"
16 #include "utils.h"
17 #include "transform.h"
18 #include "calibration.h"
19 #include "description.h"
20 #include "filtering.h"
21
22 /* Currently active sensors count, per device */
23 static int poll_sensors_per_dev[MAX_DEVICES];   /* poll-mode sensors */
24 static int trig_sensors_per_dev[MAX_DEVICES];   /* trigger, event based */
25
26 static int device_fd[MAX_DEVICES];   /* fd on the /dev/iio:deviceX file */
27
28 static int poll_fd; /* epoll instance covering all enabled sensors */
29
30 static int active_poll_sensors; /* Number of enabled poll-mode sensors */
31
32 int64_t ts_delta; /* delta between SystemClock.getNanos and our timestamp */
33
34 /* We use pthread condition variables to get worker threads out of sleep */
35 static pthread_condattr_t thread_cond_attr      [MAX_SENSORS];
36 static pthread_cond_t     thread_release_cond   [MAX_SENSORS];
37 static pthread_mutex_t    thread_release_mutex  [MAX_SENSORS];
38
39 /*
40  * We associate tags to each of our poll set entries. These tags have the
41  * following values:
42  * - a iio device number if the fd is a iio character device fd
43  * - THREAD_REPORT_TAG_BASE + sensor handle if the fd is the receiving end of a
44  *   pipe used by a sysfs data acquisition thread
45  *  */
46 #define THREAD_REPORT_TAG_BASE  0x00010000
47
48 #define ENABLE_BUFFER_RETRIES 10
49 #define ENABLE_BUFFER_RETRY_DELAY_MS 10
50
51 static int enable_buffer(int dev_num, int enabled)
52 {
53         char sysfs_path[PATH_MAX];
54         int ret, retries, millisec;
55         struct timespec req = {0};
56
57         retries = ENABLE_BUFFER_RETRIES;
58         millisec = ENABLE_BUFFER_RETRY_DELAY_MS;
59         req.tv_sec = 0;
60         req.tv_nsec = millisec * 1000000L;
61
62         sprintf(sysfs_path, ENABLE_PATH, dev_num);
63
64         while (retries--) {
65                 /* Low level, non-multiplexed, enable/disable routine */
66                 ret = sysfs_write_int(sysfs_path, enabled);
67                 if (ret > 0)
68                         break;
69
70                 ALOGE("Failed enabling buffer, retrying");
71                 nanosleep(&req, (struct timespec *)NULL);
72         }
73
74         if (ret < 0) {
75                 ALOGE("Could not enable buffer\n");
76                 return -EIO;
77         }
78
79         return 0;
80 }
81
82
83 static int setup_trigger (int s, const char* trigger_val)
84 {
85         char sysfs_path[PATH_MAX];
86         int ret = -1, attempts = 5;
87
88         sprintf(sysfs_path, TRIGGER_PATH, sensor_info[s].dev_num);
89
90         if (trigger_val[0] != '\n')
91                 ALOGI("Setting S%d (%s) trigger to %s\n", s,
92                         sensor_info[s].friendly_name, trigger_val);
93
94         while (ret == -1 && attempts) {
95                 ret = sysfs_write_str(sysfs_path, trigger_val);
96                 attempts--;
97         }
98
99         if (ret != -1)
100                 sensor_info[s].selected_trigger = trigger_val;
101         else
102                 ALOGE("Setting S%d (%s) trigger to %s FAILED.\n", s,
103                         sensor_info[s].friendly_name, trigger_val);
104         return ret;
105 }
106
107
108 void build_sensor_report_maps(int dev_num)
109 {
110         /*
111          * Read sysfs files from a iio device's scan_element directory, and
112          * build a couple of tables from that data. These tables will tell, for
113          * each sensor, where to gather relevant data in a device report, i.e.
114          * the structure that we read from the /dev/iio:deviceX file in order to
115          * sensor report, itself being the data that we return to Android when a
116          * sensor poll completes. The mapping should be straightforward in the
117          * case where we have a single sensor active per iio device but, this is
118          * not the general case. In general several sensors can be handled
119          * through a single iio device, and the _en, _index and _type syfs
120          * entries all concur to paint a picture of what the structure of the
121          * device report is.
122          */
123
124         int s;
125         int c;
126         int n;
127         int i;
128         int ch_index;
129         char* ch_spec;
130         char spec_buf[MAX_TYPE_SPEC_LEN];
131         struct datum_info_t* ch_info;
132         int size;
133         char sysfs_path[PATH_MAX];
134         int known_channels;
135         int offset;
136         int channel_size_from_index[MAX_SENSORS * MAX_CHANNELS] = { 0 };
137         int sensor_handle_from_index[MAX_SENSORS * MAX_CHANNELS] = { 0 };
138         int channel_number_from_index[MAX_SENSORS * MAX_CHANNELS] = { 0 };
139
140         known_channels = 0;
141
142         /* For each sensor that is linked to this device */
143         for (s=0; s<sensor_count; s++) {
144                 if (sensor_info[s].dev_num != dev_num)
145                         continue;
146
147                 i = sensor_info[s].catalog_index;
148
149                 /* Read channel details through sysfs attributes */
150                 for (c=0; c<sensor_info[s].num_channels; c++) {
151
152                         /* Read _type file */
153                         sprintf(sysfs_path, CHANNEL_PATH "%s",
154                                 sensor_info[s].dev_num,
155                                 sensor_catalog[i].channel[c].type_path);
156
157                         n = sysfs_read_str(sysfs_path, spec_buf, 
158                                                 sizeof(spec_buf));
159
160                         if (n == -1) {
161                                         ALOGW(  "Failed to read type: %s\n",
162                                         sysfs_path);
163                                         continue;
164                                 }
165
166                         ch_spec = sensor_info[s].channel[c].type_spec;
167
168                         memcpy(ch_spec, spec_buf, sizeof(spec_buf));
169
170                         ch_info = &sensor_info[s].channel[c].type_info;
171
172                         size = decode_type_spec(ch_spec, ch_info);
173
174                         /* Read _index file */
175                         sprintf(sysfs_path, CHANNEL_PATH "%s",
176                                 sensor_info[s].dev_num,
177                                 sensor_catalog[i].channel[c].index_path);
178
179                         n = sysfs_read_int(sysfs_path, &ch_index);
180
181                         if (n == -1) {
182                                         ALOGW(  "Failed to read index: %s\n",
183                                                 sysfs_path);
184                                         continue;
185                                 }
186
187                         if (ch_index >= MAX_SENSORS) {
188                                 ALOGE("Index out of bounds!: %s\n", sysfs_path);
189                                 continue;
190                         }
191
192                         /* Record what this index is about */
193
194                         sensor_handle_from_index [ch_index] = s;
195                         channel_number_from_index[ch_index] = c;
196                         channel_size_from_index  [ch_index] = size;
197
198                         known_channels++;
199                 }
200
201                 /* Stop sampling - if we are recovering from hal restart */
202                 enable_buffer(dev_num, 0);
203                 setup_trigger(s, "\n");
204
205                 /* Turn on channels we're aware of */
206                 for (c=0;c<sensor_info[s].num_channels; c++) {
207                         sprintf(sysfs_path, CHANNEL_PATH "%s",
208                                 sensor_info[s].dev_num,
209                                 sensor_catalog[i].channel[c].en_path);
210                         sysfs_write_int(sysfs_path, 1);
211                 }
212         }
213
214         ALOGI("Found %d channels on iio device %d\n", known_channels, dev_num);
215
216         /*
217          * Now that we know which channels are defined, their sizes and their
218          * ordering, update channels offsets within device report. Note: there
219          * is a possibility that several sensors share the same index, with
220          * their data fields being isolated by masking and shifting as specified
221          * through the real bits and shift values in type attributes. This case
222          * is not currently supported. Also, the code below assumes no hole in
223          * the sequence of indices, so it is dependent on discovery of all
224          * sensors.
225          */
226          offset = 0;
227          for (i=0; i<MAX_SENSORS * MAX_CHANNELS; i++) {
228                 s =     sensor_handle_from_index[i];
229                 c =     channel_number_from_index[i];
230                 size =  channel_size_from_index[i];
231
232                 if (!size)
233                         continue;
234
235                 ALOGI("S%d C%d : offset %d, size %d, type %s\n",
236                       s, c, offset, size, sensor_info[s].channel[c].type_spec);
237
238                 sensor_info[s].channel[c].offset        = offset;
239                 sensor_info[s].channel[c].size          = size;
240
241                 offset += size;
242          }
243 }
244
245
246 int adjust_counters (int s, int enabled)
247 {
248         /*
249          * Adjust counters based on sensor enable action. Return values are:
250          * -1 if there's an inconsistency: abort action in this case
251          *  0 if the operation was completed and we're all set
252          *  1 if we toggled the state of the sensor and there's work left
253          */
254
255         int dev_num = sensor_info[s].dev_num;
256
257         /* Refcount per sensor, in terms of enable count */
258         if (enabled) {
259                 ALOGI("Enabling sensor %d (iio device %d: %s)\n",
260                         s, dev_num, sensor_info[s].friendly_name);
261
262                 if (sensor_info[s].enabled)
263                         return 0; /* The sensor was, and remains, in use */
264
265                 sensor_info[s].enabled = 1;
266
267                 switch (sensor_info[s].type) {
268                         case SENSOR_TYPE_MAGNETIC_FIELD:
269                                 compass_read_data(&sensor_info[s]);
270                                 break;
271
272                         case SENSOR_TYPE_GYROSCOPE:
273                         case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
274                                 gyro_cal_init(&sensor_info[s]);
275                                 break;
276                 }
277         } else {
278                 if (sensor_info[s].enabled == 0)
279                         return 0; /* Spurious disable call */
280
281                 ALOGI("Disabling sensor %d (iio device %d: %s)\n", s, dev_num,
282                       sensor_info[s].friendly_name);
283
284                 sensor_info[s].enabled = 0;
285
286                 /* Sensor disabled, lower report available flag */
287                 sensor_info[s].report_pending = 0;
288
289                 if (sensor_info[s].type == SENSOR_TYPE_MAGNETIC_FIELD)
290                         compass_store_data(&sensor_info[s]);
291
292                 if(sensor_info[s].type == SENSOR_TYPE_GYROSCOPE ||
293                         sensor_info[s].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED)
294                         gyro_store_data(&sensor_info[s]);
295         }
296
297
298         /* If uncalibrated type and pair is already active don't adjust counters */
299         if (sensor_info[s].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED &&
300                 sensor_info[sensor_info[s].pair_idx].enabled != 0)
301                         return 0;
302
303         /* We changed the state of a sensor - adjust per iio device counters */
304
305         /* If this is a regular event-driven sensor */
306         if (sensor_info[s].num_channels) {
307
308                         if (enabled)
309                                 trig_sensors_per_dev[dev_num]++;
310                         else
311                                 trig_sensors_per_dev[dev_num]--;
312
313                         return 1;
314                 }
315
316         if (enabled) {
317                 active_poll_sensors++;
318                 poll_sensors_per_dev[dev_num]++;
319                 return 1;
320         }
321
322         active_poll_sensors--;
323         poll_sensors_per_dev[dev_num]--;
324         return 1;
325 }
326
327
328 static int get_field_count (int s)
329 {
330         switch (sensor_info[s].type) {
331                 case SENSOR_TYPE_ACCELEROMETER:         /* m/s^2        */
332                 case SENSOR_TYPE_MAGNETIC_FIELD:        /* micro-tesla  */
333                 case SENSOR_TYPE_ORIENTATION:           /* degrees      */
334                 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
335                 case SENSOR_TYPE_GYROSCOPE:             /* radians/s    */
336                         return 3;
337
338                 case SENSOR_TYPE_LIGHT:                 /* SI lux units */
339                 case SENSOR_TYPE_AMBIENT_TEMPERATURE:   /* Â°C          */
340                 case SENSOR_TYPE_TEMPERATURE:           /* Â°C          */
341                 case SENSOR_TYPE_PROXIMITY:             /* centimeters  */
342                 case SENSOR_TYPE_PRESSURE:              /* hecto-pascal */
343                 case SENSOR_TYPE_RELATIVE_HUMIDITY:     /* percent */
344                         return 1;
345
346                 case SENSOR_TYPE_ROTATION_VECTOR:
347                         return  4;
348
349                 default:
350                         ALOGE("Unknown sensor type!\n");
351                         return 0;                       /* Drop sample */
352         }
353 }
354
355
356 static void* acquisition_routine (void* param)
357 {
358         /*
359          * Data acquisition routine run in a dedicated thread, covering a single
360          * sensor. This loop will periodically retrieve sampling data through
361          * sysfs, then package it as a sample and transfer it to our master poll
362          * loop through a report fd. Checks for a cancellation signal quite
363          * frequently, as the thread may be disposed of at any time. Note that
364          * Bionic does not provide pthread_cancel / pthread_testcancel...
365          */
366
367         int s = (int) (size_t) param;
368         int num_fields, sample_size;
369         struct sensors_event_t data = {0};
370         int c;
371         int ret;
372         struct timespec target_time;
373         int64_t timestamp, period, start, stop;
374
375         if (s < 0 || s >= sensor_count) {
376                 ALOGE("Invalid sensor handle!\n");
377                 return NULL;
378         }
379
380         ALOGI("Entering data acquisition thread S%d (%s): rate(%f), ts(%lld)\n", s,
381                 sensor_info[s].friendly_name, sensor_info[s].sampling_rate, sensor_info[s].report_ts);
382
383         if (sensor_info[s].sampling_rate <= 0) {
384                 ALOGE("Non-positive rate in acquisition routine for sensor %d: %f\n",
385                         s, sensor_info[s].sampling_rate);
386                 return NULL;
387         }
388
389         num_fields = get_field_count(s);
390         sample_size = sizeof(int64_t) + num_fields * sizeof(float);
391
392         /*
393          * Each condition variable is associated to a mutex that has to be
394          * locked by the thread that's waiting on it. We use these condition
395          * variables to get the acquisition threads out of sleep quickly after
396          * the sampling rate is adjusted, or the sensor is disabled.
397          */
398         pthread_mutex_lock(&thread_release_mutex[s]);
399
400         /* Pinpoint the moment we start sampling */
401         timestamp = get_timestamp_monotonic();
402
403         /* Check and honor termination requests */
404         while (sensor_info[s].thread_data_fd[1] != -1) {
405                 start = get_timestamp();
406                 /* Read values through sysfs */
407                 for (c=0; c<num_fields; c++) {
408                         data.data[c] = acquire_immediate_value(s, c);
409                         /* Check and honor termination requests */
410                         if (sensor_info[s].thread_data_fd[1] == -1)
411                                 goto exit;
412                 }
413                 stop = get_timestamp();
414                 data.timestamp = start/2 + stop/2;
415
416                 /* If the sample looks good */
417                 if (sensor_info[s].ops.finalize(s, &data)) {
418
419                         /* Pipe it for transmission to poll loop */
420                         ret = write(    sensor_info[s].thread_data_fd[1],
421                                         &data.timestamp, sample_size);
422
423                         if (ret != sample_size)
424                                 ALOGE("S%d acquisition thread: tried to write %d, ret: %d\n",
425                                         s, sample_size, ret);
426                 }
427
428                 /* Check and honor termination requests */
429                 if (sensor_info[s].thread_data_fd[1] == -1)
430                         goto exit;
431
432                 /* Recalculate period asumming sensor_info[s].sampling_rate
433                  * can be changed dynamically during the thread run */
434                 if (sensor_info[s].sampling_rate <= 0) {
435                         ALOGE("Non-positive rate in acquisition routine for sensor %d: %f\n",
436                                 s, sensor_info[s].sampling_rate);
437                         goto exit;
438                 }
439
440                 period = (int64_t) (1000000000LL / sensor_info[s].sampling_rate);
441                 timestamp += period;
442                 set_timestamp(&target_time, timestamp);
443
444                 /*
445                  * Wait until the sampling time elapses, or a rate change is
446                  * signaled, or a thread exit is requested.
447                  */
448                 ret = pthread_cond_timedwait(   &thread_release_cond[s],
449                                                 &thread_release_mutex[s],
450                                                 &target_time);
451         }
452
453 exit:
454         ALOGV("Acquisition thread for S%d exiting\n", s);
455         pthread_mutex_unlock(&thread_release_mutex[s]);
456         pthread_exit(0);
457         return NULL;
458 }
459
460
461 static void start_acquisition_thread (int s)
462 {
463         int incoming_data_fd;
464         int ret;
465
466         struct epoll_event ev = {0};
467
468         ALOGV("Initializing acquisition context for sensor %d\n", s);
469
470         /* Create condition variable and mutex for quick thread release */
471         ret = pthread_condattr_init(&thread_cond_attr[s]);
472         ret = pthread_condattr_setclock(&thread_cond_attr[s], CLOCK_MONOTONIC);
473         ret = pthread_cond_init(&thread_release_cond[s], &thread_cond_attr[s]);
474         ret = pthread_mutex_init(&thread_release_mutex[s], NULL);
475
476         /* Create a pipe for inter thread communication */
477         ret = pipe(sensor_info[s].thread_data_fd);
478
479         incoming_data_fd = sensor_info[s].thread_data_fd[0];
480
481         ev.events = EPOLLIN;
482         ev.data.u32 = THREAD_REPORT_TAG_BASE + s;
483
484         /* Add incoming side of pipe to our poll set, with a suitable tag */
485         ret = epoll_ctl(poll_fd, EPOLL_CTL_ADD, incoming_data_fd , &ev);
486
487         /* Create and start worker thread */
488         ret = pthread_create(   &sensor_info[s].acquisition_thread,
489                                 NULL,
490                                 acquisition_routine,
491                                 (void*) (size_t) s);
492 }
493
494
495 static void stop_acquisition_thread (int s)
496 {
497         int incoming_data_fd = sensor_info[s].thread_data_fd[0];
498         int outgoing_data_fd = sensor_info[s].thread_data_fd[1];
499
500         ALOGV("Tearing down acquisition context for sensor %d\n", s);
501
502         /* Delete the incoming side of the pipe from our poll set */
503         epoll_ctl(poll_fd, EPOLL_CTL_DEL, incoming_data_fd, NULL);
504
505         /* Mark the pipe ends as invalid ; that's a cheap exit flag */
506         sensor_info[s].thread_data_fd[0] = -1;
507         sensor_info[s].thread_data_fd[1] = -1;
508
509         /* Close both sides of our pipe */
510         close(incoming_data_fd);
511         close(outgoing_data_fd);
512
513         /* Stop acquisition thread and clean up thread handle */
514         pthread_cond_signal(&thread_release_cond[s]);
515         pthread_join(sensor_info[s].acquisition_thread, NULL);
516
517         /* Clean up our sensor descriptor */
518         sensor_info[s].acquisition_thread = -1;
519
520         /* Delete condition variable and mutex */
521         pthread_cond_destroy(&thread_release_cond[s]);
522         pthread_mutex_destroy(&thread_release_mutex[s]);
523 }
524
525
526 int sensor_activate(int s, int enabled)
527 {
528         char device_name[PATH_MAX];
529         struct epoll_event ev = {0};
530         int dev_fd;
531         int ret;
532         int dev_num = sensor_info[s].dev_num;
533         int is_poll_sensor = !sensor_info[s].num_channels;
534
535         /* Prepare the report timestamp field for the first event, see set_report_ts method */
536         sensor_info[s].report_ts = 0;
537         ts_delta = load_timestamp_sys_clock() - get_timestamp_monotonic();
538
539
540         /* If we want to activate gyro calibrated and gyro uncalibrated is activated
541          * Deactivate gyro uncalibrated - Uncalibrated releases handler
542          * Activate gyro calibrated     - Calibrated has handler
543          * Reactivate gyro uncalibrated - Uncalibrated gets data from calibrated */
544
545         /* If we want to deactivate gyro calibrated and gyro uncalibrated is active
546          * Deactivate gyro uncalibrated - Uncalibrated no longer gets data from handler
547          * Deactivate gyro calibrated   - Calibrated releases handler
548          * Reactivate gyro uncalibrated - Uncalibrated has handler */
549
550         if (sensor_info[s].type == SENSOR_TYPE_GYROSCOPE &&
551                 sensor_info[s].pair_idx && sensor_info[sensor_info[s].pair_idx].enabled != 0) {
552
553                                 sensor_activate(sensor_info[s].pair_idx, 0);
554                                 ret = sensor_activate(s, enabled);
555                                 sensor_activate(sensor_info[s].pair_idx, 1);
556                                 return ret;
557         }
558
559         ret = adjust_counters(s, enabled);
560
561         /* If the operation was neutral in terms of state, we're done */
562         if (ret <= 0)
563                 return ret;
564
565         sensor_info[s].event_count = 0;
566         sensor_info[s].meta_data_pending = 0;
567
568         if (enabled && (sensor_info[s].quirks & QUIRK_NOISY))
569                 /* Initialize filtering data if required */
570                 setup_noise_filtering(s);
571
572         if (!is_poll_sensor) {
573
574                 /* Stop sampling */
575                 enable_buffer(dev_num, 0);
576                 setup_trigger(s, "\n");
577
578                 /* If there's at least one sensor enabled on this iio device */
579                 if (trig_sensors_per_dev[dev_num]) {
580
581                         /* Start sampling */
582                         setup_trigger(s, sensor_info[s].init_trigger_name);
583                         enable_buffer(dev_num, 1);
584                 }
585         }
586
587         /*
588          * Make sure we have a fd on the character device ; conversely, close
589          * the fd if no one is using associated sensors anymore. The assumption
590          * here is that the underlying driver will power on the relevant
591          * hardware block while someone holds a fd on the device.
592          */
593         dev_fd = device_fd[dev_num];
594
595         if (!enabled) {
596                 if (is_poll_sensor)
597                         stop_acquisition_thread(s);
598
599                 if (dev_fd != -1 && !poll_sensors_per_dev[dev_num] &&
600                         !trig_sensors_per_dev[dev_num]) {
601                                 /*
602                                  * Stop watching this fd. This should be a no-op
603                                  * in case this fd was not in the poll set.
604                                  */
605                                 epoll_ctl(poll_fd, EPOLL_CTL_DEL, dev_fd, NULL);
606
607                                 close(dev_fd);
608                                 device_fd[dev_num] = -1;
609                         }
610
611                 /* Release any filtering data we may have accumulated */
612                 release_noise_filtering_data(s);
613
614                 return 0;
615         }
616
617         if (dev_fd == -1) {
618                 /* First enabled sensor on this iio device */
619                 sprintf(device_name, DEV_FILE_PATH, dev_num);
620                 dev_fd = open(device_name, O_RDONLY | O_NONBLOCK);
621
622                 device_fd[dev_num] = dev_fd;
623
624                 if (dev_fd == -1) {
625                         ALOGE("Could not open fd on %s (%s)\n",
626                               device_name, strerror(errno));
627                         adjust_counters(s, 0);
628                         return -1;
629                 }
630
631                 ALOGV("Opened %s: fd=%d\n", device_name, dev_fd);
632
633                 if (!is_poll_sensor) {
634
635                         /* Add this iio device fd to the set of watched fds */
636                         ev.events = EPOLLIN;
637                         ev.data.u32 = dev_num;
638
639                         ret = epoll_ctl(poll_fd, EPOLL_CTL_ADD, dev_fd, &ev);
640
641                         if (ret == -1) {
642                                 ALOGE(  "Failed adding %d to poll set (%s)\n",
643                                         dev_fd, strerror(errno));
644                                 return -1;
645                         }
646
647                         /* Note: poll-mode fds are not readable */
648                 }
649         }
650
651         /* Ensure that on-change sensors send at least one event after enable */
652         sensor_info[s].prev_val = -1;
653
654         if (is_poll_sensor)
655                 start_acquisition_thread(s);
656
657         return 0;
658 }
659
660
661 static int is_fast_accelerometer (int s)
662 {
663         /*
664          * Some games don't react well to accelerometers using any-motion
665          * triggers. Even very low thresholds seem to trip them, and they tend
666          * to request fairly high event rates. Favor continuous triggers if the
667          * sensor is an accelerometer and uses a sampling rate of at least 25.
668          */
669
670         if (sensor_info[s].type != SENSOR_TYPE_ACCELEROMETER)
671                 return 0;
672
673         if (sensor_info[s].sampling_rate < 25)
674                 return 0;
675
676         return 1;
677 }
678
679
680 static void enable_motion_trigger (int dev_num)
681 {
682         /*
683          * In the ideal case, we enumerate two triggers per iio device ; the
684          * default (periodically firing) trigger, and another one (the motion
685          * trigger) that only fires up when motion is detected. This second one
686          * allows for lesser energy consumption, but requires periodic sample
687          * duplication at the HAL level for sensors that Android defines as
688          * continuous. This "duplicate last sample" logic can only be engaged
689          * once we got a first sample for the driver, so we start with the
690          * default trigger when an iio device is first opened, then adjust the
691          * trigger when we got events for all active sensors. Unfortunately in
692          * the general case several sensors can be associated to a given iio
693          * device, they can independently be controlled, and we have to adjust
694          * the trigger in use at the iio device level depending on whether or
695          * not appropriate conditions are met at the sensor level.
696          */
697
698         int s;
699         int i;
700         int active_sensors = trig_sensors_per_dev[dev_num];
701         int candidate[MAX_SENSORS];
702         int candidate_count = 0;
703
704         if  (!active_sensors)
705                 return;
706
707         /* Check that all active sensors are ready to switch */
708
709         for (s=0; s<MAX_SENSORS; s++)
710                 if (sensor_info[s].dev_num == dev_num &&
711                     sensor_info[s].enabled &&
712                     sensor_info[s].num_channels &&
713                     (!sensor_info[s].motion_trigger_name[0] ||
714                      !sensor_info[s].report_initialized ||
715                      is_fast_accelerometer(s) ||
716                      (sensor_info[s].quirks & QUIRK_FORCE_CONTINUOUS))
717                     )
718                         return; /* Nope */
719
720         /* Record which particular sensors need to switch */
721
722         for (s=0; s<MAX_SENSORS; s++)
723                 if (sensor_info[s].dev_num == dev_num &&
724                     sensor_info[s].enabled &&
725                     sensor_info[s].num_channels &&
726                     sensor_info[s].selected_trigger !=
727                         sensor_info[s].motion_trigger_name)
728                                 candidate[candidate_count++] = s;
729
730         if (!candidate_count)
731                 return;
732
733         /* Now engage the motion trigger for sensors which aren't using it */
734
735         enable_buffer(dev_num, 0);
736
737         for (i=0; i<candidate_count; i++) {
738                 s = candidate[i];
739                 setup_trigger(s, sensor_info[s].motion_trigger_name);
740         }
741
742         enable_buffer(dev_num, 1);
743 }
744
745 /* CTS acceptable thresholds:
746  *      EventGapVerification.java: (th <= 1.8)
747  *      FrequencyVerification.java: (0.9)*(expected freq) => (th <= 1.1111)
748  */
749 #define THRESHOLD 1.10
750 #define MAX_DELAY 500000000 /* 500 ms */
751 void set_report_ts(int s, int64_t ts)
752 {
753         int64_t maxTs, period;
754         int catalog_index = sensor_info[s].catalog_index;
755         int is_accel      = (sensor_catalog[catalog_index].type == SENSOR_TYPE_ACCELEROMETER);
756
757         /*
758         *  A bit of a hack to please a bunch of cts tests. They
759         *  expect the timestamp to be exacly according to the set-up
760         *  frequency but if we're simply getting the timestamp at hal level
761         *  this may not be the case. Perhaps we'll get rid of this when
762         *  we'll be reading the timestamp from the iio channel for all sensors
763         */
764         if (sensor_info[s].report_ts && sensor_info[s].sampling_rate &&
765                 REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_CONTINUOUS_MODE)
766         {
767                 period = (int64_t) (1000000000LL / sensor_info[s].sampling_rate);
768                 maxTs = sensor_info[s].report_ts + (is_accel ? 1 : THRESHOLD) * period;
769                 /* If we're too far behind get back on track */
770                 if (ts - maxTs >= MAX_DELAY)
771                         maxTs = ts;
772                 sensor_info[s].report_ts = (ts < maxTs ? ts : maxTs);
773         } else {
774                 sensor_info[s].report_ts = ts;
775         }
776 }
777
778 static int integrate_device_report (int dev_num)
779 {
780         int len;
781         int s,c;
782         unsigned char buf[MAX_SENSOR_REPORT_SIZE] = { 0 };
783         int sr_offset;
784         unsigned char *target;
785         unsigned char *source;
786         int size;
787
788         /* There's an incoming report on the specified iio device char dev fd */
789
790         if (dev_num < 0 || dev_num >= MAX_DEVICES) {
791                 ALOGE("Event reported on unexpected iio device %d\n", dev_num);
792                 return -1;
793         }
794
795         if (device_fd[dev_num] == -1) {
796                 ALOGE("Ignoring stale report on iio device %d\n", dev_num);
797                 return -1;
798         }
799
800
801
802         len = read(device_fd[dev_num], buf, MAX_SENSOR_REPORT_SIZE);
803
804         if (len == -1) {
805                 ALOGE("Could not read report from iio device %d (%s)\n",
806                       dev_num, strerror(errno));
807                 return -1;
808         }
809
810         ALOGV("Read %d bytes from iio device %d\n", len, dev_num);
811
812         /* Map device report to sensor reports */
813
814         for (s=0; s<MAX_SENSORS; s++)
815                 if (sensor_info[s].dev_num == dev_num &&
816                     sensor_info[s].enabled) {
817
818                         sr_offset = 0;
819
820                         /* Copy data from device to sensor report buffer */
821                         for (c=0; c<sensor_info[s].num_channels; c++) {
822
823                                 target = sensor_info[s].report_buffer +
824                                         sr_offset;
825
826                                 source = buf + sensor_info[s].channel[c].offset;
827
828                                 size = sensor_info[s].channel[c].size;
829
830                                 memcpy(target, source, size);
831
832                                 sr_offset += size;
833                         }
834
835                         ALOGV("Sensor %d report available (%d bytes)\n", s,
836                               sr_offset);
837
838                         set_report_ts(s, get_timestamp());
839                         sensor_info[s].report_pending = DATA_TRIGGER;
840                         sensor_info[s].report_initialized = 1;
841                 }
842
843         /* Tentatively switch to an any-motion trigger if conditions are met */
844         enable_motion_trigger(dev_num);
845
846         return 0;
847 }
848
849
850 static int propagate_sensor_report (int s, struct sensors_event_t  *data)
851 {
852         /* There's a sensor report pending for this sensor ; transmit it */
853
854         int num_fields    = get_field_count(s);
855         int c;
856         unsigned char* current_sample;
857
858         /* If there's nothing to return... we're done */
859         if (!num_fields)
860                 return 0;
861
862
863         /* Only return uncalibrated event if also gyro active */
864         if (sensor_info[s].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED &&
865                 sensor_info[sensor_info[s].pair_idx].enabled != 0)
866                         return 0;
867
868         memset(data, 0, sizeof(sensors_event_t));
869
870         data->version   = sizeof(sensors_event_t);
871         data->sensor    = s;
872         data->type      = sensor_info[s].type;
873         data->timestamp = sensor_info[s].report_ts;
874
875         ALOGV("Sample on sensor %d (type %d):\n", s, sensor_info[s].type);
876
877         current_sample = sensor_info[s].report_buffer;
878
879         /* If this is a poll sensor */
880         if (!sensor_info[s].num_channels) {
881                 /* Use the data provided by the acquisition thread */
882                 ALOGV("Reporting data from worker thread for S%d\n", s);
883                 memcpy(data->data, current_sample, num_fields * sizeof(float));
884                 return 1;
885         }
886
887         /* Convert the data into the expected Android-level format */
888         for (c=0; c<num_fields; c++) {
889
890                 data->data[c] = sensor_info[s].ops.transform
891                                                         (s, c, current_sample);
892
893                 ALOGV("\tfield %d: %f\n", c, data->data[c]);
894                 current_sample += sensor_info[s].channel[c].size;
895         }
896
897         /*
898          * The finalize routine, in addition to its late sample processing duty,
899          * has the final say on whether or not the sample gets sent to Android.
900          */
901         return sensor_info[s].ops.finalize(s, data);
902 }
903
904
905 static void synthetize_duplicate_samples (void)
906 {
907         /*
908          * Some sensor types (ex: gyroscope) are defined as continuously firing
909          * by Android, despite the fact that we can be dealing with iio drivers
910          * that only report events for new samples. For these we generate
911          * reports periodically, duplicating the last data we got from the
912          * driver. This is not necessary for polling sensors.
913          */
914
915         int s;
916         int64_t current_ts;
917         int64_t target_ts;
918         int64_t period;
919
920         for (s=0; s<sensor_count; s++) {
921
922                 /* Ignore disabled sensors */
923                 if (!sensor_info[s].enabled)
924                         continue;
925
926                 /* If the sensor is continuously firing, leave it alone */
927                 if (sensor_info[s].selected_trigger !=
928                     sensor_info[s].motion_trigger_name)
929                         continue;
930
931                 /* If we haven't seen a sample, there's nothing to duplicate */
932                 if (!sensor_info[s].report_initialized)
933                         continue;
934
935                 /* If a sample was recently buffered, leave it alone too */
936                 if (sensor_info[s].report_pending)
937                         continue;
938
939                 /* We also need a valid sampling rate to be configured */
940                 if (!sensor_info[s].sampling_rate)
941                         continue;
942
943                 period = (int64_t) (1000000000.0/ sensor_info[s].sampling_rate);
944
945                 current_ts = get_timestamp();
946                 target_ts = sensor_info[s].report_ts + period;
947
948                 if (target_ts <= current_ts) {
949                         /* Mark the sensor for event generation */
950                         set_report_ts(s, current_ts);
951                         sensor_info[s].report_pending = DATA_DUPLICATE;
952                 }
953         }
954 }
955
956
957 static void integrate_thread_report (uint32_t tag)
958 {
959         int s = tag - THREAD_REPORT_TAG_BASE;
960         int len;
961         int expected_len;
962         int64_t timestamp;
963         unsigned char current_sample[MAX_SENSOR_REPORT_SIZE];
964
965         expected_len = sizeof(int64_t) + get_field_count(s) * sizeof(float);
966
967         len = read(sensor_info[s].thread_data_fd[0],
968                    current_sample,
969                    expected_len);
970
971         memcpy(&timestamp, current_sample, sizeof(int64_t));
972         memcpy(sensor_info[s].report_buffer, sizeof(int64_t) + current_sample,
973                         expected_len - sizeof(int64_t));
974
975         if (len == expected_len) {
976                 set_report_ts(s, timestamp);
977                 sensor_info[s].report_pending = DATA_SYSFS;
978         }
979 }
980
981
982 static int get_poll_wait_timeout (void)
983 {
984         /*
985          * Compute an appropriate timeout value, in ms, for the epoll_wait
986          * call that's going to await for iio device reports and incoming
987          * reports from our sensor sysfs data reader threads.
988          */
989
990         int s;
991         int64_t target_ts = INT64_MAX;
992         int64_t ms_to_wait;
993         int64_t period;
994
995         /*
996          * Check if we're dealing with a driver that only send events when
997          * there is motion, despite the fact that the associated Android sensor
998          * type is continuous rather than on-change. In that case we have to
999          * duplicate events. Check deadline for the nearest upcoming event.
1000          */
1001         for (s=0; s<sensor_count; s++)
1002                 if (sensor_info[s].enabled &&
1003                     sensor_info[s].selected_trigger ==
1004                     sensor_info[s].motion_trigger_name &&
1005                     sensor_info[s].sampling_rate) {
1006                         period = (int64_t) (1000000000.0 /
1007                                                 sensor_info[s].sampling_rate);
1008
1009                         if (sensor_info[s].report_ts + period < target_ts)
1010                                 target_ts = sensor_info[s].report_ts + period;
1011                 }
1012
1013         /* If we don't have such a driver to deal with */
1014         if (target_ts == INT64_MAX)
1015                 return -1; /* Infinite wait */
1016
1017         ms_to_wait = (target_ts - get_timestamp()) / 1000000;
1018
1019         /* If the target timestamp is already behind us, don't wait */
1020         if (ms_to_wait < 1)
1021                 return 0;
1022
1023         return ms_to_wait;
1024 }
1025
1026
1027 int sensor_poll(struct sensors_event_t* data, int count)
1028 {
1029         int s;
1030         int i;
1031         int nfds;
1032         struct epoll_event ev[MAX_DEVICES];
1033         int returned_events;
1034         int event_count;
1035         int uncal_start;
1036
1037         /* Get one or more events from our collection of sensors */
1038
1039 return_available_sensor_reports:
1040
1041         /* Synthetize duplicate samples if needed */
1042         synthetize_duplicate_samples();
1043
1044         returned_events = 0;
1045
1046         /* Check our sensor collection for available reports */
1047         for (s=0; s<sensor_count && returned_events < count; s++) {
1048                 if (sensor_info[s].report_pending) {
1049                         event_count = 0;
1050
1051                         /* Report this event if it looks OK */
1052                         event_count = propagate_sensor_report(s, &data[returned_events]);
1053
1054                         /* Lower flag */
1055                         sensor_info[s].report_pending = 0;
1056
1057                         /* Duplicate only if both cal & uncal are active */
1058                         if (sensor_info[s].type == SENSOR_TYPE_GYROSCOPE &&
1059                                         sensor_info[s].pair_idx && sensor_info[sensor_info[s].pair_idx].enabled != 0) {
1060                                         struct gyro_cal* gyro_data = (struct gyro_cal*) sensor_info[s].cal_data;
1061
1062                                         memcpy(&data[returned_events + event_count], &data[returned_events],
1063                                                         sizeof(struct sensors_event_t) * event_count);
1064
1065                                         uncal_start = returned_events + event_count;
1066                                         for (i = 0; i < event_count; i++) {
1067                                                 data[uncal_start + i].type = SENSOR_TYPE_GYROSCOPE_UNCALIBRATED;
1068                                                 data[uncal_start + i].sensor = sensor_info[s].pair_idx;
1069
1070                                                 data[uncal_start + i].data[0] = data[returned_events + i].data[0] + gyro_data->bias_x;
1071                                                 data[uncal_start + i].data[1] = data[returned_events + i].data[1] + gyro_data->bias_y;
1072                                                 data[uncal_start + i].data[2] = data[returned_events + i].data[2] + gyro_data->bias_z;
1073
1074                                                 data[uncal_start + i].uncalibrated_gyro.bias[0] = gyro_data->bias_x;
1075                                                 data[uncal_start + i].uncalibrated_gyro.bias[1] = gyro_data->bias_y;
1076                                                 data[uncal_start + i].uncalibrated_gyro.bias[2] = gyro_data->bias_z;
1077                                         }
1078                                         event_count <<= 1;
1079                         }
1080                         sensor_info[sensor_info[s].pair_idx].report_pending = 0;
1081                         returned_events += event_count;
1082                         /*
1083                          * If the sample was deemed invalid or unreportable,
1084                          * e.g. had the same value as the previously reported
1085                          * value for a 'on change' sensor, silently drop it.
1086                          */
1087                 }
1088                 while (sensor_info[s].meta_data_pending) {
1089                         /* See sensors.h on these */
1090                         data[returned_events].version = META_DATA_VERSION;
1091                         data[returned_events].sensor = 0;
1092                         data[returned_events].type = SENSOR_TYPE_META_DATA;
1093                         data[returned_events].reserved0 = 0;
1094                         data[returned_events].timestamp = 0;
1095                         data[returned_events].meta_data.sensor = s;
1096                         data[returned_events].meta_data.what = META_DATA_FLUSH_COMPLETE;
1097                         returned_events++;
1098                         sensor_info[s].meta_data_pending--;
1099                 }
1100         }
1101         if (returned_events)
1102                 return returned_events;
1103
1104 await_event:
1105
1106         ALOGV("Awaiting sensor data\n");
1107
1108         nfds = epoll_wait(poll_fd, ev, MAX_DEVICES, get_poll_wait_timeout());
1109
1110         if (nfds == -1) {
1111                 ALOGE("epoll_wait returned -1 (%s)\n", strerror(errno));
1112                 goto await_event;
1113         }
1114
1115         ALOGV("%d fds signalled\n", nfds);
1116
1117         /* For each of the signalled sources */
1118         for (i=0; i<nfds; i++)
1119                 if (ev[i].events == EPOLLIN)
1120                         switch (ev[i].data.u32) {
1121                                 case 0 ... MAX_DEVICES-1:
1122                                         /* Read report from iio char dev fd */
1123                                         integrate_device_report(ev[i].data.u32);
1124                                         break;
1125
1126                                 case THREAD_REPORT_TAG_BASE ...
1127                                      THREAD_REPORT_TAG_BASE + MAX_SENSORS-1:
1128                                         /* Get report from acquisition thread */
1129                                         integrate_thread_report(ev[i].data.u32);
1130                                         break;
1131
1132                                 default:
1133                                         ALOGW("Unexpected event source!\n");
1134                                         break;
1135                         }
1136
1137         goto return_available_sensor_reports;
1138 }
1139
1140
1141 static void tentative_switch_trigger (int s)
1142 {
1143         /*
1144          * Under certain situations it may be beneficial to use an alternate
1145          * trigger:
1146          *
1147          * - for applications using the accelerometer with high sampling rates,
1148          *   prefer the continuous trigger over the any-motion one, to avoid
1149          *   jumps related to motion thresholds
1150          */
1151
1152         if (is_fast_accelerometer(s) &&
1153                 !(sensor_info[s].quirks & QUIRK_TERSE_DRIVER) &&
1154                         sensor_info[s].selected_trigger ==
1155                                 sensor_info[s].motion_trigger_name)
1156                 setup_trigger(s, sensor_info[s].init_trigger_name);
1157 }
1158
1159
1160 int sensor_set_delay(int s, int64_t ns)
1161 {
1162         /* Set the rate at which a specific sensor should report events */
1163
1164         /* See Android sensors.h for indication on sensor trigger modes */
1165
1166         char sysfs_path[PATH_MAX];
1167         char avail_sysfs_path[PATH_MAX];
1168         int dev_num             =       sensor_info[s].dev_num;
1169         int i                   =       sensor_info[s].catalog_index;
1170         const char *prefix      =       sensor_catalog[i].tag;
1171         float new_sampling_rate; /* Granted sampling rate after arbitration   */
1172         float cur_sampling_rate; /* Currently used sampling rate              */
1173         int per_sensor_sampling_rate;
1174         int per_device_sampling_rate;
1175         int32_t min_delay_us = sensor_desc[s].minDelay;
1176         max_delay_t max_delay_us = sensor_desc[s].maxDelay;
1177         float min_supported_rate = max_delay_us ? (1000000.0f / max_delay_us) : 1;
1178         float max_supported_rate = 
1179                 (min_delay_us && min_delay_us != -1) ? (1000000.0f / min_delay_us) : 0;
1180         char freqs_buf[100];
1181         char* cursor;
1182         int n;
1183         float sr;
1184
1185         if (ns <= 0) {
1186                 ALOGE("Rejecting non-positive delay request on sensor %d, required delay: %lld\n", s, ns);
1187                 return -EINVAL;
1188         }
1189
1190         new_sampling_rate = 1000000000LL/ns;
1191
1192         ALOGV("Entering set delay S%d (%s): old rate(%f), new rate(%f)\n",
1193                 s, sensor_info[s].friendly_name, sensor_info[s].sampling_rate,
1194                 new_sampling_rate);
1195
1196         /*
1197          * Artificially limit ourselves to 1 Hz or higher. This is mostly to
1198          * avoid setting up the stage for divisions by zero.
1199          */
1200         if (new_sampling_rate < min_supported_rate)
1201                 new_sampling_rate = min_supported_rate;
1202
1203         if (max_supported_rate &&
1204                 new_sampling_rate > max_supported_rate) {
1205                 new_sampling_rate = max_supported_rate;
1206         }
1207
1208         sensor_info[s].sampling_rate = new_sampling_rate;
1209
1210         /* If we're dealing with a poll-mode sensor */
1211         if (!sensor_info[s].num_channels) {
1212                 /* Interrupt current sleep so the new sampling gets used */
1213                 pthread_cond_signal(&thread_release_cond[s]);
1214                 return 0;
1215         }
1216
1217         sprintf(sysfs_path, SENSOR_SAMPLING_PATH, dev_num, prefix);
1218
1219         if (sysfs_read_float(sysfs_path, &cur_sampling_rate) != -1) {
1220                 per_sensor_sampling_rate = 1;
1221                 per_device_sampling_rate = 0;
1222         } else {
1223                 per_sensor_sampling_rate = 0;
1224
1225                 sprintf(sysfs_path, DEVICE_SAMPLING_PATH, dev_num);
1226
1227                 if (sysfs_read_float(sysfs_path, &cur_sampling_rate) != -1)
1228                         per_device_sampling_rate = 1;
1229                 else
1230                         per_device_sampling_rate = 0;
1231         }
1232
1233         if (!per_sensor_sampling_rate && !per_device_sampling_rate) {
1234                 ALOGE("No way to adjust sampling rate on sensor %d\n", s);
1235                 return -ENOSYS;
1236         }
1237
1238         /* Coordinate with others active sensors on the same device, if any */
1239         if (per_device_sampling_rate)
1240                 for (n=0; n<sensor_count; n++)
1241                         if (n != s && sensor_info[n].dev_num == dev_num &&
1242                             sensor_info[n].num_channels &&
1243                             sensor_info[n].enabled &&
1244                             sensor_info[n].sampling_rate > new_sampling_rate)
1245                                 new_sampling_rate= sensor_info[n].sampling_rate;
1246
1247         /* Check if we have contraints on allowed sampling rates */
1248
1249         sprintf(avail_sysfs_path, DEVICE_AVAIL_FREQ_PATH, dev_num);
1250
1251         if (sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf)) > 0){
1252                 cursor = freqs_buf;
1253
1254                 /* Decode allowed sampling rates string, ex: "10 20 50 100" */
1255
1256                 /* While we're not at the end of the string */
1257                 while (*cursor && cursor[0]) {
1258
1259                         /* Decode a single value */
1260                         sr = strtod(cursor, NULL);
1261
1262                         /* If this matches the selected rate, we're happy */
1263                         if (new_sampling_rate == sr)
1264                                 break;
1265
1266                         /*
1267                          * If we reached a higher value than the desired rate,
1268                          * adjust selected rate so it matches the first higher
1269                          * available one and stop parsing - this makes the
1270                          * assumption that rates are sorted by increasing value
1271                          * in the allowed frequencies string.
1272                          */
1273                         if (sr > new_sampling_rate) {
1274                                 new_sampling_rate = sr;
1275                                 break;
1276                         }
1277
1278                         /* Skip digits */
1279                         while (cursor[0] && !isspace(cursor[0]))
1280                                 cursor++;
1281
1282                         /* Skip spaces */
1283                         while (cursor[0] && isspace(cursor[0]))
1284                                         cursor++;
1285                 }
1286         }
1287
1288         if (max_supported_rate &&
1289                 new_sampling_rate > max_supported_rate) {
1290                 new_sampling_rate = max_supported_rate;
1291         }
1292
1293         /* If the desired rate is already active we're all set */
1294         if (new_sampling_rate == cur_sampling_rate)
1295                 return 0;
1296
1297         ALOGI("Sensor %d sampling rate set to %g\n", s, new_sampling_rate);
1298
1299         if (trig_sensors_per_dev[dev_num])
1300                 enable_buffer(dev_num, 0);
1301
1302         sysfs_write_float(sysfs_path, new_sampling_rate);
1303
1304         /* Check if it makes sense to use an alternate trigger */
1305         tentative_switch_trigger(s);
1306
1307         if (trig_sensors_per_dev[dev_num])
1308                 enable_buffer(dev_num, 1);
1309
1310         return 0;
1311 }
1312
1313 int sensor_flush (int s)
1314 {
1315         /* If one shot or not enabled return -EINVAL */
1316         if (sensor_desc[s].flags & SENSOR_FLAG_ONE_SHOT_MODE ||
1317                 sensor_info[s].enabled == 0)
1318                 return -EINVAL;
1319
1320         sensor_info[s].meta_data_pending++;
1321         return 0;
1322 }
1323
1324 int allocate_control_data (void)
1325 {
1326         int i;
1327
1328         for (i=0; i<MAX_DEVICES; i++)
1329                 device_fd[i] = -1;
1330
1331         poll_fd = epoll_create(MAX_DEVICES);
1332
1333         if (poll_fd == -1) {
1334                 ALOGE("Can't create epoll instance for iio sensors!\n");
1335                 return -1;
1336         }
1337
1338         return poll_fd;
1339 }
1340
1341
1342 void delete_control_data (void)
1343 {
1344 }