From dc9f55dffe5d4f12913d3cd075826292dd3c34c1 Mon Sep 17 00:00:00 2001 From: Kai Vehmanen Date: Thu, 11 Aug 2022 15:48:41 +0300 Subject: [PATCH 1/4] probe-app: fix handling of buffer-id of zero Don't assume buffer-id of zero is an invalid value. Signed-off-by: Kai Vehmanen --- tools/probes/probes_main.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/tools/probes/probes_main.c b/tools/probes/probes_main.c index 6bb35ce943ff..cd394f68986d 100644 --- a/tools/probes/probes_main.c +++ b/tools/probes/probes_main.c @@ -83,7 +83,18 @@ int get_buffer_file(struct wave_files *files, uint32_t buffer_id) int i; for (i = 0; i < FILES_LIMIT; i++) { - if (files[i].buffer_id == buffer_id) + if (files[i].fd != NULL && files[i].buffer_id == buffer_id) + return i; + } + return -1; +} + +int get_buffer_file_free(struct wave_files *files) +{ + int i; + + for (i = 0; i < FILES_LIMIT; i++) { + if (files[i].fd == NULL) return i; } return -1; @@ -94,7 +105,7 @@ int init_wave(struct wave_files *files, uint32_t buffer_id, uint32_t format) char path[FILE_PATH_LIMIT]; int i; - i = get_buffer_file(files, 0); + i = get_buffer_file_free(files); if (i == -1) { fprintf(stderr, "error: too many buffers\n"); exit(0); From a8867fd3bae90d3db99a3f0835026153547c53ac Mon Sep 17 00:00:00 2001 From: Kai Vehmanen Date: Thu, 11 Aug 2022 16:24:48 +0300 Subject: [PATCH 2/4] probe-app: bail out if opening file fails Do not blindly call fwrite without checking whether file open succeeded or not. Signed-off-by: Kai Vehmanen --- tools/probes/probes_main.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tools/probes/probes_main.c b/tools/probes/probes_main.c index cd394f68986d..d1f8c3d36dca 100644 --- a/tools/probes/probes_main.c +++ b/tools/probes/probes_main.c @@ -297,6 +297,13 @@ void parse_data(char *file_in) packet->buffer_id, packet->format); + if (file < 0) { + fprintf(stderr, + "unable to open file for %u\n", + packet->buffer_id); + goto err; + } + fwrite(packet->data, sizeof(uint32_t), packet->data_size_bytes / From 0e0d747514abcd42081a07d11537571e7c3e270e Mon Sep 17 00:00:00 2001 From: Kai Vehmanen Date: Fri, 12 Aug 2022 10:04:11 +0300 Subject: [PATCH 3/4] probe-app: support non-word aligned probe data streams Modify code to correctly handle probe streams where sync word can occur at any byte boundary, and where probe packet size may not be aligned to 32bit word size. Signed-off-by: Kai Vehmanen --- tools/probes/probes_main.c | 48 ++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/tools/probes/probes_main.c b/tools/probes/probes_main.c index d1f8c3d36dca..684563afd893 100644 --- a/tools/probes/probes_main.c +++ b/tools/probes/probes_main.c @@ -31,7 +31,7 @@ #define APP_NAME "sof-probes" #define PACKET_MAX_SIZE 4096 /**< Size limit for probe data packet */ -#define DATA_READ_LIMIT 1024 /**< Data limit for file read */ +#define DATA_READ_LIMIT 4096 /**< Data limit for file read */ #define FILES_LIMIT 32 /**< Maximum num of probe output files */ #define FILE_PATH_LIMIT 128 /**< Path limit for probe output files */ @@ -187,13 +187,13 @@ int validate_data_packet(struct probe_data_packet *data_packet) } } -int process_sync(struct probe_data_packet *packet, uint32_t **w_ptr, uint32_t *total_data_to_copy) +int process_sync(struct probe_data_packet *packet, uint8_t **w_ptr, uint32_t *total_data_to_copy) { struct probe_data_packet *temp_packet; /* request to copy data_size from probe packet */ - *total_data_to_copy = packet->data_size_bytes / - sizeof(uint32_t); + *total_data_to_copy = packet->data_size_bytes; + if (packet->data_size_bytes > PACKET_MAX_SIZE) { temp_packet = realloc(packet, sizeof(struct probe_data_packet) + packet->data_size_bytes); @@ -201,19 +201,30 @@ int process_sync(struct probe_data_packet *packet, uint32_t **w_ptr, uint32_t *t return -ENOMEM; } - *w_ptr = (uint32_t *)&packet->data; + *w_ptr = (uint8_t *)&packet->data; return 0; } +static bool sync_word_at(uint8_t *buf, size_t len) +{ + if (len < sizeof(uint32_t)) + return false; + + if (*((uint32_t *)buf) == PROBE_EXTRACT_SYNC_WORD) + return true; + + return false; +} + void parse_data(char *file_in) { FILE *fd_in; struct wave_files files[FILES_LIMIT]; struct probe_data_packet *packet; - uint32_t data[DATA_READ_LIMIT]; + uint8_t data[DATA_READ_LIMIT]; uint32_t total_data_to_copy = 0; uint32_t data_to_copy = 0; - uint32_t *w_ptr; + uint8_t *w_ptr; int i, j, file; enum p_state state = READY; @@ -234,16 +245,17 @@ void parse_data(char *file_in) fclose(fd_in); exit(0); } - memset(&data, 0, sizeof(uint32_t) * DATA_READ_LIMIT); + memset(&data, 0, DATA_READ_LIMIT); memset(&files, 0, sizeof(struct wave_files) * FILES_LIMIT); /* data read loop to process DATA_READ_LIMIT bytes at each iteration */ do { - i = fread(&data, sizeof(uint32_t), DATA_READ_LIMIT, fd_in); + i = fread(&data, 1, DATA_READ_LIMIT, fd_in); + /* processing all loaded bytes */ for (j = 0; j < i; j++) { - /* SYNC received */ - if (data[j] == PROBE_EXTRACT_SYNC_WORD) { + /* check for SYNC */ + if (sync_word_at(&data[j], i - j)) { if (state != READY) { fprintf(stderr, "error: wrong state %d, err %d\n", state, errno); @@ -252,9 +264,8 @@ void parse_data(char *file_in) } memset(packet, 0, PACKET_MAX_SIZE); /* request to copy full data packet */ - total_data_to_copy = sizeof(struct probe_data_packet) / - sizeof(uint32_t); - w_ptr = (uint32_t *)packet; + total_data_to_copy = sizeof(struct probe_data_packet); + w_ptr = (uint8_t *)packet; state = SYNC; } /* data copying section */ @@ -268,7 +279,7 @@ void parse_data(char *file_in) data_to_copy = total_data_to_copy; total_data_to_copy = 0; } - memcpy(w_ptr, data + j, data_to_copy * sizeof(uint32_t)); + memcpy(w_ptr, data + j, data_to_copy); w_ptr += data_to_copy; j += data_to_copy - 1; } @@ -304,11 +315,8 @@ void parse_data(char *file_in) goto err; } - fwrite(packet->data, - sizeof(uint32_t), - packet->data_size_bytes / - sizeof(uint32_t), - files[file].fd); + fwrite(packet->data, 1, + packet->data_size_bytes, files[file].fd); files[file].size += packet->data_size_bytes; } From e7ab5c1fe33860ff44b51094493b8849e8e2f5af Mon Sep 17 00:00:00 2001 From: Kai Vehmanen Date: Thu, 11 Aug 2022 16:26:22 +0300 Subject: [PATCH 4/4] probe-app: add support for non-audio probe buffers Identify non-audio probe streams and write them out with ".bin" extension and without the RIFF WAVE header. Signed-off-by: Kai Vehmanen --- tools/probes/probes_main.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/tools/probes/probes_main.c b/tools/probes/probes_main.c index 684563afd893..4114f5d86be0 100644 --- a/tools/probes/probes_main.c +++ b/tools/probes/probes_main.c @@ -38,6 +38,7 @@ struct wave_files { FILE *fd; uint32_t buffer_id; + uint32_t fmt; uint32_t size; struct wave header; }; @@ -100,8 +101,14 @@ int get_buffer_file_free(struct wave_files *files) return -1; } +bool is_audio_format(uint32_t format) +{ + return (format & PROBE_MASK_FMT_TYPE) != 0 && (format & PROBE_MASK_AUDIO_FMT) == 0; +} + int init_wave(struct wave_files *files, uint32_t buffer_id, uint32_t format) { + bool audio = is_audio_format(format); char path[FILE_PATH_LIMIT]; int i; @@ -111,10 +118,9 @@ int init_wave(struct wave_files *files, uint32_t buffer_id, uint32_t format) exit(0); } - fprintf(stdout, "%s:\t Creating wave file for buffer id: %d\n", - APP_NAME, buffer_id); + sprintf(path, "buffer_%d.%s", buffer_id, audio ? "wav" : "bin"); - sprintf(path, "buffer_%d.wav", buffer_id); + fprintf(stdout, "%s:\t Creating file %s\n", APP_NAME, path); files[i].fd = fopen(path, "wb"); if (!files[i].fd) { @@ -124,6 +130,10 @@ int init_wave(struct wave_files *files, uint32_t buffer_id, uint32_t format) } files[i].buffer_id = buffer_id; + files[i].fmt = format; + + if (!audio) + return i; files[i].header.riff.chunk_id = HEADER_RIFF; files[i].header.riff.format = HEADER_WAVE; @@ -153,6 +163,9 @@ void finalize_wave_files(struct wave_files *files) /* and close all opened files */ /* check wave struct to understand the offsets */ for (i = 0; i < FILES_LIMIT; i++) { + if (!is_audio_format(files[i].fmt)) + continue; + if (files[i].fd) { chunk_size = files[i].size + sizeof(struct wave) - offsetof(struct riff_chunk, format);