The Android Open Source Project | d6054a3 | 2008-10-21 07:00:00 -0700 | [diff] [blame^] | 1 | #include <hardware/flashlight.h> |
| 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <unistd.h> |
| 5 | #include <fcntl.h> |
| 6 | #include <errno.h> |
| 7 | |
| 8 | #define FLASHLIGHT "/sys/class/leds/spotlight/brightness" |
| 9 | #define CAMERA_FLASH "/sys/class/timed_output/flash/enable" |
| 10 | |
| 11 | |
| 12 | int get_flashlight_enabled() |
| 13 | { |
| 14 | int fd; |
| 15 | int ret = 0; |
| 16 | char value; |
| 17 | |
| 18 | fd = open(FLASHLIGHT, O_RDONLY); |
| 19 | if(fd && read(fd, &value, 1) == 1) { |
| 20 | ret = (value == '1'); |
| 21 | } |
| 22 | close(fd); |
| 23 | |
| 24 | return ret; |
| 25 | } |
| 26 | |
| 27 | int set_flashlight_enabled(int on) |
| 28 | { |
| 29 | int nwr, ret, fd; |
| 30 | char value[20]; |
| 31 | |
| 32 | fd = open(FLASHLIGHT, O_RDWR); |
| 33 | if(fd < 0) |
| 34 | return errno; |
| 35 | |
| 36 | nwr = sprintf(value, "%d\n", !!on); |
| 37 | ret = write(fd, value, nwr); |
| 38 | |
| 39 | close(fd); |
| 40 | |
| 41 | return (ret == nwr) ? 0 : -1; |
| 42 | } |
| 43 | |
| 44 | int enable_camera_flash(int milliseconds) |
| 45 | { |
| 46 | int nwr, ret, fd; |
| 47 | char value[20]; |
| 48 | |
| 49 | fd = open(CAMERA_FLASH, O_RDWR); |
| 50 | if(fd < 0) |
| 51 | return errno; |
| 52 | |
| 53 | nwr = sprintf(value, "%d\n", milliseconds); |
| 54 | ret = write(fd, value, nwr); |
| 55 | |
| 56 | close(fd); |
| 57 | |
| 58 | return (ret == nwr) ? 0 : -1; |
| 59 | } |