Rendered from docs/WIRELESS_HOST.md in the repository.
Why: everything wireless so far has the DUO host and the game join (Mystery Gift from a distributor, the Union Room's DUO trainer). Several features need it the other way round: the game leads and the DUO joins as its child.
- Mystery Gift with a friend, "Send" (pret:pokeemerald/src/union_room.c:1880 Task_SendMysteryGift): the game shares its Wonder Card or News with the DUO, so the PC can keep a copy - the one wireless way to back up a card the game already has.
- The Direct Corner when the game "becomes the leader" of a Trade Center, Colosseum, Berry Crush, and so on.
- The Union Room when the DUO talks to the game first (the game is then the parent).
Plan: docs/WIRELESS_HOST.md.
Status (18 November): stage H1 works in the simulator: the join build of adapter.c
(-DGAME_HOSTS, 8.4 KB) is listed as the game's child after a few polls and sends its name over NI
in a child's 2-byte-headed frames; tests/host_game_model.py, the game as leader (cited), reads it
and accepts its serial. The first half of H2 too: the game's answer, RFU_STATUS_JOIN_GROUP_OK, sent
over NI in the parent's 3-byte-headed sub-frames of 8, reaches the DUO and is acknowledged chunk by
chunk. Stage H2b too (18 November, afternoon): with the slots finalized, link_rfu_2 runs with the
game as parent and the DUO as player 1 - the game's 70-byte command table each frame, the DUO's
14-byte answers with sequence numbers the game accepts, player IDs, then both link players exchanged
as 200-byte blocks. And stage H3, the same afternoon: Mystery Gift with a friend, "Send" - the game's
Mystery Gift server checks the DUO's game data and sends its Wonder Card and RAM script (or its Wonder
News); the DUO runs the client scripts it is sent, keeps what arrives, agrees to close the link, and the
PC writes a .wc3 (or a news file) into the received folder. The join build is now 11.4 KB (it
leaves out the distributor's host answers, which it never gives, to make room).
On the command line: python duo_events.py friend [--out FILE], with the MGB-010 cable. Not yet
run on hardware: the first real run is item 18 of docs/HARDWARE_QUEUE.md.
What the game does as host (librfu 1026, Emerald and FireRed/LeafGreen)
The leader's link manager (pret:pokeemerald/src/AgbRfu_LinkManager.c:432 rfu_LMAN_manager_entity's states) runs, in order:
- reset, config system, config game data -
0x10,0x17,0x16, as today; the broadcast now carries the game's activity (for a friend's gift,ACTIVITY_WONDER_CARD2or_NEWS2). - start searching for children -
0x19(pret:pokeemerald/src/librfu_rfu.c:505 rfu_REQ_startSearchChild), after a system-status request (0x13) for the adapter's own ID. - poll -
0x1A, again and again (pret:pokeemerald/src/librfu_rfu.c:557 rfu_REQ_pollSearchChild). Each answer lists the children connected so far, four bytes each: the child's ID (2), its slot (1), one spare (pret:pokeemerald/src/librfu_rfu.c:592 rfu_STC_readChildList). With librfu 1026 one listing is enough: the slot is connected at once (no four polls of signal strength, which older versions wanted). - the child's name - the newly connected child must send its serial number, game name and user
name over the NI channel, data type 1, before a timer runs out
(pret:pokeemerald/src/AgbRfu_LinkManager.c:1001 rfu_LMAN_PARENT_checkRecvChildName). A serial
outside the game's accepted list (
0x0002game,0x7F7Ddistributor: pret:pokeemerald/src/link_rfu_2.c:227 sAcceptedSerialNos) is disconnected. - the answer - the leader asks its player, then sends
RFU_STATUS_JOIN_GROUP_OK(or_NO) to that child over NI (pret:pokeemerald/src/link_rfu_2.c:1691 (SendRfuStatusToPartner's rfu_NI_setSendData)). - the link - link_rfu_2 as parent: each frame the game sends its command table to the children (pret:pokeemerald/src/link_rfu_2.c:892 rfu_UNI_setSendData(gRfu.parentSlots, gRfu.recvCmds ...)) and reads each child's commands; link players, blocks, standby and close as before, only now the DUO is player 1.
What the DUO answers (the adapter emulation, reversed)
| The game sends | The DUO answers |
|---|---|
0x13 system status |
its own adapter ID (as today) |
0x19 start host |
OK |
0x1A poll host |
no children for a moment, then one: ID 0x2468 (any), slot 0 |
0x1B end host |
the same list |
0x26 receive data |
the DUO-child's frames: first the NI name (data type 1: serial 0x0002, a game name, "DUO"), then link_rfu_2's child UNI frames |
0x24/0x25 send data |
the game's frames: its NI join answer, then its command table |
The receive format, as parent (pret:pokeemerald/src/librfu_rfu.c:1924
rfu_STC_PARENT_analyzeRecvPacket): the 0x26 answer's first data word, shifted right 8, holds four
5-bit byte counts, one per child slot; the frames follow from byte 8, slot 0's first. Each frame
starts with the child's 2-byte header (pret:pokeemerald/src/librfu_rfu.c:85 llsf_struct, the
MODE_CHILD entry: slot state at bit 10, ack bit 9, n at 7, phase at 5, length in the low bits) -
the same header the game writes today as the distributor's child, which adapter.c already parses.
Every piece of that already exists in adapter.c, facing the other way: the NI sender (nis_*,
used for "you may join"), the NI receiver (ni_*, used for the game's name), the parent's and
child's UNI frame formats. The work is the parent-search answers and the turned-round link_rfu_2
roles.
Stages
- H1, the DUO appears and is named: the game's list of joiners shows "DUO". Simulator first: a
game-side model of the leader (link manager states above) beside
rfu_game_model.py's child. - H2, accepted and linked: the join answer, then link players with the DUO as player 1.
- H3, Mystery Gift with a friend: the game's Mystery Gift server sends its card
(pret:pokeemerald/src/mystery_gift_server.c), the DUO runs the client side
(pret:pokeemerald/src/mystery_gift_client.c), the PC keeps the card as a
.wc3.
How H2b works (built, simulator)
- The game as parent, each frame (pret:pokeemerald/src/link_rfu_2.c:778 RfuMain1_Parent): read every
player's commands into the table it sends (
gRfu.recvCmds, 70 bytes: its own in row 0, each child's as last received in that child's row), send it as one UNI sub-frame with the clock handed over (0x25). Then (pret:pokeemerald/src/link_rfu_2.c:824 RfuMain2_Parent), once the adapter says data are ready, receive (0x26): a child's 14-byte UNI answer has a sequence number in its first byte's top three bits, which must count up by one (five misses in a row is an error); its seven words become that player's row ingRecvCmds, the parent's own command goes in row 0, and the commands are handled as usual. Before the first table, while Task_SendMysteryGift waits 120 frames after its "OK", the parent sends only librfu's one-byte null frames, without handing the clock over (0x24). - The initial exchange is pret:pokeemerald/src/link_rfu_2.c:1754 Task_PlayerExchange (not
Task_PlayerExchangeUpdate, which is for children arriving later):SEND_PLAYER_IDS(two players, slot 0 is player 1), thenBLOCK_REQsize NONE; the game sends its own block too (row 0, announced four times, no checks) and takes the child's four frames after its last chunk. - The DUO's side (
join_linkinadapter.c): its player number fromSEND_PLAYER_IDS, its link player as a 200-byte block once asked - announced until its own row shows the announcement, then the chunks, then any chunk its row never echoed, again - and the game's block read from row 0. It answers each table at once (a real child answers with last frame's command; the game takes either). Trace events: 0x0A (the game's link player, with its trainer ID), 0x7D (every chunk of ours echoed). - Model:
ParentLink,null_frames,lead_linkandlead_and_linkintests/host_game_model.py.
How H3 works (built, simulator)
- The game's side (pret:pokeemerald/src/mystery_gift_menu.c:1522 MG_STATE_SEND onwards): once the link
players are in, the leader's standby (
Task_SendMysteryGiftstate 16) and the Mystery Gift server (pret:pokeemerald/src/mystery_gift_server.c, scriptsgMysteryGiftServerScript_SendWonderCardand_SendWonderNewsin pret:pokeemerald/src/mystery_gift_scripts.c:193) start together; the server's first send waits for the standby to finish, which needs the DUO'sREADY_EXIT_STANDBY. When the server returns,Rfu_SetCloseLinkCallback; once the DUO is ready to close too, the game disconnects (0x30). - What the server checks: the client's game data (pret:pokeemerald/src/mystery_gift.c:390
MysteryGift_ValidateLinkGameData:
0x101at 0, odd words at 4 and 8, gift-type bits at 0xC and 0x10) and its Wonder Card flag (0x14: 0 means no card, so the card goes straight out). The DUO sends the news form of the gift-type bits, which passes for cards too. - Two quirks kept: the server sends its card with
sendType"allowed" turned to "disallowed" (DisableWonderCardSending), so the copy can't be shared on; and it sends the RAM script as 1024 bytes, becauseSVR_COPY_SAVED_RAM_SCRIPTsets the pointer but not the size and a size of 0 means the whole link buffer. The DUO keeps the first 1000 (the script) and checks the CRC over all 1024. The client's "ready to end" is 1024 bytes for the same reason. - The DUO's client (
cli_run,mr_block,ms_load/ms_stepinadapter.c): the client instructions those scripts use; blocks in both directions withmystery_gift_link.c's header (ident, CRC-16, size). Our blocks wait ten tables after the last one is echoed, so the game has read the one before (it holds a child's finished block for four frames, then the server takes it). Trace events: 0x7C (a block from the server, with its ident and size), 0x7B (the client's result), 0x0D (closing). - The PC reads the buffer (polls 31 and 32) and writes a
.wc3in the layoutparse_wc3reads: the game sends no metadata, so those 0x50 bytes are empty. - Model:
MGServerandlead_and_shareintests/host_game_model.py.