forked from AgOpenGPS-Official/AOG-TaskController
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
330 lines (295 loc) · 8.39 KB
/
main.cpp
File metadata and controls
330 lines (295 loc) · 8.39 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
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
#include "app.hpp"
#include "logging.cpp"
#include "settings.hpp"
#include "isobus/hardware_integration/available_can_drivers.hpp"
#include "isobus/isobus/can_stack_logger.hpp"
#include "git.h"
#include <shellapi.h>
#include <windows.h>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <string>
#define TRAY_ICON_ID 1
static std::atomic_bool running = { true };
// Window procedure to handle messages
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CLOSE:
running = false;
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
std::vector<std::string> ParseCommandLine(LPSTR lpCmdLine)
{
std::vector<std::string> arguments;
int argc;
LPWSTR *argv = CommandLineToArgvW(GetCommandLineW(), &argc);
if (argv == NULL)
{
return arguments;
}
for (int i = 0; i < argc; i++)
{
int requiredSize = WideCharToMultiByte(CP_UTF8, 0, argv[i], -1, NULL, 0, NULL, NULL);
if (requiredSize == 0)
{
continue;
}
// Allocate a string with length excluding the null terminator.
std::string arg(requiredSize - 1, '\0');
int result = WideCharToMultiByte(CP_UTF8, 0, argv[i], -1, arg.data(), requiredSize, NULL, NULL);
if (result == 0)
{
continue;
}
arguments.push_back(arg);
}
LocalFree(argv);
return arguments;
}
enum class CANAdapter
{
NONE,
ADAPTER_PCAN_USB,
ADAPTER_INNOMAKER_USB2CAN,
ADAPTER_RUSOKU_TOUCAN,
ADAPTER_SYS_TEC_USB2CAN,
};
class ArgumentProcessor
{
public:
ArgumentProcessor(std::vector<std::string> arguments) :
arguments(arguments)
{
}
bool process()
{
for (std::string arg : arguments)
{
std::transform(arg.begin(), arg.end(), arg.begin(), [](unsigned char c) { return std::tolower(c); });
parse_option(arg);
parse_parameter(arg);
}
return true;
}
CANAdapter get_can_adapter() const
{
return canAdapter;
}
std::string get_can_channel() const
{
return canChannel;
}
bool is_file_logging() const
{
return fileLogging;
}
private:
bool parse_option(std::string option)
{
if ("--help" == option)
{
std::cout << "Usage: AOG-TaskController.exe [options]\n";
std::cout << "Options:\n";
std::cout << " --help\t\tShow this help message\n";
std::cout << " --version\t\tShow the version of the application\n";
std::cout << " --can_adapter=<driver>\tSelect the CAN driver\n";
std::cout << " --can_channel=<channel>\tSelect the CAN channel\n";
std::cout << " --log_level=<level>\tSet the log level (debug, info, warning, error, critical)\n";
std::cout << " --log2file\t\tLog to file\n";
exit(0);
}
else if ("--version" == option)
{
std::cout << std::string(git::Describe()) + (git::AnyUncommittedChanges() ? "-dirty" : "") << std::endl;
exit(0);
}
else if ("--log2file" == option)
{
fileLogging = true;
}
else
{
return false;
}
return true;
}
bool parse_parameter(std::string parameter)
{
size_t pos = parameter.find('=');
if (pos == std::string::npos)
{
return false;
}
std::string key = parameter.substr(0, pos);
std::string value = parameter.substr(pos + 1);
if ("--can_adapter" == key)
{
static const std::unordered_map<std::string, CANAdapter> adapterMap = {
{ "peak-pcan", CANAdapter::ADAPTER_PCAN_USB },
{ "innomaker-usb2can", CANAdapter::ADAPTER_INNOMAKER_USB2CAN },
{ "rusoku-toucan", CANAdapter::ADAPTER_RUSOKU_TOUCAN },
{ "sys-tec-usb2can", CANAdapter::ADAPTER_SYS_TEC_USB2CAN },
};
auto it = adapterMap.find(value);
if (it != adapterMap.end())
{
canAdapter = it->second;
return true;
}
std::cout << "Unknown CAN adapter: " << value.c_str() << std::endl;
return false;
}
else if ("--can_channel" == key)
{
canChannel = value;
}
else if ("--log_level" == key)
{
if ("debug" == value)
{
isobus::CANStackLogger::set_log_level(isobus::CANStackLogger::LoggingLevel::Debug);
}
else if ("info" == value)
{
isobus::CANStackLogger::set_log_level(isobus::CANStackLogger::LoggingLevel::Info);
}
else if ("warning" == value)
{
isobus::CANStackLogger::set_log_level(isobus::CANStackLogger::LoggingLevel::Warning);
}
else if ("error" == value)
{
isobus::CANStackLogger::set_log_level(isobus::CANStackLogger::LoggingLevel::Error);
}
else if ("critical" == value)
{
isobus::CANStackLogger::set_log_level(isobus::CANStackLogger::LoggingLevel::Critical);
}
else
{
std::cout << "Unknown log level: " << value.c_str() << std::endl;
return false;
}
}
else
{
return false;
}
return true;
}
std::vector<std::string> arguments;
CANAdapter canAdapter = CANAdapter::NONE;
std::string canChannel;
bool fileLogging = false;
};
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
// Try to attach to the parent process’s console if it exists
if (AttachConsole(ATTACH_PARENT_PROCESS))
{
FILE *fp;
freopen_s(&fp, "CONOUT$", "w", stdout); // Redirect stdout to the console
std::cout << std::string(100, '\n'); // Clear the console screen
std::cout << "WARNING: Only start the application if you know what you are doing. It is intended to be started from AgIO!" << std::endl;
std::cout << "Press Ctrl+C to stop the application..." << std::endl;
std::cout << std::endl; // White space
}
std::ofstream logFile;
std::shared_ptr<isobus::CANHardwarePlugin> canDriver;
auto arguments = ParseCommandLine(lpCmdLine);
// Print command line string and version to console
ArgumentProcessor argumentProcessor(arguments);
bool argumentsProcessed = argumentProcessor.process();
// The sequence is important here, first we process the arguments, then we check if the file logging is enabled, then we log the arguments and version to the console/file.
isobus::CANStackLogger::set_can_stack_logger_sink(&logger);
if (argumentProcessor.is_file_logging())
{
setup_file_logging();
}
// Sent the cmd-line arguments and app-version to the console
for (std::string arg : arguments)
{
std::cout << arg.c_str() << " ";
}
std::cout << std::endl;
std::cout << "AOG-TC version: v" << std::string(git::Describe()) + (git::AnyUncommittedChanges() ? "-dirty" : "") << std::endl;
if (!argumentsProcessed)
{
std::cout << "Failed to process arguments, exiting..." << std::endl;
return -1;
}
switch (argumentProcessor.get_can_adapter())
{
case CANAdapter::ADAPTER_PCAN_USB:
{
canDriver = std::make_shared<isobus::PCANBasicWindowsPlugin>(PCAN_USBBUS1 - 1 + std::stoi(argumentProcessor.get_can_channel()));
break;
}
case CANAdapter::ADAPTER_INNOMAKER_USB2CAN:
{
canDriver = std::make_shared<isobus::InnoMakerUSB2CANWindowsPlugin>(std::stoi(argumentProcessor.get_can_channel()) - 1);
break;
}
case CANAdapter::ADAPTER_RUSOKU_TOUCAN:
{
canDriver = std::make_shared<isobus::TouCANPlugin>(std::stoi(argumentProcessor.get_can_channel()), std::stoi(argumentProcessor.get_can_channel()));
break;
}
case CANAdapter::ADAPTER_SYS_TEC_USB2CAN:
{
canDriver = std::make_shared<isobus::SysTecWindowsPlugin>(static_cast<std::uint8_t>(std::stoi(argumentProcessor.get_can_channel())));
break;
}
default:
{
std::cout << "No CAN adapter selected, exiting..." << std::endl;
return -1;
}
}
WNDCLASS wc = { 0 };
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.lpszClassName = TEXT("AOG-TaskController");
RegisterClass(&wc);
HWND hwnd = CreateWindowEx(WS_EX_TOOLWINDOW, wc.lpszClassName, TEXT("AOG-TaskController"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 300, 200, NULL, NULL, hInstance, NULL);
if (!hwnd)
{
return -1;
}
ShowWindow(hwnd, SW_SHOWMINNOACTIVE); // Little hack: Keep the window hidden, but still allows AOG (or other applications) to gracefully close it
Application app(canDriver);
if (!app.initialize())
{
std::cout << "Failed to initialize application..." << std::endl;
return -1;
}
MSG msg;
while (running)
{
// This will become the apps main timer.
// Wait for a message with a timeout
// If a message arrives, process all pending messages
// If timeout occurs, continue to app.update()
DWORD result = MsgWaitForMultipleObjects(0, NULL, FALSE, 1, QS_ALLINPUT);
while (result == WAIT_OBJECT_0 && PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (!app.update())
{
std::cout << "Something unexpected happened, stopping application..." << std::endl;
break;
}
}
// Clean up
app.stop();
return 0;
}