#include "objectFactory.h"

#include <fsfw/devicehandlers/HealthDevice.h>
#include <fsfw/subsystem/Subsystem.h>
#include <fsfw/tasks/TaskFactory.h>
#include <fsfw_hal/linux/uio/UioMapper.h>
#include <linux/acs/AcsBoardPolling.h>
#include <linux/acs/GpsHyperionLinuxController.h>
#include <linux/acs/ImtqPollingTask.h>
#include <linux/acs/RwPollingTask.h>
#include <linux/acs/StrComHandler.h>
#include <linux/com/SyrlinksComHandler.h>
#include <linux/payload/PlocMemoryDumper.h>
#include <linux/payload/PlocMpsocHandler.h>
#include <linux/payload/PlocMpsocSpecialComHelper.h>
#include <linux/payload/ScexUartReader.h>
#include <linux/payload/plocMpsocHelpers.h>
#include <linux/power/CspComIF.h>
#include <mission/acs/GyrL3gCustomHandler.h>
#include <mission/acs/MgmLis3CustomHandler.h>
#include <mission/acs/MgmRm3100CustomHandler.h>
#include <mission/acs/str/StarTrackerHandler.h>
#include <mission/acs/str/strHelpers.h>
#include <mission/com/LiveTmTask.h>
#include <mission/com/PersistentLogTmStoreTask.h>
#include <mission/com/PersistentSingleTmStoreTask.h>
#include <mission/power/CspCookie.h>
#include <mission/system/acs/ImtqAssembly.h>
#include <mission/system/acs/StrAssembly.h>
#include <mission/system/acs/StrFdir.h>
#include <mission/system/com/SyrlinksAssembly.h>
#include <mission/system/objects/CamSwitcher.h>
#include <mission/system/tcs/TmpDevFdir.h>

#include <cstdint>
#include <cstring>

#include "OBSWConfig.h"
#include "bsp_q7s/acs/StrConfigPathGetter.h"
#include "bsp_q7s/boardtest/Q7STestTask.h"
#include "bsp_q7s/callbacks/gnssCallback.h"
#include "bsp_q7s/callbacks/q7sGpioCallbacks.h"
#include "busConf.h"
#include "ccsdsConfig.h"
#include "devConf.h"
#include "devices/addresses.h"
#include "devices/gpioIds.h"
#include "devices/powerSwitcherList.h"
#include "eive/definitions.h"
#include "fsfw/ipc/QueueFactory.h"
#include "linux/ObjectFactory.h"
#include "linux/boardtest/I2cTestClass.h"
#include "linux/boardtest/SpiTestClass.h"
#include "linux/boardtest/UartTestClass.h"
#include "linux/callbacks/gpioCallbacks.h"
#include "linux/ipcore/AxiPtmeConfig.h"
#include "linux/ipcore/PapbVcInterface.h"
#include "linux/ipcore/PdecHandler.h"
#include "linux/ipcore/Ptme.h"
#include "linux/ipcore/PtmeConfig.h"
#include "linux/payload/FreshSupvHandler.h"
#include "mission/config/configfile.h"
#include "mission/system/acs/AcsBoardFdir.h"
#include "mission/system/acs/AcsSubsystem.h"
#include "mission/system/acs/RwAssembly.h"
#include "mission/system/acs/SusFdir.h"
#include "mission/system/acs/acsModeTree.h"
#include "mission/system/com/SyrlinksFdir.h"
#include "mission/system/com/comModeTree.h"
#include "mission/system/payloadModeTree.h"
#include "mission/system/power/GomspacePowerFdir.h"
#include "mission/system/tcs/RtdFdir.h"
#include "mission/system/tcs/TcsBoardAssembly.h"
#include "mission/system/tcs/tcsModeTree.h"
#include "mission/tmtc/tmFilters.h"
#include "mission/utility/GlobalConfigHandler.h"
#include "tmtc/pusIds.h"

#if OBSW_TEST_LIBGPIOD == 1
#include "linux/boardtest/LibgpiodTest.h"
#endif
#include <mission/SolarArrayDeploymentHandler.h>
#include <mission/acs/GyrAdis1650XHandler.h>
#include <mission/acs/ImtqHandler.h>
#include <mission/acs/rwHelpers.h>
#include <mission/com/SyrlinksHandler.h>
#include <mission/com/VirtualChannelWithQueue.h>
#include <mission/payload/PayloadPcduHandler.h>
#include <mission/payload/RadiationSensorHandler.h>
#include <mission/payload/payloadPcduDefinitions.h>
#include <mission/payload/radSensorDefinitions.h>
#include <mission/power/AcuHandler.h>
#include <mission/power/BpxBatteryHandler.h>
#include <mission/power/P60DockHandler.h>
#include <mission/power/PcduHandler.h>
#include <mission/power/Pdu1Handler.h>
#include <mission/power/Pdu2Handler.h>
#include <mission/power/gsDefs.h>
#include <mission/tcs/HeaterHandler.h>
#include <mission/tcs/Max31865Definitions.h>
#include <mission/tcs/Max31865PT1000Handler.h>
#include <mission/tcs/Tmp1075Handler.h>

#include <sstream>

#include "bsp_q7s/core/defs.h"
#include "fsfw/datapoollocal/LocalDataPoolManager.h"
#include "fsfw/tmtcpacket/pus/tm.h"
#include "fsfw/tmtcservices/CommandingServiceBase.h"
#include "fsfw/tmtcservices/PusServiceBase.h"
#include "fsfw_hal/common/gpio/GpioCookie.h"
#include "fsfw_hal/common/gpio/gpioDefinitions.h"
#include "fsfw_hal/devicehandlers/GyroL3GD20Handler.h"
#include "fsfw_hal/devicehandlers/MgmLIS3MDLHandler.h"
#include "fsfw_hal/devicehandlers/MgmRM3100Handler.h"
#include "fsfw_hal/linux/gpio/LinuxLibgpioIF.h"
#include "fsfw_hal/linux/i2c/I2cComIF.h"
#include "fsfw_hal/linux/i2c/I2cCookie.h"
#include "fsfw_hal/linux/serial/SerialComIF.h"
#include "fsfw_hal/linux/serial/SerialCookie.h"
#include "fsfw_hal/linux/spi/SpiComIF.h"
#include "fsfw_hal/linux/spi/SpiCookie.h"
#include "mission/acs/RwHandler.h"
#include "mission/com/CcsdsIpCoreHandler.h"
#include "mission/com/syrlinksDefs.h"
#include "mission/genericFactory.h"
#include "mission/system/acs/AcsBoardAssembly.h"
#include "mission/tmtc/TmFunnelHandler.h"

using gpio::Direction;
using gpio::Levels;

ResetArgs RESET_ARGS_GNSS;
std::atomic_bool LINK_STATE = CcsdsIpCoreHandler::LINK_DOWN;
std::atomic_bool PTME_LOCKED = false;
std::atomic_uint16_t signals::I2C_FATAL_ERRORS = 0;
uint8_t core::FW_VERSION_MAJOR = 0;
uint8_t core::FW_VERSION_MINOR = 0;
uint8_t core::FW_VERSION_REVISION = 0;
bool core::FW_VERSION_HAS_SHA = false;
char core::FW_VERSION_GIT_SHA[4] = {};

void Factory::setStaticFrameworkObjectIds() {
  PusServiceBase::PUS_DISTRIBUTOR = objects::PUS_PACKET_DISTRIBUTOR;
  PusServiceBase::PACKET_DESTINATION = objects::PUS_TM_FUNNEL;

  CommandingServiceBase::defaultPacketSource = objects::PUS_PACKET_DISTRIBUTOR;
  CommandingServiceBase::defaultPacketDestination = objects::PUS_TM_FUNNEL;

#if OBSW_Q7S_EM == 1
  DeviceHandlerBase::powerSwitcherId = objects::NO_OBJECT;
#else
  DeviceHandlerBase::powerSwitcherId = objects::PCDU_HANDLER;
#endif /* OBSW_Q7S_EM == 1 */

  LocalDataPoolManager::defaultHkDestination = objects::PUS_SERVICE_3_HOUSEKEEPING;

  VerificationReporter::DEFAULT_RECEIVER = objects::PUS_SERVICE_1_VERIFICATION;
}

void ObjectFactory::setStatics() { Factory::setStaticFrameworkObjectIds(); }

