/*
 * consolehandler.h
 *
 */ 
#ifndef CONSOLEHANDLER_H_
#define CONSOLEHANDLER_H_

// avrlibc
#include <stdio.h>
// Bitcloud
#include <usart.h>
// firmware
#include "main.h"
#include "settingshandler.h"

#define DEBUG

#define USART0_TX_BUFFER_SIZE 512
#define USART0_RX_BUFFER_SIZE 128

#define DBG_SET_DEFAULT_SETTINGS	"SET DEFAULT SETTINGS"
#define DBG_NVM_MEMORY_ERROR		"NVM MAP WRONG CRC"
#define DBG_NVM_FAIL				"NVM FAIL"
#define DBG_NVM_WRONG_CRC			"NVM PACKET WRONG CRC, SKIPPING"
#define DBG_NVM_MAP_RESET			"NVM MAP RESET"
#define DBG_NVM_FULL				"NVM full"
#define DBG_DATA_SEND_SUCCESS		"DATA SEND"

#define CONSOLE_TEXT_EEPROM_WRITE_ERROR		"WRITE EEPROM ERROR"
#define CONSOLE_TEXT_EEPROM_READ_ERROR		"WRITE EEPROM ERROR"
#define CONSOLE_TEXT_APP_START				"SENSOR NODE START"
#define CONSOLE_TEXT_DATA_WAIT				"Waiting for data"
#define CONSOLE_TEXT_CHOICE					"Your Choice: "
#define CONSOLE_TEXT_MENU_OPTION_MENU						"Menu:"
#define CONSOLE_TEXT_MENU_OPTION_READ_MEASUREMENT			"1 - Read Measurement Settings"
#define CONSOLE_TEXT_MENU_OPTION_READ_NETWORK				"2 - Read Network Settings"
#define CONSOLE_TEXT_MENU_OPTION_WRITE_MEASUREMENT			"3 - Write Measurement Settings"
#define CONSOLE_TEXT_MENU_OPTION_WRITE_NETWORK				"4 - Write Network Settings"
#define CONSOLE_TEXT_MENU_OPTION_EXIT						"ANY - Exit"
#define CONSOLE_TEXT_DATA_VALID				"Data Valid"
#define CONSOLE_TEXT_DATA_INVALID			"Data Invalid"
#define CONSOLE_TEXT_TIMEOUT				"Timeout"

#define CRLF		"\r\n"
#define CRLF_LEN	2

typedef void (*ConsoleReadSettingsCallback_t)(uint8_t settingsMask); 

#define CONSOLE_SETTINGS_TIMEOUT	15000

#define CONSOLE_MENU_RECV_LEN 3  // option +  "\r\n"
#define CONSOLE_MEASUREMENT_SETTINGS_LEN sizeof(TSensorNodeEEPROM_t)
#define CONSOLE_NETWORK_SETTINGS_LEN sizeof(TNetworkSettings_t)

typedef enum {
	CONSOLE_MENU,
	CONSOLE_EXIT,
	CONSOLE_READ_SENSOR_NODE_SETTINGS,
	CONSOLE_READ_NETWORK_SETTINGS,
	CONSOLE_WRITE_SENSOR_NODE_SETTINGS,
	CONSOLE_WRITE_NETWORK_SETTINGS,
} ConsoleRxState_t;

extern result_t ConsoleInit(void);

extern void ConsoleReadSettings(uint8_t *consoleBuffer, ConsoleReadSettingsCallback_t callback);

extern void ConsolePrintStr(char *str);

extern void ConsolePrintStrCRLF(char *str);

extern void ConsolePrintHex(uint8_t *str, uint8_t len);

extern void ConsoleDebug(char *str);

extern void ConsoleDebugNumber(uint16_t num);

extern void ConsoleDebugSensorNodeState(SensorNodeState_t state);

extern void ConsolePrintArray(uint8_t *array, uint8_t len);

#endif /* CONSOLEHANDLER_H_ */