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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-07-04 19:28:21 +00:00
|
|
|
#include "hci.h"
|
|
|
|
|
2009-05-03 21:28:40 +00:00
|
|
|
#include <unistd.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
|
|
|
|
|
|
|
#include "debug.h"
|
2009-07-23 21:25:39 +00:00
|
|
|
#include "hci_dump.h"
|
2009-05-10 19:55:02 +00:00
|
|
|
|
2010-01-09 18:25:30 +00:00
|
|
|
#include "../include/btstack/hci_cmds.h"
|
|
|
|
#include "../include/btstack/version.h"
|
2010-01-09 11:01:23 +00:00
|
|
|
|
2009-07-31 21:41:15 +00:00
|
|
|
// temp
|
|
|
|
#include "l2cap.h"
|
|
|
|
|
2010-01-26 18:28:21 +00:00
|
|
|
#define HCI_CONNECTION_TIMEOUT_MS 10000
|
2009-08-09 18:39:53 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-11-23 19:45:32 +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);
|
|
|
|
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);
|
2009-11-05 22:26:43 +00:00
|
|
|
run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS);
|
2009-08-09 18:39:53 +00:00
|
|
|
} else {
|
|
|
|
// next timeout check at
|
2009-11-05 22:26:43 +00:00
|
|
|
timer->timeout.tv_sec = connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000;
|
2009-08-09 18:39:53 +00:00
|
|
|
}
|
|
|
|
run_loop_add_timer(timer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hci_connection_timestamp(hci_connection_t *connection){
|
|
|
|
gettimeofday(&connection->timestamp, NULL);
|
|
|
|
}
|
|
|
|
|
2009-08-02 13:20:32 +00:00
|
|
|
/**
|
|
|
|
* create connection for given address
|
|
|
|
*
|
|
|
|
* @return connection OR NULL, if not found
|
|
|
|
*/
|
|
|
|
static hci_connection_t * create_connection_for_addr(bd_addr_t addr){
|
|
|
|
hci_connection_t * conn = malloc( sizeof(hci_connection_t) );
|
|
|
|
if (!conn) return NULL;
|
|
|
|
BD_ADDR_COPY(conn->address, addr);
|
|
|
|
conn->con_handle = 0xffff;
|
|
|
|
conn->flags = 0;
|
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;
|
|
|
|
}
|
|
|
|
|
2009-08-15 21:31:23 +00:00
|
|
|
/**
|
|
|
|
* count connections
|
|
|
|
*/
|
|
|
|
static int nr_hci_connections(){
|
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
|
|
|
}
|
|
|
|
|
2009-07-23 21:43:37 +00:00
|
|
|
/**
|
|
|
|
* Dummy control handler
|
|
|
|
*/
|
|
|
|
static int null_control_function(void *config){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
static const char * null_control_name(void *config){
|
|
|
|
return "Hardware unknown";
|
|
|
|
}
|
|
|
|
static bt_control_t null_control = {
|
|
|
|
null_control_function,
|
|
|
|
null_control_function,
|
|
|
|
null_control_function,
|
|
|
|
null_control_name
|
|
|
|
};
|
|
|
|
|
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) {
|
|
|
|
log_err("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle);
|
|
|
|
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) {
|
|
|
|
log_err("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
free_slots -= connection->num_acl_packets_sent;
|
|
|
|
}
|
|
|
|
return free_slots;
|
|
|
|
}
|
|
|
|
|
|
|
|
int hci_ready_to_send(hci_con_handle_t handle){
|
|
|
|
return hci_number_free_acl_slots() && hci_number_outgoing_packets(handle) < 2;
|
|
|
|
}
|
2009-08-02 13:20:32 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
// update idle timestamp
|
|
|
|
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++;
|
2010-07-28 19:12:39 +00:00
|
|
|
log_dbg("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
|
|
|
|
2010-02-13 15:15:19 +00:00
|
|
|
// send packet
|
2009-08-09 18:39:53 +00:00
|
|
|
return hci_stack.hci_transport->send_acl_packet(packet, size);
|
|
|
|
}
|
|
|
|
|
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){
|
2010-07-04 19:28:21 +00:00
|
|
|
log_err( "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) {
|
2010-07-04 19:28:21 +00:00
|
|
|
log_err( "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;
|
|
|
|
|
2010-07-04 19:28:21 +00:00
|
|
|
// log_err( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n",
|
2010-02-13 15:15:19 +00:00
|
|
|
// acl_length, connection->acl_recombination_pos, connection->acl_recombination_length);
|
|
|
|
|
|
|
|
// 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) {
|
2010-07-04 19:28:21 +00:00
|
|
|
log_err( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle);
|
2010-02-13 15:15:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// peek into L2CAP packet!
|
|
|
|
uint16_t l2cap_length = READ_L2CAP_LENGTH( packet );
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
bt_store_16(conn->acl_recombination_buffer, 2, acl_length +4);
|
2010-07-04 19:28:21 +00:00
|
|
|
// log_err( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length);
|
2010-02-13 15:15:19 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
default:
|
2010-07-04 19:28:21 +00:00
|
|
|
log_err( "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
|
|
|
|
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;
|
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;
|
2009-05-17 20:51:48 +00:00
|
|
|
|
2010-07-25 15:11:33 +00:00
|
|
|
// get num_cmd_packets
|
|
|
|
if (packet[0] == HCI_EVENT_COMMAND_COMPLETE || packet[0] == HCI_EVENT_COMMAND_STATUS){
|
|
|
|
// Get Num_HCI_Command_Packets
|
|
|
|
hci_stack.num_cmd_packets = packet[2];
|
|
|
|
}
|
|
|
|
|
2009-12-05 18:55:41 +00:00
|
|
|
switch (packet[0]) {
|
|
|
|
|
|
|
|
case HCI_EVENT_COMMAND_COMPLETE:
|
2010-07-25 15:11:33 +00:00
|
|
|
if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){
|
|
|
|
// from offset 5
|
|
|
|
// status
|
|
|
|
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){
|
|
|
|
log_dbg("hci_read_buffer_size: size %u, count %u\n", hci_stack.acl_data_packet_length, hci_stack.total_num_acl_packets);
|
|
|
|
}
|
2010-07-25 15:11:33 +00:00
|
|
|
}
|
2009-12-05 18:55:41 +00:00
|
|
|
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){
|
|
|
|
log_err("hci_number_completed_packet lists unused con handle %u\n", handle);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
conn->num_acl_packets_sent -= num_packets;
|
2010-07-28 19:12:39 +00:00
|
|
|
log_dbg("hci_number_completed_packet (hci) %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
|
|
|
|
uint8_t link_type = packet[11];
|
2010-07-04 19:35:21 +00:00
|
|
|
log_dbg("Connection_incoming: "); print_bd_addr(addr); log_dbg(", type %u\n", 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);
|
|
|
|
}
|
|
|
|
// TODO: check for malloc failure
|
|
|
|
conn->state = ACCEPTED_CONNECTION_REQUEST;
|
|
|
|
hci_send_cmd(&hci_accept_connection_request, addr, 1);
|
|
|
|
} else {
|
|
|
|
// TODO: decline request
|
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]);
|
2010-07-04 19:35:21 +00:00
|
|
|
log_dbg("Connection_complete (status=%u)", packet[2]); print_bd_addr(addr); log_dbg("\n");
|
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);
|
|
|
|
conn->flags = 0;
|
|
|
|
|
|
|
|
gettimeofday(&conn->timestamp, NULL);
|
|
|
|
run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
|
|
|
|
run_loop_add_timer(&conn->timeout);
|
|
|
|
|
2010-07-04 19:35:21 +00:00
|
|
|
log_dbg("New connection: handle %u, ", conn->con_handle);
|
2009-12-05 18:55:41 +00:00
|
|
|
print_bd_addr( conn->address );
|
2010-07-04 19:35:21 +00:00
|
|
|
log_dbg("\n");
|
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);
|
|
|
|
free( conn );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2009-05-18 20:18:42 +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) {
|
2010-07-04 19:35:21 +00:00
|
|
|
log_dbg("Connection closed: handle %u, ", conn->con_handle);
|
2009-12-05 18:55:41 +00:00
|
|
|
print_bd_addr( conn->address );
|
2010-07-04 19:35:21 +00:00
|
|
|
log_dbg("\n");
|
2009-12-05 18:55:41 +00:00
|
|
|
run_loop_remove_timer(&conn->timeout);
|
|
|
|
linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
|
|
|
|
free( conn );
|
|
|
|
hci_emit_nr_connections_changed();
|
|
|
|
}
|
2009-07-24 21:13:53 +00:00
|
|
|
}
|
2009-12-05 18:55:41 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2009-09-24 19:36:52 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-05-20 20:46:35 +00:00
|
|
|
void hci_init(hci_transport_t *transport, void *config, bt_control_t *control){
|
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
|
2009-06-03 21:21:12 +00:00
|
|
|
if (control) {
|
|
|
|
hci_stack.control = control;
|
|
|
|
} else {
|
|
|
|
hci_stack.control = &null_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;
|
|
|
|
|
2009-05-10 19:55:02 +00:00
|
|
|
// empty cmd buffer
|
2009-05-17 20:32:14 +00:00
|
|
|
hci_stack.hci_cmd_buffer = malloc(3+255);
|
2009-06-03 21:21:12 +00:00
|
|
|
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
int hci_power_control(HCI_POWER_MODE power_mode){
|
2009-08-08 21:39:58 +00:00
|
|
|
if (power_mode == HCI_POWER_ON && hci_stack.state == HCI_STATE_OFF) {
|
2009-06-03 21:21:12 +00:00
|
|
|
|
|
|
|
// power on
|
2009-09-13 20:42:43 +00:00
|
|
|
int err = hci_stack.control->on(hci_stack.config);
|
|
|
|
if (err){
|
2010-07-04 19:28:21 +00:00
|
|
|
log_err( "POWER_ON failed\n");
|
2009-09-13 20:42:43 +00:00
|
|
|
hci_emit_hci_open_failed();
|
|
|
|
return err;
|
|
|
|
}
|
2009-06-03 21:21:12 +00:00
|
|
|
|
|
|
|
// open low-level device
|
2009-09-13 20:42:43 +00:00
|
|
|
err = hci_stack.hci_transport->open(hci_stack.config);
|
|
|
|
if (err){
|
2010-07-04 19:28:21 +00:00
|
|
|
log_err( "HCI_INIT failed, turning Bluetooth off again\n");
|
2009-09-13 20:42:43 +00:00
|
|
|
hci_stack.control->off(hci_stack.config);
|
|
|
|
hci_emit_hci_open_failed();
|
|
|
|
return err;
|
|
|
|
}
|
2009-06-03 21:21:12 +00:00
|
|
|
|
2009-09-13 20:42:43 +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;
|
2009-07-24 21:37:45 +00:00
|
|
|
|
2009-08-08 21:39:58 +00:00
|
|
|
} else if (power_mode == HCI_POWER_OFF && hci_stack.state == HCI_STATE_WORKING){
|
2009-06-03 21:21:12 +00:00
|
|
|
|
|
|
|
// close low-level device
|
|
|
|
hci_stack.hci_transport->close(hci_stack.config);
|
|
|
|
|
|
|
|
// power off
|
|
|
|
hci_stack.control->off(hci_stack.config);
|
2009-08-15 21:31:23 +00:00
|
|
|
|
|
|
|
// we're off now
|
|
|
|
hci_stack.state = HCI_STATE_OFF;
|
2009-05-20 20:46:35 +00:00
|
|
|
}
|
2009-07-24 21:37:45 +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
|
|
|
|
2009-07-24 20:17:46 +00:00
|
|
|
void hci_run(){
|
2009-05-18 20:18:42 +00:00
|
|
|
switch (hci_stack.state){
|
|
|
|
case HCI_STATE_INITIALIZING:
|
|
|
|
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
|
|
|
}
|
|
|
|
if (hci_stack.num_cmd_packets == 0) {
|
|
|
|
// cannot send command yet
|
2009-07-24 20:17:46 +00:00
|
|
|
return;
|
2009-05-18 20:18:42 +00:00
|
|
|
}
|
|
|
|
switch (hci_stack.substate/2){
|
|
|
|
case 0:
|
|
|
|
hci_send_cmd(&hci_reset);
|
|
|
|
break;
|
2009-06-14 21:48:08 +00:00
|
|
|
case 1:
|
|
|
|
hci_send_cmd(&hci_read_bd_addr);
|
|
|
|
break;
|
2010-07-25 15:11:33 +00:00
|
|
|
case 2:
|
|
|
|
hci_send_cmd(&hci_read_buffer_size);
|
|
|
|
break;
|
|
|
|
case 3:
|
2009-05-18 20:18:42 +00:00
|
|
|
// ca. 15 sec
|
|
|
|
hci_send_cmd(&hci_write_page_timeout, 0x6000);
|
|
|
|
break;
|
2010-07-25 15:11:33 +00:00
|
|
|
case 4:
|
2009-07-01 20:56:27 +00:00
|
|
|
hci_send_cmd(&hci_write_scan_enable, 3); // 3 inq scan + page scan
|
2009-06-14 21:48:08 +00:00
|
|
|
break;
|
2010-07-25 15:11:33 +00:00
|
|
|
case 5:
|
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;
|
|
|
|
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]);
|
2010-07-04 19:35:21 +00:00
|
|
|
log_dbg("Create_connection to "); print_bd_addr(addr); log_dbg("\n");
|
2009-08-02 13:20:32 +00:00
|
|
|
conn = connection_for_address(addr);
|
|
|
|
if (conn) {
|
|
|
|
// if connection exists
|
|
|
|
if (conn->state == OPEN) {
|
|
|
|
// if OPEN, emit connection complete command
|
|
|
|
hci_emit_connection_complete(conn);
|
|
|
|
}
|
|
|
|
// otherwise, just ignore
|
|
|
|
return 0; // don't sent packet to controller
|
|
|
|
|
|
|
|
} else{
|
|
|
|
conn = create_connection_for_addr(addr);
|
|
|
|
if (conn){
|
|
|
|
// create connection struct and register, state = SENT_CREATE_CONNECTION
|
|
|
|
conn->state = SENT_CREATE_CONNECTION;
|
|
|
|
}
|
|
|
|
}
|
2009-07-16 20:12:42 +00:00
|
|
|
}
|
2009-08-02 13:20:32 +00:00
|
|
|
|
|
|
|
// accept connection
|
|
|
|
|
|
|
|
// reject connection
|
|
|
|
|
|
|
|
// close_connection?
|
|
|
|
// set state = SENT_DISCONNECT
|
|
|
|
|
|
|
|
hci_stack.num_cmd_packets--;
|
|
|
|
return hci_stack.hci_transport->send_cmd_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
|
|
|
|
*/
|
|
|
|
int hci_send_cmd(hci_cmd_t *cmd, ...){
|
|
|
|
va_list argptr;
|
|
|
|
va_start(argptr, cmd);
|
|
|
|
uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer;
|
|
|
|
uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr);
|
|
|
|
va_end(argptr);
|
|
|
|
return hci_send_cmd_packet(hci_cmd_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(){
|
|
|
|
uint8_t len = 3;
|
|
|
|
uint8_t event[len];
|
2009-09-29 19:40:55 +00:00
|
|
|
event[0] = BTSTACK_EVENT_STATE;
|
2010-01-09 11:01:23 +00:00
|
|
|
event[1] = len - 3;
|
2009-08-02 13:20:32 +00:00
|
|
|
event[2] = hci_stack.state;
|
|
|
|
hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
|
2010-07-18 16:30:16 +00:00
|
|
|
hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
|
2009-08-02 13:20:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void hci_emit_connection_complete(hci_connection_t *conn){
|
|
|
|
uint8_t len = 13;
|
|
|
|
uint8_t event[len];
|
|
|
|
event[0] = HCI_EVENT_CONNECTION_COMPLETE;
|
2010-01-09 11:01:23 +00:00
|
|
|
event[1] = len - 3;
|
2009-08-02 13:20:32 +00:00
|
|
|
event[2] = 0; // status = OK
|
|
|
|
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
|
|
|
|
hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
|
2010-07-18 16:30:16 +00:00
|
|
|
hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
|
2009-08-02 13:20:32 +00:00
|
|
|
}
|
|
|
|
|
2009-08-09 18:39:53 +00:00
|
|
|
void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
|
|
|
|
uint8_t len = 4;
|
|
|
|
uint8_t event[len];
|
2009-09-29 19:40:55 +00:00
|
|
|
event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
|
2010-01-09 11:01:23 +00:00
|
|
|
event[1] = len - 2;
|
2009-08-09 18:39:53 +00:00
|
|
|
bt_store_16(event, 2, conn->con_handle);
|
|
|
|
hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
|
2010-07-18 16:30:16 +00:00
|
|
|
hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
|
2009-08-09 18:39:53 +00:00
|
|
|
}
|
2009-08-15 21:31:23 +00:00
|
|
|
|
|
|
|
void hci_emit_nr_connections_changed(){
|
|
|
|
uint8_t len = 3;
|
|
|
|
uint8_t event[len];
|
2009-09-29 19:40:55 +00:00
|
|
|
event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
|
2010-01-09 11:01:23 +00:00
|
|
|
event[1] = len - 2;
|
2009-08-15 21:31:23 +00:00
|
|
|
event[2] = nr_hci_connections();
|
|
|
|
hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
|
2010-07-18 16:30:16 +00:00
|
|
|
hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
|
2009-08-15 21:31:23 +00:00
|
|
|
}
|
2009-09-13 20:42:43 +00:00
|
|
|
|
|
|
|
void hci_emit_hci_open_failed(){
|
2010-01-09 11:01:23 +00:00
|
|
|
uint8_t len = 2;
|
2009-09-13 20:42:43 +00:00
|
|
|
uint8_t event[len];
|
2009-09-29 19:40:55 +00:00
|
|
|
event[0] = BTSTACK_EVENT_POWERON_FAILED;
|
2010-01-09 11:01:23 +00:00
|
|
|
event[1] = len - 2;
|
|
|
|
hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
|
2010-07-18 16:30:16 +00:00
|
|
|
hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
|
2010-01-09 11:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void hci_emit_btstack_version() {
|
|
|
|
uint8_t len = 6;
|
|
|
|
uint8_t event[len];
|
|
|
|
event[0] = BTSTACK_EVENT_VERSION;
|
|
|
|
event[1] = len - 2;
|
|
|
|
event[len++] = BTSTACK_MAJOR;
|
|
|
|
event[len++] = BTSTACK_MINOR;
|
|
|
|
bt_store_16(event, len, BTSTACK_REVISION);
|
2009-09-13 20:42:43 +00:00
|
|
|
hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
|
2010-07-18 16:30:16 +00:00
|
|
|
hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
|
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){
|
|
|
|
uint8_t len = 3;
|
|
|
|
uint8_t event[len];
|
|
|
|
event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
|
2010-05-24 15:38:58 +00:00
|
|
|
event[1] = len - 2;
|
2010-01-09 18:25:30 +00:00
|
|
|
event[2] = enabled;
|
|
|
|
hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
|
2010-07-18 16:30:16 +00:00
|
|
|
hci_stack.packet_handler(HCI_EVENT_PACKET, event, len);
|
2010-01-09 18:25:30 +00:00
|
|
|
}
|