/**************************************************************************//**
 * @item     CosyOS-III Kernel
 * @file     os_api.h
 * @brief    系统内核专用API
 * @author   迟凯峰
 * @version  v3.0.0
 * @date     2026.07.21
 ******************************************************************************/

#ifndef __OS_API_H
#define __OS_API_H


/*
 * 引用.标准库函数
 */

#define s_init_mempool  init_mempool
#define s_malloc  malloc
#define s_calloc  calloc
#define s_realloc realloc
#define s_free    free
#define s_memcpy  memcpy
#define s_memcmp  memcmp
#define s_strcpy  strcpy
#define s_strcmp  strcmp
#define s_strlen  strlen


/*
 * 操作.定时中断/查询
 */

/** 启动定时中断任务 */
#define sStartTask_TimInt(tmid) \
	sUSV_StartTask_0(s_timint_handle[tmid]->hookorload.load, OS_STATUS_SUSPENDED)

/** 启动定时查询任务 */
#define sStartTask_TimQry(tmid) \
	sUSV_StartTask_0(s_timqry_handle[tmid]->hookorload.load, OS_STATUS_SUSPENDED)

/** 恢复定时中断任务 */
#define sResumeTask_TimInt(tmid) \
	sTSV_ResumeTask(*(s_timint_handle[tmid]->hookorload.load)->h2task)

/** 恢复定时查询任务 */
#define sResumeTask_TimQry(tmid) \
	sTSV_ResumeTask(*(s_timqry_handle[tmid]->hookorload.load)->h2task)

/** 调用定时中断钩子 */
#define sCallHook_TimInt(tmid) \
	(*s_timint_handle[tmid]->hookorload.hook)()

/** 调用定时查询钩子 */
#define sCallHook_TimQry(tmid) \
	(*s_timqry_handle[tmid]->hookorload.hook)()


/*
 * 事件标志组 - 写标志位
 */

#define stWriteFlagBits_1(g, v, b0) \
	g.b0 = v

#define stWriteFlagBits_2(g, v, b0, b1) \
	g.b0 = g.b1 = v

#define stWriteFlagBits_3(g, v, b0, b1, b2) \
	g.b0 = g.b1 = g.b2 = v

#define stWriteFlagBits_4(g, v, b0, b1, b2, b3) \
	g.b0 = g.b1 = g.b2 = g.b3 = v

#define stWriteFlagBits_5(g, v, b0, b1, b2, b3, b4) \
	g.b0 = g.b1 = g.b2 = g.b3 = g.b4 = v

#define stWriteFlagBits_6(g, v, b0, b1, b2, b3, b4, b5) \
	g.b0 = g.b1 = g.b2 = g.b3 = g.b4 = g.b5 = v

#define stWriteFlagBits_7(g, v, b0, b1, b2, b3, b4, b5, b6) \
	g.b0 = g.b1 = g.b2 = g.b3 = g.b4 = g.b5 = g.b6 = v

#define stWriteFlagBits_8(g, v, b0, b1, b2, b3, b4, b5, b6, b7) \
	g.b0 = g.b1 = g.b2 = g.b3 = g.b4 = g.b5 = g.b6 = g.b7 = v

#define suWriteFlagBits_1(g, v, b0) \
do{ \
	su_kernel_locks(); \
	stWriteFlagBits_1(g, v, b0); \
	su_kernel_unlocks(); \
}while(false)

#define suWriteFlagBits_2(g, v, b0, b1) \
do{ \
	su_kernel_locks(); \
	stWriteFlagBits_2(g, v, b0, b1); \
	su_kernel_unlocks(); \
}while(false)

#define suWriteFlagBits_3(g, v, b0, b1, b2) \
do{ \
	su_kernel_locks(); \
	stWriteFlagBits_3(g, v, b0, b1, b2); \
	su_kernel_unlocks(); \
}while(false)

#define suWriteFlagBits_4(g, v, b0, b1, b2, b3) \
do{ \
	su_kernel_locks(); \
	stWriteFlagBits_4(g, v, b0, b1, b2, b3); \
	su_kernel_unlocks(); \
}while(false)

#define suWriteFlagBits_5(g, v, b0, b1, b2, b3, b4) \
do{ \
	su_kernel_locks(); \
	stWriteFlagBits_5(g, v, b0, b1, b2, b3, b4); \
	su_kernel_unlocks(); \
}while(false)

