diff --git a/include/param/param.h b/include/param/param.h index fee97101..5e0d6e61 100644 --- a/include/param/param.h +++ b/include/param/param.h @@ -10,6 +10,8 @@ #include #include +#include + #include "libparam.h" diff --git a/include/param/param_error.h b/include/param/param_error.h new file mode 100644 index 00000000..428fb841 --- /dev/null +++ b/include/param/param_error.h @@ -0,0 +1,7 @@ +#pragma once + +#define PARAM_ERR_NONE 0 /**< No error */ +#define PARAM_ERR_NOMEM -400 /**< Not enough memory */ +#define PARAM_ERR_INVAL -401 /**< Invalid argument */ +#define PARAM_ERR_NOTSUP -402 /**< Operation not supported */ + diff --git a/include/vmem/vmem_client.h b/include/vmem/vmem_client.h index fc9c1fc6..698fd422 100644 --- a/include/vmem/vmem_client.h +++ b/include/vmem/vmem_client.h @@ -3,8 +3,41 @@ #include #include -int vmem_download(int node, int timeout, uint64_t address, uint32_t length, char * dataout, int version, int use_rdp); -int vmem_upload(int node, int timeout, uint64_t address, char * datain, uint32_t length, int version); +/** + * @brief Download data from remote vmem server at vmem address + * + * Use this function for downloading data from remote node. + * + * @param node The remote node to upload to + * @param timeout Timeout for download + * @param address VMEM address to download from on remote + * @param dataout Pointer to data buffer + * @param length The length in bytes to download into dataout buffer + * @param use_rdp Set flag to use RDP connection + * @param version Protocol version to use be used. Use 2 for 64-bit VMEM address otherwise 32-bit is assumed + * @param verbosity Set verbosity level: 0 for silent, 1 for final summary, 2 = for detailed progress + * @return On success, the total number of bytes successfully uploaded + * @return On error, a negative value is returned to indicate the specific error + */ +ssize_t vmem_download(uint16_t node, uint32_t timeout, uint64_t address, char * dataout, uint32_t length, int use_rdp, int version, int verbosity); + +/** + * @brief Upload data to remote vmem server at vmem address + * + * Use this function for uploading data to remote node. + * + * @param node The remote node to upload to + * @param timeout Timeout for upload + * @param address VMEM address to upload to on remote + * @param datain Pointer to data to upload + * @param length The length in bytes of the data passed in + * @param use_rdp Set flag to use RDP connection + * @param version Protocol version to use be used. Use 2 for 64-bit VMEM address otherwise 32-bit is assumed + * @param verbosity Set verbosity level: 0 for silent, 1 for final summary, 2 = for detailed progress + * @return On success, the total number of bytes successfully downloaded + * @return On error, a negative value is returned to indicate the specific error + */ +ssize_t vmem_upload(uint16_t node, uint32_t timeout, uint64_t address, const char * datain, uint32_t size, int use_rdp, int version, int verbosity); void vmem_client_list(int node, int timeout, int version); int vmem_client_find(int node, int timeout, void * dataout, int version, char * name, int namelen); int vmem_client_backup(int node, int vmem_id, int timeout, int backup_or_restore); diff --git a/src/vmem/vmem_client.c b/src/vmem/vmem_client.c index f556b33d..cc3d455d 100644 --- a/src/vmem/vmem_client.c +++ b/src/vmem/vmem_client.c @@ -5,12 +5,15 @@ * Author: johan */ +#include #include #include #include #include #include +#include +#include "csp/csp_error.h" static int abort = 0; @@ -18,8 +21,15 @@ void vmem_client_abort(void) { abort = 1; } -int vmem_download(int node, int timeout, uint64_t address, uint32_t length, char * dataout, int version, int use_rdp) -{ +ssize_t vmem_download(uint16_t node, uint32_t timeout, uint64_t address, char * dataout, uint32_t length, int use_rdp, int version, int verbosity) { + + if((address > UINT32_MAX) && version != 2){ + if(verbosity > 0){ + printf(" Error: Address out of range 64-bit addresses require version 2\n"); + } + return PARAM_ERR_INVAL; + } + uint32_t time_begin = csp_get_ms(); abort = 0; @@ -29,12 +39,16 @@ int vmem_download(int node, int timeout, uint64_t address, uint32_t length, char opts |= CSP_O_RDP; } csp_conn_t * conn = csp_connect(CSP_PRIO_HIGH, node, VMEM_PORT_SERVER, timeout, opts); - if (conn == NULL) - return -1; + if (conn == NULL){ + if(verbosity > 0) { + printf(" Connection could not be established\n"); + } + return CSP_ERR_TIMEDOUT; + } csp_packet_t * packet = csp_buffer_get(sizeof(vmem_request_t)); if (packet == NULL) - return -1; + return CSP_ERR_NOMEM; vmem_request_t * request = (void *) packet->data; request->version = version; @@ -78,13 +92,15 @@ int vmem_download(int node, int timeout, uint64_t address, uint32_t length, char break; } - if (dotcount % 32 == 0) - printf(" "); - printf("."); - fflush(stdout); - dotcount++; - if (dotcount % 32 == 0) - printf(" - %.0f K\n", (count / 1024.0)); + if(verbosity > 1) { + if (dotcount % 32 == 0) + printf(" "); + printf("."); + fflush(stdout); + dotcount++; + if (dotcount % 32 == 0) + printf(" - %.0f K\n", (count / 1024.0)); + } /* Put data */ memcpy((void *) ((intptr_t) dataout + count), packet->data, packet->length); @@ -95,33 +111,51 @@ int vmem_download(int node, int timeout, uint64_t address, uint32_t length, char csp_buffer_free(packet); } - printf(" - %.0f K\n", (count / 1024.0)); + if(verbosity > 1) { + printf(" - %.0f K\n", (count / 1024.0)); + } csp_close(conn); uint32_t time_total = csp_get_ms() - time_begin; - printf(" Downloaded %u bytes in %.03f s at %u Bps\n", (unsigned int) count, time_total / 1000.0, (unsigned int) (count / ((float)time_total / 1000.0)) ); + if(verbosity > 0) { + printf(" Downloaded %u bytes in %.03f s at %u Bps\n", (unsigned int) count, time_total / 1000.0, (unsigned int) (count / ((float)time_total / 1000.0)) ); + } return count; } -int vmem_upload(int node, int timeout, uint64_t address, char * datain, uint32_t length, int version) { +ssize_t vmem_upload(uint16_t node, uint32_t timeout, uint64_t address, const char * datain, uint32_t length, int use_rdp, int version, int verbosity) { + + if((address > UINT32_MAX) && version != 2){ + if(verbosity > 0){ + printf(" Error: Address out of range 64-bit addresses require version 2\n"); + } + return PARAM_ERR_INVAL; + } uint32_t time_begin = csp_get_ms(); abort = 0; /* Establish RDP connection */ - csp_conn_t * conn = csp_connect(CSP_PRIO_HIGH, node, VMEM_PORT_SERVER, timeout, CSP_O_RDP | CSP_O_CRC32); + uint32_t opts = CSP_O_CRC32; + if (use_rdp) { + opts |= CSP_O_RDP; + } + csp_conn_t * conn = csp_connect(CSP_PRIO_HIGH, node, VMEM_PORT_SERVER, timeout, opts); if (conn == NULL) { - printf("Connection could not be established\n"); - return -1; + if(verbosity > 0) { + printf(" Connection could not be established\n"); + } + return CSP_ERR_TIMEDOUT; } - csp_packet_t * packet = csp_buffer_get(sizeof(vmem_request_t)); - if (packet == NULL) - return -1; + csp_packet_t * packet = csp_buffer_get(0); + if (packet == NULL) { + return CSP_ERR_NOMEM; + } vmem_request_t * request = (void *) packet->data; request->version = version; @@ -144,20 +178,24 @@ int vmem_upload(int node, int timeout, uint64_t address, char * datain, uint32_t while((count < length) && csp_conn_is_active(conn)) { if (abort) { - csp_buffer_free(packet); break; } - if (dotcount % 32 == 0) - printf(" "); - printf("."); - fflush(stdout); - dotcount++; - if (dotcount % 32 == 0) - printf(" - %.0f K\n", (count / 1024.0)); + if(verbosity > 1) { + if (dotcount % 32 == 0) + printf(" "); + printf("."); + fflush(stdout); + dotcount++; + if (dotcount % 32 == 0) + printf(" - %.0f K\n", (count / 1024.0)); + } /* Prepare packet */ - csp_packet_t * packet = csp_buffer_get(VMEM_SERVER_MTU); + csp_packet_t * packet = csp_buffer_get(0); + if(packet == NULL) { + break; + } packet->length = VMEM_MIN(VMEM_SERVER_MTU, length - count); /* Copy data */ @@ -170,18 +208,15 @@ int vmem_upload(int node, int timeout, uint64_t address, char * datain, uint32_t } - printf(" - %.0f K\n", (count / 1024.0)); + if(verbosity > 1) { + printf(" - %.0f K\n", (count / 1024.0)); + } csp_close(conn); uint32_t time_total = csp_get_ms() - time_begin; - if(count != length){ - unsigned int window_size = 0; - csp_rdp_get_opt(&window_size, NULL, NULL, NULL, NULL, NULL); - printf("Upload didn't complete, suggested offset to resume: %"PRIu32"\n", count - ((window_size + 1) * VMEM_SERVER_MTU)); - return -1; - } else { + if(verbosity > 0) { printf(" Uploaded %"PRIu32" bytes in %.03f s at %"PRIu32" Bps\n", count, time_total / 1000.0, (uint32_t)(count / ((float)time_total / 1000.0)) ); } @@ -350,6 +385,11 @@ int vmem_client_calc_crc32(int node, int timeout, uint64_t address, uint32_t len int res = -1; + if((address > UINT32_MAX) && version != 2){ + printf(" Error: 64 bit address only supported in version 2\n"); + return res; + } + uint32_t time_begin = csp_get_ms(); /* Establish connection */ @@ -395,4 +435,4 @@ int vmem_client_calc_crc32(int node, int timeout, uint64_t address, uint32_t len csp_close(conn); return res; -} \ No newline at end of file +}