David Rowe
Created Dec 2015
- FreeDV Beacon.
+ FreeDV Beacon.
*/
#include <assert.h>
#include <unistd.h>
#include <time.h>
#include <ctype.h>
+#include <netdb.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+#include <pthread.h>
#ifdef _WIN32
#include <windows.h>
/* globals used to communicate with async events and callback functions */
-volatile int keepRunning;
+volatile int keepRunning, runListener, recordAny;
char txtMsg[MAX_CHAR], *ptxtMsg, triggerString[MAX_CHAR];
int triggered;
float snr_est, snr_sample;
void lowerDTR(void);
void raiseRTS(void);
void lowerRTS(void);
+pthread_t start_udp_listener_thread(void);
/*--------------------------------------------------------------------------------------------------------*\
exit(1);
}
+ /* Start UDP listener thread */
+
+ start_udp_listener_thread();
/* Init for main loop ----------------------------------------------------------------------------*/
signal(SIGINT, intHandler); /* ctrl-C to exit gracefully */
keepRunning = 1;
+ recordAny = 0;
ptxtMsg = txtMsg;
triggered = 0;
logTimer = 0;
/* if triggered kick off recording of two files */
- if (triggered && !haveRecording) {
+ if ((triggered || recordAny) && !haveRecording) {
char timeStr[MAX_CHAR];
char recFileFromRadioName[MAX_CHAR], recFileDecAudioName[MAX_CHAR];
sfRecFileFromRadio = openRecFile(recFileFromRadioName, fsm);
sfRecFileDecAudio = openRecFile(recFileDecAudioName, FS8);
haveRecording = 1;
+ recordAny = 0;
tnout = 0;
}
if (logTimer >= LOG_TIMER) {
logTimer = 0;
if (verbose) {
- fprintf(stderr, "state: %-20s peak: %6d sync: %d SNR: %3.1f triggered: %d\n",
- state_str[state], peak, sync, snr_est, triggered);
+ fprintf(stderr, "state: %-20s peak: %6d sync: %d SNR: %3.1f triggered: %d recordany: %d\n",
+ state_str[state], peak, sync, snr_est, triggered, recordAny);
}
if (*statusPageFileName) {
char timeStr[MAX_CHAR];
fstatus = fopen(statusPageFileName, "wt");
if (fstatus != NULL) {
fprintf(fstatus, "<html>\n<head>\n<meta http-equiv=\"refresh\" content=\"2\">\n</head>\n<body>\n");
- fprintf(fstatus, "%s: state: %s peak: %d sync: %d SNR: %3.1f triggered: %d txtMsg: %s\n",
- timeStr, state_str[state], peak, sync, snr_est, triggered, txtMsg);
+ fprintf(fstatus, "%s: state: %s peak: %d sync: %d SNR: %3.1f triggered: %d recordany: %d txtMsg: %s\n",
+ timeStr, state_str[state], peak, sync, snr_est, triggered, recordAny, txtMsg);
fprintf(fstatus, "</body>\n</html>\n");
fclose(fstatus);
}
#endif
}
+#define BUFSIZE 2048
+#define SERVICE_PORT 21234
+
+void *udp_listener_thread(void* p) {
+ struct sockaddr_in myaddr; /* our address */
+ struct sockaddr_in remaddr; /* remote address */
+ socklen_t addrlen = sizeof(remaddr); /* length of addresses */
+ int recvlen; /* # bytes received */
+ int fd; /* our socket */
+ char txt[BUFSIZE]; /* receive buffer */
+
+ /* create a UDP socket */
+
+ if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
+ fprintf(stderr, "cannot create socket\n");
+ return 0;
+ }
+
+ /* bind the socket to any valid IP address and a specific port */
+
+ memset((char *)&myaddr, 0, sizeof(myaddr));
+ myaddr.sin_family = AF_INET;
+ myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
+ myaddr.sin_port = htons(SERVICE_PORT);
+
+ if (bind(fd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
+ fprintf(stderr, "bind failed");
+ return 0;
+ }
+
+ /* now loop, receiving data and printing what we received */
+
+ while (runListener) {
+ fprintf(stderr, "udp listener waiting on port %d - send me a command!\n", SERVICE_PORT);
+ recvlen = recvfrom(fd, txt, BUFSIZE, 0, (struct sockaddr *)&remaddr, &addrlen);
+ fprintf(stderr, "received %d bytes\n", recvlen);
+ if (recvlen > 0) {
+ txt[recvlen] = 0;
+ fprintf(stderr, "txt: %s\n", txt);
+ if (strcmp(txt, "recordany") == 0) {
+ recordAny = 1;
+ fprintf(stderr, "Next signal to sync will be recorded regardless of txt msg\n");
+ }
+ }
+ }
+
+ return NULL;
+}
+
+
+pthread_t start_udp_listener_thread(void) {
+ pthread_t athread;
+
+ runListener = 1;
+
+ if (!pthread_create(&athread, NULL, udp_listener_thread, NULL) != 0)
+ fprintf(stderr, "Can't create UDP listener thread\n");
+
+ return athread;
+}
+