void ObjectFactory::createTmpComponents(
    std::vector<std::pair<object_id_t, address_t>> tmpDevsToAdd) {
  const char* tmpI2cDev = q7s::I2C_PS_EIVE;
  if (core::FW_VERSION_MAJOR == 4) {
    tmpI2cDev = q7s::I2C_PL_EIVE;
  } else if (core::FW_VERSION_MAJOR >= 5) {
    tmpI2cDev = q7s::I2C_Q7_EIVE;
  }
  std::vector<I2cCookie*> tmpDevCookies;

  for (size_t idx = 0; idx < tmpDevsToAdd.size(); idx++) {
    tmpDevCookies.push_back(
        new I2cCookie(tmpDevsToAdd[idx].second, TMP1075::MAX_REPLY_LENGTH, tmpI2cDev));
    auto* tmpDevHandler =
        new Tmp1075Handler(tmpDevsToAdd[idx].first, objects::I2C_COM_IF, tmpDevCookies[idx]);
    tmpDevHandler->setCustomFdir(new TmpDevFdir(tmpDevsToAdd[idx].first));
    tmpDevHandler->connectModeTreeParent(satsystem::tcs::SUBSYSTEM);
  }
}

void ObjectFactory::createCommunicationInterfaces(LinuxLibgpioIF** gpioComIF,
                                                  SerialComIF** uartComIF, SpiComIF** spiMainComIF,
                                                  I2cComIF** i2cComIF) {
  if (gpioComIF == nullptr or uartComIF == nullptr or spiMainComIF == nullptr) {
    sif::error << "ObjectFactory::createCommunicationInterfaces: Invalid passed ComIF pointer"
               << std::endl;
  }
  *gpioComIF = new LinuxLibgpioIF(objects::GPIO_IF);

  /* Communication interfaces */
  new CspComIF(objects::CSP_COM_IF, "CSP_ROUTER", 60);
  *i2cComIF = new I2cComIF(objects::I2C_COM_IF);
  *uartComIF = new SerialComIF(objects::UART_COM_IF);
  *spiMainComIF = new SpiComIF(objects::SPI_MAIN_COM_IF, q7s::SPI_DEFAULT_DEV, **gpioComIF);
}

void ObjectFactory::createPcduComponents(LinuxLibgpioIF* gpioComIF, PowerSwitchIF** pwrSwitcher,
                                         bool enableHkSets) {
  CspCookie* p60DockCspCookie = new CspCookie(P60Dock::MAX_REPLY_SIZE, addresses::P60DOCK, 500);
  CspCookie* pdu1CspCookie = new CspCookie(PDU::MAX_REPLY_SIZE, addresses::PDU1, 500);
  CspCookie* pdu2CspCookie = new CspCookie(PDU::MAX_REPLY_SIZE, addresses::PDU2, 500);

  auto p60Fdir = new GomspacePowerFdir(objects::P60DOCK_HANDLER);
  P60DockHandler* p60dockhandler = new P60DockHandler(objects::P60DOCK_HANDLER, objects::CSP_COM_IF,
                                                      p60DockCspCookie, p60Fdir, enableHkSets);

  auto pdu1Fdir = new GomspacePowerFdir(objects::PDU1_HANDLER);
  Pdu1Handler* pdu1handler = new Pdu1Handler(objects::PDU1_HANDLER, objects::CSP_COM_IF,
                                             pdu1CspCookie, pdu1Fdir, enableHkSets);

  auto pdu2Fdir = new GomspacePowerFdir(objects::PDU2_HANDLER);
  Pdu2Handler* pdu2handler = new Pdu2Handler(objects::PDU2_HANDLER, objects::CSP_COM_IF,
                                             pdu2CspCookie, pdu2Fdir, enableHkSets);

#if OBSW_ADD_GOMSPACE_ACU == 1
  CspCookie* acuCspCookie = new CspCookie(ACU::MAX_REPLY_SIZE, addresses::ACU, 500);
  auto acuFdir = new GomspacePowerFdir(objects::ACU_HANDLER);
  ACUHandler* acuhandler = new ACUHandler(objects::ACU_HANDLER, objects::CSP_COM_IF, acuCspCookie,
                                          acuFdir, enableHkSets);
#endif
  auto pcduHandler = new PcduHandler(objects::PCDU_HANDLER, 50);

  /**
   * Setting PCDU devices to mode normal immediately after start up because PCDU is always
   * running.
   */
  p60dockhandler->setModeNormal();
  pdu1handler->setModeNormal();
  pdu2handler->setModeNormal();
#if OBSW_ADD_GOMSPACE_ACU == 1
  acuhandler->setModeNormal();
#endif
  if (pwrSwitcher != nullptr) {
    *pwrSwitcher = pcduHandler;
  }
#if OBSW_DEBUG_P60DOCK == 1
  p60dockhandler->setDebugMode(true);
#endif
#if OBSW_DEBUG_ACU == 1
  acuhandler->setDebugMode(true);
#endif
}

ReturnValue_t ObjectFactory::createRadSensorComponent(LinuxLibgpioIF* gpioComIF,
                                                      Stack5VHandler& stackHandler) {
  createRadSensorChipSelect(gpioComIF);

  SpiCookie* spiCookieRadSensor =
      new SpiCookie(addresses::RAD_SENSOR, gpioIds::CS_RAD_SENSOR, radSens::READ_SIZE,
                    spi::DEFAULT_MAX_1227_MODE, spi::DEFAULT_MAX_1227_SPEED);
  spiCookieRadSensor->setMutexParams(MutexIF::TimeoutType::WAITING, spi::RAD_SENSOR_CS_TIMEOUT);
  auto radSensor = new RadiationSensorHandler(objects::RAD_SENSOR, objects::SPI_MAIN_COM_IF,
                                              spiCookieRadSensor, gpioComIF, stackHandler);
  static_cast<void>(radSensor);
#if OBSW_DEBUG_RAD_SENSOR == 1
  radSensor->enablePeriodicDataPrint(true);
#endif
  return returnvalue::OK;
}

void ObjectFactory::createAcsBoardGpios(GpioCookie& cookie) {
  std::stringstream consumer;
  GpiodRegularByLineName* gpio = nullptr;
  consumer << "0x" << std::hex << objects::GYRO_0_ADIS_HANDLER;
  gpio = new GpiodRegularByLineName(q7s::gpioNames::GYRO_0_ADIS_CS, consumer.str(), Direction::OUT,
                                    Levels::HIGH);
  cookie.addGpio(gpioIds::GYRO_0_ADIS_CS, gpio);

  consumer.str("");
  consumer << "0x" << std::hex << objects::GYRO_1_L3G_HANDLER;
  gpio = new GpiodRegularByLineName(q7s::gpioNames::GYRO_1_L3G_CS, consumer.str(), Direction::OUT,
                                    Levels::HIGH);
  cookie.addGpio(gpioIds::GYRO_1_L3G_CS, gpio);

  consumer.str("");
  consumer << "0x" << std::hex << objects::GYRO_2_ADIS_HANDLER;
  gpio = new GpiodRegularByLineName(q7s::gpioNames::GYRO_2_ADIS_CS, consumer.str(), Direction::OUT,
                                    Levels::HIGH);
  cookie.addGpio(gpioIds::GYRO_2_ADIS_CS, gpio);

  consumer.str("");
  consumer << "0x" << std::hex << objects::GYRO_3_L3G_HANDLER;
  gpio = new GpiodRegularByLineName(q7s::gpioNames::GYRO_3_L3G_CS, consumer.str(), Direction::OUT,
                                    Levels::HIGH);
  cookie.addGpio(gpioIds::GYRO_3_L3G_CS, gpio);

  consumer.str("");
  consumer << "0x" << std::hex << objects::MGM_0_LIS3_HANDLER;
  gpio = new GpiodRegularByLineName(q7s::gpioNames::MGM_0_CS, consumer.str(), Direction::OUT,
                                    Levels::HIGH);
  cookie.addGpio(gpioIds::MGM_0_LIS3_CS, gpio);

  consumer.str("");
  consumer << "0x" << std::hex << objects::MGM_1_RM3100_HANDLER;
  gpio = new GpiodRegularByLineName(q7s::gpioNames::MGM_1_CS, consumer.str(), Direction::OUT,
                                    Levels::HIGH);
  cookie.addGpio(gpioIds::MGM_1_RM3100_CS, gpio);

  consumer.str("");
  consumer << "0x" << std::hex << objects::MGM_2_LIS3_HANDLER;
  gpio = new GpiodRegularByLineName(q7s::gpioNames::MGM_2_CS, consumer.str(), Direction::OUT,
                                    Levels::HIGH);
  cookie.addGpio(gpioIds::MGM_2_LIS3_CS, gpio);

  consumer.str("");
  consumer << "0x" << std::hex << objects::MGM_3_RM3100_HANDLER;
  gpio = new GpiodRegularByLineName(q7s::gpioNames::MGM_3_CS, consumer.str(), Direction::OUT,
                                    Levels::HIGH);
  cookie.addGpio(gpioIds::MGM_3_RM3100_CS, gpio);

  consumer.str("");
  consumer << "0x" << std::hex << objects::GPS_CONTROLLER;
  // GNSS reset pins are active low
  gpio = new GpiodRegularByLineName(q7s::gpioNames::RESET_GNSS_0, consumer.str(), Direction::OUT,
                                    Levels::HIGH);
  cookie.addGpio(gpioIds::GNSS_0_NRESET, gpio);

  consumer.str("");
  consumer << "0x" << std::hex << objects::GPS_CONTROLLER;
  gpio = new GpiodRegularByLineName(q7s::gpioNames::RESET_GNSS_1, consumer.str(), Direction::OUT,
                                    Levels::HIGH);
  cookie.addGpio(gpioIds::GNSS_1_NRESET, gpio);

  consumer.str("");
  consumer << "0x" << std::hex << objects::GYRO_0_ADIS_HANDLER;
  // Enable pins must be pulled low for regular operations
  gpio = new GpiodRegularByLineName(q7s::gpioNames::GYRO_0_ENABLE, consumer.str(), Direction::OUT,
                                    Levels::LOW);
  cookie.addGpio(gpioIds::GYRO_0_ENABLE, gpio);

  consumer.str("");
  consumer << "0x" << std::hex << objects::GYRO_2_ADIS_HANDLER;
  gpio = new GpiodRegularByLineName(q7s::gpioNames::GYRO_2_ENABLE, consumer.str(), Direction::OUT,
                                    Levels::LOW);
  cookie.addGpio(gpioIds::GYRO_2_ENABLE, gpio);

  // Enable pins for GNSS
  consumer.str("");
  consumer << "0x" << std::hex << objects::GPS_CONTROLLER;
  gpio = new GpiodRegularByLineName(q7s::gpioNames::GNSS_0_ENABLE, consumer.str(), Direction::OUT,
                                    Levels::LOW);
  cookie.addGpio(gpioIds::GNSS_0_ENABLE, gpio);

  consumer.str("");
  consumer << "0x" << std::hex << objects::GPS_CONTROLLER;
  gpio = new GpiodRegularByLineName(q7s::gpioNames::GNSS_1_ENABLE, consumer.str(), Direction::OUT,
                                    Levels::LOW);
  cookie.addGpio(gpioIds::GNSS_1_ENABLE, gpio);

  // Select pin. 0 for GPS side A, 1 for GPS side B
  consumer.str("");
  consumer << "0x" << std::hex << objects::GPS_CONTROLLER;
  gpio = new GpiodRegularByLineName(q7s::gpioNames::GNSS_SELECT, consumer.str(), Direction::OUT,
                                    Levels::LOW);
  cookie.addGpio(gpioIds::GNSS_SELECT, gpio);
}

