1 #include <hardware_legacy/flashlight.h>
2
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <fcntl.h>
6 #include <errno.h>
7 #include "qemu.h"
8
9 #define FLASHLIGHT "/sys/class/leds/spotlight/brightness"
10 #define CAMERA_FLASH "/sys/class/timed_output/flash/enable"
11
12 #ifdef QEMU_HARDWARE
qemu_get_flashlight_enabled()13 int qemu_get_flashlight_enabled()
14 {
15 char question[256];
16 char answer[256];
17 int len;
18
19 len = qemu_command_format( question, sizeof question,
20 "get_flashlight_enabled" );
21
22 len = qemu_control_query( question, len, answer, sizeof answer );
23 if (len <= 0) return 0;
24
25 /* we expect an answer of 0 or 1 */
26 return (answer[0] == '1');
27 }
28
qemu_set_flashlight_enabled(int on)29 int qemu_set_flashlight_enabled(int on)
30 {
31 return qemu_control_command( "set_flashlight_enabled:%d", on );
32 }
33
qemu_enable_camera_flash(int milliseconds)34 int qemu_enable_camera_flash(int milliseconds)
35 {
36 return qemu_control_command( "enable_camera_flash:%d", milliseconds );
37 }
38 #endif
39
get_flashlight_enabled()40 int get_flashlight_enabled()
41 {
42 int fd;
43 int ret = 0;
44 char value;
45
46 QEMU_FALLBACK(get_flashlight_enabled());
47
48 fd = open(FLASHLIGHT, O_RDONLY);
49 if(fd && read(fd, &value, 1) == 1) {
50 ret = (value == '1');
51 }
52 close(fd);
53
54 return ret;
55 }
56
set_flashlight_enabled(int on)57 int set_flashlight_enabled(int on)
58 {
59 int nwr, ret, fd;
60 char value[20];
61
62 QEMU_FALLBACK(set_flashlight_enabled(on));
63
64 fd = open(FLASHLIGHT, O_RDWR);
65 if(fd < 0)
66 return errno;
67
68 nwr = sprintf(value, "%d\n", !!on);
69 ret = write(fd, value, nwr);
70
71 close(fd);
72
73 return (ret == nwr) ? 0 : -1;
74 }
75
enable_camera_flash(int milliseconds)76 int enable_camera_flash(int milliseconds)
77 {
78 int nwr, ret, fd;
79 char value[20];
80
81 QEMU_FALLBACK(enable_camera_flash(milliseconds));
82
83 fd = open(CAMERA_FLASH, O_RDWR);
84 if(fd < 0)
85 return errno;
86
87 nwr = sprintf(value, "%d\n", milliseconds);
88 ret = write(fd, value, nwr);
89
90 close(fd);
91
92 return (ret == nwr) ? 0 : -1;
93 }
94