Skip to content

Commit f71fb92

Browse files
committed
ols: Use a dynamic sample buffer
The number of samples to be received is pretty clear if no RLE is being used. But if RLE is being used, the number of raw samples must be counted. For now, read samples until we time out. This should work for RLE and non-RLE, but may break if the device is connected via a high-latency network connection.
1 parent 93bdc8c commit f71fb92

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

src/hardware/openbench-logic-sniffer/api.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi)
402402
return SR_ERR;
403403

404404
/* Reset all operational states. */
405-
devc->rle_count = devc->num_transfers = 0;
405+
devc->rle_count = devc->raw_sample_buf_size = 0;
406406
devc->num_samples = devc->num_bytes = 0;
407407
devc->cnt_bytes = devc->cnt_samples = devc->cnt_samples_rle = 0;
408408
memset(devc->sample, 0, 4);

src/hardware/openbench-logic-sniffer/protocol.c

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -376,37 +376,23 @@ SR_PRIV int ols_receive_data(int fd, int revents, void *cb_data)
376376
serial = sdi->conn;
377377
devc = sdi->priv;
378378

379-
if (devc->num_transfers == 0 && revents == 0) {
379+
if (devc->cnt_bytes == 0 && revents == 0) {
380380
/* Ignore timeouts as long as we haven't received anything */
381381
return TRUE;
382382
}
383383

384-
if (devc->num_transfers++ == 0) {
385-
devc->raw_sample_buf = g_try_malloc(devc->limit_samples * 4);
386-
if (!devc->raw_sample_buf) {
387-
sr_err("Sample buffer malloc failed.");
388-
return FALSE;
389-
}
390-
/* fill with 1010... for debugging */
391-
memset(devc->raw_sample_buf, 0x82, devc->limit_samples * 4);
392-
}
393-
394384
num_changroups = 0;
395385
for (i = 0x20; i > 0x02; i >>= 1) {
396386
if ((devc->capture_flags & i) == 0) {
397387
num_changroups++;
398388
}
399389
}
400390

401-
if (revents == G_IO_IN && devc->num_samples < devc->limit_samples) {
391+
if (revents == G_IO_IN) {
402392
if (serial_read_nonblocking(serial, &byte, 1) != 1)
403393
return FALSE;
404394
devc->cnt_bytes++;
405395

406-
/* Ignore it if we've read enough. */
407-
if (devc->num_samples >= devc->limit_samples)
408-
return TRUE;
409-
410396
devc->sample[devc->num_bytes++] = byte;
411397
sr_spew("Received byte 0x%.2x.", byte);
412398
if (devc->num_bytes == num_changroups) {
@@ -464,6 +450,22 @@ SR_PRIV int ols_receive_data(int fd, int revents, void *cb_data)
464450
}
465451

466452
unsigned int samples_to_write = devc->rle_count + 1;
453+
unsigned int new_sample_buf_size =
454+
4 * MAX(devc->limit_samples, devc->num_samples + samples_to_write);
455+
if (devc->raw_sample_buf_size < new_sample_buf_size) {
456+
unsigned int old_size = devc->raw_sample_buf_size;
457+
new_sample_buf_size *= 2;
458+
devc->raw_sample_buf = g_try_realloc(devc->raw_sample_buf, new_sample_buf_size);
459+
devc->raw_sample_buf_size = new_sample_buf_size;
460+
461+
if (!devc->raw_sample_buf) {
462+
sr_err("Sample buffer malloc failed.");
463+
return FALSE;
464+
}
465+
/* fill with 1010... for debugging */
466+
memset(devc->raw_sample_buf + old_size, 0x82, new_sample_buf_size - old_size);
467+
}
468+
467469
for (i = 0; i < samples_to_write; i++)
468470
memcpy(devc->raw_sample_buf + (devc->num_samples + i) * 4, devc->sample, 4);
469471

@@ -505,8 +507,7 @@ SR_PRIV int ols_receive_data(int fd, int revents, void *cb_data)
505507
packet.payload = &logic;
506508
logic.length = devc->trigger_at_smpl * 4;
507509
logic.unitsize = 4;
508-
logic.data = devc->raw_sample_buf +
509-
(devc->limit_samples - devc->num_samples) * 4;
510+
logic.data = devc->raw_sample_buf;
510511
sr_session_send(sdi, &packet);
511512
}
512513

@@ -521,11 +522,11 @@ SR_PRIV int ols_receive_data(int fd, int revents, void *cb_data)
521522
packet.payload = &logic;
522523
logic.length = (devc->num_samples - num_pre_trigger_samples) * 4;
523524
logic.unitsize = 4;
524-
logic.data = devc->raw_sample_buf + (num_pre_trigger_samples +
525-
devc->limit_samples - devc->num_samples) * 4;
525+
logic.data = devc->raw_sample_buf + num_pre_trigger_samples *4;
526526
sr_session_send(sdi, &packet);
527527

528528
g_free(devc->raw_sample_buf);
529+
devc->raw_sample_buf = 0;
529530

530531
serial_flush(serial);
531532
abort_acquisition(sdi);

src/hardware/openbench-logic-sniffer/protocol.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ struct dev_context {
114114
int trigger_at_smpl;
115115
uint16_t capture_flags;
116116

117-
unsigned int num_transfers;
118117
unsigned int num_samples;
119118
int num_bytes;
120119
int cnt_bytes;
@@ -124,6 +123,7 @@ struct dev_context {
124123
unsigned int rle_count;
125124
unsigned char sample[4];
126125
unsigned char *raw_sample_buf;
126+
unsigned int raw_sample_buf_size;
127127
};
128128

129129
SR_PRIV extern const char *ols_channel_names[];

0 commit comments

Comments
 (0)