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