Skip to content

Commit 345be78

Browse files
committed
Block the reliable stream until CC is ready.
1 parent 27affe9 commit 345be78

12 files changed

Lines changed: 67 additions & 50 deletions

Common/PABotBase2/Controllers/PABotBase2_Controller_HID_Keyboard.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define PokemonAutomation_PABotBase2_Controller_HID_Keyboard_H
99

1010
#include "../../ControllerStates/StandardHid_Keyboard_State.h"
11+
#include "../PABotBase2_MessageProtocol.h"
1112

1213
#if _WIN32
1314
#pragma pack(push, 1)
@@ -24,7 +25,7 @@ namespace PABotBase2{
2425

2526

2627
#define PABB2_MESSAGE_CMD_HID_KEYBOARD_STATE 0x82
27-
struct PABB_PACK pabb2_Message_Command_HID_Keyboard_State{
28+
struct PABB_PACK pabb2_Message_Command_HID_Keyboard_State : MessageHeader{
2829
uint16_t milliseconds;
2930
pabb_HID_Keyboard_State report;
3031
};

Common/PABotBase2/Controllers/PABotBase2_Controller_NS1_OemController.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define PokemonAutomation_PABotBase2_Controller_NS1_OemController_H
99

1010
#include "../../ControllerStates/NintendoSwitch_OemController_State.h"
11+
#include "../PABotBase2_MessageProtocol.h"
1112

1213
#if _WIN32
1314
#pragma pack(push, 1)
@@ -25,7 +26,7 @@ namespace PABotBase2{
2526

2627
#define PABB_MSG_REQ_NS1_OEM_CONTROLLER_READ_SPI 0x91
2728
#define PABB_MSG_REQ_NS1_OEM_CONTROLLER_WRITE_SPI 0x92
28-
struct PABB_PACK pabb2_Message_NS1_OemController_Spi{
29+
struct PABB_PACK pabb2_Message_NS1_OemController_Spi : MessageHeader{
2930
uint32_t controller_type;
3031
uint32_t address;
3132
uint8_t bytes;
@@ -39,14 +40,14 @@ struct PABB_PACK pabb2_Message_NS1_OemController_Spi{
3940

4041

4142
#define PABB2_MESSAGE_CMD_NS1_OEM_CONTROLLER_BUTTONS 0x97
42-
struct PABB_PACK pabb2_Message_Command_NS1_OemController_Buttons{
43+
struct PABB_PACK pabb2_Message_Command_NS1_OemController_Buttons : MessageHeader{
4344
uint16_t milliseconds;
4445
pabb_NintendoSwitch_OemController_State0x30_Buttons buttons;
4546
};
4647

4748

4849
#define PABB2_MESSAGE_CMD_NS1_OEM_CONTROLLER_FULL_STATE 0x98
49-
struct PABB_PACK pabb2_Message_Command_NS1_OemController_FullState{
50+
struct PABB_PACK pabb2_Message_Command_NS1_OemController_FullState : MessageHeader{
5051
uint16_t milliseconds;
5152
pabb_NintendoSwitch_OemController_State0x30 state;
5253
};

Common/PABotBase2/Controllers/PABotBase2_Controller_NS_WiredController.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define PokemonAutomation_PABotBase2_Controller_NS_WiredController_H
99

1010
#include "../../ControllerStates/NintendoSwitch_WiredController_State.h"
11+
#include "../PABotBase2_MessageProtocol.h"
1112

1213
#if _WIN32
1314
#pragma pack(push, 1)
@@ -24,7 +25,7 @@ namespace PABotBase2{
2425

2526

2627
#define PABB2_MESSAGE_CMD_NS_WIRED_CONTROLLER_STATE 0x90
27-
struct PABB_PACK pabb2_Message_Command_NS_WiredController_State{
28+
struct PABB_PACK pabb2_Message_Command_NS_WiredController_State : MessageHeader{
2829
uint16_t milliseconds;
2930
pabb_NintendoSwitch_WiredController_State report;
3031
};

Common/PABotBase2/PABotBase2CC_MessageDumper.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ std::string tostr(const PacketHeader* header){
120120
return str;
121121
case PABB2_CONNECTION_OPCODE_INFO_STR:
122122
str += "PABB2_CONNECTION_OPCODE_INFO_STR: ";
123-
str += std::to_string(header->seqnum);
124123
str += std::string(
125124
(const char*)(header + 1),
126125
header->packet_bytes - sizeof(PacketHeader) - sizeof(uint32_t)
@@ -271,24 +270,24 @@ std::string tostr(const MessageHeader* header){
271270
str += std::to_string(header->id);
272271
return str;
273272

274-
case PABB_MESSAGE_OPCODE_READ_CONTROLLER_MODE:
275-
str += "PABB_MESSAGE_OPCODE_READ_CONTROLLER_MODE: id = ";
273+
case PABB2_MESSAGE_OPCODE_READ_CONTROLLER_MODE:
274+
str += "PABB2_MESSAGE_OPCODE_READ_CONTROLLER_MODE: id = ";
276275
str += std::to_string(header->id);
277276
return str;
278-
case PABB_MESSAGE_OPCODE_CHANGE_CONTROLLER_MODE:
279-
str += "PABB_MESSAGE_OPCODE_CHANGE_CONTROLLER_MODE: id = ";
277+
case PABB2_MESSAGE_OPCODE_CHANGE_CONTROLLER_MODE:
278+
str += "PABB2_MESSAGE_OPCODE_CHANGE_CONTROLLER_MODE: id = ";
280279
str += std::to_string(header->id);
281280
return str;
282-
case PABB_MESSAGE_OPCODE_RESET_TO_CONTROLLER:
283-
str += "PABB_MESSAGE_OPCODE_RESET_TO_CONTROLLER: id = ";
281+
case PABB2_MESSAGE_OPCODE_RESET_TO_CONTROLLER:
282+
str += "PABB2_MESSAGE_OPCODE_RESET_TO_CONTROLLER: id = ";
284283
str += std::to_string(header->id);
285284
return str;
286-
case PABB_MESSAGE_OPCODE_CONTROLLER_MAC_ADDRESS:
287-
str += "PABB_MESSAGE_OPCODE_CONTROLLER_MAC_ADDRESS: id = ";
285+
case PABB2_MESSAGE_OPCODE_CONTROLLER_MAC_ADDRESS:
286+
str += "PABB2_MESSAGE_OPCODE_CONTROLLER_MAC_ADDRESS: id = ";
288287
str += std::to_string(header->id);
289288
return str;
290-
case PABB_MESSAGE_OPCODE_PAIRED_MAC_ADDRESS:
291-
str += "PABB_MESSAGE_OPCODE_PAIRED_MAC_ADDRESS: id = ";
289+
case PABB2_MESSAGE_OPCODE_PAIRED_MAC_ADDRESS:
290+
str += "PABB2_MESSAGE_OPCODE_PAIRED_MAC_ADDRESS: id = ";
292291
str += std::to_string(header->id);
293292
return str;
294293

Common/PABotBase2/PABotBase2_MessageProtocol.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ struct PABB_PACK Message_u32{
5656
#define PABB2_MESSAGE_OPCODE_CONTROLLER_LIST 0x24
5757
#define PABB2_MESSAGE_OPCODE_CQ_CAPACITY 0x28
5858

59-
#define PABB_MESSAGE_OPCODE_READ_CONTROLLER_MODE 0x30
60-
#define PABB_MESSAGE_OPCODE_CHANGE_CONTROLLER_MODE 0x31
61-
#define PABB_MESSAGE_OPCODE_RESET_TO_CONTROLLER 0x32
62-
#define PABB_MESSAGE_OPCODE_CONTROLLER_MAC_ADDRESS 0x33
63-
#define PABB_MESSAGE_OPCODE_PAIRED_MAC_ADDRESS 0x34
59+
#define PABB2_MESSAGE_OPCODE_READ_CONTROLLER_MODE 0x30
60+
#define PABB2_MESSAGE_OPCODE_CHANGE_CONTROLLER_MODE 0x31
61+
#define PABB2_MESSAGE_OPCODE_RESET_TO_CONTROLLER 0x32
62+
#define PABB2_MESSAGE_OPCODE_CONTROLLER_MAC_ADDRESS 0x33
63+
#define PABB2_MESSAGE_OPCODE_PAIRED_MAC_ADDRESS 0x34
6464

6565
#define PABB2_MESSAGE_OPCODE_CQ_COMMAND_DROPPED 0x40
6666
#define PABB2_MESSAGE_OPCODE_CQ_CANCEL 0x41

Common/PABotBase2/ReliableConnectionLayer/PABotBase2FW_ReliableStreamConnection.cpp

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ ReliableStreamConnectionFW::ReliableStreamConnectionFW(UnreliableStreamConnectio
3232

3333

3434

35+
void ReliableStreamConnectionFW::reliable_send(const void* data, size_t bytes){
36+
if (!m_stream_ready){
37+
return;
38+
}
39+
const char* ptr = (const char*)data;
40+
while (bytes > 0){
41+
size_t sent = m_reliable_sender.send_stream(ptr, bytes);
42+
ptr += sent;
43+
bytes -= sent;
44+
}
45+
}
46+
47+
3548
void ReliableStreamConnectionFW::send_oob_info_str(const char* str){
3649
const size_t MAX_LENGTH = 256 - sizeof(PacketHeader) - sizeof(uint32_t);
3750
size_t len = strlen(str);
@@ -116,6 +129,7 @@ bool ReliableStreamConnectionFW::run_events(){
116129
m_stream_coalescer.reset();
117130
m_stream_coalescer.push_packet(0);
118131
m_reset_flag = true;
132+
m_stream_ready = false;
119133
return true;
120134
case PABB2_CONNECTION_OPCODE_ASK_VERSION:
121135
m_stream_coalescer.push_packet(packet->seqnum);
@@ -150,19 +164,16 @@ bool ReliableStreamConnectionFW::run_events(){
150164
);
151165
return true;
152166
case PABB2_CONNECTION_OPCODE_ASK_STREAM_DATA:
153-
// printf("Device: PABB2_CONNECTION_OPCODE_ASK_STREAM_DATA\n");
154-
if (m_stream_coalescer.push_stream((const PacketHeaderData*)packet)){
155-
// printf("Device: Succeeded push.\n");
156-
m_reliable_sender.send_oob_packet_u16(
157-
packet->seqnum,
158-
PABB2_CONNECTION_OPCODE_RET_STREAM_DATA,
159-
m_stream_coalescer.free_bytes()
160-
);
161-
}else{
162-
// printf("Device: Failed to push.\n");
163-
// m_stream_coalescer.print(true);
167+
m_stream_ready = true;
168+
if (!m_stream_coalescer.push_stream((const PacketHeaderData*)packet)){
169+
send_oob_info_str("Stream is full.");
170+
return true;
164171
}
165-
// fflush(stdout);
172+
m_reliable_sender.send_oob_packet_u16(
173+
packet->seqnum,
174+
PABB2_CONNECTION_OPCODE_RET_STREAM_DATA,
175+
m_stream_coalescer.free_bytes()
176+
);
166177
return true;
167178
case PABB2_CONNECTION_OPCODE_RET_STREAM_DATA:
168179
m_reliable_sender.remove(packet->seqnum);

Common/PABotBase2/ReliableConnectionLayer/PABotBase2FW_ReliableStreamConnection.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,7 @@ class ReliableStreamConnectionFW final : public ReliableStreamConnectionPolling{
4343
}
4444

4545
public:
46-
virtual void reliable_send(const void* data, size_t bytes) override{
47-
const char* ptr = (const char*)data;
48-
while (bytes > 0){
49-
size_t sent = m_reliable_sender.send_stream(ptr, bytes);
50-
ptr += sent;
51-
bytes -= sent;
52-
}
53-
}
46+
virtual void reliable_send(const void* data, size_t bytes) override;
5447
virtual size_t reliable_recv(void* data, size_t bytes) override{
5548
return m_stream_coalescer.read(data, bytes);
5649
}
@@ -61,11 +54,15 @@ class ReliableStreamConnectionFW final : public ReliableStreamConnectionPolling{
6154
virtual void clear_reset_flag() override{
6255
m_reset_flag = false;
6356
}
57+
6458
virtual bool run_events() override;
6559
virtual void wait_for_event(uint16_t milliseconds) override;
6660

6761

6862
public:
63+
// Send out-of-band messages.
64+
// These are not part of the reliable protocol and may be dropped.
65+
6966
void send_oob_info_u32(uint32_t data){
7067
m_reliable_sender.send_oob_packet_u32(0, PABB2_CONNECTION_OPCODE_INFO_U32, data);
7168
}
@@ -100,7 +97,12 @@ class ReliableStreamConnectionFW final : public ReliableStreamConnectionPolling{
10097
StreamCoalescer m_stream_coalescer;
10198

10299
WallClock m_last_retransmit;
100+
103101
bool m_reset_flag = false;
102+
103+
// Don't allow any stream traffic until CC is ready.
104+
// The MLC layer will get stuck in a bad state if we end up between packets.
105+
bool m_stream_ready = false;
104106
};
105107

106108

Common/PABotBase2/ReliableConnectionLayer/PABotBase2_PacketProtocol.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,11 @@ struct PABB_PACK PacketHeader_u32{
8989
#define PABB2_CONNECTION_OPCODE_ASK_BUFFER_BYTES 0x05
9090
#define PABB2_CONNECTION_OPCODE_RET_BUFFER_BYTES 0x45
9191

92-
#define PABB2_CONNECTION_OPCODE_ASK_STREAM_DATA 0x10
93-
#define PABB2_CONNECTION_OPCODE_RET_STREAM_DATA 0x50
94-
#define PABB2_CONNECTION_OPCODE_ASK_STREAM_REQUEST 0x11
92+
#define PABB2_CONNECTION_OPCODE_ASK_STREAM_READY 0x10
93+
#define PABB2_CONNECTION_OPCODE_RET_STREAM_READY 0x50
94+
#define PABB2_CONNECTION_OPCODE_ASK_STREAM_DATA 0x12
95+
#define PABB2_CONNECTION_OPCODE_RET_STREAM_DATA 0x52
96+
#define PABB2_CONNECTION_OPCODE_ASK_STREAM_REQUEST 0x13 // Unused for now.
9597
struct PABB_PACK PacketHeaderData{
9698
uint8_t magic_number;
9799
uint8_t seqnum;

Common/PABotBase2/ReliableConnectionLayer/PABotBase2_StreamCoalescer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ uint16_t StreamCoalescer::free_bytes() const{
106106
if (m_slot_head == m_slot_tail){
107107
return BUFFER_SIZE;
108108
}
109-
return (m_stream_tail - m_stream_head) & BUFFER_MASK;
109+
return (m_stream_head - m_stream_tail) & BUFFER_MASK;
110110
}
111111

112112
void StreamCoalescer::push_packet(uint8_t seqnum){

SerialPrograms/Source/Controllers/PABotBase2/PABotBase2_DeviceHandle.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ void DeviceHandle::query_protocol(){
8787
}
8888
{
8989
m_device_id = query_u32(PABB2_MESSAGE_OPCODE_DEVICE_IDENTIFIER);
90-
m_logger.log("[MLC]: Device ID: " + tostr_hex(m_device_id), COLOR_BLUE);
90+
m_logger.log("[MLC]: Device ID: 0x" + tostr_hex(m_device_id), COLOR_BLUE);
9191
auto iter = PROGRAMS->find(m_device_id);
9292
if (iter == PROGRAMS->end()){
9393
m_logger.Logger::log(
@@ -161,7 +161,7 @@ void DeviceHandle::connect(){
161161

162162
ControllerType DeviceHandle::refresh_controller_type(){
163163
m_logger.log("Reading Controller Mode...");
164-
uint32_t type_id = query_u32(PABB_MESSAGE_OPCODE_READ_CONTROLLER_MODE);
164+
uint32_t type_id = query_u32(PABB2_MESSAGE_OPCODE_READ_CONTROLLER_MODE);
165165

166166
ControllerType current_controller = SerialPABotBase::id_to_controller_type(type_id);
167167
m_logger.log(

0 commit comments

Comments
 (0)