/**************************************************************************//**
 * @item     CosyOS-II Kernel
 * @file     sv_int_loc.c
 * @brief    中断本地服务（仅在用户中断中调用，并在本地执行）
 * @author   迟凯峰
 * @version  V1.3.3
 * @date     2024.01.31
 ******************************************************************************/

#include "os_var.h"
#include "os_api.h"

#if SYSCFG_MCUCORE == 8051
#pragma NOAREGS
#endif



/*
 * 任务调度
 */

/* 无条件任务调度 */
void si_task_scheduling(void)
{
	if(!s_sign_scheduling){
		s_sign_scheduling = true;
		mPendSV_Set;
	}
}

/* 任务优先级调度 */
void si_taskpri_scheduling(s_tasknode_tsp node)
{
	if(!s_sign_scheduling){
		if(node->pri > s_task_current->pri){
			s_sign_scheduling = true;
			mPendSV_Set;
		}
	}
}



/*
 * 二值信号量
 */
#if SYSCFG_BINARY == __ENABLED__
/* 获取 */
s_bool_t si_take_binary(s_binary_tsp bin)
{
	if(bin->binary){
		bin->binary = false;
		return true;
	}
	return false;
}

#endif



/*
 * 飞信
 */
#if SYSCFG_FETION == __ENABLED__
/* 接收飞信 */
m_fetion_t si_recv_fetion(s_fetion_tsp fet)
{
	m_fetion_t fetion = fet->fetion;
	fet->fetion = false;
	return fetion;
}

#endif



/*
 * 消息邮箱（接收邮件）
 */
#if SYSCFG_MAILBOX == __ENABLED__

#if SYSCFG_MCUCORE == 8051

s_bool_t si_can_read_gmbox(s_mailbox_tsp gm)
{
	if(s_sign_write_mbox_accessor
	|| s_mbox_accessor == NULL || s_mbox_accessor->gm != gm){
		return true;
	}
	return false;
}

#if SYSCFG_MAILBOX_ACCESSMECHANISM == 1

s_bool_t si_can_read_lmbox(void)
{
	if(!s_mbox_accessor->lf) return false;
	s_mbox_accessor->lf = false;
	return true;
}

void *si_get_lmbox(void)
{
	return s_mbox_accessor->lm;
}

#endif

#else

s_bool_t si_recv_mail(void *lm, s_mailbox_tsp gm, size_t size) MCUCFG_251_REENTRANT
{
	if(s_mbox_accessor == NULL || s_mbox_accessor->gm != gm){
		if(gm->gf){
			gm->gf = false;
			s_memcpy(lm, &gm->mail0, size);
			return true;
		}
		return false;
	}
	#if SYSCFG_MAILBOX_ACCESSMECHANISM == 1
	else if(s_mbox_accessor->lf){
		s_mbox_accessor->lf = false;
		s_memcpy(lm, s_mbox_accessor->lm, size);
		return true;
	}
	#endif
	return false;
}

#endif

#endif



/*
 * 消息队列（接收消息）
 */
#if SYSCFG_MSGQUEUE == __ENABLED__

#if SYSCFG_MCUCORE == 8051

s_bool_t si_can_access_msgqueue(s_msgqueue_tsp queue)
{
	if(!queue->mutex) goto __RET_FALSE;
	queue->mutex = false;
	if(queue->type == __DYNAMIC__){
		#if SYSCFG_DEBUGGING == __ENABLED__
		s_fault.error_recvmsg_int = true;
		#endif
		goto __FREE_MUTEX;
	}
	if(!queue->counter) goto __FREE_MUTEX;
	return true;
	
__FREE_MUTEX:
	queue->mutex = true;
	
__RET_FALSE:
	return false;
}

static void *_recv_msg_(s_bool_t _OBJ_MEM_ *mut, void *msg)
{
	*mut = true;
	return msg;
}

void *si_recv_msg(s_msgqueue_tsp queue)
{
	void *msg;
	sRecvMsg_Static(msg);
	return _recv_msg_((s_bool_t *)&queue->mutex, msg);
}

#else

void *si_recv_msg(s_msgqueue_tsp queue)
{
	void *msg = NULL;
	if(!queue->mutex) return NULL;
	queue->mutex = false;
	if(queue->type == __DYNAMIC__){
		#if SYSCFG_DEBUGGING == __ENABLED__
		s_fault.error_recvmsg_int = true;
		#endif
	}
	else if(!queue->counter);
	else{
		sRecvMsg_Static(msg);
	}
	queue->mutex = true;
	return msg;
}

#endif

#endif
