#pragma once #include "UsbDeviceManager.h" #include "StartReceiveFlashDataCommand.h" #include "GetAddressCommand.h" #include "ByteArrayProvider.h" #include "UsbDataListener.h" #include namespace Incart::DeviceComplexCommands { class MicroMonStartReceiveFlashDataCommand final : public StartReceiveFlashDataCommand { private: const int m_transmitBlockSize = 10 * 1024; // 10Kb uint64_t m_minAddress; uint64_t m_currAddress; uint64_t m_writtenMaxAddress; Usb::UsbDeviceManager* const m_usbDeviceManager; Usb::UsbDataListener* const m_dataListener; public: MicroMonStartReceiveFlashDataCommand(uint32_t uid, Usb::UsbDeviceManager* const usbDeviceManager, Usb::UsbDataListener* const dataListener) : StartReceiveFlashDataCommand(uid) , m_usbDeviceManager(usbDeviceManager), m_dataListener(dataListener) { } public: std::shared_ptr execute() override { std::shared_ptr commandData = std::make_shared>(m_usbDeviceManager->getCommandQueue()->generateUid()); std::shared_ptr command = std::dynamic_pointer_cast(m_usbDeviceManager->getCommandSet()->createComplexCommand("GetMinAddress", commandData)); addExecutedInnerCommand(command); connect(command.get(), &GetAddressCommand::answerIsReady, this, &MicroMonStartReceiveFlashDataCommand::handleGetMinAddressIsReady); return executeCommand(command); } private slots: void handleGetMinAddressIsReady(Usb::ComplexCommand* command_, uint64_t address_, std::shared_ptr status_) { removeExecutedInnerCommand(command_); if (status_->status != Usb::DeviceCommand::EStatus::OK) { emit answerIsReady(this, status_); return; } m_minAddress = address_; m_currAddress = m_minAddress; std::shared_ptr commandData = std::make_shared>(m_usbDeviceManager->getCommandQueue()->generateUid()); std::shared_ptr command = std::dynamic_pointer_cast(m_usbDeviceManager->getCommandSet()->createComplexCommand("GetWrittenMaxAddress", commandData)); addExecutedInnerCommand(command); connect(command.get(), &GetAddressCommand::answerIsReady, this, &MicroMonStartReceiveFlashDataCommand::handleGetWrittenMaxAddressIsReady); std::shared_ptr statusInfo = executeCommand(command); if (statusInfo->status != Usb::DeviceCommand::EStatus::OK) { emit answerIsReady(this, statusInfo); return; } } void handleGetWrittenMaxAddressIsReady(Usb::ComplexCommand* command_, uint64_t address_, std::shared_ptr status_) { removeExecutedInnerCommand(command_); if (status_->status != Usb::DeviceCommand::EStatus::OK) { emit answerIsReady(this, status_); return; } m_writtenMaxAddress = address_; //m_flashDataTransmitter->startTransmit(m_minAddress, m_writtenMaxAddress, m_transmitBlockSize); if (m_minAddress < m_writtenMaxAddress) { stopReceiveData(); } else { //std::cout << __PRETTY_FUNCTION__ << " m_startAddress: " << m_startAddress << " m_finishAddress: " << m_finishAddress << std::endl; emit answerIsReady(this, std::make_shared(Usb::DeviceCommand::EStatus::OK)); emit m_dataListener->signalAnswer({}, true); } } void handleStopDataReadingIsReady(std::shared_ptr /*command*/, std::vector /*answer*/, int32_t status) { std::cout << __PRETTY_FUNCTION__ << std::endl; if (status != Usb::DeviceCommand::EStatus::OK) { std::cout << __PRETTY_FUNCTION__ << " Error: " << status << std::endl; m_dataListener->stopReceiveData(); emit answerIsReady(this, Usb::DeviceCommandStatusCreator::create({}, status)); } else { startReceiveData(); } } void handleStartDataReadingIsReady(std::shared_ptr /*command*/, std::vector /*answer*/, int32_t status) { if (status != Usb::DeviceCommand::EStatus::OK) { std::cout << __PRETTY_FUNCTION__ << " Error: " << status << std::endl; m_dataListener->stopReceiveData(); emit answerIsReady(this, Usb::DeviceCommandStatusCreator::create({}, status)); return; } emit answerIsReady(this, std::make_shared(Usb::DeviceCommand::EStatus::OK)); } private: void stopReceiveData() { std::vector commandBytes; Usb::ByteCommandQueue* commandQueue = m_usbDeviceManager->getCommandQueue(); std::shared_ptr commandInfo = std::make_shared("StopDataReading", commandQueue->generateUid(), std::vector()); Usb::DeviceCommand::EStatus status = m_usbDeviceManager->getCommandSet()->createCommandBytes(commandInfo, commandBytes); if (status != Usb::DeviceCommand::EStatus::OK) { std::cout << __PRETTY_FUNCTION__ << " Error: " << status << std::endl; emit answerIsReady(this, Usb::DeviceCommandStatusCreator::create({}, status)); return; } std::cout << __PRETTY_FUNCTION__ << std::endl; std::shared_ptr command = std::make_shared(commandInfo->name, commandInfo->uid, std::move(commandBytes)); connect(command.get(), &Usb::ByteCommand::answerIsReady, this, &MicroMonStartReceiveFlashDataCommand::handleStopDataReadingIsReady); commandQueue->enqueue(command); } void startReceiveData() { std::cout << __PRETTY_FUNCTION__ << std::endl; //std::cout << __PRETTY_FUNCTION__ << " startTransmit m_startAddress=" << m_startAddress << " m_finishAddress=" << m_finishAddress << std::endl; m_dataListener->startReceiveData(m_minAddress, m_writtenMaxAddress, m_transmitBlockSize); std::vector commandDataBytes; commandDataBytes.push_back(0x12); Common::ByteArrayProvider::addQWord(m_minAddress, commandDataBytes); Common::ByteArrayProvider::addWord(0x80, commandDataBytes); // add pack size (not used for 7 series) std::vector commandBytes; Usb::ByteCommandQueue* commandQueue = m_usbDeviceManager->getCommandQueue(); std::shared_ptr commandInfo = std::make_shared("StartDataReading", commandQueue->generateUid(), commandDataBytes); Usb::DeviceCommand::EStatus status = m_usbDeviceManager->getCommandSet()->createCommandBytes(commandInfo, commandBytes); if (status != Usb::DeviceCommand::EStatus::OK) { //std::cout << __PRETTY_FUNCTION__ << " startTransmit Error: " << status << std::endl; emit answerIsReady(this, Usb::DeviceCommandStatusCreator::create({}, status)); return; } std::shared_ptr command = std::make_shared(commandInfo->name, commandInfo->uid, std::move(commandBytes)); connect(command.get(), &Usb::ByteCommand::answerIsReady, this, &MicroMonStartReceiveFlashDataCommand::handleStartDataReadingIsReady); commandQueue->enqueue(command); } }; } // namespace Incart::DeviceComplexCommands