logging facility (log.c librarie), template, minor bug fixes, param init
This commit is contained in:
@@ -20,6 +20,7 @@
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "log.h"
|
||||
#include "rpi_artnet_utils.h"
|
||||
|
||||
/***************************************************************************************************
|
||||
@@ -33,6 +34,7 @@
|
||||
/***************************************************************************************************
|
||||
* Persistent Variables
|
||||
**************************************************************************************************/
|
||||
|
||||
/**
|
||||
* Boolean for interrupting the main thread loop
|
||||
*/
|
||||
@@ -76,7 +78,7 @@ void artnet_start() {
|
||||
isUdpListenerRunning = true;
|
||||
|
||||
if (pthread_create(&udpListener, NULL, artnet_udp_handler, NULL) < 0) {
|
||||
perror("pthread_create");
|
||||
log_error("pthread_create: %s", strerror(errno));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,23 +86,25 @@ void artnet_stop() {
|
||||
isUdpListenerRunning = false;
|
||||
|
||||
if (pthread_join(udpListener, NULL) != 0) {
|
||||
perror("pthread_join");
|
||||
log_error("pthread_join: %s", strerror(errno));
|
||||
}
|
||||
}
|
||||
|
||||
int artnet_get_dmx_data(unsigned int univerve, uint8_t *dmxData[]) {
|
||||
if (univerve > 8) {
|
||||
fprintf(stderr, "Universe %d out of bounds %d\n", univerve, 16);
|
||||
log_error("Universe %d out of bounds %d\n", univerve, 16);
|
||||
*dmxData = NULL;
|
||||
return -1;
|
||||
}
|
||||
*dmxData = artDmxBufferArray[univerve];
|
||||
log_trace("%d;%d;%d;%d", (*dmxData)[0], (*dmxData)[1], (*dmxData)[2], (*dmxData)[3]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/***************************************************************************************************
|
||||
* Internal Function Definitions
|
||||
**************************************************************************************************/
|
||||
|
||||
static void *artnet_udp_handler(void *arg) {
|
||||
int fdUdpSocket = -1;
|
||||
struct pollfd pollFdArray[1];
|
||||
@@ -113,7 +117,7 @@ static void *artnet_udp_handler(void *arg) {
|
||||
/* Create UDP socket */
|
||||
fdUdpSocket = socket(PF_INET, SOCK_DGRAM, 0);
|
||||
if (fdUdpSocket < 0) {
|
||||
perror("Opening socket failed");
|
||||
log_error("Opening socket failed: %s", strerror(errno));
|
||||
}
|
||||
|
||||
/* Set non-blocking socket */
|
||||
@@ -170,7 +174,7 @@ static void *artnet_udp_handler(void *arg) {
|
||||
}
|
||||
|
||||
} else if (pollReturn < 0) {
|
||||
fprintf(stderr, "error polling %d: %s\n", fdUdpSocket, strerror(errno));
|
||||
log_error("error polling %d: %s", fdUdpSocket, strerror(errno));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -187,7 +191,7 @@ static void artnet_send_poll_reply(int fdUdpSocket, struct sockaddr_in senderAdd
|
||||
|
||||
if (sendto(fdUdpSocket, (uint8_t *)&artPollReply, sizeof(artPollReply_t), 0,
|
||||
(struct sockaddr *)&senderAddr, sizeof(senderAddr)) < 0) {
|
||||
fprintf(stderr, "Error sending poll reply to %s:%d: %s\n", inet_ntoa(senderAddr.sin_addr),
|
||||
strerror(errno));
|
||||
log_error("%s: sending poll reply to \"%s:%d\": %s", inet_ntoa(senderAddr.sin_addr),
|
||||
senderAddr.sin_port, strerror(errno));
|
||||
}
|
||||
}
|
@@ -139,7 +139,7 @@ typedef struct {
|
||||
uint8_t Filler[26]; // Filler bytes, currently zero.
|
||||
} artPollReply_t;
|
||||
|
||||
static artPollReply_t artPollReply = {
|
||||
static artPollReply_t const artPollReply = {
|
||||
.ID = "Art-Net",
|
||||
.OpCode = OpPollReply,
|
||||
.IPAddr = {0},
|
||||
|
@@ -17,6 +17,9 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <wait.h>
|
||||
|
||||
#include "log.h"
|
||||
|
||||
/***************************************************************************************************
|
||||
* Preprocessor Constants and Macros
|
||||
@@ -65,48 +68,29 @@ uint16_t buffer[128 + 2];
|
||||
*/
|
||||
static void *fifo_to_buffer(void *arg);
|
||||
|
||||
static int start_cava_process();
|
||||
|
||||
static void stop_cava_process();
|
||||
|
||||
/***************************************************************************************************
|
||||
* External Function Definitions
|
||||
**************************************************************************************************/
|
||||
|
||||
void setup_cava() {
|
||||
if ((cavaPid = fork()) == -1) {
|
||||
perror("fork");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (cavaPid == 0) {
|
||||
/* Child process*/
|
||||
char *args[] = {"/usr/local/bin/cava", "-p", "/home/pi/LedBars/RpiLedBars/cava_config", NULL};
|
||||
|
||||
if (execv(args[0], args) != 0) {
|
||||
perror("execv");
|
||||
}
|
||||
|
||||
} else {
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
|
||||
void close_cava() {
|
||||
stop_cava_bg_worker();
|
||||
kill(cavaPid, SIGTERM);
|
||||
}
|
||||
|
||||
void start_cava_bg_worker() {
|
||||
void cava_start() {
|
||||
isFifoReaderRunning = true;
|
||||
pthread_create(&fifoReader, NULL, fifo_to_buffer, NULL);
|
||||
}
|
||||
|
||||
void stop_cava_bg_worker() {
|
||||
void cava_stop() {
|
||||
isFifoReaderRunning = false;
|
||||
if (pthread_join(fifoReader, NULL) != 0) {
|
||||
perror("pthread_join");
|
||||
log_error("pthread_join: %s", strerror(errno));
|
||||
}
|
||||
}
|
||||
|
||||
int get_cava_buffer(uint16_t **buffer_dst) {
|
||||
int cava_get_buffer(uint16_t **buffer_dst) {
|
||||
*buffer_dst = buffer;
|
||||
log_trace("%d;%d;%d;%d", buffer[0], buffer[1], buffer[2], buffer[3]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -122,12 +106,8 @@ static void *fifo_to_buffer(void *arg) {
|
||||
char strValue[6] = "0\0";
|
||||
bool hasToBeDiscarded = true;
|
||||
|
||||
if start_cava_process ()
|
||||
;
|
||||
if ((fdCavaInput = open("/tmp/cava_output", O_RDONLY | O_NONBLOCK)) < 0) {
|
||||
perror("open");
|
||||
close_cava();
|
||||
exit(1);
|
||||
if ((fdCavaInput = start_cava_process()) < 0) {
|
||||
log_error("y'a un truc qui a pas marché");
|
||||
}
|
||||
|
||||
memset(fds, 0, sizeof(fds));
|
||||
@@ -165,26 +145,27 @@ static void *fifo_to_buffer(void *arg) {
|
||||
valueIndex = 0;
|
||||
|
||||
if (valueIndex > 129) {
|
||||
fprintf(stderr, "Buffer overflow, \\n missed, discarding next\n");
|
||||
log_warn("Buffer overflow, \\n missed, discarding next");
|
||||
hasToBeDiscarded = true;
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
fprintf(stderr, "Unexpected char %d [%c]\n", current, current);
|
||||
log_warn("Unexpected char %d [%c]\n", current, current);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (errno != EAGAIN) {
|
||||
perror("read");
|
||||
log_error("read: %s", strerror(errno));
|
||||
}
|
||||
}
|
||||
} else if (ret < 0) {
|
||||
fprintf(stderr, "error polling %d: %s\n", fdCavaInput, strerror(errno));
|
||||
log_error("polling %d: %s\n", fdCavaInput, strerror(errno));
|
||||
}
|
||||
}
|
||||
|
||||
stop_cava_process();
|
||||
close(fdCavaInput);
|
||||
|
||||
return NULL;
|
||||
@@ -194,17 +175,18 @@ static int start_cava_process() {
|
||||
int fdCavaPipe[2];
|
||||
|
||||
if (pipe(fdCavaPipe) < 0) {
|
||||
fprintf(stderr, "Cava pipe failure: %s\n", strerror(errno));
|
||||
log_error("Cava pipe failure: %s", strerror(errno));
|
||||
return -EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if ((cavaPid = fork()) < 0) {
|
||||
fprintf(stderr, "Cava fork failure: %s\n", strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
log_error("Cava fork failure: %s", strerror(errno));
|
||||
exit(-EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (cavaPid == 0) {
|
||||
/* Child process*/
|
||||
int fdLogOut;
|
||||
char *args[] = {"cava", "-p", "/home/pi/LedBars/RpiLedBars/cava_config", NULL};
|
||||
|
||||
/* Close reading end of the pipe */
|
||||
@@ -214,10 +196,16 @@ static int start_cava_process() {
|
||||
/* Close writing end of the pipe */
|
||||
close(fdCavaPipe[1]);
|
||||
|
||||
/* Open / create a log file for cava */
|
||||
fdLogOut = open("/dev/null", O_WRONLY, NULL);
|
||||
/* Dup file in place of stderr */
|
||||
dup2(fdLogOut, STDERR_FILENO);
|
||||
|
||||
execvp(args[0], args);
|
||||
fprintf(stderr, "Cava execvp failure or return: %s\n", strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
log_error("Cava execvp failure or return: %s", strerror(errno));
|
||||
exit(-EXIT_FAILURE);
|
||||
} else {
|
||||
int flags;
|
||||
/* Close writing end of the pipe */
|
||||
close(fdCavaPipe[1]);
|
||||
/* Set reading end of the pipe non-blocking */
|
||||
@@ -231,4 +219,7 @@ static int start_cava_process() {
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void stop_cava_process() { kill(cavaPid, SIGTERM); }
|
||||
static void stop_cava_process() {
|
||||
kill(cavaPid, SIGTERM);
|
||||
waitpid(0, NULL, WNOHANG);
|
||||
}
|
||||
|
@@ -30,12 +30,12 @@
|
||||
**/
|
||||
void setup_cava();
|
||||
|
||||
int get_cava_buffer(uint16_t **buffer_dst);
|
||||
int cava_get_buffer(uint16_t **buffer_dst);
|
||||
|
||||
void close_cava();
|
||||
|
||||
void start_cava_bg_worker();
|
||||
void cava_start();
|
||||
|
||||
void stop_cava_bg_worker();
|
||||
void cava_stop();
|
||||
|
||||
#endif /* __RPI_CAVA_H__ */
|
Reference in New Issue
Block a user