improved error handling
This commit is contained in:
@ -146,6 +146,17 @@ int main(int argc, char** argv)
|
|||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (read) {
|
||||||
|
fd = fopen(path, "wb");
|
||||||
|
} else {
|
||||||
|
fd = fopen(path, "rb");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fd == NULL) {
|
||||||
|
fprintf(stderr, "Failed to open file: %s\n", path);
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
result = hackrf_init();
|
result = hackrf_init();
|
||||||
if (result != HACKRF_SUCCESS) {
|
if (result != HACKRF_SUCCESS) {
|
||||||
fprintf(stderr, "hackrf_init() failed: %s (%d)\n",
|
fprintf(stderr, "hackrf_init() failed: %s (%d)\n",
|
||||||
@ -160,47 +171,49 @@ int main(int argc, char** argv)
|
|||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (read) {
|
|
||||||
fd = fopen(path, "wb");
|
|
||||||
} else {
|
|
||||||
fd = fopen(path, "rb");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fd == NULL) {
|
|
||||||
fprintf(stderr, "Failed to open file: %s\n", path);
|
|
||||||
return EXIT_FAILURE;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (read) {
|
if (read) {
|
||||||
result = hackrf_spiflash_read(device, address, length, data);
|
result = hackrf_spiflash_read(device, address, length, data);
|
||||||
if (result == HACKRF_SUCCESS) {
|
if (result != HACKRF_SUCCESS) {
|
||||||
const ssize_t bytes_written = fwrite(data, 1, length, fd);
|
fprintf(stderr, "hackrf_spiflash_read() failed: %s (%d)\n",
|
||||||
if (bytes_written != length) {
|
hackrf_error_name(result), result);
|
||||||
fprintf(stderr, "Failed write to file (wrote %d bytes).\n",
|
fclose(fd);
|
||||||
(int)bytes_written);
|
fd = NULL;
|
||||||
fclose(fd);
|
return EXIT_FAILURE;
|
||||||
fd = NULL;
|
}
|
||||||
return EXIT_FAILURE;
|
const ssize_t bytes_written = fwrite(data, 1, length, fd);
|
||||||
}
|
if (bytes_written != length) {
|
||||||
|
fprintf(stderr, "Failed write to file (wrote %d bytes).\n",
|
||||||
|
(int)bytes_written);
|
||||||
|
fclose(fd);
|
||||||
|
fd = NULL;
|
||||||
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const ssize_t bytes_read = fread(data, 1, length, fd);
|
const ssize_t bytes_read = fread(data, 1, length, fd);
|
||||||
if (bytes_read == length) {
|
if (bytes_read != length) {
|
||||||
result = hackrf_spiflash_write(device, address, length, data);
|
|
||||||
} else {
|
|
||||||
fprintf(stderr, "Failed read file (read %d bytes).\n",
|
fprintf(stderr, "Failed read file (read %d bytes).\n",
|
||||||
(int)bytes_read);
|
(int)bytes_read);
|
||||||
fclose(fd);
|
fclose(fd);
|
||||||
fd = NULL;
|
fd = NULL;
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
result = hackrf_spiflash_write(device, address, length, data);
|
||||||
|
if (result != HACKRF_SUCCESS) {
|
||||||
|
fprintf(stderr, "hackrf_spiflash_write() failed: %s (%d)\n",
|
||||||
|
hackrf_error_name(result), result);
|
||||||
|
fclose(fd);
|
||||||
|
fd = NULL;
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
result = hackrf_close(device);
|
result = hackrf_close(device);
|
||||||
if (result != HACKRF_SUCCESS) {
|
if (result != HACKRF_SUCCESS) {
|
||||||
fprintf(stderr, "hackrf_close() failed: %s (%d)\n",
|
fprintf(stderr, "hackrf_close() failed: %s (%d)\n",
|
||||||
hackrf_error_name(result), result);
|
hackrf_error_name(result), result);
|
||||||
return -1;
|
fclose(fd);
|
||||||
|
fd = NULL;
|
||||||
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
hackrf_exit();
|
hackrf_exit();
|
||||||
|
Reference in New Issue
Block a user