-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.cpp
355 lines (306 loc) · 10.9 KB
/
main.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
//********************************************************************************//
// MIT License //
// //
// Copyright (c) 2022 TeamViewer Germany GmbH //
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy //
// of this software and associated documentation files (the "Software"), to deal //
// in the Software without restriction, including without limitation the rights //
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell //
// copies of the Software, and to permit persons to whom the Software is //
// furnished to do so, subject to the following conditions: //
// //
// The above copyright notice and this permission notice shall be included in all //
// copies or substantial portions of the Software. //
// //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR //
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, //
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE //
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER //
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, //
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE //
// SOFTWARE. //
//********************************************************************************//
#include <TVAgentAPI/tvagentapi.h>
#include <cstdio>
#include <cstdlib>
#include <csignal>
#include <getopt.h>
#include <initializer_list>
namespace
{
struct
{
tvagentapi::IAccessControlModule::Access fileTransferAccess =
tvagentapi::IAccessControlModule::Access::Denied;
tvagentapi::IAccessControlModule::Access remoteViewAccess =
tvagentapi::IAccessControlModule::Access::Denied;
tvagentapi::IAccessControlModule::Access remoteControlAccess =
tvagentapi::IAccessControlModule::Access::Denied;
tvagentapi::IAccessControlModule::Access screenRecordingAccess =
tvagentapi::IAccessControlModule::Access::Denied;
} s_accessData;
bool s_isInterrupted = false;
void signalHandler(int signo)
{
if (signo == SIGINT)
{
s_isInterrupted = true;
}
}
void connectionStatusChanged(tvagentapi::IAgentConnection::Status status, void* userdata) noexcept
{
printf("[IAgentConnection] Status: %s\n", toCString(status));
if (status != tvagentapi::IAgentConnection::Status::Connected)
{
return;
}
using Feature = tvagentapi::IAccessControlModule::Feature;
using Access = tvagentapi::IAccessControlModule::Access;
auto accessControlModule = static_cast<tvagentapi::IAccessControlModule*>(userdata);
printf("Getting current access...\n");
for (const auto feature:
{Feature::FileTransfer, Feature::RemoteView, Feature::RemoteControl, Feature::ScreenRecording})
{
tvagentapi::IAccessControlModule::Access access = Access::Denied;
if (!accessControlModule->getAccess(feature, access))
{
fprintf(
stderr, "Failed to retrieve %s access\n",
tvagentapi::toCString(feature));
}
else
{
printf(
"\t%s: %s\n",
tvagentapi::toCString(feature),
tvagentapi::toCString(access));
}
}
printf("Setting access...\n\t{%s => %s, %s => %s, %s => %s, %s => %s}\n",
tvagentapi::toCString(Feature::FileTransfer),
tvagentapi::toCString(s_accessData.fileTransferAccess),
tvagentapi::toCString(Feature::RemoteView),
tvagentapi::toCString(s_accessData.remoteViewAccess),
tvagentapi::toCString(Feature::RemoteControl),
tvagentapi::toCString(s_accessData.remoteControlAccess),
tvagentapi::toCString(Feature::ScreenRecording),
tvagentapi::toCString(s_accessData.screenRecordingAccess));
if (!accessControlModule->setAccess(
Feature::FileTransfer,
s_accessData.fileTransferAccess))
{
fprintf(stderr, "Failed to change access for file transfer");
}
if (!accessControlModule->setAccess(
Feature::RemoteView,
s_accessData.remoteViewAccess))
{
fprintf(stderr, "Failed to change access for remote view");
}
if (!accessControlModule->setAccess(
Feature::RemoteControl,
s_accessData.remoteControlAccess))
{
fprintf(stderr, "Failed to change access for remote control");
}
if (!accessControlModule->setAccess(
Feature::ScreenRecording,
s_accessData.screenRecordingAccess))
{
fprintf(stderr, "Failed to change access for screen recording");
}
}
void accessChanged(
tvagentapi::IAccessControlModule::Feature feature,
tvagentapi::IAccessControlModule::Access access,
void* userdata) noexcept
{
(void)userdata;
printf(
"[IAccessControlModule] accessChanged: %s => %s\n",
tvagentapi::toCString(feature),
tvagentapi::toCString(access));
}
void accessConfirmationRequested(
tvagentapi::IAccessControlModule::Feature feature,
void* userdata) noexcept
{
auto accessControlModule = static_cast<tvagentapi::IAccessControlModule*>(userdata);
printf(
"Incoming access confirmation request. Access to %s requested.\n"
"Confirm Access control request? (y/N): ",
tvagentapi::toCString(feature));
switch (getchar())
{
case 'y':
case 'Y':
accessControlModule->acceptAccessRequest(feature);
printf("Access confirmation request accepted\n");
break;
default:
accessControlModule->rejectAccessRequest(feature);
printf("Access confirmation request rejected\n");
break;
}
}
void parseArgs(int argc, char* argv[])
{
auto printUsage = [](decltype(stdout) stream)
{
constexpr const char* usageFormat =
R"(Usage: example_AccessControl [options...]
Set access control per feature. Options change defaults in access control.
Usage: example_AccessControl [options...]
Options:
-f, --file-transfer - set initial file transfer access
-v, --remote-view - set initial remote view access
-c, --remote-control - set initial remote control access
-r, --screen-recording - set initial screen recording access
Acceptable access values:
%d - Allowed
%d - After confirmation
%d - Denied
By default everything is denied.
)";
fprintf(
stream,
usageFormat,
static_cast<int>(tvagentapi::IAccessControlModule::Access::Allowed),
static_cast<int>(tvagentapi::IAccessControlModule::Access::AfterConfirmation),
static_cast<int>(tvagentapi::IAccessControlModule::Access::Denied));
};
constexpr struct option longOptions[] =
{
{ "file-transfer", required_argument, nullptr, 'f' },
{ "remote-view", required_argument, nullptr, 'v' },
{ "remote-control", required_argument, nullptr, 'c' },
{ "screen-recording", required_argument, nullptr, 'r' },
{}
};
int index = 0;
int opt;
auto toAccess = [printUsage](const char* arg) -> tvagentapi::IAccessControlModule::Access
{
switch (*arg)
{
case '0':
return tvagentapi::IAccessControlModule::Access::Allowed;
case '1':
return tvagentapi::IAccessControlModule::Access::AfterConfirmation;
case '2':
return tvagentapi::IAccessControlModule::Access::Denied;
}
printUsage(stderr);
exit(EXIT_FAILURE);
};
while ((opt = getopt_long(argc, argv, "hf:v:c:r:", longOptions, &index)) != -1)
{
switch (opt)
{
case 'h':
printUsage(stdout);
exit(EXIT_SUCCESS);
case 'f':
s_accessData.fileTransferAccess = toAccess(optarg);
break;
case 'v':
s_accessData.remoteViewAccess = toAccess(optarg);
break;
case 'c':
s_accessData.remoteControlAccess = toAccess(optarg);
break;
case 'r':
s_accessData.screenRecordingAccess = toAccess(optarg);
break;
default:
printUsage(stderr);
exit(EXIT_FAILURE);
}
}
}
} // namespace
int main(int argc, char* argv[])
{
parseArgs(argc, argv);
if (signal(SIGINT, signalHandler) == SIG_ERR)
{
fputs("Failed to set up signal handler\n", stderr);
return EXIT_FAILURE;
}
tvagentapi::IAgentAPI* agentAPI = TVGetAgentAPI();
if (!agentAPI)
{
fputs("Failed to create IAgentAPI\n", stderr);
return EXIT_FAILURE;
}
// create file logging
tvagentapi::ILogging* logging = agentAPI->createFileLogging("example_AccessControl.log");
if (!logging)
{
fputs("Failed to start file logging\n", stderr);
return EXIT_FAILURE;
}
// connect tvagentapi sdk to IoT Agent
// We pass logging to AgentConnection, but we still manage its lifetime and must pair it with IAgentAPI::destroyLogging()
// logging object will be used internally, and we may only release it (in this case by calling IAgentAPI::destroyLogging()
// after call to agentAPI->destroyAgentConnection();
tvagentapi::IAgentConnection* agentConnection =
agentAPI->createAgentConnection(logging);
if (!agentConnection)
{
fputs("Failed to create connection\n", stderr);
return EXIT_FAILURE;
}
const char* baseSdkUrl = std::getenv("TV_BASE_SDK_URL");
const char* agentApiUrl = std::getenv("TV_AGENT_API_URL");
if (baseSdkUrl && agentApiUrl)
{
const tvagentapi::IAgentConnection::SetConnectionURLsResult result =
agentConnection->setConnectionURLs(baseSdkUrl, agentApiUrl);
if (result != tvagentapi::IAgentConnection::SetConnectionURLsResult::Success)
{
fprintf(stderr, "Failed to set connection URLs: %s", tvagentapi::toCString(result));
return EXIT_FAILURE;
}
}
tvagentapi::IAccessControlModule* accessControlModule =
tvagentapi::getModule<tvagentapi::IAccessControlModule>(agentConnection);
if (!accessControlModule)
{
fputs("Failed to get AccessControlModule\n", stderr);
return EXIT_FAILURE;
}
if (!accessControlModule->isSupported())
{
fputs("AccessControlModule not supported\n", stderr);
return EXIT_FAILURE;
}
accessControlModule->setCallbacks({
{accessChanged, nullptr/*userdata*/},
{accessConfirmationRequested, accessControlModule},
});
agentConnection->setStatusChangedCallback({connectionStatusChanged, accessControlModule});
// start connection to IoT Agent
printf("Connecting to IoT Agent...\n");
agentConnection->start();
// main application's event loop
printf("Monitoring connection status changes... Press Ctrl+C to exit\n");
while (!s_isInterrupted)
{
// all the tvagentapi callbacks will be called on the thread calling processEvents()
// we wait for 100ms for more events and process them
agentConnection->processEvents(/*waitForMoreEvents = */true, /*timeoutMs = */100);
}
// stop connection to IoT Agent
printf("Stopping connection to IoT Agent...\n");
agentConnection->stop();
// we call processEvents() one more time to receive callbacks emitted by stop()
agentConnection->processEvents();
printf("Cleaning up...\n");
agentAPI->destroyAgentConnection(agentConnection);
// after destroyAgentConnection() we are sure logging is not used, and we can safely destroy it
agentAPI->destroyLogging(logging);
printf("Exiting...\n");
return EXIT_SUCCESS;
}