/**************************************************************************//**
 * @item     CosyOS-II Kernel
 * @file     sv_create.h
 * @brief    创建服务（用于创建各种内核对象）
 * @author   迟凯峰
 * @version  V1.3.0
 * @date     2024.01.23
 ******************************************************************************/

#ifndef __SV_CREATE_H
#define __SV_CREATE_H



/*
 * 声明任务
 */

/* 无私信 */
#define sCSV_ExternTask(name) \
	extern s_taskhand_ts u_taskhand_##name; \
	void name(void)

/* 有私信 */
#define sCSV_ExternTask_Msg(name) \
	extern s_taskhand_ts u_taskhand_##name; \
	void name(MCUCFG_TASKMSG_VAR, 



/*
 * 创建任务
 */

/* 创建一般任务 */
#if SYSCFG_DEBUGGING == __ENABLED__
	#if SYSCFG_TASKCREATEMODE == __STATIC__
		#define sCSV_CreateTask(name, pri, tss, srt, ntm) \
			MCUCFG_STACK_ALIGN static s_u8_t u_taskstack_##name[tss]; \
			s_taskhand_ts u_taskhand_##name = {0, 0, 0, OS_STATUS_DELETED, 0, NULL, u_taskstack_##name, tss, #name, {0}, MCUCFG_BASICSTACKSIZE, srt, 0, NULL, NULL, pri, (s_voidvoid_tfp)name}; \
			void name \
			sCreateTaskMsg_##ntm
			
	#elif SYSCFG_TASKCREATEMODE == __BALANCE__
		#define sCSV_CreateTask(name, pri, tss, srt, ntm) \
			s_taskhand_ts u_taskhand_##name = {0, 0, 0, OS_STATUS_DELETED, 0, NULL, NULL, tss, #name, {0}, MCUCFG_BASICSTACKSIZE, srt, 0, NULL, NULL, pri, (s_voidvoid_tfp)name, false}; \
			void name \
			sCreateTaskMsg_##ntm
			
	#elif SYSCFG_TASKCREATEMODE == __DYNAMIC__
		#define sCSV_CreateTask(name, pri, tss, srt, ntm) \
			s_taskhand_ts u_taskhand_##name = {0, tss, pri, (s_voidvoid_tfp)name, #name, srt, NULL}; \
			void name \
			sCreateTaskMsg_##ntm
			
	#endif
#else
	#if SYSCFG_TASKCREATEMODE == __STATIC__
		#define sCSV_CreateTask(name, pri, tss, srt, ntm) \
			MCUCFG_STACK_ALIGN static s_u8_t u_taskstack_##name[tss]; \
			s_taskhand_ts u_taskhand_##name = {0, 0, 0, OS_STATUS_DELETED, 0, NULL, u_taskstack_##name, tss, srt, 0, NULL, NULL, pri, (s_voidvoid_tfp)name}; \
			void name \
			sCreateTaskMsg_##ntm
			
	#elif SYSCFG_TASKCREATEMODE == __BALANCE__
		#define sCSV_CreateTask(name, pri, tss, srt, ntm) \
			s_taskhand_ts u_taskhand_##name = {0, 0, 0, OS_STATUS_DELETED, 0, NULL, NULL, tss, srt, 0, NULL, NULL, pri, (s_voidvoid_tfp)name}; \
			void name \
			sCreateTaskMsg_##ntm
			
	#elif SYSCFG_TASKCREATEMODE == __DYNAMIC__
		#define sCSV_CreateTask(name, pri, tss, srt, ntm) \
			s_taskhand_ts u_taskhand_##name = {0, tss, pri, (s_voidvoid_tfp)name, srt, NULL}; \
			void name \
			sCreateTaskMsg_##ntm
			
	#endif
#endif

/* 创建定时中断任务 */
#define sCSV_CreateTask_TimInt(tmid, arl, name, pri, tss, srt, ntm) \
	s_taskhand_ts u_taskhand_##name; \
	const s_timinthand_ts _CONST_MEM_ u_timinthand_##tmid = {&u_taskhand_##name, (s_voidvoid_tfp)0, arl}; \
	sCSV_CreateTask(name, pri, tss, srt, ntm)

/* 创建定时查询任务 */
#define sCSV_CreateTask_TimQry(tmid, event, arl, name, pri, tss, srt, ntm) \
	s_bool_t u_timqryevent_##tmid(void) MCUCFG_C51USING \
	{ \
		return (event ? true : false); \
	} \
	s_taskhand_ts u_taskhand_##name; \
	const s_timqryhand_ts _CONST_MEM_ u_timqryhand_##tmid = {&u_taskhand_##name, (s_voidvoid_tfp)0, (s_boolvoid_tfp)u_timqryevent_##tmid, arl}; \
	sCSV_CreateTask(name, pri, tss, srt, ntm)



/*
 * 创建钩子
 */

/* 创建定时中断钩子 */
#define sCSV_CreateHook_TimInt(tmid, arl, name) \
	void name(void); \
	const s_timinthand_ts _CONST_MEM_ u_timinthand_##tmid = {(s_taskhand_tsp)0, (s_voidvoid_tfp)name, arl}; \
	void name(void) MCUCFG_USING

/* 创建定时查询钩子 */
#define sCSV_CreateHook_TimQry(tmid, event, arl, name) \
	s_bool_t u_timqryevent_##tmid(void) MCUCFG_C51USING \
	{ \
		return (event ? true : false); \
	} \
	void name(void); \
	const s_timqryhand_ts _CONST_MEM_ u_timqryhand_##tmid = {(s_taskhand_tsp)0, (s_voidvoid_tfp)name, (s_boolvoid_tfp)u_timqryevent_##tmid, arl}; \
	void name(void) MCUCFG_USING



/*
 * 互斥信号量
 */

/* 声明 */
#define	sCSV_ExternMut(name) \
	extern s_mutex_ts name

/* 创建 */
#define	sCSV_CreateMut(name) \
	s_mutex_ts name = {false, NULL, 0}



/*
 * 二值信号量
 */

/* 声明 */
#define	sCSV_ExternBin(name) \
	extern s_binary_ts name

/* 创建 */
#define	sCSV_CreateBin(name, init) \
	s_binary_ts name = {init, NULL}



/*
 * 计数信号量
 */

/* 声明 */
#define	sCSV_ExternSem(name) \
	extern s_semaph_ts name

/* 创建 */
#define	sCSV_CreateSem(name, init, max) \
	s_semaph_ts name = {init, max, NULL}



/*
 * 飞信
 */

/* 声明信箱 */
#define sCSV_ExternFetion(name) \
	extern s_fetion_ts name

/* 创建信箱 */
#define sCSV_CreateFetion(name) \
	s_fetion_ts name = {false, NULL}



/*
 * 消息邮箱（必须声明）
 */

/* 声明邮箱 */
#define sCSV_ExternMailbox(name, type) \
	typedef struct{ \
		volatile s_bool_t gf; \
		s_tasknode_tsp node; \
		type mail0; \
	}u_##name##_ts; \
	extern u_##name##_ts name

/* 创建邮箱 */
#define sCSV_CreateMailbox(name) \
	u_##name##_ts name



/*
 * 消息队列
 */

/* 声明静态消息队列 */
#define sCSV_ExternMsgQueue_Static(name) \
	extern s_msgqueue_static_ts name

/* 声明动态消息队列 */
#define sCSV_ExternMsgQueue_Dynamic(name) \
	extern s_msgqueue_dynamic_ts name

/* 创建静态消息队列 */
#define	sCSV_CreateMsgQueue_Static(name, mode, len) \
	void *u_msgqueue_##name[len]; \
	s_msgqueue_static_ts name = {0, __STATIC__, mode, len, NULL, true, u_msgqueue_##name, u_msgqueue_##name + len - 1, u_msgqueue_##name}

/* 创建动态消息队列 */
#define	sCSV_CreateMsgQueue_Dynamic(name, mode, len) \
	s_msgqueue_dynamic_ts name = {0, __DYNAMIC__, mode, len, NULL, true, NULL, NULL}



/*
 * 事件标志组（必须声明）
 */

/* 声明 */
#define sCSV_ExternFlagGroup(name, bits) \
	typedef struct{bits}u_##name##_ts; \
	extern volatile u_##name##_ts name

/* 创建 */
#define sCSV_CreateFlagGroup(name) \
	volatile u_##name##_ts name



/*
 * 线程内存
 */

/* 创建线程内存池 */
#define	sCSV_CreateMempool \
	s_thrmem_ts u_thrmem = {NULL, NULL, 0}



#endif
