forked from ROMEO/obsw
switched to UDP from UNIX Socket
This commit is contained in:
parent
926ef26000
commit
fe5629fa85
@ -1,28 +1,121 @@
|
|||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/un.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include <hardware/interfaces.h>
|
#include <hardware/interfaces.h>
|
||||||
|
|
||||||
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) {
|
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;
|
int sock;
|
||||||
if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
|
struct addrinfo *addr_candidates, *current_candidate;
|
||||||
perror(NULL);
|
|
||||||
|
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);
|
exit(-1);
|
||||||
}
|
}
|
||||||
struct sockaddr_un address;
|
|
||||||
address.sun_family = AF_UNIX;
|
for (current_candidate = addr_candidates; current_candidate != NULL;
|
||||||
snprintf(address.sun_path, sizeof(address.sun_path), "%s%.*s", device_root,
|
current_candidate = current_candidate->ai_next) {
|
||||||
path_len, path);
|
sock = socket(current_candidate->ai_family, current_candidate->ai_socktype,
|
||||||
if (connect(sock, (struct sockaddr *)&address, sizeof(address)) != 0) {
|
current_candidate->ai_protocol);
|
||||||
perror(NULL);
|
if (sock == -1)
|
||||||
exit(-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;
|
return sock;
|
||||||
}
|
}
|
@ -4,11 +4,13 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
const char *device_root = "./";
|
const char *sim_ip = "localhost";
|
||||||
|
int ai_family = AF_UNSPEC;
|
||||||
|
|
||||||
void mission(void);
|
void mission(void);
|
||||||
|
|
||||||
@ -25,27 +27,37 @@ int test_socket();
|
|||||||
// to break anything ¯\_(ツ)_/¯
|
// to break anything ¯\_(ツ)_/¯
|
||||||
void rust_eh_personality() { puts("eh_personality"); }
|
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) {
|
int main(int argc, char **argv) {
|
||||||
static struct option long_options[] = {
|
static struct option long_options[] = {
|
||||||
/* NAME ARGUMENT FLAG SHORTNAME */
|
/* NAME ARGUMENT FLAG SHORTNAME */
|
||||||
{"device-root", required_argument, NULL, 'd'},
|
{"sim_ip", required_argument, NULL, 's'},
|
||||||
{"help", no_argument, NULL, 'h'},
|
{"help", no_argument, NULL, 'h'},
|
||||||
{NULL, 0, NULL, 0}};
|
{NULL, 0, NULL, 0}};
|
||||||
int c;
|
int c;
|
||||||
int option_index = 0;
|
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) {
|
-1) {
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 'd':
|
case 's':
|
||||||
if (optarg != NULL) {
|
if (optarg != NULL) {
|
||||||
device_root = optarg;
|
sim_ip = optarg;
|
||||||
|
break;
|
||||||
|
case '4':
|
||||||
|
ai_family = AF_INET;
|
||||||
|
break;
|
||||||
|
case '6':
|
||||||
|
ai_family = AF_INET6;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
fprintf(stderr, "Usage: %s -d device-root\n", argv[0]);
|
print_usage(argv[0]);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mission();
|
mission();
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -15,6 +15,7 @@ int _write(int fd, const char *ptr, int len) {
|
|||||||
if (ptr == NULL) {
|
if (ptr == NULL) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
//TODO check len
|
||||||
|
|
||||||
// 0 is stdin, do not write to it
|
// 0 is stdin, do not write to it
|
||||||
if (fd < 1) {
|
if (fd < 1) {
|
||||||
|
@ -18,7 +18,7 @@ void test_hardware() {
|
|||||||
int fd1 = hw_device_open("uart1", 5);
|
int fd1 = hw_device_open("uart1", 5);
|
||||||
write(fd1, "uart1\n", 6);
|
write(fd1, "uart1\n", 6);
|
||||||
|
|
||||||
vTaskDelay(3000 / portTICK_PERIOD_MS);
|
vTaskDelay(1 / portTICK_PERIOD_MS);
|
||||||
|
|
||||||
uint8_t buffer[100];
|
uint8_t buffer[100];
|
||||||
int read_bytes = read(fd0, buffer, sizeof(buffer));
|
int read_bytes = read(fd0, buffer, sizeof(buffer));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user