.. _program_listing_file_include_depthai_device_DeviceBootloader.hpp: Program Listing for File DeviceBootloader.hpp ============================================= |exhale_lsh| :ref:`Return to documentation for file ` (``include/depthai/device/DeviceBootloader.hpp``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp #pragma once // std #include #include #include // project #include "CallbackHandler.hpp" #include "DataQueue.hpp" #include "depthai/common/UsbSpeed.hpp" #include "depthai/pipeline/Pipeline.hpp" #include "depthai/xlink/XLinkConnection.hpp" #include "depthai/xlink/XLinkStream.hpp" // shared #include "depthai-bootloader-shared/Config.hpp" #include "depthai-bootloader-shared/Memory.hpp" #include "depthai-bootloader-shared/Section.hpp" #include "depthai-bootloader-shared/Type.hpp" namespace dai { // DeviceBootloader (RAII), connects to device and maintains watchdog ... class DeviceBootloader { public: // Alias using Type = dai::bootloader::Type; using Memory = dai::bootloader::Memory; using Section = dai::bootloader::Section; using UsbConfig = dai::bootloader::UsbConfig; using NetworkConfig = dai::bootloader::NetworkConfig; // Derive and extend bootloader::Config for easier usage struct Config : public bootloader::Config { void setStaticIPv4(std::string ip, std::string mask, std::string gateway); void setDynamicIPv4(std::string ip, std::string mask, std::string gateway); bool isStaticIPV4(); std::string getIPv4(); std::string getIPv4Mask(); std::string getIPv4Gateway(); void setDnsIPv4(std::string dns, std::string dnsAlt = ""); std::string getDnsIPv4(); std::string getDnsAltIPv4(); void setUsbTimeout(std::chrono::milliseconds ms); std::chrono::milliseconds getUsbTimeout(); void setNetworkTimeout(std::chrono::milliseconds ms); std::chrono::milliseconds getNetworkTimeout(); void setMacAddress(std::string mac); std::string getMacAddress(); void setUsbMaxSpeed(UsbSpeed speed); UsbSpeed getUsbMaxSpeed(); nlohmann::json toJson() const; static Config fromJson(nlohmann::json); private: nlohmann::json data; }; // Add an alias to Version using Version = dai::Version; struct ApplicationInfo { Memory memory; bool hasApplication; std::string firmwareVersion; std::string applicationName; }; struct MemoryInfo { bool available; std::int64_t size; std::string info; }; // constants static constexpr const Type DEFAULT_TYPE{Type::USB}; // Static API static std::tuple getFirstAvailableDevice(); static std::vector getAllAvailableDevices(); static std::vector createDepthaiApplicationPackage( const Pipeline& pipeline, const dai::Path& pathToCmd = {}, bool compress = false, std::string applicationName = "", bool checkChecksum = false); static std::vector createDepthaiApplicationPackage(const Pipeline& pipeline, bool compress, std::string applicationName = "", bool checkChecksum = false); static void saveDepthaiApplicationPackage(const dai::Path& path, const Pipeline& pipeline, const dai::Path& pathToCmd = {}, bool compress = false, std::string applicationName = "", bool checkChecksum = false); static void saveDepthaiApplicationPackage( const dai::Path& path, const Pipeline& pipeline, bool compress, std::string applicationName = "", bool checkChecksum = false); static Version getEmbeddedBootloaderVersion(); static std::vector getEmbeddedBootloaderBinary(Type type = DEFAULT_TYPE); DeviceBootloader() = delete; explicit DeviceBootloader(const DeviceInfo& devInfo); template ::value, bool> = true> DeviceBootloader(const DeviceInfo& devInfo, T allowFlashingBootloader); DeviceBootloader(const DeviceInfo& devInfo, Type type, bool allowFlashingBootloader = false); DeviceBootloader(const DeviceInfo& devInfo, const dai::Path& pathToBootloader, bool allowFlashingBootloader = false); DeviceBootloader(std::string nameOrDeviceId, bool allowFlashingBootloader = false); ~DeviceBootloader(); std::tuple flash(std::function progressCallback, const Pipeline& pipeline, bool compress = false, std::string applicationName = "", Memory memory = Memory::AUTO, bool checkChecksum = false); std::tuple flash( const Pipeline& pipeline, bool compress = false, std::string applicationName = "", Memory memory = Memory::AUTO, bool checkChecksum = false); ApplicationInfo readApplicationInfo(Memory memory); std::tuple flashDepthaiApplicationPackage(std::function progressCallback, std::vector package, Memory memory = Memory::AUTO); std::tuple flashDepthaiApplicationPackage(std::vector package, Memory memory = Memory::AUTO); std::tuple flashClear(Memory memory = Memory::AUTO); std::tuple flashBootloader(std::function progressCallback, const dai::Path& path = {}); std::tuple flashBootloader(Memory memory, Type type, std::function progressCallback, const dai::Path& path = {}); std::tuple flashUserBootloader(std::function progressCallback, const dai::Path& path = {}); std::tuple flashGpioModeBootHeader(Memory memory, int gpioMode); std::tuple flashUsbRecoveryBootHeader(Memory memory); std::tuple flashBootHeader(Memory memory, int32_t frequency = -1, int64_t location = -1, int32_t dummyCycles = -1, int64_t offset = -1); std::tuple flashFastBootHeader( Memory memory, int32_t frequency = -1, int64_t location = -1, int32_t dummyCycles = -1, int64_t offset = -1); std::tuple flashCustom(Memory memory, size_t offset, const std::vector& data, std::function progressCb = nullptr); std::tuple flashCustom(Memory memory, size_t offset, const uint8_t* data, size_t size, std::function progressCb = nullptr); std::tuple flashCustom(Memory memory, size_t offset, std::string filename, std::function progressCb = nullptr); std::tuple readCustom( Memory memory, size_t offset, size_t size, std::vector& data, std::function progressCb = nullptr); std::tuple readCustom(Memory memory, size_t offset, size_t size, uint8_t* data, std::function progressCb = nullptr); std::tuple readCustom(Memory memory, size_t offset, size_t size, std::string filename, std::function progressCb = nullptr); std::tuple> readCustom(Memory memory, size_t offset, size_t size, std::function progressCb = nullptr); nlohmann::json readConfigData(Memory memory = Memory::AUTO, Type type = Type::AUTO); std::tuple flashConfigData(nlohmann::json configData, Memory memory = Memory::AUTO, Type type = Type::AUTO); std::tuple flashConfigFile(const dai::Path& configPath, Memory memory = Memory::AUTO, Type type = Type::AUTO); std::tuple flashConfigClear(Memory memory = Memory::AUTO, Type type = Type::AUTO); Config readConfig(Memory memory = Memory::AUTO, Type type = Type::AUTO); std::tuple flashConfig(const Config& config, Memory memory = Memory::AUTO, Type type = Type::AUTO); MemoryInfo getMemoryInfo(Memory memory); bool isUserBootloaderSupported(); bool isUserBootloader(); void bootMemory(const std::vector& fw); void bootUsbRomBootloader(); Version getVersion() const; tl::optional getFlashedVersion() const; bool isEmbeddedVersion() const; Type getType() const; bool isAllowedFlashingBootloader() const; void close(); bool isClosed() const; private: // private static // private methods void init(bool embeddedMvcmd, const dai::Path& pathToMvcmd, tl::optional type, bool allowBlFlash); template bool sendRequest(const T& request); template void sendRequestThrow(const T& request); bool receiveResponseData(std::vector& data); template bool parseResponse(const std::vector& data, T& response); template bool receiveResponse(T& response); template void receiveResponseThrow(T& response); Version requestVersion(); std::tuple flashCustom( Memory memory, size_t offset, const uint8_t* data, size_t size, std::string filename, std::function progressCb); std::tuple readCustom( Memory memory, size_t offset, size_t size, uint8_t* data, std::string filename, std::function progressCb); void createWatchdog(); void destroyWatchdog(); // private variables std::shared_ptr connection; DeviceInfo deviceInfo = {}; bool isEmbedded = false; Type bootloaderType; tl::optional flashedVersion; // closed std::atomic closed{false}; // Watchdog thread std::thread watchdogThread; std::atomic watchdogRunning{true}; // Monitor thread std::thread monitorThread; std::mutex lastWatchdogPingTimeMtx; std::chrono::steady_clock::time_point lastWatchdogPingTime; // bootloader stream std::unique_ptr stream; // Allow flashing bootloader flag bool allowFlashingBootloader = false; // Current connected bootloader version Version version{0, 0, 2}; }; } // namespace dai // Global namespace inline std::ostream& operator<<(std::ostream& out, const dai::DeviceBootloader::Type& type) { switch(type) { case dai::DeviceBootloader::Type::AUTO: out << "AUTO"; break; case dai::DeviceBootloader::Type::USB: out << "USB"; break; case dai::DeviceBootloader::Type::NETWORK: out << "NETWORK"; break; } return out; } inline std::ostream& operator<<(std::ostream& out, const dai::DeviceBootloader::Memory& memory) { switch(memory) { case dai::DeviceBootloader::Memory::AUTO: out << "AUTO"; break; case dai::DeviceBootloader::Memory::FLASH: out << "FLASH"; break; case dai::DeviceBootloader::Memory::EMMC: out << "EMMC"; break; } return out; } inline std::ostream& operator<<(std::ostream& out, const dai::DeviceBootloader::Section& type) { switch(type) { case dai::DeviceBootloader::Section::AUTO: out << "AUTO"; break; case dai::DeviceBootloader::Section::HEADER: out << "HEADER"; break; case dai::DeviceBootloader::Section::BOOTLOADER: out << "BOOTLOADER"; break; case dai::DeviceBootloader::Section::USER_BOOTLOADER: out << "USER_BOOTLOADER"; break; case dai::DeviceBootloader::Section::BOOTLOADER_CONFIG: out << "BOOTLOADER_CONFIG"; break; case dai::DeviceBootloader::Section::APPLICATION: out << "APPLICATION"; break; } return out; } inline std::ostream& operator<<(std::ostream& out, const dai::DeviceBootloader::Version& v) { return out << v.toString(); }