From fe5629fa85ad7ae754b99a1b3356cf7bb63c9afc Mon Sep 17 00:00:00 2001 From: Ulrich Mohr Date: Fri, 2 Aug 2024 01:05:11 +0200 Subject: [PATCH] switched to UDP from UNIX Socket --- bsp_linux/hardware/hardware.c | 115 ++++++++++++++++++++++++++++++---- bsp_linux/main.c | 28 ++++++--- bsp_z7/newlib/write.c | 1 + mission/mission.c | 2 +- 4 files changed, 126 insertions(+), 20 deletions(-) diff --git a/bsp_linux/hardware/hardware.c b/bsp_linux/hardware/hardware.c index 973dcd6..602884a 100644 --- a/bsp_linux/hardware/hardware.c +++ b/bsp_linux/hardware/hardware.c @@ -1,28 +1,121 @@ +#include +#include +#include +#include #include #include #include #include #include -#include #include #include -extern const char *device_root; +extern const char *sim_ip; +extern int ai_family; + +int compare_string_chars(const char *c_string, const char *chars, + size_t chars_len) { + for (int i = 0; i < chars_len; i++) { + if (c_string[i] == 0) { + return 0; + } + if (c_string[i] != chars[i]) { + ; + return 0; + } + } + return 1; +} + +const char *get_port_number(const char *path, size_t path_len) { + if (compare_string_chars("uart0", path, path_len) == 1) { + return "8100"; + } + if (compare_string_chars("uart1", path, path_len) == 1) { + return "8101"; + } + return NULL; +} int hw_device_open(const char *path, size_t path_len) { + const char *port_number = get_port_number(path, path_len); + if (port_number == NULL) { + return -1; + } + + // struct sockaddr_in6 sin6; + // memset(&sin6, 0, sizeof(sin6)); + // sin6.sin6_family = AF_INET6; + // sin6.sin6_port = htons(port_number); + + // struct sockaddr_in sin4; + // memset(&sin4, 0, sizeof(sin4)); + // sin4.sin_family = AF_INET; + // sin4.sin_port = htons(port_number); + + // int result = inet_pton(AF_INET, sim_ip, &sin4.sin_addr); + + // int sock; + // const struct sockaddr *addr = NULL; + // socklen_t addrlen = 0; + // if (result == 1) { + // if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { + // perror("hw_device_open, creating socket failed"); + // exit(-1); + // } + // addr = (struct sockaddr *)&sin4; + // addrlen = sizeof(sin4); + // } + + // result = inet_pton(AF_INET6, sim_ip, &sin6.sin6_addr); + // if ((result == 1) && (addr == NULL)) { + // if ((sock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { + // perror("hw_device_open, creating socket failed"); + // exit(-1); + // } + // addr = (struct sockaddr *)&sin6; + // addrlen = sizeof(sin6); + // } + + struct addrinfo addrinfo_hint; + addrinfo_hint.ai_flags = AI_ADDRCONFIG; + addrinfo_hint.ai_family = ai_family; + addrinfo_hint.ai_socktype = SOCK_DGRAM; + addrinfo_hint.ai_protocol = 0; + addrinfo_hint.ai_addrlen = 0; + addrinfo_hint.ai_addr = NULL; + addrinfo_hint.ai_canonname = NULL; + addrinfo_hint.ai_next = NULL; + int sock; - if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) { - perror(NULL); + struct addrinfo *addr_candidates, *current_candidate; + + int result = + getaddrinfo(sim_ip, port_number, &addrinfo_hint, &addr_candidates); + if (result != 0) { + fprintf(stderr, "hw_device_open, reading sim address: %s\n", + gai_strerror(result)); 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); + + for (current_candidate = addr_candidates; current_candidate != NULL; + current_candidate = current_candidate->ai_next) { + sock = socket(current_candidate->ai_family, current_candidate->ai_socktype, + current_candidate->ai_protocol); + if (sock == -1) + continue; + + if (connect(sock, current_candidate->ai_addr, current_candidate->ai_addrlen) != -1) + break; /* Success */ + + close(sock); } + + if (current_candidate == NULL) { /* No address succeeded */ + fprintf(stderr, "hw_device_open, invalid sim address\n"); + exit(EXIT_FAILURE); + } + return sock; } \ No newline at end of file diff --git a/bsp_linux/main.c b/bsp_linux/main.c index 4b81135..7fa190d 100644 --- a/bsp_linux/main.c +++ b/bsp_linux/main.c @@ -4,11 +4,13 @@ #include #include #include +#include #include #include #include -const char *device_root = "./"; +const char *sim_ip = "localhost"; +int ai_family = AF_UNSPEC; void mission(void); @@ -25,28 +27,38 @@ int test_socket(); // to break anything ¯\_(ツ)_/¯ void rust_eh_personality() { puts("eh_personality"); } +void print_usage(const char * name) { +fprintf(stderr, "Usage: %s [-s sim_ip] [-4|6]\n", name); +} + int main(int argc, char **argv) { static struct option long_options[] = { /* NAME ARGUMENT FLAG SHORTNAME */ - {"device-root", required_argument, NULL, 'd'}, + {"sim_ip", required_argument, NULL, 's'}, {"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)) != + while ((c = getopt_long(argc, argv, "46hs:", long_options, &option_index)) != -1) { switch (c) { - case 'd': + case 's': if (optarg != NULL) { - device_root = optarg; + sim_ip = optarg; + break; + case '4': + ai_family = AF_INET; + break; + case '6': + ai_family = AF_INET6; break; default: - fprintf(stderr, "Usage: %s -d device-root\n", argv[0]); + print_usage(argv[0]); exit(EXIT_FAILURE); } } - } - + } + mission(); return 0; } \ No newline at end of file diff --git a/bsp_z7/newlib/write.c b/bsp_z7/newlib/write.c index e388653..7ded118 100644 --- a/bsp_z7/newlib/write.c +++ b/bsp_z7/newlib/write.c @@ -15,6 +15,7 @@ int _write(int fd, const char *ptr, int len) { if (ptr == NULL) { return 0; } + //TODO check len // 0 is stdin, do not write to it if (fd < 1) { diff --git a/mission/mission.c b/mission/mission.c index 02d330e..a4843f2 100644 --- a/mission/mission.c +++ b/mission/mission.c @@ -18,7 +18,7 @@ void test_hardware() { int fd1 = hw_device_open("uart1", 5); write(fd1, "uart1\n", 6); - vTaskDelay(3000 / portTICK_PERIOD_MS); + vTaskDelay(1 / portTICK_PERIOD_MS); uint8_t buffer[100]; int read_bytes = read(fd0, buffer, sizeof(buffer));