feature: Switch console logging to Unix sockets

This commit is contained in:
Tera << 8 2025-06-10 11:15:53 -04:00
parent 682daf4385
commit 258b7c4f26
Signed by: imterah
GPG key ID: 8FA7DD57BA6CEA37
3 changed files with 100 additions and 55 deletions

0
C Normal file
View file

View file

@ -8,6 +8,10 @@ find_package(json-c REQUIRED CONFIG)
add_subdirectory(modules/hidapi) add_subdirectory(modules/hidapi)
add_subdirectory(modules/Fusion/Fusion) add_subdirectory(modules/Fusion/Fusion)
if (CMAKE_BUILD_TYPE STREQUAL "Release")
add_definitions(-DNDEBUG)
endif()
add_library( add_library(
xrealAirLibrary xrealAirLibrary
src/crc32.c src/crc32.c
@ -24,7 +28,7 @@ target_include_directories(xrealAirLibrary
) )
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/hidapi
${CMAKE_CURRENT_SOURCE_DIR}/modules/Fusion ${CMAKE_CURRENT_SOURCE_DIR}/modules/Fusion
) )

View file

@ -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) 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 // Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal // 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 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is // copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions: // furnished to do so, subject to the following conditions:
// //
// The above copyright notice and this permission notice shall be included in // The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software. // all copies or substantial portions of the Software.
// //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@ -25,68 +26,79 @@
#include "device_imu.h" #include "device_imu.h"
#include "device_mcu.h" #include "device_mcu.h"
#include <stdio.h> #include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <sys/un.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h> #include <math.h>
void test_imu(uint64_t timestamp, enum XrealDriverCommand {
device_imu_event_type event, ROLL,
const device_imu_ahrs_type* ahrs) { PITCH,
static device_imu_quat_type old; YAW,
static float dmax = -1.0f; 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) { if (event != DEVICE_IMU_EVENT_UPDATE) {
return; return;
} }
device_imu_quat_type q = device_imu_get_orientation(ahrs); 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); device_imu_euler_type e = device_imu_get_euler(q);
if (d >= 0.00005f) { char status_event = 0;
device_imu_euler_type e0 = device_imu_get_euler(old); uint32_t encoded_data;
printf("Roll: %f; Pitch: %f; Yaw: %f\n", e0.roll, e0.pitch, e0.yaw); status_event = ROLL;
printf("Roll: %f; Pitch: %f; Yaw: %f\n", e.roll, e.pitch, e.yaw); memcpy(&encoded_data, &e.roll, sizeof(encoded_data));
printf("D = %f; ( %f )\n", d, dmax); encoded_data = htonl(encoded_data);
write(sock_fd, &status_event, 1);
printf("X: %f; Y: %f; Z: %f; W: %f;\n", old.x, old.y, old.z, old.w); write(sock_fd, &encoded_data, sizeof(encoded_data));
printf("X: %f; Y: %f; Z: %f; W: %f;\n", q.x, q.y, q.z, q.w);
} else { status_event = PITCH;
printf("Roll: %.2f; Pitch: %.2f; Yaw: %.2f\n", e.roll, e.pitch, e.yaw); memcpy(&encoded_data, &e.pitch, sizeof(encoded_data));
} encoded_data = htonl(encoded_data);
write(sock_fd, &status_event, 1);
old = q; 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, void mcu_event(uint64_t timestamp, device_mcu_event_type event, uint8_t brightness, const char* msg) {
device_mcu_event_type event, char status_event = 0;
uint8_t brightness,
const char* msg) {
switch (event) { switch (event) {
case DEVICE_MCU_EVENT_MESSAGE: 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; break;
case DEVICE_MCU_EVENT_BRIGHTNESS_UP: 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; break;
case DEVICE_MCU_EVENT_BRIGHTNESS_DOWN: 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; break;
default: default:
break; break;
@ -94,42 +106,71 @@ void test_mcu(uint64_t timestamp,
} }
int main(int argc, const char** argv) { 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(); pid_t pid = fork();
if (pid == -1) { if (pid == -1) {
perror("Could not fork!\n"); perror("Could not fork!\n");
return 1; return 1;
} }
if (pid == 0) { if (pid == 0) {
device_imu_type dev_imu; 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; return 1;
} }
printf("Clearing and calibrating IMU\n");
device_imu_clear(&dev_imu); device_imu_clear(&dev_imu);
device_imu_calibrate(&dev_imu, 1000, true, true, false); 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)); while (DEVICE_IMU_ERROR_NO_ERROR == device_imu_read(&dev_imu, -1));
device_imu_close(&dev_imu); device_imu_close(&dev_imu);
return 0; return 0;
} else { } else {
int status = 0; int status = 0;
device_mcu_type dev_mcu; 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; status = 1;
goto exit; goto exit;
} }
printf("Reading MCU data\n");
device_mcu_clear(&dev_mcu); device_mcu_clear(&dev_mcu);
while (DEVICE_MCU_ERROR_NO_ERROR == device_mcu_read(&dev_mcu, -1)); while (DEVICE_MCU_ERROR_NO_ERROR == device_mcu_read(&dev_mcu, -1));
device_mcu_close(&dev_mcu); device_mcu_close(&dev_mcu);
exit: exit:
if (pid != waitpid(pid, &status, 0)) { if (pid != waitpid(pid, &status, 0)) {
return 1; return 1;
} }
return status; return status;
} }
} }