-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
WII_IPC_HLE_Device_usb.cpp
1881 lines (1490 loc) · 66.5 KB
/
WII_IPC_HLE_Device_usb.cpp
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
// Copyright (C) 2003 Dolphin Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/
#include "../Core.h"
#include "../Debugger/Debugger_SymbolMap.h"
#include "../Host.h"
#include "../HW/SystemTimers.h"
#include "../HW/Wiimote.h"
#include "../HW/WII_IPC.h"
#include "WII_IPC_HLE.h"
#include "WII_IPC_HLE_Device_usb.h"
#include "../ConfigManager.h"
#include "../Movie.h"
#include "CoreTiming.h"
// The device class
CWII_IPC_HLE_Device_usb_oh1_57e_305::CWII_IPC_HLE_Device_usb_oh1_57e_305(u32 _DeviceID, const std::string& _rDeviceName)
: IWII_IPC_HLE_Device(_DeviceID, _rDeviceName)
, m_ScanEnable(0)
, m_HCIEndpoint(0)
, m_ACLEndpoint(0)
, m_last_ticks(0)
{
// Activate only first Wiimote by default
_conf_pads BT_DINF;
if (!SConfig::GetInstance().m_SYSCONF->GetArrayData("BT.DINF", (u8*)&BT_DINF, sizeof(_conf_pads)))
{
PanicAlertT("Trying to read from invalid SYSCONF\nWiimote bt ids are not available");
}
else
{
u8 maxWM = min<u8>(BT_DINF.num_registered, CONF_PAD_MAX_ACTIVE);
bdaddr_t tmpBD = BDADDR_ANY;
u8 i = 0;
while (i < maxWM)
{
tmpBD.b[5] = BT_DINF.active[i].bdaddr[0] = BT_DINF.registered[i].bdaddr[0];
tmpBD.b[4] = BT_DINF.active[i].bdaddr[1] = BT_DINF.registered[i].bdaddr[1];
tmpBD.b[3] = BT_DINF.active[i].bdaddr[2] = BT_DINF.registered[i].bdaddr[2];
tmpBD.b[2] = BT_DINF.active[i].bdaddr[3] = BT_DINF.registered[i].bdaddr[3];
tmpBD.b[1] = BT_DINF.active[i].bdaddr[4] = BT_DINF.registered[i].bdaddr[4];
tmpBD.b[0] = BT_DINF.active[i].bdaddr[5] = BT_DINF.registered[i].bdaddr[5];
INFO_LOG(WII_IPC_WIIMOTE, "Wiimote %d BT ID %x,%x,%x,%x,%x,%x", i, tmpBD.b[0], tmpBD.b[1], tmpBD.b[2], tmpBD.b[3], tmpBD.b[4], tmpBD.b[5]);
m_WiiMotes.push_back(CWII_IPC_HLE_WiiMote(this, i, tmpBD, false));
i++;
}
while (i < CONF_PAD_MAX_ACTIVE)
{
const char * wmName = "Nintendo RVL-CNT-01";
++BT_DINF.num_registered;
BT_DINF.active[i].bdaddr[0] = BT_DINF.registered[i].bdaddr[0] = tmpBD.b[5] = i;
BT_DINF.active[i].bdaddr[1] = BT_DINF.registered[i].bdaddr[1] = tmpBD.b[4] = 0;
BT_DINF.active[i].bdaddr[2] = BT_DINF.registered[i].bdaddr[2] = tmpBD.b[3] = 0x79;
BT_DINF.active[i].bdaddr[3] = BT_DINF.registered[i].bdaddr[3] = tmpBD.b[2] = 0x19;
BT_DINF.active[i].bdaddr[4] = BT_DINF.registered[i].bdaddr[4] = tmpBD.b[1] = 2;
BT_DINF.active[i].bdaddr[5] = BT_DINF.registered[i].bdaddr[5] = tmpBD.b[0] = 0x11;
memcpy(BT_DINF.registered[i].name, wmName, 20);
INFO_LOG(WII_IPC_WIIMOTE, "Adding to SYSConf Wiimote %d BT ID %x,%x,%x,%x,%x,%x", i, tmpBD.b[0], tmpBD.b[1], tmpBD.b[2], tmpBD.b[3], tmpBD.b[4], tmpBD.b[5]);
m_WiiMotes.push_back(CWII_IPC_HLE_WiiMote(this, i, tmpBD, false));
i++;
}
// save now so that when games load sysconf file it includes the new wiimotes
// and the correct order for connected wiimotes
if (!SConfig::GetInstance().m_SYSCONF->SetArrayData("BT.DINF", (u8*)&BT_DINF, sizeof(_conf_pads)) || !SConfig::GetInstance().m_SYSCONF->Save())
PanicAlertT("Failed to write BT.DINF to SYSCONF");
}
// The BCM2045's btaddr:
m_ControllerBD.b[0] = 0x11;
m_ControllerBD.b[1] = 0x02;
m_ControllerBD.b[2] = 0x19;
m_ControllerBD.b[3] = 0x79;
m_ControllerBD.b[4] = 0x00;
m_ControllerBD.b[5] = 0xFF;
memset(m_PacketCount, 0, sizeof(m_PacketCount));
Host_SetWiiMoteConnectionState(0);
}
CWII_IPC_HLE_Device_usb_oh1_57e_305::~CWII_IPC_HLE_Device_usb_oh1_57e_305()
{
m_WiiMotes.clear();
}
void CWII_IPC_HLE_Device_usb_oh1_57e_305::DoState(PointerWrap &p)
{
p.Do(m_Active);
p.Do(m_ControllerBD);
p.Do(m_CtrlSetup);
p.Do(m_ACLSetup);
p.Do(m_HCIEndpoint);
p.Do(m_ACLEndpoint);
p.Do(m_last_ticks);
p.DoArray(m_PacketCount,4);
p.Do(m_ScanEnable);
p.Do(m_EventQueue);
m_acl_pool.DoState(p);
for (unsigned int i = 0; i < 4; i++)
m_WiiMotes[i].DoState(p);
// Reset the connection of real and hybrid wiimotes
if (p.GetMode() == PointerWrap::MODE_READ && SConfig::GetInstance().m_WiimoteReconnectOnLoad)
{
for (unsigned int i = 0; i < 4; i++)
{
if (WIIMOTE_SRC_EMU == g_wiimote_sources[i] || WIIMOTE_SRC_NONE == g_wiimote_sources[i])
continue;
// TODO: Selectively clear real wiimote messages if possible. Or create a real wiimote channel and reporting mode pre-setup to vacate the need for m_WiimoteReconnectOnLoad.
m_EventQueue.clear();
if (!m_WiiMotes[i].IsInactive())
{
m_WiiMotes[i].Activate(false);
m_WiiMotes[i].Activate(true);
}
else
m_WiiMotes[i].Activate(false);
}
}
}
bool CWII_IPC_HLE_Device_usb_oh1_57e_305::RemoteDisconnect(u16 _connectionHandle)
{
return SendEventDisconnect(_connectionHandle, 0x13);
}
bool CWII_IPC_HLE_Device_usb_oh1_57e_305::Open(u32 _CommandAddress, u32 _Mode)
{
m_ScanEnable = 0;
m_last_ticks = 0;
memset(m_PacketCount, 0, sizeof(m_PacketCount));
m_HCIEndpoint.m_address = 0;
m_ACLEndpoint.m_address = 0;
Memory::Write_U32(GetDeviceID(), _CommandAddress + 4);
m_Active = true;
return true;
}
bool CWII_IPC_HLE_Device_usb_oh1_57e_305::Close(u32 _CommandAddress, bool _bForce)
{
m_ScanEnable = 0;
m_last_ticks = 0;
memset(m_PacketCount, 0, sizeof(m_PacketCount));
m_HCIEndpoint.m_address = 0;
m_ACLEndpoint.m_address = 0;
if (!_bForce)
Memory::Write_U32(0, _CommandAddress + 4);
m_Active = false;
return true;
}
bool CWII_IPC_HLE_Device_usb_oh1_57e_305::IOCtl(u32 _CommandAddress)
{
//ERROR_LOG(WII_IPC_WIIMOTE, "passing ioctl to ioctlv");
return IOCtlV(_CommandAddress); //hack
}
bool CWII_IPC_HLE_Device_usb_oh1_57e_305::IOCtlV(u32 _CommandAddress)
{
/*
Memory::Write_U8(255, 0x80149950); // BTM LOG // 3 logs L2Cap // 4 logs l2_csm$
Memory::Write_U8(255, 0x80149949); // Security Manager
Memory::Write_U8(255, 0x80149048); // HID
Memory::Write_U8(3, 0x80152058); // low ?? // >= 4 and you will get a lot of event messages of the same type
Memory::Write_U8(1, 0x80152018); // WUD
Memory::Write_U8(1, 0x80151FC8); // DEBUGPrint
Memory::Write_U8(1, 0x80151488); // WPAD_LOG
Memory::Write_U8(1, 0x801514A8); // USB_LOG
Memory::Write_U8(1, 0x801514D8); // WUD_DEBUGPrint
Memory::Write_U8(1, 0x80148E09); // HID LOG
*/
bool _SendReply = false;
SIOCtlVBuffer CommandBuffer(_CommandAddress);
switch (CommandBuffer.Parameter)
{
case USBV0_IOCTL_CTRLMSG: // HCI command is received from the stack
{
// This is the HCI datapath from CPU to Wiimote, the USB stuff is little endian..
m_CtrlSetup.bRequestType = *( u8*)Memory::GetPointer(CommandBuffer.InBuffer[0].m_Address);
m_CtrlSetup.bRequest = *( u8*)Memory::GetPointer(CommandBuffer.InBuffer[1].m_Address);
m_CtrlSetup.wValue = *(u16*)Memory::GetPointer(CommandBuffer.InBuffer[2].m_Address);
m_CtrlSetup.wIndex = *(u16*)Memory::GetPointer(CommandBuffer.InBuffer[3].m_Address);
m_CtrlSetup.wLength = *(u16*)Memory::GetPointer(CommandBuffer.InBuffer[4].m_Address);
m_CtrlSetup.m_PayLoadAddr = CommandBuffer.PayloadBuffer[0].m_Address;
m_CtrlSetup.m_PayLoadSize = CommandBuffer.PayloadBuffer[0].m_Size;
m_CtrlSetup.m_Address = CommandBuffer.m_Address;
// check termination
_dbg_assert_msg_(WII_IPC_WIIMOTE, *(u8*)Memory::GetPointer(CommandBuffer.InBuffer[5].m_Address) == 0,
"WIIMOTE: Termination != 0");
#if 0 // this log can get really annoying
DEBUG_LOG(WII_IPC_WIIMOTE, "USB_IOCTL_CTRLMSG (0x%08x) - execute command", _CommandAddress);
DEBUG_LOG(WII_IPC_WIIMOTE, " bRequestType: 0x%x", m_CtrlSetup.bRequestType);
DEBUG_LOG(WII_IPC_WIIMOTE, " bRequest: 0x%x", m_CtrlSetup.bRequest);
DEBUG_LOG(WII_IPC_WIIMOTE, " wValue: 0x%x", m_CtrlSetup.wValue);
DEBUG_LOG(WII_IPC_WIIMOTE, " wIndex: 0x%x", m_CtrlSetup.wIndex);
DEBUG_LOG(WII_IPC_WIIMOTE, " wLength: 0x%x", m_CtrlSetup.wLength);
DEBUG_LOG(WII_IPC_WIIMOTE, " m_PayLoadAddr: 0x%x", m_CtrlSetup.m_PayLoadAddr);
DEBUG_LOG(WII_IPC_WIIMOTE, " m_PayLoadSize: 0x%x", m_CtrlSetup.m_PayLoadSize);
#endif
// Replies are generated inside
ExecuteHCICommandMessage(m_CtrlSetup);
}
break;
case USBV0_IOCTL_BLKMSG:
{
u8 Command = Memory::Read_U8(CommandBuffer.InBuffer[0].m_Address);
switch (Command)
{
case ACL_DATA_OUT: // ACL data is received from the stack
{
// This is the ACL datapath from CPU to Wiimote
// Here we only need to record the command address in case we need to delay the reply
m_ACLSetup = CommandBuffer.m_Address;
#if defined(_DEBUG) || defined(DEBUGFAST)
DumpAsync(CommandBuffer.BufferVector, CommandBuffer.NumberInBuffer, CommandBuffer.NumberPayloadBuffer);
#endif
CtrlBuffer BulkBuffer(_CommandAddress);
hci_acldata_hdr_t* pACLHeader = (hci_acldata_hdr_t*)Memory::GetPointer(BulkBuffer.m_buffer);
_dbg_assert_(WII_IPC_WIIMOTE, HCI_BC_FLAG(pACLHeader->con_handle) == HCI_POINT2POINT);
_dbg_assert_(WII_IPC_WIIMOTE, HCI_PB_FLAG(pACLHeader->con_handle) == HCI_PACKET_START);
SendToDevice(HCI_CON_HANDLE(pACLHeader->con_handle),
Memory::GetPointer(BulkBuffer.m_buffer + sizeof(hci_acldata_hdr_t)),
pACLHeader->length);
_SendReply = true;
}
break;
case ACL_DATA_IN: // We are given an ACL buffer to fill
{
CtrlBuffer temp(_CommandAddress);
m_ACLEndpoint = temp;
DEBUG_LOG(WII_IPC_WIIMOTE, "ACL_DATA_IN: 0x%08x ", _CommandAddress);
}
break;
default:
{
_dbg_assert_msg_(WII_IPC_WIIMOTE, 0, "Unknown USBV0_IOCTL_BLKMSG: %x", Command);
}
break;
}
}
break;
case USBV0_IOCTL_INTRMSG:
{
u8 Command = Memory::Read_U8(CommandBuffer.InBuffer[0].m_Address);
if (Command == HCI_EVENT) // We are given a HCI buffer to fill
{
CtrlBuffer temp(_CommandAddress);
m_HCIEndpoint = temp;
DEBUG_LOG(WII_IPC_WIIMOTE, "HCI_EVENT: 0x%08x ", _CommandAddress);
}
else
{
_dbg_assert_msg_(WII_IPC_WIIMOTE, 0, "Unknown USBV0_IOCTL_INTRMSG: %x", Command);
}
}
break;
default:
{
_dbg_assert_msg_(WII_IPC_WIIMOTE, 0, "Unknown CWII_IPC_HLE_Device_usb_oh1_57e_305: %x", CommandBuffer.Parameter);
DEBUG_LOG(WII_IPC_WIIMOTE, "%s - IOCtlV:", GetDeviceName().c_str());
DEBUG_LOG(WII_IPC_WIIMOTE, " Parameter: 0x%x", CommandBuffer.Parameter);
DEBUG_LOG(WII_IPC_WIIMOTE, " NumberIn: 0x%08x", CommandBuffer.NumberInBuffer);
DEBUG_LOG(WII_IPC_WIIMOTE, " NumberOut: 0x%08x", CommandBuffer.NumberPayloadBuffer);
DEBUG_LOG(WII_IPC_WIIMOTE, " BufferVector: 0x%08x", CommandBuffer.BufferVector);
DEBUG_LOG(WII_IPC_WIIMOTE, " PayloadAddr: 0x%08x", CommandBuffer.PayloadBuffer[0].m_Address);
DEBUG_LOG(WII_IPC_WIIMOTE, " PayloadSize: 0x%08x", CommandBuffer.PayloadBuffer[0].m_Size);
#if defined(_DEBUG) || defined(DEBUGFAST)
DumpAsync(CommandBuffer.BufferVector, CommandBuffer.NumberInBuffer, CommandBuffer.NumberPayloadBuffer);
#endif
}
break;
}
// write return value
Memory::Write_U32(0, _CommandAddress + 4);
return _SendReply;
}
// Here we handle the USBV0_IOCTL_BLKMSG Ioctlv
void CWII_IPC_HLE_Device_usb_oh1_57e_305::SendToDevice(u16 _ConnectionHandle, u8* _pData, u32 _Size)
{
CWII_IPC_HLE_WiiMote* pWiiMote = AccessWiiMote(_ConnectionHandle);
if (pWiiMote == NULL)
return;
INFO_LOG(WII_IPC_WIIMOTE, "Send ACL Packet to ConnectionHandle 0x%04x", _ConnectionHandle);
IncDataPacket(_ConnectionHandle);
pWiiMote->ExecuteL2capCmd(_pData, _Size);
}
// Here we send ACL packets to CPU. They will consist of header + data.
// The header is for example 07 00 41 00 which means size 0x0007 and channel 0x0041.
// ---------------------------------------------------
// AyuanX: Basically, our WII_IPC_HLE is efficient enough to send the packet immediately
// rather than enqueue it to some other memory
// But...the only exception comes from the Wiimote_Plugin
void CWII_IPC_HLE_Device_usb_oh1_57e_305::IncDataPacket(u16 _ConnectionHandle)
{
m_PacketCount[_ConnectionHandle & 0xff]++;
if (m_PacketCount[_ConnectionHandle & 0xff] > (unsigned int)m_acl_pkts_num)
{
DEBUG_LOG(WII_IPC_WIIMOTE, "ACL buffer overflow");
m_PacketCount[_ConnectionHandle & 0xff] = m_acl_pkts_num;
}
}
void CWII_IPC_HLE_Device_usb_oh1_57e_305::SendACLPacket(u16 _ConnectionHandle, u8* _pData, u32 _Size)
{
DEBUG_LOG(WII_IPC_WIIMOTE, "ACL packet from %x ready to send to stack...", _ConnectionHandle);
if (m_ACLEndpoint.IsValid() && !m_HCIEndpoint.IsValid() && m_EventQueue.empty())
{
DEBUG_LOG(WII_IPC_WIIMOTE, "ACL endpoint valid, sending packet to %08x", m_ACLEndpoint.m_address);
hci_acldata_hdr_t* pHeader = (hci_acldata_hdr_t*)Memory::GetPointer(m_ACLEndpoint.m_buffer);
pHeader->con_handle = HCI_MK_CON_HANDLE(_ConnectionHandle, HCI_PACKET_START, HCI_POINT2POINT);
pHeader->length = _Size;
// Write the packet to the buffer
memcpy((u8*)pHeader + sizeof(hci_acldata_hdr_t), _pData, pHeader->length);
m_ACLEndpoint.SetRetVal(sizeof(hci_acldata_hdr_t) + _Size);
WII_IPC_HLE_Interface::EnqReply(m_ACLEndpoint.m_address);
m_ACLEndpoint.Invalidate();
}
else
{
DEBUG_LOG(WII_IPC_WIIMOTE, "ACL endpoint not currently valid, "
"queueing(%d)...", m_acl_pool.GetWritePos());
m_acl_pool.Store(_pData, _Size, _ConnectionHandle);
}
}
// These messages are sent from the Wiimote to the game, for example RequestConnection()
// or ConnectionComplete().
//
// Our WII_IPC_HLE is so efficient that we could fill the buffer immediately
// rather than enqueue it to some other memory and this will do good for StateSave
void CWII_IPC_HLE_Device_usb_oh1_57e_305::AddEventToQueue(const SQueuedEvent& _event)
{
DEBUG_LOG(WII_IPC_WIIMOTE, "HCI event %x completed...", ((hci_event_hdr_t*)_event.m_buffer)->event);
if (m_HCIEndpoint.IsValid())
{
if (m_EventQueue.empty()) // fast path :)
{
DEBUG_LOG(WII_IPC_WIIMOTE, "HCI endpoint valid, sending packet to %08x", m_HCIEndpoint.m_address);
m_HCIEndpoint.FillBuffer(_event.m_buffer, _event.m_size);
m_HCIEndpoint.SetRetVal(_event.m_size);
// Send a reply to indicate HCI buffer is filled
WII_IPC_HLE_Interface::EnqReply(m_HCIEndpoint.m_address);
m_HCIEndpoint.Invalidate();
}
else // push new one, pop oldest
{
DEBUG_LOG(WII_IPC_WIIMOTE, "HCI endpoint not "
"currently valid, queueing(%lu)...",
(unsigned long)m_EventQueue.size());
m_EventQueue.push_back(_event);
const SQueuedEvent& event = m_EventQueue.front();
DEBUG_LOG(WII_IPC_WIIMOTE, "HCI event %x "
"being written from queue(%lu) to %08x...",
((hci_event_hdr_t*)event.m_buffer)->event,
(unsigned long)m_EventQueue.size()-1,
m_HCIEndpoint.m_address);
m_HCIEndpoint.FillBuffer(event.m_buffer, event.m_size);
m_HCIEndpoint.SetRetVal(event.m_size);
// Send a reply to indicate HCI buffer is filled
WII_IPC_HLE_Interface::EnqReply(m_HCIEndpoint.m_address);
m_HCIEndpoint.Invalidate();
m_EventQueue.pop_front();
}
}
else
{
DEBUG_LOG(WII_IPC_WIIMOTE, "HCI endpoint not currently valid, "
"queueing(%lu)...", (unsigned long)m_EventQueue.size());
m_EventQueue.push_back(_event);
}
}
u32 CWII_IPC_HLE_Device_usb_oh1_57e_305::Update()
{
bool packet_transferred = false;
// check hci queue
if (!m_EventQueue.empty() && m_HCIEndpoint.IsValid())
{
// an endpoint has become available, and we have a stored response.
const SQueuedEvent& event = m_EventQueue.front();
DEBUG_LOG(WII_IPC_WIIMOTE,
"HCI event %x being written from queue(%lu) to %08x...",
((hci_event_hdr_t*)event.m_buffer)->event,
(unsigned long)m_EventQueue.size()-1,
m_HCIEndpoint.m_address);
m_HCIEndpoint.FillBuffer(event.m_buffer, event.m_size);
m_HCIEndpoint.SetRetVal(event.m_size);
// Send a reply to indicate HCI buffer is filled
WII_IPC_HLE_Interface::EnqReply(m_HCIEndpoint.m_address);
m_HCIEndpoint.Invalidate();
m_EventQueue.pop_front();
packet_transferred = true;
}
// check acl queue
if (!m_acl_pool.IsEmpty() && m_ACLEndpoint.IsValid() && m_EventQueue.empty())
{
m_acl_pool.WriteToEndpoint(m_ACLEndpoint);
packet_transferred = true;
}
// We wait for ScanEnable to be sent from the bt stack through HCI_CMD_WRITE_SCAN_ENABLE
// before we initiate the connection.
//
// FiRES: TODO find a better way to do this
// Create ACL connection
if (m_HCIEndpoint.IsValid() && (m_ScanEnable & HCI_PAGE_SCAN_ENABLE))
{
for (unsigned int i = 0; i < m_WiiMotes.size(); i++)
{
if (m_WiiMotes[i].EventPagingChanged(m_ScanEnable))
{
Host_SetWiiMoteConnectionState(1);
SendEventRequestConnection(m_WiiMotes[i]);
}
}
}
// Link channels when connected
if (m_ACLEndpoint.IsValid())
{
for (unsigned int i = 0; i < m_WiiMotes.size(); i++)
{
if (m_WiiMotes[i].LinkChannel())
break;
}
}
// The Real Wiimote sends report every ~6.66ms (150 Hz).
// However, we don't actually reach here at dependable intervals, so we
// instead just timeslice in such a way that makes the stack think we have
// perfect "radio quality" (WPADGetRadioSensitivity) and yet still have some
// idle time.
// Somehow, Dolphin's Wiimote Speaker support requires using an update interval
// of 5ms (200 Hz) for its output to work. This increased frequency tends to
// fill the ACL queue (even) quicker than it can be processed by Dolphin,
// especially during simultaneous requests involving many (emulated) Wiimotes...
// Thus, we only use that interval when the option is enabled. See issue 4608.
const u64 interval = SystemTimers::GetTicksPerSecond() / (SConfig::GetInstance().
m_LocalCoreStartupParameter.bDisableWiimoteSpeaker ? 150 : 200);
const u64 each_wiimote_interval = interval / m_WiiMotes.size();
const u64 now = CoreTiming::GetTicks();
if (now - m_last_ticks > each_wiimote_interval)
{
static int wiimote_to_update = 0;
if (m_WiiMotes[wiimote_to_update].IsConnected())
{
NetPlay_WiimoteUpdate(wiimote_to_update);
Wiimote::Update(wiimote_to_update);
}
wiimote_to_update = (wiimote_to_update + 1) % m_WiiMotes.size();
m_last_ticks = now;
}
SendEventNumberOfCompletedPackets();
return packet_transferred;
}
void CWII_IPC_HLE_Device_usb_oh1_57e_305::ACLPool::WriteToEndpoint(CtrlBuffer& endpoint)
{
const u8 *data = m_pool + m_acl_pkt_size * m_read_ptr;
const u16 size = m_info[m_read_ptr].size;
const u16 conn_handle = m_info[m_read_ptr].conn_handle;
DEBUG_LOG(WII_IPC_WIIMOTE, "ACL packet being written from "
"queue(%d) to %08x", GetReadPos(), endpoint.m_address);
hci_acldata_hdr_t* pHeader = (hci_acldata_hdr_t*)Memory::GetPointer(endpoint.m_buffer);
pHeader->con_handle = HCI_MK_CON_HANDLE(conn_handle, HCI_PACKET_START, HCI_POINT2POINT);
pHeader->length = size;
// Write the packet to the buffer
memcpy((u8*)pHeader + sizeof(hci_acldata_hdr_t), data, pHeader->length);
endpoint.SetRetVal(sizeof(hci_acldata_hdr_t) + size);
m_read_ptr = (m_read_ptr + 1) % m_acl_pkts_num;
WII_IPC_HLE_Interface::EnqReply(endpoint.m_address);
endpoint.Invalidate();
}
bool CWII_IPC_HLE_Device_usb_oh1_57e_305::SendEventInquiryComplete()
{
SQueuedEvent Event(sizeof(SHCIEventInquiryComplete), 0);
SHCIEventInquiryComplete* pInquiryComplete = (SHCIEventInquiryComplete*)Event.m_buffer;
pInquiryComplete->EventType = HCI_EVENT_INQUIRY_COMPL;
pInquiryComplete->PayloadLength = sizeof(SHCIEventInquiryComplete) - 2;
pInquiryComplete->EventStatus = 0x00;
AddEventToQueue(Event);
INFO_LOG(WII_IPC_WIIMOTE, "Event: Inquiry complete");
return true;
}
bool CWII_IPC_HLE_Device_usb_oh1_57e_305::SendEventInquiryResponse()
{
if (m_WiiMotes.empty())
return false;
_dbg_assert_(WII_IPC_WIIMOTE, sizeof(SHCIEventInquiryResult) - 2 + (m_WiiMotes.size() * sizeof(hci_inquiry_response)) < 256);
SQueuedEvent Event(static_cast<u32>(sizeof(SHCIEventInquiryResult) + m_WiiMotes.size()*sizeof(hci_inquiry_response)), 0);
SHCIEventInquiryResult* pInquiryResult = (SHCIEventInquiryResult*)Event.m_buffer;
pInquiryResult->EventType = HCI_EVENT_INQUIRY_RESULT;
pInquiryResult->PayloadLength = (u8)(sizeof(SHCIEventInquiryResult) - 2 + (m_WiiMotes.size() * sizeof(hci_inquiry_response)));
pInquiryResult->num_responses = (u8)m_WiiMotes.size();
for (size_t i=0; i < m_WiiMotes.size(); i++)
{
if (m_WiiMotes[i].IsConnected())
continue;
u8* pBuffer = Event.m_buffer + sizeof(SHCIEventInquiryResult) + i*sizeof(hci_inquiry_response);
hci_inquiry_response* pResponse = (hci_inquiry_response*)pBuffer;
pResponse->bdaddr = m_WiiMotes[i].GetBD();
pResponse->uclass[0]= m_WiiMotes[i].GetClass()[0];
pResponse->uclass[1]= m_WiiMotes[i].GetClass()[1];
pResponse->uclass[2]= m_WiiMotes[i].GetClass()[2];
pResponse->page_scan_rep_mode = 1;
pResponse->page_scan_period_mode = 0;
pResponse->page_scan_mode = 0;
pResponse->clock_offset = 0x3818;
INFO_LOG(WII_IPC_WIIMOTE, "Event: Send Fake Inquriy of one controller");
INFO_LOG(WII_IPC_WIIMOTE, " bd: %02x:%02x:%02x:%02x:%02x:%02x",
pResponse->bdaddr.b[0], pResponse->bdaddr.b[1], pResponse->bdaddr.b[2],
pResponse->bdaddr.b[3], pResponse->bdaddr.b[4], pResponse->bdaddr.b[5]);
}
AddEventToQueue(Event);
return true;
}
bool CWII_IPC_HLE_Device_usb_oh1_57e_305::SendEventConnectionComplete(const bdaddr_t& _bd)
{
CWII_IPC_HLE_WiiMote* pWiiMote = AccessWiiMote(_bd);
if (pWiiMote == NULL)
return false;
SQueuedEvent Event(sizeof(SHCIEventConnectionComplete), 0);
SHCIEventConnectionComplete* pConnectionComplete = (SHCIEventConnectionComplete*)Event.m_buffer;
pConnectionComplete->EventType = HCI_EVENT_CON_COMPL;
pConnectionComplete->PayloadLength = sizeof(SHCIEventConnectionComplete) - 2;
pConnectionComplete->EventStatus = 0x00;
pConnectionComplete->Connection_Handle = pWiiMote->GetConnectionHandle();
pConnectionComplete->bdaddr = _bd;
pConnectionComplete->LinkType = HCI_LINK_ACL;
pConnectionComplete->EncryptionEnabled = HCI_ENCRYPTION_MODE_NONE;
AddEventToQueue(Event);
CWII_IPC_HLE_WiiMote* pWiimote = AccessWiiMote(pConnectionComplete->Connection_Handle);
if (pWiimote)
pWiimote->EventConnectionAccepted();
static char s_szLinkType[][128] =
{
{ "HCI_LINK_SCO 0x00 - Voice"},
{ "HCI_LINK_ACL 0x01 - Data"},
{ "HCI_LINK_eSCO 0x02 - eSCO"},
};
INFO_LOG(WII_IPC_WIIMOTE, "Event: SendEventConnectionComplete");
INFO_LOG(WII_IPC_WIIMOTE, " Connection_Handle: 0x%04x", pConnectionComplete->Connection_Handle);
DEBUG_LOG(WII_IPC_WIIMOTE, " bd: %02x:%02x:%02x:%02x:%02x:%02x",
pConnectionComplete->bdaddr.b[0], pConnectionComplete->bdaddr.b[1], pConnectionComplete->bdaddr.b[2],
pConnectionComplete->bdaddr.b[3], pConnectionComplete->bdaddr.b[4], pConnectionComplete->bdaddr.b[5]);
DEBUG_LOG(WII_IPC_WIIMOTE, " LinkType: %s", s_szLinkType[pConnectionComplete->LinkType]);
DEBUG_LOG(WII_IPC_WIIMOTE, " EncryptionEnabled: %i", pConnectionComplete->EncryptionEnabled);
return true;
}
// This is called from Update() after ScanEnable has been enabled.
bool CWII_IPC_HLE_Device_usb_oh1_57e_305::SendEventRequestConnection(CWII_IPC_HLE_WiiMote& _rWiiMote)
{
SQueuedEvent Event(sizeof(SHCIEventRequestConnection), 0);
SHCIEventRequestConnection* pEventRequestConnection = (SHCIEventRequestConnection*)Event.m_buffer;
pEventRequestConnection->EventType = HCI_EVENT_CON_REQ;
pEventRequestConnection->PayloadLength = sizeof(SHCIEventRequestConnection) - 2;
pEventRequestConnection->bdaddr = _rWiiMote.GetBD();
pEventRequestConnection->uclass[0] = _rWiiMote.GetClass()[0];
pEventRequestConnection->uclass[1] = _rWiiMote.GetClass()[1];
pEventRequestConnection->uclass[2] = _rWiiMote.GetClass()[2];
pEventRequestConnection->LinkType = HCI_LINK_ACL;
AddEventToQueue(Event);
static char LinkType[][128] =
{
{ "HCI_LINK_SCO 0x00 - Voice"},
{ "HCI_LINK_ACL 0x01 - Data" },
{ "HCI_LINK_eSCO 0x02 - eSCO" },
};
INFO_LOG(WII_IPC_WIIMOTE, "Event: SendEventRequestConnection");
INFO_LOG(WII_IPC_WIIMOTE, " bd: %02x:%02x:%02x:%02x:%02x:%02x",
pEventRequestConnection->bdaddr.b[0], pEventRequestConnection->bdaddr.b[1], pEventRequestConnection->bdaddr.b[2],
pEventRequestConnection->bdaddr.b[3], pEventRequestConnection->bdaddr.b[4], pEventRequestConnection->bdaddr.b[5]);
DEBUG_LOG(WII_IPC_WIIMOTE, " COD[0]: 0x%02x", pEventRequestConnection->uclass[0]);
DEBUG_LOG(WII_IPC_WIIMOTE, " COD[1]: 0x%02x", pEventRequestConnection->uclass[1]);
DEBUG_LOG(WII_IPC_WIIMOTE, " COD[2]: 0x%02x", pEventRequestConnection->uclass[2]);
DEBUG_LOG(WII_IPC_WIIMOTE, " LinkType: %s", LinkType[pEventRequestConnection->LinkType]);
return true;
}
bool CWII_IPC_HLE_Device_usb_oh1_57e_305::SendEventDisconnect(u16 _connectionHandle, u8 _Reason)
{
CWII_IPC_HLE_WiiMote* pWiiMote = AccessWiiMote(_connectionHandle);
if (pWiiMote == NULL)
return false;
SQueuedEvent Event(sizeof(SHCIEventDisconnectCompleted), _connectionHandle);
SHCIEventDisconnectCompleted* pDisconnect = (SHCIEventDisconnectCompleted*)Event.m_buffer;
pDisconnect->EventType = HCI_EVENT_DISCON_COMPL;
pDisconnect->PayloadLength = sizeof(SHCIEventDisconnectCompleted) - 2;
pDisconnect->EventStatus = 0;
pDisconnect->Connection_Handle = _connectionHandle;
pDisconnect->Reason = _Reason;
AddEventToQueue(Event);
INFO_LOG(WII_IPC_WIIMOTE, "Event: SendEventDisconnect");
INFO_LOG(WII_IPC_WIIMOTE, " Connection_Handle: 0x%04x", pDisconnect->Connection_Handle);
INFO_LOG(WII_IPC_WIIMOTE, " Reason: 0x%02x", pDisconnect->Reason);
return true;
}
bool CWII_IPC_HLE_Device_usb_oh1_57e_305::SendEventAuthenticationCompleted(u16 _connectionHandle)
{
CWII_IPC_HLE_WiiMote* pWiiMote = AccessWiiMote(_connectionHandle);
if (pWiiMote == NULL)
return false;
SQueuedEvent Event(sizeof(SHCIEventAuthenticationCompleted), _connectionHandle);
SHCIEventAuthenticationCompleted* pEventAuthenticationCompleted = (SHCIEventAuthenticationCompleted*)Event.m_buffer;
pEventAuthenticationCompleted->EventType = HCI_EVENT_AUTH_COMPL;
pEventAuthenticationCompleted->PayloadLength = sizeof(SHCIEventAuthenticationCompleted) - 2;
pEventAuthenticationCompleted->EventStatus = 0;
pEventAuthenticationCompleted->Connection_Handle = _connectionHandle;
INFO_LOG(WII_IPC_WIIMOTE, "Event: SendEventAuthenticationCompleted");
INFO_LOG(WII_IPC_WIIMOTE, " Connection_Handle: 0x%04x", pEventAuthenticationCompleted->Connection_Handle);
AddEventToQueue(Event);
return true;
}
bool CWII_IPC_HLE_Device_usb_oh1_57e_305::SendEventRemoteNameReq(const bdaddr_t& _bd)
{
CWII_IPC_HLE_WiiMote* pWiiMote = AccessWiiMote(_bd);
if (pWiiMote == NULL)
return false;
SQueuedEvent Event(sizeof(SHCIEventRemoteNameReq), 0);
SHCIEventRemoteNameReq* pRemoteNameReq = (SHCIEventRemoteNameReq*)Event.m_buffer;
pRemoteNameReq->EventType = HCI_EVENT_REMOTE_NAME_REQ_COMPL;
pRemoteNameReq->PayloadLength = sizeof(SHCIEventRemoteNameReq) - 2;
pRemoteNameReq->EventStatus = 0x00;
pRemoteNameReq->bdaddr = _bd;
strcpy((char*)pRemoteNameReq->RemoteName, pWiiMote->GetName());
INFO_LOG(WII_IPC_WIIMOTE, "Event: SendEventRemoteNameReq");
INFO_LOG(WII_IPC_WIIMOTE, " bd: %02x:%02x:%02x:%02x:%02x:%02x",
pRemoteNameReq->bdaddr.b[0], pRemoteNameReq->bdaddr.b[1], pRemoteNameReq->bdaddr.b[2],
pRemoteNameReq->bdaddr.b[3], pRemoteNameReq->bdaddr.b[4], pRemoteNameReq->bdaddr.b[5]);
DEBUG_LOG(WII_IPC_WIIMOTE, " RemoteName: %s", pRemoteNameReq->RemoteName);
AddEventToQueue(Event);
return true;
}
bool CWII_IPC_HLE_Device_usb_oh1_57e_305::SendEventReadRemoteFeatures(u16 _connectionHandle)
{
CWII_IPC_HLE_WiiMote* pWiiMote = AccessWiiMote(_connectionHandle);
if (pWiiMote == NULL)
return false;
SQueuedEvent Event(sizeof(SHCIEventReadRemoteFeatures), _connectionHandle);
SHCIEventReadRemoteFeatures* pReadRemoteFeatures = (SHCIEventReadRemoteFeatures*)Event.m_buffer;
pReadRemoteFeatures->EventType = HCI_EVENT_READ_REMOTE_FEATURES_COMPL;
pReadRemoteFeatures->PayloadLength = sizeof(SHCIEventReadRemoteFeatures) - 2;
pReadRemoteFeatures->EventStatus = 0x00;
pReadRemoteFeatures->ConnectionHandle = _connectionHandle;
pReadRemoteFeatures->features[0] = pWiiMote->GetFeatures()[0];
pReadRemoteFeatures->features[1] = pWiiMote->GetFeatures()[1];
pReadRemoteFeatures->features[2] = pWiiMote->GetFeatures()[2];
pReadRemoteFeatures->features[3] = pWiiMote->GetFeatures()[3];
pReadRemoteFeatures->features[4] = pWiiMote->GetFeatures()[4];
pReadRemoteFeatures->features[5] = pWiiMote->GetFeatures()[5];
pReadRemoteFeatures->features[6] = pWiiMote->GetFeatures()[6];
pReadRemoteFeatures->features[7] = pWiiMote->GetFeatures()[7];
INFO_LOG(WII_IPC_WIIMOTE, "Event: SendEventReadRemoteFeatures");
DEBUG_LOG(WII_IPC_WIIMOTE, " Connection_Handle: 0x%04x", pReadRemoteFeatures->ConnectionHandle);
DEBUG_LOG(WII_IPC_WIIMOTE, " features: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
pReadRemoteFeatures->features[0], pReadRemoteFeatures->features[1], pReadRemoteFeatures->features[2],
pReadRemoteFeatures->features[3], pReadRemoteFeatures->features[4], pReadRemoteFeatures->features[5],
pReadRemoteFeatures->features[6], pReadRemoteFeatures->features[7]);
AddEventToQueue(Event);
return true;
}
bool CWII_IPC_HLE_Device_usb_oh1_57e_305::SendEventReadRemoteVerInfo(u16 _connectionHandle)
{
CWII_IPC_HLE_WiiMote* pWiiMote = AccessWiiMote(_connectionHandle);
if (pWiiMote == NULL)
return false;
SQueuedEvent Event(sizeof(SHCIEventReadRemoteVerInfo), _connectionHandle);
SHCIEventReadRemoteVerInfo* pReadRemoteVerInfo = (SHCIEventReadRemoteVerInfo*)Event.m_buffer;
pReadRemoteVerInfo->EventType = HCI_EVENT_READ_REMOTE_VER_INFO_COMPL;
pReadRemoteVerInfo->PayloadLength = sizeof(SHCIEventReadRemoteVerInfo) - 2;
pReadRemoteVerInfo->EventStatus = 0x00;
pReadRemoteVerInfo->ConnectionHandle = _connectionHandle;
pReadRemoteVerInfo->lmp_version = pWiiMote->GetLMPVersion();
pReadRemoteVerInfo->manufacturer = pWiiMote->GetManufactorID();
pReadRemoteVerInfo->lmp_subversion = pWiiMote->GetLMPSubVersion();
INFO_LOG(WII_IPC_WIIMOTE, "Event: SendEventReadRemoteVerInfo");
DEBUG_LOG(WII_IPC_WIIMOTE, " Connection_Handle: 0x%04x", pReadRemoteVerInfo->ConnectionHandle);
DEBUG_LOG(WII_IPC_WIIMOTE, " lmp_version: 0x%02x", pReadRemoteVerInfo->lmp_version);
DEBUG_LOG(WII_IPC_WIIMOTE, " manufacturer: 0x%04x", pReadRemoteVerInfo->manufacturer);
DEBUG_LOG(WII_IPC_WIIMOTE, " lmp_subversion: 0x%04x", pReadRemoteVerInfo->lmp_subversion);
AddEventToQueue(Event);
return true;
}
void CWII_IPC_HLE_Device_usb_oh1_57e_305::SendEventCommandComplete(u16 _OpCode, void* _pData, u32 _DataSize)
{
_dbg_assert_(WII_IPC_WIIMOTE, (sizeof(SHCIEventCommand) - 2 + _DataSize) < 256);
SQueuedEvent Event(sizeof(SHCIEventCommand) + _DataSize, 0);
SHCIEventCommand* pHCIEvent = (SHCIEventCommand*)Event.m_buffer;
pHCIEvent->EventType = HCI_EVENT_COMMAND_COMPL;
pHCIEvent->PayloadLength = (u8)(sizeof(SHCIEventCommand) - 2 + _DataSize);
pHCIEvent->PacketIndicator = 0x01;
pHCIEvent->Opcode = _OpCode;
// add the payload
if ((_pData != NULL) && (_DataSize > 0))
{
u8* pPayload = Event.m_buffer + sizeof(SHCIEventCommand);
memcpy(pPayload, _pData, _DataSize);
}
INFO_LOG(WII_IPC_WIIMOTE, "Event: Command Complete (Opcode: 0x%04x)", pHCIEvent->Opcode);
AddEventToQueue(Event);
}
bool CWII_IPC_HLE_Device_usb_oh1_57e_305::SendEventCommandStatus(u16 _Opcode)
{
SQueuedEvent Event(sizeof(SHCIEventStatus), 0);
SHCIEventStatus* pHCIEvent = (SHCIEventStatus*)Event.m_buffer;
pHCIEvent->EventType = HCI_EVENT_COMMAND_STATUS;
pHCIEvent->PayloadLength = sizeof(SHCIEventStatus) - 2;
pHCIEvent->EventStatus = 0x0;
pHCIEvent->PacketIndicator = 0x01;
pHCIEvent->Opcode = _Opcode;
INFO_LOG(WII_IPC_WIIMOTE, "Event: Command Status (Opcode: 0x%04x)", pHCIEvent->Opcode);
AddEventToQueue(Event);
return true;
}
bool CWII_IPC_HLE_Device_usb_oh1_57e_305::SendEventRoleChange(bdaddr_t _bd, bool _master)
{
CWII_IPC_HLE_WiiMote* pWiiMote = AccessWiiMote(_bd);
if (pWiiMote == NULL)
return false;
SQueuedEvent Event(sizeof(SHCIEventRoleChange), 0);
SHCIEventRoleChange* pRoleChange = (SHCIEventRoleChange*)Event.m_buffer;
pRoleChange->EventType = HCI_EVENT_ROLE_CHANGE;
pRoleChange->PayloadLength = sizeof(SHCIEventRoleChange) - 2;
pRoleChange->EventStatus = 0x00;
pRoleChange->bdaddr = _bd;
pRoleChange->NewRole = _master ? 0x00 : 0x01;
AddEventToQueue(Event);
INFO_LOG(WII_IPC_WIIMOTE, "Event: SendEventRoleChange");
DEBUG_LOG(WII_IPC_WIIMOTE, " bd: %02x:%02x:%02x:%02x:%02x:%02x",
pRoleChange->bdaddr.b[0], pRoleChange->bdaddr.b[1], pRoleChange->bdaddr.b[2],
pRoleChange->bdaddr.b[3], pRoleChange->bdaddr.b[4], pRoleChange->bdaddr.b[5]);
DEBUG_LOG(WII_IPC_WIIMOTE, " NewRole: %i", pRoleChange->NewRole);
return true;
}
bool CWII_IPC_HLE_Device_usb_oh1_57e_305::SendEventNumberOfCompletedPackets()
{
SQueuedEvent Event(sizeof(hci_event_hdr_t) + sizeof(hci_num_compl_pkts_ep) + sizeof(hci_num_compl_pkts_info) * m_WiiMotes.size(), 0);
INFO_LOG(WII_IPC_WIIMOTE, "Event: SendEventNumberOfCompletedPackets");
hci_event_hdr_t* event_hdr = (hci_event_hdr_t*)Event.m_buffer;
hci_num_compl_pkts_ep* event = (hci_num_compl_pkts_ep*)((u8*)event_hdr + sizeof(hci_event_hdr_t));
hci_num_compl_pkts_info* info = (hci_num_compl_pkts_info*)((u8*)event + sizeof(hci_num_compl_pkts_ep));
event_hdr->event = HCI_EVENT_NUM_COMPL_PKTS;
event_hdr->length = sizeof(hci_num_compl_pkts_ep);
event->num_con_handles = 0;
u32 acc = 0;
for (unsigned int i = 0; i < m_WiiMotes.size(); i++)
{
event_hdr->length += sizeof(hci_num_compl_pkts_info);
event->num_con_handles++;
info->compl_pkts = m_PacketCount[i];
info->con_handle = m_WiiMotes[i].GetConnectionHandle();
DEBUG_LOG(WII_IPC_WIIMOTE, " Connection_Handle: 0x%04x", info->con_handle);
DEBUG_LOG(WII_IPC_WIIMOTE, " Number_Of_Completed_Packets: %i", info->compl_pkts);
acc += info->compl_pkts;
m_PacketCount[i] = 0;
info++;
}
if (acc)
AddEventToQueue(Event);
else
{
INFO_LOG(WII_IPC_WIIMOTE, "SendEventNumberOfCompletedPackets: no packets; no event");
}
return true;
}
bool CWII_IPC_HLE_Device_usb_oh1_57e_305::SendEventModeChange(u16 _connectionHandle, u8 _mode, u16 _value)
{
CWII_IPC_HLE_WiiMote* pWiiMote = AccessWiiMote(_connectionHandle);
if (pWiiMote == NULL)
return false;
SQueuedEvent Event(sizeof(SHCIEventModeChange), _connectionHandle);
SHCIEventModeChange* pModeChange = (SHCIEventModeChange*)Event.m_buffer;
pModeChange->EventType = HCI_EVENT_MODE_CHANGE;
pModeChange->PayloadLength = sizeof(SHCIEventModeChange) - 2;
pModeChange->EventStatus = 0;
pModeChange->Connection_Handle = _connectionHandle;
pModeChange->CurrentMode = _mode;
pModeChange->Value = _value;
INFO_LOG(WII_IPC_WIIMOTE, "Event: SendEventModeChange");
DEBUG_LOG(WII_IPC_WIIMOTE, " Connection_Handle: 0x%04x", pModeChange->Connection_Handle);
DEBUG_LOG(WII_IPC_WIIMOTE, " Current Mode: 0x%02x", pModeChange->CurrentMode = _mode);
AddEventToQueue(Event);
return true;
}
bool CWII_IPC_HLE_Device_usb_oh1_57e_305::SendEventLinkKeyNotification(const u8 num_to_send)
{
u8 payload_length = sizeof(hci_return_link_keys_ep) + sizeof(hci_link_key_rep_cp) * num_to_send;
SQueuedEvent Event(2 + payload_length, 0);
SHCIEventLinkKeyNotification* pEventLinkKey = (SHCIEventLinkKeyNotification*)Event.m_buffer;
INFO_LOG(WII_IPC_WIIMOTE, "Event: SendEventLinkKeyNotification");
// event header
pEventLinkKey->EventType = HCI_EVENT_RETURN_LINK_KEYS;
pEventLinkKey->PayloadLength = payload_length;
// this is really hci_return_link_keys_ep.num_keys
pEventLinkKey->numKeys = num_to_send;
// copy infos - this only works correctly if we're meant to start at first device and read all keys
for (int i = 0; i < num_to_send; i++)
{
hci_link_key_rep_cp* link_key_info
= (hci_link_key_rep_cp*)((u8*)&pEventLinkKey->bdaddr + sizeof(hci_link_key_rep_cp) * i);
link_key_info->bdaddr = m_WiiMotes[i].GetBD();
memcpy(link_key_info->key, m_WiiMotes[i].GetLinkKey(), HCI_KEY_SIZE);
DEBUG_LOG(WII_IPC_WIIMOTE, " bd: %02x:%02x:%02x:%02x:%02x:%02x",
link_key_info->bdaddr.b[0], link_key_info->bdaddr.b[1], link_key_info->bdaddr.b[2],
link_key_info->bdaddr.b[3], link_key_info->bdaddr.b[4], link_key_info->bdaddr.b[5]);
LOG_LinkKey(link_key_info->key);
}
AddEventToQueue(Event);
return true;
};
bool CWII_IPC_HLE_Device_usb_oh1_57e_305::SendEventRequestLinkKey(const bdaddr_t& _bd)
{
SQueuedEvent Event(sizeof(SHCIEventRequestLinkKey), 0);
SHCIEventRequestLinkKey* pEventRequestLinkKey = (SHCIEventRequestLinkKey*)Event.m_buffer;
pEventRequestLinkKey->EventType = HCI_EVENT_LINK_KEY_REQ;
pEventRequestLinkKey->PayloadLength = sizeof(SHCIEventRequestLinkKey) - 2;
pEventRequestLinkKey->bdaddr = _bd;
INFO_LOG(WII_IPC_WIIMOTE, "Event: SendEventRequestLinkKey");
DEBUG_LOG(WII_IPC_WIIMOTE, " bd: %02x:%02x:%02x:%02x:%02x:%02x",
pEventRequestLinkKey->bdaddr.b[0], pEventRequestLinkKey->bdaddr.b[1], pEventRequestLinkKey->bdaddr.b[2],
pEventRequestLinkKey->bdaddr.b[3], pEventRequestLinkKey->bdaddr.b[4], pEventRequestLinkKey->bdaddr.b[5]);