~registry/dolphin-emu/triforce

« back to all changes in this revision

Viewing changes to Source/Core/Core/Src/HW/WiimoteReal/IONix.cpp

  • Committer: Sérgio Benjamim
  • Date: 2015-02-13 05:54:40 UTC
  • Revision ID: sergio_br2@yahoo.com.br-20150213055440-ey2rt3sjpy27km78
Dolphin Triforce branch from code.google, commit b957980 (4.0-315).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (C) 2003 Dolphin Project.
 
2
 
 
3
// This program is free software: you can redistribute it and/or modify
 
4
// it under the terms of the GNU General Public License as published by
 
5
// the Free Software Foundation, version 2.0.
 
6
 
 
7
// This program is distributed in the hope that it will be useful,
 
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
// GNU General Public License 2.0 for more details.
 
11
 
 
12
// A copy of the GPL 2.0 should have been included with the program.
 
13
// If not, see http://www.gnu.org/licenses/
 
14
 
 
15
// Official SVN repository and contact information can be found at
 
16
// http://code.google.com/p/dolphin-emu/
 
17
 
 
18
#include <bluetooth/bluetooth.h>
 
19
#include <bluetooth/hci.h>
 
20
#include <bluetooth/hci_lib.h>
 
21
#include <bluetooth/l2cap.h>
 
22
 
 
23
#include "Common.h"
 
24
#include "WiimoteReal.h"
 
25
 
 
26
namespace WiimoteReal
 
27
{
 
28
 
 
29
WiimoteScanner::WiimoteScanner()
 
30
        : m_want_wiimotes()
 
31
        , device_id(-1)
 
32
        , device_sock(-1)
 
33
{
 
34
        // Get the id of the first bluetooth device.
 
35
        device_id = hci_get_route(NULL);
 
36
        if (device_id < 0)
 
37
        {
 
38
                NOTICE_LOG(WIIMOTE, "Bluetooth not found.");
 
39
                return;
 
40
        }
 
41
 
 
42
        // Create a socket to the device
 
43
        device_sock = hci_open_dev(device_id);
 
44
        if (device_sock < 0)
 
45
        {
 
46
                ERROR_LOG(WIIMOTE, "Unable to open bluetooth.");
 
47
                return;
 
48
        }
 
49
}
 
50
 
 
51
bool WiimoteScanner::IsReady() const
 
52
{
 
53
        return device_sock > 0;
 
54
}
 
55
 
 
56
WiimoteScanner::~WiimoteScanner()
 
57
{
 
58
        if (IsReady())
 
59
                close(device_sock);
 
60
}
 
61
 
 
62
void WiimoteScanner::Update()
 
63
{}
 
64
 
 
65
void WiimoteScanner::FindWiimotes(std::vector<Wiimote*> & found_wiimotes, Wiimote* & found_board)
 
66
{
 
67
        // supposedly 1.28 seconds
 
68
        int const wait_len = 1;
 
69
 
 
70
        int const max_infos = 255;
 
71
        inquiry_info scan_infos[max_infos] = {};
 
72
        auto* scan_infos_ptr = scan_infos;
 
73
        found_board = NULL;
 
74
        
 
75
        // Scan for bluetooth devices
 
76
        int const found_devices = hci_inquiry(device_id, wait_len, max_infos, NULL, &scan_infos_ptr, IREQ_CACHE_FLUSH);
 
77
        if (found_devices < 0)
 
78
        {
 
79
                ERROR_LOG(WIIMOTE, "Error searching for bluetooth devices.");
 
80
                return;
 
81
        }
 
82
 
 
83
        DEBUG_LOG(WIIMOTE, "Found %i bluetooth device(s).", found_devices);
 
84
 
 
85
        // Display discovered devices
 
86
        for (int i = 0; i < found_devices; ++i)
 
87
        {
 
88
                ERROR_LOG(WIIMOTE, "found a device...");
 
89
                
 
90
                // BT names are a maximum of 248 bytes apparently
 
91
                char name[255] = {};
 
92
                if (hci_read_remote_name(device_sock, &scan_infos[i].bdaddr, sizeof(name), name, 1000) < 0)
 
93
                {
 
94
                        ERROR_LOG(WIIMOTE, "name request failed");
 
95
                        continue;
 
96
                }
 
97
 
 
98
                ERROR_LOG(WIIMOTE, "device name %s", name);
 
99
                if (IsValidBluetoothName(name))
 
100
                {
 
101
                        bool new_wiimote = true;
 
102
 
 
103
                        // TODO: do this
 
104
 
 
105
                        // Determine if this wiimote has already been found.
 
106
                        //for (int j = 0; j < MAX_WIIMOTES && new_wiimote; ++j)
 
107
                        //{
 
108
                        //      if (wm[j] && bacmp(&scan_infos[i].bdaddr,&wm[j]->bdaddr) == 0)
 
109
                        //              new_wiimote = false;
 
110
                        //}
 
111
 
 
112
                        if (new_wiimote)
 
113
                        {
 
114
                                // Found a new device
 
115
                                char bdaddr_str[18] = {};
 
116
                                ba2str(&scan_infos[i].bdaddr, bdaddr_str);
 
117
 
 
118
                                auto* const wm = new Wiimote;
 
119
                                wm->bdaddr = scan_infos[i].bdaddr;
 
120
                                if(IsBalanceBoardName(name))
 
121
                                {
 
122
                                        found_board = wm;
 
123
                                        NOTICE_LOG(WIIMOTE, "Found balance board (%s).", bdaddr_str);
 
124
                                }
 
125
                                else
 
126
                                {
 
127
                                        found_wiimotes.push_back(wm);
 
128
                                        NOTICE_LOG(WIIMOTE, "Found wiimote (%s).", bdaddr_str);
 
129
                                }
 
130
                        }
 
131
                }
 
132
        }
 
133
 
 
134
}
 
135
 
 
136
void Wiimote::InitInternal()
 
137
{
 
138
        cmd_sock = -1;
 
139
        int_sock = -1;
 
140
 
 
141
        int fds[2];
 
142
        if (pipe(fds))
 
143
        {
 
144
                ERROR_LOG(WIIMOTE, "pipe failed");
 
145
                abort();
 
146
        }
 
147
        wakeup_pipe_w = fds[1];
 
148
        wakeup_pipe_r = fds[0];
 
149
        bdaddr = (bdaddr_t){{0, 0, 0, 0, 0, 0}};
 
150
}
 
151
 
 
152
void Wiimote::TeardownInternal()
 
153
{
 
154
        close(wakeup_pipe_w);
 
155
        close(wakeup_pipe_r);
 
156
}
 
157
 
 
158
// Connect to a wiimote with a known address.
 
159
bool Wiimote::ConnectInternal()
 
160
{
 
161
        sockaddr_l2 addr;
 
162
        addr.l2_family = AF_BLUETOOTH;
 
163
        addr.l2_bdaddr = bdaddr;
 
164
        addr.l2_cid = 0;
 
165
 
 
166
        // Output channel
 
167
        addr.l2_psm = htobs(WM_OUTPUT_CHANNEL);
 
168
        if ((cmd_sock = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP)) == -1 ||
 
169
                        connect(cmd_sock, (sockaddr*)&addr, sizeof(addr)) < 0)
 
170
        {
 
171
                DEBUG_LOG(WIIMOTE, "Unable to open output socket to wiimote.");
 
172
                close(cmd_sock);
 
173
                cmd_sock = -1;
 
174
                return false;
 
175
        }
 
176
 
 
177
        // Input channel
 
178
        addr.l2_psm = htobs(WM_INPUT_CHANNEL);
 
179
        if ((int_sock = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP)) == -1 ||
 
180
                        connect(int_sock, (sockaddr*)&addr, sizeof(addr)) < 0)
 
