1
// Copyright (C) 2003 Dolphin Project.
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.
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.
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
15
// Official SVN repository and contact information can be found at
16
// http://code.google.com/p/dolphin-emu/
18
#include <bluetooth/bluetooth.h>
19
#include <bluetooth/hci.h>
20
#include <bluetooth/hci_lib.h>
21
#include <bluetooth/l2cap.h>
24
#include "WiimoteReal.h"
29
WiimoteScanner::WiimoteScanner()
34
// Get the id of the first bluetooth device.
35
device_id = hci_get_route(NULL);
38
NOTICE_LOG(WIIMOTE, "Bluetooth not found.");
42
// Create a socket to the device
43
device_sock = hci_open_dev(device_id);
46
ERROR_LOG(WIIMOTE, "Unable to open bluetooth.");
51
bool WiimoteScanner::IsReady() const
53
return device_sock > 0;
56
WiimoteScanner::~WiimoteScanner()
62
void WiimoteScanner::Update()
65
void WiimoteScanner::FindWiimotes(std::vector<Wiimote*> & found_wiimotes, Wiimote* & found_board)
67
// supposedly 1.28 seconds
68
int const wait_len = 1;
70
int const max_infos = 255;
71
inquiry_info scan_infos[max_infos] = {};
72
auto* scan_infos_ptr = scan_infos;
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)
79
ERROR_LOG(WIIMOTE, "Error searching for bluetooth devices.");
83
DEBUG_LOG(WIIMOTE, "Found %i bluetooth device(s).", found_devices);
85
// Display discovered devices
86
for (int i = 0; i < found_devices; ++i)
88
ERROR_LOG(WIIMOTE, "found a device...");
90
// BT names are a maximum of 248 bytes apparently
92
if (hci_read_remote_name(device_sock, &scan_infos[i].bdaddr, sizeof(name), name, 1000) < 0)
94
ERROR_LOG(WIIMOTE, "name request failed");
98
ERROR_LOG(WIIMOTE, "device name %s", name);
99
if (IsValidBluetoothName(name))
101
bool new_wiimote = true;
105
// Determine if this wiimote has already been found.
106
//for (int j = 0; j < MAX_WIIMOTES && new_wiimote; ++j)
108
// if (wm[j] && bacmp(&scan_infos[i].bdaddr,&wm[j]->bdaddr) == 0)
109
// new_wiimote = false;
114
// Found a new device
115
char bdaddr_str[18] = {};
116
ba2str(&scan_infos[i].bdaddr, bdaddr_str);
118
auto* const wm = new Wiimote;
119
wm->bdaddr = scan_infos[i].bdaddr;
120
if(IsBalanceBoardName(name))
123
NOTICE_LOG(WIIMOTE, "Found balance board (%s).", bdaddr_str);
127
found_wiimotes.push_back(wm);
128
NOTICE_LOG(WIIMOTE, "Found wiimote (%s).", bdaddr_str);
136
void Wiimote::InitInternal()
144
ERROR_LOG(WIIMOTE, "pipe failed");
147
wakeup_pipe_w = fds[1];
148
wakeup_pipe_r = fds[0];
149
bdaddr = (bdaddr_t){{0, 0, 0, 0, 0, 0}};
152
void Wiimote::TeardownInternal()
154
close(wakeup_pipe_w);
155
close(wakeup_pipe_r);
158
// Connect to a wiimote with a known address.
159
bool Wiimote::ConnectInternal()
162
addr.l2_family = AF_BLUETOOTH;
163
addr.l2_bdaddr = bdaddr;
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)
171
DEBUG_LOG(WIIMOTE, "Unable to open output socket to wiimote.");
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)
182
DEBUG_LOG(WIIMOTE, "Unable to open input socket from wiimote.");
185
int_sock = cmd_sock = -1;
192
void Wiimote::DisconnectInternal()
201
bool Wiimote::IsConnected() const
203
return cmd_sock != -1;// && int_sock != -1;
206
void Wiimote::IOWakeup()
209
if (write(wakeup_pipe_w, &c, 1) != 1)
211
ERROR_LOG(WIIMOTE, "Unable to write to wakeup pipe.");
215
// positive = read packet
216
// negative = didn't read packet
218
int Wiimote::IORead(u8* buf)
220
// Block select for 1/2000th of a second
224
FD_SET(int_sock, &fds);
225
FD_SET(wakeup_pipe_r, &fds);
227
if (select(int_sock + 1, &fds, NULL, NULL, NULL) == -1)
229
ERROR_LOG(WIIMOTE, "Unable to select wiimote %i input socket.", index + 1);
233
if (FD_ISSET(wakeup_pipe_r, &fds))
236
if (read(wakeup_pipe_r, &c, 1) != 1)
238
ERROR_LOG(WIIMOTE, "Unable to read from wakeup pipe.");
243
if (!FD_ISSET(int_sock, &fds))
246
// Read the pending message into the buffer
247
int r = read(int_sock, buf, MAX_PAYLOAD);
250
// Error reading data
251
ERROR_LOG(WIIMOTE, "Receiving data from wiimote %i.", index + 1);
253
if (errno == ENOTCONN)
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);
266
int Wiimote::IOWrite(u8 const* buf, int len)
268
return write(int_sock, buf, len);