Skip to content

Commit 6cf546f

Browse files
committed
PABotBase2
1 parent 67690e7 commit 6cf546f

6 files changed

Lines changed: 48 additions & 11 deletions

File tree

Common/Cpp/StreamConnections/MockDevice.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111
using std::cout;
1212
using std::endl;
1313

14-
14+
#if 0
1515
#define PABB2_DROP_HOST_TO_DEVICE 0.2
1616
#define PABB2_DROP_DEVICE_TO_HOST 0.2
17-
17+
#else
18+
#define PABB2_DROP_HOST_TO_DEVICE 0
19+
#define PABB2_DROP_DEVICE_TO_HOST 0
20+
#endif
1821

1922
namespace PokemonAutomation{
2023

Common/Cpp/StreamConnections/ReliableStreamConnection.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@ void ReliableStreamConnection::wait_for_pending(){
8282
// Send Path
8383
//
8484

85+
void ReliableStreamConnection::reset(){
86+
{
87+
std::lock_guard<std::mutex> lg(m_lock);
88+
pabb2_PacketSender_reset(&m_reliable_sender);
89+
pabb2_PacketParser_reset(&m_parser);
90+
pabb2_StreamCoalescer_reset(&m_stream_coalescer);
91+
throw_if_cancelled();
92+
pabb2_PacketSender_send_packet(&m_reliable_sender, PABB2_CONNECTION_OPCODE_ASK_RESET, 0, nullptr);
93+
}
94+
m_cv.notify_all();
95+
wait_for_pending();
96+
}
8597
size_t ReliableStreamConnection::send(const void* data, size_t bytes){
8698
std::lock_guard<std::mutex> lg(m_lock);
8799
throw_if_cancelled();

Common/Cpp/StreamConnections/ReliableStreamConnection.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <condition_variable>
1212
#include "Common/Cpp/Time.h"
1313
#include "Common/Cpp/CancellableScope.h"
14-
#include "Common/Cpp/Logging/TaggedLogger.h"
14+
#include "Common/Cpp/Logging/AbstractLogger.h"
1515
#include "Common/Cpp/Concurrency/Thread.h"
1616
#include "Common/PABotBase2/PABotBase2_PacketSender.h"
1717
#include "Common/PABotBase2/PABotBase2_PacketParser.h"
@@ -42,12 +42,14 @@ class ReliableStreamConnection final
4242
virtual bool cancel(std::exception_ptr exception) noexcept override;
4343
void wait_for_pending();
4444

45+
void reset();
46+
4547
// Send stream data.
4648
virtual size_t send(const void* data, size_t bytes) override;
4749

4850
bool try_send_request(uint8_t opcode);
4951
void send_request(uint8_t opcode);
50-
// void verify_version();
52+
5153

5254

5355
private:

Common/PABotBase2/PABotBase2_StreamCoalescer.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
#include <string.h>
88
#include "PABotBase2_StreamCoalescer.h"
99

10-
#include <stdio.h> // REMOVE
11-
#include "PABotBase2_ConnectionDebug.h" // REMOVE
10+
//#include <stdio.h> // REMOVE
11+
//#include "PABotBase2_ConnectionDebug.h" // REMOVE
1212

1313
void pabb2_StreamCoalescer_init(pabb2_StreamCoalescer* self){
14+
// printf("pabb2_StreamCoalescer_init(%p)\n", self);
1415
self->slot_head = 0;
1516
self->slot_tail = 0;
1617
self->stream_head = 0;
@@ -64,8 +65,11 @@ void pabb2_StreamCoalescer_pop_leading_finished(pabb2_StreamCoalescer* self){
6465
uint8_t slot_head = self->slot_head;
6566
uint8_t slot_tail = self->slot_tail;
6667

68+
// printf("pabb2_StreamCoalescer_pop_leading_finished()\n");
69+
6770
while (slot_head != slot_tail){
6871
uint8_t* slot = &self->lengths[slot_head & PABB2_StreamCoalescer_SLOTS_MASK];
72+
// printf("slot[%d] = %d\n", slot_head, *slot);
6973
if (*slot != 0xff){
7074
break;
7175
}
@@ -90,8 +94,11 @@ void pabb2_StreamCoalescer_push_packet(pabb2_StreamCoalescer* self, uint8_t seqn
9094
// printf("enter ---------------------\n");
9195
// pabb2_StreamCoalescer_print(self, false);
9296

97+
// printf("pabb2_StreamCoalescer_push_packet(%p): seqnum = %d, slot_head = %d\n", self, seqnum, slot_head);
98+
9399
// Either before (old retransmit) or too far in future.
94100
if ((uint8_t)(seqnum - slot_head) >= PABB2_StreamCoalescer_SLOTS){
101+
// printf("Device: Packet is out of range.\n");
95102
return;
96103
}
97104

@@ -118,12 +125,14 @@ bool pabb2_StreamCoalescer_push_stream(pabb2_StreamCoalescer* self, const pabb2_
118125

119126
// Zero does not fall through cleanly. So handle it here.
120127
if (stream_size == 0){
128+
// printf("Device: stream_size == 0\n");
121129
pabb2_StreamCoalescer_push_packet(self, packet->seqnum);
122130
return true;
123131
}
124132

125133
// Data is larger than the entire buffer.
126134
if (stream_size > PABB2_StreamCoalescer_BUFFER_SIZE){
135+
// printf("Device: stream_size > PABB2_StreamCoalescer_BUFFER_SIZE\n");
127136
return false;
128137
}
129138

@@ -133,7 +142,9 @@ bool pabb2_StreamCoalescer_push_stream(pabb2_StreamCoalescer* self, const pabb2_
133142
// Either before (old retransmit) or too far in future.
134143
{
135144
uint8_t diff = seqnum - slot_head;
145+
// printf("seqnum = %d, slot_head = %d\n", seqnum, slot_head);
136146
if (diff >= PABB2_StreamCoalescer_SLOTS){
147+
// printf("Device: In the past.\n");
137148
// Negative means we're in the past and we can just ack.
138149
return diff & 0x80;
139150
}
@@ -146,6 +157,7 @@ bool pabb2_StreamCoalescer_push_stream(pabb2_StreamCoalescer* self, const pabb2_
146157

147158
// Too far ahead that it's beyond our window.
148159
if (stream_offset_e - self->stream_head > PABB2_StreamCoalescer_BUFFER_SIZE){
160+
// printf("Device: To far in future.\n");
149161
return false;
150162
}
151163

Common/PABotBase2/PABotbase2_ReliableStreamConnection.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,10 @@ void pabb2_ReliableStreamConnection_run_events(pabb2_ReliableStreamConnection* s
7474
pabb2_PacketSender_reset(&self->reliable_sender);
7575
pabb2_PacketParser_reset(&self->parser);
7676
pabb2_StreamCoalescer_reset(&self->stream_coalescer);
77+
pabb2_StreamCoalescer_push_packet(&self->stream_coalescer, 0);
7778
return;
7879
case PABB2_CONNECTION_OPCODE_ASK_VERSION:
80+
pabb2_StreamCoalescer_push_packet(&self->stream_coalescer, packet->seqnum);
7981
pabb2_PacketSender_send_ack_u32(
8082
&self->reliable_sender,
8183
packet->seqnum,
@@ -84,6 +86,7 @@ void pabb2_ReliableStreamConnection_run_events(pabb2_ReliableStreamConnection* s
8486
);
8587
return;
8688
case PABB2_CONNECTION_OPCODE_ASK_PACKET_SIZE:
89+
pabb2_StreamCoalescer_push_packet(&self->stream_coalescer, packet->seqnum);
8790
pabb2_PacketSender_send_ack_u16(
8891
&self->reliable_sender,
8992
packet->seqnum,
@@ -92,6 +95,7 @@ void pabb2_ReliableStreamConnection_run_events(pabb2_ReliableStreamConnection* s
9295
);
9396
return;
9497
case PABB2_CONNECTION_OPCODE_ASK_BUFFER_SLOTS:
98+
pabb2_StreamCoalescer_push_packet(&self->stream_coalescer, packet->seqnum);
9599
pabb2_PacketSender_send_ack_u8(
96100
&self->reliable_sender,
97101
packet->seqnum,
@@ -100,6 +104,7 @@ void pabb2_ReliableStreamConnection_run_events(pabb2_ReliableStreamConnection* s
100104
);
101105
return;
102106
case PABB2_CONNECTION_OPCODE_ASK_BUFFER_BYTES:
107+
pabb2_StreamCoalescer_push_packet(&self->stream_coalescer, packet->seqnum);
103108
pabb2_PacketSender_send_ack_u16(
104109
&self->reliable_sender,
105110
packet->seqnum,
@@ -108,14 +113,19 @@ void pabb2_ReliableStreamConnection_run_events(pabb2_ReliableStreamConnection* s
108113
);
109114
return;
110115
case PABB2_CONNECTION_OPCODE_ASK_STREAM_DATA:
116+
// printf("Device: PABB2_CONNECTION_OPCODE_ASK_STREAM_DATA\n");
111117
if (pabb2_StreamCoalescer_push_stream(&self->stream_coalescer, (const pabb2_PacketHeaderData*)packet)){
118+
printf("Device: Succeeded push.\n");
112119
pabb2_PacketSender_send_ack_u16(
113120
&self->reliable_sender,
114121
packet->seqnum,
115122
PABB2_CONNECTION_OPCODE_RET_STREAM_DATA,
116123
pabb2_StreamCoalescer_bytes_available(&self->stream_coalescer)
117124
);
125+
}else{
126+
printf("Device: Failed to push.\n");
118127
}
128+
fflush(stdout);
119129
return;
120130
default:
121131
pabb2_PacketSender_send_info(

SerialPrograms/Source/NintendoSwitch/DevPrograms/TestProgramComputer.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,8 @@ void TestProgramComputer::program(ProgramEnvironment& env, CancellableScope& sco
311311
1s
312312
);
313313

314+
connection.reset();
315+
314316
connection.send_request(PABB2_CONNECTION_OPCODE_ASK_VERSION);
315317
connection.wait_for_pending();
316318

@@ -320,12 +322,8 @@ void TestProgramComputer::program(ProgramEnvironment& env, CancellableScope& sco
320322
connection.send_request(PABB2_CONNECTION_OPCODE_ASK_BUFFER_SLOTS);
321323
connection.wait_for_pending();
322324

323-
connection.send_request(PABB2_CONNECTION_OPCODE_ASK_RESET);
324-
connection.wait_for_pending();
325-
326325

327-
connection.send_request(PABB2_CONNECTION_OPCODE_ASK_VERSION);
328-
connection.wait_for_pending();
326+
connection.send("asdf", 4);
329327

330328

331329
scope.wait_for(60s);

0 commit comments

Comments
 (0)