void ObjectFactory::createAcsBoardComponents(SpiComIF& spiComIF, LinuxLibgpioIF* gpioComIF,
                                             SerialComIF* uartComIF, PowerSwitchIF& pwrSwitcher,
                                             bool enableHkSets, adis1650x::Type adisType) {
  using namespace gpio;
  GpioCookie* gpioCookieAcsBoard = new GpioCookie();
  createAcsBoardGpios(*gpioCookieAcsBoard);
  gpioChecker(gpioComIF->addGpios(gpioCookieAcsBoard), "ACS Board");

  AcsBoardFdir* fdir = nullptr;
  static_cast<void>(fdir);

#if OBSW_ADD_ACS_BOARD == 1
  new AcsBoardPolling(objects::ACS_BOARD_POLLING_TASK, spiComIF, *gpioComIF);
  std::string spiDev = q7s::SPI_DEFAULT_DEV;
  std::array<DeviceHandlerBase*, 8> assemblyChildren;
  SpiCookie* spiCookie =
      new SpiCookie(addresses::MGM_0_LIS3, gpioIds::MGM_0_LIS3_CS, mgmLis3::MAX_BUFFER_SIZE,
                    spi::DEFAULT_LIS3_MODE, spi::DEFAULT_LIS3_SPEED);
  spiCookie->setMutexParams(MutexIF::TimeoutType::WAITING, spi::ACS_BOARD_CS_TIMEOUT);
  auto mgmLis3Handler0 =
      new MgmLis3CustomHandler(objects::MGM_0_LIS3_HANDLER, objects::ACS_BOARD_POLLING_TASK,
                               spiCookie, spi::LIS3_TRANSITION_DELAY);
  fdir = new AcsBoardFdir(objects::MGM_0_LIS3_HANDLER);
  mgmLis3Handler0->setCustomFdir(fdir);
  assemblyChildren[0] = mgmLis3Handler0;
#if OBSW_TEST_ACS == 1
  mgmLis3Handler->setStartUpImmediately();
  mgmLis3Handler->setToGoToNormalMode(true);
#endif
#if OBSW_DEBUG_ACS == 1
  mgmLis3Handler->enablePeriodicPrintouts(true, 10);
#endif
  spiCookie =
      new SpiCookie(addresses::MGM_1_RM3100, gpioIds::MGM_1_RM3100_CS, mgmRm3100::MAX_BUFFER_SIZE,
                    spi::DEFAULT_RM3100_MODE, spi::DEFAULT_RM3100_SPEED);
  spiCookie->setMutexParams(MutexIF::TimeoutType::WAITING, spi::ACS_BOARD_CS_TIMEOUT);
  auto mgmRm3100Handler1 =
      new MgmRm3100CustomHandler(objects::MGM_1_RM3100_HANDLER, objects::ACS_BOARD_POLLING_TASK,
                                 spiCookie, spi::RM3100_TRANSITION_DELAY);
  fdir = new AcsBoardFdir(objects::MGM_1_RM3100_HANDLER);
  mgmRm3100Handler1->setCustomFdir(fdir);
  assemblyChildren[1] = mgmRm3100Handler1;
#if OBSW_TEST_ACS == 1
  mgmRm3100Handler->setStartUpImmediately();
  mgmRm3100Handler->setToGoToNormalMode(true);
#endif
#if OBSW_DEBUG_ACS == 1
  mgmRm3100Handler->enablePeriodicPrintouts(true, 10);
#endif
  spiCookie = new SpiCookie(addresses::MGM_2_LIS3, gpioIds::MGM_2_LIS3_CS, mgmLis3::MAX_BUFFER_SIZE,
                            spi::DEFAULT_LIS3_MODE, spi::DEFAULT_LIS3_SPEED);
  spiCookie->setMutexParams(MutexIF::TimeoutType::WAITING, spi::ACS_BOARD_CS_TIMEOUT);
  auto* mgmLis3Handler2 =
      new MgmLis3CustomHandler(objects::MGM_2_LIS3_HANDLER, objects::ACS_BOARD_POLLING_TASK,
                               spiCookie, spi::LIS3_TRANSITION_DELAY);
  fdir = new AcsBoardFdir(objects::MGM_2_LIS3_HANDLER);
  mgmLis3Handler2->setCustomFdir(fdir);
  assemblyChildren[2] = mgmLis3Handler2;
#if OBSW_TEST_ACS == 1
  mgmLis3Handler->setStartUpImmediately();
  mgmLis3Handler->setToGoToNormalMode(true);
#endif
#if OBSW_DEBUG_ACS == 1
  mgmLis3Handler->enablePeriodicPrintouts(true, 10);
#endif
  spiCookie =
      new SpiCookie(addresses::MGM_3_RM3100, gpioIds::MGM_3_RM3100_CS, mgmRm3100::MAX_BUFFER_SIZE,
                    spi::DEFAULT_RM3100_MODE, spi::DEFAULT_RM3100_SPEED);
  spiCookie->setMutexParams(MutexIF::TimeoutType::WAITING, spi::ACS_BOARD_CS_TIMEOUT);
  auto* mgmRm3100Handler3 =
      new MgmRm3100CustomHandler(objects::MGM_3_RM3100_HANDLER, objects::ACS_BOARD_POLLING_TASK,
                                 spiCookie, spi::RM3100_TRANSITION_DELAY);
  fdir = new AcsBoardFdir(objects::MGM_3_RM3100_HANDLER);
  mgmRm3100Handler3->setCustomFdir(fdir);
  assemblyChildren[3] = mgmRm3100Handler3;
#if OBSW_TEST_ACS == 1
  mgmRm3100Handler->setStartUpImmediately();
  mgmRm3100Handler->setToGoToNormalMode(true);
#endif
#if OBSW_DEBUG_ACS == 1
  mgmRm3100Handler->enablePeriodicPrintouts(true, 10);
#endif
  // Commented until ACS board V2 in in clean room again
  // Gyro 0 Side A
  spiCookie =
      new SpiCookie(addresses::GYRO_0_ADIS, gpioIds::GYRO_0_ADIS_CS, adis1650x::MAXIMUM_REPLY_SIZE,
                    spi::DEFAULT_ADIS16507_MODE, spi::DEFAULT_ADIS16507_SPEED);
  spiCookie->setMutexParams(MutexIF::TimeoutType::WAITING, spi::ACS_BOARD_CS_TIMEOUT);
  auto adisHandler = new GyrAdis1650XHandler(objects::GYRO_0_ADIS_HANDLER,
                                             objects::ACS_BOARD_POLLING_TASK, spiCookie, adisType);
  fdir = new AcsBoardFdir(objects::GYRO_0_ADIS_HANDLER);
  adisHandler->setCustomFdir(fdir);
  assemblyChildren[4] = adisHandler;
#if OBSW_TEST_ACS == 1
  adisHandler->setStartUpImmediately();
  adisHandler->setToGoToNormalModeImmediately();
#endif
#if OBSW_DEBUG_ACS == 1
  adisHandler->enablePeriodicPrintouts(true, 10);
#endif
  // Gyro 1 Side A
  spiCookie = new SpiCookie(addresses::GYRO_1_L3G, gpioIds::GYRO_1_L3G_CS, l3gd20h::MAX_BUFFER_SIZE,
                            spi::DEFAULT_L3G_MODE, spi::DEFAULT_L3G_SPEED);
  spiCookie->setMutexParams(MutexIF::TimeoutType::WAITING, spi::ACS_BOARD_CS_TIMEOUT);
  auto gyroL3gHandler1 =
      new GyrL3gCustomHandler(objects::GYRO_1_L3G_HANDLER, objects::ACS_BOARD_POLLING_TASK,
                              spiCookie, spi::L3G_TRANSITION_DELAY);
  fdir = new AcsBoardFdir(objects::GYRO_1_L3G_HANDLER);
  gyroL3gHandler1->setCustomFdir(fdir);
  assemblyChildren[5] = gyroL3gHandler1;
#if OBSW_TEST_ACS == 1
  gyroL3gHandler->setStartUpImmediately();
  gyroL3gHandler->setToGoToNormalMode(true);
#endif
#if OBSW_DEBUG_ACS == 1
  gyroL3gHandler->enablePeriodicPrintouts(true, 10);
#endif
  // Gyro 2 Side B
  spiCookie =
      new SpiCookie(addresses::GYRO_2_ADIS, gpioIds::GYRO_2_ADIS_CS, adis1650x::MAXIMUM_REPLY_SIZE,
                    spi::DEFAULT_ADIS16507_MODE, spi::DEFAULT_ADIS16507_SPEED);
  spiCookie->setMutexParams(MutexIF::TimeoutType::WAITING, spi::ACS_BOARD_CS_TIMEOUT);
  adisHandler = new GyrAdis1650XHandler(objects::GYRO_2_ADIS_HANDLER,
                                        objects::ACS_BOARD_POLLING_TASK, spiCookie, adisType);
  fdir = new AcsBoardFdir(objects::GYRO_2_ADIS_HANDLER);
  adisHandler->setCustomFdir(fdir);
  assemblyChildren[6] = adisHandler;
#if OBSW_TEST_ACS == 1
  adisHandler->setStartUpImmediately();
  adisHandler->setToGoToNormalModeImmediately();
#endif
  // Gyro 3 Side B
  spiCookie = new SpiCookie(addresses::GYRO_3_L3G, gpioIds::GYRO_3_L3G_CS, l3gd20h::MAX_BUFFER_SIZE,
                            spi::DEFAULT_L3G_MODE, spi::DEFAULT_L3G_SPEED);
  spiCookie->setMutexParams(MutexIF::TimeoutType::WAITING, spi::ACS_BOARD_CS_TIMEOUT);
  auto gyroL3gHandler3 =
      new GyrL3gCustomHandler(objects::GYRO_3_L3G_HANDLER, objects::ACS_BOARD_POLLING_TASK,
                              spiCookie, spi::L3G_TRANSITION_DELAY);
  fdir = new AcsBoardFdir(objects::GYRO_3_L3G_HANDLER);
  gyroL3gHandler3->setCustomFdir(fdir);
  assemblyChildren[7] = gyroL3gHandler3;
#if OBSW_TEST_ACS == 1
  gyroL3gHandler->setStartUpImmediately();
  gyroL3gHandler->setToGoToNormalMode(true);
#endif
#if OBSW_DEBUG_ACS == 1
  gyroL3gHandler->enablePeriodicPrintouts(true, 10);
#endif
  bool debugGps = false;
#if OBSW_DEBUG_GPS == 1
  debugGps = true;
#endif
  RESET_ARGS_GNSS.gpioComIF = gpioComIF;
  RESET_ARGS_GNSS.waitPeriodMs = 5;
  auto gpsCtrl = new GpsHyperionLinuxController(objects::GPS_CONTROLLER, objects::NO_OBJECT,
                                                enableHkSets, debugGps);
  gpsCtrl->setResetPinTriggerFunction(gps::triggerGpioResetPin, &RESET_ARGS_GNSS);

  ObjectFactory::createAcsBoardAssy(pwrSwitcher, assemblyChildren, gpsCtrl, gpioComIF);
#endif /* OBSW_ADD_ACS_HANDLERS == 1 */
}

