cake
/
libp7
Archived
1
0
Fork 1

Added MCS file request.

This commit is contained in:
Thomas Touhey 2017-02-25 11:53:47 +01:00
parent ac6f40a518
commit ef5e9c9a8f
5 changed files with 114 additions and 4 deletions

View File

@ -116,8 +116,11 @@ void p7_exit(p7_handle_t *handle);
/* MCS interaction */
/* ************************************************************************** */
/* List files */
int p7_mcs_list(p7_handle_t *handle,
void (*callback)(void*, const g1m_mcshead_t *head), void *cookie);
int p7_mcs_list(p7_handle_t *handle, p7_mcslist_t callback, void *cookie);
/* Request a file */
int p7_mcs_request(p7_handle_t *handle, g1m_mcsfile_t **file,
const g1m_mcshead_t *head, p7_disp_t disp);
# endif
/* ************************************************************************** */

View File

@ -182,10 +182,24 @@ static inline int p7_send_cmd##C(p7_handle_t *handle, \
return (p7_send_cmd_data(handle, CODE, 0, 0, 0, \
dirname, filename, NULL, newname, devname, NULL)); }
# ifndef P7_DISABLED_LIBG1M
/* - Command for interacting with an MCS file - */
# define p7cmd_mcsfile(C, CODE) \
static inline int p7_send_cmd##C(p7_handle_t *handle, \
const g1m_mcshead_t *head) { \
return (p7_send_cmd_data(handle, CODE, 0, head->_rawtype, 0, \
(char*)head->_dirname, (char*)head->name, (char*)head->_group, \
NULL, NULL, NULL)); }
# else
# define p7cmd_mcsfile(C, CODE)
# endif
/* Command definitions
* 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_mcsfile(mcs_reqfile, 0x24)
p7cmd(mcs_reqallinfo, 0x2D)
p7cmd(bak_reqram, 0x2F)
p7cmd(bak_putram, 0x30)

View File

@ -77,6 +77,11 @@ typedef int (*p7_confirm_t)(void);
/* List callback */
typedef void (*p7_list_t)(void*, const char*, const char*, p7uint_t);
#ifndef P7_DISABLED_LIBG1M
/* MCS list callback */
typedef void (*p7_mcslist_t)(void*, const g1m_mcshead_t*);
#endif
/* ************************************************************************** */
/* Useful types */
/* ************************************************************************** */

View File

@ -29,8 +29,7 @@
* @return if it worked.
*/
int p7_mcs_list(p7_handle_t *handle,
void (*callback)(void*, const g1m_mcshead_t *head), void *cookie)
int p7_mcs_list(p7_handle_t *handle, p7_mcslist_t callback, void *cookie)
{
int err;

89
src/mcs/request.c Normal file
View File

@ -0,0 +1,89 @@
/* *****************************************************************************
* mcs/request.c -- request a file from the main memory.
* Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
*
* 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 <http://www.gnu.org/licenses/>.
* ************************************************************************** */
#include <libp7/internals.h>
#ifndef P7_DISABLED_LIBG1M
/**
* p7_mcs_request:
* Requests a file.
*
* @arg handle the libp7 handle.
* @arg file the file handle to create.
* @arg head the head to get the file with.
* @arg disp the progression display callback.
* @return the libp7 error (0 if ok)
*/
int p7_mcs_request(p7_handle_t *handle, g1m_mcsfile_t **file,
const g1m_mcshead_t *head, p7_disp_t disp)
{
int err;
/* make checks */
chk_handle(handle);
chk_active(handle);
/* send command */
log_info("sending file transfer request");
if ((err = p7_send_cmdmcs_reqfile(handle, head))) {
log_fatal("couldn't send file transfer request/didn't receive answer");
return (err);
} if (response.type == p7_pt_error
&& response.code == p7_err_other) {
log_fatal("requested file didn't exist");
return (p7_error_notfound);
} else if (response.type != p7_pt_ack) {
log_fatal("didn't receive ack or known error...");
return (p7_error_unknown);
}
/* swap roles */
log_info("sending roleswap");
if ((err = p7_send_roleswp(handle))) {
log_fatal("couldn't swap roles");
return (err);
} else if (response.type != p7_pt_cmd || response.code != 0x25) {
log_fatal("didn't receive expected command");
return (p7_error_unknown);
}
/* get data */
unsigned char *tmp = malloc(response.filesize);
if (!tmp) return (p7_error_alloc);
if ((err = p7_get_data(handle, tmp, response.filesize, 0, disp)))
goto fail;
/* check if last answer was roleswap */
if (response.type != p7_pt_roleswp) {
err = p7_error_unknown;
goto fail;
}
/* decode */
if (g1m_decode_mcsfile_data(file, head, tmp, response.filesize)) {
err = p7_error_unknown;
goto fail;
}
fail:
free(tmp);
return (err);
}
#endif