/**************************************************************************//**
 * @item     CosyOS-III Kernel
 * @file     sv_create.h
 * @brief    创建服务
 * @details  用于创建 CosyOS 内核对象。
 * @author   迟凯峰
 * @version  v3.0.0
 * @date     2026.07.21
 ******************************************************************************/

#ifndef __SV_CREATE_H
#define __SV_CREATE_H

/**
 @addtogroup CosyOS_内核服务
 @{
 *//**
 \defgroup   创建服务
 \brief      用于创建 CosyOS 内核对象。
 @{
 */

/*
 * 声明任务
 */

/** 简化声明静态任务 */
#define sCSV_ExtTask(name) \
	extern s_taskctrl_ts u_taskload_##name; \
	void u_task_##name

/** 简化声明动态任务 */
#define dCSV_ExtTask(name) \
	extern s_taskload_ts u_taskload_##name; \
	void u_task_##name

/** 完整声明静态任务 */
#define sCSV_ExternTask(name) \
	extern s_taskctrl_tsp name; \
	sCSV_ExtTask(name)

/** 完整声明动态任务 */
#define dCSV_ExternTask(name) \
	extern s_taskctrl_tsp name; \
	dCSV_ExtTask(name)


/*
 * 创建任务
 */

#if (SYSCFG_DEBUGGING == __ENABLED__)
	#if (SYSCFG_SAFERUNTIME == __ENABLED__)
			/** 静态创建，启用 DEBUGGING，启用 安全运行时 */
		#define sCSV_CreateTask_0(name, pri, tss, srt, res, ety) \
			s_taskctrl_ts u_taskload_##name = {0, pri, tss, &name, ety, res, srt, u_taskstack_##name, 0, OS_STATUS_DELETED}
	#else	/** 静态创建，启用 DEBUGGING，禁用 安全运行时 */
		#define sCSV_CreateTask_0(name, pri, tss, srt, res, ety) \
			s_taskctrl_ts u_taskload_##name = {0, pri, tss, &name, ety, res, u_taskstack_##name, 0, OS_STATUS_DELETED}
	#endif
#else
	#if (SYSCFG_SAFERUNTIME == __ENABLED__)
			/** 静态创建，禁用 DEBUGGING，启用 安全运行时 */
		#define sCSV_CreateTask_0(name, pri, tss, srt, res, ety) \
			s_taskctrl_ts u_taskload_##name = {0, pri, tss, &name, ety, srt, u_taskstack_##name, 0, OS_STATUS_DELETED}
	#else	/** 静态创建，禁用 DEBUGGING，禁用 安全运行时 */
		#define sCSV_CreateTask_0(name, pri, tss, srt, res, ety) \
			s_taskctrl_ts u_taskload_##name = {0, pri, tss, &name, ety, u_taskstack_##name, 0, OS_STATUS_DELETED}
	#endif
#endif
#if (SYSCFG_DEBUGGING == __ENABLED__)
	#if (SYSCFG_SAFERUNTIME == __ENABLED__)
			/** 动态创建，启用 DEBUGGING，启用 安全运行时 */
		#define dCSV_CreateTask_0(name, pri, tss, srt, res, ety) \
			s_taskload_ts u_taskload_##name = {0, pri, tss, &name, ety, res, srt}
	#else	/** 动态创建，启用 DEBUGGING，禁用 安全运行时 */
		#define dCSV_CreateTask_0(name, pri, tss, srt, res, ety) \
			s_taskload_ts u_taskload_##name = {0, pri, tss, &name, ety, res}
	#endif
#else
	#if (SYSCFG_SAFERUNTIME == __ENABLED__)
			/** 动态创建，禁用 DEBUGGING，启用 安全运行时 */
		#define dCSV_CreateTask_0(name, pri, tss, srt, res, ety) \
			s_taskload_ts u_taskload_##name = {0, pri, tss, &name, ety, srt}
	#else	/** 动态创建，禁用 DEBUGGING，禁用 安全运行时 */
		#define dCSV_CreateTask_0(name, pri, tss, srt, res, ety) \
			s_taskload_ts u_taskload_##name = {0, pri, tss, &name, ety}
	#endif
#endif