void ObjectFactory::createHeaterComponents(GpioIF* gpioIF, PowerSwitchIF* pwrSwitcher,
                                           HealthTableIF* healthTable,
                                           HeaterHandler*& heaterHandler) {
  using namespace gpio;
  GpioCookie* heaterGpiosCookie = new GpioCookie;
  GpiodRegularByLineName* gpio = nullptr;

  std::stringstream consumer;
  consumer << "0x" << std::hex << objects::HEATER_HANDLER;
  /* Pin H2-11 on stack connector */
  gpio = new GpiodRegularByLineName(q7s::gpioNames::HEATER_0, consumer.str(), Direction::OUT,
                                    Levels::LOW);
  heaterGpiosCookie->addGpio(gpioIds::HEATER_0, gpio);
  /* Pin H2-12 on stack connector */
  gpio = new GpiodRegularByLineName(q7s::gpioNames::HEATER_1, consumer.str(), Direction::OUT,
                                    Levels::LOW);
  heaterGpiosCookie->addGpio(gpioIds::HEATER_1, gpio);

  /* Pin H2-13 on stack connector */
  gpio = new GpiodRegularByLineName(q7s::gpioNames::HEATER_2, consumer.str(), Direction::OUT,
                                    Levels::LOW);
  heaterGpiosCookie->addGpio(gpioIds::HEATER_2, gpio);

  gpio = new GpiodRegularByLineName(q7s::gpioNames::HEATER_3, consumer.str(), Direction::OUT,
                                    Levels::LOW);
  heaterGpiosCookie->addGpio(gpioIds::HEATER_3, gpio);

  gpio = new GpiodRegularByLineName(q7s::gpioNames::HEATER_4, consumer.str(), Direction::OUT,
                                    Levels::LOW);
  heaterGpiosCookie->addGpio(gpioIds::HEATER_4, gpio);

  gpio = new GpiodRegularByLineName(q7s::gpioNames::HEATER_5, consumer.str(), Direction::OUT,
                                    Levels::LOW);
  heaterGpiosCookie->addGpio(gpioIds::HEATER_5, gpio);

  gpio = new GpiodRegularByLineName(q7s::gpioNames::HEATER_6, consumer.str(), Direction::OUT,
                                    Levels::LOW);
  heaterGpiosCookie->addGpio(gpioIds::HEATER_6, gpio);

  gpio = new GpiodRegularByLineName(q7s::gpioNames::HEATER_7, consumer.str(), Direction::OUT,
                                    Levels::LOW);
  heaterGpiosCookie->addGpio(gpioIds::HEATER_7, gpio);

  gpioIF->addGpios(heaterGpiosCookie);

  ObjectFactory::createGenericHeaterComponents(*gpioIF, *pwrSwitcher, heaterHandler);
}

void ObjectFactory::createSolarArrayDeploymentComponents(PowerSwitchIF& pwrSwitcher,
                                                         GpioIF& gpioIF) {
  using namespace gpio;
  GpioCookie* solarArrayDeplCookie = new GpioCookie;
  GpiodRegularByLineName* gpio = nullptr;

  std::stringstream consumer;
  consumer << "0x" << std::hex << objects::SOLAR_ARRAY_DEPL_HANDLER;
  gpio = new GpiodRegularByLineName(q7s::gpioNames::SA_DPL_PIN_0, consumer.str(), Direction::OUT,
                                    Levels::LOW);
  solarArrayDeplCookie->addGpio(gpioIds::DEPLSA1, gpio);
  gpio = new GpiodRegularByLineName(q7s::gpioNames::SA_DPL_PIN_1, consumer.str(), Direction::OUT,
                                    Levels::LOW);
  solarArrayDeplCookie->addGpio(gpioIds::DEPLSA2, gpio);
  ReturnValue_t result = gpioIF.addGpios(solarArrayDeplCookie);
  if (result != returnvalue::OK) {
    sif::error << "Adding Solar Array Deployment GPIO cookie failed" << std::endl;
  }

  new SolarArrayDeploymentHandler(objects::SOLAR_ARRAY_DEPL_HANDLER, gpioIF, pwrSwitcher,
                                  power::Switches::PDU2_CH5_DEPLOYMENT_MECHANISM_8V,
                                  gpioIds::DEPLSA1, gpioIds::DEPLSA2, *SdCardManager::instance());
}

