-
Notifications
You must be signed in to change notification settings - Fork 0
/
set_remote.ecpp
144 lines (131 loc) · 4.43 KB
/
set_remote.ecpp
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
<%pre>
#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <fstream>
#include <ctype.h>
extern "C" {
#include <yavdr/db-utils/dbset.h>
};
#include <yavdr/common.h>
using namespace std;
</%pre><& authenticate ><%args>
//Default type of args is string and we should leave it like this
//For more robustness, avoid to define integer vars in here, better cast string to int later!!!
string lirc;
string lirc_receiver_id;
string lirc_serial_port;
string lirc_option_repeat = "0";
</%args><%cpp>
int i_receiver_id = -1;
int err = 0;
string str;
string success = "false";
string msg = "Unspecified error.";
string directories[] = {"/usr/share/yavdr-remote/hwdb.d/",
"/etc/yavdr-remote/hwdb.d/"};
#define EXTENSION ".hwdb"
if (lirc == "on") {
i_receiver_id = atoi(lirc_receiver_id.c_str());
if (i_receiver_id != 0) {
ifstream ifs;
const char *names[] = {"description", "driver", "lirc_driver", "hw_default", "lircd_conf"};
int findex = 0;
int index = 0;
bool remote_found = false;
DIR *dp;
struct dirent *dirp;
vector<string> files;
for (unsigned int i = 0; i < sizeof(directories) / sizeof(directories[0]); i++) {
if ((dp = opendir(directories[i].c_str())) == NULL) {
break;
}
while ((dirp = readdir(dp)) != NULL) {
if ((strlen(dirp->d_name) > 4) &&
strcmp(dirp->d_name + strlen(dirp->d_name) - 1 - strlen(EXTENSION), EXTENSION)) {
files.push_back(directories[i] + string(dirp->d_name));
}
}
closedir(dp);
}
for (findex = 0; (findex < files.size()) && !remote_found; findex++)
{
ifs.open(files[findex].c_str(), ifstream::in);
while (ifs.good())
{
getline(ifs, str);
if (isalpha(str[0]))
{
if (index +1 == i_receiver_id )
{
string::size_type bof = 0;
string::size_type found;
int err;
remote_found = true;
//if the chosen receiver is not a serial receiver: clear serial field in the db
err = dbset("system.remote.lirc.receiver.serial_port=%s", "");
if (err) {
msg = "Error on dbset system.remote.lirc.receiver.serial_port";
break;
}
for (int i = 0;(found = str.find_first_of(";", bof)) != string::npos; i++)
{
string value = str.substr(bof, found - bof);
err = dbset("system.remote.lirc.receiver.%s=%s", names[i], value.c_str());
if (err){
msg = "Error on dbset";
break;
}
//check if this lirc receiver really depends on the serial port
if ((i == 2 && value == "lirc_dev lirc_serial") ||
(i == 1 && value == "serial"))
{
//set serial port
err = dbset("system.remote.lirc.receiver.serial_port=%s", lirc_serial_port.c_str());
if (err){
msg = "Error on dbset system.remote.lirc.receiver.serial_port";
break;
}
}
bof = found + 1;
}
err = dbset("system.remote.lirc.option.repeat=%s", lirc_option_repeat.c_str());
if (err) {
msg = "Error on system.remote.lirc.option.repeat";
break;
}
err = dbset("system.remote.lirc.active=1");
if (err) {
msg = "Error on system.remote.lirc.active";
break;
} else {
int ret = system("/usr/bin/signal-event change-remote-hardware");
success = "true";
msg = "OK. signal-event change-remoted";
}
break;
}
index++;
}
}
ifs.close();
}
} else {
msg = "Unknown receiver";
}
} else {
err = dbset("system.remote.lirc.active=0");
if (err) {
msg = "Error on system.remote.lirc.active";
} else {
int ret = system("/usr/bin/signal-event change-remote-hardware");
success = "true";
msg = "OK. signal-event change-remote-hardware";
}
}
</%cpp>{
success: <$success$>,
msg: "<$msg$>"
}