libp7/include/libp7.h

223 lines
7.4 KiB
C

/* ************************************************************************** */
/* _____ _ */
/* libp7.h |_ _|__ _ _| |__ ___ _ _ */
/* | Project: libp7 | |/ _ \| | | | '_ \ / _ \ | | | */
/* | | (_) | |_| | | | | __/ |_| | */
/* By: thomas <thomas@touhey.fr> |_|\___/ \__,_|_| |_|\___|\__, |.fr */
/* Last updated: 2017/01/04 15:01:48 |___/ */
/* */
/* ************************************************************************** */
#ifndef LIBP7_H
# define LIBP7_H
# if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) \
&& !defined(__WINDOWS__)
# define __WINDOWS__
# endif
# include <libp7/config.h>
# include <stdio.h>
# include <stdint.h>
/* integer types */
# define PRIuP7SHORT PRIuFAST16
# define PRIxP7SHORT PRIxFAST16
typedef uint_fast16_t p7ushort_t;
# define PRIuP7INT PRIuFAST32
# define PRIxP7INT PRIxFAST32
typedef uint_fast32_t p7uint_t;
# include <libp7/buffer.h>
# include <inttypes.h> /* PRI*FAST* */
# ifdef __WINDOWS__
# include <malloc.h>
# else
# include <alloca.h>
# endif
# ifdef __cplusplus
extern "C" {
# endif
/* ************************************************************************** */
/* Types */
/* ************************************************************************** */
/* handles - from streams, include `libp7/stream.h` */
struct p7_handle_s;
typedef struct p7_handle_s p7_handle_t;
extern const size_t p7_handle_size;
# define p7_alloca_handle() (alloca(p7_handle_size))
/* A version ("xx.xx.xx" + type) */
typedef struct {
unsigned int major;
unsigned int minor;
unsigned int rev;
} p7_version_t;
/* CASIOWIN entry */
typedef struct {
/* version */
p7_version_t version;
/* syscall table address */
p7uint_t syscall_table_offset;
} p7_casiowin_entry_t;
/* ************************************************************************** */
/* libp7 errors */
/* ************************************************************************** */
/* Enumeration */
/* - Whenever you add/remove errors, think about updating core/strerror ! - */
typedef enum {
p7_noerror,
/* basic communication errors */
p7_error_uninitialized,
p7_error_nocalc,
p7_error_noaccess,
p7_error_nochar,
p7_error_com,
p7_error_timeout,
p7_error_checksum, /* not actually meant to be used */
p7_error_irrecoverable,
/* stream */
p7_error_nostream,
p7_error_noread,
p7_error_nowrite,
p7_error_noseek,
/* user interface */
p7_error_active,
p7_error_doubleshift,
p7_error_empty,
p7_error_filename,
p7_error_dirname,
p7_error_fullmem,
p7_error_notfound,
p7_error_denied_overwrite,
p7_error_terminated,
p7_error_interrupted,
/* others */
p7_error_unsupported,
p7_error_unsupported_device,
p7_error_alloc,
p7_error_unknown
} p7_error_t;
/* Message getting macro */
extern const char *p7_error_strings[];
# define p7_strerror(N) p7_error_strings[N]
# define p7_geterror(N) p7_error_strings[N]
/* ************************************************************************** */
/* Random stuff */
/* ************************************************************************** */
/* Filename and dirname */
int p7_validate_filename(const char *filename);
int p7_validate_dirname(const char *dirname);
/* ************************************************************************** */
/* Core functions */
/* ************************************************************************** */
/* Initialization and exit flags */
# define P7_ACTIVE 1
# define P7_CHECK 2
# define P7_TERM 4
/* "Normal" initialization */
int p7_init(p7_handle_t **handle, unsigned int flags);
int p7_cominit(p7_handle_t **handle, unsigned int flags, int com);
/* Testing initialization */
# ifndef P7_DISABLED_FILE
int p7_finit(p7_handle_t **handle, unsigned int flags,
FILE *readstream, FILE *writestream);
# endif
int p7_fdinit(p7_handle_t **handle, unsigned int flags,
const char *name, int readfd, int writefd);
/* De-initialization */
void p7_exit(p7_handle_t *handle);
/* ************************************************************************** */
/* Protocol */
/* ************************************************************************** */
/* Send a file using buffer */
int p7_send(p7_handle_t *handle, const p7_buffer_t *buffer,
const char *dirname, const char *filename, const char *devname,
int overwrite, int (*ow_conf)(void), void (*disp)(p7ushort_t, p7ushort_t));
/* Receive a file using buffer */
int p7_request(p7_handle_t *handle, const p7_buffer_t *buffer,
const char *dirname, const char *filename, const char *devname,
void (*disp)(p7ushort_t, p7ushort_t));
/* FILE version of the two previous functions */
# ifndef P7_DISABLED_FILE
int p7_sendfile(p7_handle_t *handle, FILE *file,
const char *dirname, const char *filename, const char *devname,
int overwrite, int (*ow_conf)(void), void (*disp)(p7ushort_t, p7ushort_t));
int p7_reqfile(p7_handle_t *handle, FILE *file,
const char *dirname, const char *filename, const char *devname,
void (*disp)(p7ushort_t, p7ushort_t));
# endif
/* Create a directory */
int p7_createdir(p7_handle_t *handle, const char *dirname, const char *devname);
/* Delete distant file */
int p7_delete(p7_handle_t *handle,
const char *dirname, const char *filename, const char *devname);
/* Copy file on distant device */
int p7_copy(p7_handle_t *handle,
const char *dirname, const char *filename,
const char *newdir, const char *newname, const char *devname);
/* List files */
int p7_list(p7_handle_t *handle, const char *devname,
void (*callback)(void*, const char*, const char*, p7uint_t), void *cookie);
/* Get the free memory amount */
int p7_getfreemem(p7_handle_t *handle, const char *devname,
p7uint_t *capacity);
/* Reset filesystem on a distant device */
int p7_reset(p7_handle_t *handle, const char *devname);
/* Optimize distant device */
int p7_optimize(p7_handle_t *handle, const char *devname);
/* Send an update.exe */
int p7_sendexe(p7_handle_t *handle, const p7_buffer_t *buffer,
p7uint_t loadaddr, p7uint_t straddr, void (*disp)(p7ushort_t, p7ushort_t));
/* Backup the flash ROM, bootcode and CASIOWIN entry */
int p7_backup_rom(p7_handle_t *handle, const p7_buffer_t *buffer,
void (*disp)(p7ushort_t, p7ushort_t));
int p7_backup_boot(p7_handle_t *handle, const p7_buffer_t *buffer,
void (*disp)(p7ushort_t, p7ushort_t));
int p7_backup_casiowin_entry(p7_handle_t *handle, p7_casiowin_entry_t *entry);
/* FILE version of the previous functions */
# ifndef P7_DISABLED_FILE
/* Send an update.exe */
int p7_sendexe_file(p7_handle_t *handle,
FILE *exe, p7uint_t loadaddr, p7uint_t straddr,
void (*disp)(p7ushort_t, p7ushort_t));
int p7_backup_romfile(p7_handle_t *handle, FILE *file,
void (*disp)(p7ushort_t, p7ushort_t));
int p7_backup_bootfile(p7_handle_t *handle, FILE *file,
void (*disp)(p7ushort_t, p7ushort_t));
# endif
/* Display streamed screen */
int p7_getscreen(p7_handle_t *handle, int (*callback)(int, int, uint32_t**));
# ifdef __cplusplus
}
# endif
# include <libp7/server.h>
#endif /* LIBP7_H */