forked from PokemonAutomation/Arduino-Source
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerialPABotBase_Connection.cpp
More file actions
366 lines (321 loc) · 12.8 KB
/
Copy pathSerialPABotBase_Connection.cpp
File metadata and controls
366 lines (321 loc) · 12.8 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
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
356
357
358
359
360
361
362
363
364
365
366
/* Serial Port (PABotBase) Connection
*
* From: https://github.com/PokemonAutomation/
*
*/
#include <QtGlobal>
#include <QSerialPortInfo>
#include <QMessageBox>
#include "Common/Cpp/PrettyPrint.h"
#include "Common/Cpp/Exceptions.h"
#include "Common/Cpp/PanicDump.h"
#include "Common/Cpp/SerialConnection/SerialConnection.h"
#include "CommonFramework/Globals.h"
#include "CommonFramework/GlobalSettingsPanel.h"
#include "CommonFramework/Options/Environment/ThemeSelectorOption.h"
#include "CommonFramework/Tools/GlobalThreadPools.h"
#include "Controllers/ControllerTypeStrings.h"
#include "Controllers/SerialPABotBase/Messages/SerialPABotBase_MessageWrappers_BaseProtocol_Errors.h"
#include "Controllers/SerialPABotBase/Messages/SerialPABotBase_MessageWrappers_BaseProtocol_Acks.h"
#include "Controllers/SerialPABotBase/Messages/SerialPABotBase_MessageWrappers_BaseProtocol_Info.h"
#include "Controllers/SerialPABotBase/Messages/SerialPABotBase_MessageWrappers_BaseProtocol_StaticRequests.h"
#include "Controllers/SerialPABotBase/Messages/SerialPABotBase_MessageWrappers_BaseProtocol_ControllerMode.h"
#include "Controllers/SerialPABotBase/Messages/SerialPABotBase_MessageWrappers_BaseProtocol_CommandQueue.h"
#include "Controllers/SerialPABotBase/Messages/SerialPABotBase_MessageWrappers_BaseProtocol_Misc.h"
#include "Controllers/SerialPABotBase/SerialPABotBase_Routines_Protocol.h"
#include "SerialPABotBase.h"
#include "SerialPABotBase_Connection.h"
//#include <iostream>
//using std::cout;
//using std::endl;
namespace PokemonAutomation{
namespace SerialPABotBase{
SerialPABotBase_Connection::SerialPABotBase_Connection(
Logger& logger,
const std::string& name,
bool set_to_null_controller
)
: m_logger(logger, GlobalSettings::instance().LOG_EVERYTHING)
{
set_status_line0("Not Connected", COLOR_RED);
QSerialPortInfo info(QString::fromStdString(name));
// Port is invalid.
if (info.isNull()){
logger.log("Serial port " + name + " is invalid. Maybe wrong port name?");
return;
}
// Prolific is banned
if (info.description().indexOf("Prolific") != -1){
QMessageBox box;
box.critical(
nullptr,
"Error",
"Cannot select Prolific controller.<br><br>"
"Prolific controllers do not work for Arduino and similar microcontrollers.<br>"
"You were warned of this in the setup instructions. Please buy a CP210x controller instead."
);
std::string text = "Cannot connect to Prolific controller.";
m_logger.log(text, COLOR_RED);
set_status_line0(text, COLOR_RED);
return;
}
m_device_name = info.portName().toStdString();
std::string error;
try{
set_status_line0("Connecting...", COLOR_DARKGREEN);
std::unique_ptr<SerialConnection> connection(
new SerialConnection(
GlobalThreadPools::unlimited_normal(),
info.systemLocation().toStdString(),
PABB_BAUD_RATE
)
);
m_botbase.reset(
new PABotBase(
m_logger,
GlobalThreadPools::unlimited_normal(),
std::move(connection)
)
);
add_message_printers();
}catch (const ConnectionException& e){
error = e.message();
}catch (const SerialProtocolException& e){
error = e.message();
}catch (const ProgramCancelledException& e){
error = e.message();
}
if (!error.empty()){
set_status_line0("Unable to open port.", COLOR_RED);
return;
}
m_status_thread = GlobalThreadPools::unlimited_normal().dispatch_now_blocking([=, this]{
run_with_catch(
"SerialPABotBase_Connection::thread_body()",
[=, this]{ thread_body(set_to_null_controller); }
);
});
}
SerialPABotBase_Connection::~SerialPABotBase_Connection(){
m_ready.store(false, std::memory_order_release);
// signal_pre_not_ready();
if (m_botbase == nullptr){
return;
}
m_botbase->stop();
{
std::lock_guard<Mutex> lg(m_lock);
m_cv.notify_all();
}
m_status_thread.wait_and_ignore_exceptions();
m_botbase.reset();
}
BotBaseController* SerialPABotBase_Connection::botbase(){
BotBaseController* ret = m_botbase.get();
if (ret == nullptr){
m_logger.log("SerialPABotBase_Connection::botbase() called with null botbase...", COLOR_RED);
}
return ret;
}
ControllerType SerialPABotBase_Connection::refresh_controller_type(){
m_logger.log("Reading Controller Mode...");
uint32_t type_id = read_controller_mode(*botbase());
ControllerType current_controller = id_to_controller_type(type_id);
m_logger.log(
"Reading Controller Mode... Mode = " +
CONTROLLER_TYPE_STRINGS.get_string(current_controller)
);
m_current_controller.store(current_controller, std::memory_order_release);
return current_controller;
}
void SerialPABotBase_Connection::add_message_printers(){
// Errors
add_message_printer<MessageType_ErrorReady>();
add_message_printer<MessageType_InvalidMessage>();
add_message_printer<MessageType_ChecksumMismatch>();
add_message_printer<MessageType_InvalidType>();
add_message_printer<MessageType_InvalidRequest>();
add_message_printer<MessageType_MissedRequest>();
add_message_printer<MessageType_CommandDropped>();
add_message_printer<MessageType_ErrorWarning>();
add_message_printer<MessageType_ErrorDisconnected>();
// Framework Acks
add_message_printer<MessageType_AckCommand>();
add_message_printer<MessageType_AckRequest>();
add_message_printer<MessageType_AckRequest_i8>();
add_message_printer<MessageType_AckRequest_i16>();
add_message_printer<MessageType_AckRequest_i32>();
add_message_printer<MessageType_AckRequest_Data>();
// Custom Info
add_message_printer<MessageType_Info_i32>();
add_message_printer<MessageType_Info_Data>();
add_message_printer<MessageType_Info_String>();
add_message_printer<MessageType_Info_Label_i32>();
add_message_printer<MessageType_Info_Label_h32>();
add_message_printer<MessageType_Info_ResetReason>();
// Static Requests
add_message_printer<MessageType_SeqnumReset>();
add_message_printer<MessageType_ProtocolVersion>();
add_message_printer<MessageType_ProgramVersion>();
add_message_printer<MessageType_ProgramID>();
add_message_printer<MessageType_ProgramName>();
add_message_printer<MessageType_ControllerList>();
add_message_printer<MessageType_QueueSize>();
// Mode Requests
add_message_printer<MessageType_ReadControllerMode>();
add_message_printer<MessageType_ChangeControllerMode>();
add_message_printer<MessageType_ResetToController>();
// Command Queue Requests
add_message_printer<MessageType_CommandFinished>();
add_message_printer<MessageType_RequestStop>();
add_message_printer<MessageType_NextCommandInterrupt>();
// Other Requests
add_message_printer<MessageType_SystemClock>();
add_message_printer<MessageType_ControllerStatus>();
add_message_printer<MessageType_ReadMacAddress>();
add_message_printer<MessageType_PairedMacAddress>();
}
void SerialPABotBase_Connection::process_queue_size(){
m_logger.log("Requesting queue size...");
uint8_t queue_size = device_queue_size(*m_botbase);
m_logger.Logger::log("Requesting queue size... Queue Size = " + std::to_string(queue_size));
// For now we don't need to use that much queue size.
queue_size = std::min<uint8_t>(queue_size, 32);
m_logger.Logger::log("Setting queue size to: " + std::to_string(queue_size));
m_botbase->set_queue_limit(queue_size);
}
void SerialPABotBase_Connection::throw_incompatible_protocol(){
throw SerialProtocolException(
m_logger, PA_CURRENT_FUNCTION,
"Incompatible protocol. Device: " + std::to_string(m_protocol) + "<br>"
"Please flash your microcontroller (e.g. ESP32, Pico W, Arduino) <br>"
"with the .bin/.uf2/.hex that came with this version of the program.<br>" +
make_text_url(ONLINE_DOC_URL_BASE + "SetupGuide/Reflash.html", "See documentation for more details.")
);
}
ControllerType SerialPABotBase_Connection::process_device(bool set_to_null_controller){
// Protocol Version
const std::map<pabb_ProgramID, uint8_t>* PROGRAMS;
{
m_logger.Logger::log("Checking Protocol Version...");
m_protocol = SerialPABotBase::protocol_version(*m_botbase);
m_logger.Logger::log("Checking Protocol Version... (" + std::to_string(m_protocol) + ")");
auto iter = SUPPORTED_VERSIONS().find(m_protocol / 100);
if (iter == SUPPORTED_VERSIONS().end()){
throw_incompatible_protocol();
}
PROGRAMS = &iter->second;
}
// Program ID
{
m_logger.Logger::log("Checking Program ID...");
m_program_id = program_id(*m_botbase);
m_logger.Logger::log("Checking Program ID... (0x" + tostr_hex(m_program_id) + ")");
auto iter = PROGRAMS->find(m_program_id);
if (iter == PROGRAMS->end()){
m_logger.Logger::log(
"Unrecognized Program ID: (0x" + tostr_hex(m_program_id) + ") for this protocol version. "
"Compatibility is not guaranteed.",
COLOR_RED
);
iter = PROGRAMS->find(PABB_PID_UNSPECIFIED);
if (iter == PROGRAMS->end()){
throw_incompatible_protocol();
}
}
if (m_protocol % 100 < iter->second){
throw_incompatible_protocol();
}
}
// Firmware Version
{
m_logger.Logger::log("Checking Firmware Version...");
m_version = program_version(*m_botbase);
m_logger.Logger::log("Checking Firmware Version... (" + std::to_string(m_version) + ")");
}
// Program Name
{
m_logger.Logger::log("Checking Program Name...");
m_program_name = program_name(*m_botbase);
m_logger.Logger::log("Checking Program Name... (" + m_program_name + ")");
}
// Controller List
{
m_logger.Logger::log("Checking Controller List...");
std::string str;
bool first = true;
for (pabb_ControllerID id : SerialPABotBase::controller_list(*m_botbase)){
if (!first){
str += ", ";
}
first = false;
str += "0x" + tostr_hex(id);
if (controller_is_valid(id)){
m_controller_list.emplace_back(id_to_controller_type(id));
}
}
m_logger.Logger::log("Checking Controller List... (" + str + ")");
}
// Queue Size
process_queue_size();
// Current Controller
ControllerType current_controller = refresh_controller_type();
if (set_to_null_controller && current_controller != ControllerType::None){
m_botbase->issue_request_and_wait(
DeviceRequest_change_controller_mode(PABB_CID_NONE),
nullptr
);
current_controller = refresh_controller_type();
}
return current_controller;
}
void SerialPABotBase_Connection::thread_body(bool set_to_null_controller){
using namespace PokemonAutomation;
// Connect
{
std::string error;
try{
m_botbase->connect();
}catch (InvalidConnectionStateException&){
m_botbase->stop();
set_status_line0("");
return;
}catch (SerialProtocolException& e){
error = e.message();
}catch (ConnectionException& e){
error = e.message();
}
if (!error.empty()){
m_botbase->stop();
set_status_line0(error, COLOR_RED);
return;
}
}
// Check protocol and version.
{
std::string error;
try{
process_device(set_to_null_controller);
// Stop pending commands.
m_botbase->stop_all_commands();
std::string text = m_program_name + " (" + std::to_string(m_version) + ")";
set_status_line0(text, theme_friendly_darkblue());
declare_ready();
}catch (InvalidConnectionStateException&){
return;
}catch (SerialProtocolException& e){
error = e.message();
}catch (ConnectionException& e){
error = e.message();
}
if (!error.empty()){
// m_ready.store(false, std::memory_order_relaxed);
set_status_line0(error, COLOR_RED);
// signal_pre_not_ready();
m_botbase->stop();
return;
}
}
}
}
}