diff --git a/griffin/griffin.c b/griffin/griffin.c index 0893c69a8d..a906e3d221 100644 --- a/griffin/griffin.c +++ b/griffin/griffin.c @@ -260,6 +260,7 @@ INPUT #include "../ios/RetroArch/input/BTStack/btpad.c" #include "../ios/RetroArch/input/BTStack/btpad_ps3.c" #include "../ios/RetroArch/input/BTStack/btpad_wii.c" +#include "../ios/RetroArch/input/BTStack/btpad_queue.c" #elif defined(__BLACKBERRY_QNX__) #include "../blackberry-qnx/qnx_input.c" #endif diff --git a/ios/RetroArch/input/BTStack/btdynamic.c b/ios/RetroArch/input/BTStack/btdynamic.c index c95f13ec25..a3bf109956 100644 --- a/ios/RetroArch/input/BTStack/btdynamic.c +++ b/ios/RetroArch/input/BTStack/btdynamic.c @@ -37,9 +37,8 @@ static struct GRAB(bt_send_l2cap), GRAB(run_loop_init), GRAB(run_loop_execute), - GRAB(btstack_get_system_bluetooth_enabled), + GRAB(btstack_set_power_mode), - GRAB(btstack_set_system_bluetooth_enabled), GRAB(hci_delete_stored_link_key), GRAB(hci_disconnect), GRAB(hci_read_bd_addr), diff --git a/ios/RetroArch/input/BTStack/btdynamic.h b/ios/RetroArch/input/BTStack/btdynamic.h index 70c20da6da..2f2e74fe51 100644 --- a/ios/RetroArch/input/BTStack/btdynamic.h +++ b/ios/RetroArch/input/BTStack/btdynamic.h @@ -41,9 +41,7 @@ BTDIMPORT void (*bt_send_l2cap_ptr)(uint16_t local_cid, uint8_t *data, uint16_t BTDIMPORT void (*run_loop_init_ptr)(RUN_LOOP_TYPE type); BTDIMPORT void (*run_loop_execute_ptr)(); -BTDIMPORT const hci_cmd_t* btstack_get_system_bluetooth_enabled_ptr; BTDIMPORT const hci_cmd_t* btstack_set_power_mode_ptr; -BTDIMPORT const hci_cmd_t* btstack_set_system_bluetooth_enabled_ptr; BTDIMPORT const hci_cmd_t* hci_delete_stored_link_key_ptr; BTDIMPORT const hci_cmd_t* hci_disconnect_ptr; BTDIMPORT const hci_cmd_t* hci_read_bd_addr_ptr; diff --git a/ios/RetroArch/input/BTStack/btpad.c b/ios/RetroArch/input/BTStack/btpad.c index 998d8d4b73..e0526c8a9f 100644 --- a/ios/RetroArch/input/BTStack/btpad.c +++ b/ios/RetroArch/input/BTStack/btpad.c @@ -23,6 +23,7 @@ #include "../../rarch_wrapper.h" #include "btdynamic.h" #include "btpad.h" +#include "btpad_queue.h" #include "wiimote.h" static btpad_connection_t btpad_connection; @@ -71,21 +72,11 @@ static void btpad_disconnect_pad() } if (btpad_connection.handle) - bt_send_cmd_ptr(hci_disconnect_ptr, btpad_connection.handle, 0x15); + btpad_queue_hci_disconnect(btpad_connection.handle, 0x15); memset(&btpad_connection, 0, sizeof(btpad_connection_t)); } -static void btpad_connect_pad() -{ - if (btpad_connection.state == BTPAD_CONNECTED) - btpad_disconnect_pad(); - memset(&btpad_connection, 0, sizeof(btpad_connection_t)); - - ios_add_log_message("BTpad: Requesting local address"); - bt_send_cmd_ptr(hci_read_bd_addr_ptr); -} - void btpad_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) { bd_addr_t event_addr; @@ -96,44 +87,52 @@ void btpad_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet { case BTSTACK_EVENT_STATE: if (packet[2] == HCI_STATE_WORKING) - btpad_connect_pad(); + { + btpad_queue_reset(); + + btpad_queue_hci_read_bd_addr(); + btpad_queue_l2cap_register_service(PSM_HID_CONTROL, 672); // TODO: Where did I get 672 for mtu? + btpad_queue_l2cap_register_service(PSM_HID_INTERRUPT, 672); + btpad_queue_hci_inquiry(HCI_INQUIRY_LAP, 3, 1); + } else if(packet[2] > HCI_STATE_WORKING && btpad_iface && btpad_device) + { btpad_disconnect_pad(); + btpad_queue_reset(); + } + break; + + case HCI_EVENT_COMMAND_STATUS: + btpad_queue_run(packet[3]); break; case HCI_EVENT_COMMAND_COMPLETE: + btpad_queue_run(packet[2]); + if (COMMAND_COMPLETE_EVENT(packet, (*hci_read_bd_addr_ptr))) { - if (packet[5]) - ios_add_log_message("BTpad: Failed to get local address (E: %02X)", packet[5]); - else + if (!packet[5]) { bt_flip_addr_ptr(event_addr, &packet[6]); ios_add_log_message("BTpad: Local address is %s", bd_addr_to_str_ptr(event_addr)); } - - ios_add_log_message("BTpad: Registering HID INTERRUPT service"); - bt_send_cmd_ptr(l2cap_register_service_ptr, PSM_HID_INTERRUPT, 672); + else + ios_add_log_message("BTpad: Failed to get local address (Status: %02X)", packet[5]); } break; case L2CAP_EVENT_SERVICE_REGISTERED: { uint32_t psm = READ_BT_16(packet, 3); - if (packet[2]) - ios_add_log_message("BTpad: Failed to register HID service (PSM: %02X, E: %02X)", psm, packet[2]); - else if (psm == PSM_HID_INTERRUPT) - { + + if (!packet[2] && psm == PSM_HID_INTERRUPT) ios_add_log_message("BTpad: HID INTERRUPT service registered"); - ios_add_log_message("BTpad: Registering HID CONTROL service"); - bt_send_cmd_ptr(l2cap_register_service_ptr, PSM_HID_CONTROL, 672); - } - else if(psm == PSM_HID_CONTROL) - { + else if (!packet[2] && psm == PSM_HID_CONTROL) ios_add_log_message("BTpad: HID CONTROL service registered"); - ios_add_log_message("BTpad: Starting inquiry"); - bt_send_cmd_ptr(hci_inquiry_ptr, HCI_INQUIRY_LAP, 3, 1); - } + else if (!packet[2]) + ios_add_log_message("BTpad: Unknown service registered (PSM: %02X)", psm); + else + ios_add_log_message("BTpad: Got failed 'Service Registered' event (PSM: %02X, Status: %02X)", psm, packet[2]); } break; @@ -161,24 +160,16 @@ void btpad_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet if (btpad_connection.state == BTPAD_WANT_INQ_COMPLETE) { ios_add_log_message("BTpad: Got inquiry complete; connecting\n"); - bt_send_cmd_ptr(l2cap_create_channel_ptr, btpad_connection.address, PSM_HID_CONTROL); - } - else if(btpad_connection.state == BTPAD_EMPTY) - { - if (btpad_connection.laps < 40) - { - btpad_connection.laps ++; - bt_send_cmd_ptr(hci_inquiry_ptr, HCI_INQUIRY_LAP, 3, 1); - } - else - ios_add_log_message("BTpad: Did not find wiimote, will stop searching"); + btpad_queue_l2cap_create_channel(btpad_connection.address, PSM_HID_CONTROL); + btpad_queue_l2cap_create_channel(btpad_connection.address, PSM_HID_INTERRUPT); } + + btpad_queue_hci_inquiry(HCI_INQUIRY_LAP, 3, 1); } break; case L2CAP_EVENT_CHANNEL_OPENED: { - const uint8_t status = packet[2]; 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); @@ -190,30 +181,26 @@ void btpad_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet break; } - if (status == 0) + if (!packet[2]) { ios_add_log_message("BTpad: L2CAP channel opened for psm: %02X", psm); if (psm == PSM_HID_CONTROL) - { btpad_connection.channels[0] = channel_id; - - ios_add_log_message("BTpad: Got HID CONTROL channel; Opening INTERRUPT"); - bt_send_cmd_ptr(l2cap_create_channel_ptr, btpad_connection.address, PSM_HID_INTERRUPT); - } else if (psm == PSM_HID_INTERRUPT) - { btpad_connection.channels[1] = channel_id; - - ios_add_log_message("BTpad: Got HID INTERRUPT channel; Requesting name"); - bt_send_cmd_ptr(hci_remote_name_request_ptr, btpad_connection.address, btpad_connection.page_scan_repetition_mode, - 0, btpad_connection.clock_offset | 0x8000); - } else - ios_add_log_message("BTpad: Got unknown L2CAP channel, ignoring"); + ios_add_log_message("BTpad: Got unknown L2CAP PSM, ignoring (PSM: %02X)", psm); + + if (btpad_connection.channels[0] && btpad_connection.channels[1]) + { + ios_add_log_message("BTpad: Got both L2CAP channels, requesting name"); + btpad_queue_hci_remote_name_request(btpad_connection.address, btpad_connection.page_scan_repetition_mode, + 0, btpad_connection.clock_offset | 0x8000); + } } else - ios_add_log_message("BTpad: Failed to open WiiMote L2CAP channel (PSM: %02X, E: %02X)", psm, status); + ios_add_log_message("BTpad: Failed to open L2CAP channel (PSM: %02X, Status: %02X)", psm, packet[2]); } break; @@ -233,20 +220,18 @@ void btpad_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet ios_add_log_message("BTpad: Connection is for unregnized handle or address, denying"); // TODO: Check error code - bt_send_cmd_ptr(l2cap_decline_connection_ptr, 0x15); + btpad_queue_l2cap_decline_connection(channel_id, 0x15); break; } btpad_connection.channels[interrupt] = channel_id; - - bt_send_cmd_ptr(l2cap_accept_connection_ptr, btpad_connection.channels[interrupt]); - ios_add_log_message("BTpad: L2CAP Connection accepted"); + btpad_queue_l2cap_accept_connection(channel_id); if (btpad_connection.channels[0] && btpad_connection.channels[1]) { ios_add_log_message("BTpad: Got both L2CAP channels, requesting name"); - bt_send_cmd_ptr(hci_remote_name_request_ptr, btpad_connection.address, 0, 0, 0); + btpad_queue_hci_remote_name_request(btpad_connection.address, 0, 0, 0); } } break; @@ -281,7 +266,7 @@ void btpad_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet ios_add_log_message("BTpad: Sending PIN"); bt_flip_addr_ptr(event_addr, &packet[2]); - bt_send_cmd_ptr(hci_pin_code_request_reply_ptr, event_addr, 6, &packet[2]); + btpad_queue_hci_pin_code_request_reply(event_addr, &packet[2]); } break; } diff --git a/ios/RetroArch/input/BTStack/btpad_queue.c b/ios/RetroArch/input/BTStack/btpad_queue.c new file mode 100644 index 0000000000..2e7cc65bff --- /dev/null +++ b/ios/RetroArch/input/BTStack/btpad_queue.c @@ -0,0 +1,258 @@ +/* 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. + * + * 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 . + */ + +#include "btdynamic.h" +#include "btpad.h" +#include "btpad_queue.h" + +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; + + struct // For wiimote only + { + bd_addr_t bd_addr; + bd_addr_t pin; + } hci_pin_code_request_reply; + + struct + { + uint16_t psm; + uint16_t mtu; + } l2cap_register_service; + + struct + { + bd_addr_t bd_addr; + uint16_t psm; + } l2cap_create_channel; + + struct + { + uint16_t cid; + } l2cap_accept_connection; + + struct + { + uint16_t cid; + uint8_t reason; + } l2cap_decline_connection; + }; +}; + +struct btpad_queue_command commands[64]; +static uint32_t insert_position; +static uint32_t read_position; +static uint32_t can_run; + +#define INCPOS(POS) { POS##_position = (POS##_position + 1) % 64; } + +void btpad_queue_reset() +{ + insert_position = 0; + read_position = 0; +} + +void btpad_queue_run(uint32_t count) +{ + can_run = count; + + btpad_queue_process(); +} + +void btpad_queue_process() +{ + for (; can_run && (insert_position != read_position); can_run --) + { + if (insert_position == read_position) + { + can_run ++; + return; + } + + struct btpad_queue_command* cmd = &commands[read_position]; + + 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); + else if (cmd->command == l2cap_register_service_ptr) + bt_send_cmd_ptr(cmd->command, cmd->l2cap_register_service.psm, cmd->l2cap_register_service.mtu); + else if (cmd->command == l2cap_create_channel_ptr) + bt_send_cmd_ptr(cmd->command, cmd->l2cap_create_channel.bd_addr, cmd->l2cap_create_channel.psm); + else if (cmd->command == l2cap_accept_connection_ptr) + bt_send_cmd_ptr(cmd->command, cmd->l2cap_accept_connection.cid); + else if (cmd->command == l2cap_decline_connection_ptr) + bt_send_cmd_ptr(cmd->command, cmd->l2cap_decline_connection.cid, cmd->l2cap_decline_connection.reason); + + INCPOS(read); + } +} + +void btpad_queue_btstack_set_power_mode(uint8_t on) +{ + struct btpad_queue_command* cmd = &commands[insert_position]; + + cmd->command = btstack_set_power_mode_ptr; + cmd->btstack_set_power_mode.on = on; + + INCPOS(insert); + btpad_queue_process(); +} + +void btpad_queue_hci_read_bd_addr() +{ + struct btpad_queue_command* cmd = &commands[insert_position]; + + cmd->command = hci_read_bd_addr_ptr; + + INCPOS(insert); + btpad_queue_process(); +} + +void btpad_queue_hci_disconnect(uint16_t handle, uint8_t reason) +{ + struct btpad_queue_command* cmd = &commands[insert_position]; + + cmd->command = hci_disconnect_ptr; + cmd->hci_disconnect.handle = handle; + cmd->hci_disconnect.reason = reason; + + INCPOS(insert); + btpad_queue_process(); +} + +void btpad_queue_hci_inquiry(uint32_t lap, uint8_t length, uint8_t num_responses) +{ + struct btpad_queue_command* cmd = &commands[insert_position]; + + cmd->command = hci_inquiry_ptr; + cmd->hci_inquiry.lap = lap; + cmd->hci_inquiry.length = length; + cmd->hci_inquiry.num_responses = num_responses; + + INCPOS(insert); + btpad_queue_process(); +} + +void btpad_queue_hci_remote_name_request(bd_addr_t bd_addr, uint8_t page_scan_repetition_mode, uint8_t reserved, uint16_t clock_offset) +{ + struct btpad_queue_command* cmd = &commands[insert_position]; + + 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; + + INCPOS(insert); + btpad_queue_process(); +} + +void btpad_queue_hci_pin_code_request_reply(bd_addr_t bd_addr, bd_addr_t pin) +{ + struct btpad_queue_command* cmd = &commands[insert_position]; + + 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)); + + INCPOS(insert); + btpad_queue_process(); +} + +void btpad_queue_l2cap_register_service(uint16_t psm, uint16_t mtu) +{ + struct btpad_queue_command* cmd = &commands[insert_position]; + + cmd->command = l2cap_register_service_ptr; + cmd->l2cap_register_service.psm = psm; + cmd->l2cap_register_service.mtu = mtu; + + INCPOS(insert); + btpad_queue_process(); +} + +void btpad_queue_l2cap_create_channel(bd_addr_t bd_addr, uint16_t psm) +{ + struct btpad_queue_command* cmd = &commands[insert_position]; + + cmd->command = l2cap_create_channel_ptr; + memcpy(cmd->l2cap_create_channel.bd_addr, bd_addr, sizeof(bd_addr_t)); + cmd->l2cap_create_channel.psm = psm; + + INCPOS(insert); + btpad_queue_process(); +} + +void btpad_queue_l2cap_accept_connection(uint16_t cid) +{ + struct btpad_queue_command* cmd = &commands[insert_position]; + + cmd->command = l2cap_accept_connection_ptr; + cmd->l2cap_accept_connection.cid = cid; + + INCPOS(insert); + btpad_queue_process(); +} + +void btpad_queue_l2cap_decline_connection(uint16_t cid, uint8_t reason) +{ + struct btpad_queue_command* cmd = &commands[insert_position]; + + cmd->command = l2cap_decline_connection_ptr; + cmd->l2cap_decline_connection.cid = cid; + cmd->l2cap_decline_connection.reason = reason; + + INCPOS(insert); + btpad_queue_process(); +} diff --git a/ios/RetroArch/input/BTStack/btpad_queue.h b/ios/RetroArch/input/BTStack/btpad_queue.h new file mode 100644 index 0000000000..6c9fc347f0 --- /dev/null +++ b/ios/RetroArch/input/BTStack/btpad_queue.h @@ -0,0 +1,34 @@ +/* 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. + * + * 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 . + */ + +#ifndef __IOS_RARCH_BTPAD_QUEUE_H__ +#define __IOS_RARCH_BTPAD_QUEUE_H__ + +void btpad_queue_reset(); +void btpad_queue_run(uint32_t count); +void btpad_queue_process(); + +void btpad_queue_btstack_set_power_mode(uint8_t on); +void btpad_queue_hci_read_bd_addr(); +void btpad_queue_hci_disconnect(uint16_t handle, uint8_t reason); +void btpad_queue_hci_inquiry(uint32_t lap, uint8_t length, uint8_t num_responses); +void btpad_queue_hci_remote_name_request(bd_addr_t bd_addr, uint8_t page_scan_repetition_mode, uint8_t reserved, uint16_t clock_offset); +void btpad_queue_hci_pin_code_request_reply(bd_addr_t bd_addr, bd_addr_t pin); +void btpad_queue_l2cap_register_service(uint16_t psm, uint16_t mtu); +void btpad_queue_l2cap_create_channel(bd_addr_t bd_addr, uint16_t psm); +void btpad_queue_l2cap_accept_connection(uint16_t cid); +void btpad_queue_l2cap_decline_connection(uint16_t cid, uint8_t reason); + +#endif