Skip to content

Instantly share code, notes, and snippets.

@kiranshila
Last active July 30, 2022 03:06
Show Gist options
  • Save kiranshila/db08f9ee501aeb09d04f5f81cc595a6f to your computer and use it in GitHub Desktop.
Save kiranshila/db08f9ee501aeb09d04f5f81cc595a6f to your computer and use it in GitHub Desktop.
#include <dada_def.h>
#include <dada_hdu.h>
#include <fcntl.h>
#include <ipcbuf.h>
#include <multilog.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
void print_arr(char *ptr, int len) {
printf("[");
for (int i = 0; i < len; i++) {
printf(" %08x,", ptr[i]);
}
printf(" ]\n");
}
int write_hdu(ipcbuf_t *buf, int eod) {
if (ipcbuf_lock_write(buf) < 0) {
printf("Could not lock HDU for writing\n");
return -1;
}
// Get the next ptr to write to
char *ptr = ipcbuf_get_next_write(buf);
// Bail if its NULL
if (ptr == NULL) {
printf("Write ptr is null\n");
return -1;
}
// Generate some nonsense
int bufsz = ipcbuf_get_bufsz(buf);
char bytes[bufsz];
int fd = open("/dev/random", O_RDONLY);
read(fd, bytes, bufsz);
// Write some nonsense
memcpy(ptr, bytes, bufsz);
// Print our nonsense
printf("Writing to ringbuffer: ");
print_arr(ptr, bufsz);
// Set EOD
if (eod)
ipcbuf_enable_eod(buf);
// Mark as filled
if (ipcbuf_mark_filled(buf, bufsz) < 0) {
printf("Could not mark buf as filled\n");
return -1;
}
// Unlock
if (ipcbuf_unlock_write(buf) < 0) {
printf("Could not lock HDU for writing\n");
return -1;
}
}
int read_hdu(ipcbuf_t *buf) {
// Lock
if (ipcbuf_lock_read(buf) < 0) {
printf("Could not lock HDU for writing\n");
return -1;
}
uint64_t readsz;
char *ptr = ipcbuf_get_next_read(buf, &readsz);
if (ptr == NULL) {
printf("Read ptr is null\n");
return -1;
}
// Read
char bytes[readsz];
memcpy(bytes, ptr, readsz);
printf("Reading from ringbuffer: ");
print_arr(bytes, readsz);
// Mark as filled
if (ipcbuf_mark_cleared(buf) < 0) {
printf("Could not mark buf as cleared\n");
return -1;
}
// Check for EOD
printf("EOD? - %d\n", ipcbuf_eod(buf));
// Unlock
if (ipcbuf_unlock_read(buf) < 0) {
printf("Could not lock HDU for writing\n");
return -1;
}
}
int main() {
int dada_key = 0xbfff;
ipcbuf_t data_block = IPCBUF_INIT;
ipcbuf_t header = IPCBUF_INIT;
// create data ring buffer - 4 blocks of 8 bytes
if (ipcbuf_create_work(&data_block, dada_key, 4, 8, 1, -1) < 0) {
printf("Could not create DADA data block\n");
return -1;
}
// create header ring buffer - 4 blocks of 8 bytes (we're not going to use it)
if (ipcbuf_create(&header, dada_key + 1, 4, 8, 1) < 0) {
printf("Could not create DADA header block\n");
return -1;
}
// Write a few
write_hdu(&data_block,0);
write_hdu(&data_block,0);
write_hdu(&data_block,1);
write_hdu(&data_block,0);
// Read a few
read_hdu(&data_block);
read_hdu(&data_block);
read_hdu(&data_block);
read_hdu(&data_block);
// Teardown
ipcbuf_destroy(&data_block);
ipcbuf_destroy(&header);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment