From 2ed6235c6196977fad37720ae86bdb392f9820c1 Mon Sep 17 00:00:00 2001 From: "matthias.ringwald" Date: Sat, 9 Jan 2010 18:25:30 +0000 Subject: [PATCH] added iPhone Bluetooth toggle functions --- src/bt_control.h | 4 +-- src/bt_control_iphone.h | 3 ++ src/bt_control_iphone.m | 71 +++++++++++++++++++++++++++++++++++++---- src/daemon.c | 13 ++++++++ src/hci.c | 12 ++++++- src/hci.h | 8 ++++- src/hci_cmds.c | 10 +++++- 7 files changed, 109 insertions(+), 12 deletions(-) diff --git a/src/bt_control.h b/src/bt_control.h index 1e41acc56..ca2a8b1ec 100644 --- a/src/bt_control.h +++ b/src/bt_control.h @@ -43,6 +43,6 @@ typedef struct { int (*on)(void *config); // <-- turn BT module on and configure int (*off)(void *config); // <-- turn BT module off - int (*valid)(void *confif); // <-- test if hardware can be supported - const char * (*name)(void *config); // <-- return hardware name + int (*valid)(void *config); // <-- test if hardware can be supported + const char * (*name)(void *config); // <-- return hardware name } bt_control_t; diff --git a/src/bt_control_iphone.h b/src/bt_control_iphone.h index 13c9fde4b..b75fc3fe8 100644 --- a/src/bt_control_iphone.h +++ b/src/bt_control_iphone.h @@ -41,3 +41,6 @@ extern bt_control_t bt_control_iphone; +// control system Bluetooth +int iphone_system_bt_enabled(); +void iphone_system_bt_set_enabled(int enabled); diff --git a/src/bt_control_iphone.m b/src/bt_control_iphone.m index c6ee20fd0..b53f64f11 100644 --- a/src/bt_control_iphone.m +++ b/src/bt_control_iphone.m @@ -35,6 +35,8 @@ * control Bluetooth module using BlueTool * * Created by Matthias Ringwald on 5/19/09. + * + * Bluetooth Toggle by BigBoss */ #include "../config.h" @@ -70,6 +72,54 @@ kern_return_t IOObjectRelease(mach_port_t object); #endif #endif +// minimal BluetoothManager swiped from BigBoss SBSettings Toggle +#import +@class UIDevice; +@interface BluetoothManager : NSObject ++ (BluetoothManager *) sharedInstance; +- (void) setPowered:(BOOL)powered; +- (void) setEnabled:(BOOL)enabled; +- (BOOL) enabled; +@end +#define kAppBTServer CFSTR("com.apple.BTServer") +#define kKeyBTPowered CFSTR("defaultPoweredState") +#define kAppNetwork CFSTR("com.apple.preferences.network") +#define kKeyBTNetwork CFSTR("bluetooth-network") + +int iphone_system_bt_enabled(){ + if ([[BluetoothManager sharedInstance] enabled]) { + return 1; + } else { + return 0; + } +} + +void iphone_system_bt_set_enabled(int enabled) +{ + /* Get a copy of the global bluetooth server */ + BluetoothManager *bm = [BluetoothManager sharedInstance]; + if ( [bm enabled] && enabled) return; + if (![bm enabled] && !enabled) return; + + if (enabled) { + /* Store into preferences that bluetooth should start on system boot */ + CFPreferencesSetAppValue(kKeyBTNetwork, kCFBooleanTrue, kAppNetwork); + + /* Turn bluetooth on */ + [bm setPowered:YES]; + } else { + /* Store into preferences taht bluetooth should not start on system boot */ + CFPreferencesSetAppValue(kKeyBTNetwork, kCFBooleanFalse, kAppNetwork); + + /* Turn bluetooth off */ + [bm setEnabled:NO]; + } + /* Synchronize to preferences, e.g. write to disk, bluetooth settings */ + CFPreferencesAppSynchronize(kAppNetwork); +} + + + #define BUFF_LEN 80 static char buffer[BUFF_LEN+1]; @@ -92,10 +142,9 @@ static char *get_machine_name(void){ */ static int iphone_valid(void *config){ char * machine = get_machine_name(); - if (!strncmp("iPhone", machine, strlen("iPhone" ))) return 1; - if (!strncmp("iPod2,1", machine, strlen("iPod2,1"))) return 1; - if (!strncmp("iPod3,1", machine, strlen("iPod3,1"))) return 1; - return 0; + if (!strncmp("iPhone", machine, strlen("iPhone" ))) return 1; // iPhone OK + if (!strncmp("iPod1", machine, strlen("iPod1"))) return 0; // 1st gen touch no BT + return 1; } static const char * iphone_name(void *config){ @@ -276,16 +325,24 @@ static int iphone_write_initscript (hci_uart_config_t *uart_config, int output){ } static int iphone_on (void *transport_config){ - - // quick test if Bluetooth UART can be opened + hci_uart_config_t * hci_uart_config = (hci_uart_config_t*) transport_config; + +#if 0 + // quick test if Bluetooth UART can be opened int fd = open(hci_uart_config->device_name, O_RDWR | O_NOCTTY | O_NDELAY); if (fd == -1) { - perror("init_serialport: Unable to open port "); + perror("iphone_on: Unable to open port "); perror(hci_uart_config->device_name); return 1; } close(fd); +#else + if (iphone_system_bt_enabled()){ + perror("iphone_on: System Bluetooth enabled!"); + return 1; + } +#endif int err = 0; #if 0 diff --git a/src/daemon.c b/src/daemon.c index 47d81067f..032857394 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -118,6 +118,19 @@ static int btstack_command_handler(connection_t *connection, uint8_t *packet, ui case BTSTACK_GET_VERSION: hci_emit_btstack_version(); break; +#ifdef USE_BLUETOOL + case BTSTACK_SET_SYSTEM_BLUETOOTH_ENABLED: + iphone_system_bt_set_enabled(packet[3]); + // fall through .. :) + case BTSTACK_GET_SYSTEM_BLUETOOTH_ENABLED: + hci_emit_system_bluetooth_enabled(iphone_system_bt_enabled()); + break; +#else + case BTSTACK_SET_SYSTEM_BLUETOOTH_ENABLED: + case BTSTACK_GET_SYSTEM_BLUETOOTH_ENABLED: + hci_emit_system_bluetooth_enabled(0); + break; +#endif case L2CAP_CREATE_CHANNEL: bt_flip_addr(addr, &packet[3]); psm = READ_BT_16(packet, 9); diff --git a/src/hci.c b/src/hci.c index 4212c4d6c..6911ff62b 100644 --- a/src/hci.c +++ b/src/hci.c @@ -43,7 +43,8 @@ #include "hci.h" #include "hci_dump.h" -#include +#include "../include/btstack/hci_cmds.h" +#include "../include/btstack/version.h" // temp #include "l2cap.h" @@ -498,3 +499,12 @@ void hci_emit_btstack_version() { hci_stack.event_packet_handler(event, len); } +void hci_emit_system_bluetooth_enabled(uint8_t enabled){ + uint8_t len = 3; + uint8_t event[len]; + event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED; + event[1] = len - 3; + event[2] = enabled; + hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); + hci_stack.event_packet_handler(event, len); +} diff --git a/src/hci.h b/src/hci.h index a331dbee9..850e2687f 100644 --- a/src/hci.h +++ b/src/hci.h @@ -73,6 +73,12 @@ // get BTstack version #define BTSTACK_GET_VERSION 0x04 +// get system Bluetooth state +#define BTSTACK_GET_SYSTEM_BLUETOOTH_ENABLED 0x05 + +// set system Bluetooth state +#define BTSTACK_SET_SYSTEM_BLUETOOTH_ENABLED 0x06 + // create l2cap channel: @param bd_addr(48), psm (16) #define L2CAP_CREATE_CHANNEL 0x20 @@ -201,4 +207,4 @@ void hci_emit_l2cap_check_timeout(hci_connection_t *conn); void hci_emit_nr_connections_changed(); void hci_emit_hci_open_failed(); void hci_emit_btstack_version(); - +void hci_emit_system_bluetooth_enabled(uint8_t enabled); diff --git a/src/hci_cmds.c b/src/hci_cmds.c index 1c8eb4bd9..34cb4a2cb 100644 --- a/src/hci_cmds.c +++ b/src/hci_cmds.c @@ -268,7 +268,15 @@ OPCODE(OGF_BTSTACK, BTSTACK_SET_ACL_CAPTURE_MODE), "1" }; hci_cmd_t btstack_get_version = { - OPCODE(OGF_BTSTACK, BTSTACK_GET_VERSION), "" +OPCODE(OGF_BTSTACK, BTSTACK_GET_VERSION), "" +}; + +hci_cmd_t btstack_get_system_bluetooth_enabled = { +OPCODE(OGF_BTSTACK, BTSTACK_GET_SYSTEM_BLUETOOTH_ENABLED), "" +}; + +hci_cmd_t btstack_set_system_bluetooth_enabled = { +OPCODE(OGF_BTSTACK, BTSTACK_SET_SYSTEM_BLUETOOTH_ENABLED), "1" }; hci_cmd_t l2cap_create_channel = {