/******************************* (C) LiteMesh **********************************
 * @module  Console
 * @file    portConsole.c
 * @version 1.0.0
 * @date    XX.XX.XXXX
 * $brief   Port functions console lib
 *******************************************************************************
 * @history     Version  Author         Comment
 * XX.XX.XXXX   1.0.0    Telenkov D.A.  First release.
 *******************************************************************************
 */
#include "common_config.h" 
#include "stm32f4xx.h" 
#include "port_microrl.h"
#include "microrl.h"
#include "config.h"
#include "sntp_api.h"
#include "gpio.h"
#include "bt_6701_settings.h"
#include "commands_api.h"

#ifdef PRINTF_STDLIB
#include <stdio.h>
#endif
#ifdef PRINTF_CUSTOM
#include "tinystdio.h"
#endif
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

#define _AVR_DEMO_VER "1.0"

// definition commands word
#define _CMD_HELP        "help"
#define _CMD_CLEAR       "clear"
#define _CMD_CLR         "clear_port"
#define _CMD_LED         "led"
#define _CMD_ETH         "ETH"
#define _CMD_PSVOLT      "PSVOLT"
#define _CMD_LOADVOLT    "LOADVOLT"
#define _CMD_BATVOLT     "BATVOLT"
#define _CMD_BATVOLTK    "BATVOLTK"
#define _CMD_BATVOLTR    "BATVOLTR"
#define _CMD_BAT1VOLT    "BAT1VOLT"
#define _CMD_BAT2VOLT    "BAT2VOLT"
#define _CMD_BAT3VOLT    "BAT3VOLT"
#define _CMD_BAT4VOLT    "BAT4VOLT"
#define _CMD_BATCUR      "BATCUR"
#define _CMD_LOADCUR     "LOADCUR"
#define _CMD_LOADCURK    "LOADCURK"
#define _CMD_DIDOOR      "DIDOOR"
#define _CMD_DIRV        "DIRV"
#define _CMD_LOW150      "LOW150"
#define _CMD_HILOW230    "HILOW230"
#define _CMD_HIGH264     "HIGH264"
#define _CMD_MPSOFF      "MPSOFF"
#define _CMD_CANTX       "CANTX"
#define _CMD_CANRX       "CANRX"
#define _CMD_DAC         "DAC"
#define _CMD_SD          "SD"
#define _CMD_ONEWIRE     "OW"
#define _CMD_SET         "SET"
#define _CMD_DEF         "DEF"
#define _CMD_RTC         "RTC"
#define _CMD_SERNO       "SERNO"

#define _CMD_STATUS      "STATUS"
#define _CMD_FREQ        "FREQ"
#define _CMD_50HZ        "50HZ"
#define _CMD_LOW         "LOW"
#define _CMD_HILOW       "HILOW"
#define _CMD_HI          "HI"
#define _CMD_T2READY     "T2READY"

#define _CMD_U232U485    "U232U485"
#define _CMD_DRY         "DRY"
#define _CMD_U232F       "U232F"

#define _CMD_RST         "reset"

#define _CMD_SPIFLASH    "SPIFLASH"
#define _CMD_U232        "U232"

// arguments for set/clear
#define _SCMD_PB  "port_b"
#define _SCMD_PD  "port_d"

#define _NUM_OF_CMD 
#define _NUM_OF_SETCLEAR_SCMD 2

//available  commands
char * keyworld [] = {
  _CMD_HELP,
  _CMD_CLEAR,
  _CMD_LED,
  _CMD_ETH,
  _CMD_PSVOLT,
  _CMD_LOADVOLT,
  _CMD_BATVOLT,
  _CMD_BATVOLTK,
  _CMD_BATVOLTR,
  _CMD_BAT1VOLT,
  _CMD_BAT2VOLT,
  _CMD_BAT3VOLT,
  _CMD_BAT4VOLT,
  _CMD_BATCUR,
  _CMD_LOADCUR,
  _CMD_LOADCURK,
  _CMD_DIDOOR,
  _CMD_DIRV,
  _CMD_LOW150,
  _CMD_HILOW230,
  _CMD_HIGH264,
  _CMD_MPSOFF,
  _CMD_CANTX,
  _CMD_CANRX,
  _CMD_DAC,
  _CMD_SD,
  _CMD_ONEWIRE,
  _CMD_SET,
  _CMD_DEF,
  _CMD_RTC,
  _CMD_SERNO,
  
  _CMD_STATUS,
  _CMD_FREQ,
  _CMD_50HZ,
  _CMD_LOW,
  _CMD_HILOW,
  _CMD_HI,
  _CMD_T2READY,
  
  _CMD_U232U485,
  _CMD_DRY,
  _CMD_U232F,
  
  _CMD_RST,

  _CMD_SPIFLASH,
  _CMD_U232
};

// 'set/clear' command argements
char * set_clear_key [] = {_SCMD_PB, _SCMD_PD};

// array for comletion
char * compl_world [_NUM_OF_CMD + 1];

microrl_t rl;
microrl_t *prl = &rl;

// параметр команды
#define PARAM_LEN  20
char param[PARAM_LEN];

float value = 0.0;
	
extern bool testLed;
extern bool testEthernet;
extern bool testDef;
extern bool testSet;
extern bool testRtc;
extern bool testSerno;
extern bool testT2Ready;

