Skip to content

Commit 18ba017

Browse files
committed
Status updates for AVR and ESP32-S3.
1 parent 37142dc commit 18ba017

File tree

8 files changed

+96
-67
lines changed

8 files changed

+96
-67
lines changed

Common/SerialPABotBase/SerialPABotBase_Messages_ESP32.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,6 @@ typedef struct{
6060

6161

6262

63-
64-
#define PABB_MSG_ESP32_REQUEST_STATUS 0x50
65-
typedef struct{
66-
seqnum_t seqnum;
67-
} PABB_PACK pabb_Message_ESP32_RequestStatus;
68-
69-
70-
7163
#define PABB_MSG_ESP32_REQUEST_READ_SPI 0x60
7264
typedef struct{
7365
seqnum_t seqnum;

Common/SerialPABotBase/SerialPABotBase_Protocol.h

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,122 +1,122 @@
11
/* Serial PABotBase Message Protocol
2-
*
2+
*
33
* From: https://github.com/PokemonAutomation/
44
*
55
*
66
* Pokemon Automation Bot-Base implements reliable data transmissions over
77
* serial communication. This is done by checksumming messages along with a
88
* protocol that is tolerant to data drops.
9-
*
9+
*
1010
* This file describes the message protocol. The data being transmitted is raw
1111
* binary and not is readable text.
12-
*
13-
*
12+
*
13+
*
1414
* Message Format:
1515
* byte 0: Length of the entire message. (bits are inverted)
1616
* byte 1: Message Type
1717
* byte X: Optional data of variable length.
1818
* Last 4 bytes: CRC32C of the entire message except these last 4 bytes.
19-
*
19+
*
2020
* Thus there are 6 bytes of overhead for each message.
21-
*
22-
*
21+
*
22+
*
2323
* There are currently 4 categories of message types:
24-
*
24+
*
2525
* 1. Info: These are simple one-way messages. They do not need to be
2626
* acked and may be dropped without adversely affecting anything.
27-
*
27+
*
2828
* 2. Ack/Response: These are messages sent in response to an earlier
2929
* message that was received.
30-
*
30+
*
3131
* 3. Request: The sender requests the receiver to do something simple.
3232
* The receiver must respond with an ack.
33-
*
33+
*
3434
* This is used for things that take no clock time. For example, querying
3535
* for program identifiers, turning on/off LEDs, or setting flags in the
3636
* program to change its behavior in the future.
37-
*
37+
*
3838
* 4. Command: The sender requests the receiver to do a large asynchronous
3939
* operation. The receiver must ack this message. Once the command is
4040
* finished, the receiver must send a request message back to the sender
4141
* to indicate that the command is finished.
42-
*
42+
*
4343
* This is used for issuing button presses or other subroutines that
4444
* consume time.
45-
*
46-
*
45+
*
46+
*
4747
* General Protocol:
48-
*
48+
*
4949
* 1. Every time you send a new request/command message, you increment
5050
* your sequence number (seqnum) by 1.
51-
*
51+
*
5252
* 2. If you receive an invalid message (bad length or bad checksum), ignore
5353
* the first byte and attempt to parse the next byte as the start of a
5454
* new message.
55-
*
55+
*
5656
* 3. If you receive a zero for the 1st byte of a message, ignore it and
5757
* attempt to parse the next byte as the start of a new message.
58-
*
58+
*
5959
* 4. At any point, you can send a bunch of zero bytes. This will cause
6060
* the receiver to re-synchronize.
61-
*
61+
*
6262
* 5. If you receive a request/command message, you must send the appropriate
6363
* ack/response message using the same seqnum.
64-
*
64+
*
6565
* 6. If you receive a command message, you must first ack the message itself.
6666
* Once the command is finished, you must send a request referencing the
6767
* command to indicate that it is finished. You will receive an ack for
6868
* this short (finishing) command, and if you don't, send it again until
6969
* you do. If the command finishes immediately, you can skip the ack and
7070
* just send the finish request.
71-
*
71+
*
7272
* 7. If you send a request/command message and don't get a response after
7373
* a time limit, you should resend the message with the same seqnum.
74-
*
74+
*
7575
* 8. If you receive a request/command that has a seqnum ahead of what you
7676
* are expecting, it means an earlier request/command was dropped.
7777
* Do not process the request/command since you will lose ordering.
78-
*
78+
*
7979
* 9. If you receive a request/command that has an old seqnum, it is a
8080
* retransmit. Send an ack for it, but don't process it again. (idempotency)
81-
*
82-
*
81+
*
82+
*
8383
* Failure Analysis:
84-
*
84+
*
8585
* - Corrupted messages will either fail checksum or will have an invalid
8686
* length/type. These are simply ignored and dropped.
87-
*
87+
*
8888
* - If either sender or receiver gets out-of-sync and loses track of
8989
* message boundaries, it will eventually find the boundary again by
9090
* simply trying to parse every byte as the start of a new message and
9191
* verifying the length and CRC.
92-
*
92+
*
9393
* - If a request/command is dropped, no ack will be received. The sender
9494
* will eventually send the command again. (#7)
95-
*
95+
*
9696
* - If an ack is dropped, the sender will eventually resend the
9797
* request/command again. The receiver will see the duplicate
9898
* request/command and ack it. But the receiver will not process it
9999
* again to preserve idempotency. (#9)
100-
*
100+
*
101101
* The current protocol guarantees that all commands are processed in order
102102
* exactly once. Requests are not guaranteed to process in order and may execute
103103
* more than once so they should be idempotent.
104-
*
104+
*
105105
* The protocol also allows both sides to queue up requests and commands.
106106
* In other words, it is possible to send multiple requests/commands at once
107107
* without waiting for the individual acks.
108-
*
109-
*
108+
*
109+
*
110110
* PABotBase Specifics:
111111
*
112-
* - PABotBase can queue 4 commands. Some implementations can handle
113-
* more. If it receives any commands while the queue is full, it drops
114-
* it and responds with "PABB_MSG_ERROR_COMMAND_DROPPED".
115-
* (Any command that results in a button press or a wait is a long command.)
116-
*
112+
* - PABotBase can queue up to 4 commands. If it receives any commands
113+
* while the queue is full, it drops it and responds with
114+
* "PABB_MSG_ERROR_COMMAND_DROPPED". (Any command that results in a
115+
* button press or a wait is a long command.)
116+
*
117117
* - PABotBase can still handle other messages while it is running a long
118118
* command.
119-
*
119+
*
120120
*/
121121

122122
#ifndef PokemonAutomation_SerialPABotBase_Protocol_H
@@ -208,6 +208,7 @@ typedef struct{
208208

209209
////////////////////////////////////////////////////////////////////////////////
210210
// Ack
211+
211212
#define PABB_MSG_ACK_COMMAND 0x10
212213
typedef struct{
213214
seqnum_t seqnum;
@@ -265,6 +266,7 @@ typedef struct{
265266

266267
////////////////////////////////////////////////////////////////////////////////
267268
// Requests
269+
268270
#define PABB_MSG_SEQNUM_RESET 0x40
269271
// After you send this message, the next seqnum you should use is (seqnum + 1).
270272
typedef struct{
@@ -325,6 +327,14 @@ typedef struct{
325327
uint32_t mode;
326328
} PABB_PACK pabb_MsgRequestChangeControllerMode;
327329

330+
////////////////////////////////////////////////////////////////////////////////
331+
// Common Requests
332+
333+
#define PABB_MSG_REQUEST_STATUS 0x50
334+
typedef struct{
335+
seqnum_t seqnum;
336+
} PABB_PACK pabb_Message_RequestStatus;
337+
328338
////////////////////////////////////////////////////////////////////////////////
329339
// Commands
330340

SerialPrograms/Source/Controllers/SerialPABotBase/SerialPABotBase.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ const std::map<
111111
}},
112112
}},
113113
}},
114-
{2025061302, {
114+
{2025061303, {
115115
{PABB_PID_PABOTBASE_ESP32S3, {
116116
{ControllerType::NintendoSwitch_WiredProController, {
117117
ControllerFeature::TickPrecise,
@@ -121,7 +121,7 @@ const std::map<
121121
}},
122122
}},
123123
}},
124-
{2025061402, {
124+
{2025061403, {
125125
{PABB_PID_PABOTBASE_ArduinoUnoR3, {
126126
{ControllerType::NintendoSwitch_WiredProController, {
127127
ControllerFeature::TickPrecise,

SerialPrograms/Source/Controllers/SerialPABotBase/SerialPABotBase_Routines_ESP32.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ namespace SerialPABotBase{
1717

1818
int register_message_converters_ESP32(){
1919
register_message_converter(
20-
PABB_MSG_ESP32_REQUEST_STATUS,
20+
PABB_MSG_REQUEST_STATUS,
2121
[](const std::string& body){
2222
// Disable this by default since it's very spammy.
2323
if (!GlobalSettings::instance().LOG_EVERYTHING){
2424
return std::string();
2525
}
2626
std::ostringstream ss;
27-
ss << "PABB_MSG_ESP32_REQUEST_STATUS() - ";
28-
if (body.size() != sizeof(pabb_Message_ESP32_RequestStatus)){ ss << "(invalid size)" << std::endl; return ss.str(); }
29-
const auto* params = (const pabb_Message_ESP32_RequestStatus*)body.c_str();
27+
ss << "PABB_MSG_REQUEST_STATUS() - ";
28+
if (body.size() != sizeof(pabb_Message_RequestStatus)){ ss << "(invalid size)" << std::endl; return ss.str(); }
29+
const auto* params = (const pabb_Message_RequestStatus*)body.c_str();
3030
ss << "seqnum = " << (uint64_t)params->seqnum;
3131
return ss.str();
3232
}

SerialPrograms/Source/Controllers/SerialPABotBase/SerialPABotBase_Routines_ESP32.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,6 @@ namespace SerialPABotBase{
1818

1919

2020

21-
class MessageControllerStatus : public BotBaseRequest{
22-
public:
23-
pabb_Message_ESP32_RequestStatus params;
24-
MessageControllerStatus()
25-
: BotBaseRequest(false)
26-
{
27-
params.seqnum = 0;
28-
}
29-
virtual BotBaseMessage message() const override{
30-
return BotBaseMessage(PABB_MSG_ESP32_REQUEST_STATUS, params);
31-
}
32-
};
3321
class MessageControllerReadSpi : public BotBaseRequest{
3422
public:
3523
pabb_Message_ESP32_ReadSpi params;

SerialPrograms/Source/Controllers/SerialPABotBase/SerialPABotBase_Routines_Protocol.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,18 @@ class DeviceRequest_change_controller_mode : public BotBaseRequest{
126126
return BotBaseMessage(PABB_MSG_REQUEST_CHANGE_CONTROLLER_MODE, params);
127127
}
128128
};
129+
class MessageControllerStatus : public BotBaseRequest{
130+
public:
131+
pabb_Message_RequestStatus params;
132+
MessageControllerStatus()
133+
: BotBaseRequest(false)
134+
{
135+
params.seqnum = 0;
136+
}
137+
virtual BotBaseMessage message() const override{
138+
return BotBaseMessage(PABB_MSG_REQUEST_STATUS, params);
139+
}
140+
};
129141

130142

131143

SerialPrograms/Source/NintendoSwitch/Controllers/SerialPABotBase/NintendoSwitch_SerialPABotBase_PokkenController.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,19 +354,45 @@ void SerialPABotBase_PokkenController::status_thread(){
354354

355355
std::string error;
356356
try{
357+
pabb_MsgAckRequestI32 response;
358+
m_serial->issue_request_and_wait(
359+
SerialPABotBase::MessageControllerStatus(),
360+
&m_scope
361+
).convert<PABB_MSG_ACK_REQUEST_I32>(m_logger, response);
362+
last_ack.store(current_time(), std::memory_order_relaxed);
363+
364+
uint32_t status = response.data;
365+
bool status_connected = status & 1;
366+
bool status_ready = status & 2;
367+
368+
std::string str;
369+
str += "Connected: " + (status_connected
370+
? html_color_text("Yes", theme_friendly_darkblue())
371+
: html_color_text("No", COLOR_RED)
372+
);
373+
str += " - Ready: " + (status_ready
374+
? html_color_text("Yes", theme_friendly_darkblue())
375+
: html_color_text("No", COLOR_RED)
376+
);
377+
378+
m_handle.set_status_line1(str);
379+
380+
381+
#if 0
357382
pabb_MsgAckRequestI32 response;
358383
m_serial->issue_request_and_wait(
359384
SerialPABotBase::DeviceRequest_system_clock(),
360385
&m_scope
361386
).convert<PABB_MSG_ACK_REQUEST_I32>(logger(), response);
362387
last_ack.store(current_time(), std::memory_order_relaxed);
388+
363389
uint64_t wallclock = clock_tracker.push_short_value(response.data);
364390
// double ticks_per_second = tick_rate_tracker.push_ticks(wallclock);
365-
366391
m_handle.set_status_line1(
367392
"Device Clock: " + tostr_u_commas(wallclock),
368393
theme_friendly_darkblue()
369394
);
395+
#endif
370396

371397
// if (tick_rate_tracker.consecutive_off_readings() >= 10){
372398
// error = "Tick rate is erratic. Arduino/Teensy is not reliable on Switch 2.";

SerialPrograms/Source/NintendoSwitch/Controllers/SerialPABotBase/NintendoSwitch_SerialPABotBase_WirelessController.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "Common/Cpp/PrettyPrint.h"
88
#include "Common/Cpp/Concurrency/ReverseLockGuard.h"
99
#include "CommonFramework/Options/Environment/ThemeSelectorOption.h"
10+
#include "Controllers/SerialPABotBase/SerialPABotBase_Routines_Protocol.h"
1011
#include "Controllers/SerialPABotBase/SerialPABotBase_Routines_ESP32.h"
1112
#include "NintendoSwitch/NintendoSwitch_Settings.h"
1213
#include "NintendoSwitch_SerialPABotBase_WirelessController.h"

0 commit comments

Comments
 (0)