blob: d9275d4045a58d683c635e2e66a654721848386e [file] [log] [blame]
#ifndef THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_COLLECTOR_GPIO_COLLECTOR_H_
#define THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_COLLECTOR_GPIO_COLLECTOR_H_
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "absl/container/flat_hash_map.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "thread/thread.h"
#include "time/clock.h"
#include "nlohmann/json.hpp"
#include "tlbmc/collector/collector.h"
#include "gpio_config.pb.h"
#include "tlbmc/hal/gpio/gpio.h"
#include "tlbmc/hal/gpio/gpio_monitor.h"
#include "tlbmc/scheduler/scheduler.h"
namespace milotic_tlbmc {
struct GpioThreadManager {
explicit GpioThreadManager(ecclesia::Clock* clock)
: task_scheduler(std::make_unique<TaskScheduler>(clock)) {}
~GpioThreadManager();
std::vector<std::unique_ptr<ecclesia::ThreadInterface>> threads;
std::vector<std::shared_ptr<boost::asio::io_context>> io_contexts;
std::vector<
boost::asio::executor_work_guard<boost::asio::io_context::executor_type>>
work_guards;
std::unique_ptr<TaskScheduler> task_scheduler;
};
class GpioCollector : public Collector {
public:
// Parameters for creating a GPIO collector.
struct Params {
// Using a thread factory to allow us to switch to fibers in future when it
// gets open sourced.
ecclesia::ThreadFactoryInterface* thread_factory =
ecclesia::GetDefaultThreadFactory();
ecclesia::Clock* clock = ecclesia::Clock::RealClock();
GpioConfigs gpio_configs;
};
static absl::StatusOr<std::unique_ptr<GpioCollector>> Create(
const Params& params);
nlohmann::json ToJson() const override;
nlohmann::json GetSchedulerStats() const override {
return thread_manager_->task_scheduler->ToJson();
}
absl::Status SetGpio(absl::string_view gpio_name, bool value);
protected:
// Protected constructor to allow mocking.
GpioCollector() = default;
private:
GpioCollector(
std::unique_ptr<GpioThreadManager>&& thread_manager,
absl::flat_hash_map<std::string, std::unique_ptr<Gpio>>&& input_gpios,
absl::flat_hash_map<std::string, std::unique_ptr<Gpio>>&& output_gpios,
absl::flat_hash_map<std::string, std::unique_ptr<GpioMonitor>>&&
gpio_monitors)
: thread_manager_(std::move(thread_manager)),
input_gpios_(std::move(input_gpios)),
output_gpios_(std::move(output_gpios)),
gpio_monitors_(std::move(gpio_monitors)) {}
std::unique_ptr<GpioThreadManager> thread_manager_;
absl::flat_hash_map<std::string, std::unique_ptr<Gpio>> input_gpios_;
absl::flat_hash_map<std::string, std::unique_ptr<Gpio>> output_gpios_;
absl::flat_hash_map<std::string, std::unique_ptr<GpioMonitor>> gpio_monitors_;
};
class EmptyGpioCollector final : public GpioCollector {
public:
static std::unique_ptr<EmptyGpioCollector> Create();
nlohmann::json GetSchedulerStats() const override;
nlohmann::json ToJson() const override;
};
} // namespace milotic_tlbmc
#endif // THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_COLLECTOR_GPIO_COLLECTOR_H_