From 1a1d119cb2323d689f33e3a464f9faf0e8af6e6b Mon Sep 17 00:00:00 2001 From: "Thomas \"Cakeisalie5\" Touhey" Date: Mon, 14 May 2018 21:55:17 +0200 Subject: [PATCH] Made progress on SCSI communications with the G90 --- include/libcasio/cdefs.h | 19 +- include/libcasio/error.h | 3 + include/libcasio/iter.h | 88 ++++++++ include/libcasio/link.h | 13 -- include/libcasio/log.h | 12 +- include/libcasio/mcs.h | 26 ++- include/libcasio/protocol/seven.h | 30 +++ include/libcasio/stream.h | 45 ++-- lib/errors.c | 3 +- lib/file/corresp/standard.c | 27 ++- lib/file/corresp/standard_sub.c | 16 +- lib/file/decode.c | 25 ++- lib/file/decode/cas.c | 7 +- lib/file/decode/casemul.c | 31 ++- lib/file/decode/decode.h | 15 +- lib/file/decode/fxi.c.draft | 15 +- lib/file/decode/std.c | 15 +- lib/file/decode/std/addin.c | 23 +- lib/file/decode/std/eact.c.draft | 52 +++-- lib/file/decode/std/fkey.c | 23 +- lib/file/decode/std/lang.c | 8 +- lib/file/decode/std/picture.c | 51 +++-- .../local/list.c => file/decode/storage.c} | 39 ++-- lib/file/file.h | 19 +- lib/file/manage.c | 15 +- lib/link/open/usb.c | 1 + lib/link/seven/command.c | 45 ++-- lib/link/seven/data.c | 16 +- lib/link/seven/dataflow.c | 15 +- lib/link/seven/datastream.c | 17 +- lib/link/seven/devices.c | 20 +- lib/link/seven/eack.c | 37 +-- lib/link/seven/scsi.c | 20 +- lib/link/seven/send.c | 16 +- lib/link/seven/server.c | 154 +++++++++++++ lib/link/seven_mcs/get.c | 31 ++- lib/link/seven_mcs/iter.c | 211 ++++++++++++++++++ lib/link/seven_mcs/list.c | 133 ----------- lib/link/seven_mcs/open.c | 25 ++- lib/link/seven_mcs/seven_mcs.h | 5 +- lib/link/usage/server/seven.c | 88 -------- lib/link/usage/usage.h | 20 +- lib/log/conv.c | 19 -- lib/log/iter.c | 90 ++++++++ lib/log/log.h | 12 +- lib/log/msg.c | 9 +- lib/mcs/list.c | 47 +++- lib/mcs/local/find.c | 3 +- lib/mcs/local/iter.c | 88 ++++++++ lib/mcs/local/local.h | 5 +- lib/mcs/local/open.c | 18 +- lib/mcsfile/decode/cas.c | 32 ++- lib/mcsfile/decode/cas/cell.c | 15 +- lib/mcsfile/decode/decode.h | 17 +- lib/mcsfile/decode/mcs.c | 28 ++- lib/mcsfile/decode/mcs/ssheet.c | 23 +- lib/mcsfile/decode/mcs/string.c | 15 +- lib/mcsfile/decode/mcs/var.c | 34 ++- lib/mcsfile/mcsfile.h | 19 +- lib/mcsfile/ref/cas_app.c | 25 ++- lib/mcsfile/ref/cas_data.c | 27 ++- lib/mcsfile/ref/mcs.c | 63 ++++-- lib/picture/decode.c | 44 ++-- lib/stream/attrs.c | 15 +- lib/stream/builtin/file.c | 18 +- lib/stream/builtin/libusb/scsi.c | 178 ++++++++++++++- lib/stream/builtin/limited.c | 14 +- lib/stream/builtin/memory.c | 14 +- lib/stream/open.c | 44 ++-- lib/utils/ascii.c | 15 +- lib/utils/iter.c | 130 +++++++++++ lib/utils/sleep.c | 26 ++- src/p7os/utils/osdisp.c | 15 +- src/p7screen/args.c | 17 +- tools/write-header-config | 15 +- 75 files changed, 1837 insertions(+), 771 deletions(-) create mode 100644 include/libcasio/iter.h rename lib/{mcs/local/list.c => file/decode/storage.c} (50%) create mode 100644 lib/link/seven/server.c create mode 100644 lib/link/seven_mcs/iter.c delete mode 100644 lib/link/seven_mcs/list.c delete mode 100644 lib/link/usage/server/seven.c create mode 100644 lib/log/iter.c create mode 100644 lib/mcs/local/iter.c create mode 100644 lib/utils/iter.c diff --git a/include/libcasio/cdefs.h b/include/libcasio/cdefs.h index e3f7cf0..bcb505d 100644 --- a/include/libcasio/cdefs.h +++ b/include/libcasio/cdefs.h @@ -115,16 +115,27 @@ # endif /* --- - * Warn if the result is unused. + * Various function attributes. * --- */ -/* To do that, we'll use the `casio_attr_wur` attribute. */ +/* Warn if the result is unused. + * To do that, we'll use the `CASIO_WUR` attribute. */ # if CASIO_GNUC_PREREQ(4, 0) -# define casio_attr_wur __attribute__((warn_unused_result)) +# define CASIO_WUR __attribute__((warn_unused_result)) # elif CASIO_MSC_PREREQ(17, 0) # include -# define casio_attr_wur _Check_return_ +# define CASIO_WUR _Check_return_ +# else +# define CASIO_WUR +# endif + +/* Warn if the function is deprecated. */ + +# if CASIO_GNUC_PREREQ(3, 0) +# define CASIO_DEPRECATED __attribute__((deprecated)) +# else +# define CASIO_DEPRECATED # endif # include "cdefs/integers.h" diff --git a/include/libcasio/error.h b/include/libcasio/error.h index 09f498b..f100da7 100644 --- a/include/libcasio/error.h +++ b/include/libcasio/error.h @@ -32,6 +32,7 @@ typedef int casio_error_t; # define casio_error_none 0x00 # define casio_error_success 0x00 +# define casio_error_ok 0x00 # define casio_noerror 0x00 /* Miscallaneous errors. */ @@ -42,6 +43,8 @@ typedef int casio_error_t; # define casio_error_arg 0x04 /* an argument was invalid. */ # define casio_error_invalid 0x04 /* (alias) */ # define casio_error_lock 0x05 /* mutex is locked */ +# define casio_error_iter 0x06 /* iteration has ended */ +# define casio_error_end 0x06 /* (alias) */ /* Stream errors. */ diff --git a/include/libcasio/iter.h b/include/libcasio/iter.h new file mode 100644 index 0000000..a05d528 --- /dev/null +++ b/include/libcasio/iter.h @@ -0,0 +1,88 @@ +/* **************************************************************************** + * libcasio/iter.h -- libcasio iterators. + * Copyright (C) 2017 Thomas "Cakeisalie5" Touhey + * + * This file is part of libcasio. + * libcasio is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 3.0 of the License, + * or (at your option) any later version. + * + * libcasio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with libcasio; if not, see . + * ************************************************************************* */ +#ifndef LIBCASIO_ITER_H +# define LIBCASIO_ITER_H 2018050901 +# include "cdefs.h" + +CASIO_BEGIN_NAMESPACE + +/* Many things in libcasio work with iterators. This is the centralized + * iterator interface. */ + +struct casio_iter_s; +typedef struct casio_iter_s casio_iter_t; + +/* --- + * Define something. + * --- */ + +/* The way this is done: + * + * When `casio_next()` is called, if there was a previous element, + * the `casio_nextfree_t` callback, if not NULL, will be called to free + * it, then in all cases, the `casio_next_t` callback will be called to + * get the next element. If the `casio_next_t` callback returns + * `casio_error_iter`, all following calls to `casio_next()` with this + * iterator won't call the callbacks and directly return the same error. + * + * When `casio_end()` is called, the `casio_end_t` callback, if not NULL, + * will be called. */ + +typedef int CASIO_EXPORT casio_next_t + OF((void *casio__cookie, void **casio__ptr)); +typedef void CASIO_EXPORT casio_nextfree_t + OF((void *casio__cookie, void *casio__ptr)); +typedef void CASIO_EXPORT casio_end_t + OF((void *casio__cookie)); + +typedef struct casio_iter_funcs_s { + casio_next_t *casio_iterfunc_next; + casio_nextfree_t *casio_iterfunc_nextfree; + casio_end_t *casio_iterfunc_end; +} casio_iter_funcs_t; + +/* --- + * Functions. + * --- */ + +CASIO_BEGIN_DECLS + +/* Start an iterator with `casio_iter_(&iter, )`, + * with `iter` being of the `casio_iter_t *` type. + * Get the next element through `casio_next_(iter, &ptr)`, + * which is usually a macro or function defined as + * `casio_next(iter, (void **)(PTRP))`. + * Then end the iterator using `casio_end(iter)`. + * + * Beware: any time you use `casio_next()` or `casio_end()`, the previous + * element might be free'd or re-used, so if you are interested in what is + * in it, copy the data before using one of the previous functions. */ + +CASIO_EXTERN int CASIO_EXPORT casio_iter + OF((casio_iter_t **casio__iterp, void *casio__cookie, + casio_iter_funcs_t const *casio__funcs)); +CASIO_EXTERN int CASIO_EXPORT casio_next + OF((casio_iter_t *casio__iter, void **casio__ptr)); +CASIO_EXTERN void CASIO_EXPORT casio_end + OF((casio_iter_t *casio__iter)); + +CASIO_END_DECLS +CASIO_END_NAMESPACE + +#endif diff --git a/include/libcasio/link.h b/include/libcasio/link.h index 7ff7e5f..ef9445a 100644 --- a/include/libcasio/link.h +++ b/include/libcasio/link.h @@ -167,19 +167,6 @@ CASIO_EXTERN void CASIO_EXPORT casio_unlock_link CASIO_EXTERN const casio_link_info_t* CASIO_EXPORT casio_get_link_info OF((casio_link_t *casio__handle)); -/* --- - * Run servers. - * --- */ - -/* Run a Protocol 7.00 server. */ - -typedef int CASIO_EXPORT casio_seven_server_func_t - OF((void *casio__cookie, casio_link_t *casio__handle)); - -CASIO_EXTERN int CASIO_EXPORT casio_seven_serve - OF((casio_link_t *casio__handle, - casio_seven_server_func_t **casio__callbacks, void *casio__cookie)); - /* --- * General-purpose link operations. * --- */ diff --git a/include/libcasio/log.h b/include/libcasio/log.h index c07ec18..0ccce1d 100644 --- a/include/libcasio/log.h +++ b/include/libcasio/log.h @@ -19,6 +19,7 @@ #ifndef LIBCASIO_LOG_H # define LIBCASIO_LOG_H # include "cdefs.h" +# include "iter.h" CASIO_BEGIN_NAMESPACE CASIO_BEGIN_DECLS @@ -29,14 +30,21 @@ CASIO_EXTERN void CASIO_EXPORT casio_setlog CASIO_EXTERN const char* CASIO_EXPORT casio_getlog OF((void)); -/* List log levels */ +/* List log levels (deprecated interface) */ typedef void casio_log_list_t OF((void *casio__cookie, const char *casio__str)); -CASIO_EXTERN void CASIO_EXPORT casio_listlog +CASIO_EXTERN CASIO_DEPRECATED void CASIO_EXPORT casio_listlog OF((casio_log_list_t *casio__callback, void *casio__cookie)); +/* List log levels (new interface). + * This iterator yields strings (`const char *`). */ + +CASIO_EXTERN int CASIO_EXPORT casio_iter_log + OF((casio_iter_t **casio__iter)); +# define casio_next_log(ITER, PTRP) (casio_next((ITER), (void **)(PTRP))) + CASIO_END_DECLS CASIO_END_NAMESPACE #endif /* LIBCASIO_LOG_H */ diff --git a/include/libcasio/mcs.h b/include/libcasio/mcs.h index 10dfd63..e455dbd 100644 --- a/include/libcasio/mcs.h +++ b/include/libcasio/mcs.h @@ -20,6 +20,7 @@ # define LIBCASIO_MCS_H # include "cdefs.h" # include "mcsfile.h" +# include "iter.h" CASIO_BEGIN_NAMESPACE /* Forward structure declarations (don't mind). */ @@ -54,12 +55,8 @@ typedef int CASIO_EXPORT casio_mcs_put_t typedef int CASIO_EXPORT casio_mcs_delete_t OF((void *casio__cookie, casio_mcshead_t *casio__mcshead)); -typedef void CASIO_EXPORT casio_mcslist_t - OF((void *casio__cookie, const casio_mcshead_t *casio__mcshead)); - -typedef int CASIO_EXPORT casio_mcs_list_t - OF((void *casio__cookie, casio_mcslist_t *casio__mcslist, - void *casio__mcslist_cookie)); +typedef int CASIO_EXPORT casio_mcs_iter_t + OF((void *casio__cookie, casio_iter_t **casio__iter)); typedef int CASIO_EXPORT casio_mcs_close_t OF((void *casio__cookie)); @@ -68,7 +65,7 @@ struct casio_mcsfuncs_s { casio_mcs_get_t *casio_mcsfuncs_get; casio_mcs_put_t *casio_mcsfuncs_put; casio_mcs_delete_t *casio_mcsfuncs_delete; - casio_mcs_list_t *casio_mcsfuncs_list; + casio_mcs_iter_t *casio_mcsfuncs_iter; casio_mcs_close_t *casio_mcsfuncs_close; }; @@ -108,12 +105,23 @@ CASIO_EXTERN int CASIO_EXPORT casio_transfer_mcsfile CASIO_EXTERN int CASIO_EXPORT casio_delete_mcsfile OF((casio_mcs_t *casio__mcs, casio_mcshead_t *casio__mcshead)); -/* List MCS files. */ +/* List MCS files (the deprecated way). */ -CASIO_EXTERN int CASIO_EXPORT casio_list_mcsfiles +typedef void CASIO_EXPORT casio_mcslist_t + OF((void *casio__cookie, const casio_mcshead_t *casio__mcshead)); + +CASIO_EXTERN CASIO_DEPRECATED int CASIO_EXPORT casio_list_mcsfiles OF((casio_mcs_t *casio__mcs, casio_mcslist_t *casio__mcslist, void *casio__mcookie)); +/* Iterate on MCS entries. */ + +CASIO_EXTERN int CASIO_EXPORT casio_iter_mcsfiles + OF((casio_mcs_t *casio__mcs, casio_iter_t **casio__iter)); + +# define casio_next_mcshead(ITER, MCSFILEP) \ + (casio_next((ITER), (void **)(casio_mcshead_t **)(MCSFILEP))) + /* Make a temporary main memory. */ CASIO_EXTERN int CASIO_EXPORT casio_open_local_mcs diff --git a/include/libcasio/protocol/seven.h b/include/libcasio/protocol/seven.h index 0d61124..91f6cab 100644 --- a/include/libcasio/protocol/seven.h +++ b/include/libcasio/protocol/seven.h @@ -448,6 +448,36 @@ CASIO_EXTERN int CASIO_EXPORT casio_seven_open_data_stream casio_link_t *casio__link, casio_off_t casio__size, casio_link_progress_t *casio__disp, void *casio__dcookie)); +/* Server-related functions. + * Put the link in server mode and process a command. + * + * `casio_seven_start_server()` does the following: + * - if active, send a swap and become passive; + * - if already passive, wait for the initial check to ACK and become + * ready to process requests. + * + * `casio_seven_process_request()` returns: + * - an error if the link isn't correct (valid, protocol 7, passive); + * - 0 if the next command has been fetched successfully, + * - `casio_error_iter` if the counterpart has finished sending commands; + * - `casio_error_unknown` otherwise. */ + +CASIO_EXTERN int CASIO_EXPORT casio_seven_start_server + OF((casio_link_t *casio__handle)); +CASIO_EXTERN int CASIO_EXPORT casio_seven_get_next_request + OF((casio_link_t *casio__handle)); + +/* Passive function that takes control of the execution thread and makes + * a server with callbacks. */ + +typedef int CASIO_EXPORT casio_seven_server_func_t + OF((void *casio__cookie, casio_link_t *casio__handle)); + +CASIO_EXTERN int CASIO_EXPORT casio_seven_serve + OF((casio_link_t *casio__handle, + casio_seven_server_func_t * const *casio__callbacks, + void *casio__cookie)); + /* --- * Protocol 7.00 high-level abstractions. * --- */ diff --git a/include/libcasio/stream.h b/include/libcasio/stream.h index 257c474..5a94c05 100644 --- a/include/libcasio/stream.h +++ b/include/libcasio/stream.h @@ -29,7 +29,8 @@ # include "cdefs.h" CASIO_BEGIN_NAMESPACE -/* forward structure declarations (don't mind) */ +/* Forward structure declarations (don't mind). */ + struct casio_stream_s; typedef struct casio_stream_s casio_stream_t; struct casio_streamfuncs_s; @@ -41,9 +42,10 @@ typedef struct casio_timeouts_s casio_timeouts_t; struct casio_scsi_s; typedef struct casio_scsi_s casio_scsi_t; -/* ************************************************************************* */ -/* Stream */ -/* ************************************************************************* */ +/* --- + * Stream. + * --- */ + /* The stream is a private structure that has more or less all of the features * that the libc FILE interface has, but has more and is cross-platform. * It is basically what libcasio uses to communicate with the calculator. @@ -149,9 +151,10 @@ struct casio_streamfuncs_s { (casio_stream_write_t*)(CASIO__WRITE), \ (casio_stream_seek_t*)(CASIO__SEEK), NULL, NULL} -/* ************************************************************************* */ -/* Stream settings values and flags */ -/* ************************************************************************* */ +/* --- + * Stream serial settings ad flags. + * --- */ + /* Here are the different baud speeds you can encounter, in bauds. * Note that one speed is not supported by all models. */ @@ -213,9 +216,11 @@ struct casio_streamfuncs_s { # define CASIO_XOFFMASK 0x0100 # define CASIO_XOFFCTL_DISABLE 0x0000 /* disable XOFF */ # define CASIO_XOFFCTL_ENABLE 0x0100 /* enable XOFF */ -/* ************************************************************************* */ -/* Stream settings */ -/* ************************************************************************* */ + +/* --- + * Stream settings. + * --- */ + /* Here is the stream settings structure: */ struct casio_streamattrs_s { @@ -246,9 +251,11 @@ struct casio_timeouts_s { /* This structure will be sent to your `settm` callback, usually after a state * change in the communication. Also, all timeouts are in milliseconds (ms). */ -/* ************************************************************************* */ -/* SCSI request */ -/* ************************************************************************* */ + +/* --- + * SCSI requests. + * --- */ + /* CASIO's fx-CG devices, also known as Prizms or Graph 90+E, use SCSI along * with Protocol 7.00 to communicate with the PC for things like file * transferring or screenstreaming (which use vendor-specific command slots). @@ -279,12 +286,12 @@ struct casio_timeouts_s { * `status` is the status byte returned by the device. */ struct casio_scsi_s { - void *casio_scsi_cmd; - unsigned int casio_scsi_cmd_len; - int casio_scsi_direction; - void *casio_scsi_data; - int casio_scsi_data_len; - int casio_scsi_status; + void *casio_scsi_cmd; + size_t casio_scsi_cmd_len; + int casio_scsi_direction; + void *casio_scsi_data; + size_t casio_scsi_data_len; + int casio_scsi_status; }; /* It will be sent to the `scsi` callback of the stream. */ diff --git a/lib/errors.c b/lib/errors.c index be544d4..928f696 100644 --- a/lib/errors.c +++ b/lib/errors.c @@ -27,7 +27,8 @@ const char* CASIO_EXPORT casio_error_strings[128] = { "a memory allocation has failed", "this operation is unsupported", "an argument was invalid", - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, + "no more elements in the iterator", + NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* Stream errors. */ diff --git a/lib/file/corresp/standard.c b/lib/file/corresp/standard.c index d11e5e6..f467ae3 100644 --- a/lib/file/corresp/standard.c +++ b/lib/file/corresp/standard.c @@ -18,16 +18,19 @@ * ************************************************************************* */ #include "standard.h" -/* ************************************************************************* */ -/* Helpers */ -/* ************************************************************************* */ +/* --- + * Helpers. + * --- */ + /* Flags */ + #define f_c1 casio_stdflag_check1 #define f_c2 casio_stdflag_check2 #define f_sub casio_stdflag_sub #define f_pic casio_stdflag_pic /* Subtype correspondance type */ + struct type_info { /* identification */ const char *type; @@ -42,6 +45,7 @@ struct type_info { }; /* Main type correspondance type */ + struct main_info { /* identification */ const char *type; @@ -51,6 +55,7 @@ struct main_info { }; /* Extension correspondance type */ + struct ext_corresp { /* identification */ const char *ext; @@ -61,9 +66,11 @@ struct ext_corresp { unsigned int libtype; unsigned int flags; }; -/* ************************************************************************* */ -/* Main correspondances */ -/* ************************************************************************* */ + +/* --- + * Main correspondances. + * --- */ + #define magic_common "\x00\x10\x00\x10\x00" #define cp_magic "\x00\x01\x00\x01\x00" #define blank "\xFF\xFF\xFF\xFF\xFF\xFF" @@ -165,9 +172,11 @@ CASIO_LOCAL struct ext_corresp ext_types[] = { /* sentinel */ {NULL, NULL, 0, 0, 0} }; -/* ************************************************************************* */ -/* Get the type information */ -/* ************************************************************************* */ + +/* --- + * Get the type information. + * --- */ + /** * casio_maketype_std: * Get the standard header type information. diff --git a/lib/file/corresp/standard_sub.c b/lib/file/corresp/standard_sub.c index 33d713c..0838b2f 100644 --- a/lib/file/corresp/standard_sub.c +++ b/lib/file/corresp/standard_sub.c @@ -19,15 +19,17 @@ #include "standard.h" /* Platforms and types */ + #define p_cp 0x00 #define p_cg 0x01 #define t_addin 0x01 #define t_fkey 0x02 #define t_lang 0x04 -/* ************************************************************************* */ -/* Helpers */ -/* ************************************************************************* */ +/* --- + * Helpers. + * --- */ + /* Type correspondance type */ struct type_info { /* identification */ @@ -38,9 +40,11 @@ struct type_info { casio_filefor_t platform; casio_filetype_t type; }; -/* ************************************************************************* */ -/* Correspondances */ -/* ************************************************************************* */ + +/* --- + * Correspondances. + * --- */ + /* Main types */ CASIO_LOCAL struct type_info types[] = { /* fx-CP types */ diff --git a/lib/file/decode.c b/lib/file/decode.c index 4baa29e..d7c5f73 100644 --- a/lib/file/decode.c +++ b/lib/file/decode.c @@ -18,13 +18,16 @@ * ************************************************************************* */ #include "file.h" -/* ************************************************************************* */ -/* Check using extensions */ -/* ************************************************************************* */ -/* Decode function */ +/* --- + * Check using extensions. + * --- */ + +/* Decode function. */ + typedef int decode_func OF((casio_file_t**, casio_stream_t*)); -/* Correspondance type */ +/* Correspondance type. */ + struct corresp { /* extension */ const char *ext; @@ -38,9 +41,7 @@ struct corresp { /* Correspondances */ CASIO_LOCAL struct corresp correspondances[] = { -#if 0 - {"g1s", casio_type_storage, casio_decode_storage}, -#endif + {"g1s", casio_filetype_storage, casio_decode_storage}, /* terminating entry */ {NULL, 0, NULL} @@ -74,9 +75,11 @@ CASIO_LOCAL int lookup_extension(const char *path, casio_filetype_t types, *func = c->decode; return (0); } -/* ************************************************************************* */ -/* Main decoding function */ -/* ************************************************************************* */ + +/* --- + * Main decoding function. + * --- */ + /** * casio_decode: * Decode a file. diff --git a/lib/file/decode/cas.c b/lib/file/decode/cas.c index b379d85..89d9894 100644 --- a/lib/file/decode/cas.c +++ b/lib/file/decode/cas.c @@ -102,9 +102,10 @@ fail: return (err); } -/* ************************************************************************* */ -/* Public decoding functions */ -/* ************************************************************************* */ +/* --- + * Public decoding functions. + * --- */ + /** * casio_decode_cas: * Decode a CAS file. diff --git a/lib/file/decode/casemul.c b/lib/file/decode/casemul.c index 9e90c5b..79d8c32 100644 --- a/lib/file/decode/casemul.c +++ b/lib/file/decode/casemul.c @@ -28,9 +28,10 @@ #define htoe32(N) ((big_endian) ? htobe32(N) : htole32(N)) #define VER htoe32(0x00000100) /* 1.00 */ -/* ************************************************************************* */ -/* MAKELONG */ -/* ************************************************************************* */ +/* --- + * MAKELONG + * --- */ + /* This utility comes from Microsoft Windows. * It makes a 32-bit integer out of two 16-bit integers. */ @@ -66,9 +67,11 @@ CASIO_LOCAL casio_uint32_t makelong_be(casio_uint16_t one, casio_uint16_t two) # undef MAKELONG #endif #define MAKELONG(A, B) ((big_endian) ? makelong_be(A, B) : makelong_le(A, B)) -/* ************************************************************************* */ -/* Utilities */ -/* ************************************************************************* */ + +/* --- + * Utilities. + * --- */ + /** * read_internal: * Read the internal headers, and check them. @@ -148,9 +151,11 @@ CASIO_LOCAL int read_top(casio_stream_t *buffer, char *name, /* no error! */ return (0); } -/* ************************************************************************* */ -/* Intermediate functions */ -/* ************************************************************************* */ + +/* --- + * Intermediate functions. + * --- */ + /** * read_picture: * Read a picture record and content. @@ -365,9 +370,11 @@ fail: casio_free(tab); return (0); } -/* ************************************************************************* */ -/* Main function */ -/* ************************************************************************* */ + +/* --- + * Main function. + * --- */ + /** * casio_decode_casemul: * Decode a CasEmul file. diff --git a/lib/file/decode/decode.h b/lib/file/decode/decode.h index 42a9b16..8aff9bd 100644 --- a/lib/file/decode/decode.h +++ b/lib/file/decode/decode.h @@ -21,9 +21,10 @@ # include "../file.h" # include -/* ************************************************************************* */ -/* Main decoding utilities */ -/* ************************************************************************* */ +/* --- + * Main decoding utilities. + * --- */ + /* Main decoding functions with expected types. */ CASIO_EXTERN int CASIO_EXPORT casio_decode_std OF((casio_file_t **casio__handle, @@ -41,9 +42,11 @@ CASIO_EXTERN int CASIO_EXPORT casio_decode_cas OF((casio_file_t **casio__handle, casio_stream_t *casio__buffer)); CASIO_EXTERN int CASIO_EXPORT casio_decode_grc OF((casio_file_t **casio__handle, casio_stream_t *casio__buffer)); -/* ************************************************************************* */ -/* "Standard"-specific decoding functions */ -/* ************************************************************************* */ + +/* --- + * "Standard"-specific decoding functions. + * --- */ + /* Schemes */ # define CASIO_STDFUNC(CASIO__NAME) \ CASIO_EXTERN int CASIO_EXPORT casio_decode_std_##CASIO__NAME \ diff --git a/lib/file/decode/fxi.c.draft b/lib/file/decode/fxi.c.draft index 32a7283..eb7b4e0 100644 --- a/lib/file/decode/fxi.c.draft +++ b/lib/file/decode/fxi.c.draft @@ -28,9 +28,10 @@ DREAD(NAM, TYPE) \ obf(&NAM, sizeof(struct TYPE)); } -/* ************************************************************************* */ -/* Obfuscation-related functions */ -/* ************************************************************************* */ +/* --- + * Obfuscation-related functions. + * --- */ + /** * obf: * Obfuscate AND de-obfuscate a string. @@ -51,9 +52,11 @@ CASIO_LOCAL void obf(unsigned char *s, size_t n) while (n--) *s++ ^= ~24; } -/* ************************************************************************* */ -/* File header */ -/* ************************************************************************* */ + +/* --- + * File header. + * --- */ + static unsigned char header[] = "\xD5\xD7\x1F" "f" "FX-INTERFACE - YELLOW COMPUTING" /* 35 */ diff --git a/lib/file/decode/std.c b/lib/file/decode/std.c index e696db2..7ffb91e 100644 --- a/lib/file/decode/std.c +++ b/lib/file/decode/std.c @@ -20,9 +20,10 @@ #include "../corresp/standard.h" #define FUNC(NAME) casio_decode_std_##NAME -/* ************************************************************************* */ -/* Getting the decoding function */ -/* ************************************************************************* */ +/* --- + * Getting the decoding function. + * --- */ + /* Correspondance type */ typedef int decode_func (); struct decode_corresp { @@ -90,9 +91,11 @@ CASIO_LOCAL int find_decode_function(casio_filefor_t platform, *rd = c->decode; return (0); } -/* ************************************************************************* */ -/* Main standard header decoding function */ -/* ************************************************************************* */ + +/* --- + * Main standard header decoding function. + * --- */ + /** * casio_decode_std: * Decode a file with standard header. diff --git a/lib/file/decode/std/addin.c b/lib/file/decode/std/addin.c index d6f2bf5..aa017bf 100644 --- a/lib/file/decode/std/addin.c +++ b/lib/file/decode/std/addin.c @@ -18,9 +18,10 @@ * ************************************************************************* */ #include "../decode.h" -/* ************************************************************************* */ -/* Add-in for the fx-9860G (G1A) */ -/* ************************************************************************* */ +/* --- + * Add-in for the fx-9860G (G1A). + * --- */ + /** * casio_decode_std_addin: * Decodes a "normal" add-in (after Standard Header). @@ -85,9 +86,11 @@ fail: casio_free_file(*h); *h = NULL; return (err); } -/* ************************************************************************* */ -/* Add-in for the fx-CP/Classpad (C1A) */ -/* ************************************************************************* */ + +/* --- + * Add-in for the fx-CP/Classpad (C1A). + * --- */ + /** * casio_decode_std_cp_addin: * Decode fx-CP add-in. @@ -155,9 +158,11 @@ fail: casio_free_file(*h); *h = NULL; return (err); } -/* ************************************************************************* */ -/* Add-in for the fx-CG/Prizm (G3A) */ -/* ************************************************************************* */ + +/* --- + * Add-in for the fx-CG/Prizm (G3A). + * --- */ + /** * casio_decode_std_cg_addin: * Decode fx-CG add-in. diff --git a/lib/file/decode/std/eact.c.draft b/lib/file/decode/std/eact.c.draft index ed5a0d7..376b92c 100644 --- a/lib/file/decode/std/eact.c.draft +++ b/lib/file/decode/std/eact.c.draft @@ -18,10 +18,12 @@ * ************************************************************************* */ #include "../decode.h" -/* ************************************************************************* */ -/* Content parsing */ -/* ************************************************************************* */ -/* line content parsing function prototype */ +/* --- + * Content parsing. + * --- */ + +/* Line content parsing function prototype. */ + static int eact_decode_line(casio_line_t *handle, uint8_t *buf, size_t size, uint_fast8_t type); @@ -133,25 +135,29 @@ fail: return (err); } -/* ************************************************************************* */ -/* Content Type correspondance list */ -/* ************************************************************************* */ -/* Correspondance type */ +/* --- + * Content Type correspondance list. + * --- */ + +/* Correspondance type. */ + struct eact_content_type_corresp { const char *type; int (*decode)(casio_line_t*, uint8_t*, size_t); }; -/* Correspondance list */ +/* Correspondance list. */ + static struct eact_content_type_corresp eact_content_types[] = { {"@EACT\0\0", eact_decode_content_eact}, {} }; -/* ************************************************************************* */ -/* Line parsing */ -/* ************************************************************************* */ +/* --- + * Line parsing. + * --- */ + /** * eact_decode_line_content: * Decodes an E-Activity content. @@ -371,10 +377,13 @@ static int eact_decode_line_code(casio_line_t *handle, uint8_t *buf, mem((ll_info, buf, size)); return (0); } -/* ************************************************************************* */ -/* Line Type correspondance list */ -/* ************************************************************************* */ -/* Correspondance type */ + +/* --- + * Line Type correspondance list. + * --- */ + +/* Correspondance type. */ + struct eact_line_type_corresp { int rawtype; @@ -382,7 +391,8 @@ struct eact_line_type_corresp { const char *info; }; -/* All correspondances */ +/* All correspondances. */ + static struct eact_line_type_corresp eact_line_types[] = { {casio_eact_ltype_calc, eact_decode_line_calculation, "calculation"}, @@ -434,9 +444,11 @@ static int eact_decode_line(casio_line_t *handle, uint8_t *buf, size_t size, msg((ll_info, "line type is '%s' (0x%02x)", linetype->info, type)); return ((*linetype->decode)(handle, buf, size)); } -/* ************************************************************************* */ -/* Main parsing functions */ -/* ************************************************************************* */ + +/* --- + * Main parsing functions. + * --- */ + /** * casio_decode_std_eact: * Decodes an EACT. diff --git a/lib/file/decode/std/fkey.c b/lib/file/decode/std/fkey.c index 51012da..3099912 100644 --- a/lib/file/decode/std/fkey.c +++ b/lib/file/decode/std/fkey.c @@ -18,9 +18,10 @@ * ************************************************************************* */ #include "../decode.h" -/* ************************************************************************* */ -/* Utilities */ -/* ************************************************************************* */ +/* --- + * Utilities. + * --- */ + /** * fkeydup: * "Duplicate" function key. @@ -58,9 +59,11 @@ CASIO_LOCAL casio_pixel_t **fkeydup3(unsigned char *fkey) FKEY3_WIDTH, FKEY3_HEIGHT); return (pixels); } -/* ************************************************************************* */ -/* Decoding fx function keys file (G1N) */ -/* ************************************************************************* */ + +/* --- + * Decoding fx function keys file (G1N). + * --- */ + /** * casio_decode_std_fkey: * Decode fx function keys files. @@ -127,9 +130,11 @@ fail: casio_free_file(*h); *h = NULL; return (err); } -/* ************************************************************************* */ -/* Decoding fx-CG/Prizm function keys file (G3N) */ -/* ************************************************************************* */ + +/* --- + * Decoding fx-CG/Prizm function keys file (G3N). + * --- */ + /** * casio_decode_std_cg_fkey: * Decode fx-CG key files. diff --git a/lib/file/decode/std/lang.c b/lib/file/decode/std/lang.c index 3c0a879..6d30450 100644 --- a/lib/file/decode/std/lang.c +++ b/lib/file/decode/std/lang.c @@ -53,9 +53,11 @@ CASIO_LOCAL int decode_lang_entry(casio_file_t *handle, msg((ll_info, "[#%d] '%s'", id, str)); return (0); } -/* ************************************************************************* */ -/* fx language files parsing */ -/* ************************************************************************* */ + +/* --- + * fx language files parsing. + * --- */ + /** * casio_decode_std_lang: * Decode fx language files. diff --git a/lib/file/decode/std/picture.c b/lib/file/decode/std/picture.c index 6914c19..178557b 100644 --- a/lib/file/decode/std/picture.c +++ b/lib/file/decode/std/picture.c @@ -19,9 +19,10 @@ #include "../decode.h" #include -/* ************************************************************************* */ -/* Obfuscation-related functions */ -/* ************************************************************************* */ +/* --- + * Obfuscation-related functions. + * --- */ + /** * g3p_deobfuscate: * De-obfuscate the image data. @@ -37,9 +38,11 @@ CASIO_LOCAL void g3p_deobfuscate(unsigned char *buf, size_t n) *buf++ = (byte << 5) | (byte >> 3); } } -/* ************************************************************************* */ -/* Prizm Picture decoding function */ -/* ************************************************************************* */ + +/* --- + * Prizm Picture decoding function. + * --- */ + /** * casio_decode_std_g3p: * Decode a G3P file. @@ -64,8 +67,11 @@ int CASIO_EXPORT casio_decode_std_g3p(casio_file_t **h, casio_stream_t *buffer, casio_uint8_t *defbuf = NULL, *infbuf = NULL; casio_file_t *handle; - *h = NULL; (void)pic; - /* read the image header */ + *h = NULL; + (void)pic; + + /* Read the image header. */ + DREAD(ihd) width = be16toh(ihd.casio_prizm_picture_subheader_width); height = be16toh(ihd.casio_prizm_picture_subheader_height); @@ -81,37 +87,42 @@ int CASIO_EXPORT casio_decode_std_g3p(casio_file_t **h, casio_stream_t *buffer, rawcol)); err = casio_error_magic; goto fail; } + msg((ll_info, "Picture format: %s", coldesc)); gen = be16toh(ihd.casio_prizm_picture_subheader_generator_id); msg((ll_info, "Generator ID: 0x%04X", gen)); msg((ll_info, "-")); - /* read deflated image */ + /* Read the deflated image. */ + deflated_size = be32toh(ihd.casio_prizm_picture_subheader_data_size) - 6; /* FIXME: dangerous */ msg((ll_info, "Reading %" CASIO_PRIuSIZE "B of deflated data", deflated_size)); err = casio_error_alloc; - if (!(defbuf = casio_alloc(deflated_size, 1))) goto fail; + if (!(defbuf = casio_alloc(deflated_size, 1))) + goto fail; READ(defbuf, deflated_size) - /* unobfuscate if required */ + /* Unobfuscate if required. */ + if (std->casio_standard_header_obfuscated0) { msg((ll_info, "Is obfuscated, let's deobfuscate!")); g3p_deobfuscate(defbuf, deflated_size); } - /* make the destination buffer */ + /* Make the destination buffer. */ + rawsize = casio_get_picture_size(NULL, picfmt, width, height); err = casio_error_alloc; - if (!(infbuf = casio_alloc(rawsize, 1))) goto fail; + if (!(infbuf = casio_alloc(rawsize, 1))) + goto fail; /* uncompress */ inflated_size = (uLong)rawsize; - z_err = uncompress(infbuf, &inflated_size, - defbuf, (uLong)deflated_size); + z_err = uncompress(infbuf, &inflated_size, defbuf, (uLong)deflated_size); if (z_err) { - msg((ll_fatal, "Zlib error: error #%d", z_err)); + msg((ll_fatal, "Zlib error %d: %s", z_err, zError(err))); err = casio_error_magic; goto fail; } @@ -151,9 +162,11 @@ fail: casio_free(infbuf); casio_free(defbuf); return (err); } -/* ************************************************************************* */ -/* Classpad Picture decoding function */ -/* ************************************************************************* */ + +/* --- + * Classpad Picture decoding function. + * --- */ + /** * casio_decode_std_c2p: * Decode Classpad images. diff --git a/lib/mcs/local/list.c b/lib/file/decode/storage.c similarity index 50% rename from lib/mcs/local/list.c rename to lib/file/decode/storage.c index f12f8a2..6c00ac9 100644 --- a/lib/mcs/local/list.c +++ b/lib/file/decode/storage.c @@ -1,6 +1,6 @@ /* **************************************************************************** - * mcs/local/list.c -- list files in a local main memory. - * Copyright (C) 2017 Thomas "Cakeisalie5" Touhey + * file/decode/decode.c -- decode a "standard" CASIO file. + * Copyright (C) 2018 Thomas "Cakeisalie5" Touhey * * This file is part of libcasio. * libcasio is free software; you can redistribute it and/or modify it @@ -16,35 +16,24 @@ * You should have received a copy of the GNU Lesser General Public License * along with libcasio; if not, see . * ************************************************************************* */ -#include "local.h" +#include "decode.h" /** - * casio_localmcs_list: - * List files in a local main memory. + * casio_decode_storage: + * Decode a fx-9860G storage memory. * - * @arg cookie the local main memory cookie. - * @arg mcslist the listing callback. - * @arg mcookie the callback cookie. + * @arg handlep the handle to create. + * @arg buffer the buffer to read from. */ -int CASIO_EXPORT casio_localmcs_list(localmcs_t *cookie, - casio_mcslist_t *mcslist, void *mcookie) +int CASIO_EXPORT casio_decode_storage(casio_file_t **handlep, + casio_stream_t *buffer) { - int i, count = cookie->localmcs_count; + /* TODO: code this function. */ - for (i = 0; count && i < cookie->localmcs_size; i++) { - casio_mcsfile_t *file = cookie->localmcs_files[i]; + (void)handlep; + (void)buffer; - /* Check that the file exists. */ - if (!file) continue; - - /* Decrement the count, so that we don't have to go to the end. */ - count--; - - /* Call back the callback. */ - (*mcslist)(mcookie, &file->casio_mcsfile_head); - } - - /* Everything went well :) */ - return (0); + msg((ll_info, "storage memory decoding is not available yet.")); + return (casio_error_op); } diff --git a/lib/file/file.h b/lib/file/file.h index b24c9f9..f818d3d 100644 --- a/lib/file/file.h +++ b/lib/file/file.h @@ -21,10 +21,12 @@ # include "../internals.h" # include "decode/decode.h" -/* ************************************************************************* */ -/* Macros for interacting with the buffer */ -/* ************************************************************************* */ +/* --- + * Macros for interacting with the buffer. + * --- */ + /* Read from a stream. */ + # define READ(CASIO__TO, CASIO__SZ) /* normal read */ { \ int READ_err = casio_read(buffer, (CASIO__TO), (CASIO__SZ)); \ if (READ_err) return (READ_err); } @@ -35,12 +37,14 @@ goto fail; /* Read using size of the object. */ + # define DREAD(CASIO__TO) \ READ(&CASIO__TO, sizeof(CASIO__TO)) # define GDREAD(CASIO__TO) \ GREAD(&CASIO__TO, sizeof(CASIO__TO)) /* Skip. */ + # define SKIP(CASIO__SZ) { \ int SKIP_err = casio_skip(buffer, CASIO__SZ); \ if (SKIP_err) return (SKIP_err); } @@ -49,14 +53,17 @@ if (err) goto fail; } /* Write. */ + # define WRITE(CASIO__BUF, CASIO__SZ) { \ int WRITE_err = casio_write(buffer, (CASIO__BUF), (CASIO__SZ)); \ if (WRITE_err) return (WRITE_err); } # define DWRITE(CASIO__OBJECT) \ WRITE(&(CASIO__OBJECT), sizeof(CASIO__OBJECT)) -/* ************************************************************************* */ -/* Picture utilities */ -/* ************************************************************************* */ + +/* --- + * Picture utilities. + * --- */ + # define alloc_pixels(W, H) \ casio_alloc(sizeof(casio_pixel_t*) * \ (H) + sizeof(casio_pixel_t) * (W) * (H), 1) diff --git a/lib/file/manage.c b/lib/file/manage.c index 6eac551..291fda9 100644 --- a/lib/file/manage.c +++ b/lib/file/manage.c @@ -23,9 +23,10 @@ return (casio_error_alloc); \ handle = *h; memset(handle, 0, sizeof(casio_file_t)) -/* ************************************************************************* */ -/* Make a handle */ -/* ************************************************************************* */ +/* --- + * Make a handle. + * --- */ + /** * casio_make_picture: * Make a picture handle. @@ -262,9 +263,11 @@ fail: casio_free_file(*h); *h = NULL; return (casio_error_alloc); } -/* ************************************************************************* */ -/* Free a handle */ -/* ************************************************************************* */ + +/* --- + * Free a handle. + * --- */ + /** * casio_free_file: * Free a handle and its data. diff --git a/lib/link/open/usb.c b/lib/link/open/usb.c index 8c04ca4..e5bb101 100644 --- a/lib/link/open/usb.c +++ b/lib/link/open/usb.c @@ -63,6 +63,7 @@ int CASIO_EXPORT casio_open_usb(casio_link_t **handle, unsigned long flags, switch ((err = casio_open_seven_scsi(&ns, stream))) { case 0: + msg((ll_info, "Using stream over SCSI commands.")); stream = ns; break; diff --git a/lib/link/seven/command.c b/lib/link/seven/command.c index e42bad5..647f6ac 100644 --- a/lib/link/seven/command.c +++ b/lib/link/seven/command.c @@ -24,9 +24,10 @@ #include #include -/* ************************************************************************* */ -/* Logging */ -/* ************************************************************************* */ +/* --- + * Logging. + * --- */ + /** * getowstring: * Get overwrite string (useful for logging). @@ -46,10 +47,13 @@ CASIO_LOCAL const char* getowstring(casio_seven_ow_t code) if (code > 0x02) return (""); return (moo[code]); } -/* ************************************************************************* */ -/* Macros */ -/* ************************************************************************* */ -/* if command is not supported, return */ + +/* --- + * Macros. + * --- */ + +/* if command is not supported, return. */ + #define CHECK_IF_COMMAND_IS_SUPPORTED(N) \ if (!command_is_supported(N)) \ return (casio_error_op); @@ -147,9 +151,11 @@ int CASIO_EXPORT casio_seven_send_cmd_data(casio_link_t *handle, return (casio_seven_send_ext(handle, casio_seven_type_cmd, subtype, buf, buflen, 1)); } -/* ************************************************************************* */ -/* Typical MCS command */ -/* ************************************************************************* */ + +/* --- + * Typical MCS command. + * --- */ + /** * casio_seven_send_typical_mcs_command: * Send a typical MCS command. @@ -174,9 +180,11 @@ int CASIO_EXPORT casio_seven_send_typical_mcs_command(casio_link_t *handle, head->casio_mcshead_dirname, head->casio_mcshead_name, head->casio_mcshead_group, NULL, NULL, NULL)); } -/* ************************************************************************* */ -/* Special commands */ -/* ************************************************************************* */ + +/* --- + * Special commands. + * --- */ + /** * casio_seven_send_cmdsys_setlink: * Set link settings. @@ -240,10 +248,13 @@ int CASIO_EXPORT casio_seven_send_cmdosu_upandrun(casio_link_t *handle, return (casio_seven_send_ext(handle, casio_seven_type_cmd, casio_seven_cmdosu_upandrun, buf, 24, 1)); } -/* ************************************************************************* */ -/* Decode incoming packet */ -/* ************************************************************************* */ -/* macro to copy an argument into the packet */ + +/* --- + * Decode incoming packet. + * --- */ + +/* Macro to copy an argument into the packet. */ + #define cpy_arg(I) { \ size_t ALEN = arglengths[I]; \ memcpy(packet->casio_seven_packet__argsdata[I], p, ALEN); \ diff --git a/lib/link/seven/data.c b/lib/link/seven/data.c index b6a6093..0d995ae 100644 --- a/lib/link/seven/data.c +++ b/lib/link/seven/data.c @@ -20,9 +20,10 @@ * ************************************************************************* */ #include "data.h" -/* ************************************************************************* */ -/* Main functions */ -/* ************************************************************************* */ +/* --- + * Main functions. + * --- */ + /** * casio_seven_send_data_packet: * Send data packet. @@ -109,9 +110,11 @@ int CASIO_EXPORT casio_seven_unshift(casio_link_t *handle) /* then return */ return (0); } -/* ************************************************************************* */ -/* Decode a data packet data */ -/* ************************************************************************* */ + +/* --- + * Decode data from a data packet. + * --- */ + /** * casio_seven_decode_data: * Get data from data packet data field. @@ -135,6 +138,7 @@ int CASIO_EXPORT casio_seven_decode_data(casio_link_t *handle, * data packet, without any need for an answer from the other part. * This is a preferable method of identifying these strangly * formatted data packets. */ + if (packet->casio_seven_packet_code < 0x70 || packet->casio_seven_packet_code > 0x7F) { /* total number */ diff --git a/lib/link/seven/dataflow.c b/lib/link/seven/dataflow.c index d9530e2..3286976 100644 --- a/lib/link/seven/dataflow.c +++ b/lib/link/seven/dataflow.c @@ -23,9 +23,10 @@ #define BUFNUM 64 #define BUFSIZE (ONEBUF * BUFNUM) -/* ************************************************************************* */ -/* Buffer version */ -/* ************************************************************************* */ +/* --- + * Buffer version. + * --- */ + /** * casio_seven_send_buffer: * Part of the packet flows where data is sent. @@ -190,9 +191,11 @@ int CASIO_EXPORT casio_seven_get_buffer(casio_link_t *handle, fail: return (err); } -/* ************************************************************************* */ -/* Data version */ -/* ************************************************************************* */ + +/* --- + * Data version. + * --- */ + /** * casio_seven_send_data: * Part of the packet flow where data is sent - buffer version. diff --git a/lib/link/seven/datastream.c b/lib/link/seven/datastream.c index e1bf5d1..2ffc850 100644 --- a/lib/link/seven/datastream.c +++ b/lib/link/seven/datastream.c @@ -24,6 +24,7 @@ #define BUFSIZE CASIO_SEVEN_MAX_RAWDATA_SIZE /* Cookie structure. */ + typedef struct { int _faulty, _read; @@ -38,9 +39,11 @@ typedef struct { unsigned char _reserved[8]; unsigned char _current[BUFSIZE]; } seven_data_cookie_t; -/* ************************************************************************* */ -/* Callbacks */ -/* ************************************************************************* */ + +/* --- + * Callbacks. + * --- */ + /** * casio_seven_data_read: * Read data from the calculator, using Protocol 7.00 data flow. @@ -288,9 +291,11 @@ fail: casio_free(cookie); return (err); } -/* ************************************************************************* */ -/* Opening functions */ -/* ************************************************************************* */ + +/* --- + * Opening functions. + * --- */ + CASIO_LOCAL const casio_streamfuncs_t seven_data_callbacks = casio_stream_callbacks_for_virtual(casio_seven_data_close, casio_seven_data_read, casio_seven_data_write, NULL); diff --git a/lib/link/seven/devices.c b/lib/link/seven/devices.c index a046a67..e70746c 100644 --- a/lib/link/seven/devices.c +++ b/lib/link/seven/devices.c @@ -29,9 +29,11 @@ typedef struct { const char *model; casio_seven_env_t env; } env_corresp_t; -/* ************************************************************************* */ -/* Main data */ -/* ************************************************************************* */ + +/* --- + * Main data. + * --- */ + /* mask bits */ #define MASK_ALL 1 #define MASK_RESET 2 @@ -189,14 +191,14 @@ CASIO_LOCAL env_corresp_t known_environments[] = { MASK_ALL | MASK_RESET | MASK_MCS}}, {"Gy49000F", {"Graph 25+E (modified fx-7400GII-2)", MASK_ALL | MASK_RESET | MASK_MCS}}, - {"Gy363004", {"fx-9860G (Graph 85)", - MASK_ALL | MASK_RESET | MASK_MCS | MASK_FLS}}, {"Gy362006", {"fx-9750GII (Graph 25+Pro)", MASK_ALL | MASK_RESET | MASK_MCS}}, {"Gy362007", {"fx-9750GII-2 (Graph 35+ SH4)", MASK_ALL | MASK_RESET | MASK_MCS}}, {"Gy36200F", {"Graph 35+E (modified fx-9750GII-2)", MASK_ALL | MASK_RESET | MASK_MCS}}, + {"Gy363004", {"fx-9860G (Graph 85)", + MASK_ALL | MASK_RESET | MASK_MCS | MASK_FLS}}, {"Gy363006", {"fx-9860GII (Graph 75 SH3)", MASK_ALL | MASK_RESET | MASK_MCS | MASK_FLS | MASK_OSUPDATE_1}}, @@ -211,9 +213,11 @@ CASIO_LOCAL env_corresp_t known_environments[] = { {NULL, {"Default environment", MASK_ALL | MASK_MCS | MASK_FLS}} }; -/* ************************************************************************* */ -/* Internal functions */ -/* ************************************************************************* */ + +/* --- + * Internal functions. + * --- */ + /** * casio_seven_getenv: * Get the environment type. diff --git a/lib/link/seven/eack.c b/lib/link/seven/eack.c index 441cb7c..afbb6fd 100644 --- a/lib/link/seven/eack.c +++ b/lib/link/seven/eack.c @@ -19,20 +19,21 @@ #include "../link.h" #include -/* ************************************************************************* */ -/* Raw layout of an extended ack data */ -/* ************************************************************************* */ +/* --- + * Raw layout of an extended ack data. + * --- */ + typedef struct { /* hardware identifier - ASCII */ unsigned char hwid[8]; /* processor identifier - ASCII */ unsigned char cpuid[16]; - /* preprogrammed ROM capacity - ASCII-hex (Ko) */ + /* preprogrammed ROM capacity - ASCII-dec (Ko) */ unsigned char preprog_rom_capacity[8]; - /* flash ROM capacity - ASCII-hex (Ko) */ + /* flash ROM capacity - ASCII-dec (Ko) */ unsigned char flash_rom_capacity[8]; - /* RAM capacity - ASCII-hex (Ko) */ + /* RAM capacity - ASCII-dec (Ko) */ unsigned char ram_capacity[8]; /* preprogrammed ROM version - "xx.xx.xxxx" + 0xff bytes */ @@ -56,13 +57,14 @@ typedef struct { unsigned char protocol_version[4]; /* product ID */ unsigned char product_id[16]; - /* name set by user in SYSTEM */ + /* name set by user in SYSTEM (if not in exam mode) */ unsigned char username[16]; } packetdata_ackext_t; -/* ************************************************************************* */ -/* Utilities */ -/* ************************************************************************* */ +/* --- + * Utilities. + * --- */ + /** * version_of_string: * Get version from 16-bytes char buffer. @@ -92,9 +94,11 @@ CASIO_LOCAL void string_of_version(unsigned char *data, casio_encode_version((char*)data, ver); memset(&data[10], '\xFF', 6); } -/* ************************************************************************* */ -/* Send ACK packets */ -/* ************************************************************************* */ + +/* --- + * Send ACK packets. + * --- */ + /** * casio_seven_send_eack: * Sends an extended ack. @@ -157,9 +161,10 @@ int CASIO_EXPORT casio_seven_send_eack(casio_link_t *handle, sizeof(packetdata_ackext_t), 1)); } -/* ************************************************************************* */ -/* Decode an ACK packet */ -/* ************************************************************************* */ +/* --- + * Decode an ACK packet. + * --- */ + /** * cpy_string: * Copy a string terminated with 0xFFs, with a maximum size. diff --git a/lib/link/seven/scsi.c b/lib/link/seven/scsi.c index 1951590..fd3dfff 100644 --- a/lib/link/seven/scsi.c +++ b/lib/link/seven/scsi.c @@ -107,7 +107,6 @@ CASIO_LOCAL int seven_scsi_read(seven_scsi_cookie_t *cookie, do { casio_uint8_t *to; size_t avail; - /* casio_uint16_t activity; */ /* Polling loop. */ @@ -116,6 +115,8 @@ CASIO_LOCAL int seven_scsi_read(seven_scsi_cookie_t *cookie, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; casio_uint8_t poll_data[16]; + msg((ll_info, "Polling available data using command C0...")); + /* Send the polling command, extract the data. */ scsi.casio_scsi_cmd = poll_command; @@ -127,13 +128,16 @@ CASIO_LOCAL int seven_scsi_read(seven_scsi_cookie_t *cookie, if ((err = casio_scsi_request(cookie->stream, &scsi))) return (err); - avail = (poll_data[7] << 8) | poll_data[8]; - /* activity = (poll_data[10] << 8) | poll_data[11]; */ + mem((ll_info, poll_data, 16)); + + avail = (poll_data[6] << 8) | poll_data[7]; /* Check if there are some bytes to get. */ if (!avail) { - /* FIXME: delay and check the timeout!!! */ + msg((ll_info, "No available data, sleeping for 1 second.")); + casio_sleep(1000); + /* FIXME: check the timeout!!! */ continue; } @@ -208,6 +212,8 @@ CASIO_LOCAL int seven_scsi_write(seven_scsi_cookie_t *cookie, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; casio_uint8_t poll_data[16]; + msg((ll_info, "Polling the activity using command C0...")); + /* Poll to check the activity. */ scsi.casio_scsi_cmd = poll_command; @@ -219,14 +225,16 @@ CASIO_LOCAL int seven_scsi_write(seven_scsi_cookie_t *cookie, if ((err = casio_scsi_request(cookie->stream, &scsi))) return (err); +#if 0 activity = (poll_data[10] << 8) | poll_data[11]; - if (activity == 0x100) { + if (activity == 0x1000) { /* The calculator is busy. * FIXME: delay and check the timeout!! */ continue; } +#endif break; } @@ -238,6 +246,8 @@ CASIO_LOCAL int seven_scsi_write(seven_scsi_cookie_t *cookie, casio_uint8_t send_command[16] = {0xC2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + msg((ll_info, "Sending the data using command C2...")); + send_command[6] = (to_send >> 8) & 0xFF; send_command[7] = to_send & 0xFF; diff --git a/lib/link/seven/send.c b/lib/link/seven/send.c index f22d24d..9eb4892 100644 --- a/lib/link/seven/send.c +++ b/lib/link/seven/send.c @@ -36,9 +36,11 @@ handle->casio_link_flags = \ ( handle->casio_link_flags & ~casio_linkflag_sendalt) | \ (~handle->casio_link_flags & casio_linkflag_sendalt); -/* ************************************************************************* */ -/* Main functions */ -/* ************************************************************************* */ + +/* --- + * Main functions. + * --- */ + /** * casio_seven_send_buf: * Send a buffer. @@ -206,9 +208,11 @@ int CASIO_EXPORT casio_seven_send_again(casio_link_t *handle) /* send it */ return (casio_seven_send_buf(handle, NULL, 0, 1)); } -/* ************************************************************************* */ -/* Special packets */ -/* ************************************************************************* */ + +/* --- + * Special packets. + * --- */ + /** * casio_seven_send_err_resend: * Resend the resend error. diff --git a/lib/link/seven/server.c b/lib/link/seven/server.c new file mode 100644 index 0000000..a4b9666 --- /dev/null +++ b/lib/link/seven/server.c @@ -0,0 +1,154 @@ +/* **************************************************************************** + * link/usage/server/seven.c -- serving a Protocol 7.00 server. + * Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey + * + * This file is part of libcasio. + * libcasio is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 3.0 of the License, + * or (at your option) any later version. + * + * libcasio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with libcasio; if not, see . + * ************************************************************************* */ +#include "../link.h" + +/* --- + * New-style server requests and responses. + * --- */ + +/** + * casio_seven_start_server: + * Become passive when active. + * + * @arg handle the link handle. + * @return the error code (0 if ok). + */ + +int CASIO_EXPORT casio_seven_start_server(casio_link_t *handle) +{ + int err; + + /* Make checks. */ + + if (!handle) + return (casio_error_init); + if (handle->casio_link_flags & casio_linkflag_ended) + return (casio_error_init); + + /* If active, we just want to send a swap and become passive. */ + + if (handle->casio_link_flags & casio_linkflag_active) { + if ((err = casio_seven_send_swp(handle))) + return (err); + return (0); + } + + /* We are passive, so we want to wait for the initial check from + * the counterpart and ACK it. If the received packet is not an + * initial check, we don't respond, just like the calculator. + * + * TODO: and if the start packet is in the middle of a packet? */ + + do { + if ((err = casio_seven_receive(handle, 0))) + return (err); + } while (response.casio_seven_packet_type != casio_seven_type_chk + || !response.casio_seven_packet_initial); + + err = casio_seven_send_ack(handle, 1); + if (err) return (err); + + return (0); +} + +/** + * casio_seven_get_next_request: + * Get the next request if server. + * + * @arg handle the link handle. + * @return the error. + */ + +int CASIO_EXPORT casio_seven_get_next_request(casio_link_t *handle) +{ + int err; + + /* TODO: check if the last action was receive, and if it is not + * the case, receive? */ + + if (response.casio_seven_packet_type != casio_seven_type_cmd) { + if (response.casio_seven_packet_type == casio_seven_type_end) + return (casio_seven_send_ack(handle, 0)); + if (response.casio_seven_packet_type == casio_seven_type_swp) + return (casio_error_iter); + if ((err = casio_seven_send_err(handle, casio_seven_err_other))) + return (err); + } + + return (0); +} + +/* --- + * Old-style server using the new-style utilities. + * --- */ + +/** + * casio_seven_serve: + * Take control of the execution thread to make a passive server. + * + * @arg handle the link handle. + * @arg callbacks the callbacks. + * @arg cookie i suppose that's just the way that cookie crumbles. + * @return the error code (0 if ok). + */ + +int CASIO_EXPORT casio_seven_serve(casio_link_t *handle, + casio_seven_server_func_t * const *callbacks, void *cookie) +{ + int err; + + if ((err = casio_seven_start_server(handle)) && err != casio_error_active) + return (err); + while (1) { + /* Check if the next element is a command. */ + + switch (casio_seven_get_next_request(handle)) { + case 0: + break; + case casio_error_iter: + return (0); + default: + return (err); + } + + /* Check if the callback exists. */ + + if (!callbacks[response.casio_seven_packet_code]) { + err = casio_seven_send_err(handle, casio_seven_err_other); + if (err) + return (err); + continue; + } + + /* Call the callback. */ + + err = (*callbacks[response.casio_seven_packet_code])(cookie, handle); + if (err) { + if (err != casio_error_unknown) + return (err); + + err = casio_seven_send_err(handle, casio_seven_err_other); + if (err) + return (err); + continue; + } + } + + return (0); +} diff --git a/lib/link/seven_mcs/get.c b/lib/link/seven_mcs/get.c index 7660234..9c51f68 100644 --- a/lib/link/seven_mcs/get.c +++ b/lib/link/seven_mcs/get.c @@ -20,9 +20,10 @@ * ************************************************************************* */ #include "seven_mcs.h" -/* ************************************************************************* */ -/* Protocol 7.00 MCS file gathering */ -/* ************************************************************************* */ +/* --- + * Protocol 7.00 MCS file gathering. + * --- */ + struct thecookie { int _called; int _err; @@ -91,9 +92,11 @@ CASIO_LOCAL int get_setup_entry(casio_setup_t *setup, casio_link_t *handle) /* Once the setup has been fed, send the ACK. */ return (casio_seven_send_ack(handle, 1)); } -/* ************************************************************************* */ -/* File request function */ -/* ************************************************************************* */ + +/* --- + * File request function. + * --- */ + /** * request_file: * Request a file. @@ -141,9 +144,11 @@ CASIO_LOCAL int request_file(sevenmcs_t *cookie, /* Check if the function was called. */ return (thecookie._called ? thecookie._err : casio_error_unknown); } -/* ************************************************************************* */ -/* Special types */ -/* ************************************************************************* */ + +/* --- + * Special types. + * --- */ + /** * request_alphamem: * Get the alpha memory. @@ -241,9 +246,11 @@ fail: *mcsfile = NULL; return (err); } -/* ************************************************************************* */ -/* Main function */ -/* ************************************************************************* */ + +/* --- + * Main function. + * --- */ + /** * casio_sevenmcs_get: * Get a file from a Protocol 7.00 main memory interface. diff --git a/lib/link/seven_mcs/iter.c b/lib/link/seven_mcs/iter.c new file mode 100644 index 0000000..3c606a0 --- /dev/null +++ b/lib/link/seven_mcs/iter.c @@ -0,0 +1,211 @@ +/* **************************************************************************** + * link/seven_mcs/iter.c -- iterate on files on a protocol seven main memory. + * Copyright (C) 2018 Thomas "Cakeisalie5" Touhey + * + * This file is part of libcasio. + * libcasio is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 3.0 of the License, + * or (at your option) any later version. + * + * libcasio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with libcasio; if not, see . + * ************************************************************************* */ +#include "seven_mcs.h" + +#define IS_ALPHA 0 +#define IS_SETUP 1 +#define IS_OTHER 2 + +struct thecookie { + casio_link_t *handle; + int status; + casio_mcshead_t head; +}; + +/* Alpha memory and settings are two special entries that are not explicitely + * listed and got/sent as files, we ought to make an abstraction powerful + * enough to treat them as they are. */ + +const casio_mcshead_t casio_sevenmcs_list_alpha_entry = { + 0, casio_mcstype_alphamem, 0, + /* count: */ 29, 0, 0, 0, + /* rawtype: */ 0x00, 0, 0, + /* name: */ "ALPHA MEM", "", + /* group: */ "ALPHA MEM", + /* dirname: */ "$GLOBAL", + "", "" +}; +const casio_mcshead_t casio_sevenmcs_list_setup_entry = { + 0, casio_mcstype_setup, 0, + 0, 0, 0, 100, + /* rawtype: */ 0x14, 0, 0, + /* name: */ "SETUP", "", + /* group: */ "SETUP", + /* dirname: */ "$GLOBAL", + "", "" +}; + +/** + * next_entry: + * Get the next file. + * + * @arg cookie the iterator cookie. + * @arg ptr the pointer to set. + * @return the error code (0 if ok). + */ + +CASIO_LOCAL int next_entry(struct thecookie *cookie, void **ptr) +{ + int err; + casio_link_t *handle = cookie->handle; + casio_mcshead_t *head = &cookie->head; + + /* The pointer will always be on the head contained in the cookie. */ + + *ptr = head; + + /* Special file. */ + + if (cookie->status == IS_ALPHA) { + cookie->status++; + memcpy(head, &casio_sevenmcs_list_alpha_entry, sizeof(*head)); + return (0); + } + + if (cookie->status == IS_SETUP) { + cookie->status++; + memcpy(head, &casio_sevenmcs_list_setup_entry, sizeof(*head)); + return (0); + } + + /* Main loop. */ + + while (1) { + /* Get the next request. */ + + err = casio_seven_get_next_request(handle); + if (err) { /* `casio_error_iter` included */ + *ptr = NULL; + return (err); + } + + /* Copy the raw information. */ + + head->casio_mcshead_flags = casio_mcsfor_mcs; + head->casio_mcshead_size = response.casio_seven_packet_filesize; + head->casio_mcshead_rawtype = response.casio_seven_packet_mcstype; + + if (response.casio_seven_packet_args[0]) { + strncpy(head->casio_mcshead_dirname, + response.casio_seven_packet_args[0], 8); + head->casio_mcshead_dirname[8] = 0; + } else + head->casio_mcshead_dirname[0] = 0; + + if (response.casio_seven_packet_args[1]) { + strncpy(head->casio_mcshead_name, + response.casio_seven_packet_args[1], 12); + head->casio_mcshead_name[12] = 0; + } else + head->casio_mcshead_name[0] = 0; + + if (response.casio_seven_packet_args[2]) { + strncpy(head->casio_mcshead_group, + response.casio_seven_packet_args[2], 16); + head->casio_mcshead_group[16] = 0; + } else + head->casio_mcshead_group[0] = 0; + + casio_correct_mcshead(head, 0); + + /* Send an ACK. */ + + if ((err = casio_seven_send_ack(handle, 1))) + return (err); + + /* Check if it is a file we want to ignore. */ + + if (head->casio_mcshead_type + & (casio_mcstype_alpha | casio_mcstype_setup)) + continue; + + break; + } + + /* The head is ready, and `*ptr` is already filled (since the beginning + * of the function), so let's send this! */ + + return (0); +} + +/** + * free_cookie: + * Free the cookie. + * + * @arg cookie the cookie. + */ + +CASIO_LOCAL void free_cookie(struct thecookie *cookie) +{ + int err; + + /* Try to empty the iterator until we're active */ + + while (!casio_seven_get_next_request(cookie->handle) + && !casio_seven_send_ack(cookie->handle, 1)); + + /* Free the cookie. */ + + casio_free(cookie); +} + +/* The functions structure. */ + +CASIO_LOCAL casio_iter_funcs_t funcs = { + (casio_next_t *)next_entry, + NULL, + (casio_end_t *)free_cookie +}; + +/** + * casio_sevenmcs_iter: + * Iterate on a Protocol 7.00 MCS. + * + * @arg cookie the cookie. + * @arg iterp the iterator to create. + * @return the error code (0 if ok). + */ + +int CASIO_EXPORT casio_sevenmcs_iter(sevenmcs_t *cookie, + casio_iter_t **iterp) +{ + casio_link_t *handle = cookie->sevenmcs_link; + struct thecookie *itercookie; + int err; + + /* Allocate the cookie. */ + + itercookie = casio_alloc(sizeof(*cookie), 1); + if (!itercookie) + return (casio_error_alloc); + itercookie->handle = handle; + itercookie->status = IS_ALPHA; + + /* Prepare the link. */ + + if ((err = casio_seven_send_cmdmcs_reqallinfo(handle)) + || (err = casio_seven_start_server(handle))) { + casio_free(cookie); + return (err); + } + + /* We're good, let's create the iterator. */ + + return (casio_iter(iterp, itercookie, &funcs)); +} diff --git a/lib/link/seven_mcs/list.c b/lib/link/seven_mcs/list.c deleted file mode 100644 index 7a6fed4..0000000 --- a/lib/link/seven_mcs/list.c +++ /dev/null @@ -1,133 +0,0 @@ -/* **************************************************************************** - * link/seven_mcs/list.c -- list files on a protocol seven main memory. - * Copyright (C) 2017 Thomas "Cakeisalie5" Touhey - * - * This file is part of libcasio. - * libcasio is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation; either version 3.0 of the License, - * or (at your option) any later version. - * - * libcasio is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with libcasio; if not, see . - * ************************************************************************* */ -#include "seven_mcs.h" - -struct thecookie { - casio_mcslist_t *_mcslist; - void *_mcookie; -}; - -/** - * file_info: - * The file information callback. - * - * @arg cookie the cookie. - * @arg handle the link handle. - * @return the error code (0 if ok). - */ - -CASIO_LOCAL int file_info(struct thecookie *cookie, casio_link_t *handle) -{ - casio_mcshead_t head; - - /* Copy the raw information. */ - head.casio_mcshead_flags = casio_mcsfor_mcs; - head.casio_mcshead_size = response.casio_seven_packet_filesize; - head.casio_mcshead_rawtype = response.casio_seven_packet_mcstype; - if (response.casio_seven_packet_args[0]) { - strncpy(head.casio_mcshead_dirname, - response.casio_seven_packet_args[0], 8); - head.casio_mcshead_dirname[8] = 0; - } else head.casio_mcshead_dirname[0] = 0; - if (response.casio_seven_packet_args[1]) { - strncpy(head.casio_mcshead_name, - response.casio_seven_packet_args[1], 12); - head.casio_mcshead_name[12] = 0; - } else head.casio_mcshead_name[0] = 0; - if (response.casio_seven_packet_args[2]) { - strncpy(head.casio_mcshead_group, - response.casio_seven_packet_args[2], 16); - head.casio_mcshead_group[16] = 0; - } else head.casio_mcshead_group[0] = 0; - - /* Try to make abstract information out of the raw information. */ - casio_correct_mcshead(&head, 0); - - /* If these are not special files we want to ignore, - * return this head to the user, and end the command response. */ - if (!(head.casio_mcshead_type - & (casio_mcstype_alpha | casio_mcstype_setup))) - (*cookie->_mcslist)(cookie->_mcookie, &head); - - /* Don't forget the ack! */ - return (casio_seven_send_ack(handle, 1)); -} - -/* Alpha memory and settings are two special entries that are not explicitely - * listed and got/sent as files, we ought to make an abstraction powerful - * enough to treat them as they are. */ - -const casio_mcshead_t CASIO_EXPORT casio_sevenmcs_list_alpha_entry = { - 0, casio_mcstype_alphamem, 0, - /* count: */ 29, 0, 0, 0, - /* rawtype: */ 0x00, 0, 0, - /* name: */ "ALPHA MEM", "", - /* group: */ "ALPHA MEM", - /* dirname: */ "$GLOBAL", - "", "" -}; -const casio_mcshead_t CASIO_EXPORT casio_sevenmcs_list_setup_entry = { - 0, casio_mcstype_setup, 0, - 0, 0, 0, 100, - /* rawtype: */ 0x14, 0, 0, - /* name: */ "SETUP", "", - /* group: */ "SETUP", - /* dirname: */ "$GLOBAL", - "", "" -}; - -/** - * casio_sevenmcs_list: - * List a Protocol 7.00 MCS. - * - * @arg cookie the cookie. - * @arg mcslist the listing callback. - * @arg mcookie the listing callback cookie. - * @return the error code (0 if ok). - */ - -int CASIO_EXPORT casio_sevenmcs_list(sevenmcs_t *cookie, - casio_mcslist_t *mcslist, void *mcookie) -{ - int err; casio_link_t *handle = cookie->sevenmcs_link; - struct thecookie thecookie; - - msg((ll_info, "Sending the file info transfer all request.")); - err = casio_seven_send_cmdmcs_reqallinfo(handle); - if (err) return (err); - if (response.casio_seven_packet_type != casio_seven_type_ack) - return (casio_error_unknown); - - /* Prepare the cookie. */ - thecookie._mcslist = mcslist; - thecookie._mcookie = mcookie; - - /* Call the callbacks with the default entries. */ - (*mcslist)(mcookie, &casio_sevenmcs_list_alpha_entry); - (*mcslist)(mcookie, &casio_sevenmcs_list_setup_entry); - - msg((ll_info, "Preparing the callbacks and running the server.")); - memset(handle->casio_link_seven_callbacks, 0, - 256 * sizeof(casio_seven_server_func_t*)); - handle->casio_link_seven_callbacks[casio_seven_cmdmcs_fileinfo] = - (casio_seven_server_func_t*)&file_info; - err = casio_seven_serve(handle, handle->casio_link_seven_callbacks, - &thecookie); - return (err); -} diff --git a/lib/link/seven_mcs/open.c b/lib/link/seven_mcs/open.c index 7d04cce..e9699f9 100644 --- a/lib/link/seven_mcs/open.c +++ b/lib/link/seven_mcs/open.c @@ -18,9 +18,10 @@ * ************************************************************************* */ #include "seven_mcs.h" -/* ************************************************************************* */ -/* Close callback */ -/* ************************************************************************* */ +/* --- + * Close callback. + * --- */ + /** * casio_sevenmcs_close: * Close a Protocol 7.00 MCS cookie. @@ -38,14 +39,16 @@ CASIO_LOCAL int casio_sevenmcs_close(sevenmcs_t *cookie) /* Callbacks. */ CASIO_LOCAL casio_mcsfuncs_t seven_mcs_funcs = { - (casio_mcs_get_t*)&casio_sevenmcs_get, -/* (casio_mcs_put_t*)&casio_sevenmcs_put, */ NULL, - (casio_mcs_delete_t*)&casio_sevenmcs_delete, - (casio_mcs_list_t*)&casio_sevenmcs_list, - (casio_mcs_close_t*)&casio_sevenmcs_close}; -/* ************************************************************************* */ -/* Main opening function */ -/* ************************************************************************* */ + (casio_mcs_get_t *)&casio_sevenmcs_get, +/* (casio_mcs_put_t *)&casio_sevenmcs_put, */ NULL, + (casio_mcs_delete_t *)&casio_sevenmcs_delete, + (casio_mcs_iter_t *)&casio_sevenmcs_iter, + (casio_mcs_close_t *)&casio_sevenmcs_close}; + +/* --- + * Main opening function. + * --- */ + /** * casio_open_seven_mcs: * Open a Protocol 7.00 MCS interface. diff --git a/lib/link/seven_mcs/seven_mcs.h b/lib/link/seven_mcs/seven_mcs.h index 286d9bc..c2b82b8 100644 --- a/lib/link/seven_mcs/seven_mcs.h +++ b/lib/link/seven_mcs/seven_mcs.h @@ -34,9 +34,8 @@ CASIO_EXTERN int CASIO_EXPORT casio_sevenmcs_put CASIO_EXTERN int CASIO_EXPORT casio_sevenmcs_delete OF((sevenmcs_t *casio__cookie, casio_mcshead_t *casio__mcshead)); -CASIO_EXTERN int CASIO_EXPORT casio_sevenmcs_list - OF((sevenmcs_t *casio__cookie, casio_mcslist_t *casio__mcslist, - void *casio__mcslist_cookie)); +CASIO_EXTERN int CASIO_EXPORT casio_sevenmcs_iter + OF((sevenmcs_t *casio__cookie, casio_iter_t **casio__iterp)); /* Other globals. */ diff --git a/lib/link/usage/server/seven.c b/lib/link/usage/server/seven.c deleted file mode 100644 index 2822de9..0000000 --- a/lib/link/usage/server/seven.c +++ /dev/null @@ -1,88 +0,0 @@ -/* **************************************************************************** - * link/usage/server/seven.c -- serving a Protocol 7.00 server. - * Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey - * - * This file is part of libcasio. - * libcasio is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation; either version 3.0 of the License, - * or (at your option) any later version. - * - * libcasio is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with libcasio; if not, see . - * ************************************************************************* */ -#include "../usage.h" - -/** - * casio_seven_serve: - * Serve a Protocol 7.00 server. - * - * @arg handle the link handle. - * @arg callbacks the server callbacks (256 entries). - * @arg cookie the server cookie. - * @return the error. - */ - -int CASIO_EXPORT casio_seven_serve(casio_link_t *handle, - casio_seven_server_func_t **callbacks, void *cookie) -{ - int err; - - /* Make checks. */ - chk_handle(handle); - chk_seven(handle); - - if (handle->casio_link_flags & casio_linkflag_active) { - err = casio_seven_send_swp(handle); - if (err) return (err); - } else { - /* Initial check. */ - do { - if ((err = casio_seven_receive(handle, 0))) - return (err); - } while (response.casio_seven_packet_type != casio_seven_type_chk - || !response.casio_seven_packet_initial); - - /* Ack and start! */ - err = casio_seven_send_ack(handle, 1); - if (err) return (err); - } - - /* Main loop. */ - while (1) { - /* Check command packet. */ - if (response.casio_seven_packet_type != casio_seven_type_cmd) { - if (response.casio_seven_packet_type == casio_seven_type_end) - return (casio_seven_send_ack(handle, 0)); - if (response.casio_seven_packet_type == casio_seven_type_swp) - break; - - err = casio_seven_send_err(handle, casio_seven_err_other); - if (err) return (err); - } - - /* Check if the callback exists. */ - if (!callbacks[response.casio_seven_packet_code]) { - err = casio_seven_send_err(handle, casio_seven_err_other); - if (err) return (err); - continue; - } - - /* Call the callback. */ - err = (*callbacks[response.casio_seven_packet_code])(cookie, handle); - if (err) { - if (err != casio_error_unknown) return (err); - err = casio_seven_send_err(handle, casio_seven_err_other); - if (err) return (err); - continue; - } - } - - /* Ack and disconnect. */ - return (0); -} diff --git a/lib/link/usage/usage.h b/lib/link/usage/usage.h index 575dee7..72ec0a1 100644 --- a/lib/link/usage/usage.h +++ b/lib/link/usage/usage.h @@ -20,59 +20,73 @@ # define LOCAL_LINK_USAGE_H 1 # include "../link.h" -/* ************************************************************************* */ -/* Quick-checking macros. */ -/* ************************************************************************* */ +/* --- + * Quick-checking macros. + * --- */ + /* Check that the handle exists. */ + # define chk_handle_exists(CASIO__H) \ if (!(CASIO__H)) return (casio_error_init); /* Check that the handle exists and is usable. */ + # define chk_handle(CASIO__H) \ chk_handle_exists(CASIO__H) \ if ((CASIO__H)->casio_link_flags & casio_linkflag_ended) \ return (casio_error_init); /* Check that the protocol we're using is for Protocol Seven. */ + # define chk_seven(CASIO__H) /* TODO */ /* Check that the handle is active. */ + # define chk_active(CASIO__H) \ if (~(CASIO__H)->casio_link_flags & casio_linkflag_active) \ return (casio_error_active); /* Check that the handle is passive. */ + # define chk_passive(CASIO__H) \ if ((CASIO__H)->casio_link_flags & casio_linkflag_active) \ return (casio_error_active); /* Check that the file name is correct. */ + # define chk_filename(CASIO__FN) /* TODO */ /* Check that the filename is there and ok. */ + # define chk_required_filename(CASIO__FN) \ if (!(CASIO__FN)) return (casio_error_invalid); /* Check that the directory name is ok. */ + # define chk_dirname(CASIO__DN) /* TODO */ /* Check that the MCS request head is ok. */ + # define chk_head(CASIO__HD) \ casio_correct_mcsfile_head(CASIO__HD); \ if (!(CASIO__HD)->casio_mcshead_name[0]) return (casio_error_invalid); \ if (!(CASIO__HD)->casio_mcshead_group[0]) return (casio_error_invalid); /* Check if the buffer is readable. */ + # define chk_bufread(CASIO__BUF) /* TODO */ /* Check if the buffer is writable. */ + # define chk_bufwrite(CASIO__BUF) /* TODO */ /* Check that the memory buffer is valid and not empty. */ + # define chk_mem(CASIO__MEM) \ if (!(CASIO__MEM)) return (casio_error_nostream); /* Check if the file is not empty. */ + # define chk_filesize(CASIO__SZ) \ if (!(casio_off_t)(CASIO__SZ)) return (casio_error_empty); diff --git a/lib/log/conv.c b/lib/log/conv.c index bd8c4bd..78e2a87 100644 --- a/lib/log/conv.c +++ b/lib/log/conv.c @@ -61,22 +61,3 @@ casio_loglevel_t CASIO_EXPORT casio_loglevel_fromstring(const char *string) return (casio_loglevel_none); } #endif - -/** - * casio_listlog: - * List log levels. - * - * @arg callback the callback. - * @arg cookie the callback cookie. - */ - -void CASIO_EXPORT casio_listlog(casio_log_list_t *callback, void *cookie) -{ - (*callback)(cookie, "none"); -#if !defined(LIBCASIO_DISABLED_LOG) - (*callback)(cookie, "info"); - (*callback)(cookie, "warn"); - (*callback)(cookie, "error"); - (*callback)(cookie, "fatal"); -#endif -} diff --git a/lib/log/iter.c b/lib/log/iter.c new file mode 100644 index 0000000..d080cdc --- /dev/null +++ b/lib/log/iter.c @@ -0,0 +1,90 @@ +/* **************************************************************************** + * log/iter.c -- iterate on log levels. + * Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey + * + * This file is part of libcasio. + * libcasio is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 3.0 of the License, + * or (at your option) any later version. + * + * libcasio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with libcasio; if not, see . + * ************************************************************************* */ +#include "log.h" + +CASIO_LOCAL const char *log_levels[] = { + "none", +#if !defined(LIBCASIO_DISABLED_LOG) + "info", + "warn", + "error", + "fatal", +#endif + NULL +}; + +/* `next_log()`: next log level. */ + +CASIO_LOCAL int CASIO_EXPORT next_log(const char **cookie, const char **level) +{ + if (!*cookie) + return (casio_error_iter); + + *level = *cookie++; + return (0); +} + +/* `end_log_iter()`: end the iteration log. */ + +CASIO_LOCAL int CASIO_EXPORT end_log_iter(const char **cookie) +{ + free(cookie); + return (0); +} + +/* Functions. */ + +CASIO_LOCAL casio_iter_funcs_t log_funcs = { + (casio_next_t *)next_log, + NULL, + (casio_end_t *)end_log_iter +}; + +/* `casio_iter_log()`: make a loglevels iterator. */ + +int CASIO_EXPORT casio_iter_log(casio_iter_t **iterp) +{ + const char **ptr; + + ptr = malloc(sizeof(*ptr)); + if (!ptr) + return (casio_error_alloc); + + *ptr = "none"; + return (casio_iter(iterp, ptr, &log_funcs)); +} + +/* --- + * List logs, per compatibility. + * --- */ + +/* `casio_listlog()`: list log levels. */ + +void CASIO_EXPORT casio_listlog(casio_log_list_t *callback, void *cookie) +{ + casio_iter_t *iter; + const char *level; + + if (casio_iter_log(&iter)) + return ; + while (casio_next_log(iter, &level)) + (*callback)(cookie, level); + + casio_end(iter); +} diff --git a/lib/log/log.h b/lib/log/log.h index 24983ed..069bc96 100644 --- a/lib/log/log.h +++ b/lib/log/log.h @@ -62,10 +62,13 @@ typedef int casio_loglevel_t; # if defined(LIBCASIO_DISABLED_FILE) && !defined(LIBCASIO_DISABLED_LOG) # define LIBCASIO_DISABLED_LOG # endif -/* ************************************************************************* */ -/* Log utilities */ -/* ************************************************************************* */ + +/* --- + * Log utilities. + * --- */ + /* Main functions */ + # if defined(LIBCASIO_DISABLED_LOG) # define msg(CASIO__ARGS) # define mem(CASIO__ARGS) @@ -129,6 +132,7 @@ CASIO_EXTERN void CASIO_EXPORT casio_log_prefix OF((casio_loglevel_t casio__loglevel, const char *casio__func)); # if defined(__STDC__) && __STDC__ + CASIO_EXTERN void CASIO_EXPORT casio_log_msg (casio_loglevel_t casio__loglevel, const char *casio__func, const char *casio__format, ...); @@ -136,8 +140,10 @@ CASIO_EXTERN void CASIO_EXPORT casio_log_mem (casio_loglevel_t casio__loglevel, const char *casio__func, const void *casio__m, size_t casio__n); # else + CASIO_EXTERN void CASIO_EXPORT casio_log_msg(); CASIO_EXTERN void CASIO_EXPORT casio_log_mem(); + # endif # endif diff --git a/lib/log/msg.c b/lib/log/msg.c index 5a5cfa3..c8c78ee 100644 --- a/lib/log/msg.c +++ b/lib/log/msg.c @@ -32,10 +32,11 @@ void CASIO_EXPORT casio_log_prefix(casio_loglevel_t loglevel, const char *func) { if (func && !strncmp(func, "casio_", 6)) func = &func[6]; - if (func) fprintf(stderr, "[libcasio %5s] %s: ", - casio_loglevel_tostring(loglevel), func); - else fprintf(stderr, "[libcasio %5s] ", - casio_loglevel_tostring(loglevel)); + if (func) + fprintf(stderr, "\r[libcasio %5s] %s: ", + casio_loglevel_tostring(loglevel), func); + else + fprintf(stderr, "\r[libcasio %5s] ", casio_loglevel_tostring(loglevel)); } /** diff --git a/lib/mcs/list.c b/lib/mcs/list.c index 6308746..5126eaf 100644 --- a/lib/mcs/list.c +++ b/lib/mcs/list.c @@ -18,9 +18,30 @@ * ************************************************************************* */ #include "mcs.h" +/** + * casio_iter_mcsfiles: + * Get an iterator to iterate on MCS file entries (heads). + * + * @arg mcs the main memory interface. + * @arg iter the iterator to make. + * @return the error code (0 if ok). + */ + +int CASIO_EXPORT casio_iter_mcsfiles(casio_mcs_t *mcs, casio_iter_t **iterp) +{ + casio_mcs_iter_t *func; + + if (!mcs) + return (casio_error_invalid); + func = mcs->casio_mcs_funcs.casio_mcsfuncs_iter; + if (!func) + return (casio_error_op); + return ((*func)(mcs->casio_mcs_cookie, iterp)); +} + /** * casio_list_mcsfiles: - * List an MCS file. + * List MCS files on a main memory interface (deprecated function). * * @arg mcs the main memory interface. * @arg list the listing callback. @@ -31,10 +52,24 @@ int CASIO_EXPORT casio_list_mcsfiles(casio_mcs_t *mcs, casio_mcslist_t *list, void *cookie) { - casio_mcs_list_t *func; + casio_iter_t *iter; + casio_mcshead_t *head; + int err; - if (!mcs) return (casio_error_invalid); - func = mcs->casio_mcs_funcs.casio_mcsfuncs_list; - if (!func) return (casio_error_op); - return ((*func)(mcs->casio_mcs_cookie, list, cookie)); + if ((err = casio_iter_mcsfiles(mcs, &iter))) + return (err); + + while (1) { + if ((err = casio_next_mcshead(iter, &head))) { + casio_end(iter); + + if (err == casio_error_stopped) + break; + return (err); + } + + (*list)(cookie, head); + } + + return (0); } diff --git a/lib/mcs/local/find.c b/lib/mcs/local/find.c index 6727f69..ad17d79 100644 --- a/lib/mcs/local/find.c +++ b/lib/mcs/local/find.c @@ -75,8 +75,7 @@ int CASIO_EXPORT casio_localmcs_find(localmcs_t *cookie, memcpy(newindex, cookie->localmcs_files, cookie->localmcs_size * sizeof(casio_mcsfile_t*)); - /* Initialize the new entries. - * (XXX: this shouldn't be necessary, should we remove it?) */ + /* Initialize the new entries. */ memset(&newindex[cookie->localmcs_size], 0, (newsize - cookie->localmcs_size) * sizeof(casio_mcsfile_t*)); diff --git a/lib/mcs/local/iter.c b/lib/mcs/local/iter.c new file mode 100644 index 0000000..5a55d8d --- /dev/null +++ b/lib/mcs/local/iter.c @@ -0,0 +1,88 @@ +/* **************************************************************************** + * mcs/local/iter.c -- iterate on heads in a local main memory. + * Copyright (C) 2017 Thomas "Cakeisalie5" Touhey + * + * This file is part of libcasio. + * libcasio is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 3.0 of the License, + * or (at your option) any later version. + * + * libcasio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with libcasio; if not, see . + * ************************************************************************* */ +#include "local.h" + +typedef struct localmcs_iter_s { + localmcs_t *mcs; + int id; +} localmcs_iter_t; + +/** + * next_mcsfile: + * Get the next element. + * + * @arg cookie the iteration cookie. + * @arg nextp the next element to get. + * @return the error code (0 if ok). + */ + +CASIO_LOCAL int CASIO_EXPORT next_mcsfile(localmcs_iter_t *cookie, + void **nextp) +{ + casio_mcsfile_t *file; + + if (cookie->id >= cookie->mcs->localmcs_count) + return (casio_error_iter); + + file = cookie->mcs->localmcs_files[cookie->id++]; + *nextp = &file->casio_mcsfile_head; + return (0); +} + +/** + * end_iter: + * End the iterator. + * + * @arg cookie the iteration cookie. + * @return the error code (0 if ok). + */ + +CASIO_LOCAL void CASIO_EXPORT end_iter(localmcs_iter_t *cookie) +{ + free(cookie); +} + +/** + * casio_localmcs_iter: + * Iterate on files in a local main memory. + * + * @arg cookie the local main memory cookie. + * @arg iterp the iterator to make. + * @return the error code (0 if ok). + */ + +CASIO_LOCAL casio_iter_funcs_t const casio_localmcs_iter_funcs = { + (casio_next_t *)next_mcsfile, + NULL, + (casio_end_t *)end_iter +}; + +int CASIO_EXPORT casio_localmcs_iter(localmcs_t *cookie, casio_iter_t **iterp) +{ + localmcs_iter_t *icookie; + + icookie = casio_alloc(1, sizeof(*icookie)); + if (!cookie) + return (casio_error_alloc); + + icookie->mcs = cookie; + icookie->id = 0; + + return (casio_iter(iterp, icookie, &casio_localmcs_iter_funcs)); +} diff --git a/lib/mcs/local/local.h b/lib/mcs/local/local.h index 9aa52dc..1071ad0 100644 --- a/lib/mcs/local/local.h +++ b/lib/mcs/local/local.h @@ -46,8 +46,7 @@ CASIO_EXTERN int CASIO_EXPORT casio_localmcs_put CASIO_EXTERN int CASIO_EXPORT casio_localmcs_delete OF((localmcs_t *casio__cookie, casio_mcshead_t *casio__mcshead)); -CASIO_EXTERN int CASIO_EXPORT casio_localmcs_list - OF((localmcs_t *casio__cookie, casio_mcslist_t *casio__mcslist, - void *casio__mcslist_cookie)); +CASIO_EXTERN int CASIO_EXPORT casio_localmcs_iter + OF((localmcs_t *cookie, casio_iter_t **iterp)); #endif /* LOCAL_MCS_LOCAL_H */ diff --git a/lib/mcs/local/open.c b/lib/mcs/local/open.c index 9e1b3b2..07acfb1 100644 --- a/lib/mcs/local/open.c +++ b/lib/mcs/local/open.c @@ -18,9 +18,10 @@ * ************************************************************************* */ #include "local.h" -/* ************************************************************************* */ -/* Close callback, not Close Kombat */ -/* ************************************************************************* */ +/* --- + * Close callback, not Close Kombat + * --- */ + /** * casio_localmcs_close: * Close a local main memory. @@ -45,15 +46,18 @@ CASIO_LOCAL int casio_localmcs_close(localmcs_t *cookie) } /* Callbacks */ + CASIO_LOCAL casio_mcsfuncs_t funcs = { (casio_mcs_get_t*)&casio_localmcs_get, (casio_mcs_put_t*)&casio_localmcs_put, (casio_mcs_delete_t*)&casio_localmcs_delete, - (casio_mcs_list_t*)&casio_localmcs_list, + (casio_mcs_iter_t*)&casio_localmcs_iter, (casio_mcs_close_t*)&casio_localmcs_close}; -/* ************************************************************************* */ -/* Main opening function */ -/* ************************************************************************* */ + +/* --- + * Main opening function. + * --- */ + /** * casio_open_local_mcs: * Open a local main memory. diff --git a/lib/mcsfile/decode/cas.c b/lib/mcsfile/decode/cas.c index 9cf2999..231c0f7 100644 --- a/lib/mcsfile/decode/cas.c +++ b/lib/mcsfile/decode/cas.c @@ -24,23 +24,27 @@ #define FUNC(NAME) &casio_decode_caspart_##NAME #define HFUNC(NAME) &casio_decode_cashpart_##NAME -/* ************************************************************************* */ -/* Type correspondance list */ -/* ************************************************************************* */ -/* Part parsing function type */ +/* --- + * Type correspondance list. + * --- */ + +/* Part parsing function type. */ + typedef int decode_func (); typedef int cas_decode_function OF((casio_mcsfile_t*, casio_stream_t*)); typedef int cas_heads_decode_function OF((casio_mcshead_t *head, casio_mcshead_t *heads, casio_stream_t *buffer)); -/* Correspondance type */ +/* Correspondance type. */ + struct cas_corresp { unsigned int type; cas_decode_function *decode; cas_heads_decode_function *hdecode; }; -/* All correspondances */ +/* All correspondances. */ + #define TTERM {0, NULL, NULL} CASIO_LOCAL struct cas_corresp cas_types[] = { {casio_mcstype_var, FUNC(var), NULL}, @@ -72,9 +76,11 @@ CASIO_LOCAL decode_func *lookup_cas_decode(casio_mcstype_t type, int heads) /* return the function */ return (heads ? (decode_func*)c->hdecode : (decode_func*)c->decode); } -/* ************************************************************************* */ -/* Head decoding functions */ -/* ************************************************************************* */ + +/* --- + * Head decoding functions. + * --- */ + /** * decode_cas50: * Decode a CASIOLINK Protocol header. @@ -216,9 +222,11 @@ int CASIO_EXPORT casio_decode_casfile_head(casio_mcshead_t *head, /* no error! */ return (0); } -/* ************************************************************************* */ -/* Part decoding functions */ -/* ************************************************************************* */ + +/* --- + * Part decoding functions. + * --- */ + /** * casio_decode_casfile_part: * Decode a CASIOLINK Protocol content part. diff --git a/lib/mcsfile/decode/cas/cell.c b/lib/mcsfile/decode/cas/cell.c index bab5653..0582309 100644 --- a/lib/mcsfile/decode/cas/cell.c +++ b/lib/mcsfile/decode/cas/cell.c @@ -18,9 +18,10 @@ * ************************************************************************* */ #include "../decode.h" -/* ************************************************************************* */ -/* Internal function */ -/* ************************************************************************* */ +/* --- + * Internal function. + * --- */ + /** * decode_cell: * Read a cell. @@ -64,9 +65,11 @@ CASIO_LOCAL int decode_cell(casio_stream_t *buffer, casio_mcscell_t *cell, /* no prob'! */ return (0); } -/* ************************************************************************* */ -/* Cell-reading CAS part decoding functions */ -/* ************************************************************************* */ + +/* --- + * Cell-reading CAS part decoding functions. + * --- */ + /** * casio_decode_caspart_matrix: * Decode a CAS matrix part. diff --git a/lib/mcsfile/decode/decode.h b/lib/mcsfile/decode/decode.h index 956a285..f3e2fce 100644 --- a/lib/mcsfile/decode/decode.h +++ b/lib/mcsfile/decode/decode.h @@ -20,10 +20,12 @@ # define LOCAL_MCSFILE_DECODE_H 1 # include "../mcsfile.h" -/* ************************************************************************* */ -/* MCS files specific decoding functions */ -/* ************************************************************************* */ +/* --- + * MCS files specific decoding functions. + * --- */ + /* Schemes. */ + # define CASIO_MCSFUNC(CASIO__NAME) \ extern int casio_decode_mcs_##CASIO__NAME \ OF((casio_mcsfile_t **casio__handle, casio_stream_t *casio__buffer, \ @@ -37,10 +39,13 @@ CASIO_MCSFUNC(program) CASIO_MCSFUNC(setup) CASIO_MCSFUNC(spreadsheet) CASIO_MCSFUNC(string) -/* ************************************************************************* */ -/* CAS-specific decoding functions */ -/* ************************************************************************* */ + +/* --- + * CAS-specific decoding functions. + * --- */ + /* Schemes. */ + # define CASIO_CASFUNC(CASIO__NAME) \ extern int casio_decode_caspart_##CASIO__NAME \ OF((casio_mcsfile_t *casio__handle, casio_stream_t *casio__buffer)); diff --git a/lib/mcsfile/decode/mcs.c b/lib/mcsfile/decode/mcs.c index 5be2d01..7996a83 100644 --- a/lib/mcsfile/decode/mcs.c +++ b/lib/mcsfile/decode/mcs.c @@ -19,20 +19,24 @@ #include "decode.h" #define FUNC(NAME) casio_decode_mcs_##NAME -/* ************************************************************************* */ -/* Type correspondance list */ -/* ************************************************************************* */ -/* MCS file parsing function type */ +/* --- + * Type correspondance list. + * --- */ + +/* MCS file parsing function type. */ + typedef int mcs_decode_func_t OF((casio_mcsfile_t**, casio_stream_t*, casio_mcshead_t*)); /* Correspondance type */ + struct mcs_corresp { casio_mcstype_t type; mcs_decode_func_t *decode; }; /* All correspondances */ + #define TTERM {0, NULL} CASIO_LOCAL struct mcs_corresp mcs_types[] = { {casio_mcstype_program, FUNC(program)}, @@ -67,9 +71,11 @@ CASIO_LOCAL mcs_decode_func_t *lookup_mcsfile_decode(casio_mcstype_t type) /* return the function */ return (c->decode); } -/* ************************************************************************* */ -/* Head decoding function */ -/* ************************************************************************* */ + +/* --- + * Head decoding function. + * --- */ + /** * casio_decode_mcsfile_head: * Decode MCS file head. @@ -114,9 +120,11 @@ int CASIO_EXPORT casio_decode_mcsfile_head(casio_mcshead_t *head, /* everything went well! */ return (0); } -/* ************************************************************************* */ -/* File decoding functions */ -/* ************************************************************************* */ + +/* --- + * File decoding functions. + * --- */ + /** * casio_decode_mcsfile: * Decode MCS file content. diff --git a/lib/mcsfile/decode/mcs/ssheet.c b/lib/mcsfile/decode/mcs/ssheet.c index f17eb5a..f64ba40 100644 --- a/lib/mcsfile/decode/mcs/ssheet.c +++ b/lib/mcsfile/decode/mcs/ssheet.c @@ -40,23 +40,25 @@ int CASIO_EXPORT casio_decode_mcs_spreadsheet(casio_mcsfile_t **h, unsigned long rows = 0, cols = 0; unsigned long x, y; - /* read header */ + /* Read the header. */ + GDREAD(hd) if (hd.casio_mcs_spreadsheet_header_has_subheader != 0x01) return (0); - /* read subheader */ + /* Read the subheader. */ + GDREAD(shd) colcount = hd.casio_mcs_spreadsheet_header_column_count; colcount = be32toh(colcount << 8); shd.casio_mcs_spreadsheet_subheader_defs_size = be32toh(shd.casio_mcs_spreadsheet_subheader_defs_size); - /* prepare */ + /* Prepare. */ + cells = casio_alloc(1000 * colcount, sizeof(casio_mcscell_t)); memset(cells, 0, sizeof(casio_mcscell_t) * 1000 * colcount); - /* log some info */ msg((ll_info, "%lu columns to read!", colcount)); if (colcount) { @@ -107,10 +109,13 @@ int CASIO_EXPORT casio_decode_mcs_spreadsheet(casio_mcsfile_t **h, } } - /* we have max rows and columns, increment to have sizes */ - rows++, cols++; + /* We have max rows and columns, increment to have sizes. */ + + rows++; + cols++; + + /* Create final tab. */ - /* create final tab */ head->casio_mcshead_width = 0; head->casio_mcshead_height = 0; if (cells_count) { head->casio_mcshead_width = cols; @@ -120,13 +125,13 @@ int CASIO_EXPORT casio_decode_mcs_spreadsheet(casio_mcsfile_t **h, if (err) return (err); handle = *h; - /* main copying loop */ + /* Main copying loop. */ + tab = handle->casio_mcsfile_cells; for (y = 0; y < head->casio_mcshead_height; y++) for (x = 0; x < head->casio_mcshead_width; x++) tab[y][x] = cells[x * 1000 + y]; - /* end */ err = 0; fail: casio_free(cells); diff --git a/lib/mcsfile/decode/mcs/string.c b/lib/mcsfile/decode/mcs/string.c index 9c23722..af950fb 100644 --- a/lib/mcsfile/decode/mcs/string.c +++ b/lib/mcsfile/decode/mcs/string.c @@ -34,18 +34,25 @@ int CASIO_EXPORT casio_decode_mcs_string(casio_mcsfile_t **h, int err; unsigned char *str = NULL; unsigned long length = head->casio_mcshead_size; - /* print content */ + /* Print the content. */ + msg((ll_info, "String MCS file is not managed yet. Content:")); str = casio_alloc(length, 1); - if (!str) { err = casio_error_alloc; goto fail; } + if (!str) { + err = casio_error_alloc; + goto fail; + } + GREAD(str, length) mem((ll_info, str, length)); - /* make the file */ + /* XXX: store it some day? */ + + /* Make the file. */ + err = casio_make_mcsfile(h, head); if (err) goto fail; - /* end */ err = 0; fail: casio_free(str); diff --git a/lib/mcsfile/decode/mcs/var.c b/lib/mcsfile/decode/mcs/var.c index 4e94c33..cb01c74 100644 --- a/lib/mcsfile/decode/mcs/var.c +++ b/lib/mcsfile/decode/mcs/var.c @@ -17,6 +17,7 @@ * along with libcasio; if not, see . * ************************************************************************* */ #include "../decode.h" +#define MAX_VAR_NUMBER 30 /* if more than 30 one day, augment this… */ /** * casio_decode_mcs_var: @@ -31,32 +32,45 @@ int CASIO_EXPORT casio_decode_mcs_var(casio_mcsfile_t **handle, casio_stream_t *buffer, casio_mcshead_t *head) { - int err; + int err, num; unsigned long length = head->casio_mcshead_size; - unsigned char *buf = alloca(length); + unsigned char buf[MAX_VAR_NUMBER * sizeof(casio_mcsbcd_t)]; const casio_mcsbcd_t *b; casio_mcsfile_t *h; int i; - /* read the data */ + /* Get the number of elements. */ + + num = length / (2 * sizeof(casio_mcsbcd_t)); + if (num > MAX_VAR_NUMBER) + num = MAX_VAR_NUMBER; + length = num * 2 * sizeof(casio_mcsbcd_t); + + /* Read the data. */ + READ(buf, length) - /* complete header */ - head->casio_mcshead_count = length / (2 * sizeof(casio_mcsbcd_t)); + /* Complete header */ + + head->casio_mcshead_count = num; err = casio_make_mcsfile(handle, head); - if (err) return (err); + if (err) + return (err); h = *handle; - /* check the count */ - ifmsg(head->casio_mcshead_count, (ll_info, "Is a single variable!")); + /* Check the count. */ + + ifmsg(head->casio_mcshead_count == 1, (ll_info, "Is a single variable!")); + + /* Copy and decode the variables. */ - /* copy */ for (b = (void*)buf, i = 0; i < head->casio_mcshead_count; i++) { casio_bcd_frommcs(&h->casio_mcsfile_vars[i].casio_mcscell_real, b++); casio_bcd_frommcs(&h->casio_mcsfile_vars[i].casio_mcscell_imgn, b++); h->casio_mcsfile_vars[i].casio_mcscell_flags = casio_mcscellflag_used; } - /* no problem, woop woop */ + /* No problem, woop woop. */ + return (0); } diff --git a/lib/mcsfile/mcsfile.h b/lib/mcsfile/mcsfile.h index a387d55..336751f 100644 --- a/lib/mcsfile/mcsfile.h +++ b/lib/mcsfile/mcsfile.h @@ -20,10 +20,12 @@ # define LOCAL_MCSFILE_H 1 # include "../internals.h" -/* ************************************************************************* */ -/* Macros for interacting with the buffer */ -/* ************************************************************************* */ +/* --- + * Macros for interacting with the buffer. + * --- */ + /* Read from a stream. */ + # define READ(CASIO__TO, CASIO__SZ) /* normal read */ { \ int READ_err = casio_read(buffer, (CASIO__TO), (CASIO__SZ)); \ if (READ_err) return (READ_err); } @@ -34,12 +36,14 @@ goto fail; /* Read using size of the object. */ + # define DREAD(CASIO__TO) \ READ(&CASIO__TO, sizeof(CASIO__TO)) # define GDREAD(CASIO__TO) \ GREAD(&CASIO__TO, sizeof(CASIO__TO)) /* Skip. */ + # define SKIP(CASIO__SZ) { \ int SKIP_err = casio_skip(buffer, CASIO__SZ); \ if (SKIP_err) return (SKIP_err); } @@ -48,14 +52,17 @@ if (err) goto fail; } /* Write. */ + # define WRITE(CASIO__BUF, CASIO__SZ) { \ int WRITE_err = casio_write(buffer, (CASIO__BUF), (CASIO__SZ)); \ if (WRITE_err) return (WRITE_err); } # define DWRITE(CASIO__OBJECT) \ WRITE(&(CASIO__OBJECT), sizeof(CASIO__OBJECT)) -/* ************************************************************************* */ -/* Picture utilities */ -/* ************************************************************************* */ + +/* --- + * Picture utilities. + * --- */ + # define alloc_pixels(W, H) \ casio_alloc(sizeof(casio_pixel_t*) \ * (H) + sizeof(casio_pixel_t) * (W) * (H), 1) diff --git a/lib/mcsfile/ref/cas_app.c b/lib/mcsfile/ref/cas_app.c index 5fb1300..c823611 100644 --- a/lib/mcsfile/ref/cas_app.c +++ b/lib/mcsfile/ref/cas_app.c @@ -18,10 +18,12 @@ * ************************************************************************* */ #include "ref.h" -/* ************************************************************************* */ -/* Local types */ -/* ************************************************************************* */ +/* --- + * Local types. + * --- */ + /* Correspondance type */ + struct app_corresp { /* identification */ const char *name; @@ -30,6 +32,7 @@ struct app_corresp { }; /* Extension */ + struct ext_corresp { /* identification */ int value; @@ -40,9 +43,11 @@ struct ext_corresp { /* app correspondances */ const struct app_corresp *apps; }; -/* ************************************************************************* */ -/* Correspondances */ -/* ************************************************************************* */ + +/* --- + * Correspondances. + * --- */ + /* App correspondances. Remarks: * - Correspondances with a NULL data type means the data type isn't to be * read. */ @@ -82,9 +87,11 @@ CASIO_LOCAL const struct ext_corresp apps[] = { /* (sentinel) */ {0, 0, NULL} }; -/* ************************************************************************* */ -/* Main functions */ -/* ************************************************************************* */ + +/* --- + * Main functions. + * --- */ + /** * casio_check_cas_app: * Get the CAS application from raw CASDYN identification data. diff --git a/lib/mcsfile/ref/cas_data.c b/lib/mcsfile/ref/cas_data.c index 6f47677..a826f68 100644 --- a/lib/mcsfile/ref/cas_data.c +++ b/lib/mcsfile/ref/cas_data.c @@ -18,16 +18,19 @@ * ************************************************************************* */ #include "ref.h" -/* ************************************************************************* */ -/* Local types */ -/* ************************************************************************* */ -/* Flags */ +/* --- + * Local types. + * --- */ + +/* Flags. */ + #define mult 0x0001 #define noarg 0x0000 #define arg 0x0002 #define arg_is_num 0x0000 /* Correspondance type */ + struct type_corresp { /* identification */ const char *datatype; @@ -37,9 +40,11 @@ struct type_corresp { casio_mcstype_t type; casio_pictureformat_t picformat; }; -/* ************************************************************************* */ -/* Correspondances */ -/* ************************************************************************* */ + +/* --- + * Correspondances. + * --- */ + /* All correspondances found by Tom Wheeley, Tom Lynn (creators of CaS), * and Göran Weinholt (creator of Cafix). */ @@ -47,6 +52,7 @@ struct type_corresp { #define CAPT(S, PT) {S, noarg, casio_mcstype_capture, PT} #define UNIMPLEMENTED(S) {S, noarg, 0, 0} #define TTERM {NULL, noarg, 0, 0} + CASIO_LOCAL const struct type_corresp cas_groups[] = { /* basic things */ BASIC("LT", casio_mcstype_list), @@ -101,9 +107,10 @@ CASIO_LOCAL const struct type_corresp cas_groups[] = { TTERM }; -/* ************************************************************************* */ -/* Main functions */ -/* ************************************************************************* */ +/* --- + * Main functions. + * --- */ + /** * get_number: * Get number from string. diff --git a/lib/mcsfile/ref/mcs.c b/lib/mcsfile/ref/mcs.c index ebfb24b..8b58f74 100644 --- a/lib/mcsfile/ref/mcs.c +++ b/lib/mcsfile/ref/mcs.c @@ -18,10 +18,12 @@ * ************************************************************************* */ #include "ref.h" -/* ************************************************************************* */ -/* Local types */ -/* ************************************************************************* */ -/* Type flags */ +/* --- + * Local types. + * --- */ + +/* Type flags. */ + #define noarg 0x0000 #define arg 0x0001 #define arg_is_num 0x0002 @@ -29,7 +31,8 @@ #define weight_by_gid 0x0004 #define groupable 0x0008 -/* Correspondance type */ +/* Correspondance type. */ + struct type_corresp { /* identification */ unsigned int rawtype; @@ -44,7 +47,8 @@ struct type_corresp { casio_mcstype_t type; }; -/* Group correspondance type */ +/* Group correspondance type. */ + struct group_corresp { /* identification */ const char *name; /* NULL = don't check */ @@ -53,9 +57,11 @@ struct group_corresp { /* types */ const struct type_corresp *types; }; -/* ************************************************************************* */ -/* Correspondances */ -/* ************************************************************************* */ + +/* --- + * Correspondances. + * --- */ + /* All correspondances. Some remarks: * - I think files with "PROGRAM" group are split because it was originally * planned they would be in groups like 'PROGRAM ', like for captures @@ -263,9 +269,11 @@ CASIO_LOCAL const struct group_corresp mcs_groups[] = { /* terminating entry */ {NULL, 0, NULL} }; -/* ************************************************************************* */ -/* Main functions */ -/* ************************************************************************* */ + +/* --- + * Main functions. + * --- */ + /** * get_number: * Get number from string. @@ -451,9 +459,11 @@ found: /* no error */ return (0); } -/* ************************************************************************* */ -/* Compare function */ -/* ************************************************************************* */ + +/* --- + * Compare functions. + * --- */ + /** * find_offset_in_group: * Find offset in a group. @@ -498,18 +508,25 @@ CASIO_LOCAL int find_offset_in_group(const struct group_corresp *g, int CASIO_EXPORT casio_compare_mcsfiles(casio_mcshead_t *first, casio_mcshead_t *second) { - /* find the group correspondance */ int offset1 = -1, offset2 = -1; - const struct group_corresp *g; for (g = mcs_groups; g->types; g++) { + const struct group_corresp *g; + + for (g = mcs_groups; g->types; g++) { /* get offsets */ - if (offset1 >= -1) offset1 = find_offset_in_group(g, first); - if (offset2 >= -1) offset2 = find_offset_in_group(g, second); + if (offset1 >= -1) + offset1 = find_offset_in_group(g, first); + if (offset2 >= -1) + offset2 = find_offset_in_group(g, second); /* check if the first one corresponds */ - if (offset1 < 0 && offset2 < 0) continue; - if (offset1 == offset2) break; - if (offset1 < offset2) return (-1); - if (offset1 > offset2) return (1); + if (offset1 < 0 && offset2 < 0) + continue; + if (offset1 == offset2) + break; + if (offset1 < offset2) + return (-1); + if (offset1 > offset2) + return (1); } /* so they're equal, huh... */ diff --git a/lib/picture/decode.c b/lib/picture/decode.c index 095d52f..92fa9dd 100644 --- a/lib/picture/decode.c +++ b/lib/picture/decode.c @@ -30,28 +30,27 @@ CASIO_LOCAL casio_uint32_t dual2b_colors[] = { /* prizm colors. */ CASIO_LOCAL const casio_uint32_t prizm_colors[16] = { - /* [casio_color_black] = */ casio_pixel(0, 0, 0), - /* [casio_color_blue] = */ casio_pixel(0, 0, 255), - /* [casio_color_green] = */ casio_pixel(0, 255, 0), - /* [casio_color_cyan] = */ casio_pixel(0, 255, 255), - /* [casio_color_red] = */ 0xff0000, - /* [casio_color_magenta] = */ 0xff00ff, - /* [casio_color_yellow] = */ 0xffff00, - /* [casio_color_white] = */ 0xffffff, + /* [casio_color_black] = */ casio_pixel( 0, 0, 0), + /* [casio_color_blue] = */ casio_pixel( 0, 0, 255), + /* [casio_color_green] = */ casio_pixel( 0, 255, 0), + /* [casio_color_cyan] = */ casio_pixel( 0, 255, 255), + /* [casio_color_red] = */ casio_pixel(255, 0, 0), + /* [casio_color_magenta] = */ casio_pixel(255, 0, 255), + /* [casio_color_yellow] = */ casio_pixel(255, 255, 0), + /* [casio_color_white] = */ casio_pixel(255, 255, 255) /* RESERVED */ }; /* Colors used in Casemul pictures. */ CASIO_LOCAL const casio_uint32_t casemul_colors[256] = { - 0xFFFFFF, /* white */ - 0xFF8000, /* orange */ - 0x00FF00, /* green */ - 0x0000FF /* blue */ - /* other colours are black, i.e. 0x00000000 */ + /* white */ casio_pixel(255, 255, 255), + /* orange */ casio_pixel(255, 128, 0), + /* green */ casio_pixel( 0, 255, 0), + /* blue */ casio_pixel( 0, 0, 255) + + /* other colours are black, i.e. casio_pixel(0, 0, 0) == 0x000000 */ }; -/* ************************************************************************* */ -/* Picture decoding */ -/* ************************************************************************* */ + /** * casio_decode_picture: * Decode a picture. @@ -221,11 +220,14 @@ int CASIO_EXPORT casio_decode_picture(casio_pixel_t **pixels, for (y = height - 1; y != (unsigned int)-1; y--) { msk = 0x80; for (x = bx; x < bx + 8; x++) { - /* get pixel */ - if (*o & msk) pixels[y][x] = 0xFF8C00; /* orange */ - else if (*g & msk) pixels[y][x] = 0x00FF00; /* green */ - else if (*b & msk) pixels[y][x] = 0x0000FF; /* blue */ - else pixels[y][x] = 0xFFFFFF; /* white */ + if (*o & msk) /* Orange! */ + casio_set_pixel(pixels[y][x], 255, 140, 0); + else if (*g & msk) /* Green! */ + casio_set_pixel(pixels[y][x], 0, 255, 0); + else if (*b & msk) /* Blue! */ + casio_set_pixel(pixels[y][x], 0, 0, 255); + else /* White! */ + casio_set_pixel(pixels[y][x], 255, 255, 255); /* go to next */ msk >>= 1; diff --git a/lib/stream/attrs.c b/lib/stream/attrs.c index 38d37b5..5659f10 100644 --- a/lib/stream/attrs.c +++ b/lib/stream/attrs.c @@ -18,9 +18,10 @@ * ************************************************************************* */ #include "stream.h" -/* ************************************************************************* */ -/* Manage attributes */ -/* ************************************************************************* */ +/* --- + * Manage attributes. + * --- */ + /** * casio_make_attrs: * Make attributes from a string. @@ -77,9 +78,11 @@ int CASIO_EXPORT casio_make_attrs(casio_streamattrs_t *attrs, const char *raw) : CASIO_PARENB | CASIO_PARODD; return (0); } -/* ************************************************************************* */ -/* Set the attributes of a stream */ -/* ************************************************************************* */ + +/* --- + * Set the attributes of a stream. + * --- */ + /** * casio_init_attrs: * Initialize the attributes. diff --git a/lib/stream/builtin/file.c b/lib/stream/builtin/file.c index 2b8ab90..50c9188 100644 --- a/lib/stream/builtin/file.c +++ b/lib/stream/builtin/file.c @@ -28,15 +28,17 @@ # define __fwritable(F) (1) # endif -/* cookie structure */ +/* Cookie structure. */ + typedef struct { int _rstream_cl, _wstream_cl; FILE *_rstream, *_wstream; } file_cookie_t; -/* ************************************************************************* */ -/* Callbacks */ -/* ************************************************************************* */ +/* --- + * Callbacks. + * --- */ + /** * casio_file_read: * Read from a FILE. @@ -214,9 +216,11 @@ CASIO_LOCAL int casio_file_close(file_cookie_t *cookie) casio_free(cookie); return (0); } -/* ************************************************************************* */ -/* Opening functions */ -/* ************************************************************************* */ + +/* --- + * Opening functions. + * --- */ + CASIO_LOCAL const casio_streamfuncs_t casio_file_callbacks = casio_stream_callbacks_for_virtual(casio_file_close, casio_file_read, casio_file_write, casio_file_seek); diff --git a/lib/stream/builtin/libusb/scsi.c b/lib/stream/builtin/libusb/scsi.c index 295c6a4..bf7a53d 100644 --- a/lib/stream/builtin/libusb/scsi.c +++ b/lib/stream/builtin/libusb/scsi.c @@ -29,7 +29,7 @@ * [0x55, 0x53, 0x42, 0x43] (USBC) * - Tag (4B): answerer will echo this field to us in the associated CSW. * Used by BrandonW: [0x41, 0x42, 0x43, 0x44] (ABCD) - * - Data Transfer Length (4B): — + * - Data Transfer Length (4B): what is to come as data. * - Flags (1B): * - Bit 7 (128): direction, 0 for host to device, 1 for device to host. * - Other bits: reserved or obsolete, set to zero (0). @@ -44,12 +44,28 @@ * [0x55, 0x53, 0x42, 0x53] (USBS) * - Tag (4B): echo the field from the CBW. * Used by BrandonW: [0x41, 0x42, 0x43, 0x44] (ABCD) - * - Data Residue (4B): + * - Data Residue (4B): — * - Status (1B): success or failure. * - 0: command passed (good). * - 1: command failed (bad). * - 2: phase error. */ +struct cbw { + casio_uint8_t signature[4]; + casio_uint8_t tag[4]; + casio_uint8_t data_length[4]; + casio_uint8_t flags; + casio_uint8_t lun; + casio_uint8_t cmd_len; +}; + +struct csw { + casio_uint8_t signature[4]; + casio_uint8_t tag[4]; + casio_uint8_t residue[4]; + casio_uint8_t status; +}; + /** * casio_libusb_scsi_request: * Make an SCSI request on a libusb connected device, using CBW. @@ -58,10 +74,164 @@ int CASIO_EXPORT casio_libusb_scsi_request(cookie_libusb_t *cookie, casio_scsi_t *request) { + unsigned char cmd[32]; + struct cbw *cbw; + struct csw csw; + int sent, recv, libusberr; + /* Prepare and send the request. */ - /* TODO */ - return (casio_error_op); + memset(cmd, 0, 32); + cbw = (void *)&cmd[0]; + cbw->signature[0] = 'U'; + cbw->signature[1] = 'S'; + cbw->signature[2] = 'B'; + cbw->signature[3] = 'C'; + cbw->tag[0] = 'A'; + cbw->tag[1] = 'B'; + cbw->tag[2] = 'C'; + cbw->tag[3] = 'D'; + cbw->data_length[0] = request->casio_scsi_data_len & 0xFF; + cbw->data_length[1] = (request->casio_scsi_data_len >> 8) & 0xFF; + cbw->data_length[2] = 0; + cbw->data_length[3] = 0; + cbw->flags = request->casio_scsi_data_len && request->casio_scsi_direction + == CASIO_SCSI_DIREC_FROM_DEV ? 128 : 0; + cbw->lun = 0; + cbw->cmd_len = request->casio_scsi_cmd_len; + memcpy(&cmd[15], request->casio_scsi_cmd, request->casio_scsi_cmd_len); + + libusberr = libusb_bulk_transfer(cookie->_handle, ENDPOINT_OUT, + cmd, 31, &sent, cookie->tmwrite); + switch (libusberr) { + case 0: break; + + case LIBUSB_ERROR_PIPE: + case LIBUSB_ERROR_NO_DEVICE: + case LIBUSB_ERROR_IO: + msg((ll_error, "The calculator is not here anymore :(")); + return (casio_error_nocalc); + + default: + msg((ll_fatal, "libusb error was %d: %s", libusberr, + libusb_strerror(libusberr))); + return (casio_error_unknown); + } + + /* Send or receive the data. */ + + if (!request->casio_scsi_data_len) { + /* Nothing, no data is sent. */ + } else if (request->casio_scsi_direction == CASIO_SCSI_DIREC_FROM_DEV) { + int left = (int)request->casio_scsi_data_len; + unsigned char *data = (unsigned char *)request->casio_scsi_data; + + /* Data is received. */ + + while (left) { + libusberr = libusb_bulk_transfer(cookie->_handle, ENDPOINT_IN, + data, (int)left, &recv, cookie->tmread); + switch (libusberr) { + case 0: + break; + + case LIBUSB_ERROR_PIPE: + case LIBUSB_ERROR_NO_DEVICE: + case LIBUSB_ERROR_IO: + msg((ll_error, "The calculator is not here anymore :(")); + return (casio_error_nocalc); + + case LIBUSB_ERROR_TIMEOUT: + return (casio_error_timeout); + + default: + msg((ll_fatal, "libusb error was %d: %s", libusberr, + libusb_strerror(libusberr))); + return (casio_error_unknown); + } + + data += recv; + left -= recv; + } + } else { + int left = (int)request->casio_scsi_data_len; + unsigned char *data = (unsigned char *)request->casio_scsi_data; + + msg((ll_info, "Sending %d bytes.", left)); + + /* Data is sent. */ + + while (left) { + libusberr = libusb_bulk_transfer(cookie->_handle, ENDPOINT_OUT, + data, (int)left, &sent, cookie->tmwrite); + switch (libusberr) { + case 0: + break; + + case LIBUSB_ERROR_PIPE: + case LIBUSB_ERROR_NO_DEVICE: + case LIBUSB_ERROR_IO: + msg((ll_error, "The calculator is not here anymore :(")); + return (casio_error_nocalc); + + case LIBUSB_ERROR_TIMEOUT: + return (casio_error_timeout); + + default: + msg((ll_fatal, "libusb error was %d: %s", libusberr, + libusb_strerror(libusberr))); + return (casio_error_unknown); + } + + data += sent; + left -= sent; + } + } + + /* Get the status. */ + + { + int left = 13; + unsigned char *data = (void *)&csw; + + do { + libusberr = libusb_bulk_transfer(cookie->_handle, ENDPOINT_IN, + data, (int)left, &recv, cookie->tmread); + switch (libusberr) { + case 0: + break; + + case LIBUSB_ERROR_PIPE: + case LIBUSB_ERROR_NO_DEVICE: + case LIBUSB_ERROR_IO: + msg((ll_error, "The calculator is not here anymore :(")); + return (casio_error_nocalc); + + case LIBUSB_ERROR_TIMEOUT: + return (casio_error_timeout); + + default: + msg((ll_fatal, "libusb error was %d: %s", libusberr, + libusb_strerror(libusberr))); + return (casio_error_unknown); + } + + data += recv; + left -= recv; + } while (left); + } + + /* Check the status block itself. */ + + if (memcmp(csw.signature, "USBS", 4) + || memcmp(csw.tag, cbw->tag, 4)) + return (casio_error_unknown); + /* TODO: check if csw->status is good and find the error to return + * if not */ + + /* We're good! */ + + return (0); } #endif diff --git a/lib/stream/builtin/limited.c b/lib/stream/builtin/limited.c index 4d37628..6b847bf 100644 --- a/lib/stream/builtin/limited.c +++ b/lib/stream/builtin/limited.c @@ -26,9 +26,10 @@ typedef struct { size_t _left; } limited_cookie_t; -/* ************************************************************************* */ -/* Callbacks */ -/* ************************************************************************* */ +/* --- + * Callbacks. + * --- */ + /** * casio_limited_read: * Read from a limited stream. @@ -87,9 +88,10 @@ CASIO_LOCAL const casio_streamfuncs_t casio_limited_callbacks = casio_stream_callbacks_for_virtual(casio_limited_close, casio_limited_read, NULL, NULL); -/* ************************************************************************* */ -/* Main functions */ -/* ************************************************************************* */ +/* --- + * Main functions. + * --- */ + /** * casio_open_limited: * Open a limited stream. diff --git a/lib/stream/builtin/memory.c b/lib/stream/builtin/memory.c index 8b32e56..ea4936c 100644 --- a/lib/stream/builtin/memory.c +++ b/lib/stream/builtin/memory.c @@ -24,9 +24,10 @@ typedef struct { casio_off_t _size, _offset; } memory_cookie_t; -/* ************************************************************************* */ -/* Callbacks */ -/* ************************************************************************* */ +/* --- + * Callbacks. + * --- */ + /** * casio_memory_read: * Read from a memory area. @@ -145,9 +146,10 @@ CASIO_LOCAL const casio_streamfuncs_t casio_memory_callbacks = casio_stream_callbacks_for_virtual(casio_memory_close, casio_memory_read, casio_memory_write, casio_memory_seek); -/* ************************************************************************* */ -/* Opening functions */ -/* ************************************************************************* */ +/* --- + * Opening functions. + * --- */ + /** * casio_open_memory: * Open a FILE stream. diff --git a/lib/stream/open.c b/lib/stream/open.c index 59eab8f..db09f2a 100644 --- a/lib/stream/open.c +++ b/lib/stream/open.c @@ -17,8 +17,6 @@ * along with libcasio; if not, see . * ************************************************************************* */ #include "stream.h" -#define checknot(CASIO__COND, CASIO__ERR) \ - { if (CASIO__COND) {err = CASIO__ERR; goto fail;}} /** * casio_open_stream: @@ -40,36 +38,50 @@ int CASIO_EXPORT casio_open_stream(casio_stream_t **pstream, int err; casio_stream_t *stream = NULL; casio_streamfuncs_t *c; - /* allocate the stream */ - *pstream = casio_alloc(1, sizeof(casio_stream_t)); - stream = *pstream; - checknot(stream == NULL, casio_error_alloc) + /* Allocate the stream. */ - /* initialize the stream callbacks */ + *pstream = casio_alloc(1, sizeof(casio_stream_t)); + if (!(stream = *pstream)) { + err = casio_error_alloc; + goto fail; + } + + /* Initialize the stream callbacks. */ + + stream->casio_stream_mode = 0; c = &stream->casio_stream_callbacks; memset(c, 0, sizeof(casio_streamfuncs_t)); c->casio_streamfuncs_close = callbacks->casio_streamfuncs_close; c->casio_streamfuncs_settm = callbacks->casio_streamfuncs_settm; - if (mode & CASIO_OPENMODE_READ) + if ((mode & CASIO_OPENMODE_READ) && callbacks->casio_streamfuncs_read) { + stream->casio_stream_mode |= CASIO_OPENMODE_READ; c->casio_streamfuncs_read = callbacks->casio_streamfuncs_read; - if (mode & CASIO_OPENMODE_WRITE) + } + if ((mode & CASIO_OPENMODE_WRITE) && callbacks->casio_streamfuncs_write) { + stream->casio_stream_mode |= CASIO_OPENMODE_WRITE; c->casio_streamfuncs_write = callbacks->casio_streamfuncs_write; + } if (mode & (CASIO_OPENMODE_READ | CASIO_OPENMODE_WRITE)) c->casio_streamfuncs_seek = callbacks->casio_streamfuncs_seek; - if (mode & CASIO_OPENMODE_SERIAL) - c->casio_streamfuncs_setattrs = callbacks->casio_streamfuncs_setattrs; - if (mode & CASIO_OPENMODE_SCSI) - c->casio_streamfuncs_scsi = callbacks->casio_streamfuncs_scsi; - /* initialize the stream properties */ - stream->casio_stream_mode = mode; + if ((mode & CASIO_OPENMODE_SERIAL) + && callbacks->casio_streamfuncs_setattrs) { + stream->casio_stream_mode |= CASIO_OPENMODE_SERIAL; + c->casio_streamfuncs_setattrs = callbacks->casio_streamfuncs_setattrs; + } + if ((mode & CASIO_OPENMODE_SCSI) && callbacks->casio_streamfuncs_scsi) { + stream->casio_stream_mode |= CASIO_OPENMODE_SCSI; + c->casio_streamfuncs_scsi = callbacks->casio_streamfuncs_scsi; + } + + /* Initialize the stream properties. */ + stream->casio_stream_cookie = cookie; stream->casio_stream_offset = ioff; stream->casio_stream_lasterr = 0; casio_init_attrs(stream); casio_init_timeouts(stream); - /* no error! */ err = 0; fail: if (err) { diff --git a/lib/utils/ascii.c b/lib/utils/ascii.c index c27d6ba..55c55b9 100644 --- a/lib/utils/ascii.c +++ b/lib/utils/ascii.c @@ -19,9 +19,10 @@ #include "../internals.h" #include -/* ************************************************************************* */ -/* ASCII-HEX utilities */ -/* ************************************************************************* */ +/* --- + * ASCII-HEX (base16) utilities. + * --- */ + /** * casio_putascii: * Put a number in ASCII-hex, in a n-dimensionned field. @@ -61,9 +62,11 @@ unsigned long CASIO_EXPORT casio_getascii(const unsigned char *p, int n) } return (i); } -/* ************************************************************************* */ -/* BIN-HEX to BIN-DEC (BCD) utilities */ -/* ************************************************************************* */ + +/* --- + * BIN-HEX to BIN-DEC (BCD) utilities. + * --- */ + /** * casio_getdec: * Get decimal of hex. diff --git a/lib/utils/iter.c b/lib/utils/iter.c new file mode 100644 index 0000000..7054bce --- /dev/null +++ b/lib/utils/iter.c @@ -0,0 +1,130 @@ +/* **************************************************************************** + * utils/iter.c -- iterator internals. + * Copyright (C) 2017 Thomas "Cakeisalie5" Touhey + * + * This file is part of libcasio. + * libcasio is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 3.0 of the License, + * or (at your option) any later version. + * + * libcasio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with libcasio; if not, see . + * ************************************************************************* */ +#include + +/* Internal structure of an iterator. */ + +struct casio_iter_s { + int stopped, has_last; + void *last; + void *cookie; + casio_next_t *next; + casio_nextfree_t *nextfree; + casio_end_t *end; +}; + +/* `default_next()`: default next function. */ + +CASIO_LOCAL int default_next(void *cookie, void **ptr) +{ + (void)cookie; + (void)ptr; + return (casio_error_iter); +} + +/* `default_nextfree()`: default nextfree function. */ + +CASIO_LOCAL void default_nextfree(void *cookie, void *ptr) +{ + /* Do not free. */ + + (void)cookie; + (void)ptr; +} + +/* `default_end()`: default iterator end function. */ + +CASIO_LOCAL void default_end(void *cookie) +{ + (void)cookie; +} + +/* `casio_iter()`: create an iterator. */ + +int CASIO_EXPORT casio_iter(casio_iter_t **iterp, void *cookie, + casio_iter_funcs_t const *funcs) +{ + casio_iter_t *iter; + + /* Allocate the iterator. */ + + iter = casio_alloc(1, sizeof(*iter)); + if (!iter) { + if (funcs->casio_iterfunc_end) + (*funcs->casio_iterfunc_end)(cookie); + return (casio_error_alloc); + } + + /* Prepare the iterator. */ + + iter->stopped = 0; + iter->has_last = 0; + iter->cookie = cookie; + iter->next = funcs->casio_iterfunc_next; + if (!iter->next) + iter->next = &default_next; + iter->nextfree = funcs->casio_iterfunc_nextfree; + if (!iter->nextfree) + iter->nextfree = &default_nextfree; + iter->end = funcs->casio_iterfunc_end; + if (!iter->end) + iter->end = &default_end; + + *iterp = iter; + return (0); +} + +/* `casio_next()`: get the next element from an iterator. */ + +int CASIO_EXPORT casio_next(casio_iter_t *iter, void **ptrp) +{ + int err; + + if (iter->stopped) + return (casio_error_iter); + if (iter->has_last) { + (*iter->nextfree)(iter->cookie, iter->last); + iter->has_last = 0; + } + + err = (*iter->next)(iter->cookie, ptrp); + if (err == casio_error_iter) { + iter->stopped = 1; + return (err); + } else if (err) + return (err); + + iter->has_last = 1; + iter->last = *ptrp; + + return (0); +} + +/* `casio_end()`: end and free the iterator. */ + +void CASIO_EXPORT casio_end(casio_iter_t *iter) +{ + if (iter->has_last) { + (*iter->nextfree)(iter->cookie, iter->last); + iter->has_last = 0; + } + + (*iter->end)(iter->cookie); + casio_free(iter); +} diff --git a/lib/utils/sleep.c b/lib/utils/sleep.c index ccd0d38..bdb6457 100644 --- a/lib/utils/sleep.c +++ b/lib/utils/sleep.c @@ -18,9 +18,10 @@ * ************************************************************************* */ #include "../internals.h" -/* ************************************************************************* */ -/* Microsoft Windows environment */ -/* ************************************************************************* */ +/* --- + * Microsoft Windows environment. + * --- */ + #if defined(__WINDOWS__) # define default_callback &casio_winsleep # include @@ -29,9 +30,11 @@ CASIO_LOCAL void casio_winsleep(unsigned long ms) { Sleep(ms); } -/* ************************************************************************* */ -/* UNIX environments */ -/* ************************************************************************* */ + +/* --- + * UNIX-like environments. + * --- */ + #elif defined(__unix__) || defined(__unix) # define default_callback &casio_unixsleep # include @@ -42,15 +45,18 @@ CASIO_LOCAL void casio_unixsleep(unsigned long ms) struct timespec requested_timestamp; requested_timestamp.tv_sec = ms / 1000; - requested_timestamp.tv_nsec = ms * 1000; + requested_timestamp.tv_nsec = (ms % 1000) * 1000000; nanosleep(&requested_timestamp, NULL); } -/* ************************************************************************* */ -/* Default and main function */ -/* ************************************************************************* */ + +/* --- + * Default and main function. + * --- */ + #else # define default_callback NULL #endif + CASIO_LOCAL casio_sleep_t *casio_sleep_callback = default_callback; /** diff --git a/src/p7os/utils/osdisp.c b/src/p7os/utils/osdisp.c index ee13f7d..940f147 100644 --- a/src/p7os/utils/osdisp.c +++ b/src/p7os/utils/osdisp.c @@ -19,9 +19,10 @@ #include "../main.h" #include -/* ************************************************************************* */ -/* Main function */ -/* ************************************************************************* */ +/* --- + * Main function. + * --- */ + /** * osdisp: * Nice little loading bar. @@ -73,9 +74,11 @@ void osdisp(void *vcookie, unsigned int id, unsigned int total) fputs("\x1B""8", stdout); fflush(stdout); } -/* ************************************************************************* */ -/* Initialization, miscallaneous actions */ -/* ************************************************************************* */ + +/* --- + * Initialization, miscallaneous actions. + * --- */ + /** * osdisp_init: * Initialize a display cookie. diff --git a/src/p7screen/args.c b/src/p7screen/args.c index 2cd3d27..cfc6948 100644 --- a/src/p7screen/args.c +++ b/src/p7screen/args.c @@ -21,10 +21,12 @@ #include #include -/* ************************************************************************* */ -/* Help and version messages */ -/* ************************************************************************* */ +/* --- + * Help and version messages. + * --- */ + /* Version message */ + static const char version_message[] = BIN " - from " NAME " v" VERSION " (licensed under GPLv2)\n" "Maintained by " MAINTAINER ".\n" @@ -34,6 +36,7 @@ BIN " - from " NAME " v" VERSION " (licensed under GPLv2)\n" "FITNESS FOR A PARTICULAR PURPOSE."; /* Help message */ + static const char help_main0[] = "Usage: " BIN " [--help|-h] [--version|-v]\n" "\n" @@ -52,9 +55,11 @@ static const char help_main1[] = " By default, the zoom will be " DEFAULT_ZOOM ".\n" "\n" "Report bugs to " MAINTAINER ".\n"; -/* ************************************************************************* */ -/* Main function */ -/* ************************************************************************* */ + +/* --- + * Main function. + * --- */ + /** * put_loglevel: * Put a loglevel (for listing). diff --git a/tools/write-header-config b/tools/write-header-config index e7500af..4f970a6 100755 --- a/tools/write-header-config +++ b/tools/write-header-config @@ -54,13 +54,17 @@ cat <<_EOF * You should have received a copy of the GNU Lesser General Public License * along with libcasio; if not, see . * - * This file is GENERATED from the options you pass to the configure script. + * This file is *GENERATED* from the options you pass to the configure script. * It shall not be modified by the user after its generation, as this could * lead to unresolved symbols! If you want another configuration, then you * will have to build the library again, with your different configuration. * ************************************************************************* */ #ifndef LIBCASIO_CONFIG_H # define LIBCASIO_CONFIG_H + +/* Version and maintainer related macros. + * Generated from the variables in \`Makefile.vars\`. */ + # define LIBCASIO_VERSION "${version}" # define LIBCASIO_VERNUM ${version_num} # define LIBCASIO_MAJOR ${version_major} @@ -74,7 +78,8 @@ _EOF # File part if [ "$no_file" ]; then cat <<_EOF -/* FILE interface is disabled */ +/* Standard FILE interface is disabled. */ + # define LIBCASIO_DISABLED_FILE 1 _EOF @@ -82,7 +87,8 @@ fi # libusb part if [ "$no_libusb" ]; then cat <<_EOF -/* libusb is disabled */ +/* libusb support is disabled. */ + # define LIBCASIO_DISABLED_LIBUSB 1 _EOF @@ -90,7 +96,8 @@ fi # disable logging if [ "$no_log" ]; then cat <<_EOF -/* logging is disabled */ +/* Logging is disabled. */ + # define LIBCASIO_DISABLED_LOG 1 _EOF