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/getfreemem.c

81 lines
2.5 KiB
C

/* *****************************************************************************
* storage/getfreemem.c -- get the amount of free memory on 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_getfreemem:
* Request a device's capacity.
*
* @arg handle the libp7 handle.
* @arg devname the device name.
* @arg capacity pointer to the capacity to fill.
* @return if there was an error.
*/
int p7_getfreemem(p7_handle_t *handle, const char *devname,
p7uint_t *capacity)
{
int err;
/* make checks */
chk_handle(handle);
chk_active(handle);
/* send command packet */
log_info("sending capacity transmit request");
if ((err = p7_send_cmdfls_reqcapacity(handle, devname))) {
log_fatal("couldn't send request/didn't receive answer");
return (err);
}
/* check response packet */
if (response.type == p7_pt_error
&& response.code == p7_err_other) {
log_fatal("filesystem probably doesn't exist");
return (p7_error_unsupported_device);
} 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 != 0x4C
|| !response.args[4]) {
log_fatal("didn't receive expected command");
return (p7_error_unknown);
}
/* decode thingy */
*capacity = response.filesize;
/* ack and check for the roleswap */
log_info("return ack and wait for roleswp");
if ((err = p7_send_ack(handle, 1))) {
log_fatal("unable to send ack.");
return (err);
} else if (response.type != p7_pt_roleswp)
return (p7_error_unknown);
/* no error! */
return (0);
}