2013-03-25 22:25:21 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
|
|
|
* Copyright (C) 2013 - Jason Fetters
|
|
|
|
*
|
|
|
|
* RetroArch 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 Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
2013-02-27 04:14:27 +00:00
|
|
|
*
|
2013-03-25 22:25:21 +00:00
|
|
|
* RetroArch 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 for more details.
|
2013-02-27 04:14:27 +00:00
|
|
|
*
|
2013-03-25 22:25:21 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along with RetroArch.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
2013-02-27 04:14:27 +00:00
|
|
|
*/
|
|
|
|
|
2013-04-04 21:58:51 +00:00
|
|
|
// I take back everything I ever said about bad bluetooth stacks, this shit is hard.
|
|
|
|
|
2013-02-27 04:14:27 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <stdio.h>
|
2013-03-24 17:13:36 +00:00
|
|
|
#include <string.h>
|
2013-02-27 04:14:27 +00:00
|
|
|
|
2013-09-05 05:20:56 +00:00
|
|
|
#include "apple/common/rarch_wrapper.h"
|
2013-03-19 00:32:24 +00:00
|
|
|
#include "btdynamic.h"
|
2013-03-24 17:13:36 +00:00
|
|
|
#include "btpad.h"
|
2013-06-19 20:35:32 +00:00
|
|
|
#include "btpad_queue.h"
|
2013-02-27 04:14:27 +00:00
|
|
|
|
2013-09-11 23:16:47 +00:00
|
|
|
// Private interface
|
|
|
|
enum btpad_state { BTPAD_EMPTY, BTPAD_CONNECTING, BTPAD_CONNECTED };
|
|
|
|
|
2013-10-03 21:43:41 +00:00
|
|
|
struct apple_pad_connection
|
2013-09-11 23:16:47 +00:00
|
|
|
{
|
|
|
|
uint32_t slot;
|
|
|
|
|
|
|
|
enum btpad_state state;
|
|
|
|
|
|
|
|
bool has_address;
|
|
|
|
bd_addr_t address;
|
|
|
|
|
|
|
|
uint16_t handle;
|
|
|
|
uint16_t channels[2]; //0: Control, 1: Interrupt
|
|
|
|
};
|
|
|
|
|
2013-10-03 21:43:41 +00:00
|
|
|
static struct apple_pad_connection g_connections[MAX_PLAYERS];
|
2013-09-11 23:16:47 +00:00
|
|
|
|
2013-10-03 21:43:41 +00:00
|
|
|
void apple_pad_send_control(struct apple_pad_connection* connection, uint8_t* data, size_t size)
|
2013-09-11 23:16:47 +00:00
|
|
|
{
|
|
|
|
bt_send_l2cap_ptr(connection->channels[0], data, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-22 00:18:24 +00:00
|
|
|
static bool inquiry_off;
|
|
|
|
static bool inquiry_running;
|
2013-04-04 21:58:51 +00:00
|
|
|
|
2013-06-22 00:18:24 +00:00
|
|
|
// External interface (MAIN THREAD ONLY)
|
|
|
|
void btpad_set_inquiry_state(bool on)
|
|
|
|
{
|
|
|
|
inquiry_off = !on;
|
|
|
|
|
|
|
|
if (!inquiry_off && !inquiry_running)
|
|
|
|
btpad_queue_hci_inquiry(HCI_INQUIRY_LAP, 3, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Internal interface:
|
2013-10-03 21:43:41 +00:00
|
|
|
static struct apple_pad_connection* btpad_find_empty_connection()
|
|
|
|
{
|
|
|
|
for (int i = 0; i != MAX_PLAYERS; i ++)
|
|
|
|
if (g_connections[i].state == BTPAD_EMPTY)
|
|
|
|
return &g_connections[i];
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct apple_pad_connection* btpad_find_connection_for(uint16_t handle, bd_addr_t address)
|
2013-04-04 21:58:51 +00:00
|
|
|
{
|
2013-10-03 21:43:41 +00:00
|
|
|
for (int i = 0; i < MAX_PLAYERS; i ++)
|
2013-06-19 22:31:40 +00:00
|
|
|
{
|
2013-10-03 21:43:41 +00:00
|
|
|
if (!g_connections[i].handle && !g_connections[i].has_address)
|
2013-06-19 22:31:40 +00:00
|
|
|
continue;
|
2013-04-04 21:58:51 +00:00
|
|
|
|
2013-10-03 21:43:41 +00:00
|
|
|
if (handle && g_connections[i].handle && handle != g_connections[i].handle)
|
2013-06-19 22:31:40 +00:00
|
|
|
continue;
|
2013-04-04 21:58:51 +00:00
|
|
|
|
2013-10-03 21:43:41 +00:00
|
|
|
if (address && g_connections[i].has_address && (BD_ADDR_CMP(address, g_connections[i].address)))
|
2013-06-19 22:31:40 +00:00
|
|
|
continue;
|
|
|
|
|
2013-10-03 21:43:41 +00:00
|
|
|
return &g_connections[i];
|
2013-04-04 21:58:51 +00:00
|
|
|
}
|
|
|
|
|
2013-10-03 21:43:41 +00:00
|
|
|
return 0;
|
2013-06-19 22:31:40 +00:00
|
|
|
}
|
2013-03-24 17:13:36 +00:00
|
|
|
|
2013-10-03 21:43:41 +00:00
|
|
|
static void btpad_close_connection(struct apple_pad_connection* connection)
|
2013-03-24 20:18:57 +00:00
|
|
|
{
|
2013-10-03 21:43:41 +00:00
|
|
|
if (connection->handle)
|
|
|
|
btpad_queue_hci_disconnect(connection->handle, 0x15);
|
2013-04-04 21:58:51 +00:00
|
|
|
|
2013-10-03 21:43:41 +00:00
|
|
|
memset(connection, 0, sizeof(struct apple_pad_connection));
|
2013-06-19 22:31:40 +00:00
|
|
|
}
|
2013-04-04 21:58:51 +00:00
|
|
|
|
2013-10-03 21:43:41 +00:00
|
|
|
static void btpad_close_all_connections()
|
2013-06-19 22:31:40 +00:00
|
|
|
{
|
2013-10-03 21:43:41 +00:00
|
|
|
for (int i = 0; i < MAX_PLAYERS; i ++)
|
|
|
|
btpad_close_connection(&g_connections[i]);
|
2013-04-04 21:58:51 +00:00
|
|
|
}
|
|
|
|
|
2013-03-25 22:25:21 +00:00
|
|
|
void btpad_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)
|
2013-03-24 20:18:57 +00:00
|
|
|
{
|
2013-04-04 21:58:51 +00:00
|
|
|
bd_addr_t event_addr;
|
|
|
|
|
|
|
|
if (packet_type == HCI_EVENT_PACKET)
|
2013-03-24 20:18:57 +00:00
|
|
|
{
|
2013-04-04 21:58:51 +00:00
|
|
|
switch (packet[0])
|
|
|
|
{
|
2013-06-19 22:31:40 +00:00
|
|
|
case BTSTACK_EVENT_STATE:
|
|
|
|
{
|
2013-04-04 21:58:51 +00:00
|
|
|
if (packet[2] == HCI_STATE_WORKING)
|
2013-06-19 20:35:32 +00:00
|
|
|
{
|
|
|
|
btpad_queue_reset();
|
|
|
|
|
|
|
|
btpad_queue_hci_read_bd_addr();
|
2013-06-21 19:49:35 +00:00
|
|
|
bt_send_cmd_ptr(l2cap_register_service_ptr, PSM_HID_CONTROL, 672); // TODO: Where did I get 672 for mtu?
|
|
|
|
bt_send_cmd_ptr(l2cap_register_service_ptr, PSM_HID_INTERRUPT, 672);
|
2013-06-19 20:35:32 +00:00
|
|
|
btpad_queue_hci_inquiry(HCI_INQUIRY_LAP, 3, 1);
|
2013-06-19 22:31:40 +00:00
|
|
|
|
|
|
|
btpad_queue_run(1);
|
2013-06-19 20:35:32 +00:00
|
|
|
}
|
2013-09-11 23:16:47 +00:00
|
|
|
else if(packet[2] > HCI_STATE_WORKING)
|
2013-06-19 20:35:32 +00:00
|
|
|
{
|
2013-10-03 21:43:41 +00:00
|
|
|
btpad_close_all_connections();
|
2013-06-19 20:35:32 +00:00
|
|
|
}
|
2013-06-19 22:31:40 +00:00
|
|
|
}
|
|
|
|
break;
|
2013-06-19 20:35:32 +00:00
|
|
|
|
|
|
|
case HCI_EVENT_COMMAND_STATUS:
|
2013-06-19 22:31:40 +00:00
|
|
|
{
|
2013-06-19 20:35:32 +00:00
|
|
|
btpad_queue_run(packet[3]);
|
2013-06-19 22:31:40 +00:00
|
|
|
}
|
|
|
|
break;
|
2013-04-04 21:58:51 +00:00
|
|
|
|
2013-05-24 21:04:46 +00:00
|
|
|
case HCI_EVENT_COMMAND_COMPLETE:
|
2013-06-19 22:31:40 +00:00
|
|
|
{
|
2013-06-19 20:35:32 +00:00
|
|
|
btpad_queue_run(packet[2]);
|
|
|
|
|
2013-05-24 21:04:46 +00:00
|
|
|
if (COMMAND_COMPLETE_EVENT(packet, (*hci_read_bd_addr_ptr)))
|
|
|
|
{
|
2013-06-19 22:31:40 +00:00
|
|
|
bt_flip_addr_ptr(event_addr, &packet[6]);
|
|
|
|
if (!packet[5]) ios_add_log_message("BTpad: Local address is %s", bd_addr_to_str_ptr(event_addr));
|
|
|
|
else ios_add_log_message("BTpad: Failed to get local address (Status: %02X)", packet[5]);
|
2013-05-24 21:04:46 +00:00
|
|
|
}
|
2013-06-19 22:31:40 +00:00
|
|
|
}
|
|
|
|
break;
|
2013-05-24 21:04:46 +00:00
|
|
|
|
2013-04-04 21:58:51 +00:00
|
|
|
case HCI_EVENT_INQUIRY_RESULT:
|
|
|
|
{
|
|
|
|
if (packet[2])
|
|
|
|
{
|
|
|
|
bt_flip_addr_ptr(event_addr, &packet[3]);
|
2013-06-19 22:31:40 +00:00
|
|
|
|
2013-10-03 21:43:41 +00:00
|
|
|
struct apple_pad_connection* connection = btpad_find_empty_connection();
|
|
|
|
if (connection)
|
2013-04-04 21:58:51 +00:00
|
|
|
{
|
2013-10-03 21:43:41 +00:00
|
|
|
ios_add_log_message("BTpad: Inquiry found device");
|
|
|
|
memset(connection, 0, sizeof(struct apple_pad_connection));
|
2013-06-19 22:31:40 +00:00
|
|
|
|
2013-10-03 21:43:41 +00:00
|
|
|
memcpy(connection->address, event_addr, sizeof(bd_addr_t));
|
|
|
|
connection->has_address = true;
|
|
|
|
connection->state = BTPAD_CONNECTING;
|
2013-04-04 21:58:51 +00:00
|
|
|
|
2013-10-03 21:43:41 +00:00
|
|
|
bt_send_cmd_ptr(l2cap_create_channel_ptr, connection->address, PSM_HID_CONTROL);
|
|
|
|
bt_send_cmd_ptr(l2cap_create_channel_ptr, connection->address, PSM_HID_INTERRUPT);
|
2013-04-04 21:58:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HCI_EVENT_INQUIRY_COMPLETE:
|
|
|
|
{
|
2013-10-03 21:43:41 +00:00
|
|
|
// This must be turned off during gameplay as it causes a ton of lag
|
2013-06-22 00:18:24 +00:00
|
|
|
inquiry_running = !inquiry_off;
|
|
|
|
|
|
|
|
if (inquiry_running)
|
|
|
|
btpad_queue_hci_inquiry(HCI_INQUIRY_LAP, 3, 1);
|
2013-04-04 21:58:51 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case L2CAP_EVENT_CHANNEL_OPENED:
|
|
|
|
{
|
|
|
|
bt_flip_addr_ptr(event_addr, &packet[3]);
|
|
|
|
const uint16_t handle = READ_BT_16(packet, 9);
|
|
|
|
const uint16_t psm = READ_BT_16(packet, 11);
|
|
|
|
const uint16_t channel_id = READ_BT_16(packet, 13);
|
|
|
|
|
2013-10-03 21:43:41 +00:00
|
|
|
struct apple_pad_connection* connection = btpad_find_connection_for(handle, event_addr);
|
2013-04-04 21:58:51 +00:00
|
|
|
|
2013-06-19 20:35:32 +00:00
|
|
|
if (!packet[2])
|
2013-04-04 21:58:51 +00:00
|
|
|
{
|
2013-10-03 21:43:41 +00:00
|
|
|
if (!connection)
|
2013-06-19 22:31:40 +00:00
|
|
|
{
|
|
|
|
ios_add_log_message("BTpad: Got L2CAP 'Channel Opened' event for unrecognized device");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-10-03 21:43:41 +00:00
|
|
|
ios_add_log_message("BTpad: L2CAP channel opened: (PSM: %02X)", psm);
|
|
|
|
connection->handle = handle;
|
2013-04-04 21:58:51 +00:00
|
|
|
|
|
|
|
if (psm == PSM_HID_CONTROL)
|
2013-10-03 21:43:41 +00:00
|
|
|
connection->channels[0] = channel_id;
|
2013-04-04 21:58:51 +00:00
|
|
|
else if (psm == PSM_HID_INTERRUPT)
|
2013-10-03 21:43:41 +00:00
|
|
|
connection->channels[1] = channel_id;
|
2013-06-19 20:35:32 +00:00
|
|
|
else
|
2013-10-03 21:43:41 +00:00
|
|
|
ios_add_log_message("BTpad: Got unknown L2CAP PSM, ignoring (PSM: %02X)", psm);
|
2013-04-04 21:58:51 +00:00
|
|
|
|
2013-10-03 21:43:41 +00:00
|
|
|
if (connection->channels[0] && connection->channels[1])
|
2013-06-19 20:35:32 +00:00
|
|
|
{
|
2013-10-03 21:43:41 +00:00
|
|
|
ios_add_log_message("BTpad: Got both L2CAP channels, requesting name");
|
|
|
|
btpad_queue_hci_remote_name_request(connection->address, 0, 0, 0);
|
2013-04-04 21:58:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2013-10-03 21:43:41 +00:00
|
|
|
ios_add_log_message("BTpad: Got failed L2CAP 'Channel Opened' event (PSM: %02X, Status: %02X)", psm, packet[2]);
|
2013-04-04 21:58:51 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case L2CAP_EVENT_INCOMING_CONNECTION:
|
|
|
|
{
|
|
|
|
bt_flip_addr_ptr(event_addr, &packet[2]);
|
|
|
|
const uint16_t handle = READ_BT_16(packet, 8);
|
|
|
|
const uint32_t psm = READ_BT_16(packet, 10);
|
|
|
|
const uint32_t channel_id = READ_BT_16(packet, 12);
|
|
|
|
|
2013-10-03 21:43:41 +00:00
|
|
|
struct apple_pad_connection* connection = btpad_find_connection_for(handle, event_addr);
|
2013-04-04 21:58:51 +00:00
|
|
|
|
2013-10-03 21:43:41 +00:00
|
|
|
if (!connection)
|
|
|
|
{
|
|
|
|
connection = btpad_find_empty_connection();
|
|
|
|
if (connection)
|
2013-06-19 22:31:40 +00:00
|
|
|
{
|
2013-10-03 21:43:41 +00:00
|
|
|
ios_add_log_message("BTpad: Got new incoming connection");
|
2013-04-04 21:58:51 +00:00
|
|
|
|
2013-10-03 21:43:41 +00:00
|
|
|
memset(connection, 0, sizeof(struct apple_pad_connection));
|
2013-06-19 22:31:40 +00:00
|
|
|
|
2013-10-03 21:43:41 +00:00
|
|
|
memcpy(connection->address, event_addr, sizeof(bd_addr_t));
|
|
|
|
connection->has_address = true;
|
|
|
|
connection->handle = handle;
|
|
|
|
connection->state = BTPAD_CONNECTING;
|
2013-06-19 22:31:40 +00:00
|
|
|
}
|
|
|
|
else break;
|
2013-04-04 21:58:51 +00:00
|
|
|
}
|
|
|
|
|
2013-10-03 21:43:41 +00:00
|
|
|
ios_add_log_message("BTpad: Incoming L2CAP connection (PSM: %02X)", psm);
|
2013-06-21 19:49:35 +00:00
|
|
|
bt_send_cmd_ptr(l2cap_accept_connection_ptr, channel_id);
|
2013-04-04 21:58:51 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
|
|
|
|
{
|
|
|
|
bt_flip_addr_ptr(event_addr, &packet[3]);
|
2013-10-03 21:43:41 +00:00
|
|
|
|
|
|
|
struct apple_pad_connection* connection = btpad_find_connection_for(0, event_addr);
|
2013-04-04 21:58:51 +00:00
|
|
|
|
2013-10-03 21:43:41 +00:00
|
|
|
if (!connection)
|
2013-04-04 21:58:51 +00:00
|
|
|
{
|
|
|
|
ios_add_log_message("BTpad: Got unexpected remote name, ignoring");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-10-03 21:43:41 +00:00
|
|
|
ios_add_log_message("BTpad: Got %.200s", (char*)&packet[9]);
|
|
|
|
|
|
|
|
connection->slot = apple_joypad_connect((char*)packet + 9, connection);
|
|
|
|
connection->state = BTPAD_CONNECTED;
|
2013-04-04 21:58:51 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HCI_EVENT_PIN_CODE_REQUEST:
|
|
|
|
{
|
2013-06-19 22:31:40 +00:00
|
|
|
ios_add_log_message("BTpad: Sending WiiMote PIN");
|
2013-04-04 21:58:51 +00:00
|
|
|
|
|
|
|
bt_flip_addr_ptr(event_addr, &packet[2]);
|
2013-06-19 20:35:32 +00:00
|
|
|
btpad_queue_hci_pin_code_request_reply(event_addr, &packet[2]);
|
2013-04-04 21:58:51 +00:00
|
|
|
}
|
|
|
|
break;
|
2013-06-19 23:24:32 +00:00
|
|
|
|
|
|
|
case HCI_EVENT_DISCONNECTION_COMPLETE:
|
|
|
|
{
|
|
|
|
const uint32_t handle = READ_BT_16(packet, 3);
|
|
|
|
|
|
|
|
if (!packet[2])
|
|
|
|
{
|
2013-10-03 21:43:41 +00:00
|
|
|
struct apple_pad_connection* connection = btpad_find_connection_for(handle, 0);
|
|
|
|
if (connection)
|
2013-06-19 23:24:32 +00:00
|
|
|
{
|
2013-10-03 21:43:41 +00:00
|
|
|
connection->handle = 0;
|
|
|
|
|
|
|
|
apple_joypad_disconnect(connection->slot);
|
|
|
|
btpad_close_connection(connection);
|
2013-06-19 23:24:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ios_add_log_message("BTpad: Got failed 'Disconnection Complete' event (Status: %02X)", packet[2]);
|
|
|
|
}
|
|
|
|
break;
|
2013-06-22 00:18:24 +00:00
|
|
|
|
|
|
|
case L2CAP_EVENT_SERVICE_REGISTERED:
|
|
|
|
{
|
|
|
|
if (!packet[2])
|
|
|
|
ios_add_log_message("BTpad: Got failed 'Service Registered' event (PSM: %02X, Status: %02X)", READ_BT_16(packet, 3), packet[2]);
|
|
|
|
}
|
|
|
|
break;
|
2013-04-04 21:58:51 +00:00
|
|
|
}
|
2013-03-24 20:18:57 +00:00
|
|
|
}
|
2013-09-11 23:16:47 +00:00
|
|
|
else if (packet_type == L2CAP_DATA_PACKET)
|
|
|
|
{
|
2013-10-03 21:43:41 +00:00
|
|
|
for (int i = 0; i < MAX_PLAYERS; i ++)
|
2013-09-11 23:16:47 +00:00
|
|
|
{
|
2013-10-03 21:43:41 +00:00
|
|
|
struct apple_pad_connection* connection = &g_connections[i];
|
2013-09-11 23:16:47 +00:00
|
|
|
|
2013-10-03 21:43:41 +00:00
|
|
|
if (connection->state == BTPAD_CONNECTED && (connection->channels[0] == channel || connection->channels[1] == channel))
|
|
|
|
apple_joypad_packet(connection->slot, packet, size);
|
2013-09-11 23:16:47 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-23 23:25:09 +00:00
|
|
|
}
|