From 258b7c4f26343099fda984b82e18a8888bfd7438 Mon Sep 17 00:00:00 2001 From: imterah Date: Tue, 10 Jun 2025 11:15:53 -0400 Subject: [PATCH] feature: Switch console logging to Unix sockets --- C | 0 interface_lib/CMakeLists.txt | 6 +- src/driver.c | 149 ++++++++++++++++++++++------------- 3 files changed, 100 insertions(+), 55 deletions(-) create mode 100644 C diff --git a/C b/C new file mode 100644 index 0000000..e69de29 diff --git a/interface_lib/CMakeLists.txt b/interface_lib/CMakeLists.txt index 07aeace..2fcb2d6 100644 --- a/interface_lib/CMakeLists.txt +++ b/interface_lib/CMakeLists.txt @@ -8,6 +8,10 @@ find_package(json-c REQUIRED CONFIG) add_subdirectory(modules/hidapi) add_subdirectory(modules/Fusion/Fusion) +if (CMAKE_BUILD_TYPE STREQUAL "Release") + add_definitions(-DNDEBUG) +endif() + add_library( xrealAirLibrary src/crc32.c @@ -24,7 +28,7 @@ target_include_directories(xrealAirLibrary ) target_include_directories(xrealAirLibrary - SYSTEM BEFORE PRIVATE + SYSTEM BEFORE PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/modules/hidapi ${CMAKE_CURRENT_SOURCE_DIR}/modules/Fusion ) diff --git a/src/driver.c b/src/driver.c index 4d6b433..57b269e 100644 --- a/src/driver.c +++ b/src/driver.c @@ -1,7 +1,8 @@ // -// Created by thejackimonster on 29.03.23. +// Created by thejackimonster on 29.03.23. Modified by UnrealXR on 08.06.25. // // Copyright (c) 2023-2024 thejackimonster. All rights reserved. +// Copyright (c) 2025 UnrealXR. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -9,10 +10,10 @@ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. -// +// // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE @@ -25,68 +26,79 @@ #include "device_imu.h" #include "device_mcu.h" -#include +#include +#include #include +#include #include - +#include +#include #include -void test_imu(uint64_t timestamp, - device_imu_event_type event, - const device_imu_ahrs_type* ahrs) { - static device_imu_quat_type old; - static float dmax = -1.0f; - +enum XrealDriverCommand { + ROLL, + PITCH, + YAW, + GENERIC_MESSAGE, + BRIGHTNESS_UP, + BRIGHTNESS_DOWN, +}; + +int sock_fd; + +void imu_event(uint64_t timestamp, device_imu_event_type event, const device_imu_ahrs_type* ahrs) { if (event != DEVICE_IMU_EVENT_UPDATE) { return; } - + device_imu_quat_type q = device_imu_get_orientation(ahrs); - - const float dx = (old.x - q.x) * (old.x - q.x); - const float dy = (old.y - q.y) * (old.y - q.y); - const float dz = (old.z - q.z) * (old.z - q.z); - const float dw = (old.w - q.w) * (old.w - q.w); - - const float d = sqrtf(dx*dx + dy*dy + dz*dz + dw*dw); - - if (dmax < 0.0f) { - dmax = 0.0f; - } else { - dmax = (d > dmax? d : dmax); - } - device_imu_euler_type e = device_imu_get_euler(q); - - if (d >= 0.00005f) { - device_imu_euler_type e0 = device_imu_get_euler(old); - - printf("Roll: %f; Pitch: %f; Yaw: %f\n", e0.roll, e0.pitch, e0.yaw); - printf("Roll: %f; Pitch: %f; Yaw: %f\n", e.roll, e.pitch, e.yaw); - printf("D = %f; ( %f )\n", d, dmax); - - printf("X: %f; Y: %f; Z: %f; W: %f;\n", old.x, old.y, old.z, old.w); - printf("X: %f; Y: %f; Z: %f; W: %f;\n", q.x, q.y, q.z, q.w); - } else { - printf("Roll: %.2f; Pitch: %.2f; Yaw: %.2f\n", e.roll, e.pitch, e.yaw); - } - - old = q; + + char status_event = 0; + uint32_t encoded_data; + + status_event = ROLL; + memcpy(&encoded_data, &e.roll, sizeof(encoded_data)); + encoded_data = htonl(encoded_data); + write(sock_fd, &status_event, 1); + write(sock_fd, &encoded_data, sizeof(encoded_data)); + + status_event = PITCH; + memcpy(&encoded_data, &e.pitch, sizeof(encoded_data)); + encoded_data = htonl(encoded_data); + write(sock_fd, &status_event, 1); + write(sock_fd, &encoded_data, sizeof(encoded_data)); + + status_event = YAW; + memcpy(&encoded_data, &e.yaw, sizeof(encoded_data)); + encoded_data = htonl(encoded_data); + write(sock_fd, &status_event, 1); + write(sock_fd, &encoded_data, sizeof(encoded_data)); } -void test_mcu(uint64_t timestamp, - device_mcu_event_type event, - uint8_t brightness, - const char* msg) { +void mcu_event(uint64_t timestamp, device_mcu_event_type event, uint8_t brightness, const char* msg) { + char status_event = 0; + switch (event) { case DEVICE_MCU_EVENT_MESSAGE: - printf("Message: `%s`\n", msg); + status_event = GENERIC_MESSAGE; + write(sock_fd, &status_event, 1); + + uint32_t msg_len = strlen(msg); + uint32_t len_net = htonl(msg_len); + + write(sock_fd, &len_net, sizeof(len_net)); + write(sock_fd, msg, msg_len); break; case DEVICE_MCU_EVENT_BRIGHTNESS_UP: - printf("Increase Brightness: %u\n", brightness); + status_event = BRIGHTNESS_UP; + write(sock_fd, &status_event, 1); + write(sock_fd, &brightness, 1); break; case DEVICE_MCU_EVENT_BRIGHTNESS_DOWN: - printf("Decrease Brightness: %u\n", brightness); + status_event = BRIGHTNESS_DOWN; + write(sock_fd, &status_event, 1); + write(sock_fd, &brightness, 1); break; default: break; @@ -94,42 +106,71 @@ void test_mcu(uint64_t timestamp, } int main(int argc, const char** argv) { + printf("Connecting to Unix socket\n"); + char *unix_socket_path = getenv("UNREALXR_NREAL_DRIVER_SOCK"); + + if (unix_socket_path == NULL) { + printf("Unix socket not set!\n"); + return 1; + } + + sock_fd = socket(AF_UNIX, SOCK_STREAM, 0); + + struct sockaddr_un server_addr; + server_addr.sun_family = AF_UNIX; + strcpy(server_addr.sun_path, unix_socket_path); + + int connection_result = connect(sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)); + + if (connection_result == -1) { + perror("Failed to connect to Unix socket"); + exit(1); + } + + printf("Connected to Unix socket\n"); + pid_t pid = fork(); - + if (pid == -1) { perror("Could not fork!\n"); return 1; } - + if (pid == 0) { device_imu_type dev_imu; - if (DEVICE_IMU_ERROR_NO_ERROR != device_imu_open(&dev_imu, test_imu)) { + if (DEVICE_IMU_ERROR_NO_ERROR != device_imu_open(&dev_imu, imu_event)) { return 1; } + printf("Clearing and calibrating IMU\n"); device_imu_clear(&dev_imu); device_imu_calibrate(&dev_imu, 1000, true, true, false); + printf("Finished IMU tasks. Beginning data read\n"); + while (DEVICE_IMU_ERROR_NO_ERROR == device_imu_read(&dev_imu, -1)); device_imu_close(&dev_imu); + return 0; } else { int status = 0; device_mcu_type dev_mcu; - if (DEVICE_MCU_ERROR_NO_ERROR != device_mcu_open(&dev_mcu, test_mcu)) { + if (DEVICE_MCU_ERROR_NO_ERROR != device_mcu_open(&dev_mcu, mcu_event)) { status = 1; goto exit; } + printf("Reading MCU data\n"); + device_mcu_clear(&dev_mcu); while (DEVICE_MCU_ERROR_NO_ERROR == device_mcu_read(&dev_mcu, -1)); device_mcu_close(&dev_mcu); - + exit: if (pid != waitpid(pid, &status, 0)) { return 1; } - + return status; } }