Program Listing for File DeviceBase.hpp

Return to documentation for file (include/depthai/device/DeviceBase.hpp)

#pragma once

// std
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <functional>
#include <memory>
#include <mutex>
#include <string>
#include <thread>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <vector>

// project
#include "depthai/common/CameraBoardSocket.hpp"
#include "depthai/common/CameraFeatures.hpp"
#include "depthai/common/UsbSpeed.hpp"
#include "depthai/device/CalibrationHandler.hpp"
#include "depthai/device/Version.hpp"
#include "depthai/openvino/OpenVINO.hpp"
#include "depthai/utility/Pimpl.hpp"
#include "depthai/utility/ProfilingData.hpp"
#include "depthai/xlink/XLinkConnection.hpp"
#include "depthai/xlink/XLinkStream.hpp"

// shared
#include "depthai-shared/common/ChipTemperature.hpp"
#include "depthai-shared/common/ConnectionInterface.hpp"
#include "depthai-shared/common/CpuUsage.hpp"
#include "depthai-shared/common/MemoryInfo.hpp"
#include "depthai-shared/common/StereoPair.hpp"
#include "depthai-shared/datatype/RawIMUData.hpp"
#include "depthai-shared/device/BoardConfig.hpp"
#include "depthai-shared/device/CrashDump.hpp"
#include "depthai-shared/log/LogLevel.hpp"
#include "depthai-shared/log/LogMessage.hpp"
#include "depthai-shared/pipeline/PipelineSchema.hpp"

namespace dai {

// Forward declare Pipeline
class Pipeline;

class DeviceBase {
   public:
    // constants

    static constexpr std::chrono::seconds DEFAULT_SEARCH_TIME{3};
    static constexpr float DEFAULT_SYSTEM_INFORMATION_LOGGING_RATE_HZ{1.0f};
    static constexpr UsbSpeed DEFAULT_USB_SPEED{UsbSpeed::SUPER};
    static constexpr std::chrono::milliseconds DEFAULT_TIMESYNC_PERIOD{5000};
    static constexpr int DEFAULT_TIMESYNC_NUM_SAMPLES{10};
    static constexpr bool DEFAULT_TIMESYNC_RANDOM{true};

    // Structures

    struct Config {
        OpenVINO::Version version = OpenVINO::VERSION_UNIVERSAL;
        BoardConfig board;
        bool nonExclusiveMode = false;
        tl::optional<LogLevel> outputLogLevel;
        tl::optional<LogLevel> logLevel;
    };

    // static API

    static std::chrono::milliseconds getDefaultSearchTime();

    static std::tuple<bool, DeviceInfo> getAnyAvailableDevice(std::chrono::milliseconds timeout);

    static std::tuple<bool, DeviceInfo> getAnyAvailableDevice();

    static std::tuple<bool, DeviceInfo> getAnyAvailableDevice(std::chrono::milliseconds timeout, std::function<void()> cb);

    static std::tuple<bool, DeviceInfo> getFirstAvailableDevice(bool skipInvalidDevice = true);

    static std::tuple<bool, DeviceInfo> getDeviceByMxId(std::string mxId);

    static std::vector<DeviceInfo> getAllAvailableDevices();

    static std::vector<DeviceInfo> getAllConnectedDevices();

    static std::vector<std::uint8_t> getEmbeddedDeviceBinary(bool usb2Mode, OpenVINO::Version version = OpenVINO::VERSION_UNIVERSAL);

    static std::vector<std::uint8_t> getEmbeddedDeviceBinary(Config config);

    static ProfilingData getGlobalProfilingData();

    explicit DeviceBase(const Pipeline& pipeline);

    template <typename T, std::enable_if_t<std::is_same<T, bool>::value, bool> = true>
    DeviceBase(const Pipeline& pipeline, T usb2Mode) : DeviceBase(pipeline, usb2Mode ? UsbSpeed::HIGH : DeviceBase::DEFAULT_USB_SPEED) {}

    DeviceBase(const Pipeline& pipeline, UsbSpeed maxUsbSpeed);

    DeviceBase(const Pipeline& pipeline, const dai::Path& pathToCmd);

    DeviceBase(const Pipeline& pipeline, const DeviceInfo& devInfo);

    template <typename T, std::enable_if_t<std::is_same<T, bool>::value, bool> = true>
    DeviceBase(const Pipeline& pipeline, const DeviceInfo& devInfo, T usb2Mode)
        : DeviceBase(pipeline, devInfo, usb2Mode ? UsbSpeed::HIGH : DeviceBase::DEFAULT_USB_SPEED) {}

    DeviceBase(const Pipeline& pipeline, const DeviceInfo& devInfo, UsbSpeed maxUsbSpeed);

    DeviceBase(const Pipeline& pipeline, const DeviceInfo& devInfo, const dai::Path& pathToCmd);

    DeviceBase();

    explicit DeviceBase(OpenVINO::Version version);

