/** 
*******************************************************************************
* @file __modbus_compat.h
* @brief Модуль для совместимости библиотеки MODBUS.
*******************************************************************************
* @details Файл содержит API старых функций, а также заглушки для отключенных модулей:
******************************************************************************/
#ifndef __MODBUS_COMPAT_H_
#define __MODBUS_COMPAT_H_
#include "modbus_core.h"


#define CREATE_DEPRECATED_ALIAS(old_name, new_name, type) \
  __attribute__((deprecated("Use " #new_name " instead"))) \
  static const type old_name = new_name;

// Для функций
#define CREATE_DEPRECATED_FUNCTION(old_name, new_name, return_type, ...) \
  __attribute__((deprecated("Use " #new_name " instead"))) \
  return_type old_name(__VA_ARGS__) { \
      return new_name(__VA_ARGS__); \
  }
  
/** @addtogroup MODBUS_LEGACY_API Legacy API 
  * @ingroup MODBUS_FUNCTIONS
  * @brief Старые API функций, сохранённые для обратной совместимости.
  * @note   Не используйте эти функции в новом коде. Они будут удалены в будущих версиях.
  *         Вместо них используйте актуальные API, приведенные в втором столбце.
  * @{
  */
  
// Устаревшие enum      //Старый замените на    // Новый
                        //  |                   //  |
                        //  v                   //  v
CREATE_DEPRECATED_ALIAS(NO_ERRORS,              ET_NO_ERRORS,             MB_ExceptionTypeDef);
CREATE_DEPRECATED_ALIAS(ILLEGAL_FUNCTION,       ET_ILLEGAL_FUNCTION,      MB_ExceptionTypeDef);
CREATE_DEPRECATED_ALIAS(ILLEGAL_DATA_ADDRESS,   ET_ILLEGAL_DATA_ADDRESS,  MB_ExceptionTypeDef);
CREATE_DEPRECATED_ALIAS(ILLEGAL_DATA_VALUE,     ET_ILLEGAL_DATA_VALUE,    MB_ExceptionTypeDef);
CREATE_DEPRECATED_ALIAS(SLAVE_DEVICE_FAILURE,   ET_SLAVE_DEVICE_FAILURE,  MB_ExceptionTypeDef);

CREATE_DEPRECATED_ALIAS(MB_R_COILS,             FC_R_COILS,               MB_FunctonTypeDef);
CREATE_DEPRECATED_ALIAS(MB_R_DISC_IN,           FC_R_DISC_IN,             MB_FunctonTypeDef);
CREATE_DEPRECATED_ALIAS(MB_R_HOLD_REGS,         FC_R_HOLD_REGS,           MB_FunctonTypeDef);
CREATE_DEPRECATED_ALIAS(MB_R_IN_REGS,           FC_R_IN_REGS,             MB_FunctonTypeDef);
CREATE_DEPRECATED_ALIAS(MB_W_COIL,              FC_W_COIL,                MB_FunctonTypeDef);
CREATE_DEPRECATED_ALIAS(MB_W_HOLD_REG,          FC_W_HOLD_REG,            MB_FunctonTypeDef);
CREATE_DEPRECATED_ALIAS(MB_W_COILS,             FC_W_COILS,               MB_FunctonTypeDef);
CREATE_DEPRECATED_ALIAS(MB_W_HOLD_REGS,         FC_W_HOLD_REGS,           MB_FunctonTypeDef);
CREATE_DEPRECATED_ALIAS(MB_R_DIAGNOSTICS,       FC_R_DIAGNOSTICS,         MB_FunctonTypeDef);
CREATE_DEPRECATED_ALIAS(MB_R_DEVICE_ID,         FC_R_DEVICE_ID,           MB_FunctonTypeDef);
CREATE_DEPRECATED_ALIAS(MB_ERR_R_COILS,         FC_ERR_R_COILS,           MB_FunctonTypeDef);
CREATE_DEPRECATED_ALIAS(MB_ERR_R_DISC_IN,       FC_ERR_R_DISC_IN,         MB_FunctonTypeDef);
CREATE_DEPRECATED_ALIAS(MB_ERR_R_IN_REGS,       FC_ERR_R_IN_REGS,         MB_FunctonTypeDef);
CREATE_DEPRECATED_ALIAS(MB_ERR_R_HOLD_REGS,     FC_ERR_R_HOLD_REGS,       MB_FunctonTypeDef);
CREATE_DEPRECATED_ALIAS(MB_ERR_W_COIL,          FC_ERR_W_COIL,            MB_FunctonTypeDef);
CREATE_DEPRECATED_ALIAS(MB_ERR_W_HOLD_REG,      FC_ERR_W_HOLD_REG,        MB_FunctonTypeDef);
CREATE_DEPRECATED_ALIAS(MB_ERR_W_COILS,         FC_ERR_W_COILS,           MB_FunctonTypeDef);
CREATE_DEPRECATED_ALIAS(MB_ERR_W_HOLD_REGS,     FC_ERR_W_HOLD_REGS,       MB_FunctonTypeDef);
CREATE_DEPRECATED_ALIAS(MB_ERR_R_DIAGNOSTIC,    FC_ERR_R_DIAGNOSTIC,      MB_FunctonTypeDef);
CREATE_DEPRECATED_ALIAS(MB_ERR_R_DEVICE_INFO,   FC_ERR_R_DEVICE_INFO,     MB_FunctonTypeDef);

CREATE_DEPRECATED_ALIAS(MB_BASIC_IDENTIFICATIONS,     RID_BASIC_IDENTIFICATIONS,    MB_ReadDevId);
CREATE_DEPRECATED_ALIAS(MB_REGULAR_IDENTIFICATIONS,   RID_REGULAR_IDENTIFICATIONS,  MB_ReadDevId);
CREATE_DEPRECATED_ALIAS(MB_EXTENDED_IDENTIFICATIONS,  RID_EXTENDED_IDENTIFICATIONS, MB_ReadDevId);
CREATE_DEPRECATED_ALIAS(MB_SPEDIFIC_IDENTIFICATIONS,  RID_SPEDIFIC_IDENTIFICATIONS, MB_ReadDevId);


#define MbAddr DeviceAddr


/** MODBUS_LEGACY_API
  * @}
  */

/** @cond Заглушки отключенных модулей */

#ifndef MODBUS_ENABLE_COILS
    #define MB_Coil_Write_Global(Addr, WriteVal) ET_ILLEGAL_FUNCTION
    #define MB_Coil_Read_Global(Addr, Exception) 0
    #define MB_Process_Read_Coils(modbus_msg) 0
    #define MB_Process_Write_Single_Coil(modbus_msg) 0
    #define MB_Process_Write_Miltuple_Coils(modbus_msg) 0
#endif

#ifndef MODBUS_ENABLE_HOLDINGS
    #define MB_Holding_Write_Global(Addr, WriteVal) ET_ILLEGAL_FUNCTION
    #define MB_Holding_Read_Global(Addr, Exception) 0
    #define MB_Process_Read_Hold_Regs(modbus_msg) 0
    #define MB_Process_Write_Single_Reg(modbus_msg) 0
    #define MB_Process_Write_Miltuple_Regs(modbus_msg) 0
#endif

#ifndef MODBUS_ENABLE_INPUTS
    #define MB_Input_Write_Global(Addr, WriteVal) ET_ILLEGAL_FUNCTION
    #define MB_Input_Read_Global(Addr, Exception) 0
    #define MB_Process_Read_Input_Regs(modbus_msg) 0
#endif

#ifndef MODBUS_ENABLE_DEVICE_IDENTIFICATIONS
    #define MB_WriteSingleObjectToMessage(mbdata, ind, obj) 
    #define MB_WriteObjectsToMessage(modbus_msg, maxidofobj) 
    #define MB_Process_Read_Device_Identifications(modbus_msg) 0
    #define MB_DeviceInentificationInit() 
#endif


#ifndef MODBUS_ENABLE_DIAGNOSTICS
    #define MB_DiagnosticsInit() 
    #define MB_Diagnostics_WriteBit(bit_num, bit_state) 0
    #define MB_Diagnostics_GetBit(bit_num) 0
    #define MB_Process_Diagnostics(modbus_msg) 0
    #define MB_Diagnostics_BusMessageCnt() 
    #define MB_Diagnostics_CommunicationErrorCnt() 
    #define MB_Diagnostics_ExceptionErrorCnt() 
    #define MB_Diagnostics_CharacterOverrunCnt() 
    #define MB_Diagnostics_SlaveMessageCnt() 
    #define MB_Diagnostics_SlaveNoResponseCnt() 
    #define MB_Diagnostics_SlaveNAKCnt() 
    #define MB_Diagnostics_SlaveBusyCnt() 
    #define MB_GetDeviceMode() MODBUS_NORMAL_MODE
#endif



#ifndef MODBUS_ENABLE_MASTER
    #define MB_RespGet_RegisterValue(modbus_msg, reg_addr, reg_value) 0
    #define MB_RespGet_CoilState(modbus_msg, coil_addr, coil_state) 0
    #define MB_RespGet_NumberOfObjects(modbus_msg) 0
    #define MB_RespGet_ObjectById(modbus_msg, obj_id, obj_data, obj_length) 0
    #define MB_RespGet_ObjectByIndex(modbus_msg, index, obj_id, obj_data, obj_length) 0
    #define MB_RespGet_Diagnostic(modbus_msg, data) 0
    #define MB_REQUEST_READ_COILS(slave_addr, start_addr, quantity) {0}
    #define MB_REQUEST_READ_DISCRETE_INPUTS(slave_addr, start_addr, quantity) {0}
    #define MB_REQUEST_READ_HOLDING_REGS(slave_addr, start_addr, quantity) {0}
    #define MB_REQUEST_READ_INPUT_REGS(slave_addr, start_addr, quantity) {0}
    #define MB_REQUEST_WRITE_SINGLE_COIL(slave_addr, coil_addr, value) {0}
    #define MB_REQUEST_WRITE_SINGLE_REG(slave_addr, reg_addr, value) {0}
    #define MB_REQUEST_WRITE_MULTIPLE_COILS(slave_addr, start_addr, quantity, coils_data) {0}
    #define MB_REQUEST_WRITE_MULTIPLE_REGS(slave_addr, start_addr, quantity, regs_data) {0}
    #define MB_REQUEST_DIAGNOSTIC_QUERY(slave_addr, sub_function, data) {0}
    #define MB_REQUEST_RETURN_QUERY_DATA(slave_addr) {0}
    #define MB_REQUEST_RESTART_COMMUNICATIONS(slave_addr, data) {0}
    #define MB_REQUEST_RETURN_DIAGNOSTIC_REGISTER(slave_addr) {0}
    #define MB_REQUEST_FORCE_LISTEN_ONLY_MODE(slave_addr) {0}
    #define MB_REQUEST_CLEAR_COUNTERS_AND_DIAGNOSTIC_REGISTER(slave_addr) {0}
    #define MB_REQUEST_RETURN_BUS_MESSAGE_COUNT(slave_addr) {0}
    #define MB_REQUEST_RETURN_BUS_COMMUNICATION_ERROR_COUNT(slave_addr) {0}
    #define MB_REQUEST_RETURN_SLAVE_EXCEPTION_ERROR_COUNT(slave_addr) {0}
    #define MB_REQUEST_RETURN_SLAVE_MESSAGE_COUNT(slave_addr) {0}
    #define MB_REQUEST_RETURN_SLAVE_NO_RESPONSE_COUNT(slave_addr) {0}
    #define MB_REQUEST_RETURN_SLAVE_NAK_COUNT(slave_addr) {0}
    #define MB_REQUEST_RETURN_SLAVE_BUSY_COUNT(slave_addr) {0}
    #define MB_REQUEST_RETURN_BUS_CHARACTER_OVERRUN_COUNT(slave_addr) {0}
    #define MB_REQUEST_READ_DEVICE_ID_BASIC(slave_addr) {0}
    #define MB_REQUEST_READ_DEVICE_ID_REGULAR(slave_addr) {0}
    #define MB_REQUEST_READ_DEVICE_ID_EXTENDED(slave_addr) {0}
    #define MB_REQUEST_READ_DEVICE_ID_SPECIFIC(slave_addr, object_id) {0}
    #define MB_Master_Collect_Message(hmodbus, modbus_msg, modbus_uart_buff) RS_ERR
    #define MB_Master_Parse_Message(hmodbus, modbus_msg, modbus_uart_buff) RS_ERR
#endif

#ifndef  MODBUS_ENABLE_SLAVE
    #define MB_Slave_Response(hmodbus, modbus_msg) RS_ERR
    #define MB_Slave_Collect_Message(hmodbus, modbus_msg, modbus_uart_buff) RS_ERR
    #define MB_Slave_Parse_Message(hmodbus, modbus_msg, modbus_uart_buff) RS_ERR
#endif 
/** @endcond */
#endif //__MODBUS_COMPAT_H_