blob: 27c1720dcdc60dd2618699523e8a88768adcb290 [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>
9
Andrew Jefferyfa500ae2021-05-21 16:46:36 +093010class NVMeContext : public std::enable_shared_from_this<NVMeContext>
Andrew Jefferydae6e182021-05-21 16:23:07 +093011{
Andrew Jefferyfa500ae2021-05-21 16:46:36 +093012 public:
Andrew Jefferya9d15082021-05-24 13:55:12 +093013 NVMeContext(boost::asio::io_service& io, int rootBus) :
14 scanTimer(io), rootBus(rootBus)
15 {}
Andrew Jefferydae6e182021-05-21 16:23:07 +093016
Andrew Jefferya9d15082021-05-24 13:55:12 +093017 virtual ~NVMeContext()
18 {
19 close();
20 }
Andrew Jefferydae6e182021-05-21 16:23:07 +093021
Ed Tanous74cffa82022-01-25 13:00:28 -080022 void addSensor(const std::shared_ptr<NVMeSensor>& sensor)
Andrew Jefferya9d15082021-05-24 13:55:12 +093023 {
24 sensors.emplace_back(sensor);
25 }
Andrew Jefferydae6e182021-05-21 16:23:07 +093026
Andrew Jeffery3db6f9c2021-12-08 10:55:47 +103027 std::optional<std::shared_ptr<NVMeSensor>>
28 getSensorAtPath(const std::string& path)
29 {
30 for (auto& sensor : sensors)
31 {
32 if (sensor->configurationPath == path)
33 {
34 return sensor;
35 }
36 }
37
38 return std::nullopt;
39 }
40
Ed Tanous74cffa82022-01-25 13:00:28 -080041 void removeSensor(const std::shared_ptr<NVMeSensor>& sensor)
Andrew Jeffery3db6f9c2021-12-08 10:55:47 +103042 {
43 sensors.remove(sensor);
44 }
45
Andrew Jefferya9d15082021-05-24 13:55:12 +093046 virtual void pollNVMeDevices()
47 {}
48
49 virtual void close()
50 {
51 scanTimer.cancel();
52 }
53
54 virtual void readAndProcessNVMeSensor()
55 {}
56
57 virtual void processResponse(void* msg, size_t len)
58 {
59 (void)msg;
60 (void)len;
61 }
62
63 protected:
Andrew Jefferydae6e182021-05-21 16:23:07 +093064 boost::asio::deadline_timer scanTimer;
Andrew Jefferya9d15082021-05-24 13:55:12 +093065 int rootBus; // Root bus for this drive
Andrew Jefferydae6e182021-05-21 16:23:07 +093066 std::list<std::shared_ptr<NVMeSensor>> sensors; // used as a poll queue
Andrew Jefferydae6e182021-05-21 16:23:07 +093067};
68
69using NVMEMap = boost::container::flat_map<int, std::shared_ptr<NVMeContext>>;
70
Andrew Jefferydae6e182021-05-21 16:23:07 +093071NVMEMap& getNVMEMap(void);