mirror of
https://github.com/Lorenzooone/cc3dsfs.git
synced 2026-09-12 02:25:28 -05:00
Refactor IS Nitro to IS Devices
This commit is contained in:
@@ -0,0 +1,446 @@
|
||||
#include "devicecapture.hpp"
|
||||
#include "usb_is_device_setup_general.hpp"
|
||||
#include "usb_is_device_libusb.hpp"
|
||||
#include "usb_is_device_is_driver.hpp"
|
||||
#include "usb_is_device_communications.hpp"
|
||||
#include "usb_is_device_acquisition.hpp"
|
||||
#include "usb_is_device_acquisition_general.hpp"
|
||||
#include "usb_is_nitro_acquisition_capture.hpp"
|
||||
#include "usb_is_nitro_acquisition_emulator.hpp"
|
||||
#include "usb_generic.hpp"
|
||||
|
||||
#include <libusb.h>
|
||||
#include <chrono>
|
||||
#include <cstring>
|
||||
|
||||
// Code based off of Gericom's sample code. Distributed under the MIT License. Copyright (c) 2024 Gericom
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// 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
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
// IN THE SOFTWARE.
|
||||
|
||||
#define SERIAL_NUMBER_SIZE (IS_DEVICE_REAL_SERIAL_NUMBER_SIZE + 1)
|
||||
#define MAX_TIME_WAIT 1.0
|
||||
#define FRAME_BUFFER_SIZE 32
|
||||
|
||||
static void is_device_read_frame_cb(void* user_data, int transfer_length, int transfer_status);
|
||||
|
||||
static bool initial_cleanup(const is_device_usb_device* usb_device_desc, is_device_device_handlers* handlers) {
|
||||
switch(usb_device_desc->device_type) {
|
||||
case IS_NITRO_EMULATOR_DEVICE:
|
||||
return initial_cleanup_emulator(usb_device_desc, handlers) != LIBUSB_SUCCESS;
|
||||
case IS_NITRO_CAPTURE_DEVICE:
|
||||
return initial_cleanup_capture(usb_device_desc, handlers) != LIBUSB_SUCCESS;
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
std::string get_serial(const is_device_usb_device* usb_device_desc, is_device_device_handlers* handlers, int& curr_serial_extra_id) {
|
||||
uint8_t data[SERIAL_NUMBER_SIZE];
|
||||
std::string serial_str = std::to_string(curr_serial_extra_id);
|
||||
bool conn_success = true;
|
||||
if(initial_cleanup(usb_device_desc, handlers))
|
||||
conn_success = false;
|
||||
if (conn_success && (GetDeviceSerial(handlers, data, usb_device_desc) != LIBUSB_SUCCESS))
|
||||
conn_success = false;
|
||||
if (conn_success) {
|
||||
data[IS_DEVICE_REAL_SERIAL_NUMBER_SIZE] = '\0';
|
||||
serial_str = std::string((const char*)data);
|
||||
}
|
||||
else
|
||||
curr_serial_extra_id += 1;
|
||||
return serial_str;
|
||||
}
|
||||
|
||||
void is_device_insert_device(std::vector<CaptureDevice>& devices_list, is_device_device_handlers* handlers, const is_device_usb_device* usb_device_desc, int& curr_serial_extra_id_is_device, std::string path) {
|
||||
devices_list.emplace_back(get_serial(usb_device_desc, handlers, curr_serial_extra_id_is_device), usb_device_desc->name, usb_device_desc->long_name, path, CAPTURE_CONN_IS_NITRO, (void*)usb_device_desc, false, false, false, WIDTH_DS, HEIGHT_DS + HEIGHT_DS, 0, 0, 0, 0, 0, HEIGHT_DS, usb_device_desc->video_data_type);
|
||||
}
|
||||
|
||||
void is_device_insert_device(std::vector<CaptureDevice>& devices_list, is_device_device_handlers* handlers, const is_device_usb_device* usb_device_desc, int& curr_serial_extra_id_is_device) {
|
||||
devices_list.emplace_back(get_serial(usb_device_desc, handlers, curr_serial_extra_id_is_device), usb_device_desc->name, usb_device_desc->long_name, CAPTURE_CONN_IS_NITRO, (void*)usb_device_desc, false, false, false, WIDTH_DS, HEIGHT_DS + HEIGHT_DS, 0, 0, 0, 0, 0, HEIGHT_DS, usb_device_desc->video_data_type);
|
||||
}
|
||||
|
||||
static is_device_device_handlers* usb_find_by_serial_number(const is_device_usb_device* usb_device_desc, CaptureDevice* device) {
|
||||
is_device_device_handlers* final_handlers = NULL;
|
||||
int curr_serial_extra_id = 0;
|
||||
final_handlers = is_device_libusb_serial_reconnection(usb_device_desc, device, curr_serial_extra_id);
|
||||
|
||||
if (final_handlers == NULL)
|
||||
final_handlers = is_driver_serial_reconnection(device);
|
||||
return final_handlers;
|
||||
}
|
||||
|
||||
void list_devices_is_device(std::vector<CaptureDevice> &devices_list, std::vector<no_access_recap_data> &no_access_list) {
|
||||
const size_t num_is_device_desc = GetNumISDeviceDesc();
|
||||
int* curr_serial_extra_id_is_device = new int[num_is_device_desc];
|
||||
bool* no_access_elems = new bool[num_is_device_desc];
|
||||
bool* not_supported_elems = new bool[num_is_device_desc];
|
||||
for (int i = 0; i < num_is_device_desc; i++) {
|
||||
no_access_elems[i] = false;
|
||||
not_supported_elems[i] = false;
|
||||
curr_serial_extra_id_is_device[i] = 0;
|
||||
}
|
||||
is_device_libusb_list_devices(devices_list, no_access_elems, not_supported_elems, curr_serial_extra_id_is_device, num_is_device_desc);
|
||||
|
||||
bool any_not_supported = false;
|
||||
for(int i = 0; i < num_is_device_desc; i++)
|
||||
any_not_supported |= not_supported_elems[i];
|
||||
for(int i = 0; i < num_is_device_desc; i++)
|
||||
if(no_access_elems[i]) {
|
||||
const is_device_usb_device* usb_device = GetISDeviceDesc(i);
|
||||
no_access_list.emplace_back(usb_device->vid, usb_device->pid);
|
||||
}
|
||||
if(any_not_supported)
|
||||
is_driver_list_devices(devices_list, not_supported_elems, curr_serial_extra_id_is_device, num_is_device_desc);
|
||||
|
||||
delete[] curr_serial_extra_id_is_device;
|
||||
delete[] no_access_elems;
|
||||
delete[] not_supported_elems;
|
||||
}
|
||||
|
||||
static void is_device_connection_end(is_device_device_handlers* handlers, const is_device_usb_device *device_desc, bool interface_claimed = true) {
|
||||
if (handlers == NULL)
|
||||
return;
|
||||
if (handlers->usb_handle)
|
||||
is_device_libusb_end_connection(handlers, device_desc, interface_claimed);
|
||||
else
|
||||
is_driver_end_connection(handlers);
|
||||
delete handlers;
|
||||
}
|
||||
|
||||
bool is_device_connect_usb(bool print_failed, CaptureData* capture_data, CaptureDevice* device) {
|
||||
const is_device_usb_device* usb_device_info = (const is_device_usb_device*)device->descriptor;
|
||||
is_device_device_handlers* handlers = usb_find_by_serial_number(usb_device_info, device);
|
||||
if(handlers == NULL) {
|
||||
capture_error_print(true, capture_data, "Device not found");
|
||||
return false;
|
||||
}
|
||||
capture_data->handle = (void*)handlers;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
uint64_t usb_is_device_get_video_in_size(CaptureScreensType capture_type, is_device_type device_type) {
|
||||
if((capture_type == CAPTURE_SCREENS_TOP) || (capture_type == CAPTURE_SCREENS_BOTTOM))
|
||||
return sizeof(ISNitroEmulatorVideoInputData) / 2;
|
||||
return sizeof(ISNitroEmulatorVideoInputData);
|
||||
}
|
||||
|
||||
|
||||
uint64_t usb_is_device_get_video_in_size(CaptureStatus* status) {
|
||||
return usb_is_device_get_video_in_size(status->capture_type, ((const is_device_usb_device*)(status->device.descriptor))->device_type);
|
||||
}
|
||||
|
||||
|
||||
uint64_t usb_is_device_get_video_in_size(CaptureData* capture_data) {
|
||||
return usb_is_device_get_video_in_size(&capture_data->status);
|
||||
}
|
||||
|
||||
int set_acquisition_mode(is_device_device_handlers* handlers, CaptureScreensType capture_type, CaptureSpeedsType capture_speed, const is_device_usb_device* usb_device_desc) {
|
||||
is_device_forward_config_values_screens capture_mode_flag = IS_DEVICE_FORWARD_CONFIG_MODE_BOTH;
|
||||
switch(capture_type) {
|
||||
case CAPTURE_SCREENS_TOP:
|
||||
capture_mode_flag = IS_DEVICE_FORWARD_CONFIG_MODE_TOP;
|
||||
break;
|
||||
case CAPTURE_SCREENS_BOTTOM:
|
||||
capture_mode_flag = IS_DEVICE_FORWARD_CONFIG_MODE_BOTTOM;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
is_device_forward_config_values_rate capture_rate_flag = IS_DEVICE_FORWARD_CONFIG_RATE_FULL;
|
||||
switch(capture_speed) {
|
||||
case CAPTURE_SPEEDS_HALF:
|
||||
capture_rate_flag = IS_DEVICE_FORWARD_CONFIG_RATE_HALF;
|
||||
break;
|
||||
case CAPTURE_SPEEDS_THIRD:
|
||||
capture_rate_flag = IS_DEVICE_FORWARD_CONFIG_RATE_THIRD;
|
||||
break;
|
||||
case CAPTURE_SPEEDS_QUARTER:
|
||||
capture_rate_flag = IS_DEVICE_FORWARD_CONFIG_RATE_QUARTER;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return UpdateFrameForwardConfig(handlers, IS_DEVICE_FORWARD_CONFIG_COLOR_RGB24, capture_mode_flag, capture_rate_flag, usb_device_desc);
|
||||
}
|
||||
|
||||
int EndAcquisition(CaptureData* capture_data, ISDeviceCaptureReceivedData* is_device_capture_recv_data, bool do_drain_frames, int start_frames, CaptureScreensType capture_type) {
|
||||
switch(((const is_device_usb_device*)(capture_data->status.device.descriptor))->device_type) {
|
||||
case IS_NITRO_EMULATOR_DEVICE:
|
||||
return EndAcquisitionEmulator(capture_data, is_device_capture_recv_data, do_drain_frames, start_frames, capture_type);
|
||||
case IS_NITRO_CAPTURE_DEVICE:
|
||||
return EndAcquisitionCapture(capture_data, is_device_capture_recv_data);
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int EndAcquisition(const is_device_usb_device* usb_device_desc, is_device_device_handlers* handlers, bool do_drain_frames, int start_frames, CaptureScreensType capture_type) {
|
||||
switch(usb_device_desc->device_type) {
|
||||
case IS_NITRO_EMULATOR_DEVICE:
|
||||
return EndAcquisitionEmulator(usb_device_desc, handlers, do_drain_frames, start_frames, capture_type);
|
||||
case IS_NITRO_CAPTURE_DEVICE:
|
||||
return EndAcquisitionCapture(usb_device_desc, handlers);
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static void output_to_thread(CaptureData* capture_data, CaptureReceived* capture_buf, CaptureScreensType curr_capture_type, std::chrono::time_point<std::chrono::high_resolution_clock>* clock_start) {
|
||||
// Output to the other threads...
|
||||
const is_device_usb_device* usb_device_info = (const is_device_usb_device*)capture_data->status.device.descriptor;
|
||||
const auto curr_time = std::chrono::high_resolution_clock::now();
|
||||
const std::chrono::duration<double> diff = curr_time - (*clock_start);
|
||||
*clock_start = curr_time;
|
||||
capture_data->data_buffers.WriteToBuffer(capture_buf, usb_is_device_get_video_in_size(curr_capture_type, usb_device_info->device_type), diff.count(), &capture_data->status.device, curr_capture_type);
|
||||
|
||||
if (capture_data->status.cooldown_curr_in)
|
||||
capture_data->status.cooldown_curr_in = capture_data->status.cooldown_curr_in - 1;
|
||||
capture_data->status.video_wait.unlock();
|
||||
capture_data->status.audio_wait.unlock();
|
||||
}
|
||||
|
||||
int is_device_read_frame_and_output(CaptureData* capture_data, CaptureReceived* capture_buf, CaptureScreensType curr_capture_type, std::chrono::time_point<std::chrono::high_resolution_clock> &clock_start) {
|
||||
const is_device_usb_device* usb_device_info = (const is_device_usb_device*)capture_data->status.device.descriptor;
|
||||
int ret = ReadFrame((is_device_device_handlers*)capture_data->handle, (uint8_t*)capture_buf, usb_is_device_get_video_in_size(curr_capture_type, usb_device_info->device_type), usb_device_info);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
output_to_thread(capture_data, capture_buf, curr_capture_type, &clock_start);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void is_device_read_frame_request(CaptureData* capture_data, ISDeviceCaptureReceivedData* is_device_capture_recv_data, CaptureScreensType curr_capture_type, uint32_t index) {
|
||||
if(is_device_capture_recv_data == NULL)
|
||||
return;
|
||||
const is_device_usb_device* usb_device_info = (const is_device_usb_device*)capture_data->status.device.descriptor;
|
||||
is_device_capture_recv_data->index = index;
|
||||
is_device_capture_recv_data->curr_capture_type = curr_capture_type;
|
||||
is_device_capture_recv_data->cb_data.function = is_device_read_frame_cb;
|
||||
ReadFrameAsync((is_device_device_handlers*)capture_data->handle, (uint8_t*)&is_device_capture_recv_data->buffer, usb_is_device_get_video_in_size(curr_capture_type, usb_device_info->device_type), usb_device_info, &is_device_capture_recv_data->cb_data);
|
||||
}
|
||||
|
||||
static void end_is_device_read_frame_cb(ISDeviceCaptureReceivedData* is_device_capture_recv_data) {
|
||||
is_device_capture_recv_data->in_use = false;
|
||||
is_device_capture_recv_data->is_buffer_free_shared_mutex->specific_unlock(is_device_capture_recv_data->cb_data.internal_index);
|
||||
}
|
||||
|
||||
static void is_device_read_frame_cb(void* user_data, int transfer_length, int transfer_status) {
|
||||
ISDeviceCaptureReceivedData* is_device_capture_recv_data = (ISDeviceCaptureReceivedData*)user_data;
|
||||
if((*is_device_capture_recv_data->status) < 0)
|
||||
return end_is_device_read_frame_cb(is_device_capture_recv_data);
|
||||
if(transfer_status != LIBUSB_TRANSFER_COMPLETED) {
|
||||
*is_device_capture_recv_data->status = LIBUSB_ERROR_OTHER;
|
||||
return end_is_device_read_frame_cb(is_device_capture_recv_data);
|
||||
}
|
||||
|
||||
if(((int32_t)(is_device_capture_recv_data->index - (*is_device_capture_recv_data->last_index))) <= 0) {
|
||||
//*is_device_capture_recv_data->status = LIBUSB_ERROR_INTERRUPTED;
|
||||
return end_is_device_read_frame_cb(is_device_capture_recv_data);
|
||||
}
|
||||
*is_device_capture_recv_data->last_index = is_device_capture_recv_data->index;
|
||||
|
||||
output_to_thread(is_device_capture_recv_data->capture_data, &is_device_capture_recv_data->buffer, is_device_capture_recv_data->curr_capture_type, is_device_capture_recv_data->clock_start);
|
||||
end_is_device_read_frame_cb(is_device_capture_recv_data);
|
||||
}
|
||||
|
||||
int is_device_get_num_free_buffers(ISDeviceCaptureReceivedData* is_device_capture_recv_data) {
|
||||
int num_free = 0;
|
||||
for(int i = 0; i < NUM_CAPTURE_RECEIVED_DATA_BUFFERS; i++)
|
||||
if(!is_device_capture_recv_data[i].in_use)
|
||||
num_free += 1;
|
||||
return num_free;
|
||||
}
|
||||
|
||||
static void close_all_reads_error(CaptureData* capture_data, ISDeviceCaptureReceivedData* is_device_capture_recv_data, bool &errored_out) {
|
||||
if(*is_device_capture_recv_data[0].status < 0) {
|
||||
if(!errored_out) {
|
||||
for (int i = 0; i < NUM_CAPTURE_RECEIVED_DATA_BUFFERS; i++)
|
||||
CloseAsyncRead((is_device_device_handlers*)capture_data->handle, &is_device_capture_recv_data[i].cb_data);
|
||||
int ret = ResetUSBDevice((is_device_device_handlers*)capture_data->handle);
|
||||
if((ret == LIBUSB_ERROR_NO_DEVICE) || (ret == LIBUSB_ERROR_NOT_FOUND)) {
|
||||
for(int i = 0; i < NUM_CAPTURE_RECEIVED_DATA_BUFFERS; i++) {
|
||||
is_device_capture_recv_data[i].cb_data.transfer_data_access.lock();
|
||||
is_device_capture_recv_data[i].cb_data.transfer_data = NULL;
|
||||
is_device_capture_recv_data[i].cb_data.transfer_data_access.unlock();
|
||||
is_device_capture_recv_data[i].in_use = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
errored_out = true;
|
||||
}
|
||||
}
|
||||
|
||||
static void check_too_much_time_passed(CaptureData* capture_data, ISDeviceCaptureReceivedData* is_device_capture_recv_data, bool &errored_out, const std::chrono::time_point<std::chrono::high_resolution_clock> &start_time) {
|
||||
const auto curr_time = std::chrono::high_resolution_clock::now();
|
||||
const std::chrono::duration<double> diff = curr_time - start_time;
|
||||
if(diff.count() > MAX_TIME_WAIT)
|
||||
*is_device_capture_recv_data[0].status = -1;
|
||||
close_all_reads_error(capture_data, is_device_capture_recv_data, errored_out);
|
||||
}
|
||||
|
||||
void wait_all_is_device_transfers_done(CaptureData* capture_data, ISDeviceCaptureReceivedData* is_device_capture_recv_data) {
|
||||
if (is_device_capture_recv_data == NULL)
|
||||
return;
|
||||
bool errored_out = false;
|
||||
close_all_reads_error(capture_data, is_device_capture_recv_data, errored_out);
|
||||
const auto start_time = std::chrono::high_resolution_clock::now();
|
||||
for (int i = 0; i < NUM_CAPTURE_RECEIVED_DATA_BUFFERS; i++) {
|
||||
void* transfer_data;
|
||||
do {
|
||||
is_device_capture_recv_data[i].cb_data.transfer_data_access.lock();
|
||||
transfer_data = is_device_capture_recv_data[i].cb_data.transfer_data;
|
||||
is_device_capture_recv_data[i].cb_data.transfer_data_access.unlock();
|
||||
if(transfer_data) {
|
||||
check_too_much_time_passed(capture_data, is_device_capture_recv_data, errored_out, start_time);
|
||||
is_device_capture_recv_data[i].cb_data.is_transfer_done_mutex->specific_timed_lock(i);
|
||||
}
|
||||
} while(transfer_data);
|
||||
}
|
||||
}
|
||||
|
||||
void wait_all_is_device_buffers_free(CaptureData* capture_data, ISDeviceCaptureReceivedData* is_device_capture_recv_data) {
|
||||
if(is_device_capture_recv_data == NULL)
|
||||
return;
|
||||
bool errored_out = false;
|
||||
close_all_reads_error(capture_data, is_device_capture_recv_data, errored_out);
|
||||
const auto start_time = std::chrono::high_resolution_clock::now();
|
||||
for(int i = 0; i < NUM_CAPTURE_RECEIVED_DATA_BUFFERS; i++)
|
||||
while(is_device_capture_recv_data[i].in_use) {
|
||||
check_too_much_time_passed(capture_data, is_device_capture_recv_data, errored_out, start_time);
|
||||
is_device_capture_recv_data[i].is_buffer_free_shared_mutex->specific_timed_lock(i);
|
||||
}
|
||||
}
|
||||
|
||||
void wait_one_is_device_buffer_free(CaptureData* capture_data, ISDeviceCaptureReceivedData* is_device_capture_recv_data) {
|
||||
bool done = false;
|
||||
bool errored_out = false;
|
||||
const auto start_time = std::chrono::high_resolution_clock::now();
|
||||
while(!done) {
|
||||
for(int i = 0; i < NUM_CAPTURE_RECEIVED_DATA_BUFFERS; i++) {
|
||||
if(!is_device_capture_recv_data[i].in_use)
|
||||
done = true;
|
||||
}
|
||||
if(!done) {
|
||||
check_too_much_time_passed(capture_data, is_device_capture_recv_data, errored_out, start_time);
|
||||
if(*is_device_capture_recv_data[0].status < 0)
|
||||
return;
|
||||
int dummy = 0;
|
||||
is_device_capture_recv_data[0].is_buffer_free_shared_mutex->general_timed_lock(&dummy);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool is_device_are_buffers_all_free(ISDeviceCaptureReceivedData* is_device_capture_recv_data) {
|
||||
return is_device_get_num_free_buffers(is_device_capture_recv_data) == NUM_CAPTURE_RECEIVED_DATA_BUFFERS;
|
||||
}
|
||||
|
||||
ISDeviceCaptureReceivedData* is_device_get_free_buffer(CaptureData* capture_data, ISDeviceCaptureReceivedData* is_device_capture_recv_data) {
|
||||
wait_one_is_device_buffer_free(capture_data, is_device_capture_recv_data);
|
||||
if(*is_device_capture_recv_data[0].status < 0)
|
||||
return NULL;
|
||||
for(int i = 0; i < NUM_CAPTURE_RECEIVED_DATA_BUFFERS; i++)
|
||||
if(!is_device_capture_recv_data[i].in_use) {
|
||||
is_device_capture_recv_data[i].is_buffer_free_shared_mutex->specific_try_lock(i);
|
||||
is_device_capture_recv_data[i].in_use = true;
|
||||
return &is_device_capture_recv_data[i];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int get_is_device_status(ISDeviceCaptureReceivedData* is_device_capture_recv_data) {
|
||||
return *is_device_capture_recv_data[0].status;
|
||||
}
|
||||
|
||||
void reset_is_device_status(ISDeviceCaptureReceivedData* is_device_capture_recv_data) {
|
||||
*is_device_capture_recv_data[0].status = 0;
|
||||
}
|
||||
|
||||
void is_device_acquisition_main_loop(CaptureData* capture_data) {
|
||||
if(!usb_is_initialized())
|
||||
return;
|
||||
bool is_done_thread;
|
||||
ConsumerMutex has_data_been_processed;
|
||||
std::thread async_processing_thread;
|
||||
|
||||
uint32_t last_index = -1;
|
||||
int status = 0;
|
||||
isd_async_callback_data* cb_queue[NUM_CAPTURE_RECEIVED_DATA_BUFFERS];
|
||||
int queue_elems = 0;
|
||||
bool one_transfer_active = false;
|
||||
SharedConsumerMutex is_buffer_free_shared_mutex(NUM_CAPTURE_RECEIVED_DATA_BUFFERS);
|
||||
SharedConsumerMutex is_transfer_done_shared_mutex(NUM_CAPTURE_RECEIVED_DATA_BUFFERS);
|
||||
SharedConsumerMutex is_transfer_data_ready_shared_mutex(NUM_CAPTURE_RECEIVED_DATA_BUFFERS);
|
||||
std::chrono::time_point<std::chrono::high_resolution_clock> clock_start = std::chrono::high_resolution_clock::now();
|
||||
ISDeviceCaptureReceivedData* is_device_capture_recv_data = new ISDeviceCaptureReceivedData[NUM_CAPTURE_RECEIVED_DATA_BUFFERS];
|
||||
for(int i = 0; i < NUM_CAPTURE_RECEIVED_DATA_BUFFERS; i++) {
|
||||
is_device_capture_recv_data[i].in_use = false;
|
||||
is_device_capture_recv_data[i].index = i;
|
||||
is_device_capture_recv_data[i].capture_data = capture_data;
|
||||
is_device_capture_recv_data[i].last_index = &last_index;
|
||||
is_device_capture_recv_data[i].clock_start = &clock_start;
|
||||
is_device_capture_recv_data[i].is_buffer_free_shared_mutex = &is_buffer_free_shared_mutex;
|
||||
is_device_capture_recv_data[i].status = &status;
|
||||
is_device_capture_recv_data[i].cb_data.actual_user_data = &is_device_capture_recv_data[i];
|
||||
is_device_capture_recv_data[i].cb_data.transfer_data = NULL;
|
||||
is_device_capture_recv_data[i].cb_data.is_transfer_done_mutex = &is_transfer_done_shared_mutex;
|
||||
is_device_capture_recv_data[i].cb_data.internal_index = i;
|
||||
is_device_capture_recv_data[i].cb_data.is_transfer_data_ready_mutex = &is_transfer_data_ready_shared_mutex;
|
||||
is_device_capture_recv_data[i].cb_data.is_data_ready = false;
|
||||
}
|
||||
SetupISDeviceAsyncThread((is_device_device_handlers*)capture_data->handle, is_device_capture_recv_data, &async_processing_thread, &is_done_thread, &has_data_been_processed);
|
||||
capture_data->status.reset_hardware = false;
|
||||
switch(((const is_device_usb_device*)(capture_data->status.device.descriptor))->device_type) {
|
||||
case IS_NITRO_EMULATOR_DEVICE:
|
||||
is_nitro_acquisition_emulator_main_loop(capture_data, is_device_capture_recv_data);
|
||||
break;
|
||||
case IS_NITRO_CAPTURE_DEVICE:
|
||||
is_nitro_acquisition_capture_main_loop(capture_data, is_device_capture_recv_data);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
wait_all_is_device_buffers_free(capture_data, is_device_capture_recv_data);
|
||||
EndISDeviceAsyncThread((is_device_device_handlers*)capture_data->handle, is_device_capture_recv_data, &async_processing_thread, &is_done_thread, &has_data_been_processed);
|
||||
delete []is_device_capture_recv_data;
|
||||
}
|
||||
|
||||
void usb_is_device_acquisition_cleanup(CaptureData* capture_data) {
|
||||
if(!usb_is_initialized())
|
||||
return;
|
||||
is_device_connection_end((is_device_device_handlers*)capture_data->handle, (const is_device_usb_device*)capture_data->status.device.descriptor);
|
||||
capture_data->handle = NULL;
|
||||
}
|
||||
|
||||
bool is_device_is_capture(CaptureDevice* device) {
|
||||
const is_device_usb_device* usb_device_info = (const is_device_usb_device*)device->descriptor;
|
||||
return usb_device_info->device_type != IS_NITRO_EMULATOR_DEVICE;
|
||||
}
|
||||
|
||||
bool is_device_is_nitro(CaptureDevice* device) {
|
||||
const is_device_usb_device* usb_device_info = (const is_device_usb_device*)device->descriptor;
|
||||
return (usb_device_info->device_type == IS_NITRO_EMULATOR_DEVICE) || (usb_device_info->device_type == IS_NITRO_CAPTURE_DEVICE);
|
||||
}
|
||||
|
||||
void usb_is_device_init() {
|
||||
return usb_init();
|
||||
}
|
||||
|
||||
void usb_is_device_close() {
|
||||
usb_close();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,641 @@
|
||||
#include "frontend.hpp"
|
||||
#include "usb_is_device_communications.hpp"
|
||||
#include "usb_is_device_libusb.hpp"
|
||||
#include "usb_is_device_is_driver.hpp"
|
||||
|
||||
#include <libusb.h>
|
||||
#include <cstring>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
|
||||
// Code based off of Gericom's sample code. Distributed under the MIT License. Copyright (c) 2024 Gericom
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// 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
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
// IN THE SOFTWARE.
|
||||
|
||||
#define USB_PACKET_LIMIT 0x2000
|
||||
|
||||
#define REG_USB_DMA_CONTROL_2 0x0C000028
|
||||
#define REG_USB_BIU_CONTROL_2 0x0C0000A4
|
||||
|
||||
enum is_nitro_packet_emulator_dir {
|
||||
IS_NITRO_PACKET_EMU_DIR_WRITE = 0x10,
|
||||
IS_NITRO_PACKET_EMU_DIR_READ = 0x11
|
||||
};
|
||||
|
||||
enum is_nitro_packet_capture_dir {
|
||||
IS_NITRO_PACKET_CAP_DIR_WRITE = 0x00,
|
||||
IS_NITRO_PACKET_CAP_DIR_READ = 0x01
|
||||
};
|
||||
|
||||
enum is_device_packet_type {
|
||||
IS_NITRO_PACKET_TYPE_COMMAND = 0,
|
||||
IS_NITRO_PACKET_TYPE_EMU_MEMORY = 1,
|
||||
IS_NITRO_EMULATOR_PACKET_TYPE_AGB_SRAM = 2,
|
||||
IS_NITRO_CAPTURE_PACKET_TYPE_DMA_CONTROL = 2,
|
||||
IS_NITRO_PACKET_TYPE_CAPTURE = 3,
|
||||
IS_NITRO_PACKET_TYPE_AGB_CART_ROM = 4,
|
||||
IS_NITRO_PACKET_TYPE_AGB_BUS2 = 5
|
||||
};
|
||||
|
||||
enum is_nitro_emulator_command {
|
||||
IS_NITRO_EMU_CMD_GET_SERIAL = 0x13,
|
||||
IS_NITRO_EMU_CMD_READ_NEC_MEM = 0x17,
|
||||
IS_NITRO_EMU_CMD_WRITE_NEC_MEM = 0x24,
|
||||
IS_NITRO_EMU_CMD_SET_READ_NEC_MEM = 0x25,
|
||||
IS_NITRO_EMU_CMD_FULL_HARDWARE_RESET = 0x81,
|
||||
IS_NITRO_EMU_CMD_CPU_RESET = 0x8A,
|
||||
IS_NITRO_EMU_CMD_AD = 0xAD,
|
||||
};
|
||||
|
||||
enum is_nitro_capture_command {
|
||||
IS_NITRO_CAP_CMD_ENABLE_CAP = 0x00,
|
||||
IS_NITRO_CAP_CMD_GET_SERIAL = 0x03,
|
||||
IS_NITRO_CAP_CMD_SET_FWD_RATE = 0x12,
|
||||
IS_NITRO_CAP_CMD_SET_FWD_MODE = 0x13,
|
||||
IS_NITRO_CAP_CMD_SET_FWD_COLOURS = 0x14,
|
||||
IS_NITRO_CAP_CMD_SET_FWD_FRAMES = 0x15,
|
||||
IS_NITRO_CAP_CMD_GET_DEBUG_STATE = 0x18,
|
||||
IS_NITRO_CAP_CMD_SET_RESTART_FULL = 0x18,
|
||||
IS_NITRO_CAP_CMD_GET_LID_STATE = 0x19,
|
||||
IS_NITRO_CAP_CMD_SET_FWD_RESTART = 0x1C,
|
||||
IS_NITRO_CAP_CMD_SET_RESET_CPU_ON = 0x21,
|
||||
IS_NITRO_CAP_CMD_SET_RESET_CPU_OFF = 0x22,
|
||||
};
|
||||
|
||||
enum is_nitro_emulator_forward_bits {
|
||||
IS_NITRO_EMULATOR_FORWARD_ENABLE_BIT = 0,
|
||||
IS_NITRO_EMULATOR_FORWARD_COUNTER_RESTART_BIT = 1,
|
||||
};
|
||||
|
||||
enum is_nitro_capture_forward_bits {
|
||||
IS_NITRO_CAPTURE_FORWARD_COUNTER_RESTART_BIT = 0,
|
||||
};
|
||||
|
||||
#pragma pack(push, 1)
|
||||
struct PACKED is_nitro_nec_packet_header {
|
||||
uint8_t command;
|
||||
uint8_t unit_size;
|
||||
uint16_t count;
|
||||
uint32_t address;
|
||||
};
|
||||
|
||||
struct PACKED is_device_packet_header {
|
||||
uint16_t command;
|
||||
uint8_t direction;
|
||||
uint8_t type;
|
||||
uint32_t address;
|
||||
uint32_t length;
|
||||
uint32_t padding;
|
||||
};
|
||||
#pragma pack(pop)
|
||||
|
||||
static const is_device_usb_device usb_is_nitro_emu_rare_desc = {
|
||||
.name = "ISNEr", .long_name = "IS Nitro Emulator(R)",
|
||||
.vid = 0x0f6e, .pid = 0x0400,
|
||||
.default_config = 1, .default_interface = 0,
|
||||
.bulk_timeout = 500,
|
||||
.ep2_in = 2 | LIBUSB_ENDPOINT_IN, .ep1_out = 1 | LIBUSB_ENDPOINT_OUT,
|
||||
.product_id = 2, .manufacturer_id = 1, .device_type = IS_NITRO_EMULATOR_DEVICE,
|
||||
.video_data_type = VIDEO_DATA_BGR
|
||||
};
|
||||
|
||||
static const is_device_usb_device usb_is_nitro_emu_common_desc = {
|
||||
.name = "ISNE", .long_name = "IS Nitro Emulator",
|
||||
.vid = 0x0f6e, .pid = 0x0404,
|
||||
.default_config = 1, .default_interface = 0,
|
||||
.bulk_timeout = 500,
|
||||
.ep2_in = 2 | LIBUSB_ENDPOINT_IN, .ep1_out = 1 | LIBUSB_ENDPOINT_OUT,
|
||||
.product_id = 2, .manufacturer_id = 1, .device_type = IS_NITRO_EMULATOR_DEVICE,
|
||||
.video_data_type = VIDEO_DATA_BGR
|
||||
};
|
||||
|
||||
static const is_device_usb_device usb_is_nitro_cap_desc = {
|
||||
.name = "ISNC", .long_name = "IS Nitro Capture",
|
||||
.vid = 0x0f6e, .pid = 0x0403,
|
||||
.default_config = 1, .default_interface = 0,
|
||||
.bulk_timeout = 500,
|
||||
.ep2_in = 2 | LIBUSB_ENDPOINT_IN, .ep1_out = 1 | LIBUSB_ENDPOINT_OUT,
|
||||
.product_id = 2, .manufacturer_id = 1, .device_type = IS_NITRO_CAPTURE_DEVICE,
|
||||
.video_data_type = VIDEO_DATA_BGR
|
||||
};
|
||||
|
||||
static const is_device_usb_device* all_usb_is_device_devices_desc[] = {
|
||||
&usb_is_nitro_emu_rare_desc,
|
||||
&usb_is_nitro_emu_common_desc,
|
||||
&usb_is_nitro_cap_desc,
|
||||
};
|
||||
|
||||
int GetNumISDeviceDesc() {
|
||||
return sizeof(all_usb_is_device_devices_desc) / sizeof(all_usb_is_device_devices_desc[0]);
|
||||
}
|
||||
|
||||
const is_device_usb_device* GetISDeviceDesc(int index) {
|
||||
if((index < 0) || (index >= GetNumISDeviceDesc()))
|
||||
index = 0;
|
||||
return all_usb_is_device_devices_desc[index];
|
||||
}
|
||||
|
||||
static void fix_endianness_header(is_device_packet_header* header) {
|
||||
header->command = to_le(header->command);
|
||||
header->address = to_le(header->address);
|
||||
header->length = to_le(header->length);
|
||||
}
|
||||
|
||||
static void fix_endianness_header(is_nitro_nec_packet_header* header) {
|
||||
header->count = to_le(header->count);
|
||||
header->address = to_le(header->address);
|
||||
}
|
||||
|
||||
// Write to bulk endpoint. Returns libusb error code
|
||||
static int bulk_out(is_device_device_handlers* handlers, const is_device_usb_device* usb_device_desc, uint8_t *buf, int length, int *transferred) {
|
||||
if(handlers->usb_handle)
|
||||
return is_device_libusb_bulk_out(handlers, usb_device_desc, buf, length, transferred);
|
||||
return is_driver_bulk_out(handlers, usb_device_desc, buf, length, transferred);
|
||||
}
|
||||
|
||||
// Read from bulk endpoint. Returns libusb error code
|
||||
static int bulk_in(is_device_device_handlers* handlers, const is_device_usb_device* usb_device_desc, uint8_t *buf, int length, int *transferred) {
|
||||
if(handlers->usb_handle)
|
||||
return is_device_libusb_bulk_in(handlers, usb_device_desc, buf, length, transferred);
|
||||
return is_driver_bulk_in(handlers, usb_device_desc, buf, length, transferred);
|
||||
}
|
||||
|
||||
// Read from bulk endpoint. Returns libusb error code
|
||||
static void bulk_in_async(is_device_device_handlers* handlers, const is_device_usb_device* usb_device_desc, uint8_t *buf, int length, isd_async_callback_data* cb_data) {
|
||||
if(handlers->usb_handle)
|
||||
return is_device_libusb_async_in_start(handlers, usb_device_desc, buf, length, cb_data);
|
||||
is_drive_async_in_start(handlers, usb_device_desc, buf, length, cb_data);
|
||||
}
|
||||
|
||||
static int SendWritePacket(is_device_device_handlers* handlers, uint16_t command, is_device_packet_type type, uint32_t address, uint8_t* buf, int length, const is_device_usb_device* device_desc, bool expect_result = true) {
|
||||
is_device_packet_header header;
|
||||
bool append_mode = true;
|
||||
if(device_desc->device_type == IS_NITRO_CAPTURE_DEVICE)
|
||||
append_mode = false;
|
||||
uint8_t single_usb_packet[USB_PACKET_LIMIT];
|
||||
int single_packet_covered_size = USB_PACKET_LIMIT - sizeof(header);
|
||||
if(!append_mode)
|
||||
single_packet_covered_size = USB_PACKET_LIMIT;
|
||||
int num_iters = (length + single_packet_covered_size - 1) / single_packet_covered_size;
|
||||
if(!num_iters)
|
||||
num_iters = 1;
|
||||
for(int i = 0; i < num_iters; i++) {
|
||||
int transfer_size = length - (i * single_packet_covered_size);
|
||||
if(transfer_size > single_packet_covered_size)
|
||||
transfer_size = single_packet_covered_size;
|
||||
|
||||
uint8_t packet_direction = 0;
|
||||
switch(device_desc->device_type) {
|
||||
case IS_NITRO_EMULATOR_DEVICE:
|
||||
packet_direction = IS_NITRO_PACKET_EMU_DIR_WRITE;
|
||||
break;
|
||||
case IS_NITRO_CAPTURE_DEVICE:
|
||||
packet_direction = IS_NITRO_PACKET_CAP_DIR_WRITE;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
header.command = command;
|
||||
header.direction = packet_direction;
|
||||
header.type = type;
|
||||
header.address = address;
|
||||
header.length = transfer_size;
|
||||
header.padding = 0;
|
||||
fix_endianness_header(&header);
|
||||
int ret = 0;
|
||||
int num_bytes = 0;
|
||||
for(int j = 0; j < sizeof(is_device_packet_header); j++)
|
||||
single_usb_packet[j] = ((uint8_t*)&header)[j];
|
||||
if(append_mode && (buf != NULL)) {
|
||||
for(int j = 0; j < transfer_size; j++)
|
||||
single_usb_packet[sizeof(is_device_packet_header) + j] = buf[(i * single_packet_covered_size) + j];
|
||||
ret = bulk_out(handlers, device_desc, single_usb_packet, transfer_size + sizeof(is_device_packet_header), &num_bytes);
|
||||
}
|
||||
else {
|
||||
int header_bytes = 0;
|
||||
ret = bulk_out(handlers, device_desc, single_usb_packet, sizeof(is_device_packet_header), &header_bytes);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
if(header_bytes != sizeof(is_device_packet_header))
|
||||
return LIBUSB_ERROR_INTERRUPTED;
|
||||
if((buf != NULL) && (transfer_size > 0))
|
||||
ret = bulk_out(handlers, device_desc, &buf[(i * single_packet_covered_size)], transfer_size, &num_bytes);
|
||||
num_bytes += header_bytes;
|
||||
}
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
if(num_bytes != (transfer_size + sizeof(is_device_packet_header)))
|
||||
return LIBUSB_ERROR_INTERRUPTED;
|
||||
}
|
||||
if((device_desc->device_type == IS_NITRO_CAPTURE_DEVICE) && expect_result) {
|
||||
uint8_t status[16];
|
||||
int status_bytes = 0;
|
||||
int ret = bulk_in(handlers, device_desc, status, sizeof(status), &status_bytes);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
if(status_bytes != sizeof(status))
|
||||
return LIBUSB_ERROR_INTERRUPTED;
|
||||
if(status[0] != 1)
|
||||
return LIBUSB_ERROR_OTHER;
|
||||
}
|
||||
return LIBUSB_SUCCESS;
|
||||
}
|
||||
|
||||
static int SendReadPacket(is_device_device_handlers* handlers, uint16_t command, is_device_packet_type type, uint32_t address, uint8_t* buf, int length, const is_device_usb_device* device_desc, bool expect_result = true) {
|
||||
is_device_packet_header header;
|
||||
int single_packet_covered_size = USB_PACKET_LIMIT;
|
||||
int num_iters = (length + single_packet_covered_size - 1) / single_packet_covered_size;
|
||||
if(num_iters == 0)
|
||||
num_iters = 1;
|
||||
for(int i = 0; i < num_iters; i++) {
|
||||
int transfer_size = length - (i * single_packet_covered_size);
|
||||
if(transfer_size > single_packet_covered_size)
|
||||
transfer_size = single_packet_covered_size;
|
||||
|
||||
uint8_t packet_direction = 0;
|
||||
switch(device_desc->device_type) {
|
||||
case IS_NITRO_EMULATOR_DEVICE:
|
||||
packet_direction = IS_NITRO_PACKET_EMU_DIR_READ;
|
||||
break;
|
||||
case IS_NITRO_CAPTURE_DEVICE:
|
||||
packet_direction = IS_NITRO_PACKET_CAP_DIR_READ;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
header.command = command;
|
||||
header.direction = packet_direction;
|
||||
header.type = type;
|
||||
header.address = address;
|
||||
header.length = transfer_size;
|
||||
header.padding = 0;
|
||||
fix_endianness_header(&header);
|
||||
int num_bytes = 0;
|
||||
int ret = bulk_out(handlers, device_desc, (uint8_t*)&header, sizeof(is_device_packet_header), &num_bytes);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
if(num_bytes != sizeof(is_device_packet_header))
|
||||
return LIBUSB_ERROR_INTERRUPTED;
|
||||
if(buf != NULL) {
|
||||
ret = bulk_in(handlers, device_desc, buf + (i * single_packet_covered_size), transfer_size, &num_bytes);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
if(num_bytes != transfer_size)
|
||||
return LIBUSB_ERROR_INTERRUPTED;
|
||||
}
|
||||
}
|
||||
if((device_desc->device_type == IS_NITRO_CAPTURE_DEVICE) && expect_result) {
|
||||
uint8_t status[16];
|
||||
int status_bytes = 0;
|
||||
int ret = bulk_in(handlers, device_desc, status, sizeof(status), &status_bytes);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
if(status_bytes != sizeof(status))
|
||||
return LIBUSB_ERROR_INTERRUPTED;
|
||||
if(status[0] != 1)
|
||||
return LIBUSB_ERROR_OTHER;
|
||||
}
|
||||
return LIBUSB_SUCCESS;
|
||||
}
|
||||
|
||||
int SendReadCommand(is_device_device_handlers* handlers, uint16_t command, uint8_t* buf, int length, const is_device_usb_device* device_desc) {
|
||||
return SendReadPacket(handlers, command, IS_NITRO_PACKET_TYPE_COMMAND, 0, buf, length, device_desc);
|
||||
}
|
||||
|
||||
int SendWriteCommand(is_device_device_handlers* handlers, uint16_t command, uint8_t* buf, int length, const is_device_usb_device* device_desc) {
|
||||
return SendWritePacket(handlers, command, IS_NITRO_PACKET_TYPE_COMMAND, 0, buf, length, device_desc);
|
||||
}
|
||||
|
||||
int SendReadCommandU32(is_device_device_handlers* handlers, uint16_t command, uint32_t* out, const is_device_usb_device* device_desc) {
|
||||
uint32_t buffer;
|
||||
int ret = SendReadCommand(handlers, command, (uint8_t*)&buffer, sizeof(uint32_t), device_desc);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
*out = from_le(buffer);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int SendWriteCommandU32(is_device_device_handlers* handlers, uint16_t command, uint32_t value, const is_device_usb_device* device_desc) {
|
||||
uint32_t buffer = to_le(value);
|
||||
return SendWriteCommand(handlers, command, (uint8_t*)&buffer, sizeof(uint32_t), device_desc);
|
||||
}
|
||||
|
||||
int GetDeviceSerial(is_device_device_handlers* handlers, uint8_t* buf, const is_device_usb_device* device_desc) {
|
||||
switch(device_desc->device_type) {
|
||||
case IS_NITRO_EMULATOR_DEVICE:
|
||||
return SendReadCommand(handlers, IS_NITRO_EMU_CMD_GET_SERIAL, buf, IS_DEVICE_REAL_SERIAL_NUMBER_SIZE, device_desc);
|
||||
case IS_NITRO_CAPTURE_DEVICE:
|
||||
return SendReadCommand(handlers, IS_NITRO_CAP_CMD_GET_SERIAL, buf, IS_DEVICE_REAL_SERIAL_NUMBER_SIZE, device_desc);
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int ReadNecMem(is_device_device_handlers* handlers, uint32_t address, uint8_t unit_size, uint8_t* buf, int count, const is_device_usb_device* device_desc) {
|
||||
is_nitro_nec_packet_header header;
|
||||
header.command = IS_NITRO_EMU_CMD_SET_READ_NEC_MEM;
|
||||
header.unit_size = unit_size;
|
||||
header.count = count;
|
||||
header.address = address;
|
||||
fix_endianness_header(&header);
|
||||
int ret = SendWriteCommand(handlers, header.command, (uint8_t*)&header, sizeof(is_nitro_nec_packet_header), device_desc);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
return SendReadCommand(handlers, IS_NITRO_EMU_CMD_READ_NEC_MEM, buf, count * unit_size, device_desc);
|
||||
}
|
||||
|
||||
int ReadNecMemU16(is_device_device_handlers* handlers, uint32_t address, uint16_t* out, const is_device_usb_device* device_desc) {
|
||||
uint16_t buffer;
|
||||
int ret = ReadNecMem(handlers, address, 2, (uint8_t*)&buffer, 1, device_desc);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
*out = from_le(buffer);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ReadNecMemU32(is_device_device_handlers* handlers, uint32_t address, uint32_t* out, const is_device_usb_device* device_desc) {
|
||||
uint32_t buffer;
|
||||
int ret = ReadNecMem(handlers, address, 2, (uint8_t*)&buffer, 2, device_desc);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
*out = from_le(buffer);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int WriteNecMem(is_device_device_handlers* handlers, uint32_t address, uint8_t unit_size, uint8_t* buf, int count, const is_device_usb_device* device_desc) {
|
||||
uint8_t* buffer = new uint8_t[(count * unit_size) + sizeof(is_nitro_nec_packet_header)];
|
||||
is_nitro_nec_packet_header header;
|
||||
header.command = IS_NITRO_EMU_CMD_WRITE_NEC_MEM;
|
||||
header.unit_size = unit_size;
|
||||
header.count = count;
|
||||
header.address = address;
|
||||
fix_endianness_header(&header);
|
||||
for(int i = 0; i < sizeof(is_nitro_nec_packet_header); i++)
|
||||
buffer[i] = ((uint8_t*)&header)[i];
|
||||
for(int i = 0; i < count * unit_size; i++)
|
||||
buffer[i + sizeof(is_nitro_nec_packet_header)] = buf[i];
|
||||
int ret = SendWriteCommand(handlers, header.command, buffer, (count * unit_size) + sizeof(is_nitro_nec_packet_header), device_desc);
|
||||
delete []buffer;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int WriteNecMemU16(is_device_device_handlers* handlers, uint32_t address, uint16_t value, const is_device_usb_device* device_desc) {
|
||||
uint16_t buffer = to_le(value);
|
||||
return WriteNecMem(handlers, address, 2, (uint8_t*)&buffer, 1, device_desc);
|
||||
}
|
||||
|
||||
int WriteNecMemU32(is_device_device_handlers* handlers, uint32_t address, uint32_t value, const is_device_usb_device* device_desc) {
|
||||
uint32_t buffer = to_le(value);
|
||||
return WriteNecMem(handlers, address, 2, (uint8_t*)&buffer, 2, device_desc);
|
||||
}
|
||||
|
||||
int DisableLca2(is_device_device_handlers* handlers, const is_device_usb_device* device_desc) {
|
||||
if(device_desc->device_type != IS_NITRO_EMULATOR_DEVICE)
|
||||
return LIBUSB_SUCCESS;
|
||||
int ret = WriteNecMemU16(handlers, 0x00805180, 0, device_desc);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
ret = WriteNecMemU16(handlers, 0x0F84000A, 1, device_desc);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
//SleepBetweenTransfers(handlers, 2000);
|
||||
return WriteNecMemU16(handlers, 0x0F84000A, 0, device_desc);
|
||||
}
|
||||
|
||||
int StartUsbCaptureDma(is_device_device_handlers* handlers, const is_device_usb_device* device_desc) {
|
||||
int ret = 0;
|
||||
switch(device_desc->device_type) {
|
||||
case IS_NITRO_EMULATOR_DEVICE:
|
||||
ret = WriteNecMemU16(handlers, REG_USB_DMA_CONTROL_2, 2, device_desc);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
return WriteNecMemU16(handlers, REG_USB_BIU_CONTROL_2, 1, device_desc);
|
||||
case IS_NITRO_CAPTURE_DEVICE:
|
||||
return SendReadPacket(handlers, IS_NITRO_CAP_CMD_ENABLE_CAP, IS_NITRO_CAPTURE_PACKET_TYPE_DMA_CONTROL, 0, NULL, 1, device_desc, false);
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int StopUsbCaptureDma(is_device_device_handlers* handlers, const is_device_usb_device* device_desc) {
|
||||
int ret = 0;
|
||||
switch(device_desc->device_type) {
|
||||
case IS_NITRO_EMULATOR_DEVICE:
|
||||
ret = WriteNecMemU16(handlers, REG_USB_DMA_CONTROL_2, 0, device_desc);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
return WriteNecMemU16(handlers, REG_USB_BIU_CONTROL_2, 0, device_desc);
|
||||
case IS_NITRO_CAPTURE_DEVICE:
|
||||
return SendReadPacket(handlers, IS_NITRO_CAP_CMD_ENABLE_CAP, IS_NITRO_CAPTURE_PACKET_TYPE_DMA_CONTROL, 0, NULL, 0, device_desc);
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int SetForwardFrameCount(is_device_device_handlers* handlers, uint16_t count, const is_device_usb_device* device_desc) {
|
||||
if(!count)
|
||||
return LIBUSB_ERROR_INTERRUPTED;
|
||||
count -= 1;
|
||||
switch(device_desc->device_type) {
|
||||
case IS_NITRO_EMULATOR_DEVICE:
|
||||
return WriteNecMemU32(handlers, 0x0800000C, (count >> 8) | ((count & 0xFF) << 16), device_desc);
|
||||
case IS_NITRO_CAPTURE_DEVICE:
|
||||
return SendWriteCommandU32(handlers, IS_NITRO_CAP_CMD_SET_FWD_FRAMES, count, device_desc);
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int SetForwardFramePermanent(is_device_device_handlers* handlers, const is_device_usb_device* device_desc) {
|
||||
return SetForwardFrameCount(handlers, 0x4001, device_desc);
|
||||
}
|
||||
|
||||
int GetFrameCounter(is_device_device_handlers* handlers, uint16_t* out, const is_device_usb_device* device_desc) {
|
||||
int ret = 0;
|
||||
uint32_t counter = 0;
|
||||
switch(device_desc->device_type) {
|
||||
case IS_NITRO_EMULATOR_DEVICE:
|
||||
ReadNecMemU32(handlers, 0x08000028, &counter, device_desc);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
*out = (counter & 0xFF) | ((counter & 0xFF0000) >> 8);
|
||||
return ret;
|
||||
case IS_NITRO_CAPTURE_DEVICE:
|
||||
*out = 0;
|
||||
return LIBUSB_SUCCESS;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int UpdateFrameForwardConfig(is_device_device_handlers* handlers, is_device_forward_config_values_colors colors, is_device_forward_config_values_screens screens, is_device_forward_config_values_rate rate, const is_device_usb_device* device_desc) {
|
||||
int ret = 0;
|
||||
switch(device_desc->device_type) {
|
||||
case IS_NITRO_EMULATOR_DEVICE:
|
||||
return WriteNecMemU16(handlers, 0x0800000A, ((colors & 1) << 4) | ((screens & 3) << 2) | ((rate & 3) << 0), device_desc);
|
||||
case IS_NITRO_CAPTURE_DEVICE:
|
||||
ret = SendWriteCommandU32(handlers, IS_NITRO_CAP_CMD_SET_FWD_RATE, rate & 3, device_desc);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
ret = SendWriteCommandU32(handlers, IS_NITRO_CAP_CMD_SET_FWD_MODE, screens & 3, device_desc);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
return SendWriteCommandU32(handlers, IS_NITRO_CAP_CMD_SET_FWD_COLOURS, colors & 1, device_desc);
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int UpdateFrameForwardEnable(is_device_device_handlers* handlers, bool enable, bool restart, const is_device_usb_device* device_desc) {
|
||||
uint32_t value = 0;
|
||||
switch(device_desc->device_type) {
|
||||
case IS_NITRO_EMULATOR_DEVICE:
|
||||
if(enable)
|
||||
value |= (1 << IS_NITRO_EMULATOR_FORWARD_ENABLE_BIT);
|
||||
if(restart)
|
||||
value |= (1 << IS_NITRO_EMULATOR_FORWARD_COUNTER_RESTART_BIT);
|
||||
return WriteNecMemU16(handlers, 0x08000008, value, device_desc);
|
||||
case IS_NITRO_CAPTURE_DEVICE:
|
||||
if(restart)
|
||||
value |= (1 << IS_NITRO_CAPTURE_FORWARD_COUNTER_RESTART_BIT);
|
||||
return SendWriteCommandU32(handlers, IS_NITRO_CAP_CMD_SET_FWD_RESTART, value, device_desc);
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int ReadLidState(is_device_device_handlers* handlers, bool* out, const is_device_usb_device* device_desc) {
|
||||
uint32_t flags = 0;
|
||||
int ret = 0;
|
||||
switch(device_desc->device_type) {
|
||||
case IS_NITRO_EMULATOR_DEVICE:
|
||||
ret = ReadNecMemU32(handlers, 0x08000000, &flags, device_desc);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
*out = (flags & 2) ? true : false;
|
||||
return ret;
|
||||
case IS_NITRO_CAPTURE_DEVICE:
|
||||
ret = SendReadCommandU32(handlers, IS_NITRO_CAP_CMD_GET_LID_STATE, &flags, device_desc);
|
||||
*out = (flags & 1) ? true : false;
|
||||
return ret;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int ReadDebugButtonState(is_device_device_handlers* handlers, bool* out, const is_device_usb_device* device_desc) {
|
||||
uint32_t flags = 0;
|
||||
int ret = 0;
|
||||
switch(device_desc->device_type) {
|
||||
case IS_NITRO_EMULATOR_DEVICE:
|
||||
ret = ReadNecMemU32(handlers, 0x08000000, &flags, device_desc);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
*out = (flags & 1) ? true : false;
|
||||
return ret;
|
||||
case IS_NITRO_CAPTURE_DEVICE:
|
||||
ret = SendReadCommandU32(handlers, IS_NITRO_CAP_CMD_GET_DEBUG_STATE, &flags, device_desc);
|
||||
*out = (flags & 1) ? true : false;
|
||||
return ret;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int ReadPowerButtonState(is_device_device_handlers* handlers, bool* out, const is_device_usb_device* device_desc) {
|
||||
return ReadDebugButtonState(handlers, out, device_desc);
|
||||
}
|
||||
|
||||
static int ResetCPUEmulatorGeneral(is_device_device_handlers* handlers, bool on, const is_device_usb_device* device_desc) {
|
||||
uint8_t data[] = {IS_NITRO_EMU_CMD_CPU_RESET, 0, (uint8_t)(on ? 1 : 0), 0};
|
||||
return SendWriteCommand(handlers, IS_NITRO_EMU_CMD_CPU_RESET, data, sizeof(data), device_desc);
|
||||
}
|
||||
|
||||
int ResetCPUStart(is_device_device_handlers* handlers, const is_device_usb_device* device_desc) {
|
||||
switch(device_desc->device_type) {
|
||||
case IS_NITRO_EMULATOR_DEVICE:
|
||||
return ResetCPUEmulatorGeneral(handlers, true, device_desc);
|
||||
case IS_NITRO_CAPTURE_DEVICE:
|
||||
return SendWriteCommand(handlers, IS_NITRO_CAP_CMD_SET_RESET_CPU_ON, NULL, 0, device_desc);
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int ResetCPUEnd(is_device_device_handlers* handlers, const is_device_usb_device* device_desc) {
|
||||
switch(device_desc->device_type) {
|
||||
case IS_NITRO_EMULATOR_DEVICE:
|
||||
return ResetCPUEmulatorGeneral(handlers, false, device_desc);
|
||||
case IS_NITRO_CAPTURE_DEVICE:
|
||||
return SendWriteCommand(handlers, IS_NITRO_CAP_CMD_SET_RESET_CPU_OFF, NULL, 0, device_desc);
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int ResetFullHardware(is_device_device_handlers* handlers, const is_device_usb_device* device_desc) {
|
||||
uint8_t isn_emu_rst_data[] = {IS_NITRO_EMU_CMD_FULL_HARDWARE_RESET, 0xF2};
|
||||
switch(device_desc->device_type) {
|
||||
case IS_NITRO_EMULATOR_DEVICE:
|
||||
return SendWriteCommand(handlers, IS_NITRO_EMU_CMD_FULL_HARDWARE_RESET, isn_emu_rst_data, sizeof(isn_emu_rst_data), device_desc);
|
||||
case IS_NITRO_CAPTURE_DEVICE:
|
||||
return SendWriteCommand(handlers, IS_NITRO_CAP_CMD_SET_RESTART_FULL, NULL, 0, device_desc);
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int ReadFrame(is_device_device_handlers* handlers, uint8_t* buf, int length, const is_device_usb_device* device_desc) {
|
||||
// Maybe making this async would be better for lower end hardware...
|
||||
int num_bytes = 0;
|
||||
int ret = bulk_in(handlers, device_desc, buf, length, &num_bytes);
|
||||
if(num_bytes != length)
|
||||
return LIBUSB_ERROR_INTERRUPTED;
|
||||
return ret;
|
||||
}
|
||||
|
||||
void ReadFrameAsync(is_device_device_handlers* handlers, uint8_t* buf, int length, const is_device_usb_device* device_desc, isd_async_callback_data* cb_data) {
|
||||
return bulk_in_async(handlers, device_desc, buf, length, cb_data);
|
||||
}
|
||||
|
||||
void CloseAsyncRead(is_device_device_handlers* handlers, isd_async_callback_data* cb_data) {
|
||||
if(handlers->usb_handle)
|
||||
return is_device_libusb_cancell_callback(cb_data);
|
||||
return is_device_is_driver_cancel_callback(cb_data);
|
||||
}
|
||||
|
||||
int ResetUSBDevice(is_device_device_handlers* handlers) {
|
||||
if(handlers->usb_handle)
|
||||
return is_device_libusb_reset_device(handlers);
|
||||
return is_device_is_driver_reset_device(handlers);
|
||||
}
|
||||
|
||||
void SetupISDeviceAsyncThread(is_device_device_handlers* handlers, void* user_data, std::thread* thread_ptr, bool* keep_going, ConsumerMutex* is_data_ready) {
|
||||
if(handlers->usb_handle)
|
||||
return is_device_libusb_start_thread(thread_ptr, keep_going);
|
||||
return is_device_is_driver_start_thread(thread_ptr, keep_going, (ISDeviceCaptureReceivedData*)user_data, handlers, is_data_ready);
|
||||
}
|
||||
|
||||
void EndISDeviceAsyncThread(is_device_device_handlers* handlers, void* user_data, std::thread* thread_ptr, bool* keep_going, ConsumerMutex* is_data_ready) {
|
||||
if(handlers->usb_handle)
|
||||
return is_device_libusb_close_thread(thread_ptr, keep_going);
|
||||
return is_device_is_driver_close_thread(thread_ptr, keep_going, (ISDeviceCaptureReceivedData*)user_data);
|
||||
}
|
||||
@@ -0,0 +1,315 @@
|
||||
#include "usb_is_device_communications.hpp"
|
||||
#include "usb_is_device_setup_general.hpp"
|
||||
#include "usb_is_device_is_driver.hpp"
|
||||
#include "frontend.hpp"
|
||||
#include "utils.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <filesystem>
|
||||
#ifdef _WIN32
|
||||
#include <setupapi.h>
|
||||
|
||||
// Code based off of Gericom's sample code. Distributed under the MIT License. Copyright (c) 2024 Gericom
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// 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
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
// IN THE SOFTWARE.
|
||||
|
||||
const GUID is_device_driver_guid = { .Data1 = 0xB78D7ADA, .Data2 = 0xDDF4, .Data3 = 0x418F, .Data4 = {0x8C, 0x7C, 0x4A, 0xC8, 0x80, 0x30, 0xF5, 0x42} };
|
||||
|
||||
static void is_device_is_driver_function(bool* usb_thread_run, ISDeviceCaptureReceivedData* is_device_capture_recv_data, is_device_device_handlers* handlers) {
|
||||
while(*usb_thread_run) {
|
||||
for (int i = 0; i < NUM_CAPTURE_RECEIVED_DATA_BUFFERS; i++) {
|
||||
isd_async_callback_data* cb_data = (isd_async_callback_data*)&is_device_capture_recv_data[i].cb_data;
|
||||
bool read_data_ready = false;
|
||||
cb_data->transfer_data_access.lock();
|
||||
read_data_ready = cb_data->is_data_ready;
|
||||
cb_data->is_data_ready = false;
|
||||
cb_data->transfer_data_access.unlock();
|
||||
if(is_device_capture_recv_data[i].in_use && read_data_ready)
|
||||
cb_data->function(&is_device_capture_recv_data[i], cb_data->actual_length, cb_data->status_value);
|
||||
}
|
||||
int dummy = 0;
|
||||
is_device_capture_recv_data[0].cb_data.is_transfer_data_ready_mutex->general_timed_lock(&dummy);
|
||||
}
|
||||
}
|
||||
|
||||
static bool read_is_driver_device_info(HANDLE handle, uint8_t* buffer, size_t size, DWORD* num_read) {
|
||||
return DeviceIoControl(handle, 0x22000C, buffer, size, buffer, size, num_read, NULL);
|
||||
}
|
||||
|
||||
static bool is_driver_device_reset(HANDLE handle) {
|
||||
DWORD num_read;
|
||||
return DeviceIoControl(handle, 0x220004, NULL, 0, NULL, 0, &num_read, NULL);
|
||||
}
|
||||
|
||||
static bool is_driver_pipe_reset(HANDLE handle) {
|
||||
DWORD num_read;
|
||||
return DeviceIoControl(handle, 0x220008, NULL, 0, NULL, 0, &num_read, NULL);
|
||||
}
|
||||
|
||||
static std::string is_driver_get_device_path(HDEVINFO DeviceInfoSet, SP_DEVICE_INTERFACE_DATA* DeviceInterfaceData) {
|
||||
std::string result = "";
|
||||
// Call this with an empty buffer to the required size of the structure.
|
||||
ULONG requiredSize;
|
||||
SetupDiGetDeviceInterfaceDetail(DeviceInfoSet, DeviceInterfaceData, NULL, 0, &requiredSize, NULL);
|
||||
if(GetLastError() != ERROR_INSUFFICIENT_BUFFER)
|
||||
return result;
|
||||
|
||||
PSP_DEVICE_INTERFACE_DETAIL_DATA devInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)(new uint8_t[requiredSize]);
|
||||
if(!devInterfaceDetailData)
|
||||
return result;
|
||||
|
||||
devInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
|
||||
if(SetupDiGetDeviceInterfaceDetail(DeviceInfoSet, DeviceInterfaceData, devInterfaceDetailData, requiredSize, &requiredSize, NULL))
|
||||
result = (std::string)(devInterfaceDetailData->DevicePath);
|
||||
|
||||
delete []((uint8_t*)devInterfaceDetailData);
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool is_driver_get_device_pid_vid(std::string path, uint16_t& out_vid, uint16_t& out_pid) {
|
||||
HANDLE handle = CreateFile(path.c_str(), (GENERIC_READ | GENERIC_WRITE), (FILE_SHARE_READ | FILE_SHARE_WRITE), NULL, OPEN_EXISTING, 0, NULL);
|
||||
if (handle == INVALID_HANDLE_VALUE)
|
||||
return false;
|
||||
uint8_t buffer[0x412];
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
DWORD num_read = 0;
|
||||
bool result = read_is_driver_device_info(handle, buffer, sizeof(buffer), &num_read);
|
||||
CloseHandle(handle);
|
||||
if(!result)
|
||||
return false;
|
||||
if (num_read < 11)
|
||||
return false;
|
||||
out_vid = buffer[8] + (buffer[9] << 8);
|
||||
out_pid = buffer[10] + (buffer[11] << 8);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool is_driver_setup_connection(is_device_device_handlers* handlers, std::string path) {
|
||||
handlers->usb_handle = NULL;
|
||||
handlers->mutex = NULL;
|
||||
handlers->write_handle = INVALID_HANDLE_VALUE;
|
||||
handlers->read_handle = INVALID_HANDLE_VALUE;
|
||||
std::string mutex_name = "Global\\ISU_" + path.substr(4);
|
||||
std::replace(mutex_name.begin(), mutex_name.end(), '\\', '@');
|
||||
handlers->mutex = CreateMutex(NULL, true, mutex_name.c_str());
|
||||
if ((handlers->mutex != NULL) && (GetLastError() == ERROR_ALREADY_EXISTS)) {
|
||||
CloseHandle(handlers->mutex);
|
||||
handlers->mutex = NULL;
|
||||
}
|
||||
if (handlers->mutex == NULL)
|
||||
return false;
|
||||
handlers->write_handle = CreateFile((path + (char)(std::filesystem::path::preferred_separator)+"Pipe00").c_str(), (GENERIC_READ | GENERIC_WRITE), (FILE_SHARE_READ | FILE_SHARE_WRITE), NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
|
||||
handlers->read_handle = CreateFile((path + (char)(std::filesystem::path::preferred_separator)+"Pipe01").c_str(), (GENERIC_READ | GENERIC_WRITE), (FILE_SHARE_READ | FILE_SHARE_WRITE), NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
|
||||
if ((handlers->write_handle == INVALID_HANDLE_VALUE) || (handlers->read_handle == INVALID_HANDLE_VALUE))
|
||||
return false;
|
||||
if (!is_driver_device_reset(handlers->write_handle))
|
||||
return false;
|
||||
if (!is_driver_pipe_reset(handlers->write_handle))
|
||||
return false;
|
||||
if (!is_driver_pipe_reset(handlers->read_handle))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
static int wait_result_io_operation(HANDLE handle, OVERLAPPED* overlapped_var, int* transferred, const is_device_usb_device* usb_device_desc) {
|
||||
DWORD num_bytes = 0;
|
||||
int retval = 0;
|
||||
int error = 0;
|
||||
do {
|
||||
retval = GetOverlappedResultEx(handle, overlapped_var, &num_bytes, usb_device_desc->bulk_timeout, true);
|
||||
error = GetLastError();
|
||||
} while((retval == 0) && (error == WAIT_IO_COMPLETION));
|
||||
*transferred = num_bytes;
|
||||
if (retval == 0) {
|
||||
if (error == WAIT_TIMEOUT) {
|
||||
CancelIoEx(handle, overlapped_var);
|
||||
return LIBUSB_ERROR_TIMEOUT;
|
||||
}
|
||||
return LIBUSB_ERROR_OTHER;
|
||||
}
|
||||
return LIBUSB_SUCCESS;
|
||||
}
|
||||
|
||||
static void STDCALL BasicCompletionOutputRoutine(isd_async_callback_data* cb_data, int num_bytes, int error) {
|
||||
cb_data->transfer_data_access.lock();
|
||||
if ((error == LIBUSB_SUCCESS) && (num_bytes != cb_data->requested_length))
|
||||
error = LIBUSB_ERROR_INTERRUPTED;
|
||||
cb_data->transfer_data = NULL;
|
||||
cb_data->actual_length = num_bytes;
|
||||
cb_data->status_value = error;
|
||||
cb_data->is_data_ready = true;
|
||||
cb_data->is_transfer_data_ready_mutex->specific_unlock(cb_data->internal_index);
|
||||
cb_data->is_transfer_done_mutex->specific_unlock(cb_data->internal_index);
|
||||
cb_data->transfer_data_access.unlock();
|
||||
}
|
||||
|
||||
static void STDCALL OverlappedCompletionNothingRoutine(DWORD dwErrorCode, DWORD dwNumberOfBytesTransfered, OVERLAPPED* overlapped_var) {
|
||||
return;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
is_device_device_handlers* is_driver_serial_reconnection(CaptureDevice* device) {
|
||||
is_device_device_handlers* final_handlers = NULL;
|
||||
#ifdef _WIN32
|
||||
if (device->path != "") {
|
||||
is_device_device_handlers handlers;
|
||||
if (is_driver_setup_connection(&handlers, device->path)) {
|
||||
final_handlers = new is_device_device_handlers;
|
||||
final_handlers->usb_handle = NULL;
|
||||
final_handlers->mutex = handlers.mutex;
|
||||
final_handlers->read_handle = handlers.read_handle;
|
||||
final_handlers->write_handle = handlers.write_handle;
|
||||
}
|
||||
else
|
||||
is_driver_end_connection(&handlers);
|
||||
}
|
||||
#endif
|
||||
return final_handlers;
|
||||
}
|
||||
|
||||
void is_driver_end_connection(is_device_device_handlers* handlers) {
|
||||
#ifdef _WIN32
|
||||
if (handlers->write_handle != INVALID_HANDLE_VALUE)
|
||||
CloseHandle(handlers->write_handle);
|
||||
handlers->write_handle = INVALID_HANDLE_VALUE;
|
||||
if (handlers->read_handle != INVALID_HANDLE_VALUE)
|
||||
CloseHandle(handlers->read_handle);
|
||||
handlers->read_handle = INVALID_HANDLE_VALUE;
|
||||
if(handlers->mutex != NULL)
|
||||
CloseHandle(handlers->mutex);
|
||||
handlers->mutex = NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
void is_driver_list_devices(std::vector<CaptureDevice> &devices_list, bool* not_supported_elems, int *curr_serial_extra_id_is_device, const size_t num_is_device_desc) {
|
||||
#ifdef _WIN32
|
||||
HDEVINFO DeviceInfoSet = SetupDiGetClassDevs(
|
||||
&is_device_driver_guid,
|
||||
NULL,
|
||||
NULL,
|
||||
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
|
||||
|
||||
SP_DEVICE_INTERFACE_DATA DeviceInterfaceData;
|
||||
ZeroMemory(&DeviceInterfaceData, sizeof(SP_DEVICE_INTERFACE_DATA));
|
||||
DeviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
|
||||
uint32_t i = 0;
|
||||
while (SetupDiEnumDeviceInterfaces(DeviceInfoSet, NULL, &is_device_driver_guid, i++, &DeviceInterfaceData)) {
|
||||
std::string path = is_driver_get_device_path(DeviceInfoSet, &DeviceInterfaceData);
|
||||
if (path == "")
|
||||
continue;
|
||||
uint16_t vid = 0;
|
||||
uint16_t pid = 0;
|
||||
if(!is_driver_get_device_pid_vid(path, vid, pid))
|
||||
continue;
|
||||
for (int j = 0; j < num_is_device_desc; j++) {
|
||||
const is_device_usb_device* usb_device_desc = GetISDeviceDesc(j);
|
||||
if(not_supported_elems[j] && (usb_device_desc->vid == vid) && (usb_device_desc->pid == pid)) {
|
||||
is_device_device_handlers handlers;
|
||||
if(is_driver_setup_connection(&handlers, path))
|
||||
is_device_insert_device(devices_list, &handlers, usb_device_desc, curr_serial_extra_id_is_device[j], path);
|
||||
is_driver_end_connection(&handlers);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (DeviceInfoSet) {
|
||||
SetupDiDestroyDeviceInfoList(DeviceInfoSet);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// Write to bulk
|
||||
int is_driver_bulk_out(is_device_device_handlers* handlers, const is_device_usb_device* usb_device_desc, uint8_t* buf, int length, int* transferred) {
|
||||
#ifdef _WIN32
|
||||
OVERLAPPED overlapped_var;
|
||||
memset(&overlapped_var, 0, sizeof(OVERLAPPED));
|
||||
DWORD num_bytes = 0;
|
||||
int retval = WriteFileEx(handlers->write_handle, buf, length, &overlapped_var, OverlappedCompletionNothingRoutine);
|
||||
if(retval == 0)
|
||||
return LIBUSB_ERROR_BUSY;
|
||||
return wait_result_io_operation(handlers->write_handle, &overlapped_var, transferred, usb_device_desc);
|
||||
#endif
|
||||
return LIBUSB_SUCCESS;
|
||||
}
|
||||
|
||||
// Read from bulk
|
||||
int is_driver_bulk_in(is_device_device_handlers* handlers, const is_device_usb_device* usb_device_desc, uint8_t* buf, int length, int* transferred) {
|
||||
#ifdef _WIN32
|
||||
OVERLAPPED overlapped_var;
|
||||
memset(&overlapped_var, 0, sizeof(OVERLAPPED));
|
||||
int retval = ReadFileEx(handlers->read_handle, buf, length, &overlapped_var, OverlappedCompletionNothingRoutine);
|
||||
if (retval == 0)
|
||||
return LIBUSB_ERROR_BUSY;
|
||||
return wait_result_io_operation(handlers->read_handle, &overlapped_var, transferred, usb_device_desc);
|
||||
#endif
|
||||
return LIBUSB_SUCCESS;
|
||||
}
|
||||
|
||||
int is_drive_async_in_start(is_device_device_handlers* handlers, const is_device_usb_device* usb_device_desc, uint8_t* buf, int length, isd_async_callback_data* cb_data) {
|
||||
#ifdef _WIN32
|
||||
cb_data->transfer_data_access.lock();
|
||||
cb_data->is_data_ready = false;
|
||||
cb_data->transfer_data = cb_data->base_transfer_data;
|
||||
cb_data->handle = handlers->read_handle;
|
||||
cb_data->requested_length = length;
|
||||
cb_data->buffer = buf;
|
||||
OVERLAPPED* overlap_data = (OVERLAPPED*)cb_data->transfer_data;
|
||||
memset(overlap_data, 0, sizeof(OVERLAPPED));
|
||||
cb_data->is_transfer_done_mutex->specific_try_lock(cb_data->internal_index);
|
||||
cb_data->is_transfer_data_ready_mutex->specific_try_lock(cb_data->internal_index);
|
||||
int retval = ReadFileEx(cb_data->handle, cb_data->buffer, cb_data->requested_length, (OVERLAPPED*)cb_data->transfer_data, OverlappedCompletionNothingRoutine);
|
||||
cb_data->transfer_data_access.unlock();
|
||||
if (retval == 0)
|
||||
return LIBUSB_ERROR_BUSY;
|
||||
int num_bytes = 0;
|
||||
int error = wait_result_io_operation(handlers->read_handle, (OVERLAPPED*)overlap_data, &num_bytes, usb_device_desc);
|
||||
BasicCompletionOutputRoutine(cb_data, num_bytes, error);
|
||||
#endif
|
||||
return LIBUSB_SUCCESS;
|
||||
}
|
||||
|
||||
void is_device_is_driver_start_thread(std::thread* thread_ptr, bool* usb_thread_run, ISDeviceCaptureReceivedData* is_device_capture_recv_data, is_device_device_handlers* handlers, ConsumerMutex* AsyncMutexPtr) {
|
||||
#ifdef _WIN32
|
||||
*usb_thread_run = true;
|
||||
for (int i = 0; i < NUM_CAPTURE_RECEIVED_DATA_BUFFERS; i++)
|
||||
is_device_capture_recv_data[i].cb_data.base_transfer_data = new OVERLAPPED;
|
||||
*thread_ptr = std::thread(is_device_is_driver_function, usb_thread_run, is_device_capture_recv_data, handlers);
|
||||
#endif
|
||||
}
|
||||
|
||||
void is_device_is_driver_close_thread(std::thread* thread_ptr, bool* usb_thread_run, ISDeviceCaptureReceivedData* is_device_capture_recv_data) {
|
||||
#ifdef _WIN32
|
||||
*usb_thread_run = false;
|
||||
is_device_capture_recv_data[0].cb_data.is_transfer_data_ready_mutex->specific_unlock(0);
|
||||
thread_ptr->join();
|
||||
for (int i = 0; i < NUM_CAPTURE_RECEIVED_DATA_BUFFERS; i++)
|
||||
delete is_device_capture_recv_data[i].cb_data.base_transfer_data;
|
||||
#endif
|
||||
}
|
||||
|
||||
int is_device_is_driver_reset_device(is_device_device_handlers* handlers) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void is_device_is_driver_cancel_callback(isd_async_callback_data* cb_data) {
|
||||
#ifdef _WIN32
|
||||
// Nothing
|
||||
#endif
|
||||
}
|
||||
197
source/CaptureDeviceSpecific/ISDevices/usb_is_device_libusb.cpp
Normal file
197
source/CaptureDeviceSpecific/ISDevices/usb_is_device_libusb.cpp
Normal file
@@ -0,0 +1,197 @@
|
||||
#include "usb_is_device_communications.hpp"
|
||||
#include "usb_is_device_setup_general.hpp"
|
||||
#include "usb_is_device_libusb.hpp"
|
||||
#include "usb_generic.hpp"
|
||||
|
||||
#include <libusb.h>
|
||||
#include <frontend.hpp>
|
||||
|
||||
// Code based off of Gericom's sample code. Distributed under the MIT License. Copyright (c) 2024 Gericom
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// 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
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
// IN THE SOFTWARE.
|
||||
|
||||
static void is_device_usb_thread_function(bool* usb_thread_run) {
|
||||
if(!usb_is_initialized())
|
||||
return;
|
||||
struct timeval tv;
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = 300000;
|
||||
while(*usb_thread_run)
|
||||
libusb_handle_events_timeout_completed(get_usb_ctx(), &tv, NULL);
|
||||
}
|
||||
|
||||
void is_device_libusb_start_thread(std::thread* thread_ptr, bool* usb_thread_run) {
|
||||
if(!usb_is_initialized())
|
||||
return;
|
||||
*usb_thread_run = true;
|
||||
*thread_ptr = std::thread(is_device_usb_thread_function, usb_thread_run);
|
||||
}
|
||||
|
||||
void is_device_libusb_close_thread(std::thread* thread_ptr, bool* usb_thread_run) {
|
||||
if(!usb_is_initialized())
|
||||
return;
|
||||
*usb_thread_run = false;
|
||||
thread_ptr->join();
|
||||
}
|
||||
|
||||
static bool is_device_libusb_setup_connection(libusb_device_handle* handle, const is_device_usb_device* usb_device_desc) {
|
||||
if (libusb_set_configuration(handle, usb_device_desc->default_config) != LIBUSB_SUCCESS)
|
||||
return false;
|
||||
if(libusb_claim_interface(handle, usb_device_desc->default_interface) != LIBUSB_SUCCESS)
|
||||
return false;
|
||||
if(libusb_clear_halt(handle, usb_device_desc->ep1_out) != LIBUSB_SUCCESS)
|
||||
return false;
|
||||
if(libusb_clear_halt(handle, usb_device_desc->ep2_in) != LIBUSB_SUCCESS)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
static int is_device_libusb_insert_device(std::vector<CaptureDevice> &devices_list, const is_device_usb_device* usb_device_desc, libusb_device *usb_device, libusb_device_descriptor *usb_descriptor, int &curr_serial_extra_id) {
|
||||
libusb_device_handle *handle = NULL;
|
||||
if((usb_descriptor->idVendor != usb_device_desc->vid) || (usb_descriptor->idProduct != usb_device_desc->pid))
|
||||
return LIBUSB_ERROR_NOT_FOUND;
|
||||
if((usb_descriptor->iManufacturer != usb_device_desc->manufacturer_id) || (usb_descriptor->iProduct != usb_device_desc->product_id))
|
||||
return LIBUSB_ERROR_NOT_FOUND;
|
||||
int result = libusb_open(usb_device, &handle);
|
||||
if((result < 0) || (handle == NULL))
|
||||
return result;
|
||||
if(is_device_libusb_setup_connection(handle, usb_device_desc)) {
|
||||
is_device_device_handlers handlers;
|
||||
handlers.usb_handle = handle;
|
||||
is_device_insert_device(devices_list, &handlers, usb_device_desc, curr_serial_extra_id);
|
||||
libusb_release_interface(handle, usb_device_desc->default_interface);
|
||||
}
|
||||
libusb_close(handle);
|
||||
return result;
|
||||
}
|
||||
|
||||
is_device_device_handlers* is_device_libusb_serial_reconnection(const is_device_usb_device* usb_device_desc, CaptureDevice* device, int &curr_serial_extra_id) {
|
||||
if(!usb_is_initialized())
|
||||
return NULL;
|
||||
libusb_device **usb_devices;
|
||||
int num_devices = libusb_get_device_list(get_usb_ctx(), &usb_devices);
|
||||
libusb_device_descriptor usb_descriptor{};
|
||||
is_device_device_handlers* final_handlers = NULL;
|
||||
|
||||
for(int i = 0; i < num_devices; i++) {
|
||||
is_device_device_handlers handlers;
|
||||
int result = libusb_get_device_descriptor(usb_devices[i], &usb_descriptor);
|
||||
if(result < 0)
|
||||
continue;
|
||||
if((usb_descriptor.idVendor != usb_device_desc->vid) || (usb_descriptor.idProduct != usb_device_desc->pid))
|
||||
continue;
|
||||
if((usb_descriptor.iManufacturer != usb_device_desc->manufacturer_id) || (usb_descriptor.iProduct != usb_device_desc->product_id))
|
||||
continue;
|
||||
result = libusb_open(usb_devices[i], &handlers.usb_handle);
|
||||
if(result || (handlers.usb_handle == NULL))
|
||||
continue;
|
||||
if (is_device_libusb_setup_connection(handlers.usb_handle, usb_device_desc)) {
|
||||
std::string device_serial_number = get_serial(usb_device_desc, &handlers, curr_serial_extra_id);
|
||||
if (device->serial_number == device_serial_number) {
|
||||
final_handlers = new is_device_device_handlers;
|
||||
final_handlers->usb_handle = handlers.usb_handle;
|
||||
break;
|
||||
}
|
||||
libusb_release_interface(handlers.usb_handle, usb_device_desc->default_interface);
|
||||
}
|
||||
libusb_close(handlers.usb_handle);
|
||||
}
|
||||
|
||||
if(num_devices >= 0)
|
||||
libusb_free_device_list(usb_devices, 1);
|
||||
|
||||
return final_handlers;
|
||||
}
|
||||
|
||||
void is_device_libusb_end_connection(is_device_device_handlers* handlers, const is_device_usb_device* device_desc, bool interface_claimed) {
|
||||
if (interface_claimed)
|
||||
libusb_release_interface(handlers->usb_handle, device_desc->default_interface);
|
||||
libusb_close(handlers->usb_handle);
|
||||
handlers->usb_handle = NULL;
|
||||
}
|
||||
|
||||
void is_device_libusb_list_devices(std::vector<CaptureDevice> &devices_list, bool* no_access_elems, bool* not_supported_elems, int* curr_serial_extra_id_is_device, const size_t num_is_device_desc) {
|
||||
if(!usb_is_initialized())
|
||||
return;
|
||||
libusb_device **usb_devices;
|
||||
int num_devices = libusb_get_device_list(get_usb_ctx(), &usb_devices);
|
||||
libusb_device_descriptor usb_descriptor{};
|
||||
|
||||
for(int i = 0; i < num_devices; i++) {
|
||||
int result = libusb_get_device_descriptor(usb_devices[i], &usb_descriptor);
|
||||
if(result < 0)
|
||||
continue;
|
||||
for (int j = 0; j < num_is_device_desc; j++) {
|
||||
result = is_device_libusb_insert_device(devices_list, GetISDeviceDesc(j), usb_devices[i], &usb_descriptor, curr_serial_extra_id_is_device[j]);
|
||||
if (result != LIBUSB_ERROR_NOT_FOUND) {
|
||||
if (result == LIBUSB_ERROR_ACCESS)
|
||||
no_access_elems[j] = true;
|
||||
if (result == LIBUSB_ERROR_NOT_SUPPORTED)
|
||||
not_supported_elems[j] = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(num_devices >= 0)
|
||||
libusb_free_device_list(usb_devices, 1);
|
||||
}
|
||||
|
||||
// Write to bulk
|
||||
int is_device_libusb_bulk_out(is_device_device_handlers* handlers, const is_device_usb_device* usb_device_desc, uint8_t* buf, int length, int* transferred) {
|
||||
return libusb_bulk_transfer(handlers->usb_handle, usb_device_desc->ep1_out, buf, length, transferred, usb_device_desc->bulk_timeout);
|
||||
}
|
||||
|
||||
// Read from bulk
|
||||
int is_device_libusb_bulk_in(is_device_device_handlers* handlers, const is_device_usb_device* usb_device_desc, uint8_t* buf, int length, int* transferred) {
|
||||
return libusb_bulk_transfer(handlers->usb_handle, usb_device_desc->ep2_in, buf, length, transferred, usb_device_desc->bulk_timeout);
|
||||
}
|
||||
|
||||
void is_device_libusb_cancell_callback(isd_async_callback_data* cb_data) {
|
||||
cb_data->transfer_data_access.lock();
|
||||
if(cb_data->transfer_data)
|
||||
libusb_cancel_transfer((libusb_transfer*)cb_data->transfer_data);
|
||||
cb_data->transfer_data_access.unlock();
|
||||
}
|
||||
|
||||
int is_device_libusb_reset_device(is_device_device_handlers* handlers) {
|
||||
return libusb_reset_device(handlers->usb_handle);
|
||||
}
|
||||
|
||||
void STDCALL is_device_libusb_async_callback(libusb_transfer* transfer) {
|
||||
isd_async_callback_data* cb_data = (isd_async_callback_data*)transfer->user_data;
|
||||
cb_data->transfer_data_access.lock();
|
||||
cb_data->transfer_data = NULL;
|
||||
cb_data->is_transfer_done_mutex->specific_unlock(cb_data->internal_index);
|
||||
cb_data->transfer_data_access.unlock();
|
||||
cb_data->function(cb_data->actual_user_data, transfer->actual_length, transfer->status);
|
||||
}
|
||||
|
||||
// Read from bulk
|
||||
void is_device_libusb_async_in_start(is_device_device_handlers* handlers, const is_device_usb_device* usb_device_desc, uint8_t* buf, int length, isd_async_callback_data* cb_data) {
|
||||
libusb_transfer *transfer_in = libusb_alloc_transfer(0);
|
||||
if(!transfer_in)
|
||||
return;
|
||||
cb_data->transfer_data_access.lock();
|
||||
cb_data->transfer_data = transfer_in;
|
||||
cb_data->is_transfer_done_mutex->specific_try_lock(cb_data->internal_index);
|
||||
libusb_fill_bulk_transfer(transfer_in, handlers->usb_handle, usb_device_desc->ep2_in, buf, length, is_device_libusb_async_callback, cb_data, usb_device_desc->bulk_timeout);
|
||||
transfer_in->flags |= LIBUSB_TRANSFER_FREE_TRANSFER;
|
||||
libusb_submit_transfer(transfer_in);
|
||||
cb_data->transfer_data_access.unlock();
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
#include "devicecapture.hpp"
|
||||
#include "usb_is_device_communications.hpp"
|
||||
#include "usb_is_device_acquisition_general.hpp"
|
||||
#include "usb_is_nitro_acquisition_capture.hpp"
|
||||
|
||||
// Code created by analyzing the USB packets sent and received by the IS Nitro Capture device.
|
||||
|
||||
// The code for video capture of the IS Nitro Emulator and of the IS Nitro Capture is wildly different.
|
||||
// For this reason, the code was split into two device-specific files.
|
||||
|
||||
#define CAPTURE_SKIP_LID_REOPEN_FRAMES 32
|
||||
#define RESET_TIMEOUT 4.0
|
||||
#define SLEEP_CHECKS_TIME_MS 20
|
||||
#define SLEEP_RESET_TIME_MS 2000
|
||||
|
||||
static int StartAcquisitionCapture(CaptureData* capture_data, ISDeviceCaptureReceivedData* is_device_capture_recv_data, CaptureScreensType capture_type, CaptureSpeedsType capture_speed, bool* is_acquisition_off, uint32_t &index) {
|
||||
is_device_device_handlers* handlers = (is_device_device_handlers*)capture_data->handle;
|
||||
const is_device_usb_device* usb_device_desc = (const is_device_usb_device*)capture_data->status.device.descriptor;
|
||||
int ret = 0;
|
||||
ret = ReadLidState(handlers, is_acquisition_off, usb_device_desc);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
if(*is_acquisition_off)
|
||||
return LIBUSB_SUCCESS;
|
||||
ret = set_acquisition_mode(handlers, capture_type, capture_speed, usb_device_desc);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
ret = SetForwardFramePermanent(handlers, usb_device_desc);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
ret = UpdateFrameForwardEnable(handlers, true, true, usb_device_desc);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
ret = StartUsbCaptureDma(handlers, usb_device_desc);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
reset_is_device_status(is_device_capture_recv_data);
|
||||
for(int i = 0; i < NUM_CAPTURE_RECEIVED_DATA_BUFFERS; i++)
|
||||
is_device_read_frame_request(capture_data, is_device_get_free_buffer(capture_data, is_device_capture_recv_data), capture_type, index++);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int LidReopenCaptureCheck(CaptureData* capture_data, ISDeviceCaptureReceivedData* is_device_capture_recv_data, CaptureScreensType &curr_capture_type, CaptureSpeedsType &curr_capture_speed, bool* is_acquisition_off, uint32_t &index) {
|
||||
curr_capture_type = capture_data->status.capture_type;
|
||||
curr_capture_speed = capture_data->status.capture_speed;
|
||||
int ret = StartAcquisitionCapture(capture_data, is_device_capture_recv_data, curr_capture_type, curr_capture_speed, is_acquisition_off, index);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
if(!(*is_acquisition_off))
|
||||
capture_data->status.cooldown_curr_in = CAPTURE_SKIP_LID_REOPEN_FRAMES;
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int CaptureResetHardware(CaptureData* capture_data, ISDeviceCaptureReceivedData* is_device_capture_recv_data, CaptureScreensType &curr_capture_type, CaptureSpeedsType &curr_capture_speed, bool *is_acquisition_off, std::chrono::time_point<std::chrono::high_resolution_clock> &clock_last_reset, uint32_t &index) {
|
||||
is_device_device_handlers* handlers = (is_device_device_handlers*)capture_data->handle;
|
||||
const is_device_usb_device* usb_device_desc = (const is_device_usb_device*)capture_data->status.device.descriptor;
|
||||
bool reset_hardware = capture_data->status.reset_hardware;
|
||||
capture_data->status.reset_hardware = false;
|
||||
int ret = LIBUSB_SUCCESS;
|
||||
if(!reset_hardware)
|
||||
return ret;
|
||||
const auto curr_time = std::chrono::high_resolution_clock::now();
|
||||
const std::chrono::duration<double> diff = curr_time - clock_last_reset;
|
||||
// Do not reset too fast... In general.
|
||||
if(diff.count() < RESET_TIMEOUT)
|
||||
return ret;
|
||||
clock_last_reset = curr_time;
|
||||
|
||||
if(!(*is_acquisition_off))
|
||||
ret = EndAcquisitionCapture(capture_data, is_device_capture_recv_data);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
ret = ResetCPUStart(handlers, usb_device_desc);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
ret = ResetCPUEnd(handlers, usb_device_desc);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
if(!(*is_acquisition_off)) {
|
||||
default_sleep(SLEEP_RESET_TIME_MS);
|
||||
curr_capture_type = capture_data->status.capture_type;
|
||||
curr_capture_speed = capture_data->status.capture_speed;
|
||||
ret = StartAcquisitionCapture(capture_data, is_device_capture_recv_data, curr_capture_type, curr_capture_speed, is_acquisition_off, index);
|
||||
}
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int initial_cleanup_capture(const is_device_usb_device* usb_device_desc, is_device_device_handlers* handlers) {
|
||||
//EndAcquisition(handlers, usb_device_desc, false, 0, CAPTURE_SCREENS_BOTH);
|
||||
return LIBUSB_SUCCESS;
|
||||
}
|
||||
|
||||
int EndAcquisitionCapture(CaptureData* capture_data, ISDeviceCaptureReceivedData* is_device_capture_recv_data) {
|
||||
wait_all_is_device_transfers_done(capture_data, is_device_capture_recv_data);
|
||||
return EndAcquisitionCapture((const is_device_usb_device*)capture_data->status.device.descriptor, (is_device_device_handlers*)capture_data->handle);
|
||||
}
|
||||
|
||||
int EndAcquisitionCapture(const is_device_usb_device* usb_device_desc, is_device_device_handlers* handlers) {
|
||||
return StopUsbCaptureDma(handlers, usb_device_desc);
|
||||
}
|
||||
|
||||
void is_nitro_acquisition_capture_main_loop(CaptureData* capture_data, ISDeviceCaptureReceivedData* is_device_capture_recv_data) {
|
||||
is_device_device_handlers* handlers = (is_device_device_handlers*)capture_data->handle;
|
||||
const is_device_usb_device* usb_device_desc = (const is_device_usb_device*)capture_data->status.device.descriptor;
|
||||
bool is_acquisition_off = true;
|
||||
uint32_t index = 0;
|
||||
CaptureScreensType curr_capture_type = capture_data->status.capture_type;
|
||||
CaptureSpeedsType curr_capture_speed = capture_data->status.capture_speed;
|
||||
int ret = StartAcquisitionCapture(capture_data, is_device_capture_recv_data, curr_capture_type, curr_capture_speed, &is_acquisition_off, index);
|
||||
if(ret < 0) {
|
||||
capture_error_print(true, capture_data, "Capture Start: Failed");
|
||||
return;
|
||||
}
|
||||
auto clock_last_reset = std::chrono::high_resolution_clock::now();
|
||||
|
||||
while(capture_data->status.connected && capture_data->status.running) {
|
||||
ret = CaptureResetHardware(capture_data, is_device_capture_recv_data, curr_capture_type, curr_capture_speed, &is_acquisition_off, clock_last_reset, index);
|
||||
if(ret < 0) {
|
||||
capture_error_print(true, capture_data, "Hardware reset: Failed");
|
||||
return;
|
||||
}
|
||||
if(is_acquisition_off) {
|
||||
default_sleep(SLEEP_CHECKS_TIME_MS);
|
||||
ret = LidReopenCaptureCheck(capture_data, is_device_capture_recv_data, curr_capture_type, curr_capture_speed, &is_acquisition_off, index);
|
||||
if(ret < 0) {
|
||||
capture_error_print(true, capture_data, "Lid Reopen: Failed");
|
||||
return;
|
||||
}
|
||||
}
|
||||
if(is_acquisition_off)
|
||||
continue;
|
||||
wait_one_is_device_buffer_free(capture_data, is_device_capture_recv_data);
|
||||
ret = get_is_device_status(is_device_capture_recv_data);
|
||||
if(ret < 0) {
|
||||
ret = EndAcquisition(capture_data, is_device_capture_recv_data, true, 0, curr_capture_type);
|
||||
if(ret < 0) {
|
||||
capture_error_print(true, capture_data, "Disconnected: Read error");
|
||||
return;
|
||||
}
|
||||
is_acquisition_off = true;
|
||||
continue;
|
||||
}
|
||||
is_device_read_frame_request(capture_data, is_device_get_free_buffer(capture_data, is_device_capture_recv_data), curr_capture_type, index++);
|
||||
|
||||
if((curr_capture_type != capture_data->status.capture_type) || (curr_capture_speed != capture_data->status.capture_speed)) {
|
||||
ret = EndAcquisition(capture_data, is_device_capture_recv_data, true, 0, curr_capture_type);
|
||||
if(ret < 0) {
|
||||
capture_error_print(true, capture_data, "Capture End: Failed");
|
||||
return;
|
||||
}
|
||||
curr_capture_type = capture_data->status.capture_type;
|
||||
curr_capture_speed = capture_data->status.capture_speed;
|
||||
ret = StartAcquisitionCapture(capture_data, is_device_capture_recv_data, curr_capture_type, curr_capture_speed, &is_acquisition_off, index);
|
||||
if(ret < 0) {
|
||||
capture_error_print(true, capture_data, "Capture Restart: Failed");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(!is_acquisition_off)
|
||||
EndAcquisition(capture_data, is_device_capture_recv_data, true, 0, curr_capture_type);
|
||||
}
|
||||
@@ -0,0 +1,362 @@
|
||||
#include "devicecapture.hpp"
|
||||
#include "usb_is_device_communications.hpp"
|
||||
#include "usb_is_device_acquisition_general.hpp"
|
||||
#include "usb_is_nitro_acquisition_emulator.hpp"
|
||||
|
||||
#include <chrono>
|
||||
|
||||
// Code based off of Gericom's sample code. Distributed under the MIT License. Copyright (c) 2024 Gericom
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// 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
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
// IN THE SOFTWARE.
|
||||
|
||||
// The code for video capture of the IS Nitro Emulator and of the IS Nitro Capture is wildly different.
|
||||
// For this reason, the code was split into two device-specific files.
|
||||
|
||||
#define FRAME_BUFFER_SIZE 32
|
||||
#define SLEEP_CHECKS_TIME_MS 20
|
||||
|
||||
#define SLEEP_TIME_DIVISOR 8
|
||||
|
||||
static int drain_frames(is_device_device_handlers* handlers, int num_frames, int start_frames, CaptureScreensType capture_type, const is_device_usb_device* usb_device_desc) {
|
||||
ISNitroEmulatorVideoInputData* video_in_buffer = new ISNitroEmulatorVideoInputData;
|
||||
for (int i = start_frames; i < num_frames; i++) {
|
||||
int ret = ReadFrame(handlers, (uint8_t*)video_in_buffer, usb_is_device_get_video_in_size(capture_type, usb_device_desc->device_type), usb_device_desc);
|
||||
if(ret < 0) {
|
||||
delete video_in_buffer;
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
delete video_in_buffer;
|
||||
return LIBUSB_SUCCESS;
|
||||
}
|
||||
|
||||
static int StartAcquisitionEmulator(is_device_device_handlers* handlers, uint16_t &out_frame_count, float &single_frame_time, CaptureScreensType capture_type, CaptureSpeedsType capture_speed, const is_device_usb_device* usb_device_desc) {
|
||||
int ret = 0;
|
||||
ret = DisableLca2(handlers, usb_device_desc);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
ret = set_acquisition_mode(handlers, capture_type, capture_speed, usb_device_desc);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
ret = SetForwardFrameCount(handlers, FRAME_BUFFER_SIZE, usb_device_desc);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
// Reset this in case it's high. At around 0xFFFF, reading from the USB DMA seems to fail...
|
||||
ret = UpdateFrameForwardEnable(handlers, true, true, usb_device_desc);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
ret = UpdateFrameForwardEnable(handlers, true, false, usb_device_desc);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
|
||||
// Get to the closest next frame
|
||||
auto clock_start = std::chrono::high_resolution_clock::now();
|
||||
uint16_t oldFrameCount;
|
||||
uint16_t newFrameCount;
|
||||
ret = GetFrameCounter(handlers, &oldFrameCount, usb_device_desc);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
newFrameCount = oldFrameCount;
|
||||
while(newFrameCount == oldFrameCount) {
|
||||
ret = GetFrameCounter(handlers, &newFrameCount, usb_device_desc);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
const auto curr_time = std::chrono::high_resolution_clock::now();
|
||||
const std::chrono::duration<double> diff = curr_time - clock_start;
|
||||
// If too much time has passed, the DS is probably either turned off or sleeping. If so, avoid locking up
|
||||
if(diff.count() > 0.2)
|
||||
break;
|
||||
}
|
||||
|
||||
// Get to the next modulo 32 frame.
|
||||
// We also do this to measure the time that is needed for each frame...
|
||||
// To do so, a minimum of 4 frames is required (FRAME_BUFFER_SIZE - 1 + 4)
|
||||
clock_start = std::chrono::high_resolution_clock::now();
|
||||
ret = GetFrameCounter(handlers, &oldFrameCount, usb_device_desc);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
uint16_t targetFrameCount = (newFrameCount + FRAME_BUFFER_SIZE + 3) & (~(FRAME_BUFFER_SIZE - 1));
|
||||
while(oldFrameCount != targetFrameCount) {
|
||||
ret = GetFrameCounter(handlers, &oldFrameCount, usb_device_desc);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
// Placing a sleep of some kind here would be much better...
|
||||
// Though this is only executed for a small time when first connecting...
|
||||
const auto curr_time = std::chrono::high_resolution_clock::now();
|
||||
const std::chrono::duration<double> diff = curr_time - clock_start;
|
||||
// If too much time has passed, the DS is probably either turned off or sleeping. If so, avoid locking up
|
||||
if(diff.count() > 1.0)
|
||||
break;
|
||||
}
|
||||
const auto curr_time = std::chrono::high_resolution_clock::now();
|
||||
const std::chrono::duration<double> diff = curr_time - clock_start;
|
||||
// Sometimes the upper 8 bits aren't updated... Use only the lower 8 bits.
|
||||
newFrameCount &= 0xFF;
|
||||
oldFrameCount &= 0xFF;
|
||||
int frame_diff = ((int)oldFrameCount) - ((int)newFrameCount);
|
||||
if(frame_diff < 0)
|
||||
frame_diff += 1 << 8;
|
||||
out_frame_count = oldFrameCount;
|
||||
// Determine how much time a single frame takes. We'll use it for sleeps
|
||||
if(frame_diff == 0)
|
||||
single_frame_time = 0;
|
||||
else
|
||||
single_frame_time = diff.count() / frame_diff;
|
||||
|
||||
// Start the actual DMA
|
||||
if(single_frame_time > 0) {
|
||||
ret = StartUsbCaptureDma(handlers, usb_device_desc);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static bool do_sleep(float single_frame_time, std::chrono::time_point<std::chrono::high_resolution_clock> clock_last_reset, CaptureScreensType capture_type, CaptureSpeedsType capture_speed, int curr_frame_counter, int last_frame_counter, float* out_time) {
|
||||
auto curr_time = std::chrono::high_resolution_clock::now();
|
||||
std::chrono::duration<double> diff = curr_time - clock_last_reset;
|
||||
float expected_time = single_frame_time * curr_frame_counter;
|
||||
// Frames aren't ready asap. It takes a while for them to be, in slower modes...
|
||||
int adding_single_frame_time_divisor = 0;
|
||||
switch(capture_speed) {
|
||||
case CAPTURE_SPEEDS_HALF:
|
||||
adding_single_frame_time_divisor = 1;
|
||||
break;
|
||||
case CAPTURE_SPEEDS_THIRD:
|
||||
adding_single_frame_time_divisor = 2;
|
||||
break;
|
||||
case CAPTURE_SPEEDS_QUARTER:
|
||||
adding_single_frame_time_divisor = 3;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if((capture_type == CAPTURE_SCREENS_TOP) || (capture_type == CAPTURE_SCREENS_BOTTOM))
|
||||
adding_single_frame_time_divisor *= 2;
|
||||
if(adding_single_frame_time_divisor > 0) {
|
||||
float adding_single_frame_time_multiplier = adding_single_frame_time_divisor - 1;
|
||||
if(adding_single_frame_time_multiplier == 0)
|
||||
adding_single_frame_time_multiplier = 0.33;
|
||||
expected_time += (single_frame_time * adding_single_frame_time_multiplier) / adding_single_frame_time_divisor;
|
||||
}
|
||||
float low_single_frame_time = 1.0 / ((int)((1.0 / single_frame_time) + 1));
|
||||
float low_expected_time = low_single_frame_time * curr_frame_counter;
|
||||
// If the current time is too low, sleep a bit to make sure we don't overrun the framerate counter
|
||||
// Don't do it regardless of the situation, and only in small increments...
|
||||
// Otherwise there is the risk of sleeping too much
|
||||
bool result = (diff.count() < expected_time) && ((expected_time - diff.count()) > (single_frame_time / SLEEP_TIME_DIVISOR));
|
||||
*out_time = single_frame_time;
|
||||
if(last_frame_counter & (FRAME_BUFFER_SIZE - 1)) {
|
||||
result = (diff.count() < low_expected_time) && ((low_expected_time - diff.count()) > (low_single_frame_time / SLEEP_TIME_DIVISOR));
|
||||
*out_time = low_single_frame_time;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static void frame_wait(float single_frame_time, std::chrono::time_point<std::chrono::high_resolution_clock> clock_last_reset, CaptureScreensType capture_type, CaptureSpeedsType capture_speed, int curr_frame_counter, int last_frame_counter) {
|
||||
if(curr_frame_counter == FRAME_BUFFER_SIZE)
|
||||
return;
|
||||
float sleep_time = 0;
|
||||
int sleep_counter = 0;
|
||||
while(do_sleep(single_frame_time, clock_last_reset, capture_type, capture_speed, curr_frame_counter, last_frame_counter, &sleep_time)) {
|
||||
default_sleep(sleep_time * 1000.0 / SLEEP_TIME_DIVISOR);
|
||||
sleep_counter++;
|
||||
}
|
||||
}
|
||||
|
||||
static int reset_acquisition_frames(CaptureData* capture_data, uint16_t &curr_frame_counter, uint16_t &last_frame_counter, float &single_frame_time, std::chrono::time_point<std::chrono::high_resolution_clock> &clock_last_reset, CaptureScreensType &curr_capture_type, CaptureScreensType wanted_capture_type, CaptureSpeedsType &curr_capture_speed, CaptureSpeedsType wanted_capture_speed, ISDeviceCaptureReceivedData* is_device_capture_recv_data) {
|
||||
curr_frame_counter += 1;
|
||||
|
||||
if(curr_frame_counter < FRAME_BUFFER_SIZE)
|
||||
return LIBUSB_SUCCESS;
|
||||
|
||||
is_device_device_handlers* handlers = (is_device_device_handlers*)capture_data->handle;
|
||||
const is_device_usb_device* usb_device_desc = (const is_device_usb_device*)capture_data->status.device.descriptor;
|
||||
wait_all_is_device_transfers_done(capture_data, is_device_capture_recv_data);
|
||||
int ret = get_is_device_status(is_device_capture_recv_data);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
|
||||
int multiplier = 1;
|
||||
switch(curr_capture_speed) {
|
||||
case CAPTURE_SPEEDS_HALF:
|
||||
multiplier = 2;
|
||||
break;
|
||||
case CAPTURE_SPEEDS_THIRD:
|
||||
multiplier = 3;
|
||||
break;
|
||||
case CAPTURE_SPEEDS_QUARTER:
|
||||
multiplier = 4;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
ret = StopUsbCaptureDma(handlers, usb_device_desc);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
|
||||
// If the user requests a mode change, accomodate them.
|
||||
// Though it may lag for a bit...
|
||||
if((wanted_capture_type != curr_capture_type) || (wanted_capture_speed != curr_capture_speed)) {
|
||||
curr_capture_type = wanted_capture_type;
|
||||
curr_capture_speed = wanted_capture_speed;
|
||||
ret = set_acquisition_mode(handlers, curr_capture_type, curr_capture_speed, usb_device_desc);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint16_t internalFrameCount = 0;
|
||||
uint16_t full_internalFrameCount = 0;
|
||||
int frame_diff = 0;
|
||||
int diff_target = FRAME_BUFFER_SIZE * multiplier;
|
||||
std::chrono::time_point<std::chrono::high_resolution_clock> curr_time;
|
||||
std::chrono::duration<double> diff;
|
||||
do {
|
||||
// Is this not the first loop? Also, do not sleep too much...
|
||||
if((frame_diff > 0) && (((internalFrameCount & (FRAME_BUFFER_SIZE - 1)) < (FRAME_BUFFER_SIZE - 1)))) {
|
||||
float expected_time = single_frame_time * FRAME_BUFFER_SIZE;
|
||||
if(diff.count() < expected_time)
|
||||
default_sleep(single_frame_time * 1000.0 / SLEEP_TIME_DIVISOR);
|
||||
}
|
||||
// Check how many frames have passed...
|
||||
ret = GetFrameCounter(handlers, &internalFrameCount, usb_device_desc);
|
||||
full_internalFrameCount = internalFrameCount;
|
||||
// Sometimes the upper 8 bits aren't updated... Use only the lower 8 bits.
|
||||
internalFrameCount &= 0xFF;
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
frame_diff = internalFrameCount - last_frame_counter;
|
||||
if(frame_diff < 0)
|
||||
frame_diff += 1 << 8;
|
||||
// If the frames haven't advanced, the DS is either turned off or sleeping. If so, avoid locking up
|
||||
if(frame_diff == 0)
|
||||
break;
|
||||
curr_time = std::chrono::high_resolution_clock::now();
|
||||
diff = curr_time - clock_last_reset;
|
||||
// If too much time has passed, the DS is probably either turned off or sleeping. If so, avoid locking up
|
||||
if(diff.count() > (1.0 * multiplier)) {
|
||||
frame_diff = 0;
|
||||
break;
|
||||
}
|
||||
// Exit if enough frames have passed, or if there currently is some delay.
|
||||
// Exiting early makes it possible to catch up to the DMA, if we're behind.
|
||||
} while(((frame_diff < diff_target) && (!(last_frame_counter & (FRAME_BUFFER_SIZE - 1)))) || ((internalFrameCount & (FRAME_BUFFER_SIZE - 1)) >= (FRAME_BUFFER_SIZE - ( 1 + multiplier))));
|
||||
// Determine how much time a single frame takes. We'll use it for sleeps
|
||||
curr_time = std::chrono::high_resolution_clock::now();
|
||||
diff = curr_time - clock_last_reset;
|
||||
if((frame_diff > 0) && (frame_diff < (diff_target - (1 + multiplier)))) {
|
||||
bool is_lid_closed = false;
|
||||
ret = ReadLidState(handlers, &is_lid_closed, usb_device_desc);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
if(is_lid_closed)
|
||||
frame_diff = 0;
|
||||
}
|
||||
if(frame_diff == 0)
|
||||
single_frame_time = 0;
|
||||
else
|
||||
single_frame_time = diff.count() / (frame_diff / ((float)multiplier));
|
||||
clock_last_reset = curr_time;
|
||||
|
||||
// Save the current frame counter's 8 LSB
|
||||
last_frame_counter = internalFrameCount;
|
||||
|
||||
// If we're nearing 0xFFFF for the frame counter, reset it.
|
||||
// It's a problematic value for DMA reading
|
||||
if(frame_diff && (full_internalFrameCount >= 0xF000)) {
|
||||
ret = UpdateFrameForwardEnable(handlers, true, true, usb_device_desc);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
clock_last_reset = std::chrono::high_resolution_clock::now();
|
||||
}
|
||||
ret = UpdateFrameForwardEnable(handlers, true, false, usb_device_desc);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
curr_frame_counter = 0;
|
||||
|
||||
// Start the actual DMA
|
||||
if(single_frame_time > 0) {
|
||||
ret = StartUsbCaptureDma(handlers, usb_device_desc);
|
||||
if(ret < 0)
|
||||
return ret;
|
||||
}
|
||||
return LIBUSB_SUCCESS;
|
||||
}
|
||||
|
||||
int initial_cleanup_emulator(const is_device_usb_device* usb_device_desc, is_device_device_handlers* handlers) {
|
||||
return EndAcquisition(usb_device_desc, handlers, false, 0, CAPTURE_SCREENS_BOTH);
|
||||
}
|
||||
|
||||
int EndAcquisitionEmulator(CaptureData* capture_data, ISDeviceCaptureReceivedData* is_device_capture_recv_data, bool do_drain_frames, int start_frames, CaptureScreensType capture_type) {
|
||||
wait_all_is_device_transfers_done(capture_data, is_device_capture_recv_data);
|
||||
return EndAcquisitionEmulator((const is_device_usb_device*)capture_data->status.device.descriptor, (is_device_device_handlers*)capture_data->handle, do_drain_frames, start_frames, capture_type);
|
||||
}
|
||||
|
||||
int EndAcquisitionEmulator(const is_device_usb_device* usb_device_desc, is_device_device_handlers* handlers, bool do_drain_frames, int start_frames, CaptureScreensType capture_type) {
|
||||
int ret = 0;
|
||||
if (do_drain_frames)
|
||||
ret = drain_frames(handlers, FRAME_BUFFER_SIZE, start_frames, capture_type, usb_device_desc);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
ret = StopUsbCaptureDma(handlers, usb_device_desc);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
return UpdateFrameForwardEnable(handlers, false, false, usb_device_desc);
|
||||
}
|
||||
|
||||
void is_nitro_acquisition_emulator_main_loop(CaptureData* capture_data, ISDeviceCaptureReceivedData* is_device_capture_recv_data) {
|
||||
is_device_device_handlers* handlers = (is_device_device_handlers*)capture_data->handle;
|
||||
const is_device_usb_device* usb_device_desc = (const is_device_usb_device*)capture_data->status.device.descriptor;
|
||||
uint32_t index = 0;
|
||||
uint16_t last_frame_counter = 0;
|
||||
float single_frame_time = 0;
|
||||
uint16_t curr_frame_counter = 0;
|
||||
CaptureScreensType curr_capture_type = capture_data->status.capture_type;
|
||||
CaptureSpeedsType curr_capture_speed = capture_data->status.capture_speed;
|
||||
int ret = StartAcquisitionEmulator(handlers, last_frame_counter, single_frame_time, curr_capture_type, curr_capture_speed, usb_device_desc);
|
||||
if (ret < 0) {
|
||||
capture_error_print(true, capture_data, "Capture Start: Failed");
|
||||
return;
|
||||
}
|
||||
auto clock_last_reset = std::chrono::high_resolution_clock::now();
|
||||
|
||||
while (capture_data->status.connected && capture_data->status.running) {
|
||||
ret = get_is_device_status(is_device_capture_recv_data);
|
||||
if(ret < 0) {
|
||||
capture_error_print(true, capture_data, "Disconnected: Read error");
|
||||
return;
|
||||
}
|
||||
if(single_frame_time > 0) {
|
||||
is_device_read_frame_request(capture_data, is_device_get_free_buffer(capture_data, is_device_capture_recv_data), curr_capture_type, index++);
|
||||
frame_wait(single_frame_time, clock_last_reset, curr_capture_type, curr_capture_speed, curr_frame_counter + 1, last_frame_counter);
|
||||
}
|
||||
else {
|
||||
capture_data->status.cooldown_curr_in = FIX_PARTIAL_FIRST_FRAME_NUM;
|
||||
clock_last_reset = std::chrono::high_resolution_clock::now();
|
||||
default_sleep(SLEEP_CHECKS_TIME_MS);
|
||||
}
|
||||
capture_data->status.curr_delay = last_frame_counter % FRAME_BUFFER_SIZE;
|
||||
ret = reset_acquisition_frames(capture_data, curr_frame_counter, last_frame_counter, single_frame_time, clock_last_reset, curr_capture_type, capture_data->status.capture_type, curr_capture_speed, capture_data->status.capture_speed, is_device_capture_recv_data);
|
||||
if(ret < 0) {
|
||||
capture_error_print(true, capture_data, "Disconnected: Frame counter reset error");
|
||||
break;
|
||||
}
|
||||
}
|
||||
EndAcquisition(capture_data, is_device_capture_recv_data, true, curr_frame_counter, curr_capture_type);
|
||||
}
|
||||
Reference in New Issue
Block a user