#pragma once
#include "bsp/bsp.h"
#include "os/os_types.h"
#include "bsp/delay.h"
#define MAX_TASK 32

#define TIMER_INIT(timer, handler) {&timer, &timer, handler}
typedef uint32_t (*TskFunction_t)( void * );

typedef struct shark_task {
	u64 time;
	struct shark_task *next;
	TskFunction_t handler;
	void *params;
} shark_task_t;

typedef struct shark_timer {
	struct shark_timer *prev;
	struct shark_timer *next;
	void (*handler)(struct shark_timer *timer);
	u64 time;
} shark_timer_t;

static inline bool shark_timer_started(shark_timer_t *timer)
{
	return timer->next != timer;
}

static inline bool shark_timer_stopped(shark_timer_t *timer)
{
	return timer->next == timer;
}

static inline uint32_t cpu_enter_critical() {
    uint32_t primask = __get_PRIMASK();
    __disable_irq();
    return primask;
}

static inline void cpu_exit_critical(uint32_t priority_mask) {
    __set_PRIMASK(priority_mask);
}

u32 get_tick_ms(void);
u32 get_delta_ms(uint32_t prev_ms);
u64 shark_get_mseconds(void);
u32 shark_get_seconds(void);
void shark_timer_post(shark_timer_t *timer, u32 delay);
void shark_timer_cancel(shark_timer_t *timer);
void shark_task_add(shark_task_t *task);
void shark_task_run(void);
shark_task_t *shark_task_create(TskFunction_t, void *);

