blob: b17fc05dbec4f30bc8cd0819e1f1adbbe60ade2e [file] [log] [blame]
Andrew Jefferye3e3c972021-05-26 14:37:07 +09301#pragma once
2
3#include "NVMeContext.hpp"
4
5#include <boost/asio/io_service.hpp>
6#include <boost/asio/posix/stream_descriptor.hpp>
7
Andrew Jeffery3cbd5a12022-07-18 16:32:11 +09308#include <thread>
9
Andrew Jefferye3e3c972021-05-26 14:37:07 +093010class NVMeBasicContext : public NVMeContext
11{
12 public:
13 NVMeBasicContext(boost::asio::io_service& io, int rootBus);
Ed Tanous74cffa82022-01-25 13:00:28 -080014 ~NVMeBasicContext() override = default;
15 void pollNVMeDevices() override;
Andrew Jefferyb5d7a7f2022-05-02 11:57:03 +093016 void readAndProcessNVMeSensor() override;
Andrew Jeffery8c7074e2022-03-21 14:58:13 +103017 void processResponse(std::shared_ptr<NVMeSensor>& sensor, void* msg,
18 size_t len) override;
Andrew Jefferye3e3c972021-05-26 14:37:07 +093019
20 private:
21 NVMeBasicContext(boost::asio::io_service& io, int rootBus, int cmdOut,
22 int streamIn, int streamOut, int cmdIn);
23 boost::asio::io_service& io;
Andrew Jeffery3cbd5a12022-07-18 16:32:11 +093024
25 // The IO thread must be destructed after the stream descriptors, so
26 // initialise it first. http://eel.is/c++draft/class.base.init#note-6
27 //
28 // Providing a stop-source to the thread execution function isn't
29 // particularly useful as it will spend most of its time blocked in a system
30 // call - ioctl() for the actual device communication, or read() and write()
31 // on the pipes associated with reqStream and respStream. Rather than trying
32 // to force a stop, rely on read()/write() failures from closed pipes to
33 // coerce it to exit and thus allow completion of the join().
34 std::jthread thread;
35
36 // Destruction of the stream descriptors has the effect of issuing cancel(),
37 // destroying the closure of the callback where we might be carrying
38 // weak_ptrs to `this`.
39 // https://www.boost.org/doc/libs/1_79_0/doc/html/boost_asio/reference/posix__basic_descriptor/_basic_descriptor.html
Andrew Jefferye3e3c972021-05-26 14:37:07 +093040 boost::asio::posix::stream_descriptor reqStream;
41 boost::asio::posix::stream_descriptor respStream;
Andrew Jeffery7aeb1a52022-03-15 22:49:04 +103042
43 enum
44 {
45 NVME_MI_BASIC_SFLGS_DRIVE_NOT_READY = 0x40,
46 NVME_MI_BASIC_SFLGS_DRIVE_FUNCTIONAL = 0x20,
47 };
Andrew Jefferye3e3c972021-05-26 14:37:07 +093048};