#include "ObjectFactory.h"

#include <fsfw/subsystem/Subsystem.h>
#include <linux/devices/ImtqPollingTask.h>
#include <linux/devices/RwPollingTask.h>
#include <mission/system/objects/CamSwitcher.h>

#include "OBSWConfig.h"
#include "bsp_q7s/boardtest/Q7STestTask.h"
#include "bsp_q7s/callbacks/gnssCallback.h"
#include "bsp_q7s/callbacks/pcduSwitchCb.h"
#include "bsp_q7s/callbacks/q7sGpioCallbacks.h"
#include "bsp_q7s/callbacks/rwSpiCallback.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/csp/CspComIF.h"
#include "linux/devices/GpsHyperionLinuxController.h"
#include "linux/devices/ScexUartReader.h"
#include "linux/devices/devicedefinitions/PlocMPSoCDefinitions.h"
#include "linux/devices/devicedefinitions/StarTrackerDefinitions.h"
#include "linux/devices/ploc/PlocMPSoCHandler.h"
#include "linux/devices/ploc/PlocMPSoCHelper.h"
#include "linux/devices/ploc/PlocMemoryDumper.h"
#include "linux/devices/ploc/PlocSupervisorHandler.h"
#include "linux/devices/startracker/StarTrackerHandler.h"
#include "linux/devices/startracker/StrHelper.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 "mission/csp/CspCookie.h"
#include "mission/system/fdir/AcsBoardFdir.h"
#include "mission/system/fdir/GomspacePowerFdir.h"
#include "mission/system/fdir/RtdFdir.h"
#include "mission/system/fdir/SusFdir.h"
#include "mission/system/fdir/SyrlinksFdir.h"
#include "mission/system/objects/AcsSubsystem.h"
#include "mission/system/objects/RwAssembly.h"
#include "mission/system/objects/TcsBoardAssembly.h"
#include "mission/system/tree/acsModeTree.h"
#include "mission/system/tree/comModeTree.h"
#include "mission/system/tree/payloadModeTree.h"
#include "mission/system/tree/tcsModeTree.h"
#include "tmtc/pusIds.h"
#if OBSW_TEST_LIBGPIOD == 1
#include "linux/boardtest/LibgpiodTest.h"
#endif
#include <mission/devices/ImtqHandler.h>
#include <mission/devices/PcduHandler.h>
#include <mission/devices/SyrlinksHandler.h>
#include <mission/devices/devicedefinitions/rwHelpers.h>

#include <sstream>

#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/core/GenericFactory.h"
#include "mission/devices/ACUHandler.h"
#include "mission/devices/BpxBatteryHandler.h"
#include "mission/devices/GyroADIS1650XHandler.h"
#include "mission/devices/HeaterHandler.h"
#include "mission/devices/Max31865PT1000Handler.h"
#include "mission/devices/P60DockHandler.h"
#include "mission/devices/PDU1Handler.h"
#include "mission/devices/PDU2Handler.h"
#include "mission/devices/PayloadPcduHandler.h"
#include "mission/devices/RadiationSensorHandler.h"
#include "mission/devices/RwHandler.h"
#include "mission/devices/SolarArrayDeploymentHandler.h"
#include "mission/devices/Tmp1075Handler.h"
#include "mission/devices/devicedefinitions/GomspaceDefinitions.h"
#include "mission/devices/devicedefinitions/Max31865Definitions.h"
#include "mission/devices/devicedefinitions/RadSensorDefinitions.h"
#include "mission/devices/devicedefinitions/SyrlinksDefinitions.h"
#include "mission/devices/devicedefinitions/payloadPcduDefinitions.h"
#include "mission/system/objects/AcsBoardAssembly.h"
#include "mission/tmtc/CcsdsIpCoreHandler.h"
#include "mission/tmtc/TmFunnelHandler.h"
#include "mission/tmtc/VirtualChannel.h"

