1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
/* horst - Highly Optimized Radio Scanning Tool
*
* Copyright (C) 2005-2014 Bruno Randolf (br1@einfach.org)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <err.h>
#include "main.h"
#include "channel.h"
#include "control.h"
#define MAX_CMD 255
/* FIFO (named pipe) */
int ctlpipe = -1;
void
control_init_pipe(void)
{
mkfifo(conf.control_pipe, 0666);
ctlpipe = open(conf.control_pipe, O_RDWR|O_NONBLOCK);
}
void
control_send_command(char* cmd)
{
int len = strlen(cmd);
char new[len+1];
char* pos;
while (access(conf.control_pipe, F_OK) < 0) {
printf("Waiting for control pipe...\n");
sleep(1);
}
ctlpipe = open(conf.control_pipe, O_WRONLY);
if (ctlpipe < 0)
err(1, "Could not open control socket '%s'", conf.control_pipe);
/* always terminate command with newline */
strncpy(new, cmd, len);
new[len] = '\n';
new[len+1] = '\0';
/* replace : with newline */
while ((pos = strchr(new, ':')) != NULL) {
*pos = '\n';
}
printf("Sending command: %s\n", new);
write(ctlpipe, new, len+1);
close(ctlpipe);
}
static void
parse_command(char* in) {
char* cmd;
char* val;
int n;
cmd = strsep(&in, "=");
val = in;
//printlog("RECV CMD %s VAL %s", cmd, val);
/* commands without value */
if (strcmp(cmd, "pause") == 0) {
main_pause(1);
}
else if (strcmp(cmd, "resume") == 0) {
main_pause(0);
}
/* all other commands require a value */
if (val == NULL)
return;
else if (strcmp(cmd, "channel") == 0) {
n = atoi(val);
printlog("- CHANNEL = %d", n);
conf.do_change_channel = 0;
channel_change(channel_find_index_from_chan(n));
}
else if (strcmp(cmd, "channel_auto") == 0) {
n = (strcmp(val, "1") == 0);
printlog("- CHANNEL AUTO = %d", n);
conf.do_change_channel = n;
}
else if (strcmp(cmd, "channel_dwell") == 0) {
n = atoi(val);
printlog("- CHANNEL DWELL = %d", n);
conf.channel_time = n*1000;
}
else if (strcmp(cmd, "channel_upper") == 0) {
n = atoi(val);
printlog("- CHANNEL MAX = %d", n);
conf.channel_max = n;
}
else if (strcmp(cmd, "outfile") == 0) {
dumpfile_open(val);
}
}
void
control_receive_command(void) {
char buf[MAX_CMD];
char *pos = buf;
char *end;
int len;
len = read(ctlpipe, buf, MAX_CMD);
if (len > 0) {
buf[len] = '\0';
/* we can receive multiple \n separated commands */
while ((end = strchr(pos, '\n')) != NULL) {
*end = '\0';
parse_command(pos);
pos = end + 1;
}
}
}
void
control_finish(void)
{
if (ctlpipe == -1)
return;
close(ctlpipe);
unlink(conf.control_pipe);
ctlpipe = -1;
}
|