-
Notifications
You must be signed in to change notification settings - Fork 1
/
daemon.cpp
268 lines (227 loc) · 5.44 KB
/
daemon.cpp
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include <iostream>
#include <fstream>
#include <ext/hash_map>
#include <string>
#include <unordered_map>
#include <cstring>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <sys/signal.h>
#include <sys/types.h>
#include <pty.h>
#include <time.h>
using namespace std;
using namespace __gnu_cxx;
namespace __gnu_cxx {
template<>
struct hash<std::string>
{
hash<char*> h;
size_t operator()(const std::string &s) const
{
return h(s.c_str());
};
};
}
#define BAUDRATE B9600
#define DBLINESIZE 13
#define _POSIX_SOURCE 1
#define VERSION "0.3"
std::string device;
string version = VERSION;
int fd;
ofstream logp;
struct termios oldtio;
hash_map<string, string> db;
int buf0_index = 0;
int buf1_index = 0;
int buf2_index = 0;
char buf0[12];
char buf1[12];
char buf2[12];
void signal_handler_IO(int);
void logdaemonevent(std::string);
void
signal_handler_IO(int status) {
char buf[2];
int index = 0;
int process = false;
std::string raw;
std::string cardid;
std::string ans;
memset(buf, 0, 2);
read(fd, buf, 2);
if(buf[1] == 10){
switch(buf[0]){
case 33:
cardid = (std::string)buf0;
buf0_index = 0;
process = true;
memset(buf0, 0, 12);
break;
case 34:
cardid = (std::string)buf1;
buf1_index = 0;
process = true;
memset(buf1, 0, 12);
break;
case 35:
cardid = (std::string)buf2;
buf2_index = 0;
process = true;
memset(buf2, 0, 12);
break;
}
}
else{
switch(buf[0]){
case 33:
if(buf0_index<12){
buf0[buf0_index++] = buf[1];
}
break;
case 34:
if(buf1_index<12){
buf1[buf1_index++] = buf[1];
}
break;
case 35:
if(buf2_index<12){
buf2[buf2_index++] = buf[1];
}
break;
}
}
tcflush(fd, TCIFLUSH);
if(!process) return;
if(db.find(cardid) != db.end()) ans = db[cardid];
else ans = "5";
error_condition:
if(cardid.compare("3400C2DF0B22") == 0){ ans="1"; }
cardid.insert(0,"IO HANDLER -- received: ");
cardid.insert(cardid.length(), " +++ answered with: ");
cardid += ans;
logdaemonevent(cardid);
write(fd, ans.c_str(), 1);
}
int
load() {
ifstream fp;
std::string key;
std::string value;
std::string line;
fp.open("./db_real.txt");
device = "/dev/ttyUSB0";
if (fp.is_open())
{
while (fp.good())
{
getline(fp, line);
if(line.length()<DBLINESIZE) continue;
key = line.substr(0,line.length()-1);
value = line.substr(line.length()-1, line.length());
db[key] = value;
key.insert(0, "[cardnum] - ");
key.insert(key.length(), " - [answer] -- ");
key+=value;
logdaemonevent(key);
}
fp.close();
}
}
int
opendaemonlog() {
logp.open("/var/log/rfid_daemon.log", ios::app);
if(!logp.is_open()) {
cout << "failed to open log file: /var/log/rfid_daemon.log" << endl;
exit(EXIT_FAILURE);
}
return 0;
}
int
closedaemonlog() {
logp.close();
return 0;
}
void
logdaemonevent(std::string log) {
time_t t = time(NULL);
std::string logtime = (std::string)ctime(&t);
logp << logtime.substr(0,logtime.length()-1) << " ::: " << log << endl;
}
int
dumpdatabase() {
std::string value;
std::string key;
logdaemonevent("starting dump of db");
for(hash_map<std::string, std::string>::iterator it=db.begin(); it!=db.end(); it++) {
key = (*it).first;
key.insert(key.length(), " => ");
value = (*it).second;
key+=value;
logdaemonevent(key);
}
logdaemonevent("ending dump of db");
return 0;
}
void
cleanup(int sig) {
tcsetattr(fd,TCSANOW,&oldtio);
close(fd);
closedaemonlog();
system("killall daemon");
(void) signal(SIGINT, SIG_DFL);
}
int main(){
struct termios newtio;
struct sigaction saio;
pid_t pid, sid;
sigset_t st;
(void) signal(SIGINT, cleanup);
opendaemonlog();
load();
//dumpdatabase();
pid = fork();
if(pid < 0) { logdaemonevent("failed to fork daemon event handler process."); exit(EXIT_FAILURE); }
if(pid > 0) { logdaemonevent("forked daemon event handler process."); exit(EXIT_SUCCESS); }
umask(0);
sid = setsid();
if (sid < 0) { logdaemonevent("sid of daemon event handler less than 0."); exit(EXIT_FAILURE);}
if((chdir("/")) < 0) { logdaemonevent("daemon event handler: could not chdir to /."); exit(EXIT_FAILURE);}
close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO);
fd = open(device.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK);
if(fd<0) { logdaemonevent("failed to open serial device."); perror(device.c_str()); exit(EXIT_FAILURE); }
saio.sa_handler = signal_handler_IO;
saio.sa_mask = st;
saio.sa_flags = 0;
saio.sa_restorer = NULL;
sigaction(SIGIO,&saio,NULL);
char buff[100];
sprintf(buff, "pid of event handler: %d", getpid());
logdaemonevent(buff);
fcntl(fd, F_SETOWN, getpid());
fcntl(fd, F_SETFL, FASYNC);
tcgetattr(fd,&oldtio);
newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR | ICRNL;
newtio.c_oflag = 0;
newtio.c_lflag = ICANON;
newtio.c_cc[VMIN]=1;
newtio.c_cc[VTIME]=0;
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
while (1) {
usleep(100000);
}
exit(EXIT_SUCCESS);
return 0;
}