void ObjectFactory::createSyrlinksComponents(PowerSwitchIF* pwrSwitcher) {
  auto* syrlinksUartCookie =
      new SerialCookie(objects::SYRLINKS_HANDLER, q7s::UART_SYRLINKS_DEV, serial::SYRLINKS_BAUD,
                       syrlinks::MAX_REPLY_SIZE, UartModes::NON_CANONICAL);
  syrlinksUartCookie->setParityEven();

  new SyrlinksComHandler(objects::SYRLINKS_COM_HANDLER);
  auto* syrlinksAssy = new SyrlinksAssembly(objects::SYRLINKS_ASSY);
  syrlinksAssy->connectModeTreeParent(satsystem::com::SUBSYSTEM);
  auto syrlinksFdir = new SyrlinksFdir(objects::SYRLINKS_HANDLER);
  auto syrlinksHandler =
      new SyrlinksHandler(objects::SYRLINKS_HANDLER, objects::SYRLINKS_COM_HANDLER,
                          syrlinksUartCookie, power::PDU1_CH1_SYRLINKS_12V, syrlinksFdir);
  syrlinksHandler->setPowerSwitcher(pwrSwitcher);
  syrlinksHandler->connectModeTreeParent(*syrlinksAssy);
#if OBSW_DEBUG_SYRLINKS == 1
  syrlinksHandler->setDebugMode(true);
#endif
}

void ObjectFactory::createPayloadComponents(LinuxLibgpioIF* gpioComIF, PowerSwitchIF& pwrSwitcher) {
  using namespace gpio;
  std::stringstream consumer;
  auto* camSwitcher =
      new CamSwitcher(objects::CAM_SWITCHER, pwrSwitcher, power::PDU2_CH8_PAYLOAD_CAMERA);
  camSwitcher->connectModeTreeParent(satsystem::payload::SUBSYSTEM);
#if OBSW_ADD_PLOC_MPSOC == 1
  consumer << "0x" << std::hex << objects::PLOC_MPSOC_HANDLER;
  auto gpioConfigMPSoC = new GpiodRegularByLineName(q7s::gpioNames::ENABLE_MPSOC_UART,
                                                    consumer.str(), Direction::OUT, Levels::HIGH);
  auto mpsocGpioCookie = new GpioCookie;
  mpsocGpioCookie->addGpio(gpioIds::ENABLE_MPSOC_UART, gpioConfigMPSoC);
  gpioChecker(gpioComIF->addGpios(mpsocGpioCookie), "PLOC MPSoC");
  auto mpsocCookie =
      new SerialCookie(objects::PLOC_MPSOC_HANDLER, q7s::UART_PLOC_MPSOC_DEV,
                       serial::PLOC_MPSOC_BAUD, mpsoc::MAX_REPLY_SIZE, UartModes::NON_CANONICAL);
  mpsocCookie->setNoFixedSizeReply();
  auto plocMpsocHelper = new PlocMpsocSpecialComHelper(objects::PLOC_MPSOC_HELPER);
  auto* mpsocHandler = new PlocMpsocHandler(
      objects::PLOC_MPSOC_HANDLER, objects::UART_COM_IF, mpsocCookie, plocMpsocHelper,
      Gpio(gpioIds::ENABLE_MPSOC_UART, gpioComIF), objects::PLOC_SUPERVISOR_HANDLER);
  mpsocHandler->connectModeTreeParent(satsystem::payload::SUBSYSTEM);
#endif /* OBSW_ADD_PLOC_MPSOC == 1 */
#if OBSW_ADD_PLOC_SUPERVISOR == 1
  consumer << "0x" << std::hex << objects::PLOC_SUPERVISOR_HANDLER;
  auto gpioConfigSupv = new GpiodRegularByLineName(q7s::gpioNames::ENABLE_SUPV_UART, consumer.str(),
                                                   Direction::OUT, Levels::LOW);
  auto supvGpioCookie = new GpioCookie;
  supvGpioCookie->addGpio(gpioIds::ENABLE_SUPV_UART, gpioConfigSupv);
  gpioComIF->addGpios(supvGpioCookie);
  const char* plocSupvDev = q7s::UART_PLOC_SUPERVISOR_DEV;
  if (not std::filesystem::exists(plocSupvDev)) {
    plocSupvDev = q7s::UART_PLOC_SUPERVISOR_DEV_FALLBACK;
  }
  auto supervisorCookie =
      new SerialCookie(objects::PLOC_SUPERVISOR_HANDLER, plocSupvDev, serial::PLOC_SUPV_BAUD,
                       supv::MAX_PACKET_SIZE * 20, UartModes::NON_CANONICAL);
  supervisorCookie->setNoFixedSizeReply();
  new PlocSupvUartManager(objects::PLOC_SUPERVISOR_HELPER);
  DhbConfig dhbConf(objects::PLOC_SUPERVISOR_HANDLER);
  auto* supvHandler =
      new FreshSupvHandler(dhbConf, supervisorCookie, Gpio(gpioIds::ENABLE_SUPV_UART, gpioComIF),
                           pwrSwitcher, power::PDU1_CH6_PLOC_12V);
  supvHandler->connectModeTreeParent(satsystem::payload::SUBSYSTEM);
#endif /* OBSW_ADD_PLOC_SUPERVISOR == 1 */
  static_cast<void>(consumer);
}

void ObjectFactory::createReactionWheelComponents(LinuxLibgpioIF* gpioComIF,
                                                  PowerSwitchIF* pwrSwitcher) {
  using namespace gpio;
  GpioCookie* gpioCookieRw = new GpioCookie;
  GpioCallback* csRw1 =
      new GpioCallback("Chip select reaction wheel 1", Direction::OUT, Levels::HIGH,
                       &gpioCallbacks::spiCsDecoderCallback, gpioComIF);
  gpioCookieRw->addGpio(gpioIds::CS_RW1, csRw1);
  GpioCallback* csRw2 =
      new GpioCallback("Chip select reaction wheel 2", Direction::OUT, Levels::HIGH,
                       &gpioCallbacks::spiCsDecoderCallback, gpioComIF);
  gpioCookieRw->addGpio(gpioIds::CS_RW2, csRw2);
  GpioCallback* csRw3 =
      new GpioCallback("Chip select reaction wheel 3", Direction::OUT, Levels::HIGH,
                       &gpioCallbacks::spiCsDecoderCallback, gpioComIF);
  gpioCookieRw->addGpio(gpioIds::CS_RW3, csRw3);
  GpioCallback* csRw4 =
      new GpioCallback("Chip select reaction wheel 4", Direction::OUT, Levels::HIGH,
                       &gpioCallbacks::spiCsDecoderCallback, gpioComIF);
  gpioCookieRw->addGpio(gpioIds::CS_RW4, csRw4);

  std::stringstream consumer;
  GpiodRegularByLineName* gpio = nullptr;
  consumer << "0x" << std::hex << objects::RW1;
  gpio = new GpiodRegularByLineName(q7s::gpioNames::EN_RW_1, consumer.str(), Direction::OUT,
                                    Levels::LOW);
  gpioCookieRw->addGpio(gpioIds::EN_RW1, gpio);
  consumer.str("");
  consumer << "0x" << std::hex << objects::RW2;
  gpio = new GpiodRegularByLineName(q7s::gpioNames::EN_RW_2, consumer.str(), Direction::OUT,
                                    Levels::LOW);
  gpioCookieRw->addGpio(gpioIds::EN_RW2, gpio);
  consumer.str("");
  consumer << "0x" << std::hex << objects::RW3;
  gpio = new GpiodRegularByLineName(q7s::gpioNames::EN_RW_3, consumer.str(), Direction::OUT,
                                    Levels::LOW);
  gpioCookieRw->addGpio(gpioIds::EN_RW3, gpio);
  consumer.str("");
  consumer << "0x" << std::hex << objects::RW4;
  gpio = new GpiodRegularByLineName(q7s::gpioNames::EN_RW_4, consumer.str(), Direction::OUT,
                                    Levels::LOW);
  gpioCookieRw->addGpio(gpioIds::EN_RW4, gpio);

  gpioChecker(gpioComIF->addGpios(gpioCookieRw), "RWs");

#if OBSW_ADD_RW == 1
  std::array<std::pair<address_t, gpioId_t>, 4> rwCookieParams = {
      {{addresses::RW1, gpioIds::CS_RW1},
       {addresses::RW2, gpioIds::CS_RW2},
       {addresses::RW3, gpioIds::CS_RW3},
       {addresses::RW4, gpioIds::CS_RW4}}};
  std::array<SpiCookie*, 4> rwCookies = {};
  std::array<object_id_t, 4> rwIds = {objects::RW1, objects::RW2, objects::RW3, objects::RW4};
  std::array<gpioId_t, 4> rwGpioIds = {gpioIds::EN_RW1, gpioIds::EN_RW2, gpioIds::EN_RW3,
                                       gpioIds::EN_RW4};
  std::array<DeviceHandlerBase*, 4> rws = {};
  new RwPollingTask(objects::RW_POLLING_TASK, q7s::SPI_RW_DEV, *gpioComIF);
  for (uint8_t idx = 0; idx < rwCookies.size(); idx++) {
    rwCookies[idx] = new RwCookie(idx, rwCookieParams[idx].first, rwCookieParams[idx].second,
                                  rws::MAX_REPLY_SIZE, spi::RW_MODE, spi::RW_SPEED);
    auto* rwHandler = new RwHandler(rwIds[idx], objects::RW_POLLING_TASK, rwCookies[idx], gpioComIF,
                                    rwGpioIds[idx], idx);
#if OBSW_TEST_RW == 1
    rws[idx]->setStartUpImmediately();
#endif
#if OBSW_DEBUG_RW == 1
    rwHandler->setDebugMode(true);
#endif
    rws[idx] = rwHandler;
  }

  createRwAssy(*pwrSwitcher, power::Switches::PDU2_CH2_RW_5V, rws, rwIds);
#endif /* OBSW_ADD_RW == 1 */
}

