diff --git a/include/communication.h b/include/communication.h new file mode 100644 index 0000000..e53a2db --- /dev/null +++ b/include/communication.h @@ -0,0 +1,6 @@ +#include + +bool ping(); +void connect(int port); +void sendcmd(const char* cmd, ...); +void ioctl(...) \ No newline at end of file diff --git a/include/systemapicommunication/communication.c b/include/systemapicommunication/communication.c new file mode 100644 index 0000000..1fa099e --- /dev/null +++ b/include/systemapicommunication/communication.c @@ -0,0 +1,85 @@ +#include "../communication.h" +#include +#include +#include +#include +#include +#include +#include + +static int sock = -1; + +void connect(int port) { + struct sockaddr_in server; + + sock = socket(AF_INET, SOCK_STREAM, 0); + if (sock < 0) { + perror("socket"); + return; + } + + server.sin_family = AF_INET; + server.sin_port = htons(port); + server.sin_addr.s_addr = inet_addr("127.0.0.1"); + + if (connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0) { + perror("connect"); + close(sock); + sock = -1; + return; + } + + printf("Connected to server on port %d\n", port); + +} + +void sendcmd(const char* cmd, ...) { + if (sock == -1) { + printf("Not connected to server\n"); + return; + } + + char buffer[4096]; + + va_list args; + va_start(args, cmd); + vsnprintf(buffer, sizeof(buffer), cmd, args); + va_end(args); + + send(sock, buffer, strlen(buffer), 0); +} + + +void ioctl(...) { + sendcmd("ioctl", ...) +} + +bool ping() { + sendcmd( + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + "\r\n" + ); + + char buffer[4096]; + ssize_t bytes = recv(sock, buffer, sizeof(buffer), 0); + + if (bytes <= 0) { + printf("No response from server\n"); + return 0; + } + + buffer[bytes] = '\0'; + + if (strstr(buffer, "Pong") != NULL) { + if (output) { + printf("Server replied with Pong\n"); + return 1; + } + } else { + if (output) { + printf("Unexpected response:\n%s\n", buffer); + return 0; + } + } +} \ No newline at end of file diff --git a/input/gamepad.c b/input/gamepad.c index 08d2991..d6e5e30 100644 --- a/input/gamepad.c +++ b/input/gamepad.c @@ -18,7 +18,7 @@ #include #include #include -#include +#include "../libfinite/libfinite/include/communication.h" //replaces ioctl has the same stuff #include #include #include diff --git a/mailroom/src/core.c b/mailroom/src/core.c index b3a6422..cf29410 100644 --- a/mailroom/src/core.c +++ b/mailroom/src/core.c @@ -4,5 +4,5 @@ int main() { init_log(stdout, LOG_LEVEL_DEBUG, false); - FINITE_LOG("Test"); + FINITE_LOG("Test"); }