181
        {
 
182
                DEBUG_LOG(WIIMOTE, "Unable to open input socket from wiimote.");
 
183
                close(int_sock);
 
184
                close(cmd_sock);
 
185
                int_sock = cmd_sock = -1;
 
186
                return false;
 
187
        }
 
188
 
 
189
        return true;
 
190
}
 
191
 
 
192
void Wiimote::DisconnectInternal()
 
193
{
 
194
        close(cmd_sock);
 
195
        close(int_sock);
 
196
 
 
197
        cmd_sock = -1;
 
198
        int_sock = -1;
 
199
}
 
200
 
 
201
bool Wiimote::IsConnected() const
 
202
{
 
203
        return cmd_sock != -1;// && int_sock != -1;
 
204
}
 
205
 
 
206
void Wiimote::IOWakeup()
 
207
{
 
208
        char c = 0;
 
209
        if (write(wakeup_pipe_w, &c, 1) != 1)
 
210
        {
 
211
                ERROR_LOG(WIIMOTE, "Unable to write to wakeup pipe.");
 
212
        }
 
213
}
 
214
 
 
215
// positive = read packet
 
216
// negative = didn't read packet
 
217
// zero = error
 
218
int Wiimote::IORead(u8* buf)
 
219
{
 
220
        // Block select for 1/2000th of a second
 
221
 
 
222
        fd_set fds;
 
223
        FD_ZERO(&fds);
 
224
        FD_SET(int_sock, &fds);
 
225
        FD_SET(wakeup_pipe_r, &fds);
 
226
 
 
227
        if (select(int_sock + 1, &fds, NULL, NULL, NULL) == -1)
 
228
        {
 
229
                ERROR_LOG(WIIMOTE, "Unable to select wiimote %i input socket.", index + 1);
 
230
                return -1;
 
231
        }
 
232
 
 
233
        if (FD_ISSET(wakeup_pipe_r, &fds))
 
234
        {
 
235
                char c;
 
236
                if (read(wakeup_pipe_r, &c, 1) != 1)
 
237
                {
 
238
                        ERROR_LOG(WIIMOTE, "Unable to read from wakeup pipe.");
 
239
                }
 
240
                return -1;
 
241
        }
 
242
 
 
243
        if (!FD_ISSET(int_sock, &fds))
 
244
                return -1;
 
245
 
 
246
        // Read the pending message into the buffer
 
247
        int r = read(int_sock, buf, MAX_PAYLOAD);
 
248
        if (r == -1)
 
249
        {
 
250
                // Error reading data
 
251
                ERROR_LOG(WIIMOTE, "Receiving data from wiimote %i.", index + 1);
 
252
 
 
253
                if (errno == ENOTCONN)
 
254
                {
 
255
                        // This can happen if the bluetooth dongle is disconnected
 
256
                        ERROR_LOG(WIIMOTE, "Bluetooth appears to be disconnected.  "
 
257
                                        "Wiimote %i will be disconnected.", index + 1);
 
258
                }
 
259
 
 
260
                r = 0;
 
261
        }
 
262
 
 
263
        return r;
 
264
}
 
265
 
 
266
int Wiimote::IOWrite(u8 const* buf, int len)
 
267
{
 
268
        return write(int_sock, buf, len);
 
269
}
 
270
 
 
271
}; // WiimoteReal