NVMeContext: Split out NVMeMCTPContext

The MCTP-oriented implementation of NVMeContext requires linking against
a libmctp build that provides an SMBus binding implementation. The SMBus
binding implementation isn't upstream, and neither are the kernel
interfaces it relies on.

Later, an NVMe MI Basic implementation will be introduced. NVMe MI Basic
only requires support for SMBus block reads rather than MCTP, though is
not as fully featured as the MCTP interface and is not required to be
supported by implementers of the NVMe specification.

For now, reduce NVMeContext to a base class that NVMeMCTPContext
extends and only use the NVMeMCTPContext type where explicitly
required. This opens up the opportunity to make MCTP support a
build-time configurable option.

Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
Change-Id: I6920f79ca389481f3be43ee9a0d336bf8f72d55b
diff --git a/include/NVMeContext.hpp b/include/NVMeContext.hpp
index 7f24e17..f7216f1 100644
--- a/include/NVMeContext.hpp
+++ b/include/NVMeContext.hpp
@@ -4,38 +4,49 @@
 
 #include <boost/asio/deadline_timer.hpp>
 #include <boost/asio/io_service.hpp>
-#include <boost/asio/ip/tcp.hpp>
 
 #include <memory>
 
 class NVMeContext : public std::enable_shared_from_this<NVMeContext>
 {
   public:
-    NVMeContext(boost::asio::io_service& io, int rootBus);
+    NVMeContext(boost::asio::io_service& io, int rootBus) :
+        scanTimer(io), rootBus(rootBus)
+    {}
 
-    virtual ~NVMeContext();
+    virtual ~NVMeContext()
+    {
+        close();
+    }
 
-    void addSensor(std::shared_ptr<NVMeSensor> sensor);
-    virtual void pollNVMeDevices();
-    virtual void close();
-    virtual void readAndProcessNVMeSensor();
-    virtual void processResponse(void* msg, size_t len);
+    void addSensor(std::shared_ptr<NVMeSensor> sensor)
+    {
+        sensors.emplace_back(sensor);
+    }
 
-  private:
+    virtual void pollNVMeDevices()
+    {}
+
+    virtual void close()
+    {
+        scanTimer.cancel();
+    }
+
+    virtual void readAndProcessNVMeSensor()
+    {}
+
+    virtual void processResponse(void* msg, size_t len)
+    {
+        (void)msg;
+        (void)len;
+    }
+
+  protected:
     boost::asio::deadline_timer scanTimer;
-    int rootBus; // Root bus for this drive
-    boost::asio::deadline_timer mctpResponseTimer;
-    boost::asio::ip::tcp::socket nvmeSlaveSocket;
+    int rootBus;                                    // Root bus for this drive
     std::list<std::shared_ptr<NVMeSensor>> sensors; // used as a poll queue
-
-    void readResponse();
 };
 
 using NVMEMap = boost::container::flat_map<int, std::shared_ptr<NVMeContext>>;
 
-namespace nvmeMCTP
-{
-void init(void);
-}
-
 NVMEMap& getNVMEMap(void);
diff --git a/include/NVMeMCTPContext.hpp b/include/NVMeMCTPContext.hpp
new file mode 100644
index 0000000..656e1fd
--- /dev/null
+++ b/include/NVMeMCTPContext.hpp
@@ -0,0 +1,29 @@
+#pragma once
+
+#include "NVMeContext.hpp"
+
+#include <boost/asio/ip/tcp.hpp>
+
+class NVMeMCTPContext : public NVMeContext
+{
+  public:
+    NVMeMCTPContext(boost::asio::io_service& io, int rootBus);
+
+    virtual ~NVMeMCTPContext();
+
+    virtual void pollNVMeDevices() override;
+    virtual void close() override;
+    virtual void readAndProcessNVMeSensor() override;
+    virtual void processResponse(void* msg, size_t len) override;
+
+  private:
+    boost::asio::ip::tcp::socket nvmeSlaveSocket;
+    boost::asio::deadline_timer mctpResponseTimer;
+
+    void readResponse();
+};
+
+namespace nvmeMCTP
+{
+void init(void);
+}
diff --git a/src/NVMeContext.cpp b/src/NVMeMCTPContext.cpp
similarity index 95%
rename from src/NVMeContext.cpp
rename to src/NVMeMCTPContext.cpp
index 36be841..40bb8ac 100644
--- a/src/NVMeContext.cpp
+++ b/src/NVMeMCTPContext.cpp
@@ -14,7 +14,7 @@
 // limitations under the License.
 */
 