#define suWriteFlagBits_6(g, v, b0, b1, b2, b3, b4, b5) \
do{ \
	su_kernel_locks(); \
	stWriteFlagBits_6(g, v, b0, b1, b2, b3, b4, b5); \
	su_kernel_unlocks(); \
}while(false)

#define suWriteFlagBits_7(g, v, b0, b1, b2, b3, b4, b5, b6) \
do{ \
	su_kernel_locks(); \
	stWriteFlagBits_7(g, v, b0, b1, b2, b3, b4, b5, b6); \
	su_kernel_unlocks(); \
}while(false)

#define suWriteFlagBits_8(g, v, b0, b1, b2, b3, b4, b5, b6, b7) \
do{ \
	su_kernel_locks(); \
	stWriteFlagBits_8(g, v, b0, b1, b2, b3, b4, b5, b6, b7); \
	su_kernel_unlocks(); \
}while(false)

#define siWriteFlagBits_1(b0) \
	stWriteFlagBits_1(u_grp, true, b0); \
	siWriteFlagBits()

#define siWriteFlagBits_2(b0, b1) \
	stWriteFlagBits_2(u_grp, true, b0, b1); \
	siWriteFlagBits()

#define siWriteFlagBits_3(b0, b1, b2) \
	stWriteFlagBits_3(u_grp, true, b0, b1, b2); \
	siWriteFlagBits()

#define siWriteFlagBits_4(b0, b1, b2, b3) \
	stWriteFlagBits_4(u_grp, true, b0, b1, b2, b3); \
	siWriteFlagBits()

#define siWriteFlagBits_5(b0, b1, b2, b3, b4) \
	stWriteFlagBits_5(u_grp, true, b0, b1, b2, b3, b4); \
	siWriteFlagBits()

#define siWriteFlagBits_6(b0, b1, b2, b3, b4, b5) \
	stWriteFlagBits_6(u_grp, true, b0, b1, b2, b3, b4, b5); \
	siWriteFlagBits()

#define siWriteFlagBits_7(b0, b1, b2, b3, b4, b5, b6) \
	stWriteFlagBits_7(u_grp, true, b0, b1, b2, b3, b4, b5, b6); \
	siWriteFlagBits()

#define siWriteFlagBits_8(b0, b1, b2, b3, b4, b5, b6, b7) \
	stWriteFlagBits_8(u_grp, true, b0, b1, b2, b3, b4, b5, b6, b7); \
	siWriteFlagBits()

#define siWriteFlagBits() \
		u_psv.value = ( \
			  sizeof(u_grp) == 1 ? *(s_u8_t  *)&u_grp \
			: sizeof(u_grp) == 2 ? *(s_u16_t *)&u_grp \
			: sizeof(u_grp) == 4 ? *(s_u32_t *)&u_grp \
			: 0 \
		); \
	} \
	mPendSV_FIFOLoad(); \
}while(false)


/*
 * 私信 - 创建信箱
 */

#define sTaskMsg_Init_1(g) \
tm1##g = tm1_##g

#define sTaskMsg_Init_2(g) \
sTaskMsg_Init_1(g); \
tm2##g = tm2_##g

#define sTaskMsg_Init_3(g) \
sTaskMsg_Init_2(g); \
tm3##g = tm3_##g

#define sTaskMsg_Init_4(g) \
sTaskMsg_Init_3(g); \
tm4##g = tm4_##g

#define sTaskMsg_Init_5(g) \
sTaskMsg_Init_4(g); \
tm5##g = tm5_##g

#define sTaskMsg_Init_6(g) \
sTaskMsg_Init_5(g); \
tm6##g = tm6_##g

#define sTaskMsg_Init_7(g) \
sTaskMsg_Init_6(g); \
tm7##g = tm7_##g

#define sTaskMsg_Init_8(g) \
sTaskMsg_Init_7(g); \
tm8##g = tm8_##g

#define sTaskMsg_Init(n) \
static s_u8_t volatile u_tmrf = 0; \
if(s_taskmsg_sign){ \
	u_tmrf = 1; \
	sTaskMsg_Init_##n(_); \
	if(--s_taskmsg_sign){ \
		s_taskmsg_sign = 0; \
		su_kernel_unlocks(); \
	} \
	return; \
} \
if(u_tmrf == 2){ \
	u_tmrf = 0; \
	sTaskMsg_Init_##n( ); \
	return; \
}

