add function write_file() (#2181)

* add function write_file()

* handle EAGIN & EWOULDBLOCK

* don't  handle errno
old-commit-hash: 6476207009
commatwo_master
Dean Lee 5 years ago committed by GitHub
parent e9642080ac
commit ef8b6d20fb
  1. 11
      selfdrive/common/framebuffer.cc
  2. 60
      selfdrive/common/gpio.cc
  3. 13
      selfdrive/common/util.c
  4. 1
      selfdrive/common/util.h

@ -1,3 +1,4 @@
#include "util.h"
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <cassert> #include <cassert>
@ -37,13 +38,9 @@ extern "C" void framebuffer_swap(FramebufferState *s) {
} }
extern "C" bool set_brightness(int brightness) { extern "C" bool set_brightness(int brightness) {
FILE *f = fopen("/sys/class/leds/lcd-backlight/brightness", "wb"); char bright[64];
if (f != NULL) { snprintf(bright, sizeof(bright), "%d", brightness);
fprintf(f, "%d", brightness); return 0 == write_file("/sys/class/leds/lcd-backlight/brightness", bright, strlen(bright));
fclose(f);
return true;
}
return false;
} }
extern "C" void framebuffer_set_power(FramebufferState *s, int mode) { extern "C" void framebuffer_set_power(FramebufferState *s, int mode) {

@ -1,75 +1,29 @@
#include "gpio.h" #include "gpio.h"
#include "util.h"
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <stdio.h> #include <string.h>
// We assume that all pins have already been exported on boot, // We assume that all pins have already been exported on boot,
// and that we have permission to write to them. // and that we have permission to write to them.
int gpio_init(int pin_nr, bool output){ int gpio_init(int pin_nr, bool output){
int ret = 0;
int fd = -1, tmp;
char pin_dir_path[50]; char pin_dir_path[50];
int pin_dir_path_len = snprintf(pin_dir_path, sizeof(pin_dir_path), int pin_dir_path_len = snprintf(pin_dir_path, sizeof(pin_dir_path),
"/sys/class/gpio/gpio%d/direction", pin_nr); "/sys/class/gpio/gpio%d/direction", pin_nr);
if(pin_dir_path_len <= 0){ if(pin_dir_path_len <= 0){
ret = -1; return -1;
goto cleanup;
}
fd = open(pin_dir_path, O_WRONLY);
if(fd == -1){
ret = -1;
goto cleanup;
}
if(output){
tmp = write(fd, "out", 3);
if(tmp != 3){
ret = -1;
goto cleanup;
}
} else {
tmp = write(fd, "in", 2);
if(tmp != 2){
ret = -1;
goto cleanup;
}
}
cleanup:
if(fd >= 0){
close(fd);
} }
return ret; const char *value = output ? "out" : "in";
return write_file(pin_dir_path, (void*)value, strlen(value));
} }
int gpio_set(int pin_nr, bool high){ int gpio_set(int pin_nr, bool high){
int ret = 0;
int fd = -1, tmp;
char pin_val_path[50]; char pin_val_path[50];
int pin_val_path_len = snprintf(pin_val_path, sizeof(pin_val_path), int pin_val_path_len = snprintf(pin_val_path, sizeof(pin_val_path),
"/sys/class/gpio/gpio%d/value", pin_nr); "/sys/class/gpio/gpio%d/value", pin_nr);
if(pin_val_path_len <= 0){ if(pin_val_path_len <= 0){
ret = -1; return -1;
goto cleanup;
}
fd = open(pin_val_path, O_WRONLY);
if(fd == -1){
ret = -1;
goto cleanup;
}
tmp = write(fd, high ? "1" : "0", 1);
if(tmp != 1){
ret = -1;
goto cleanup;
}
cleanup:
if(fd >= 0){
close(fd);
} }
return ret; return write_file(pin_val_path, (void*)(high ? "1" : "0"), 1);
} }

@ -3,7 +3,8 @@
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#ifdef __linux__ #ifdef __linux__
#include <sys/prctl.h> #include <sys/prctl.h>
#include <sys/syscall.h> #include <sys/syscall.h>
@ -41,6 +42,16 @@ void* read_file(const char* path, size_t* out_len) {
return buf; return buf;
} }
int write_file(const char* path, const void* data, size_t size) {
int fd = open(path, O_WRONLY);
if (fd == -1) {
return -1;
}
ssize_t n = write(fd, data, size);
close(fd);
return n == size ? 0 : -1;
}
void set_thread_name(const char* name) { void set_thread_name(const char* name) {
#ifdef __linux__ #ifdef __linux__
// pthread_setname_np is dumb (fails instead of truncates) // pthread_setname_np is dumb (fails instead of truncates)

@ -41,6 +41,7 @@ extern "C" {
// Returns NULL on failure, otherwise the NULL-terminated file contents. // Returns NULL on failure, otherwise the NULL-terminated file contents.
// The result must be freed by the caller. // The result must be freed by the caller.
void* read_file(const char* path, size_t* out_len); void* read_file(const char* path, size_t* out_len);
int write_file(const char* path, const void* data, size_t size);
void set_thread_name(const char* name); void set_thread_name(const char* name);

Loading…
Cancel
Save