Andrew Jeffery | dae6e18 | 2021-05-21 16:23:07 +0930 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "NVMeSensor.hpp" |
| 4 | |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 5 | #include <boost/asio/io_context.hpp> |
Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 6 | #include <boost/asio/steady_timer.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: |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 14 | NVMeContext(boost::asio::io_context& io, int rootBus) : |
Andrew Jeffery | b5d7a7f | 2022-05-02 11:57:03 +0930 | [diff] [blame] | 15 | scanTimer(io), rootBus(rootBus), pollCursor(sensors.end()) |
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 | { |
Ed Tanous | ec77caa | 2023-02-17 10:46:06 -0800 | [diff] [blame] | 26 | scanTimer.cancel(); |
Andrew Jeffery | a9d1508 | 2021-05-24 13:55:12 +0930 | [diff] [blame] | 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 | |
Andrew Jeffery | b5d7a7f | 2022-05-02 11:57:03 +0930 | [diff] [blame] | 48 | // Post-condition: The sensor list does not contain the provided sensor |
| 49 | // Post-condition: pollCursor is a valid iterator for the sensor list |
Ed Tanous | 74cffa8 | 2022-01-25 13:00:28 -0800 | [diff] [blame] | 50 | void removeSensor(const std::shared_ptr<NVMeSensor>& sensor) |
Andrew Jeffery | 3db6f9c | 2021-12-08 10:55:47 +1030 | [diff] [blame] | 51 | { |
Andrew Jeffery | b5d7a7f | 2022-05-02 11:57:03 +0930 | [diff] [blame] | 52 | // Locate the sensor that we're removing in the sensor list |
| 53 | auto found = std::find(sensors.begin(), sensors.end(), sensor); |
| 54 | |
| 55 | // If we failed to find the sensor in the list the post-condition is |
| 56 | // already satisfied |
| 57 | if (found == sensors.end()) |
| 58 | { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | // We've found the sensor in the list |
| 63 | |
| 64 | // If we're not actively polling the sensor list, then remove the sensor |
| 65 | if (pollCursor == sensors.end()) |
| 66 | { |
| 67 | sensors.erase(found); |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | // We're actively polling the sensor list |
| 72 | |
| 73 | // If we're not polling the specific sensor that has been removed, then |
| 74 | // remove the sensor |
| 75 | if (*pollCursor != *found) |
| 76 | { |
| 77 | sensors.erase(found); |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | // We're polling the sensor that is being removed |
| 82 | |
| 83 | // Remove the sensor and update the poll cursor so the cursor remains |
| 84 | // valid |
| 85 | pollCursor = sensors.erase(found); |
Andrew Jeffery | 3db6f9c | 2021-12-08 10:55:47 +1030 | [diff] [blame] | 86 | } |
| 87 | |
Andrew Jeffery | a9d1508 | 2021-05-24 13:55:12 +0930 | [diff] [blame] | 88 | virtual void close() |
| 89 | { |
| 90 | scanTimer.cancel(); |
| 91 | } |
| 92 | |
Andrew Jeffery | 8c7074e | 2022-03-21 14:58:13 +1030 | [diff] [blame] | 93 | virtual void pollNVMeDevices() = 0; |
Andrew Jeffery | a9d1508 | 2021-05-24 13:55:12 +0930 | [diff] [blame] | 94 | |
Andrew Jeffery | b5d7a7f | 2022-05-02 11:57:03 +0930 | [diff] [blame] | 95 | virtual void readAndProcessNVMeSensor() = 0; |
Andrew Jeffery | 8c7074e | 2022-03-21 14:58:13 +1030 | [diff] [blame] | 96 | |
| 97 | virtual void processResponse(std::shared_ptr<NVMeSensor>& sensor, void* msg, |
| 98 | size_t len) = 0; |
Andrew Jeffery | a9d1508 | 2021-05-24 13:55:12 +0930 | [diff] [blame] | 99 | |
| 100 | protected: |
Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 101 | boost::asio::steady_timer scanTimer; |
Andrew Jeffery | b5d7a7f | 2022-05-02 11:57:03 +0930 | [diff] [blame] | 102 | int rootBus; // Root bus for this drive |
| 103 | std::list<std::shared_ptr<NVMeSensor>> sensors; |
| 104 | std::list<std::shared_ptr<NVMeSensor>>::iterator pollCursor; |
Andrew Jeffery | dae6e18 | 2021-05-21 16:23:07 +0930 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | using NVMEMap = boost::container::flat_map<int, std::shared_ptr<NVMeContext>>; |
| 108 | |
Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 109 | NVMEMap& getNVMEMap(); |