98 lines
2.9 KiB
C
98 lines
2.9 KiB
C
/* *****************************************************************************
|
|
* storage/list.c -- list the elements 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_list:
|
|
* List files.
|
|
*
|
|
* @arg handle the libp7 handle
|
|
* @arg devname the device name
|
|
* @arg callback callback (directory, filename, filesize)
|
|
* @arg cookie something to send.
|
|
* @return if it worked
|
|
*/
|
|
|
|
int p7_list(p7_handle_t *handle, const char *devname,
|
|
void (*callback)(void*, const char*, const char*, p7uint_t),
|
|
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_cmdfls_reqallinfo(handle, devname))) {
|
|
log_fatal("couldn't send file transfer request/didn't receive answer");
|
|
return (err);
|
|
} else if (response.type == p7_pt_error
|
|
&& response.code == p7_err_other) {
|
|
log_fatal("invalid filesystem");
|
|
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);
|
|
}
|
|
|
|
/* - Note: we are now in passive mode - */
|
|
while (1) {
|
|
/* check answer */
|
|
char *dir, *filename; unsigned int fs;
|
|
switch (response.type) {
|
|
/* - if is roleswap, we have finished our job here - */
|
|
case p7_pt_roleswp:
|
|
return (0);
|
|
|
|
/* - if is command, should be another file info - */
|
|
case p7_pt_cmd:
|
|
/* check args */
|
|
dir = response.args[0];
|
|
filename = response.args[1];
|
|
fs = response.filesize;
|
|
/* device root should not be sent */
|
|
if (!dir && !filename) continue;
|
|
/* call callback */
|
|
(*callback)(cookie, dir, filename, fs);
|
|
break;
|
|
|
|
default:
|
|
/* wtf?! */
|
|
log_fatal("wtf?!");
|
|
return (p7_error_unknown);
|
|
}
|
|
|
|
/* send ack to continue */
|
|
log_info("sending ack to continue");
|
|
if ((err = p7_send_ack(handle, 1))) {
|
|
log_fatal("unable to send ack/receive answer!");
|
|
return (err);
|
|
}
|
|
}
|
|
}
|