/* * spar.h — scorpiox-scorpioplayer-aria2-rpc public API. * * A JSON-RPC 2.0 client for a REMOTE aria2 daemon (aria2c --enable-rpc). * This engine talks to a daemon on the user's NAS/seedbox; it does not embed * libaria2 (that is the sibling repo `scorpiox-aria2`, exports `aria2_dl_*`). * * Two surfaces over one engine: * - the spar_* stateful client below (fleet style: global state behind one lock); * - the sprch handler (sprch_get(), RuntimeType "ARIA2", caps DOWNLOAD|VALIDATE). * * Transport is the `scorpiox_io` vtable (scorpiox-io.h): libcurl on desktop, * URLSession on iOS, the spar fake backend in tests. The auth token is * positional: "token:" as params[0] (SPEC D2). Every numeric field on * the wire is a JSON string (SPEC D6); progress is an integer per-mille, no * floats. Write verbs refuse unless spar_arm_writes(1) (SPEC D12); * spar_shutdown_server additionally needs spar_arm_shutdown(1) (SPEC D13). * See SPEC.md for every decision and its C# citation. */ #ifndef SPAR_H #define SPAR_H #include #include #include "sprch.h" #ifdef __cplusplus extern "C" { #endif #if defined(_WIN32) # if defined(SPAR_BUILD) # define SPAR_API __declspec(dllexport) # else # define SPAR_API __declspec(dllimport) # endif #else # define SPAR_API __attribute__((visibility("default"))) #endif #define SPAR_VERSION "1.0.1" /* ---- error taxonomy (SPEC D11) ---- */ #define SPAR_OK 0 #define SPAR_ERR_TRANSPORT -1 /* io backend failure: never reached the daemon */ #define SPAR_ERR_HTTP -2 /* non-2xx with no parseable JSON-RPC error */ #define SPAR_ERR_RPC -3 /* JSON-RPC error object (code in spar_last_rpc_error_code) */ #define SPAR_ERR_JSON -4 /* HTTP 200 but not the JSON-RPC response we sent */ #define SPAR_ERR_UNAUTHORIZED -5 /* error.code==1 "Unauthorized": bad token */ #define SPAR_ERR_NO_SUCH_GID -6 /* GID not found / no URI data for GID */ #define SPAR_ERR_NOT_ARMED -7 /* write verb while unarmed (no request was sent) */ #define SPAR_ERR_NO_IO -8 /* no scorpiox_io installed */ #define SPAR_ERR_INVALID_ARG -10 /* NULL / empty argument */ #define SPAR_ERR_OOM -11 /* ---- transport (SPEC D14) ---- */ SPAR_API int spar_set_io(const scorpiox_io *io); /* NULL = clear */ SPAR_API int spar_use_curl(void); /* default when libcurl is available */ SPAR_API int spar_use_fake(void); /* offline canned responses */ SPAR_API const scorpiox_io *spar_io_curl(void); /* NULL when built without libcurl */ SPAR_API const scorpiox_io *spar_io_fake(void); /* ---- configuration ---- */ SPAR_API int spar_set_endpoint(const char *rpc_url); /* http://host:6800/jsonrpc */ SPAR_API const char *spar_endpoint(void); /* "" when unset */ SPAR_API int spar_set_token(const char *secret); /* NULL or "" = no token */ SPAR_API int spar_set_id_seed(uint64_t seed); /* deterministic validate IDs */ /* ---- read (safe; one request each) ---- */ SPAR_API int spar_get_version(void); SPAR_API int spar_get_session_info(void); SPAR_API int spar_get_global_stat(void); SPAR_API int spar_global_download_speed(uint64_t *out); SPAR_API int spar_global_upload_speed(uint64_t *out); SPAR_API int spar_global_active(uint64_t *out); SPAR_API int spar_global_waiting(uint64_t *out); SPAR_API int spar_global_stopped(uint64_t *out); /* bounded; raw JSON has numStoppedTotal */ /* task lists: replace the task cache (SPEC D5: real paging, not 0,1000) */ SPAR_API int spar_tell_active(void); SPAR_API int spar_tell_waiting(int offset, int num); /* num <= 0 -> 100000 */ SPAR_API int spar_tell_stopped(int offset, int num); /* SPEC D3: the C# called tellWaiting */ SPAR_API int spar_tell_status(const char *gid); /* task cache (last tell_active/tell_waiting/tell_stopped) */ SPAR_API int spar_task_count(void); /* -2: no list loaded yet */ SPAR_API const char *spar_task_gid(int i); SPAR_API const char *spar_task_status(int i); SPAR_API const char *spar_task_dir(int i); SPAR_API int spar_task_total_length(int i, uint64_t *out); /* -2 out of range */ SPAR_API int spar_task_completed_length(int i, uint64_t *out); SPAR_API int spar_task_download_speed(int i, uint64_t *out); SPAR_API int spar_task_upload_speed(int i, uint64_t *out); SPAR_API int spar_task_connections(int i, uint64_t *out); SPAR_API int spar_task_error_code(int i, uint64_t *out); SPAR_API const char *spar_task_error_message(int i); SPAR_API int spar_task_progress_permille(int i); /* 0-1000, -2 out of range */ SPAR_API const char *spar_task_display_name(int i); /* SPEC D8: guarded files[0] */ SPAR_API int spar_task_file_count(int i); /* -2 out of range */ SPAR_API const char *spar_task_file_path(int i, int f); SPAR_API int spar_task_file_length(int i, int f, uint64_t *out); SPAR_API int spar_task_file_selected(int i, int f); /* 0/1, -2 out of range */ /* per-task reads (one request each; see SPEC D20 for getUris states) */ SPAR_API int spar_get_uris(const char *gid); SPAR_API int spar_get_files(const char *gid); SPAR_API int spar_get_option(const char *gid); SPAR_API int spar_get_peers(const char *gid); SPAR_API int spar_get_servers(const char *gid); SPAR_API int spar_get_global_option(void); /* ---- write: ALL REFUSE UNLESS ARMED (SPEC D12) ---- */ SPAR_API int spar_arm_writes(int armed); SPAR_API int spar_writes_armed(void); SPAR_API int spar_arm_shutdown(int armed); /* SPEC D13: second gate */ SPAR_API int spar_shutdown_armed(void); SPAR_API int spar_add_uri(const char *uri, const char *out_dir, const char *filename); SPAR_API int spar_add_torrent(const unsigned char *torrent, size_t n, const char *out_dir); SPAR_API int spar_add_magnet(const char *magnet, const char *out_dir); SPAR_API int spar_pause(const char *gid, int force); /* force -> aria2.forcePause */ SPAR_API int spar_unpause(const char *gid); SPAR_API int spar_pause_all(int force); /* force -> aria2.forcePauseAll */ SPAR_API int spar_unpause_all(void); SPAR_API int spar_remove(const char *gid, int force); /* force -> aria2.forceRemove (deletes files) */ SPAR_API int spar_remove_download_result(const char *gid); SPAR_API int spar_purge_download_result(void); SPAR_API int spar_change_option(const char *gid, const char *key, const char *value); SPAR_API int spar_change_global_option(const char *key, const char *value); SPAR_API int spar_change_uri(const char *gid, const char *uri, int index); /* SPEC Q4 */ SPAR_API int spar_change_position(const char *gid, int offset, int position); SPAR_API int spar_save_session(void); SPAR_API int spar_shutdown_server(int force); /* force -> aria2.forceShutdown */ /* ---- option keys: data from AriaOption.cs (SPEC D9) ---- */ SPAR_API int spar_option_key_count(void); SPAR_API const char *spar_option_key(int i); /* ---- diagnostics ---- */ SPAR_API int spar_last_rpc_error_code(void); /* 0 when none (SPEC D1) */ SPAR_API const char *spar_last_rpc_error_message(void); SPAR_API int spar_last_http_code(void); SPAR_API const char *spar_last_error(void); /* "" after success; never a token */ SPAR_API const char *spar_result_json(void); /* result, or the error object */ SPAR_API int spar_warning_count(void); SPAR_API const char *spar_warning(int i); /* ---- fake backend (test support, ships in the library) ---- */ SPAR_API int spar_fake_reset(void); /* kind 0: canned {code, body}; kind 1: transport failure {msg}. * method may be "*" for any method; url may be NULL. */ SPAR_API int spar_fake_route(const char *method, const char *url, int kind, long code, const char *body_or_msg); SPAR_API int spar_fake_calls(void); SPAR_API int spar_fake_write_calls(void); /* write verbs the fake received */ SPAR_API const char *spar_fake_last_method(void); SPAR_API const char *spar_fake_last_body(void); /* raw request JSON (test-only) */ SPAR_API const char *spar_fake_last_content_type(void); SPAR_API const char *spar_strerror(int code); SPAR_API const char *spar_version(void); #ifdef __cplusplus } #endif #endif /* SPAR_H */