forked from ROMEO/obsw
portable device access api based on unix file descriptors
This commit is contained in:
@ -1 +1,3 @@
|
||||
add_subdirectory(hardware)
|
||||
|
||||
target_sources(${TARGET_NAME} PRIVATE main.c)
|
1
bsp_linux/hardware/CMakeLists.txt
Normal file
1
bsp_linux/hardware/CMakeLists.txt
Normal file
@ -0,0 +1 @@
|
||||
target_sources(${TARGET_NAME} PRIVATE hardware.c)
|
28
bsp_linux/hardware/hardware.c
Normal file
28
bsp_linux/hardware/hardware.c
Normal 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;
|
||||
}
|
@ -1,27 +1,52 @@
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <getopt.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
const char *device_root = "./";
|
||||
|
||||
void mission(void);
|
||||
|
||||
int get_descriptor_rw() {
|
||||
return 1;
|
||||
}
|
||||
int get_descriptor_rw() { return 1; }
|
||||
|
||||
void done() {
|
||||
printf("done.\n");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
int test_socket();
|
||||
|
||||
// Don't ask me, it makes the linker happy and does not seem
|
||||
// to break anything ¯\_(ツ)_/¯
|
||||
void rust_eh_personality() {
|
||||
puts("eh_personality");
|
||||
}
|
||||
void rust_eh_personality() { puts("eh_personality"); }
|
||||
|
||||
int main(void) {
|
||||
mission();
|
||||
return 0;
|
||||
int main(int argc, char **argv) {
|
||||
static struct option long_options[] = {
|
||||
/* NAME ARGUMENT FLAG SHORTNAME */
|
||||
{"device-root", required_argument, NULL, 'd'},
|
||||
{"help", no_argument, NULL, 'h'},
|
||||
{NULL, 0, NULL, 0}};
|
||||
int c;
|
||||
int option_index = 0;
|
||||
while ((c = getopt_long(argc, argv, "hd:", long_options, &option_index)) !=
|
||||
-1) {
|
||||
switch (c) {
|
||||
case 'd':
|
||||
if (optarg != NULL) {
|
||||
device_root = optarg;
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "Usage: %s -d device-root\n", argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mission();
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user