implemented Producer API and header, stub and tests. Builds cleanly, tests fail for now

This commit is contained in:
2026-03-10 16:54:28 +00:00
parent a798caf1a8
commit 9c2117e64b
3 changed files with 211 additions and 0 deletions

46
include/Producer.hpp Normal file
View File

@@ -0,0 +1,46 @@
#pragma once
// Producer.hpp
// SPDX-License-Identifier: GPL-3.0-or-later
// Author: Unai Blazquez <unaibg2000@gmail.com>
#include <atomic>
#include <chrono>
#include <functional>
#include <thread>
#include "SysfsRead.hpp"
using RandomFn = std::function<int()>;
using SleepFn = std::function<void(std::chrono::milliseconds)>;
class Producer
{
public:
/// @brief Construct a Producer bound to a sysfs-like control file.
/// @param sysfs_path Path to the control file (e.g. "./fake_sysfs_input").
/// @param send_fn Function called whenever a new integer should be sent.
/// @param sleep_fn Stub for sleeping function, allows reducing sleep on
/// testing
Producer(const std::filesystem::path& sysfs_path,
std::function<void(int)> send_fn, RandomFn random_fn,
SleepFn sleep_fn = default_sleep);
/// @brief Start the worker thread. Safe to call only once.
void start();
/// @brief Request the worker thread to stop and wait for it to finish.
void stop();
private:
/// @brief Main loop executed by the worker thread.
void run_loop();
std::thread m_thread;
std::atomic<bool> m_running;
SysfsReader m_reader;
RandomFn m_random;
SleepFn m_sleep;
static void default_sleep(std::chrono::milliseconds d)
{
std::this_thread::sleep_for(d);
}
std::function<void(int)> m_send;
};