2009-10-29 20:25:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2009 by Matthias Ringwald
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the copyright holders nor the names of
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
|
|
|
|
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
|
|
|
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-04-29 22:00:24 +00:00
|
|
|
/*
|
|
|
|
* hci.c
|
|
|
|
*
|
|
|
|
* Created by Matthias Ringwald on 4/29/09.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-08-26 21:35:05 +00:00
|
|
|
#include "../config.h"
|
|
|
|
|
2010-07-04 19:28:21 +00:00
|
|
|
#include "hci.h"
|
|
|
|
|
2009-05-09 17:34:17 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
2009-05-09 17:43:27 +00:00
|
|
|
#include <stdio.h>
|
2010-07-04 19:28:21 +00:00
|
|
|
|
2011-06-06 14:34:29 +00:00
|
|
|
#ifndef EMBEDDED
|
|
|
|
#include <unistd.h> // gethostbyname
|
|
|
|
#endif
|
|
|
|
|
2011-07-24 08:51:17 +00:00
|
|
|
#include "btstack_memory.h"
|
2010-07-04 19:28:21 +00:00
|
|
|
#include "debug.h"
|
2009-07-23 21:25:39 +00:00
|
|
|
#include "hci_dump.h"
|
2009-05-10 19:55:02 +00:00
|
|
|
|
2011-07-12 15:31:06 +00:00
|
|
|
#include <btstack/hci_cmds.h>
|
|
|
|
#include <btstack/version.h>
|
2010-01-09 11:01:23 +00:00
|
|
|
|
2010-01-26 18:28:21 +00:00
|
|
|
#define HCI_CONNECTION_TIMEOUT_MS 10000
|
2009-08-09 18:39:53 +00:00
|
|
|
|
2011-08-26 21:35:05 +00:00
|
|
|
#ifdef USE_BLUETOOL
|
|
|
|
#include "bt_control_iphone.h"
|
|
|
|
#endif
|
|
|
|
|
2009-07-24 20:17:46 +00:00
|
|
|
// the STACK is here
|
2009-05-17 20:32:14 +00:00
|
|
|
static hci_stack_t hci_stack;
|
|
|
|
|
2009-08-09 18:39:53 +00:00
|
|
|
/**
|
|
|
|
* get connection for a given handle
|
|
|
|
*
|
|
|
|
* @return connection OR NULL, if not found
|
|
|
|
*/
|
2010-01-25 22:22:55 +00:00
|
|
|
hci_connection_t * connection_for_handle(hci_con_handle_t con_handle){
|
2009-08-09 18:39:53 +00:00
|
|
|
linked_item_t *it;
|
|
|
|
for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
|
|
|
|
if ( ((hci_connection_t *) it)->con_handle == con_handle){
|
|
|
|
return (hci_connection_t *) it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-07-12 20:14:42 +00:00
|
|
|
static void hci_connection_timeout_handler(timer_source_t *timer){
|
2009-08-09 18:39:53 +00:00
|
|
|
hci_connection_t * connection = linked_item_get_user(&timer->item);
|
2011-07-25 21:12:09 +00:00
|
|
|
#ifdef HAVE_TIME
|
2009-08-09 18:39:53 +00:00
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, NULL);
|
2009-11-05 22:26:43 +00:00
|
|
|
if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) {
|
2009-08-09 18:39:53 +00:00
|
|
|
// connections might be timed out
|
|
|
|
hci_emit_l2cap_check_timeout(connection);
|
|
|
|
}
|
2011-07-25 21:12:09 +00:00
|
|
|
#endif
|
2011-08-05 14:53:18 +00:00
|
|
|
#ifdef HAVE_TICK
|
2011-07-25 21:12:09 +00:00
|
|
|
if (embedded_get_ticks() > connection->timestamp + embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){
|
|
|
|
// connections might be timed out
|
|
|
|
hci_emit_l2cap_check_timeout(connection);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS);
|
2009-08-09 18:39:53 +00:00
|
|
|
run_loop_add_timer(timer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hci_connection_timestamp(hci_connection_t *connection){
|
2011-06-05 09:58:33 +00:00
|
|
|
#ifdef HAVE_TIME
|
2009-08-09 18:39:53 +00:00
|
|
|
gettimeofday(&connection->timestamp, NULL);
|
2011-06-05 09:58:33 +00:00
|
|
|
#endif
|
2011-08-05 14:53:18 +00:00
|
|
|
#ifdef HAVE_TICK
|
2011-07-25 21:12:09 +00:00
|
|
|
connection->timestamp = embedded_get_ticks();
|
|
|
|
#endif
|
2009-08-09 18:39:53 +00:00
|
|
|
}
|
|
|
|
|
2009-08-02 13:20:32 +00:00
|
|
|
/**
|
|
|
|
* create connection for given address
|
|
|
|
*
|
2011-07-25 18:05:47 +00:00
|
|
|
* @return connection OR NULL, if no memory left
|
2009-08-02 13:20:32 +00:00
|
|
|
*/
|
|
|
|
static hci_connection_t * create_connection_for_addr(bd_addr_t addr){
|
2011-07-24 08:51:17 +00:00
|
|
|
hci_connection_t * conn = btstack_memory_hci_connection_get();
|
2009-08-02 13:20:32 +00:00
|
|
|
if (!conn) return NULL;
|
|
|
|
BD_ADDR_COPY(conn->address, addr);
|
|
|
|
conn->con_handle = 0xffff;
|
2011-07-22 10:09:45 +00:00
|
|
|
conn->authentication_flags = AUTH_FLAGS_NONE;
|
2009-08-09 18:39:53 +00:00
|
|
|
linked_item_set_user(&conn->timeout.item, conn);
|
|
|
|
conn->timeout.process = hci_connection_timeout_handler;
|
|
|
|
hci_connection_timestamp(conn);
|
2010-02-13 10:49:00 +00:00
|
|
|
conn->acl_recombination_length = 0;
|
2010-02-13 15:15:19 +00:00
|
|
|
conn->acl_recombination_pos = 0;
|
2010-07-25 15:51:03 +00:00
|
|
|
conn->num_acl_packets_sent = 0;
|
2009-08-02 13:20:32 +00:00
|
|
|
linked_list_add(&hci_stack.connections, (linked_item_t *) conn);
|
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
|
2009-05-19 21:49:12 +00:00
|
|
|
/**
|
2009-07-24 20:17:46 +00:00
|
|
|
* get connection for given address
|
2009-05-19 21:49:12 +00:00
|
|
|
*
|
|
|
|
* @return connection OR NULL, if not found
|
|
|
|
*/
|
2009-07-24 21:13:53 +00:00
|
|
|
static hci_connection_t * connection_for_address(bd_addr_t address){
|
2009-07-24 20:17:46 +00:00
|
|
|
linked_item_t *it;
|
|
|
|
for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
|
|
|
|
if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){
|
|
|
|
return (hci_connection_t *) it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-08-26 20:12:05 +00:00
|
|
|
/**
|
2010-08-26 20:26:48 +00:00
|
|
|
* add authentication flags and reset timer
|
2010-08-26 20:12:05 +00:00
|
|
|
*/
|
|
|
|
static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){
|
|
|
|
bd_addr_t addr;
|
|
|
|
bt_flip_addr(addr, *(bd_addr_t *) bd_addr);
|
|
|
|
hci_connection_t * conn = connection_for_address(addr);
|
|
|
|
if (conn) {
|
|
|
|
conn->authentication_flags |= flags;
|
2010-08-26 20:26:48 +00:00
|
|
|
hci_connection_timestamp(conn);
|
2010-08-26 20:12:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-26 20:26:48 +00:00
|
|
|
int hci_authentication_active_for_handle(hci_con_handle_t handle){
|
|
|
|
hci_connection_t * conn = connection_for_handle(handle);
|
|
|
|
if (!conn) return 0;
|
|
|
|
if (!conn->authentication_flags) return 0;
|
|
|
|
if (conn->authentication_flags & SENT_LINK_KEY_REPLY) return 0;
|
|
|
|
if (conn->authentication_flags & RECV_LINK_KEY_NOTIFICATION) return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-08-30 19:05:14 +00:00
|
|
|
void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){
|
|
|
|
if (hci_stack.remote_device_db) {
|
|
|
|
hci_stack.remote_device_db->delete_link_key(addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-26 20:12:05 +00:00
|
|
|
|
2009-08-15 21:31:23 +00:00
|
|
|
/**
|
|
|
|
* count connections
|
|
|
|
*/
|
2011-06-06 14:12:38 +00:00
|
|
|
static int nr_hci_connections(void){
|
2009-09-20 15:49:42 +00:00
|
|
|
int count = 0;
|
2009-08-15 21:31:23 +00:00
|
|
|
linked_item_t *it;
|
|
|
|
for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++);
|
|
|
|
return count;
|
|
|
|
}
|
2009-08-02 13:20:32 +00:00
|
|
|
|
2009-05-17 20:32:14 +00:00
|
|
|
/**
|
2009-07-23 21:43:37 +00:00
|
|
|
* Dummy handler called by HCI
|
2009-05-17 20:32:14 +00:00
|
|
|
*/
|
2010-07-18 16:30:16 +00:00
|
|
|
static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
|
2009-05-17 20:32:14 +00:00
|
|
|
}
|
|
|
|
|
2010-07-28 19:12:39 +00:00
|
|
|
uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){
|
|
|
|
hci_connection_t * connection = connection_for_handle(handle);
|
|
|
|
if (!connection) {
|
2011-07-22 18:34:18 +00:00
|
|
|
log_error("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle);
|
2010-07-28 19:12:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return connection->num_acl_packets_sent;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t hci_number_free_acl_slots(){
|
|
|
|
uint8_t free_slots = hci_stack.total_num_acl_packets;
|
|
|
|
linked_item_t *it;
|
|
|
|
for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
|
|
|
|
hci_connection_t * connection = (hci_connection_t *) it;
|
|
|
|
if (free_slots < connection->num_acl_packets_sent) {
|
2011-07-22 18:34:18 +00:00
|
|
|
log_error("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n");
|
2010-07-28 19:12:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
free_slots -= connection->num_acl_packets_sent;
|
|
|
|
}
|
|
|
|
return free_slots;
|
|
|
|
}
|
|
|
|
|
2011-06-21 18:19:07 +00:00
|
|
|
int hci_can_send_packet_now(uint8_t packet_type){
|
2011-07-12 20:14:42 +00:00
|
|
|
|
|
|
|
// check for async hci transport implementations
|
|
|
|
if (hci_stack.hci_transport->can_send_packet_now){
|
|
|
|
if (!hci_stack.hci_transport->can_send_packet_now(packet_type)){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check regular Bluetooth flow control
|
2011-06-21 18:19:07 +00:00
|
|
|
switch (packet_type) {
|
|
|
|
case HCI_ACL_DATA_PACKET:
|
|
|
|
return hci_number_free_acl_slots();
|
|
|
|
case HCI_COMMAND_DATA_PACKET:
|
2011-06-21 19:34:08 +00:00
|
|
|
return hci_stack.num_cmd_packets;
|
2011-06-21 18:19:07 +00:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-09 18:39:53 +00:00
|
|
|
int hci_send_acl_packet(uint8_t *packet, int size){
|
2010-02-13 15:15:19 +00:00
|
|
|
|
2010-07-29 19:35:34 +00:00
|
|
|
// check for free places on BT module
|
2011-06-18 10:57:37 +00:00
|
|
|
if (!hci_number_free_acl_slots()) return BTSTACK_ACL_BUFFERS_FULL;
|
2010-07-29 19:35:34 +00:00
|
|
|
|
2010-02-13 15:15:19 +00:00
|
|
|
hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
|
|
|
|
hci_connection_t *connection = connection_for_handle( con_handle);
|
2010-07-25 15:51:03 +00:00
|
|
|
if (!connection) return 0;
|
|
|
|
hci_connection_timestamp(connection);
|
2010-02-13 15:15:19 +00:00
|
|
|
|
2010-07-25 15:51:03 +00:00
|
|
|
// count packet
|
|
|
|
connection->num_acl_packets_sent++;
|
2011-07-22 18:31:02 +00:00
|
|
|
// log_info("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent);
|
2010-07-25 15:51:03 +00:00
|
|
|
|
2011-08-21 11:10:11 +00:00
|
|
|
// send packet
|
|
|
|
int err = hci_stack.hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size);
|
2010-08-16 17:23:22 +00:00
|
|
|
|
2011-08-21 11:10:11 +00:00
|
|
|
return err;
|
2009-08-09 18:39:53 +00:00
|
|
|
}
|
|
|
|
|
2009-05-17 20:32:14 +00:00
|
|
|
static void acl_handler(uint8_t *packet, int size){
|
2010-02-13 15:15:19 +00:00
|
|
|
|
|
|
|
// get info
|
|
|
|
hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
|
|
|
|
hci_connection_t *conn = connection_for_handle(con_handle);
|
|
|
|
uint8_t acl_flags = READ_ACL_FLAGS(packet);
|
|
|
|
uint16_t acl_length = READ_ACL_LENGTH(packet);
|
|
|
|
|
|
|
|
// ignore non-registered handle
|
|
|
|
if (!conn){
|
2011-07-22 18:34:18 +00:00
|
|
|
log_error( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle);
|
2010-02-13 15:15:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update idle timestamp
|
|
|
|
hci_connection_timestamp(conn);
|
|
|
|
|
|
|
|
// handle different packet types
|
|
|
|
switch (acl_flags & 0x03) {
|
|
|
|
|
|
|
|
case 0x01: // continuation fragment
|
|
|
|
|
|
|
|
// sanity check
|
|
|
|
if (conn->acl_recombination_pos == 0) {
|
2011-07-22 18:34:18 +00:00
|
|
|
log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle);
|
2010-02-13 15:15:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// append fragment payload (header already stored)
|
|
|
|
memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length );
|
|
|
|
conn->acl_recombination_pos += acl_length;
|
|
|
|
|
2011-07-22 18:34:18 +00:00
|
|
|
// log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", acl_length,
|
2011-07-07 21:47:47 +00:00
|
|
|
// conn->acl_recombination_pos, conn->acl_recombination_length);
|
2010-02-13 15:15:19 +00:00
|
|
|
|
|
|
|
// forward complete L2CAP packet if complete.
|
|
|
|
if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header
|
|
|
|
|
2010-07-18 16:30:16 +00:00
|
|
|
hci_stack.packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos);
|
2010-02-13 15:15:19 +00:00
|
|
|
// reset recombination buffer
|
|
|
|
conn->acl_recombination_length = 0;
|
|
|
|
conn->acl_recombination_pos = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x02: { // first fragment
|
|
|
|
|
|
|
|
// sanity check
|
|
|
|
if (conn->acl_recombination_pos) {
|
2011-07-22 18:34:18 +00:00
|
|
|
log_error( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle);
|
2010-02-13 15:15:19 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-07-07 21:47:47 +00:00
|
|
|
|
2010-02-13 15:15:19 +00:00
|
|
|
// peek into L2CAP packet!
|
|
|
|
uint16_t l2cap_length = READ_L2CAP_LENGTH( packet );
|
|
|
|
|
2011-07-22 18:34:18 +00:00
|
|
|
// log_error( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length);
|
2011-07-07 21:47:47 +00:00
|
|
|
|
2010-02-13 15:15:19 +00:00
|
|
|
// compare fragment size to L2CAP packet size
|
|
|
|
if (acl_length >= l2cap_length + 4){
|
|
|
|
|
|
|
|
// forward fragment as L2CAP packet
|
2010-07-18 16:30:16 +00:00
|
|
|
hci_stack.packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4);
|
2010-02-13 15:15:19 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// store first fragment and tweak acl length for complete package
|
|
|
|
memcpy(conn->acl_recombination_buffer, packet, acl_length + 4);
|
|
|
|
conn->acl_recombination_pos = acl_length + 4;
|
|
|
|
conn->acl_recombination_length = l2cap_length;
|
2011-07-07 21:47:47 +00:00
|
|
|
bt_store_16(conn->acl_recombination_buffer, 2, l2cap_length +4);
|
2010-02-13 15:15:19 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
default:
|
2011-07-22 18:34:18 +00:00
|
|
|
log_error( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03);
|
2010-02-13 15:15:19 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-06-06 19:00:32 +00:00
|
|
|
|
|
|
|
// execute main loop
|
|
|
|
hci_run();
|
2009-05-17 20:32:14 +00:00
|
|
|
}
|
2009-05-17 20:51:48 +00:00
|
|
|
|
2011-01-12 19:30:25 +00:00
|
|
|
static void hci_shutdown_connection(hci_connection_t *conn){
|
2011-07-27 22:03:34 +00:00
|
|
|
log_info("Connection closed: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
|
2011-04-30 20:11:20 +00:00
|
|
|
|
|
|
|
// cancel all l2cap connections
|
|
|
|
hci_emit_disconnection_complete(conn->con_handle, 0x16); // terminated by local host
|
|
|
|
|
2011-01-06 23:45:57 +00:00
|
|
|
run_loop_remove_timer(&conn->timeout);
|
2011-07-25 21:12:09 +00:00
|
|
|
|
2011-01-06 23:45:57 +00:00
|
|
|
linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
|
2011-07-24 08:51:17 +00:00
|
|
|
btstack_memory_hci_connection_free( conn );
|
2011-04-30 20:11:20 +00:00
|
|
|
|
|
|
|
// now it's gone
|
2011-01-06 23:45:57 +00:00
|
|
|
hci_emit_nr_connections_changed();
|
|
|
|
}
|
|
|
|
|
2011-08-18 16:08:33 +00:00
|
|
|
static const uint16_t packet_type_sizes[] = {
|
2011-07-26 21:18:45 +00:00
|
|
|
0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE,
|
|
|
|
HCI_ACL_DH1_SIZE, 0, 0, 0,
|
|
|
|
HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE,
|
|
|
|
HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE
|
|
|
|
};
|
|
|
|
|
|
|
|
static uint16_t hci_acl_packet_types_for_buffer_size(uint16_t buffer_size){
|
|
|
|
uint16_t packet_types = 0;
|
|
|
|
int i;
|
|
|
|
for (i=0;i<16;i++){
|
|
|
|
if (packet_type_sizes[i] == 0) continue;
|
|
|
|
if (packet_type_sizes[i] <= buffer_size){
|
|
|
|
packet_types |= 1 << i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// flip bits for "may not be used"
|
|
|
|
packet_types ^= 0x3306;
|
|
|
|
return packet_types;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t hci_usable_acl_packet_types(void){
|
|
|
|
return hci_stack.packet_types;
|
|
|
|
}
|
|
|
|
|
2011-07-29 21:04:09 +00:00
|
|
|
uint8_t* hci_get_outgoing_acl_packet_buffer(void){
|
|
|
|
// hci packet buffer is >= acl data packet length
|
|
|
|
return hci_stack.hci_packet_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t hci_max_acl_data_packet_length(){
|
|
|
|
return hci_stack.acl_data_packet_length;
|
|
|
|
}
|
|
|
|
|
2010-09-01 22:09:58 +00:00
|
|
|
// avoid huge local variables
|
2011-07-28 21:46:10 +00:00
|
|
|
#ifndef EMBEDDED
|
2010-09-01 22:09:58 +00:00
|
|
|
static device_name_t device_name;
|
2011-07-28 21:46:10 +00:00
|
|
|
#endif
|
2009-05-17 20:32:14 +00:00
|
|
|
static void event_handler(uint8_t *packet, int size){
|
2009-05-17 21:06:39 +00:00
|
|
|
bd_addr_t addr;
|
2011-07-22 10:14:38 +00:00
|
|
|
uint8_t link_type;
|
2009-07-24 21:13:53 +00:00
|
|
|
hci_con_handle_t handle;
|
2010-01-25 20:25:20 +00:00
|
|
|
hci_connection_t * conn;
|
2010-07-25 15:51:03 +00:00
|
|
|
int i;
|
2011-01-30 18:37:02 +00:00
|
|
|
|
2009-12-05 18:55:41 +00:00
|
|
|
switch (packet[0]) {
|
2011-01-30 18:37:02 +00:00
|
|
|
|
2009-12-05 18:55:41 +00:00
|
|
|
case HCI_EVENT_COMMAND_COMPLETE:
|
2011-01-30 18:37:02 +00:00
|
|
|
// get num cmd packets
|
2011-07-22 18:31:02 +00:00
|
|
|
// log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack.num_cmd_packets, packet[2]);
|
2011-01-30 18:37:02 +00:00
|
|
|
hci_stack.num_cmd_packets = packet[2];
|
|
|
|
|
2010-07-25 15:11:33 +00:00
|
|
|
if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){
|
|
|
|
// from offset 5
|
|
|
|
// status
|
2011-06-12 07:46:50 +00:00
|
|
|
// "The HC_ACL_Data_Packet_Length return parameter will be used to determine the size of the L2CAP segments contained in ACL Data Packets"
|
2010-07-25 15:11:33 +00:00
|
|
|
hci_stack.acl_data_packet_length = READ_BT_16(packet, 6);
|
2010-07-25 15:51:03 +00:00
|
|
|
// ignore: SCO data packet len (8)
|
2010-07-25 15:11:33 +00:00
|
|
|
hci_stack.total_num_acl_packets = packet[9];
|
2010-07-25 15:51:03 +00:00
|
|
|
// ignore: total num SCO packets
|
|
|
|
if (hci_stack.state == HCI_STATE_INITIALIZING){
|
2011-07-27 20:16:17 +00:00
|
|
|
// determine usable ACL payload size
|
2011-07-30 17:48:48 +00:00
|
|
|
if (HCI_ACL_PAYLOAD_SIZE < hci_stack.acl_data_packet_length){
|
|
|
|
hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
|
2011-07-26 21:18:45 +00:00
|
|
|
}
|
2011-07-27 20:16:17 +00:00
|
|
|
// determine usable ACL packet types
|
2011-07-27 20:42:17 +00:00
|
|
|
hci_stack.packet_types = hci_acl_packet_types_for_buffer_size(hci_stack.acl_data_packet_length);
|
2011-07-26 21:18:45 +00:00
|
|
|
|
2011-07-30 17:48:48 +00:00
|
|
|
log_error("hci_read_buffer_size: used size %u, count %u, packet types %04x\n",
|
2011-07-26 21:18:45 +00:00
|
|
|
hci_stack.acl_data_packet_length, hci_stack.total_num_acl_packets, hci_stack.packet_types);
|
2010-07-25 15:51:03 +00:00
|
|
|
}
|
2010-07-25 15:11:33 +00:00
|
|
|
}
|
2011-01-10 21:11:58 +00:00
|
|
|
if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
|
|
|
|
hci_emit_discoverable_enabled(hci_stack.discoverable);
|
|
|
|
}
|
2009-12-05 18:55:41 +00:00
|
|
|
break;
|
2010-07-25 15:51:03 +00:00
|
|
|
|
2011-01-30 18:37:02 +00:00
|
|
|
case HCI_EVENT_COMMAND_STATUS:
|
|
|
|
// get num cmd packets
|
2011-07-22 18:31:02 +00:00
|
|
|
// log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack.num_cmd_packets, packet[3]);
|
2011-01-30 18:37:02 +00:00
|
|
|
hci_stack.num_cmd_packets = packet[3];
|
|
|
|
break;
|
|
|
|
|
2010-07-25 15:51:03 +00:00
|
|
|
case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
|
|
|
|
for (i=0; i<packet[2];i++){
|
|
|
|
handle = READ_BT_16(packet, 3 + 2*i);
|
|
|
|
uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i);
|
|
|
|
conn = connection_for_handle(handle);
|
|
|
|
if (!conn){
|
2011-07-22 18:34:18 +00:00
|
|
|
log_error("hci_number_completed_packet lists unused con handle %u\n", handle);
|
2010-07-25 15:51:03 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
conn->num_acl_packets_sent -= num_packets;
|
2011-07-22 18:31:02 +00:00
|
|
|
// log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent);
|
2010-07-25 15:51:03 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2010-01-25 20:25:20 +00:00
|
|
|
case HCI_EVENT_CONNECTION_REQUEST:
|
2010-01-25 20:53:43 +00:00
|
|
|
bt_flip_addr(addr, &packet[2]);
|
|
|
|
// TODO: eval COD 8-10
|
2011-07-22 10:14:38 +00:00
|
|
|
link_type = packet[11];
|
2011-07-27 22:03:34 +00:00
|
|
|
log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type);
|
2010-01-25 20:53:43 +00:00
|
|
|
if (link_type == 1) { // ACL
|
|
|
|
conn = connection_for_address(addr);
|
|
|
|
if (!conn) {
|
|
|
|
conn = create_connection_for_addr(addr);
|
|
|
|
}
|
2011-07-25 18:48:56 +00:00
|
|
|
if (!conn) {
|
|
|
|
// CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D)
|
|
|
|
hci_stack.decline_reason = 0x0d;
|
|
|
|
BD_ADDR_COPY(hci_stack.decline_addr, addr);
|
|
|
|
break;
|
|
|
|
}
|
2011-06-21 19:30:58 +00:00
|
|
|
conn->state = RECEIVED_CONNECTION_REQUEST;
|
|
|
|
hci_run();
|
2010-01-25 20:53:43 +00:00
|
|
|
} else {
|
2011-07-25 18:48:56 +00:00
|
|
|
// SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A)
|
|
|
|
hci_stack.decline_reason = 0x0a;
|
|
|
|
BD_ADDR_COPY(hci_stack.decline_addr, addr);
|
2010-01-25 20:25:20 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2009-12-05 18:55:41 +00:00
|
|
|
case HCI_EVENT_CONNECTION_COMPLETE:
|
|
|
|
// Connection management
|
|
|
|
bt_flip_addr(addr, &packet[5]);
|
2011-07-27 22:03:34 +00:00
|
|
|
log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr));
|
2010-01-25 20:25:20 +00:00
|
|
|
conn = connection_for_address(addr);
|
2009-12-05 18:55:41 +00:00
|
|
|
if (conn) {
|
|
|
|
if (!packet[2]){
|
|
|
|
conn->state = OPEN;
|
|
|
|
conn->con_handle = READ_BT_16(packet, 3);
|
|
|
|
|
2011-07-25 21:12:09 +00:00
|
|
|
// restart timer
|
2009-12-05 18:55:41 +00:00
|
|
|
run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
|
|
|
|
run_loop_add_timer(&conn->timeout);
|
2011-07-25 21:12:09 +00:00
|
|
|
|
2011-07-27 22:03:34 +00:00
|
|
|
log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
|
2009-12-05 18:55:41 +00:00
|
|
|
|
|
|
|
hci_emit_nr_connections_changed();
|
|
|
|
} else {
|
|
|
|
// connection failed, remove entry
|
|
|
|
linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
|
2011-07-24 08:51:17 +00:00
|
|
|
btstack_memory_hci_connection_free( conn );
|
2010-08-30 19:05:14 +00:00
|
|
|
|
|
|
|
// if authentication error, also delete link key
|
|
|
|
if (packet[2] == 0x05) {
|
|
|
|
hci_drop_link_key_for_bd_addr(&addr);
|
|
|
|
}
|
2009-12-05 18:55:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2009-05-18 20:18:42 +00:00
|
|
|
|
2010-08-26 20:12:05 +00:00
|
|
|
case HCI_EVENT_LINK_KEY_REQUEST:
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("HCI_EVENT_LINK_KEY_REQUEST\n");
|
2010-08-26 20:12:05 +00:00
|
|
|
hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
|
2010-09-01 22:09:58 +00:00
|
|
|
if (!hci_stack.remote_device_db) break;
|
2011-06-21 19:30:58 +00:00
|
|
|
hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST);
|
2011-06-21 20:28:09 +00:00
|
|
|
hci_run();
|
2010-09-01 22:09:58 +00:00
|
|
|
// request already answered
|
|
|
|
return;
|
2010-08-26 20:12:05 +00:00
|
|
|
|
|
|
|
case HCI_EVENT_LINK_KEY_NOTIFICATION:
|
|
|
|
hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_NOTIFICATION);
|
2010-09-01 22:09:58 +00:00
|
|
|
if (!hci_stack.remote_device_db) break;
|
|
|
|
bt_flip_addr(addr, &packet[2]);
|
2010-09-03 18:40:27 +00:00
|
|
|
hci_stack.remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8]);
|
2010-08-30 18:44:50 +00:00
|
|
|
// still forward event to allow dismiss of pairing dialog
|
2010-08-26 20:12:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case HCI_EVENT_PIN_CODE_REQUEST:
|
|
|
|
hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_PIN_CODE_REQUEST);
|
|
|
|
break;
|
|
|
|
|
2011-07-28 21:46:10 +00:00
|
|
|
#ifndef EMBEDDED
|
2010-09-01 22:09:58 +00:00
|
|
|
case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
|
|
|
|
if (!hci_stack.remote_device_db) break;
|
|
|
|
if (packet[2]) break; // status not ok
|
|
|
|
bt_flip_addr(addr, &packet[3]);
|
2011-06-17 17:46:08 +00:00
|
|
|
// fix for invalid remote names - terminate on 0xff
|
|
|
|
for (i=0; i<248;i++){
|
|
|
|
if (packet[9+i] == 0xff){
|
|
|
|
packet[9+i] = 0;
|
|
|
|
break;
|
2011-06-17 20:44:46 +00:00
|
|
|
}
|
2011-06-17 17:46:08 +00:00
|
|
|
}
|
2011-08-18 16:43:15 +00:00
|
|
|
memset(&device_name, 0, sizeof(device_name_t));
|
2010-09-01 22:09:58 +00:00
|
|
|
strncpy((char*) device_name, (char*) &packet[9], 248);
|
|
|
|
hci_stack.remote_device_db->put_name(&addr, &device_name);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HCI_EVENT_INQUIRY_RESULT:
|
|
|
|
case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:
|
|
|
|
if (!hci_stack.remote_device_db) break;
|
|
|
|
// first send inq result packet
|
|
|
|
hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
|
|
|
|
// then send cached remote names
|
|
|
|
for (i=0; i<packet[2];i++){
|
|
|
|
bt_flip_addr(addr, &packet[3+i*6]);
|
|
|
|
if (hci_stack.remote_device_db->get_name(&addr, &device_name)){
|
|
|
|
hci_emit_remote_name_cached(&addr, &device_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
2011-07-28 21:46:10 +00:00
|
|
|
#endif
|
2011-06-17 17:46:08 +00:00
|
|
|
|
2009-12-05 18:55:41 +00:00
|
|
|
case HCI_EVENT_DISCONNECTION_COMPLETE:
|
2009-12-05 17:29:51 +00:00
|
|
|
if (!packet[2]){
|
2009-12-05 18:55:41 +00:00
|
|
|
handle = READ_BT_16(packet, 3);
|
|
|
|
hci_connection_t * conn = connection_for_handle(handle);
|
|
|
|
if (conn) {
|
2011-01-06 23:45:57 +00:00
|
|
|
hci_shutdown_connection(conn);
|
2009-12-05 18:55:41 +00:00
|
|
|
}
|
2009-07-24 21:13:53 +00:00
|
|
|
}
|
2009-12-05 18:55:41 +00:00
|
|
|
break;
|
|
|
|
|
2011-06-08 16:13:30 +00:00
|
|
|
case HCI_EVENT_HARDWARE_ERROR:
|
|
|
|
if(hci_stack.control->hw_error){
|
|
|
|
(*hci_stack.control->hw_error)();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2009-12-05 18:55:41 +00:00
|
|
|
default:
|
|
|
|
break;
|
2009-07-24 21:13:53 +00:00
|
|
|
}
|
2009-12-05 18:55:41 +00:00
|
|
|
|
2009-05-18 20:18:42 +00:00
|
|
|
// handle BT initialization
|
|
|
|
if (hci_stack.state == HCI_STATE_INITIALIZING){
|
2009-06-03 21:21:12 +00:00
|
|
|
// handle H4 synchronization loss on restart
|
|
|
|
// if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){
|
|
|
|
// hci_stack.substate = 0;
|
|
|
|
// }
|
|
|
|
// handle normal init sequence
|
2009-05-18 20:18:42 +00:00
|
|
|
if (hci_stack.substate % 2){
|
|
|
|
// odd: waiting for event
|
|
|
|
if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
|
|
|
|
hci_stack.substate++;
|
|
|
|
}
|
|
|
|
}
|
2009-05-17 20:51:48 +00:00
|
|
|
}
|
2011-01-30 17:38:07 +00:00
|
|
|
|
|
|
|
// help with BT sleep
|
|
|
|
if (hci_stack.state == HCI_STATE_FALLING_ASLEEP
|
|
|
|
&& hci_stack.substate == 1
|
|
|
|
&& COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
|
|
|
|
hci_stack.substate++;
|
|
|
|
}
|
|
|
|
|
2010-07-18 16:30:16 +00:00
|
|
|
hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size);
|
2009-06-06 19:00:32 +00:00
|
|
|
|
|
|
|
// execute main loop
|
|
|
|
hci_run();
|
2009-05-17 20:32:14 +00:00
|
|
|
}
|
|
|
|
|
2010-07-18 16:14:55 +00:00
|
|
|
void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
|
|
|
|
switch (packet_type) {
|
|
|
|
case HCI_EVENT_PACKET:
|
|
|
|
event_handler(packet, size);
|
|
|
|
break;
|
|
|
|
case HCI_ACL_DATA_PACKET:
|
|
|
|
acl_handler(packet, size);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-02 10:51:41 +00:00
|
|
|
/** Register HCI packet handlers */
|
2010-07-18 16:30:16 +00:00
|
|
|
void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
|
|
|
|
hci_stack.packet_handler = handler;
|
2009-05-17 20:32:14 +00:00
|
|
|
}
|
|
|
|
|
2010-08-30 20:10:51 +00:00
|
|
|
void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t * remote_device_db){
|
2009-05-03 21:28:40 +00:00
|
|
|
|
|
|
|
// reference to use transport layer implementation
|
2009-05-17 20:32:14 +00:00
|
|
|
hci_stack.hci_transport = transport;
|
2009-05-03 21:28:40 +00:00
|
|
|
|
2009-05-20 20:46:35 +00:00
|
|
|
// references to used control implementation
|
2010-08-05 20:23:06 +00:00
|
|
|
hci_stack.control = control;
|
2009-05-20 20:46:35 +00:00
|
|
|
|
|
|
|
// reference to used config
|
|
|
|
hci_stack.config = config;
|
|
|
|
|
2009-07-24 21:13:53 +00:00
|
|
|
// no connections yet
|
|
|
|
hci_stack.connections = NULL;
|
2011-02-03 19:41:43 +00:00
|
|
|
hci_stack.discoverable = 0;
|
2009-07-24 21:13:53 +00:00
|
|
|
|
2011-08-20 17:02:52 +00:00
|
|
|
// no pending cmds
|
|
|
|
hci_stack.decline_reason = 0;
|
|
|
|
hci_stack.new_scan_enable_value = 0xff;
|
|
|
|
|
2009-05-17 20:32:14 +00:00
|
|
|
// higher level handler
|
2010-07-18 16:30:16 +00:00
|
|
|
hci_stack.packet_handler = dummy_handler;
|
2009-06-03 21:21:12 +00:00
|
|
|
|
2010-08-30 20:10:51 +00:00
|
|
|
// store and open remote device db
|
|
|
|
hci_stack.remote_device_db = remote_device_db;
|
|
|
|
if (hci_stack.remote_device_db) {
|
|
|
|
hci_stack.remote_device_db->open();
|
|
|
|
}
|
2010-08-30 18:44:50 +00:00
|
|
|
|
2011-07-30 17:48:48 +00:00
|
|
|
// max acl payload size defined in config.h
|
|
|
|
hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
|
|
|
|
|
2009-05-17 20:32:14 +00:00
|
|
|
// register packet handlers with transport
|
2010-07-18 16:14:55 +00:00
|
|
|
transport->register_packet_handler(&packet_handler);
|
2009-05-03 21:28:40 +00:00
|
|
|
}
|
|
|
|
|
2010-08-30 20:10:51 +00:00
|
|
|
void hci_close(){
|
|
|
|
// close remote device db
|
|
|
|
if (hci_stack.remote_device_db) {
|
|
|
|
hci_stack.remote_device_db->close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-07 19:36:05 +00:00
|
|
|
// State-Module-Driver overview
|
|
|
|
// state module low-level
|
|
|
|
// HCI_STATE_OFF off close
|
|
|
|
// HCI_STATE_INITIALIZING, on open
|
|
|
|
// HCI_STATE_WORKING, on open
|
|
|
|
// HCI_STATE_HALTING, on open
|
2011-01-07 22:20:37 +00:00
|
|
|
// HCI_STATE_SLEEPING, off/sleep close
|
|
|
|
// HCI_STATE_FALLING_ASLEEP on open
|
2011-01-07 19:36:05 +00:00
|
|
|
|
2011-06-06 14:12:38 +00:00
|
|
|
static int hci_power_control_on(void){
|
2011-01-07 19:36:05 +00:00
|
|
|
|
|
|
|
// power on
|
|
|
|
int err = 0;
|
|
|
|
if (hci_stack.control && hci_stack.control->on){
|
|
|
|
err = (*hci_stack.control->on)(hci_stack.config);
|
|
|
|
}
|
|
|
|
if (err){
|
2011-07-22 18:34:18 +00:00
|
|
|
log_error( "POWER_ON failed\n");
|
2011-01-07 19:36:05 +00:00
|
|
|
hci_emit_hci_open_failed();
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
// open low-level device
|
|
|
|
err = hci_stack.hci_transport->open(hci_stack.config);
|
|
|
|
if (err){
|
2011-07-22 18:34:18 +00:00
|
|
|
log_error( "HCI_INIT failed, turning Bluetooth off again\n");
|
2011-01-07 19:36:05 +00:00
|
|
|
if (hci_stack.control && hci_stack.control->off){
|
|
|
|
(*hci_stack.control->off)(hci_stack.config);
|
|
|
|
}
|
|
|
|
hci_emit_hci_open_failed();
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-06-06 14:12:38 +00:00
|
|
|
static void hci_power_control_off(void){
|
2011-01-06 23:45:57 +00:00
|
|
|
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("hci_power_control_off\n");
|
2011-06-03 22:00:20 +00:00
|
|
|
|
2011-01-06 23:45:57 +00:00
|
|
|
// close low-level device
|
|
|
|
hci_stack.hci_transport->close(hci_stack.config);
|
2011-06-03 22:00:20 +00:00
|
|
|
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("hci_power_control_off - hci_transport closed\n");
|
2011-01-06 23:45:57 +00:00
|
|
|
|
|
|
|
// power off
|
|
|
|
if (hci_stack.control && hci_stack.control->off){
|
|
|
|
(*hci_stack.control->off)(hci_stack.config);
|
|
|
|
}
|
2011-06-03 22:00:20 +00:00
|
|
|
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("hci_power_control_off - control closed\n");
|
2011-06-03 22:00:20 +00:00
|
|
|
|
2011-01-08 23:03:00 +00:00
|
|
|
hci_stack.state = HCI_STATE_OFF;
|
|
|
|
}
|
|
|
|
|
2011-06-06 14:12:38 +00:00
|
|
|
static void hci_power_control_sleep(void){
|
2011-01-11 21:38:56 +00:00
|
|
|
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("hci_power_control_sleep\n");
|
2011-01-11 21:38:56 +00:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
// don't close serial port during sleep
|
|
|
|
|
2011-01-08 23:03:00 +00:00
|
|
|
// close low-level device
|
|
|
|
hci_stack.hci_transport->close(hci_stack.config);
|
2011-01-11 21:38:56 +00:00
|
|
|
#endif
|
2011-01-08 23:03:00 +00:00
|
|
|
|
|
|
|
// sleep mode
|
2011-01-09 16:19:42 +00:00
|
|
|
if (hci_stack.control && hci_stack.control->sleep){
|
2011-01-08 23:03:00 +00:00
|
|
|
(*hci_stack.control->sleep)(hci_stack.config);
|
|
|
|
}
|
2011-01-11 21:38:56 +00:00
|
|
|
|
2011-01-08 23:03:00 +00:00
|
|
|
hci_stack.state = HCI_STATE_SLEEPING;
|
2011-01-06 23:45:57 +00:00
|
|
|
}
|
|
|
|
|
2011-06-06 14:12:38 +00:00
|
|
|
static int hci_power_control_wake(void){
|
2011-01-11 21:38:56 +00:00
|
|
|
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("hci_power_control_wake\n");
|
2011-01-11 21:38:56 +00:00
|
|
|
|
|
|
|
// wake on
|
|
|
|
if (hci_stack.control && hci_stack.control->wake){
|
|
|
|
(*hci_stack.control->wake)(hci_stack.config);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// open low-level device
|
|
|
|
int err = hci_stack.hci_transport->open(hci_stack.config);
|
|
|
|
if (err){
|
2011-07-22 18:34:18 +00:00
|
|
|
log_error( "HCI_INIT failed, turning Bluetooth off again\n");
|
2011-01-11 21:38:56 +00:00
|
|
|
if (hci_stack.control && hci_stack.control->off){
|
|
|
|
(*hci_stack.control->off)(hci_stack.config);
|
|
|
|
}
|
|
|
|
hci_emit_hci_open_failed();
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-03 21:28:40 +00:00
|
|
|
int hci_power_control(HCI_POWER_MODE power_mode){
|
2011-01-07 22:20:37 +00:00
|
|
|
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack.state);
|
2011-01-07 22:20:37 +00:00
|
|
|
|
2011-01-07 19:36:05 +00:00
|
|
|
int err = 0;
|
|
|
|
switch (hci_stack.state){
|
|
|
|
|
|
|
|
case HCI_STATE_OFF:
|
|
|
|
switch (power_mode){
|
|
|
|
case HCI_POWER_ON:
|
|
|
|
err = hci_power_control_on();
|
|
|
|
if (err) return err;
|
|
|
|
// set up state machine
|
|
|
|
hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
|
|
|
|
hci_stack.state = HCI_STATE_INITIALIZING;
|
|
|
|
hci_stack.substate = 0;
|
|
|
|
break;
|
|
|
|
case HCI_POWER_OFF:
|
|
|
|
// do nothing
|
|
|
|
break;
|
|
|
|
case HCI_POWER_SLEEP:
|
2011-01-07 20:54:57 +00:00
|
|
|
// do nothing (with SLEEP == OFF)
|
2011-01-07 19:36:05 +00:00
|
|
|
break;
|
2010-08-05 20:23:06 +00:00
|
|
|
}
|
2011-01-07 19:36:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case HCI_STATE_INITIALIZING:
|
|
|
|
switch (power_mode){
|
|
|
|
case HCI_POWER_ON:
|
|
|
|
// do nothing
|
|
|
|
break;
|
|
|
|
case HCI_POWER_OFF:
|
|
|
|
// no connections yet, just turn it off
|
|
|
|
hci_power_control_off();
|
|
|
|
break;
|
|
|
|
case HCI_POWER_SLEEP:
|
2011-01-07 20:54:57 +00:00
|
|
|
// no connections yet, just turn it off
|
2011-01-08 23:03:00 +00:00
|
|
|
hci_power_control_sleep();
|
2011-01-07 19:36:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HCI_STATE_WORKING:
|
|
|
|
switch (power_mode){
|
|
|
|
case HCI_POWER_ON:
|
|
|
|
// do nothing
|
|
|
|
break;
|
|
|
|
case HCI_POWER_OFF:
|
|
|
|
// see hci_run
|
|
|
|
hci_stack.state = HCI_STATE_HALTING;
|
|
|
|
break;
|
|
|
|
case HCI_POWER_SLEEP:
|
2011-01-07 20:54:57 +00:00
|
|
|
// see hci_run
|
|
|
|
hci_stack.state = HCI_STATE_FALLING_ASLEEP;
|
2011-01-30 17:38:07 +00:00
|
|
|
hci_stack.substate = 0;
|
2011-01-07 19:36:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HCI_STATE_HALTING:
|
|
|
|
switch (power_mode){
|
|
|
|
case HCI_POWER_ON:
|
|
|
|
// set up state machine
|
|
|
|
hci_stack.state = HCI_STATE_INITIALIZING;
|
|
|
|
hci_stack.substate = 0;
|
|
|
|
break;
|
|
|
|
case HCI_POWER_OFF:
|
|
|
|
// do nothing
|
|
|
|
break;
|
|
|
|
case HCI_POWER_SLEEP:
|
2011-01-07 20:54:57 +00:00
|
|
|
// see hci_run
|
|
|
|
hci_stack.state = HCI_STATE_FALLING_ASLEEP;
|
2011-01-30 17:38:07 +00:00
|
|
|
hci_stack.substate = 0;
|
2011-01-07 19:36:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HCI_STATE_FALLING_ASLEEP:
|
|
|
|
switch (power_mode){
|
|
|
|
case HCI_POWER_ON:
|
2011-08-26 21:35:05 +00:00
|
|
|
|
|
|
|
#if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
|
|
|
|
// nothing to do, if H4 supports power management
|
|
|
|
if (bt_control_iphone_power_management_enabled()){
|
2011-08-26 22:04:18 +00:00
|
|
|
hci_stack.state = HCI_STATE_INITIALIZING;
|
|
|
|
hci_stack.substate = 6;
|
2011-08-26 21:35:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
2011-01-07 20:54:57 +00:00
|
|
|
// set up state machine
|
|
|
|
hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
|
|
|
|
hci_stack.state = HCI_STATE_INITIALIZING;
|
|
|
|
hci_stack.substate = 0;
|
2011-01-07 19:36:05 +00:00
|
|
|
break;
|
|
|
|
case HCI_POWER_OFF:
|
2011-01-07 20:54:57 +00:00
|
|
|
// see hci_run
|
|
|
|
hci_stack.state = HCI_STATE_HALTING;
|
2011-01-07 19:36:05 +00:00
|
|
|
break;
|
|
|
|
case HCI_POWER_SLEEP:
|
2011-01-07 20:54:57 +00:00
|
|
|
// do nothing
|
2011-01-07 19:36:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HCI_STATE_SLEEPING:
|
|
|
|
switch (power_mode){
|
|
|
|
case HCI_POWER_ON:
|
2011-08-26 21:35:05 +00:00
|
|
|
|
|
|
|
#if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
|
|
|
|
// nothing to do, if H4 supports power management
|
|
|
|
if (bt_control_iphone_power_management_enabled()){
|
2011-08-26 22:04:18 +00:00
|
|
|
hci_stack.state = HCI_STATE_INITIALIZING;
|
|
|
|
hci_stack.substate = 6;
|
2011-08-26 21:35:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
2011-01-09 16:19:42 +00:00
|
|
|
err = hci_power_control_wake();
|
|
|
|
if (err) return err;
|
2011-01-07 20:54:57 +00:00
|
|
|
// set up state machine
|
|
|
|
hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
|
|
|
|
hci_stack.state = HCI_STATE_INITIALIZING;
|
|
|
|
hci_stack.substate = 0;
|
2011-01-07 19:36:05 +00:00
|
|
|
break;
|
|
|
|
case HCI_POWER_OFF:
|
2011-02-02 22:05:11 +00:00
|
|
|
hci_stack.state = HCI_STATE_HALTING;
|
2011-01-07 19:36:05 +00:00
|
|
|
break;
|
|
|
|
case HCI_POWER_SLEEP:
|
2011-01-07 20:54:57 +00:00
|
|
|
// do nothing
|
2011-01-07 19:36:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2009-05-20 20:46:35 +00:00
|
|
|
}
|
2011-01-07 19:36:05 +00:00
|
|
|
|
2009-09-13 20:42:43 +00:00
|
|
|
// create internal event
|
2009-07-24 21:37:45 +00:00
|
|
|
hci_emit_state();
|
|
|
|
|
2009-06-07 15:45:31 +00:00
|
|
|
// trigger next/first action
|
|
|
|
hci_run();
|
|
|
|
|
2009-05-03 21:28:40 +00:00
|
|
|
return 0;
|
2009-04-29 22:00:24 +00:00
|
|
|
}
|
2009-05-03 21:28:40 +00:00
|
|
|
|
2011-01-10 21:11:58 +00:00
|
|
|
void hci_discoverable_control(uint8_t enable){
|
|
|
|
if (enable) enable = 1; // normalize argument
|
|
|
|
|
|
|
|
if (hci_stack.discoverable == enable){
|
|
|
|
hci_emit_discoverable_enabled(hci_stack.discoverable);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-08-20 17:02:52 +00:00
|
|
|
// store request to send command but accept in higher layer view
|
|
|
|
hci_stack.new_scan_enable_value = 2 | enable; // 1 = inq scan, 2 = page scan
|
2011-01-10 21:11:58 +00:00
|
|
|
hci_stack.discoverable = enable;
|
2011-08-20 17:02:52 +00:00
|
|
|
|
|
|
|
hci_run();
|
2011-01-10 21:11:58 +00:00
|
|
|
}
|
|
|
|
|
2009-07-24 20:17:46 +00:00
|
|
|
void hci_run(){
|
2011-01-30 18:37:02 +00:00
|
|
|
|
2011-01-06 23:45:57 +00:00
|
|
|
hci_connection_t * connection;
|
2011-06-21 19:30:58 +00:00
|
|
|
linked_item_t * it;
|
2011-01-06 23:45:57 +00:00
|
|
|
|
2011-07-25 18:48:56 +00:00
|
|
|
if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
|
|
|
|
|
2011-08-20 17:02:52 +00:00
|
|
|
// global/non-connection oriented commands
|
|
|
|
|
|
|
|
// decline incoming connections
|
2011-07-25 18:48:56 +00:00
|
|
|
if (hci_stack.decline_reason){
|
|
|
|
uint8_t reason = hci_stack.decline_reason;
|
|
|
|
hci_stack.decline_reason = 0;
|
|
|
|
hci_send_cmd(&hci_reject_connection_request, hci_stack.decline_addr, reason);
|
|
|
|
}
|
2011-08-20 17:02:52 +00:00
|
|
|
|
|
|
|
if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
|
|
|
|
|
|
|
|
// send scan enable
|
2011-08-20 20:21:40 +00:00
|
|
|
if (hci_stack.new_scan_enable_value != 0xff){
|
2011-08-20 17:02:52 +00:00
|
|
|
hci_send_cmd(&hci_write_scan_enable, hci_stack.new_scan_enable_value);
|
|
|
|
hci_stack.new_scan_enable_value = 0xff;
|
|
|
|
}
|
2011-07-25 18:48:56 +00:00
|
|
|
|
2011-06-21 19:30:58 +00:00
|
|
|
// send pending HCI commands
|
|
|
|
for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
|
|
|
|
|
2011-08-20 17:02:52 +00:00
|
|
|
if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
|
2011-06-21 20:28:09 +00:00
|
|
|
|
2011-08-20 17:02:52 +00:00
|
|
|
connection = (hci_connection_t *) it;
|
2011-06-21 19:30:58 +00:00
|
|
|
|
|
|
|
if (connection->state == RECEIVED_CONNECTION_REQUEST){
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("sending hci_accept_connection_request\n");
|
2011-06-21 19:30:58 +00:00
|
|
|
hci_send_cmd(&hci_accept_connection_request, connection->address, 1);
|
|
|
|
connection->state = ACCEPTED_CONNECTION_REQUEST;
|
|
|
|
}
|
|
|
|
|
2011-06-21 19:34:08 +00:00
|
|
|
if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
|
2011-06-21 19:30:58 +00:00
|
|
|
|
|
|
|
if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
|
|
|
|
link_key_t link_key;
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("responding to link key request\n");
|
2011-06-21 19:30:58 +00:00
|
|
|
if ( hci_stack.remote_device_db->get_link_key( &connection->address, &link_key)){
|
|
|
|
hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
|
|
|
|
} else {
|
|
|
|
hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
|
|
|
|
}
|
|
|
|
connection->authentication_flags &= ~HANDLE_LINK_KEY_REQUEST;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-21 19:34:08 +00:00
|
|
|
if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
|
2011-06-21 19:30:58 +00:00
|
|
|
|
2009-05-18 20:18:42 +00:00
|
|
|
switch (hci_stack.state){
|
|
|
|
case HCI_STATE_INITIALIZING:
|
2011-07-22 18:31:02 +00:00
|
|
|
// log_info("hci_init: substate %u\n", hci_stack.substate);
|
2009-05-18 20:18:42 +00:00
|
|
|
if (hci_stack.substate % 2) {
|
|
|
|
// odd: waiting for command completion
|
2009-07-24 20:17:46 +00:00
|
|
|
return;
|
2009-05-18 20:18:42 +00:00
|
|
|
}
|
2010-08-05 20:13:02 +00:00
|
|
|
switch (hci_stack.substate >> 1){
|
2011-06-05 09:58:33 +00:00
|
|
|
case 0: // RESET
|
2009-05-18 20:18:42 +00:00
|
|
|
hci_send_cmd(&hci_reset);
|
2011-06-05 09:58:33 +00:00
|
|
|
if (hci_stack.config == 0 || ((hci_uart_config_t *)hci_stack.config)->baudrate_main == 0){
|
|
|
|
// skip baud change
|
|
|
|
hci_stack.substate = 4; // >> 1 = 2
|
|
|
|
}
|
2009-05-18 20:18:42 +00:00
|
|
|
break;
|
2011-06-05 09:58:33 +00:00
|
|
|
case 1: // SEND BAUD CHANGE
|
2011-07-29 21:04:09 +00:00
|
|
|
hci_stack.control->baudrate_cmd(hci_stack.config, ((hci_uart_config_t *)hci_stack.config)->baudrate_main, hci_stack.hci_packet_buffer);
|
|
|
|
hci_send_cmd_packet(hci_stack.hci_packet_buffer, 3 + hci_stack.hci_packet_buffer[2]);
|
2011-06-05 09:58:33 +00:00
|
|
|
break;
|
|
|
|
case 2: // LOCAL BAUD CHANGE
|
|
|
|
hci_stack.hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack.config)->baudrate_main);
|
|
|
|
hci_stack.substate += 2;
|
|
|
|
// break missing here for fall through
|
|
|
|
|
|
|
|
case 3:
|
2010-08-05 20:13:02 +00:00
|
|
|
// custom initialization
|
2011-07-29 19:43:17 +00:00
|
|
|
if (hci_stack.control && hci_stack.control->next_cmd){
|
2011-07-29 21:04:09 +00:00
|
|
|
int valid_cmd = (*hci_stack.control->next_cmd)(hci_stack.config, hci_stack.hci_packet_buffer);
|
2011-07-28 22:05:38 +00:00
|
|
|
if (valid_cmd){
|
2011-07-29 21:04:09 +00:00
|
|
|
int size = 3 + hci_stack.hci_packet_buffer[2];
|
|
|
|
hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack.hci_packet_buffer, size);
|
2011-06-05 09:58:33 +00:00
|
|
|
hci_stack.substate = 4; // more init commands
|
2010-08-05 20:13:02 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-07-28 22:05:38 +00:00
|
|
|
log_info("hci_run: init script done\n\r");
|
2010-08-05 20:13:02 +00:00
|
|
|
}
|
2010-08-05 20:37:33 +00:00
|
|
|
// otherwise continue
|
2009-06-14 21:48:08 +00:00
|
|
|
hci_send_cmd(&hci_read_bd_addr);
|
|
|
|
break;
|
2011-06-05 09:58:33 +00:00
|
|
|
case 4:
|
2010-07-25 15:11:33 +00:00
|
|
|
hci_send_cmd(&hci_read_buffer_size);
|
|
|
|
break;
|
2011-06-05 09:58:33 +00:00
|
|
|
case 5:
|
2009-05-18 20:18:42 +00:00
|
|
|
// ca. 15 sec
|
|
|
|
hci_send_cmd(&hci_write_page_timeout, 0x6000);
|
|
|
|
break;
|
2011-06-05 09:58:33 +00:00
|
|
|
case 6:
|
2011-02-03 19:41:43 +00:00
|
|
|
hci_send_cmd(&hci_write_scan_enable, 2 | hci_stack.discoverable); // page scan
|
2009-06-14 21:48:08 +00:00
|
|
|
break;
|
2011-06-05 09:58:33 +00:00
|
|
|
case 7:
|
2010-08-21 22:33:09 +00:00
|
|
|
#ifndef EMBEDDED
|
|
|
|
{
|
|
|
|
char hostname[30];
|
|
|
|
gethostname(hostname, 30);
|
|
|
|
hostname[29] = '\0';
|
|
|
|
hci_send_cmd(&hci_write_local_name, hostname);
|
|
|
|
break;
|
|
|
|
}
|
2011-06-05 09:58:33 +00:00
|
|
|
case 8:
|
2011-04-30 20:11:20 +00:00
|
|
|
#ifdef USE_BLUETOOL
|
|
|
|
hci_send_cmd(&hci_write_class_of_device, 0x007a020c); // Smartphone
|
|
|
|
break;
|
|
|
|
|
2011-06-05 09:58:33 +00:00
|
|
|
case 9:
|
2011-04-30 20:11:20 +00:00
|
|
|
#endif
|
2010-08-21 22:33:09 +00:00
|
|
|
#endif
|
2009-05-18 20:18:42 +00:00
|
|
|
// done.
|
|
|
|
hci_stack.state = HCI_STATE_WORKING;
|
2009-09-30 20:29:30 +00:00
|
|
|
hci_emit_state();
|
2009-05-18 20:18:42 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
hci_stack.substate++;
|
|
|
|
break;
|
2011-01-06 23:45:57 +00:00
|
|
|
|
|
|
|
case HCI_STATE_HALTING:
|
|
|
|
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("HCI_STATE_HALTING\n");
|
2011-01-06 23:45:57 +00:00
|
|
|
// close all open connections
|
|
|
|
connection = (hci_connection_t *) hci_stack.connections;
|
|
|
|
if (connection){
|
2011-06-21 19:30:58 +00:00
|
|
|
|
2011-01-06 23:45:57 +00:00
|
|
|
// send disconnect
|
2011-06-21 19:30:58 +00:00
|
|
|
if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
|
|
|
|
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("HCI_STATE_HALTING, connection %lu, handle %u\n", (uintptr_t) connection, (uint16_t)connection->con_handle);
|
2011-01-12 19:35:08 +00:00
|
|
|
hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection
|
2011-01-08 23:03:00 +00:00
|
|
|
|
2011-01-06 23:45:57 +00:00
|
|
|
// send disconnected event right away - causes higher layer connections to get closed, too.
|
|
|
|
hci_shutdown_connection(connection);
|
|
|
|
return;
|
|
|
|
}
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("HCI_STATE_HALTING, calling off\n");
|
2011-01-06 23:45:57 +00:00
|
|
|
|
2011-01-08 23:03:00 +00:00
|
|
|
// switch mode
|
2011-01-06 23:45:57 +00:00
|
|
|
hci_power_control_off();
|
2011-06-03 22:00:20 +00:00
|
|
|
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("HCI_STATE_HALTING, emitting state\n");
|
2011-01-08 23:03:00 +00:00
|
|
|
hci_emit_state();
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("HCI_STATE_HALTING, done\n");
|
2011-01-08 23:03:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case HCI_STATE_FALLING_ASLEEP:
|
2011-01-30 17:38:07 +00:00
|
|
|
switch(hci_stack.substate) {
|
|
|
|
case 0:
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("HCI_STATE_FALLING_ASLEEP\n");
|
2011-01-30 17:38:07 +00:00
|
|
|
// close all open connections
|
|
|
|
connection = (hci_connection_t *) hci_stack.connections;
|
2011-08-26 20:48:20 +00:00
|
|
|
|
2011-08-26 21:35:05 +00:00
|
|
|
#if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
|
2011-08-26 20:48:20 +00:00
|
|
|
// don't close connections, if H4 supports power management
|
|
|
|
if (bt_control_iphone_power_management_enabled()){
|
|
|
|
connection = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
2011-01-30 17:38:07 +00:00
|
|
|
if (connection){
|
2011-06-21 19:30:58 +00:00
|
|
|
|
2011-01-30 17:38:07 +00:00
|
|
|
// send disconnect
|
2011-06-21 19:30:58 +00:00
|
|
|
if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
|
|
|
|
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("HCI_STATE_FALLING_ASLEEP, connection %lu, handle %u\n", (uintptr_t) connection, (uint16_t)connection->con_handle);
|
2011-01-30 17:38:07 +00:00
|
|
|
hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection
|
|
|
|
|
|
|
|
// send disconnected event right away - causes higher layer connections to get closed, too.
|
|
|
|
hci_shutdown_connection(connection);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// disable page and inquiry scan
|
2011-06-21 19:30:58 +00:00
|
|
|
if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
|
|
|
|
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("HCI_STATE_HALTING, disabling inq & page scans\n");
|
2011-01-30 17:38:07 +00:00
|
|
|
hci_send_cmd(&hci_write_scan_enable, 0); // none
|
|
|
|
|
|
|
|
// continue in next sub state
|
|
|
|
hci_stack.substate++;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
// wait for command complete "hci_write_scan_enable" in event_handler();
|
|
|
|
break;
|
|
|
|
case 2:
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("HCI_STATE_HALTING, calling sleep\n");
|
2011-08-26 21:35:05 +00:00
|
|
|
#if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
|
|
|
|
// don't actually go to sleep, if H4 supports power management
|
|
|
|
if (bt_control_iphone_power_management_enabled()){
|
|
|
|
// SLEEP MODE reached
|
|
|
|
hci_stack.state = HCI_STATE_SLEEPING;
|
|
|
|
hci_emit_state();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
2011-01-30 17:38:07 +00:00
|
|
|
// switch mode
|
|
|
|
hci_power_control_sleep(); // changes hci_stack.state to SLEEP
|
|
|
|
hci_emit_state();
|
2011-08-26 21:35:05 +00:00
|
|
|
break;
|
|
|
|
|
2011-01-30 17:38:07 +00:00
|
|
|
default:
|
|
|
|
break;
|
2011-01-08 23:03:00 +00:00
|
|
|
}
|
2011-01-06 23:45:57 +00:00
|
|
|
break;
|
|
|
|
|
2009-05-18 20:18:42 +00:00
|
|
|
default:
|
|
|
|
break;
|
2009-05-03 21:28:40 +00:00
|
|
|
}
|
2009-05-09 17:34:17 +00:00
|
|
|
}
|
|
|
|
|
2009-07-16 22:04:11 +00:00
|
|
|
int hci_send_cmd_packet(uint8_t *packet, int size){
|
2009-08-02 13:20:32 +00:00
|
|
|
bd_addr_t addr;
|
|
|
|
hci_connection_t * conn;
|
|
|
|
// house-keeping
|
|
|
|
|
|
|
|
// create_connection?
|
|
|
|
if (IS_COMMAND(packet, hci_create_connection)){
|
|
|
|
bt_flip_addr(addr, &packet[3]);
|
2011-07-27 22:03:34 +00:00
|
|
|
log_info("Create_connection to %s\n", bd_addr_to_str(addr));
|
2009-08-02 13:20:32 +00:00
|
|
|
conn = connection_for_address(addr);
|
|
|
|
if (conn) {
|
|
|
|
// if connection exists
|
|
|
|
if (conn->state == OPEN) {
|
2011-07-25 18:05:47 +00:00
|
|
|
// and OPEN, emit connection complete command
|
|
|
|
hci_emit_connection_complete(conn, 0);
|
2009-08-02 13:20:32 +00:00
|
|
|
}
|
2011-07-25 18:05:47 +00:00
|
|
|
// otherwise, just ignore as it is already in the open process
|
2009-08-02 13:20:32 +00:00
|
|
|
return 0; // don't sent packet to controller
|
|
|
|
|
|
|
|
}
|
2011-07-25 18:05:47 +00:00
|
|
|
// create connection struct and register, state = SENT_CREATE_CONNECTION
|
|
|
|
conn = create_connection_for_addr(addr);
|
|
|
|
if (!conn){
|
|
|
|
// notify client that alloc failed
|
|
|
|
hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED);
|
|
|
|
return 0; // don't sent packet to controller
|
|
|
|
}
|
|
|
|
conn->state = SENT_CREATE_CONNECTION;
|
2009-07-16 20:12:42 +00:00
|
|
|
}
|
2009-08-02 13:20:32 +00:00
|
|
|
|
2010-08-26 20:12:05 +00:00
|
|
|
if (IS_COMMAND(packet, hci_link_key_request_reply)){
|
|
|
|
hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
|
|
|
|
}
|
|
|
|
if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
|
|
|
|
hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
|
|
|
|
}
|
|
|
|
if (IS_COMMAND(packet, hci_pin_code_request_reply)){
|
|
|
|
hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_REPLY);
|
|
|
|
}
|
|
|
|
if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)){
|
|
|
|
hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_NEGATIVE_REPLY);
|
|
|
|
}
|
|
|
|
|
2010-09-02 21:56:28 +00:00
|
|
|
if (IS_COMMAND(packet, hci_delete_stored_link_key)){
|
|
|
|
if (hci_stack.remote_device_db){
|
|
|
|
bt_flip_addr(addr, &packet[3]);
|
|
|
|
hci_stack.remote_device_db->delete_link_key(&addr);
|
|
|
|
}
|
|
|
|
}
|
2009-08-02 13:20:32 +00:00
|
|
|
|
|
|
|
hci_stack.num_cmd_packets--;
|
2010-08-16 17:23:22 +00:00
|
|
|
return hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
|
2009-05-16 21:29:51 +00:00
|
|
|
}
|
|
|
|
|
2009-07-15 22:12:54 +00:00
|
|
|
/**
|
|
|
|
* pre: numcmds >= 0 - it's allowed to send a command to the controller
|
|
|
|
*/
|
2010-08-08 18:10:45 +00:00
|
|
|
int hci_send_cmd(const hci_cmd_t *cmd, ...){
|
2009-07-15 22:12:54 +00:00
|
|
|
va_list argptr;
|
|
|
|
va_start(argptr, cmd);
|
2011-07-29 21:04:09 +00:00
|
|
|
uint16_t size = hci_create_cmd_internal(hci_stack.hci_packet_buffer, cmd, argptr);
|
2009-07-15 22:12:54 +00:00
|
|
|
va_end(argptr);
|
2011-07-29 21:04:09 +00:00
|
|
|
return hci_send_cmd_packet(hci_stack.hci_packet_buffer, size);
|
2009-08-02 13:20:32 +00:00
|
|
|
}
|
|
|
|
|
2009-08-09 18:39:53 +00:00
|
|
|
// Create various non-HCI events.
|
|
|
|
// TODO: generalize, use table similar to hci_create_command
|
|
|
|
|
2009-08-02 13:20:32 +00:00
|
|
|
void hci_emit_state(){
|
2011-07-16 09:44:07 +00:00
|
|
|
uint8_t event[3];
|
2009-09-29 19:40:55 +00:00
|
|
|
event[0] = BTSTACK_EVENT_STATE;
|
2011-07-16 09:44:07 +00:00
|
|
|
event[1] = sizeof(event) - 2;
|
2009-08-02 13:20:32 +00:00
|
|
|
event[2] = hci_stack.state;
|
2011-07-16 09:44:07 +00:00
|
|
|
hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
|
|
|
|
hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
|
2009-08-02 13:20:32 +00:00
|
|
|
}
|
|
|
|
|
2011-07-25 18:05:47 +00:00
|
|
|
void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){
|
2011-07-16 09:44:07 +00:00
|
|
|
uint8_t event[13];
|
2009-08-02 13:20:32 +00:00
|
|
|
event[0] = HCI_EVENT_CONNECTION_COMPLETE;
|
2011-07-16 09:44:07 +00:00
|
|
|
event[1] = sizeof(event) - 2;
|
2011-07-25 18:05:47 +00:00
|
|
|
event[2] = status;
|
2009-08-02 13:20:32 +00:00
|
|
|
bt_store_16(event, 3, conn->con_handle);
|
|
|
|
bt_flip_addr(&event[5], conn->address);
|
|
|
|
event[11] = 1; // ACL connection
|
|
|
|
event[12] = 0; // encryption disabled
|
2011-07-16 09:44:07 +00:00
|
|
|
hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
|
|
|
|
hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
|
2009-08-02 13:20:32 +00:00
|
|
|
}
|
|
|
|
|
2011-04-30 20:11:20 +00:00
|
|
|
void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
|
2011-07-16 09:44:07 +00:00
|
|
|
uint8_t event[6];
|
2011-04-30 20:11:20 +00:00
|
|
|
event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
|
2011-07-16 09:47:52 +00:00
|
|
|
event[1] = sizeof(event) - 2;
|
2011-04-30 20:11:20 +00:00
|
|
|
event[2] = 0; // status = OK
|
|
|
|
bt_store_16(event, 3, handle);
|
|
|
|
event[5] = reason;
|
2011-07-16 09:44:07 +00:00
|
|
|
hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
|
|
|
|
hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
|
2011-04-30 20:11:20 +00:00
|
|
|
}
|
|
|
|
|
2009-08-09 18:39:53 +00:00
|
|
|
void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
|
2011-07-16 09:44:07 +00:00
|
|
|
uint8_t event[4];
|
2009-09-29 19:40:55 +00:00
|
|
|
event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
|
2011-07-16 09:44:07 +00:00
|
|
|
event[1] = sizeof(event) - 2;
|
2009-08-09 18:39:53 +00:00
|
|
|
bt_store_16(event, 2, conn->con_handle);
|
2011-07-16 09:44:07 +00:00
|
|
|
hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
|
|
|
|
hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
|
2009-08-09 18:39:53 +00:00
|
|
|
}
|
2009-08-15 21:31:23 +00:00
|
|
|
|
|
|
|
void hci_emit_nr_connections_changed(){
|
2011-07-16 09:44:07 +00:00
|
|
|
uint8_t event[3];
|
2009-09-29 19:40:55 +00:00
|
|
|
event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
|
2011-07-16 09:44:07 +00:00
|
|
|
event[1] = sizeof(event) - 2;
|
2009-08-15 21:31:23 +00:00
|
|
|
event[2] = nr_hci_connections();
|
2011-07-16 09:44:07 +00:00
|
|
|
hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
|
|
|
|
hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
|
2009-08-15 21:31:23 +00:00
|
|
|
}
|
2009-09-13 20:42:43 +00:00
|
|
|
|
|
|
|
void hci_emit_hci_open_failed(){
|
2011-07-16 09:44:07 +00:00
|
|
|
uint8_t event[2];
|
2009-09-29 19:40:55 +00:00
|
|
|
event[0] = BTSTACK_EVENT_POWERON_FAILED;
|
2011-07-16 09:44:07 +00:00
|
|
|
event[1] = sizeof(event) - 2;
|
|
|
|
hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
|
|
|
|
hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
|
2010-01-09 11:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void hci_emit_btstack_version() {
|
2011-07-16 09:44:07 +00:00
|
|
|
uint8_t event[6];
|
2010-01-09 11:01:23 +00:00
|
|
|
event[0] = BTSTACK_EVENT_VERSION;
|
2011-07-16 09:44:07 +00:00
|
|
|
event[1] = sizeof(event) - 2;
|
|
|
|
event[2] = BTSTACK_MAJOR;
|
|
|
|
event[3] = BTSTACK_MINOR;
|
|
|
|
bt_store_16(event, 4, BTSTACK_REVISION);
|
|
|
|
hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
|
|
|
|
hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
|
2009-09-13 20:42:43 +00:00
|
|
|
}
|
2010-01-09 11:01:23 +00:00
|
|
|
|
2010-01-09 18:25:30 +00:00
|
|
|
void hci_emit_system_bluetooth_enabled(uint8_t enabled){
|
2011-07-16 09:44:07 +00:00
|
|
|
uint8_t event[3];
|
2010-01-09 18:25:30 +00:00
|
|
|
event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
|
2011-07-16 09:44:07 +00:00
|
|
|
event[1] = sizeof(event) - 2;
|
2010-01-09 18:25:30 +00:00
|
|
|
event[2] = enabled;
|
2011-07-16 09:44:07 +00:00
|
|
|
hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
|
2011-07-16 09:47:52 +00:00
|
|
|
hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
|
2010-01-09 18:25:30 +00:00
|
|
|
}
|
2010-09-01 21:25:40 +00:00
|
|
|
|
|
|
|
void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){
|
2011-07-16 09:44:07 +00:00
|
|
|
uint8_t event[2+1+6+248];
|
2010-09-01 21:25:40 +00:00
|
|
|
event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
|
2011-07-16 09:44:07 +00:00
|
|
|
event[1] = sizeof(event) - 2;
|
2010-09-02 18:39:04 +00:00
|
|
|
event[2] = 0; // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
|
2010-09-14 20:12:22 +00:00
|
|
|
bt_flip_addr(&event[3], *addr);
|
2010-09-02 18:39:04 +00:00
|
|
|
memcpy(&event[9], name, 248);
|
2011-07-16 09:44:07 +00:00
|
|
|
hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
|
|
|
|
hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
|
2010-09-01 21:25:40 +00:00
|
|
|
}
|
2011-01-10 21:11:58 +00:00
|
|
|
|
|
|
|
void hci_emit_discoverable_enabled(uint8_t enabled){
|
2011-07-16 09:44:07 +00:00
|
|
|
uint8_t event[3];
|
2011-01-10 21:11:58 +00:00
|
|
|
event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
|
2011-07-16 09:44:07 +00:00
|
|
|
event[1] = sizeof(event) - 2;
|
2011-01-10 21:11:58 +00:00
|
|
|
event[2] = enabled;
|
2011-07-16 09:44:07 +00:00
|
|
|
hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
|
|
|
|
hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
|
2011-01-10 21:11:58 +00:00
|
|
|
}
|