-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathlink_rfu_2.c
3011 lines (2773 loc) · 85 KB
/
link_rfu_2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#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"
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,
RFUSTATE_RECONNECTED,
RFUSTATE_CONNECTED,
RFUSTATE_CHILD_TRY_JOIN,
RFUSTATE_CHILD_JOINED,
RFUSTATE_UR_PLAYER_EXCHANGE,
RFUSTATE_UR_STOP_MANAGER,
RFUSTATE_UR_STOP_MANAGER_END,
RFUSTATE_UR_FINALIZE,
};
// These states are used for different purposes
// depending on the link mode (parent, child, union room)
#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
// States for the 'receiving' field of RfuBlockSend
enum {
RECV_STATE_READY,
RECV_STATE_RECEIVING,
RECV_STATE_FINISHED,
};
struct SioInfo
{
char magic[sizeof("PokemonSioInfo")];
u8 playerCount;
u8 linkPlayerIdx[RFU_CHILD_MAX];
struct LinkPlayer linkPlayers[MAX_RFU_PLAYERS];
u8 filler[92];
};
// Struct is mostly empty, presumably because usage of
// its fields was largely removed before release
struct RfuDebug
{
u8 unused0[6];
u16 recvCount;
u8 unused1[6];
vu8 unkFlag;
u8 childJoinCount;
u8 unused2[84];
u16 blockSendFailures;
u8 unused3[29];
u8 blockSendTime;
u8 unused4[88];
};
COMMON_DATA u32 gRfuAPIBuffer[RFU_API_BUFF_SIZE_RAM / 4] = {0};
COMMON_DATA struct RfuManager gRfu = {0};
static u8 sHeldKeyCount;
static u8 sResendBlock8[CMD_LENGTH * 2];
static u16 sResendBlock16[CMD_LENGTH];
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 = {};
static void ResetSendDataManager(struct RfuBlockSend *);
static void InitChildRecvBuffers(void);
static void InitParentSendData(void);
static void MSCCallback_Child(u16);
static void MSCCallback_Parent(u16);
static void UpdateBackupQueue(void);
static void Task_PlayerExchange(u8);
static void Task_PlayerExchangeUpdate(u8);
static void Task_PlayerExchangeChat(u8);
static void RfuHandleReceiveCommand(u8);
static void CallRfuFunc(void);
static void RfuPrepareSendBuffer(u16);
static void HandleBlockSend(void);
static void SendNextBlock(void);
static void SendLastBlock(void);
static u8 GetPartnerIndexByNameAndTrainerID(const u8 *, u16);
static void UpdateChildStatuses(void);
static s32 GetJoinGroupStatus(void);
static void ClearSelectedLinkPlayerIds(u16);
static void ValidateAndReceivePokemonSioInfo(void *);
static void ParentResetChildRecvMetadata(s32);
static void CB2_RfuIdle(void);
static void RfuReqDisconnectSlot(u32);
static void SendDisconnectCommand(u32, u32);
static void Task_TryConnectToUnionRoomParent(u8);
static void Debug_PrintEmpty(void);
static void Task_Idle(u8);
static const INIT_PARAM sRfuReqConfigTemplate = {
.maxMFrame = 4,
.MC_TimerCount = 32,
.availSlot_flag = 0,
.mboot_flag = 0,
.serialNo = RFU_SERIAL_GAME,
.gameName = (void *)&gHostRfuGameData,
.userName = gHostRfuUsername,
.fastSearchParent_flag = TRUE,
.linkRecovery_enable = FALSE,
.linkRecovery_period = 600,
.NI_failCounter_limit = 300
};
static const u8 sAvailSlots[] = {
[1] = AVAIL_SLOT1,
[2] = AVAIL_SLOT2,
[3] = AVAIL_SLOT3,
[4] = AVAIL_SLOT4
};
#define BLOCK_MASK(bitNum) ((1 << (bitNum)) - 1)
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),
};
#undef BLOCK_MASK
static const u8 sSlotToLinkPlayerTableId[] = {
0, 0, 1,
1, 2, 2,
2, 2, 3
};
// 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
};
// 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
};
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 }
};
static const u16 sAcceptedSerialNos[] = {
RFU_SERIAL_GAME,
RFU_SERIAL_WONDER_DISTRIBUTOR,
RFU_SERIAL_UNKNOWN,
RFU_SERIAL_END
};
static const char sASCII_RfuCmds[][15] = {
"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"
};
static const char sASCII_RecoverCmds[][16] = {
" ",
"RECOVER START ",
"DISSCONECT ",
"RECOVER SUUSES",
"RECOVER FAILED"
};
// List of additional tasks to destroy (if active) when the RFU shuts down
static const TaskFunc sShutdownTasks[] = {
Task_PlayerExchange,
Task_PlayerExchangeUpdate,
Task_PlayerExchangeChat
};
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";
static const char sASCII_ClockCmds[][12] = {
" ",
"CLOCK DRIFT",
"BUSY SEND ",
"CMD REJECT ",
"CLOCK SLAVE"
};
static const char sASCII_ChildParentSearch[][8] = {
"CHILD ",
"PARENT",
"SEARCH"
};
static void Debug_PrintString(const void *str, u8 x, u8 y)
{
}
static void Debug_PrintNum(u16 num, u8 x, u8 y, u8 numDigits)
{
}
void ResetLinkRfuGFLayer(void)
{
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);
CpuFill16(0, gSendCmd, sizeof gSendCmd);
CpuFill16(0, gRecvCmds, sizeof gRecvCmds);
CpuFill16(0, gLinkPlayers, sizeof gLinkPlayers);
}
void InitRFU(void)
{
IntrFunc serialIntr = gIntrTable[1];
IntrFunc timerIntr = gIntrTable[2];
InitRFUAPI();
rfu_REQ_stopMode();
rfu_waitREQComplete();
REG_IME = 0;
gIntrTable[1] = serialIntr;
gIntrTable[2] = timerIntr;
REG_IME = INTR_FLAG_VBLANK;
}
void InitRFUAPI(void)
{
if (!rfu_initializeAPI((void *)gRfuAPIBuffer, sizeof(gRfuAPIBuffer), &gIntrTable[1], TRUE))
{
gLinkType = 0;
ClearSavedLinkPlayers();
RfuSetIgnoreError(FALSE);
ResetLinkRfuGFLayer();
rfu_setTimerInterrupt(3, &gIntrTable[2]);
}
}
static void Task_ParentSearchForChildren(u8 taskId)
{
UpdateChildStatuses();
switch (gRfu.state)
{
case RFUSTATE_INIT:
rfu_LMAN_initializeRFU(&sRfuReqConfig);
gRfu.state = RFUSTATE_INIT_END;
gTasks[taskId].data[1] = 1;
break;
case RFUSTATE_INIT_END:
break;
case RFUSTATE_PARENT_CONNECT:
rfu_LMAN_establishConnection(gRfu.parentChild, 0, 240, (u16 *)sAcceptedSerialNos);
gRfu.state = RFUSTATE_PARENT_CONNECT_END;
gTasks[taskId].data[1] = 6;
break;
case RFUSTATE_PARENT_CONNECT_END:
break;
case RFUSTATE_STOP_MANAGER:
rfu_LMAN_stopManager(FALSE);
gRfu.state = RFUSTATE_STOP_MANAGER_END;
break;
case RFUSTATE_STOP_MANAGER_END:
break;
case RFUSTATE_PARENT_FINALIZE:
gRfu.parentFinished = FALSE;
rfu_LMAN_setMSCCallback(MSCCallback_Parent);
InitChildRecvBuffers();
InitParentSendData();
gRfu.state = RFUSTATE_FINALIZED;
gTasks[taskId].data[1] = 8;
CreateTask(Task_PlayerExchange, 5);
DestroyTask(taskId);
break;
}
}
s32 Rfu_GetIndexOfNewestChild(u8 bits)
{
return sPlayerBitsToNewChildIdx[bits];
}
static void SetLinkPlayerIdsFromSlots(s32 baseSlots, s32 addSlots)
{
u8 i;
u8 baseId = 1;
s32 baseSlotsCopy = baseSlots;
s32 newId = 0;
if (addSlots == -1)
{
// Initialize
for (i = 0; i < RFU_CHILD_MAX; baseSlots >>= 1, i++)
{
if (baseSlots & 1)
{
gRfu.linkPlayerIdx[i] = baseId;
baseId++;
}
}
}
else
{
// Clear id for any empty slot
for (i = 0; i < RFU_CHILD_MAX; baseSlotsCopy >>= 1, i++)
{
if (!(baseSlotsCopy & 1))
gRfu.linkPlayerIdx[i] = 0;
}
// Get starting id by checking existing slots
for (baseId = RFU_CHILD_MAX; baseId != 0; baseId--)
{
for (i = 0; i < RFU_CHILD_MAX && gRfu.linkPlayerIdx[i] != baseId; i++)
;
if (i == RFU_CHILD_MAX)
newId = baseId;
}
// Set id for new slots
for (addSlots &= ~baseSlots, i = 0; i < RFU_CHILD_MAX; addSlots >>= 1, i++)
{
if (addSlots & 1)
gRfu.linkPlayerIdx[i] = newId++;
}
}
}
static void Task_ChildSearchForParent(u8 taskId)
{
switch (gRfu.state)
{
case RFUSTATE_INIT:
rfu_LMAN_initializeRFU((INIT_PARAM *)&sRfuReqConfigTemplate);
gRfu.state = RFUSTATE_INIT_END;
gTasks[taskId].data[1] = 1;
break;
case RFUSTATE_INIT_END:
break;
case RFUSTATE_CHILD_CONNECT:
rfu_LMAN_establishConnection(gRfu.parentChild, 0, 240, (u16 *)sAcceptedSerialNos);
gRfu.state = RFUSTATE_CHILD_CONNECT_END;
gTasks[taskId].data[1] = 7;
break;
case RFUSTATE_CHILD_CONNECT_END:
break;
case RFUSTATE_RECONNECTED:
gTasks[taskId].data[1] = 10;
break;
case RFUSTATE_CHILD_TRY_JOIN:
switch (GetJoinGroupStatus())
{
case RFU_STATUS_JOIN_GROUP_OK:
gRfu.state = RFUSTATE_CHILD_JOINED;
break;
case RFU_STATUS_JOIN_GROUP_NO:
case RFU_STATUS_LEAVE_GROUP:
rfu_LMAN_requestChangeAgbClockMaster();
gRfu.disconnectMode = RFU_DISCONNECT_NORMAL;
DestroyTask(taskId);
break;
}
break;
case RFUSTATE_CHILD_JOINED:
{
u8 bmChildSlot = 1 << gRfu.childSlot;
rfu_clearSlot(TYPE_NI_SEND | TYPE_NI_RECV, gRfu.childSlot);
rfu_setRecvBuffer(TYPE_UNI, gRfu.childSlot, gRfu.childRecvQueue, sizeof(gRfu.childRecvQueue));
rfu_UNI_setSendData(bmChildSlot, gRfu.childSendBuffer, sizeof(gRfu.childSendBuffer));
gTasks[taskId].data[1] = 8;
DestroyTask(taskId);
if (sRfuDebug.childJoinCount == 0)
{
Debug_PrintEmpty();
sRfuDebug.childJoinCount++;
}
CreateTask(Task_PlayerExchange, 5);
break;
}
}
}
static void InitChildRecvBuffers(void)
{
u8 i;
u8 acceptSlot = lman.acceptSlot_flag;
for (i = 0; i < RFU_CHILD_MAX; i++)
{
if (acceptSlot & 1)
{
rfu_setRecvBuffer(TYPE_UNI, i, gRfu.childRecvBuffer[i], sizeof(gRfu.childRecvBuffer[0]));
rfu_clearSlot(TYPE_UNI_SEND | TYPE_UNI_RECV, i);
}
acceptSlot >>= 1;
}
}
static void InitParentSendData(void)
{
u8 acceptSlot = lman.acceptSlot_flag;
rfu_UNI_setSendData(acceptSlot, gRfu.recvCmds, sizeof(gRfu.recvCmds));
gRfu.parentSendSlot = Rfu_GetIndexOfNewestChild(acceptSlot);
gRfu.parentSlots = acceptSlot;
SetLinkPlayerIdsFromSlots(acceptSlot, -1);
gRfu.parentChild = MODE_PARENT;
}
#define tConnectingForChat data[7]
static void Task_UnionRoomListen(u8 taskId)
{
if (GetHostRfuGameData()->activity == (ACTIVITY_PLYRTALK | IN_UNION_ROOM) && RfuGetStatus() == RFU_STATUS_NEW_CHILD_DETECTED)
{
rfu_REQ_disconnect(lman.acceptSlot_flag);
rfu_waitREQComplete();
RfuSetStatus(RFU_STATUS_OK, 0);
}
switch (gRfu.state)
{
case RFUSTATE_INIT:
rfu_LMAN_initializeRFU(&sRfuReqConfig);
gRfu.state = RFUSTATE_INIT_END;
gTasks[taskId].data[1] = 1;
break;
case RFUSTATE_INIT_END:
break;
case RFUSTATE_UR_CONNECT:
rfu_LMAN_establishConnection(MODE_P_C_SWITCH, 0, 240, (u16 *)sAcceptedSerialNos);
rfu_LMAN_setMSCCallback(MSCCallback_Child);
gRfu.state = RFUSTATE_UR_CONNECT_END;
break;
case RFUSTATE_UR_CONNECT_END:
break;
case RFUSTATE_UR_PLAYER_EXCHANGE:
if (rfu_UNI_setSendData(1 << gRfu.childSlot, gRfu.childSendBuffer, sizeof(gRfu.childSendBuffer)) == 0)
{
gRfu.parentChild = MODE_CHILD;
DestroyTask(taskId);
if (gTasks[taskId].tConnectingForChat)
CreateTask(Task_PlayerExchangeChat, 1);
else
CreateTask(Task_PlayerExchange, 5);
}
break;
case RFUSTATE_UR_STOP_MANAGER:
rfu_LMAN_stopManager(FALSE);
gRfu.state = RFUSTATE_UR_STOP_MANAGER_END;
break;
case RFUSTATE_UR_STOP_MANAGER_END:
break;
case RFUSTATE_UR_FINALIZE:
gRfu.parentFinished = FALSE;
rfu_LMAN_setMSCCallback(MSCCallback_Parent);
UpdateGameData_GroupLockedIn(TRUE);
InitChildRecvBuffers();
InitParentSendData();
gRfu.state = RFUSTATE_FINALIZED;
gTasks[taskId].data[1] = 8;
gRfu.parentChild = MODE_PARENT;
CreateTask(Task_PlayerExchange, 5);
gRfu.playerExchangeActive = TRUE;
DestroyTask(taskId);
break;
}
}
void LinkRfu_CreateConnectionAsParent(void)
{
rfu_LMAN_establishConnection(MODE_PARENT, 0, 240, (u16 *)sAcceptedSerialNos);
}
void LinkRfu_StopManagerBeforeEnteringChat(void)
{
rfu_LMAN_stopManager(FALSE);
}
// Argument is provided by the RFU and is unused.
static void MSCCallback_Child(u16 REQ_commandID)
{
s32 i;
for (i = 0; i < COMM_SLOT_LENGTH; i++)
gRfu.childSendBuffer[i] = 0;
rfu_REQ_recvData();
rfu_waitREQComplete();
if (gRfuSlotStatusUNI[gRfu.childSlot]->recv.newDataFlag)
{
gRfu.childSendCount++;
RfuRecvQueue_Enqueue(&gRfu.recvQueue, gRfu.childRecvQueue);
sRfuDebug.recvCount++;
UpdateBackupQueue();
rfu_UNI_readySendData(gRfu.childSlot);
rfu_UNI_clearRecvNewDataFlag(gRfu.childSlot);
}
rfu_LMAN_REQ_sendData(TRUE);
}
// Argument is provided by the RFU and is unused.
static void MSCCallback_Parent(u16 REQ_commandID)
{
gRfu.parentFinished = TRUE;
}
void LinkRfu_Shutdown(void)
{
u8 i;
rfu_LMAN_powerDownRFU();
if (gRfu.parentChild == MODE_PARENT)
{
// Stop parent searching for children
if (FuncIsActiveTask(Task_ParentSearchForChildren) == TRUE)
{
DestroyTask(gRfu.searchTaskId);
ResetLinkRfuGFLayer();
}
}
else if (gRfu.parentChild == MODE_CHILD)
{
// Stop child searching for parent
if (FuncIsActiveTask(Task_ChildSearchForParent) == TRUE)
{
DestroyTask(gRfu.searchTaskId);
ResetLinkRfuGFLayer();
}
}
else if (gRfu.parentChild == MODE_P_C_SWITCH)
{
// Stop parent-child switching mode (union room)
if (FuncIsActiveTask(Task_UnionRoomListen) == TRUE)
{
DestroyTask(gRfu.searchTaskId);
ResetLinkRfuGFLayer();
}
}
// Destroy additional tasks
for (i = 0; i < ARRAY_COUNT(sShutdownTasks); i++)
{
if (FuncIsActiveTask(sShutdownTasks[i]) == TRUE)
DestroyTask(FindTaskIdByFunc(sShutdownTasks[i]));
}
}
static void CreateTask_ParentSearchForChildren(void)
{
gRfu.searchTaskId = CreateTask(Task_ParentSearchForChildren, 1);
}
// If no parent ID (or if child connection not ready) can't reconnect with parent yet
static bool8 CanTryReconnectParent(void)
{
if (gRfu.state == RFUSTATE_CHILD_CONNECT_END && gRfu.parentId)
return TRUE;
return FALSE;
}
static bool32 TryReconnectParent(void)
{
if (gRfu.state == RFUSTATE_CHILD_CONNECT_END && !rfu_LMAN_CHILD_connectParent(gRfuLinkStatus->partner[gRfu.reconnectParentId].id, 240))
{
gRfu.state = RFUSTATE_RECONNECTED;
return TRUE;
}
return FALSE;
}
static void CreateTask_ChildSearchForParent(void)
{
gRfu.searchTaskId = CreateTask(Task_ChildSearchForParent, 1);
}
bool8 LmanAcceptSlotFlagIsNotZero(void)
{
if (lman.acceptSlot_flag)
return TRUE;
return FALSE;
}
void LinkRfu_StopManagerAndFinalizeSlots(void)
{
gRfu.state = RFUSTATE_STOP_MANAGER;
gRfu.acceptSlot_flag = lman.acceptSlot_flag;
}
bool32 WaitRfuState(bool32 force)
{
if (gRfu.state == RFUSTATE_PARENT_FINALIZE_START || force)
{
gRfu.state = RFUSTATE_PARENT_FINALIZE;
return TRUE;
}
return FALSE;
}
void StopUnionRoomLinkManager(void)
{
gRfu.state = RFUSTATE_UR_STOP_MANAGER;
}
static void UNUSED ReadySendDataForSlots(u8 slots)
{
u8 i;
for (i = 0; i < RFU_CHILD_MAX; i++)
{
if (slots & 1)
{
rfu_UNI_readySendData(i);
break;
}
slots >>= 1;
}
}
static void ReadAllPlayerRecvCmds(void)
{
s32 i, j;
for (i = 0; i < MAX_RFU_PLAYERS; i++)
{
struct RfuManager *rfu = &gRfu;
for (j = 0; j < CMD_LENGTH - 1; j++)
{
rfu->recvCmds[i][j][1] = gRecvCmds[i][j] >> 8;
rfu->recvCmds[i][j][0] = gRecvCmds[i][j];
}
}
CpuFill16(0, gRecvCmds, sizeof gRecvCmds);
}
static void MoveSendCmdToRecv(void)
{
s32 i;
for (i = 0; i < CMD_LENGTH - 1; i++)
gRecvCmds[0][i] = gSendCmd[i];
for (i = 0; i < CMD_LENGTH - 1; i++)
gSendCmd[i] = 0;
}
static void UpdateBackupQueue(void)
{
if (gRfu.linkRecovered)
{
bool8 backupEmpty = RfuBackupQueue_Dequeue(&gRfu.backupQueue, gRfu.childSendBuffer);
if (gRfu.backupQueue.count == 0)
gRfu.linkRecovered = FALSE;
if (backupEmpty)
return;
}
if (!gRfu.linkRecovered)
{
RfuSendQueue_Dequeue(&gRfu.sendQueue, gRfu.childSendBuffer);
RfuBackupQueue_Enqueue(&gRfu.backupQueue, gRfu.childSendBuffer);
}
}
bool32 IsRfuRecvQueueEmpty(void)
{
s32 i;
s32 j;
if (!gRfuLinkStatus->sendSlotUNIFlag)
return FALSE;
for (i = 0; i < MAX_RFU_PLAYERS; i++)
for (j = 0; j < CMD_LENGTH - 1; j++)
if (gRecvCmds[i][j] != 0)
return FALSE;
return TRUE;
}
static bool32 RfuMain1_Parent(void)
{
if (gRfu.state < RFUSTATE_FINALIZED)
{
rfu_REQ_recvData();
rfu_waitREQComplete();
rfu_LMAN_REQ_sendData(FALSE);
}
else
{
gRfu.parentFinished = FALSE;
if ((gRfu.parentSlots & gRfuLinkStatus->connSlotFlag) == gRfu.parentSlots && (gRfu.parentSlots & gRfuLinkStatus->connSlotFlag))
{
if (!gRfu.parentMain2Failed)
{
if (gRfu.disconnectSlots)
{
RfuReqDisconnectSlot(gRfu.disconnectSlots);
gRfu.disconnectSlots = 0;
if (gRfu.disconnectMode == RFU_DISCONNECT_ERROR)
{
RfuSetStatus(RFU_STATUS_CONNECTION_ERROR, F_RFU_ERROR_8);
RfuSetErrorParams(F_RFU_ERROR_8);
return FALSE;
}
if (!lman.acceptSlot_flag)
{
LinkRfu_Shutdown();
gReceivedRemoteLinkPlayers = 0;
return FALSE;
}
}
ReadAllPlayerRecvCmds();
rfu_UNI_readySendData(gRfu.parentSendSlot);
rfu_LMAN_REQ_sendData(TRUE);
}
else
{
rfu_REQ_PARENT_resumeRetransmitAndChange();
}
gRfu.runParentMain2 = TRUE;
}
}
return FALSE;
}
static bool32 RfuMain2_Parent(void)
{
u16 i;
u16 flags;
u8 r0;
u16 j;
bool8 failed;
if (gRfu.state >= RFUSTATE_FINALIZED && gRfu.runParentMain2 == TRUE)
{
rfu_waitREQComplete();
while (gRfu.parentFinished == FALSE)
{
if (gRfu.errorState != RFU_ERROR_STATE_NONE)
return FALSE;
}
rfu_REQ_recvData();
rfu_waitREQComplete();
if ((lman.parentAck_flag & gRfu.parentSlots) == gRfu.parentSlots)
{
gRfu.parentMain2Failed = FALSE;
sRfuDebug.recvCount++;
flags = lman.acceptSlot_flag;
for (i = 0; i < RFU_CHILD_MAX; i++)
{
if (flags & 1)
{
if (gRfu.childRecvBuffer[i][1])
{
if (gRfu.childRecvIds[i] != 0xFF && (gRfu.childRecvBuffer[i][0] >> 5) != ((gRfu.childRecvIds[i] + 1) & 7))
{
if (++gRfu.numChildRecvErrors[i] > 4)
RfuSetErrorParams(F_RFU_ERROR_8 | F_RFU_ERROR_1);
}
else
{
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++)
{
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;
}
}
}
rfu_UNI_clearRecvNewDataFlag(i);
}
flags >>= 1;
}
MoveSendCmdToRecv();
RfuHandleReceiveCommand(0);
CallRfuFunc();
if (gRfu.nextChildBits && !gRfu.stopNewConnections)
{
sRfuDebug.unkFlag = FALSE;
rfu_clearSlot(TYPE_UNI_SEND | TYPE_UNI_RECV, gRfu.parentSendSlot);
for (i = 0; i < RFU_CHILD_MAX; i++)
{
if ((gRfu.nextChildBits >> i) & 1)
rfu_setRecvBuffer(TYPE_UNI, i, gRfu.childRecvBuffer[i], sizeof(gRfu.childRecvBuffer[0]));
}
SetLinkPlayerIdsFromSlots(gRfu.parentSlots, gRfu.parentSlots | gRfu.nextChildBits);
gRfu.incomingChild = gRfu.nextChildBits;
gRfu.parentSlots |= gRfu.nextChildBits;
gRfu.nextChildBits = 0;
rfu_UNI_setSendData(gRfu.parentSlots, gRfu.recvCmds, sizeof(gRfu.recvCmds));
gRfu.parentSendSlot = Rfu_GetIndexOfNewestChild(gRfu.parentSlots);
CreateTask(Task_PlayerExchangeUpdate, 0);
}
}
else
{
gRfu.parentMain2Failed = TRUE;
gRfu.runParentMain2 = FALSE;
}
gRfu.runParentMain2 = FALSE;
}
failed = gRfu.parentMain2Failed;
return gRfuLinkStatus->sendSlotUNIFlag ? failed & 1 : FALSE;
}
static void ChildBuildSendCmd(u16 *sendCmd, u8 *dst)
{
s32 i;
if (sendCmd[0])
{
sendCmd[0] |= (gRfu.childSendCmdId << 5);
gRfu.childSendCmdId = (gRfu.childSendCmdId + 1) & 7;
for (i = 0; i < CMD_LENGTH - 1; i++)
{
dst[2 * i + 1] = sendCmd[i] >> 8;
dst[2 * i + 0] = sendCmd[i];
}
}
else
{
for (i = 0; i < COMM_SLOT_LENGTH; i++)
dst[i] = 0;
}
}
static bool32 RfuMain1_Child(void)
{
u8 i;
u8 j;
u8 recv[MAX_RFU_PLAYERS * (2 * (CMD_LENGTH - 1))];
u8 send[2 * (CMD_LENGTH - 1)];
u8 status;
RfuRecvQueue_Dequeue(&gRfu.recvQueue, recv);
for (i = 0; i < MAX_RFU_PLAYERS; i++)
{
for (j = 0; j < CMD_LENGTH - 1; j++)
gRecvCmds[i][j] = (recv[i * COMM_SLOT_LENGTH + (j * 2) + 1] << 8)
| recv[i * COMM_SLOT_LENGTH + (j * 2) + 0];
}
RfuHandleReceiveCommand(0);
if (lman.childClockSlave_flag == 0 && gRfu.disconnectMode != RFU_DISCONNECT_NONE)
{
rfu_REQ_disconnect(gRfuLinkStatus->connSlotFlag | gRfuLinkStatus->linkLossSlotFlag);
rfu_waitREQComplete();
status = RfuGetStatus();
if (status != RFU_STATUS_FATAL_ERROR && status != RFU_STATUS_JOIN_GROUP_NO && status != RFU_STATUS_LEAVE_GROUP)
RfuSetStatus(RFU_STATUS_CONNECTION_ERROR, F_RFU_ERROR_5 | F_RFU_ERROR_8);
rfu_clearAllSlot();
gReceivedRemoteLinkPlayers = FALSE;
gRfu.callback = NULL;
if (gRfu.disconnectMode == RFU_DISCONNECT_ERROR)
{
RfuSetStatus(RFU_STATUS_CONNECTION_ERROR, F_RFU_ERROR_5 | F_RFU_ERROR_8);
RfuSetErrorParams(F_RFU_ERROR_5 | F_RFU_ERROR_8);
}
lman.state = lman.next_state = 0;
gRfu.disconnectMode = RFU_DISCONNECT_NONE;
}
if (gRfu.childSendCount)
{
gRfu.childSendCount--;
CallRfuFunc();
ChildBuildSendCmd(gSendCmd, send);
RfuSendQueue_Enqueue(&gRfu.sendQueue, send);
for (i = 0; i < CMD_LENGTH - 1; i++)
gSendCmd[i] = 0;
}
return IsRfuRecvQueueEmpty();
}
static void HandleSendFailure(u8 unused, u32 flags)
{
s32 i, j, temp;
const u8 *payload = gRfu.sendBlock.payload;
for (i = 0; i < gRfu.sendBlock.count; i++)
{
if (!(flags & 1))
{
sResendBlock16[0] = RFUCMD_SEND_BLOCK | i;
for (j = 0; j < CMD_LENGTH - 1; j++)
{
temp = j * 2;
sResendBlock16[j + 1] = (payload[(COMM_SLOT_LENGTH - 2) * i + temp + 1] << 8)
| payload[(COMM_SLOT_LENGTH - 2) * i + temp + 0];
}
for (j = 0; j < CMD_LENGTH - 1; j++)
{
temp = j * 2;
sResendBlock8[temp + 1] = sResendBlock16[j] >> 8;
sResendBlock8[temp + 0] = sResendBlock16[j];
}
RfuSendQueue_Enqueue(&gRfu.sendQueue, sResendBlock8);
gRfu.sendBlock.failedFlags |= (1 << i);
}
flags >>= 1;