
#ifndef ETHERCAT_UTILS_DARRA_ASYNC_ISOLATE_H
#define ETHERCAT_UTILS_DARRA_ASYNC_ISOLATE_H

#pragma once

#include <stdint.h>
#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif

struct dll;

#define DARRA_ASYNC_OK             0
#define DARRA_ASYNC_ERR_PARAM     -1
#define DARRA_ASYNC_ERR_OOM       -2
#define DARRA_ASYNC_ERR_SHUTDOWN  -3
#define DARRA_ASYNC_ERR_OS        -4

typedef struct darra_async_gate_s* darra_async_gate_t;

typedef struct darra_async_token_s* darra_async_token_t;

typedef void (*darra_async_progress_cb)(const char* message, void* user_data);

typedef void (*darra_async_build_done_cb)(uint16_t result_master, int canceled, void* user_data);

typedef void (*darra_async_setstate_done_cb)(int success, int canceled, void* user_data);

typedef void (*darra_async_scan_done_cb)(int slave_count, int canceled, void* user_data);

typedef void (*darra_async_sdo_read_done_cb)(const uint8_t* data, int len,
                                             uint32_t abort_code, int canceled, void* user_data);

typedef void (*darra_async_sdo_write_done_cb)(int success, uint32_t abort_code,
                                              int canceled, void* user_data);

darra_async_gate_t darra_async_gate_create(struct dll* dll);

int darra_async_gate_cancel(darra_async_gate_t gate, darra_async_token_t token);

int darra_async_token_cancel(darra_async_token_t token);

void darra_async_token_release(darra_async_token_t token);

void darra_async_gate_destroy(darra_async_gate_t gate);

int darra_async_gate_busy(darra_async_gate_t gate);

int darra_build_async_json(darra_async_gate_t gate, const char* json,
                           darra_async_progress_cb progress_cb,
                           darra_async_build_done_cb done_cb, void* user_data,
                           darra_async_token_t* out_token);

int darra_build_async_file(darra_async_gate_t gate, const char* path,
                           darra_async_progress_cb progress_cb,
                           darra_async_build_done_cb done_cb, void* user_data,
                           darra_async_token_t* out_token);

int darra_set_state_async(darra_async_gate_t gate, uint16_t master,
                          int target_state, uint32_t timeout_ms,
                          darra_async_progress_cb progress_cb,
                          darra_async_setstate_done_cb done_cb, void* user_data,
                          darra_async_token_t* out_token);

int darra_sdo_read_async(darra_async_gate_t gate, uint16_t master, uint16_t slave,
                         uint16_t index, uint8_t subindex, int complete_access,
                         darra_async_sdo_read_done_cb done_cb, void* user_data,
                         darra_async_token_t* out_token);

int darra_sdo_write_async(darra_async_gate_t gate, uint16_t master, uint16_t slave,
                          uint16_t index, uint8_t subindex,
                          const uint8_t* data, int len, int complete_access,
                          darra_async_sdo_write_done_cb done_cb, void* user_data,
                          darra_async_token_t* out_token);

int darra_scan_async(struct dll* dll, const char* adapter,
                     darra_async_scan_done_cb done_cb, void* user_data,
                     darra_async_token_t* out_token);

int darra_scan_async_cancel(struct dll* dll, darra_async_token_t token);

void darra_scan_async_shutdown(void);

#ifdef __cplusplus
}
#endif

#endif