ReturnValue_t ObjectFactory::createCcsdsComponents(CcsdsComponentArgs& args) {
  using namespace gpio;
  // GPIO definitions of signals connected to the virtual channel interfaces of the PTME IP Core
  GpioCookie* gpioCookiePtmeIp = new GpioCookie;
  GpiodRegularByLineName* gpio = nullptr;
  gpio = new GpiodRegularByLineName(q7s::gpioNames::PAPB_EMPTY_SIGNAL_VC0, "PAPB VC0");
  gpioCookiePtmeIp->addGpio(gpioIds::VC0_PAPB_EMPTY, gpio);
  gpio = new GpiodRegularByLineName(q7s::gpioNames::PAPB_EMPTY_SIGNAL_VC1, "PAPB VC1");
  gpioCookiePtmeIp->addGpio(gpioIds::VC1_PAPB_EMPTY, gpio);
  gpio = new GpiodRegularByLineName(q7s::gpioNames::PAPB_EMPTY_SIGNAL_VC2, "PAPB VC2");
  gpioCookiePtmeIp->addGpio(gpioIds::VC2_PAPB_EMPTY, gpio);
  gpio = new GpiodRegularByLineName(q7s::gpioNames::PAPB_EMPTY_SIGNAL_VC3, "PAPB VC3");
  gpioCookiePtmeIp->addGpio(gpioIds::VC3_PAPB_EMPTY, gpio);
  gpio = new GpiodRegularByLineName(q7s::gpioNames::PTME_RESETN, "PTME RESETN",
                                    gpio::Direction::OUT, gpio::Levels::HIGH);
  gpioCookiePtmeIp->addGpio(gpioIds::PTME_RESETN, gpio);
  gpioChecker(args.gpioComIF.addGpios(gpioCookiePtmeIp), "PTME PAPB VCs");

  // Creating virtual channel interfaces
  VirtualChannelIF* vc0 =
      new PapbVcInterface(&args.gpioComIF, gpioIds::VC0_PAPB_EMPTY, q7s::UIO_PTME,
                          q7s::uiomapids::PTME_VC0, config::MAX_SPACEPACKET_TC_SIZE);
  VirtualChannelIF* vc1 =
      new PapbVcInterface(&args.gpioComIF, gpioIds::VC1_PAPB_EMPTY, q7s::UIO_PTME,
                          q7s::uiomapids::PTME_VC1, config::MAX_SPACEPACKET_TC_SIZE);
  VirtualChannelIF* vc2 =
      new PapbVcInterface(&args.gpioComIF, gpioIds::VC2_PAPB_EMPTY, q7s::UIO_PTME,
                          q7s::uiomapids::PTME_VC2, config::MAX_SPACEPACKET_TC_SIZE);
  VirtualChannelIF* vc3 =
      new PapbVcInterface(&args.gpioComIF, gpioIds::VC3_PAPB_EMPTY, q7s::UIO_PTME,
                          q7s::uiomapids::PTME_VC3, config::MAX_SPACEPACKET_TC_SIZE);
  // Creating ptme object and adding virtual channel interfaces
  Ptme* ptme = new Ptme(objects::PTME);
  ptme->addVcInterface(ccsds::VC0, vc0);
  ptme->addVcInterface(ccsds::VC1, vc1);
  ptme->addVcInterface(ccsds::VC2, vc2);
  ptme->addVcInterface(ccsds::VC3, vc3);
  AxiPtmeConfig* axiPtmeConfig =
      new AxiPtmeConfig(objects::AXI_PTME_CONFIG, q7s::UIO_PTME, q7s::uiomapids::PTME_CONFIG);
  PtmeConfig* ptmeConfig = new PtmeConfig(objects::PTME_CONFIG, axiPtmeConfig);

  PtmeGpios gpios;
  gpios.enableTxClock = gpioIds::RS485_EN_TX_CLOCK;
  gpios.enableTxData = gpioIds::RS485_EN_TX_DATA;
  gpios.ptmeResetn = gpioIds::PTME_RESETN;

  *args.ipCoreHandler =
      new CcsdsIpCoreHandler(objects::CCSDS_HANDLER, objects::CCSDS_PACKET_DISTRIBUTOR, *ptmeConfig,
                             LINK_STATE, &args.gpioComIF, gpios, PTME_LOCKED);
  // This VC will receive all live TM
  auto* vcWithQueue = new VirtualChannel(objects::PTME_VC0_LIVE_TM, ccsds::VC0, "PTME VC0 LIVE TM",
                                         *ptme, LINK_STATE);
  auto* liveTask = new LiveTmTask(objects::LIVE_TM_TASK, args.pusFunnel, args.cfdpFunnel,
                                  *vcWithQueue, PTME_LOCKED, config::LIVE_CHANNEL_NORMAL_QUEUE_SIZE,
                                  config::LIVE_CHANNEL_CFDP_QUEUE_SIZE);
  args.normalLiveTmDest = liveTask->getNormalLiveQueueId();
  args.cfdpLiveTmDest = liveTask->getCfdpLiveQueueId();
  liveTask->connectModeTreeParent(satsystem::com::SUBSYSTEM);

  // Set up log store.
  auto* vc = new VirtualChannel(objects::PTME_VC1_LOG_TM, ccsds::VC1, "PTME VC1 LOG TM", *ptme,
                                LINK_STATE);
  LogStores logStores(args.stores);
  // Core task which handles the LOG store and takes care of dumping it as TM using a VC directly
  auto* logStore =
      new PersistentLogTmStoreTask(objects::LOG_STORE_AND_TM_TASK, args.ipcStore, logStores, *vc,
                                   *SdCardManager::instance(), PTME_LOCKED);
  logStore->connectModeTreeParent(satsystem::com::SUBSYSTEM);

  vc = new VirtualChannel(objects::PTME_VC2_HK_TM, ccsds::VC2, "PTME VC2 HK TM", *ptme, LINK_STATE);
  // Core task which handles the HK store and takes care of dumping it as TM using a VC directly
  auto* hkStore = new PersistentSingleTmStoreTask(
      objects::HK_STORE_AND_TM_TASK, args.ipcStore, *args.stores.hkStore, *vc,
      persTmStore::DUMP_HK_STORE_DONE, persTmStore::DUMP_HK_CANCELLED, *SdCardManager::instance(),
      PTME_LOCKED);
  hkStore->connectModeTreeParent(satsystem::com::SUBSYSTEM);

  vc = new VirtualChannel(objects::PTME_VC3_CFDP_TM, ccsds::VC3, "PTME VC3 CFDP TM", *ptme,
                          LINK_STATE);
  // Core task which handles the CFDP store and takes care of dumping it as TM using a VC directly
  auto* cfdpTask = new PersistentSingleTmStoreTask(
      objects::CFDP_STORE_AND_TM_TASK, args.ipcStore, *args.stores.cfdpStore, *vc,
      persTmStore::DUMP_CFDP_STORE_DONE, persTmStore::DUMP_CFDP_CANCELLED,
      *SdCardManager::instance(), PTME_LOCKED);
  cfdpTask->connectModeTreeParent(satsystem::com::SUBSYSTEM);

  ReturnValue_t result = (*args.ipCoreHandler)->connectModeTreeParent(satsystem::com::SUBSYSTEM);
  if (result != returnvalue::OK) {
    sif::error
        << "ObjectFactory::createCcsdsComponents: Connecting COM subsystem to CCSDS handler failed"
        << std::endl;
  }

  GpioCookie* gpioCookiePdec = new GpioCookie;
  // GPIO also low after linux boot (specified by device-tree)
  gpio = new GpiodRegularByLineName(q7s::gpioNames::PDEC_RESET, "PDEC Handler", Direction::OUT,
                                    Levels::LOW);
  gpioCookiePdec->addGpio(gpioIds::PDEC_RESET, gpio);
  gpioChecker(args.gpioComIF.addGpios(gpioCookiePdec), "PDEC");
  struct UioNames uioNames {};
  uioNames.configMemory = q7s::UIO_PDEC_CONFIG_MEMORY;
  uioNames.ramMemory = q7s::UIO_PDEC_RAM;
  uioNames.registers = q7s::UIO_PDEC_REGISTERS;
  uioNames.irq = q7s::UIO_PDEC_IRQ;
  new PdecHandler(objects::PDEC_HANDLER, objects::CCSDS_HANDLER, &args.gpioComIF,
                  gpioIds::PDEC_RESET, uioNames, args.pdecCfgMemBaseAddr, args.pdecRamBaseAddr);
  GpioCookie* gpioRS485Chip = new GpioCookie;
  gpio = new GpiodRegularByLineName(q7s::gpioNames::RS485_EN_TX_CLOCK, "RS485 Transceiver",
                                    Direction::OUT, Levels::LOW);
  gpioRS485Chip->addGpio(gpioIds::RS485_EN_TX_CLOCK, gpio);
  gpio = new GpiodRegularByLineName(q7s::gpioNames::RS485_EN_TX_DATA, "RS485 Transceiver",
                                    Direction::OUT, Levels::LOW);
  gpioRS485Chip->addGpio(gpioIds::RS485_EN_TX_DATA, gpio);
  // Default configuration enables RX channels (RXEN = LOW)
  gpio = new GpiodRegularByLineName(q7s::gpioNames::RS485_EN_RX_CLOCK, "RS485 Transceiver",
                                    Direction::OUT, Levels::LOW);
  gpioRS485Chip->addGpio(gpioIds::RS485_EN_RX_CLOCK, gpio);
  gpio = new GpiodRegularByLineName(q7s::gpioNames::RS485_EN_RX_DATA, "RS485 Transceiver",
                                    Direction::OUT, Levels::LOW);
  gpioRS485Chip->addGpio(gpioIds::RS485_EN_RX_DATA, gpio);
  gpioChecker(args.gpioComIF.addGpios(gpioRS485Chip), "RS485 Transceiver");
  return returnvalue::OK;
}

