text: /***************************************************************************
  **
  ** Copyright (C) 2017 The Qt Company Ltd.
  ** Contact: https://www.qt.io/licensing/
  **
  ** This file is part of the examples of the QtBluetooth module of the Qt Toolkit.
  **
  ** $QT_BEGIN_LICENSE:BSD$
  ** Commercial License Usage
  ** Licensees holding valid commercial Qt licenses may use this file in
  ** accordance with the commercial license agreement provided with the
  ** Software or, alternatively, in accordance with the terms contained in
  ** a written agreement between you and The Qt Company. For licensing terms
  ** and conditions see https://www.qt.io/terms-conditions. For further
  ** information use the contact form at https://www.qt.io/contact-us.
  **
  ** BSD License Usage
  ** Alternatively, you may use this file under the terms of the BSD license
  ** as follows:
  **
  ** "Redistribution and use in source and binary forms, with or without
  ** modification, are permitted provided that the following conditions are
  ** met:
  **   * Redistributions of source code must retain the above copyright
  **     notice, this list of conditions and the following disclaimer.
  **   * Redistributions in binary form must reproduce the above copyright
  **     notice, this list of conditions and the following disclaimer in
  **     the documentation and/or other materials provided with the
  **     distribution.
  **   * Neither the name of The Qt Company Ltd nor the names of its
  **     contributors may be used to endorse or promote products derived
  **     from this software without specific prior written permission.
  **
  **
  ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
  **
  ** $QT_END_LICENSE$
  **
  ****************************************************************************/
  
  #include "globals.h"
  #include "devicehandler.h"
  #include "deviceinfo.h"
  #include <QtEndian>
  #include <QRandomGenerator>
  
  DeviceHandler::DeviceHandler(QObject *parent) :
      BluetoothBaseClass(parent),
      found_service_(false)
  {}
  
  void DeviceHandler::setAddressType(AddressType type)
  {
      switch (type) {
      case DeviceHandler::AddressType::PublicAddress:
          m_addressType = QLowEnergyController::PublicAddress;
          break;
      case DeviceHandler::AddressType::RandomAddress:
          m_addressType = QLowEnergyController::RandomAddress;
          break;
      }
  }
  
  DeviceHandler::AddressType DeviceHandler::addressType() const
  {
      if (m_addressType == QLowEnergyController::RandomAddress)
          return DeviceHandler::AddressType::RandomAddress;
  
      return DeviceHandler::AddressType::PublicAddress;
  }
  
  void DeviceHandler::setDevice(DeviceInfo *device)
  {
      clearMessages();
      m_currentDevice = device;
  
      // Disconnect and delete old connection
      if (m_control) {
          m_control->disconnectFromDevice();
          delete m_control;
          m_control = nullptr;
      }
  
      setInfo("Connecting to device...");
  
      // Create new controller and connect it if device available
      if (m_currentDevice) {
          // Make connections
          m_control = QLowEnergyController::createCentral(m_currentDevice->getDevice(), this);
          m_control->setRemoteAddressType(m_addressType);
          connect(m_control, &QLowEnergyController::serviceDiscovered,
                  this, &DeviceHandler::serviceDiscovered);
          connect(m_control, &QLowEnergyController::discoveryFinished,
                  this, &DeviceHandler::serviceScanDone);
  
          connect(m_control, &QLowEnergyController::errorOccurred, this,
                  [this](QLowEnergyController::Error error) {
                      Q_UNUSED(error);
                      setError("Cannot connect to remote device.");
                  });
          connect(m_control, &QLowEnergyController::connected, this, [this]() {
              setInfo("Controller connected. Search services...");
              m_control->discoverServices();
          });
          connect(m_control, &QLowEnergyController::disconnected, this, [this]() {
              setError("BLE controller disconnected");
          });
  
          // Connect
          m_control->connectToDevice();
      }
  }
  
  void DeviceHandler::serviceDiscovered(const QBluetoothUuid &gatt)
  {
      if (gatt == QBluetoothUuid(kServiceUuid)) {
          setInfo("Service discovered. Waiting for service scan to be done...");
          found_service_ = true;
      }
  }
  
  void DeviceHandler::serviceScanDone()
  {
      setInfo("Service scan done.");
  
      // Delete old service if available
      if (m_service) {
          delete m_service;
          m_service = nullptr;
      }
  
      if (found_service_)
          m_service = m_control->createServiceObject(QBluetoothUuid(kServiceUuid), this);
  
      if (m_service) {
          connect(m_service, &QLowEnergyService::stateChanged, this, &DeviceHandler::serviceStateChanged);
  
          // Make a deferred call to discoverDetails().
          // This is a workaround for Windows: https://bugreports.qt.io/browse/QTBUG-78488
          QTimer::singleShot(0, this, [=] () {
              m_service->discoverDetails();
          });
      } else {
          setError("Service not found.");
      }
  }
  
  // Service functions
  void DeviceHandler::serviceStateChanged(QLowEnergyService::ServiceState s)
  {
      switch (s) {
      case QLowEnergyService::RemoteServiceDiscovering:
          setInfo(tr("Discovering services..."));
          break;
      case QLowEnergyService::RemoteServiceDiscovered:
      {
          setInfo(tr("Service discovered."));
          run_char_ = m_service->characteristic(QBluetoothUuid(kStartStopCharUuid));
          if (!run_char_.isValid()) {
              setError("Run characteristic not found.");
              break;
          }
          setInfo(tr("Ready"));
          break;
      }
      case QLowEnergyService::InvalidService:
          setError(tr("Invalid service"));
          break;
      case QLowEnergyService::LocalService:
          setError(tr("Local service"));
          break;
      default:
          //nothing for now
          break;
      }
  
      emit aliveChanged();
  }
  
  void DeviceHandler::disconnectService()
  {
      found_service_ = false;
  
      if (m_control)
          m_control->disconnectFromDevice();
  
      delete m_service;
      m_service = nullptr;
  }
  
  void DeviceHandler::start() {
      if (m_service) {
          m_service->writeCharacteristic(run_char_, QByteArray::fromHex("01"));
      }
  }
  
  void DeviceHandler::stop() {
      if (m_service) {
          m_service->writeCharacteristic(run_char_, QByteArray::fromHex("00"));
      }
  }
  
  bool DeviceHandler::alive() const
  {
      if (m_service)
          return m_service->state() == QLowEnergyService::RemoteServiceDiscovered;
  
      return false;
  }
  