
/**************************************************************************//**
  \file blank.c
  
  \brief: Coordinator

  \author: Bc. Jan Popelka
		   ID: 106726 

******************************************************************************/

// hlavickove soubory Bitcloudu
#include <stdio.h>
#include <appTimer.h>
#include <zdo.h>
#include <taskManager.h>
#include <leds.h>
#include <usart.h>
#include <adc.h> 
#include <stdio.h>
#include <types.h>

// hlavickove soubory Aplikace
#include "blank.h"
#include "parameters.h"


// function declaration
static void ZDO_StartNetworkConf(ZDO_StartNetworkConf_t *confirmInfo);
static void APS_DataIndication(APS_DataInd_t *ind);

static AsduMessage_t AsduMessage; 

// USART Tx buffer
static uint8_t usartTxBuffer[USART_TX_BUFFER_SIZE];

// app state machine init
static uint8_t appState = APP_STATE_INIT;

uint8_t last_seq, next_seq, header = 0;
uint8_t error = 1;


// definice SimpleDescriptoru
SimpleDescriptor_t simpleDescriptor = { 1, 1, 1, 1, 0, 0, NULL, 0, NULL };


// Register end point struct
static APS_RegisterEndpointReq_t endpointParams = {
	.simpleDescriptor = &simpleDescriptor,
	.APS_DataInd = APS_DataIndication,
};


static HAL_UsartDescriptor_t usartDescriptor =
{
  .tty             = APP_USART_CHANNEL,
  .mode            = USART_MODE_ASYNC,
  .baudrate        = APP_USART_BAUDRATE,
  .dataLength      = APP_USART_DATA,
  .parity          = APP_USART_STOPBIT,
  .stopbits        = USART_STOPBIT_1,
  .rxBuffer        = NULL,
  .rxBufferLength  = 0,
  .txBuffer        = usartTxBuffer,
  .txBufferLength  = USART_TX_BUFFER_SIZE,
  .rxCallback      = NULL,
  .txCallback      = NULL,
  .flowControl     = USART_FLOW_CONTROL_NONE,
};



//The parameters for the network start request
static ZDO_StartNetworkReq_t startNetwork = {
	.ZDO_StartNetworkConf = ZDO_StartNetworkConf
};


//The data indication callback to handle data reception on the endpoint
void APS_DataIndication(APS_DataInd_t *ind)
{
   
   memcpy(&AsduMessage, ind->asdu, sizeof(AsduMessage));
   BSP_ToggleLed(LED_YELLOW);
   error = error -1;
   if(error > 0) BSP_OnLed(LED_RED);
   if(ind->status == APS_SUCCESS_STATUS)
   {
	  	char pom[127];
		if(header == 0){
			 
			 snprintf(pom, sizeof(pom), "TEXT; TX; SEQ;\n");
			 HAL_WriteUsart(&usartDescriptor, (uint8_t*)pom, strlen(pom));
		}			 
		header = 1;
			
		last_seq = next_seq;
		
	
		snprintf(pom, sizeof(pom), "%s; %d; %d;\n", AsduMessage.payload, AsduMessage.curr_txPwr, AsduMessage.seq);
		HAL_WriteUsart(&usartDescriptor, (uint8_t*)pom, strlen(pom)); 
		next_seq = AsduMessage.seq;
		error = next_seq - last_seq;
		
   }
}


// callback for the network start request
static void ZDO_StartNetworkConf(ZDO_StartNetworkConf_t *confirmInfo)
{
    if(confirmInfo->status == ZDO_SUCCESS_STATUS)
	{
		appState = APP_STATE_NETWORK_STARTED;
		SYS_PostTask(APL_TASK_ID);
	}
	else
	{
		HAL_WriteUsart(&usartDescriptor, (uint8_t*)MESSAGE_NETWORK_ERR, sizeof(MESSAGE_NETWORK_ERR)-1);
		BSP_OnLed(LED_RED);
	}
}
 
 
/*******************************************************************************
  Description: application task handler.

  Parameters: none.

  Returns: nothing.
*******************************************************************************/
void APL_TaskHandler(void)
{
   // Application starts here
   switch(appState)
   {
	   case APP_STATE_INIT:
			BSP_OpenLeds();
			//BSP_OnLed(LED_GREEN);
			HAL_OpenUsart(&usartDescriptor);
			HAL_WriteUsart(&usartDescriptor, (uint8_t*)MESSAGE_APP_STARTED, sizeof(MESSAGE_APP_STARTED)-1);
			
			ZDO_StartNetworkReq(&startNetwork);
	   break;
	   
	   case APP_STATE_NETWORK_STARTED:
		   APS_RegisterEndpointReq(&endpointParams);
	       BSP_OnLed(LED_GREEN);
		   HAL_WriteUsart(&usartDescriptor, (uint8_t*)MESSAGE_NETWORK_STARTED, sizeof(MESSAGE_NETWORK_STARTED)-1);
	   break;
   }
}

/*******************************************************************************
  Description: just a stub.

  Parameters: are not used.

  Returns: nothing.
*******************************************************************************/
void ZDO_MgmtNwkUpdateNotf(ZDO_MgmtNwkUpdateNotf_t *nwkParams)
{
  nwkParams = nwkParams;  // Unused parameter warning prevention
}

/*******************************************************************************
  Description: just a stub.

  Parameters: none.

  Returns: nothing.
*******************************************************************************/
void ZDO_WakeUpInd(void)
{
}

#ifdef _BINDING_
/***********************************************************************************
  Stub for ZDO Binding Indication

  Parameters:
    bindInd - indication

  Return:
    none

 ***********************************************************************************/
void ZDO_BindIndication(ZDO_BindInd_t *bindInd)
{
  (void)bindInd;
}

/***********************************************************************************
  Stub for ZDO Unbinding Indication

  Parameters:
    unbindInd - indication

  Return:
    none

 ***********************************************************************************/
void ZDO_UnbindIndication(ZDO_UnbindInd_t *unbindInd)
{
  (void)unbindInd;
}
#endif //_BINDING_

/**********************************************************************//**
  \brief Main - C program main start function

  \param none
  \return none
**************************************************************************/
int main(void)
{
  SYS_SysInit();

  for(;;)
  {
    SYS_RunTask();
  }
}



//eof blink.c
