Skip to content

Commit 157e4d2

Browse files
1.9.0
1 parent 1fd621a commit 157e4d2

File tree

15,952 files changed

+3369152
-468
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

15,952 files changed

+3369152
-468
lines changed

ArduinoStrike/ArduinoStrike/Arduino.cpp

+63-83
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,67 @@
11
#include "Arduino.h"
2-
#include "Utils.h"
2+
#include "Logger.h"
33

44
Arduino::~Arduino()
55
{
6-
if (handle != INVALID_HANDLE_VALUE)
6+
if (serial_port.is_open())
77
{
8-
CloseHandle(handle);
9-
LOG("Disconnected from Arduino.");
8+
serial_port.close();
9+
Logger::LogMessage("Disconnected from Arduino.", boost::log::trivial::debug);
1010
}
1111
}
1212

13-
Arduino::Arduino(LPCSTR name) : handle(INVALID_HANDLE_VALUE)
13+
Arduino::Arduino(LPCSTR name) : io_context(), serial_port(io_context)
1414
{
1515
char port[100] = "\\.\\";
1616

17-
LOG("Searching for device...");
17+
Logger::LogMessage("Searching for device...");
1818
while (!GetDevice(name, port))
1919
{
20-
LOG("Device not found. Retrying...");
20+
Logger::LogMessage("Device not found. Retrying...");
2121
sleep_for(milliseconds(1000));
2222
}
2323

24-
LOG(string("Device found: ") + name + " (" + port + ")");
24+
Logger::LogMessage(string("Device found: ") + name + " (" + port + ")");
2525

26-
handle = CreateFile(port, GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
27-
28-
if (handle == INVALID_HANDLE_VALUE)
26+
try
2927
{
30-
DWORD error_code = GetLastError();
31-
LOG("Error opening port: " + to_string(error_code));
32-
cerr << "Error opening port: " << error_code << endl;
33-
return;
34-
}
35-
36-
LOG("Port opened successfully.");
28+
serial_port.open(port);
3729

38-
DCB dcb = {};
39-
dcb.DCBlength = sizeof(dcb);
40-
41-
if (!GetCommState(handle, &dcb))
30+
if (!serial_port.is_open())
31+
{
32+
Logger::LogMessage("Error opening port: Serial port not open after attempt.");
33+
cerr << "Error opening port: Serial port not open after attempt." << endl;
34+
return;
35+
}
36+
}
37+
catch (boost::system::system_error& error)
4238
{
43-
LOG("Failed to get port state.");
39+
DWORD error_code = error.code().value();
40+
Logger::LogMessage("Error opening port: " + to_string(error_code) + " - " + error.what());
41+
cerr << "Error opening port: " << error_code << " - " << error.what() << endl;
4442
return;
4543
}
4644

47-
dcb.BaudRate = CBR_9600;
48-
dcb.ByteSize = 8;
49-
dcb.StopBits = ONESTOPBIT;
50-
dcb.Parity = NOPARITY;
45+
Logger::LogMessage("Port opened successfully.");
5146

52-
if (!SetCommState(handle, &dcb))
47+
try
5348
{
54-
LOG("Failed to set port state.");
55-
return;
49+
serial_port.set_option(serial_port::baud_rate(9600));
50+
serial_port.set_option(serial_port::baud_rate(9600));
51+
serial_port.set_option(serial_port::character_size(8));
52+
serial_port.set_option(serial_port::stop_bits(serial_port::stop_bits::one));
53+
serial_port.set_option(serial_port::parity(serial_port::parity::none));
54+
serial_port.set_option(serial_port::flow_control(serial_port::flow_control::none));
5655
}
57-
58-
COMMTIMEOUTS cto = {};
59-
cto.ReadIntervalTimeout = 50;
60-
cto.ReadTotalTimeoutConstant = 100;
61-
cto.ReadTotalTimeoutMultiplier = 10;
62-
cto.WriteTotalTimeoutConstant = 50;
63-
cto.WriteTotalTimeoutMultiplier = 10;
64-
65-
if (!SetCommTimeouts(handle, &cto))
56+
catch (boost::system::system_error& error)
6657
{
67-
LOG("Failed to set timeouts.");
58+
Logger::LogMessage("Failed to set port options: " + string(error.what()));
59+
cerr << "Failed to set port options: " << error.what() << endl;
60+
serial_port.close();
6861
return;
6962
}
7063

71-
LOG("Port initialized successfully.");
64+
Logger::LogMessage("Port initialized successfully.");
7265
cout << "Successfully connected!" << endl;
7366
}
7467

@@ -78,7 +71,7 @@ bool Arduino::GetDevice(LPCSTR name, LPSTR port)
7871

