/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    app_lorawan.c
  * @author  MCD Application Team
  * @brief   Application of the LRWAN Middleware
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under Ultimate Liberty license
  * SLA0044, the "License"; You may not use this file except in compliance with
  * the License. You may obtain a copy of the License at:
  *                             www.st.com/SLA0044
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "app_lorawan.h"
#include "lora_app.h"
#include "sys_app.h"
#include "stm32_seq.h"
#include "commonMsg.h"
#include "utilities_def.h"
/* USER CODE BEGIN Includes */
#include "stm32l4xx_hal.h"
#include "stm32_nucleo_errno.h"

#define TIMEOUT_DURATION 1000

/* USER CODE END Includes */

/* External variables ---------------------------------------------------------*/
/* USER CODE BEGIN EV */
extern SPI_HandleTypeDef hspi3;

/* USER CODE END EV */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

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

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/

/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Exported functions --------------------------------------------------------*/

void MX_LoRaWAN_Init(void)
{
  /* USER CODE BEGIN MX_LoRaWAN_Init_1 */

  /* USER CODE END MX_LoRaWAN_Init_1 */
  /* USER CODE BEGIN MX_LoRaWAN_Init_2 */

  /* USER CODE END MX_LoRaWAN_Init_2 */

  LoRaWAN_Init();
  /* USER CODE BEGIN MX_LoRaWAN_Init_3 */

  /* USER CODE END MX_LoRaWAN_Init_3 */
}

void MX_LoRaWAN_Process(void)
{
  /* USER CODE BEGIN MX_LoRaWAN_Process_1 */

  /* USER CODE END MX_LoRaWAN_Process_1 */
  UTIL_SEQ_Run(UTIL_SEQ_DEFAULT);
  /* USER CODE BEGIN MX_LoRaWAN_Process_2 */

  /* USER CODE END MX_LoRaWAN_Process_2 */
}

/* USER CODE BEGIN EF */

/* USER CODE END EF */

/* Private Functions Definition -----------------------------------------------*/
/* USER CODE BEGIN PrFD */
static void LoRa_processMsg(MSG_STRUCT *p_msg)
{
	if( p_msg->opcode == OP_LORA_EVENT1 )
		UTIL_SEQ_SetTask((1 << CFG_SEQ_Task_LoRaSendOnTxSystemEvent), CFG_SEQ_Prio_0);
}

void vLoRaWANTask( void *pvParameters )
{
/* Remove warning for unused parameter */
    (void)pvParameters ;
	osMessageQId myQueueId;
	osEvent event;

	myQueueId = getQueueId(LORA_COMM);
	//taskENTER_CRITICAL();
    MX_LoRaWAN_Init();
	//taskEXIT_CRITICAL();

	while(1)
	{
		event = osMessageGet( myQueueId,  100 );
		if (event.status == osEventMessage)
		{
			MSG_STRUCT* p_recvMsg = event.value.p;

			LoRa_processMsg(p_recvMsg);
			freeMemoryPoolMessage(p_recvMsg);
		}
		else if (event.status == osEventTimeout) {
			MX_LoRaWAN_Process();
		}
	}
}

/* USER CODE END PrFD */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
