-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathcontrol.c
133 lines (110 loc) · 2.88 KB
/
control.c
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
/* horst - Highly Optimized Radio Scanning Tool
*
* Copyright (C) 2005-2016 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 <uwifi/log.h>
#include "main.h"
#include "control.h"
#include "conf_options.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(const char* cmd)
{
int len = strlen(cmd);
char new[len + 2];
char* pos;
if (conf.control_pipe[0] == '\0') {
strncpy(conf.control_pipe, DEFAULT_CONTROL_PIPE, MAX_CONF_VALUE_STRLEN);
conf.control_pipe[MAX_CONF_VALUE_STRLEN] = '\0';
}
while (access(conf.control_pipe, F_OK) < 0) {
LOG_INF("Waiting for control pipe '%s'...", conf.control_pipe);
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';
}
LOG_INF("Sending command: %s", new);
write(ctlpipe, new, len+1);
close(ctlpipe);
}
static void parse_command(char* in) {
char* cmd;
char* val;
cmd = strsep(&in, "=");
val = in;
//LOG_ERR("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);
}
else if (strcmp(cmd, "reset") == 0) {
main_reset();
}
else {
/* handle the rest thru config options */
config_handle_option(0, cmd, 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;
}