Andrew Jeffery | dae6e18 | 2021-05-21 16:23:07 +0930 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "NVMeSensor.hpp" |
| 4 | |
| 5 | #include <boost/asio/deadline_timer.hpp> |
| 6 | #include <boost/asio/io_service.hpp> |
Andrew Jeffery | dae6e18 | 2021-05-21 16:23:07 +0930 | [diff] [blame] | 7 | |
| 8 | #include <memory> |
Andrew Jeffery | 25e20bd | 2022-03-15 22:26:04 +1030 | [diff] [blame^] | 9 | #include <stdexcept> |
Andrew Jeffery | dae6e18 | 2021-05-21 16:23:07 +0930 | [diff] [blame] | 10 | |
Andrew Jeffery | fa500ae | 2021-05-21 16:46:36 +0930 | [diff] [blame] | 11 | class NVMeContext : public std::enable_shared_from_this<NVMeContext> |
Andrew Jeffery | dae6e18 | 2021-05-21 16:23:07 +0930 | [diff] [blame] | 12 | { |
Andrew Jeffery | fa500ae | 2021-05-21 16:46:36 +0930 | [diff] [blame] | 13 | public: |
Andrew Jeffery | a9d1508 | 2021-05-24 13:55:12 +0930 | [diff] [blame] | 14 | NVMeContext(boost::asio::io_service& io, int rootBus) : |
| 15 | scanTimer(io), rootBus(rootBus) |
Andrew Jeffery | 25e20bd | 2022-03-15 22:26:04 +1030 | [diff] [blame^] | 16 | { |
| 17 | if (rootBus < 0) |
| 18 | { |
| 19 | throw std::invalid_argument( |
| 20 | "Invalid root bus: Bus ID must not be negative"); |
| 21 | } |
| 22 | } |
Andrew Jeffery | dae6e18 | 2021-05-21 16:23:07 +0930 | [diff] [blame] | 23 | |
Andrew Jeffery | a9d1508 | 2021-05-24 13:55:12 +0930 | [diff] [blame] | 24 | virtual ~NVMeContext() |
| 25 | { |
| 26 | close(); |
| 27 | } |
Andrew Jeffery | dae6e18 | 2021-05-21 16:23:07 +0930 | [diff] [blame] | 28 | |
Ed Tanous | 74cffa8 | 2022-01-25 13:00:28 -0800 | [diff] [blame] | 29 | void addSensor(const std::shared_ptr<NVMeSensor>& sensor) |
Andrew Jeffery | a9d1508 | 2021-05-24 13:55:12 +0930 | [diff] [blame] | 30 | { |
| 31 | sensors.emplace_back(sensor); |
| 32 | } |
Andrew Jeffery | dae6e18 | 2021-05-21 16:23:07 +0930 | [diff] [blame] | 33 | |
Andrew Jeffery | 3db6f9c | 2021-12-08 10:55:47 +1030 | [diff] [blame] | 34 | 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 Tanous | 74cffa8 | 2022-01-25 13:00:28 -0800 | [diff] [blame] | 48 | void removeSensor(const std::shared_ptr<NVMeSensor>& sensor) |
Andrew Jeffery | 3db6f9c | 2021-12-08 10:55:47 +1030 | [diff] [blame] | 49 | { |
| 50 | sensors.remove(sensor); |
| 51 | } |
| 52 | |
Andrew Jeffery | a9d1508 | 2021-05-24 13:55:12 +0930 | [diff] [blame] | 53 | 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 Jeffery | dae6e18 | 2021-05-21 16:23:07 +0930 | [diff] [blame] | 71 | boost::asio::deadline_timer scanTimer; |
Andrew Jeffery | a9d1508 | 2021-05-24 13:55:12 +0930 | [diff] [blame] | 72 | int rootBus; // Root bus for this drive |
Andrew Jeffery | dae6e18 | 2021-05-21 16:23:07 +0930 | [diff] [blame] | 73 | std::list<std::shared_ptr<NVMeSensor>> sensors; // used as a poll queue |
Andrew Jeffery | dae6e18 | 2021-05-21 16:23:07 +0930 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | using NVMEMap = boost::container::flat_map<int, std::shared_ptr<NVMeContext>>; |
| 77 | |
Andrew Jeffery | dae6e18 | 2021-05-21 16:23:07 +0930 | [diff] [blame] | 78 | NVMEMap& getNVMEMap(void); |