-#include "NVMeContext.hpp"
+#include "NVMeMCTPContext.hpp"
 
 #include "NVMeDevice.hpp"
 
@@ -164,7 +164,7 @@
     return reading;
 }
 
-void NVMeContext::processResponse(void* msg, size_t len)
+void NVMeMCTPContext::processResponse(void* msg, size_t len)
 {
     struct nvme_mi_msg_response_header header
     {};
@@ -314,7 +314,7 @@
     return mctp_message_tx(&mctp, 0, messageBuf.data(), msgSize);
 }
 
-void NVMeContext::readResponse()
+void NVMeMCTPContext::readResponse()
 {
     nvmeSlaveSocket.async_wait(
         boost::asio::ip::tcp::socket::wait_error,
@@ -331,7 +331,7 @@
         });
 }
 
-void NVMeContext::readAndProcessNVMeSensor()
+void NVMeMCTPContext::readAndProcessNVMeSensor()
 {
     struct nvme_mi_msg_request requestMsg = {};
     requestMsg.header.opcode = NVME_MI_OPCODE_HEALTH_STATUS_POLL;
@@ -386,14 +386,15 @@
     }
 }
 
-NVMeContext::NVMeContext(boost::asio::io_service& io, int rootBus) :
-    scanTimer(io), rootBus(rootBus), mctpResponseTimer(io), nvmeSlaveSocket(io)
+NVMeMCTPContext::NVMeMCTPContext(boost::asio::io_service& io, int rootBus) :
+    NVMeContext::NVMeContext(io, rootBus), nvmeSlaveSocket(io),
+    mctpResponseTimer(io)
 {
     nvmeSlaveSocket.assign(boost::asio::ip::tcp::v4(),
                            nvmeMCTP::getInFd(rootBus));
 }
 
-void NVMeContext::pollNVMeDevices()
+void NVMeMCTPContext::pollNVMeDevices()
 {
     scanTimer.expires_from_now(boost::posix_time::seconds(1));
     scanTimer.async_wait(
@@ -416,20 +417,16 @@
         });
 }
 
-void NVMeContext::close()
+void NVMeMCTPContext::close()
 {
-    scanTimer.cancel();
+    this->NVMeContext::close();
+
     mctpResponseTimer.cancel();
     nvmeSlaveSocket.cancel();
     nvmeMCTP::closeInFd(rootBus);
 }
 
-void NVMeContext::addSensor(std::shared_ptr<NVMeSensor> sensor)
-{
-    sensors.emplace_back(sensor);
-}
-
-NVMeContext::~NVMeContext()
+NVMeMCTPContext::~NVMeMCTPContext()
 {
     close();
 }
diff --git a/src/NVMeSensorMain.cpp b/src/NVMeSensorMain.cpp
index d744ece..6c10eb5 100644
--- a/src/NVMeSensorMain.cpp
+++ b/src/NVMeSensorMain.cpp
@@ -15,6 +15,7 @@
 */
 
 #include <NVMeContext.hpp>
+#include <NVMeMCTPContext.hpp>
 #include <NVMeSensor.hpp>
 #include <boost/asio/deadline_timer.hpp>
 
@@ -127,7 +128,7 @@
                 }
                 else
                 {
-                    context = std::make_shared<NVMeContext>(io, rootBus);
+                    context = std::make_shared<NVMeMCTPContext>(io, rootBus);
                     nvmeDeviceMap[rootBus] = context;
                 }
 
diff --git a/src/meson.build b/src/meson.build
index cd861cb..c1f77ca 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -158,7 +158,7 @@
         'nvmesensor',
         'NVMeSensorMain.cpp',
         'NVMeSensor.cpp',
-        'NVMeContext.cpp',
+        'NVMeMCTPContext.cpp',
         dependencies: [
             i2c,
             sdbusplus,