portable device access api based on unix file descriptors

This commit is contained in:
2024-07-20 00:27:24 +02:00
parent 49c19ba675
commit cbe8184fab
23 changed files with 241 additions and 133 deletions

View File

@ -0,0 +1,28 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
#include <hardware/interfaces.h>
extern const char *device_root;
int hw_device_open(const char *path, size_t path_len) {
int sock;
if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
perror(NULL);
exit(-1);
}
struct sockaddr_un address;
address.sun_family = AF_UNIX;
snprintf(address.sun_path, sizeof(address.sun_path), "%s%.*s", device_root,
path_len, path);
if (connect(sock, (struct sockaddr *)&address, sizeof(address)) != 0) {
perror(NULL);
exit(-1);
}
return sock;
}