2013-03-18 20:32:24 -04:00
|
|
|
/* RetroArch - A frontend for libretro.
|
2014-01-01 01:50:59 +01:00
|
|
|
* Copyright (C) 2013-2014 - Jason Fetters
|
2013-03-18 20:32:24 -04:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
2014-01-05 20:12:04 -05:00
|
|
|
#include <pthread.h>
|
2013-03-18 20:32:24 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <dlfcn.h>
|
2013-03-23 21:12:29 -04:00
|
|
|
#include <CoreFoundation/CFRunLoop.h>
|
2013-03-19 19:04:51 -04:00
|
|
|
|
2013-09-05 01:20:56 -04:00
|
|
|
#include "apple/common/rarch_wrapper.h"
|
2013-03-24 18:23:46 -04:00
|
|
|
|
2013-03-19 19:04:51 -04:00
|
|
|
#define BUILDING_BTDYNAMIC
|
2013-03-18 20:32:24 -04:00
|
|
|
#include "btdynamic.h"
|
|
|
|
|
2013-03-19 19:04:51 -04:00
|
|
|
#define GRAB(A) {#A, (void**)&A##_ptr}
|
2013-03-18 20:32:24 -04:00
|
|
|
static struct
|
|
|
|
{
|
|
|
|
const char* name;
|
|
|
|
void** target;
|
|
|
|
} grabbers[] =
|
|
|
|
{
|
2013-03-19 19:04:51 -04:00
|
|
|
GRAB(bt_open),
|
2014-01-05 20:12:04 -05:00
|
|
|
GRAB(bt_close),
|
2013-03-19 19:04:51 -04:00
|
|
|
GRAB(bt_flip_addr),
|
2013-05-24 17:04:46 -04:00
|
|
|
GRAB(bd_addr_to_str),
|
2013-03-19 19:04:51 -04:00
|
|
|
GRAB(bt_register_packet_handler),
|
|
|
|
GRAB(bt_send_cmd),
|
|
|
|
GRAB(bt_send_l2cap),
|
|
|
|
GRAB(run_loop_init),
|
2013-03-23 21:12:29 -04:00
|
|
|
GRAB(run_loop_execute),
|
2013-06-19 16:35:32 -04:00
|
|
|
|
2013-03-19 19:04:51 -04:00
|
|
|
GRAB(btstack_set_power_mode),
|
|
|
|
GRAB(hci_delete_stored_link_key),
|
2013-03-25 18:25:21 -04:00
|
|
|
GRAB(hci_disconnect),
|
2013-05-24 17:04:46 -04:00
|
|
|
GRAB(hci_read_bd_addr),
|
2013-03-19 19:04:51 -04:00
|
|
|
GRAB(hci_inquiry),
|
|
|
|
GRAB(hci_inquiry_cancel),
|
|
|
|
GRAB(hci_pin_code_request_reply),
|
2013-03-24 16:18:57 -04:00
|
|
|
GRAB(hci_pin_code_request_negative_reply),
|
2013-03-19 19:04:51 -04:00
|
|
|
GRAB(hci_remote_name_request),
|
|
|
|
GRAB(hci_remote_name_request_cancel),
|
|
|
|
GRAB(hci_write_authentication_enable),
|
|
|
|
GRAB(hci_write_inquiry_mode),
|
|
|
|
GRAB(l2cap_create_channel),
|
2013-03-23 19:25:09 -04:00
|
|
|
GRAB(l2cap_register_service),
|
|
|
|
GRAB(l2cap_accept_connection),
|
2013-03-24 16:18:57 -04:00
|
|
|
GRAB(l2cap_decline_connection),
|
2013-03-18 20:32:24 -04:00
|
|
|
{0, 0}
|
|
|
|
};
|
|
|
|
|
2013-03-25 18:25:21 -04:00
|
|
|
extern void btpad_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
|
2013-03-23 21:12:29 -04:00
|
|
|
|
|
|
|
static bool btstack_tested;
|
|
|
|
static bool btstack_loaded;
|
2014-01-05 20:12:04 -05:00
|
|
|
|
|
|
|
static pthread_t btstack_thread;
|
|
|
|
static CFRunLoopSourceRef btstack_quit_source;
|
2013-03-23 21:12:29 -04:00
|
|
|
|
2013-06-21 21:39:36 -04:00
|
|
|
bool btstack_try_load()
|
2013-03-18 20:32:24 -04:00
|
|
|
{
|
2013-03-23 21:12:29 -04:00
|
|
|
assert(sizeof(void**) == sizeof(void(*)()));
|
2013-03-18 20:32:24 -04:00
|
|
|
|
2013-03-23 21:12:29 -04:00
|
|
|
if (btstack_tested)
|
|
|
|
return btstack_loaded;
|
2013-03-24 18:23:46 -04:00
|
|
|
|
2013-12-24 15:27:44 -05:00
|
|
|
RARCH_LOG("BTstack: Attempting to load\n");
|
2013-03-23 21:12:29 -04:00
|
|
|
|
|
|
|
btstack_tested = true;
|
|
|
|
btstack_loaded = false;
|
2013-03-18 20:32:24 -04:00
|
|
|
|
|
|
|
void* btstack = dlopen("/usr/lib/libBTstack.dylib", RTLD_LAZY);
|
|
|
|
|
|
|
|
if (!btstack)
|
2013-03-24 18:23:46 -04:00
|
|
|
{
|
2013-12-24 15:27:44 -05:00
|
|
|
RARCH_LOG("BTstack: /usr/lib/libBTstack.dylib not loadable\n");
|
|
|
|
RARCH_LOG("BTstack: Not loaded\n");
|
2013-03-18 20:32:24 -04:00
|
|
|
return false;
|
2013-03-24 18:23:46 -04:00
|
|
|
}
|
2013-03-18 20:32:24 -04:00
|
|
|
|
|
|
|
for (int i = 0; grabbers[i].name; i ++)
|
|
|
|
{
|
|
|
|
*grabbers[i].target = dlsym(btstack, grabbers[i].name);
|
|
|
|
|
|
|
|
if (!*grabbers[i].target)
|
|
|
|
{
|
2013-12-24 15:27:44 -05:00
|
|
|
RARCH_LOG("BTstack: Symbol %s not found in /usr/lib/libBTstack.dylib\n", grabbers[i].name);
|
|
|
|
RARCH_LOG("BTstack: Not loaded\n");
|
2013-03-24 18:23:46 -04:00
|
|
|
|
2013-03-18 20:32:24 -04:00
|
|
|
dlclose(btstack);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-02 20:50:22 -04:00
|
|
|
run_loop_init_ptr(RUN_LOOP_COCOA);
|
|
|
|
bt_register_packet_handler_ptr(btpad_packet_handler);
|
|
|
|
|
2013-12-24 15:27:44 -05:00
|
|
|
RARCH_LOG("BTstack: Loaded\n");
|
2013-03-23 21:12:29 -04:00
|
|
|
btstack_loaded = true;
|
|
|
|
|
2013-03-18 20:32:24 -04:00
|
|
|
return true;
|
|
|
|
}
|
2013-03-23 21:12:29 -04:00
|
|
|
|
2014-01-05 20:12:04 -05:00
|
|
|
void btstack_thread_stop()
|
|
|
|
{
|
|
|
|
bt_send_cmd_ptr(btstack_set_power_mode_ptr, HCI_POWER_OFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* btstack_thread_func(void* data)
|
|
|
|
{
|
|
|
|
RARCH_LOG("BTstack: Thread started");
|
|
|
|
|
|
|
|
if (bt_open_ptr())
|
|
|
|
{
|
|
|
|
RARCH_LOG("BTstack: bt_open() failed\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
CFRunLoopSourceContext ctx = { 0, 0, 0, 0, 0, 0, 0, 0, 0, btstack_thread_stop };
|
|
|
|
btstack_quit_source = CFRunLoopSourceCreate(0, 0, &ctx);
|
|
|
|
CFRunLoopAddSource(CFRunLoopGetCurrent(), btstack_quit_source, kCFRunLoopCommonModes);
|
|
|
|
|
|
|
|
RARCH_LOG("BTstack: Turning on\n");
|
|
|
|
bt_send_cmd_ptr(btstack_set_power_mode_ptr, HCI_POWER_ON);
|
|
|
|
|
|
|
|
RARCH_LOG("BTstack: Running\n");
|
|
|
|
CFRunLoopRun();
|
|
|
|
|
|
|
|
RARCH_LOG("BTstack: Done\n");
|
|
|
|
|
|
|
|
CFRunLoopSourceInvalidate(btstack_quit_source);
|
|
|
|
CFRelease(btstack_quit_source);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-21 21:39:36 -04:00
|
|
|
void btstack_set_poweron(bool on)
|
2013-03-23 21:12:29 -04:00
|
|
|
{
|
2013-06-21 21:39:36 -04:00
|
|
|
if (!btstack_try_load())
|
2013-03-25 18:25:21 -04:00
|
|
|
return;
|
2013-06-21 21:39:36 -04:00
|
|
|
|
2014-01-05 20:12:04 -05:00
|
|
|
if (on && !btstack_thread)
|
|
|
|
pthread_create(&btstack_thread, 0, btstack_thread_func, 0);
|
|
|
|
else if (!on && btstack_thread && btstack_quit_source)
|
2013-03-24 18:23:46 -04:00
|
|
|
{
|
2014-01-05 20:12:04 -05:00
|
|
|
CFRunLoopSourceSignal(btstack_quit_source);
|
|
|
|
pthread_join(btstack_thread, 0);
|
|
|
|
btstack_thread = 0;
|
2013-03-24 18:23:46 -04:00
|
|
|
}
|
2013-03-23 21:12:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool btstack_is_running()
|
|
|
|
{
|
2014-01-05 20:12:04 -05:00
|
|
|
return btstack_thread;
|
2013-03-23 21:12:29 -04:00
|
|
|
}
|