////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
// All Rights Reserved.
// Confidential and Proprietary - Qualcomm Technologies, Inc.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// THIS IS AUTO-GENERATED CODE.  PLEASE DO NOT EDIT (File bug reports against tools).
///
/// Auto-generated by: Parameter Parser V5.5.1 (2411131018)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @file  imagesensormodulesetmanager.cpp
/// @brief Auto-generated Chromatix parameter file
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


#include "imagesensormodulesetmanager.h"

ImageSensorModuleSetManager::ImageSensorModuleSetManager()
{
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    /// MinimumNodes
    ///
    /// @brief  Set the minimum number of scenario modes to keep in memory before unloading scenario modes.
    ///
    /// The minimum number of scenario modes to keep active before unloading modes. Used when loading binaries from
    /// a file to enable the scenario mode delay load feature (default is loaded at startup, and new scenario modes
    /// are loaded as they are entered). Switching between Snapshot and Preview mode takes up to 10 scenarios
    /// modes (depending on if the scenario modes have scenes and effects), with no links, and higher with links.
    /// to allow seamless picture taking, set the minimum to at least 10. The default value is 17 to account for
    /// linked modes. Smaller values will increase memory usage efficiency at the cost of scenario mode switching
    /// latency. To use a different value, set the ParameterParser --MinNodes <number> command line option when
    /// generating source (the s command). Setting this value to 0 disables auto scenario mode unloading.
    /// Call the UnloadMode function to manually unload a scenario mode. Calling UnloadMode directly will ignore
    /// Module reference counts, unload all modules and set their reference counts to 0.  Do not call UnloadMode
    /// unless you are certain no modules are referenced within the mode.
    ///
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    MinimumModes   = 17;
    DelayLoad      = true;
    LogError       = true;
    LogWarning     = true;
    LogDebug       = true;
    SimulatorMode  = false;

    AddModule(PARAMETER_NEW CamX::ActuatorDriverDataClass("actuatorDriver"));
    AddModule(PARAMETER_NEW CamX::ActuatorDriverDataClass("actuatorDriver1"));
    AddModule(PARAMETER_NEW CamX::EEbinDriverClass("EEbinDriver"));
    AddModule(PARAMETER_NEW CamX::EEPROMDriverDataClass("EEPROMDriverData"));
    AddModule(PARAMETER_NEW CamX::FlashDriverDataClass("flashDriverData"));
    AddModule(PARAMETER_NEW CamX::FlashDriverDataClass("flashDriverDataSecondary"));
    AddModule(PARAMETER_NEW CamX::CameraModuleDataClass("cameraModuleData"));
    AddModule(PARAMETER_NEW CamX::OISDriverDataClass("OISDriver"));
    AddModule(PARAMETER_NEW CamX::PDAFConfigurationDataClass("PDConfigData"));
    AddModule(PARAMETER_NEW CamX::SensorDriverDataClass("sensorDriverData"));
    AddModule(PARAMETER_NEW CamX::SensorBinSocIdMapClass("sensorBinSocIdMap"));

    PARAMETER_LOG_DEBUG(this, "ImageSensorModuleSetManager constructed: %s\n", VERSION);
};

ImageSensorModuleSetManager::~ImageSensorModuleSetManager()
{
    PARAMETER_LOG_DEBUG(this, "ImageSensorModuleSetManager deleting\n");

    ParameterMutexLock rlock {ReloadMutex, false, true}; // Unique LOCK

    std::unordered_map<std::string, DefaultModule*>::iterator iter = DefaultModuleMap.begin();
    while (iter != DefaultModuleMap.end())
    {
        PARAMETER_DELETE iter->second;
        iter++;
    }

    DefaultModuleMap.clear();
}

