From 815e67bdfb2d83649a84df101abc8f04588929a7 Mon Sep 17 00:00:00 2001 From: Moritz Martinius Date: Sat, 12 Nov 2022 15:28:21 +0100 Subject: [PATCH] Add USB stub, add logging using spdlog --- .vscode/c_cpp_properties.json | 5 ++++- .vscode/settings.json | 6 +++++- inc/IUsb.hpp | 16 +++++++++++++++ inc/Usb.hpp | 20 +++++++++++++++++++ inc/UsbTypes.hpp | 13 ++++++++++++ meson.build | 9 ++++++--- src/Usb.cpp | 37 +++++++++++++++++++++++++++++++++++ src/main.cpp | 7 +++++-- 8 files changed, 106 insertions(+), 7 deletions(-) create mode 100644 inc/IUsb.hpp create mode 100644 inc/Usb.hpp create mode 100644 inc/UsbTypes.hpp create mode 100644 src/Usb.cpp diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 1dad740..a99ab15 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -5,7 +5,10 @@ "compilerPath": "/usr/bin/clang", "cStandard": "c11", "cppStandard": "c++17", - "compileCommands": "/builddir/compile_commands.json" + "compileCommands": "${workspaceFolder}/builddir/compile_commands.json", + "browse": { + "path": ["${workspaceFolder}"] + } } ], "version": 4 diff --git a/.vscode/settings.json b/.vscode/settings.json index bd3632f..9e3b11c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -78,6 +78,10 @@ "typeindex": "cpp", "typeinfo": "cpp", "valarray": "cpp", - "variant": "cpp" + "variant": "cpp", + "csignal": "cpp", + "any": "cpp", + "regex": "cpp", + "future": "cpp" } } \ No newline at end of file diff --git a/inc/IUsb.hpp b/inc/IUsb.hpp new file mode 100644 index 0000000..a964425 --- /dev/null +++ b/inc/IUsb.hpp @@ -0,0 +1,16 @@ +#include "UsbTypes.hpp" + +#include + +#pragma once + +namespace ptprnt::driver { + +class IUsb { + public: + virtual ~IUsb(){}; + + virtual std::vector listDevices() = 0; +}; + +} // namespace ptprnt::driver \ No newline at end of file diff --git a/inc/Usb.hpp b/inc/Usb.hpp new file mode 100644 index 0000000..9249086 --- /dev/null +++ b/inc/Usb.hpp @@ -0,0 +1,20 @@ +#include "IUsb.hpp" + +#include + +#pragma once + +namespace ptprnt::driver { + +class Usb : public IUsb { + public: + Usb(); + ~Usb() override; + + std::vector listDevices() override; + + private: + +}; + +} // namespace ptprnt::driver \ No newline at end of file diff --git a/inc/UsbTypes.hpp b/inc/UsbTypes.hpp new file mode 100644 index 0000000..6403602 --- /dev/null +++ b/inc/UsbTypes.hpp @@ -0,0 +1,13 @@ + +#include + +#pragma once + +namespace ptprnt::driver { + +struct UsbDevice { + libusb_device* dev{nullptr}; + libusb_device_handle* hndl{nullptr}; +}; + +} // namespace ptprnt::driver \ No newline at end of file diff --git a/meson.build b/meson.build index 460c233..55f448e 100644 --- a/meson.build +++ b/meson.build @@ -1,10 +1,11 @@ project('ptprnt', 'cpp', - version: '0.1.0', + version: 'v0.1.0-'+run_command('git', 'rev-parse', '--short', 'HEAD').stdout().strip(), license: 'GPLv3', default_options : ['c_std=c11', 'cpp_std=c++17'] ) usbdep = dependency('libusb-1.0') +logdep = dependency('spdlog') incdir = include_directories('inc') @@ -16,7 +17,9 @@ srcs = [ ] executable( - 'ptprnt', srcs, + 'ptprnt', + srcs, include_directories : incdir, - dependencies : usbdep + dependencies : [usbdep, logdep], + cpp_args : ['-DPROJ_VERSION="'+meson.project_version()+'"'] ) \ No newline at end of file diff --git a/src/Usb.cpp b/src/Usb.cpp new file mode 100644 index 0000000..645c40c --- /dev/null +++ b/src/Usb.cpp @@ -0,0 +1,37 @@ +#include "Usb.hpp" + +#include +#include + +namespace ptprnt::driver { + +Usb::Usb() { + std::cout << "Usb starting" << std::endl; + libusb_init(NULL); +} +Usb::~Usb() { + std::cout << "Usb stopping" << std::endl; +} + +std::vector Usb::listDevices() { + libusb_device ** devs; + libusb_device *dev; + struct libusb_device_descriptor desc; + int r,i=0; + + libusb_get_device_list(NULL, &devs); + + while ((dev=devs[i++]) != NULL) { + if ((r=libusb_get_device_descriptor(dev, &desc)) < 0) { + std::cerr << "failed to open device" << std::endl; + libusb_free_device_list(devs, 1); + } + + std::cout << std::hex << std::setw(4) << desc.idVendor << ":" << desc.idProduct; + std::cout << std::endl; + } + + return std::vector(); +} + +} // namespace ptprnt::driver \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index f697b18..c3c80e7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,11 +6,13 @@ #include "Usb.hpp" #include +#include + using namespace ptprnt; int main(int argc, char** argv) { - std::cout << "Hello ptprnt!" << std::endl; + spdlog::info("Starting ptprnt {}", PROJ_VERSION); auto usb = std::make_unique(); auto devs = usb->listDevices(); @@ -24,4 +26,5 @@ int main(int argc, char** argv) { printer::info info = printer->getInfo();*/ return 0; -} \ No newline at end of file +} +