/**
  * @brief  
  * @retval 
  */
void MICRORL_Init(void)
{
  microrl_init(prl, print);
  microrl_set_execute_callback (prl, execute);
  
}

/**
  * @brief  Print callback for microrl library
  * @retval 
  */
void print (const char *str)
{
  //printf(str);
}

/**
  * @brief  
  * @retval 
  */
void MICRORL_GetChar(uint8_t ch)
{
  microrl_insert_char(prl, ch);
}

//*****************************************************************************
// execute callback for microrl library
// do what you want here, but don't write to argv!!! read only!!
int execute (int argc, const char * const * argv)
{
	int i = 0;
	uint16_t len = 0;
	
	// just iterate through argv word and compare it with your commands
	while (i < argc) 
	{
	  memset(param, 0, PARAM_LEN);  
	  
	  if (strcmp (argv[i], _CMD_HELP) == 0) 
      {
 		print ("microrl library v");
		print (MICRORL_LIB_VER);
		print ("\n\r");
		print_help ();        // print help
	  } 
      else if (strcmp (argv[i], _CMD_CLEAR) == 0) 
      {
		print ("\033[2J");    // ESC seq for clear entire screen
		print ("\033[H");     // ESC seq for move cursor at left-top corner
	  }  
	  /* Тестовая команда */
      else if (strcmp (argv[i], _CMD_LED) == 0) 
      {
		//testLed = true;
		// TODO Убрать затычку
		printf("STATUS T1OK\r");
		return 0;
      } 
	  /* Тест Ethernet */
	  else if (strcmp (argv[i], _CMD_ETH) == 0) 
      {
        COM_SetMAC();
		//testEthernet = true;
        TEST_SetTest(TEST_ETHERNET);
        print ("\n\r");
        return 0;
      }
	  /* Тест кнопки SET */
	  else if (strcmp (argv[i], _CMD_SET) == 0) 
      {
		//testSet = true;
		TEST_SetTest(TEST_SET);
		print ("\n\r");
		return 0;
      }
	  /* Тест кнопки DEF */
	  else if (strcmp (argv[i], _CMD_DEF) == 0) 
      {
		//testDef = true;
        TEST_SetTest(TEST_DEF);
		print ("\n\r");
		return 0;
      }
	  /* Тест RTC */
	  else if (strcmp (argv[i], _CMD_RTC) == 0) 
      {
		if (++i < argc)
        {
          len = strlen(argv[i]);
          strncpy(param, argv[i], len);
		  SNTP_SetServerAddr(param);
		  TEST_SetTest(TEST_RTC);
		 // testRtc = true;
          print ("\n\r");
          return 0;
        }
		print ("\n\r");
      }
	  /* Установка SERNO */
	  else if (strcmp (argv[i], _CMD_SERNO) == 0) 
      {
		if (++i < argc)
        {
          len = strlen(argv[i]);
          strncpy(param, argv[i], len);
		  SETTINGS_SetSerno(param);
		  //testSerno = true;
          TEST_SetTest(TEST_SERNO);
          print ("\n\r");
          return 0;
        }
		print ("\n\r");
      }
	  
      /* -------------------------------------------------------------------- */
      /*                        Тесты для SmartUPS                            */
      
      /* Тест сухих контактов */
	  else if (strcmp (argv[i], _CMD_DRY) == 0) 
      {
		//testSet = true;
        TEST_SetTest(TEST_DRY);
		print ("\n\r");
		return 0;
      }
      
      /* Тест дополнительных пинов RS232 */
	  else if (strcmp (argv[i], _CMD_U232) == 0)
      {
		//testSet = true;
        TEST_SetTest(TEST_U232);
		print ("\n\r");
		return 0;
      }

	  /* Тест spi flash */
	  else if (strcmp (argv[i], _CMD_SPIFLASH) == 0)
	   {
		//testSet = true;
		 TEST_SetTest(TEST_SPIFLASH);
		print ("\n\r");
		return 0;
	   }
#ifdef DALLAS_SENSOR_ENABLE
     	/* Тест линии OW */
      else if (strcmp (argv[i], _CMD_ONEWIRE) == 0)
      {
        TEST_SetTest(TEST_OW);
        print ("\n\r");
        return 0;
      }
#endif
	  /* -------------------------------------------------------------------- */
	  /*                          Тесты этапа Т2                              */
	  /* Статус тестера */
	  else if (strcmp (argv[i], _CMD_STATUS) == 0) 
      {
		//printf("STATUS %s\r", sSettings.sFlags.testState);
        printf("STATUS %s\r", SETTINGS_GetTestState());
		return 0;
      }
	  /* T2READY */
	  else if (strcmp (argv[i], _CMD_T2READY) == 0) 
      {
		testT2Ready = true;		
		return 0;
      }
	  
	  /* Перезагрузить контроллер */
      else if (strcmp (argv[i], _CMD_RST) == 0) 
      {
        NVIC_SystemReset();
      }  
      else 
      {
		print ("command: '");
		print ((char*)argv[i]);
		print ("' Not found.\n\r");
	  }
	  i++;
	}
	return 0;
}

void print_help (void)
{
	print ("Use TAB key for completion\n\rCommand:\n\r");
	print ("\tclear               - clear screen\n\r");
    print ("\tdac                 - send test value\n\r");
}

/******************************* (C) LiteMesh *********************************/