7972
if (device_info == INVALID_HANDLE_VALUE)
8073
{
81-
LOG("Failed to get device information (SetupDiGetClassDevs returned INVALID_HANDLE_VALUE).");
74+
Logger::LogMessage("Failed to get device information (SetupDiGetClassDevs returned INVALID_HANDLE_VALUE).");
8275
return false;
8376
}
8477

@@ -96,7 +89,7 @@ bool Arduino::GetDevice(LPCSTR name, LPSTR port)
9689
bool config_loaded = Arduino::LoadConfiguration("device_config.cfg", config_device);
9790

9891
// Collect system devices
99-
LOG("Enumerating connected devices...");
92+
Logger::LogMessage("Enumerating connected devices...");
10093
DWORD count = 0;
10194
SP_DEVINFO_DATA dev_info_data = {};
10295
dev_info_data.cbSize = sizeof(dev_info_data);
@@ -108,13 +101,13 @@ bool Arduino::GetDevice(LPCSTR name, LPSTR port)
108101
if (Arduino::ExtractProperties(device_info, dev_info_data, system_device))
109102
{
110103
devices.push_back(system_device);
111-
LOG("Detected device: FRIENDLYNAME = " + system_device.friendly_name + ", HARDWAREID = " + system_device.hardware_id + ", PORT = " + system_device.port);
104+
Logger::LogMessage("Detected device: FRIENDLYNAME = " + system_device.friendly_name + ", HARDWAREID = " + system_device.hardware_id + ", PORT = " + system_device.port);
112105

113106
// Check against configuration file
114107
if (config_loaded && system_device.friendly_name == config_device.friendly_name && system_device.hardware_id == config_device.hardware_id)
115108
{
116109
strncpy_s(port, 100, system_device.port.c_str(), system_device.port.size());
117-
LOG("Using saved device: FRIENDLYNAME = " + system_device.friendly_name + ", HARDWAREID = " + system_device.hardware_id + ", PORT = " + system_device.port);
110+
Logger::LogMessage("Using saved device: FRIENDLYNAME = " + system_device.friendly_name + ", HARDWAREID = " + system_device.hardware_id + ", PORT = " + system_device.port);
118111
found_device = true;
119112
break;
120113
}
@@ -123,7 +116,7 @@ bool Arduino::GetDevice(LPCSTR name, LPSTR port)
123116
if (!found_device && name && system_device.friendly_name.find(name) != string::npos)
124117
{
125118
strncpy_s(port, 100, system_device.port.c_str(), system_device.port.size());
126-
LOG("Device matched by FRIENDLYNAME: " + system_device.friendly_name);
119+
Logger::LogMessage("Device matched by FRIENDLYNAME: " + system_device.friendly_name);
127120
found_device = true;
128121
break;
129122
}
@@ -133,7 +126,7 @@ bool Arduino::GetDevice(LPCSTR name, LPSTR port)
133126
if (regex_search(system_device.hardware_id, pattern))
134127
{
135128
strncpy_s(port, 100, system_device.port.c_str(), system_device.port.size());
136-
LOG("Device matched by VID/PID: " + system_device.hardware_id);
129+
Logger::LogMessage("Device matched by VID/PID: " + system_device.hardware_id);
137130
found_device = true;
138131
break;
139132
}
@@ -151,17 +144,17 @@ bool Arduino::GetDevice(LPCSTR name, LPSTR port)
151144
{
152145
if (config_loaded)
153146
{
154-
LOG("Saved device not found in the system. Deleting configuration file.");
147+
Logger::LogMessage("Saved device not found in the system. Deleting configuration file.");
155148
remove("device_config.cfg");
156149
}
157150

158151
if (devices.empty())
159152
{
160-
LOG("No devices found in the system.");
153+
Logger::LogMessage("No devices found in the system.");
161154
return false;
162155
}
163156

164-
LOG("Prompting user to select a device...");
157+
Logger::LogMessage("Prompting user to select a device...");
165158
return Arduino::SelectDevice(devices, port);
166159
}
167160

@@ -175,7 +168,7 @@ bool Arduino::LoadConfiguration(const string& file_name, DeviceInfo& config_devi
175168

176169
if (!config)
177170
{
178-
LOG("Configuration file not found.");
171+
Logger::LogMessage("Configuration file not found.", boost::log::trivial::debug);
179172
return false;
180173
}
181174

@@ -197,11 +190,11 @@ bool Arduino::LoadConfiguration(const string& file_name, DeviceInfo& config_devi
197190

198191
if (!config_device.friendly_name.empty() && !config_device.hardware_id.empty())
199192
{
200-
LOG("Loaded device from configuration: FRIENDLYNAME = " + config_device.friendly_name + ", HARDWAREID = " + config_device.hardware_id + ", PORT = " + config_device.port);
193+
Logger::LogMessage("Loaded device from configuration: FRIENDLYNAME = " + config_device.friendly_name + ", HARDWAREID = " + config_device.hardware_id + ", PORT = " + config_device.port);
201194
return true;
202195
}
203196

204-
LOG("Configuration file is incomplete or corrupted.");
197+
Logger::LogMessage("Configuration file is incomplete or corrupted.");
205198
return false;
206199
}
207200

@@ -250,7 +243,7 @@ bool Arduino::SelectDevice(const vector<DeviceInfo>& devices, LPSTR port)
250243
{
251244
const auto& selected_device = devices[choice - 1];
252245
strncpy_s(port, 100, selected_device.port.c_str(), selected_device.port.size());
253-
LOG("User selected device: " + selected_device.friendly_name);
246+
Logger::LogMessage("User selected device: " + selected_device.friendly_name);
254247

255248
ofstream config("device_config.cfg");
256249

@@ -260,50 +253,37 @@ bool Arduino::SelectDevice(const vector<DeviceInfo>& devices, LPSTR port)
260253
config << "HARDWAREID=" << selected_device.hardware_id << endl;
261254
config << "PORT=" << selected_device.port << endl;
262255

263-
LOG("Device selection saved to configuration.");
256+
Logger::LogMessage("Device selection saved to configuration.");
264257
}
265258

266259
return true;
267260
}
268261

269262
utils.PrintCenteredText("No device selected by the user.");
270-
LOG("No device selected by the user.");
263+
Logger::LogMessage("No device selected by the user.");
271264

272265
return false;
273266
}
274267

