2024-09-19 15:44:34 +00:00
|
|
|
// A lot of this code is made because of the work of https://github.com/capull0/SkyDumper
|
|
|
|
|
2024-09-18 13:30:30 +00:00
|
|
|
#include "SkyPortalSubsystem.h"
|
|
|
|
#include "Engine/Engine.h"
|
2024-09-19 12:03:16 +00:00
|
|
|
|
2024-09-19 15:44:34 +00:00
|
|
|
DEFINE_LOG_CATEGORY(LogHIDApi);
|
|
|
|
|
2024-09-18 13:30:30 +00:00
|
|
|
|
|
|
|
void USkyPortalSubsystem::Initialize(FSubsystemCollectionBase& Collection)
|
|
|
|
{
|
2024-09-19 12:03:16 +00:00
|
|
|
|
2024-09-18 13:30:30 +00:00
|
|
|
Super::Initialize(Collection);
|
|
|
|
// Custom initialization logic
|
|
|
|
UE_LOG(LogTemp, Warning, TEXT("SkyPortalSubsystem Initialized"));
|
2024-09-18 14:54:36 +00:00
|
|
|
|
2024-09-18 16:10:50 +00:00
|
|
|
// Initialize HIDAPI
|
|
|
|
int res = hid_init();
|
|
|
|
if (res == 0)
|
|
|
|
{
|
2024-09-19 15:44:34 +00:00
|
|
|
UE_LOG(LogHIDApi, Log, TEXT("HIDAPI initialized successfully."));
|
2024-09-19 12:03:16 +00:00
|
|
|
|
2024-09-18 16:10:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-09-19 15:44:34 +00:00
|
|
|
UE_LOG(LogHIDApi, Error, TEXT("Failed to initialize HIDAPI."));
|
2024-09-19 12:03:16 +00:00
|
|
|
HidError = hid_error(NULL);
|
2024-09-19 15:44:34 +00:00
|
|
|
UE_LOG(LogHIDApi, Error, TEXT("%s"), *HidError);
|
2024-09-18 16:10:50 +00:00
|
|
|
}
|
2024-09-18 14:54:36 +00:00
|
|
|
|
2024-09-18 13:30:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void USkyPortalSubsystem::Deinitialize()
|
|
|
|
{
|
2024-09-19 12:03:16 +00:00
|
|
|
// Disconnect portal
|
|
|
|
if (PortalDevice) {
|
|
|
|
hid_close(PortalDevice);
|
|
|
|
}
|
|
|
|
hid_exit();
|
|
|
|
|
2024-09-18 16:10:50 +00:00
|
|
|
|
2024-09-18 13:30:30 +00:00
|
|
|
UE_LOG(LogTemp, Warning, TEXT("SkyPortalSubsystem Deinitialized"));
|
|
|
|
Super::Deinitialize();
|
2024-09-19 12:03:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-09-19 15:44:34 +00:00
|
|
|
/*
|
|
|
|
Number of endpoints : 2
|
|
|
|
found an IN End Point 0 with attributes interrupt and address 0x1
|
|
|
|
found an OUT End Point 1 with attributes interrupt and address 0x1
|
|
|
|
*/
|
|
|
|
bool USkyPortalSubsystem::ConnectPortal() {
|
|
|
|
/*
|
|
|
|
Declare two pointers to hold information about HID devices.
|
|
|
|
"list" will point to the head of the linked list of devices,
|
|
|
|
"attributes" will be used to iterate through the list.
|
|
|
|
*/
|
|
|
|
struct hid_device_info* list, * attributes;
|
|
|
|
|
|
|
|
list = hid_enumerate(0x0, 0x0);
|
|
|
|
// If `list` is NULL, that means no devices were found or there was an error.
|
|
|
|
// In this case, print an error message and terminate the program.
|
|
|
|
if (!list) {
|
|
|
|
UE_LOG(LogHIDApi, Error, TEXT("No devices found"));
|
2024-09-19 12:03:16 +00:00
|
|
|
// Get the error message from the HIDAPI
|
|
|
|
HidError = hid_error(NULL);
|
2024-09-19 15:44:34 +00:00
|
|
|
UE_LOG(LogHIDApi, Error, TEXT("%s"), *HidError);
|
2024-09-19 12:03:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
2024-09-19 15:44:34 +00:00
|
|
|
attributes = list;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Iterate through the linked list of devices
|
|
|
|
int vendorCount = sizeof(VendorIds) / sizeof(VendorIds[0]);
|
|
|
|
int productCount = sizeof(ProductIds) / sizeof(ProductIds[0]);
|
|
|
|
|
|
|
|
while (attributes) {
|
|
|
|
// Check if the devices match any of vendor_id and product_id
|
|
|
|
for (int i = 0; i < vendorCount; i++) {
|
|
|
|
for (int j = 0; j < productCount; j++) {
|
|
|
|
if (attributes->vendor_id == VendorIds[i] && attributes->product_id == ProductIds[j]) {
|
|
|
|
UE_LOG(LogHIDApi, Display, TEXT("Portal found"));
|
|
|
|
UE_LOG(LogHIDApi, Log, TEXT("Vendor ID: 0x%x, Product ID: 0x%x"), attributes->vendor_id, attributes->product_id);
|
|
|
|
|
|
|
|
PortalDevice = hid_open(attributes->vendor_id, attributes->product_id, NULL);
|
|
|
|
if (PortalDevice) {
|
|
|
|
UE_LOG(LogHIDApi, Display, TEXT("Successful connection to Portal."));
|
|
|
|
|
|
|
|
// Free the device list
|
|
|
|
hid_free_enumeration(list);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move to the next device in the list
|
|
|
|
attributes = attributes->next;
|
|
|
|
}
|
2024-09-19 12:03:16 +00:00
|
|
|
|
2024-09-19 15:44:34 +00:00
|
|
|
// Free the device list
|
|
|
|
hid_free_enumeration(list);
|
2024-09-19 20:17:18 +00:00
|
|
|
UE_LOG(LogHIDApi, Error, TEXT("No Portals found"));
|
2024-09-19 15:44:34 +00:00
|
|
|
return false;
|
2024-09-19 12:03:16 +00:00
|
|
|
}
|
|
|
|
|
2024-09-19 15:44:34 +00:00
|
|
|
|
|
|
|
|
2024-09-19 12:03:16 +00:00
|
|
|
void USkyPortalSubsystem::ReadPortal() {
|
2024-09-19 15:44:34 +00:00
|
|
|
if (PortalDevice) {
|
|
|
|
hid_read(PortalDevice, PortalRawData, 16);
|
|
|
|
//RawDataArray.Append(PortalRawData, 16);
|
|
|
|
}
|
|
|
|
|
2024-09-19 12:03:16 +00:00
|
|
|
}
|
|
|
|
|