//////////////////////////////////////////////////////////////////
//
// App:			Guardian task
// Author:		Milan Korinek
// Version:		1.0
// Notice:		Supervisor of Digital I/O USB Card
//////////////////////////////////////////////////////////////////


#include "guardian.h"

typedef struct {
	xQueueHandle control_pipe;	
} guardian_task_param_t;

const char *const guardian_type_str[] = {
	FOREACH_guardian_msg_type(GENERATE_STRING)
};

status_code_t guardian_init(guardian_handle_t * task_handle)
{
	portBASE_TYPE retTask;
	
	static guardian_task_param_t task_param;
	
	task_param.control_pipe = 0;
	
	task_handle->task_handle = NULL;
	task_handle->control_pipe = 0;
	
	/* Create FreeRTOS queue for task control pipe */
	task_param.control_pipe = xQueueCreate(TASK_GUARDIAN_CONTROL_PIPE_LEN, sizeof(guardian_task_param_t));
	if (task_param.control_pipe == 0) {
		return ERR_NO_MEMORY;
	}
	task_handle->control_pipe = task_param.control_pipe;

	/* Create the task. */
	retTask = xTaskCreate(guardian_task, (const signed char *const) TASK_GUARDIAN_NAME,
		TASK_GUARDIAN_STACK_DEPTH_WORDS, (void *)(& task_param), TASK_GUARDIAN_PRIORITY, &(task_handle->task_handle));
		
	if ((retTask != pdPASS) || (task_handle->task_handle == NULL))	{
		return ERR_NO_MEMORY;
	}
	
	return STATUS_OK;
}

void guardian_task(void *pvParameters)
{
	guardian_task_param_t * task_param = (guardian_task_param_t *)pvParameters;
	guardian_msg_t control_msg;
	
	debug_event(DIO_SEVERITY_IMPORTANT, "Task 'Guardian' running...");
	for(;;)	{
		if (xQueueReceive(task_param->control_pipe, (void *)(&control_msg) , portMAX_DELAY) == pdPASS) { /* Nothing to do if queue is empty */
			
		}
	}

	/* If all is well, the following line will never be reached. */
	vTaskDelete(NULL);
}
