10#include <sys/syslog.h>
12#include <sys/socket.h>
28#include "../inc/microphone.h"
30#define detectCryingPrio 3
31#define watchFlagPrio 2
33#define MSGQOBJ_NAME "/mqLocalDaemon"
34#define SHMEMOBJ_NAME "/shLocalDaemon"
47void initThread(
int priority, pthread_attr_t *pthread_attr,
struct sched_param *pthread_param);
66 syslog(LOG_INFO,
"SIGTERM received. Closing daemon...\n");
85 syslog(LOG_INFO,
"Recording will start.\n");
90 syslog(LOG_INFO,
"Recording ended.\n");
95 syslog(LOG_INFO,
"Signaling local system at PID: %d\n", local_pid);
96 kill(local_pid, SIGUSR1);
98 syslog(LOG_INFO,
"Audio process returned: %d | loudness : %.3f\n", ret, loudness);
101 syslog(LOG_ERR,
"Error when opening file audiorecord.wav.\n" );
104 syslog(LOG_ERR,
"Error when reading from file audiorecord.wav.\n" );
107 syslog(LOG_ERR,
"In processAudio()\n" );
112 syslog(LOG_ERR,
"In startRecording().\n");
127 const char msg_type[4] =
"LV-";
136 ret = mq_receive(
msgq_id, msg, 6, NULL);
139 syslog(LOG_INFO,
"Command received: %s\n", msg);
140 if (!strncmp(msg, msg_type, strlen(msg_type)))
142 syslog(LOG_INFO,
"Command valid.\n");
147 syslog(LOG_ERR,
"Command not valid.\n");
152 syslog(LOG_ERR,
"Error (%d) when receiving message.\n",errno);
162int main(
int argc,
char *args[])
169 syslog(LOG_ERR,
"mq_open() got error %d\n",
msgq_id);
188 syslog(LOG_ERR,
"%s\n",
"fork");
194 printf(
"Deamon PID: %d\n", pid);
202 syslog(LOG_ERR,
"%s\n",
"setsid");
209 syslog(LOG_ERR,
"%s\n",
"chdir");
215 close(STDOUT_FILENO);
216 close(STDERR_FILENO);
223 fd = shm_open(
SHMEMOBJ_NAME, O_RDWR, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
225 ptr = mmap(0,
sizeof(pid_t), PROT_WRITE | PROT_READ, MAP_SHARED, fd, 0);
226 if(ptr == (
void *)-1)
229 syslog(LOG_ERR,
"Error %s in mmap()\n",errno);
232 localPID = atoi((
char *)ptr);
233 sprintf(ptr,
"%d", getpid());
234 ret = munmap(0,
sizeof(pid_t));
238 syslog(LOG_ERR,
"Error %s in munmap()\n",errno);
241 syslog(LOG_INFO,
"Received local PID: %d\n",localPID);
252 pthread_attr_t thread_attr;
253 struct sched_param thread_param;
255 pthread_t detectCryingID, watchStreamFlagID;
257 pthread_attr_init(&thread_attr);
258 pthread_attr_getschedparam(&thread_attr, &thread_param);
261 anyError = pthread_create(&watchStreamFlagID, &thread_attr,
tWatchStreamFlag, NULL);
265 anyError = pthread_create(&detectCryingID, &thread_attr,
tDetectCrying, (
void *)localPID);
268 pthread_detach(watchStreamFlagID);
269 pthread_detach(detectCryingID);
274void initThread(
int priority, pthread_attr_t *pthread_attr,
struct sched_param *pthread_param)
276 int min = sched_get_priority_min (SCHED_RR);
277 int max = sched_get_priority_max (SCHED_RR);
279 if(priority < min || priority > max)
281 fprintf(stderr,
"Thread priorities not valid.\n");
282 perror(
"initThread()");
285 pthread_param->sched_priority = priority;
286 pthread_attr_setschedparam(&pthread_attr, &pthread_param);
293 fprintf(stderr,
"pthread_create() got error %d\n",status);
295 perror(
"pthread_create()");
void initThread(int priority, pthread_attr_t *pthread_attr, struct sched_param *pthread_param)
Initializes the thread parameters with defined priority.
void * tDetectCrying(void *local_pid)
Detects crying from microphone.
static void signalHandler(int signo)
Process signal handler.
void * tWatchStreamFlag(void *arg)
Watches message queue for any changes in livestream flag.
void checkErrors(int status)
Check for errors upon creating threads.
int processAudio(double *loudness)
Processes the audio to check whether the baby is crying.
int startRecording()
Records a 2 second .wav audio from microphone.