aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/utils/libservice
diff options
context:
space:
mode:
Diffstat (limited to 'utils/libservice')
-rw-r--r--utils/libservice/include/libservice/device.hpp71
-rw-r--r--utils/libservice/include/libservice/handle.hpp80
-rw-r--r--utils/libservice/include/libservice/interface.hpp2
-rw-r--r--utils/libservice/src/device.cpp126
-rw-r--r--utils/libservice/src/handle.cpp29
5 files changed, 308 insertions, 0 deletions
diff --git a/utils/libservice/include/libservice/device.hpp b/utils/libservice/include/libservice/device.hpp
new file mode 100644
index 0000000..3a9540e
--- /dev/null
+++ b/utils/libservice/include/libservice/device.hpp
@@ -0,0 +1,71 @@
+/**
+ * \file
+ * \author Egor Tensin <Egor.Tensin@gmail.com>
+ * \date 2015
+ * \copyright This file is licensed under the terms of the MIT License.
+ * See LICENSE.txt for details.
+ */
+
+#pragma once
+
+#include "common.hpp"
+#include "handle.hpp"
+
+#include <Windows.h>
+
+#include <string>
+#include <utility>
+
+namespace libservice
+{
+ class Device
+ {
+ public:
+ static Device open(const std::string& path);
+
+ Device(Device&& other) LIBSERVICE_NOEXCEPT
+ {
+ swap(other);
+ }
+
+ Device& operator=(Device other) LIBSERVICE_NOEXCEPT
+ {
+ swap(other);
+ return *this;
+ }
+
+ void swap(Device& other) LIBSERVICE_NOEXCEPT
+ {
+ using std::swap;
+ swap(m_handle, other.m_handle);
+ }
+
+ DWORD get_required_output_size(DWORD code,
+ const void* in_buf,
+ DWORD in_buf_size) const;
+
+ DWORD send_control_code(DWORD code,
+ const void* in_buf,
+ DWORD in_buf_size,
+ void* out_buf,
+ DWORD out_buf_size) const;
+
+ private:
+ explicit Device(Handle h)
+ : m_handle(std::move(h))
+ { }
+
+ Handle m_handle;
+
+ Device(const Device&) = delete;
+ };
+
+ void swap(libservice::Device&, libservice::Device&) LIBSERVICE_NOEXCEPT;
+}
+
+namespace std
+{
+ template <>
+ void swap<libservice::Device>(
+ libservice::Device&, libservice::Device&) LIBSERVICE_NOEXCEPT;
+}
diff --git a/utils/libservice/include/libservice/handle.hpp b/utils/libservice/include/libservice/handle.hpp
new file mode 100644
index 0000000..1292090
--- /dev/null
+++ b/utils/libservice/include/libservice/handle.hpp
@@ -0,0 +1,80 @@
+/**
+ * \file
+ * \author Egor Tensin <Egor.Tensin@gmail.com>
+ * \date 2015
+ * \copyright This file is licensed under the terms of the MIT License.
+ * See LICENSE.txt for details.
+ */
+
+#pragma once
+
+#include "common.hpp"
+
+#include <Windows.h>
+
+#include <memory>
+#include <type_traits>
+#include <utility>
+
+namespace libservice
+{
+ class Handle
+ {
+ public:
+ Handle() = default;
+
+ explicit Handle(HANDLE raw)
+ : m_impl(raw)
+ { }
+
+ Handle(Handle&& other) LIBSERVICE_NOEXCEPT
+ {
+ swap(other);
+ }
+
+ Handle& operator=(Handle other) LIBSERVICE_NOEXCEPT
+ {
+ swap(other);
+ return *this;
+ }
+
+ explicit operator bool() const
+ {
+ return static_cast<bool>(m_impl);
+ }
+
+ explicit operator HANDLE() const
+ {
+ return m_impl.get();
+ }
+
+ void swap(Handle& other) LIBSERVICE_NOEXCEPT
+ {
+ using std::swap;
+ swap(m_impl, other.m_impl);
+ }
+
+ private:
+ struct Deleter
+ {
+ void operator()(HANDLE raw)
+ {
+ ::CloseHandle(raw);
+ }
+ };
+
+ std::unique_ptr<std::remove_pointer<HANDLE>::type, Deleter> m_impl;
+
+ Handle(const Handle&) = delete;
+ };
+
+ void swap(Handle& a, Handle& b) LIBSERVICE_NOEXCEPT;
+}
+
+namespace std
+{
+ template <>
+ void swap<libservice::Handle>(
+ libservice::Handle& a,
+ libservice::Handle& b) LIBSERVICE_NOEXCEPT;
+}
diff --git a/utils/libservice/include/libservice/interface.hpp b/utils/libservice/include/libservice/interface.hpp
index 53751b2..e68439b 100644
--- a/utils/libservice/include/libservice/interface.hpp
+++ b/utils/libservice/include/libservice/interface.hpp
@@ -9,6 +9,8 @@
#pragma once
#include "common.hpp"
+#include "device.hpp"
+#include "handle.hpp"
#include "service.hpp"
#include "service_handle.hpp"
#include "service_manager.hpp"
diff --git a/utils/libservice/src/device.cpp b/utils/libservice/src/device.cpp
new file mode 100644
index 0000000..3cfb760
--- /dev/null
+++ b/utils/libservice/src/device.cpp
@@ -0,0 +1,126 @@
+/**
+ * \file
+ * \author Egor Tensin <Egor.Tensin@gmail.com>
+ * \date 2015
+ * \copyright This file is licensed under the terms of the MIT License.
+ * See LICENSE.txt for details.
+ */
+
+#include "libservice/common.hpp"
+#include "libservice/device.hpp"
+#include "libservice/handle.hpp"
+#include "libservice/windows_error.hpp"
+
+#include <Windows.h>
+
+#include <string>
+#include <system_error>
+#include <utility>
+
+namespace libservice
+{
+ namespace
+ {
+ Handle open_device(const std::string& path)
+ {
+ const auto raw = CreateFileA(path.c_str(),
+ GENERIC_READ | GENERIC_WRITE,
+ 0,
+ NULL,
+ OPEN_EXISTING,
+ FILE_ATTRIBUTE_NORMAL,
+ NULL);
+
+ if (INVALID_HANDLE_VALUE == raw)
+ {
+ const auto ec = GetLastError();
+ throw std::system_error(ec, WinErrorCategory::get(), LIBSERVICE_ERROR_PREFIX);
+ }
+
+ return Handle(raw);
+ }
+ }
+
+ Device Device::open(const std::string& path)
+ {
+ return Device(open_device(path));
+ }
+
+ DWORD Device::get_required_output_size(
+ DWORD code,
+ const void* in_buf,
+ DWORD in_buf_size) const
+ {
+ DWORD nbreq;
+
+ DWORD ret = DeviceIoControl(
+ static_cast<HANDLE>(m_handle),
+ code,
+ const_cast<void*>(in_buf),
+ in_buf_size,
+ NULL,
+ 0,
+ &nbreq,
+ NULL);
+
+ if (0 == ret)
+ {
+ const auto ec = GetLastError();
+
+ switch (ec)
+ {
+ case ERROR_MORE_DATA:
+ return nbreq;
+
+ default:
+ throw std::system_error(ec, WinErrorCategory::get(), LIBSERVICE_ERROR_PREFIX);
+ }
+ }
+
+ return ret;
+ }
+
+ DWORD Device::send_control_code(
+ DWORD code,
+ const void* in_buf,
+ DWORD in_buf_size,
+ void* out_buf,
+ DWORD out_buf_size) const
+ {
+ DWORD nbreq;
+
+ DWORD ret = DeviceIoControl(
+ static_cast<HANDLE>(m_handle),
+ code,
+ const_cast<void*>(in_buf),
+ in_buf_size,
+ out_buf,
+ out_buf_size,
+ &nbreq,
+ NULL);
+
+ if (0 == ret)
+ {
+ const auto ec = GetLastError();
+ throw std::system_error(ec, WinErrorCategory::get(), LIBSERVICE_ERROR_PREFIX);
+ }
+
+ return ret;
+ }
+
+ void swap(Device& a, Device& b) LIBSERVICE_NOEXCEPT
+ {
+ a.swap(b);
+ }
+}
+
+namespace std
+{
+ template <>
+ void swap<libservice::Device>(
+ libservice::Device& a,
+ libservice::Device& b)
+ {
+ a.swap(b);
+ }
+}
diff --git a/utils/libservice/src/handle.cpp b/utils/libservice/src/handle.cpp
new file mode 100644
index 0000000..2f9a65f
--- /dev/null
+++ b/utils/libservice/src/handle.cpp
@@ -0,0 +1,29 @@
+/**
+ * \file
+ * \author Egor Tensin <Egor.Tensin@gmail.com>
+ * \date 2015
+ * \copyright This file is licensed under the terms of the MIT License.
+ * See LICENSE.txt for details.
+ */
+
+#include "libservice/common.hpp"
+#include "libservice/handle.hpp"
+
+namespace libservice
+{
+ void swap(Handle& a, Handle& b) LIBSERVICE_NOEXCEPT
+ {
+ a.swap(b);
+ }
+}
+
+namespace std
+{
+ template <>
+ void swap<libservice::Handle>(
+ libservice::Handle& a,
+ libservice::Handle& b) LIBSERVICE_NOEXCEPT
+ {
+ a.swap(b);
+ }
+}