From 729886a7bb6054efc42cb6ccd14957cde7a1fed9 Mon Sep 17 00:00:00 2001 From: "Thomas \"Cakeisalie5\" Touhey" Date: Sat, 25 Mar 2017 13:43:44 +0100 Subject: [PATCH] Reorganized a little, better comments in headers. --- Makefile | 16 +- Makefile.vars | 21 +-- include/libp7/internals.h | 38 ++-- include/libp7/protocol/seven.h | 21 ++- include/libp7/stream.h | 212 ++++++++++++++-------- src/core/_init.c | 52 +----- src/{ => protocol}/legacy/init.c.draft | 6 +- src/{ => protocol}/legacy/recv.c.draft | 0 src/{ => protocol}/legacy/special.c.draft | 0 src/{packet => protocol/seven}/command.c | 2 +- src/{packet => protocol/seven}/data.c | 2 +- src/{core => protocol/seven}/devices.c | 3 +- src/{packet => protocol/seven}/eack.c | 2 +- src/protocol/seven/init.c | 69 +++++++ src/{packet => protocol/seven}/recv.c | 2 +- src/{packet => protocol/seven}/send.c | 2 +- src/{misc => usage}/backup.c | 2 +- src/{misc => usage}/getscreen.c | 2 +- src/{ => usage}/mcs/list.c | 2 +- src/{ => usage}/mcs/request.c | 2 +- src/{misc => usage}/poke.c | 2 +- src/{misc => usage}/sendexe.c | 2 +- src/{misc => usage}/server.c | 2 +- src/{misc => usage}/setlink.c | 2 +- src/{ => usage}/storage/copy.c | 2 +- src/{ => usage}/storage/createdir.c | 2 +- src/{ => usage}/storage/delete.c | 2 +- src/{ => usage}/storage/getfreemem.c | 2 +- src/{ => usage}/storage/list.c | 2 +- src/{ => usage}/storage/optimize.c | 2 +- src/{ => usage}/storage/request.c | 2 +- src/{ => usage}/storage/reset.c | 2 +- src/{ => usage}/storage/send.c | 2 +- 33 files changed, 289 insertions(+), 193 deletions(-) rename src/{ => protocol}/legacy/init.c.draft (92%) rename src/{ => protocol}/legacy/recv.c.draft (100%) rename src/{ => protocol}/legacy/special.c.draft (100%) rename src/{packet => protocol/seven}/command.c (99%) rename src/{packet => protocol/seven}/data.c (99%) rename src/{core => protocol/seven}/devices.c (99%) rename src/{packet => protocol/seven}/eack.c (99%) create mode 100644 src/protocol/seven/init.c rename src/{packet => protocol/seven}/recv.c (99%) rename src/{packet => protocol/seven}/send.c (99%) rename src/{misc => usage}/backup.c (99%) rename src/{misc => usage}/getscreen.c (98%) rename src/{ => usage}/mcs/list.c (97%) rename src/{ => usage}/mcs/request.c (97%) rename src/{misc => usage}/poke.c (95%) rename src/{misc => usage}/sendexe.c (98%) rename src/{misc => usage}/server.c (99%) rename src/{misc => usage}/setlink.c (98%) rename src/{ => usage}/storage/copy.c (97%) rename src/{ => usage}/storage/createdir.c (96%) rename src/{ => usage}/storage/delete.c (96%) rename src/{ => usage}/storage/getfreemem.c (96%) rename src/{ => usage}/storage/list.c (97%) rename src/{ => usage}/storage/optimize.c (95%) rename src/{ => usage}/storage/request.c (97%) rename src/{ => usage}/storage/reset.c (96%) rename src/{ => usage}/storage/send.c (99%) diff --git a/Makefile b/Makefile index ae02e18..b2cdf51 100755 --- a/Makefile +++ b/Makefile @@ -94,23 +94,23 @@ dist: mrproper all-lib: $(CHECKCFG) $(if $(STATIC),$(ANAME),$(SONAME)) # Make a module object directory. - $(MODULES:%=$(OBJDIR)/%): + $(OBJDIR)/ $(DIRS:%=$(OBJDIR)/%): $(call bcmd,mkdir,$@,$(MD) $@) -# Make a module object out of a module source file. -define make-moduleobj-rule - $(OBJDIR)/$1/%.o: $(SRCDIR)/$1/%.c $(INC) | $(OBJDIR)/$1 +# Make an object out of a source file. +define make-obj-rule + $(OBJDIR)/$1.o: $(SRCDIR)/$1.c $(INC) | $(dir $(OBJDIR)/$1) $(call bcmd,cc,$$@,$(CC) -c -o $$@ $$< $(CFLAGS)) endef -$(foreach mod,$(MODULES), \ -$(eval $(call make-moduleobj-rule,$(mod)))) +$(foreach src,$(SRC),\ +$(eval $(call make-obj-rule,$(src)))) # Make the shared library. - $(SONAME): $(foreach m,$(MODULES),$(SRC_$(m):%=$(OBJDIR)/$(m)/%.o)) + $(SONAME): $(SRC:%=$(OBJDIR)/%.o) $(call bcmd,ld,$@,$(LD) -o $@ $^ $(LDFLAGS)) # Make the static library. - lib$(NAME).a: $(foreach m,$(MODULES),$(SRC_$(m):%=$(OBJDIR)/$(m)/%.o)) + lib$(NAME).a: $(SRC:%=$(OBJDIR)/%.o) $(call bcmd,ar rc,$@,$(AR) rcs $@ $^) # Remove the objects directory. diff --git a/Makefile.vars b/Makefile.vars index 114fb78..a387812 100755 --- a/Makefile.vars +++ b/Makefile.vars @@ -128,20 +128,19 @@ endif GZIP := gzip -f #******************************************************************************# -# Look for modules and modules sources # +# Look up the sources and includes # #******************************************************************************# -# Look for modules first - MODULES := $(notdir $(shell find $(SRCDIR) -mindepth 1 -maxdepth 1 \ - -type d | sort)) +# Look up the sources + SRC := $(basename $(shell find $(SRCDIR) -mindepth 1 -type f \ + -name "*.c" -printf "%P\n")) + DIRS := $(sort $(dir $(SRC))) -# Then look for their content -define get-module-source - SRC_$1 := $(basename $(shell find $(SRCDIR)/$1 \ - -maxdepth 1 -mindepth 1 -type f -name "*.[cs]" -printf "%P\n" | sort)) -endef -$(foreach mod,$(MODULES), \ -$(eval $(call get-module-source,$(mod)))) +# Look up the includes + INCPUB := $(shell find $(INCDIR) -name "*.h" -and \ + -not -path "*internals*" -printf "%P\n") +# Look up the includes + INC := $(shell find $(INCDIR) -name "*.h") #******************************************************************************# # Look for public headers (not internals.h or internals/**/*.h # #******************************************************************************# diff --git a/include/libp7/internals.h b/include/libp7/internals.h index 3ef3fd8..db94918 100644 --- a/include/libp7/internals.h +++ b/include/libp7/internals.h @@ -88,8 +88,8 @@ typedef struct { } p7_env_t; /* environment functions */ -p7_env_t *p7_get_env(const char *model); -int p7_command_is_supported(unsigned int code, const p7_env_t *env); +extern p7_env_t *p7_get_env(const char *model); +extern int p7_command_is_supported(unsigned int code, const p7_env_t *env); # define command_is_supported(N) p7_command_is_supported(N, handle->_env) /* ************************************************************************** */ /* Handle-related */ @@ -111,14 +111,18 @@ struct p7_handle_s { /* flags - see above */ unsigned int _flags; - /* stream, environment, response, server information */ - const p7_env_t *_env; /* see `core/devices.c` */ + /* debug name, for multi-handle logging (e.g. `p7servtest` in p7utils) + * a space/null, 8 chars max, and imposed null char */ + char _name[10]; + + /* stream settings */ p7_stream_t _stream; p7_streamsettings_t _settings; + + /* protocol 7.00-related core things */ + const p7_env_t *_env; /* see `protocol/seven/devices.c` */ p7_packet_t _response; p7_server_t _server; - - /* last sent command, response */ p7ushort_t _last_sent_command; p7_packet_t _response_data; @@ -133,33 +137,33 @@ struct p7_handle_s { /* raw reception packet buffer */ unsigned char _recv_buffer[MAX_PACKET_SIZE]; - - /* name - a space/null, 8 characters max, then imposed null char. */ - char _name[10]; }; /* sub-sub-init utils */ # ifndef P7_DISABLED_FILE -int _p7_finit(p7_handle_t **handle, unsigned int flags, +extern int _p7_finit(p7_handle_t **handle, unsigned int flags, FILE *rstream, FILE *wstream, int rstream_close); # endif /* ************************************************************************** */ -/* Packet I/O */ +/* Protocol 7.00 Packet I/O utilities */ /* ************************************************************************** */ +/* start communication */ +extern int p7_start(p7_handle_t *handle, int check); + /* decode packets */ -int p7_decode_command(p7_handle_t *handle, +extern int p7_decode_command(p7_handle_t *handle, const unsigned char *data, size_t data_size); -int p7_decode_ack(p7_handle_t *handle, +extern int p7_decode_ack(p7_handle_t *handle, const unsigned char *data, size_t data_size); -int p7_decode_data(p7_handle_t *handle, +extern int p7_decode_data(p7_handle_t *handle, const void *raw, p7ushort_t raw_size); /* utilities */ -int p7_send_again(p7_handle_t *handle); -int p7_recv(p7_handle_t *handle, int checksum); +extern int p7_send_again(p7_handle_t *handle); +extern int p7_recv(p7_handle_t *handle, int checksum); /* Resend error packet (meant to be used with recv, not suited for public) */ -int p7_send_err_resend(p7_handle_t *handle); +extern int p7_send_err_resend(p7_handle_t *handle); /* active response */ # define response (handle->_response) diff --git a/include/libp7/protocol/seven.h b/include/libp7/protocol/seven.h index 65ebe90..5bc3044 100644 --- a/include/libp7/protocol/seven.h +++ b/include/libp7/protocol/seven.h @@ -90,7 +90,11 @@ typedef int p7_termtype_t; * type, there is a five-letter subtype (such as "TYP01" or "TYPZ1"), and * for some types, subheaders. libp7 translates this into a VRAM and a picture * format, which you can see in the libp7 Protocol 7.00 packet - * representation. */ + * representation. + * + * Note that the screenstreaming packet flow is totally different from + * the normal packet flow: it's just the calculator sending its screen + * repeateadly, not expecting any answer from the other side. */ /* ************************************************************************** */ /* Command payload */ /* ************************************************************************** */ @@ -254,7 +258,20 @@ extern int p7_send_cmd_data(p7_handle_t *handle, p7ushort_t subtype, * `p7_flag_active | p7_flag_check` flags takes care of that), * Protocol 7.00 is basically a set of packet flows. * - * Command, TODO + * A packet flow always start with a command from the active side to the + * passive side. Then, once the command is ACK-ed, according to the command, + * different packet flows can occur. Here are a few characteristic examples: + * + * - File action. For example, file removing require nothing after the command + * confirmation; + * - File sending. After the overwrite flow, the active side sends data + * packets containing the file data (`p7_send_buffer`/`p7_send_data`); + * - File receiving. The active side sends a role swap packet, then the + * server (now active) does the file sending procedure (with command). Once + * it has finished sending the file, it swaps roles again; + * - File listing. The active side sends a role swap packet, then the + * server (now active) sends file information commands for each entry. + * Once it has finished, it swap roles again. * * If you have finished doing what you wanted to do and the communication is * still active, send an END packet, to which the other calculator should diff --git a/include/libp7/stream.h b/include/libp7/stream.h index 936d630..0ffc31d 100644 --- a/include/libp7/stream.h +++ b/include/libp7/stream.h @@ -19,15 +19,10 @@ * The libp7 stream abstraction is there so that the core code can be more * platform-agnostic (althrough platform-specific helpers are built-in for * popular platforms like Microsoft Windows or GNU/Linux distributions). - * * A stream is basically what separates libp7 from the calculator. * When data is read from the stream, what is expected is what the calculator * has sent, and when data is written to the stream, it is what the calculator * shall receive. - * - * As some streams can have buffering, there is no buffering system in this - * superset. Also, if you don't succeed totally (all bytes read/written), - * to the library, you have failed, and shall return an error. * ************************************************************************** */ #ifndef LIBP7_STREAM_H # define LIBP7_STREAM_H @@ -36,70 +31,33 @@ extern "C" { # endif +/* forward structure declarations (don't mind) */ +struct p7_streamsettings_s; +typedef struct p7_streamsettings_s p7_streamsettings_t; +struct p7_streamtimeouts_s; +typedef struct p7_streamtimeouts_s p7_streamtimeouts_t; /* ************************************************************************** */ -/* Communication settings */ +/* Stream structure */ /* ************************************************************************** */ -/* baud speeds */ -# define P7_B9600 9600 -# define P7_B19200 19200 -# define P7_B38400 38400 -# define P7_B57600 57600 -# define P7_B115200 115200 +/* This is the main structure of a stream. It is basically only made of + * callbacks the libp7 functions will use to interact with the other device. + * In case of any error (buffer not fully read/written, invalid settings), + * your callbacks will have to return one of the libp7 error codes. + * There is no partial read or write. + * + * First, the basic functions: once your stream is open (and it should be + * open before calling `p7_sinit`), it will call your `setcomm` and `settm` + * functions using the protocol information and the information given by the + * user to the initialization function (don't use this information directly!). + * Then, to read data from your stream, it will use the `read` callback, + * and to write data to your stream, it will use the `write` callback. + * On exit, your `close` callback will be called, to free memory you have + * allocated for your needs, and other I/O requests. + * + * Mind that what you will receive is the direct request from the library/user, + * there is no buffering between your stream and the library/user! If you need + * buffering, you'll have to implement it. */ -/* Flags */ -# define P7_STOPBITSMASK 0x00000001 -# define P7_ONESTOPBIT 0x00000000 -# define P7_TWOSTOPBITS 0x00000001 -# define P7_PARMASK 0x00000006 -# define P7_PARDIS 0x00000000 -# define P7_PARENB 0x00000002 -# define P7_PAREVEN 0x00000000 -# define P7_PARODD 0x00000004 -# define P7_DTRMASK 0x00000018 -# define P7_DTRCTL_DISABLE 0x00000000 -# define P7_DTRCTL_ENABLE 0x00000008 -# define P7_DTRCTL_HANDSHAKE 0x00000010 -# define P7_RTSMASK 0x00000060 -# define P7_RTSCTL_DISABLE 0x00000000 -# define P7_RTSCTL_ENABLE 0x00000020 -# define P7_RTSCTL_HANDSHAKE 0x00000040 -# define P7_XONCTL_DISABLE 0x00000000 -# define P7_XONCTL_ENABLE 0x00000080 -# define P7_XOFFCTL_DISABLE 0x00000000 -# define P7_XOFFCTL_ENABLE 0x00000100 - -/* Control characters */ -# define P7_NCCS 0x02 -# define P7_XON 0x00 -# define P7_XOFF 0x01 - -/* Communication settings. */ -typedef struct p7_streamsettings_s { - /* flags - see above */ - unsigned int flags; - - /* speed: one of the P7_B* constants */ - unsigned int speed; - - /* characters */ - unsigned char cc[P7_NCCS]; -} p7_streamsettings_t; - -/* Timeout settings */ -typedef struct p7_streamtimeouts_s { - /* read timeout in ms */ - unsigned int read; - - /* in-between read timeout in ms */ - unsigned int read_bw; - - /* write timeout in ms */ - unsigned int write; -} p7_streamtimeouts_t; - -/* The callbacks take the uncasted cookie and the required arguments for - * the action (`timeout` being in milliseconds). They should return the libp7 - * error code, or 0 if no error has occured. Again, no partial success. */ typedef struct p7_stream_s { /* main callbacks */ int (*read)(void *cookie, unsigned char *dest, size_t size); @@ -114,34 +72,134 @@ typedef struct p7_stream_s { void *cookie; } p7_stream_t; -/* initialize libp7 with a custom stream */ +/* ************************************************************************** */ +/* Stream settings values and flags */ +/* ************************************************************************** */ +/* Here are the different baud speeds you can encounter, in bauds. + * Note that one speed is not supported by all models. */ + +# define P7_B1200 1200 +# define P7_B2400 2400 +# define P7_B4800 4800 +# define P7_B9600 9600 +# define P7_B19200 19200 +# define P7_B38400 38400 +# define P7_B57600 57600 +# define P7_B115200 115200 + +/* Here are the control characters and other values you have in the + * stream settings. */ + +# define P7_NCCS 0x02 /* number of control characters */ +# define P7_XON 0x00 /* XON character: re-enable transmission */ +# define P7_XOFF 0x01 /* XOFF character: disable transmission */ + +/* From here, those are all in the stream settings flags. + * Here are the stop bits settings: */ + +# define P7_STOPBITSMASK 0x0001 +# define P7_ONESTOPBIT 0x0000 /* one stop bit */ +# define P7_TWOSTOPBITS 0x0001 /* two stop bits */ + +/* Here are the parity settings: */ + +# define P7_PARMASK 0x0006 +# define P7_PARDIS 0x0000 /* disable parity checking */ +# define P7_PARENB 0x0002 /* enable parity checking */ +# define P7_PAREVEN 0x0000 /* even parity */ +# define P7_PARODD 0x0004 /* odd parity */ + +/* Here are the DTR/RTS settings. + * Notice that not all platforms implement this. Just do as you can. */ + +# define P7_DTRMASK 0x0018 +# define P7_DTRCTL_DISABLE 0x0000 /* disable DTR */ +# define P7_DTRCTL_ENABLE 0x0008 /* enable DTR */ +# define P7_DTRCTL_HANDSHAKE 0x0010 /* enable DTR and handshake */ + +# define P7_RTSMASK 0x0060 +# define P7_RTSCTL_DISABLE 0x0000 /* disable RTS */ +# define P7_RTSCTL_ENABLE 0x0020 /* enable RTS */ +# define P7_RTSCTL_HANDSHAKE 0x0040 /* enable RTS and handshake */ + +/* Here are the XON/XOFF software control settings. + * XOFF disables the transmission temporarily, usually because the device at + * the other end can't manage too much data too quickly. */ + +# define P7_XONMASK 0x0080 +# define P7_XONCTL_DISABLE 0x0000 /* disable XON */ +# define P7_XONCTL_ENABLE 0x0080 /* enable XON */ + +/* XON re-enables the transmission, probably because the device at the end + * has finished processing the data you sent and is ready to process more. */ + +# define P7_XOFFMASK 0x0100 +# define P7_XOFFCTL_DISABLE 0x0000 /* disable XOFF */ +# define P7_XOFFCTL_ENABLE 0x0100 /* enable XOFF */ +/* ************************************************************************** */ +/* Stream settings */ +/* ************************************************************************** */ +/* Here is the stream settings structure. + * This structure will be sent to your `setcomm` callback to set communication + * settings. */ + +struct p7_streamsettings_s { + /* flags - see the above section */ + unsigned int flags; + + /* speed: one of the P7_B* constants */ + unsigned int speed; + + /* characters */ + unsigned char cc[P7_NCCS]; +}; + +/* And here is the stream timeouts structure. + * This structure will be sent to your `settm` callback, usually after a state + * change in the communication. + * All timeouts are in milliseconds. */ + +struct p7_streamtimeouts_s { + /* Initial read timeout */ + unsigned int read; + + /* In-between bytes read timeout */ + unsigned int read_bw; + + /* Total write timeout */ + unsigned int write; +}; +/* ************************************************************************** */ +/* Stream functions */ +/* ************************************************************************** */ +/* Initialize a handle using a custom stream. */ extern int p7_sinit(p7_handle_t **h, unsigned int flags, const char *name, p7_stream_t stream, const p7_streamsettings_t *settings); -/* get the stream from the handle */ +/* Extract the stream out of the handle. */ extern const p7_stream_t *p7_get_stream(p7_handle_t *handle); -/* initialize settings */ +/* Default stream settings utilities. + * `p7_initcomm` initializes a stream settings structure. */ extern void p7_initcomm(p7_streamsettings_t *settings); extern p7_streamsettings_t p7_default_settings; -/* read and write data from and to stream */ +/* Read and write data from and to a stream, set stream settings and timeouts. + * You should use these functions instead of the callbacks directly. */ extern int p7_read(p7_stream_t *stream, void *dest, size_t size); extern int p7_write(p7_stream_t *stream, const void *data, size_t size); - -/* set stream settings */ extern int p7_setcomm(p7_stream_t *stream, const p7_streamsettings_t *settings); extern int p7_settm(p7_stream_t *stream, const p7_streamtimeouts_t *timeouts); /* ************************************************************************** */ /* Built-in streams */ /* ************************************************************************** */ -/* FILE stream */ +/* Make a libp7 stream using the standard FILE interface. */ # ifndef P7_DISABLED_FILE extern int p7_finit(p7_handle_t **handle, unsigned int flags, FILE *readstream, FILE *writestream); # endif -/* STREAMS device */ +/* Make a libp7 stream using the POSIX STREAMS interface. */ # if defined(__linux__) extern int p7_fdinit(p7_handle_t **handle, unsigned int flags, const char *name, int readfd, int writefd, @@ -153,12 +211,12 @@ extern int p7_fdcominit(p7_handle_t **handle, unsigned int flags, # define P7_DISABLED_STREAMS # endif -/* libusb initialization */ +/* Make a libp7 stream using libusb. */ # ifndef P7_DISABLED_LIBUSB extern int p7_libusbinit(p7_handle_t **handle, unsigned int flags); # endif -/* Microsoft Windows initialization */ +/* Make a libp7 stream using the Windows API. */ # ifdef __WINDOWS__ extern int p7_winit(p7_handle_t **handle, unsigned int flags, const char *path, p7_streamsettings_t *settings); @@ -167,7 +225,7 @@ extern int p7_wcomlist(void (*callback)(void*, const char*), void *cookie); # define P7_DISABLED_WINDOWS # endif -/* Check if there is serial/usb on this platform */ +/* Using the macros, check if there is serial/usb on this platform */ # if defined(P7_DISABLED_STREAMS) && defined(P7_DISABLED_WINDOWS) # define P7_NOSERIAL # endif diff --git a/src/core/_init.c b/src/core/_init.c index f6242c3..3b1891a 100644 --- a/src/core/_init.c +++ b/src/core/_init.c @@ -77,56 +77,6 @@ const p7_server_t *p7_get_info(p7_handle_t *handle) /* ************************************************************************** */ /* Initialization */ /* ************************************************************************** */ -/** - * start: - * Start the communication. - * - * @arg handle the handle. - * @return the error, if any. - */ - -static int start(p7_handle_t *handle, int check) -{ - int err = 0; - log_info("so we're active? let's do what active people do then!"); - - /* send initial check */ - if (check) { - log_info("sending initial check packet"); - if ((err = p7_send_ini_check(handle))) { - log_fatal("couldn't send check/didn't receive answer"); - return (err); - } else if (response.type != p7_pt_ack) { - log_fatal("received packet wasn't ack, ask your dentist"); - return (p7_error_unknown); - } -#if LOGLEVEL <= ll_info - } else { - log_info("skipping initial check, we suppose already initialized"); -#endif - } - - /* discover environment */ - log_info("checkin' up da environment"); - if ((err = p7_send_cmdsys_getinfo(handle))) { - log_fatal("couldn't send env query/receive answer"); - return (err); - } else if (response.type != p7_pt_ack || !response.extended) { - log_fatal("response to env query wasn't an extended ack packet"); - return (p7_error_unknown); - } - - /* save server */ - handle->_server = response.info; - - /* get environment based on sent hardware id */ - handle->_env = p7_get_env(handle->_server.hwid); - log_info("environment is '%s'", handle->_env->name); - - /* no error */ - return (0); -} - /** * p7_sinit: * Initialize libp7 with a P7 stream. @@ -179,7 +129,7 @@ int p7_sinit(p7_handle_t **h, unsigned int flags, /* if active, start */ if ((handle->_flags & p7_intflag_active) - && (err = start(handle, flags & P7_CHECK))) { + && (err = p7_start(handle, flags & P7_CHECK))) { p7_exit(*h); *h = NULL; return (err); } diff --git a/src/legacy/init.c.draft b/src/protocol/legacy/init.c.draft similarity index 92% rename from src/legacy/init.c.draft rename to src/protocol/legacy/init.c.draft index eb2e088..4c0372e 100644 --- a/src/legacy/init.c.draft +++ b/src/protocol/legacy/init.c.draft @@ -1,5 +1,5 @@ /* ***************************************************************************** - * legacy/init.c -- initialize the legacy communication. + * protocol/legacy/init.c -- initialize the legacy communication. * Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey * * This file is part of libp7. @@ -22,14 +22,14 @@ /* Initialization */ /* ************************************************************************** */ /** - * start: + * p7_legacy_start: * Start the communication. * * @arg handle the handle. * @return the error, if any. */ -static int start(p7_handle_t *handle) +int p7_legacy_start(p7_handle_t *handle) { /* receive the attention request */ unsigned char byte; diff --git a/src/legacy/recv.c.draft b/src/protocol/legacy/recv.c.draft similarity index 100% rename from src/legacy/recv.c.draft rename to src/protocol/legacy/recv.c.draft diff --git a/src/legacy/special.c.draft b/src/protocol/legacy/special.c.draft similarity index 100% rename from src/legacy/special.c.draft rename to src/protocol/legacy/special.c.draft diff --git a/src/packet/command.c b/src/protocol/seven/command.c similarity index 99% rename from src/packet/command.c rename to src/protocol/seven/command.c index 6266ab9..1023c06 100644 --- a/src/packet/command.c +++ b/src/protocol/seven/command.c @@ -1,5 +1,5 @@ /* ***************************************************************************** - * packet/command.c -- the command utilities. + * protocol/seven/command.c -- the command utilities. * Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey * * This file is part of libp7. diff --git a/src/packet/data.c b/src/protocol/seven/data.c similarity index 99% rename from src/packet/data.c rename to src/protocol/seven/data.c index 04e7a6c..e06bcda 100644 --- a/src/packet/data.c +++ b/src/protocol/seven/data.c @@ -1,5 +1,5 @@ /* ***************************************************************************** - * packet/data.c -- data packet and buffer sending. + * protocol/seven/data.c -- data packet and buffer sending. * Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey * * This file is part of libp7. diff --git a/src/core/devices.c b/src/protocol/seven/devices.c similarity index 99% rename from src/core/devices.c rename to src/protocol/seven/devices.c index 8dbf729..36ac41d 100644 --- a/src/core/devices.c +++ b/src/protocol/seven/devices.c @@ -1,5 +1,5 @@ /* ***************************************************************************** - * core/devices.c -- libp7 known devices. + * protocol/seven/devices.c -- libp7 known devices. * Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey * * This file is part of libp7. @@ -198,7 +198,6 @@ static p7_env_t known_environments[] = { static p7_env_t default_environment = {NULL, "Default environment", MASK_ALL | MASK_MCS | MASK_LINK | MASK_FLS}; - /* ************************************************************************** */ /* Internal functions */ /* ************************************************************************** */ diff --git a/src/packet/eack.c b/src/protocol/seven/eack.c similarity index 99% rename from src/packet/eack.c rename to src/protocol/seven/eack.c index d9fd559..89ad418 100644 --- a/src/packet/eack.c +++ b/src/protocol/seven/eack.c @@ -1,5 +1,5 @@ /* ***************************************************************************** - * packet/eack.c -- extended ACK management. + * protocol/seven/eack.c -- extended ACK management. * Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey * * This file is part of libp7. diff --git a/src/protocol/seven/init.c b/src/protocol/seven/init.c new file mode 100644 index 0000000..c1ef85a --- /dev/null +++ b/src/protocol/seven/init.c @@ -0,0 +1,69 @@ +/* ***************************************************************************** + * protocol/seven/init.c -- initialize a P7 handle. + * 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_start: + * Start the communication. + * + * @arg handle the handle. + * @return the error, if any. + */ + +int p7_start(p7_handle_t *handle, int check) +{ + int err = 0; + log_info("so we're active? let's do what active people do then!"); + + /* send initial check */ + if (check) { + log_info("sending initial check packet"); + if ((err = p7_send_ini_check(handle))) { + log_fatal("couldn't send check/didn't receive answer"); + return (err); + } else if (response.type != p7_pt_ack) { + log_fatal("received packet wasn't ack, ask your dentist"); + return (p7_error_unknown); + } +#if LOGLEVEL <= ll_info + } else { + log_info("skipping initial check, we suppose already initialized"); +#endif + } + + /* discover environment */ + log_info("checkin' up da environment"); + if ((err = p7_send_cmdsys_getinfo(handle))) { + log_fatal("couldn't send env query/receive answer"); + return (err); + } else if (response.type != p7_pt_ack || !response.extended) { + log_fatal("response to env query wasn't an extended ack packet"); + return (p7_error_unknown); + } + + /* save server */ + handle->_server = response.info; + + /* get environment based on sent hardware id */ + handle->_env = p7_get_env(handle->_server.hwid); + log_info("environment is '%s'", handle->_env->name); + + /* no error */ + return (0); +} diff --git a/src/packet/recv.c b/src/protocol/seven/recv.c similarity index 99% rename from src/packet/recv.c rename to src/protocol/seven/recv.c index 581e3a3..3f59218 100644 --- a/src/packet/recv.c +++ b/src/protocol/seven/recv.c @@ -1,5 +1,5 @@ /* ***************************************************************************** - * packet/recv.c -- receive and decode the packet. + * protocol/seven/recv.c -- receive and decode the packet. * Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey * * This file is part of libp7. diff --git a/src/packet/send.c b/src/protocol/seven/send.c similarity index 99% rename from src/packet/send.c rename to src/protocol/seven/send.c index 64aa8e9..430c273 100644 --- a/src/packet/send.c +++ b/src/protocol/seven/send.c @@ -1,5 +1,5 @@ /* ***************************************************************************** - * packet/send.c -- prepare and send a packet. + * protocol/seven/send.c -- prepare and send a packet. * Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey * * This file is part of libp7. diff --git a/src/misc/backup.c b/src/usage/backup.c similarity index 99% rename from src/misc/backup.c rename to src/usage/backup.c index ea71955..c8ec750 100644 --- a/src/misc/backup.c +++ b/src/usage/backup.c @@ -1,5 +1,5 @@ /* ***************************************************************************** - * misc/backup.c -- backup software elements of the calculator. + * usage/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/misc/getscreen.c b/src/usage/getscreen.c similarity index 98% rename from src/misc/getscreen.c rename to src/usage/getscreen.c index 848d508..e82c1f9 100644 --- a/src/misc/getscreen.c +++ b/src/usage/getscreen.c @@ -1,5 +1,5 @@ /* ***************************************************************************** - * misc/getscreen.c -- receive the streamed screen from the calculator. + * usage/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/mcs/list.c b/src/usage/mcs/list.c similarity index 97% rename from src/mcs/list.c rename to src/usage/mcs/list.c index 12cca5a..cbd9600 100644 --- a/src/mcs/list.c +++ b/src/usage/mcs/list.c @@ -1,5 +1,5 @@ /* ***************************************************************************** - * mcs/list.c -- list the elements on the main memory. + * usage/mcs/list.c -- list the elements on the main memory. * Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey * * This file is part of libp7. diff --git a/src/mcs/request.c b/src/usage/mcs/request.c similarity index 97% rename from src/mcs/request.c rename to src/usage/mcs/request.c index bf74188..d614b6a 100644 --- a/src/mcs/request.c +++ b/src/usage/mcs/request.c @@ -1,5 +1,5 @@ /* ***************************************************************************** - * mcs/request.c -- request a file from the main memory. + * usage/mcs/request.c -- request a file from the main memory. * Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey * * This file is part of libp7. diff --git a/src/misc/poke.c b/src/usage/poke.c similarity index 95% rename from src/misc/poke.c rename to src/usage/poke.c index c1ff7d1..03213e3 100644 --- a/src/misc/poke.c +++ b/src/usage/poke.c @@ -1,5 +1,5 @@ /* ***************************************************************************** - * misc/poke.c -- poke the calculator so the communication doesn't stop. + * usage/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/misc/sendexe.c b/src/usage/sendexe.c similarity index 98% rename from src/misc/sendexe.c rename to src/usage/sendexe.c index a44313e..4d944ee 100644 --- a/src/misc/sendexe.c +++ b/src/usage/sendexe.c @@ -1,5 +1,5 @@ /* ***************************************************************************** - * misc/sendexe.c -- send an update.exe to the calculator. + * usage/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/misc/server.c b/src/usage/server.c similarity index 99% rename from src/misc/server.c rename to src/usage/server.c index d2771a9..110cb49 100644 --- a/src/misc/server.c +++ b/src/usage/server.c @@ -1,5 +1,5 @@ /* ***************************************************************************** - * misc/server.c -- set up a protocol 7 server. + * usage/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/misc/setlink.c b/src/usage/setlink.c similarity index 98% rename from src/misc/setlink.c rename to src/usage/setlink.c index c8b90a8..833dfde 100644 --- a/src/misc/setlink.c +++ b/src/usage/setlink.c @@ -1,5 +1,5 @@ /* ***************************************************************************** - * misc/setlink.c -- set link settings. + * usage/setlink.c -- set link settings. * Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey * * This file is part of libp7. diff --git a/src/storage/copy.c b/src/usage/storage/copy.c similarity index 97% rename from src/storage/copy.c rename to src/usage/storage/copy.c index 666e70c..fc07c2a 100644 --- a/src/storage/copy.c +++ b/src/usage/storage/copy.c @@ -1,5 +1,5 @@ /* ***************************************************************************** - * storage/copy.c -- copy a file on the calculator. + * usage/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/storage/createdir.c b/src/usage/storage/createdir.c similarity index 96% rename from src/storage/createdir.c rename to src/usage/storage/createdir.c index 5053be6..8827490 100644 --- a/src/storage/createdir.c +++ b/src/usage/storage/createdir.c @@ -1,5 +1,5 @@ /* ***************************************************************************** - * storage/createdir.c -- create a directory on the calculator. + * usage/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/storage/delete.c b/src/usage/storage/delete.c similarity index 96% rename from src/storage/delete.c rename to src/usage/storage/delete.c index aa7bd3b..1596d74 100644 --- a/src/storage/delete.c +++ b/src/usage/storage/delete.c @@ -1,5 +1,5 @@ /* ***************************************************************************** - * storage/delete.c -- delete a file/directory on the calculator. + * usage/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/storage/getfreemem.c b/src/usage/storage/getfreemem.c similarity index 96% rename from src/storage/getfreemem.c rename to src/usage/storage/getfreemem.c index d8614e7..b3c9a5a 100644 --- a/src/storage/getfreemem.c +++ b/src/usage/storage/getfreemem.c @@ -1,5 +1,5 @@ /* ***************************************************************************** - * storage/getfreemem.c -- get the amount of free memory on the calculator. + * usage/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/storage/list.c b/src/usage/storage/list.c similarity index 97% rename from src/storage/list.c rename to src/usage/storage/list.c index 9209f80..1becf3c 100644 --- a/src/storage/list.c +++ b/src/usage/storage/list.c @@ -1,5 +1,5 @@ /* ***************************************************************************** - * storage/list.c -- list the elements on the calculator. + * usage/storage/list.c -- list the elements on the calculator. * Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey * * This file is part of libp7. diff --git a/src/storage/optimize.c b/src/usage/storage/optimize.c similarity index 95% rename from src/storage/optimize.c rename to src/usage/storage/optimize.c index ff7cc84..3146447 100644 --- a/src/storage/optimize.c +++ b/src/usage/storage/optimize.c @@ -1,5 +1,5 @@ /* ***************************************************************************** - * storage/optimize.c -- optimize a filesystem on the calculator. + * usage/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/storage/request.c b/src/usage/storage/request.c similarity index 97% rename from src/storage/request.c rename to src/usage/storage/request.c index 12b56e7..8d9cb27 100644 --- a/src/storage/request.c +++ b/src/usage/storage/request.c @@ -1,5 +1,5 @@ /* ***************************************************************************** - * storage/request.c -- request a file from the calculator. + * usage/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/storage/reset.c b/src/usage/storage/reset.c similarity index 96% rename from src/storage/reset.c rename to src/usage/storage/reset.c index ecb9496..4ab60ca 100644 --- a/src/storage/reset.c +++ b/src/usage/storage/reset.c @@ -1,5 +1,5 @@ /* ***************************************************************************** - * storage/reset.c -- reset a filesystem on the calculator. + * usage/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/storage/send.c b/src/usage/storage/send.c similarity index 99% rename from src/storage/send.c rename to src/usage/storage/send.c index 14a859e..4f899a5 100644 --- a/src/storage/send.c +++ b/src/usage/storage/send.c @@ -1,5 +1,5 @@ /* ***************************************************************************** - * storage/send.c -- send a file to the calculator. + * usage/storage/send.c -- send a file to the calculator. * Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey * * This file is part of libp7.