ResetArgs RESET_ARGS_GNSS;

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>> tmpDevIds = {{
      {objects::TMP1075_HANDLER_TCS_0, addresses::TMP1075_TCS_0},
      {objects::TMP1075_HANDLER_TCS_1, addresses::TMP1075_TCS_1},
      {objects::TMP1075_HANDLER_PLPCDU_0, addresses::TMP1075_PLPCDU_0},
      // damaged
      // {objects::TMP1075_HANDLER_PLPCDU_1, addresses::TMP1075_PLPCDU_1},
      {objects::TMP1075_HANDLER_IF_BOARD, addresses::TMP1075_IF_BOARD},
  }};
  std::vector<I2cCookie*> tmpDevCookies;

  for (size_t idx = 0; idx < tmpDevIds.size(); idx++) {
    tmpDevCookies.push_back(
        new I2cCookie(tmpDevIds[idx].second, TMP1075::MAX_REPLY_LENGTH, q7s::I2C_PS_EIVE));
    auto* tmpDevHandler =
        new Tmp1075Handler(tmpDevIds[idx].first, objects::I2C_COM_IF, tmpDevCookies[idx]);
    tmpDevHandler->connectModeTreeParent(satsystem::tcs::SUBSYSTEM);
    // TODO: Remove this after TCS subsystem was added
    // These devices are connected to the 3V3 stack and should be powered permanently. Therefore,
    // we set them to normal mode immediately here.
    tmpDevHandler->setModeNormal();
  }
}

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);
  *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);
  //*spiRWComIF = new SpiComIF(objects::SPI_RW_COM_IF, q7s::SPI_RW_DEV, **gpioComIF);
}

void ObjectFactory::createPcduComponents(LinuxLibgpioIF* gpioComIF, PowerSwitchIF** pwrSwitcher) {
  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);
  CspCookie* acuCspCookie = new CspCookie(ACU::MAX_REPLY_SIZE, addresses::ACU, 500);

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

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

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

  auto acuFdir = new GomspacePowerFdir(objects::ACU_HANDLER);
  ACUHandler* acuhandler =
      new ACUHandler(objects::ACU_HANDLER, objects::CSP_COM_IF, acuCspCookie, acuFdir);
  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();
  acuhandler->setModeNormal();
  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) {
  using namespace gpio;
  if (gpioComIF == nullptr) {
    return returnvalue::FAILED;
  }
  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(gpioComIF->addGpios(gpioCookieRadSensor), "RAD sensor");

  SpiCookie* spiCookieRadSensor =
      new SpiCookie(addresses::RAD_SENSOR, gpioIds::CS_RAD_SENSOR, RAD_SENSOR::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::createAcsBoardComponents(LinuxLibgpioIF* gpioComIF, SerialComIF* uartComIF,
                                             PowerSwitchIF& pwrSwitcher) {
  using namespace gpio;
  GpioCookie* gpioCookieAcsBoard = new GpioCookie();

  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);
  gpioCookieAcsBoard->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);
  gpioCookieAcsBoard->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);
  gpioCookieAcsBoard->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);
  gpioCookieAcsBoard->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);
  gpioCookieAcsBoard->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);
  gpioCookieAcsBoard->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);
  gpioCookieAcsBoard->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);
  gpioCookieAcsBoard->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);
  gpioCookieAcsBoard->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);
  gpioCookieAcsBoard->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);
  gpioCookieAcsBoard->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);
  gpioCookieAcsBoard->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);
  gpioCookieAcsBoard->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);
  gpioCookieAcsBoard->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);
  gpioCookieAcsBoard->addGpio(gpioIds::GNSS_SELECT, gpio);
  gpioChecker(gpioComIF->addGpios(gpioCookieAcsBoard), "ACS Board");
  AcsBoardFdir* fdir = nullptr;
  static_cast<void>(fdir);

