pokeemerald/src/link_rfu_2.c

2979 lines
80 KiB
C
Raw Normal View History

2020-02-16 19:49:36 +01:00
#include "global.h"
#include "malloc.h"
#include "battle.h"
#include "berry_blender.h"
#include "decompress.h"
#include "event_data.h"
#include "gpu_regs.h"
#include "librfu.h"
#include "link.h"
#include "link_rfu.h"
#include "overworld.h"
#include "random.h"
#include "palette.h"
#include "union_room.h"
#include "string_util.h"
#include "task.h"
#include "text.h"
#include "save.h"
#include "mystery_gift.h"
2021-07-12 02:42:05 +02:00
enum {
RFUSTATE_INIT,
RFUSTATE_INIT_END,
RFUSTATE_PARENT_CONNECT,
RFUSTATE_PARENT_CONNECT_END,
RFUSTATE_STOP_MANAGER,
RFUSTATE_STOP_MANAGER_END,
RFUSTATE_CHILD_CONNECT,
RFUSTATE_CHILD_CONNECT_END,
RFUSTATE_8, // Unused
RFUSTATE_RECONNECTED,
RFUSTATE_10,
RFUSTATE_CHILD_TRY_JOIN,
RFUSTATE_CHILD_JOINED,
RFUSTATE_13,
RFUSTATE_14,
RFUSTATE_15,
RFUSTATE_UR_FINALIZE,
};
// These states are re-used for different purposes
#define RFUSTATE_17 17
#define RFUSTATE_18 18
#define RFUSTATE_PARENT_FINALIZE_START 17
#define RFUSTATE_PARENT_FINALIZE 18
#define RFUSTATE_UR_CONNECT 17
#define RFUSTATE_UR_CONNECT_END 18
#define RFUSTATE_FINALIZED 20
2021-07-12 02:42:05 +02:00
2020-06-06 22:46:19 +02:00
struct SioInfo
{
char magic[15]; // PokemonSioInfo
u8 playerCount;
u8 linkPlayerIdx[RFU_CHILD_MAX];
struct LinkPlayer linkPlayers[MAX_RFU_PLAYERS];
u8 filler[92];
};
2020-02-16 19:49:36 +01:00
struct RfuDebug
{
u8 filler0[6];
u16 unk_06;
u8 filler1[6];
vu8 unk_0e;
u8 childJoinCount;
u8 filler2[84];
u16 unk_64;
u8 filler3[29];
u8 blockSendTime;
u8 filler4[88];
};
2020-02-16 19:49:36 +01:00
u32 gf_rfu_REQ_api[RFU_API_BUFF_SIZE_RAM / 4];
2020-06-06 22:46:19 +02:00
struct GFRfuManager Rfu;
2020-02-16 19:49:36 +01:00
static u8 sHeldKeyCount;
static u8 sResendBlock8[16];
static u16 sResendBlock16[8];
2020-02-16 19:49:36 +01:00
2020-06-06 22:46:19 +02:00
EWRAM_DATA struct GFtgtGname gHostRFUtgtGnameBuffer = {};
EWRAM_DATA u8 gHostRFUtgtUnameBuffer[PLAYER_NAME_LENGTH + 1] = {};
static EWRAM_DATA INIT_PARAM sRfuReqConfig = {};
static EWRAM_DATA struct RfuDebug sRfuDebug = {};
2020-02-16 19:49:36 +01:00
2020-06-06 22:46:19 +02:00
static void ResetSendDataManager(struct RfuBlockSend *);
static void InitChildRecvBuffers(void);
2020-06-06 22:46:19 +02:00
static void sub_800EAFC(void);
2020-06-07 23:37:09 +02:00
static void sub_800ED34(u16);
2020-06-06 22:46:19 +02:00
static void sub_800EDBC(u16);
2020-06-07 23:37:09 +02:00
static void UpdateBackupQueue(void);
2020-06-06 22:46:19 +02:00
static void Task_ExchangeLinkPlayers(u8);
static void RfuHandleReceiveCommand(u8);
static void CallRfuFunc(void);
static void RfuPrepareSendBuffer(u16);
static void HandleBlockSend(void);
static void SendNextBlock(void);
static void SendLastBlock(void);
static u8 GetPartnerIndexByNameAndTrainerID(const u8 *, u16);
2020-06-09 00:16:57 +02:00
static void UpdateChildStatuses(void);
2020-06-06 22:46:19 +02:00
static s32 sub_80107A0(void);
static void sub_801084C(u8);
2020-06-07 23:37:09 +02:00
static void ClearSelectedLinkPlayerIds(u16);
2020-06-06 22:46:19 +02:00
static void ValidateAndReceivePokemonSioInfo(void *);
static void sub_8010D0C(u8);
static void sub_80115EC(s32);
static void sub_8011BF8(void);
static void RfuReqDisconnectSlot(u32);
static void SendDisconnectCommand(u32, u32);
2020-06-06 22:46:19 +02:00
static void sub_801209C(u8);
static void Debug_PrintEmpty(void);
static void Task_Idle(u8);
static const INIT_PARAM sRfuReqConfigTemplate = {
.maxMFrame = 4,
.MC_TimerCount = 32,
.availSlot_flag = 0,
.mboot_flag = 0,
.serialNo = 2,
.gameName = (void *)&gHostRFUtgtGnameBuffer,
.userName = gHostRFUtgtUnameBuffer,
.fastSearchParent_flag = TRUE,
.linkRecovery_enable = FALSE,
.linkRecovery_period = 600,
.NI_failCounter_limit = 0x12c
2020-02-16 19:49:36 +01:00
};
2020-06-06 22:46:19 +02:00
static const u8 sAvailSlots[] = {
[1] = AVAIL_SLOT1,
[2] = AVAIL_SLOT2,
[3] = AVAIL_SLOT3,
[4] = AVAIL_SLOT4
2020-02-16 19:49:36 +01:00
};
2020-06-06 22:46:19 +02:00
static const u32 sAllBlocksReceived[] = {
2020-02-16 19:49:36 +01:00
0x000000,
0x000001,
0x000003,
0x000007,
0x00000f,
0x00001f,
0x00003f,
0x00007f,
0x0000ff,
0x0001ff,
0x0003ff,
0x0007ff,
0x000fff,
0x001fff,
0x003fff,
0x007fff,
0x00ffff,
0x01ffff,
0x03ffff,
0x07ffff,
0x0fffff,
0x1fffff,
0x3fffff,
0x7fffff,
0xffffff
};
2020-06-06 22:46:19 +02:00
static const u8 sUnknown_082ED68C[] = {
2020-02-16 19:49:36 +01:00
0, 0, 1,
1, 2, 2,
2, 2, 3
};
2020-06-06 22:46:19 +02:00
// Effectively just returns the number of bits set in the index value
// Used for masks of the other players, MAX_RFU_PLAYERS - 1 excludes self
static const u8 sPlayerBitsToCount[1 << (MAX_RFU_PLAYERS - 1)] = {
0, // 0000
1, // 0001
1, // 0010
2, // 0011
1, // 0100
2, // 0101
2, // 0110
3, // 0111
1, // 1000
2, // 1001
2, // 1010
3, // 1011
2, // 1100
3, // 1101
3, // 1110
4 // 1111
2020-02-16 19:49:36 +01:00
};
2020-06-06 22:46:19 +02:00
static const u8 sUnknown_082ED6A5[1 << (MAX_RFU_PLAYERS - 1)] = {
0,
0,
1,
0,
2,
0,
1,
0,
3,
0,
1,
0,
2,
0,
1,
0
2020-02-16 19:49:36 +01:00
};
2020-06-06 22:46:19 +02:00
2020-08-13 09:09:47 +02:00
static const struct BlockRequest sBlockRequests[] = {
[BLOCK_REQ_SIZE_NONE] = { gBlockSendBuffer, 200 },
[BLOCK_REQ_SIZE_200] = { gBlockSendBuffer, 200 },
[BLOCK_REQ_SIZE_100] = { gBlockSendBuffer, 100 },
[BLOCK_REQ_SIZE_220] = { gBlockSendBuffer, 220 },
[BLOCK_REQ_SIZE_40] = { gBlockSendBuffer, 40 }
2020-02-16 19:49:36 +01:00
};
2020-06-06 22:46:19 +02:00
static const u16 sAcceptedSerialNos[] = {
RFU_SERIAL_A,
RFU_SERIAL_B,
RFU_SERIAL_C,
RFU_SERIAL_END
2020-02-16 19:49:36 +01:00
};
2020-06-06 22:46:19 +02:00
static const char sASCII_RfuCmds[][15] = {
2020-02-16 19:49:36 +01:00
"RFU WAIT",
"RFU BOOT",
"RFU ERROR",
"RFU RESET",
"RFU CONFIG",
"RFU START",
"RFU SC POLL",
"RFU SP POLL",
"RFU START",
"RFU SEND ERR",
"RFU CP POLL"
};
2020-06-06 22:46:19 +02:00
static const char sASCII_RecoverCmds[][16] = {
2020-02-16 19:49:36 +01:00
" ",
"RECOVER START ",
"DISSCONECT ",
"RECOVER SUUSES",
"RECOVER FAILED"
};
2020-06-06 22:46:19 +02:00
// List of additional tasks to destroy (if active) when the RFU shuts down
static const TaskFunc sShutdownTasks[] = {
2020-02-16 19:49:36 +01:00
sub_801084C,
2020-06-06 22:46:19 +02:00
Task_ExchangeLinkPlayers,
2020-02-16 19:49:36 +01:00
sub_8010D0C
};
2020-06-06 22:46:19 +02:00
static const char sASCII_PokemonSioInfo[] = "PokemonSioInfo";
static const char sASCII_LinkLossDisconnect[] = "LINK LOSS DISCONNECT!";
static const char sASCII_LinkLossRecoveryNow[] = "LINK LOSS RECOVERY NOW";
ALIGNED(4) static const char sASCII_30Spaces[] = {" "};
static const char sASCII_15Spaces[] = {" "};
static const char sASCII_8Spaces[] = {" "};
ALIGNED(4) static const char sASCII_Space[] = {" "};
static const char sASCII_Asterisk[] = {"*"};
static const char sASCII_NowSlot[] = "NOWSLOT";
2020-06-06 22:46:19 +02:00
static const char sASCII_ClockCmds[][12] = {
" ",
"CLOCK DRIFT",
"BUSY SEND ",
"CMD REJECT ",
"CLOCK SLAVE"
};
static const char sASCII_ChildParentSearch[][8] = {
2020-02-16 19:49:36 +01:00
"CHILD ",
"PARENT",
"SEARCH"
};
2020-06-06 22:46:19 +02:00
static void Debug_PrintString(const void *str, u8 x, u8 y)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
2020-02-16 19:49:36 +01:00
}
2020-06-06 22:46:19 +02:00
static void Debug_PrintNum(u16 num, u8 x, u8 y, u8 numDigits)
2020-02-16 19:49:36 +01:00
{
}
2020-06-06 22:46:19 +02:00
void ResetLinkRfuGFLayer(void)
2020-02-16 19:49:36 +01:00
{
s32 i;
2020-06-06 22:46:19 +02:00
u8 errorState = Rfu.errorState;
2020-02-16 19:49:36 +01:00
CpuFill16(0, &Rfu, sizeof Rfu);
2020-06-06 22:46:19 +02:00
Rfu.errorState = errorState;
Rfu.parentChild = 0xFF;
if (Rfu.errorState != RFU_ERROR_STATE_IGNORE)
Rfu.errorState = RFU_ERROR_STATE_NONE;
for (i = 0; i < MAX_RFU_PLAYERS; i++)
ResetSendDataManager(&Rfu.recvBlock[i]);
ResetSendDataManager(&Rfu.sendBlock);
2020-06-07 23:37:09 +02:00
RfuRecvQueue_Reset(&Rfu.recvQueue);
RfuSendQueue_Reset(&Rfu.sendQueue);
2020-02-16 19:49:36 +01:00
CpuFill16(0, gSendCmd, sizeof gSendCmd);
CpuFill16(0, gRecvCmds, sizeof gRecvCmds);
CpuFill16(0, gLinkPlayers, sizeof gLinkPlayers);
}
2020-06-06 22:46:19 +02:00
void InitRFU(void)
2020-02-16 19:49:36 +01:00
{
IntrFunc serialIntr = gIntrTable[1];
IntrFunc timerIntr = gIntrTable[2];
InitRFUAPI();
2020-02-16 19:49:36 +01:00
rfu_REQ_stopMode();
rfu_waitREQComplete();
REG_IME = 0;
gIntrTable[1] = serialIntr;
gIntrTable[2] = timerIntr;
REG_IME = INTR_FLAG_VBLANK;
}
void InitRFUAPI(void)
2020-02-16 19:49:36 +01:00
{
if (!rfu_initializeAPI((void *)gf_rfu_REQ_api, sizeof gf_rfu_REQ_api, &gIntrTable[1], TRUE))
2020-02-16 19:49:36 +01:00
{
gLinkType = 0;
ClearSavedLinkPlayers();
RfuSetIgnoreError(FALSE);
2020-06-06 22:46:19 +02:00
ResetLinkRfuGFLayer();
rfu_setTimerInterrupt(3, &gIntrTable[2]);
2020-02-16 19:49:36 +01:00
}
}
2020-06-06 22:46:19 +02:00
static void Task_LinkLeaderSearchForChildren(u8 taskId)
2020-02-16 19:49:36 +01:00
{
2020-06-09 00:16:57 +02:00
UpdateChildStatuses();
2020-06-06 22:46:19 +02:00
switch (Rfu.state)
2020-02-16 19:49:36 +01:00
{
2021-07-12 02:42:05 +02:00
case RFUSTATE_INIT:
2020-06-06 22:46:19 +02:00
rfu_LMAN_initializeRFU(&sRfuReqConfig);
2021-07-12 02:42:05 +02:00
Rfu.state = RFUSTATE_INIT_END;
2020-06-06 22:46:19 +02:00
gTasks[taskId].data[1] = 1;
break;
2021-07-12 02:42:05 +02:00
case RFUSTATE_INIT_END:
2020-06-06 22:46:19 +02:00
break;
2021-07-12 02:42:05 +02:00
case RFUSTATE_PARENT_CONNECT:
2020-06-06 22:46:19 +02:00
rfu_LMAN_establishConnection(Rfu.parentChild, 0, 240, (u16 *)sAcceptedSerialNos);
2021-07-12 02:42:05 +02:00
Rfu.state = RFUSTATE_PARENT_CONNECT_END;
2020-06-06 22:46:19 +02:00
gTasks[taskId].data[1] = 6;
break;
2021-07-12 02:42:05 +02:00
case RFUSTATE_PARENT_CONNECT_END:
2020-06-06 22:46:19 +02:00
break;
2021-07-12 02:42:05 +02:00
case RFUSTATE_STOP_MANAGER:
2020-06-06 22:46:19 +02:00
rfu_LMAN_stopManager(FALSE);
2021-07-12 02:42:05 +02:00
Rfu.state = RFUSTATE_STOP_MANAGER_END;
2020-06-06 22:46:19 +02:00
break;
2021-07-12 02:42:05 +02:00
case RFUSTATE_STOP_MANAGER_END:
2020-06-06 22:46:19 +02:00
break;
2021-07-12 02:42:05 +02:00
case RFUSTATE_PARENT_FINALIZE:
Rfu.unk_cdb = FALSE;
2020-06-06 22:46:19 +02:00
rfu_LMAN_setMSCCallback(sub_800EDBC);
InitChildRecvBuffers();
2020-06-06 22:46:19 +02:00
sub_800EAFC();
Rfu.state = RFUSTATE_FINALIZED;
2020-06-06 22:46:19 +02:00
gTasks[taskId].data[1] = 8;
CreateTask(sub_801084C, 5);
DestroyTask(taskId);
break;
2020-02-16 19:49:36 +01:00
}
}
s32 sub_800E87C(u8 slot)
2020-02-16 19:49:36 +01:00
{
return sUnknown_082ED6A5[slot];
2020-02-16 19:49:36 +01:00
}
void sub_800E88C(s32 r2, s32 r5)
{
u8 i;
u8 r4 = 1;
s32 r1 = r2;
s32 r6 = 0;
if (r5 == -1)
{
2020-06-06 22:46:19 +02:00
for (i = 0; i < RFU_CHILD_MAX; r2 >>= 1, i++)
2020-02-16 19:49:36 +01:00
{
if (r2 & 1)
{
2020-06-06 22:46:19 +02:00
Rfu.linkPlayerIdx[i] = r4;
2020-02-16 19:49:36 +01:00
r4++;
}
}
}
else
{
2020-06-06 22:46:19 +02:00
for (i = 0; i < RFU_CHILD_MAX; r1 >>= 1, i++)
2020-02-16 19:49:36 +01:00
{
if (!(r1 & 1))
2020-06-06 22:46:19 +02:00
Rfu.linkPlayerIdx[i] = 0;
2020-02-16 19:49:36 +01:00
}
2020-06-06 22:46:19 +02:00
for (r4 = RFU_CHILD_MAX; r4 != 0; r4--)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
for (i = 0; i < RFU_CHILD_MAX && Rfu.linkPlayerIdx[i] != r4; i++);
if (i == RFU_CHILD_MAX)
2020-02-16 19:49:36 +01:00
r6 = r4;
}
2020-06-06 22:46:19 +02:00
for (r5 &= ~r2, i = 0; i < RFU_CHILD_MAX; r5 >>= 1, i++)
2020-02-16 19:49:36 +01:00
{
if (r5 & 1)
2020-06-06 22:46:19 +02:00
Rfu.linkPlayerIdx[i] = r6++;
2020-02-16 19:49:36 +01:00
}
}
}
2020-06-06 22:46:19 +02:00
static void Task_JoinGroupSearchForParent(u8 taskId)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
switch (Rfu.state)
2020-02-16 19:49:36 +01:00
{
2021-07-12 02:42:05 +02:00
case RFUSTATE_INIT:
2020-06-06 22:46:19 +02:00
rfu_LMAN_initializeRFU((INIT_PARAM *)&sRfuReqConfigTemplate);
2021-07-12 02:42:05 +02:00
Rfu.state = RFUSTATE_INIT_END;
2020-06-06 22:46:19 +02:00
gTasks[taskId].data[1] = 1;
break;
2021-07-12 02:42:05 +02:00
case RFUSTATE_INIT_END:
2020-06-06 22:46:19 +02:00
break;
2021-07-12 02:42:05 +02:00
case RFUSTATE_CHILD_CONNECT:
2020-06-06 22:46:19 +02:00
rfu_LMAN_establishConnection(Rfu.parentChild, 0, 240, (u16 *)sAcceptedSerialNos);
2021-07-12 02:42:05 +02:00
Rfu.state = RFUSTATE_CHILD_CONNECT_END;
2020-06-06 22:46:19 +02:00
gTasks[taskId].data[1] = 7;
break;
2021-07-12 02:42:05 +02:00
case RFUSTATE_CHILD_CONNECT_END:
2020-06-06 22:46:19 +02:00
break;
2021-07-12 02:42:05 +02:00
case RFUSTATE_RECONNECTED:
2020-06-06 22:46:19 +02:00
gTasks[taskId].data[1] = 10;
break;
2021-07-12 02:42:05 +02:00
case RFUSTATE_CHILD_TRY_JOIN:
2020-06-06 22:46:19 +02:00
switch (sub_80107A0())
{
2020-06-09 00:16:57 +02:00
case RFU_STATUS_JOIN_GROUP_OK:
2021-07-12 02:42:05 +02:00
Rfu.state = RFUSTATE_CHILD_JOINED;
2020-02-16 19:49:36 +01:00
break;
2020-06-09 00:16:57 +02:00
case RFU_STATUS_JOIN_GROUP_NO:
case RFU_STATUS_LEAVE_GROUP:
2020-06-06 22:46:19 +02:00
rfu_LMAN_requestChangeAgbClockMaster();
Rfu.disconnectMode = RFU_DISCONNECT_NORMAL;
2020-02-16 19:49:36 +01:00
DestroyTask(taskId);
break;
}
2020-06-06 22:46:19 +02:00
break;
2021-07-12 02:42:05 +02:00
case RFUSTATE_CHILD_JOINED:
2020-06-06 22:46:19 +02:00
{
u8 bmChildSlot = 1 << Rfu.childSlot;
rfu_clearSlot(TYPE_NI_SEND | TYPE_NI_RECV, Rfu.childSlot);
rfu_setRecvBuffer(TYPE_UNI, Rfu.childSlot, Rfu.unk_c3f, sizeof(Rfu.unk_c3f));
rfu_UNI_setSendData(bmChildSlot, Rfu.childSendBuffer, sizeof(Rfu.childSendBuffer));
2020-06-06 22:46:19 +02:00
gTasks[taskId].data[1] = 8;
DestroyTask(taskId);
if (sRfuDebug.childJoinCount == 0)
2020-06-06 22:46:19 +02:00
{
Debug_PrintEmpty();
sRfuDebug.childJoinCount++;
2020-06-06 22:46:19 +02:00
}
CreateTask(sub_801084C, 5);
break;
}
2020-02-16 19:49:36 +01:00
}
}
static void InitChildRecvBuffers(void)
2020-02-16 19:49:36 +01:00
{
u8 i;
2020-06-06 22:46:19 +02:00
u8 acceptSlot = lman.acceptSlot_flag;
for (i = 0; i < RFU_CHILD_MAX; i++)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
if (acceptSlot & 1)
2020-02-16 19:49:36 +01:00
{
rfu_setRecvBuffer(TYPE_UNI, i, Rfu.childRecvBuffer[i], sizeof(Rfu.childRecvBuffer[0]));
2020-06-06 22:46:19 +02:00
rfu_clearSlot(TYPE_UNI_SEND | TYPE_UNI_RECV, i);
2020-02-16 19:49:36 +01:00
}
2020-06-06 22:46:19 +02:00
acceptSlot >>= 1;
2020-02-16 19:49:36 +01:00
}
}
2020-06-06 22:46:19 +02:00
static void sub_800EAFC(void)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
u8 acceptSlot = lman.acceptSlot_flag;
rfu_UNI_setSendData(acceptSlot, Rfu.recvCmds, 70);
Rfu.unk_cda = sub_800E87C(acceptSlot);
Rfu.unk_ce2 = acceptSlot;
sub_800E88C(acceptSlot, -1);
Rfu.parentChild = MODE_PARENT;
2020-02-16 19:49:36 +01:00
}
#define tData7 data[7] // sub_
2020-06-06 22:46:19 +02:00
static void Task_LinkRfu_UnionRoomListen(u8 taskId)
2020-02-16 19:49:36 +01:00
{
2020-06-09 00:16:57 +02:00
if (GetHostRFUtgtGname()->activity == (ACTIVITY_PLYRTALK | IN_UNION_ROOM) && RfuGetStatus() == RFU_STATUS_NEW_CHILD_DETECTED)
2020-02-16 19:49:36 +01:00
{
rfu_REQ_disconnect(lman.acceptSlot_flag);
rfu_waitREQComplete();
2020-06-09 00:16:57 +02:00
RfuSetStatus(RFU_STATUS_OK, 0);
2020-02-16 19:49:36 +01:00
}
2020-06-06 22:46:19 +02:00
switch (Rfu.state)
2020-02-16 19:49:36 +01:00
{
2021-07-12 02:42:05 +02:00
case RFUSTATE_INIT:
2020-06-06 22:46:19 +02:00
rfu_LMAN_initializeRFU(&sRfuReqConfig);
2021-07-12 02:42:05 +02:00
Rfu.state = RFUSTATE_INIT_END;
2020-06-06 22:46:19 +02:00
gTasks[taskId].data[1] = 1;
break;
2021-07-12 02:42:05 +02:00
case RFUSTATE_INIT_END:
2020-06-06 22:46:19 +02:00
break;
2021-07-12 02:42:05 +02:00
case RFUSTATE_UR_CONNECT:
rfu_LMAN_establishConnection(MODE_P_C_SWITCH, 0, 240, (u16 *)sAcceptedSerialNos);
2020-06-06 22:46:19 +02:00
rfu_LMAN_setMSCCallback(sub_800ED34);
2021-07-12 02:42:05 +02:00
Rfu.state = RFUSTATE_UR_CONNECT_END;
2020-06-06 22:46:19 +02:00
break;
2021-07-12 02:42:05 +02:00
case RFUSTATE_UR_CONNECT_END:
2020-06-06 22:46:19 +02:00
break;
2021-07-12 02:42:05 +02:00
case RFUSTATE_13:
if (rfu_UNI_setSendData(1 << Rfu.childSlot, Rfu.childSendBuffer, sizeof(Rfu.childSendBuffer)) == 0)
2020-06-06 22:46:19 +02:00
{
Rfu.parentChild = MODE_CHILD;
DestroyTask(taskId);
if (gTasks[taskId].tData7)
2020-06-06 22:46:19 +02:00
CreateTask(sub_8010D0C, 1);
else
CreateTask(sub_801084C, 5);
}
break;
2021-07-12 02:42:05 +02:00
case RFUSTATE_14:
2020-06-06 22:46:19 +02:00
rfu_LMAN_stopManager(0);
2021-07-12 02:42:05 +02:00
Rfu.state = RFUSTATE_15;
2020-06-06 22:46:19 +02:00
break;
2021-07-12 02:42:05 +02:00
case RFUSTATE_15:
2020-06-06 22:46:19 +02:00
break;
2021-07-12 02:42:05 +02:00
case RFUSTATE_UR_FINALIZE:
Rfu.unk_cdb = FALSE;
2020-06-06 22:46:19 +02:00
rfu_LMAN_setMSCCallback(sub_800EDBC);
UpdateGameData_GroupLockedIn(TRUE);
InitChildRecvBuffers();
2020-06-06 22:46:19 +02:00
sub_800EAFC();
Rfu.state = RFUSTATE_FINALIZED;
2020-06-06 22:46:19 +02:00
gTasks[taskId].data[1] = 8;
Rfu.parentChild = MODE_PARENT;
CreateTask(sub_801084C, 5);
Rfu.unk_ce8 = TRUE;
DestroyTask(taskId);
break;
2020-02-16 19:49:36 +01:00
}
}
2020-05-30 10:09:21 +02:00
void LinkRfu_CreateConnectionAsParent(void)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
rfu_LMAN_establishConnection(MODE_PARENT, 0, 240, (u16 *)sAcceptedSerialNos);
2020-02-16 19:49:36 +01:00
}
2020-05-30 10:09:21 +02:00
void LinkRfu_StopManagerBeforeEnteringChat(void)
2020-02-16 19:49:36 +01:00
{
rfu_LMAN_stopManager(FALSE);
}
2020-06-07 23:37:09 +02:00
static void sub_800ED34(u16 unused)
2020-02-16 19:49:36 +01:00
{
s32 i;
for (i = 0; i < CHILD_DATA_LENGTH; i++)
Rfu.childSendBuffer[i] = 0;
2020-02-16 19:49:36 +01:00
rfu_REQ_recvData();
rfu_waitREQComplete();
2020-06-06 22:46:19 +02:00
if (gRfuSlotStatusUNI[Rfu.childSlot]->recv.newDataFlag)
2020-02-16 19:49:36 +01:00
{
Rfu.childSendCount++;
2020-06-07 23:37:09 +02:00
RfuRecvQueue_Enqueue(&Rfu.recvQueue, Rfu.unk_c3f);
sRfuDebug.unk_06++;
2020-06-07 23:37:09 +02:00
UpdateBackupQueue();
2020-06-06 22:46:19 +02:00
rfu_UNI_readySendData(Rfu.childSlot);
rfu_UNI_clearRecvNewDataFlag(Rfu.childSlot);
2020-02-16 19:49:36 +01:00
}
2020-06-06 22:46:19 +02:00
rfu_LMAN_REQ_sendData(TRUE);
2020-02-16 19:49:36 +01:00
}
2020-06-06 22:46:19 +02:00
static void sub_800EDBC(u16 unused)
2020-02-16 19:49:36 +01:00
{
Rfu.unk_cdb = TRUE;
2020-02-16 19:49:36 +01:00
}
2020-05-30 10:09:21 +02:00
void LinkRfu_Shutdown(void)
2020-02-16 19:49:36 +01:00
{
u8 i;
rfu_LMAN_powerDownRFU();
2020-06-06 22:46:19 +02:00
if (Rfu.parentChild == MODE_PARENT)
2020-02-16 19:49:36 +01:00
{
// Stop parent searching for children
2020-06-06 22:46:19 +02:00
if (FuncIsActiveTask(Task_LinkLeaderSearchForChildren) == TRUE)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
DestroyTask(Rfu.searchTaskId);
ResetLinkRfuGFLayer();
2020-02-16 19:49:36 +01:00
}
}
2020-06-06 22:46:19 +02:00
else if (Rfu.parentChild == MODE_CHILD)
2020-02-16 19:49:36 +01:00
{
// Stop child searching for parent
2020-06-06 22:46:19 +02:00
if (FuncIsActiveTask(Task_JoinGroupSearchForParent) == TRUE)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
DestroyTask(Rfu.searchTaskId);
ResetLinkRfuGFLayer();
2020-02-16 19:49:36 +01:00
}
}
2021-07-12 02:42:05 +02:00
else if (Rfu.parentChild == MODE_P_C_SWITCH)
2020-02-16 19:49:36 +01:00
{
// Stop parent-child switching mode (union room)
2020-06-06 22:46:19 +02:00
if (FuncIsActiveTask(Task_LinkRfu_UnionRoomListen) == TRUE)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
DestroyTask(Rfu.searchTaskId);
ResetLinkRfuGFLayer();
2020-02-16 19:49:36 +01:00
}
}
// Destroy additional tasks
for (i = 0; i < ARRAY_COUNT(sShutdownTasks); i++)
2020-02-16 19:49:36 +01:00
{
if (FuncIsActiveTask(sShutdownTasks[i]) == TRUE)
DestroyTask(FindTaskIdByFunc(sShutdownTasks[i]));
2020-02-16 19:49:36 +01:00
}
}
2020-06-06 22:46:19 +02:00
static void CreateTask_LinkLeaderSearchForChildren(void)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
Rfu.searchTaskId = CreateTask(Task_LinkLeaderSearchForChildren, 1);
2020-02-16 19:49:36 +01:00
}
// If no parent ID (or if child connection not ready) can't reconnect with parent yet
static bool8 CanTryReconnectParent(void)
2020-02-16 19:49:36 +01:00
{
2021-07-12 02:42:05 +02:00
if (Rfu.state == RFUSTATE_CHILD_CONNECT_END && Rfu.parentId)
2020-02-16 19:49:36 +01:00
return TRUE;
return FALSE;
}
static bool32 TryReconnectParent(void)
2020-02-16 19:49:36 +01:00
{
2021-07-12 02:42:05 +02:00
if (Rfu.state == RFUSTATE_CHILD_CONNECT_END && !rfu_LMAN_CHILD_connectParent(gRfuLinkStatus->partner[Rfu.unk_c3d].id, 240))
2020-02-16 19:49:36 +01:00
{
2021-07-12 02:42:05 +02:00
Rfu.state = RFUSTATE_RECONNECTED;
2020-02-16 19:49:36 +01:00
return TRUE;
}
return FALSE;
}
2020-06-06 22:46:19 +02:00
static void CreateTask_JoinGroupSearchForParent(void)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
Rfu.searchTaskId = CreateTask(Task_JoinGroupSearchForParent, 1);
2020-02-16 19:49:36 +01:00
}
2020-05-30 10:09:21 +02:00
bool8 LmanAcceptSlotFlagIsNotZero(void)
2020-02-16 19:49:36 +01:00
{
if (lman.acceptSlot_flag)
return TRUE;
return FALSE;
}
2020-05-30 10:09:21 +02:00
void LinkRfu_StopManagerAndFinalizeSlots(void)
2020-02-16 19:49:36 +01:00
{
2021-07-12 02:42:05 +02:00
Rfu.state = RFUSTATE_STOP_MANAGER;
2020-06-06 22:46:19 +02:00
Rfu.acceptSlot_flag = lman.acceptSlot_flag;
2020-02-16 19:49:36 +01:00
}
2020-06-06 22:46:19 +02:00
bool32 WaitRfuState(bool32 force)
2020-02-16 19:49:36 +01:00
{
2021-07-12 02:42:05 +02:00
if (Rfu.state == RFUSTATE_PARENT_FINALIZE_START || force)
2020-02-16 19:49:36 +01:00
{
2021-07-12 02:42:05 +02:00
Rfu.state = RFUSTATE_PARENT_FINALIZE;
2020-02-16 19:49:36 +01:00
return TRUE;
}
return FALSE;
}
void sub_800EF7C(void)
{
2021-07-12 02:42:05 +02:00
Rfu.state = RFUSTATE_14;
2020-02-16 19:49:36 +01:00
}
// Unused
static void ReadySendDataForSlots(u8 slots)
2020-02-16 19:49:36 +01:00
{
u8 i;
2020-06-06 22:46:19 +02:00
for (i = 0; i < RFU_CHILD_MAX; i++)
2020-02-16 19:49:36 +01:00
{
if (slots & 1)
2020-02-16 19:49:36 +01:00
{
rfu_UNI_readySendData(i);
break;
}
slots >>= 1;
2020-02-16 19:49:36 +01:00
}
}
2020-06-06 22:46:19 +02:00
static void sub_800EFB0(void)
2020-02-16 19:49:36 +01:00
{
s32 i, j;
for (i = 0; i < 5; i++)
{
2020-06-06 22:46:19 +02:00
struct GFRfuManager *ptr = &Rfu;
2020-02-16 19:49:36 +01:00
for (j = 0; j < 7; j++)
{
2020-06-06 22:46:19 +02:00
ptr->recvCmds[i][j][1] = gRecvCmds[i][j] >> 8;
ptr->recvCmds[i][j][0] = gRecvCmds[i][j];
2020-02-16 19:49:36 +01:00
}
}
CpuFill16(0, gRecvCmds, sizeof gRecvCmds);
}
static void MoveSendCmdToRecv(void)
2020-02-16 19:49:36 +01:00
{
s32 i;
for (i = 0; i < CMD_LENGTH - 1; i++)
2020-02-16 19:49:36 +01:00
gRecvCmds[0][i] = gSendCmd[i];
for (i = 0; i < CMD_LENGTH - 1; i++)
2020-02-16 19:49:36 +01:00
gSendCmd[i] = 0;
}
2020-06-07 23:37:09 +02:00
static void UpdateBackupQueue(void)
2020-02-16 19:49:36 +01:00
{
2020-06-07 23:37:09 +02:00
if (Rfu.linkRecovered)
2020-02-16 19:49:36 +01:00
{
bool8 backupEmpty = RfuBackupQueue_Dequeue(&Rfu.backupQueue, Rfu.childSendBuffer);
2020-06-07 23:37:09 +02:00
if (Rfu.backupQueue.count == 0)
Rfu.linkRecovered = FALSE;
if (backupEmpty)
2020-02-16 19:49:36 +01:00
return;
}
2020-06-07 23:37:09 +02:00
if (!Rfu.linkRecovered)
2020-02-16 19:49:36 +01:00
{
RfuSendQueue_Dequeue(&Rfu.sendQueue, Rfu.childSendBuffer);
RfuBackupQueue_Enqueue(&Rfu.backupQueue, Rfu.childSendBuffer);
2020-02-16 19:49:36 +01:00
}
}
bool32 IsRfuRecvQueueEmpty(void)
{
s32 i;
s32 j;
if (gRfuLinkStatus->sendSlotUNIFlag == 0)
{
return FALSE;
}
for (i = 0; i < MAX_RFU_PLAYERS; i++)
2020-02-16 19:49:36 +01:00
{
for (j = 0; j < CMD_LENGTH - 1; j++)
2020-02-16 19:49:36 +01:00
{
if (gRecvCmds[i][j] != 0)
{
return FALSE;
}
}
}
return TRUE;
}
static bool32 RfuMain1_Parent(void)
2020-02-16 19:49:36 +01:00
{
if (Rfu.state < RFUSTATE_FINALIZED)
2020-02-16 19:49:36 +01:00
{
rfu_REQ_recvData();
rfu_waitREQComplete();
2020-06-06 22:46:19 +02:00
rfu_LMAN_REQ_sendData(FALSE);
2020-02-16 19:49:36 +01:00
}
else
{
Rfu.unk_cdb = FALSE;
2020-02-16 19:49:36 +01:00
if ((Rfu.unk_ce2 & gRfuLinkStatus->connSlotFlag) == Rfu.unk_ce2 && (Rfu.unk_ce2 & gRfuLinkStatus->connSlotFlag))
{
if (!Rfu.unk_cdc)
{
if (Rfu.disconnectSlots)
2020-02-16 19:49:36 +01:00
{
RfuReqDisconnectSlot(Rfu.disconnectSlots);
Rfu.disconnectSlots = 0;
if (Rfu.disconnectMode == RFU_DISCONNECT_ERROR)
2020-02-16 19:49:36 +01:00
{
2020-06-09 00:16:57 +02:00
RfuSetStatus(RFU_STATUS_CONNECTION_ERROR, 0x8000);
2020-06-06 22:46:19 +02:00
GetLinkmanErrorParams(0x8000);
2020-02-16 19:49:36 +01:00
return FALSE;
}
if (!lman.acceptSlot_flag)
{
2020-05-30 10:09:21 +02:00
LinkRfu_Shutdown();
2020-02-16 19:49:36 +01:00
gReceivedRemoteLinkPlayers = 0;
return FALSE;
}
}
sub_800EFB0();
rfu_UNI_readySendData(Rfu.unk_cda);
2020-06-06 22:46:19 +02:00
rfu_LMAN_REQ_sendData(TRUE);
2020-02-16 19:49:36 +01:00
}
else
{
rfu_REQ_PARENT_resumeRetransmitAndChange();
}
Rfu.unk_0e = TRUE;
2020-02-16 19:49:36 +01:00
}
}
return FALSE;
}
static bool32 RfuMain2_Parent(void)
2020-02-16 19:49:36 +01:00
{
u16 i;
u16 flags;
u8 r0;
u16 j;
u8 retval;
if (Rfu.state >= RFUSTATE_FINALIZED && Rfu.unk_0e == TRUE)
2020-02-16 19:49:36 +01:00
{
rfu_waitREQComplete();
while (Rfu.unk_cdb == FALSE)
2020-02-16 19:49:36 +01:00
{
if (Rfu.errorState != RFU_ERROR_STATE_NONE)
2020-02-16 19:49:36 +01:00
return FALSE;
}
rfu_REQ_recvData();
rfu_waitREQComplete();
if ((lman.parentAck_flag & Rfu.unk_ce2) == Rfu.unk_ce2)
{
Rfu.unk_cdc = FALSE;
sRfuDebug.unk_06++;
2020-02-16 19:49:36 +01:00
flags = lman.acceptSlot_flag;
2020-06-06 22:46:19 +02:00
for (i = 0; i < RFU_CHILD_MAX; i++)
2020-02-16 19:49:36 +01:00
{
if (flags & 1)
{
if (Rfu.childRecvBuffer[i][1])
2020-02-16 19:49:36 +01:00
{
if (Rfu.unk_cee[i] != 0xFF && (Rfu.childRecvBuffer[i][0] >> 5) != ((Rfu.unk_cee[i] + 1) & 7))
2020-02-16 19:49:36 +01:00
{
if (++Rfu.unk_cea[i] > 4)
GetLinkmanErrorParams(0x8000 | 0x100);
2020-02-16 19:49:36 +01:00
}
else
{
Rfu.unk_cee[i] = Rfu.childRecvBuffer[i][0] / 32;
2020-02-16 19:49:36 +01:00
Rfu.unk_cea[i] = 0;
Rfu.childRecvBuffer[i][0] &= 0x1f;
2020-06-06 22:46:19 +02:00
r0 = Rfu.linkPlayerIdx[i];
2020-02-16 19:49:36 +01:00
for (j = 0; j < 7; j++)
{
gRecvCmds[r0][j] = (Rfu.childRecvBuffer[i][(j << 1) + 1] << 8) | Rfu.childRecvBuffer[i][(j << 1) + 0];
Rfu.childRecvBuffer[i][(j << 1) + 1] = 0;
Rfu.childRecvBuffer[i][(j << 1) + 0] = 0;
2020-02-16 19:49:36 +01:00
}
}
}
rfu_UNI_clearRecvNewDataFlag(i);
}
flags >>= 1;
}
MoveSendCmdToRecv();
2020-06-06 22:46:19 +02:00
RfuHandleReceiveCommand(0);
CallRfuFunc();
if (Rfu.unk_ce5 && !Rfu.stopNewConnections)
2020-02-16 19:49:36 +01:00
{
sRfuDebug.unk_0e = FALSE;
2020-06-06 22:46:19 +02:00
rfu_clearSlot(TYPE_UNI_SEND | TYPE_UNI_RECV, Rfu.unk_cda);
for (i = 0; i < RFU_CHILD_MAX; i++)
2020-02-16 19:49:36 +01:00
{
if ((Rfu.unk_ce5 >> i) & 1)
rfu_setRecvBuffer(TYPE_UNI, i, Rfu.childRecvBuffer[i], sizeof(Rfu.childRecvBuffer[0]));
2020-02-16 19:49:36 +01:00
}
sub_800E88C(Rfu.unk_ce2, Rfu.unk_ce2 | Rfu.unk_ce5);
Rfu.unk_ce9 = Rfu.unk_ce5;
Rfu.unk_ce2 |= Rfu.unk_ce5;
Rfu.unk_ce5 = 0;
2020-06-06 22:46:19 +02:00
rfu_UNI_setSendData(Rfu.unk_ce2, Rfu.recvCmds, 70);
2020-02-16 19:49:36 +01:00
Rfu.unk_cda = sub_800E87C(Rfu.unk_ce2);
2020-06-06 22:46:19 +02:00
CreateTask(Task_ExchangeLinkPlayers, 0);
2020-02-16 19:49:36 +01:00
}
}
else
{
Rfu.unk_cdc = TRUE;
Rfu.unk_0e = FALSE;
2020-02-16 19:49:36 +01:00
}
Rfu.unk_0e = FALSE;
2020-02-16 19:49:36 +01:00
}
retval = Rfu.unk_cdc;
return gRfuLinkStatus->sendSlotUNIFlag ? retval & 1 : FALSE;
}
2020-06-06 22:46:19 +02:00
static void sub_800F498(u16 *a0, u8 *a1)
2020-02-16 19:49:36 +01:00
{
s32 i;
if (a0[0])
{
a0[0] |= (Rfu.unk_102 << 5);
Rfu.unk_102 = (Rfu.unk_102 + 1) & 7;
for (i = 0; i < 7; i++)
{
a1[2 * i + 1] = a0[i] >> 8;
a1[2 * i + 0] = a0[i];
}
}
else
{
for (i = 0; i < 14; i++)
a1[i] = 0;
}
}
static bool32 RfuMain1_Child(void)
2020-02-16 19:49:36 +01:00
{
u8 i;
u8 j;
u8 recv[MAX_RFU_PLAYERS * (2 * (CMD_LENGTH - 1))];
u8 send[2 * (CMD_LENGTH - 1)];
2020-06-09 00:16:57 +02:00
u8 status;
2020-02-16 19:49:36 +01:00
RfuRecvQueue_Dequeue(&Rfu.recvQueue, recv);
2020-02-16 19:49:36 +01:00
for (i = 0; i < MAX_RFU_PLAYERS; i++)
{
for (j = 0; j < CMD_LENGTH - 1; j++)
gRecvCmds[i][j] = (recv[i * 14 + (j << 1) + 1] << 8) | recv[i * 14 + (j << 1) + 0];
2020-02-16 19:49:36 +01:00
}
2020-06-06 22:46:19 +02:00
RfuHandleReceiveCommand(0);
if (lman.childClockSlave_flag == 0 && Rfu.disconnectMode != RFU_DISCONNECT_NONE)
2020-02-16 19:49:36 +01:00
{
rfu_REQ_disconnect(gRfuLinkStatus->connSlotFlag | gRfuLinkStatus->linkLossSlotFlag);
rfu_waitREQComplete();
2020-06-09 00:16:57 +02:00
status = RfuGetStatus();
if (status != RFU_STATUS_FATAL_ERROR && status != RFU_STATUS_JOIN_GROUP_NO && status != RFU_STATUS_LEAVE_GROUP)
RfuSetStatus(RFU_STATUS_CONNECTION_ERROR, 0x9000);
2020-02-16 19:49:36 +01:00
rfu_clearAllSlot();
gReceivedRemoteLinkPlayers = FALSE;
2020-06-06 22:46:19 +02:00
Rfu.callback = NULL;
if (Rfu.disconnectMode == RFU_DISCONNECT_ERROR)
2020-02-16 19:49:36 +01:00
{
2020-06-09 00:16:57 +02:00
RfuSetStatus(RFU_STATUS_CONNECTION_ERROR, 0x9000);
2020-06-06 22:46:19 +02:00
GetLinkmanErrorParams(0x9000);
2020-02-16 19:49:36 +01:00
}
lman.state = lman.next_state = 0;
Rfu.disconnectMode = RFU_DISCONNECT_NONE;
2020-02-16 19:49:36 +01:00
}
if (Rfu.childSendCount)
2020-02-16 19:49:36 +01:00
{
Rfu.childSendCount--;
2020-06-06 22:46:19 +02:00
CallRfuFunc();
sub_800F498(gSendCmd, send);
RfuSendQueue_Enqueue(&Rfu.sendQueue, send);
2020-02-16 19:49:36 +01:00
for (i = 0; i < CMD_LENGTH - 1; i++)
gSendCmd[i] = 0;
}
return IsRfuRecvQueueEmpty();
}
2020-06-06 22:46:19 +02:00
static void HandleSendFailure(u8 unused, u32 flags)
2020-02-16 19:49:36 +01:00
{
2020-10-22 11:45:53 +02:00
s32 i, j, temp;
2020-02-16 19:49:36 +01:00
const u8 *r10 = Rfu.sendBlock.payload;
for (i = 0; i < Rfu.sendBlock.count; i++)
2020-02-16 19:49:36 +01:00
{
if (!(flags & 1))
{
sResendBlock16[0] = RFUCMD_SEND_BLOCK | i;
2020-02-16 19:49:36 +01:00
for (j = 0; j < 7; j++)
{
2020-10-22 11:45:53 +02:00
temp = j << 1;
sResendBlock16[j + 1] = (r10[12 * i + temp + 1] << 8) | r10[12 * i + temp + 0];
2020-02-16 19:49:36 +01:00
}
for (j = 0; j < 7; j++)
{
2020-10-22 11:45:53 +02:00
temp = j << 1;
sResendBlock8[temp + 1] = sResendBlock16[j] >> 8;
sResendBlock8[temp + 0] = sResendBlock16[j];
2020-02-16 19:49:36 +01:00
}
2020-06-07 23:37:09 +02:00
RfuSendQueue_Enqueue(&Rfu.sendQueue, sResendBlock8);
Rfu.sendBlock.failedFlags |= (1 << i);
2020-02-16 19:49:36 +01:00
}
flags >>= 1;
}
}
2020-06-06 22:46:19 +02:00
void Rfu_SetBlockReceivedFlag(u8 linkPlayerId)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
if (Rfu.parentChild == MODE_PARENT && linkPlayerId)
2020-06-07 23:37:09 +02:00
Rfu.numBlocksReceived[linkPlayerId] = 1;
2020-02-16 19:49:36 +01:00
else
2020-06-07 23:37:09 +02:00
Rfu.blockReceived[linkPlayerId] = TRUE;
2020-02-16 19:49:36 +01:00
}
2020-06-06 22:46:19 +02:00
void Rfu_ResetBlockReceivedFlag(u8 linkPlayerId)
2020-02-16 19:49:36 +01:00
{
2020-06-07 23:37:09 +02:00
Rfu.blockReceived[linkPlayerId] = FALSE;
Rfu.recvBlock[linkPlayerId].receiving = 0;
2020-02-16 19:49:36 +01:00
}
static u8 LoadLinkPlayerIds(const u8 *ids)
2020-02-16 19:49:36 +01:00
{
u8 i;
2020-06-06 22:46:19 +02:00
if (Rfu.parentChild == MODE_PARENT)
2020-02-16 19:49:36 +01:00
return FALSE;
2020-06-06 22:46:19 +02:00
for (i = 0; i < RFU_CHILD_MAX; i++)
Rfu.linkPlayerIdx[i] = ids[i];
return ids[Rfu.childSlot];
2020-02-16 19:49:36 +01:00
}
2020-06-06 22:46:19 +02:00
static void RfuFunc_SendKeysToRfu(void)
2020-02-16 19:49:36 +01:00
{
if (gReceivedRemoteLinkPlayers
&& gHeldKeyCodeToSend != LINK_KEY_CODE_NULL
&& gLinkTransferringData != TRUE)
{
sHeldKeyCount++;
gHeldKeyCodeToSend |= (sHeldKeyCount << 8);
2020-08-24 20:52:33 +02:00
RfuPrepareSendBuffer(RFUCMD_SEND_HELD_KEYS);
2020-02-16 19:49:36 +01:00
}
}
2020-05-30 10:09:21 +02:00
struct GFtgtGname *GetHostRFUtgtGname(void)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
return &gHostRFUtgtGnameBuffer;
2020-02-16 19:49:36 +01:00
}
bool32 IsSendingKeysToRfu(void)
{
2020-06-06 22:46:19 +02:00
return Rfu.callback == RfuFunc_SendKeysToRfu;
2020-02-16 19:49:36 +01:00
}
2020-06-06 22:46:19 +02:00
void StartSendingKeysToRfu(void)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
Rfu.callback = RfuFunc_SendKeysToRfu;
2020-02-16 19:49:36 +01:00
}
void ClearLinkRfuCallback(void)
{
2020-06-06 22:46:19 +02:00
Rfu.callback = NULL;
2020-02-16 19:49:36 +01:00
}
2020-08-24 20:52:33 +02:00
static void Rfu_BerryBlenderSendHeldKeys(void)
2020-02-16 19:49:36 +01:00
{
2020-08-24 20:52:33 +02:00
RfuPrepareSendBuffer(RFUCMD_BLENDER_SEND_KEYS);
2020-02-16 19:49:36 +01:00
if (GetMultiplayerId() == 0)
2020-08-24 20:52:33 +02:00
gSendCmd[BLENDER_COMM_ARROW_POS] = GetBlenderArrowPosition();
gBerryBlenderKeySendAttempts++;
2020-02-16 19:49:36 +01:00
}
2020-08-24 20:52:33 +02:00
void Rfu_SetBerryBlenderLinkCallback(void)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
if (Rfu.callback == NULL)
2020-08-24 20:52:33 +02:00
Rfu.callback = Rfu_BerryBlenderSendHeldKeys;
2020-02-16 19:49:36 +01:00
}
2020-06-06 22:46:19 +02:00
static void RfuHandleReceiveCommand(u8 unused)
2020-02-16 19:49:36 +01:00
{
u16 i;
u16 j;
2020-02-16 19:49:36 +01:00
for (i = 0; i < MAX_RFU_PLAYERS; i++)
{
switch (gRecvCmds[i][0] & RFUCMD_MASK)
2020-02-16 19:49:36 +01:00
{
case RFUCMD_SEND_PLAYER_IDS_NEW:
2020-06-06 22:46:19 +02:00
if (Rfu.parentChild == MODE_CHILD && gReceivedRemoteLinkPlayers)
2020-02-16 19:49:36 +01:00
return;
// fallthrough
case RFUCMD_SEND_PLAYER_IDS:
2020-06-06 22:46:19 +02:00
if (gRfuLinkStatus->parentChild == MODE_CHILD)
2020-02-16 19:49:36 +01:00
{
Rfu.playerCount = gRecvCmds[i][1];
Rfu.multiplayerId = LoadLinkPlayerIds((u8 *)(gRecvCmds[i] + 2));
2020-02-16 19:49:36 +01:00
}
break;
case RFUCMD_SEND_BLOCK_INIT:
if (Rfu.recvBlock[i].receiving == 0)
2020-02-16 19:49:36 +01:00
{
Rfu.recvBlock[i].next = 0;
Rfu.recvBlock[i].count = gRecvCmds[i][1];
Rfu.recvBlock[i].owner = gRecvCmds[i][2];
Rfu.recvBlock[i].receivedFlags = 0;
Rfu.recvBlock[i].receiving = 1;
2020-06-07 23:37:09 +02:00
Rfu.blockReceived[i] = FALSE;
2020-02-16 19:49:36 +01:00
}
break;
case RFUCMD_SEND_BLOCK:
if (Rfu.recvBlock[i].receiving == 1)
2020-02-16 19:49:36 +01:00
{
Rfu.recvBlock[i].next = gRecvCmds[i][0] & 0xff;
Rfu.recvBlock[i].receivedFlags |= (1 << Rfu.recvBlock[i].next);
2020-02-16 19:49:36 +01:00
for (j = 0; j < 6; j++)
gBlockRecvBuffer[i][Rfu.recvBlock[i].next * 6 + j] = gRecvCmds[i][j + 1];
if (Rfu.recvBlock[i].receivedFlags == sAllBlocksReceived[Rfu.recvBlock[i].count])
2020-02-16 19:49:36 +01:00
{
Rfu.recvBlock[i].receiving = 2;
2020-06-06 22:46:19 +02:00
Rfu_SetBlockReceivedFlag(i);
if (GetHostRFUtgtGname()->activity == (ACTIVITY_CHAT | IN_UNION_ROOM) && gReceivedRemoteLinkPlayers != 0 && Rfu.parentChild == MODE_CHILD)
2020-06-06 22:46:19 +02:00
ValidateAndReceivePokemonSioInfo(gBlockRecvBuffer);
2020-02-16 19:49:36 +01:00
}
}
break;
2020-08-24 20:52:33 +02:00
case RFUCMD_SEND_BLOCK_REQ:
2020-08-13 09:09:47 +02:00
Rfu_InitBlockSend(sBlockRequests[gRecvCmds[i][1]].address, (u16)sBlockRequests[gRecvCmds[i][1]].size);
2020-02-16 19:49:36 +01:00
break;
2020-08-24 20:52:33 +02:00
case RFUCMD_READY_CLOSE_LINK:
2020-08-13 09:09:47 +02:00
Rfu.readyCloseLink[i] = TRUE;
2020-02-16 19:49:36 +01:00
break;
2020-08-24 20:52:33 +02:00
case RFUCMD_READY_EXIT_STANDBY:
if (Rfu.allReadyNum == gRecvCmds[i][1])
2020-08-13 09:09:47 +02:00
Rfu.readyExitStandby[i] = TRUE;
2020-02-16 19:49:36 +01:00
break;
case RFUCMD_DISCONNECT:
2020-06-06 22:46:19 +02:00
if (Rfu.parentChild == MODE_CHILD)
2020-02-16 19:49:36 +01:00
{
// Disconnect child
2020-06-06 22:46:19 +02:00
if (gReceivedRemoteLinkPlayers)
2020-02-16 19:49:36 +01:00
{
if (gRecvCmds[i][1] & gRfuLinkStatus->connSlotFlag)
{
gReceivedRemoteLinkPlayers = 0;
rfu_LMAN_requestChangeAgbClockMaster();
Rfu.disconnectMode = gRecvCmds[i][2];
2020-02-16 19:49:36 +01:00
}
Rfu.playerCount = gRecvCmds[i][3];
2020-06-07 23:37:09 +02:00
ClearSelectedLinkPlayerIds(gRecvCmds[i][1]);
2020-02-16 19:49:36 +01:00
}
}
else
{
// Disconnect parent
RfuPrepareSendBuffer(RFUCMD_DISCONNECT_PARENT);
2020-02-16 19:49:36 +01:00
gSendCmd[1] = gRecvCmds[i][1];
gSendCmd[2] = gRecvCmds[i][2];
gSendCmd[3] = gRecvCmds[i][3];
}
break;
case RFUCMD_DISCONNECT_PARENT:
2020-06-06 22:46:19 +02:00
if (Rfu.parentChild == MODE_PARENT)
2020-02-16 19:49:36 +01:00
{
Rfu.disconnectSlots |= gRecvCmds[i][1];
Rfu.disconnectMode = gRecvCmds[i][2];
2020-06-07 23:37:09 +02:00
ClearSelectedLinkPlayerIds(gRecvCmds[i][1]);
2020-02-16 19:49:36 +01:00
}
break;
2020-08-24 20:52:33 +02:00
case RFUCMD_BLENDER_SEND_KEYS:
case RFUCMD_SEND_HELD_KEYS:
2020-02-16 19:49:36 +01:00
gLinkPartnersHeldKeys[i] = gRecvCmds[i][1];
break;
}
2020-06-07 23:37:09 +02:00
if (Rfu.parentChild == MODE_PARENT && Rfu.numBlocksReceived[i])
2020-02-16 19:49:36 +01:00
{
2020-06-07 23:37:09 +02:00
if (Rfu.numBlocksReceived[i] == 4)
2020-02-16 19:49:36 +01:00
{
2020-06-07 23:37:09 +02:00
Rfu.blockReceived[i] = TRUE;
Rfu.numBlocksReceived[i] = 0;
2020-02-16 19:49:36 +01:00
}
else
2020-06-07 23:37:09 +02:00
Rfu.numBlocksReceived[i]++;
2020-02-16 19:49:36 +01:00
}
}
}
2020-06-07 23:37:09 +02:00
static bool8 AreNoPlayersReceiving(void)
2020-02-16 19:49:36 +01:00
{
s32 i;
2020-06-06 22:46:19 +02:00
for (i = 0; i < MAX_RFU_PLAYERS; i++)
2020-02-16 19:49:36 +01:00
{
if (Rfu.recvBlock[i].receiving)
2020-02-16 19:49:36 +01:00
return FALSE;
}
return TRUE;
}
2020-06-06 22:46:19 +02:00
static bool8 sub_800FC88(void)
2020-02-16 19:49:36 +01:00
{
s32 i;
for (i = 0; i < Rfu.playerCount; i++)
{
if (Rfu.recvBlock[i].receiving != 2 || Rfu.blockReceived[i] != TRUE)
2020-02-16 19:49:36 +01:00
return FALSE;
}
return TRUE;
}
2020-06-06 22:46:19 +02:00
static void ResetSendDataManager(struct RfuBlockSend *data)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
data->next = 0;
data->count = 0;
data->payload = NULL;
data->receivedFlags = 0;
data->sending = FALSE;
data->owner = 0;
data->receiving = 0;
2020-02-16 19:49:36 +01:00
}
2020-06-06 22:46:19 +02:00
u8 Rfu_GetBlockReceivedStatus(void)
2020-02-16 19:49:36 +01:00
{
u8 flags = 0;
s32 i;
2020-06-06 22:46:19 +02:00
for (i = 0; i < MAX_RFU_PLAYERS; i++)
2020-02-16 19:49:36 +01:00
{
if (Rfu.recvBlock[i].receiving == 2 && Rfu.blockReceived[i] == TRUE)
2020-02-16 19:49:36 +01:00
flags |= (1 << i);
}
return flags;
}
2020-06-06 22:46:19 +02:00
static void RfuPrepareSendBuffer(u16 command)
2020-02-16 19:49:36 +01:00
{
u8 i;
u8 *buff;
u8 tmp;
gSendCmd[0] = command;
switch (command)
{
case RFUCMD_SEND_BLOCK_INIT:
gSendCmd[1] = Rfu.sendBlock.count;
gSendCmd[2] = Rfu.sendBlock.owner + 0x80;
2020-02-16 19:49:36 +01:00
break;
2020-08-24 20:52:33 +02:00
case RFUCMD_SEND_BLOCK_REQ:
2020-06-07 23:37:09 +02:00
if (AreNoPlayersReceiving())
2020-08-13 09:09:47 +02:00
gSendCmd[1] = Rfu.blockRequestType;
2020-02-16 19:49:36 +01:00
break;
case RFUCMD_SEND_PLAYER_IDS:
case RFUCMD_SEND_PLAYER_IDS_NEW:
tmp = Rfu.unk_ce2 ^ Rfu.disconnectSlots;
Rfu.playerCount = sPlayerBitsToCount[tmp] + 1;
2020-02-16 19:49:36 +01:00
gSendCmd[1] = Rfu.playerCount;
buff = (u8 *)&gSendCmd[2];
2020-06-06 22:46:19 +02:00
for (i = 0; i < RFU_CHILD_MAX; i++)
buff[i] = Rfu.linkPlayerIdx[i];
2020-02-16 19:49:36 +01:00
break;
2020-08-24 20:52:33 +02:00
case RFUCMD_READY_EXIT_STANDBY:
case RFUCMD_READY_CLOSE_LINK:
gSendCmd[1] = Rfu.allReadyNum;
2020-02-16 19:49:36 +01:00
break;
2020-08-24 20:52:33 +02:00
case RFUCMD_BLENDER_SEND_KEYS:
2020-02-16 19:49:36 +01:00
gSendCmd[0] = command;
gSendCmd[1] = gMain.heldKeys;
break;
2020-08-24 20:52:33 +02:00
case RFUCMD_SEND_PACKET:
for (i = 0; i < RFU_PACKET_SIZE; i++)
gSendCmd[1 + i] = Rfu.packet[i];
2020-02-16 19:49:36 +01:00
break;
2020-08-24 20:52:33 +02:00
case RFUCMD_SEND_HELD_KEYS:
2020-02-16 19:49:36 +01:00
gSendCmd[1] = gHeldKeyCodeToSend;
break;
case RFUCMD_DISCONNECT_PARENT:
case RFUCMD_DISCONNECT:
2020-02-16 19:49:36 +01:00
break;
}
}
2020-08-24 20:52:33 +02:00
void Rfu_SendPacket(void *data)
2020-02-16 19:49:36 +01:00
{
2020-06-09 00:16:57 +02:00
if (gSendCmd[0] == 0 && !RfuHasErrored())
2020-02-16 19:49:36 +01:00
{
2020-08-24 20:52:33 +02:00
memcpy(Rfu.packet, data, sizeof(Rfu.packet));
RfuPrepareSendBuffer(RFUCMD_SEND_PACKET);
2020-02-16 19:49:36 +01:00
}
}
2020-06-06 22:46:19 +02:00
bool32 Rfu_InitBlockSend(const u8 *src, size_t size)
2020-02-16 19:49:36 +01:00
{
bool8 r4;
if (Rfu.callback != NULL)
return FALSE;
if (gSendCmd[0] != 0)
2020-02-16 19:49:36 +01:00
return FALSE;
if (Rfu.sendBlock.sending)
2020-02-16 19:49:36 +01:00
{
sRfuDebug.blockSendTime++;
2020-02-16 19:49:36 +01:00
return FALSE;
}
r4 = (size % 12) != 0;
Rfu.sendBlock.owner = GetMultiplayerId();
Rfu.sendBlock.sending = TRUE;
Rfu.sendBlock.count = (size / 12) + r4;
Rfu.sendBlock.next = 0;
if (size > BLOCK_BUFFER_SIZE)
Rfu.sendBlock.payload = src;
2020-02-16 19:49:36 +01:00
else
{
if (src != gBlockSendBuffer)
memcpy(gBlockSendBuffer, src, size);
Rfu.sendBlock.payload = gBlockSendBuffer;
2020-02-16 19:49:36 +01:00
}
RfuPrepareSendBuffer(RFUCMD_SEND_BLOCK_INIT);
2020-06-06 22:46:19 +02:00
Rfu.callback = HandleBlockSend;
2020-02-16 19:49:36 +01:00
Rfu.unk_5b = 0;
return TRUE;
}
2020-06-06 22:46:19 +02:00
static void HandleBlockSend(void)
2020-02-16 19:49:36 +01:00
{
if (gSendCmd[0] == 0)
{
RfuPrepareSendBuffer(RFUCMD_SEND_BLOCK_INIT);
2020-06-06 22:46:19 +02:00
if (Rfu.parentChild == MODE_PARENT)
2020-02-16 19:49:36 +01:00
{
if (++Rfu.unk_5b > 2)
2020-06-06 22:46:19 +02:00
Rfu.callback = SendNextBlock;
2020-02-16 19:49:36 +01:00
}
else
{
if ((gRecvCmds[GetMultiplayerId()][0] & RFUCMD_MASK) == RFUCMD_SEND_BLOCK_INIT)
2020-06-06 22:46:19 +02:00
Rfu.callback = SendNextBlock;
2020-02-16 19:49:36 +01:00
}
}
}
2020-06-06 22:46:19 +02:00
static void SendNextBlock(void)
2020-02-16 19:49:36 +01:00
{
s32 i;
const u8 *src = Rfu.sendBlock.payload;
gSendCmd[0] = RFUCMD_SEND_BLOCK | Rfu.sendBlock.next;
for (i = 0; i < CMD_LENGTH - 1; i++)
gSendCmd[i + 1] = (src[(i << 1) + Rfu.sendBlock.next * 12 + 1] << 8) | src[(i << 1) + Rfu.sendBlock.next * 12 + 0];
Rfu.sendBlock.next++;
if (Rfu.sendBlock.count <= Rfu.sendBlock.next)
{
Rfu.sendBlock.sending = FALSE;
2020-06-06 22:46:19 +02:00
Rfu.callback = SendLastBlock;
2020-02-16 19:49:36 +01:00
}
}
2020-06-06 22:46:19 +02:00
static void SendLastBlock(void)
2020-02-16 19:49:36 +01:00
{
const u8 *src = Rfu.sendBlock.payload;
2020-02-16 19:49:36 +01:00
u8 mpId = GetMultiplayerId();
s32 i;
2020-06-06 22:46:19 +02:00
if (Rfu.parentChild == MODE_CHILD)
2020-02-16 19:49:36 +01:00
{
gSendCmd[0] = RFUCMD_SEND_BLOCK | (Rfu.sendBlock.count - 1);
for (i = 0; i < CMD_LENGTH - 1; i++)
gSendCmd[i + 1] = (src[(i << 1) + (Rfu.sendBlock.count - 1) * 12 + 1] << 8) | src[(i << 1) + (Rfu.sendBlock.count - 1) * 12 + 0];
if ((u8)gRecvCmds[mpId][0] == Rfu.sendBlock.count - 1)
2020-02-16 19:49:36 +01:00
{
if (Rfu.recvBlock[mpId].receivedFlags != sAllBlocksReceived[Rfu.recvBlock[mpId].count])
2020-02-16 19:49:36 +01:00
{
HandleSendFailure(mpId, Rfu.recvBlock[mpId].receivedFlags);
sRfuDebug.unk_64++;
2020-02-16 19:49:36 +01:00
}
else
{
2020-06-06 22:46:19 +02:00
Rfu.callback = NULL;
}
2020-02-16 19:49:36 +01:00
}
}
else
{
2020-06-06 22:46:19 +02:00
Rfu.callback = NULL;
}
2020-02-16 19:49:36 +01:00
}
2020-08-13 09:09:47 +02:00
bool8 Rfu_SendBlockRequest(u8 type)
2020-02-16 19:49:36 +01:00
{
2020-08-13 09:09:47 +02:00
Rfu.blockRequestType = type;
2020-08-24 20:52:33 +02:00
RfuPrepareSendBuffer(RFUCMD_SEND_BLOCK_REQ);
2020-02-16 19:49:36 +01:00
return TRUE;
}
static void RfuShutdownAfterDisconnect(void)
2020-02-16 19:49:36 +01:00
{
rfu_clearAllSlot();
rfu_LMAN_powerDownRFU();
gReceivedRemoteLinkPlayers = 0;
2020-06-06 22:46:19 +02:00
Rfu.isShuttingDown = TRUE;
Rfu.callback = NULL;
2020-02-16 19:49:36 +01:00
}
2020-08-13 09:09:47 +02:00
static void DisconnectRfu(void)
2020-02-16 19:49:36 +01:00
{
rfu_REQ_disconnect(gRfuLinkStatus->connSlotFlag | gRfuLinkStatus->linkLossSlotFlag);
rfu_waitREQComplete();
RfuShutdownAfterDisconnect();
2020-02-16 19:49:36 +01:00
}
2020-08-13 09:09:47 +02:00
static void TryDisconnectRfu(void)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
if (Rfu.parentChild == MODE_CHILD)
2020-02-16 19:49:36 +01:00
{
rfu_LMAN_requestChangeAgbClockMaster();
Rfu.disconnectMode = RFU_DISCONNECT_NORMAL;
2020-02-16 19:49:36 +01:00
}
else
{
2020-08-13 09:09:47 +02:00
Rfu.callback = DisconnectRfu;
}
2020-02-16 19:49:36 +01:00
}
void LinkRfu_FatalError(void)
{
rfu_LMAN_requestChangeAgbClockMaster();
Rfu.disconnectMode = RFU_DISCONNECT_ERROR;
Rfu.disconnectSlots = gRfuLinkStatus->connSlotFlag | gRfuLinkStatus->linkLossSlotFlag;
2020-02-16 19:49:36 +01:00
}
2020-08-13 09:09:47 +02:00
// RFU equivalent of LinkCB_WaitCloseLink
static void WaitAllReadyToCloseLink(void)
2020-02-16 19:49:36 +01:00
{
s32 i;
u8 playerCount = Rfu.playerCount;
s32 count = 0;
2020-08-13 09:09:47 +02:00
// Wait for all players to be ready
2020-02-16 19:49:36 +01:00
for (i = 0; i < MAX_RFU_PLAYERS; i++)
{
2020-08-13 09:09:47 +02:00
if (Rfu.readyCloseLink[i])
2020-02-16 19:49:36 +01:00
count++;
}
if (count == playerCount)
{
2020-08-13 09:09:47 +02:00
// All ready, close link
2021-01-13 21:17:32 +01:00
gBattleTypeFlags &= ~BATTLE_TYPE_LINK_IN_BATTLE;
2020-06-06 22:46:19 +02:00
if (Rfu.parentChild == MODE_CHILD)
2020-02-16 19:49:36 +01:00
{
Rfu.errorState = RFU_ERROR_STATE_3;
2020-08-13 09:09:47 +02:00
TryDisconnectRfu();
2020-02-16 19:49:36 +01:00
}
else
{
2020-08-13 09:09:47 +02:00
Rfu.callback = TryDisconnectRfu;
}
2020-02-16 19:49:36 +01:00
}
}
2020-08-13 09:09:47 +02:00
static void SendReadyCloseLink(void)
2020-02-16 19:49:36 +01:00
{
if (gSendCmd[0] == 0 && !Rfu.unk_ce8)
2020-02-16 19:49:36 +01:00
{
2020-08-24 20:52:33 +02:00
RfuPrepareSendBuffer(RFUCMD_READY_CLOSE_LINK);
2020-08-13 09:09:47 +02:00
Rfu.callback = WaitAllReadyToCloseLink;
2020-02-16 19:49:36 +01:00
}
}
2020-08-13 09:09:47 +02:00
static void Task_TryReadyCloseLink(u8 taskId)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
if (Rfu.callback == NULL)
2020-02-16 19:49:36 +01:00
{
Rfu.stopNewConnections = TRUE;
2020-08-13 09:09:47 +02:00
Rfu.callback = SendReadyCloseLink;
2020-02-16 19:49:36 +01:00
DestroyTask(taskId);
}
}
2020-08-13 09:09:47 +02:00
void Rfu_SetCloseLinkCallback(void)
2020-02-16 19:49:36 +01:00
{
2020-08-13 09:09:47 +02:00
if (!FuncIsActiveTask(Task_TryReadyCloseLink))
CreateTask(Task_TryReadyCloseLink, 5);
2020-02-16 19:49:36 +01:00
}
2020-08-13 09:09:47 +02:00
static void SendReadyExitStandbyUntilAllReady(void)
2020-02-16 19:49:36 +01:00
{
u8 playerCount;
u8 i;
2020-02-16 19:49:36 +01:00
if (GetMultiplayerId() != 0)
2020-02-16 19:49:36 +01:00
{
if (Rfu.recvQueue.count == 0 && Rfu.resendExitStandbyTimer > 60)
{
RfuPrepareSendBuffer(RFUCMD_READY_EXIT_STANDBY);
Rfu.resendExitStandbyTimer = 0;
}
2020-02-16 19:49:36 +01:00
}
playerCount = GetLinkPlayerCount();
for (i = 0; i < playerCount; i++)
{
2020-08-13 09:09:47 +02:00
if (!Rfu.readyExitStandby[i])
2020-02-16 19:49:36 +01:00
break;
}
if (i == playerCount)
{
for (i = 0; i < MAX_RFU_PLAYERS; i++)
2020-08-13 09:09:47 +02:00
Rfu.readyExitStandby[i] = FALSE;
Rfu.allReadyNum++;
2020-06-06 22:46:19 +02:00
Rfu.callback = NULL;
2020-02-16 19:49:36 +01:00
}
2020-08-13 09:09:47 +02:00
Rfu.resendExitStandbyTimer++;
2020-02-16 19:49:36 +01:00
}
2020-08-13 09:09:47 +02:00
static void LinkLeaderReadyToExitStandby(void)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
if (Rfu.recvQueue.count == 0 && gSendCmd[0] == 0)
2020-02-16 19:49:36 +01:00
{
2020-08-24 20:52:33 +02:00
RfuPrepareSendBuffer(RFUCMD_READY_EXIT_STANDBY);
2020-08-13 09:09:47 +02:00
Rfu.callback = SendReadyExitStandbyUntilAllReady;
2020-02-16 19:49:36 +01:00
}
}
2020-08-13 09:09:47 +02:00
// RFU equivalent of LinkCB_Standby and LinkCB_StandbyForAll
static void Rfu_LinkStandby(void)
2020-02-16 19:49:36 +01:00
{
u8 i;
u8 playerCount;
2020-02-16 19:49:36 +01:00
if (GetMultiplayerId() != 0)
{
2020-08-13 09:09:47 +02:00
// Not link leader, send exit standby when ready
2020-06-06 22:46:19 +02:00
if (Rfu.recvQueue.count == 0 && gSendCmd[0] == 0)
2020-02-16 19:49:36 +01:00
{
2020-08-24 20:52:33 +02:00
RfuPrepareSendBuffer(RFUCMD_READY_EXIT_STANDBY);
2020-08-13 09:09:47 +02:00
Rfu.callback = SendReadyExitStandbyUntilAllReady;
2020-02-16 19:49:36 +01:00
}
}
else
{
2020-08-13 09:09:47 +02:00
// Link leader, wait for all members to send exit ready
2020-02-16 19:49:36 +01:00
playerCount = GetLinkPlayerCount();
for (i = 1; i < playerCount; i++)
{
2020-08-13 09:09:47 +02:00
if (!Rfu.readyExitStandby[i])
2020-02-16 19:49:36 +01:00
break;
}
if (i == playerCount)
{
2020-06-06 22:46:19 +02:00
if (Rfu.recvQueue.count == 0 && gSendCmd[0] == 0)
2020-02-16 19:49:36 +01:00
{
2020-08-24 20:52:33 +02:00
RfuPrepareSendBuffer(RFUCMD_READY_EXIT_STANDBY);
2020-08-13 09:09:47 +02:00
Rfu.callback = LinkLeaderReadyToExitStandby;
2020-02-16 19:49:36 +01:00
}
}
}
}
2020-08-13 09:09:47 +02:00
void Rfu_SetLinkStandbyCallback(void)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
if (Rfu.callback == NULL)
2020-02-16 19:49:36 +01:00
{
2020-08-13 09:09:47 +02:00
Rfu.callback = Rfu_LinkStandby;
Rfu.resendExitStandbyTimer = 0;
2020-02-16 19:49:36 +01:00
}
}
2020-06-07 23:37:09 +02:00
bool32 IsRfuSerialNumberValid(u32 serialNo)
2020-02-16 19:49:36 +01:00
{
s32 i;
2020-06-06 22:46:19 +02:00
for (i = 0; sAcceptedSerialNos[i] != serialNo; i++)
2020-02-16 19:49:36 +01:00
{
if (sAcceptedSerialNos[i] == RFU_SERIAL_END)
2020-02-16 19:49:36 +01:00
return FALSE;
}
return TRUE;
}
u8 Rfu_SetLinkRecovery(bool32 enable)
2020-02-16 19:49:36 +01:00
{
if (enable == FALSE)
2020-02-16 19:49:36 +01:00
return rfu_LMAN_setLinkRecovery(0, 0);
2020-06-06 22:46:19 +02:00
rfu_LMAN_setLinkRecovery(1, 600);
2020-02-16 19:49:36 +01:00
return 0;
}
void Rfu_StopPartnerSearch(void)
2020-02-16 19:49:36 +01:00
{
Rfu.stopNewConnections = TRUE;
2020-02-16 19:49:36 +01:00
rfu_LMAN_stopManager(FALSE);
}
2020-06-06 22:46:19 +02:00
u8 Rfu_GetMultiplayerId(void)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
if (Rfu.parentChild == MODE_PARENT)
2020-02-16 19:49:36 +01:00
return 0;
2020-06-06 22:46:19 +02:00
return Rfu.multiplayerId;
2020-02-16 19:49:36 +01:00
}
2020-06-06 22:46:19 +02:00
u8 Rfu_GetLinkPlayerCount(void)
2020-02-16 19:49:36 +01:00
{
return Rfu.playerCount;
}
bool8 IsLinkRfuTaskFinished(void)
{
2020-06-09 00:16:57 +02:00
if (Rfu.status == RFU_STATUS_CONNECTION_ERROR)
2020-02-16 19:49:36 +01:00
return FALSE;
2020-06-06 22:46:19 +02:00
return Rfu.callback ? FALSE : TRUE;
2020-02-16 19:49:36 +01:00
}
2020-06-06 22:46:19 +02:00
static void CallRfuFunc(void)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
if (Rfu.callback)
Rfu.callback();
2020-02-16 19:49:36 +01:00
}
2020-06-09 00:16:57 +02:00
static bool8 CheckForLeavingGroupMembers(void)
2020-02-16 19:49:36 +01:00
{
s32 i;
2020-06-09 00:16:57 +02:00
bool8 memberLeft = FALSE;
2020-06-06 22:46:19 +02:00
for (i = 0; i < RFU_CHILD_MAX; i++)
2020-02-16 19:49:36 +01:00
{
if (Rfu.partnerSendStatuses[i] < RFU_STATUS_JOIN_GROUP_OK
|| Rfu.partnerSendStatuses[i] > RFU_STATUS_JOIN_GROUP_NO)
2020-02-16 19:49:36 +01:00
{
if (gRfuSlotStatusNI[i]->recv.state == SLOT_STATE_RECV_SUCCESS
|| gRfuSlotStatusNI[i]->recv.state == SLOT_STATE_RECV_SUCCESS_AND_SENDSIDE_UNKNOWN)
2020-02-16 19:49:36 +01:00
{
if (Rfu.partnerRecvStatuses[i] == RFU_STATUS_LEAVE_GROUP_NOTICE)
{
Rfu.partnerSendStatuses[i] = RFU_STATUS_LEAVE_GROUP;
2021-07-12 02:42:05 +02:00
Rfu.partnerRecvStatuses[i] = RFU_STATUS_CHILD_LEAVE_READY;
rfu_clearSlot(TYPE_NI_RECV, i);
rfu_NI_setSendData(1 << i, 8, &Rfu.partnerSendStatuses[i], 1);
memberLeft = TRUE;
}
}
else if (gRfuSlotStatusNI[Rfu.childSlot]->recv.state == SLOT_STATE_RECV_FAILED)
2020-06-06 22:46:19 +02:00
rfu_clearSlot(TYPE_NI_RECV, i);
{
2020-02-16 19:49:36 +01:00
}
}
}
2020-06-09 00:16:57 +02:00
return memberLeft;
2020-02-16 19:49:36 +01:00
}
2021-07-12 02:42:05 +02:00
bool32 RfuTryDisconnectLeavingChildren(void)
2020-02-16 19:49:36 +01:00
{
2021-07-12 02:42:05 +02:00
u8 childrenLeaving = 0;
2020-02-16 19:49:36 +01:00
s32 i;
2021-07-12 02:42:05 +02:00
// Check all children, get those waiting to be disconnected
2020-06-06 22:46:19 +02:00
for (i = 0; i < RFU_CHILD_MAX; i++)
2020-02-16 19:49:36 +01:00
{
2021-07-12 02:42:05 +02:00
if (Rfu.partnerRecvStatuses[i] == RFU_STATUS_CHILD_LEAVE)
2020-02-16 19:49:36 +01:00
{
2021-07-12 02:42:05 +02:00
childrenLeaving |= (1 << i);
2020-06-09 00:16:57 +02:00
Rfu.partnerRecvStatuses[i] = RFU_STATUS_OK;
2020-02-16 19:49:36 +01:00
}
}
2021-07-12 02:42:05 +02:00
// Disconnect any leaving children
if (childrenLeaving)
2020-02-16 19:49:36 +01:00
{
2021-07-12 02:42:05 +02:00
rfu_REQ_disconnect(childrenLeaving);
2020-02-16 19:49:36 +01:00
rfu_waitREQComplete();
}
2021-07-12 02:42:05 +02:00
// Return true if any children have left or are still waiting to leave
2020-06-06 22:46:19 +02:00
for (i = 0; i < RFU_CHILD_MAX; i++)
2020-02-16 19:49:36 +01:00
{
2021-07-12 02:42:05 +02:00
if (Rfu.partnerRecvStatuses[i] == RFU_STATUS_CHILD_LEAVE_READY
|| Rfu.partnerRecvStatuses[i] == RFU_STATUS_CHILD_LEAVE)
2020-02-16 19:49:36 +01:00
return TRUE;
}
return FALSE;
}
2020-06-09 00:16:57 +02:00
bool32 HasTrainerLeftPartnersList(u16 trainerId, const u8 *name)
2020-02-16 19:49:36 +01:00
{
2020-06-09 00:16:57 +02:00
u8 idx = GetPartnerIndexByNameAndTrainerID(name, trainerId);
2020-06-06 22:46:19 +02:00
if (idx == 0xFF)
2020-02-16 19:49:36 +01:00
return TRUE;
2020-06-09 00:16:57 +02:00
if (Rfu.partnerSendStatuses[idx] == RFU_STATUS_LEAVE_GROUP)
2020-02-16 19:49:36 +01:00
return TRUE;
return FALSE;
}
2020-06-09 00:16:57 +02:00
void SendRfuStatusToPartner(u8 status, u16 trainerId, const u8 *name)
2020-02-16 19:49:36 +01:00
{
2020-06-09 00:16:57 +02:00
u8 idx = GetPartnerIndexByNameAndTrainerID(name, trainerId);
Rfu.partnerSendStatuses[idx] = status;
2020-06-06 22:46:19 +02:00
rfu_clearSlot(TYPE_NI_SEND, idx);
2020-06-09 00:16:57 +02:00
rfu_NI_setSendData(1 << idx, 8, &Rfu.partnerSendStatuses[idx], 1);
2020-02-16 19:49:36 +01:00
}
2020-06-09 00:16:57 +02:00
void SendLeaveGroupNotice(void)
2020-02-16 19:49:36 +01:00
{
2020-06-09 00:16:57 +02:00
Rfu.unk_c85 = RFU_STATUS_LEAVE_GROUP_NOTICE;
2020-06-06 22:46:19 +02:00
rfu_clearSlot(TYPE_NI_SEND, Rfu.childSlot);
rfu_NI_setSendData(1 << Rfu.childSlot, 8, &Rfu.unk_c85, 1);
2020-02-16 19:49:36 +01:00
}
2020-06-09 00:16:57 +02:00
u32 WaitSendRfuStatusToPartner(u16 trainerId, const u8 *name)
2020-02-16 19:49:36 +01:00
{
2020-06-09 00:16:57 +02:00
u8 idx = GetPartnerIndexByNameAndTrainerID(name, trainerId);
2020-06-06 22:46:19 +02:00
if (idx == 0xFF)
2020-02-16 19:49:36 +01:00
return 2;
2020-06-06 22:46:19 +02:00
if (gRfuSlotStatusNI[idx]->send.state == 0)
2020-02-16 19:49:36 +01:00
return 1;
return 0;
}
2020-06-09 00:16:57 +02:00
static void UpdateChildStatuses(void)
2020-02-16 19:49:36 +01:00
{
s32 i;
2020-06-09 00:16:57 +02:00
CheckForLeavingGroupMembers();
2020-06-06 22:46:19 +02:00
for (i = 0; i < RFU_CHILD_MAX; i++)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
if (gRfuSlotStatusNI[i]->send.state == SLOT_STATE_SEND_SUCCESS
|| gRfuSlotStatusNI[i]->send.state == SLOT_STATE_SEND_FAILED)
2020-02-16 19:49:36 +01:00
{
2021-07-12 02:42:05 +02:00
if (Rfu.partnerRecvStatuses[i] == RFU_STATUS_CHILD_LEAVE_READY)
Rfu.partnerRecvStatuses[i] = RFU_STATUS_CHILD_LEAVE;
2020-06-06 22:46:19 +02:00
rfu_clearSlot(TYPE_NI_SEND, i);
2020-02-16 19:49:36 +01:00
}
}
}
2020-06-06 22:46:19 +02:00
static s32 sub_80107A0(void)
2020-02-16 19:49:36 +01:00
{
2020-06-09 00:16:57 +02:00
s32 status = RFU_STATUS_OK;
if (Rfu.unk_c85 == RFU_STATUS_LEAVE_GROUP_NOTICE)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
if (gRfuSlotStatusNI[Rfu.childSlot]->send.state == SLOT_STATE_SEND_SUCCESS
|| gRfuSlotStatusNI[Rfu.childSlot]->send.state == SLOT_STATE_SEND_FAILED)
rfu_clearSlot(TYPE_NI_SEND, Rfu.childSlot);
2020-02-16 19:49:36 +01:00
}
2020-06-06 22:46:19 +02:00
if (gRfuSlotStatusNI[Rfu.childSlot]->recv.state == SLOT_STATE_RECV_SUCCESS
|| gRfuSlotStatusNI[Rfu.childSlot]->recv.state == SLOT_STATE_RECV_SUCCESS_AND_SENDSIDE_UNKNOWN)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
rfu_clearSlot(TYPE_NI_RECV, Rfu.childSlot);
2020-06-09 00:16:57 +02:00
RfuSetStatus(Rfu.recvStatus, 0);
status = Rfu.recvStatus;
2020-02-16 19:49:36 +01:00
}
2020-06-07 23:37:09 +02:00
else if (gRfuSlotStatusNI[Rfu.childSlot]->recv.state == SLOT_STATE_RECV_FAILED)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
rfu_clearSlot(TYPE_NI_RECV, Rfu.childSlot);
2020-06-09 00:16:57 +02:00
status = RFU_STATUS_JOIN_GROUP_NO;
2020-02-16 19:49:36 +01:00
}
2020-06-09 00:16:57 +02:00
return status;
2020-02-16 19:49:36 +01:00
}
#define tState data[0]
2020-06-06 22:46:19 +02:00
static void sub_801084C(u8 taskId)
2020-02-16 19:49:36 +01:00
{
s32 i;
2020-06-09 00:16:57 +02:00
if (Rfu.status == RFU_STATUS_FATAL_ERROR || Rfu.status == RFU_STATUS_CONNECTION_ERROR)
2020-02-16 19:49:36 +01:00
{
Rfu.unk_ce8 = FALSE;
2020-02-16 19:49:36 +01:00
DestroyTask(taskId);
}
switch (gTasks[taskId].tState)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
case 0:
2020-06-07 23:37:09 +02:00
if (AreNoPlayersReceiving())
2020-06-06 22:46:19 +02:00
{
ResetBlockReceivedFlags();
2021-03-19 19:10:13 +01:00
LocalLinkPlayerToBlock();
gTasks[taskId].tState++;
2020-06-06 22:46:19 +02:00
}
break;
case 1:
if (Rfu.parentChild == MODE_PARENT)
{
if (gReceivedRemoteLinkPlayers)
RfuPrepareSendBuffer(RFUCMD_SEND_PLAYER_IDS_NEW);
2020-02-16 19:49:36 +01:00
else
RfuPrepareSendBuffer(RFUCMD_SEND_PLAYER_IDS);
gTasks[taskId].tState = 101;
2020-06-06 22:46:19 +02:00
}
else
gTasks[taskId].tState = 2;
2020-06-06 22:46:19 +02:00
break;
case 101:
if (gSendCmd[0] == 0)
gTasks[taskId].tState = 2;
2020-06-06 22:46:19 +02:00
break;
case 2:
if (Rfu.playerCount)
gTasks[taskId].tState++;
2020-06-06 22:46:19 +02:00
break;
case 3:
if (Rfu.parentChild == MODE_PARENT)
{
2020-06-07 23:37:09 +02:00
if (AreNoPlayersReceiving())
2020-02-16 19:49:36 +01:00
{
Rfu.blockRequestType = BLOCK_REQ_SIZE_NONE;
2020-08-24 20:52:33 +02:00
RfuPrepareSendBuffer(RFUCMD_SEND_BLOCK_REQ);
gTasks[taskId].tState++;
2020-02-16 19:49:36 +01:00
}
2020-06-06 22:46:19 +02:00
}
else
gTasks[taskId].tState++;
2020-06-06 22:46:19 +02:00
break;
case 4:
if (sub_800FC88())
gTasks[taskId].tState++;
2020-06-06 22:46:19 +02:00
break;
case 5:
for (i = 0; i < Rfu.playerCount; i++)
{
LinkPlayerFromBlock(i);
Rfu_ResetBlockReceivedFlag(i);
}
gTasks[taskId].tState++;
2020-06-06 22:46:19 +02:00
break;
case 6:
DestroyTask(taskId);
gReceivedRemoteLinkPlayers = TRUE;
Rfu.unk_ce8 = FALSE;
rfu_LMAN_setLinkRecovery(1, 600);
if (Rfu.unk_ce6)
{
for (i = 0; i < RFU_CHILD_MAX; i++)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
if ((Rfu.unk_ce6 >> i) & 1)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
Rfu.unk_ce5 = 1 << i;
Rfu.unk_ce6 ^= (1 << i);
2020-02-16 19:49:36 +01:00
}
}
2020-06-06 22:46:19 +02:00
}
break;
2020-02-16 19:49:36 +01:00
}
}
2020-06-07 23:37:09 +02:00
static void ClearSelectedLinkPlayerIds(u16 selected)
2020-02-16 19:49:36 +01:00
{
s32 i;
2020-06-06 22:46:19 +02:00
for (i = 0; i < RFU_CHILD_MAX; i++)
2020-02-16 19:49:36 +01:00
{
2020-06-07 23:37:09 +02:00
if ((selected >> i) & 1)
2020-06-06 22:46:19 +02:00
Rfu.linkPlayerIdx[i] = 0;
2020-02-16 19:49:36 +01:00
}
}
2020-06-06 22:46:19 +02:00
static void ReceiveRfuLinkPlayers(const struct SioInfo *sioInfo)
2020-02-16 19:49:36 +01:00
{
s32 i;
2020-06-06 22:46:19 +02:00
Rfu.playerCount = sioInfo->playerCount;
for (i = 0; i < RFU_CHILD_MAX; i++)
Rfu.linkPlayerIdx[i] = sioInfo->linkPlayerIdx[i];
2020-02-16 19:49:36 +01:00
for (i = 0; i < MAX_RFU_PLAYERS; i++)
{
2020-06-06 22:46:19 +02:00
gLinkPlayers[i] = sioInfo->linkPlayers[i];
2021-03-19 19:10:13 +01:00
ConvertLinkPlayerName(gLinkPlayers + i);
2020-02-16 19:49:36 +01:00
}
}
2020-06-06 22:46:19 +02:00
static void ValidateAndReceivePokemonSioInfo(void *recvBuffer)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
if (strcmp(sASCII_PokemonSioInfo, recvBuffer) == 0)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
ReceiveRfuLinkPlayers(recvBuffer);
CpuFill16(0, recvBuffer, sizeof(struct SioInfo));
2020-02-16 19:49:36 +01:00
ResetBlockReceivedFlag(0);
}
}
2020-06-06 22:46:19 +02:00
static void Task_ExchangeLinkPlayers(u8 taskId)
2020-02-16 19:49:36 +01:00
{
s32 i;
struct LinkPlayerBlock *playerBlock;
struct SioInfo *sio;
u8 playerId = Rfu.linkPlayerIdx[sUnknown_082ED68C[Rfu.unk_ce9]];
2020-06-09 00:16:57 +02:00
if (Rfu.status == RFU_STATUS_FATAL_ERROR || Rfu.status == RFU_STATUS_CONNECTION_ERROR)
2020-02-16 19:49:36 +01:00
{
Rfu.unk_ce8 = FALSE;
2020-02-16 19:49:36 +01:00
DestroyTask(taskId);
}
switch (gTasks[taskId].tState)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
case 0:
if (gSendCmd[0] == 0)
{
ResetBlockReceivedFlag(playerId);
RfuPrepareSendBuffer(RFUCMD_SEND_PLAYER_IDS_NEW);
gTasks[taskId].tState++;
2020-06-06 22:46:19 +02:00
}
break;
case 1:
if (gSendCmd[0] == 0)
gTasks[taskId].tState++;
2020-06-06 22:46:19 +02:00
break;
case 2:
if ((GetBlockReceivedStatus() >> playerId) & 1)
2020-06-06 22:46:19 +02:00
{
ResetBlockReceivedFlag(playerId);
playerBlock = (struct LinkPlayerBlock *)gBlockRecvBuffer[playerId];
gLinkPlayers[playerId] = playerBlock->linkPlayer;
ConvertLinkPlayerName(&gLinkPlayers[playerId]);
gTasks[taskId].tState++;
2020-06-06 22:46:19 +02:00
}
break;
case 3:
sio = (struct SioInfo *)gBlockSendBuffer;
memcpy(sio->magic, sASCII_PokemonSioInfo, sizeof sASCII_PokemonSioInfo);
sio->playerCount = Rfu.playerCount;
2020-06-06 22:46:19 +02:00
for (i = 0; i < RFU_CHILD_MAX; i++)
sio->linkPlayerIdx[i] = Rfu.linkPlayerIdx[i];
memcpy(sio->linkPlayers, gLinkPlayers, sizeof gLinkPlayers);
gTasks[taskId].tState++;
2020-06-06 22:46:19 +02:00
// fallthrough
case 4:
sio = (struct SioInfo *)gBlockSendBuffer;
sio->playerCount = Rfu.playerCount;
2020-06-06 22:46:19 +02:00
for (i = 0; i < RFU_CHILD_MAX; i++)
sio->linkPlayerIdx[i] = Rfu.linkPlayerIdx[i];
memcpy(sio->linkPlayers, gLinkPlayers, sizeof(gLinkPlayers));
2020-06-06 22:46:19 +02:00
if (SendBlock(0, gBlockSendBuffer, 0xa0))
gTasks[taskId].tState++;
2020-06-06 22:46:19 +02:00
break;
case 5:
if (IsLinkTaskFinished() && GetBlockReceivedStatus() & 1)
{
CpuFill16(0, gBlockRecvBuffer, sizeof(struct SioInfo));
ResetBlockReceivedFlag(0);
Rfu.unk_ce8 = FALSE;
2020-06-06 22:46:19 +02:00
if (Rfu.unk_ce6)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
for (i = 0; i < 4; i++)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
if ((Rfu.unk_ce6 >> i) & 1)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
Rfu.unk_ce5 = 1 << i;
Rfu.unk_ce6 ^= (1 << i);
Rfu.unk_ce8 = TRUE;
2020-06-06 22:46:19 +02:00
break;
2020-02-16 19:49:36 +01:00
}
}
}
2020-06-06 22:46:19 +02:00
DestroyTask(taskId);
}
break;
2020-02-16 19:49:36 +01:00
}
}
2020-06-06 22:46:19 +02:00
static void sub_8010D0C(u8 taskId)
2020-02-16 19:49:36 +01:00
{
2020-06-09 00:16:57 +02:00
if (Rfu.status == RFU_STATUS_FATAL_ERROR || Rfu.status == RFU_STATUS_CONNECTION_ERROR)
2020-02-16 19:49:36 +01:00
DestroyTask(taskId);
switch (gTasks[taskId].tState)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
case 0:
if (Rfu.playerCount)
{
2021-03-19 19:10:13 +01:00
LocalLinkPlayerToBlock();
2020-06-06 22:46:19 +02:00
SendBlock(0, gBlockSendBuffer, sizeof(struct LinkPlayerBlock));
gTasks[taskId].tState++;
2020-06-06 22:46:19 +02:00
}
break;
case 1:
if (IsLinkTaskFinished())
gTasks[taskId].tState++;
2020-06-06 22:46:19 +02:00
break;
case 2:
if (GetBlockReceivedStatus() & 1)
{
ReceiveRfuLinkPlayers((const struct SioInfo *)gBlockRecvBuffer);
ResetBlockReceivedFlag(0);
gReceivedRemoteLinkPlayers = 1;
DestroyTask(taskId);
}
break;
2020-02-16 19:49:36 +01:00
}
}
2020-06-06 22:46:19 +02:00
static void RfuCheckErrorStatus(void)
2020-02-16 19:49:36 +01:00
{
if (Rfu.errorState == RFU_ERROR_STATE_1 && lman.childClockSlave_flag == 0)
2020-02-16 19:49:36 +01:00
{
if (gMain.callback2 == c2_mystery_gift_e_reader_run || lman.init_param->mboot_flag)
gWirelessCommType = 2;
SetMainCallback2(CB2_LinkError);
gMain.savedCallback = CB2_LinkError;
2020-06-09 00:16:57 +02:00
BufferLinkErrorInfo((Rfu.linkmanMsg << 16) | (Rfu.unk_10 << 8) | Rfu.unk_12, Rfu.recvQueue.count, Rfu.sendQueue.count, RfuGetStatus() == RFU_STATUS_CONNECTION_ERROR);
Rfu.errorState = RFU_ERROR_STATE_2;
2020-02-16 19:49:36 +01:00
CloseLink();
}
2020-06-06 22:46:19 +02:00
else if (Rfu.sendQueue.full == TRUE || Rfu.recvQueue.full == TRUE)
2020-02-16 19:49:36 +01:00
{
if (lman.childClockSlave_flag)
rfu_LMAN_requestChangeAgbClockMaster();
2020-06-09 00:16:57 +02:00
RfuSetStatus(RFU_STATUS_FATAL_ERROR, 0x7000);
2020-06-06 22:46:19 +02:00
GetLinkmanErrorParams(0x7000);
2020-02-16 19:49:36 +01:00
}
}
static void RfuMain1_UnionRoom(void)
2020-02-16 19:49:36 +01:00
{
if (lman.parent_child == MODE_PARENT)
2020-02-16 19:49:36 +01:00
{
rfu_REQ_recvData();
rfu_waitREQComplete();
rfu_LMAN_REQ_sendData(0);
}
}
2021-07-12 02:42:05 +02:00
// Rfu equivalent of LinkMain1
bool32 RfuMain1(void)
2020-02-16 19:49:36 +01:00
{
bool32 retval = FALSE;
2020-06-06 22:46:19 +02:00
Rfu.parentId = 0;
2020-02-16 19:49:36 +01:00
rfu_LMAN_manager_entity(Random2());
2020-06-06 22:46:19 +02:00
if (!Rfu.isShuttingDown)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
switch (Rfu.parentChild)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
case MODE_PARENT:
RfuMain1_Parent();
2020-06-06 22:46:19 +02:00
break;
case MODE_CHILD:
retval = RfuMain1_Child();
2020-06-06 22:46:19 +02:00
break;
2021-07-12 02:42:05 +02:00
case MODE_P_C_SWITCH:
RfuMain1_UnionRoom();
2020-06-06 22:46:19 +02:00
break;
2020-02-16 19:49:36 +01:00
}
}
return retval;
}
2021-07-12 02:42:05 +02:00
// Rfu equivalent of LinkMain2
bool32 RfuMain2(void)
2020-02-16 19:49:36 +01:00
{
bool32 retval = FALSE;
2020-06-06 22:46:19 +02:00
if (!Rfu.isShuttingDown)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
if (Rfu.parentChild == MODE_PARENT)
retval = RfuMain2_Parent();
2020-06-06 22:46:19 +02:00
RfuCheckErrorStatus();
2020-02-16 19:49:36 +01:00
}
return retval;
}
2020-06-06 22:46:19 +02:00
static void CopyPlayerNameToUnameBuffer(void)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
StringCopy(gHostRFUtgtUnameBuffer, gSaveBlock2Ptr->playerName);
2020-02-16 19:49:36 +01:00
}
2020-05-30 10:09:21 +02:00
void ClearAndInitHostRFUtgtGname(void)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
memset(&gHostRFUtgtGnameBuffer, 0, RFU_GAME_NAME_LENGTH);
InitHostRFUtgtGname(&gHostRFUtgtGnameBuffer, ACTIVITY_NONE, FALSE, 0);
2020-02-16 19:49:36 +01:00
}
2020-06-09 00:16:57 +02:00
void SetHostRFUtgtGname(u8 activity, u32 child_sprite_genders, bool32 started)
2020-02-16 19:49:36 +01:00
{
2020-06-07 23:37:09 +02:00
InitHostRFUtgtGname(&gHostRFUtgtGnameBuffer, activity, started, child_sprite_genders);
2020-02-16 19:49:36 +01:00
}
2020-06-06 22:46:19 +02:00
void SetGnameBufferWonderFlags(bool32 hasNews, bool32 hasCard)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
gHostRFUtgtGnameBuffer.unk_00.hasNews = hasNews;
gHostRFUtgtGnameBuffer.unk_00.hasCard = hasCard;
2020-02-16 19:49:36 +01:00
}
2020-06-06 22:46:19 +02:00
void SetTradeBoardRegisteredMonInfo(u32 type, u32 species, u32 level)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
gHostRFUtgtGnameBuffer.type = type;
gHostRFUtgtGnameBuffer.species = species;
gHostRFUtgtGnameBuffer.level = level;
2020-02-16 19:49:36 +01:00
}
u8 GetLinkPlayerInfoFlags(s32 playerId)
2020-02-16 19:49:36 +01:00
{
u8 retval = 0x80;
retval |= (gLinkPlayers[playerId].gender << 3);
retval |= (gLinkPlayers[playerId].trainerId & 7);
2020-02-16 19:49:36 +01:00
return retval;
}
void GetOtherPlayersInfoFlags(void)
2020-02-16 19:49:36 +01:00
{
struct GFtgtGname *gname = &gHostRFUtgtGnameBuffer;
2020-02-16 19:49:36 +01:00
s32 i;
for (i = 1; i < GetLinkPlayerCount(); i++)
gname->child_sprite_gender[i - 1] = GetLinkPlayerInfoFlags(i);
2020-02-16 19:49:36 +01:00
}
2020-06-09 00:16:57 +02:00
void UpdateGameData_GroupLockedIn(bool8 started)
2020-02-16 19:49:36 +01:00
{
2020-06-09 00:16:57 +02:00
gHostRFUtgtGnameBuffer.started = started;
rfu_REQ_configGameData(0, RFU_SERIAL_A, (void *)&gHostRFUtgtGnameBuffer, gHostRFUtgtUnameBuffer);
2020-02-16 19:49:36 +01:00
}
2020-06-09 00:16:57 +02:00
void UpdateGameData_SetActivity(u8 activity, u32 flags, bool32 started)
2020-02-16 19:49:36 +01:00
{
2020-06-09 00:16:57 +02:00
if (activity != ACTIVITY_NONE)
SetHostRFUtgtGname(activity, flags, started);
rfu_REQ_configGameData(0, RFU_SERIAL_A, (void *)&gHostRFUtgtGnameBuffer, gHostRFUtgtUnameBuffer);
2020-02-16 19:49:36 +01:00
}
void SetUnionRoomChatPlayerData(u32 numPlayers)
2020-02-16 19:49:36 +01:00
{
s32 i;
2020-06-06 22:46:19 +02:00
u32 numConnectedChildren;
u32 child_sprite_genders;
s32 slots;
2020-02-16 19:49:36 +01:00
2020-06-06 22:46:19 +02:00
if (GetHostRFUtgtGname()->activity == (ACTIVITY_CHAT | IN_UNION_ROOM))
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
numConnectedChildren = 0;
child_sprite_genders = 0;
slots = Rfu.unk_ce2 ^ Rfu.disconnectSlots;
2020-02-16 19:49:36 +01:00
for (i = 0; i < 4; i++)
{
if ((slots >> i) & 1)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
child_sprite_genders |= ((0x80 | ((gLinkPlayers[Rfu.linkPlayerIdx[i]].gender & 1) << 3) | (gLinkPlayers[Rfu.linkPlayerIdx[i]].trainerId & 7)) << (numConnectedChildren << 3));
numConnectedChildren++;
if (numConnectedChildren == numPlayers - 1)
2020-02-16 19:49:36 +01:00
break;
}
}
UpdateGameData_SetActivity(ACTIVITY_CHAT | IN_UNION_ROOM, child_sprite_genders, FALSE);
2020-02-16 19:49:36 +01:00
}
}
2020-06-06 22:46:19 +02:00
void GetLinkmanErrorParams(u32 msg)
2020-02-16 19:49:36 +01:00
{
if (Rfu.errorState == RFU_ERROR_STATE_NONE)
2020-02-16 19:49:36 +01:00
{
Rfu.unk_10 = lman.param[0];
Rfu.unk_12 = lman.param[1];
2020-06-06 22:46:19 +02:00
Rfu.linkmanMsg = msg;
Rfu.errorState = RFU_ERROR_STATE_1;
2020-02-16 19:49:36 +01:00
}
}
2020-06-06 22:46:19 +02:00
static void ResetErrorState(void)
2020-02-16 19:49:36 +01:00
{
Rfu.errorState = RFU_ERROR_STATE_NONE;
2020-02-16 19:49:36 +01:00
}
void RfuSetIgnoreError(bool32 enable)
2020-02-16 19:49:36 +01:00
{
if (!enable)
Rfu.errorState = RFU_ERROR_STATE_NONE;
2020-02-16 19:49:36 +01:00
else
Rfu.errorState = RFU_ERROR_STATE_IGNORE;
2020-02-16 19:49:36 +01:00
}
static void DisconnectNewChild(void)
2020-02-16 19:49:36 +01:00
{
SendDisconnectCommand(lman.acceptSlot_flag, RFU_DISCONNECT_ERROR);
2020-06-06 22:46:19 +02:00
Rfu.callback = NULL;
2020-02-16 19:49:36 +01:00
}
static void StartDisconnectNewChild(void)
2020-02-16 19:49:36 +01:00
{
Rfu.callback = DisconnectNewChild;
2020-02-16 19:49:36 +01:00
}
2020-06-06 22:46:19 +02:00
static void sub_801120C(u8 msg, u8 paramCount)
2020-02-16 19:49:36 +01:00
{
u8 i;
u8 disconnectFlag = 0;
2020-06-06 22:46:19 +02:00
switch (msg)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
case LMAN_MSG_INITIALIZE_COMPLETED:
2021-07-12 02:42:05 +02:00
Rfu.state = RFUSTATE_PARENT_CONNECT;
2020-02-16 19:49:36 +01:00
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_NEW_CHILD_CONNECT_DETECTED:
2020-02-16 19:49:36 +01:00
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_NEW_CHILD_CONNECT_ACCEPTED:
2020-02-16 19:49:36 +01:00
sub_80115EC(lman.param[0]);
2020-06-06 22:46:19 +02:00
for (i = 0; i < RFU_CHILD_MAX; i++)
2020-02-16 19:49:36 +01:00
{
if ((lman.param[0] >> i) & 1)
{
struct GFtgtGname *structPtr = (void *)gRfuLinkStatus->partner[i].gname;
2020-05-30 10:09:21 +02:00
if (structPtr->activity == GetHostRFUtgtGname()->activity)
2020-02-16 19:49:36 +01:00
{
2020-06-09 00:16:57 +02:00
Rfu.partnerSendStatuses[i] = RFU_STATUS_OK;
Rfu.partnerRecvStatuses[i] = RFU_STATUS_OK;
rfu_setRecvBuffer(TYPE_NI, i, &Rfu.partnerRecvStatuses[i], sizeof(Rfu.partnerRecvStatuses[0]));
2020-02-16 19:49:36 +01:00
}
else
{
2020-06-06 22:46:19 +02:00
disconnectFlag |= (1 << i);
2020-02-16 19:49:36 +01:00
}
}
}
2020-06-06 22:46:19 +02:00
if (disconnectFlag)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
rfu_REQ_disconnect(disconnectFlag);
2020-02-16 19:49:36 +01:00
rfu_waitREQComplete();
}
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_NEW_CHILD_CONNECT_REJECTED:
2020-02-16 19:49:36 +01:00
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_SEARCH_CHILD_PERIOD_EXPIRED:
2020-02-16 19:49:36 +01:00
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_END_WAIT_CHILD_NAME:
if (Rfu.acceptSlot_flag != lman.acceptSlot_flag)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
rfu_REQ_disconnect(Rfu.acceptSlot_flag ^ lman.acceptSlot_flag);
2020-02-16 19:49:36 +01:00
rfu_waitREQComplete();
}
2021-07-12 02:42:05 +02:00
Rfu.state = RFUSTATE_PARENT_FINALIZE_START;
2020-02-16 19:49:36 +01:00
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_LINK_LOSS_DETECTED_AND_START_RECOVERY:
Rfu.linkLossRecoveryState = 1;
2020-02-16 19:49:36 +01:00
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_LINK_RECOVERY_SUCCESSED:
Rfu.linkLossRecoveryState = 3;
2020-02-16 19:49:36 +01:00
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_LINK_LOSS_DETECTED_AND_DISCONNECTED:
case LMAN_MSG_LINK_RECOVERY_FAILED_AND_DISCONNECTED:
Rfu.linkLossRecoveryState = 4;
2020-02-16 19:49:36 +01:00
Rfu.unk_ce2 &= ~lman.param[0];
if (gReceivedRemoteLinkPlayers == 1)
{
if (Rfu.unk_ce2 == 0)
2020-06-06 22:46:19 +02:00
GetLinkmanErrorParams(msg);
2020-02-16 19:49:36 +01:00
else
StartDisconnectNewChild();
2020-02-16 19:49:36 +01:00
}
2020-06-09 00:16:57 +02:00
RfuSetStatus(RFU_STATUS_CONNECTION_ERROR, msg);
2020-02-16 19:49:36 +01:00
break;
2021-07-12 02:42:05 +02:00
case 0x34: // ? Not a valid LMAN_MSG value
2020-06-06 22:46:19 +02:00
case LMAN_MSG_RFU_POWER_DOWN:
case LMAN_MSG_MANAGER_STOPPED:
case LMAN_MSG_MANAGER_FORCED_STOPPED_AND_RFU_RESET:
2020-02-16 19:49:36 +01:00
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_LMAN_API_ERROR_RETURN:
2020-06-09 00:16:57 +02:00
RfuSetStatus(RFU_STATUS_FATAL_ERROR, msg);
2020-06-06 22:46:19 +02:00
GetLinkmanErrorParams(msg);
Rfu.isShuttingDown = TRUE;
2020-02-16 19:49:36 +01:00
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_REQ_API_ERROR:
case LMAN_MSG_WATCH_DOG_TIMER_ERROR:
case LMAN_MSG_CLOCK_SLAVE_MS_CHANGE_ERROR_BY_DMA:
case LMAN_MSG_RFU_FATAL_ERROR:
GetLinkmanErrorParams(msg);
2020-06-09 00:16:57 +02:00
RfuSetStatus(RFU_STATUS_FATAL_ERROR, msg);
Rfu.unk_cdb = TRUE;
2020-02-16 19:49:36 +01:00
break;
}
}
2020-06-06 22:46:19 +02:00
void sub_8011404(u8 msg, u8 unused1)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
switch (msg)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
case LMAN_MSG_INITIALIZE_COMPLETED:
2021-07-12 02:42:05 +02:00
Rfu.state = RFUSTATE_CHILD_CONNECT;
2020-02-16 19:49:36 +01:00
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_PARENT_FOUND:
Rfu.parentId = lman.param[0];
2020-02-16 19:49:36 +01:00
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_SEARCH_PARENT_PERIOD_EXPIRED:
2020-02-16 19:49:36 +01:00
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_CONNECT_PARENT_SUCCESSED:
Rfu.childSlot = lman.param[0];
2020-02-16 19:49:36 +01:00
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_CONNECT_PARENT_FAILED:
2020-06-09 00:16:57 +02:00
RfuSetStatus(RFU_STATUS_CONNECTION_ERROR, msg);
2020-02-16 19:49:36 +01:00
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_CHILD_NAME_SEND_COMPLETED:
2021-07-12 02:42:05 +02:00
Rfu.state = RFUSTATE_CHILD_TRY_JOIN;
Rfu.unk_c85 = RFU_STATUS_OK;
2020-06-09 00:16:57 +02:00
Rfu.recvStatus = RFU_STATUS_OK;
rfu_setRecvBuffer(TYPE_NI, Rfu.childSlot, &Rfu.recvStatus, sizeof(Rfu.recvStatus));
rfu_setRecvBuffer(TYPE_UNI, Rfu.childSlot, Rfu.unk_c3f, sizeof(Rfu.unk_c3f));
2020-02-16 19:49:36 +01:00
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_CHILD_NAME_SEND_FAILED_AND_DISCONNECTED:
2020-06-09 00:16:57 +02:00
RfuSetStatus(RFU_STATUS_CONNECTION_ERROR, msg);
2020-02-16 19:49:36 +01:00
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_LINK_LOSS_DETECTED_AND_DISCONNECTED:
Rfu.linkLossRecoveryState = 2;
2020-06-09 00:16:57 +02:00
if (Rfu.recvStatus == RFU_STATUS_JOIN_GROUP_NO)
2020-02-16 19:49:36 +01:00
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_LINK_RECOVERY_FAILED_AND_DISCONNECTED:
if (Rfu.linkLossRecoveryState != 2)
Rfu.linkLossRecoveryState = 4;
2020-06-09 00:16:57 +02:00
if (Rfu.recvStatus != RFU_STATUS_LEAVE_GROUP)
RfuSetStatus(RFU_STATUS_CONNECTION_ERROR, msg);
2020-06-06 22:46:19 +02:00
Debug_PrintString(sASCII_LinkLossDisconnect, 5, 5);
2020-02-16 19:49:36 +01:00
if (gReceivedRemoteLinkPlayers == 1)
2020-06-06 22:46:19 +02:00
GetLinkmanErrorParams(msg);
2020-02-16 19:49:36 +01:00
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_LINK_LOSS_DETECTED_AND_START_RECOVERY:
Rfu.linkLossRecoveryState = 1;
Debug_PrintString(sASCII_LinkLossRecoveryNow, 5, 5);
2020-02-16 19:49:36 +01:00
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_LINK_RECOVERY_SUCCESSED:
Rfu.linkLossRecoveryState = 3;
2020-06-07 23:37:09 +02:00
Rfu.linkRecovered = TRUE;
2020-02-16 19:49:36 +01:00
break;
case 0x34: // ? Not a valid LMAN_MSG value
2020-02-16 19:49:36 +01:00
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_RFU_POWER_DOWN:
case LMAN_MSG_MANAGER_STOPPED:
case LMAN_MSG_MANAGER_FORCED_STOPPED_AND_RFU_RESET:
2020-02-16 19:49:36 +01:00
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_LMAN_API_ERROR_RETURN:
2020-06-09 00:16:57 +02:00
RfuSetStatus(RFU_STATUS_FATAL_ERROR, msg);
2020-06-06 22:46:19 +02:00
GetLinkmanErrorParams(msg);
Rfu.isShuttingDown = TRUE;
2020-02-16 19:49:36 +01:00
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_REQ_API_ERROR:
case LMAN_MSG_WATCH_DOG_TIMER_ERROR:
case LMAN_MSG_CLOCK_SLAVE_MS_CHANGE_ERROR_BY_DMA:
case LMAN_MSG_RFU_FATAL_ERROR:
2020-06-09 00:16:57 +02:00
RfuSetStatus(RFU_STATUS_FATAL_ERROR, msg);
2020-06-06 22:46:19 +02:00
GetLinkmanErrorParams(msg);
Rfu.unk_cdb = TRUE;
2020-02-16 19:49:36 +01:00
break;
}
}
2020-06-06 22:46:19 +02:00
static void sub_80115EC(s32 a0)
2020-02-16 19:49:36 +01:00
{
s32 i;
2020-06-06 22:46:19 +02:00
for (i = 0; i < RFU_CHILD_MAX; i++)
2020-02-16 19:49:36 +01:00
{
if ((a0 >> i) & 1)
{
Rfu.unk_cea[i] = 0;
Rfu.unk_cee[i] = 0xFF;
}
}
}
2020-06-06 22:46:19 +02:00
static u8 GetNewChildrenInUnionRoomChat(s32 a0)
2020-02-16 19:49:36 +01:00
{
u8 ret = 0;
u8 i;
2020-06-07 23:37:09 +02:00
for (i = 0; i < RFU_CHILD_MAX; i++)
2020-02-16 19:49:36 +01:00
{
if ((a0 >> i) & 1)
{
struct GFtgtGname *structPtr = (void *)gRfuLinkStatus->partner[i].gname;
2020-06-06 22:46:19 +02:00
if (structPtr->activity == (ACTIVITY_CHAT | IN_UNION_ROOM))
2020-02-16 19:49:36 +01:00
ret |= (1 << i);
}
}
return ret;
}
2020-06-06 22:46:19 +02:00
static void sub_8011674(u8 msg, u8 paramCount)
2020-02-16 19:49:36 +01:00
{
u8 r1;
2020-06-06 22:46:19 +02:00
switch (msg)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
case LMAN_MSG_INITIALIZE_COMPLETED:
2021-07-12 02:42:05 +02:00
Rfu.state = RFUSTATE_17;
2020-02-16 19:49:36 +01:00
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_NEW_CHILD_CONNECT_DETECTED:
2020-06-09 00:16:57 +02:00
RfuSetStatus(RFU_STATUS_NEW_CHILD_DETECTED, 0);
2020-02-16 19:49:36 +01:00
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_NEW_CHILD_CONNECT_ACCEPTED:
if (GetHostRFUtgtGname()->activity == (ACTIVITY_CHAT | IN_UNION_ROOM) && !Rfu.stopNewConnections)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
u8 idx = GetNewChildrenInUnionRoomChat(lman.param[0]);
2020-02-16 19:49:36 +01:00
if (idx != 0)
{
r1 = 1 << sub_800E87C(idx);
if (Rfu.unk_ce6 == 0 && !Rfu.unk_ce8)
2020-02-16 19:49:36 +01:00
{
Rfu.unk_ce5 = r1;
Rfu.unk_ce6 |= (r1 ^ idx);
2020-06-06 22:46:19 +02:00
Rfu.unk_ce8 = TRUE;
2020-02-16 19:49:36 +01:00
}
else
{
Rfu.unk_ce6 |= idx;
}
}
if (idx != lman.param[0])
{
Rfu.disconnectSlots |= (idx ^ lman.param[0]);
Rfu.disconnectMode = RFU_DISCONNECT_NORMAL;
2020-02-16 19:49:36 +01:00
}
}
else if (GetHostRFUtgtGname()->activity == (ACTIVITY_PLYRTALK | IN_UNION_ROOM))
2020-02-16 19:49:36 +01:00
{
rfu_REQ_disconnect(lman.acceptSlot_flag);
rfu_waitREQComplete();
}
sub_80115EC(lman.param[0]);
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_NEW_CHILD_CONNECT_REJECTED:
2020-02-16 19:49:36 +01:00
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_SEARCH_CHILD_PERIOD_EXPIRED:
2020-02-16 19:49:36 +01:00
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_END_WAIT_CHILD_NAME:
if (GetHostRFUtgtGname()->activity != (ACTIVITY_CHAT | IN_UNION_ROOM) && lman.acceptCount > 1)
2020-02-16 19:49:36 +01:00
{
r1 = 1 << sub_800E87C(lman.param[0]);
rfu_REQ_disconnect(lman.acceptSlot_flag ^ r1);
rfu_waitREQComplete();
}
2021-07-12 02:42:05 +02:00
if (Rfu.state == RFUSTATE_15)
Rfu.state = RFUSTATE_UR_FINALIZE;
2020-02-16 19:49:36 +01:00
break;
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_PARENT_FOUND:
Rfu.parentId = lman.param[0];
2020-02-16 19:49:36 +01:00
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_SEARCH_PARENT_PERIOD_EXPIRED:
2020-02-16 19:49:36 +01:00
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_CONNECT_PARENT_SUCCESSED:
Rfu.childSlot = lman.param[0];
2020-02-16 19:49:36 +01:00
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_CONNECT_PARENT_FAILED:
2021-07-12 02:42:05 +02:00
Rfu.state = RFUSTATE_18;
if (Rfu.connectParentFailures < 2)
2020-02-16 19:49:36 +01:00
{
Rfu.connectParentFailures++;
2020-02-16 19:49:36 +01:00
CreateTask(sub_801209C, 2);
}
else
{
2020-06-09 00:16:57 +02:00
RfuSetStatus(RFU_STATUS_CONNECTION_ERROR, msg);
2020-02-16 19:49:36 +01:00
}
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_CHILD_NAME_SEND_COMPLETED:
2021-07-12 02:42:05 +02:00
Rfu.state = RFUSTATE_13;
2020-06-09 00:16:57 +02:00
RfuSetStatus(RFU_STATUS_CHILD_SEND_COMPLETE, 0);
rfu_setRecvBuffer(TYPE_UNI, Rfu.childSlot, Rfu.unk_c3f, sizeof(Rfu.unk_c3f));
2020-02-16 19:49:36 +01:00
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_CHILD_NAME_SEND_FAILED_AND_DISCONNECTED:
2020-06-09 00:16:57 +02:00
RfuSetStatus(RFU_STATUS_CONNECTION_ERROR, msg);
2020-02-16 19:49:36 +01:00
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_LINK_LOSS_DETECTED_AND_START_RECOVERY:
2020-02-16 19:49:36 +01:00
if (lman.acceptSlot_flag & lman.param[0])
2020-06-06 22:46:19 +02:00
Rfu.linkLossRecoveryState = 1;
2020-02-16 19:49:36 +01:00
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_LINK_RECOVERY_SUCCESSED:
Rfu.linkLossRecoveryState = 3;
if (gRfuLinkStatus->parentChild == MODE_CHILD)
2020-06-07 23:37:09 +02:00
Rfu.linkRecovered = TRUE;
2020-02-16 19:49:36 +01:00
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_LINK_LOSS_DETECTED_AND_DISCONNECTED:
Rfu.linkLossRecoveryState = 2;
case LMAN_MSG_LINK_RECOVERY_FAILED_AND_DISCONNECTED:
if (Rfu.linkLossRecoveryState != 2)
Rfu.linkLossRecoveryState = 4;
if (Rfu.parentChild == MODE_PARENT)
2020-02-16 19:49:36 +01:00
{
if (gReceivedRemoteLinkPlayers == 1)
{
Rfu.unk_ce2 &= ~(lman.param[0]);
if (Rfu.unk_ce2 == 0)
2020-06-06 22:46:19 +02:00
GetLinkmanErrorParams(msg);
2020-02-16 19:49:36 +01:00
else
StartDisconnectNewChild();
2020-02-16 19:49:36 +01:00
}
}
else if (Rfu.disconnectMode != RFU_DISCONNECT_NORMAL && gReceivedRemoteLinkPlayers == 1)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
GetLinkmanErrorParams(msg);
2020-02-16 19:49:36 +01:00
rfu_LMAN_stopManager(0);
}
2021-07-12 02:42:05 +02:00
if (gRfuLinkStatus->parentChild == MODE_NEUTRAL
&& lman.pcswitch_flag == 0
&& FuncIsActiveTask(Task_LinkRfu_UnionRoomListen) == TRUE)
Rfu.state = RFUSTATE_17;
2020-02-16 19:49:36 +01:00
2020-06-09 00:16:57 +02:00
RfuSetStatus(RFU_STATUS_CONNECTION_ERROR, msg);
2020-02-16 19:49:36 +01:00
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_LINK_DISCONNECTED_BY_USER:
Rfu.disconnectSlots = 0;
2020-02-16 19:49:36 +01:00
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_RFU_POWER_DOWN:
case LMAN_MSG_MANAGER_STOPPED:
case LMAN_MSG_MANAGER_FORCED_STOPPED_AND_RFU_RESET:
2020-02-16 19:49:36 +01:00
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_LMAN_API_ERROR_RETURN:
2020-06-09 00:16:57 +02:00
RfuSetStatus(RFU_STATUS_FATAL_ERROR, msg);
2020-06-06 22:46:19 +02:00
GetLinkmanErrorParams(msg);
Rfu.isShuttingDown = TRUE;
2020-02-16 19:49:36 +01:00
break;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_REQ_API_ERROR:
case LMAN_MSG_WATCH_DOG_TIMER_ERROR:
case LMAN_MSG_CLOCK_SLAVE_MS_CHANGE_ERROR_BY_DMA:
case LMAN_MSG_RFU_FATAL_ERROR:
GetLinkmanErrorParams(msg);
2020-06-09 00:16:57 +02:00
RfuSetStatus(RFU_STATUS_FATAL_ERROR, msg);
Rfu.unk_cdb = FALSE;
2020-02-16 19:49:36 +01:00
break;
}
}
void RfuSetNormalDisconnectMode(void)
2020-02-16 19:49:36 +01:00
{
Rfu.disconnectMode = RFU_DISCONNECT_NORMAL;
2020-02-16 19:49:36 +01:00
}
2020-06-09 00:16:57 +02:00
void RfuSetStatus(u8 status, u16 msg)
2020-02-16 19:49:36 +01:00
{
2020-06-09 00:16:57 +02:00
Rfu.status = status;
2020-06-06 22:46:19 +02:00
Rfu.linkmanMsg = msg;
2020-02-16 19:49:36 +01:00
}
2020-06-09 00:16:57 +02:00
u8 RfuGetStatus(void)
2020-02-16 19:49:36 +01:00
{
2020-06-09 00:16:57 +02:00
return Rfu.status;
2020-02-16 19:49:36 +01:00
}
2020-06-09 00:16:57 +02:00
bool32 RfuHasErrored(void)
2020-02-16 19:49:36 +01:00
{
2020-06-09 00:16:57 +02:00
// RFU_STATUS_OK will underflow here intentionally
u32 var = RfuGetStatus() - 1;
if (var < RFU_STATUS_CONNECTION_ERROR)
2020-02-16 19:49:36 +01:00
return TRUE;
else
return FALSE;
}
bool32 sub_8011A9C(void)
{
return Rfu.unk_ce8;
}
bool8 Rfu_IsMaster(void)
{
2020-06-06 22:46:19 +02:00
return Rfu.parentChild;
2020-02-16 19:49:36 +01:00
}
void RfuVSync(void)
{
rfu_LMAN_syncVBlank();
}
2021-03-01 07:54:51 +01:00
void ClearRecvCommands(void)
2020-02-16 19:49:36 +01:00
{
CpuFill32(0, gRecvCmds, sizeof(gRecvCmds));
}
2020-06-06 22:46:19 +02:00
static void sub_8011AE8(void)
2020-02-16 19:49:36 +01:00
{
LoadOam();
ProcessSpriteCopyRequests();
TransferPlttBuffer();
}
2020-06-06 22:46:19 +02:00
static void sub_8011AFC(void)
2020-02-16 19:49:36 +01:00
{
s32 i;
ResetSpriteData();
FreeAllSpritePalettes();
ResetTasks();
ResetPaletteFade();
SetVBlankCallback(sub_8011AE8);
if (IsWirelessAdapterConnected())
{
2020-06-04 00:00:53 +02:00
gLinkType = LINKTYPE_TRADE;
2020-05-30 10:09:21 +02:00
SetWirelessCommType1();
2020-02-16 19:49:36 +01:00
OpenLink();
SeedRng(gMain.vblankCounter2);
for (i = 0; i < 4; i++)
gSaveBlock2Ptr->playerTrainerId[i] = Random() % 256;
SetGpuReg(REG_OFFSET_DISPCNT, DISPCNT_OBJ_ON | DISPCNT_BG0_ON | DISPCNT_BG2_ON | DISPCNT_OBJ_1D_MAP);
RunTasks();
AnimateSprites();
BuildOamBuffer();
UpdatePaletteFade();
2020-06-06 22:46:19 +02:00
CreateTask_RfuIdle();
2020-02-16 19:49:36 +01:00
SetMainCallback2(sub_8011BF8);
}
}
2020-05-30 10:09:21 +02:00
bool32 IsUnionRoomListenTaskActive(void)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
return FuncIsActiveTask(Task_LinkRfu_UnionRoomListen);
2020-02-16 19:49:36 +01:00
}
2020-06-06 22:46:19 +02:00
void CreateTask_RfuIdle(void)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
if (!FuncIsActiveTask(Task_Idle))
Rfu.idleTaskId = CreateTask(Task_Idle, 0);
2020-02-16 19:49:36 +01:00
}
2020-06-06 22:46:19 +02:00
void DestroyTask_RfuIdle(void)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
if (FuncIsActiveTask(Task_Idle) == TRUE)
DestroyTask(Rfu.idleTaskId);
2020-02-16 19:49:36 +01:00
}
2020-06-06 22:46:19 +02:00
static void sub_8011BF8(void)
2020-02-16 19:49:36 +01:00
{
RunTasks();
AnimateSprites();
BuildOamBuffer();
UpdatePaletteFade();
}
2020-05-30 10:09:21 +02:00
void InitializeRfuLinkManager_LinkLeader(u32 a0)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
Rfu.parentChild = MODE_PARENT;
CopyPlayerNameToUnameBuffer();
2020-02-16 19:49:36 +01:00
rfu_LMAN_initializeManager(sub_801120C, NULL);
2020-06-06 22:46:19 +02:00
sRfuReqConfig = sRfuReqConfigTemplate;
sRfuReqConfig.availSlot_flag = sAvailSlots[a0 - 1];
CreateTask_LinkLeaderSearchForChildren();
2020-02-16 19:49:36 +01:00
}
2020-05-30 10:09:21 +02:00
void InitializeRfuLinkManager_JoinGroup(void)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
Rfu.parentChild = MODE_CHILD;
CopyPlayerNameToUnameBuffer();
2020-02-16 19:49:36 +01:00
rfu_LMAN_initializeManager(sub_8011404, sub_800ED34);
2020-06-06 22:46:19 +02:00
CreateTask_JoinGroupSearchForParent();
2020-02-16 19:49:36 +01:00
}
2020-05-30 10:09:21 +02:00
void InitializeRfuLinkManager_EnterUnionRoom(void)
2020-02-16 19:49:36 +01:00
{
2021-07-12 02:42:05 +02:00
Rfu.parentChild = MODE_P_C_SWITCH;
2020-06-06 22:46:19 +02:00
CopyPlayerNameToUnameBuffer();
2020-02-16 19:49:36 +01:00
rfu_LMAN_initializeManager(sub_8011674, NULL);
2020-06-06 22:46:19 +02:00
sRfuReqConfig = sRfuReqConfigTemplate;
sRfuReqConfig.linkRecovery_enable = 0;
sRfuReqConfig.linkRecovery_period = 600;
Rfu.searchTaskId = CreateTask(Task_LinkRfu_UnionRoomListen, 1);
2020-02-16 19:49:36 +01:00
}
2020-06-06 22:46:19 +02:00
static u16 ReadU16(const void *ptr)
2020-02-16 19:49:36 +01:00
{
const u8 *ptr_ = ptr;
return (ptr_[1] << 8) | (ptr_[0]);
}
2020-06-06 22:46:19 +02:00
static u8 GetPartnerIndexByNameAndTrainerID(const u8 *name, u16 id)
2020-02-16 19:49:36 +01:00
{
u8 i;
2020-06-06 22:46:19 +02:00
u8 idx = 0xFF;
2020-02-16 19:49:36 +01:00
2020-06-06 22:46:19 +02:00
for (i = 0; i < RFU_CHILD_MAX; i++)
2020-02-16 19:49:36 +01:00
{
u16 trainerId = ReadU16(gRfuLinkStatus->partner[i].gname + 2);
2020-06-07 23:37:09 +02:00
if (IsRfuSerialNumberValid(gRfuLinkStatus->partner[i].serialNo)
2020-06-06 22:46:19 +02:00
&& !StringCompare(name, gRfuLinkStatus->partner[i].uname)
&& id == trainerId)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
idx = i;
2020-02-16 19:49:36 +01:00
if (gRfuLinkStatus->partner[i].slot != 0xFF)
break;
}
}
2020-06-06 22:46:19 +02:00
return idx;
2020-02-16 19:49:36 +01:00
}
2020-06-06 22:46:19 +02:00
static void RfuReqDisconnectSlot(u32 slot)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
rfu_REQ_disconnect(slot);
2020-02-16 19:49:36 +01:00
rfu_waitREQComplete();
2020-06-06 22:46:19 +02:00
Rfu.unk_ce2 &= ~(slot);
2020-02-16 19:49:36 +01:00
rfu_clearSlot(1, Rfu.unk_cda);
2020-06-06 22:46:19 +02:00
rfu_UNI_setSendData(Rfu.unk_ce2, Rfu.recvCmds, 70);
2020-02-16 19:49:36 +01:00
Rfu.unk_cda = sub_800E87C(Rfu.unk_ce2);
}
2020-06-06 22:46:19 +02:00
void RequestDisconnectSlotByTrainerNameAndId(const u8 *name, u16 id)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
u8 var = GetPartnerIndexByNameAndTrainerID(name, id);
2020-02-16 19:49:36 +01:00
if (var != 0xFF)
2020-06-06 22:46:19 +02:00
RfuReqDisconnectSlot(1 << var);
2020-02-16 19:49:36 +01:00
}
void Rfu_DisconnectPlayerById(u32 playerIdx)
2020-02-16 19:49:36 +01:00
{
if (playerIdx != 0)
2020-02-16 19:49:36 +01:00
{
s32 i;
u8 toDisconnect = 0;
2020-02-16 19:49:36 +01:00
2020-06-06 22:46:19 +02:00
for (i = 0; i < RFU_CHILD_MAX; i++)
2020-02-16 19:49:36 +01:00
{
if (Rfu.linkPlayerIdx[i] == playerIdx && (Rfu.unk_ce2 >> i) & 1)
toDisconnect |= 1 << i;
2020-02-16 19:49:36 +01:00
}
if (toDisconnect)
SendDisconnectCommand(toDisconnect, RFU_DISCONNECT_NORMAL);
2020-02-16 19:49:36 +01:00
}
}
#define tDisconnectPlayers data[0]
#define tDisconnectMode data[1]
static void Task_SendDisconnectCommand(u8 taskId)
2020-02-16 19:49:36 +01:00
{
if (gSendCmd[0] == 0 && !Rfu.unk_ce8)
2020-02-16 19:49:36 +01:00
{
RfuPrepareSendBuffer(RFUCMD_DISCONNECT);
gSendCmd[1] = gTasks[taskId].tDisconnectPlayers;
gSendCmd[2] = gTasks[taskId].tDisconnectMode;
Rfu.playerCount -= sPlayerBitsToCount[gTasks[taskId].tDisconnectPlayers];
2020-02-16 19:49:36 +01:00
gSendCmd[3] = Rfu.playerCount;
DestroyTask(taskId);
}
}
static void SendDisconnectCommand(u32 playersToDisconnect, u32 disconnectMode)
2020-02-16 19:49:36 +01:00
{
u8 taskId = FindTaskIdByFunc(Task_SendDisconnectCommand);
2021-02-20 05:22:26 +01:00
if (taskId == TASK_NONE)
2020-02-16 19:49:36 +01:00
{
taskId = CreateTask(Task_SendDisconnectCommand, 5);
gTasks[taskId].tDisconnectPlayers = playersToDisconnect;
2020-02-16 19:49:36 +01:00
}
else
{
// Task is already active, just add the new players to disconnect
gTasks[taskId].tDisconnectPlayers |= playersToDisconnect;
2020-02-16 19:49:36 +01:00
}
gTasks[taskId].tDisconnectMode = disconnectMode;
2020-02-16 19:49:36 +01:00
}
#undef tDisconnectMode
#define tTime data[15]
2020-06-06 22:46:19 +02:00
static void Task_RfuReconnectWithParent(u8 taskId)
2020-02-16 19:49:36 +01:00
{
s16 *data = gTasks[taskId].data;
if (CanTryReconnectParent())
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
u8 id = GetPartnerIndexByNameAndTrainerID((u8*)data, ReadU16(&data[8]));
2020-02-16 19:49:36 +01:00
if (id != 0xFF)
{
if (gRfuLinkStatus->partner[id].slot != 0xFF)
{
Rfu.unk_c3d = id;
if (TryReconnectParent())
2020-02-16 19:49:36 +01:00
DestroyTask(taskId);
}
2020-06-06 22:46:19 +02:00
else if (GetHostRFUtgtGname()->activity == ACTIVITY_WONDER_CARD2
|| GetHostRFUtgtGname()->activity == ACTIVITY_WONDER_NEWS2)
2020-02-16 19:49:36 +01:00
{
tTime++;
2020-02-16 19:49:36 +01:00
}
else
{
// Error, unable to reconnect to parent
2020-06-09 00:16:57 +02:00
RfuSetStatus(RFU_STATUS_CONNECTION_ERROR, 0x7000);
2020-02-16 19:49:36 +01:00
DestroyTask(taskId);
}
}
else
{
tTime++;
2020-02-16 19:49:36 +01:00
Rfu.unk_c3d = id;
}
}
else
{
tTime++;
2020-02-16 19:49:36 +01:00
}
if (tTime > 240)
2020-02-16 19:49:36 +01:00
{
// Timeout error
2020-06-09 00:16:57 +02:00
RfuSetStatus(RFU_STATUS_CONNECTION_ERROR, 0x7000);
2020-02-16 19:49:36 +01:00
DestroyTask(taskId);
}
}
#undef tTime
2020-06-06 22:46:19 +02:00
void CreateTask_RfuReconnectWithParent(const u8 *name, u16 trainerId)
2020-02-16 19:49:36 +01:00
{
u8 taskId;
s16 *data;
2020-06-09 00:16:57 +02:00
Rfu.status = RFU_STATUS_OK;
2020-06-06 22:46:19 +02:00
taskId = CreateTask(Task_RfuReconnectWithParent, 3);
2020-02-16 19:49:36 +01:00
data = gTasks[taskId].data;
2020-06-06 22:46:19 +02:00
StringCopy((u8*)(data), name);
2020-02-16 19:49:36 +01:00
data[8] = trainerId;
}
static bool32 IsPartnerActivityIncompatible(s16 activity, struct GFtgtGname *partnerGname)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
if (GetHostRFUtgtGname()->activity == (ACTIVITY_CHAT | IN_UNION_ROOM))
2020-02-16 19:49:36 +01:00
{
// Host trying to chat, but partner isn't
2020-06-06 22:46:19 +02:00
if (partnerGname->activity != (ACTIVITY_CHAT | IN_UNION_ROOM))
2020-02-16 19:49:36 +01:00
return TRUE;
}
2020-06-06 22:46:19 +02:00
else if (partnerGname->activity != IN_UNION_ROOM)
2020-02-16 19:49:36 +01:00
{
// Partner not in union room
2020-02-16 19:49:36 +01:00
return TRUE;
}
2020-06-06 22:46:19 +02:00
else if (activity == (ACTIVITY_TRADE | IN_UNION_ROOM))
2020-02-16 19:49:36 +01:00
{
// Trying to trade, make sure trade matches request
2020-06-06 22:46:19 +02:00
struct GFtgtGname *tradeGname = &Rfu.unk_10A;
if (tradeGname->species == SPECIES_EGG)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
if (partnerGname->species == tradeGname->species)
2020-02-16 19:49:36 +01:00
return FALSE;
else
return TRUE;
}
2020-06-06 22:46:19 +02:00
else if (partnerGname->species != tradeGname->species
|| partnerGname->level != tradeGname->level
|| partnerGname->type != tradeGname->type)
2020-02-16 19:49:36 +01:00
{
return TRUE;
}
}
return FALSE;
}
#define tTime data[0]
#define tActivity data[1]
2020-06-06 22:46:19 +02:00
static void sub_801209C(u8 taskId)
2020-02-16 19:49:36 +01:00
{
2020-06-09 00:16:57 +02:00
if (Rfu.status == RFU_STATUS_NEW_CHILD_DETECTED)
2020-02-16 19:49:36 +01:00
DestroyTask(taskId);
if (++gTasks[taskId].tTime > 300)
2020-02-16 19:49:36 +01:00
{
// Timeout error
2020-06-09 00:16:57 +02:00
RfuSetStatus(RFU_STATUS_CONNECTION_ERROR, 0x7000);
2020-02-16 19:49:36 +01:00
DestroyTask(taskId);
}
if (Rfu.parentId != 0 && lman.parent_child == MODE_CHILD)
2020-02-16 19:49:36 +01:00
{
u16 trainerId = ReadU16(Rfu.unk_10A.unk_00.playerTrainerId);
2020-06-06 22:46:19 +02:00
u8 id = GetPartnerIndexByNameAndTrainerID(Rfu.playerName, trainerId);
2020-02-16 19:49:36 +01:00
if (id != 0xFF)
{
if (!IsPartnerActivityIncompatible(gTasks[taskId].tActivity, (void *)gRfuLinkStatus->partner[id].gname))
2020-02-16 19:49:36 +01:00
{
if (gRfuLinkStatus->partner[id].slot != 0xFF && !rfu_LMAN_CHILD_connectParent(gRfuLinkStatus->partner[id].id, 90))
2020-02-16 19:49:36 +01:00
{
2021-07-12 02:42:05 +02:00
Rfu.state = RFUSTATE_10;
2020-02-16 19:49:36 +01:00
DestroyTask(taskId);
}
}
else
{
// Incompatible partner activity
2020-06-09 00:16:57 +02:00
RfuSetStatus(RFU_STATUS_CONNECTION_ERROR, 0x7000);
2020-02-16 19:49:36 +01:00
DestroyTask(taskId);
}
}
}
}
void sub_8012188(const u8 *name, struct GFtgtGname *gname, u8 activity)
2020-02-16 19:49:36 +01:00
{
u8 taskId, taskId2;
Rfu.connectParentFailures = 0;
2020-06-09 00:16:57 +02:00
Rfu.status = RFU_STATUS_OK;
2020-02-16 19:49:36 +01:00
StringCopy(Rfu.playerName, name);
memcpy(&Rfu.unk_10A, gname, RFU_GAME_NAME_LENGTH);
2020-02-16 19:49:36 +01:00
rfu_LMAN_forceChangeSP();
taskId = CreateTask(sub_801209C, 2);
gTasks[taskId].tActivity = activity;
2020-06-06 22:46:19 +02:00
taskId2 = FindTaskIdByFunc(Task_LinkRfu_UnionRoomListen);
if (activity == (ACTIVITY_CHAT | IN_UNION_ROOM))
2020-02-16 19:49:36 +01:00
{
2021-02-20 05:22:26 +01:00
if (taskId2 != TASK_NONE)
gTasks[taskId2].tData7 = TRUE;
2020-02-16 19:49:36 +01:00
}
else
{
2021-02-20 05:22:26 +01:00
if (taskId2 != TASK_NONE)
gTasks[taskId2].tData7 = FALSE;
2020-02-16 19:49:36 +01:00
}
}
2020-06-07 23:37:09 +02:00
bool8 IsRfuRecoveringFromLinkLoss(void)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
if (Rfu.linkLossRecoveryState == 1)
2020-02-16 19:49:36 +01:00
return TRUE;
else
return FALSE;
}
2021-07-12 02:42:05 +02:00
bool32 IsRfuCommunicatingWithAllChildren(void)
2020-02-16 19:49:36 +01:00
{
s32 i;
2020-06-06 22:46:19 +02:00
for (i = 0; i < RFU_CHILD_MAX; i++)
2020-02-16 19:49:36 +01:00
{
2021-07-12 02:42:05 +02:00
// RFU_STATUS_OK is the default status.
// If any connected child is receiving a status other
// than OK, then the parent is communicating with them
2020-06-09 00:16:57 +02:00
if ((lman.acceptSlot_flag >> i) & 1 && Rfu.partnerSendStatuses[i] == RFU_STATUS_OK)
2020-02-16 19:49:36 +01:00
return FALSE;
}
return TRUE;
}
2020-06-06 22:46:19 +02:00
static void Debug_PrintEmpty(void)
2020-02-16 19:49:36 +01:00
{
s32 i;
for (i = 0; i < 20; i++)
2021-07-12 02:42:05 +02:00
Debug_PrintString(sASCII_30Spaces, 0, i);
2020-02-16 19:49:36 +01:00
}
2020-06-06 22:46:19 +02:00
static void Debug_PrintStatus(void)
2020-02-16 19:49:36 +01:00
{
s32 i, j;
Debug_PrintNum(GetBlockReceivedStatus(), 28, 19, 2);
Debug_PrintNum(gRfuLinkStatus->connSlotFlag, 20, 1, 1);
Debug_PrintNum(gRfuLinkStatus->linkLossSlotFlag, 23, 1, 1);
2020-06-06 22:46:19 +02:00
if (Rfu.parentChild == MODE_PARENT)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
for (i = 0; i < RFU_CHILD_MAX; i++)
2020-02-16 19:49:36 +01:00
{
if ((gRfuLinkStatus->getNameFlag >> i) & 1)
{
2020-06-06 22:46:19 +02:00
Debug_PrintNum(gRfuLinkStatus->partner[i].serialNo, 1, i + 3, 4);
Debug_PrintString((void*)gRfuLinkStatus->partner[i].gname, 6, i + 3);
Debug_PrintString(gRfuLinkStatus->partner[i].uname, 22, i + 3);
2020-02-16 19:49:36 +01:00
}
}
2020-06-06 22:46:19 +02:00
for (i = 0; i < RFU_CHILD_MAX; i++)
2020-02-16 19:49:36 +01:00
{
for (j = 0; j < CHILD_DATA_LENGTH; j++)
Debug_PrintNum(Rfu.childRecvBuffer[i][j], j * 2, i + 11, 2);
2020-02-16 19:49:36 +01:00
}
Debug_PrintString(sASCII_NowSlot, 1, 15);
2020-02-16 19:49:36 +01:00
}
else if (gRfuLinkStatus->connSlotFlag != 0 && gRfuLinkStatus->getNameFlag != 0)
{
2020-06-06 22:46:19 +02:00
for (i = 0; i < RFU_CHILD_MAX; i++)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
Debug_PrintNum(0, 1, i + 3, 4);
2021-07-12 02:42:05 +02:00
Debug_PrintString(sASCII_15Spaces, 6, i + 3);
Debug_PrintString(sASCII_8Spaces, 22, i + 3);
2020-02-16 19:49:36 +01:00
}
2020-06-06 22:46:19 +02:00
Debug_PrintNum(gRfuLinkStatus->partner[Rfu.childSlot].serialNo, 1, 3, 4);
Debug_PrintString((void*)gRfuLinkStatus->partner[Rfu.childSlot].gname, 6, 3);
Debug_PrintString(gRfuLinkStatus->partner[Rfu.childSlot].uname, 22, 3);
2020-02-16 19:49:36 +01:00
}
else
{
for (i = 0; i < gRfuLinkStatus->findParentCount; i++)
{
if (gRfuLinkStatus->partner[i].slot != 0xFF)
{
2020-06-06 22:46:19 +02:00
Debug_PrintNum(gRfuLinkStatus->partner[i].serialNo, 1, i + 3, 4);
Debug_PrintNum(gRfuLinkStatus->partner[i].id, 6, i + 3, 4);
Debug_PrintString(gRfuLinkStatus->partner[i].uname, 22, i + 3);
2020-02-16 19:49:36 +01:00
}
}
2020-06-06 22:46:19 +02:00
for (; i < RFU_CHILD_MAX; i++)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
Debug_PrintNum(0, 1, i + 3, 4);
2021-07-12 02:42:05 +02:00
Debug_PrintString(sASCII_15Spaces, 6, i + 3);
Debug_PrintString(sASCII_8Spaces, 22, i + 3);
2020-02-16 19:49:36 +01:00
}
}
}
2020-06-06 22:46:19 +02:00
static u32 GetRfuSendQueueLength(void)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
return Rfu.sendQueue.count;
2020-02-16 19:49:36 +01:00
}
u32 GetRfuRecvQueueLength(void)
{
2020-06-06 22:46:19 +02:00
return Rfu.recvQueue.count;
2020-02-16 19:49:36 +01:00
}
2020-02-16 20:34:58 +01:00
2020-06-06 22:46:19 +02:00
static void Task_Idle(u8 taskId)
2020-02-16 20:34:58 +01:00
{
}