void ObjectFactory::createPlPcduComponents(LinuxLibgpioIF* gpioComIF, SpiComIF* spiComIF,
                                           PowerSwitchIF* pwrSwitcher,
                                           Stack5VHandler& stackHandler) {
  using namespace gpio;
  // Create all GPIO components first
  GpioCookie* plPcduGpios = new GpioCookie;
  GpiodRegularByLineName* gpio = nullptr;
  std::string consumer;
  // Switch pins are active high
  consumer = "PLPCDU_ENB_VBAT_0";
  gpio = new GpiodRegularByLineName(q7s::gpioNames::PL_PCDU_ENABLE_VBAT0, consumer, Direction::OUT,
                                    gpio::Levels::LOW);
  plPcduGpios->addGpio(gpioIds::PLPCDU_ENB_VBAT0, gpio);
  consumer = "PLPCDU_ENB_VBAT_1";
  gpio = new GpiodRegularByLineName(q7s::gpioNames::PL_PCDU_ENABLE_VBAT1, consumer, Direction::OUT,
                                    gpio::Levels::LOW);
  plPcduGpios->addGpio(gpioIds::PLPCDU_ENB_VBAT1, gpio);
  consumer = "PLPCDU_ENB_DRO";
  gpio = new GpiodRegularByLineName(q7s::gpioNames::PL_PCDU_ENABLE_DRO, consumer, Direction::OUT,
                                    gpio::Levels::LOW);
  plPcduGpios->addGpio(gpioIds::PLPCDU_ENB_DRO, gpio);
  consumer = "PLPCDU_ENB_X8";
  gpio = new GpiodRegularByLineName(q7s::gpioNames::PL_PCDU_ENABLE_X8, consumer, Direction::OUT,
                                    gpio::Levels::LOW);
  plPcduGpios->addGpio(gpioIds::PLPCDU_ENB_X8, gpio);
  consumer = "PLPCDU_ENB_TX";
  gpio = new GpiodRegularByLineName(q7s::gpioNames::PL_PCDU_ENABLE_TX, consumer, Direction::OUT,
                                    gpio::Levels::LOW);
  plPcduGpios->addGpio(gpioIds::PLPCDU_ENB_TX, gpio);
  consumer = "PLPCDU_ENB_MPA";
  gpio = new GpiodRegularByLineName(q7s::gpioNames::PL_PCDU_ENABLE_MPA, consumer, Direction::OUT,
                                    gpio::Levels::LOW);
  plPcduGpios->addGpio(gpioIds::PLPCDU_ENB_MPA, gpio);
  consumer = "PLPCDU_ENB_HPA";
  gpio = new GpiodRegularByLineName(q7s::gpioNames::PL_PCDU_ENABLE_HPA, consumer, Direction::OUT,
                                    gpio::Levels::LOW);
  plPcduGpios->addGpio(gpioIds::PLPCDU_ENB_HPA, gpio);

  // Chip select pin is active low
  consumer = "PLPCDU_ADC_CS";
  gpio = new GpiodRegularByLineName(q7s::gpioNames::PL_PCDU_ADC_CS, consumer, Direction::OUT,
                                    gpio::Levels::HIGH);
  plPcduGpios->addGpio(gpioIds::PLPCDU_ADC_CS, gpio);
  gpioChecker(gpioComIF->addGpios(plPcduGpios), "PL PCDU");
  SpiCookie* spiCookie =
      new SpiCookie(addresses::PLPCDU_ADC, gpioIds::PLPCDU_ADC_CS, plpcdu::MAX_ADC_REPLY_SIZE,
                    spi::DEFAULT_MAX_1227_MODE, spi::PL_PCDU_MAX_1227_SPEED);
  // Create device handler components
  auto plPcduHandler =
      new PayloadPcduHandler(objects::PLPCDU_HANDLER, objects::SPI_MAIN_COM_IF, spiCookie,
                             gpioComIF, SdCardManager::instance(), stackHandler, false);
  spiCookie->setCallbackMode(PayloadPcduHandler::extConvAsTwoCallback, plPcduHandler);
#if OBSW_TEST_PL_PCDU == 1
  plPcduHandler->setStartUpImmediately();
#endif
#if OBSW_DEBUG_PL_PCDU == 1
  plPcduHandler->setToGoToNormalModeImmediately(true);
  plPcduHandler->enablePeriodicPrintout(true, 10);
#endif
  plPcduHandler->connectModeTreeParent(satsystem::payload::SUBSYSTEM);
}

void ObjectFactory::createTestComponents(LinuxLibgpioIF* gpioComIF) {
  new Q7STestTask(objects::TEST_TASK);
#if OBSW_ADD_SPI_TEST_CODE == 1
  new SpiTestClass(objects::SPI_TEST, gpioComIF);
#endif
#if OBSW_ADD_I2C_TEST_CODE == 1
  new I2cTestClass(objects::I2C_TEST, q7s::I2C_PL_EIVE);
#endif
#if OBSW_ADD_UART_TEST_CODE == 1
  // auto* reader= new ScexUartReader(objects::SCEX_UART_READER);
  new UartTestClass(objects::UART_TEST);
#endif
}

