APM:Libraries
Led_Sysfs.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 Mathieu Othacehe. All rights reserved.
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "Led_Sysfs.h"
19 
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <inttypes.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 
27 #include <AP_HAL/AP_HAL.h>
28 
29 static const AP_HAL::HAL &hal = AP_HAL::get_HAL();
30 
31 namespace Linux {
32 
33 Led_Sysfs::Led_Sysfs(const char *led_name)
34  : _led_name(led_name)
35 {
36 }
37 
39 {
40  if (_brightness_fd >= 0) {
42  }
43 }
44 
46 {
47  char *br_path;
48  char *max_br_path;
49 
50  if (asprintf(&br_path, "/sys/class/leds/%s/brightness", _led_name) == -1) {
51  AP_HAL::panic("LinuxLed_Sysfs : Couldn't allocate brightness path");
52  }
53 
54  _brightness_fd = open(br_path, O_WRONLY | O_CLOEXEC);
55  if (_brightness_fd < 0) {
56  printf("LinuxLed_Sysfs: Unable to open file %s\n", br_path);
57  free(br_path);
58  return false;
59  }
60 
61  if (asprintf(&max_br_path, "/sys/class/leds/%s/max_brightness", _led_name) == -1) {
62  AP_HAL::panic("LinuxLed_Sysfs : Couldn't allocate max_brightness path");
63  }
64 
65  if (Util::from(hal.util)->read_file(max_br_path, "%u", &_max_brightness) < 0) {
66  AP_HAL::panic("LinuxLed_Sysfs : Unable to read max_brightness in %s",
67  max_br_path);
68  }
69 
70  free(max_br_path);
71  free(br_path);
72 
73  return true;
74 }
75 
76 bool Led_Sysfs::set_brightness(uint8_t brightness)
77 {
78  if (_brightness_fd < 0) {
79  return false;
80  }
81 
82  unsigned int br = brightness * _max_brightness / UINT8_MAX;
83 
84  /* Don't log fails since this could spam the console */
85  if (dprintf(_brightness_fd, "%u", br) < 0) {
86  return false;
87  }
88 
89  return true;
90 }
91 
92 }
int printf(const char *fmt,...)
Definition: stdio.c:113
bool set_brightness(uint8_t brightness)
Definition: Led_Sysfs.cpp:76
const char * _led_name
Definition: Led_Sysfs.h:37
static const AP_HAL::HAL & hal
Definition: Led_Sysfs.cpp:29
int open(const char *pathname, int flags)
POSIX Open a file with integer mode flags.
Definition: posix.c:885
AP_HAL::Util * util
Definition: HAL.h:115
int int read_file(const char *path, const char *fmt,...) FMT_SCANF(3
Definition: Util.cpp:151
static Util * from(AP_HAL::Util *util)
Definition: Util.h:27
int close(int fileno)
POSIX Close a file with fileno handel.
Definition: posix.c:675
const HAL & get_HAL()
void free(void *ptr)
Definition: malloc.c:81
Led_Sysfs(const char *led_name)
Definition: Led_Sysfs.cpp:33
int asprintf(char **strp, const char *fmt,...)
Definition: stdio.c:91
void panic(const char *errormsg,...) FMT_PRINTF(1
Definition: system.cpp:140