/** 静态创建一般任务 */
#define sCSV_CreateTask(name, pri, tss, srt, res) \
	void u_task_##name(void); \
	s_taskctrl_ts u_taskload_##name; \
	s_taskctrl_tsp name = &u_taskload_##name; \
	MCUCFG_STACK_ALIGN static s_u8_t u_taskstack_##name[tss]; \
	sCSV_CreateTask_0(name, pri, tss, srt, res, u_task_##name); \
	void u_task_##name(void) MCUCFG_TASK_ATTRIBUTE

/** 动态创建一般任务 */
#define dCSV_CreateTask(name, pri, tss, srt, res) \
	void u_task_##name(void); \
	s_taskctrl_tsp name = OS_NULL; \
	dCSV_CreateTask_0(name, pri, tss, srt, res, u_task_##name); \
	void u_task_##name(void) MCUCFG_TASK_ATTRIBUTE

/** 静态创建私信任务 */
#define sCSV_CreateTaskmsg(name, pri, tss, srt, res, ntm) \
	s_taskctrl_ts u_taskload_##name; \
	s_taskctrl_tsp name = &u_taskload_##name; \
	MCUCFG_STACK_ALIGN static s_u8_t u_taskstack_##name[tss]; \
	sCSV_CreateTask_0(name, pri, tss, srt, res, OS_NULL); \
	void u_task_##name sCreateTaskMsg_##ntm

/** 动态创建私信任务 */
#define dCSV_CreateTaskmsg(name, pri, tss, srt, res, ntm) \
	s_taskctrl_tsp name = OS_NULL; \
	dCSV_CreateTask_0(name, pri, tss, srt, res, OS_NULL); \
	void u_task_##name sCreateTaskMsg_##ntm

/** for 定时任务 */
#if defined ( __GNUC__ )
#define s_hookorload_load(p) .load=(s_taskload_tsp)p
#define s_hookorload_hook(p) .hook=p
#else
#define s_hookorload_load(p) (s_voidvoid_tfp)p
#define s_hookorload_hook(p) p
#endif
#define OS_TIMQRYINIT        (s_timqry_t)(SYSCFG_USERTIMQRYINIT - 1) /*!< 定时查询初始值 */

/** 静态创建定时中断任务 */
#define sCSV_CreateTask_TimInt(tmid, arl, name, pri, tss, srt, res) \
	s_taskctrl_ts u_taskload_##name; \
	s_timinthand_ts _STATIC_MEM_ u_timinthand_##tmid = {0, 0, arl, 1, {s_hookorload_load(&u_taskload_##name)}}; \
	sCSV_CreateTask(name, pri, tss, srt, res)

/** 动态创建定时中断任务 */
#define dCSV_CreateTask_TimInt(tmid, arl, name, pri, tss, srt, res) \
	s_taskload_ts u_taskload_##name; \
	s_timinthand_ts _STATIC_MEM_ u_timinthand_##tmid = {0, 0, arl, 1, {s_hookorload_load(&u_taskload_##name)}}; \
	dCSV_CreateTask(name, pri, tss, srt, res)

/** 静态创建定时查询任务 */
#define sCSV_CreateTask_TimQry(tmid, event, arl, name, pri, tss, srt, res) \
	bool u_timqryevent_##tmid(void) MCUCFG_OSIT_ATTRIBUTE {return (event ? true : false);} \
	s_taskctrl_ts u_taskload_##name; \
	s_timqryhand_ts _STATIC_MEM_ u_timqryhand_##tmid = {OS_TIMQRYINIT, OS_TIMQRYINIT, arl, 1, {s_hookorload_load(&u_taskload_##name)}, u_timqryevent_##tmid}; \
	sCSV_CreateTask(name, pri, tss, srt, res)

/** 动态创建定时查询任务 */
#define dCSV_CreateTask_TimQry(tmid, event, arl, name, pri, tss, srt, res) \
	bool u_timqryevent_##tmid(void) MCUCFG_OSIT_ATTRIBUTE {return (event ? true : false);} \
	s_taskload_ts u_taskload_##name; \
	s_timqryhand_ts _STATIC_MEM_ u_timqryhand_##tmid = {OS_TIMQRYINIT, OS_TIMQRYINIT, arl, 1, {s_hookorload_load(&u_taskload_##name)}, u_timqryevent_##tmid}; \
	dCSV_CreateTask(name, pri, tss, srt, res)


/*
 * 创建钩子
 */

/** 创建定时中断钩子 */
#define sCSV_CreateHook_TimInt(tmid, arl) \
	void u_timint_##tmid(void); \
	s_timinthand_ts _STATIC_MEM_ u_timinthand_##tmid = {0, 0, arl, 0, {s_hookorload_hook(u_timint_##tmid)}}; \
	void u_timint_##tmid(void) MCUCFG_OSIT_ATTRIBUTE

/** 创建定时查询钩子 */
#define sCSV_CreateHook_TimQry(tmid, event, arl) \
	void u_timqry_##tmid(void); \
	bool u_timqryevent_##tmid(void) MCUCFG_OSIT_ATTRIBUTE {return (event ? true : false);} \
	s_timqryhand_ts _STATIC_MEM_ u_timqryhand_##tmid = {OS_TIMQRYINIT, OS_TIMQRYINIT, arl, 0, {s_hookorload_hook(u_timqry_##tmid)}, u_timqryevent_##tmid}; \
	void u_timqry_##tmid(void) MCUCFG_OSIT_ATTRIBUTE


/*
 * 互斥信号量
 */

/** 声明互斥信号量 */
#define sCSV_ExternMut(name) \
	extern s_mutex_ts name

/** 创建互斥信号量并初始化 */
#define sCSV_CreateMut(name) \
	s_mutex_ts name = {0}

/** 创建互斥信号量不初始化 */
#define sCSV_CreateMut_NotInit(name) \
	s_mutex_ts name


/*
 * 二值信号量
 */

/** 声明二值信号量 */
#define sCSV_ExternBin(name) \
	extern s_binary_ts name

/** 创建二值信号量并初始化 */
#define sCSV_CreateBin(name, init) \
	s_binary_ts name = {OS_NULL, init}

/** 创建二值信号量不初始化 */
#define sCSV_CreateBin_NotInit(name) \
	s_binary_ts name


/*
 * 计数信号量
 */

/** 声明计数信号量 */
#define sCSV_ExternSem(name) \
	extern s_semaph_ts name

/** 创建计数信号量并初始化 */
#define sCSV_CreateSem(name, init, max) \
	s_semaph_ts name = {OS_NULL, init, max}

/** 创建计数信号量不初始化 */
#define sCSV_CreateSem_NotInit(name) \
	s_semaph_ts name


/*
 * 飞信
 */

/** 声明信箱 */
#define sCSV_ExternTionbox(name) \
	extern s_tionbox_ts name

/** 创建信箱并初始化 */
#define sCSV_CreateTionbox(name) \
	s_tionbox_ts name = {0}

/** 创建信箱不初始化 */
#define sCSV_CreateTionbox_NotInit(name) \
	s_tionbox_ts name


/*
 * 邮箱
 */

/** 声明邮箱 */
#define sCSV_ExternMailbox(name) \
	extern s_mailbox_ts name

/** 创建邮箱并初始化 */
#define sCSV_CreateMailbox(name) \
	s_mailbox_ts name = {0}

/** 创建邮箱不初始化 */
#define sCSV_CreateMailbox_NotInit(name) \
	s_mailbox_ts name


/*
 * 消息队列
 */

/** 声明静态队列 */
#define sCSV_ExternQueue_Static(name) \
	extern s_staque_ts name

/** 声明动态队列 */
#define sCSV_ExternQueue_Dynamic(name) \
	extern s_dynque_ts name

/** 创建静态队列 */
#define sCSV_CreateQueue_Static(name, mode, len) \
	void *u_queue_##name[len]; \
	s_staque_ts name = {OS_NULL, true, 0, __STATIC__, mode, len, u_queue_##name, u_queue_##name + len - 1, u_queue_##name}

/** 创建动态队列 */
#define sCSV_CreateQueue_Dynamic(name, mode, len) \
	s_dynque_ts name = {OS_NULL, true, 0, __DYNAMIC__, mode, len, OS_NULL, OS_NULL}


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

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

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


/*
 * 线程内存
 */

/** 创建线程内存池 */
#define sCSV_CreateMempool() \
	static s_thrmem_ts u_thrmem = {0}

/** @} */
/** @} */

#endif
