finished iPhone power control

This commit is contained in:
matthias.ringwald 2009-05-20 20:46:35 +00:00
parent 97addcc576
commit 11e23e5f86
6 changed files with 92 additions and 23 deletions

View File

@ -7,6 +7,8 @@
*
*/
#pragma once
typedef struct {
int (*on)(void *config); // <-- turn BT module on and configure
int (*off)(void *config); // <-- turn BT module off

View File

@ -16,7 +16,7 @@
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <errno.h>
#define BUFF_LEN 80
static char buffer[BUFF_LEN+1];
@ -44,27 +44,52 @@ static const char * iphone_name(void *config){
static int iphone_on (void *config){
// get path to script
strcpy(buffer, "./etc/bluetool/");
strcpy(buffer, "/etc/bluetool/");
char *machine = get_machine_name();
strcat(buffer, machine);
strcat(buffer, ".init.script");
// hack
// strcpy(buffer, "iPhone1,2.init.script");
// open script
int input = open(buffer, O_RDONLY);
// open tool
FILE * outputFile = popen("BlueTool", "r+");
int output = fileno(outputFile);
int pos = 0;
int mirror = 0;
int store = 1;
struct timeval noTime;
bzero (&noTime, sizeof(struct timeval));
while (1){
int chars = read(input, &buffer[pos], 1);
int ready;
do {
fd_set fds;
FD_ZERO(&fds);
FD_SET(output,&fds);
ready=select(output+1,&fds,NULL,NULL,&noTime);
if (ready>0)
{
if (FD_ISSET(output,&fds)) {
char singlechar = fgetc(outputFile);
printf("%c", singlechar);
}
}
} while (ready);
// end-of-line
if (chars == 0 || buffer[pos]=='\n' || buffer[pos]== '\r'){
if (store) {
// stored characters
write(fileno(stdout), buffer, pos+chars);
write(output, buffer, pos+chars);
}
if (mirror) {
write(fileno(stdout), "\n", 1);
write(output, "\n", 1);
}
pos = 0;
mirror = 0;
@ -78,7 +103,7 @@ static int iphone_on (void *config){
// mirror
if (mirror){
write(fileno(stdout), &buffer[pos], 1);
write(output, &buffer[pos], 1);
}
// store
@ -86,32 +111,27 @@ static int iphone_on (void *config){
pos++;
}
// enough chars, check for pskey store command
if (mirror == 0 && pos == 9) {
if (strncmp(buffer, "csr -p 0x", 9) != 0) {
write(fileno(stdout), buffer, pos);
mirror = 1;
store = 0;
}
}
// check for "csr -p 0x002a=0x0011" (20)
if (mirror == 0 && store == 1 && pos == 20) {
if (store == 1 && pos == 20) {
int pskey, value;
store = 0;
if (sscanf(buffer, "csr -p 0x%x=0x%x", &pskey, &value) == 2){
if (pskey == 0x01f9) { // UART MODE
write(fileno(stdout), "Skipping: ", 10);
write(fileno(stdout), buffer, pos);
write(output, "Skipping: ", 10);
write(output, buffer, pos);
mirror = 1;
} else if (pskey == 0x01be) { // UART Baud
write(fileno(stdout), "Skipping: ", 10);
write(fileno(stdout), buffer, pos);
write(output, "Skipping: ", 10);
write(output, buffer, pos);
mirror = 1;
} else {
// dump buffer and start forwarding
write(fileno(stdout), buffer, pos);
write(output, buffer, pos);
mirror = 1;
}
} else {
write(output, buffer, pos);
mirror = 1;
}
}
}

View File

@ -203,6 +203,34 @@
};
name = Release;
};
9C8852C10FC360E8004980E4 /* Debug iPhone */ = {
isa = XCBuildConfiguration;
buildSettings = {
ARCHS = "$(ARCHS_STANDARD_32_BIT)";
GCC_C_LANGUAGE_STANDARD = c99;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
ONLY_ACTIVE_ARCH = YES;
PREBINDING = NO;
SDKROOT = iphoneos2.0;
};
name = "Debug iPhone";
};
9C8852C20FC360E8004980E4 /* Debug iPhone */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
COPY_PHASE_STRIP = NO;
GCC_DYNAMIC_NO_PIC = NO;
GCC_ENABLE_FIX_AND_CONTINUE = YES;
GCC_MODEL_TUNING = G5;
GCC_OPTIMIZATION_LEVEL = 0;
INSTALL_PATH = /usr/local/bin;
PRODUCT_NAME = project;
};
name = "Debug iPhone";
};
/* End XCBuildConfiguration section */
/* Begin XCConfigurationList section */
@ -210,6 +238,7 @@
isa = XCConfigurationList;
buildConfigurations = (
1DEB928608733DD80010E9CD /* Debug */,
9C8852C20FC360E8004980E4 /* Debug iPhone */,
1DEB928708733DD80010E9CD /* Release */,
);
defaultConfigurationIsVisible = 0;
@ -219,6 +248,7 @@
isa = XCConfigurationList;
buildConfigurations = (
1DEB928A08733DD80010E9CD /* Debug */,
9C8852C10FC360E8004980E4 /* Debug iPhone */,
1DEB928B08733DD80010E9CD /* Release */,
);
defaultConfigurationIsVisible = 0;

View File

@ -155,11 +155,17 @@ void hci_register_acl_packet_handler (void (*handler)(uint8_t *packet, int size
hci_stack.acl_packet_handler = handler;
}
void hci_init(hci_transport_t *transport, void *config){
void hci_init(hci_transport_t *transport, void *config, bt_control_t *control){
// reference to use transport layer implementation
hci_stack.hci_transport = transport;
// references to used control implementation
hci_stack.control = control;
// reference to used config
hci_stack.config = config;
// empty cmd buffer
hci_stack.hci_cmd_buffer = malloc(3+255);
@ -181,6 +187,13 @@ void hci_init(hci_transport_t *transport, void *config){
}
int hci_power_control(HCI_POWER_MODE power_mode){
if (hci_stack.control) {
if (power_mode == HCI_POWER_ON) {
hci_stack.control->on(hci_stack.config);
} else if (power_mode == HCI_POWER_OFF){
hci_stack.control->off(hci_stack.config);
}
}
return 0;
}

View File

@ -11,6 +11,7 @@
#include <stdlib.h>
#include "hci_transport.h"
#include "bt_control.h"
// helper for BT little endian format
#define READ_BT_16( buffer, pos) (buffer[pos] | (buffer[pos+1] << 8))
@ -122,6 +123,9 @@ typedef struct hci_connection {
typedef struct {
hci_transport_t * hci_transport;
bt_control_t * control;
void * config;
uint8_t * hci_cmd_buffer;
hci_connection_t * connections;
@ -142,7 +146,7 @@ typedef struct {
// set up HCI
void hci_init(hci_transport_t *transport, void *config);
void hci_init(hci_transport_t *transport, void *config, bt_control_t *control);
void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, int size));

View File

@ -114,7 +114,7 @@ int main (int argc, const char * argv[]) {
config.flowcontrol = 1;
// init HCI
hci_init(transport, &config);
hci_init(transport, &config, NULL);
hci_power_control(HCI_POWER_ON);
//