/*
 * socket.h
 *
 * Created: 14/08/2018 9:38:15
 *  Author: Ledo
 */ 


#ifndef SOCKET_H_
#define SOCKET_H_

#include <stdio.h>
#include <string.h>
#include "globales.h"
#include "W5200.h"


	//variables
	//public:
	uint8_t Mac[6];// = {0x00, 0x08, 0xDC, 0x01, 0x02, 0x03};//MAC Address
	uint8_t Ip[4];// = {192, 168, 0, 22};//IP Address
	uint8_t GateWay[4];// = {192, 168, 0, 1};//Gateway Address
	uint8_t SubNet[4] ;//= {255, 255, 255, 0};//SubnetMask Address
	uint8_t Dest_IP[4];// = {192, 168, 0, 38};//IP Address;
	uint16_t Dest_PORT;// = 3000;
	uint16_t local_port;//was static
	
	uint8_t txsize[MAX_SOCK_NUM];
	uint8_t rxsize[MAX_SOCK_NUM];
	uint8_t ch_status[MAX_SOCK_NUM];
	//uint8_t TX_BUF[TX_RX_MAX_BUF_SIZE]; // TX Buffer for applications
	//uint8_t RX_BUF[TX_RX_MAX_BUF_SIZE]; // RX Buffer for applications
	uint8_t rxBuffer[64]; // RX Buffer for PC application
	uint8_t txBuffer[64]; // TX Buffer for PC application
	uint8_t rxBufferB[64]; // RX Buffer for application Modbus
	uint8_t txBufferB[64]; // TX Buffer for application Modbus


	//protected:
	//private:
	uint8_t I_STATUS[MAX_SOCK_NUM];//was static
	uint16_t SMASK[MAX_SOCK_NUM]; /**< Variable for Tx buffer MASK in each channel *///was static
	uint16_t RMASK[MAX_SOCK_NUM]; /**< Variable for Rx buffer MASK in each channel *///was static
	uint16_t SSIZE[MAX_SOCK_NUM]; /**< Max Tx buffer size by each channel *///was static
	uint16_t RSIZE[MAX_SOCK_NUM]; /**< Max Rx buffer size by each channel *///was static
	uint16_t SBUFBASEADDRESS[MAX_SOCK_NUM]; /**< Tx buffer base address by each channel *///was static
	uint16_t RBUFBASEADDRESS[MAX_SOCK_NUM]; /**< Rx buffer base address by each channel *///was static
	uint8_t windowfull_retry_cnt[MAX_SOCK_NUM];
	uint16_t pre_sent_ptr, sent_ptr;

	//functions
	//public:
	uint8_t socket(SOCKET s, uint8_t protocol, uint16_t port, uint8_t flag); // Opens a socket(TCP or UDP or IP_RAW mode)
	uint8_t listen(SOCKET s);	// Establish TCP connection (Passive connection)
	void close(SOCKET s); // Close socket
	uint8_t connect(SOCKET s, uint8_t * addr, uint16_t port); // Establish TCP connection (Active connection)
	void disconnect(SOCKET s); // disconnect the connection
	uint16_t send(SOCKET s, const uint8_t * buf, uint16_t len, bool retry); // Send data (TCP)
	uint16_t recv(SOCKET s, uint8_t * buf, uint16_t len);	// Receive data (TCP)
	uint16_t sendto(SOCKET s, const uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t port); // Send data (UDP/IP RAW)
	uint16_t recvfrom(SOCKET s, uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t  *port); // Receive data (UDP/IP RAW)
	uint16_t igmpsend(SOCKET s, const uint8_t * buf, uint16_t len);
	void setSn_TTL(SOCKET s, uint8_t ttl);
	void send_data_processing(SOCKET s, uint8_t *wizdata, uint16_t len);
	void recv_data_processing(SOCKET s, uint8_t *wizdata, uint16_t len);
	void write_data(SOCKET s, volatile uint8_t * src, volatile uint8_t * dst, uint16_t len);
	void read_data(SOCKET s, volatile uint8_t * src, volatile uint8_t * dst, uint16_t len);
	void setSn_MSS(SOCKET s, uint16_t Sn_MSSR0); // set maximum segment size
	void setSn_PROTO(SOCKET s, uint8_t proto); // set IP Protocol value using IP-Raw mode
	uint8_t getSn_IR(SOCKET s); // get socket interrupt status
	uint8_t getSn_SR(SOCKET s); // get socket status
	uint16_t getSn_TX_FSR(SOCKET s); // get socket TX free buf size
	uint16_t getSn_RX_RSR(SOCKET s); // get socket RX recv buf size
	void initialize( void );
	uint8_t getISR(uint8_t s);
	void putISR(uint8_t s, uint8_t val);
	uint16_t getIINCHIP_RxMAX(uint8_t s);
	uint16_t getIINCHIP_TxMAX(uint8_t s);
	uint16_t getIINCHIP_RxMASK(uint8_t s);
	uint16_t getIINCHIP_TxMASK(uint8_t s);
	uint16_t getIINCHIP_RxBASE(uint8_t s);
	uint16_t getIINCHIP_TxBASE(uint8_t s);
	void setRTR(uint16_t timeout);
	void setRCR(uint8_t retry);
	void ListenServer(SOCKET s, uint16_t port);
	void ListenPc(SOCKET s, uint16_t port);
	void readDatos(void);
	void Set_defaults(void);
	void Read_eeprom_params(void);
	//protected:
	//private:
	uint16_t wiz_write_buf(uint16_t addr,uint8_t* buf,uint16_t len);
	uint16_t wiz_read_buf(uint16_t addr, uint8_t* buf,uint16_t len);
	uint8_t incr_windowfull_retry_cnt(uint8_t s);
	void init_windowfull_retry_cnt(uint8_t s);







#endif /* SOCKET_H_ */