void ObjectFactory::createStrComponents(PowerSwitchIF* pwrSwitcher, SdCardManager& sdcMan) {
  auto* strAssy = new StrAssembly(objects::STR_ASSY);
  strAssy->connectModeTreeParent(satsystem::acs::ACS_SUBSYSTEM);
  auto* starTrackerCookie =
      new SerialCookie(objects::STAR_TRACKER, q7s::UART_STAR_TRACKER_DEV, serial::STAR_TRACKER_BAUD,
                       startracker::MAX_FRAME_SIZE * 2 + 2, UartModes::NON_CANONICAL);
  starTrackerCookie->setNoFixedSizeReply();
  StrComHandler* strComIF = new StrComHandler(objects::STR_COM_IF);

  const char* paramJsonFile = "/mnt/sd0/startracker/flight-config.json";
  if (paramJsonFile == nullptr) {
    sif::error << "No valid Star Tracker parameter JSON file" << std::endl;
  }
  auto strFdir = new StrFdir(objects::STAR_TRACKER);
  auto cfgGetter = new StrConfigPathGetter(sdcMan);
  auto starTracker =
      new StarTrackerHandler(objects::STAR_TRACKER, objects::STR_COM_IF, starTrackerCookie,
                             strComIF, power::PDU1_CH2_STAR_TRACKER_5V, *cfgGetter, sdcMan);
  starTracker->setPowerSwitcher(pwrSwitcher);
  starTracker->connectModeTreeParent(*strAssy);
  starTracker->setCustomFdir(strFdir);
}

void ObjectFactory::createImtqComponents(PowerSwitchIF* pwrSwitcher, bool enableHkSets,
                                         const char* i2cDev) {
  auto* imtqAssy = new ImtqAssembly(objects::IMTQ_ASSY);
  imtqAssy->connectModeTreeParent(satsystem::acs::ACS_SUBSYSTEM);

  new ImtqPollingTask(objects::IMTQ_POLLING, signals::I2C_FATAL_ERRORS);
  I2cCookie* imtqI2cCookie = new I2cCookie(addresses::IMTQ, imtq::MAX_REPLY_SIZE, i2cDev);
  auto imtqHandler = new ImtqHandler(objects::IMTQ_HANDLER, objects::IMTQ_POLLING, imtqI2cCookie,
                                     power::Switches::PDU1_CH3_MGT_5V, enableHkSets);
  imtqHandler->enableThermalModule(ThermalStateCfg());
  imtqHandler->setPowerSwitcher(pwrSwitcher);
  imtqHandler->connectModeTreeParent(*imtqAssy);
  static_cast<void>(imtqHandler);
#if OBSW_TEST_IMTQ == 1
  imtqHandler->setStartUpImmediately();
  imtqHandler->setToGoToNormal(true);
#endif
#if OBSW_DEBUG_IMTQ == 1
  imtqHandler->setDebugMode(true);
#endif
}

void ObjectFactory::createBpxBatteryComponent(bool enableHkSets, const char* i2cDev) {
  I2cCookie* bpxI2cCookie = new I2cCookie(addresses::BPX_BATTERY, 100, i2cDev);
  BpxBatteryHandler* bpxHandler = new BpxBatteryHandler(
      objects::BPX_BATT_HANDLER, objects::I2C_COM_IF, bpxI2cCookie, enableHkSets);
  bpxHandler->setStartUpImmediately();
  bpxHandler->setToGoToNormalMode(true);
#if OBSW_DEBUG_BPX_BATT == 1
  bpxHandler->setDebugMode(true);
#endif
}

void ObjectFactory::createMiscComponents() { new PlocMemoryDumper(objects::PLOC_MEMORY_DUMPER); }

void ObjectFactory::testAcsBrdAss(AcsBoardAssembly* acsAss) {
  CommandMessage msg;
  ModeMessage::setModeMessage(&msg, ModeMessage::CMD_MODE_COMMAND, DeviceHandlerIF::MODE_NORMAL,
                              duallane::A_SIDE);
  ReturnValue_t result = MessageQueueSenderIF::sendMessage(acsAss->getCommandQueue(), &msg);
  if (result != returnvalue::OK) {
    sif::warning << "Sending mode command failed" << std::endl;
  }
}

void ObjectFactory::createRadSensorChipSelect(LinuxLibgpioIF* gpioIF) {
  using namespace gpio;
  if (gpioIF == nullptr) {
    return;
  }
  GpioCookie* gpioCookieRadSensor = new GpioCookie;
  std::stringstream consumer;
  consumer << "0x" << std::hex << objects::RAD_SENSOR;
  GpiodRegularByLineName* gpio = new GpiodRegularByLineName(
      q7s::gpioNames::RAD_SENSOR_CHIP_SELECT, consumer.str(), Direction::OUT, Levels::HIGH);
  gpioCookieRadSensor->addGpio(gpioIds::CS_RAD_SENSOR, gpio);
  gpio = new GpiodRegularByLineName(q7s::gpioNames::ENABLE_RADFET, consumer.str(), Direction::OUT,
                                    Levels::LOW);
  gpioCookieRadSensor->addGpio(gpioIds::ENABLE_RADFET, gpio);
  gpioChecker(gpioIF->addGpios(gpioCookieRadSensor), "RAD sensor");
}

void ObjectFactory::createPlI2cResetGpio(LinuxLibgpioIF* gpioIF) {
  using namespace gpio;
  if (common::OBSW_VERSION_MAJOR >= 6 or common::OBSW_VERSION_MAJOR == 4) {
    if (gpioIF == nullptr) {
      return;
    }
    GpioCookie* gpioI2cResetnCookie = new GpioCookie;
    GpiodRegularByLineName* gpioI2cResetn = new GpiodRegularByLineName(
        q7s::gpioNames::PL_I2C_ARESETN, "PL_I2C_ARESETN", Direction::OUT, Levels::HIGH);
    gpioI2cResetnCookie->addGpio(gpioIds::PL_I2C_ARESETN, gpioI2cResetn);
    gpioChecker(gpioIF->addGpios(gpioI2cResetnCookie), "PL I2C ARESETN");
    // Reset I2C explicitely again.
    gpioIF->pullLow(gpioIds::PL_I2C_ARESETN);
    TaskFactory::delayTask(1);
    gpioIF->pullHigh(gpioIds::PL_I2C_ARESETN);
  }
}

ReturnValue_t ObjectFactory::readFirmwareVersion() {
  uint32_t* mappedSysRomAddr = nullptr;
  // The SYS ROM FPGA block is only available in those versions.
  if (not(common::OBSW_VERSION_MAJOR >= 6) or (common::OBSW_VERSION_MAJOR == 4)) {
    return returnvalue::OK;
  }
  // This has to come before the version announce because it might be required for retrieving
  // the firmware version.
  UioMapper sysRomMapper(q7s::UIO_SYS_ROM);
  ReturnValue_t result =
      sysRomMapper.getMappedAdress(&mappedSysRomAddr, UioMapper::Permissions::READ_ONLY);
  if (result != returnvalue::OK) {
    sif::error << "Getting mapped SYS ROM UIO address failed" << std::endl;
    return returnvalue::FAILED;
  }
  if (mappedSysRomAddr != nullptr) {
    uint32_t firstEntry = *(reinterpret_cast<uint32_t*>(mappedSysRomAddr));
    uint32_t secondEntry = *(reinterpret_cast<uint32_t*>(mappedSysRomAddr) + 1);
    core::FW_VERSION_MAJOR = (firstEntry >> 24) & 0xff;
    core::FW_VERSION_MINOR = (firstEntry >> 16) & 0xff;
    core::FW_VERSION_REVISION = (firstEntry >> 8) & 0xff;
    bool hasGitSha = (firstEntry & 0x0b1);
    if (hasGitSha) {
      std::memcpy(core::FW_VERSION_GIT_SHA, &secondEntry, 4);
    }
  }
  return returnvalue::OK;
}

ReturnValue_t ObjectFactory::createCcsdsIpComponentsWrapper(CcsdsComponentArgs& ccsdsArgs) {
  ccsdsArgs.pdecCfgMemBaseAddr = config::pdec::PDEC_CONFIG_BASE_ADDR;
  ccsdsArgs.pdecRamBaseAddr = config::pdec::PDEC_RAM_ADDR;
  if (core::FW_VERSION_MAJOR < 6) {
    ccsdsArgs.pdecCfgMemBaseAddr = config::pdec::PDEC_CONFIG_BASE_ADDR_LEGACY;
    ccsdsArgs.pdecRamBaseAddr = config::pdec::PDEC_RAM_ADDR_LEGACY;
  }
  ReturnValue_t result = createCcsdsComponents(ccsdsArgs);
#if OBSW_TM_TO_PTME == 1
  if (ccsdsArgs.normalLiveTmDest != MessageQueueIF::NO_QUEUE) {
    ccsdsArgs.pusFunnel.addLiveDestinationByRawId("VC0 NORMAL LIVE TM", ccsdsArgs.normalLiveTmDest,
                                                  0);
  }
  if (ccsdsArgs.cfdpLiveTmDest != MessageQueueIF::NO_QUEUE) {
    ccsdsArgs.cfdpFunnel.addLiveDestinationByRawId("VC0 CFDP LIVE TM", ccsdsArgs.cfdpLiveTmDest, 0);
  }
#endif
  return result;
}