/******************** (C) COPYRIGHT 2016 STMicroelectronics ********************
* File Name          : SensorDemo_central_role_main.c
* Author             : AMG - RF FW team 
* Version            : V1.0.0
* Date               : 25-August-2016
* Description        : BlueNRG-1 Sensor Demo central role main file
********************************************************************************
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/
/** @addtogroup BlueNRG1_demonstrations_applications
 * BlueNRG-1 SensorDemo Central \see SensorDemo_central_role_main.c for documentation.
 *
 *@{
 */

/** @} */
/** \cond DOXYGEN_SHOULD_SKIP_THIS
 */
/* Includes ------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include "BlueNRG1_it.h"
#include "BlueNRG1_conf.h"
#include "ble_const.h"
#include "bluenrg1_stack.h"
#include "SDK_EVAL_Config.h"
#include "sleep.h"
#include "gatt_db.h"
#include "master_basic_profile.h"
#include "master_config.h"
#include "SensorDemo_config.h"

#ifndef DEBUG
#define DEBUG 0
#endif

#if DEBUG
#include <stdio.h>
#define PRINTF(...) printf(__VA_ARGS__)
#else
#define PRINTF(...)
#endif


/* Private variables ---------------------------------------------------------*/


/*******************************************************************************
* Function Name  : main.
* Description    : Main routine.
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/
int main(void)
{    

  uint32_t timeTemperature, currentTime;
  uint8_t ret;
   
  /* System Init */
  SystemInit();
  
  /* Identify BlueNRG1 platform */
  SdkEvalIdentification(); 
   
  /* Configure I/O communication channel */
  SdkEvalComUartInit(UART_BAUDRATE);
  
  /* Configure I/O communication channel  */
  SdkEvalComIOConfig(SdkEvalComIOProcessInputData);
  
  /* BlueNRG-1 stack init */
  ret = BlueNRG_Stack_Initialization(&BlueNRG_Stack_Init_params);
  if (ret != BLE_STATUS_SUCCESS) {
    PRINTF("Error in BlueNRG_Stack_Initialization() 0x%02x\r\n", ret);
    while(1);
  }
     
  /* Master Init procedure */
  if (deviceInit() != BLE_STATUS_SUCCESS) {
    PRINTF("Error during the device init procedure\r\n");
  }
  
  /* Start Master Device Discovery procedure */
  if (deviceDiscovery() != BLE_STATUS_SUCCESS) {
    PRINTF("Error during the device discovery procedure\r\n");
  }

  PRINTF("Sensor Demo Central application\r\n");
  
  /* Main loop */
  while(1) {
    /* BLE Stack Tick */
    BTLE_StackTick();
    
    /* Master Tick */
    Master_Process(); 
    
    /* Power Save management */
    BlueNRG_Sleep(SLEEPMODE_WAKETIMER, 0, 0, 0); 
      
    /* Start the connection procedure */
    if (masterContext.startDeviceDisc) {
      deviceConnection();
      masterContext.startDeviceDisc = FALSE;
   }
      
    /* Discovery characteristics of service to enable the notifications/indications */
    if (masterContext.findCharacOfService) {
      findCharcOfService();
      masterContext.findCharacOfService = FALSE;
      timeTemperature = HAL_VTimerGetCurrentTime_sysT32();
    }

    /* Enable the notifications/indications */
    if (masterContext.enableNotif) {
      enableSensorNotifications();
      masterContext.enableNotif = FALSE;
      timeTemperature = HAL_VTimerGetCurrentTime_sysT32();
    }
    /* Read the temperature sensor every 3 sec */
    currentTime = HAL_VTimerGetCurrentTime_sysT32();  
    if ((HAL_VTimerDiff_ms_sysT32(currentTime, timeTemperature)) >= READ_TEMPERATURE_TIMEOUT) {
      timeTemperature = HAL_VTimerGetCurrentTime_sysT32();
      if (masterContext.connHandle != 0xFFFF)
	readTemperature();
    }
  }
}

#ifdef USE_FULL_ASSERT
/*******************************************************************************
* Function Name  : assert_failed
* Description    : Reports the name of the source file and the source line number
*                  where the assert_param error has occurred.
* Input          : - file: pointer to the source file name
*                  - line: assert_param error line source number
* Output         : None
* Return         : None
*******************************************************************************/
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
  ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  
  /* Infinite loop */
  while (1)
  {}
}
#endif

/******************* (C) COPYRIGHT 2015 STMicroelectronics *****END OF FILE****/
/** \endcond
 */
