2021-05-02 00:02:49 +02:00
|
|
|
//
|
|
|
|
// Written for Computer Networks and Systems lab classes
|
|
|
|
// AUTHOR : Sergiusz Warga
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/mman.h>
|
2021-05-03 20:23:01 +02:00
|
|
|
// #include <sys/types.h>
|
2021-05-02 00:02:49 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include "CircBuffer.h"
|
2021-05-03 20:23:01 +02:00
|
|
|
#include "funs.h"
|
2021-05-02 00:02:49 +02:00
|
|
|
|
|
|
|
#define PI 3.14
|
|
|
|
#define SIG_FREQ 100
|
|
|
|
|
2021-05-03 20:23:01 +02:00
|
|
|
void handle_arguments(int argc, char *argv[]) {
|
|
|
|
if (argc != 2) {
|
|
|
|
fprintf(stderr, "Usage: %s SAMPLING_FREQUENCY\n", argv[0]);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strtol(argv[1], NULL, 10) < 400) {
|
|
|
|
fprintf(stderr, "Sampling frequency should be at least 400Hz!\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
double get_sample(int signal_frequeuncy, int sampling_frequency, unsigned int sample_no) {
|
2021-05-02 00:02:49 +02:00
|
|
|
// TODO: Just a sketch, gotta rewrite
|
2021-05-03 20:23:01 +02:00
|
|
|
double step = 2.*PI/((double)sampling_frequency/signal_frequeuncy); // Step in radians
|
2021-05-02 00:02:49 +02:00
|
|
|
return sin(sample_no*step);
|
|
|
|
}
|
|
|
|
|
2021-05-03 20:23:01 +02:00
|
|
|
_Noreturn void producer(int fd, int sampling_frequency) { // That's something new I've learned and I think that
|
|
|
|
// function specifiers are cute.
|
2021-05-02 00:02:49 +02:00
|
|
|
printf("The producer has been called\n");
|
|
|
|
|
2021-05-03 20:23:01 +02:00
|
|
|
//----- Initialize the buffer -----
|
2021-05-02 00:02:49 +02:00
|
|
|
struct CircBuffer *buffer;
|
|
|
|
buffer = mmap(NULL, sizeof(struct CircBuffer), PROT_WRITE, MAP_SHARED, fd, 0);
|
|
|
|
if (buffer == MAP_FAILED) {
|
|
|
|
fprintf(stderr, "mmap: ");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2021-05-03 20:23:01 +02:00
|
|
|
unsigned int i = 0;
|
|
|
|
int free_idx;
|
|
|
|
struct timespec delay, tic, toc;
|
2021-05-02 00:02:49 +02:00
|
|
|
delay.tv_sec = 0;
|
2021-05-03 20:23:01 +02:00
|
|
|
delay.tv_nsec = (int) (1./sampling_frequency * NSEC_PER_SEC); // 1/f in nanoseconds
|
|
|
|
clock_gettime(CLOCK_REALTIME, &tic);
|
|
|
|
clock_gettime(CLOCK_REALTIME, &toc);
|
|
|
|
printf("Starting to sampling the signal at %dHz with t_s = %ldns\n", sampling_frequency, delay.tv_nsec);
|
2021-05-02 00:02:49 +02:00
|
|
|
|
2021-05-03 20:23:01 +02:00
|
|
|
//----- Fill the buffer -----
|
2021-05-02 00:02:49 +02:00
|
|
|
while (1) {
|
2021-05-03 20:23:01 +02:00
|
|
|
clock_gettime(CLOCK_REALTIME, &toc);
|
|
|
|
free_idx = buffer->info.free_idx;
|
|
|
|
buffer->data[free_idx] = get_sample(SIG_FREQ, sampling_frequency, i);
|
|
|
|
// printf("CircBuffer[%d] = \t %lf\n", free_idx, buffer->data[free_idx]);
|
|
|
|
buffer->info.free_idx = (free_idx + 1 ) % buffer->info.size;
|
|
|
|
++i;
|
|
|
|
while (timespec_diff_nano(&toc, &tic) < delay.tv_nsec) {
|
|
|
|
// printf("%lld\n", timespec_diff_nano(&toc, &tic));
|
|
|
|
clock_gettime(CLOCK_REALTIME, &toc);
|
|
|
|
}
|
|
|
|
clock_gettime(CLOCK_REALTIME, &tic);
|
|
|
|
}
|
2021-05-02 00:02:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
2021-05-03 20:23:01 +02:00
|
|
|
//----- Arguments handling -----
|
|
|
|
handle_arguments(argc, argv);
|
|
|
|
int sampling_frequency = strtol(argv[1], NULL, 10) ;
|
2021-05-02 00:02:49 +02:00
|
|
|
|
|
|
|
//----- Create a shared memory -----
|
|
|
|
const char *memory_name = "/dsp";
|
|
|
|
int fd = shm_open(memory_name, O_CREAT | O_RDWR, 0777);
|
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr, "shm_open: ");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2021-05-03 20:23:01 +02:00
|
|
|
// printf("Shared memory file descriptor: %d\n", fd);
|
2021-05-02 00:02:49 +02:00
|
|
|
|
|
|
|
//----- Truncate memory to the buffer's size
|
|
|
|
if (ftruncate(fd, sizeof(struct CircBuffer)) < 0) {
|
|
|
|
fprintf(stderr, "ftruncate: \n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----- Initialize the buffer -----
|
|
|
|
struct CircBuffer *buffer;
|
|
|
|
buffer = (struct CircBuffer *)mmap(NULL, sizeof(struct CircBuffer), PROT_WRITE, MAP_SHARED, fd, 0);
|
|
|
|
if (buffer == MAP_FAILED) {
|
|
|
|
fprintf(stderr, "mmap: ");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
buffer->info.size = sizeof(buffer->data)/sizeof(double);
|
|
|
|
buffer->info.free_idx = 0;
|
|
|
|
|
2021-05-03 20:23:01 +02:00
|
|
|
//----- Sample the producer -----
|
|
|
|
producer(fd, sampling_frequency);
|
2021-05-02 00:02:49 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|