/*
 * ssl_server.h
 *
 *  Created on: 08.11.2017
 *      Author: balbekova
 */

#ifndef MY_SSL_SERVER_H_
#define MY_SSL_SERVER_H_

#include "common_config.h"
#include "lwip/opt.h"
#include "lwip/arch.h"
#include "lwip/api.h"

#include "mbedtls/ssl.h"

#ifdef SSL_SERVER_ENABLE

#define HTTP_AUTH_ENABLE

#define SSL_TASK_PRIO   ( configMAX_PRIORITIES - 3 )
#define FRAME_SIZE      (1000)

#define SEND_BUF_MAX_LEN      4000
#define RECIVE_BUF_MAX_LEN    2000

typedef enum
{
  SSL_ACCEPT = 0,
  SSL_ERROR,
  SSL_CRITICAL_ERROR,
  SSL_HANDSHAKE,
  SSL_READ,
  SSL_PROCESSING,
  SSL_CLOSE,
  SSL_WRITE,
  SSL_WRITE_OK,
  SSL_WRITE_ERROR,

} SSL_SERVER_STATE;

typedef enum
{
	ALL_ACCESS = 0,
  TIME_ACCESS
} ACCESS_TYPE_t;

typedef enum
{
	COMMON_ANSWER = 0,
  PARAMETER_ANSWER,
	HISTORY_ANSWER,
	UPS_HISTORY_ANSWER

} REQ_TYPE_SEND_t;

typedef enum
{
  SEND_REQUIRED_YES = 0,
  SEND_REQUIRED_NO,
  SEND_REQUIRED_FILE
} REQ_TYPE_t;

struct http_state
{
  char *file;
  u32_t left;
  u32_t reqnum;
  void *locked;
};

struct fs_file {
  char *data;
  int len;
};

void HTTP_Init();

/**
  * @brief  Выводим запросы в консоль
  */
void vTaskPrintHttp(void *pvParameters);

/**
  * @brief  Установка даты производства
  */
void HTTP_Prodate(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);

/**
  * @brief  Парсинг строки настроек
  */
void HTTP_SetSettings(char *buf, uint16_t lenBuf);

/**
  * @brief
  * @retval None
  */
char *HTTP_GetSert(uint32_t reqNum, char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);

/**
  * @brief
  * @retval None
  */
char *HTTP_Upload_Sert(uint32_t reqNum, char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);

/**
  * @brief
  */
void HTTP_SetInfo(char *buf, uint16_t lenBuf);

/**
  * @brief
  */
char* HTTP_SetSettingsPage(uint32_t reqNum, char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);

/**
  * @brief  
  */
char* HTTP_SetInfoPage(uint32_t reqNum, char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);

#ifdef HTTP_AUTH_ENABLE
/**
  * @brief  
  */
char* HTTP_LoginPage(uint32_t reqNum, char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);

/**
  * @brief  
  */
char* HTTP_LogoutPage(uint32_t reqNum, char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);

/**
  * @brief  Смена пароля пользователя
  * @retval None
  */
char *HTTP_ChangeUserPwd(uint32_t reqNum, char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);
#endif
/**
  * @brief  
  */
char* HTTP_UpsHistoryPage(uint32_t reqNum, char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);

/**
  * @brief  
  */
char* HTTP_HistoryPage(uint32_t reqNum, char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);

/**
  * @brief  
  */
char* HTTP_Reset(uint32_t reqNum, char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);

/**
  * @brief  
  */
char* HTTP_Reboot(uint32_t reqNum, char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);

/**
  * @brief  
  */
char* HTTP_Confirm(uint32_t reqNum, char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);

/**
  * @brief  Запуск/останов теста UPS
  */
char* HTTP_UPSTest(uint32_t reqNum, char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);

/**
  * @brief  Выклюение UPS
  */
char* HTTP_UPSshutdown(uint32_t reqNum, char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);

char *HTTP_UPS_Serial(uint32_t reqNum, char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);

/**
  * @brief  Проверка пароля для перехода в режим bootloader
  */
char* HTTP_ConfirmBootPwd(uint32_t reqNum, char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);

/**
  * @brief  Проверка пароля для входа в Web
  */
int HTTP_ConfirmWebPwd(char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);

void HTTP_LOGIN(char *bufOut, uint16_t *lenBufOut);

/**
  * @brief  
  */
char* HTTP_GetRequest(uint32_t reqNum, char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);

/**
  * @brief  
  */
char* HTTP_NoFound(uint32_t reqNum, char *bufIn, char *bufOut, uint16_t lenBufIn, uint16_t *lenBufOut);

/**
  * @brief
  */
uint8_t GetParamValue(char *inStr, char *paramName, char *paramValue, uint8_t *paramLen);

uint8_t GetCookieValue(char *inStr, char *paramName, char *paramValue, uint8_t *paramLen);

/**
  * @brief
  * @retval None
  */
uint8_t GetParamValueInEnd(char *inStr, char *paramName, char *paramValue, uint8_t *paramLen);

void ClearParamString(char *inBuf);

/**
  * @brief  Замена символа в строке
  * @param  *str - входная строка
  * @param  sim1 - символ который надо заменить
  * @param  sim2 - символ на который надо заменить
  */
void HTTP_ReplaceSimbol(char *str, char sim1, char sim2);


// -----------------------------------------------------------------------------
//
//
//  SSL тесты
//
//
// -----------------------------------------------------------------------------

#ifdef __cplusplus
 extern "C" {
#endif


/* Exported types ------------------------------------------------------------*/
typedef struct
{
  uint32_t State;
}rng_state;

void ssl_server(void *pvParameters);


int RandVal(void* arg);
void HTTP_SendHistory(void);
void HTTP_SendLog(void);
void HTTP_Init();
void HTTPS_Init();
int SSL_ReadRoutine(mbedtls_ssl_context *ssl, unsigned char* recvBuf);
char* SSL_ProcessingRoutine(uint16_t* sendLen);
SSL_SERVER_STATE SSL_WriteRoutine(mbedtls_ssl_context *ssl, char *data, int datalen);
SSL_SERVER_STATE SSL_SendFrames(mbedtls_ssl_context *ssl, char *data, int datalen);
SSL_SERVER_STATE SSL_Write(mbedtls_ssl_context *ssl, char *data, int datalen);

#ifdef __cplusplus
}
#endif


#endif

#endif /* SSL_SERVER_H_ */

