cake
/
libp7
Archived
1
0
Fork 1
This repository has been archived on 2024-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
libp7/src/storage/request.c

114 lines
3.2 KiB
C

/* *****************************************************************************
* storage/request.c -- request a file from the calculator.
* 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>
/**
* p7_request:
* Requests a file.
*
* @arg handle the libp7 handle
* @arg buffer the buffer (should be writable)
* @arg dirname the directory name
* @arg filename the filename
* @arg devname the device name
* @arg disp callback display
* @return if it worked
*/
int p7_request(p7_handle_t *handle, const p7_buffer_t *buffer,
const char *dirname, const char *filename, const char *devname,
p7_disp_t disp)
{
int err;
/* make checks */
chk_required_filename(filename);
chk_dirname(dirname);
chk_bufwrite(buffer);
chk_handle(handle);
chk_active(handle);
/* send command packet */
log_info("sending file transfer request");
if ((err = p7_send_cmdfls_reqfile(handle, dirname, filename, devname))) {
log_fatal("couldn't send file transfer request/didn't receive answer");
return (err);
}
/* check response packet */
if (response.type == p7_pt_error
&& response.code == p7_err_other) {
log_fatal("requested file doesn'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 != 0x45) {
log_fatal("didn't receive expected command");
return (p7_error_unknown);
}
/* get data */
if ((err = p7_get_buffer(handle, buffer, response.filesize, 0, disp)))
return (err);
/* check if last answer was roleswap */
if (response.type != p7_pt_roleswp)
return (p7_error_unknown);
/* no error! */
return (0);
}
#ifndef P7_DISABLED_FILE
/**
* p7_reqfile:
* Request a FILE.
*
* @arg handle the handle.
* @arg file the FILE.
* @arg dirname the directory name
* @arg filename the file name
* @arg devname the storage device name
* @arg disp the display callback
* @return the error code (0 if ok).
*/
int p7_reqfile(p7_handle_t *handle, FILE *file,
const char *dirname, const char *filename, const char *devname,
p7_disp_t disp)
{
chk_iswrite(file);
p7_buffer_t buffer = {
.cookie = file,
.write = p7_filebuffer_write
};
return (p7_request(handle, &buffer, dirname, filename, devname, disp));
}
#endif