#if OBSW_ADD_ACS_BOARD == 1
  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, MGMLIS3MDL::MAX_BUFFER_SIZE,
                    spi::DEFAULT_LIS3_MODE, spi::DEFAULT_LIS3_SPEED);
  spiCookie->setMutexParams(MutexIF::TimeoutType::WAITING, spi::ACS_BOARD_CS_TIMEOUT);
  auto mgmLis3Handler0 = new MgmLIS3MDLHandler(
      objects::MGM_0_LIS3_HANDLER, objects::SPI_MAIN_COM_IF, 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, RM3100::MAX_BUFFER_SIZE,
                    spi::DEFAULT_RM3100_MODE, spi::DEFAULT_RM3100_SPEED);
  spiCookie->setMutexParams(MutexIF::TimeoutType::WAITING, spi::ACS_BOARD_CS_TIMEOUT);
  auto mgmRm3100Handler1 =
      new MgmRM3100Handler(objects::MGM_1_RM3100_HANDLER, objects::SPI_MAIN_COM_IF, 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, MGMLIS3MDL::MAX_BUFFER_SIZE,
                    spi::DEFAULT_LIS3_MODE, spi::DEFAULT_LIS3_SPEED);
  spiCookie->setMutexParams(MutexIF::TimeoutType::WAITING, spi::ACS_BOARD_CS_TIMEOUT);
  auto* mgmLis3Handler2 = new MgmLIS3MDLHandler(
      objects::MGM_2_LIS3_HANDLER, objects::SPI_MAIN_COM_IF, 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, RM3100::MAX_BUFFER_SIZE,
                    spi::DEFAULT_RM3100_MODE, spi::DEFAULT_RM3100_SPEED);
  spiCookie->setMutexParams(MutexIF::TimeoutType::WAITING, spi::ACS_BOARD_CS_TIMEOUT);
  auto* mgmRm3100Handler3 =
      new MgmRM3100Handler(objects::MGM_3_RM3100_HANDLER, objects::SPI_MAIN_COM_IF, 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 GyroADIS1650XHandler(objects::GYRO_0_ADIS_HANDLER, objects::SPI_MAIN_COM_IF, spiCookie,
                               ADIS1650X::Type::ADIS16505);
  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 GyroHandlerL3GD20H(
      objects::GYRO_1_L3G_HANDLER, objects::SPI_MAIN_COM_IF, 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 GyroADIS1650XHandler(objects::GYRO_2_ADIS_HANDLER, objects::SPI_MAIN_COM_IF,
                                         spiCookie, ADIS1650X::Type::ADIS16505);
  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 GyroHandlerL3GD20H(
      objects::GYRO_3_L3G_HANDLER, objects::SPI_MAIN_COM_IF, 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 = 100;
  auto gpsCtrl =
      new GpsHyperionLinuxController(objects::GPS_CONTROLLER, objects::NO_OBJECT, 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,
                                  pcdu::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, uart::SYRLINKS_BAUD,
                       syrlinks::MAX_REPLY_SIZE, UartModes::NON_CANONICAL);
  syrlinksUartCookie->setParityEven();

  auto syrlinksFdir = new SyrlinksFdir(objects::SYRLINKS_HANDLER);
  auto syrlinksHandler =
      new SyrlinksHandler(objects::SYRLINKS_HANDLER, objects::UART_COM_IF, syrlinksUartCookie,
                          pcdu::PDU1_CH1_SYRLINKS_12V, syrlinksFdir);
  syrlinksHandler->setPowerSwitcher(pwrSwitcher);
  syrlinksHandler->setStartUpImmediately();
  syrlinksHandler->connectModeTreeParent(satsystem::com::SUBSYSTEM);
#if OBSW_DEBUG_SYRLINKS == 1
  syrlinksHandler->setDebugMode(true);
#endif
}

void ObjectFactory::createPayloadComponents(LinuxLibgpioIF* gpioComIF, PowerSwitchIF& pwrSwitch) {
  using namespace gpio;
  std::stringstream consumer;
  auto* camSwitcher =
      new CamSwitcher(objects::CAM_SWITCHER, pwrSwitch, pcdu::PDU2_CH8_PAYLOAD_CAMERA);
  camSwitcher->connectModeTreeParent(satsystem::pl::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, uart::PLOC_MPSOC_BAUD,
                       mpsoc::MAX_REPLY_SIZE, UartModes::NON_CANONICAL);
  mpsocCookie->setNoFixedSizeReply();
  auto plocMpsocHelper = new PlocMPSoCHelper(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::pl::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);
  auto supervisorCookie =
      new SerialCookie(objects::PLOC_SUPERVISOR_HANDLER, q7s::UART_PLOC_SUPERVSIOR_DEV,
                       uart::PLOC_SUPV_BAUD, supv::MAX_PACKET_SIZE * 20, UartModes::NON_CANONICAL);
  supervisorCookie->setNoFixedSizeReply();
  auto supvHelper = new PlocSupvUartManager(objects::PLOC_SUPERVISOR_HELPER);
  auto* supvHandler = new PlocSupervisorHandler(objects::PLOC_SUPERVISOR_HANDLER, supervisorCookie,
                                                Gpio(gpioIds::ENABLE_SUPV_UART, gpioComIF),
                                                pcdu::PDU1_CH6_PLOC_12V, *supvHelper);
  supvHandler->connectModeTreeParent(satsystem::pl::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, pcdu::Switches::PDU2_CH2_RW_5V, rws, rwIds);
#endif /* OBSW_ADD_RW == 1 */
}

ReturnValue_t ObjectFactory::createCcsdsComponents(LinuxLibgpioIF* gpioComIF,
                                                   CcsdsIpCoreHandler** ipCoreHandler) {
  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;
  std::stringstream consumer;
  consumer.str("PAPB VC0");
  gpio = new GpiodRegularByLineName(q7s::gpioNames::PAPB_BUSY_SIGNAL_VC0, consumer.str());
  gpioCookiePtmeIp->addGpio(gpioIds::VC0_PAPB_BUSY, gpio);
  consumer.str("PAPB VC0");
  gpio = new GpiodRegularByLineName(q7s::gpioNames::PAPB_EMPTY_SIGNAL_VC0, consumer.str());
  gpioCookiePtmeIp->addGpio(gpioIds::VC0_PAPB_EMPTY, gpio);
  consumer.str("PAPB VC 1");
  gpio = new GpiodRegularByLineName(q7s::gpioNames::PAPB_BUSY_SIGNAL_VC1, consumer.str());
  gpioCookiePtmeIp->addGpio(gpioIds::VC1_PAPB_BUSY, gpio);
  consumer.str("");
  consumer.str("PAPB VC 1");
  gpioCookiePtmeIp->addGpio(gpioIds::VC1_PAPB_EMPTY, gpio);
  consumer.str("");
  consumer.str("PAPB VC 2");
  gpio = new GpiodRegularByLineName(q7s::gpioNames::PAPB_BUSY_SIGNAL_VC2, consumer.str());
  gpioCookiePtmeIp->addGpio(gpioIds::VC2_PAPB_BUSY, gpio);
  consumer.str("");
  consumer.str("PAPB VC 2");
  gpio = new GpiodRegularByLineName(q7s::gpioNames::PAPB_EMPTY_SIGNAL_VC2, consumer.str());
  gpioCookiePtmeIp->addGpio(gpioIds::VC2_PAPB_EMPTY, gpio);
  consumer.str("");
  consumer.str("PAPB VC 3");
  gpio = new GpiodRegularByLineName(q7s::gpioNames::PAPB_BUSY_SIGNAL_VC3, consumer.str());
  gpioCookiePtmeIp->addGpio(gpioIds::VC3_PAPB_BUSY, gpio);
  consumer.str("");
  consumer.str("PAPB VC 3");
  gpio = new GpiodRegularByLineName(q7s::gpioNames::PAPB_EMPTY_SIGNAL_VC3, consumer.str());
  gpioCookiePtmeIp->addGpio(gpioIds::VC3_PAPB_EMPTY, gpio);
  gpioChecker(gpioComIF->addGpios(gpioCookiePtmeIp), "PTME PAPB VCs");
  // Creating virtual channel interfaces
  VcInterfaceIF* vc0 =
      new PapbVcInterface(gpioComIF, gpioIds::VC0_PAPB_BUSY, gpioIds::VC0_PAPB_EMPTY, q7s::UIO_PTME,
                          q7s::uiomapids::PTME_VC0);
  VcInterfaceIF* vc1 =
      new PapbVcInterface(gpioComIF, gpioIds::VC1_PAPB_BUSY, gpioIds::VC1_PAPB_EMPTY, q7s::UIO_PTME,
                          q7s::uiomapids::PTME_VC1);
  VcInterfaceIF* vc2 =
      new PapbVcInterface(gpioComIF, gpioIds::VC2_PAPB_BUSY, gpioIds::VC2_PAPB_EMPTY, q7s::UIO_PTME,
                          q7s::uiomapids::PTME_VC2);
  VcInterfaceIF* vc3 =
      new PapbVcInterface(gpioComIF, gpioIds::VC3_PAPB_BUSY, gpioIds::VC3_PAPB_EMPTY, q7s::UIO_PTME,
                          q7s::uiomapids::PTME_VC3);
  // 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);
#if OBSW_ENABLE_SYRLINKS_TRANSMIT_TIMEOUT == 1
  // Set to high value when not sending via syrlinks
  static const uint32_t TRANSMITTER_TIMEOUT = 86400000;  // 1 day
#else
  static const uint32_t TRANSMITTER_TIMEOUT = 900000;  // 15 minutes
#endif
  *ipCoreHandler = new CcsdsIpCoreHandler(
      objects::CCSDS_HANDLER, objects::PTME, objects::CCSDS_PACKET_DISTRIBUTOR, ptmeConfig,
      gpioComIF, gpioIds::RS485_EN_TX_CLOCK, gpioIds::RS485_EN_TX_DATA, TRANSMITTER_TIMEOUT);
  VirtualChannel* vc = nullptr;
  vc = new VirtualChannel(ccsds::VC0, config::VC0_QUEUE_SIZE, objects::CCSDS_HANDLER);
  (*ipCoreHandler)->addVirtualChannel(ccsds::VC0, vc);
  vc = new VirtualChannel(ccsds::VC1, config::VC1_QUEUE_SIZE, objects::CCSDS_HANDLER);
  (*ipCoreHandler)->addVirtualChannel(ccsds::VC1, vc);
  vc = new VirtualChannel(ccsds::VC2, config::VC2_QUEUE_SIZE, objects::CCSDS_HANDLER);
  (*ipCoreHandler)->addVirtualChannel(ccsds::VC2, vc);
  vc = new VirtualChannel(ccsds::VC3, config::VC3_QUEUE_SIZE, objects::CCSDS_HANDLER);
  (*ipCoreHandler)->addVirtualChannel(ccsds::VC3, vc);

  ReturnValue_t result = (*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;
  consumer.str("");
  consumer << "0x" << std::hex << objects::PDEC_HANDLER;
  // GPIO also low after linux boot (specified by device-tree)
  gpio = new GpiodRegularByLineName(q7s::gpioNames::PDEC_RESET, consumer.str(), Direction::OUT,
                                    Levels::LOW);
  gpioCookiePdec->addGpio(gpioIds::PDEC_RESET, gpio);
  gpioChecker(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, gpioComIF, gpioIds::PDEC_RESET,
                  uioNames);
  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(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);
//  plPcduHandler->enablePeriodicPrintout(true, 5);
//  static_cast<void>(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::pl::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) {
  auto* starTrackerCookie =
      new SerialCookie(objects::STAR_TRACKER, q7s::UART_STAR_TRACKER_DEV, uart::STAR_TRACKER_BAUD,
                       startracker::MAX_FRAME_SIZE * 2 + 2, UartModes::NON_CANONICAL);
  starTrackerCookie->setNoFixedSizeReply();
  StrHelper* strHelper = new StrHelper(objects::STR_HELPER);
  auto starTracker =
      new StarTrackerHandler(objects::STAR_TRACKER, objects::UART_COM_IF, starTrackerCookie,
                             strHelper, pcdu::PDU1_CH2_STAR_TRACKER_5V);
  starTracker->setPowerSwitcher(pwrSwitcher);
  starTracker->connectModeTreeParent(satsystem::acs::ACS_SUBSYSTEM);
}

void ObjectFactory::createImtqComponents(PowerSwitchIF* pwrSwitcher) {
  new ImtqPollingTask(objects::IMTQ_POLLING);
  I2cCookie* imtqI2cCookie = new I2cCookie(addresses::IMTQ, imtq::MAX_REPLY_SIZE, q7s::I2C_PL_EIVE);
  auto imtqHandler = new ImtqHandler(objects::IMTQ_HANDLER, objects::IMTQ_POLLING, imtqI2cCookie,
                                     pcdu::Switches::PDU1_CH3_MGT_5V);
  imtqHandler->enableThermalModule(ThermalStateCfg());
  imtqHandler->setPowerSwitcher(pwrSwitcher);
  imtqHandler->connectModeTreeParent(satsystem::acs::ACS_SUBSYSTEM);
  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() {
  I2cCookie* bpxI2cCookie = new I2cCookie(addresses::BPX_BATTERY, 100, q7s::I2C_PL_EIVE);
  BpxBatteryHandler* bpxHandler =
      new BpxBatteryHandler(objects::BPX_BATT_HANDLER, objects::I2C_COM_IF, bpxI2cCookie);
  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;
  }
}