#include "awplc.h"

#ifdef AWPLC_USE_CPP
#include "user_header_files.hpp"
#endif/*AWPLC_USE_CPP*/

namespace AWPLC_APP {
#include "app.hpp"
#include "app.inc"
#include "app_extra.inc"
};

extern "C" uint32_t awplc_get_min_interval(void) {
  return AWPLC_APP::_plc_get_min_interval();
}

extern "C" ret_t awplc_init(void* plc) {
  iec_global_init(plc);
  iec_task_threads_init();
  IEC_PLC::instance()->set_name("app");
  ret_t ret = AWPLC_APP::_plc_init();

#ifdef HAS_SYSTEM_INIT
  IEC_Program* system_init = AWPLC_APP::SYSTEM_INIT::create();
  if(system_init != NULL) {
    system_init->exec_();
    system_init->unref();
  }
#endif/*HAS_SYSTEM_INIT*/  


  return ret;
}

extern "C" ret_t awplc_deinit(void) {
#ifdef HAS_SYSTEM_DEINIT
  IEC_Program* system_deinit = AWPLC_APP::SYSTEM_DEINIT::create();
  if(system_deinit != NULL) {
    system_deinit->exec_();
    system_deinit->unref();
  }
#endif/*HAS_SYSTEM_DEINIT*/  

  iec_task_threads_deinit();

  return iec_global_deinit();
}

extern "C" bool awplc_global_save(ubjson_writer_t* writer, iec_save_type_t type) {
  return AWPLC_APP::_plc_global_save(writer, type);
}

extern "C" bool awplc_global_load(conf_node_t* parent, iec_load_type_t type) {
  return AWPLC_APP::_plc_global_load(parent, type);
}

extern "C" bool awplc_global_get(const char* name, value_t* v, IEC_Property_OBJ* obj) {
  return AWPLC_APP::_plc_global_get(name, v, obj);
}

extern "C" bool awplc_global_set(const char* name, const value_t* v, IEC_Property_OBJ* obj) {
  return AWPLC_APP::_plc_global_set(name, v, obj);
}

