Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .github/workflows/lab-device-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Lab device ownership
on:
pull_request:
workflow_dispatch:
jobs:
native-leases:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v7
- uses: jurplel/install-qt-action@v4
with:
version: '6.10.2'
cache: true
- run: cmake -S SerialPrograms/Source/CommonFramework/Devices -B build-lab-devices -DCMAKE_BUILD_TYPE=Debug
- run: cmake --build build-lab-devices --config Debug -j4
- run: ctest --test-dir build-lab-devices -C Debug --output-on-failure
9 changes: 9 additions & 0 deletions SerialPrograms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -909,3 +909,12 @@ if(COMMAND_LINE_EXE)
# Add command-line executable (GUI-free) from subdirectory
include(Source/CommandLine/CommandLineExecutable.cmake)
endif()

# The registered cross-process tests use this GUI-free native runner as their child.
add_subdirectory(Source/CommonFramework/Devices)
if(WIN32)
set_target_properties(LabDeviceAccessTests PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${WIN_DEPLOY_DIR}")
else()
set_target_properties(LabDeviceAccessTests PROPERTIES RUNTIME_OUTPUT_DIRECTORY "$<TARGET_FILE_DIR:SerialPrograms>")
endif()
add_dependencies(SerialPrograms LabDeviceAccessTests)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Audio Passthrough Pair (Qt)

Check warning on line 1 in SerialPrograms/Source/CommonFramework/AudioPipeline/Backends/AudioPassthroughPairQt.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

<file>: file_lines=566 (advisory above 500, limit 1000)
*
* From: https://github.com/PokemonAutomation/
*
Expand Down Expand Up @@ -397,7 +397,7 @@
: m_logger(logger)
{}

void AudioPassthroughPairQt::reset(

Check warning on line 400 in SerialPrograms/Source/CommonFramework/AudioPipeline/Backends/AudioPassthroughPairQt.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::AudioPassthroughPairQt::reset( const std :: shared_ptr<AudioSourceDescriptor> & descriptor , const AudioInputChoice & format , const AudioDeviceInfo & output , double output_volume): function_lines=26 (advisory above 25, limit 50)
const std::shared_ptr<AudioSourceDescriptor>& descriptor,
const AudioInputChoice& format,
const AudioDeviceInfo& output,
Expand All @@ -406,16 +406,12 @@
auto scope_check = m_sanitizer.check_scope();
QMetaObject::invokeMethod(this, [this, format, descriptor, output, output_volume]{
auto scope_check = m_sanitizer.check_scope();
close_audio_source();
std::unique_ptr<AudioSource> source;
if (descriptor){
source = descriptor->make_AudioSource(m_logger, format);
}
WriteSpinLock lg(m_lock, PA_CURRENT_FUNCTION);
if (m_reader){
teardown_tree();
m_sink_device.reset();
m_reader.reset();
}
m_input_choice = format;
m_output_device = output;
m_output_volume = output_volume;
Expand All @@ -427,6 +423,15 @@
}
});
}
// Called on this object's Qt thread before constructing a replacement input.
void AudioPassthroughPairQt::close_audio_source(){
WriteSpinLock lg(m_lock, PA_CURRENT_FUNCTION);
if (m_reader){
teardown_tree();
m_sink_device.reset();
m_reader.reset();
}
}
void AudioPassthroughPairQt::clear_audio_source(){
auto scope_check = m_sanitizer.check_scope();
QMetaObject::invokeMethod(this, [this]{
Expand All @@ -447,16 +452,12 @@
auto scope_check = m_sanitizer.check_scope();
QMetaObject::invokeMethod(this, [this, descriptor, format]{
auto scope_check = m_sanitizer.check_scope();
close_audio_source();
std::unique_ptr<AudioSource> source;
if (descriptor){
source = descriptor->make_AudioSource(m_logger, format);
}
WriteSpinLock lg(m_lock, PA_CURRENT_FUNCTION);
if (m_reader){
teardown_tree();
m_sink_device.reset();
m_reader.reset();
}
m_input_choice = format;
if (source){
m_reader = std::move(source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class AudioPassthroughPairQt final : public QObject, public AudioPassthroughPair
// thread only ever sees them through the node listener sets, where
// ListenerSet::remove blocks on in-flight pushes so rebuilds never
// race a live push.
void close_audio_source();
void teardown_tree();
void teardown_sink_branch();
void build_tree(float input_volume);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@



AudioInputDevice::AudioInputDevice(

Check warning on line 45 in SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioInputDevice.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::AudioInputDevice::AudioInputDevice( Logger & logger , const NativeAudioInfo & device , const QAudioFormat & format , bool reinterpret_96k_mono_as_stereo): function_lines=38 (advisory above 25, limit 50)
Logger& logger,
const NativeAudioInfo& device, const QAudioFormat& format,
bool reinterpret_96k_mono_as_stereo
Expand All @@ -53,6 +53,7 @@
RAW_BUFFER_CAPACITY
)
, ObjectStreamListener(raw_output_frame_bytes(format, reinterpret_96k_mono_as_stereo))
, m_device_lease(acquire_lab_capture(logger, LabDeviceKind::AUDIO, device.id().toHex().toStdString()))
, m_sample_type(sample_format_of(format))
, m_sample_rate(format.sampleRate())
, m_channels(format.channelCount())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ using NativeAudioSource = QAudioSource;
#include "Common/Cpp/StreamConverters.h"
#include "Kernels/AudioPipeline/Kernels_AudioPipeline.h"
#include "CommonFramework/AudioPipeline/AudioFormat.h"
#include "CommonFramework/Devices/LabDeviceRuntime.h"

class QAudioFormat;

Expand Down Expand Up @@ -87,6 +88,7 @@ class AudioInputDevice final


private:
std::unique_ptr<LabDeviceLease> m_device_lease;
std::unique_ptr<NativeAudioSource> m_source;
Kernels::AudioPipeline::SampleFormat m_sample_type;
size_t m_sample_rate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ std::unique_ptr<AudioSource> AudioSourceDescriptor_Device::make_AudioSource(
return nullptr;
}
// cout << "format = " << (int)format << endl;
return std::make_unique<AudioSource_Device>(logger, m_info, format);
try{
return std::make_unique<AudioSource_Device>(logger, m_info, format);
}catch (UserSetupError&){
return nullptr; // Refusal is logged; never throw across the Qt audio callback.
}
}


Expand Down
20 changes: 20 additions & 0 deletions SerialPrograms/Source/CommonFramework/Devices/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Focused native tests, without configuring/linking the GUI application:
# cmake -S SerialPrograms/Source/CommonFramework/Devices -B /path/to/test-build
# cmake --build /path/to/test-build -j4
# ctest --test-dir /path/to/test-build --output-on-failure
cmake_minimum_required(VERSION 3.24)
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
project(LabDeviceAccessTests LANGUAGES CXX)
enable_testing()
find_package(Qt6 REQUIRED COMPONENTS Core)
endif()
add_executable(LabDeviceAccessTests LabDeviceAccessQt.cpp LabDeviceAccessQt_Tests.cpp)
target_compile_features(LabDeviceAccessTests PRIVATE cxx_std_23)
target_compile_definitions(LabDeviceAccessTests PRIVATE PA_LAB_DEVICE_STANDALONE_TESTS)
target_link_libraries(LabDeviceAccessTests PRIVATE Qt6::Core)
if(MSVC)
target_compile_options(LabDeviceAccessTests PRIVATE /W4 /WX)
else()
target_compile_options(LabDeviceAccessTests PRIVATE -Wall -Wextra -Werror)
endif()
add_test(NAME LabDeviceAccess COMMAND LabDeviceAccessTests)
130 changes: 130 additions & 0 deletions SerialPrograms/Source/CommonFramework/Devices/LabDeviceAccessQt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/* Lab Device Access (Qt process-lock seam)
*
* From: https://github.com/PokemonAutomation/
*/
#include <algorithm>
#include <chrono>
#include <format>
#include <stdexcept>
#include <QCryptographicHash>
#include <QDir>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include "LabDeviceAccessQt.h"

namespace PokemonAutomation{
namespace{

QString text(std::string_view value){
return QString::fromUtf8(value.data(), (qsizetype)value.size());
}

std::set<std::string> read_ids(const QJsonObject& object, const char* key){
if (!object.value(key).isArray()){
throw std::runtime_error(std::string("Expected device array: ") + key);
}
std::set<std::string> ids;
for (const QJsonValue& value : object.value(key).toArray()){
const std::string id = value.toString().toStdString();
if (!value.isString() || id.empty() || id.find('\0') != std::string::npos || !ids.emplace(id).second){
throw std::runtime_error(std::string("Empty, invalid or duplicate device ID in ") + key);
}
}
return ids;
}

bool permitted(const LabDevicePolicy& policy, const LabDeviceIdentity& device){
switch (device.kind){
case LabDeviceKind::SERIAL: return policy.serial.contains(device.id);
case LabDeviceKind::VIDEO: return policy.video.contains(device.id);
case LabDeviceKind::AUDIO: return policy.audio.contains(device.id);
case LabDeviceKind::PROFILE: return false;
}
return false;
}

std::string normalize_serial_path(std::string_view path){
if (path.starts_with("/dev/tty.")){
return "/dev/cu." + std::string(path.substr(9));
}
return std::string(path);
}

} // namespace

LabDevicePolicy parse_lab_device_policy(std::string_view json, std::string_view profile){
QJsonParseError error;
const QJsonDocument document = QJsonDocument::fromJson(QByteArray(json.data(), (qsizetype)json.size()), &error);
const QJsonObject object = document.object();
const std::set<QString> keys{"schema", "profile", "serial", "video", "audio"};
for (auto it = object.begin(); it != object.end(); ++it){
if (!keys.contains(it.key())){
throw std::runtime_error("Unknown field in lab device policy.");
}
}
if (error.error != QJsonParseError::NoError || !document.isObject() || object.size() != 5 ||
!object.value("schema").isDouble() || object.value("schema").toDouble() != 1 ||
profile.empty() || object.value("profile").toString() != text(profile)){
throw std::runtime_error("Missing, malformed or mismatched lab device policy.");
}
return {std::string(profile), read_ids(object, "serial"), read_ids(object, "video"), read_ids(object, "audio")};
}

std::string lab_serial_identity(unsigned vendor, unsigned product, std::string_view serial){
if (vendor > 0xffff || product > 0xffff || serial.empty() || serial.find('\0') != std::string_view::npos){
throw std::runtime_error("A unique USB serial, vendor and product identity are required.");
}
return std::format("{:04x}:{:04x}:{}", vendor, product, serial);
}

std::string resolve_lab_serial_identity(std::span<const LabSerialDevice> devices, std::string_view port){
const std::string path = normalize_serial_path(port);
const auto selected = std::ranges::find_if(devices, [&](const LabSerialDevice& device){
return normalize_serial_path(device.port) == path;
});
if (selected == devices.end()){
throw std::runtime_error("Controller has no current USB identity; hardware access is disabled.");
}
const std::string identity = lab_serial_identity(selected->vendor, selected->product, selected->serial);
for (const LabSerialDevice& other : devices){
if (normalize_serial_path(other.port) != path && !other.serial.empty() &&
lab_serial_identity(other.vendor, other.product, other.serial) == identity){
throw std::runtime_error("Two adapters report the same USB identity; refusing ambiguous ownership.");
}
}
return identity;
}

std::string lab_device_lock_path(std::string_view directory, const LabDeviceIdentity& device){
if (directory.empty() || device.id.empty()){
throw std::runtime_error("Device lease directory and identity must be nonempty.");
}
const QByteArray key = QByteArray::number((int)device.kind) + ':' + QByteArray(device.id.data(), (qsizetype)device.id.size());
const QByteArray digest = QCryptographicHash::hash(key, QCryptographicHash::Sha256).toHex();
return QDir(text(directory)).filePath(QString::fromLatin1(digest) + ".lock").toStdString();
}

LabDeviceLease::LabDeviceLease(std::string_view path)
: m_lock(text(path))
{
// Age alone must never evict an active multi-hour lab run.
m_lock.setStaleLockTime(std::chrono::milliseconds::zero());
if (!m_lock.tryLock(std::chrono::milliseconds::zero())){
throw std::runtime_error("Device/profile is already leased, or its lock cannot be created. Nothing was opened.");
}
}

std::unique_ptr<LabDeviceLease> acquire_lab_device(
const LabDevicePolicy& policy, std::string_view directory, const LabDeviceIdentity& device
){
if (!permitted(policy, device)){
throw std::runtime_error("Device is not allowlisted for profile " + policy.profile + ". Nothing was opened.");
}
if (!QDir().mkpath(text(directory))){
throw std::runtime_error("Cannot create shared device lease directory. Nothing was opened.");
}
return std::make_unique<LabDeviceLease>(lab_device_lock_path(directory, device));
}

} // namespace PokemonAutomation
72 changes: 72 additions & 0 deletions SerialPrograms/Source/CommonFramework/Devices/LabDeviceAccessQt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* Lab Device Access (Qt process-lock seam)
*
* From: https://github.com/PokemonAutomation/
*/
#ifndef PokemonAutomation_LabDeviceAccessQt_H
#define PokemonAutomation_LabDeviceAccessQt_H

#include <memory>
#include <set>
#include <span>
#include <string>
#include <string_view>
#include <QLockFile>

namespace PokemonAutomation{

// Separate namespaces for serial, video, audio and whole-profile reservations.
enum class LabDeviceKind{ SERIAL, VIDEO, AUDIO, PROFILE };

// Stable hardware identity, never a display alias, ordinal or USB port pathname.
struct LabDeviceIdentity{
LabDeviceKind kind;
std::string id;
};

// Immutable allowlist loaded once for the selected lab profile.
struct LabDevicePolicy{
std::string profile;
std::set<std::string> serial;
std::set<std::string> video;
std::set<std::string> audio;
};

// One currently enumerated USB port; tty/cu aliases may describe the same adapter.
struct LabSerialDevice{
std::string port;
unsigned vendor;
unsigned product;
std::string serial;
};

// Resolve a selected current port, rejecting missing or ambiguous physical identity.
std::string resolve_lab_serial_identity(std::span<const LabSerialDevice> devices, std::string_view port);

// Parse the strict schema; malformed/mismatched input throws std::runtime_error.
LabDevicePolicy parse_lab_device_policy(std::string_view json, std::string_view profile);

// Normalize VID/PID and a nonempty USB serial; empty identifiers are refused.
std::string lab_serial_identity(unsigned vendor, unsigned product, std::string_view serial);

// Compute a shared lock path independent of profile, executable and display names.
std::string lab_device_lock_path(std::string_view directory, const LabDeviceIdentity& device);

// A noncopyable process lease. Destruction releases it; dead owners are recovered by Qt.
// Keep this alive until after the native device has closed. Acquisition never waits.
class LabDeviceLease{
public:
explicit LabDeviceLease(std::string_view path);
LabDeviceLease(const LabDeviceLease&) = delete;
LabDeviceLease& operator=(const LabDeviceLease&) = delete;
private:
QLockFile m_lock;
};

// Check the allowlist before acquiring a lease. A denial never touches hardware.
// Throws std::runtime_error on denied access, contention or filesystem failure.
std::unique_ptr<LabDeviceLease> acquire_lab_device(
const LabDevicePolicy& policy, std::string_view directory, const LabDeviceIdentity& device
);

} // namespace PokemonAutomation
#endif
Loading
Loading