blob: f8270f22de9f252f47d9b0d7723baeb8adaa76e1 [file] [log] [blame]
Andrew Jefferydae6e182021-05-21 16:23:07 +09301#pragma once
2
3#include "NVMeSensor.hpp"
4
5#include <boost/asio/deadline_timer.hpp>
6#include <boost/asio/io_service.hpp>
Andrew Jefferydae6e182021-05-21 16:23:07 +09307
8#include <memory>
Andrew Jeffery25e20bd2022-03-15 22:26:04 +10309#include <stdexcept>
Andrew Jefferydae6e182021-05-21 16:23:07 +093010
Andrew Jefferyfa500ae2021-05-21 16:46:36 +093011class NVMeContext : public std::enable_shared_from_this<NVMeContext>
Andrew Jefferydae6e182021-05-21 16:23:07 +093012{
Andrew Jefferyfa500ae2021-05-21 16:46:36 +093013 public:
Andrew Jefferya9d15082021-05-24 13:55:12 +093014 NVMeContext(boost::asio::io_service& io, int rootBus) :
15 scanTimer(io), rootBus(rootBus)
Andrew Jeffery25e20bd2022-03-15 22:26:04 +103016 {
17 if (rootBus < 0)
18 {
19 throw std::invalid_argument(
20 "Invalid root bus: Bus ID must not be negative");
21 }
22 }
Andrew Jefferydae6e182021-05-21 16:23:07 +093023
Andrew Jefferya9d15082021-05-24 13:55:12 +093024 virtual ~NVMeContext()
25 {
26 close();
27 }
Andrew Jefferydae6e182021-05-21 16:23:07 +093028
Ed Tanous74cffa82022-01-25 13:00:28 -080029 void addSensor(const std::shared_ptr<NVMeSensor>& sensor)
Andrew Jefferya9d15082021-05-24 13:55:12 +093030 {
31 sensors.emplace_back(sensor);
32 }
Andrew Jefferydae6e182021-05-21 16:23:07 +093033
Andrew Jeffery3db6f9c2021-12-08 10:55:47 +103034 std::optional<std::shared_ptr<NVMeSensor>>
35 getSensorAtPath(const std::string& path)
36 {
37 for (auto& sensor : sensors)
38 {
39 if (sensor->configurationPath == path)
40 {
41 return sensor;
42 }
43 }
44
45 return std::nullopt;
46 }
47
Ed Tanous74cffa82022-01-25 13:00:28 -080048 void removeSensor(const std::shared_ptr<NVMeSensor>& sensor)
Andrew Jeffery3db6f9c2021-12-08 10:55:47 +103049 {
50 sensors.remove(sensor);
51 }
52
Andrew Jefferya9d15082021-05-24 13:55:12 +093053 virtual void pollNVMeDevices()
54 {}
55
56 virtual void close()
57 {
58 scanTimer.cancel();
59 }
60
61 virtual void readAndProcessNVMeSensor()
62 {}
63
64 virtual void processResponse(void* msg, size_t len)
65 {
66 (void)msg;
67 (void)len;
68 }
69
70 protected:
Andrew Jefferydae6e182021-05-21 16:23:07 +093071 boost::asio::deadline_timer scanTimer;
Andrew Jefferya9d15082021-05-24 13:55:12 +093072 int rootBus; // Root bus for this drive
Andrew Jefferydae6e182021-05-21 16:23:07 +093073 std::list<std::shared_ptr<NVMeSensor>> sensors; // used as a poll queue
Andrew Jefferydae6e182021-05-21 16:23:07 +093074};
75
76using NVMEMap = boost::container::flat_map<int, std::shared_ptr<NVMeContext>>;
77
Andrew Jefferydae6e182021-05-21 16:23:07 +093078NVMEMap& getNVMEMap(void);