Clean up opera cake range parsing code and improve error handling

This commit is contained in:
Dominic Spill
2018-10-03 11:58:15 -06:00
parent 31b83ef42b
commit 4c2146aa7a

View File

@ -72,54 +72,64 @@ int parse_uint16(char* const s, uint16_t* const value) {
} }
} }
int parse_u16_range(char* s, hackrf_oc_range* range) { int parse_port(char* str, uint8_t* port) {
int result = 0; uint16_t tmp_port;
uint16_t tmp_port, port = 0; int result;
if(str[0] == 'A' || str[0] == 'B') {
// The port was specified as a side and number eg. A1 or B3
result = parse_uint16(str+1, &tmp_port);
if (result != HACKRF_SUCCESS)
return result;
if(tmp_port >= 5 || tmp_port <= 0) {
fprintf(stderr, "invalid port: %s\n", str);
return HACKRF_ERROR_INVALID_PARAM;
}
// Value was a valid port between 0-4
if(str[0] == 'A') {
// A1=0, A2=1, A3=2, A4=3
tmp_port -= 1;
} else {
// If B was specfied just add 4-1 ports
// B1=4, B2=5, B3=6, B4=7
tmp_port += 3;
}
} else {
result = parse_uint16(str, &tmp_port);
if (result != HACKRF_SUCCESS)
return result;
}
*port = tmp_port & 0xFF;
// printf("Port: %d\n", *port);
return HACKRF_SUCCESS;
}
int parse_range(char* s, hackrf_oc_range* range) {
int result;
char *sep = strchr(s, ':'); char *sep = strchr(s, ':');
if (!sep) if (!sep)
return HACKRF_ERROR_INVALID_PARAM; return HACKRF_ERROR_INVALID_PARAM;
// Replace : separator to null terminate string for strtol()
*sep = 0; *sep = 0;
sep++; // Skip past the separator
char *sep2 = strchr(sep+1, ':'); char *sep2 = strchr(sep, ':');
if (!sep2) if (!sep2)
return HACKRF_ERROR_INVALID_PARAM; return HACKRF_ERROR_INVALID_PARAM;
// Replace : separator to null terminate string for strtol()
*sep2 = 0; *sep2 = 0;
sep2++; // Skip past the separator
result = parse_uint16(s, &range->freq_min); result = parse_uint16(s, &range->freq_min);
if (result != HACKRF_SUCCESS) if (result != HACKRF_SUCCESS)
return result; return result;
result = parse_uint16(sep + 1, &range->freq_max); result = parse_uint16(sep, &range->freq_max);
if (result != HACKRF_SUCCESS) if (result != HACKRF_SUCCESS)
return result; return result;
result = parse_port(sep2, &(range->port));
sep2++; // Skip past the ':'
if(sep2[0] == 'A' || sep2[0] == 'B') {
// The port was specified as a side and number eg. A1 or B3
if(sep2[1] > 0x30 && sep2[1] < 0x35) {
tmp_port = sep2[1] - 0x30;
if(tmp_port >= 5 || tmp_port <= 0)
return HACKRF_ERROR_INVALID_PARAM;
// Value was a valid port between 0-4
if(sep2[0] == 'A') {
// A1=0, A2=1, A3=2, A4=3
port = (uint16_t) tmp_port-1;
} else {
// If B was specfied just add 4-1 ports
// B1=4, B2=5, B3=6, B4=7
port = (uint16_t) tmp_port+3;
}
//printf("Setting port %c%c to port %d\n", sep2[0], sep2[1], (uint16_t)port);
}
} else {
result = parse_uint16(sep2, &port);
if (result != HACKRF_SUCCESS)
return result; return result;
}
range->port = port & 0xFF;
return HACKRF_SUCCESS;
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {
@ -156,7 +166,18 @@ int main(int argc, char** argv) {
break; break;
case 'f': case 'f':
result = parse_u16_range(optarg, &ranges[range_idx]); if(MAX_FREQ_RANGES == range_idx) {
fprintf(stderr,
"argument error: specify a maximum of %u frequency ranges.\n",
MAX_FREQ_RANGES);
usage();
return EXIT_FAILURE;
}
result = parse_range(optarg, &ranges[range_idx]);
if (result != HACKRF_SUCCESS) {
fprintf(stderr, "failed to parse range\n");
return EXIT_FAILURE;
}
if(ranges[range_idx].freq_min >= ranges[range_idx].freq_max) { if(ranges[range_idx].freq_min >= ranges[range_idx].freq_max) {
fprintf(stderr, fprintf(stderr,
"argument error: freq_max must be greater than freq_min.\n"); "argument error: freq_max must be greater than freq_min.\n");
@ -170,13 +191,7 @@ int main(int argc, char** argv) {
usage(); usage();
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if(MAX_FREQ_RANGES <= range_idx++) { range_idx++;
fprintf(stderr,
"argument error: specify a maximum of %u frequency ranges.\n",
MAX_FREQ_RANGES);
usage();
return EXIT_FAILURE;
}
break; break;
case 'a': case 'a':
@ -243,7 +258,7 @@ int main(int argc, char** argv) {
} }
if(range_idx) { if(range_idx) {
uint8_t range_bytes[MAX_FREQ_RANGES * 5]; uint8_t range_bytes[MAX_FREQ_RANGES * sizeof(hackrf_oc_range)];
uint8_t ptr; uint8_t ptr;
for(i=0; i<range_idx; i++) { for(i=0; i<range_idx; i++) {
ptr = 5*i; ptr = 5*i;