Skip to content

Commit 8f8e332

Browse files
committed
Don't crash if sending to a null controller.
1 parent 5cb288d commit 8f8e332

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

SerialPrograms/Source/NintendoSwitch/Controllers/NintendoSwitch_SerialPABotBase.cpp

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*
55
*/
66

7+
#include "Common/Cpp/Exceptions.h"
78
#include "Common/Cpp/Json/JsonValue.h"
89
#include "CommonFramework/GlobalSettingsPanel.h"
910
#include "Controllers/ControllerDescriptor.h"
@@ -129,7 +130,7 @@ SwitchController_SerialPABotBase::SwitchController_SerialPABotBase(
129130
, m_logger(logger, GlobalSettings::instance().LOG_EVERYTHING)
130131
, m_logging_suppress(0)
131132
, m_handle(m_logger, &descriptor.port(), requirements)
132-
, m_serial(*m_handle.botbase())
133+
, m_serial(m_handle.botbase())
133134
{
134135
m_handle.connect(
135136
&m_handle, &BotBaseHandle::on_not_connected,
@@ -236,19 +237,28 @@ void SwitchController_SerialPABotBase::update_status_string(){
236237

237238
void SwitchController_SerialPABotBase::wait_for_all(const Cancellable* cancellable){
238239
// cout << "wait_for_all() - enter" << endl;
240+
if (!m_serial){
241+
throw InvalidConnectionStateException();
242+
}
239243
{
240244
WriteSpinLock lg(m_lock);
241245
this->issue_wait_for_all(cancellable);
242246
}
243-
m_serial.wait_for_all_requests(cancellable);
247+
m_serial->wait_for_all_requests(cancellable);
244248
// cout << "wait_for_all() - exit" << endl;
245249
}
246250
void SwitchController_SerialPABotBase::cancel_all(const Cancellable* cancellable){
247-
m_serial.stop_all_commands();
251+
if (!m_serial){
252+
throw InvalidConnectionStateException();
253+
}
254+
m_serial->stop_all_commands();
248255
this->clear_on_next();
249256
}
250257
void SwitchController_SerialPABotBase::replace_on_next_command(const Cancellable* cancellable){
251-
m_serial.next_command_interrupt();
258+
if (!m_serial){
259+
throw InvalidConnectionStateException();
260+
}
261+
m_serial->next_command_interrupt();
252262
this->clear_on_next();
253263
}
254264
void SwitchController_SerialPABotBase::issue_controller_state(
@@ -259,11 +269,14 @@ void SwitchController_SerialPABotBase::issue_controller_state(
259269
uint8_t right_x, uint8_t right_y,
260270
Milliseconds duration
261271
){
272+
if (!m_serial){
273+
throw InvalidConnectionStateException();
274+
}
262275
uint32_t ticks = milliseconds_to_ticks_8ms(duration.count());
263276
// Divide the controller state into smaller chunks of 255 ticks.
264277
while (ticks > 0){
265278
uint16_t curr_ticks = (uint16_t)std::min(ticks, (uint32_t)255);
266-
m_serial.issue_request(
279+
m_serial->issue_request(
267280
DeviceRequest_controller_state(button, position, left_x, left_y, right_x, right_y, (uint8_t)curr_ticks),
268281
cancellable
269282
);
@@ -274,13 +287,19 @@ void SwitchController_SerialPABotBase::send_botbase_request(
274287
const Cancellable* cancellable,
275288
const BotBaseRequest& request
276289
){
277-
m_serial.issue_request(request, cancellable);
290+
if (!m_serial){
291+
throw InvalidConnectionStateException();
292+
}
293+
m_serial->issue_request(request, cancellable);
278294
}
279295
BotBaseMessage SwitchController_SerialPABotBase::send_botbase_request_and_wait(
280296
const Cancellable* cancellable,
281297
const BotBaseRequest& request
282298
){
283-
return m_serial.issue_request_and_wait(request, cancellable);
299+
if (!m_serial){
300+
throw InvalidConnectionStateException();
301+
}
302+
return m_serial->issue_request_and_wait(request, cancellable);
284303
}
285304

286305
void SwitchController_SerialPABotBase::issue_barrier(const Cancellable* cancellable){

SerialPrograms/Source/NintendoSwitch/Controllers/NintendoSwitch_SerialPABotBase.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class SwitchController_SerialPABotBase :
100100
);
101101

102102
virtual Logger& logger() override{
103-
return m_serial.logger();
103+
return m_logger;
104104
}
105105

106106

@@ -213,7 +213,7 @@ class SwitchController_SerialPABotBase :
213213
SpinLock m_lock;
214214

215215
BotBaseHandle m_handle;
216-
BotBaseController& m_serial;
216+
BotBaseController* m_serial;
217217
};
218218

219219

0 commit comments

Comments
 (0)