-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
269 lines (229 loc) · 7.32 KB
/
Copy pathmain.cpp
File metadata and controls
269 lines (229 loc) · 7.32 KB
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
268
269
#include "main.hpp"
/*
* Utils block
*
* */
void app_terminate_handler(int) {
sigint_flag = 1;
}
void app_server_thread_run() {
std::cout << "Starting RpiControl server ..." << std::endl;
auto handler = new RpiControlCore;
handler->start_server();
}
constexpr unsigned int sw_str(const char* str, int h = 0) {
return !str[h] ? 5381 : (sw_str(str, h+1) * 33) ^ str[h];
}
std::string init_response(std::string header) {
std::string value = std::move(header);
value += "\n";
return value;
}
/*
* Process package helpers
*
* */
bool auth_user(const std::string &package) {
auto v = Utils::split(package, "\n");
if (v.size() < 2) return false;
auto dp = new DeviceProperties;
auto name = dp->get_device_name();
auto auth_state = dp->auth_user(v[1]);
delete dp;
return ((v[0] == name) && auth_state);
}
std::string get_device_info() {
auto dp = new DeviceProperties;
std::string res = dp->get_device_name() + "|" +
dp->get_device_model() + "|" +
dp->get_os_version() + "|" +
dp->get_temperature();
delete dp;
return res;
}
unsigned short get_sound_volume() {
auto audio = new AudioControl;
auto result = audio->get_volume();
delete audio;
return result;
}
void set_sound_volume(unsigned short value) {
auto audio = new AudioControl;
audio->set_volume(value);
delete audio;
}
std::string set_device_name(const std::string &name) {
auto dp = new DeviceProperties;
dp->set_device_name(name);
auto result = dp->get_device_name();
delete dp;
return result;
}
bool change_password(const std::string &package) {
auto v = Utils::split(package, "\\|");
if (v.size() < 2) return false;
auto dp = new DeviceProperties;
auto result = dp->set_new_pwd(v[0], v[1]);
delete dp;
return result;
}
std::string get_dir(const std::string &message) {
auto f = new FileManager;
std::vector<std::string> *data;
if (message == "home") {
data = f->get_home_dir();
} else {
data = f->get_dir(message);
}
std::string result;
for (auto &d: *data) {
result += d + "^";
}
delete f;
result = result.substr(0, result.size() - 1);
return result;
}
std::string get_file(const std::string &message) {
auto f = new FileManager;
std::string data;
auto result = f->get_file(message);
delete f;
return result;
}
std::string get_eth_connection() {
auto c = new Connectivity;
auto conn = c->get_ethernet_connection();
std::string data;
data = conn->name + "|" + conn->ip + "|" + conn->mac;
delete c;
delete conn;
return data;
}
std::string run_cmd(const std::string &message) {
auto raw = Utils::exec(message.data());
auto raw_arr = Utils::split(raw, "\\n");
std::string result;
for (auto s: raw_arr) {
result += s + "\\n";
}
return result;
}
std::string RpiControlCore::process_package(std::string &header, std::string &message) {
std::string response;
switch (sw_str(header.data())) {
case sw_str(H_SCANNER):
response = init_response(H_SCANNER);
if (message == PKG_MASK) {
auto d = new DeviceProperties;
auto answer = d->get_device_name() + "|" + d->get_device_model() + "|" + d->get_os_version();
delete d;
response += answer;
} else {
response += "ACCESS_DENY";
}
break;
case sw_str(H_AUTH):
response = init_response(H_AUTH);
response += (auth_user(message)) ? "true" : "false";
break;
case sw_str(H_DEVICE_INFO):
response = init_response(H_DEVICE_INFO);
response += get_device_info();
break;
case sw_str(H_GET_SOUND_VOLUME):
response = init_response(H_GET_SOUND_VOLUME);
response += std::to_string(get_sound_volume());
break;
case sw_str(H_SET_SOUND_VOLUME):
response = init_response(H_SET_SOUND_VOLUME);
set_sound_volume(static_cast<unsigned short>( std::strtoul(message.c_str(), nullptr, 0)));
response += std::to_string(get_sound_volume());
break;
case sw_str(H_SET_DEVICE_NAME):
response = init_response(H_SET_DEVICE_NAME);
response += set_device_name(message);
break;
case sw_str(H_SET_NEW_PASSWORD):
response = init_response(H_SET_NEW_PASSWORD);
response += (change_password(message)) ? "true" : "false";
break;
case sw_str(H_REBOOT_DEVICE):
response = init_response(H_SET_DEVICE_NAME) + "OK";
Utils::exec("sudo reboot");
break;
case sw_str(H_SHUTDOWN_DEVICE):
response = init_response(H_SHUTDOWN_DEVICE) + "OK";
Utils::exec("sudo shutdown");
break;
case sw_str(H_GET_DIR):
response = init_response(H_GET_DIR);
response += get_dir(message);
break;
case sw_str(H_GET_FILE):
response = init_response(H_GET_FILE);
response += get_file(message);
break;
case sw_str(H_GET_ETH_CONNECTION):
response = init_response(H_GET_ETH_CONNECTION);
response += get_eth_connection();
break;
case sw_str(H_EXEC_CMD):
response = init_response(H_EXEC_CMD);
response += run_cmd(message);
break;
default:
break;
}
return response;
}
void RpiControlCore::start_server() {
int sock, listener;
struct sockaddr_in addr{};
char line[BUFF_SIZE];
listener = socket(AF_INET, SOCK_STREAM, 0);
if(listener < 0) exit_app("socket", 1);
addr.sin_family = AF_INET;
addr.sin_port = htons(PORT);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(listener, (struct sockaddr *)&addr, sizeof(addr)) < 0) exit_app("bind", 2);
listen(listener, 10);
std::cout << "-> RpiControl server started! Listen on port #" << PORT << std::endl;
while(true)
{
sock = accept(listener, nullptr, nullptr);
std::cout << "-> Accept new client connection." << std::endl;
if(sock < 0) exit_app("accept", 3);
recv(sock, line, BUFF_SIZE, 0);
auto raw_msg = Utils::trim_copy(line);
std::fill(&line[0], &line[0] + sizeof(line), 0);
auto data = Utils::split(raw_msg, "\\^");
if (data.size() < 2) continue;
string header = data[0];
string message;
for(int i = 1; i < data.size(); i++) {
message += data[i];
}
std::cout << std::endl << "-> Recived a package:" << std::endl;
std::cout << " Header: " << header << std::endl;
std::cout << " Message: " << message << std::endl;
string answer = process_package(header, message);
send(sock, answer.data(), answer.size(), 0);
close(sock);
}
}
void RpiControlCore::exit_app(const char *message, int code) {
perror(message);
delete this;
exit(code);
}
int main() {
signal(SIGINT, app_terminate_handler);
std::thread t(app_server_thread_run);
while(true)
{
if (sigint_flag) {
std::cout << std::endl << "-> Get SIGINT!\nExiting an application..." << std::endl;
return 0;
}
}
}