////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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  camxflashdriver.cpp
/// @brief Auto-generated Chromatix parameter file
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include "camxflashdriver.h"
#include "parametersetmanager.h"

CAMX_NAMESPACE_BEGIN

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// SetValue/GetValue Functions
/// Gets or sets the requested value
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Read enum value
/// @param entry [in] Symbol table entry
/// @param enumValue [out] Enum value to read
/// @param alignment [in] Symbol table byte alignment
/// @return true if successful
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool ReadEnum_FlashDriverType(
    ParameterFileSymbolTableEntry* const entry,
    FlashDriverType& enumValue,
    BYTE const alignment)
{
    bool result {false};

    UINT32 uintValue {entry->ReadEnum(&result, alignment)};

    if (result)
    {
        switch (uintValue)
        {
        case static_cast<UINT32>(FlashDriverType::PMIC):
            enumValue = FlashDriverType::PMIC;
            result    = true;
            break;
        case static_cast<UINT32>(FlashDriverType::I2C):
            enumValue = FlashDriverType::I2C;
            result    = true;
            break;
        default:
            PARAMETER_LOG_ERROR(entry->SetManager, "Invalid value(% u) converting UINT32 to FlashDriverType", uintValue);
            break;
        }
    }
    return result;
}


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Default Constructor
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
FlashDriverDataClass::FlashDriverDataClass(
    const CHAR* const name)
    : ParameterModule(name, MAJOR, MINOR, PATCH, nullptr)
{
    PARAMETER_INITIALIZE(SymbolTableID);
    PARAMETER_INITIALIZE(module_version);
    PARAMETER_INITIALIZE(flashName);
    PARAMETER_INITIALIZE(flashDriverType);
    PARAMETER_INITIALIZE(powerUpSequenceExists);
    PARAMETER_INITIALIZE(powerUpSequence);
    PARAMETER_INITIALIZE(powerDownSequenceExists);
    PARAMETER_INITIALIZE(powerDownSequence);
    PARAMETER_INITIALIZE(regAddrInfoFlashExists);
    PARAMETER_INITIALIZE(regAddrInfoFlash);
    PARAMETER_INITIALIZE(i2cInfoExists);
    PARAMETER_INITIALIZE(i2cInfo);
    PARAMETER_INITIALIZE(flashInformationExists);
    PARAMETER_INITIALIZE(flashInformation);
    PARAMETER_INITIALIZE(numberOfFlashs);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Constructor
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
FlashDriverDataClass::FlashDriverDataClass(
    const CHAR* const name,
    ModuleData* const moduleData)
    : ParameterModule(name, MAJOR, MINOR, PATCH, moduleData)
{
    PARAMETER_INITIALIZE(SymbolTableID);
    PARAMETER_INITIALIZE(module_version);
    PARAMETER_INITIALIZE(flashName);
    PARAMETER_INITIALIZE(flashDriverType);
    PARAMETER_INITIALIZE(powerUpSequence);
    PARAMETER_INITIALIZE(powerDownSequence);
    PARAMETER_INITIALIZE(regAddrInfoFlash);
    PARAMETER_INITIALIZE(i2cInfo);
    PARAMETER_INITIALIZE(flashInformation);
    PARAMETER_INITIALIZE(numberOfFlashs);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Destructor
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
FlashDriverDataClass::~FlashDriverDataClass()
{
    if (Loaded)
    {
        UnloadModule();
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// GetName
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const CHAR* FlashDriverDataClass::GetName(
    const CHAR* const type)
{
    const CHAR* name = type;
    CHAR temp[ParameterFileSymbolTableEntry::TYPE_LEN + 1];
    if (PARAMETER_STRCMP(type, ModuleData::GetType(temp, "flashDriverData")) == 0)
    {
        name = "flashDriverData";
    }
    if (PARAMETER_STRCMP(type, ModuleData::GetType(temp, "flashDriverDataSecondary")) == 0)
    {
        name = "flashDriverDataSecondary";
    }
    return name;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Load FlashI2CInformation
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool FlashI2CInformation::Load(
    ParameterFileSymbolTableEntry* const entry,
    BYTE const alignment)
{
    bool result = true;

    PARAMETER_INITIALIZE(slaveAddress);
    PARAMETER_INITIALIZE(regAddrType);
    PARAMETER_INITIALIZE(regDataType);
    PARAMETER_INITIALIZE(i2cFrequencyMode);
    PARAMETER_INITIALIZE(flashInitSettings);
    PARAMETER_INITIALIZE(flashOffSettings);
    PARAMETER_INITIALIZE(flashLowSettings);
    PARAMETER_INITIALIZE(flashHighSettings);

    SYMBOL_TABLE_ENTRY_READ(entry, result, slaveAddress, alignment)
    SYMBOL_TABLE_ENTRY_READ(entry, result, regAddrType, alignment)
    SYMBOL_TABLE_ENTRY_READ(entry, result, regDataType, alignment)
    if (result)
    {
        result = ReadEnum_I2CFrequencyMode(entry, i2cFrequencyMode, alignment);
    }
    if (result)
    {
        result = flashInitSettings.Load(entry, alignment);
    }
    if (result)
    {
        result = flashOffSettings.Load(entry, alignment);
    }
    if (result)
    {
        result = flashLowSettings.Load(entry, alignment);
    }
    if (result)
    {
        result = flashHighSettings.Load(entry, alignment);
    }

    return result;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Load TriggerInformation
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool TriggerInformation::Load(
    ParameterFileSymbolTableEntry* const entry,
    BYTE const alignment)
{
    bool result = true;

    PARAMETER_INITIALIZE(maxFlashCurrent);
    PARAMETER_INITIALIZE(maxTorchCurrent);
    PARAMETER_INITIALIZE(maxFlashDuration);

    SYMBOL_TABLE_ENTRY_READ(entry, result, maxFlashCurrent, alignment)
    SYMBOL_TABLE_ENTRY_READ(entry, result, maxTorchCurrent, alignment)
    SYMBOL_TABLE_ENTRY_READ(entry, result, maxFlashDuration, alignment)

    return result;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Load FlashInformation
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool FlashInformation::Load(
    ParameterFileSymbolTableEntry* const entry,
    BYTE const alignment)
{
    bool result = true;

    PARAMETER_INITIALIZE(triggerInfo);


    // Reading Array triggerInfo from symbol table
    // Do not check result value here because the count value needs to be initialized in this block
    {
        triggerInfoCount = 0;
        SYMBOL_TABLE_ENTRY_READ(entry, result, triggerInfoCount, alignment)
        constexpr const CHAR* const file {__FILE__};
        constexpr INT32 line {LINE_CONST};
        ParameterFileSymbolTableEntry  pointerEntryCls;
        ParameterFileSymbolTableEntry* const pointerEntry = &pointerEntryCls;
        if (result)
        {
            result = entry->Table->ReadPointerEntry(pointerEntryCls, entry, alignment);
        }

        if (result)
        {
            triggerInfoID = pointerEntry->ID;
            DupObjInfo* triggerInfoDupInfo = pointerEntry->GetDupObject(triggerInfoCount, file, line);
            if (triggerInfoDupInfo != nullptr && triggerInfoDupInfo->Object != nullptr)
            {
                triggerInfo = (TriggerInformation*)triggerInfoDupInfo->Object;
                if (triggerInfo == nullptr)
                {
                    if (triggerInfoDupInfo->File != nullptr)
                    {
                        PARAMETER_LOG_ERROR(entry->SetManager, "triggerInfo duplicate found but object is nullptr: %s:%d, RefCount: %d\n",
                            triggerInfoDupInfo->File, triggerInfoDupInfo->Line, triggerInfoDupInfo->RefCount);
                    }
                    else
                    {
                        PARAMETER_LOG_ERROR(entry->SetManager, "triggerInfo duplicate found but Object is nullptr: <nullptr file ptr>:%d, RefCount: %d\n",
                            triggerInfoDupInfo->Line, triggerInfoDupInfo->RefCount);
                    }
                }
            }

            if (triggerInfo == nullptr)
            {
                if (result && triggerInfoCount > 0)
                {
                    triggerInfo = PARAMETER_NEW TriggerInformation[triggerInfoCount];
                    (void)pointerEntry->SetDupObject(triggerInfo, triggerInfoCount, file, line);
                }
                if (!result || triggerInfo == nullptr)
                {
                    if (triggerInfoCount > 0)
                    {
                        PARAMETER_LOG_ERROR(entry->SetManager, "Failed to allocate triggerInfo\n");
                    }
                    triggerInfoCount = 0;
                }
                for (UINT32 i = 0; result && i < triggerInfoCount; i++)
                {
                    result = triggerInfo[i].Load(pointerEntry, alignment);
                }
            }
        }
        if (triggerInfo == nullptr)
        {
            triggerInfoCount = 0;
        }
    }

    return result;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Load FlashRegAddrInformation
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool FlashRegAddrInformation::Load(
    ParameterFileSymbolTableEntry* const entry,
    BYTE const alignment)
{
    bool result = true;

    PARAMETER_INITIALIZE(brightnessFlashLED1);
    PARAMETER_INITIALIZE(brightnessFlashLED2);
    PARAMETER_INITIALIZE(brightnessTorchLED1);
    PARAMETER_INITIALIZE(brightnessTorchLED2);

    SYMBOL_TABLE_ENTRY_READ(entry, result, brightnessFlashLED1, alignment)
    SYMBOL_TABLE_ENTRY_READ(entry, result, brightnessFlashLED2, alignment)
    SYMBOL_TABLE_ENTRY_READ(entry, result, brightnessTorchLED1, alignment)
    SYMBOL_TABLE_ENTRY_READ(entry, result, brightnessTorchLED2, alignment)

    return result;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Load FlashDriverData
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool FlashDriverData::Load(
    ParameterFileSymbolTableEntry* const entry,
    BYTE const alignment)
{
    bool result = true;

    PARAMETER_INITIALIZE(module_version);
    PARAMETER_INITIALIZE(flashName);
    PARAMETER_INITIALIZE(flashDriverType);
    PARAMETER_INITIALIZE(powerUpSequenceExists);
    PARAMETER_INITIALIZE(powerUpSequence);
    PARAMETER_INITIALIZE(powerDownSequenceExists);
    PARAMETER_INITIALIZE(powerDownSequence);
    PARAMETER_INITIALIZE(regAddrInfoFlashExists);
    PARAMETER_INITIALIZE(regAddrInfoFlash);
    PARAMETER_INITIALIZE(i2cInfoExists);
    PARAMETER_INITIALIZE(i2cInfo);
    PARAMETER_INITIALIZE(flashInformationExists);
    PARAMETER_INITIALIZE(flashInformation);
    PARAMETER_INITIALIZE(numberOfFlashs);

    result = module_version.Load(entry, alignment);

    // Reading String flashName from symbol table
    {
        UINT32 count = 0;
        SYMBOL_TABLE_ENTRY_READ(entry, result, count, alignment)
        ParameterFileSymbolTableEntry stringPointerEntryCls;
        ParameterFileSymbolTableEntry* const stringPointerEntry = &stringPointerEntryCls;
        if (result)
        {
            result = entry->Table->ReadPointerEntry(stringPointerEntryCls, entry, alignment);
        }
        SYMBOL_TABLE_ENTRY_READ_STRING(stringPointerEntry, result, flashName, count, alignment)
    }
    if (result)
    {
        result = ReadEnum_FlashDriverType(entry, flashDriverType, alignment);
    }

    // Reading Optional parameter powerUpSequence from symbol table
    {
        SYMBOL_TABLE_ENTRY_READ(entry, result, powerUpSequenceExists, alignment)
        powerUpSequenceID = entry->ID;
        ParameterFileSymbolTableEntry  pointerEntryCls;
        ParameterFileSymbolTableEntry* const pointerEntry = &pointerEntryCls;
        if (result)
        {
            result = entry->Table->ReadPointerEntry(pointerEntryCls, entry, alignment);
        }
        if (result && powerUpSequenceExists)
        {
            if (result)
            {
                result = powerUpSequence.Load(pointerEntry, alignment);
            }
        }
        else
        {
            PARAMETER_INITIALIZE(powerUpSequence);
        }
    }

    // Reading Optional parameter powerDownSequence from symbol table
    {
        SYMBOL_TABLE_ENTRY_READ(entry, result, powerDownSequenceExists, alignment)
        powerDownSequenceID = entry->ID;
        ParameterFileSymbolTableEntry  pointerEntryCls;
        ParameterFileSymbolTableEntry* const pointerEntry = &pointerEntryCls;
        if (result)
        {
            result = entry->Table->ReadPointerEntry(pointerEntryCls, entry, alignment);
        }
        if (result && powerDownSequenceExists)
        {
            if (result)
            {
                result = powerDownSequence.Load(pointerEntry, alignment);
            }
        }
        else
        {
            PARAMETER_INITIALIZE(powerDownSequence);
        }
    }

    // Reading Optional parameter regAddrInfoFlash from symbol table
    {
        SYMBOL_TABLE_ENTRY_READ(entry, result, regAddrInfoFlashExists, alignment)
        regAddrInfoFlashID = entry->ID;
        ParameterFileSymbolTableEntry  pointerEntryCls;
        ParameterFileSymbolTableEntry* const pointerEntry = &pointerEntryCls;
        if (result)
        {
            result = entry->Table->ReadPointerEntry(pointerEntryCls, entry, alignment);
        }
        if (result && regAddrInfoFlashExists)
        {
            if (result)
            {
                result = regAddrInfoFlash.Load(pointerEntry, alignment);
            }
        }
        else
        {
            PARAMETER_INITIALIZE(regAddrInfoFlash);
        }
    }

    // Reading Optional parameter i2cInfo from symbol table
    {
        SYMBOL_TABLE_ENTRY_READ(entry, result, i2cInfoExists, alignment)
        i2cInfoID = entry->ID;
        ParameterFileSymbolTableEntry  pointerEntryCls;
        ParameterFileSymbolTableEntry* const pointerEntry = &pointerEntryCls;
        if (result)
        {
            result = entry->Table->ReadPointerEntry(pointerEntryCls, entry, alignment);
        }
        if (result && i2cInfoExists)
        {
            if (result)
            {
                result = i2cInfo.Load(pointerEntry, alignment);
            }
        }
        else
        {
            PARAMETER_INITIALIZE(i2cInfo);
        }
    }

    // Reading Optional parameter flashInformation from symbol table
    {
        SYMBOL_TABLE_ENTRY_READ(entry, result, flashInformationExists, alignment)
        flashInformationID = entry->ID;
        ParameterFileSymbolTableEntry  pointerEntryCls;
        ParameterFileSymbolTableEntry* const pointerEntry = &pointerEntryCls;
        if (result)
        {
            result = entry->Table->ReadPointerEntry(pointerEntryCls, entry, alignment);
        }
        if (result && flashInformationExists)
        {
            if (result)
            {
                result = flashInformation.Load(pointerEntry, alignment);
            }
        }
        else
        {
            PARAMETER_INITIALIZE(flashInformation);
        }
    }
    SYMBOL_TABLE_ENTRY_READ(entry, result, numberOfFlashs, alignment)

    return result;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Load module_versionStruct
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool FlashDriverData::module_versionStruct::Load(
    ParameterFileSymbolTableEntry* const entry,
    BYTE const alignment)
{
    bool result = true;

    PARAMETER_INITIALIZE(major_revision);
    PARAMETER_INITIALIZE(minor_revision);
    PARAMETER_INITIALIZE(incr_revision);

    SYMBOL_TABLE_ENTRY_READ(entry, result, major_revision, alignment)
    SYMBOL_TABLE_ENTRY_READ(entry, result, minor_revision, alignment)
    SYMBOL_TABLE_ENTRY_READ(entry, result, incr_revision, alignment)

    return result;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Unload FlashI2CInformation
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FlashI2CInformation::Unload(ParameterFileSymbolTable* const __table)
{
    flashInitSettings.Unload(__table);
    flashOffSettings.Unload(__table);
    flashLowSettings.Unload(__table);
    flashHighSettings.Unload(__table);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Unload FlashInformation
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FlashInformation::Unload(ParameterFileSymbolTable* const __table)
{
    if (__table != nullptr && __table->ReleaseDupObject(triggerInfo))
    {
        PARAMETER_DELETE[] triggerInfo;
    }
    triggerInfo = nullptr;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Unload FlashDriverData
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FlashDriverData::Unload(ParameterFileSymbolTable* const __table)
{
    if (__table != nullptr && __table->ReleaseDupObject(flashName))
    {
        PARAMETER_DELETE[] flashName;
    }
    flashName = nullptr;
    powerUpSequence.Unload(__table);
    powerDownSequence.Unload(__table);
    i2cInfo.Unload(__table);
    flashInformation.Unload(__table);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Release the ParameterModule object
/// Requires that this object was created via a ParameterSetManager::GetModule call
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FlashDriverData::Release()
{
    ((FlashDriverDataClass*)this)->Release();
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Returns the ParameterModule::Modified value
/// The Modified value is incremented when the module's data or pointer values change
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
UINT32 FlashDriverData::GetModified()
{
    return ((FlashDriverDataClass*)this)->Modified;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Create module, and return a new object
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ParameterModule* FlashDriverDataClass::Create(
    ModuleData*    moduleData,
    DefaultModule* const defModule) const
{
    FlashDriverDataClass* cls = nullptr;

    if (PARAMETER_STRCMP(Type, moduleData->Type) == 0 && Version.Value == moduleData->Version.Value)
    {
        const UINT32 id = moduleData->DuplicateID > 0 ? moduleData->DuplicateID : moduleData->ID;

        ParameterModule* const module = defModule->Find(id);
        if (module != nullptr)
        {
            cls = (FlashDriverDataClass*)module;
        }
        else
        {
            cls = PARAMETER_NEW FlashDriverDataClass(GetName(moduleData->Type), moduleData);
            cls->SymbolTableID = id;
            defModule->AddModule((ParameterModule*)cls);
        }
    }
    else if (Version.Value != moduleData->Version.Value)
    {
        PARAMETER_LOG_ERROR(moduleData->SetManager, "Unable to create FlashDriverDataClass, binary version (%lld) does not match source version (%lld)\n",
            moduleData->Version.Value, Version.Value);
    }
    else
    {
        PARAMETER_LOG_ERROR(moduleData->SetManager, "Unable to create FlashDriverDataClass, binary name (%s) does not match source name (%s)\n",
            moduleData->Type, Type);
    }

    return (ParameterModule*)cls;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Load module data, return true is successful
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool FlashDriverDataClass::LoadModule(
    ParameterFileSymbolTable* const table)
{
    bool result = true;

    if (!Loaded)
    {
        BYTE const alignment = table->Alignment;
        FlashDriverDataClass* const cls = this;

        ParameterFileSymbolTableEntry  entryObj;
        ParameterFileSymbolTableEntry* const entry = &entryObj;
        result = table->Find(entryObj, SymbolTableID);

        if (!result)
        {
            PARAMETER_LOG_ERROR(entry->SetManager, "FlashDriverDataClass::LoadModule Failed to find module ID (%d) in Symbol Table\n", SymbolTableID);
        }

        if (result)
        {
            result = cls->Load(entry, alignment);
        }
        Modified++;
        Loaded = true;
    }

    if (!result)
    {
        /// Module failed to fully load, unload it
        UnloadModule();
    }

    return result;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Unload module data
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FlashDriverDataClass::UnloadModule()
{
    if (Loaded)
    {
        if (Data != nullptr && Data->SymbolTable != nullptr)
        {
            Unload(Data->SymbolTable);
        }
        else
        {
            PARAMETER_LOG_ERROR(Data != nullptr ? Data->SetManager : nullptr, "UnloadModule %s called with nullptr "
                "Data->SymbolTable object\n", Name);
            Unload(nullptr);
        }
        Loaded = false;
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// /// Release the ParameterModule object
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FlashDriverDataClass::Release()
{
    ParameterModule::Release();
}

CAMX_NAMESPACE_END
