#ifndef _LIB_BARRELFISH_AOS_URPC_H
#define _LIB_BARRELFISH_AOS_URPC_H

#include <aos/aos.h>
#include <spawn/rpc_server.h>

struct aos_urpc_meta {
    volatile uint8_t call_in_progress;

    uintptr_t a0;
    uintptr_t a1;
    uintptr_t a2;
    uintptr_t a3;
    struct capability cap;
};

struct aos_urpc_frame {
    struct aos_urpc_meta meta_to_app;
    struct aos_urpc_meta meta_to_bsp;
    uint8_t padding[PAGE_SIZE - 2 * sizeof(struct aos_urpc_meta)];
    uint8_t shared_mem_to_app[PAGE_SIZE];
    uint8_t shared_mem_to_bsp[PAGE_SIZE];
};

STATIC_ASSERT_SIZEOF(struct aos_urpc_frame, MON_URPC_SIZE);

struct aos_urpc {
    struct aos_urpc_meta *meta;
    void *shared_mem;
};

struct aos_urpc_server {
    struct generic_rpc_server g;
    struct aos_urpc_meta *meta;
    struct thread_sem sem;
};

errval_t do_aos_urpc(
    struct aos_urpc *rpc, uintptr_t msg_type,
    struct capref arg_cap, size_t arg_size, uintptr_t arg0, uintptr_t arg1,
    struct capref *ret_cap, size_t *ret_size, uintptr_t *ret0, uintptr_t *ret1
);


/**
 * \brief Get bootinfo from rsp core
 */
errval_t aos_urpc_get_bootinfo(struct aos_urpc * rpc, struct bootinfo_serialized ** ret);

int urpc_client_loop(void *arg);

int urpc_server(void *arg);

#endif // _LIB_BARRELFISH_AOS_URPC_H
