#ifndef _requester
#define _requester
#include "main.h"
#include "can.h"
#include "rtc.h"
#include "tim.h"

#define _DEMO

#define ONLINE 1;
#define OFFLINE 0;

#define TIM_REQUESTER_Prescaler 64000

//CAN Filter
#define ID_MAIN_DEVICE 0x000
#define CURRENT_ID_DEVICE 0x002
#define CAN_IDE_32 0b00000100            // Для 32-х битного масштаба

#define CAN_DATA_TYPE_FILTER 0x1F000000
#define CAN_DEVICE_ID_FILTER 0x000000FF
#define CAN_SENSOR_TYPE_FILTER 0x00F80000
#define CAN_SENSOR_ID_FILTER 0x0007FF00


void REQUESTER_Init(void);
void REQUESTER_MainWhile(void);
void Boot_SystemClock_Config(void);
void REQUESTER_AnalogProcessing(void);
void REQUESTER_BroadcastProcessing(void);
void REQUESTER_DiscreticProcessing(void);
void REQUESTER_ModbusProcessing(void);
void REQUESTER_RTC_SYNC(uint8_t *data);
void REQUESTER_Pulse_TIM_Handler(void);
void Boot_SystemClock_Config(void);
void REQUESTER_CAN_FILTERS(void);

union Byte{
	struct bitfield{
		unsigned bit0:1;
		unsigned bit1:1;
		unsigned bit2:1;
		unsigned bit3:1;
		unsigned bit4:1;
		unsigned bit5:1;
		unsigned bit6:1;
		unsigned bit7:1;
	}Bitfield;
	char AllBit;
};

_Bool IsLeapYear(uint8_t year);

#define COUNT_OF_MODBUS_SECTIONS 1
#define MODBUS_SECTION_MASK 0
#define MODBUS_SECTION_FILTER 1

#define SensorToModbusRegister(SensorType, SensorID) SensorType << 11 | SensorID
#define HighByteOfWord(WORD) (WORD>>8)&0xFF
#define LowByteOfWord(WORD) WORD&0xFF

//Route mode
#define ROUTE_MASTER 0
#define ROUTE_SLAVE 1

//Data Types
#define DATA_TYPE_BROADCAST 0b0000
#define DATA_TYPE_DISCRETE 0b0001
#define DATA_TYPE_ANALOG 0b0010
#define DATA_TYPE_MODBUS_COIL 0b0011
#define DATA_TYPE_MODBUS_DISCRETE 0b0100
#define DATA_TYPE_MODBUS_HOLDING 0b0101
#define DATA_TYPE_MODBUS_INPUT 0b0110
#define DATA_TYPE_ERROR 0b0111
#define DATA_TYPE_PULSE 0b1111

//Sensor Types for DATA_TYPE_BROADCAST
#define SENSOR_TYPE_STATUS 0b00000
#define SENSOR_TYPE_ONOFF 0b00001
#define SENSOR_TYPE_RTCSETUP 0b00010

//Sensor Types for DATA_TYPE_DISCRETE
#define SENSOR_TYPE_ACCIDENT 0b00000
#define SENSOR_TYPE_WARNING 0b00001
#define SENSOR_TYPE_CONTROL_SIGNALS 0b00010
#define SENSOR_TYPE_FLAGS 0b00011
#define SENSOR_TYPE_RESET 0b00100
#define SENSOR_TYPE_CHANGE_MODE 0b00101
#define SENSOR_TYPE_REQUEST_LIST_OF_PARAMETERS 0b00110

//Sensor Types for DATA_TYPE_ANALOG
#define SENSOR_TYPE_UNIVERSAL 0b00000
#define SENSOR_TYPE_U 0b00001
#define SENSOR_TYPE_I 0b00010
#define SENSOR_TYPE_T 0b00011

//Error Code
#define NONEXISTENT_ELEMENT 0x01

#define HighIdFilter(x) x<<24


struct controlflags{
	unsigned IsPulse:1;
	unsigned IsRtrMode:1;
};

//Device settings
union ext_ID{
	struct ext_ID_fields{
		unsigned DeviceID:8;
		unsigned SensorID:11;
		unsigned SensorType:5;
		unsigned DataType:4;
		unsigned Route:1;
	}Fields;
	unsigned int BitAll:29;	
};

union ext_ID_Modbus{
	struct ext_ID_Modbus_fields{
		unsigned DeviceID:8;
		unsigned CountReg:8;
		unsigned StrAdr:8;
		unsigned DataType:4;
		unsigned Route;
	}Fields;
	unsigned int BitAll:29;	
};

struct device
{
	unsigned Status:1;
	union ext_ID ExtID;
	unsigned TimeFromLastPulse;
	unsigned LastPulseStep;
};

struct data
{
	struct T_sens{
		char info[256];
		unsigned SensID;
		unsigned AvrgValue;
		unsigned LastTenValues[10];
	}TS;
	
	struct U_sens{
		char info[256];
		unsigned SensID;
		unsigned AvrgValue;
		unsigned LastTenValues[10];
	}Uu, Uv, Uw;
	
	struct I_sens{
		char info[256];
		unsigned SensID;
		unsigned AvrgValue;
		unsigned LastTenValues[10];		
	}Iu, Iv, Iw;
};

struct received_request{
	union ext_ID RequestedExtID;
	unsigned RequestedDLC;
	uint8_t RxData[8]; 
	
	//DATA_TYPE_DISCRETIC
	union discreticflags{
		struct discretictype{
			unsigned Request_Accident:1;
			unsigned Request_Warning:1;
			unsigned Request_Control_Signals:1;
			unsigned Request_Flags:1;
			unsigned Request_Reset:1;
			unsigned Request_List_of_Parameters:1;
		}DiscreticType;
		unsigned AllFlags:5;
	}DiscreticFlags;
	
	//DATA_TYPE_BROADCAST
	union broadcastflags{
		struct broadcasttype{
			unsigned Request_Status:1;
			unsigned Request_OnOff:1;
			unsigned Request_RTC_Setup:1;
		}BroadcastType;
		unsigned AllFlags:3;
	}BroadcastFlags;
	
	//DATA_TYPE_ANALOG
	union analogflags{
		struct analogtype{
			unsigned Request_Universal_Sens:1;
			unsigned Request_U_Sens:1;
			unsigned Request_I_Sens:1;
			unsigned Request_T_Sens:1;
		}AnalogType;
		unsigned AllFlags:4;
	}AnalogFlags;
	
	//DATA_TYPE_MODBUS
	union modbusflags{
		struct modbustype{
			unsigned Coil:1;
			unsigned Discrete:1;
			unsigned Holding:1;
			unsigned Input:1;
		}ModbusType;
		unsigned AllFlags:4;
	}ModbusFlags;
	
	union sensor_To_Modbus{
		struct modbus{
			unsigned Count:8;
			unsigned StrAdr:8;
		}Modbus;
		unsigned Sensor:16;
	}SensorToModbus;
	
	unsigned ModbusFlag:1;
};

#endif
