/* 
 * File:   WiFiCtrl.h
 * Author: JF
 *
 * Created on December 8, 2018, 6:52 AM
 */

#ifndef WIFICTRL_H
#define	WIFICTRL_H

#include "driver/include/m2m_wifi.h"
#include "socket/include/socket.h"

/*!
*     Used for code portability.
*/

/*!
 * @typedef      void (*tpfNmBspIsr) (void);
 * @brief           Pointer to function.\n
 *                     Used as a data type of ISR function registered by \ref nm_bsp_register_isr
 * @return         None
 */
typedef void (*tpfNmBspIsr)(void);

  /*!
 * @ingroup DataTypes
 * @typedef      unsigned char	uint8;
 * @brief        Range of values between 0 to 255
 */
typedef unsigned char	uint8;

 /*!
 * @ingroup DataTypes
 * @typedef      unsigned short	uint16;
 * @brief        Range of values between 0 to 65535
 */
typedef unsigned short	uint16;

 /*!
 * @ingroup Data Types
 * @typedef      unsigned long	uint32;
 * @brief        Range of values between 0 to 4294967295
 */ 
typedef unsigned long	uint32;


  /*!
 * @ingroup Data Types
 * @typedef      signed char		sint8;
 * @brief        Range of values between -128 to 127
 */
typedef signed char		sint8;

 /*!
 * @ingroup DataTypes
 * @typedef      signed short	sint16;
 * @brief        Range of values between -32768 to 32767
 */
typedef signed short	sint16;

  /*!
 * @ingroup DataTypes
 * @typedef      signed long		sint32;
 * @brief        Range of values between -2147483648 to 2147483647
 */

typedef signed long		sint32;
 //@}






int InitWiFi();
void TickWiFi();
int TurnOFFWiFi();
int CloseSockets();
char GetWiFiSate();

int OpenTerminalServer();
void SendTerminalData(uint8 *data, int size);
void SentTerminalByte(uint8 data);

int OpenNetworkServer();
void SendNetworkData(uint8 *data, int size);
void SentNetworkByte(uint8 data);

int OpenSyslogServer();
void SendSyslogData(uint8 *data, int size);
void SendSyslogByte(uint8 data);
int IsSyslogClientConnected();

int OpenBootloaderServer();
int CloseBootloaderServer();
void SendBootloaderData(uint8 *data, int size);
void SendBootloaderByte(uint8 data);
int IsBootloaderClientConnected();

//#define USE_STATIC_IP


// <<< Use Configuration Wizard in Context Menu >>>
//<h> General network settings in AP (access point) mode
// <s> Network SSID
// <id> app_main_m2m_device_name
#ifndef MAIN_M2M_SSID
#define MAIN_M2M_SSID "WINC3400_00:00"
#endif

// <o> Security type
// <1=> Wi-Fi network is not secured
// <2=> WPA/WPA2 personal(PSK)
// <3=> WEP (40 or 104) OPEN OR SHARED
// <4=> WPA/WPA2 Enterprise.IEEE802.1x
// <id> app_main_m2m_ap_sec
#ifndef MAIN_M2M_AP_SEC
#define MAIN_M2M_AP_SEC 1
#endif

// <s> Security key
// <id> app_main_m2m_ap_key
#ifndef MAIN_M2M_AP_KEY
#define MAIN_M2M_AP_KEY "12345FFFFF"
#endif

// <o> SSID mode
// <0=>SSID is visible to others
// <1=>SSID is hidden
// <id> app_main_m2m_ap_ssid_mode
#ifndef MAIN_M2M_AP_SSID_MODE
#define MAIN_M2M_AP_SSID_MODE 0
#endif
//</h>
//<h> DHCP server IP address in AP (access point) mode
// <o> DHCP server IP address 1 <0-255>
// <id> app_ip_address_1
#ifndef DHCP_IP_ADDRESS_1
#define DHCP_IP_ADDRESS_1 192
#endif

// <o> DHCP server IP address 2 <0-255>
// <id> app_ip_address_2
#ifndef DHCP_IP_ADDRESS_2
#define DHCP_IP_ADDRESS_2 168
#endif

// <o> DHCP server IP address 3 <0-255>
// <id> app_ip_address_3
#ifndef DHCP_IP_ADDRESS_3
#define DHCP_IP_ADDRESS_3 50
#endif

// <o> DHCP server IP address 4 <0-255>
// <id> app_ip_address_4
#ifndef DHCP_IP_ADDRESS_4
#define DHCP_IP_ADDRESS_4 1
#endif
//</h>
// <<< end of configuration section >>>

