1 // Copyright 2018 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <fcntl.h>
6 #include <fuchsia/hardware/backlight/c/fidl.h>
7 #include <lib/fdio/unsafe.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11
usage(char * argv[])12 static void usage(char* argv[]) {
13 printf("Usage: %s [--read|--off|<brightness-val>]\n", argv[0]);
14 printf("options:\n <brightness-val>: 0-255\n");
15 }
16
main(int argc,char * argv[])17 int main(int argc, char* argv[]) {
18 if (argc != 2) {
19 usage(argv);
20 return -1;
21 }
22
23 int fd = open("/dev/class/backlight/000", O_RDONLY);
24 if (fd < 0) {
25 printf("Failed to open backlight\n");
26 return -1;
27 }
28
29 fdio_t* io = fdio_unsafe_fd_to_io(fd);
30 if (io == NULL) {
31 printf("Failed to get fdio_t\n");
32 return -1;
33 }
34 zx_handle_t channel = fdio_unsafe_borrow_channel(io);
35
36 if (strcmp(argv[1], "--read") == 0) {
37 fuchsia_hardware_backlight_State state;
38 zx_status_t status = fuchsia_hardware_backlight_DeviceGetState(channel, &state);
39 if (status != ZX_OK) {
40 printf("Get backlight state failed %d\n", status);
41 return -1;
42 }
43 printf("Backlight:%s Brightness:%d\n", state.on ? "on" : "off", state.brightness);
44 return 0;
45 }
46
47 bool on;
48 uint32_t brightness;
49 if (strcmp(argv[1], "--off") == 0) {
50 on = false;
51 brightness = 0;
52 } else {
53 char* endptr;
54 brightness = strtoul(argv[1], &endptr, 10);
55 if (endptr == argv[1] || *endptr != '\0') {
56 usage(argv);
57 return -1;
58 }
59 if (brightness > 255) {
60 printf("Invalid brightness %d\n", brightness);
61 return -1;
62 }
63 on = true;
64 }
65
66 fuchsia_hardware_backlight_State state = {.on = on, .brightness = (uint8_t)brightness};
67 zx_status_t status = fuchsia_hardware_backlight_DeviceSetState(channel, &state);
68 if (status != ZX_OK) {
69 printf("Set brightness failed %d\n", status);
70 return -1;
71 }
72
73 return 0;
74 }
75