1 #pragma once
2 
3 #include "NVMeContext.hpp"
4 
5 #include <boost/asio/io_context.hpp>
6 #include <boost/asio/posix/stream_descriptor.hpp>
7 
8 #include <thread>
9 
10 class NVMeBasicContext : public NVMeContext
11 {
12   public:
13     NVMeBasicContext(boost::asio::io_context& io, int rootBus);
14     ~NVMeBasicContext() override = default;
15     void pollNVMeDevices() override;
16     void readAndProcessNVMeSensor() override;
17     void processResponse(std::shared_ptr<NVMeSensor>& sensor, void* msg,
18                          size_t len) override;
19 
20   private:
21     NVMeBasicContext(boost::asio::io_context& io, int rootBus, int cmdOut,
22                      int streamIn, int streamOut, int cmdIn);
23     boost::asio::io_context& io;
24 
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
40     boost::asio::posix::stream_descriptor reqStream;
41     boost::asio::posix::stream_descriptor respStream;
42 
43     enum
44     {
45         NVME_MI_BASIC_SFLGS_DRIVE_NOT_READY = 0x40,
46         NVME_MI_BASIC_SFLGS_DRIVE_FUNCTIONAL = 0x20,
47     };
48 };
49