|
| 1 | +/* PABotBase2 Message Dumper |
| 2 | + * |
| 3 | + * From: https://github.com/PokemonAutomation/ |
| 4 | + * |
| 5 | + */ |
| 6 | + |
| 7 | +#include "Common/Cpp/PrettyPrint.h" |
| 8 | +#include "PABotBase2CC_MessageDumper.h" |
| 9 | + |
| 10 | +namespace PokemonAutomation{ |
| 11 | +namespace PABotBase2{ |
| 12 | + |
| 13 | + |
| 14 | +std::string tostr(const PacketHeader* header){ |
| 15 | + // TODO: Make sure packets are large enough before reading them. |
| 16 | + |
| 17 | + std::string str; |
| 18 | + if (header->opcode & PABB2_CONNECTION_RETRANSMIT_FLAG){ |
| 19 | + str += "(Retransmit) "; |
| 20 | + } |
| 21 | + switch (header->opcode & PABB2_CONNECTION_OPCODE_MASK){ |
| 22 | + case PABB2_CONNECTION_OPCODE_INVALID: |
| 23 | + str += "PABB2_CONNECTION_OPCODE_INVALID: seqnum = "; |
| 24 | + str += std::to_string(header->seqnum); |
| 25 | + return str; |
| 26 | + |
| 27 | + case PABB2_CONNECTION_OPCODE_ASK_RESET: |
| 28 | + str += "PABB2_CONNECTION_OPCODE_ASK_RESET: seqnum = "; |
| 29 | + str += std::to_string(header->seqnum); |
| 30 | + return str; |
| 31 | + case PABB2_CONNECTION_OPCODE_RET_RESET: |
| 32 | + str += "PABB2_CONNECTION_OPCODE_RET_RESET: seqnum = "; |
| 33 | + str += std::to_string(header->seqnum); |
| 34 | + return str; |
| 35 | + |
| 36 | + case PABB2_CONNECTION_OPCODE_ASK_VERSION: |
| 37 | + str += "PABB2_CONNECTION_OPCODE_ASK_VERSION: seqnum = "; |
| 38 | + str += std::to_string(header->seqnum); |
| 39 | + return str; |
| 40 | + case PABB2_CONNECTION_OPCODE_RET_VERSION: |
| 41 | + str += "PABB2_CONNECTION_OPCODE_RET_VERSION: seqnum = "; |
| 42 | + str += std::to_string(header->seqnum); |
| 43 | + str += ", version = " + std::to_string(((const PacketHeader_Ack_u32*)header)->data); |
| 44 | + return str; |
| 45 | + |
| 46 | + case PABB2_CONNECTION_OPCODE_ASK_PACKET_SIZE: |
| 47 | + str += "PABB2_CONNECTION_OPCODE_ASK_PACKET_SIZE: seqnum = "; |
| 48 | + str += std::to_string(header->seqnum); |
| 49 | + return str; |
| 50 | + case PABB2_CONNECTION_OPCODE_RET_PACKET_SIZE: |
| 51 | + str += "PABB2_CONNECTION_OPCODE_RET_PACKET_SIZE: seqnum = "; |
| 52 | + str += std::to_string(header->seqnum); |
| 53 | + str += ", bytes = " + std::to_string(((const PacketHeader_Ack_u16*)header)->data); |
| 54 | + return str; |
| 55 | + |
| 56 | + case PABB2_CONNECTION_OPCODE_ASK_BUFFER_SLOTS: |
| 57 | + str += "PABB2_CONNECTION_OPCODE_ASK_BUFFER_SLOTS: seqnum = "; |
| 58 | + str += std::to_string(header->seqnum); |
| 59 | + return str; |
| 60 | + case PABB2_CONNECTION_OPCODE_RET_BUFFER_SLOTS: |
| 61 | + str += "PABB2_CONNECTION_OPCODE_RET_BUFFER_SLOTS: seqnum = "; |
| 62 | + str += std::to_string(header->seqnum); |
| 63 | + str += ", slots = " + std::to_string(((const PacketHeader_Ack_u8*)header)->data); |
| 64 | + return str; |
| 65 | + |
| 66 | + case PABB2_CONNECTION_OPCODE_ASK_BUFFER_BYTES: |
| 67 | + str += "PABB2_CONNECTION_OPCODE_ASK_BUFFER_BYTES: seqnum = "; |
| 68 | + str += std::to_string(header->seqnum); |
| 69 | + return str; |
| 70 | + case PABB2_CONNECTION_OPCODE_RET_BUFFER_BYTES: |
| 71 | + str += "PABB2_CONNECTION_OPCODE_RET_BUFFER_BYTES: seqnum = "; |
| 72 | + str += std::to_string(header->seqnum); |
| 73 | + str += ", bytes = " + std::to_string(((const PacketHeader_Ack_u16*)header)->data); |
| 74 | + return str; |
| 75 | + |
| 76 | + case PABB2_CONNECTION_OPCODE_ASK_STREAM_DATA: |
| 77 | + str += "PABB2_CONNECTION_OPCODE_ASK_STREAM_DATA: seqnum = "; |
| 78 | + str += std::to_string(header->seqnum); |
| 79 | + str += ", offset = " + std::to_string(((const PacketHeaderData*)header)->stream_offset); |
| 80 | + str += ", bytes = " + std::to_string(header->packet_bytes - sizeof(PacketHeaderData) - sizeof(uint32_t)); |
| 81 | + return str; |
| 82 | + case PABB2_CONNECTION_OPCODE_RET_STREAM_DATA: |
| 83 | + str += "PABB2_CONNECTION_OPCODE_RET_STREAM_REQUEST: seqnum = "; |
| 84 | + str += std::to_string(header->seqnum); |
| 85 | + return str; |
| 86 | + case PABB2_CONNECTION_OPCODE_ASK_STREAM_REQUEST: |
| 87 | + str += "PABB2_CONNECTION_OPCODE_RET_STREAM: seqnum = "; |
| 88 | + str += std::to_string(header->seqnum); |
| 89 | + str += ", offset = " + std::to_string(((const PacketHeaderData*)header)->stream_offset); |
| 90 | + return str; |
| 91 | + |
| 92 | + case PABB2_CONNECTION_OPCODE_INFO: |
| 93 | + str += "PABB2_CONNECTION_OPCODE_INFO: seqnum = "; |
| 94 | + str += std::to_string(header->seqnum); |
| 95 | + return str; |
| 96 | + case PABB2_CONNECTION_OPCODE_INFO_U8: |
| 97 | + str += "PABB2_CONNECTION_OPCODE_INFO_U8: seqnum = "; |
| 98 | + str += std::to_string(header->seqnum); |
| 99 | + str += ", data = " + std::to_string(((const PacketHeader_Ack_u8*)header)->data); |
| 100 | + return str; |
| 101 | + case PABB2_CONNECTION_OPCODE_INFO_U16: |
| 102 | + str += "PABB2_CONNECTION_OPCODE_INFO_U16: seqnum = "; |
| 103 | + str += std::to_string(header->seqnum); |
| 104 | + str += ", data = " + std::to_string(((const PacketHeader_Ack_u16*)header)->data); |
| 105 | + return str; |
| 106 | + case PABB2_CONNECTION_OPCODE_INFO_U32: |
| 107 | + str += "PABB2_CONNECTION_OPCODE_INFO_U32: seqnum = "; |
| 108 | + str += std::to_string(header->seqnum); |
| 109 | + str += ", data = " + std::to_string(((const PacketHeader_Ack_u32*)header)->data); |
| 110 | + return str; |
| 111 | + |
| 112 | + case PABB2_CONNECTION_OPCODE_INVALID_LENGTH: |
| 113 | + str += "PABB2_CONNECTION_OPCODE_INVALID_LENGTH: seqnum = "; |
| 114 | + str += std::to_string(header->seqnum); |
| 115 | + return str; |
| 116 | + case PABB2_CONNECTION_OPCODE_INVALID_CHECKSUM_FAIL: |
| 117 | + str += "PABB2_CONNECTION_OPCODE_INVALID_CHECKSUM_FAIL: seqnum = "; |
| 118 | + str += std::to_string(header->seqnum); |
| 119 | + return str; |
| 120 | + case PABB2_CONNECTION_OPCODE_UNKNOWN_OPCODE: |
| 121 | + str += "PABB2_CONNECTION_OPCODE_UNKNOWN_OPCODE: seqnum = "; |
| 122 | + str += std::to_string(header->seqnum); |
| 123 | + return str; |
| 124 | + } |
| 125 | + return "Unknown Packet Opcode: 0x" + tostr_hex(header->opcode); |
| 126 | +} |
| 127 | + |
| 128 | +std::string tostr(const MessageHeader* header){ |
| 129 | + // TODO: Make sure packets are large enough before reading them. |
| 130 | + |
| 131 | + std::string str; |
| 132 | + switch (header->opcode & PABB2_CONNECTION_OPCODE_MASK){ |
| 133 | + case PABB2_MESSAGE_OPCODE_INVALID: |
| 134 | + str += "PABB2_MESSAGE_OPCODE_INVALID: id = "; |
| 135 | + str += std::to_string(header->id); |
| 136 | + return str; |
| 137 | + case PABB2_MESSAGE_OPCODE_LOG_STRING: |
| 138 | + str += "PABB2_MESSAGE_OPCODE_LOG_STRING: id = "; |
| 139 | + str += std::to_string(header->id); |
| 140 | + str += ": " + std::string( |
| 141 | + (const char*)(header + 1), |
| 142 | + header->message_bytes - sizeof(MessageHeader) |
| 143 | + ); |
| 144 | + return str; |
| 145 | + case PABB2_MESSAGE_OPCODE_LOG_LABEL_H32:{ |
| 146 | + const Message_u32* message = (const Message_u32*)header; |
| 147 | + str += "PABB2_MESSAGE_OPCODE_LOG_LABEL_U32: id = "; |
| 148 | + str += std::to_string(message->id); |
| 149 | + str += ": " + std::string( |
| 150 | + (const char*)(message + 1), |
| 151 | + message->message_bytes - sizeof(Message_u32) |
| 152 | + ); |
| 153 | + str += ": "; |
| 154 | + str += tostr_hex(message->data); |
| 155 | + return str; |
| 156 | + } |
| 157 | + case PABB2_MESSAGE_OPCODE_LOG_LABEL_U32:{ |
| 158 | + const Message_u32* message = (const Message_u32*)header; |
| 159 | + str += "PABB2_MESSAGE_OPCODE_LOG_LABEL_U32: id = "; |
| 160 | + str += std::to_string(message->id); |
| 161 | + str += ": " + std::string( |
| 162 | + (const char*)(message + 1), |
| 163 | + message->message_bytes - sizeof(Message_u32) |
| 164 | + ); |
| 165 | + str += ": "; |
| 166 | + str += std::to_string((uint32_t)message->data); |
| 167 | + return str; |
| 168 | + } |
| 169 | + case PABB2_MESSAGE_OPCODE_LOG_LABEL_I32:{ |
| 170 | + const Message_u32* message = (const Message_u32*)header; |
| 171 | + str += "PABB2_MESSAGE_OPCODE_LOG_LABEL_U32: id = "; |
| 172 | + str += std::to_string(message->id); |
| 173 | + str += ": " + std::string( |
| 174 | + (const char*)(message + 1), |
| 175 | + message->message_bytes - sizeof(Message_u32) |
| 176 | + ); |
| 177 | + str += ": "; |
| 178 | + str += std::to_string((int32_t)message->data); |
| 179 | + return str; |
| 180 | + } |
| 181 | + |
| 182 | + case PABB2_MESSAGE_OPCODE_RET: |
| 183 | + str += "PABB2_MESSAGE_OPCODE_RET: id = "; |
| 184 | + str += std::to_string(header->id); |
| 185 | + return str; |
| 186 | + case PABB2_MESSAGE_OPCODE_REQUEST_DROPPED: |
| 187 | + str += "PABB2_MESSAGE_OPCODE_REQUEST_DROPPED: id = "; |
| 188 | + str += std::to_string(header->id); |
| 189 | + return str; |
| 190 | + case PABB2_MESSAGE_OPCODE_RET_U32: |
| 191 | + str += "PABB2_MESSAGE_OPCODE_RET_U32: id = "; |
| 192 | + str += std::to_string(header->id); |
| 193 | + str += ", data = " + std::to_string(((const Message_u32*)header)->data); |
| 194 | + return str; |
| 195 | + case PABB2_MESSAGE_OPCODE_RET_DATA: |
| 196 | + str += "PABB2_MESSAGE_OPCODE_RET_DATA: id = "; |
| 197 | + str += std::to_string(header->id); |
| 198 | + str += ", bytes = " + std::to_string(header->message_bytes - sizeof(MessageHeader)); |
| 199 | + return str; |
| 200 | + |
| 201 | + case PABB2_MESSAGE_OPCODE_PROTOCOL_VERSION: |
| 202 | + str += "PABB2_MESSAGE_OPCODE_PROTOCOL_VERSION: id = "; |
| 203 | + str += std::to_string(header->id); |
| 204 | + return str; |
| 205 | + case PABB2_MESSAGE_OPCODE_FIRMWARE_VERSION: |
| 206 | + str += "PABB2_MESSAGE_OPCODE_FIRMWARE_VERSION: id = "; |
| 207 | + str += std::to_string(header->id); |
| 208 | + return str; |
| 209 | + case PABB2_MESSAGE_OPCODE_DEVICE_IDENTIFIER: |
| 210 | + str += "PABB2_MESSAGE_OPCODE_DEVICE_IDENTIFIER: id = "; |
| 211 | + str += std::to_string(header->id); |
| 212 | + return str; |
| 213 | + case PABB2_MESSAGE_OPCODE_DEVICE_NAME: |
| 214 | + str += "PABB2_MESSAGE_OPCODE_DEVICE_NAME: id = "; |
| 215 | + str += std::to_string(header->id); |
| 216 | + return str; |
| 217 | + case PABB2_MESSAGE_OPCODE_CONTROLLER_LIST: |
| 218 | + str += "PABB2_MESSAGE_OPCODE_CONTROLLER_LIST: id = "; |
| 219 | + str += std::to_string(header->id); |
| 220 | + return str; |
| 221 | + case PABB2_MESSAGE_OPCODE_CQ_CAPACITY: |
| 222 | + str += "PABB2_MESSAGE_OPCODE_CQ_CAPACITY: id = "; |
| 223 | + str += std::to_string(header->id); |
| 224 | + return str; |
| 225 | + |
| 226 | + case PABB_MESSAGE_OPCODE_READ_CONTROLLER_MODE: |
| 227 | + str += "PABB_MESSAGE_OPCODE_READ_CONTROLLER_MODE: id = "; |
| 228 | + str += std::to_string(header->id); |
| 229 | + return str; |
| 230 | + case PABB_MESSAGE_OPCODE_CHANGE_CONTROLLER_MODE: |
| 231 | + str += "PABB_MESSAGE_OPCODE_CHANGE_CONTROLLER_MODE: id = "; |
| 232 | + str += std::to_string(header->id); |
| 233 | + return str; |
| 234 | + case PABB_MESSAGE_OPCODE_RESET_TO_CONTROLLER: |
| 235 | + str += "PABB_MESSAGE_OPCODE_RESET_TO_CONTROLLER: id = "; |
| 236 | + str += std::to_string(header->id); |
| 237 | + return str; |
| 238 | + case PABB_MESSAGE_OPCODE_CONTROLLER_MAC_ADDRESS: |
| 239 | + str += "PABB_MESSAGE_OPCODE_CONTROLLER_MAC_ADDRESS: id = "; |
| 240 | + str += std::to_string(header->id); |
| 241 | + return str; |
| 242 | + case PABB_MESSAGE_OPCODE_PAIRED_MAC_ADDRESS: |
| 243 | + str += "PABB_MESSAGE_OPCODE_PAIRED_MAC_ADDRESS: id = "; |
| 244 | + str += std::to_string(header->id); |
| 245 | + return str; |
| 246 | + |
| 247 | + case PABB2_MESSAGE_OPCODE_CQ_CANCEL: |
| 248 | + str += "PABB2_MESSAGE_OPCODE_CQ_CANCEL: id = "; |
| 249 | + str += std::to_string(header->id); |
| 250 | + return str; |
| 251 | + case PABB2_MESSAGE_OPCODE_CQ_REPLACE_ON_NEXT: |
| 252 | + str += "PABB2_MESSAGE_OPCODE_CQ_REPLACE_ON_NEXT: id = "; |
| 253 | + str += std::to_string(header->id); |
| 254 | + return str; |
| 255 | + case PABB2_MESSAGE_OPCODE_CQ_COMMAND_FINISHED: |
| 256 | + str += "PABB2_MESSAGE_OPCODE_CQ_COMMAND_FINISHED: id = "; |
| 257 | + str += std::to_string(header->id); |
| 258 | + return str; |
| 259 | + } |
| 260 | + |
| 261 | + |
| 262 | + return "Unknown Message Opcode: 0x" + tostr_hex(header->opcode); |
| 263 | +} |
| 264 | + |
| 265 | + |
| 266 | + |
| 267 | + |
| 268 | + |
| 269 | +} |
| 270 | +} |
0 commit comments