//Acces Point settings
#define HOME_AP_SEC_TYPE M2M_WIFI_SEC_WPA_PSK
//#define HOME_AP_NAME "ImprVEmard"
//#define HOME_AP_PWD "12345fffff"

#define HOME_AP_NAME "ChaletVilleEmard"
//#define HOME_AP_NAME "ElRouteurDuChalet"
//#define HOME_AP_NAME "LeChalet"
#define HOME_AP_PWD "Evinrude30"

#define TERMINAL_SERVER_PORT 85
#define NETWORK_SERVER_PORT 86
#define SYSLOG_SERVER_PORT 87
#define BOOTLOADER_SERVER_PORT 99

#define AUTH_CREDENTIALS {M2M_802_1X_USR_NAME, M2M_802_1X_PWD }

//Module Static IP settings
#define STATIC_IP_ADDRESS_1 192
#define STATIC_IP_ADDRESS_2 168
#define STATIC_IP_ADDRESS_3 30
#define STATIC_IP_ADDRESS_4 127

#define GATEWAY_ADDRESS_1 192
#define GATEWAY_ADDRESS_2 168
#define GATEWAY_ADDRESS_3 30
#define GATEWAY_ADDRESS_4 1

#define DEFAULT_DNS_ADD_1 8
#define DEFAULT_DNS_ADD_2 8
#define DEFAULT_DNS_ADD_3 8
#define DEFAULT_DNS_ADD_4 8

#define ALT_DNS_ADD_1 8
#define ALT_DNS_ADD_2 8
#define ALT_DNS_ADD_3 4
#define ALT_DNS_ADD_4 4

#define SUBNET_MASK_1 255
#define SUBNET_MASK_2 255
#define SUBNET_MASK_3 255
#define SUBNET_MASK_4 0

#define IP_TO_U32(add1,add2,add3,add4) (((add4<<24)&0xFF000000) | ((add3<<16)&0x00FF0000) | ((add2<<8)&0x0000FF00) | (add1&0x000000FF))

/** Using broadcast address for simplicity. */
#define MAIN_SERVER_PORT (80)

/** Using IP address. */
#define IPV4_BYTE(val, index) ((val >> (index * 8)) & 0xFF)

/** Send buffer of TCP socket. */
#define MAIN_PREFIX_BUFFER "GET /data/2.5/weather?q="
#define MAIN_POST_BUFFER                                                                                               \
	"&mode=xml&units=metric&appid=c592e14137c3471fa9627b44f6649db4 HTTP/1.1\r\nHost: "                                 \
	"api.openweathermap.org\r\nAccept: */*\r\n\r\n"
/** Weather information provider server. */
#define MAIN_WEATHER_SERVER_NAME "openweathermap.org"

/** Input City Name. */
#define MAIN_CITY_NAME "london"

/** Receive buffer size. */
#define MAIN_WIFI_M2M_BUFFER_SIZE 1024

#define MAIN_M2M_DHCP_SERVER_IP                                                                                        \
	{                                                                                                                  \
		DHCP_IP_ADDRESS_1, DHCP_IP_ADDRESS_2, DHCP_IP_ADDRESS_3, DHCP_IP_ADDRESS_4                                     \
	}
#define MAIN_HTTP_PROV_SERVER_DOMAIN_NAME "JF.com"

#define MAIN_MAC_ADDRESS                                                                                               \
	{                                                                                                                  \
		0xf8, 0xf0, 0x05, 0x45, 0xD4, 0x84                                                                             \
	}

#define MAIN_HEX2ASCII(x) (((x) >= 10) ? (((x)-10) + 'A') : ((x) + '0'))

#define TEMPERATURE_ABS(a) (((a) > 0) ? (a) : -(a))

static tstrM2MAPConfig gstrM2MAPConfig = {MAIN_M2M_SSID,
                                          1,
                                          0,
                                          sizeof(MAIN_M2M_AP_KEY) - 1,
                                          MAIN_M2M_AP_KEY,
                                          MAIN_M2M_AP_SEC,
                                          MAIN_M2M_AP_SSID_MODE,
                                          MAIN_M2M_DHCP_SERVER_IP};

static const char gacHttpProvDomainName[] = MAIN_HTTP_PROV_SERVER_DOMAIN_NAME;

static uint8 gau8MacAddr[]   = MAIN_MAC_ADDRESS;
static sint8 gacDeviceName[] = MAIN_M2M_SSID;

#define MAIN_WAITING_TIME 3000
#define MAIN_RETRY_COUNT 10

extern  SOCKET TerminalSocket;


#endif	/* WIFICTRL_H */

