From fdbb5af441e3db35f07de4b0ec11d6d904896304 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Sun, 5 Oct 2014 00:58:47 +0200 Subject: [PATCH] (BTPad) Cleanups --- apple/iOS/bluetooth/btdynamic.c | 12 ++-- apple/iOS/bluetooth/btpad.c | 36 +++++----- apple/iOS/bluetooth/btpad_queue.c | 33 ++++++--- apple/iOS/bluetooth/btstack/btstack.h | 38 +++++++---- apple/iOS/bluetooth/btstack/linked_list.h | 5 +- apple/iOS/bluetooth/btstack/run_loop.h | 83 ++++++++++++++--------- apple/iOS/bluetooth/btstack/sdp_util.h | 5 +- apple/iOS/bluetooth/btstack/utils.h | 48 ++++++------- 8 files changed, 155 insertions(+), 105 deletions(-) diff --git a/apple/iOS/bluetooth/btdynamic.c b/apple/iOS/bluetooth/btdynamic.c index f0881ce9e4..b0d8a5d643 100644 --- a/apple/iOS/bluetooth/btdynamic.c +++ b/apple/iOS/bluetooth/btdynamic.c @@ -73,8 +73,6 @@ bool btstack_try_load(void) if (btstack_tested) return btstack_loaded; - RARCH_LOG("BTstack: Attempting to load\n"); - btstack_tested = true; btstack_loaded = false; @@ -82,8 +80,7 @@ bool btstack_try_load(void) if (!btstack) { - RARCH_LOG("BTstack: /usr/lib/libBTstack.dylib not loadable\n"); - RARCH_LOG("BTstack: Not loaded\n"); + RARCH_ERR("[BTstack]: Not loaded\n"); return false; } @@ -93,9 +90,8 @@ bool btstack_try_load(void) if (!*grabbers[i].target) { - RARCH_LOG("BTstack: Symbol %s not found in /usr/lib/libBTstack.dylib\n", grabbers[i].name); - RARCH_LOG("BTstack: Not loaded\n"); - + RARCH_ERR("[BTstack]: Symbol %s not found, not loaded.\n", grabbers[i].name); + dlclose(btstack); return false; } @@ -104,7 +100,7 @@ bool btstack_try_load(void) run_loop_init_ptr(RUN_LOOP_COCOA); bt_register_packet_handler_ptr(btpad_packet_handler); - RARCH_LOG("BTstack: Loaded\n"); + RARCH_LOG("[BTstack]: Loaded.\n"); btstack_loaded = true; return true; diff --git a/apple/iOS/bluetooth/btpad.c b/apple/iOS/bluetooth/btpad.c index 6e655a5f86..f6bdceb2b8 100644 --- a/apple/iOS/bluetooth/btpad.c +++ b/apple/iOS/bluetooth/btpad.c @@ -43,7 +43,9 @@ struct pad_connection bd_addr_t address; uint16_t handle; - uint16_t channels[2]; /* 0: Control, 1: Interrupt */ + + /* 0: Control, 1: Interrupt */ + uint16_t channels[2]; }; static bool inquiry_off; @@ -149,7 +151,7 @@ void btpad_packet_handler(uint8_t packet_type, { case BTSTACK_EVENT_STATE: { - RARCH_LOG("BTstack: HCI State %d\n", packet[2]); + RARCH_LOG("[BTstack]: HCI State %d.\n", packet[2]); switch (packet[2]) { @@ -186,10 +188,10 @@ void btpad_packet_handler(uint8_t packet_type, { bt_flip_addr_ptr(event_addr, &packet[6]); if (!packet[5]) - RARCH_LOG("BTpad: Local address is %s\n", + 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", + RARCH_LOG("[BTpad]: Failed to get local address (Status: %02X).\n", packet[5]); } } @@ -207,7 +209,7 @@ void btpad_packet_handler(uint8_t packet_type, if (!connection) return; - RARCH_LOG("BTpad: Inquiry found device\n"); + RARCH_LOG("[BTpad]: Inquiry found device\n"); memset(connection, 0, sizeof(struct pad_connection)); memcpy(connection->address, event_addr, sizeof(bd_addr_t)); @@ -246,11 +248,11 @@ void btpad_packet_handler(uint8_t packet_type, { if (!connection) { - RARCH_LOG("BTpad: Got L2CAP 'Channel Opened' event for unrecognized device\n"); + RARCH_LOG("[BTpad]: Got L2CAP 'Channel Opened' event for unrecognized device.\n"); break; } - RARCH_LOG("BTpad: L2CAP channel opened: (PSM: %02X)\n", psm); + RARCH_LOG("[BTpad]: L2CAP channel opened: (PSM: %02X)\n", psm); connection->handle = handle; if (psm == PSM_HID_CONTROL) @@ -258,18 +260,18 @@ void btpad_packet_handler(uint8_t packet_type, else if (psm == PSM_HID_INTERRUPT) connection->channels[1] = channel_id; else - RARCH_LOG("BTpad: Got unknown L2CAP PSM, ignoring (PSM: %02X)\n", psm); + RARCH_LOG("[BTpad]: Got unknown L2CAP PSM, ignoring (PSM: %02X).\n", psm); if (connection->channels[0] && connection->channels[1]) { - RARCH_LOG("BTpad: Got both L2CAP channels, requesting name\n"); + RARCH_LOG("[BTpad]: Got both L2CAP channels, requesting name.\n"); btpad_queue_hci_remote_name_request( connection->address, 0, 0, 0); } } else - RARCH_LOG("BTpad: Got failed L2CAP 'Channel Opened' event (PSM: %02X, Status: %02X)\n", psm, packet[2]); + RARCH_LOG("[BTpad]: Got failed L2CAP 'Channel Opened' event (PSM: %02X, Status: %02X).\n", psm, packet[2]); } break; @@ -290,7 +292,7 @@ void btpad_packet_handler(uint8_t packet_type, if (!connection) break; - RARCH_LOG("BTpad: Got new incoming connection\n"); + RARCH_LOG("[BTpad]: Got new incoming connection\n"); memset(connection, 0, sizeof(struct pad_connection)); @@ -302,7 +304,7 @@ void btpad_packet_handler(uint8_t packet_type, connection->state = BTPAD_CONNECTING; } - RARCH_LOG("BTpad: Incoming L2CAP connection (PSM: %02X)\n", + RARCH_LOG("[BTpad]: Incoming L2CAP connection (PSM: %02X).\n", psm); bt_send_cmd_ptr(l2cap_accept_connection_ptr, channel_id); } @@ -318,11 +320,11 @@ void btpad_packet_handler(uint8_t packet_type, if (!connection) { - RARCH_LOG("BTpad: Got unexpected remote name, ignoring\n"); + RARCH_LOG("[BTpad]: Got unexpected remote name, ignoring.\n"); break; } - RARCH_LOG("BTpad: Got %.200s\n", (char*)&packet[9]); + RARCH_LOG("[BTpad]: Got %.200s.\n", (char*)&packet[9]); connection->slot = pad_connection_connect( (char*)packet + 9, connection, &btpad_connection_send_control); @@ -331,7 +333,7 @@ void btpad_packet_handler(uint8_t packet_type, break; case HCI_EVENT_PIN_CODE_REQUEST: - RARCH_LOG("BTpad: Sending WiiMote PIN\n"); + RARCH_LOG("[BTpad]: Sending Wiimote PIN.\n"); bt_flip_addr_ptr(event_addr, &packet[2]); btpad_queue_hci_pin_code_request_reply(event_addr, &packet[2]); @@ -356,13 +358,13 @@ void btpad_packet_handler(uint8_t packet_type, } } else - RARCH_LOG("BTpad: Got failed 'Disconnection Complete' event (Status: %02X)\n", packet[2]); + RARCH_LOG("[BTpad]: Got failed 'Disconnection Complete' event (Status: %02X).\n", packet[2]); } break; case L2CAP_EVENT_SERVICE_REGISTERED: if (packet[2]) - RARCH_LOG("BTpad: Got failed 'Service Registered' event (PSM: %02X, Status: %02X)\n", + RARCH_LOG("[BTpad]: Got failed 'Service Registered' event (PSM: %02X, Status: %02X).\n", READ_BT_16(packet, 3), packet[2]); break; } diff --git a/apple/iOS/bluetooth/btpad_queue.c b/apple/iOS/bluetooth/btpad_queue.c index a4c85a66da..13652858eb 100644 --- a/apple/iOS/bluetooth/btpad_queue.c +++ b/apple/iOS/bluetooth/btpad_queue.c @@ -1,5 +1,6 @@ /* RetroArch - A frontend for libretro. * Copyright (C) 2013-2014 - Jason Fetters + * Copyright (C) 2011-2014 - 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- @@ -93,24 +94,36 @@ void btpad_queue_process(void) return; if (cmd->command == btstack_set_power_mode_ptr) - bt_send_cmd_ptr(cmd->command, cmd->btstack_set_power_mode.on); + 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, + 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); + 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, + 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); + bt_send_cmd_ptr( + cmd->command, + cmd->hci_pin_code_request_reply.bd_addr, + 6, + cmd->hci_pin_code_request_reply.pin); INCPOS(read); } @@ -161,7 +174,8 @@ void btpad_queue_hci_disconnect(uint16_t handle, uint8_t reason) btpad_queue_process(); } -void btpad_queue_hci_inquiry(uint32_t lap, uint8_t length, uint8_t num_responses) +void btpad_queue_hci_inquiry(uint32_t lap, + uint8_t length, uint8_t num_responses) { struct btpad_queue_command* cmd = (struct btpad_queue_command*) &commands[insert_position]; @@ -190,7 +204,8 @@ void btpad_queue_hci_remote_name_request(bd_addr_t bd_addr, 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.page_scan_repetition_mode = + page_scan_repetition_mode; cmd->hci_remote_name_request.reserved = reserved; cmd->hci_remote_name_request.clock_offset = clock_offset; diff --git a/apple/iOS/bluetooth/btstack/btstack.h b/apple/iOS/bluetooth/btstack/btstack.h index 03c02a0fb2..416bb269aa 100644 --- a/apple/iOS/bluetooth/btstack/btstack.h +++ b/apple/iOS/bluetooth/btstack/btstack.h @@ -38,7 +38,8 @@ * */ -#pragma once +#ifndef _BTSTACK_HEADER_H +#define _BTSTACK_HEADER_H #include "hci_cmds.h" #include "run_loop.h" @@ -50,31 +51,40 @@ extern "C" { #endif -// Default TCP port for BTstack daemon +/* Default TCP port for BTstack daemon. */ #define BTSTACK_PORT 13333 -// UNIX domain socket for BTstack */ +/* UNIX domain socket for BTstack. */ #define BTSTACK_UNIX "/tmp/BTstack" -// packet handler -typedef void (*btstack_packet_handler_t) (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); +/* Packet handler. */ +typedef void (*btstack_packet_handler_t) (uint8_t packet_type, + uint16_t channel, uint8_t *packet, uint16_t size); -// optional: if called before bt_open, TCP socket is used instead of local unix socket -// note: address is not copied and must be valid during bt_open +/* Optional + * + * If called before bt_open, TCP socket is used + * instead of local UNIX socket. + * + * note: Address is not copied and must be + * valid during bt_open. + */ void bt_use_tcp(const char * address, uint16_t port); -// init BTstack library +/* Init BTstack library. */ int bt_open(void); -// stop using BTstack library +/* Stop using BTstack library. */ int bt_close(void); -// send hci cmd packet +/* Send HCI cmd packet. */ int bt_send_cmd(const hci_cmd_t *cmd, ...); -// register packet handler -- channel only valid for l2cap and rfcomm packets -// @returns old packet handler -btstack_packet_handler_t bt_register_packet_handler(btstack_packet_handler_t handler); +/* Register packet handler -- channel only valid + * for L2CAP and RFCOMM packets. + */ +btstack_packet_handler_t bt_register_packet_handler( + btstack_packet_handler_t handler); void bt_send_acl(uint8_t * data, uint16_t len); @@ -84,3 +94,5 @@ void bt_send_rfcomm(uint16_t rfcom_cid, uint8_t *data, uint16_t len); #if defined __cplusplus } #endif + +#endif diff --git a/apple/iOS/bluetooth/btstack/linked_list.h b/apple/iOS/bluetooth/btstack/linked_list.h index d9d4fba554..a49d9f0697 100644 --- a/apple/iOS/bluetooth/btstack/linked_list.h +++ b/apple/iOS/bluetooth/btstack/linked_list.h @@ -35,7 +35,8 @@ * Created by Matthias Ringwald on 7/13/09. */ -#pragma once +#ifndef _BTSTACK_LINKED_LIST_H +#define _BTSTACK_LINKED_LIST_H #if defined __cplusplus extern "C" { @@ -61,3 +62,5 @@ void test_linked_list(void); #if defined __cplusplus } #endif + +#endif diff --git a/apple/iOS/bluetooth/btstack/run_loop.h b/apple/iOS/bluetooth/btstack/run_loop.h index 3b3835fb75..f09bac03eb 100644 --- a/apple/iOS/bluetooth/btstack/run_loop.h +++ b/apple/iOS/bluetooth/btstack/run_loop.h @@ -50,69 +50,88 @@ extern "C" { #endif -typedef enum { - RUN_LOOP_POSIX = 1, - RUN_LOOP_COCOA, - RUN_LOOP_EMBEDDED +typedef enum +{ + RUN_LOOP_POSIX = 1, + RUN_LOOP_COCOA, + RUN_LOOP_EMBEDDED } RUN_LOOP_TYPE; -typedef struct data_source { - linked_item_t item; - int fd; // <-- file descriptor to watch or 0 - int (*process)(struct data_source *ds); // <-- do processing +typedef struct data_source +{ + linked_item_t item; + + /* File descriptor to watch or 0. */ + int fd; + + int (*process)(struct data_source *ds); } data_source_t; -typedef struct timer { - linked_item_t item; +typedef struct timer +{ + linked_item_t item; #ifdef HAVE_TIME - struct timeval timeout; // <-- next timeout + /* Next timeout. */ + struct timeval timeout; #endif #ifdef HAVE_TICK - uint32_t timeout; // timeout in system ticks + /* Timeout in system ticks. */ + uint32_t timeout; #endif - void (*process)(struct timer *ts); // <-- do processing + void (*process)(struct timer *ts); } timer_source_t; -// Set timer based on current time in milliseconds. +/* Set timer based on current time in milliseconds. */ void run_loop_set_timer(timer_source_t *a, uint32_t timeout_in_ms); -// Set callback that will be executed when timer expires. -void run_loop_set_timer_handler(timer_source_t *ts, void (*process)(timer_source_t *_ts)); +/* Set callback that will be executed when timer expires. */ +void run_loop_set_timer_handler(timer_source_t *ts, + void (*process)(timer_source_t *_ts)); -// Add/Remove timer source. +/* Add timer source. */ void run_loop_add_timer(timer_source_t *timer); + +/* Remove timer source. */ int run_loop_remove_timer(timer_source_t *timer); -// Init must be called before any other run_loop call. -// Use RUN_LOOP_EMBEDDED for embedded devices. +/* Init must be called before any other run_loop call. + * Use RUN_LOOP_EMBEDDED for embedded devices. + */ void run_loop_init(RUN_LOOP_TYPE type); -// Set data source callback. -void run_loop_set_data_source_handler(data_source_t *ds, int (*process)(data_source_t *_ds)); +/* Set data source callback. */ +void run_loop_set_data_source_handler(data_source_t *ds, + int (*process)(data_source_t *_ds)); - -// Add/Remove data source. +/* Add data source. */ void run_loop_add_data_source(data_source_t *dataSource); + +/* Remove data source. */ int run_loop_remove_data_source(data_source_t *dataSource); - -// Execute configured run loop. This function does not return. +/* Execute configured run loop. + * This function does not return. */ void run_loop_execute(void); -// hack to fix HCI timer handling +/* Hack to fix HCI timer handling. */ #ifdef HAVE_TICK -// Sets how many miliseconds has one tick. +/* Sets how many milliseconds has one tick. */ uint32_t embedded_ticks_for_ms(uint32_t time_in_ms); -// Queries the current time in ticks. + +/* Queries the current time in ticks. */ uint32_t embedded_get_ticks(void); + #endif #ifdef EMBEDDED -// Sets an internal flag that is checked in the critical section -// just before entering sleep mode. Has to be called by the interupt -// handler of a data source to signal the run loop that a new data -// is available. + +/* Sets an internal flag that is checked in the critical section + * just before entering sleep mode. Has to be called by the interupt + * handler of a data source to signal the run loop that a new data + * is available. + */ void embedded_trigger(void); + #endif #if defined __cplusplus } diff --git a/apple/iOS/bluetooth/btstack/sdp_util.h b/apple/iOS/bluetooth/btstack/sdp_util.h index 0ebb219766..e0ff06b44f 100644 --- a/apple/iOS/bluetooth/btstack/sdp_util.h +++ b/apple/iOS/bluetooth/btstack/sdp_util.h @@ -33,7 +33,8 @@ * sdp_util.h */ -#pragma once +#ifndef _BTSTACK_SDP_UTIL_H +#define _BTSTACK_SDP_UTIL_H #include @@ -136,3 +137,5 @@ void sdp_create_spp_service(uint8_t *service, int service_id, const char *n #if defined __cplusplus } #endif + +#endif diff --git a/apple/iOS/bluetooth/btstack/utils.h b/apple/iOS/bluetooth/btstack/utils.h index 3fea5d5158..370d8f6a0d 100644 --- a/apple/iOS/bluetooth/btstack/utils.h +++ b/apple/iOS/bluetooth/btstack/utils.h @@ -37,8 +37,8 @@ * Created by Matthias Ringwald on 7/23/09. */ -#pragma once - +#ifndef _BT_UTILS_H +#define _BT_UTILS_H #if defined __cplusplus extern "C" { @@ -46,76 +46,75 @@ extern "C" { #include -/** - * @brief hci connection handle type - */ +/* Connection handle type. */ typedef uint16_t hci_con_handle_t; -/** - * @brief Length of a bluetooth device address. - */ +/* Length of a Bluetooth device address. */ #define BD_ADDR_LEN 6 + typedef uint8_t bd_addr_t[BD_ADDR_LEN]; -/** - * @brief The link key type - */ +/* The link key type. */ #define LINK_KEY_LEN 16 + typedef uint8_t link_key_t[LINK_KEY_LEN]; -/** - * @brief The device name type - */ +/* The device name type. */ #define DEVICE_NAME_LEN 248 typedef uint8_t device_name_t[DEVICE_NAME_LEN+1]; - -// helper for BT little endian format +/* helper for BT little endian format. */ #define READ_BT_16( buffer, pos) ( ((uint16_t) buffer[pos]) | (((uint16_t)buffer[pos+1]) << 8)) #define READ_BT_24( buffer, pos) ( ((uint32_t) buffer[pos]) | (((uint32_t)buffer[pos+1]) << 8) | (((uint32_t)buffer[pos+2]) << 16)) #define READ_BT_32( buffer, pos) ( ((uint32_t) buffer[pos]) | (((uint32_t)buffer[pos+1]) << 8) | (((uint32_t)buffer[pos+2]) << 16) | (((uint32_t) buffer[pos+3])) << 24) -// helper for SDP big endian format +/* helper for SDP big endian format. */ #define READ_NET_16( buffer, pos) ( ((uint16_t) buffer[pos+1]) | (((uint16_t)buffer[pos ]) << 8)) #define READ_NET_32( buffer, pos) ( ((uint32_t) buffer[pos+3]) | (((uint32_t)buffer[pos+2]) << 8) | (((uint32_t)buffer[pos+1]) << 16) | (((uint32_t) buffer[pos])) << 24) -// HCI CMD OGF/OCF +/* HCI CMD OGF/OCF. */ #define READ_CMD_OGF(buffer) (buffer[1] >> 2) #define READ_CMD_OCF(buffer) ((buffer[1] & 0x03) << 8 | buffer[0]) -// check if command complete event for given command +/* Check if command complete event for given command. */ #define COMMAND_COMPLETE_EVENT(event,cmd) ( event[0] == HCI_EVENT_COMMAND_COMPLETE && READ_BT_16(event,3) == cmd.opcode) #define COMMAND_STATUS_EVENT(event,cmd) ( event[0] == HCI_EVENT_COMMAND_STATUS && READ_BT_16(event,4) == cmd.opcode) -// Code+Len=2, Pkts+Opcode=3; total=5 +/* Code+Len=2, Pkts+Opcode=3; total=5 */ #define OFFSET_OF_DATA_IN_COMMAND_COMPLETE 5 -// ACL Packet +/* ACL Packet. */ #define READ_ACL_CONNECTION_HANDLE( buffer ) ( READ_BT_16(buffer,0) & 0x0fff) #define READ_ACL_FLAGS( buffer ) ( buffer[1] >> 4 ) #define READ_ACL_LENGTH( buffer ) (READ_BT_16(buffer, 2)) -// L2CAP Packet +/* L2CAP Packet. */ #define READ_L2CAP_LENGTH(buffer) ( READ_BT_16(buffer, 4)) #define READ_L2CAP_CHANNEL_ID(buffer) ( READ_BT_16(buffer, 6)) void bt_store_16(uint8_t *buffer, uint16_t pos, uint16_t value); + void bt_store_32(uint8_t *buffer, uint16_t pos, uint32_t value); + void bt_flip_addr(bd_addr_t dest, bd_addr_t src); void net_store_16(uint8_t *buffer, uint16_t pos, uint16_t value); + void net_store_32(uint8_t *buffer, uint16_t pos, uint32_t value); void hexdump(void *data, int size); + void printUUID(uint8_t *uuid); -// @deprecated please use more convenient bd_addr_to_str +/* Deprecated - please use more convenient bd_addr_to_str. */ void print_bd_addr( bd_addr_t addr); + char * bd_addr_to_str(bd_addr_t addr); int sscan_bd_addr(uint8_t * addr_string, bd_addr_t addr); uint8_t crc8_check(uint8_t *data, uint16_t len, uint8_t check_sum); + uint8_t crc8_calc(uint8_t *data, uint16_t len); #define BD_ADDR_CMP(a,b) memcmp(a,b, BD_ADDR_LEN) @@ -124,4 +123,5 @@ uint8_t crc8_calc(uint8_t *data, uint16_t len); #if defined __cplusplus } #endif - + +#endif