    template <typename T, std::enable_if_t<std::is_same<T, bool>::value, bool> = true>
    DeviceBase(OpenVINO::Version version, T usb2Mode) : DeviceBase(version, usb2Mode ? UsbSpeed::HIGH : DeviceBase::DEFAULT_USB_SPEED) {}

    DeviceBase(OpenVINO::Version version, UsbSpeed maxUsbSpeed);

    DeviceBase(OpenVINO::Version version, const dai::Path& pathToCmd);

    DeviceBase(OpenVINO::Version version, const DeviceInfo& devInfo);

    template <typename T, std::enable_if_t<std::is_same<T, bool>::value, bool> = true>
    DeviceBase(OpenVINO::Version version, const DeviceInfo& devInfo, T usb2Mode)
        : DeviceBase(version, devInfo, usb2Mode ? UsbSpeed::HIGH : DeviceBase::DEFAULT_USB_SPEED) {}

    DeviceBase(OpenVINO::Version version, const DeviceInfo& devInfo, UsbSpeed maxUsbSpeed);

    DeviceBase(OpenVINO::Version version, const DeviceInfo& devInfo, const dai::Path& pathToCmd);

    explicit DeviceBase(Config config);

    DeviceBase(Config config, const DeviceInfo& devInfo);

    explicit DeviceBase(const DeviceInfo& devInfo);

    DeviceBase(const DeviceInfo& devInfo, UsbSpeed maxUsbSpeed);

    DeviceBase(std::string nameOrDeviceId);

    DeviceBase(std::string nameOrDeviceId, UsbSpeed maxUsbSpeed);

    template <typename T, std::enable_if_t<std::is_same<T, bool>::value, bool> = true>
    DeviceBase(Config config, T usb2Mode) : DeviceBase(config, usb2Mode ? UsbSpeed::HIGH : DeviceBase::DEFAULT_USB_SPEED) {}

    DeviceBase(Config config, UsbSpeed maxUsbSpeed);

    DeviceBase(Config config, const dai::Path& pathToCmd);

    template <typename T, std::enable_if_t<std::is_same<T, bool>::value, bool> = true>
    DeviceBase(Config config, const DeviceInfo& devInfo, T usb2Mode) : DeviceBase(config, devInfo, usb2Mode ? UsbSpeed::HIGH : DeviceBase::DEFAULT_USB_SPEED) {}

    DeviceBase(Config config, const DeviceInfo& devInfo, UsbSpeed maxUsbSpeed);

    DeviceBase(Config config, const DeviceInfo& devInfo, const dai::Path& pathToCmd, bool dumpOnly = false);

    virtual ~DeviceBase();

    tl::optional<Version> getBootloaderVersion();

    bool isPipelineRunning();

    [[deprecated("Device(pipeline) starts the pipeline automatically. See Device() and startPipeline(pipeline) otherwise")]] bool startPipeline();

    bool startPipeline(const Pipeline& pipeline);

    void setLogLevel(LogLevel level);

    LogLevel getLogLevel();

    void setXLinkChunkSize(int sizeBytes);

    int getXLinkChunkSize();

    void setXLinkRateLimit(int maxRateBytesPerSecond, int burstSize = 0, int waitUs = 0);

    DeviceInfo getDeviceInfo() const;

    std::string getDeviceName();

    std::string getProductName();

    std::string getMxId();

    void setLogOutputLevel(LogLevel level);

    LogLevel getLogOutputLevel();

    [[deprecated("Use setIrLaserDotProjectorIntensity(float intensity) instead.")]] bool setIrLaserDotProjectorBrightness(float mA, int mask = -1);

    [[deprecated("Use setIrFloodLightIntensity(float intensity) instead.")]] bool setIrFloodLightBrightness(float mA, int mask = -1);

    bool setIrLaserDotProjectorIntensity(float intensity, int mask = -1);

    bool setIrFloodLightIntensity(float intensity, int mask = -1);

    std::vector<std::tuple<std::string, int, int>> getIrDrivers();

    dai::CrashDump getCrashDump(bool clearCrashDump = true);

    bool hasCrashDump();

    ProfilingData getProfilingData();

    int addLogCallback(std::function<void(LogMessage)> callback);

    bool removeLogCallback(int callbackId);

    void setSystemInformationLoggingRate(float rateHz);

    float getSystemInformationLoggingRate();

    std::vector<CameraBoardSocket> getConnectedCameras();

    std::vector<ConnectionInterface> getConnectionInterfaces();

    std::vector<CameraFeatures> getConnectedCameraFeatures();

    std::vector<StereoPair> getStereoPairs();

    std::vector<StereoPair> getAvailableStereoPairs();

    std::unordered_map<CameraBoardSocket, std::string> getCameraSensorNames();

    std::string getConnectedIMU();

    dai::Version getIMUFirmwareVersion();

    dai::Version getEmbeddedIMUFirmwareVersion();

