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

  \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>
#include <sensors.h>


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


// deklarace callbacku pro hlavni casovac
void mainTimerFired(void);

int max_packet_count = PACKET_COUNT;
int packet_counter = 0;			// pocitadlo packet count


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



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


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

// application message 
static AppMessageBuffer_t appMessageBuffer;
static AsduMessage_t AsduMessage;  

// datareq variable
static APS_DataReq_t dataReq; 	 


// definice hlavniho casovace mainTimer
static HAL_AppTimer_t mainTimer = {
 .interval = PACKET_INTERVAL,
 .mode = TIMER_REPEAT_MODE,
 .callback = mainTimerFired,
};


// definice SimpleDescriptoru
SimpleDescriptor_t simpleDescriptor = { 10, 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_PARITY,
  .stopbits        = APP_USART_STOPBIT,
  .rxBuffer        = NULL,
  .rxBufferLength  = 0,
  .txBuffer        = usartTxBuffer,
  .txBufferLength  = USART_TX_BUFFER_SIZE,
  .rxCallback      = NULL,
  .txCallback      = NULL,
  .flowControl     = USART_FLOW_CONTROL_NONE,
};



void mainTimerFired(){
	//appState = APP_GO;	
	//BSP_ToggleLed(LED_RED);
	appState = APP_SEND;
	SYS_PostTask(APL_TASK_ID);

}


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


// APS_DataReq() callback
static void APS_DataConf(APS_DataConf_t *confInfo)
{
	// data was sent
	BSP_ToggleLed(LED_YELLOW);
	
	if(confInfo->status == APS_SUCCESS_STATUS)
	{
		if(packet_counter<max_packet_count-1)
		{
			
		  HAL_WriteUsart(&usartDescriptor, (uint8_t*)MESSAGE_SENT_OK, sizeof(MESSAGE_SENT_OK)-1);
		  packet_counter++;
		  
	    }
		else if (packet_counter==max_packet_count-1)
		{
			packet_counter = 0;
			HAL_StopAppTimer(&mainTimer);
		}
	}	  
    else
		HAL_WriteUsart(&usartDescriptor, (uint8_t*)MESSAGE_SENT_ERR, sizeof(MESSAGE_SENT_ERR)-1);	
}


// 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);
	}
}
 
 //The data indication callback to handle data reception on the endpoint
static void APS_DataIndication(APS_DataInd_t *ind)
{
   
  
   
   if(ind->status == APS_SUCCESS_STATUS)
   {

	 }
}

/*******************************************************************************
  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_StartAppTimer(&mainTimer);
	   break;
	   
	 	   
	   case APP_SEND:
									 						
			
			AsduMessage.seq++;
			

			CS_ReadParameter(CS_RF_TX_POWER_ID, &AsduMessage.curr_txPwr);
			
			
			char msg[]="Zprava";
			
			memcpy(AsduMessage.payload, msg, strlen(msg));
			
			memcpy(appMessageBuffer.data,&AsduMessage, sizeof(AsduMessage));		
			dataReq.profileId				 = 1;
			dataReq.srcEndpoint			 = 10;
			dataReq.dstAddrMode			 = APS_SHORT_ADDRESS;
			dataReq.dstEndpoint			 = 1;
			dataReq.clusterId               = 1;
			dataReq.radius					 = 0;
			dataReq.APS_DataConf			 = APS_DataConf;
			dataReq.asdu					 = appMessageBuffer.data;
			dataReq.asduLength				 = sizeof(AsduMessage);
			dataReq.dstAddress.shortAddress = 0x0000;
			APS_DataReq(&dataReq);
			
			 appState = APP_IDLE;
	   break;
	   
	   case APP_TRSH:
			BSP_OnLed(LED_RED);			
			while(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
