From 9c3e94faaa0820d7968acecf4888dd0444bed047 Mon Sep 17 00:00:00 2001 From: liangzhuohua Date: Wed, 12 Jun 2024 13:53:55 +0800 Subject: [PATCH] add start --- qml/main.qml | 13 +++-- src/QmlNativeAPI.h | 7 ++- src/WFBReceiver.cpp | 128 ++++++++++++++++++++++++++++++++++---------- src/WFBReceiver.h | 20 ++++--- 4 files changed, 128 insertions(+), 40 deletions(-) diff --git a/qml/main.qml b/qml/main.qml index 04de01f..fb87b9d 100644 --- a/qml/main.qml +++ b/qml/main.qml @@ -4,8 +4,8 @@ import realTimePlayer 1.0 ApplicationWindow { visible: true - width: 960 - height: 480 + width: 1024 + height: 768 title: qsTr("") QQuickRealTimePlayer { @@ -15,7 +15,6 @@ ApplicationWindow { width: parent.width - 200 height:parent.height Component.onCompleted: { - play("rtsp://10.8.109.230:554/rtp/44050000001310000002"); } } Rectangle { @@ -183,6 +182,14 @@ ApplicationWindow { font.pixelSize: 32 color: "#ffffff" } + MouseArea{ + cursorShape: Qt.PointingHandCursor + anchors.fill: parent + onClicked: function(){ + console.log(selectDev.currentText,Number(selectChannel.currentText),Number(selectBw.currentIndex)); + NativeApi.Start(selectDev.currentText,Number(selectChannel.currentText),Number(selectBw.currentIndex)); + } + } } } Rectangle { diff --git a/src/QmlNativeAPI.h b/src/QmlNativeAPI.h index f4b3387..b602f37 100644 --- a/src/QmlNativeAPI.h +++ b/src/QmlNativeAPI.h @@ -14,7 +14,7 @@ class QmlNativeAPI : public QObject{ Q_OBJECT public: explicit QmlNativeAPI(QObject* parent = nullptr): QObject(parent){}; - //获取本地所有IP + //get all dongle Q_INVOKABLE static QList GetDongleList(){ QList l; for (auto &item : WFBReceiver::GetDongleList()){ @@ -22,7 +22,10 @@ class QmlNativeAPI : public QObject{ } return l; }; - + Q_INVOKABLE static bool Start( + const QString &vidPid, int channel,int channelWidth){ + return WFBReceiver::Start(vidPid.toStdString(),channel,channelWidth); + } }; diff --git a/src/WFBReceiver.cpp b/src/WFBReceiver.cpp index 2319fe7..dda9076 100644 --- a/src/WFBReceiver.cpp +++ b/src/WFBReceiver.cpp @@ -3,47 +3,117 @@ // #include "WFBReceiver.h" +#include "WiFiDriver.h" +#include "logger.h" #include -#include #include +libusb_context *WFBReceiver::ctx{}; +libusb_device_handle *WFBReceiver::dev_handle{}; +std::shared_ptr WFBReceiver::usbThread{}; + std::vector WFBReceiver::GetDongleList() { - std::vector list; + std::vector list; - libusb_context *ctx; - // Initialize libusb - libusb_init(&ctx); + libusb_context *findctx; + // Initialize libusb + libusb_init(&findctx); - // Get list of USB devices - libusb_device **devs; - ssize_t count = libusb_get_device_list(nullptr, &devs); - if (count < 0) { - return list; - } + // Get list of USB devices + libusb_device **devs; + ssize_t count = libusb_get_device_list(findctx, &devs); + if (count < 0) { + return list; + } - // Iterate over devices - for (ssize_t i = 0; i < count; ++i) { - libusb_device *dev = devs[i]; - struct libusb_device_descriptor desc; - if (libusb_get_device_descriptor(dev, &desc) == 0) { - // Check if the device is using libusb driver - if (desc.bDeviceClass == LIBUSB_CLASS_PER_INTERFACE) { - std::stringstream ss; - ss<> std::hex >> wifiDeviceVid >> c >> wifiDevicePid; - // Deinitialize libusb + auto logger = std::make_shared(); + + rc = libusb_init(&ctx); + if (rc < 0) { + return false; + } + dev_handle = + libusb_open_device_with_vid_pid(ctx, wifiDeviceVid, wifiDevicePid); + if (dev_handle == nullptr) { + logger->error("Cannot find device {:04x}:{:04x}", wifiDeviceVid, + wifiDevicePid); libusb_exit(ctx); - return list; + return false; + } + + /*Check if kenel driver attached*/ + if (libusb_kernel_driver_active(dev_handle, 0)) { + rc = libusb_detach_kernel_driver(dev_handle, 0); // detach driver + } + rc = libusb_claim_interface(dev_handle, 0); + + if (rc < 0) { + return false; + } + + usbThread = std::make_shared([=](){ + WiFiDriver wifi_driver{logger}; + auto rtlDevice = wifi_driver.CreateRtlDevice(dev_handle); + rtlDevice->Init( + [](const Packet & p) { + handle80211Frame(p); + }, + SelectedChannel{ + .Channel = channel, + .ChannelOffset = 0, + .ChannelWidth = static_cast(channelWidth), + }); + }); + usbThread->detach(); + + return true; } -void WFBReceiver::Start(const std::string &vidPid) { +void WFBReceiver::handle80211Frame(const Packet &pkt) { } +WFBReceiver::~WFBReceiver() { + auto rc = libusb_release_interface(dev_handle, 0); + if (rc < 0) { + // error + } + libusb_close(dev_handle); + libusb_exit(ctx); +} diff --git a/src/WFBReceiver.h b/src/WFBReceiver.h index f8ebee7..34f3f5d 100644 --- a/src/WFBReceiver.h +++ b/src/WFBReceiver.h @@ -4,16 +4,24 @@ #ifndef WFBRECEIVER_H #define WFBRECEIVER_H +#include "FrameParser.h" +#include #include #include - +#include class WFBReceiver { public: - static std::vector GetDongleList(); - void Start(const std::string& vidPid); + WFBReceiver() = default; + ~WFBReceiver(); + static std::vector GetDongleList(); + static bool Start(const std::string &vidPid, uint8_t channel, + int channelWidth); + static void handle80211Frame(const Packet &pkt); +protected: + static libusb_context *ctx; + static libusb_device_handle *dev_handle; + static std::shared_ptr usbThread; }; - - -#endif //WFBRECEIVER_H +#endif // WFBRECEIVER_H