pokeemerald/src/link_rfu_2.c

3006 lines
85 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_menu.h"
2020-02-16 19:49:36 +01:00
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_UNUSED,
2021-07-12 02:42:05 +02:00
RFUSTATE_RECONNECTED,
RFUSTATE_CONNECTED,
2021-07-12 02:42:05 +02:00
RFUSTATE_CHILD_TRY_JOIN,
RFUSTATE_CHILD_JOINED,
RFUSTATE_UR_PLAYER_EXCHANGE,
2021-10-07 20:55:15 +02:00
RFUSTATE_UR_STOP_MANAGER,
RFUSTATE_UR_STOP_MANAGER_END,
2021-07-12 02:42:05 +02:00
RFUSTATE_UR_FINALIZE,
};
2021-10-07 20:55:15 +02:00
// These states are used for different purposes
// depending on the link mode (parent, child, union room)
2021-07-12 02:42:05 +02:00
#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
// States for the 'receiving' field of RfuBlockSend
enum {
RECV_STATE_READY,
RECV_STATE_RECEIVING,
RECV_STATE_FINISHED,
};
2020-06-06 22:46:19 +02:00
struct SioInfo
{
char magic[sizeof("PokemonSioInfo")];
2020-06-06 22:46:19 +02:00
u8 playerCount;
u8 linkPlayerIdx[RFU_CHILD_MAX];
struct LinkPlayer linkPlayers[MAX_RFU_PLAYERS];
u8 filler[92];
};
2020-02-16 19:49:36 +01:00
2022-09-11 20:14:49 +02:00
// Struct is mostly empty, presumably because usage of
// its fields was largely removed before release
struct RfuDebug
{
2021-10-07 20:55:15 +02:00
u8 unused0[6];
u16 recvCount;
u8 unused1[6];
vu8 unkFlag;
u8 childJoinCount;
2021-10-07 20:55:15 +02:00
u8 unused2[84];
u16 blockSendFailures;
2021-10-07 20:55:15 +02:00
u8 unused3[29];
u8 blockSendTime;
2021-10-07 20:55:15 +02:00
u8 unused4[88];
};
u32 gRfuAPIBuffer[RFU_API_BUFF_SIZE_RAM / 4];
struct RfuManager gRfu;
2020-02-16 19:49:36 +01:00
static u8 sHeldKeyCount;
static u8 sResendBlock8[CMD_LENGTH * 2];
static u16 sResendBlock16[CMD_LENGTH];
2020-02-16 19:49:36 +01:00
EWRAM_DATA struct RfuGameData gHostRfuGameData = {};
EWRAM_DATA u8 gHostRfuUsername[RFU_USER_NAME_LENGTH] = {};
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);
2021-10-07 20:55:15 +02:00
static void InitParentSendData(void);
static void MSCCallback_Child(u16);
static void MSCCallback_Parent(u16);
2020-06-07 23:37:09 +02:00
static void UpdateBackupQueue(void);
static void Task_PlayerExchange(u8);
static void Task_PlayerExchangeUpdate(u8);
static void Task_PlayerExchangeChat(u8);
2020-06-06 22:46:19 +02:00
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);
static s32 GetJoinGroupStatus(void);
2020-06-07 23:37:09 +02:00
static void ClearSelectedLinkPlayerIds(u16);
2020-06-06 22:46:19 +02:00
static void ValidateAndReceivePokemonSioInfo(void *);
2021-10-07 20:55:15 +02:00
static void ParentResetChildRecvMetadata(s32);
static void CB2_RfuIdle(void);
2020-06-06 22:46:19 +02:00
static void RfuReqDisconnectSlot(u32);
static void SendDisconnectCommand(u32, u32);
static void Task_TryConnectToUnionRoomParent(u8);
2020-06-06 22:46:19 +02:00
static void Debug_PrintEmpty(void);
static void Task_Idle(u8);
static const INIT_PARAM sRfuReqConfigTemplate = {
.maxMFrame = 4,
2020-06-06 22:46:19 +02:00
.MC_TimerCount = 32,
.availSlot_flag = 0,
.mboot_flag = 0,
.serialNo = RFU_SERIAL_GAME,
.gameName = (void *)&gHostRfuGameData,
.userName = gHostRfuUsername,
2020-06-06 22:46:19 +02:00
.fastSearchParent_flag = TRUE,
.linkRecovery_enable = FALSE,
.linkRecovery_period = 600,
.NI_failCounter_limit = 300
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
#define BLOCK_MASK(bitNum)((1 << (bitNum)) - 1)
2020-06-06 22:46:19 +02:00
static const u32 sAllBlocksReceived[] = {
BLOCK_MASK(0),
BLOCK_MASK(1),
BLOCK_MASK(2),
BLOCK_MASK(3),
BLOCK_MASK(4),
BLOCK_MASK(5),
BLOCK_MASK(6),
BLOCK_MASK(7),
BLOCK_MASK(8),
BLOCK_MASK(9),
BLOCK_MASK(10),
BLOCK_MASK(11),
BLOCK_MASK(12),
BLOCK_MASK(13),
BLOCK_MASK(14),
BLOCK_MASK(15),
BLOCK_MASK(16),
BLOCK_MASK(17),
BLOCK_MASK(18),
BLOCK_MASK(19),
BLOCK_MASK(20),
BLOCK_MASK(21),
BLOCK_MASK(22),
BLOCK_MASK(23),
BLOCK_MASK(24),
2020-02-16 19:49:36 +01:00
};
#undef BLOCK_MASK
2020-06-06 22:46:19 +02:00
2021-10-07 20:55:15 +02:00
static const u8 sSlotToLinkPlayerTableId[] = {
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
// If the 4 bits representing child slots were an array, this table
// would return the index of the most recently set bit
static const u8 sPlayerBitsToNewChildIdx[1 << (MAX_RFU_PLAYERS - 1)] = {
0, // 0000
0, // 0001
1, // 0010
0, // 0011
2, // 0100
0, // 0101
1, // 0110
0, // 0111
3, // 1000
0, // 1001
1, // 1010
0, // 1011
2, // 1100
0, // 1101
1, // 1110
0 // 1111
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_GAME,
2021-10-07 22:10:35 +02:00
RFU_SERIAL_WONDER_DISTRIBUTOR,
RFU_SERIAL_UNKNOWN,
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[] = {
Task_PlayerExchange,
Task_PlayerExchangeUpdate,
Task_PlayerExchangeChat
2020-02-16 19:49:36 +01:00
};
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;
u8 errorState = gRfu.errorState;
CpuFill16(0, &gRfu, sizeof(gRfu));
gRfu.errorState = errorState;
gRfu.parentChild = 0xFF;
if (gRfu.errorState != RFU_ERROR_STATE_IGNORE)
gRfu.errorState = RFU_ERROR_STATE_NONE;
for (i = 0; i < MAX_RFU_PLAYERS; i++)
ResetSendDataManager(&gRfu.recvBlock[i]);
ResetSendDataManager(&gRfu.sendBlock);
RfuRecvQueue_Reset(&gRfu.recvQueue);
RfuSendQueue_Reset(&gRfu.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 *)gRfuAPIBuffer, sizeof(gRfuAPIBuffer), &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
}
}
static void Task_ParentSearchForChildren(u8 taskId)
2020-02-16 19:49:36 +01:00
{
2020-06-09 00:16:57 +02:00
UpdateChildStatuses();
switch (gRfu.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);
gRfu.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:
rfu_LMAN_establishConnection(gRfu.parentChild, 0, 240, (u16 *)sAcceptedSerialNos);
gRfu.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);
gRfu.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:
2021-10-07 20:55:15 +02:00
gRfu.parentFinished = FALSE;
rfu_LMAN_setMSCCallback(MSCCallback_Parent);
InitChildRecvBuffers();
2021-10-07 20:55:15 +02:00
InitParentSendData();
gRfu.state = RFUSTATE_FINALIZED;
2020-06-06 22:46:19 +02:00
gTasks[taskId].data[1] = 8;
CreateTask(Task_PlayerExchange, 5);
2020-06-06 22:46:19 +02:00
DestroyTask(taskId);
break;
2020-02-16 19:49:36 +01:00
}
}
s32 Rfu_GetIndexOfNewestChild(u8 bits)
2020-02-16 19:49:36 +01:00
{
return sPlayerBitsToNewChildIdx[bits];
2020-02-16 19:49:36 +01:00
}
2021-10-07 20:55:15 +02:00
static void SetLinkPlayerIdsFromSlots(s32 baseSlots, s32 addSlots)
2020-02-16 19:49:36 +01:00
{
u8 i;
2021-10-07 20:55:15 +02:00
u8 baseId = 1;
s32 baseSlotsCopy = baseSlots;
s32 newId = 0;
if (addSlots == -1)
2020-02-16 19:49:36 +01:00
{
2021-10-07 20:55:15 +02:00
// Initialize
for (i = 0; i < RFU_CHILD_MAX; baseSlots >>= 1, i++)
2020-02-16 19:49:36 +01:00
{
2021-10-07 20:55:15 +02:00
if (baseSlots & 1)
2020-02-16 19:49:36 +01:00
{
2021-10-07 20:55:15 +02:00
gRfu.linkPlayerIdx[i] = baseId;
baseId++;
2020-02-16 19:49:36 +01:00
}
}
}
else
{
2021-10-07 20:55:15 +02:00
// Clear id for any empty slot
for (i = 0; i < RFU_CHILD_MAX; baseSlotsCopy >>= 1, i++)
2020-02-16 19:49:36 +01:00
{
2021-10-07 20:55:15 +02:00
if (!(baseSlotsCopy & 1))
gRfu.linkPlayerIdx[i] = 0;
2020-02-16 19:49:36 +01:00
}
2021-10-07 20:55:15 +02:00
// Get starting id by checking existing slots
for (baseId = RFU_CHILD_MAX; baseId != 0; baseId--)
2020-02-16 19:49:36 +01:00
{
2021-10-07 20:55:15 +02:00
for (i = 0; i < RFU_CHILD_MAX && gRfu.linkPlayerIdx[i] != baseId; i++)
;
2020-06-06 22:46:19 +02:00
if (i == RFU_CHILD_MAX)
2021-10-07 20:55:15 +02:00
newId = baseId;
2020-02-16 19:49:36 +01:00
}
2021-10-07 20:55:15 +02:00
// Set id for new slots
for (addSlots &= ~baseSlots, i = 0; i < RFU_CHILD_MAX; addSlots >>= 1, i++)
2020-02-16 19:49:36 +01:00
{
2021-10-07 20:55:15 +02:00
if (addSlots & 1)
gRfu.linkPlayerIdx[i] = newId++;
2020-02-16 19:49:36 +01:00
}
}
}
static void Task_ChildSearchForParent(u8 taskId)
2020-02-16 19:49:36 +01:00
{
switch (gRfu.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);
gRfu.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:
rfu_LMAN_establishConnection(gRfu.parentChild, 0, 240, (u16 *)sAcceptedSerialNos);
gRfu.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:
switch (GetJoinGroupStatus())
2020-06-06 22:46:19 +02:00
{
2020-06-09 00:16:57 +02:00
case RFU_STATUS_JOIN_GROUP_OK:
gRfu.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();
gRfu.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 << gRfu.childSlot;
rfu_clearSlot(TYPE_NI_SEND | TYPE_NI_RECV, gRfu.childSlot);
2021-10-07 20:55:15 +02:00
rfu_setRecvBuffer(TYPE_UNI, gRfu.childSlot, gRfu.childRecvQueue, sizeof(gRfu.childRecvQueue));
rfu_UNI_setSendData(bmChildSlot, gRfu.childSendBuffer, sizeof(gRfu.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(Task_PlayerExchange, 5);
2020-06-06 22:46:19 +02:00
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, gRfu.childRecvBuffer[i], sizeof(gRfu.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
}
}
2021-10-07 20:55:15 +02:00
static void InitParentSendData(void)
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
u8 acceptSlot = lman.acceptSlot_flag;
2021-10-07 20:55:15 +02:00
rfu_UNI_setSendData(acceptSlot, gRfu.recvCmds, sizeof(gRfu.recvCmds));
gRfu.parentSendSlot = Rfu_GetIndexOfNewestChild(acceptSlot);
gRfu.parentSlots = acceptSlot;
SetLinkPlayerIdsFromSlots(acceptSlot, -1);
gRfu.parentChild = MODE_PARENT;
2020-02-16 19:49:36 +01:00
}
#define tConnectingForChat data[7]
static void Task_UnionRoomListen(u8 taskId)
2020-02-16 19:49:36 +01:00
{
if (GetHostRfuGameData()->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
}
switch (gRfu.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);
gRfu.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);
2021-10-07 20:55:15 +02:00
rfu_LMAN_setMSCCallback(MSCCallback_Child);
gRfu.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;
case RFUSTATE_UR_PLAYER_EXCHANGE:
if (rfu_UNI_setSendData(1 << gRfu.childSlot, gRfu.childSendBuffer, sizeof(gRfu.childSendBuffer)) == 0)
2020-06-06 22:46:19 +02:00
{
gRfu.parentChild = MODE_CHILD;
2020-06-06 22:46:19 +02:00
DestroyTask(taskId);
if (gTasks[taskId].tConnectingForChat)
CreateTask(Task_PlayerExchangeChat, 1);
2020-06-06 22:46:19 +02:00
else
CreateTask(Task_PlayerExchange, 5);
2020-06-06 22:46:19 +02:00
}
break;
2021-10-07 20:55:15 +02:00
case RFUSTATE_UR_STOP_MANAGER:
rfu_LMAN_stopManager(FALSE);
gRfu.state = RFUSTATE_UR_STOP_MANAGER_END;
2020-06-06 22:46:19 +02:00
break;
2021-10-07 20:55:15 +02:00
case RFUSTATE_UR_STOP_MANAGER_END:
2020-06-06 22:46:19 +02:00
break;
2021-07-12 02:42:05 +02:00
case RFUSTATE_UR_FINALIZE:
2021-10-07 20:55:15 +02:00
gRfu.parentFinished = FALSE;
rfu_LMAN_setMSCCallback(MSCCallback_Parent);
2020-06-06 22:46:19 +02:00
UpdateGameData_GroupLockedIn(TRUE);
InitChildRecvBuffers();
2021-10-07 20:55:15 +02:00
InitParentSendData();
gRfu.state = RFUSTATE_FINALIZED;
2020-06-06 22:46:19 +02:00
gTasks[taskId].data[1] = 8;
gRfu.parentChild = MODE_PARENT;
CreateTask(Task_PlayerExchange, 5);
2021-10-07 20:55:15 +02:00
gRfu.playerExchangeActive = TRUE;
2020-06-06 22:46:19 +02:00
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);
}
2021-10-07 20:55:15 +02:00
// Argument is provided by the RFU and is unused.
static void MSCCallback_Child(u16 REQ_commandID)
2020-02-16 19:49:36 +01:00
{
s32 i;
2021-10-07 22:19:02 +02:00
for (i = 0; i < COMM_SLOT_LENGTH; i++)
gRfu.childSendBuffer[i] = 0;
2020-02-16 19:49:36 +01:00
rfu_REQ_recvData();
rfu_waitREQComplete();
if (gRfuSlotStatusUNI[gRfu.childSlot]->recv.newDataFlag)
2020-02-16 19:49:36 +01:00
{
gRfu.childSendCount++;
2021-10-07 20:55:15 +02:00
RfuRecvQueue_Enqueue(&gRfu.recvQueue, gRfu.childRecvQueue);
sRfuDebug.recvCount++;
2020-06-07 23:37:09 +02:00
UpdateBackupQueue();
rfu_UNI_readySendData(gRfu.childSlot);
rfu_UNI_clearRecvNewDataFlag(gRfu.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
}
2021-10-07 20:55:15 +02:00
// Argument is provided by the RFU and is unused.
static void MSCCallback_Parent(u16 REQ_commandID)
2020-02-16 19:49:36 +01:00
{
2021-10-07 20:55:15 +02:00
gRfu.parentFinished = 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();
if (gRfu.parentChild == MODE_PARENT)
2020-02-16 19:49:36 +01:00
{
// Stop parent searching for children
if (FuncIsActiveTask(Task_ParentSearchForChildren) == TRUE)
2020-02-16 19:49:36 +01:00
{
DestroyTask(gRfu.searchTaskId);
2020-06-06 22:46:19 +02:00
ResetLinkRfuGFLayer();
2020-02-16 19:49:36 +01:00
}
}
else if (gRfu.parentChild == MODE_CHILD)
2020-02-16 19:49:36 +01:00
{
// Stop child searching for parent
if (FuncIsActiveTask(Task_ChildSearchForParent) == TRUE)
2020-02-16 19:49:36 +01:00
{
DestroyTask(gRfu.searchTaskId);
2020-06-06 22:46:19 +02:00
ResetLinkRfuGFLayer();
2020-02-16 19:49:36 +01:00
}
}
else if (gRfu.parentChild == MODE_P_C_SWITCH)
2020-02-16 19:49:36 +01:00
{
// Stop parent-child switching mode (union room)
if (FuncIsActiveTask(Task_UnionRoomListen) == TRUE)
2020-02-16 19:49:36 +01:00
{
DestroyTask(gRfu.searchTaskId);
2020-06-06 22:46:19 +02:00
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
}
}
static void CreateTask_ParentSearchForChildren(void)
2020-02-16 19:49:36 +01:00
{
gRfu.searchTaskId = CreateTask(Task_ParentSearchForChildren, 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
{
if (gRfu.state == RFUSTATE_CHILD_CONNECT_END && gRfu.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-10-07 20:55:15 +02:00
if (gRfu.state == RFUSTATE_CHILD_CONNECT_END && !rfu_LMAN_CHILD_connectParent(gRfuLinkStatus->partner[gRfu.reconnectParentId].id, 240))
2020-02-16 19:49:36 +01:00
{
gRfu.state = RFUSTATE_RECONNECTED;
2020-02-16 19:49:36 +01:00
return TRUE;
}
return FALSE;
}
static void CreateTask_ChildSearchForParent(void)
2020-02-16 19:49:36 +01:00
{
gRfu.searchTaskId = CreateTask(Task_ChildSearchForParent, 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
{
gRfu.state = RFUSTATE_STOP_MANAGER;
gRfu.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
{
if (gRfu.state == RFUSTATE_PARENT_FINALIZE_START || force)
2020-02-16 19:49:36 +01:00
{
gRfu.state = RFUSTATE_PARENT_FINALIZE;
2020-02-16 19:49:36 +01:00
return TRUE;
}
return FALSE;
}
2021-10-07 20:55:15 +02:00
void StopUnionRoomLinkManager(void)
2020-02-16 19:49:36 +01:00
{
2021-10-07 20:55:15 +02:00
gRfu.state = RFUSTATE_UR_STOP_MANAGER;
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
}
}
2021-10-07 20:55:15 +02:00
static void ReadAllPlayerRecvCmds(void)
2020-02-16 19:49:36 +01:00
{
s32 i, j;
for (i = 0; i < MAX_RFU_PLAYERS; i++)
2020-02-16 19:49:36 +01:00
{
struct RfuManager *rfu = &gRfu;
for (j = 0; j < CMD_LENGTH - 1; j++)
2020-02-16 19:49:36 +01:00
{
rfu->recvCmds[i][j][1] = gRecvCmds[i][j] >> 8;
rfu->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
{
if (gRfu.linkRecovered)
2020-02-16 19:49:36 +01:00
{
bool8 backupEmpty = RfuBackupQueue_Dequeue(&gRfu.backupQueue, gRfu.childSendBuffer);
2020-06-07 23:37:09 +02:00
if (gRfu.backupQueue.count == 0)
gRfu.linkRecovered = FALSE;
2020-06-07 23:37:09 +02:00
if (backupEmpty)
2020-02-16 19:49:36 +01:00
return;
}
if (!gRfu.linkRecovered)
2020-02-16 19:49:36 +01:00
{
RfuSendQueue_Dequeue(&gRfu.sendQueue, gRfu.childSendBuffer);
RfuBackupQueue_Enqueue(&gRfu.backupQueue, gRfu.childSendBuffer);
2020-02-16 19:49:36 +01:00
}
}
bool32 IsRfuRecvQueueEmpty(void)
{
s32 i;
s32 j;
if (!gRfuLinkStatus->sendSlotUNIFlag)
2020-02-16 19:49:36 +01:00
return FALSE;
for (i = 0; i < MAX_RFU_PLAYERS; i++)
for (j = 0; j < CMD_LENGTH - 1; j++)
2020-02-16 19:49:36 +01:00
if (gRecvCmds[i][j] != 0)
return FALSE;
2020-02-16 19:49:36 +01:00
return TRUE;
}
static bool32 RfuMain1_Parent(void)
2020-02-16 19:49:36 +01:00
{
if (gRfu.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
{
2021-10-07 20:55:15 +02:00
gRfu.parentFinished = FALSE;
if ((gRfu.parentSlots & gRfuLinkStatus->connSlotFlag) == gRfu.parentSlots && (gRfu.parentSlots & gRfuLinkStatus->connSlotFlag))
2020-02-16 19:49:36 +01:00
{
2021-10-07 20:55:15 +02:00
if (!gRfu.parentMain2Failed)
2020-02-16 19:49:36 +01:00
{
if (gRfu.disconnectSlots)
2020-02-16 19:49:36 +01:00
{
RfuReqDisconnectSlot(gRfu.disconnectSlots);
gRfu.disconnectSlots = 0;
if (gRfu.disconnectMode == RFU_DISCONNECT_ERROR)
2020-02-16 19:49:36 +01:00
{
2021-10-07 20:55:15 +02:00
RfuSetStatus(RFU_STATUS_CONNECTION_ERROR, F_RFU_ERROR_8);
RfuSetErrorParams(F_RFU_ERROR_8);
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;
}
}
2021-10-07 20:55:15 +02:00
ReadAllPlayerRecvCmds();
rfu_UNI_readySendData(gRfu.parentSendSlot);
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();
}
2021-10-07 20:55:15 +02:00
gRfu.runParentMain2 = 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;
2021-10-07 20:55:15 +02:00
bool8 failed;
2020-02-16 19:49:36 +01:00
2021-10-07 20:55:15 +02:00
if (gRfu.state >= RFUSTATE_FINALIZED && gRfu.runParentMain2 == TRUE)
2020-02-16 19:49:36 +01:00
{
rfu_waitREQComplete();
2021-10-07 20:55:15 +02:00
while (gRfu.parentFinished == FALSE)
2020-02-16 19:49:36 +01:00
{
if (gRfu.errorState != RFU_ERROR_STATE_NONE)
2020-02-16 19:49:36 +01:00
return FALSE;
}
rfu_REQ_recvData();
rfu_waitREQComplete();
2021-10-07 20:55:15 +02:00
if ((lman.parentAck_flag & gRfu.parentSlots) == gRfu.parentSlots)
2020-02-16 19:49:36 +01:00
{
2021-10-07 20:55:15 +02:00
gRfu.parentMain2Failed = FALSE;
sRfuDebug.recvCount++;
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 (gRfu.childRecvBuffer[i][1])
2020-02-16 19:49:36 +01:00
{
2021-10-07 20:55:15 +02:00
if (gRfu.childRecvIds[i] != 0xFF && (gRfu.childRecvBuffer[i][0] >> 5) != ((gRfu.childRecvIds[i] + 1) & 7))
2020-02-16 19:49:36 +01:00
{
2021-10-07 20:55:15 +02:00
if (++gRfu.numChildRecvErrors[i] > 4)
RfuSetErrorParams(F_RFU_ERROR_8 | F_RFU_ERROR_1);
2020-02-16 19:49:36 +01:00
}
else
{
2021-10-07 20:55:15 +02:00
gRfu.childRecvIds[i] = gRfu.childRecvBuffer[i][0] / 32;
gRfu.numChildRecvErrors[i] = 0;
gRfu.childRecvBuffer[i][0] &= 0x1f;
r0 = gRfu.linkPlayerIdx[i];
for (j = 0; j < CMD_LENGTH - 1; j++)
2020-02-16 19:49:36 +01:00
{
gRecvCmds[r0][j] = (gRfu.childRecvBuffer[i][(j << 1) + 1] << 8) | gRfu.childRecvBuffer[i][(j << 1) + 0];
gRfu.childRecvBuffer[i][(j << 1) + 1] = 0;
gRfu.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 (gRfu.nextChildBits && !gRfu.stopNewConnections)
2020-02-16 19:49:36 +01:00
{
2021-10-07 20:55:15 +02:00
sRfuDebug.unkFlag = FALSE;
rfu_clearSlot(TYPE_UNI_SEND | TYPE_UNI_RECV, gRfu.parentSendSlot);
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 ((gRfu.nextChildBits >> i) & 1)
rfu_setRecvBuffer(TYPE_UNI, i, gRfu.childRecvBuffer[i], sizeof(gRfu.childRecvBuffer[0]));
2020-02-16 19:49:36 +01:00
}
2021-10-07 20:55:15 +02:00
SetLinkPlayerIdsFromSlots(gRfu.parentSlots, gRfu.parentSlots | gRfu.nextChildBits);
gRfu.incomingChild = gRfu.nextChildBits;
2021-10-07 20:55:15 +02:00
gRfu.parentSlots |= gRfu.nextChildBits;
gRfu.nextChildBits = 0;
2021-10-07 20:55:15 +02:00
rfu_UNI_setSendData(gRfu.parentSlots, gRfu.recvCmds, sizeof(gRfu.recvCmds));
gRfu.parentSendSlot = Rfu_GetIndexOfNewestChild(gRfu.parentSlots);
CreateTask(Task_PlayerExchangeUpdate, 0);
2020-02-16 19:49:36 +01:00
}
}
else
{
2021-10-07 20:55:15 +02:00
gRfu.parentMain2Failed = TRUE;
gRfu.runParentMain2 = FALSE;
2020-02-16 19:49:36 +01:00
}
2021-10-07 20:55:15 +02:00
gRfu.runParentMain2 = FALSE;
2020-02-16 19:49:36 +01:00
}
2021-10-07 20:55:15 +02:00
failed = gRfu.parentMain2Failed;
return gRfuLinkStatus->sendSlotUNIFlag ? failed & 1 : FALSE;
2020-02-16 19:49:36 +01:00
}
2021-10-07 20:55:15 +02:00
static void ChildBuildSendCmd(u16 *sendCmd, u8 *dst)
2020-02-16 19:49:36 +01:00
{
s32 i;
if (sendCmd[0])
2020-02-16 19:49:36 +01:00
{
2021-10-07 20:55:15 +02:00
sendCmd[0] |= (gRfu.childSendCmdId << 5);
gRfu.childSendCmdId = (gRfu.childSendCmdId + 1) & 7;
for (i = 0; i < CMD_LENGTH - 1; i++)
2020-02-16 19:49:36 +01:00
{
2021-10-07 20:55:15 +02:00
dst[2 * i + 1] = sendCmd[i] >> 8;
dst[2 * i + 0] = sendCmd[i];
2020-02-16 19:49:36 +01:00
}
}
else
{
2021-10-07 22:19:02 +02:00
for (i = 0; i < COMM_SLOT_LENGTH; i++)
2021-10-07 20:55:15 +02:00
dst[i] = 0;
2020-02-16 19:49:36 +01:00
}
}
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(&gRfu.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++)
2021-10-07 22:19:02 +02:00
gRecvCmds[i][j] = (recv[i * COMM_SLOT_LENGTH + (j * 2) + 1] << 8)
| recv[i * COMM_SLOT_LENGTH + (j * 2) + 0];
2020-02-16 19:49:36 +01:00
}
2020-06-06 22:46:19 +02:00
RfuHandleReceiveCommand(0);
if (lman.childClockSlave_flag == 0 && gRfu.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)
2021-10-07 20:55:15 +02:00
RfuSetStatus(RFU_STATUS_CONNECTION_ERROR, F_RFU_ERROR_5 | F_RFU_ERROR_8);
2020-02-16 19:49:36 +01:00
rfu_clearAllSlot();
gReceivedRemoteLinkPlayers = FALSE;
gRfu.callback = NULL;
if (gRfu.disconnectMode == RFU_DISCONNECT_ERROR)
2020-02-16 19:49:36 +01:00
{
2021-10-07 20:55:15 +02:00
RfuSetStatus(RFU_STATUS_CONNECTION_ERROR, F_RFU_ERROR_5 | F_RFU_ERROR_8);
RfuSetErrorParams(F_RFU_ERROR_5 | F_RFU_ERROR_8);
2020-02-16 19:49:36 +01:00
}
lman.state = lman.next_state = 0;
gRfu.disconnectMode = RFU_DISCONNECT_NONE;
2020-02-16 19:49:36 +01:00
}
if (gRfu.childSendCount)
2020-02-16 19:49:36 +01:00
{
gRfu.childSendCount--;
2020-06-06 22:46:19 +02:00
CallRfuFunc();
2021-10-07 20:55:15 +02:00
ChildBuildSendCmd(gSendCmd, send);
RfuSendQueue_Enqueue(&gRfu.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 *payload = gRfu.sendBlock.payload;
for (i = 0; i < gRfu.sendBlock.count; i++)
2020-02-16 19:49:36 +01:00
{
if (!(flags & 1))
{
sResendBlock16[0] = RFUCMD_SEND_BLOCK | i;
for (j = 0; j < CMD_LENGTH - 1; j++)
2020-02-16 19:49:36 +01:00
{
temp = j * 2;
2021-10-07 22:19:02 +02:00
sResendBlock16[j + 1] = (payload[(COMM_SLOT_LENGTH - 2) * i + temp + 1] << 8)
| payload[(COMM_SLOT_LENGTH - 2) * i + temp + 0];
2020-02-16 19:49:36 +01:00
}
for (j = 0; j < CMD_LENGTH - 1; j++)
2020-02-16 19:49:36 +01:00
{
temp = j * 2;
2020-10-22 11:45:53 +02:00
sResendBlock8[temp + 1] = sResendBlock16[j] >> 8;
sResendBlock8[temp + 0] = sResendBlock16[j];
2020-02-16 19:49:36 +01:00
}
RfuSendQueue_Enqueue(&gRfu.sendQueue, sResendBlock8);
gRfu.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
{
if (gRfu.parentChild == MODE_PARENT && linkPlayerId)
gRfu.numBlocksReceived[linkPlayerId] = 1;
2020-02-16 19:49:36 +01:00
else
gRfu.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
{
gRfu.blockReceived[linkPlayerId] = FALSE;
gRfu.recvBlock[linkPlayerId].receiving = RECV_STATE_READY;
2020-02-16 19:49:36 +01:00
}
static u8 LoadLinkPlayerIds(const u8 *ids)
2020-02-16 19:49:36 +01:00
{
u8 i;
if (gRfu.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++)
gRfu.linkPlayerIdx[i] = ids[i];
return ids[gRfu.childSlot];
2020-02-16 19:49:36 +01:00
}
static void 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
}
}
struct RfuGameData *GetHostRfuGameData(void)
2020-02-16 19:49:36 +01:00
{
return &gHostRfuGameData;
2020-02-16 19:49:36 +01:00
}
bool32 IsSendingKeysToRfu(void)
{
return gRfu.callback == 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
{
gRfu.callback = SendKeysToRfu;
2020-02-16 19:49:36 +01:00
}
void ClearLinkRfuCallback(void)
{
gRfu.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
{
if (gRfu.callback == NULL)
gRfu.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:
if (gRfu.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
{
gRfu.playerCount = gRecvCmds[i][1];
gRfu.multiplayerId = LoadLinkPlayerIds((u8 *)(gRecvCmds[i] + 2));
2020-02-16 19:49:36 +01:00
}
break;
case RFUCMD_SEND_BLOCK_INIT:
if (gRfu.recvBlock[i].receiving == RECV_STATE_READY)
2020-02-16 19:49:36 +01:00
{
gRfu.recvBlock[i].next = 0;
gRfu.recvBlock[i].count = gRecvCmds[i][1];
gRfu.recvBlock[i].owner = gRecvCmds[i][2];
gRfu.recvBlock[i].receivedFlags = 0;
gRfu.recvBlock[i].receiving = RECV_STATE_RECEIVING;
gRfu.blockReceived[i] = FALSE;
2020-02-16 19:49:36 +01:00
}
break;
case RFUCMD_SEND_BLOCK:
if (gRfu.recvBlock[i].receiving == RECV_STATE_RECEIVING)
2020-02-16 19:49:36 +01:00
{
gRfu.recvBlock[i].next = gRecvCmds[i][0] & 0xff;
gRfu.recvBlock[i].receivedFlags |= (1 << gRfu.recvBlock[i].next);
2020-02-16 19:49:36 +01:00
for (j = 0; j < 6; j++)
gBlockRecvBuffer[i][gRfu.recvBlock[i].next * 6 + j] = gRecvCmds[i][j + 1];
if (gRfu.recvBlock[i].receivedFlags == sAllBlocksReceived[gRfu.recvBlock[i].count])
2020-02-16 19:49:36 +01:00
{
gRfu.recvBlock[i].receiving = RECV_STATE_FINISHED;
2020-06-06 22:46:19 +02:00
Rfu_SetBlockReceivedFlag(i);
if (GetHostRfuGameData()->activity == (ACTIVITY_CHAT | IN_UNION_ROOM) && gReceivedRemoteLinkPlayers && gRfu.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:
gRfu.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 (gRfu.allReadyNum == gRecvCmds[i][1])
gRfu.readyExitStandby[i] = TRUE;
2020-02-16 19:49:36 +01:00
break;
case RFUCMD_DISCONNECT:
if (gRfu.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();
gRfu.disconnectMode = gRecvCmds[i][2];
2020-02-16 19:49:36 +01:00
}
gRfu.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:
if (gRfu.parentChild == MODE_PARENT)
2020-02-16 19:49:36 +01:00
{
gRfu.disconnectSlots |= gRecvCmds[i][1];
gRfu.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;
}
if (gRfu.parentChild == MODE_PARENT && gRfu.numBlocksReceived[i])
2020-02-16 19:49:36 +01:00
{
if (gRfu.numBlocksReceived[i] == 4)
2020-02-16 19:49:36 +01:00
{
gRfu.blockReceived[i] = TRUE;
gRfu.numBlocksReceived[i] = 0;
2020-02-16 19:49:36 +01:00
}
else
gRfu.numBlocksReceived[i]++;
2020-02-16 19:49:36 +01:00
}
}
}
static bool8 AreAllPlayersReadyToReceive(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 (gRfu.recvBlock[i].receiving != RECV_STATE_READY)
2020-02-16 19:49:36 +01:00
return FALSE;
}
return TRUE;
}
static bool8 AreAllPlayersFinishedReceiving(void)
2020-02-16 19:49:36 +01:00
{
s32 i;
for (i = 0; i < gRfu.playerCount; i++)
2020-02-16 19:49:36 +01:00
{
if (gRfu.recvBlock[i].receiving != RECV_STATE_FINISHED || gRfu.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 = RECV_STATE_READY;
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 (gRfu.recvBlock[i].receiving == RECV_STATE_FINISHED && gRfu.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] = gRfu.sendBlock.count;
gSendCmd[2] = gRfu.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:
if (AreAllPlayersReadyToReceive())
gSendCmd[1] = gRfu.blockRequestType;
2020-02-16 19:49:36 +01:00
break;
case RFUCMD_SEND_PLAYER_IDS:
case RFUCMD_SEND_PLAYER_IDS_NEW:
2021-10-07 20:55:15 +02:00
tmp = gRfu.parentSlots ^ gRfu.disconnectSlots;
gRfu.playerCount = sPlayerBitsToCount[tmp] + 1;
gSendCmd[1] = gRfu.playerCount;
buff = (u8 *)&gSendCmd[2];
2020-06-06 22:46:19 +02:00
for (i = 0; i < RFU_CHILD_MAX; i++)
buff[i] = gRfu.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] = gRfu.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] = gRfu.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
{
memcpy(gRfu.packet, data, sizeof(gRfu.packet));
2020-08-24 20:52:33 +02:00
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 (gRfu.callback != NULL)
return FALSE;
if (gSendCmd[0] != 0)
2020-02-16 19:49:36 +01:00
return FALSE;
if (gRfu.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;
gRfu.sendBlock.owner = GetMultiplayerId();
gRfu.sendBlock.sending = TRUE;
gRfu.sendBlock.count = (size / 12) + r4;
gRfu.sendBlock.next = 0;
if (size > BLOCK_BUFFER_SIZE)
gRfu.sendBlock.payload = src;
2020-02-16 19:49:36 +01:00
else
{
if (src != gBlockSendBuffer)
memcpy(gBlockSendBuffer, src, size);
gRfu.sendBlock.payload = gBlockSendBuffer;
2020-02-16 19:49:36 +01:00
}
RfuPrepareSendBuffer(RFUCMD_SEND_BLOCK_INIT);
gRfu.callback = HandleBlockSend;
gRfu.blockSendAttempts = 0;
2020-02-16 19:49:36 +01:00
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);
if (gRfu.parentChild == MODE_PARENT)
2020-02-16 19:49:36 +01:00
{
if (++gRfu.blockSendAttempts > 2)
gRfu.callback = SendNextBlock;
2020-02-16 19:49:36 +01:00
}
else
{
if ((gRecvCmds[GetMultiplayerId()][0] & RFUCMD_MASK) == RFUCMD_SEND_BLOCK_INIT)
gRfu.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 = gRfu.sendBlock.payload;
gSendCmd[0] = RFUCMD_SEND_BLOCK | gRfu.sendBlock.next;
for (i = 0; i < CMD_LENGTH - 1; i++)
gSendCmd[i + 1] = (src[(i << 1) + gRfu.sendBlock.next * 12 + 1] << 8) | src[(i << 1) + gRfu.sendBlock.next * 12 + 0];
gRfu.sendBlock.next++;
if (gRfu.sendBlock.count <= gRfu.sendBlock.next)
{
gRfu.sendBlock.sending = FALSE;
gRfu.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 = gRfu.sendBlock.payload;
2020-02-16 19:49:36 +01:00
u8 mpId = GetMultiplayerId();
s32 i;
if (gRfu.parentChild == MODE_CHILD)
2020-02-16 19:49:36 +01:00
{
gSendCmd[0] = RFUCMD_SEND_BLOCK | (gRfu.sendBlock.count - 1);
for (i = 0; i < CMD_LENGTH - 1; i++)
gSendCmd[i + 1] = (src[(i << 1) + (gRfu.sendBlock.count - 1) * 12 + 1] << 8) | src[(i << 1) + (gRfu.sendBlock.count - 1) * 12 + 0];
if ((u8)gRecvCmds[mpId][0] == gRfu.sendBlock.count - 1)
2020-02-16 19:49:36 +01:00
{
if (gRfu.recvBlock[mpId].receivedFlags != sAllBlocksReceived[gRfu.recvBlock[mpId].count])
2020-02-16 19:49:36 +01:00
{
HandleSendFailure(mpId, gRfu.recvBlock[mpId].receivedFlags);
sRfuDebug.blockSendFailures++;
2020-02-16 19:49:36 +01:00
}
else
{
gRfu.callback = NULL;
}
2020-02-16 19:49:36 +01:00
}
}
else
{
gRfu.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
{
gRfu.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;
gRfu.isShuttingDown = TRUE;
gRfu.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
{
if (gRfu.parentChild == MODE_CHILD)
2020-02-16 19:49:36 +01:00
{
rfu_LMAN_requestChangeAgbClockMaster();
gRfu.disconnectMode = RFU_DISCONNECT_NORMAL;
2020-02-16 19:49:36 +01:00
}
else
{
gRfu.callback = DisconnectRfu;
}
2020-02-16 19:49:36 +01:00
}
void LinkRfu_FatalError(void)
{
rfu_LMAN_requestChangeAgbClockMaster();
gRfu.disconnectMode = RFU_DISCONNECT_ERROR;
gRfu.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 = gRfu.playerCount;
2020-02-16 19:49:36 +01:00
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++)
{
if (gRfu.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;
if (gRfu.parentChild == MODE_CHILD)
2020-02-16 19:49:36 +01:00
{
gRfu.errorState = RFU_ERROR_STATE_DISCONNECTING;
2020-08-13 09:09:47 +02:00
TryDisconnectRfu();
2020-02-16 19:49:36 +01:00
}
else
{
gRfu.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
{
2021-10-07 20:55:15 +02:00
if (gSendCmd[0] == 0 && !gRfu.playerExchangeActive)
2020-02-16 19:49:36 +01:00
{
2020-08-24 20:52:33 +02:00
RfuPrepareSendBuffer(RFUCMD_READY_CLOSE_LINK);
gRfu.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
{
if (gRfu.callback == NULL)
2020-02-16 19:49:36 +01:00
{
gRfu.stopNewConnections = TRUE;
gRfu.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 (gRfu.recvQueue.count == 0 && gRfu.resendExitStandbyTimer > 60)
{
RfuPrepareSendBuffer(RFUCMD_READY_EXIT_STANDBY);
gRfu.resendExitStandbyTimer = 0;
}
2020-02-16 19:49:36 +01:00
}
playerCount = GetLinkPlayerCount();
for (i = 0; i < playerCount; i++)
{
if (!gRfu.readyExitStandby[i])
2020-02-16 19:49:36 +01:00
break;
}
if (i == playerCount)
{
for (i = 0; i < MAX_RFU_PLAYERS; i++)
gRfu.readyExitStandby[i] = FALSE;
gRfu.allReadyNum++;
gRfu.callback = NULL;
2020-02-16 19:49:36 +01:00
}
gRfu.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
{
if (gRfu.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);
gRfu.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
if (gRfu.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);
gRfu.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++)
{
if (!gRfu.readyExitStandby[i])
2020-02-16 19:49:36 +01:00
break;
}
if (i == playerCount)
{
if (gRfu.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);
gRfu.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
{
if (gRfu.callback == NULL)
2020-02-16 19:49:36 +01:00
{
gRfu.callback = Rfu_LinkStandby;
gRfu.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
{
gRfu.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
{
if (gRfu.parentChild == MODE_PARENT)
2020-02-16 19:49:36 +01:00
return 0;
return gRfu.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 gRfu.playerCount;
2020-02-16 19:49:36 +01:00
}
bool8 IsLinkRfuTaskFinished(void)
{
if (gRfu.status == RFU_STATUS_CONNECTION_ERROR)
2020-02-16 19:49:36 +01:00
return FALSE;
return gRfu.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
{
if (gRfu.callback)
gRfu.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
{
2021-10-07 22:10:35 +02:00
if (gRfu.partnerSendStatuses[i] < RFU_STATUS_JOIN_GROUP_OK
|| gRfu.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 (gRfu.partnerRecvStatuses[i] == RFU_STATUS_LEAVE_GROUP_NOTICE)
{
gRfu.partnerSendStatuses[i] = RFU_STATUS_LEAVE_GROUP;
gRfu.partnerRecvStatuses[i] = RFU_STATUS_CHILD_LEAVE_READY;
rfu_clearSlot(TYPE_NI_RECV, i);
rfu_NI_setSendData(1 << i, 8, &gRfu.partnerSendStatuses[i], 1);
memberLeft = TRUE;
}
}
else if (gRfuSlotStatusNI[gRfu.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;
2022-09-11 20:14:49 +02:00
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
{
if (gRfu.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);
gRfu.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-10-07 22:10:35 +02:00
if (gRfu.partnerRecvStatuses[i] == RFU_STATUS_CHILD_LEAVE_READY
|| gRfu.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;
if (gRfu.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);
gRfu.partnerSendStatuses[idx] = status;
2020-06-06 22:46:19 +02:00
rfu_clearSlot(TYPE_NI_SEND, idx);
rfu_NI_setSendData(1 << idx, 8, &gRfu.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
{
gRfu.leaveGroupStatus = RFU_STATUS_LEAVE_GROUP_NOTICE;
rfu_clearSlot(TYPE_NI_SEND, gRfu.childSlot);
rfu_NI_setSendData(1 << gRfu.childSlot, 8, &gRfu.leaveGroupStatus, 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
{
if (gRfuSlotStatusNI[i]->send.state == SLOT_STATE_SEND_SUCCESS
2020-06-06 22:46:19 +02:00
|| gRfuSlotStatusNI[i]->send.state == SLOT_STATE_SEND_FAILED)
2020-02-16 19:49:36 +01:00
{
if (gRfu.partnerRecvStatuses[i] == RFU_STATUS_CHILD_LEAVE_READY)
gRfu.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
}
}
}
static s32 GetJoinGroupStatus(void)
2020-02-16 19:49:36 +01:00
{
2020-06-09 00:16:57 +02:00
s32 status = RFU_STATUS_OK;
if (gRfu.leaveGroupStatus == RFU_STATUS_LEAVE_GROUP_NOTICE)
2020-02-16 19:49:36 +01:00
{
2021-10-07 22:10:35 +02:00
if (gRfuSlotStatusNI[gRfu.childSlot]->send.state == SLOT_STATE_SEND_SUCCESS
|| gRfuSlotStatusNI[gRfu.childSlot]->send.state == SLOT_STATE_SEND_FAILED)
rfu_clearSlot(TYPE_NI_SEND, gRfu.childSlot);
2020-02-16 19:49:36 +01:00
}
if (gRfuSlotStatusNI[gRfu.childSlot]->recv.state == SLOT_STATE_RECV_SUCCESS
|| gRfuSlotStatusNI[gRfu.childSlot]->recv.state == SLOT_STATE_RECV_SUCCESS_AND_SENDSIDE_UNKNOWN)
2020-02-16 19:49:36 +01:00
{
rfu_clearSlot(TYPE_NI_RECV, gRfu.childSlot);
2021-10-07 20:55:15 +02:00
RfuSetStatus(gRfu.childRecvStatus, 0);
status = gRfu.childRecvStatus;
2020-02-16 19:49:36 +01:00
}
else if (gRfuSlotStatusNI[gRfu.childSlot]->recv.state == SLOT_STATE_RECV_FAILED)
2020-02-16 19:49:36 +01:00
{
rfu_clearSlot(TYPE_NI_RECV, gRfu.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]
static void Task_PlayerExchange(u8 taskId)
2020-02-16 19:49:36 +01:00
{
s32 i;
if (gRfu.status == RFU_STATUS_FATAL_ERROR || gRfu.status == RFU_STATUS_CONNECTION_ERROR)
2020-02-16 19:49:36 +01:00
{
2021-10-07 20:55:15 +02:00
gRfu.playerExchangeActive = 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 (AreAllPlayersReadyToReceive())
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 (gRfu.parentChild == MODE_PARENT)
2020-06-06 22:46:19 +02:00
{
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 (gRfu.playerCount)
gTasks[taskId].tState++;
2020-06-06 22:46:19 +02:00
break;
case 3:
if (gRfu.parentChild == MODE_PARENT)
2020-06-06 22:46:19 +02:00
{
if (AreAllPlayersReadyToReceive())
2020-02-16 19:49:36 +01:00
{
gRfu.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 (AreAllPlayersFinishedReceiving())
gTasks[taskId].tState++;
2020-06-06 22:46:19 +02:00
break;
case 5:
for (i = 0; i < gRfu.playerCount; i++)
2020-06-06 22:46:19 +02:00
{
LinkPlayerFromBlock(i);
Rfu_ResetBlockReceivedFlag(i);
}
gTasks[taskId].tState++;
2020-06-06 22:46:19 +02:00
break;
case 6:
DestroyTask(taskId);
gReceivedRemoteLinkPlayers = TRUE;
2021-10-07 20:55:15 +02:00
gRfu.playerExchangeActive = FALSE;
2020-06-06 22:46:19 +02:00
rfu_LMAN_setLinkRecovery(1, 600);
if (gRfu.newChildQueue)
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 ((gRfu.newChildQueue >> i) & 1)
2020-02-16 19:49:36 +01:00
{
gRfu.nextChildBits = 1 << i;
gRfu.newChildQueue ^= (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)
gRfu.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;
gRfu.playerCount = sioInfo->playerCount;
2020-06-06 22:46:19 +02:00
for (i = 0; i < RFU_CHILD_MAX; i++)
gRfu.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);
}
}
// Equivalent to Task_PlayerExchange, but for when new children arrive after the first exchange
static void Task_PlayerExchangeUpdate(u8 taskId)
2020-02-16 19:49:36 +01:00
{
s32 i;
struct LinkPlayerBlock *playerBlock;
struct SioInfo *sio;
2021-10-07 20:55:15 +02:00
u8 playerId = gRfu.linkPlayerIdx[sSlotToLinkPlayerTableId[gRfu.incomingChild]];
if (gRfu.status == RFU_STATUS_FATAL_ERROR || gRfu.status == RFU_STATUS_CONNECTION_ERROR)
2020-02-16 19:49:36 +01:00
{
2021-10-07 20:55:15 +02:00
gRfu.playerExchangeActive = 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 = gRfu.playerCount;
2020-06-06 22:46:19 +02:00
for (i = 0; i < RFU_CHILD_MAX; i++)
sio->linkPlayerIdx[i] = gRfu.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 = gRfu.playerCount;
2020-06-06 22:46:19 +02:00
for (i = 0; i < RFU_CHILD_MAX; i++)
sio->linkPlayerIdx[i] = gRfu.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);
2021-10-07 20:55:15 +02:00
gRfu.playerExchangeActive = FALSE;
if (gRfu.newChildQueue)
2020-02-16 19:49:36 +01:00
{
for (i = 0; i < RFU_CHILD_MAX; i++)
2020-02-16 19:49:36 +01:00
{
if ((gRfu.newChildQueue >> i) & 1)
2020-02-16 19:49:36 +01:00
{
gRfu.nextChildBits = 1 << i;
gRfu.newChildQueue ^= (1 << i);
2021-10-07 20:55:15 +02:00
gRfu.playerExchangeActive = 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
}
}
// Equivalent to Task_PlayerExchange but for chatting with a Union Room partner
static void Task_PlayerExchangeChat(u8 taskId)
2020-02-16 19:49:36 +01:00
{
if (gRfu.status == RFU_STATUS_FATAL_ERROR || gRfu.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 (gRfu.playerCount)
2020-06-06 22:46:19 +02:00
{
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 (gRfu.errorState == RFU_ERROR_STATE_OCCURRED && lman.childClockSlave_flag == 0)
2020-02-16 19:49:36 +01:00
{
2021-10-14 20:10:42 +02:00
if (gMain.callback2 == CB2_MysteryGiftEReader || lman.init_param->mboot_flag)
2020-02-16 19:49:36 +01:00
gWirelessCommType = 2;
SetMainCallback2(CB2_LinkError);
gMain.savedCallback = CB2_LinkError;
2021-10-07 20:55:15 +02:00
SetLinkErrorBuffer((gRfu.errorInfo << 16) | (gRfu.errorParam0 << 8) | gRfu.errorParam1, gRfu.recvQueue.count, gRfu.sendQueue.count, RfuGetStatus() == RFU_STATUS_CONNECTION_ERROR);
gRfu.errorState = RFU_ERROR_STATE_PROCESSED;
2020-02-16 19:49:36 +01:00
CloseLink();
}
else if (gRfu.sendQueue.full == TRUE || gRfu.recvQueue.full == TRUE)
2020-02-16 19:49:36 +01:00
{
if (lman.childClockSlave_flag)
rfu_LMAN_requestChangeAgbClockMaster();
2021-10-07 20:55:15 +02:00
RfuSetStatus(RFU_STATUS_FATAL_ERROR, F_RFU_ERROR_5 | F_RFU_ERROR_6 | F_RFU_ERROR_7);
RfuSetErrorParams(F_RFU_ERROR_5 | F_RFU_ERROR_6 | F_RFU_ERROR_7);
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();
2022-07-25 20:59:14 +02:00
rfu_LMAN_REQ_sendData(FALSE);
2020-02-16 19:49:36 +01:00
}
}
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;
gRfu.parentId = 0;
2020-02-16 19:49:36 +01:00
rfu_LMAN_manager_entity(Random2());
if (!gRfu.isShuttingDown)
2020-02-16 19:49:36 +01:00
{
switch (gRfu.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;
if (!gRfu.isShuttingDown)
2020-02-16 19:49:36 +01:00
{
if (gRfu.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;
}
static void SetHostRfuUsername(void)
2020-02-16 19:49:36 +01:00
{
StringCopy(gHostRfuUsername, gSaveBlock2Ptr->playerName);
2020-02-16 19:49:36 +01:00
}
void ResetHostRfuGameData(void)
2020-02-16 19:49:36 +01:00
{
memset(&gHostRfuGameData, 0, RFU_GAME_NAME_LENGTH);
InitHostRfuGameData(&gHostRfuGameData, ACTIVITY_NONE, FALSE, 0);
2020-02-16 19:49:36 +01:00
}
void SetHostRfuGameData(u8 activity, u32 partnerInfo, bool32 startedActivity)
2020-02-16 19:49:36 +01:00
{
InitHostRfuGameData(&gHostRfuGameData, activity, startedActivity, partnerInfo);
2020-02-16 19:49:36 +01:00
}
void SetHostRfuWonderFlags(bool32 hasNews, bool32 hasCard)
2020-02-16 19:49:36 +01:00
{
gHostRfuGameData.compatibility.hasNews = hasNews;
gHostRfuGameData.compatibility.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
{
gHostRfuGameData.tradeType = type;
gHostRfuGameData.tradeSpecies = species;
gHostRfuGameData.tradeLevel = level;
2020-02-16 19:49:36 +01:00
}
u8 GetLinkPlayerInfoFlags(s32 playerId)
2020-02-16 19:49:36 +01:00
{
u8 retval = PINFO_ACTIVE_FLAG;
retval |= (gLinkPlayers[playerId].gender << PINFO_GENDER_SHIFT);
retval |= (gLinkPlayers[playerId].trainerId & PINFO_TID_MASK);
2020-02-16 19:49:36 +01:00
return retval;
}
void GetOtherPlayersInfoFlags(void)
2020-02-16 19:49:36 +01:00
{
struct RfuGameData *data = &gHostRfuGameData;
2020-02-16 19:49:36 +01:00
s32 i;
for (i = 1; i < GetLinkPlayerCount(); i++)
data->partnerInfo[i - 1] = GetLinkPlayerInfoFlags(i);
2020-02-16 19:49:36 +01:00
}
void UpdateGameData_GroupLockedIn(bool8 startedActivity)
2020-02-16 19:49:36 +01:00
{
gHostRfuGameData.startedActivity = startedActivity;
rfu_REQ_configGameData(0, RFU_SERIAL_GAME, (void *)&gHostRfuGameData, gHostRfuUsername);
2020-02-16 19:49:36 +01:00
}
void UpdateGameData_SetActivity(u8 activity, u32 partnerInfo, bool32 startedActivity)
2020-02-16 19:49:36 +01:00
{
2020-06-09 00:16:57 +02:00
if (activity != ACTIVITY_NONE)
SetHostRfuGameData(activity, partnerInfo, startedActivity);
rfu_REQ_configGameData(0, RFU_SERIAL_GAME, (void *)&gHostRfuGameData, gHostRfuUsername);
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 partnerInfo;
s32 slots;
2020-02-16 19:49:36 +01:00
if (GetHostRfuGameData()->activity == (ACTIVITY_CHAT | IN_UNION_ROOM))
2020-02-16 19:49:36 +01:00
{
2020-06-06 22:46:19 +02:00
numConnectedChildren = 0;
partnerInfo = 0;
2021-10-07 20:55:15 +02:00
slots = gRfu.parentSlots ^ gRfu.disconnectSlots;
for (i = 0; i < RFU_CHILD_MAX; i++)
2020-02-16 19:49:36 +01:00
{
if ((slots >> i) & 1)
2020-02-16 19:49:36 +01:00
{
// Only trainerId is shifted by the number of children, so the active flag and gender
// are only ever set for the first child
partnerInfo |= ((PINFO_ACTIVE_FLAG
2022-09-11 20:14:49 +02:00
| ((gLinkPlayers[gRfu.linkPlayerIdx[i]].gender & 1) << PINFO_GENDER_SHIFT)
| (gLinkPlayers[gRfu.linkPlayerIdx[i]].trainerId & PINFO_TID_MASK)) << (numConnectedChildren * 8));
numConnectedChildren++;
if (numConnectedChildren == numPlayers - 1)
2020-02-16 19:49:36 +01:00
break;
}
}
UpdateGameData_SetActivity(ACTIVITY_CHAT | IN_UNION_ROOM, partnerInfo, FALSE);
2020-02-16 19:49:36 +01:00
}
}
2021-10-07 20:55:15 +02:00
void RfuSetErrorParams(u32 errorInfo)
2020-02-16 19:49:36 +01:00
{
if (gRfu.errorState == RFU_ERROR_STATE_NONE)
2020-02-16 19:49:36 +01:00
{
2021-10-07 20:55:15 +02:00
gRfu.errorParam0 = lman.param[0];
gRfu.errorParam1 = lman.param[1];
gRfu.errorInfo = errorInfo;
gRfu.errorState = RFU_ERROR_STATE_OCCURRED;
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
{
gRfu.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)
gRfu.errorState = RFU_ERROR_STATE_NONE;
2020-02-16 19:49:36 +01:00
else
gRfu.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);
gRfu.callback = NULL;
2020-02-16 19:49:36 +01:00
}
static void StartDisconnectNewChild(void)
2020-02-16 19:49:36 +01:00
{
gRfu.callback = DisconnectNewChild;
2020-02-16 19:49:36 +01:00
}
static void LinkManagerCB_Parent(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:
gRfu.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:
2021-10-07 20:55:15 +02:00
ParentResetChildRecvMetadata(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 RfuGameData *data = (void *)gRfuLinkStatus->partner[i].gname;
if (data->activity == GetHostRfuGameData()->activity)
2020-02-16 19:49:36 +01:00
{
gRfu.partnerSendStatuses[i] = RFU_STATUS_OK;
gRfu.partnerRecvStatuses[i] = RFU_STATUS_OK;
rfu_setRecvBuffer(TYPE_NI, i, &gRfu.partnerRecvStatuses[i], sizeof(gRfu.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 (gRfu.acceptSlot_flag != lman.acceptSlot_flag)
2020-02-16 19:49:36 +01:00
{
rfu_REQ_disconnect(gRfu.acceptSlot_flag ^ lman.acceptSlot_flag);
2020-02-16 19:49:36 +01:00
rfu_waitREQComplete();
}
gRfu.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:
gRfu.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:
gRfu.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:
gRfu.linkLossRecoveryState = 4;
2021-10-07 20:55:15 +02:00
gRfu.parentSlots &= ~lman.param[0];
2020-02-16 19:49:36 +01:00
if (gReceivedRemoteLinkPlayers == 1)
{
2021-10-07 20:55:15 +02:00
if (gRfu.parentSlots == 0)
RfuSetErrorParams(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);
2021-10-07 20:55:15 +02:00
RfuSetErrorParams(msg);
gRfu.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:
2021-10-07 20:55:15 +02:00
RfuSetErrorParams(msg);
2020-06-09 00:16:57 +02:00
RfuSetStatus(RFU_STATUS_FATAL_ERROR, msg);
2021-10-07 20:55:15 +02:00
gRfu.parentFinished = TRUE;
2020-02-16 19:49:36 +01:00
break;
}
}
static void LinkManagerCB_Child(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:
gRfu.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:
gRfu.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:
gRfu.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:
gRfu.state = RFUSTATE_CHILD_TRY_JOIN;
gRfu.leaveGroupStatus = RFU_STATUS_OK;
2021-10-07 20:55:15 +02:00
gRfu.childRecvStatus = RFU_STATUS_OK;
rfu_setRecvBuffer(TYPE_NI, gRfu.childSlot, &gRfu.childRecvStatus, sizeof(gRfu.childRecvStatus));
rfu_setRecvBuffer(TYPE_UNI, gRfu.childSlot, gRfu.childRecvQueue, sizeof(gRfu.childRecvQueue));
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:
gRfu.linkLossRecoveryState = 2;
2021-10-07 20:55:15 +02:00
if (gRfu.childRecvStatus == 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 (gRfu.linkLossRecoveryState != 2)
gRfu.linkLossRecoveryState = 4;
2021-10-07 20:55:15 +02:00
if (gRfu.childRecvStatus != RFU_STATUS_LEAVE_GROUP)
2020-06-09 00:16:57 +02:00
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)
2021-10-07 20:55:15 +02:00
RfuSetErrorParams(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:
gRfu.linkLossRecoveryState = 1;
2020-06-06 22:46:19 +02:00
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:
gRfu.linkLossRecoveryState = 3;
gRfu.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);
2021-10-07 20:55:15 +02:00
RfuSetErrorParams(msg);
gRfu.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);
2021-10-07 20:55:15 +02:00
RfuSetErrorParams(msg);
gRfu.parentFinished = TRUE;
2020-02-16 19:49:36 +01:00
break;
}
}
2021-10-07 20:55:15 +02:00
static void ParentResetChildRecvMetadata(s32 slot)
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-10-07 20:55:15 +02:00
if ((slot >> i) & 1)
2020-02-16 19:49:36 +01:00
{
2021-10-07 20:55:15 +02:00
gRfu.numChildRecvErrors[i] = 0;
gRfu.childRecvIds[i] = 0xFF;
2020-02-16 19:49:36 +01:00
}
}
}
static u8 GetNewChildrenInUnionRoomChat(s32 emptySlotMask)
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 ((emptySlotMask >> i) & 1)
2020-02-16 19:49:36 +01:00
{
struct RfuGameData *data = (void *)gRfuLinkStatus->partner[i].gname;
if (data->activity == (ACTIVITY_CHAT | IN_UNION_ROOM))
2020-02-16 19:49:36 +01:00
ret |= (1 << i);
}
}
return ret;
}
static void LinkManagerCB_UnionRoom(u8 msg, u8 paramCount)
2020-02-16 19:49:36 +01:00
{
u8 acceptSlot;
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:
gRfu.state = RFUSTATE_UR_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-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 (GetHostRfuGameData()->activity == (ACTIVITY_CHAT | IN_UNION_ROOM) && !gRfu.stopNewConnections)
2020-02-16 19:49:36 +01:00
{
u8 newChildren = GetNewChildrenInUnionRoomChat(lman.param[0]);
if (newChildren != 0)
2020-02-16 19:49:36 +01:00
{
acceptSlot = 1 << Rfu_GetIndexOfNewestChild(newChildren);
2021-10-07 20:55:15 +02:00
if (gRfu.newChildQueue == 0 && !gRfu.playerExchangeActive)
2020-02-16 19:49:36 +01:00
{
gRfu.nextChildBits = acceptSlot;
gRfu.newChildQueue |= (acceptSlot ^ newChildren);
2021-10-07 20:55:15 +02:00
gRfu.playerExchangeActive = TRUE;
2020-02-16 19:49:36 +01:00
}
else
{
gRfu.newChildQueue |= newChildren;
2020-02-16 19:49:36 +01:00
}
}
if (newChildren != lman.param[0])
2020-02-16 19:49:36 +01:00
{
gRfu.disconnectSlots |= (newChildren ^ lman.param[0]);
gRfu.disconnectMode = RFU_DISCONNECT_NORMAL;
2020-02-16 19:49:36 +01:00
}
}
else if (GetHostRfuGameData()->activity == (ACTIVITY_PLYRTALK | IN_UNION_ROOM))
2020-02-16 19:49:36 +01:00
{
rfu_REQ_disconnect(lman.acceptSlot_flag);
rfu_waitREQComplete();
}
2021-10-07 20:55:15 +02:00
ParentResetChildRecvMetadata(lman.param[0]);
2020-02-16 19:49:36 +01:00
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 (GetHostRfuGameData()->activity != (ACTIVITY_CHAT | IN_UNION_ROOM) && lman.acceptCount > 1)
2020-02-16 19:49:36 +01:00
{
acceptSlot = 1 << Rfu_GetIndexOfNewestChild(lman.param[0]);
rfu_REQ_disconnect(lman.acceptSlot_flag ^ acceptSlot);
2020-02-16 19:49:36 +01:00
rfu_waitREQComplete();
}
2021-10-07 20:55:15 +02:00
if (gRfu.state == RFUSTATE_UR_STOP_MANAGER_END)
gRfu.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:
gRfu.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:
gRfu.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:
gRfu.state = RFUSTATE_UR_CONNECT_END;
if (gRfu.connectParentFailures < 2)
2020-02-16 19:49:36 +01:00
{
gRfu.connectParentFailures++;
CreateTask(Task_TryConnectToUnionRoomParent, 2);
2020-02-16 19:49:36 +01:00
}
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:
gRfu.state = RFUSTATE_UR_PLAYER_EXCHANGE;
2020-06-09 00:16:57 +02:00
RfuSetStatus(RFU_STATUS_CHILD_SEND_COMPLETE, 0);
2021-10-07 20:55:15 +02:00
rfu_setRecvBuffer(TYPE_UNI, gRfu.childSlot, gRfu.childRecvQueue, sizeof(gRfu.childRecvQueue));
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])
gRfu.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:
gRfu.linkLossRecoveryState = 3;
2020-06-06 22:46:19 +02:00
if (gRfuLinkStatus->parentChild == MODE_CHILD)
gRfu.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:
gRfu.linkLossRecoveryState = 2;
2020-06-06 22:46:19 +02:00
case LMAN_MSG_LINK_RECOVERY_FAILED_AND_DISCONNECTED:
if (gRfu.linkLossRecoveryState != 2)
gRfu.linkLossRecoveryState = 4;
if (gRfu.parentChild == MODE_PARENT)
2020-02-16 19:49:36 +01:00
{
if (gReceivedRemoteLinkPlayers == 1)
{
2021-10-07 20:55:15 +02:00
gRfu.parentSlots &= ~(lman.param[0]);
if (gRfu.parentSlots == 0)
RfuSetErrorParams(msg);
2020-02-16 19:49:36 +01:00
else
StartDisconnectNewChild();
2020-02-16 19:49:36 +01:00
}
}
else if (gRfu.disconnectMode != RFU_DISCONNECT_NORMAL && gReceivedRemoteLinkPlayers == 1)
2020-02-16 19:49:36 +01:00
{
2021-10-07 20:55:15 +02:00
RfuSetErrorParams(msg);
rfu_LMAN_stopManager(FALSE);
2020-02-16 19:49:36 +01:00
}
2022-09-11 20:14:49 +02:00
if (gRfuLinkStatus->parentChild == MODE_NEUTRAL
2021-10-07 20:55:15 +02:00
&& !lman.pcswitch_flag
&& FuncIsActiveTask(Task_UnionRoomListen) == TRUE)
gRfu.state = RFUSTATE_UR_CONNECT;
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:
gRfu.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);
2021-10-07 20:55:15 +02:00
RfuSetErrorParams(msg);
gRfu.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:
2021-10-07 20:55:15 +02:00
RfuSetErrorParams(msg);
2020-06-09 00:16:57 +02:00
RfuSetStatus(RFU_STATUS_FATAL_ERROR, msg);
2021-10-07 20:55:15 +02:00
gRfu.parentFinished = FALSE;
2020-02-16 19:49:36 +01:00
break;
}
}
void RfuSetNormalDisconnectMode(void)
2020-02-16 19:49:36 +01:00
{
gRfu.disconnectMode = RFU_DISCONNECT_NORMAL;
2020-02-16 19:49:36 +01:00
}
2021-10-07 20:55:15 +02:00
void RfuSetStatus(u8 status, u16 errorInfo)
2020-02-16 19:49:36 +01:00
{
gRfu.status = status;
2021-10-07 20:55:15 +02:00
gRfu.errorInfo = errorInfo;
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
{
return gRfu.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
{
2022-11-09 20:29:57 +01:00
u32 status = RfuGetStatus();
if (status == RFU_STATUS_FATAL_ERROR || status == RFU_STATUS_CONNECTION_ERROR)
2020-02-16 19:49:36 +01:00
return TRUE;
else
return FALSE;
}
2021-10-07 20:55:15 +02:00
bool32 Rfu_IsPlayerExchangeActive(void)
2020-02-16 19:49:36 +01:00
{
2021-10-07 20:55:15 +02:00
return gRfu.playerExchangeActive;
2020-02-16 19:49:36 +01:00
}
bool8 Rfu_IsMaster(void)
{
return gRfu.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));
}
static void VBlank_RfuIdle(void)
2020-02-16 19:49:36 +01:00
{
LoadOam();
ProcessSpriteCopyRequests();
TransferPlttBuffer();
}
// Unused
static void Debug_RfuIdle(void)
2020-02-16 19:49:36 +01:00
{
s32 i;
ResetSpriteData();
FreeAllSpritePalettes();
ResetTasks();
ResetPaletteFade();
SetVBlankCallback(VBlank_RfuIdle);
2020-02-16 19:49:36 +01:00
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 < TRAINER_ID_LENGTH; i++)
2020-02-16 19:49:36 +01:00
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();
SetMainCallback2(CB2_RfuIdle);
2020-02-16 19:49:36 +01:00
}
}
2020-05-30 10:09:21 +02:00
bool32 IsUnionRoomListenTaskActive(void)
2020-02-16 19:49:36 +01:00
{
return FuncIsActiveTask(Task_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))
gRfu.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(gRfu.idleTaskId);
2020-02-16 19:49:36 +01:00
}
static void CB2_RfuIdle(void)
2020-02-16 19:49:36 +01:00
{
RunTasks();
AnimateSprites();
BuildOamBuffer();
UpdatePaletteFade();
}
void InitializeRfuLinkManager_LinkLeader(u32 groupMax)
2020-02-16 19:49:36 +01:00
{
gRfu.parentChild = MODE_PARENT;
SetHostRfuUsername();
rfu_LMAN_initializeManager(LinkManagerCB_Parent, NULL);
2020-06-06 22:46:19 +02:00
sRfuReqConfig = sRfuReqConfigTemplate;
sRfuReqConfig.availSlot_flag = sAvailSlots[groupMax - 1];
CreateTask_ParentSearchForChildren();
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
{
gRfu.parentChild = MODE_CHILD;
SetHostRfuUsername();
2021-10-07 20:55:15 +02:00
rfu_LMAN_initializeManager(LinkManagerCB_Child, MSCCallback_Child);
CreateTask_ChildSearchForParent();
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
{
gRfu.parentChild = MODE_P_C_SWITCH;
SetHostRfuUsername();
rfu_LMAN_initializeManager(LinkManagerCB_UnionRoom, NULL);
2020-06-06 22:46:19 +02:00
sRfuReqConfig = sRfuReqConfigTemplate;
sRfuReqConfig.linkRecovery_enable = 0;
sRfuReqConfig.linkRecovery_period = 600;
gRfu.searchTaskId = CreateTask(Task_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
{
2022-11-09 20:29:57 +01:00
u16 trainerId = ReadU16(((struct RfuGameData *)gRfuLinkStatus->partner[i].gname)->compatibility.playerTrainerId);
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();
2021-10-07 20:55:15 +02:00
gRfu.parentSlots &= ~slot;
rfu_clearSlot(1, gRfu.parentSendSlot);
rfu_UNI_setSendData(gRfu.parentSlots, gRfu.recvCmds, sizeof(gRfu.recvCmds));
gRfu.parentSendSlot = Rfu_GetIndexOfNewestChild(gRfu.parentSlots);
2020-02-16 19:49:36 +01:00
}
2020-06-06 22:46:19 +02:00
void RequestDisconnectSlotByTrainerNameAndId(const u8 *name, u16 id)
2020-02-16 19:49:36 +01:00
{
2022-11-09 20:29:57 +01:00
u8 index = GetPartnerIndexByNameAndTrainerID(name, id);
if (index != 0xFF)
RfuReqDisconnectSlot(1 << index);
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
{
2021-10-07 20:55:15 +02:00
if (gRfu.linkPlayerIdx[i] == playerIdx && (gRfu.parentSlots >> 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
{
2021-10-07 20:55:15 +02:00
if (gSendCmd[0] == 0 && !gRfu.playerExchangeActive)
2020-02-16 19:49:36 +01:00
{
RfuPrepareSendBuffer(RFUCMD_DISCONNECT);
gSendCmd[1] = gTasks[taskId].tDisconnectPlayers;
gSendCmd[2] = gTasks[taskId].tDisconnectMode;
gRfu.playerCount -= sPlayerBitsToCount[gTasks[taskId].tDisconnectPlayers];
gSendCmd[3] = gRfu.playerCount;
2020-02-16 19:49:36 +01:00
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
{
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)
{
2021-10-07 20:55:15 +02:00
gRfu.reconnectParentId = id;
if (TryReconnectParent())
2020-02-16 19:49:36 +01:00
DestroyTask(taskId);
}
2021-10-07 22:10:35 +02:00
else if (GetHostRfuGameData()->activity == ACTIVITY_WONDER_CARD
|| GetHostRfuGameData()->activity == ACTIVITY_WONDER_NEWS)
2020-02-16 19:49:36 +01:00
{
tTime++;
2020-02-16 19:49:36 +01:00
}
else
{
// Error, unable to reconnect to parent
2021-10-07 20:55:15 +02:00
RfuSetStatus(RFU_STATUS_CONNECTION_ERROR, F_RFU_ERROR_5 | F_RFU_ERROR_6 | F_RFU_ERROR_7);
2020-02-16 19:49:36 +01:00
DestroyTask(taskId);
}
}
else
{
tTime++;
2021-10-07 20:55:15 +02:00
gRfu.reconnectParentId = id;
2020-02-16 19:49:36 +01:00
}
}
else
{
tTime++;
2020-02-16 19:49:36 +01:00
}
if (tTime > 240)
2020-02-16 19:49:36 +01:00
{
// Timeout error
2021-10-07 20:55:15 +02:00
RfuSetStatus(RFU_STATUS_CONNECTION_ERROR, F_RFU_ERROR_5 | F_RFU_ERROR_6 | F_RFU_ERROR_7);
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;
gRfu.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;
StringCopy((u8 *)(data), name);
2020-02-16 19:49:36 +01:00
data[8] = trainerId;
}
static bool32 IsPartnerActivityIncompatible(s16 activity, struct RfuGameData *partner)
2020-02-16 19:49:36 +01:00
{
if (GetHostRfuGameData()->activity == (ACTIVITY_CHAT | IN_UNION_ROOM))
2020-02-16 19:49:36 +01:00
{
// Host trying to chat, but partner isn't
if (partner->activity != (ACTIVITY_CHAT | IN_UNION_ROOM))
2020-02-16 19:49:36 +01:00
return TRUE;
}
else if (partner->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
{
2022-09-11 20:14:49 +02:00
// Verify that the trade offered hasn't changed
struct RfuGameData *original = &gRfu.parent;
if (original->tradeSpecies == SPECIES_EGG)
2020-02-16 19:49:36 +01:00
{
if (partner->tradeSpecies == original->tradeSpecies)
2020-02-16 19:49:36 +01:00
return FALSE;
else
return TRUE;
}
else if (partner->tradeSpecies != original->tradeSpecies
|| partner->tradeLevel != original->tradeLevel
|| partner->tradeType != original->tradeType)
2020-02-16 19:49:36 +01:00
{
return TRUE;
}
}
return FALSE;
}
#define tTime data[0]
#define tActivity data[1]
static void Task_TryConnectToUnionRoomParent(u8 taskId)
2020-02-16 19:49:36 +01:00
{
// Stop task if player is the new parent
if (gRfu.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
2021-10-07 20:55:15 +02:00
RfuSetStatus(RFU_STATUS_CONNECTION_ERROR, F_RFU_ERROR_5 | F_RFU_ERROR_6 | F_RFU_ERROR_7);
2020-02-16 19:49:36 +01:00
DestroyTask(taskId);
}
// Check if parent should be searched for
if (gRfu.parentId != 0 && lman.parent_child == MODE_CHILD)
2020-02-16 19:49:36 +01:00
{
// Search for parent
u16 trainerId = ReadU16(gRfu.parent.compatibility.playerTrainerId);
u8 id = GetPartnerIndexByNameAndTrainerID(gRfu.parentName, trainerId);
2020-02-16 19:49:36 +01:00
if (id != 0xFF)
{
// Parent found, try to connect
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
{
// Succesfully connected to parent
gRfu.state = RFUSTATE_CONNECTED;
2020-02-16 19:49:36 +01:00
DestroyTask(taskId);
}
}
else
{
// Incompatible partner activity
2021-10-07 20:55:15 +02:00
RfuSetStatus(RFU_STATUS_CONNECTION_ERROR, F_RFU_ERROR_5 | F_RFU_ERROR_6 | F_RFU_ERROR_7);
2020-02-16 19:49:36 +01:00
DestroyTask(taskId);
}
}
}
}
void TryConnectToUnionRoomParent(const u8 *name, struct RfuGameData *parent, u8 activity)
2020-02-16 19:49:36 +01:00
{
u8 taskId, listenTaskId;
2020-02-16 19:49:36 +01:00
gRfu.connectParentFailures = 0;
gRfu.status = RFU_STATUS_OK;
StringCopy(gRfu.parentName, name);
memcpy(&gRfu.parent, parent, RFU_GAME_NAME_LENGTH);
2020-02-16 19:49:36 +01:00
rfu_LMAN_forceChangeSP();
taskId = CreateTask(Task_TryConnectToUnionRoomParent, 2);
gTasks[taskId].tActivity = activity;
listenTaskId = FindTaskIdByFunc(Task_UnionRoomListen);
2020-06-06 22:46:19 +02:00
if (activity == (ACTIVITY_CHAT | IN_UNION_ROOM))
2020-02-16 19:49:36 +01:00
{
if (listenTaskId != TASK_NONE)
gTasks[listenTaskId].tConnectingForChat = TRUE;
2020-02-16 19:49:36 +01:00
}
else
{
if (listenTaskId != TASK_NONE)
gTasks[listenTaskId].tConnectingForChat = 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
{
if (gRfu.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
if ((lman.acceptSlot_flag >> i) & 1 && gRfu.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);
if (gRfu.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);
2022-07-29 16:52:35 +02:00
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
{
2021-10-07 22:19:02 +02:00
for (j = 0; j < COMM_SLOT_LENGTH; j++)
Debug_PrintNum(gRfu.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
}
Debug_PrintNum(gRfuLinkStatus->partner[gRfu.childSlot].serialNo, 1, 3, 4);
2022-07-29 16:52:35 +02:00
Debug_PrintString((void *)gRfuLinkStatus->partner[gRfu.childSlot].gname, 6, 3);
Debug_PrintString(gRfuLinkStatus->partner[gRfu.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
{
return gRfu.sendQueue.count;
2020-02-16 19:49:36 +01:00
}
u32 GetRfuRecvQueueLength(void)
{
return gRfu.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
{
}