RetroArch/apple/iOS/bluetooth/btpad.c

578 lines
17 KiB
C
Raw Normal View History

/* RetroArch - A frontend for libretro.
2014-01-01 00:50:59 +00:00
* Copyright (C) 2013-2014 - Jason Fetters
2015-01-07 17:17:42 +00:00
* Copyright (C) 2011-2015 - Daniel De Matteis
*
* 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.
*
* 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.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <sys/types.h>
#include <sys/sysctl.h>
#include <stdio.h>
#include <string.h>
#include "btdynamic.h"
2015-04-03 13:38:50 +00:00
#include "btstack.h"
#include "../input/connect/joypad_connection.h"
extern joypad_connection_t *slots;
2014-10-04 16:11:39 +00:00
/* Private interface. */
2014-06-11 10:51:49 +00:00
enum btpad_state
{
BTPAD_EMPTY,
BTPAD_CONNECTING,
BTPAD_CONNECTED
};
2015-04-03 13:02:18 +00:00
struct btpad_queue_command
{
const hci_cmd_t* command;
union
{
struct
{
uint8_t on;
} btstack_set_power_mode;
struct
{
uint16_t handle;
uint8_t reason;
} hci_disconnect;
struct
{
uint32_t lap;
uint8_t length;
uint8_t num_responses;
} hci_inquiry;
struct
{
bd_addr_t bd_addr;
uint8_t page_scan_repetition_mode;
uint8_t reserved;
uint16_t clock_offset;
} hci_remote_name_request;
/* For wiimote only.
* TODO - should we repurpose this so
* that it's for more than just Wiimote?
* */
struct
{
bd_addr_t bd_addr;
bd_addr_t pin;
} hci_pin_code_request_reply;
};
};
struct pad_connection
{
uint32_t slot;
enum btpad_state state;
bool has_address;
bd_addr_t address;
uint16_t handle;
2014-10-04 22:58:47 +00:00
/* 0: Control, 1: Interrupt */
uint16_t channels[2];
};
2014-10-04 15:01:56 +00:00
static bool inquiry_off;
static bool inquiry_running;
2015-01-05 00:58:00 +00:00
static struct pad_connection g_connections[MAX_USERS];
2015-04-03 13:02:18 +00:00
struct btpad_queue_command commands[64];
static uint32_t insert_position;
static uint32_t read_position;
static uint32_t can_run;
2015-04-03 13:42:40 +00:00
static void btpad_increment_position(uint32_t *ptr)
{
*ptr = (*ptr + 1) % 64;
}
2015-04-03 13:02:18 +00:00
2015-04-03 13:11:47 +00:00
static void btpad_queue_process_cmd(struct btpad_queue_command *cmd)
{
if (!cmd)
return;
if (cmd->command == btstack_set_power_mode_ptr)
bt_send_cmd_ptr(
cmd->command,
cmd->btstack_set_power_mode.on);
else if (cmd->command == hci_read_bd_addr_ptr)
bt_send_cmd_ptr(cmd->command);
else if (cmd->command == hci_disconnect_ptr)
bt_send_cmd_ptr(
cmd->command,
cmd->hci_disconnect.handle,
cmd->hci_disconnect.reason);
else if (cmd->command == hci_inquiry_ptr)
bt_send_cmd_ptr(
cmd->command,
cmd->hci_inquiry.lap,
cmd->hci_inquiry.length,
cmd->hci_inquiry.num_responses);
else if (cmd->command == hci_remote_name_request_ptr)
bt_send_cmd_ptr(
cmd->command,
cmd->hci_remote_name_request.bd_addr,
cmd->hci_remote_name_request.page_scan_repetition_mode,
cmd->hci_remote_name_request.reserved,
cmd->hci_remote_name_request.clock_offset);
else if (cmd->command == hci_pin_code_request_reply_ptr)
bt_send_cmd_ptr(
cmd->command,
cmd->hci_pin_code_request_reply.bd_addr,
6,
cmd->hci_pin_code_request_reply.pin);
}
2015-04-03 13:02:18 +00:00
static void btpad_queue_process(void)
{
2015-04-03 13:53:04 +00:00
for (; can_run && (insert_position != read_position); can_run--)
2015-04-03 13:02:18 +00:00
{
2015-04-03 13:05:30 +00:00
struct btpad_queue_command* cmd = &commands[read_position];
2015-04-03 13:11:47 +00:00
btpad_queue_process_cmd(cmd);
2015-04-03 13:42:40 +00:00
btpad_increment_position(&read_position);
2015-04-03 13:02:18 +00:00
}
}
static void btpad_queue_reset(void)
{
insert_position = 0;
read_position = 0;
can_run = 1;
}
static void btpad_queue_run(uint32_t count)
{
can_run = count;
btpad_queue_process();
}
2015-04-03 14:21:55 +00:00
static void btpad_queue_btstack_set_power_mode(
struct btpad_queue_command *cmd, uint8_t on)
2015-04-03 13:02:18 +00:00
{
if (!cmd)
return;
cmd->command = btstack_set_power_mode_ptr;
cmd->btstack_set_power_mode.on = on;
2015-04-03 13:42:40 +00:00
btpad_increment_position(&insert_position);
2015-04-03 13:02:18 +00:00
btpad_queue_process();
}
2015-04-03 14:21:55 +00:00
static void btpad_queue_hci_read_bd_addr(
struct btpad_queue_command *cmd)
2015-04-03 13:02:18 +00:00
{
if (!cmd)
return;
cmd->command = hci_read_bd_addr_ptr;
2015-04-03 13:42:40 +00:00
btpad_increment_position(&insert_position);
2015-04-03 13:02:18 +00:00
btpad_queue_process();
}
2015-04-03 14:21:55 +00:00
static void btpad_queue_hci_disconnect(
struct btpad_queue_command *cmd,
uint16_t handle, uint8_t reason)
2015-04-03 13:02:18 +00:00
{
if (!cmd)
return;
cmd->command = hci_disconnect_ptr;
cmd->hci_disconnect.handle = handle;
cmd->hci_disconnect.reason = reason;
2015-04-03 13:42:40 +00:00
btpad_increment_position(&insert_position);
2015-04-03 13:02:18 +00:00
btpad_queue_process();
}
2015-04-03 14:21:55 +00:00
static void btpad_queue_hci_inquiry(
struct btpad_queue_command *cmd,
uint32_t lap,
2015-04-03 13:02:18 +00:00
uint8_t length, uint8_t num_responses)
{
if (!cmd)
return;
cmd->command = hci_inquiry_ptr;
cmd->hci_inquiry.lap = lap;
cmd->hci_inquiry.length = length;
cmd->hci_inquiry.num_responses = num_responses;
2015-04-03 13:42:40 +00:00
btpad_increment_position(&insert_position);
2015-04-03 13:02:18 +00:00
btpad_queue_process();
}
2015-04-03 14:21:55 +00:00
static void btpad_queue_hci_remote_name_request(
struct btpad_queue_command *cmd,
bd_addr_t bd_addr,
2015-04-03 13:02:18 +00:00
uint8_t page_scan_repetition_mode,
uint8_t reserved, uint16_t clock_offset)
{
if (!cmd)
return;
cmd->command = hci_remote_name_request_ptr;
memcpy(cmd->hci_remote_name_request.bd_addr, bd_addr, sizeof(bd_addr_t));
cmd->hci_remote_name_request.page_scan_repetition_mode =
page_scan_repetition_mode;
cmd->hci_remote_name_request.reserved = reserved;
cmd->hci_remote_name_request.clock_offset = clock_offset;
2015-04-03 13:42:40 +00:00
btpad_increment_position(&insert_position);
2015-04-03 13:02:18 +00:00
btpad_queue_process();
}
static void btpad_queue_hci_pin_code_request_reply(
2015-04-03 14:21:55 +00:00
struct btpad_queue_command *cmd,
2015-04-03 13:02:18 +00:00
bd_addr_t bd_addr, bd_addr_t pin)
{
if (!cmd)
return;
cmd->command = hci_pin_code_request_reply_ptr;
memcpy(cmd->hci_pin_code_request_reply.bd_addr, bd_addr, sizeof(bd_addr_t));
memcpy(cmd->hci_pin_code_request_reply.pin, pin, sizeof(bd_addr_t));
2015-04-03 13:42:40 +00:00
btpad_increment_position(&insert_position);
2015-04-03 13:02:18 +00:00
btpad_queue_process();
}
static void btpad_connection_send_control(void *data,
2015-04-02 17:34:36 +00:00
uint8_t* data_buf, size_t size)
{
struct pad_connection *connection = (struct pad_connection*)data;
2014-10-04 16:11:39 +00:00
2014-10-04 15:01:56 +00:00
if (connection)
bt_send_l2cap_ptr(connection->channels[0], data_buf, size);
}
void btpad_set_inquiry_state(bool on)
{
inquiry_off = !on;
if (!inquiry_off && !inquiry_running)
2015-04-03 14:21:55 +00:00
btpad_queue_hci_inquiry(&commands[insert_position],
HCI_INQUIRY_LAP, 3, 1);
}
2014-10-04 16:11:39 +00:00
/* Internal interface. */
2015-04-03 13:53:04 +00:00
static struct pad_connection *btpad_find_empty_connection(void)
{
2015-04-03 13:11:47 +00:00
unsigned i;
2015-04-03 14:21:55 +00:00
2015-04-03 13:11:47 +00:00
for (i = 0; i < MAX_USERS; i++)
{
if (g_connections[i].state == BTPAD_EMPTY)
return &g_connections[i];
2015-04-03 13:11:47 +00:00
}
2014-10-04 16:11:39 +00:00
return 0;
}
2015-04-03 13:53:04 +00:00
static struct pad_connection *btpad_find_connection_for(
2014-10-04 16:11:39 +00:00
uint16_t handle, bd_addr_t address)
{
2015-04-03 13:11:47 +00:00
unsigned i;
2015-04-03 13:53:04 +00:00
2015-04-03 13:11:47 +00:00
for (i = 0; i < MAX_USERS; i++)
{
if (!g_connections[i].handle && !g_connections[i].has_address)
continue;
2014-10-04 16:11:39 +00:00
if (handle && g_connections[i].handle
&& handle != g_connections[i].handle)
continue;
2014-10-04 16:11:39 +00:00
if (address && g_connections[i].has_address
&& (BD_ADDR_CMP(address, g_connections[i].address)))
continue;
return &g_connections[i];
}
return 0;
}
static void btpad_close_connection(struct pad_connection* connection)
{
2014-10-04 16:11:39 +00:00
if (!connection)
return;
if (connection->handle)
2015-04-03 14:21:55 +00:00
btpad_queue_hci_disconnect(&commands[insert_position],
connection->handle, 0x15);
memset(connection, 0, sizeof(struct pad_connection));
}
2014-06-11 10:51:49 +00:00
static void btpad_close_all_connections(void)
{
2015-04-03 13:53:04 +00:00
unsigned i;
2015-01-05 00:58:00 +00:00
for (i = 0; i < MAX_USERS; i ++)
btpad_close_connection(&g_connections[i]);
2015-04-03 13:53:04 +00:00
2014-10-04 20:56:48 +00:00
/* TODO/FIXME - create platform-agnostic solution for this
* and figure out why/if this is needed. */
CFRunLoopStop(CFRunLoopGetCurrent());
}
2014-10-04 16:11:39 +00:00
void btpad_packet_handler(uint8_t packet_type,
uint16_t channel, uint8_t *packet, uint16_t size)
{
2015-04-03 13:53:04 +00:00
unsigned i;
bd_addr_t event_addr;
2015-04-03 14:21:55 +00:00
struct btpad_queue_command* cmd = &commands[insert_position];
2014-10-04 16:11:39 +00:00
switch (packet_type)
{
2014-10-04 16:11:39 +00:00
case L2CAP_DATA_PACKET:
2015-01-05 00:58:00 +00:00
for (i = 0; i < MAX_USERS; i ++)
{
2015-04-03 13:53:04 +00:00
struct pad_connection *connection = &g_connections[i];
2014-10-04 16:11:39 +00:00
if (connection && connection->state == BTPAD_CONNECTED
&& (connection->channels[0] == channel ||
connection->channels[1] == channel))
2014-10-05 23:49:00 +00:00
pad_connection_packet(&slots[connection->slot], connection->slot, packet, size);
}
break;
2014-10-04 16:11:39 +00:00
case HCI_EVENT_PACKET:
switch (packet[0])
{
2014-10-04 16:11:39 +00:00
case BTSTACK_EVENT_STATE:
2015-04-03 13:53:04 +00:00
RARCH_LOG("[BTstack]: HCI State %d.\n", packet[2]);
switch (packet[2])
{
case HCI_STATE_WORKING:
btpad_queue_reset();
2015-04-03 14:21:55 +00:00
btpad_queue_hci_read_bd_addr(cmd);
2015-04-03 13:53:04 +00:00
/* TODO: Where did I get 672 for MTU? */
bt_send_cmd_ptr(l2cap_register_service_ptr,
PSM_HID_CONTROL, 672);
bt_send_cmd_ptr(l2cap_register_service_ptr,
PSM_HID_INTERRUPT, 672);
2015-04-03 14:21:55 +00:00
btpad_queue_hci_inquiry(cmd, HCI_INQUIRY_LAP, 3, 1);
2014-10-04 16:11:39 +00:00
2015-04-03 13:53:04 +00:00
btpad_queue_run(1);
break;
case HCI_STATE_HALTING:
btpad_close_all_connections();
break;
2014-10-04 16:11:39 +00:00
}
break;
2014-10-04 16:11:39 +00:00
case HCI_EVENT_COMMAND_STATUS:
btpad_queue_run(packet[3]);
break;
2014-06-11 10:51:49 +00:00
2014-10-04 16:11:39 +00:00
case HCI_EVENT_COMMAND_COMPLETE:
2015-04-03 13:53:04 +00:00
btpad_queue_run(packet[2]);
2014-10-04 16:11:39 +00:00
2015-04-03 13:53:04 +00:00
if (COMMAND_COMPLETE_EVENT(packet, (*hci_read_bd_addr_ptr)))
{
bt_flip_addr_ptr(event_addr, &packet[6]);
if (!packet[5])
RARCH_LOG("[BTpad]: Local address is %s.\n",
bd_addr_to_str_ptr(event_addr));
else
RARCH_LOG("[BTpad]: Failed to get local address (Status: %02X).\n",
packet[5]);
2014-10-04 16:11:39 +00:00
}
break;
2014-10-04 16:11:39 +00:00
case HCI_EVENT_INQUIRY_RESULT:
2015-04-03 13:53:04 +00:00
if (packet[2])
2014-10-04 16:11:39 +00:00
{
2015-04-03 13:53:04 +00:00
bt_flip_addr_ptr(event_addr, &packet[3]);
2015-04-03 13:53:04 +00:00
struct pad_connection* connection = btpad_find_empty_connection();
2015-04-03 13:53:04 +00:00
if (!connection)
return;
2015-04-03 13:53:04 +00:00
RARCH_LOG("[BTpad]: Inquiry found device\n");
memset(connection, 0, sizeof(struct pad_connection));
2015-04-03 13:53:04 +00:00
memcpy(connection->address, event_addr, sizeof(bd_addr_t));
connection->has_address = true;
connection->state = BTPAD_CONNECTING;
2015-04-03 13:53:04 +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);
2014-10-04 16:11:39 +00:00
}
break;
2014-10-04 16:11:39 +00:00
case HCI_EVENT_INQUIRY_COMPLETE:
2015-04-03 13:53:04 +00:00
/* This must be turned off during gameplay
* as it causes a ton of lag. */
inquiry_running = !inquiry_off;
2014-10-04 16:11:39 +00:00
2015-04-03 13:53:04 +00:00
if (inquiry_running)
2015-04-03 14:21:55 +00:00
btpad_queue_hci_inquiry(cmd, HCI_INQUIRY_LAP, 3, 1);
2014-10-04 16:11:39 +00:00
break;
2014-10-04 16:11:39 +00:00
case L2CAP_EVENT_CHANNEL_OPENED:
2013-06-19 20:35:32 +00:00
{
2015-04-03 13:53:04 +00:00
uint16_t handle, psm, channel_id;
struct pad_connection *connection = NULL;
2014-10-04 16:11:39 +00:00
bt_flip_addr_ptr(event_addr, &packet[3]);
2015-04-03 13:53:04 +00:00
handle = READ_BT_16(packet, 9);
psm = READ_BT_16(packet, 11);
channel_id = READ_BT_16(packet, 13);
connection = btpad_find_connection_for(handle, event_addr);
2014-10-04 16:11:39 +00:00
if (!packet[2])
{
if (!connection)
{
2014-10-04 22:58:47 +00:00
RARCH_LOG("[BTpad]: Got L2CAP 'Channel Opened' event for unrecognized device.\n");
2014-10-04 16:11:39 +00:00
break;
}
2014-10-04 22:58:47 +00:00
RARCH_LOG("[BTpad]: L2CAP channel opened: (PSM: %02X)\n", psm);
2014-10-04 16:11:39 +00:00
connection->handle = handle;
if (psm == PSM_HID_CONTROL)
connection->channels[0] = channel_id;
else if (psm == PSM_HID_INTERRUPT)
connection->channels[1] = channel_id;
else
2014-10-04 22:58:47 +00:00
RARCH_LOG("[BTpad]: Got unknown L2CAP PSM, ignoring (PSM: %02X).\n", psm);
2014-10-04 16:11:39 +00:00
2015-04-03 13:53:04 +00:00
if (connection->channels[0] && connection->channels[1])
2014-10-04 16:11:39 +00:00
{
2014-10-04 22:58:47 +00:00
RARCH_LOG("[BTpad]: Got both L2CAP channels, requesting name.\n");
2015-04-03 14:21:55 +00:00
btpad_queue_hci_remote_name_request(cmd, connection->address, 0, 0, 0);
2014-10-04 16:11:39 +00:00
}
}
else
2014-10-04 22:58:47 +00:00
RARCH_LOG("[BTpad]: Got failed L2CAP 'Channel Opened' event (PSM: %02X, Status: %02X).\n", psm, packet[2]);
}
2014-10-04 16:11:39 +00:00
break;
2014-10-04 16:11:39 +00:00
case L2CAP_EVENT_INCOMING_CONNECTION:
{
2015-04-03 13:53:04 +00:00
uint16_t handle, psm, channel_id;
struct pad_connection* connection = NULL;
2014-10-04 16:11:39 +00:00
bt_flip_addr_ptr(event_addr, &packet[2]);
2015-04-03 13:53:04 +00:00
handle = READ_BT_16(packet, 8);
psm = READ_BT_16(packet, 10);
channel_id = READ_BT_16(packet, 12);
connection = btpad_find_connection_for(handle, event_addr);
2014-10-04 16:11:39 +00:00
if (!connection)
{
connection = btpad_find_empty_connection();
if (!connection)
break;
2014-10-04 22:58:47 +00:00
RARCH_LOG("[BTpad]: Got new incoming connection\n");
2014-10-04 16:11:39 +00:00
memset(connection, 0,
sizeof(struct pad_connection));
2014-10-04 16:11:39 +00:00
memcpy(connection->address, event_addr,
sizeof(bd_addr_t));
connection->has_address = true;
connection->handle = handle;
connection->state = BTPAD_CONNECTING;
}
2014-10-04 22:58:47 +00:00
RARCH_LOG("[BTpad]: Incoming L2CAP connection (PSM: %02X).\n",
2014-10-04 16:11:39 +00:00
psm);
bt_send_cmd_ptr(l2cap_accept_connection_ptr, channel_id);
}
2014-10-04 16:11:39 +00:00
break;
2014-10-04 16:11:39 +00:00
case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
{
2015-04-03 13:53:04 +00:00
struct pad_connection *connection = NULL;
2014-10-04 16:11:39 +00:00
bt_flip_addr_ptr(event_addr, &packet[3]);
2015-04-03 13:53:04 +00:00
connection = btpad_find_connection_for(0, event_addr);
2014-10-04 16:11:39 +00:00
if (!connection)
{
2014-10-04 22:58:47 +00:00
RARCH_LOG("[BTpad]: Got unexpected remote name, ignoring.\n");
2014-10-04 16:11:39 +00:00
break;
}
2014-10-04 22:58:47 +00:00
RARCH_LOG("[BTpad]: Got %.200s.\n", (char*)&packet[9]);
2015-04-03 13:11:47 +00:00
connection->slot = pad_connection_pad_init(&slots[connection->slot],
2014-10-04 20:13:46 +00:00
(char*)packet + 9, connection, &btpad_connection_send_control);
2014-10-04 16:11:39 +00:00
connection->state = BTPAD_CONNECTED;
}
break;
2014-10-04 16:11:39 +00:00
case HCI_EVENT_PIN_CODE_REQUEST:
2014-10-04 22:58:47 +00:00
RARCH_LOG("[BTpad]: Sending Wiimote PIN.\n");
2014-10-04 16:11:39 +00:00
bt_flip_addr_ptr(event_addr, &packet[2]);
2015-04-03 14:21:55 +00:00
btpad_queue_hci_pin_code_request_reply(cmd, event_addr, &packet[2]);
2014-10-04 16:11:39 +00:00
break;
2014-06-11 10:51:49 +00:00
2014-10-04 16:11:39 +00:00
case HCI_EVENT_DISCONNECTION_COMPLETE:
{
2014-10-04 16:11:39 +00:00
const uint32_t handle = READ_BT_16(packet, 3);
if (!packet[2])
{
2015-04-03 13:53:04 +00:00
struct pad_connection* connection = btpad_find_connection_for(handle, 0);
2014-10-04 16:11:39 +00:00
if (connection)
{
connection->handle = 0;
2014-10-22 06:01:23 +00:00
pad_connection_pad_deinit(&slots[connection->slot], connection->slot);
2014-10-04 16:11:39 +00:00
btpad_close_connection(connection);
}
}
else
2014-10-04 22:58:47 +00:00
RARCH_LOG("[BTpad]: Got failed 'Disconnection Complete' event (Status: %02X).\n", packet[2]);
}
2014-10-04 16:11:39 +00:00
break;
2014-10-04 16:11:39 +00:00
case L2CAP_EVENT_SERVICE_REGISTERED:
if (packet[2])
2014-10-04 22:58:47 +00:00
RARCH_LOG("[BTpad]: Got failed 'Service Registered' event (PSM: %02X, Status: %02X).\n",
2014-10-04 16:11:39 +00:00
READ_BT_16(packet, 3), packet[2]);
break;
}
break;
}
}