    bool startIMUFirmwareUpdate(bool forceUpdate = false);

    std::tuple<bool, unsigned int> getIMUFirmwareUpdateStatus();

    MemoryInfo getDdrMemoryUsage();

    MemoryInfo getCmxMemoryUsage();

    MemoryInfo getLeonCssHeapUsage();

    MemoryInfo getLeonMssHeapUsage();

    ChipTemperature getChipTemperature();

    CpuUsage getLeonCssCpuUsage();

    CpuUsage getLeonMssCpuUsage();

    bool isEepromAvailable();

    bool flashCalibration(CalibrationHandler calibrationDataHandler);

    void flashCalibration2(CalibrationHandler calibrationDataHandler);

    void setCalibration(CalibrationHandler calibrationDataHandler);

    CalibrationHandler getCalibration();

    CalibrationHandler readCalibration();

    CalibrationHandler readCalibration2();

    CalibrationHandler readCalibrationOrDefault();

    void factoryResetCalibration();

    void flashFactoryCalibration(CalibrationHandler calibrationHandler);

    void flashEepromClear();

    void flashFactoryEepromClear();

    CalibrationHandler readFactoryCalibration();

    CalibrationHandler readFactoryCalibrationOrDefault();

    std::vector<std::uint8_t> readCalibrationRaw();

    std::vector<std::uint8_t> readFactoryCalibrationRaw();

    UsbSpeed getUsbSpeed();

    void setTimesync(std::chrono::milliseconds period, int numSamples, bool random);

    void setTimesync(bool enable);

    void close();

    bool isClosed() const;

    std::shared_ptr<XLinkConnection> getConnection() {
        return connection;
    }

    std::shared_ptr<const XLinkConnection> getConnection() const {
        return connection;
    }

   protected:
    std::shared_ptr<XLinkConnection> connection;

    void tryStartPipeline(const Pipeline& pipeline);

    virtual bool startPipelineImpl(const Pipeline& pipeline);

    virtual void closeImpl();

   protected:
    // protected functions
    void init(OpenVINO::Version version);
    void init(OpenVINO::Version version, const dai::Path& pathToCmd);
    void init(OpenVINO::Version version, UsbSpeed maxUsbSpeed);
    void init(OpenVINO::Version version, UsbSpeed maxUsbSpeed, const dai::Path& pathToMvcmd);
    void init(const Pipeline& pipeline);
    void init(const Pipeline& pipeline, UsbSpeed maxUsbSpeed);
    void init(const Pipeline& pipeline, const dai::Path& pathToCmd);
    void init(const Pipeline& pipeline, const DeviceInfo& devInfo);
    void init(const Pipeline& pipeline, const DeviceInfo& devInfo, bool usb2Mode);
    void init(const Pipeline& pipeline, const DeviceInfo& devInfo, UsbSpeed maxUsbSpeed);
    void init(const Pipeline& pipeline, const DeviceInfo& devInfo, const dai::Path& pathToCmd);
    void init(const Pipeline& pipeline, UsbSpeed maxUsbSpeed, const dai::Path& pathToMvcmd);
    void init(Config config, UsbSpeed maxUsbSpeed, const dai::Path& pathToMvcmd);
    void init(Config config, UsbSpeed maxUsbSpeed);
    void init(Config config, const dai::Path& pathToCmd);
    void init(Config config, const DeviceInfo& devInfo, UsbSpeed maxUsbSpeed);
    void init(Config config, const DeviceInfo& devInfo, const dai::Path& pathToCmd);

   private:
    // private functions
    void init2(Config cfg, const dai::Path& pathToMvcmd, tl::optional<const Pipeline&> pipeline);
    void tryGetDevice();

    DeviceInfo deviceInfo = {};
    tl::optional<Version> bootloaderVersion;

    // Log callback
    int uniqueCallbackId = 0;
    std::mutex logCallbackMapMtx;
    std::unordered_map<int, std::function<void(LogMessage)>> logCallbackMap;

    // Watchdog thread
    std::thread watchdogThread;
    std::atomic<bool> watchdogRunning{true};

    // Timesync thread
    std::thread timesyncThread;
    std::atomic<bool> timesyncRunning{true};

    // Logging thread
    std::thread loggingThread;
    std::atomic<bool> loggingRunning{true};

    // Profiling thread
    std::thread profilingThread;
    std::atomic<bool> profilingRunning{true};

    // Monitor thread
    std::thread monitorThread;
    std::mutex lastWatchdogPingTimeMtx;
    std::chrono::steady_clock::time_point lastWatchdogPingTime;

    // closed
    mutable std::mutex closedMtx;
    bool closed{false};

    // pimpl
    class Impl;
    Pimpl<Impl> pimpl;

    // Device config
    Config config;

    dai::Path firmwarePath;
    bool dumpOnly = false;

    // Schema of the started pipeline
    tl::optional<PipelineSchema> pipelineSchema;
};
}  // namespace dai