Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 64 additions & 25 deletions tools/probes/probes_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@
#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 */

struct wave_files {
FILE *fd;
uint32_t buffer_id;
uint32_t fmt;
uint32_t size;
struct wave header;
};
Expand Down Expand Up @@ -83,27 +84,43 @@ 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;
}

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;

i = get_buffer_file(files, 0);
i = get_buffer_file_free(files);
if (i == -1) {
fprintf(stderr, "error: too many buffers\n");
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) {
Expand All @@ -113,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;
Expand Down Expand Up @@ -142,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);
Expand Down Expand Up @@ -176,33 +200,44 @@ 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);
if (!temp_packet)
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return *((uint32_t *)buf) == PROBE_EXTRACT_SYNC_WORD; or even...

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;
Expand All @@ -223,16 +258,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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

love C. I guess here both memset(&data...) and memset(data...) would work and mean the same

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lyakh Well, true, but this isn't really part of my PR. If you change the style, you should change across the file and better done in a separate patch.

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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here - and subjectively fread(data,...) seems clearer to me, but it's just me


/* 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);
Expand All @@ -241,9 +277,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 */
Expand All @@ -257,7 +292,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;
}
Expand Down Expand Up @@ -286,11 +321,15 @@ void parse_data(char *file_in)
packet->buffer_id,
packet->format);

fwrite(packet->data,
sizeof(uint32_t),
packet->data_size_bytes /
sizeof(uint32_t),
files[file].fd);
if (file < 0) {
fprintf(stderr,
"unable to open file for %u\n",
packet->buffer_id);
goto err;
}

fwrite(packet->data, 1,
packet->data_size_bytes, files[file].fd);

files[file].size += packet->data_size_bytes;
}
Expand Down