From c0d9590ce07a45cd17aa017809fb73d52ff9c09c Mon Sep 17 00:00:00 2001 From: "Thomas \"Cakeisalie5\" Touhey" Date: Sat, 25 Feb 2017 10:55:27 +0100 Subject: [PATCH] Started integrating libg1m usage, reorganized. --- Makefile.vars | 7 +-- README.md | 1 + include/libp7.h | 19 ++++-- include/libp7/internals.h | 24 ++++---- include/libp7/packetio.h | 1 + include/libp7/types.h | 1 + src/mcs/list.c | 84 ++++++++++++++++++++++++++ src/{protocol => misc}/backup.c | 2 +- src/{protocol => misc}/getscreen.c | 2 +- src/{protocol => misc}/poke.c | 2 +- src/{protocol => misc}/sendexe.c | 2 +- src/{protocol => misc}/server.c | 2 +- src/{protocol => misc}/setlink.c | 2 +- src/{protocol => storage}/copy.c | 2 +- src/{protocol => storage}/createdir.c | 2 +- src/{protocol => storage}/delete.c | 2 +- src/{protocol => storage}/getfreemem.c | 2 +- src/{protocol => storage}/list.c | 4 +- src/{protocol => storage}/optimize.c | 2 +- src/{protocol => storage}/request.c | 2 +- src/{protocol => storage}/reset.c | 2 +- src/{protocol => storage}/send.c | 2 +- 22 files changed, 133 insertions(+), 36 deletions(-) create mode 100644 src/mcs/list.c rename src/{protocol => misc}/backup.c (99%) rename src/{protocol => misc}/getscreen.c (97%) rename src/{protocol => misc}/poke.c (94%) rename src/{protocol => misc}/sendexe.c (98%) rename src/{protocol => misc}/server.c (99%) rename src/{protocol => misc}/setlink.c (97%) rename src/{protocol => storage}/copy.c (97%) rename src/{protocol => storage}/createdir.c (96%) rename src/{protocol => storage}/delete.c (96%) rename src/{protocol => storage}/getfreemem.c (97%) rename src/{protocol => storage}/list.c (97%) rename src/{protocol => storage}/optimize.c (96%) rename src/{protocol => storage}/request.c (98%) rename src/{protocol => storage}/reset.c (96%) rename src/{protocol => storage}/send.c (99%) diff --git a/Makefile.vars b/Makefile.vars index 767d9f8..e5a11fe 100755 --- a/Makefile.vars +++ b/Makefile.vars @@ -28,10 +28,10 @@ VERSION := $(MAJOR).$(MINOR)$(if $(INDEV),-indev) # Runtime libs (later: libg1m and libfontcharacter) - RLIBS := + RLIBS := libg1m # Compilation time libs - LIBS := zlib $(RLIBS) + LIBS := zlib $(if $(USE_LIBUSB),libusb-1.0) $(RLIBS) #******************************************************************************# # Project directories # @@ -62,9 +62,6 @@ ANAME := $(if $(FOR_WINDOWS),lib$(NAME).lib,lib$(NAME).a) ANAMES := lib$(NAME).lib lib$(NAME).a lib$(NAME).dll.a -# Required libs - LIBS := $(if $(USE_LIBUSB),libusb-1.0) - #******************************************************************************# # Binary utilities # #******************************************************************************# diff --git a/README.md b/README.md index ba2ac2a..cb9f51e 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,7 @@ I took these as a reference because these are the ones I work with. | Name | Version | | -------------------------------------------------- | -------- | | [libusb](http://libusb.info/) | >= 1.0 | +| [libg1m](https://github.com/cakeisalie5/libg1m/) | >= 1.0 | ## Configuring First of all, the configure script has a help message where all of the diff --git a/include/libp7.h b/include/libp7.h index f6c5d7c..cbebb57 100644 --- a/include/libp7.h +++ b/include/libp7.h @@ -110,8 +110,16 @@ int p7_comlist(void (*callback)(void*, const char*), void *cookie); /* De-initialization */ void p7_exit(p7_handle_t *handle); + /* ************************************************************************** */ -/* Protocol */ +/* MCS interaction */ +/* ************************************************************************** */ +/* List files */ +int p7_mcs_list(p7_handle_t *handle, + void (*callback)(void*, const g1m_mcshead_t *head), void *cookie); + +/* ************************************************************************** */ +/* Storage memory interaction */ /* ************************************************************************** */ /* Send a file using buffer */ int p7_send(p7_handle_t *handle, const p7_buffer_t *buffer, @@ -164,6 +172,12 @@ int p7_reset(p7_handle_t *handle, const char *devname); /* Optimize distant device */ int p7_optimize(p7_handle_t *handle, const char *devname); +/* ************************************************************************** */ +/* Miscallenous interactions */ +/* ************************************************************************** */ +/* Poke the calculator in order to keep it happy */ +int p7_poke(p7_handle_t *handle); + /* Send an update.exe */ int p7_sendexe(p7_handle_t *handle, const p7_buffer_t *buffer, p7uint_t loadaddr, p7uint_t straddr, void (*disp)(p7ushort_t, p7ushort_t)); @@ -202,9 +216,6 @@ int p7_sendexe_mem(p7_handle_t *handle, /* Display streamed screen */ int p7_getscreen(p7_handle_t *handle, int (*callback)(int, int, uint32_t**)); -/* Poke the calculator in order to keep it happy */ -int p7_poke(p7_handle_t *handle); - # ifdef __cplusplus } # endif diff --git a/include/libp7/internals.h b/include/libp7/internals.h index cc71e98..2f9f4de 100644 --- a/include/libp7/internals.h +++ b/include/libp7/internals.h @@ -23,7 +23,9 @@ #ifndef LIBP7_INTERNALS_H # define LIBP7_INTERNALS_H /* Microsoft Windows minimal version (Vista) */ -# undef _WIN32_WINNT +# ifdef _WIN32_WINNT +# undef _WIN32_WINNT +# endif # define _WIN32_WINNT 0x0600 # include @@ -122,16 +124,16 @@ struct p7_handle_s { p7_server_t _server; const p7_env_t *_env; - /* is a serial connexion */ - int _serial; - /* active status */ - int _active; - /* shift status */ - int _shifted; - /* was terminated */ - int _terminated; - /* should terminate */ - int _term; + /* head of the current MCS file */ + g1m_mcshead_t mcshead; + + /* various flags */ + int _serial; /* is a serial connexion */ + int _active; /* active status */ + int _shifted; /* shift status */ + int _terminated; /* was terminated */ + int _term; /* should terminate at exit */ + /* last sent command */ p7ushort_t _last_sent_command; /* response */ diff --git a/include/libp7/packetio.h b/include/libp7/packetio.h index 0d4dae8..20d7ce2 100644 --- a/include/libp7/packetio.h +++ b/include/libp7/packetio.h @@ -186,6 +186,7 @@ static inline int p7_send_cmd##C(p7_handle_t *handle, \ * Remember that these are only for the ease of use; you can use the macros * to implement more commands without putting them here. */ p7cmd(sys_getinfo, 0x01) +p7cmd(mcs_reqallinfo, 0x2D) p7cmd(bak_reqram, 0x2F) p7cmd(bak_putram, 0x30) p7cmd_dir(fls_mkdir, 0x40) diff --git a/include/libp7/types.h b/include/libp7/types.h index 91978aa..990530b 100644 --- a/include/libp7/types.h +++ b/include/libp7/types.h @@ -19,6 +19,7 @@ #ifndef LIBP7_TYPES_H # define LIBP7_TYPES_H # include +# include # if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) \ && !defined(__WINDOWS__) # define __WINDOWS__ diff --git a/src/mcs/list.c b/src/mcs/list.c new file mode 100644 index 0000000..d0f8abe --- /dev/null +++ b/src/mcs/list.c @@ -0,0 +1,84 @@ +/* ***************************************************************************** + * mcs/list.c -- list the elements on the main memory. + * Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey + * + * This file is part of libp7. + * libp7 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. + * + * libp7 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 libp7; if not, see . + * ************************************************************************** */ +#include + +/** + * p7_mcs_list: + * List files on the main memory. + * + * @arg handle the libp7 handle. + * @arg callback the callback. + * @arg cookie something to send to the callback. + * @return if it worked. + */ + +int p7_mcs_list(p7_handle_t *handle, + void (*callback)(void*, const g1m_mcshead_t *head), void *cookie) +{ + int err; + + /* make checks */ + chk_handle(handle); + chk_active(handle); + + /* send command packet */ + log_info("sending file transfer request"); + if ((err = p7_send_cmdmcs_reqallinfo(handle))) { + log_fatal("couldn't send transfer request"); + return (err); + } else if (response.type != p7_pt_ack) { + log_fatal("didn't receive ack..."); + return (p7_error_unknown); + } + + /* swap roles */ + log_info("sending roleswap"); + if ((err = p7_send_roleswp(handle))) { + log_fatal("couldn't swap roles"); + return (err); + } + + /* - Note: we are now in passive mode - */ + g1m_mcshead_t head; + while (1) switch (response.type) { + /* if answer is roleswap, we are done */ + case p7_pt_roleswp: return (0); + + /* if it is command, should be another file info */ + case p7_pt_cmd: + /* check args */ + if (g1m_decode_mcsfile_head(&head, response.mcstype, + (unsigned char*)response.args[2], (unsigned char*)response.args[0], + (unsigned char*)response.args[1], response.filesize)) + continue; + (*callback)(cookie, &head); + + /* send ack to continue */ + log_info("sending ack to continue"); + if ((err = p7_send_ack(handle, 1))) { + log_fatal("unable to send ack"); + return (err); + } + break; + + default: /* wtf? */ + log_fatal("wtf?!"); + return (p7_error_unknown); + } +} diff --git a/src/protocol/backup.c b/src/misc/backup.c similarity index 99% rename from src/protocol/backup.c rename to src/misc/backup.c index 059ea7e..ea71955 100644 --- a/src/protocol/backup.c +++ b/src/misc/backup.c @@ -1,5 +1,5 @@ /* ***************************************************************************** - * protocol/backup.c -- backup software elements of the calculator. + * misc/backup.c -- backup software elements of the calculator. * Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey * * This file is part of libp7. diff --git a/src/protocol/getscreen.c b/src/misc/getscreen.c similarity index 97% rename from src/protocol/getscreen.c rename to src/misc/getscreen.c index 900a8b4..972ac63 100644 --- a/src/protocol/getscreen.c +++ b/src/misc/getscreen.c @@ -1,5 +1,5 @@ /* ***************************************************************************** - * protocol/getscreen.c -- receive the streamed screen from the calculator. + * misc/getscreen.c -- receive the streamed screen from the calculator. * Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey * * This file is part of libp7. diff --git a/src/protocol/poke.c b/src/misc/poke.c similarity index 94% rename from src/protocol/poke.c rename to src/misc/poke.c index 1b4df4c..c1ff7d1 100644 --- a/src/protocol/poke.c +++ b/src/misc/poke.c @@ -1,5 +1,5 @@ /* ***************************************************************************** - * protocol/poke.c -- poke the calculator so the communication doesn't stop. + * misc/poke.c -- poke the calculator so the communication doesn't stop. * Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey * * This file is part of libp7. diff --git a/src/protocol/sendexe.c b/src/misc/sendexe.c similarity index 98% rename from src/protocol/sendexe.c rename to src/misc/sendexe.c index c908091..a44313e 100644 --- a/src/protocol/sendexe.c +++ b/src/misc/sendexe.c @@ -1,5 +1,5 @@ /* ***************************************************************************** - * protocol/sendexe.c -- send an update.exe to the calculator. + * misc/sendexe.c -- send an update.exe to the calculator. * Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey * * This file is part of libp7. diff --git a/src/protocol/server.c b/src/misc/server.c similarity index 99% rename from src/protocol/server.c rename to src/misc/server.c index 89c5039..d2771a9 100644 --- a/src/protocol/server.c +++ b/src/misc/server.c @@ -1,5 +1,5 @@ /* ***************************************************************************** - * protocol/server.c -- set up a protocol 7 server. + * misc/server.c -- set up a protocol 7 server. * Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey * * This file is part of libp7. diff --git a/src/protocol/setlink.c b/src/misc/setlink.c similarity index 97% rename from src/protocol/setlink.c rename to src/misc/setlink.c index b493b05..08a10c7 100644 --- a/src/protocol/setlink.c +++ b/src/misc/setlink.c @@ -1,5 +1,5 @@ /* ***************************************************************************** - * protocol/setlink.c -- set link settings. + * misc/setlink.c -- set link settings. * Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey * * This file is part of libp7. diff --git a/src/protocol/copy.c b/src/storage/copy.c similarity index 97% rename from src/protocol/copy.c rename to src/storage/copy.c index e410abd..666e70c 100644 --- a/src/protocol/copy.c +++ b/src/storage/copy.c @@ -1,5 +1,5 @@ /* ***************************************************************************** - * protocol/copy.c -- copy a file on the calculator. + * storage/copy.c -- copy a file on the calculator. * Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey * * This file is part of libp7. diff --git a/src/protocol/createdir.c b/src/storage/createdir.c similarity index 96% rename from src/protocol/createdir.c rename to src/storage/createdir.c index 798e05f..5053be6 100644 --- a/src/protocol/createdir.c +++ b/src/storage/createdir.c @@ -1,5 +1,5 @@ /* ***************************************************************************** - * protocol/createdir.c -- create a directory on the calculator. + * storage/createdir.c -- create a directory on the calculator. * Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey * * This file is part of libp7. diff --git a/src/protocol/delete.c b/src/storage/delete.c similarity index 96% rename from src/protocol/delete.c rename to src/storage/delete.c index b936f4a..aa7bd3b 100644 --- a/src/protocol/delete.c +++ b/src/storage/delete.c @@ -1,5 +1,5 @@ /* ***************************************************************************** - * protocol/delete.c -- delete a file/directory on the calculator. + * storage/delete.c -- delete a file/directory on the calculator. * Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey * * This file is part of libp7. diff --git a/src/protocol/getfreemem.c b/src/storage/getfreemem.c similarity index 97% rename from src/protocol/getfreemem.c rename to src/storage/getfreemem.c index c2ebd66..d8614e7 100644 --- a/src/protocol/getfreemem.c +++ b/src/storage/getfreemem.c @@ -1,5 +1,5 @@ /* ***************************************************************************** - * protocol/getfreemem.c -- get the amount of free memory on the calculator. + * storage/getfreemem.c -- get the amount of free memory on the calculator. * Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey * * This file is part of libp7. diff --git a/src/protocol/list.c b/src/storage/list.c similarity index 97% rename from src/protocol/list.c rename to src/storage/list.c index d45432c..9209f80 100644 --- a/src/protocol/list.c +++ b/src/storage/list.c @@ -1,5 +1,5 @@ /* ***************************************************************************** - * protocol/list.c -- list the elements on the calculator. + * storage/list.c -- list the elements on the calculator. * Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey * * This file is part of libp7. @@ -20,7 +20,7 @@ /** * p7_list: - * Requests a file. + * List files. * * @arg handle the libp7 handle * @arg devname the device name diff --git a/src/protocol/optimize.c b/src/storage/optimize.c similarity index 96% rename from src/protocol/optimize.c rename to src/storage/optimize.c index a972174..ff7cc84 100644 --- a/src/protocol/optimize.c +++ b/src/storage/optimize.c @@ -1,5 +1,5 @@ /* ***************************************************************************** - * protocol/optimize.c -- optimize a filesystem on the calculator. + * storage/optimize.c -- optimize a filesystem on the calculator. * Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey * * This file is part of libp7. diff --git a/src/protocol/request.c b/src/storage/request.c similarity index 98% rename from src/protocol/request.c rename to src/storage/request.c index aef33ce..12b56e7 100644 --- a/src/protocol/request.c +++ b/src/storage/request.c @@ -1,5 +1,5 @@ /* ***************************************************************************** - * protocol/request.c -- request a file from the calculator. + * storage/request.c -- request a file from the calculator. * Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey * * This file is part of libp7. diff --git a/src/protocol/reset.c b/src/storage/reset.c similarity index 96% rename from src/protocol/reset.c rename to src/storage/reset.c index 858cf0f..ecb9496 100644 --- a/src/protocol/reset.c +++ b/src/storage/reset.c @@ -1,5 +1,5 @@ /* ***************************************************************************** - * protocol/reset.c -- reset a filesystem on the calculator. + * storage/reset.c -- reset a filesystem on the calculator. * Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey * * This file is part of libp7. diff --git a/src/protocol/send.c b/src/storage/send.c similarity index 99% rename from src/protocol/send.c rename to src/storage/send.c index 3a099ff..14a859e 100644 --- a/src/protocol/send.c +++ b/src/storage/send.c @@ -1,5 +1,5 @@ /* ***************************************************************************** - * protocol/send.c -- send a file to the calculator. + * storage/send.c -- send a file to the calculator. * Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey * * This file is part of libp7.