#define sCreateTaskMsg_0_1(m1) \
(m1##__) \
{ \
	static m1##_; \
	static m1   ; \
	sTaskMsg_Init(1); \
	do

#define sCreateTaskMsg_0_2(m1, m2) \
(m1##__, m2##__) \
{ \
	static m1##_; static m2##_; \
	static m1   ; static m2   ; \
	sTaskMsg_Init(2); \
	do

#define sCreateTaskMsg_0_3(m1, m2, m3) \
(m1##__, m2##__, m3##__) \
{ \
	static m1##_; static m2##_; static m3##_; \
	static m1   ; static m2   ; static m3   ; \
	sTaskMsg_Init(3); \
	do

#define sCreateTaskMsg_0_4(m1, m2, m3, m4) \
(m1##__, m2##__, m3##__, m4##__) \
{ \
	static m1##_; static m2##_; static m3##_; static m4##_; \
	static m1   ; static m2   ; static m3   ; static m4   ; \
	sTaskMsg_Init(4); \
	do

#define sCreateTaskMsg_0_5(m1, m2, m3, m4, m5) \
(m1##__, m2##__, m3##__, m4##__, m5##__) \
{ \
	static m1##_; static m2##_; static m3##_; static m4##_; static m5##_; \
	static m1   ; static m2   ; static m3   ; static m4   ; static m5   ; \
	sTaskMsg_Init(5); \
	do

#define sCreateTaskMsg_0_6(m1, m2, m3, m4, m5, m6) \
(m1##__, m2##__, m3##__, m4##__, m5##__, m6##__) \
{ \
	static m1##_; static m2##_; static m3##_; static m4##_; static m5##_; static m6##_; \
	static m1   ; static m2   ; static m3   ; static m4   ; static m5   ; static m6   ; \
	sTaskMsg_Init(6); \
	do

#define sCreateTaskMsg_0_7(m1, m2, m3, m4, m5, m6, m7) \
(m1##__, m2##__, m3##__, m4##__, m5##__, m6##__, m7##__) \
{ \
	static m1##_; static m2##_; static m3##_; static m4##_; static m5##_; static m6##_; static m7##_; \
	static m1   ; static m2   ; static m3   ; static m4   ; static m5   ; static m6   ; static m7   ; \
	sTaskMsg_Init(7); \
	do

#define sCreateTaskMsg_0_8(m1, m2, m3, m4, m5, m6, m7, m8) \
(m1##__, m2##__, m3##__, m4##__, m5##__, m6##__, m7##__, m8##__) \
{ \
	static m1##_; static m2##_; static m3##_; static m4##_; static m5##_; static m6##_; static m7##_; static m8##_; \
	static m1   ; static m2   ; static m3   ; static m4   ; static m5   ; static m6   ; static m7   ; static m8   ; \
	sTaskMsg_Init(8); \
	do

#define sCreateTaskMsg_1(m1) sCreateTaskMsg_0_1(m1)
#define sCreateTaskMsg_2(m1, m2) sCreateTaskMsg_0_2(m1, m2)
#define sCreateTaskMsg_3(m1, m2, m3) sCreateTaskMsg_0_3(m1, m2, m3)
#define sCreateTaskMsg_4(m1, m2, m3, m4) sCreateTaskMsg_0_4(m1, m2, m3, m4)
#define sCreateTaskMsg_5(m1, m2, m3, m4, m5) sCreateTaskMsg_0_5(m1, m2, m3, m4, m5)
#define sCreateTaskMsg_6(m1, m2, m3, m4, m5, m6) sCreateTaskMsg_0_6(m1, m2, m3, m4, m5, m6)
#define sCreateTaskMsg_7(m1, m2, m3, m4, m5, m6, m7) sCreateTaskMsg_0_7(m1, m2, m3, m4, m5, m6, m7)
#define sCreateTaskMsg_8(m1, m2, m3, m4, m5, m6, m7, m8) sCreateTaskMsg_0_8(m1, m2, m3, m4, m5, m6, m7, m8)


#endif
