diff --git a/CMakeLists.txt b/CMakeLists.txt index 773c143..7a530db 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -62,10 +62,13 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR} link_directories(${CMAKE_LIBRARY_OUTPUT_DIRECTORY}) -set(autoapp_sources_directory ${sources_directory}/autoapp) -file(GLOB_RECURSE source_files ${autoapp_sources_directory}/*.ui ${autoapp_sources_directory}/*.cpp ${include_directory}/*.hpp ${resources_directory}/*.qrc) +set(common_include_directory ${include_directory}/f1x/openauto/Common) -add_executable(autoapp ${source_files}) +set(autoapp_sources_directory ${sources_directory}/autoapp) +set(autoapp_include_directory ${include_directory}/f1x/openauto/autoapp) +file(GLOB_RECURSE autoapp_source_files ${autoapp_sources_directory}/*.ui ${autoapp_sources_directory}/*.cpp ${autoapp_include_directory}/*.hpp ${common_include_directory}/*.hpp ${resources_directory}/*.qrc) + +add_executable(autoapp ${autoapp_source_files}) target_link_libraries(autoapp ${Boost_LIBRARIES} @@ -79,3 +82,16 @@ target_link_libraries(autoapp ${WINSOCK2_LIBRARIES} ${AASDK_PROTO_LIBRARIES} ${AASDK_LIBRARIES}) + +set(btservice_sources_directory ${sources_directory}/btservice) +set(btservice_include_directory ${include_directory}/f1x/openauto/btservice) +file(GLOB_RECURSE btservice_source_files ${btservice_sources_directory}/*.cpp ${btservice_include_directory}/*.hpp ${common_include_directory}/*.hpp) + +add_executable(btservice ${btservice_source_files}) + +target_link_libraries(btservice + ${Boost_LIBRARIES} + ${Qt5Bluetooth_LIBRARIES} + ${Qt5MultimediaWidgets_LIBRARIES} + ${PROTOBUF_LIBRARIES} + ${AASDK_PROTO_LIBRARIES}) diff --git a/include/f1x/openauto/Common/Log.hpp b/include/f1x/openauto/Common/Log.hpp index f0f544a..4215961 100644 --- a/include/f1x/openauto/Common/Log.hpp +++ b/include/f1x/openauto/Common/Log.hpp @@ -20,4 +20,4 @@ #include -#define OPENAUTO_LOG(severity) BOOST_LOG_TRIVIAL(severity) << "[AaApp] " +#define OPENAUTO_LOG(severity) BOOST_LOG_TRIVIAL(severity) << "[OpenAuto] " diff --git a/include/f1x/openauto/autoapp/USB/USBApp.hpp b/include/f1x/openauto/autoapp/App.hpp similarity index 77% rename from include/f1x/openauto/autoapp/USB/USBApp.hpp rename to include/f1x/openauto/autoapp/App.hpp index 6b0a81b..f31e2d5 100644 --- a/include/f1x/openauto/autoapp/USB/USBApp.hpp +++ b/include/f1x/openauto/autoapp/App.hpp @@ -29,23 +29,21 @@ namespace openauto { namespace autoapp { -namespace usb -{ -class USBApp: public projection::IAndroidAutoEntityEventHandler, public std::enable_shared_from_this +class App: public projection::IAndroidAutoEntityEventHandler, public std::enable_shared_from_this { public: - typedef std::shared_ptr Pointer; + typedef std::shared_ptr Pointer; - USBApp(boost::asio::io_service& ioService, projection::IAndroidAutoEntityFactory& androidAutoEntityFactory, - aasdk::usb::IUSBHub::Pointer usbHub, aasdk::usb::IConnectedAccessoriesEnumerator::Pointer connectedAccessoriesEnumerator); + App(boost::asio::io_service& ioService, projection::IAndroidAutoEntityFactory& androidAutoEntityFactory, + aasdk::usb::IUSBHub::Pointer usbHub, aasdk::usb::IConnectedAccessoriesEnumerator::Pointer connectedAccessoriesEnumerator); void start(); void stop(); void onAndroidAutoQuit() override; private: - using std::enable_shared_from_this::shared_from_this; + using std::enable_shared_from_this::shared_from_this; void enumerateDevices(); void waitForDevice(); @@ -64,4 +62,3 @@ private: } } } -} diff --git a/include/f1x/openauto/autoapp/USB/USBMain.hpp b/include/f1x/openauto/autoapp/Main.hpp similarity index 73% rename from include/f1x/openauto/autoapp/USB/USBMain.hpp rename to include/f1x/openauto/autoapp/Main.hpp index 481d84b..4cb6746 100644 --- a/include/f1x/openauto/autoapp/USB/USBMain.hpp +++ b/include/f1x/openauto/autoapp/Main.hpp @@ -18,12 +18,11 @@ #pragma once -#include #include #include #include #include -#include +#include #include #include #include @@ -34,35 +33,25 @@ namespace openauto { namespace autoapp { -namespace usb -{ -class USBMain +class Main { public: - USBMain(libusb_context* context); + Main(aasdk::usb::IUSBWrapper& usbWrapper, boost::asio::io_service& ioService, configuration::IConfiguration::Pointer configuration); - int exec(int argc, char* argv[]); + void start(); + void stop(); private: - typedef std::vector ThreadPool; - - void startUSBWorkers(); - void startIOServiceWorkers(); - - libusb_context* usbContext_; - aasdk::usb::USBWrapper usbWrapper_; - boost::asio::io_service ioService_; + aasdk::usb::IUSBWrapper& usbWrapper_; + boost::asio::io_service& ioService_; aasdk::usb::AccessoryModeQueryFactory queryFactory_; aasdk::usb::AccessoryModeQueryChainFactory queryChainFactory_; - configuration::IConfiguration::Pointer configuration_; projection::ServiceFactory serviceFactory_; projection::AndroidAutoEntityFactory androidAutoEntityFactory_; - autoapp::usb::USBApp::Pointer usbApp_; - ThreadPool threadPool_; + autoapp::App::Pointer app_; }; } } } -} diff --git a/include/f1x/openauto/autoapp/Projection/AndroidAutoEntityFactory.hpp b/include/f1x/openauto/autoapp/Projection/AndroidAutoEntityFactory.hpp index aa3017c..1df64dc 100644 --- a/include/f1x/openauto/autoapp/Projection/AndroidAutoEntityFactory.hpp +++ b/include/f1x/openauto/autoapp/Projection/AndroidAutoEntityFactory.hpp @@ -19,6 +19,7 @@ #pragma once #include +#include #include #include #include @@ -41,8 +42,11 @@ public: IServiceFactory& serviceFactory); IAndroidAutoEntity::Pointer create(aasdk::usb::DeviceHandle deviceHandle) override; + IAndroidAutoEntity::Pointer create(aasdk::tcp::ITCPEndpoint::Pointer tcpEndpoint) override; private: + IAndroidAutoEntity::Pointer create(aasdk::transport::ITransport::Pointer transport); + aasdk::usb::IUSBWrapper& usbWrapper_; boost::asio::io_service& ioService_; configuration::IConfiguration::Pointer configuration_; diff --git a/include/f1x/openauto/autoapp/Projection/IAndroidAutoEntityFactory.hpp b/include/f1x/openauto/autoapp/Projection/IAndroidAutoEntityFactory.hpp index ab51c4f..945c247 100644 --- a/include/f1x/openauto/autoapp/Projection/IAndroidAutoEntityFactory.hpp +++ b/include/f1x/openauto/autoapp/Projection/IAndroidAutoEntityFactory.hpp @@ -18,6 +18,7 @@ #pragma once +#include #include #include @@ -36,6 +37,7 @@ public: virtual ~IAndroidAutoEntityFactory() = default; virtual IAndroidAutoEntity::Pointer create(aasdk::usb::DeviceHandle deviceHandle) = 0; + virtual IAndroidAutoEntity::Pointer create(aasdk::tcp::ITCPEndpoint::Pointer tcpEndpoint) = 0; }; } diff --git a/include/f1x/openauto/btservice/AndroidBluetoothServer.hpp b/include/f1x/openauto/btservice/AndroidBluetoothServer.hpp new file mode 100644 index 0000000..4e159bc --- /dev/null +++ b/include/f1x/openauto/btservice/AndroidBluetoothServer.hpp @@ -0,0 +1,51 @@ +/* +* This file is part of openauto project. +* Copyright (C) 2018 f1x.studio (Michal Szwaj) +* +* openauto is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 3 of the License, or +* (at your option) any later version. + +* openauto is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with openauto. If not, see . +*/ + +#pragma once + +#include +#include +#include +#include + +namespace f1x +{ +namespace openauto +{ +namespace btservice +{ + +class AndroidBluetoothServer: public QObject, public IAndroidBluetoothServer +{ + Q_OBJECT + +public: + AndroidBluetoothServer(); + + bool start(const QBluetoothAddress& address, uint16_t portNumber) override; + +private slots: + void onClientConnected(); + +private: + std::unique_ptr rfcommServer_; +}; + +} +} +} diff --git a/include/f1x/openauto/btservice/AndroidBluetoothService.hpp b/include/f1x/openauto/btservice/AndroidBluetoothService.hpp new file mode 100644 index 0000000..ebbb860 --- /dev/null +++ b/include/f1x/openauto/btservice/AndroidBluetoothService.hpp @@ -0,0 +1,45 @@ +/* +* This file is part of openauto project. +* Copyright (C) 2018 f1x.studio (Michal Szwaj) +* +* openauto is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 3 of the License, or +* (at your option) any later version. + +* openauto is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with openauto. If not, see . +*/ + +#pragma once + +#include +#include + +namespace f1x +{ +namespace openauto +{ +namespace btservice +{ + +class AndroidBluetoothService: public IAndroidBluetoothService +{ +public: + AndroidBluetoothService(uint16_t portNumber); + + bool registerService(const QBluetoothAddress& bluetoothAddress) override; + bool unregisterService() override; + +private: + QBluetoothServiceInfo serviceInfo_; +}; + +} +} +} diff --git a/include/f1x/openauto/btservice/IAndroidBluetoothServer.hpp b/include/f1x/openauto/btservice/IAndroidBluetoothServer.hpp new file mode 100644 index 0000000..fb109ed --- /dev/null +++ b/include/f1x/openauto/btservice/IAndroidBluetoothServer.hpp @@ -0,0 +1,40 @@ +/* +* This file is part of openauto project. +* Copyright (C) 2018 f1x.studio (Michal Szwaj) +* +* openauto is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 3 of the License, or +* (at your option) any later version. + +* openauto is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with openauto. If not, see . +*/ + +#pragma once + +#include + +namespace f1x +{ +namespace openauto +{ +namespace btservice +{ + +class IAndroidBluetoothServer +{ +public: + virtual ~IAndroidBluetoothServer() = default; + + virtual bool start(const QBluetoothAddress& address, uint16_t portNumber) = 0; +}; + +} +} +} diff --git a/include/f1x/openauto/btservice/IAndroidBluetoothService.hpp b/include/f1x/openauto/btservice/IAndroidBluetoothService.hpp new file mode 100644 index 0000000..71352c9 --- /dev/null +++ b/include/f1x/openauto/btservice/IAndroidBluetoothService.hpp @@ -0,0 +1,41 @@ +/* +* This file is part of openauto project. +* Copyright (C) 2018 f1x.studio (Michal Szwaj) +* +* openauto is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 3 of the License, or +* (at your option) any later version. + +* openauto is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with openauto. If not, see . +*/ + +#pragma once + +#include + +namespace f1x +{ +namespace openauto +{ +namespace btservice +{ + +class IAndroidBluetoothService +{ +public: + virtual ~IAndroidBluetoothService() = default; + + virtual bool registerService(const QBluetoothAddress& bluetoothAddress) = 0; + virtual bool unregisterService() = 0; +}; + +} +} +} diff --git a/src/autoapp/USB/USBApp.cpp b/src/autoapp/App.cpp similarity index 66% rename from src/autoapp/USB/USBApp.cpp rename to src/autoapp/App.cpp index fe4064b..73a7b3d 100644 --- a/src/autoapp/USB/USBApp.cpp +++ b/src/autoapp/App.cpp @@ -17,7 +17,7 @@ */ #include -#include +#include #include namespace f1x @@ -26,11 +26,9 @@ namespace openauto { namespace autoapp { -namespace usb -{ -USBApp::USBApp(boost::asio::io_service& ioService, projection::IAndroidAutoEntityFactory& androidAutoEntityFactory, - aasdk::usb::IUSBHub::Pointer usbHub, aasdk::usb::IConnectedAccessoriesEnumerator::Pointer connectedAccessoriesEnumerator) +App::App(boost::asio::io_service& ioService, projection::IAndroidAutoEntityFactory& androidAutoEntityFactory, + aasdk::usb::IUSBHub::Pointer usbHub, aasdk::usb::IConnectedAccessoriesEnumerator::Pointer connectedAccessoriesEnumerator) : ioService_(ioService) , strand_(ioService_) , androidAutoEntityFactory_(androidAutoEntityFactory) @@ -41,7 +39,7 @@ USBApp::USBApp(boost::asio::io_service& ioService, projection::IAndroidAutoEntit } -void USBApp::start() +void App::start() { strand_.dispatch([this, self = this->shared_from_this()]() { this->waitForDevice(); @@ -49,7 +47,7 @@ void USBApp::start() }); } -void USBApp::stop() +void App::stop() { strand_.dispatch([this, self = this->shared_from_this()]() { isStopped_ = true; @@ -63,9 +61,9 @@ void USBApp::stop() }); } -void USBApp::aoapDeviceHandler(aasdk::usb::DeviceHandle deviceHandle) +void App::aoapDeviceHandler(aasdk::usb::DeviceHandle deviceHandle) { - OPENAUTO_LOG(info) << "[USBApp] Device connected."; + OPENAUTO_LOG(info) << "[App] Device connected."; if(androidAutoEntity_ == nullptr) { @@ -76,7 +74,7 @@ void USBApp::aoapDeviceHandler(aasdk::usb::DeviceHandle deviceHandle) } catch(const aasdk::error::Error& error) { - OPENAUTO_LOG(error) << "[USBApp] AndroidAutoEntity create error: " << error.what(); + OPENAUTO_LOG(error) << "[App] AndroidAutoEntity create error: " << error.what(); androidAutoEntity_.reset(); this->waitForDevice(); @@ -84,37 +82,37 @@ void USBApp::aoapDeviceHandler(aasdk::usb::DeviceHandle deviceHandle) } else { - OPENAUTO_LOG(warning) << "[USBApp] android auto entity is still running."; + OPENAUTO_LOG(warning) << "[App] android auto entity is still running."; } } -void USBApp::enumerateDevices() +void App::enumerateDevices() { auto promise = aasdk::usb::IConnectedAccessoriesEnumerator::Promise::defer(strand_); promise->then([this, self = this->shared_from_this()](auto result) { - OPENAUTO_LOG(info) << "[USBApp] Devices enumeration result: " << result; + OPENAUTO_LOG(info) << "[App] Devices enumeration result: " << result; }, [this, self = this->shared_from_this()](auto e) { - OPENAUTO_LOG(error) << "[USBApp] Devices enumeration failed: " << e.what(); + OPENAUTO_LOG(error) << "[App] Devices enumeration failed: " << e.what(); }); connectedAccessoriesEnumerator_->enumerate(std::move(promise)); } -void USBApp::waitForDevice() +void App::waitForDevice() { - OPENAUTO_LOG(info) << "[USBApp] Waiting for device..."; + OPENAUTO_LOG(info) << "[App] Waiting for device..."; auto promise = aasdk::usb::IUSBHub::Promise::defer(strand_); - promise->then(std::bind(&USBApp::aoapDeviceHandler, this->shared_from_this(), std::placeholders::_1), - std::bind(&USBApp::onUSBHubError, this->shared_from_this(), std::placeholders::_1)); + promise->then(std::bind(&App::aoapDeviceHandler, this->shared_from_this(), std::placeholders::_1), + std::bind(&App::onUSBHubError, this->shared_from_this(), std::placeholders::_1)); usbHub_->start(std::move(promise)); } -void USBApp::onAndroidAutoQuit() +void App::onAndroidAutoQuit() { strand_.dispatch([this, self = this->shared_from_this()]() { - OPENAUTO_LOG(info) << "[USBApp] quit."; + OPENAUTO_LOG(info) << "[App] quit."; androidAutoEntity_->stop(); androidAutoEntity_.reset(); @@ -126,9 +124,9 @@ void USBApp::onAndroidAutoQuit() }); } -void USBApp::onUSBHubError(const aasdk::error::Error& error) +void App::onUSBHubError(const aasdk::error::Error& error) { - OPENAUTO_LOG(error) << "[USBApp] usb hub error: " << error.what(); + OPENAUTO_LOG(error) << "[App] usb hub error: " << error.what(); if(error.getCode() == aasdk::error::ErrorCode::OPERATION_ABORTED || error.getCode() == aasdk::error::ErrorCode::OPERATION_IN_PROGRESS) @@ -140,4 +138,3 @@ void USBApp::onUSBHubError(const aasdk::error::Error& error) } } } -} diff --git a/src/autoapp/Main.cpp b/src/autoapp/Main.cpp index 0f7783a..84ea91e 100644 --- a/src/autoapp/Main.cpp +++ b/src/autoapp/Main.cpp @@ -16,24 +16,43 @@ * along with openauto. If not, see . */ -#include -#include +#include +#include +#include +#include -namespace aasdk = f1x::aasdk; -namespace autoapp = f1x::openauto::autoapp; - -int main(int argc, char* argv[]) +namespace f1x +{ +namespace openauto +{ +namespace autoapp { - libusb_context* context; - if(libusb_init(&context) != 0) - { - OPENAUTO_LOG(error) << "[OpenAuto] libusb init failed."; - return 1; - } - autoapp::usb::USBMain main(context); - auto result = main.exec(argc, argv); +Main::Main(aasdk::usb::IUSBWrapper& usbWrapper, boost::asio::io_service& ioService, configuration::IConfiguration::Pointer configuration) + : usbWrapper_(usbWrapper) + , ioService_(ioService) + , queryFactory_(usbWrapper_, ioService_) + , queryChainFactory_(usbWrapper_, ioService_, queryFactory_) + , serviceFactory_(ioService_, configuration) + , androidAutoEntityFactory_(usbWrapper_, ioService_, configuration, serviceFactory_) +{ + auto usbHub(std::make_shared(usbWrapper_, ioService_, queryChainFactory_)); + auto ConnectedAccessoriesEnumerator(std::make_shared(usbWrapper_, ioService_, queryChainFactory_)); - libusb_exit(context); - return result; + app_ = std::make_shared(ioService_, androidAutoEntityFactory_, + std::move(usbHub), std::move(ConnectedAccessoriesEnumerator)); +} + +void Main::start() +{ + app_->start(); +} + +void Main::stop() +{ + app_->stop(); +} + +} +} } diff --git a/src/autoapp/Projection/AndroidAutoEntityFactory.cpp b/src/autoapp/Projection/AndroidAutoEntityFactory.cpp index 612b818..cc31fae 100644 --- a/src/autoapp/Projection/AndroidAutoEntityFactory.cpp +++ b/src/autoapp/Projection/AndroidAutoEntityFactory.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -49,6 +50,17 @@ IAndroidAutoEntity::Pointer AndroidAutoEntityFactory::create(aasdk::usb::DeviceH auto aoapDevice(aasdk::usb::AOAPDevice::create(usbWrapper_, ioService_, deviceHandle)); auto transport(std::make_shared(ioService_, aoapDevice)); + return create(std::move(transport)); +} + +IAndroidAutoEntity::Pointer AndroidAutoEntityFactory::create(aasdk::tcp::ITCPEndpoint::Pointer tcpEndpoint) +{ + auto transport(std::make_shared(ioService_, std::move(tcpEndpoint))); + return create(std::move(transport)); +} + +IAndroidAutoEntity::Pointer AndroidAutoEntityFactory::create(aasdk::transport::ITransport::Pointer transport) +{ auto sslWrapper(std::make_shared()); auto cryptor(std::make_shared(std::move(sslWrapper))); diff --git a/src/autoapp/USB/USBMain.cpp b/src/autoapp/USB/USBMain.cpp deleted file mode 100644 index 6c3602f..0000000 --- a/src/autoapp/USB/USBMain.cpp +++ /dev/null @@ -1,118 +0,0 @@ -/* -* This file is part of openauto project. -* Copyright (C) 2018 f1x.studio (Michal Szwaj) -* -* openauto is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation; either version 3 of the License, or -* (at your option) any later version. - -* openauto is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with openauto. If not, see . -*/ - -#include -#include -#include -#include -#include -#include -#include -#include - -namespace f1x -{ -namespace openauto -{ -namespace autoapp -{ -namespace usb -{ - -USBMain::USBMain(libusb_context* context) - : usbContext_(context) - , usbWrapper_(usbContext_) - , queryFactory_(usbWrapper_, ioService_) - , queryChainFactory_(usbWrapper_, ioService_, queryFactory_) - , configuration_(std::make_shared()) - , serviceFactory_(ioService_, configuration_) - , androidAutoEntityFactory_(usbWrapper_, ioService_, configuration_, serviceFactory_) -{ - auto usbHub(std::make_shared(usbWrapper_, ioService_, queryChainFactory_)); - auto ConnectedAccessoriesEnumerator(std::make_shared(usbWrapper_, ioService_, queryChainFactory_)); - - usbApp_ = std::make_shared(ioService_, androidAutoEntityFactory_, - std::move(usbHub), std::move(ConnectedAccessoriesEnumerator)); -} - -int USBMain::exec(int argc, char* argv[]) -{ - QApplication qApplication(argc, argv); - - ui::MainWindow mainWindow; - mainWindow.setWindowFlags(Qt::WindowStaysOnTopHint); - - ui::SettingsWindow settingsWindow(configuration_); - settingsWindow.setWindowFlags(Qt::WindowStaysOnTopHint); - - QObject::connect(&mainWindow, &ui::MainWindow::exit, []() { std::exit(0); }); - QObject::connect(&mainWindow, &ui::MainWindow::openSettings, &settingsWindow, &ui::SettingsWindow::showFullScreen); - - qApplication.setOverrideCursor(Qt::BlankCursor); - bool cursorVisible = false; - QObject::connect(&mainWindow, &ui::MainWindow::toggleCursor, [&cursorVisible, &qApplication]() { - cursorVisible = !cursorVisible; - qApplication.setOverrideCursor(cursorVisible ? Qt::ArrowCursor : Qt::BlankCursor); - }); - - mainWindow.showFullScreen(); - - boost::asio::io_service::work work(ioService_); - this->startIOServiceWorkers(); - this->startUSBWorkers(); - usbApp_->start(); - - auto result = qApplication.exec(); - std::for_each(threadPool_.begin(), threadPool_.end(), std::bind(&std::thread::join, std::placeholders::_1)); - return result; -} - -void USBMain::startUSBWorkers() -{ - auto usbWorker = [this]() { - timeval libusbEventTimeout{180, 0}; - - while(!ioService_.stopped()) - { - libusb_handle_events_timeout_completed(usbContext_, &libusbEventTimeout, nullptr); - } - }; - threadPool_.emplace_back(usbWorker); - threadPool_.emplace_back(usbWorker); - threadPool_.emplace_back(usbWorker); - threadPool_.emplace_back(usbWorker); -} - -void USBMain::startIOServiceWorkers() -{ - auto ioServiceWorker = [this]() { - while(!ioService_.stopped()) - { - ioService_.run(); - } - }; - threadPool_.emplace_back(ioServiceWorker); - threadPool_.emplace_back(ioServiceWorker); - threadPool_.emplace_back(ioServiceWorker); - threadPool_.emplace_back(ioServiceWorker); -} - -} -} -} -} diff --git a/src/autoapp/autoapp.cpp b/src/autoapp/autoapp.cpp new file mode 100644 index 0000000..4b4b4b5 --- /dev/null +++ b/src/autoapp/autoapp.cpp @@ -0,0 +1,104 @@ +/* +* This file is part of openauto project. +* Copyright (C) 2018 f1x.studio (Michal Szwaj) +* +* openauto is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 3 of the License, or +* (at your option) any later version. + +* openauto is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with openauto. If not, see . +*/ + +#include +#include +#include +#include +#include +#include +#include + +namespace aasdk = f1x::aasdk; +namespace autoapp = f1x::openauto::autoapp; +using ThreadPool = std::vector; + +void startUSBWorkers(boost::asio::io_service& ioService, libusb_context* usbContext, ThreadPool& threadPool) +{ + auto usbWorker = [&ioService, usbContext]() { + timeval libusbEventTimeout{180, 0}; + + while(!ioService.stopped()) + { + libusb_handle_events_timeout_completed(usbContext, &libusbEventTimeout, nullptr); + } + }; + + threadPool.emplace_back(usbWorker); + threadPool.emplace_back(usbWorker); + threadPool.emplace_back(usbWorker); + threadPool.emplace_back(usbWorker); +} + +void startIOServiceWorkers(boost::asio::io_service& ioService, ThreadPool& threadPool) +{ + auto ioServiceWorker = [&ioService]() { + ioService.run(); + }; + + threadPool.emplace_back(ioServiceWorker); + threadPool.emplace_back(ioServiceWorker); + threadPool.emplace_back(ioServiceWorker); + threadPool.emplace_back(ioServiceWorker); +} + +int main(int argc, char* argv[]) +{ + libusb_context* usbContext; + if(libusb_init(&usbContext) != 0) + { + OPENAUTO_LOG(error) << "[OpenAuto] libusb init failed."; + return 1; + } + + QApplication qApplication(argc, argv); + boost::asio::io_service ioService; + boost::asio::io_service::work work(ioService); + + autoapp::ui::MainWindow mainWindow; + mainWindow.setWindowFlags(Qt::WindowStaysOnTopHint); + mainWindow.showFullScreen(); + + auto configuration = std::make_shared(); + autoapp::ui::SettingsWindow settingsWindow(configuration); + settingsWindow.setWindowFlags(Qt::WindowStaysOnTopHint); + + qApplication.setOverrideCursor(Qt::BlankCursor); + bool cursorVisible = false; + QObject::connect(&mainWindow, &autoapp::ui::MainWindow::toggleCursor, [&cursorVisible, &qApplication]() { + cursorVisible = !cursorVisible; + qApplication.setOverrideCursor(cursorVisible ? Qt::ArrowCursor : Qt::BlankCursor); + }); + + aasdk::usb::USBWrapper usbWrapper(usbContext); + autoapp::Main main(usbWrapper, ioService, configuration); + + QObject::connect(&mainWindow, &autoapp::ui::MainWindow::exit, []() { std::exit(0); }); + QObject::connect(&mainWindow, &autoapp::ui::MainWindow::openSettings, &settingsWindow, &autoapp::ui::SettingsWindow::showFullScreen); + + std::vector threadPool; + startUSBWorkers(ioService, usbContext, threadPool); + startIOServiceWorkers(ioService, threadPool); + main.start(); + + auto result = qApplication.exec(); + std::for_each(threadPool.begin(), threadPool.end(), std::bind(&std::thread::join, std::placeholders::_1)); + + libusb_exit(usbContext); + return result; +} diff --git a/src/btservice/AndroidBluetoothServer.cpp b/src/btservice/AndroidBluetoothServer.cpp new file mode 100644 index 0000000..c827e2b --- /dev/null +++ b/src/btservice/AndroidBluetoothServer.cpp @@ -0,0 +1,56 @@ +/* +* This file is part of openauto project. +* Copyright (C) 2018 f1x.studio (Michal Szwaj) +* +* openauto is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 3 of the License, or +* (at your option) any later version. + +* openauto is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with openauto. If not, see . +*/ + +#include +#include + +namespace f1x +{ +namespace openauto +{ +namespace btservice +{ + +AndroidBluetoothServer::AndroidBluetoothServer() + : rfcommServer_(std::make_unique(QBluetoothServiceInfo::RfcommProtocol, this)) +{ + connect(rfcommServer_.get(), &QBluetoothServer::newConnection, this, &AndroidBluetoothServer::onClientConnected); +} + +bool AndroidBluetoothServer::start(const QBluetoothAddress& address, uint16_t portNumber) +{ + return rfcommServer_->listen(address, portNumber); +} + +void AndroidBluetoothServer::onClientConnected() +{ + auto socket = rfcommServer_->nextPendingConnection(); + + if(socket != nullptr) + { + OPENAUTO_LOG(info) << "[AndroidBluetoothServer] rfcomm client connected, peer name: " << socket->peerName().toStdString(); + } + else + { + OPENAUTO_LOG(error) << "[AndroidBluetoothServer] received null socket during client connection."; + } +} + +} +} +} diff --git a/src/btservice/AndroidBluetoothService.cpp b/src/btservice/AndroidBluetoothService.cpp new file mode 100644 index 0000000..f178662 --- /dev/null +++ b/src/btservice/AndroidBluetoothService.cpp @@ -0,0 +1,71 @@ +/* +* This file is part of openauto project. +* Copyright (C) 2018 f1x.studio (Michal Szwaj) +* +* openauto is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 3 of the License, or +* (at your option) any later version. + +* openauto is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with openauto. If not, see . +*/ + +#include + +namespace f1x +{ +namespace openauto +{ +namespace btservice +{ + +AndroidBluetoothService::AndroidBluetoothService(uint16_t portNumber) +{ + //"4de17a00-52cb-11e6-bdf4-0800200c9a66"; + //"669a0c20-0008-f4bd-e611-cb52007ae14d"; + const QBluetoothUuid serviceUuid(QLatin1String("4de17a00-52cb-11e6-bdf4-0800200c9a66")); + + QBluetoothServiceInfo::Sequence classId; + classId << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::SerialPort)); + serviceInfo_.setAttribute(QBluetoothServiceInfo::BluetoothProfileDescriptorList, classId); + classId.prepend(QVariant::fromValue(serviceUuid)); + serviceInfo_.setAttribute(QBluetoothServiceInfo::ServiceClassIds, classId); + serviceInfo_.setAttribute(QBluetoothServiceInfo::ServiceName, "OpenAuto Bluetooth Service"); + serviceInfo_.setAttribute(QBluetoothServiceInfo::ServiceDescription, "AndroidAuto WiFi projection automatic setup"); + serviceInfo_.setAttribute(QBluetoothServiceInfo::ServiceProvider, "f1xstudio.com"); + serviceInfo_.setServiceUuid(serviceUuid); + + QBluetoothServiceInfo::Sequence publicBrowse; + publicBrowse << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::PublicBrowseGroup)); + serviceInfo_.setAttribute(QBluetoothServiceInfo::BrowseGroupList, publicBrowse); + + QBluetoothServiceInfo::Sequence protocolDescriptorList; + QBluetoothServiceInfo::Sequence protocol; + protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::L2cap)); + protocolDescriptorList.append(QVariant::fromValue(protocol)); + protocol.clear(); + protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::Rfcomm)) + << QVariant::fromValue(quint16(portNumber)); + protocolDescriptorList.append(QVariant::fromValue(protocol)); + serviceInfo_.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList, protocolDescriptorList); +} + +bool AndroidBluetoothService::registerService(const QBluetoothAddress& bluetoothAddress) +{ + return serviceInfo_.registerService(bluetoothAddress); +} + +bool AndroidBluetoothService::unregisterService() +{ + return serviceInfo_.unregisterService(); +} + +} +} +} diff --git a/src/btservice/btservice.cpp b/src/btservice/btservice.cpp new file mode 100644 index 0000000..e759c81 --- /dev/null +++ b/src/btservice/btservice.cpp @@ -0,0 +1,58 @@ +/* +* This file is part of openauto project. +* Copyright (C) 2018 f1x.studio (Michal Szwaj) +* +* openauto is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 3 of the License, or +* (at your option) any later version. + +* openauto is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with openauto. If not, see . +*/ + +#include +#include +#include +#include + +namespace btservice = f1x::openauto::btservice; + +int main(int argc, char* argv[]) +{ + QApplication qApplication(argc, argv); + + const QBluetoothAddress address; + const uint16_t portNumber = 5000; + + btservice::AndroidBluetoothServer androidBluetoothServer; + if(!androidBluetoothServer.start(address, portNumber)) + { + OPENAUTO_LOG(error) << "[btservice] Server start failed."; + return 2; + } + + OPENAUTO_LOG(info) << "[btservice] Listening for connections, address: " << address.toString().toStdString() + << ", port: " << portNumber; + + btservice::AndroidBluetoothService androidBluetoothService(portNumber); + if(!androidBluetoothService.registerService(address)) + { + OPENAUTO_LOG(error) << "[btservice] Service registration failed."; + return 1; + } + else + { + OPENAUTO_LOG(info) << "[btservice] Service registered, port: " << portNumber; + } + + qApplication.exec(); + androidBluetoothService.unregisterService(); + + return 0; +}