void ImageSensorModuleSetManager::AddModule(ParameterModule* module)
{
    if (module != nullptr && module->Type != nullptr)
    {
        ParameterMutexLock rlock {ReloadMutex, false, true}; // Unique LOCK

        DefaultModuleMap[module->Type] = PARAMETER_NEW DefaultModule(module);
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// UpdateBinaryParameters
///
/// @brief Replaces exising parameters with the ones in a buffer. LoadBinaryParameters must be called first
///
/// @param buffer   Binary parameter buffer
/// @param length   Length of the binary parameter buffer
///
/// @return Node
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool ImageSensorModuleSetManager::UpdateBinaryParameters(
    UINT8* const buffer,
    const SIZE_T length)
{
    bool result = Valid;

    if (result)
    {
        ParameterMutexLock rlock {ReloadMutex, false, true}; // Unique LOCK
        if (IsFileMode())
        {
            result = ReloadBinaryParameters(true);
        }

        if (result)
        {
            ImageSensorModuleSetManager* update = PARAMETER_NEW ImageSensorModuleSetManager();

            if (update->LoadBinaryParameters(buffer, length))
            {
                (void)UpdateParameters(update);
                result = true;
            }
            else
            {
                SetError("UpdateBinaryParameters failed to load parameters");
            }
            m_updateSM.push_back((ParameterSetManager*)update);
        }
        else
        {
            SetError("UpdateBinaryParameters failed to reload parameters in buffer mode");
        }
    }
    else
    {
        SetError("UpdateBinaryParameters failed, original parameter load is invalid");
    }

    return result;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// SaveModules
///
/// @brief  Saves all current modules to the m_replaced list to prevent accessing deleted memory
///
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ImageSensorModuleSetManager::SaveAndClearModules()
{
    std::unordered_map<std::string, DefaultModule*>::iterator iter = DefaultModuleMap.begin();
    while (iter != DefaultModuleMap.end())
    {
        DefaultModule* const module = iter->second;
        ModuleLink*    link   = module->FirstModule;
        while (link != nullptr)
        {
            ModuleLink* const next = link->Next;
            m_replaced.push_back(link->Module);
            link->Module = nullptr;
            link->Next   = nullptr;
            PARAMETER_DELETE link;
            link = next;

        }
        module->FirstModule = nullptr;
        module->LastModule  = nullptr;
        iter++;
    }
}

CamX::ActuatorDriverData* ImageSensorModuleSetManager::GetModule_actuatorDriver(
    const TuningMode* const modeBranch,
    UINT32 const modeCount,
    CamX::ActuatorDriverData* const release)
{
    ParameterModule* const pModule = GetModule((const CHAR *)"actuatorDriver",
        modeBranch, modeCount, (ParameterModule*)(CamX::ActuatorDriverDataClass*)release);

    return (CamX::ActuatorDriverData*)(CamX::ActuatorDriverDataClass*)pModule;
}

CamX::ActuatorDriverData* ImageSensorModuleSetManager::GetModule_actuatorDriver1(
    const TuningMode* const modeBranch,
    UINT32 const modeCount,
    CamX::ActuatorDriverData* const release)
{
    ParameterModule* const pModule = GetModule((const CHAR *)"actuatorDriver1",
        modeBranch, modeCount, (ParameterModule*)(CamX::ActuatorDriverDataClass*)release);

    return (CamX::ActuatorDriverData*)(CamX::ActuatorDriverDataClass*)pModule;
}

CamX::EEbinDriver* ImageSensorModuleSetManager::GetModule_EEbinDriver(
    const TuningMode* const modeBranch,
    UINT32 const modeCount,
    CamX::EEbinDriver* const release)
{
    ParameterModule* const pModule = GetModule((const CHAR *)"EEbinDriver",
        modeBranch, modeCount, (ParameterModule*)(CamX::EEbinDriverClass*)release);

    return (CamX::EEbinDriver*)(CamX::EEbinDriverClass*)pModule;
}

CamX::EEPROMDriverData* ImageSensorModuleSetManager::GetModule_EEPROMDriverData(
    const TuningMode* const modeBranch,
    UINT32 const modeCount,
    CamX::EEPROMDriverData* const release)
{
    ParameterModule* const pModule = GetModule((const CHAR *)"EEPROMDriverData",
        modeBranch, modeCount, (ParameterModule*)(CamX::EEPROMDriverDataClass*)release);

    return (CamX::EEPROMDriverData*)(CamX::EEPROMDriverDataClass*)pModule;
}

CamX::FlashDriverData* ImageSensorModuleSetManager::GetModule_flashDriverData(
    const TuningMode* const modeBranch,
    UINT32 const modeCount,
    CamX::FlashDriverData* const release)
{
    ParameterModule* const pModule = GetModule((const CHAR *)"flashDriverData",
        modeBranch, modeCount, (ParameterModule*)(CamX::FlashDriverDataClass*)release);

    return (CamX::FlashDriverData*)(CamX::FlashDriverDataClass*)pModule;
}

CamX::FlashDriverData* ImageSensorModuleSetManager::GetModule_flashDriverDataSecondary(
    const TuningMode* const modeBranch,
    UINT32 const modeCount,
    CamX::FlashDriverData* const release)
{
    ParameterModule* const pModule = GetModule((const CHAR *)"flashDriverDataSecondary",
        modeBranch, modeCount, (ParameterModule*)(CamX::FlashDriverDataClass*)release);

    return (CamX::FlashDriverData*)(CamX::FlashDriverDataClass*)pModule;
}

CamX::CameraModuleData* ImageSensorModuleSetManager::GetModule_cameraModuleData(
    const TuningMode* const modeBranch,
    UINT32 const modeCount,
    CamX::CameraModuleData* const release)
{
    ParameterModule* const pModule = GetModule((const CHAR *)"cameraModuleData",
        modeBranch, modeCount, (ParameterModule*)(CamX::CameraModuleDataClass*)release);

    return (CamX::CameraModuleData*)(CamX::CameraModuleDataClass*)pModule;
}

CamX::OISDriverData* ImageSensorModuleSetManager::GetModule_OISDriver(
    const TuningMode* const modeBranch,
    UINT32 const modeCount,
    CamX::OISDriverData* const release)
{
    ParameterModule* const pModule = GetModule((const CHAR *)"OISDriver",
        modeBranch, modeCount, (ParameterModule*)(CamX::OISDriverDataClass*)release);

    return (CamX::OISDriverData*)(CamX::OISDriverDataClass*)pModule;
}

CamX::PDAFConfigurationData* ImageSensorModuleSetManager::GetModule_PDConfigData(
    const TuningMode* const modeBranch,
    UINT32 const modeCount,
    CamX::PDAFConfigurationData* const release)
{
    ParameterModule* const pModule = GetModule((const CHAR *)"PDConfigData",
        modeBranch, modeCount, (ParameterModule*)(CamX::PDAFConfigurationDataClass*)release);

    return (CamX::PDAFConfigurationData*)(CamX::PDAFConfigurationDataClass*)pModule;
}

CamX::SensorDriverData* ImageSensorModuleSetManager::GetModule_sensorDriverData(
    const TuningMode* const modeBranch,
    UINT32 const modeCount,
    CamX::SensorDriverData* const release)
{
    ParameterModule* const pModule = GetModule((const CHAR *)"sensorDriverData",
        modeBranch, modeCount, (ParameterModule*)(CamX::SensorDriverDataClass*)release);

    return (CamX::SensorDriverData*)(CamX::SensorDriverDataClass*)pModule;
}

CamX::SensorBinSocIdMap* ImageSensorModuleSetManager::GetModule_sensorBinSocIdMap(
    const TuningMode* const modeBranch,
    UINT32 const modeCount,
    CamX::SensorBinSocIdMap* const release)
{
    ParameterModule* const pModule = GetModule((const CHAR *)"sensorBinSocIdMap",
        modeBranch, modeCount, (ParameterModule*)(CamX::SensorBinSocIdMapClass*)release);

    return (CamX::SensorBinSocIdMap*)(CamX::SensorBinSocIdMapClass*)pModule;
}