275-
bool Arduino::WriteMessage(const string& message) const
268+
bool Arduino::WriteMessage(const string& message)
276269
{
277-
DWORD bytes = 0;
278-
BOOL result = WriteFile(handle, message.c_str(), message.size() + 1, &bytes, nullptr);
279-
280-
if (result == 0 || bytes != message.size() + 1)
270+
if (!serial_port.is_open())
281271
{
282-
LOG("Failed to send message: " + message);
283-
cerr << "Failed to send message!" << endl;
272+
Logger::LogMessage("Attempt to write to a closed serial port");
273+
cerr << "Attempt to write to a closed serial port" << endl;
284274
return false;
285275
}
286276

287-
LOG("Message sent: " + message);
288-
return true;
289-
}
290-
291-
void Arduino::LogMessage(const string& message)
292-
{
293-
static ofstream log_file("arduino_debug.log", ios::app);
294-
295-
if (!log_file.is_open())
277+
try
296278
{
297-
cerr << "Error opening the log file." << endl;
298-
return;
279+
write(serial_port, buffer(message.c_str(), message.size() + 1));
280+
Logger::LogMessage("Message sent: " + message);
281+
return true;
282+
}
283+
catch (boost::system::system_error& error)
284+
{
285+
Logger::LogMessage("Failed to send message: " + message + " - " + error.what());
286+
cerr << "Failed to send message: " << message << " - " << error.what() << endl;
287+
return false;
299288
}
300-
301-
tm local;
302-
time_t now = time(nullptr);
303-
localtime_s(&local, &now);
304-
305-
char buffer[100];
306-
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &local);
307-
308-
log_file << "[" << buffer << "] " << message << std::endl;
309289
}

ArduinoStrike/ArduinoStrike/Arduino.h

+8-16
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,14 @@
11
#pragma once
2-
#pragma comment(lib, "Setupapi.lib")
32

4-
#include <regex>
5-
#include <thread>
6-
#include <fstream>
7-
#include <iostream>
8-
#include <windows.h>
3+
#pragma comment(lib, "Setupapi.lib")
4+
#include "Utils.h"
95
#include <devguid.h>
106
#include <SetupAPI.h>
117

12-
#ifdef _DEBUG
13-
#define LOG(msg) Arduino::LogMessage(msg)
14-
#else
15-
#define LOG(msg)
16-
#endif
17-
188
using namespace std;
19-
using namespace chrono;
9+
using namespace std::chrono;
2010
using namespace this_thread;
11+
using namespace boost::asio;
2112

2213
struct DeviceInfo
2314
{
@@ -31,13 +22,14 @@ class Arduino
3122
public:
3223
~Arduino();
3324
Arduino(LPCSTR name);
34-
bool WriteMessage(const string& message) const;
25+
bool WriteMessage(const string& message);
3526

3627
private:
37-
HANDLE handle;
28+
io_context io_context;
29+
serial_port serial_port;
30+
3831
bool GetDevice(LPCSTR name, LPSTR port);
3932
bool LoadConfiguration(const string& file_name, DeviceInfo& config_device);
4033
bool ExtractProperties(HDEVINFO device_info, SP_DEVINFO_DATA& dev_info_data, DeviceInfo& device);
4134
bool SelectDevice(const vector<DeviceInfo>& devices, LPSTR port);
42-
static void LogMessage(const string& message);
4335
};

0 commit comments

Comments
 (0)