forked from keesj/saleae-logic-libusb
/
main.c
205 lines (183 loc) · 5.49 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// vim: sw=8:ts=8:noexpandtab
#include "slogic.h"
#include "usbutil.h"
#include "log.h"
#include <assert.h>
#include <libusb.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/* Command line arguments */
struct slogic_sample_rate *sample_rate = NULL;
const char *output_file_name = NULL;
size_t n_samples = 0;
const char *me = "main";
static struct logger logger = {
.name = __FILE__,
.verbose = 0,
};
void short_usage(const char *message, ...)
{
char p[1024];
va_list ap;
fprintf(stderr, "usage: %s -f <output file> -r <sample rate> [-n <number of samples>] [-h ] \n\n", me);
va_start(ap, message);
(void)vsnprintf(p, 1024, message, ap);
va_end(ap);
fprintf(stderr, "Error: %s\n", p);
}
void full_usage()
{
const struct slogic_sample_rate *sample_iterator = slogic_get_sample_rates();
fprintf(stderr, "usage: %s -f <output file> -r <sample rate> [-n <number of samples>]\n", me);
fprintf(stderr, "\n");
fprintf(stderr, " -n: Number of samples to record\n");
fprintf(stderr, " Defaults to one second of samples for the specified sample rate\n");
fprintf(stderr, " -f: The output file. Using '-' means that the bytes will be output to stdout.\n");
fprintf(stderr, " -h: This help message.\n");
fprintf(stderr, " -r: Select sample rate for the Logic.\n");
fprintf(stderr, " Available sample rates:\n");
while (sample_iterator->text != NULL) {
fprintf(stderr, " o %s\n", sample_iterator->text);
sample_iterator++;
}
fprintf(stderr, "\n");
fprintf(stderr, "Advanced options:\n");
fprintf(stderr, " -b: Transfer buffer size.\n");
fprintf(stderr, " -t: Number of transfer buffers.\n");
fprintf(stderr, " -o: Transfer timeout.\n");
fprintf(stderr, " -u: libusb debug level: 0 to 3, 3 is most verbose. Defaults to '0'.\n");
fprintf(stderr, "\n");
}
/* Returns true if everything was OK */
bool parse_args(int argc, char **argv, struct slogic_handle *handle)
{
int c;
int libusb_debug_level = 0;
char *endptr;
opterr = 0;
/* TODO: Add a -d flag to turn on internal debugging */
while ((c = getopt(argc, argv, "n:f:r:hb:t:o:u:")) != -1) {
switch (c) {
case 'n':
n_samples = strtol(optarg, &endptr, 10);
if (*endptr != '\0' || n_samples <= 0) {
short_usage("Invalid number of samples, must be a positive integer: %s", optarg);
return false;
}
break;
case 'f':
output_file_name = optarg;
break;
case 'r':
sample_rate = slogic_parse_sample_rate(optarg);
if (!sample_rate) {
short_usage("Invalid sample rate: %s", optarg);
return false;
}
break;
case 'h':
full_usage();
return false;
case 'b':
handle->transfer_buffer_size = strtol(optarg, &endptr, 10);
if (*endptr != '\0' || handle->transfer_buffer_size <= 0) {
short_usage("Invalid transfer buffer size, must be a positive integer: %s", optarg);
return false;
}
break;
case 't':
handle->n_transfer_buffers = strtol(optarg, &endptr, 10);
if (*endptr != '\0' || handle->n_transfer_buffers <= 0) {
short_usage("Invalid transfer buffer count, must be a positive integer: %s", optarg);
return false;
}
break;
case 'o':
handle->transfer_timeout = strtol(optarg, &endptr, 10);
if (*endptr != '\0' || handle->transfer_timeout <= 0) {
short_usage("Invalid transfer timeout, must be a positive integer: %s", optarg);
return false;
}
break;
case 'u':
libusb_debug_level = strtol(optarg, &endptr, 10);
if (*endptr != '\0' || libusb_debug_level < 0 || libusb_debug_level > 3) {
short_usage("Invalid libusb debug level, must be a positive integer between "
"0 and 3: %s", optarg);
return false;
}
libusb_set_debug(handle->context, libusb_debug_level);
break;
/* I'm not sure in which cases this really can happen, but optopt is not valid. */
default:
short_usage("Something is wrong. Use %s -h for usage.", c, me);
return false;
case '?':
short_usage("Unknown argument: %c. Use %s -h for usage.", optopt, me);
return false;
}
}
if (!output_file_name) {
short_usage("An output file has to be specified.", optarg);
return false;
}
if (!sample_rate) {
short_usage("A sample rate has to be specified.", optarg);
return false;
}
if (!n_samples) {
n_samples = sample_rate->samples_per_second;
}
return true;
}
int count = 0;
int sum = 0;
bool on_data_callback(uint8_t * data, size_t size, void *user_data)
{
bool more = sum < 24 * 1024 * 1024;
if (size == 0){
more = 0;
}
log_printf(&logger, DEBUG, "Got sample: size: %zu, #samples: %d, aggregate size: %d, more: %d\n", size, count,
sum, more);
count++;
sum += size;
if (size == 0){
log_printf(&logger, WARNING, "logic level buffer overun\n");
exit(EXIT_FAILURE);
}
return more;
}
int main(int argc, char **argv)
{
struct slogic_recording recording;
struct slogic_handle *handle = slogic_init();
if (!handle) {
log_printf(&logger, INFO, "Failed initialise lib slogic\n");
exit(42);
}
if (!parse_args(argc, argv, handle)) {
exit(EXIT_FAILURE);
}
if (slogic_open(handle) != 0) {
log_printf(&logger, INFO, "Failed to open the logic analyzer\n");
exit(EXIT_FAILURE);
}
if (!slogic_is_firmware_uploaded(handle)) {
log_printf(&logger, INFO, "Uploading the firmware\n");
slogic_upload_firmware(handle);
slogic_close(handle);
return 42;
}
slogic_fill_recording(&recording, sample_rate, on_data_callback, NULL);
if (slogic_execute_recording(handle, &recording)) {
slogic_close(handle);
exit(EXIT_FAILURE);
}
slogic_close(handle);
exit(EXIT_SUCCESS);
}