/**
 * @file exo_ral_main.c
 *
 * @brief This file has linux RAL test functions.
 *
 * @copyright Copyright 2023 Antaris, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include "exo_os_common.h"
#include "exo_common.h"
void task_test( void *ptr );

ral_thread_handle_ptr task_2;
ral_status_t task_sts_2;
ral_sem_handle_ptr sem1;
ral_status_t sem_sts;
uint32_t sem_count;
ral_timer_handle_ptr tmr_id;
ral_status_t tmr_sts;
ral_queue_handle_ptr q_id;
ral_status_t q_sts;
int v1 = 1;
int* var;

/**
 * @brief Testing function
 */
void tmr_handler(void* args)
{
	static int tmr_count= 0;
	if(tmr_count<=5)
	{
		DEBUG_CPRINT(("\n EXO Timer Expired"));
		tmr_count++;
	}
	else
	{
		ral_timer_stop(tmr_id);
	}
}

/**
 * @brief Testing function
 */
void ral_main()
{
	task_sts_2 = ral_thread_create(&task_2,1024,40,"task_2",task_test,NULL);
	DEBUG_CPRINT(("\n EXO Thread created\n"));

	sem_sts = ral_sem_create_bin(&sem1, NULL);
	sem_sts = ral_sem_take(sem1,1000);
	sem_sts = ral_sem_get_count(sem1,&sem_count);
	sem_sts = ral_sem_give(sem1);
	sem_sts = ral_sem_get_count(sem1,&sem_count);

	q_sts = ral_queue_create(&q_id,4,10, NULL);
	q_sts = ral_queue_send(q_id,(void*)&v1,1000);
	q_sts = ral_queue_receive(q_id,(void*)&var,1000);

#if(0)
	pthread_t thread1, thread2;
	char *message1 = "Thread 1";
	char *message2 = "Thread 2";
	int  iret1, iret2;
	/* Create independent threads each of which will execute function */
	iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
	iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);

	/* Wait till threads are complete before main continues. Unless we  */
	/* wait we run the risk of executing an exit which will terminate   */
	/* the process and all threads before the threads have completed.   */
	pthread_join( thread1, NULL);
	pthread_join( thread2, NULL);
	DEBUG_CPRINT(("Thread 1 returns: %d\n",iret1));
	DEBUG_CPRINT(("Thread 2 returns: %d\n",iret2));
#endif
}

/**
 * @brief Testing function
 */
void task_test( void *ptr )
{
	DEBUG_CPRINT(("\n EXO Thread INIT "));
	tmr_sts = ral_timer_create(&tmr_id,"tmr_1",1,tmr_handler,NULL);
	tmr_sts = ral_timer_start(tmr_id,1000);
}

