
#ifndef ETHERCAT_MASTER_CONFIG_H
#define ETHERCAT_MASTER_CONFIG_H

#include "../ethercat_types.h"
#include "../data/structures.h"
#include <stdint.h>
#include <string.h>

#ifdef __cplusplus
extern "C" {
#endif

struct dll;

#ifndef DYNAMIC_LOAD

int      LoadConfigJson(uint16_t master_index, const char* json_str);

int      AutoConfigureSM(uint16_t master_index, uint16_t slave_index);

uint16_t EcInit(const char* json_config);

uint16_t EcInitFromFile(const char* json_file_path);

void     EcClose(uint16_t master_index);

#endif

#define CIA402_OD_CONTROLWORD             0x6040
#define CIA402_OD_STATUSWORD              0x6041
#define CIA402_OD_QUICK_STOP_OPTION_CODE  0x605A
#define CIA402_OD_MODES_OF_OPERATION      0x6060
#define CIA402_OD_MODES_OF_OPERATION_DISPLAY 0x6061
#define CIA402_OD_POSITION_ACTUAL         0x6064
#define CIA402_OD_VELOCITY_ACTUAL         0x606C
#define CIA402_OD_TARGET_TORQUE           0x6071
#define CIA402_OD_MAX_TORQUE              0x6072
#define CIA402_OD_MOTOR_RATED_TORQUE      0x6076
#define CIA402_OD_TORQUE_ACTUAL           0x6077
#define CIA402_OD_TARGET_POSITION         0x607A
#define CIA402_OD_HOME_OFFSET             0x607C
#define CIA402_OD_SOFTWARE_POSITION_LIMIT 0x607D
#define CIA402_OD_POLARITY                0x607E
#define CIA402_OD_MAX_PROFILE_VELOCITY    0x6080
#define CIA402_OD_PROFILE_VELOCITY        0x6081
#define CIA402_OD_PROFILE_ACCELERATION    0x6083
#define CIA402_OD_PROFILE_DECELERATION    0x6084
#define CIA402_OD_QUICK_STOP_DECELERATION 0x6085
#define CIA402_OD_MOTION_PROFILE_TYPE     0x6086
#define CIA402_OD_HOMING_METHOD           0x6098
#define CIA402_OD_HOMING_SPEEDS           0x6099
#define CIA402_OD_HOMING_ACCELERATION     0x609A
#define CIA402_OD_POSITION_OFFSET         0x60B0
#define CIA402_OD_VELOCITY_OFFSET         0x60B1
#define CIA402_OD_TORQUE_OFFSET           0x60B2
#define CIA402_OD_TOUCH_PROBE_FUNCTION    0x60B8
#define CIA402_OD_TOUCH_PROBE_STATUS      0x60B9
#define CIA402_OD_TOUCH_PROBE_POS_EDGE    0x60BA
#define CIA402_OD_TOUCH_PROBE_NEG_EDGE    0x60BB
#define CIA402_OD_INTERPOLATION_TIME      0x60C2
#define CIA402_OD_DIGITAL_INPUTS          0x60FD
#define CIA402_OD_DIGITAL_OUTPUTS         0x60FE
#define CIA402_OD_TARGET_VELOCITY         0x60FF
#define CIA402_OD_SUPPORTED_DRIVE_MODES   0x6502

#define AOE_COE_INDEX_GROUP     0xF302
#define AOE_FOE_READ_INDEX_GROUP  0xF401
#define AOE_FOE_WRITE_INDEX_GROUP 0xF402
#define AOE_FOE_DATA_INDEX_GROUP  0xF404
#define AOE_FOE_ACK_INDEX_GROUP   0xF405
#define AOE_SOE_INDEX_GROUP     0xF420
#define AOE_VOE_INDEX_GROUP     0xF430

#define EC_STATE_OFF_ADAPTIVE_TIMEOUT    131113
#define EC_STATE_OFF_OVERLAPPING_GROUPS  131114
#define EC_STATE_OFF_PACKED_MODE         131115
#define EC_STATE_OFF_FRAME_HIGH_PRIORITY 131116
#define EC_STATE_OFF_VLAN_ID             131118
#define EC_STATE_OFF_VLAN_PRIORITY       131120
#define EC_STATE_OFF_TIMEOUT_IP          131124
#define EC_STATE_OFF_TIMEOUT_PS          131128
#define EC_STATE_OFF_TIMEOUT_SO          131132
#define EC_STATE_OFF_WD_PD_TIMEOUT       131136
#define EC_STATE_OFF_WD_PDI_TIMEOUT      131138
#define EC_STATE_OFF_FILTER_THRESHOLD    131140
#define EC_STATE_OFF_FRAME_REPEAT_COUNT  131144

uint16_t config_get_vlan_id(const struct dll* dll, uint16_t master);

void config_set_vlan_id(const struct dll* dll, uint16_t master, uint16_t vlan_id);

uint8_t config_get_vlan_priority(const struct dll* dll, uint16_t master);

void config_set_vlan_priority(const struct dll* dll, uint16_t master, uint8_t priority);

BOOL config_get_frame_high_priority(const struct dll* dll, uint16_t master);

void config_set_frame_high_priority(const struct dll* dll, uint16_t master, BOOL enabled);

BOOL config_get_overlapping_groups(const struct dll* dll, uint16_t master);

void config_set_overlapping_groups(const struct dll* dll, uint16_t master, BOOL enabled);

BOOL config_get_packed_mode(const struct dll* dll, uint16_t master);

void config_set_packed_mode(const struct dll* dll, uint16_t master, BOOL enabled);

uint8_t config_get_frame_repeat_count(const struct dll* dll, uint16_t master);

void config_set_frame_repeat_count(const struct dll* dll, uint16_t master, uint8_t count);

uint16_t config_get_filter_threshold(const struct dll* dll, uint16_t master);

void config_set_filter_threshold(const struct dll* dll, uint16_t master, uint16_t threshold);

BOOL config_get_adaptive_timeout(const struct dll* dll, uint16_t master);

void config_set_adaptive_timeout(const struct dll* dll, uint16_t master, BOOL enabled);

uint32_t config_get_timeout_init_to_preop(const struct dll* dll, uint16_t master);

void config_set_timeout_init_to_preop(const struct dll* dll, uint16_t master, uint32_t timeout_ms);

uint32_t config_get_timeout_preop_to_safeop(const struct dll* dll, uint16_t master);

void config_set_timeout_preop_to_safeop(const struct dll* dll, uint16_t master, uint32_t timeout_ms);

uint32_t config_get_timeout_safeop_to_op(const struct dll* dll, uint16_t master);

void config_set_timeout_safeop_to_op(const struct dll* dll, uint16_t master, uint32_t timeout_ms);

uint16_t config_get_pd_watchdog_ms(const struct dll* dll, uint16_t master);

void config_set_pd_watchdog_ms(const struct dll* dll, uint16_t master, uint16_t timeout_ms);

uint16_t config_get_pdi_watchdog_ms(const struct dll* dll, uint16_t master);

void config_set_pdi_watchdog_ms(const struct dll* dll, uint16_t master, uint16_t timeout_ms);

#ifdef __cplusplus
}
#endif

#endif
