2011-05-01 11:36:12 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 by Matthias Ringwald
|
2011-06-12 12:33:31 +00:00
|
|
|
*
|
2011-05-01 11:36:12 +00:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* rfcomm.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2011-06-04 15:16:32 +00:00
|
|
|
#include <string.h> // memcpy
|
2011-05-01 11:36:12 +00:00
|
|
|
|
|
|
|
#include <btstack/btstack.h>
|
|
|
|
#include <btstack/hci_cmds.h>
|
|
|
|
#include <btstack/utils.h>
|
|
|
|
|
|
|
|
#include <btstack/utils.h>
|
|
|
|
#include "hci.h"
|
|
|
|
#include "hci_dump.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "rfcomm.h"
|
|
|
|
|
|
|
|
// Control field values bit no. 1 2 3 4 PF 6 7 8
|
|
|
|
#define BT_RFCOMM_SABM 0x3F // 1 1 1 1 1 1 0 0
|
|
|
|
#define BT_RFCOMM_UA 0x73 // 1 1 0 0 1 1 1 0
|
|
|
|
#define BT_RFCOMM_DM 0x0F // 1 1 1 1 0 0 0 0
|
|
|
|
#define BT_RFCOMM_DM_PF 0x1F // 1 1 1 1 1 0 0 0
|
|
|
|
#define BT_RFCOMM_DISC 0x53 // 1 1 0 0 1 0 1 0
|
|
|
|
#define BT_RFCOMM_UIH 0xEF // 1 1 1 1 0 1 1 1
|
|
|
|
#define BT_RFCOMM_UIH_PF 0xFF // 1 1 1 1 0 1 1 1
|
|
|
|
|
|
|
|
// Multiplexer message types
|
|
|
|
#define BT_RFCOMM_CLD_CMD 0xC3
|
|
|
|
#define BT_RFCOMM_FCON_CMD 0xA3
|
|
|
|
#define BT_RFCOMM_FCON_RSP 0xA1
|
|
|
|
#define BT_RFCOMM_FCOFF_CMD 0x63
|
|
|
|
#define BT_RFCOMM_FCOFF_RSP 0x61
|
|
|
|
#define BT_RFCOMM_MSC_CMD 0xE3
|
|
|
|
#define BT_RFCOMM_MSC_RSP 0xE1
|
|
|
|
#define BT_RFCOMM_NSC_RSP 0x11
|
|
|
|
#define BT_RFCOMM_PN_CMD 0x83
|
|
|
|
#define BT_RFCOMM_PN_RSP 0x81
|
|
|
|
#define BT_RFCOMM_RLS_CMD 0x53
|
|
|
|
#define BT_RFCOMM_RLS_RSP 0x51
|
|
|
|
#define BT_RFCOMM_RPN_CMD 0x93
|
|
|
|
#define BT_RFCOMM_RPN_RSP 0x91
|
|
|
|
#define BT_RFCOMM_TEST_CMD 0x23
|
|
|
|
#define BT_RFCOMM_TEST_RSP 0x21
|
|
|
|
|
2011-07-07 22:50:58 +00:00
|
|
|
#define RFCOMM_MAX_PAYLOAD (HCI_ACL_3DH5_SIZE-HCI_ACL_DATA_PKT_HDR)
|
2011-05-01 11:36:12 +00:00
|
|
|
#define RFCOMM_MULIPLEXER_TIMEOUT_MS 60000
|
|
|
|
|
|
|
|
// FCS calc
|
|
|
|
#define BT_RFCOMM_CODE_WORD 0xE0 // pol = x8+x2+x1+1
|
|
|
|
#define BT_RFCOMM_CRC_CHECK_LEN 3
|
|
|
|
#define BT_RFCOMM_UIHCRC_CHECK_LEN 2
|
|
|
|
|
|
|
|
#include "l2cap.h"
|
|
|
|
|
|
|
|
// private structs
|
|
|
|
typedef enum {
|
|
|
|
RFCOMM_MULTIPLEXER_CLOSED = 1,
|
|
|
|
RFCOMM_MULTIPLEXER_W4_CONNECT, // outgoing
|
2011-06-24 21:11:56 +00:00
|
|
|
RFCOMM_MULTIPLEXER_SEND_SABM_0, // "
|
2011-05-01 11:36:12 +00:00
|
|
|
RFCOMM_MULTIPLEXER_W4_UA_0, // "
|
|
|
|
RFCOMM_MULTIPLEXER_W4_SABM_0, // incoming
|
2011-06-24 21:11:56 +00:00
|
|
|
RFCOMM_MULTIPLEXER_SEND_UA_0,
|
2011-05-01 11:36:12 +00:00
|
|
|
RFCOMM_MULTIPLEXER_OPEN,
|
2011-06-24 21:11:56 +00:00
|
|
|
RFCOMM_MULTIPLEXER_SEND_UA_0_AND_DISC
|
2011-05-01 11:36:12 +00:00
|
|
|
} RFCOMM_MULTIPLEXER_STATE;
|
|
|
|
|
2011-07-07 22:37:17 +00:00
|
|
|
typedef enum {
|
|
|
|
MULT_EV_READY_TO_SEND = 1,
|
|
|
|
|
|
|
|
} RFCOMM_MULTIPLEXER_EVENT;
|
|
|
|
|
2011-05-01 11:36:12 +00:00
|
|
|
typedef enum {
|
|
|
|
RFCOMM_CHANNEL_CLOSED = 1,
|
|
|
|
RFCOMM_CHANNEL_W4_MULTIPLEXER,
|
2011-07-04 21:49:59 +00:00
|
|
|
RFCOMM_CHANNEL_SEND_UIH_PN,
|
2011-05-01 11:36:12 +00:00
|
|
|
RFCOMM_CHANNEL_W4_PN_RSP,
|
2011-06-24 18:37:10 +00:00
|
|
|
RFCOMM_CHANNEL_SEND_SABM_W4_UA,
|
2011-05-01 11:36:12 +00:00
|
|
|
RFCOMM_CHANNEL_W4_UA,
|
2011-07-06 14:46:29 +00:00
|
|
|
RFCOMM_CHANNEL_INCOMING_SETUP,
|
|
|
|
RFCOMM_CHANNEL_DLC_SETUP,
|
|
|
|
RFCOMM_CHANNEL_OPEN,
|
2011-07-07 16:42:40 +00:00
|
|
|
RFCOMM_CHANNEL_SEND_UA_AFTER_DISC,
|
2011-07-06 14:46:29 +00:00
|
|
|
RFCOMM_CHANNEL_SEND_DISC,
|
|
|
|
RFCOMM_CHANNEL_SEND_DM,
|
|
|
|
|
2011-05-01 11:36:12 +00:00
|
|
|
} RFCOMM_CHANNEL_STATE;
|
|
|
|
|
2011-07-05 08:30:43 +00:00
|
|
|
typedef enum {
|
2011-07-22 10:09:45 +00:00
|
|
|
STATE_VAR_NONE = 0,
|
2011-07-05 08:30:43 +00:00
|
|
|
STATE_VAR_CLIENT_ACCEPTED = 1 << 0,
|
2011-07-05 14:59:00 +00:00
|
|
|
STATE_VAR_RCVD_PN = 1 << 1,
|
|
|
|
STATE_VAR_RCVD_RPN = 1 << 2,
|
|
|
|
STATE_VAR_RCVD_SABM = 1 << 3,
|
2011-07-15 20:42:57 +00:00
|
|
|
|
2011-07-06 14:46:29 +00:00
|
|
|
STATE_VAR_RCVD_MSC_CMD = 1 << 4,
|
|
|
|
STATE_VAR_RCVD_MSC_RSP = 1 << 5,
|
|
|
|
STATE_VAR_SEND_PN_RSP = 1 << 6,
|
|
|
|
STATE_VAR_SEND_RPN_INFO = 1 << 7,
|
2011-07-15 20:42:57 +00:00
|
|
|
|
2011-07-06 14:46:29 +00:00
|
|
|
STATE_VAR_SEND_RPN_RSP = 1 << 8,
|
|
|
|
STATE_VAR_SEND_UA = 1 << 9,
|
|
|
|
STATE_VAR_SEND_MSC_CMD = 1 << 10,
|
|
|
|
STATE_VAR_SEND_MSC_RSP = 1 << 11,
|
2011-07-15 20:42:57 +00:00
|
|
|
|
2011-07-07 15:07:27 +00:00
|
|
|
STATE_VAR_SEND_CREDITS = 1 << 12,
|
|
|
|
STATE_VAR_SENT_MSC_CMD = 1 << 13,
|
|
|
|
STATE_VAR_SENT_MSC_RSP = 1 << 14,
|
|
|
|
STATE_VAR_SENT_CREDITS = 1 << 15,
|
2011-07-05 08:30:43 +00:00
|
|
|
} RFCOMM_CHANNEL_STATE_VAR;
|
|
|
|
|
2011-07-05 16:29:22 +00:00
|
|
|
typedef enum {
|
|
|
|
CH_EVT_RCVD_SABM = 1,
|
|
|
|
CH_EVT_RCVD_UA,
|
|
|
|
CH_EVT_RCVD_PN,
|
|
|
|
CH_EVT_RCVD_PN_RSP,
|
2011-07-05 16:58:18 +00:00
|
|
|
CH_EVT_RCVD_DISC,
|
|
|
|
CH_EVT_RCVD_DM,
|
2011-07-06 10:52:28 +00:00
|
|
|
CH_EVT_RCVD_MSC_CMD,
|
|
|
|
CH_EVT_RCVD_MSC_RSP,
|
|
|
|
CH_EVT_RCVD_RPN_CMD,
|
|
|
|
CH_EVT_RCVD_RPN_REQ,
|
2011-07-06 14:00:01 +00:00
|
|
|
CH_EVT_MULTIPLEXER_READY,
|
2011-07-07 15:10:29 +00:00
|
|
|
CH_EVT_READY_TO_SEND,
|
2011-07-05 16:29:22 +00:00
|
|
|
} RFCOMM_CHANNEL_EVENT;
|
|
|
|
|
|
|
|
typedef struct rfcomm_channel_event {
|
|
|
|
RFCOMM_CHANNEL_EVENT type;
|
|
|
|
} rfcomm_channel_event_t;
|
|
|
|
|
|
|
|
typedef struct rfcomm_channel_event_pn {
|
|
|
|
rfcomm_channel_event_t super;
|
|
|
|
uint16_t max_frame_size;
|
|
|
|
uint8_t priority;
|
|
|
|
uint8_t credits_outgoing;
|
|
|
|
} rfcomm_channel_event_pn_t;
|
|
|
|
|
2011-07-06 10:52:28 +00:00
|
|
|
typedef struct rfcomm_rpn_data {
|
|
|
|
uint8_t baud_rate;
|
|
|
|
uint8_t flags;
|
|
|
|
uint8_t flow_control;
|
|
|
|
uint8_t xon;
|
|
|
|
uint8_t xoff;
|
|
|
|
uint8_t parameter_mask_0; // first byte
|
|
|
|
uint8_t parameter_mask_1; // second byte
|
|
|
|
} rfcomm_rpn_data_t;
|
|
|
|
|
|
|
|
typedef struct rfcomm_channel_event_rpn {
|
|
|
|
rfcomm_channel_event_t super;
|
|
|
|
rfcomm_rpn_data_t data;
|
|
|
|
} rfcomm_channel_event_rpn_t;
|
|
|
|
|
2011-05-01 11:36:12 +00:00
|
|
|
// info regarding potential connections
|
|
|
|
typedef struct {
|
|
|
|
// linked list - assert: first field
|
|
|
|
linked_item_t item;
|
|
|
|
|
|
|
|
// server channel
|
2011-05-03 21:14:41 +00:00
|
|
|
uint8_t server_channel;
|
2011-05-01 11:36:12 +00:00
|
|
|
|
|
|
|
// incoming max frame size
|
|
|
|
uint16_t max_frame_size;
|
|
|
|
|
|
|
|
// client connection
|
|
|
|
void *connection;
|
|
|
|
|
|
|
|
// internal connection
|
|
|
|
btstack_packet_handler_t packet_handler;
|
|
|
|
|
|
|
|
} rfcomm_service_t;
|
|
|
|
|
|
|
|
// info regarding multiplexer
|
|
|
|
// note: spec mandates single multplexer per device combination
|
|
|
|
typedef struct {
|
|
|
|
// linked list - assert: first field
|
|
|
|
linked_item_t item;
|
|
|
|
|
|
|
|
timer_source_t timer;
|
|
|
|
int timer_active;
|
|
|
|
|
|
|
|
RFCOMM_MULTIPLEXER_STATE state;
|
|
|
|
|
|
|
|
uint16_t l2cap_cid;
|
|
|
|
uint8_t l2cap_credits;
|
|
|
|
|
|
|
|
bd_addr_t remote_addr;
|
|
|
|
hci_con_handle_t con_handle;
|
|
|
|
|
|
|
|
uint8_t outgoing;
|
|
|
|
|
|
|
|
// hack to deal with authentication failure only observed by remote side
|
|
|
|
uint8_t at_least_one_connection;
|
|
|
|
|
2011-06-12 12:33:31 +00:00
|
|
|
uint16_t max_frame_size;
|
2011-07-07 23:06:51 +00:00
|
|
|
|
|
|
|
// send DM for DLCI != 0
|
|
|
|
uint8_t send_dm_for_dlci;
|
2011-06-12 12:33:31 +00:00
|
|
|
|
2011-05-01 11:36:12 +00:00
|
|
|
} rfcomm_multiplexer_t;
|
|
|
|
|
|
|
|
// info regarding an actual coneection
|
|
|
|
typedef struct {
|
|
|
|
// linked list - assert: first field
|
|
|
|
linked_item_t item;
|
|
|
|
|
|
|
|
rfcomm_multiplexer_t *multiplexer;
|
|
|
|
uint16_t rfcomm_cid;
|
|
|
|
uint8_t outgoing;
|
|
|
|
uint8_t dlci;
|
|
|
|
|
|
|
|
// credits for outgoing traffic
|
|
|
|
uint8_t credits_outgoing;
|
|
|
|
|
|
|
|
// number of packets granted to client
|
|
|
|
uint8_t packets_granted;
|
|
|
|
|
|
|
|
// credits for incoming traffic
|
|
|
|
uint8_t credits_incoming;
|
|
|
|
|
2011-07-05 08:30:43 +00:00
|
|
|
// channel state
|
|
|
|
RFCOMM_CHANNEL_STATE state;
|
|
|
|
|
|
|
|
// state variables used in RFCOMM_CHANNEL_INCOMING
|
|
|
|
RFCOMM_CHANNEL_STATE_VAR state_var;
|
|
|
|
|
2011-05-01 11:36:12 +00:00
|
|
|
// priority set by incoming side in PN
|
|
|
|
uint8_t pn_priority;
|
|
|
|
|
|
|
|
// negotiated frame size
|
|
|
|
uint16_t max_frame_size;
|
|
|
|
|
2011-07-07 23:01:10 +00:00
|
|
|
// rpn data
|
|
|
|
rfcomm_rpn_data_t rpn_data;
|
|
|
|
|
2011-05-01 11:36:12 +00:00
|
|
|
// server channel (see rfcomm_service_t) - NULL => outgoing channel
|
|
|
|
rfcomm_service_t * service;
|
|
|
|
|
|
|
|
// internal connection
|
|
|
|
btstack_packet_handler_t packet_handler;
|
|
|
|
|
|
|
|
// client connection
|
|
|
|
void * connection;
|
|
|
|
|
|
|
|
} rfcomm_channel_t;
|
|
|
|
|
|
|
|
// global rfcomm data
|
|
|
|
static uint16_t rfcomm_client_cid_generator; // used for client channel IDs
|
|
|
|
|
|
|
|
// linked lists for all
|
|
|
|
static linked_list_t rfcomm_multiplexers = NULL;
|
|
|
|
static linked_list_t rfcomm_channels = NULL;
|
|
|
|
static linked_list_t rfcomm_services = NULL;
|
|
|
|
|
|
|
|
// used to assemble rfcomm packets
|
|
|
|
uint8_t rfcomm_out_buffer[RFCOMM_MAX_PAYLOAD];
|
|
|
|
|
|
|
|
static void (*app_packet_handler)(void * connection, uint8_t packet_type,
|
|
|
|
uint16_t channel, uint8_t *packet, uint16_t size);
|
|
|
|
|
2011-06-23 20:32:12 +00:00
|
|
|
static void rfcomm_run(void);
|
2011-07-06 14:00:01 +00:00
|
|
|
static void rfcomm_hand_out_credits(void);
|
|
|
|
static void rfcomm_channel_state_machine(rfcomm_channel_t *channel, rfcomm_channel_event_t *event);
|
2011-07-06 13:45:50 +00:00
|
|
|
static void rfcomm_channel_state_machine_2(rfcomm_multiplexer_t * multiplexer, uint8_t dlci, rfcomm_channel_event_t *event);
|
2011-07-07 15:07:27 +00:00
|
|
|
static int rfcomm_channel_ready_for_open(rfcomm_channel_t *channel);
|
2011-07-07 22:50:58 +00:00
|
|
|
static void rfcomm_multiplexer_state_machine(rfcomm_multiplexer_t * multiplexer, RFCOMM_MULTIPLEXER_EVENT event);
|
2011-07-04 20:54:35 +00:00
|
|
|
|
|
|
|
// MARK: RFCOMM CLIENT EVENTS
|
|
|
|
|
|
|
|
// data: event (8), len(8), address(48), channel (8), rfcomm_cid (16)
|
|
|
|
static void rfcomm_emit_connection_request(rfcomm_channel_t *channel) {
|
|
|
|
uint8_t event[11];
|
|
|
|
event[0] = RFCOMM_EVENT_INCOMING_CONNECTION;
|
|
|
|
event[1] = sizeof(event) - 2;
|
|
|
|
bt_flip_addr(&event[2], channel->multiplexer->remote_addr);
|
|
|
|
event[8] = channel->dlci >> 1;
|
|
|
|
bt_store_16(event, 9, channel->rfcomm_cid);
|
|
|
|
hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
|
|
|
|
(*app_packet_handler)(channel->connection, HCI_EVENT_PACKET, 0, (uint8_t *) event, sizeof(event));
|
|
|
|
}
|
|
|
|
|
|
|
|
// API Change: BTstack-0.3.50x uses
|
|
|
|
// data: event(8), len(8), status (8), address (48), server channel(8), rfcomm_cid(16), max frame size(16)
|
|
|
|
// next Cydia release will use SVN version of this
|
|
|
|
// data: event(8), len(8), status (8), address (48), handle (16), server channel(8), rfcomm_cid(16), max frame size(16)
|
|
|
|
static void rfcomm_emit_channel_opened(rfcomm_channel_t *channel, uint8_t status) {
|
|
|
|
uint8_t event[16];
|
|
|
|
uint8_t pos = 0;
|
|
|
|
event[pos++] = RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE;
|
|
|
|
event[pos++] = sizeof(event) - 2;
|
|
|
|
event[pos++] = status;
|
|
|
|
bt_flip_addr(&event[pos], channel->multiplexer->remote_addr); pos += 6;
|
|
|
|
bt_store_16(event, pos, channel->multiplexer->con_handle); pos += 2;
|
|
|
|
event[pos++] = channel->dlci >> 1;
|
|
|
|
bt_store_16(event, pos, channel->rfcomm_cid); pos += 2; // channel ID
|
|
|
|
bt_store_16(event, pos, channel->max_frame_size); pos += 2; // max frame size
|
|
|
|
hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
|
|
|
|
(*app_packet_handler)(channel->connection, HCI_EVENT_PACKET, 0, (uint8_t *) event, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
// data: event(8), len(8), rfcomm_cid(16)
|
|
|
|
static void rfcomm_emit_channel_closed(rfcomm_channel_t * channel) {
|
|
|
|
uint8_t event[4];
|
|
|
|
event[0] = RFCOMM_EVENT_CHANNEL_CLOSED;
|
|
|
|
event[1] = sizeof(event) - 2;
|
|
|
|
bt_store_16(event, 2, channel->rfcomm_cid);
|
|
|
|
hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
|
|
|
|
(*app_packet_handler)(channel->connection, HCI_EVENT_PACKET, 0, (uint8_t *) event, sizeof(event));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rfcomm_emit_credits(rfcomm_channel_t * channel, uint8_t credits) {
|
|
|
|
uint8_t event[5];
|
|
|
|
event[0] = RFCOMM_EVENT_CREDITS;
|
|
|
|
event[1] = sizeof(event) - 2;
|
|
|
|
bt_store_16(event, 2, channel->rfcomm_cid);
|
|
|
|
event[4] = credits;
|
|
|
|
hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event));
|
|
|
|
(*app_packet_handler)(channel->connection, HCI_EVENT_PACKET, 0, (uint8_t *) event, sizeof(event));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rfcomm_emit_service_registered(void *connection, uint8_t status, uint8_t channel){
|
|
|
|
uint8_t event[4];
|
|
|
|
event[0] = RFCOMM_EVENT_SERVICE_REGISTERED;
|
|
|
|
event[1] = sizeof(event) - 2;
|
|
|
|
event[2] = status;
|
|
|
|
event[3] = channel;
|
|
|
|
hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
|
|
|
|
(*app_packet_handler)(connection, HCI_EVENT_PACKET, 0, (uint8_t *) event, sizeof(event));
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: RFCOMM MULTIPLEXER HELPER
|
|
|
|
|
2011-05-01 11:36:12 +00:00
|
|
|
static void rfcomm_multiplexer_initialize(rfcomm_multiplexer_t *multiplexer){
|
|
|
|
bzero(multiplexer, sizeof(rfcomm_multiplexer_t));
|
|
|
|
multiplexer->state = RFCOMM_MULTIPLEXER_CLOSED;
|
|
|
|
multiplexer->l2cap_credits = 0;
|
2011-07-07 23:06:51 +00:00
|
|
|
multiplexer->send_dm_for_dlci = 0;
|
|
|
|
|
2011-06-12 19:37:31 +00:00
|
|
|
// - Max RFCOMM header has 6 bytes (P/F bit is set, payload length >= 128)
|
|
|
|
// - therefore, we set RFCOMM max frame size <= Local L2CAP MTU - 6
|
2011-07-07 22:50:58 +00:00
|
|
|
multiplexer->max_frame_size = RFCOMM_MAX_PAYLOAD - 6; // max
|
2011-05-01 11:36:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static rfcomm_multiplexer_t * rfcomm_multiplexer_create_for_addr(bd_addr_t *addr){
|
|
|
|
|
|
|
|
// alloc structure
|
|
|
|
rfcomm_multiplexer_t * multiplexer = malloc(sizeof(rfcomm_multiplexer_t));
|
|
|
|
if (!multiplexer) return NULL;
|
|
|
|
|
|
|
|
// fill in
|
|
|
|
rfcomm_multiplexer_initialize(multiplexer);
|
|
|
|
BD_ADDR_COPY(&multiplexer->remote_addr, addr);
|
|
|
|
|
|
|
|
// add to services list
|
|
|
|
linked_list_add(&rfcomm_multiplexers, (linked_item_t *) multiplexer);
|
|
|
|
|
|
|
|
return multiplexer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static rfcomm_multiplexer_t * rfcomm_multiplexer_for_addr(bd_addr_t *addr){
|
|
|
|
linked_item_t *it;
|
|
|
|
for (it = (linked_item_t *) rfcomm_multiplexers; it ; it = it->next){
|
|
|
|
rfcomm_multiplexer_t * multiplexer = ((rfcomm_multiplexer_t *) it);
|
|
|
|
if (BD_ADDR_CMP(addr, multiplexer->remote_addr) == 0) {
|
|
|
|
return multiplexer;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static rfcomm_multiplexer_t * rfcomm_multiplexer_for_l2cap_cid(uint16_t l2cap_cid) {
|
|
|
|
linked_item_t *it;
|
|
|
|
for (it = (linked_item_t *) rfcomm_multiplexers; it ; it = it->next){
|
|
|
|
rfcomm_multiplexer_t * multiplexer = ((rfcomm_multiplexer_t *) it);
|
|
|
|
if (multiplexer->l2cap_cid == l2cap_cid) {
|
|
|
|
return multiplexer;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rfcomm_multiplexer_has_channels(rfcomm_multiplexer_t * multiplexer){
|
|
|
|
linked_item_t *it;
|
|
|
|
for (it = (linked_item_t *) rfcomm_channels; it ; it = it->next){
|
|
|
|
rfcomm_channel_t * channel = ((rfcomm_channel_t *) it);
|
|
|
|
if (channel->multiplexer == multiplexer) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-07-04 20:54:35 +00:00
|
|
|
// MARK: RFCOMM CHANNEL HELPER
|
|
|
|
|
2011-06-06 14:12:38 +00:00
|
|
|
static void rfcomm_dump_channels(void){
|
2011-07-12 15:38:49 +00:00
|
|
|
#ifndef EMBEDDED
|
2011-05-01 11:36:12 +00:00
|
|
|
linked_item_t * it;
|
|
|
|
int channels = 0;
|
|
|
|
for (it = (linked_item_t *) rfcomm_channels; it ; it = it->next){
|
|
|
|
rfcomm_channel_t * channel = (rfcomm_channel_t *) it;
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("Channel #%u: addr %p, state %u\n", channels, channel, channel->state);
|
2011-07-12 15:38:49 +00:00
|
|
|
channels++;
|
|
|
|
}
|
|
|
|
#endif
|
2011-05-01 11:36:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void rfcomm_channel_initialize(rfcomm_channel_t *channel, rfcomm_multiplexer_t *multiplexer,
|
2011-05-03 21:14:41 +00:00
|
|
|
rfcomm_service_t *service, uint8_t server_channel){
|
2011-05-01 11:36:12 +00:00
|
|
|
|
|
|
|
// don't use 0 as channel id
|
|
|
|
if (rfcomm_client_cid_generator == 0) ++rfcomm_client_cid_generator;
|
|
|
|
|
|
|
|
// setup channel
|
|
|
|
bzero(channel, sizeof(rfcomm_channel_t));
|
2011-07-05 08:30:43 +00:00
|
|
|
|
2011-07-05 09:33:05 +00:00
|
|
|
channel->state = RFCOMM_CHANNEL_CLOSED;
|
2011-07-22 10:09:45 +00:00
|
|
|
channel->state_var = STATE_VAR_NONE;
|
2011-07-05 08:30:43 +00:00
|
|
|
|
2011-05-01 11:36:12 +00:00
|
|
|
channel->multiplexer = multiplexer;
|
|
|
|
channel->service = service;
|
|
|
|
channel->rfcomm_cid = rfcomm_client_cid_generator++;
|
2011-06-12 12:33:31 +00:00
|
|
|
channel->max_frame_size = multiplexer->max_frame_size;
|
|
|
|
|
2011-05-01 11:36:12 +00:00
|
|
|
channel->credits_incoming = 0;
|
|
|
|
channel->credits_outgoing = 0;
|
|
|
|
channel->packets_granted = 0;
|
2011-07-05 08:30:43 +00:00
|
|
|
|
2011-05-01 11:36:12 +00:00
|
|
|
if (service) {
|
|
|
|
// incoming connection
|
|
|
|
channel->outgoing = 0;
|
|
|
|
channel->dlci = (server_channel << 1) | multiplexer->outgoing;
|
|
|
|
} else {
|
|
|
|
// outgoing connection
|
|
|
|
channel->outgoing = 1;
|
|
|
|
channel->dlci = (server_channel << 1) | (multiplexer->outgoing ^ 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// service == NULL -> outgoing channel
|
|
|
|
static rfcomm_channel_t * rfcomm_channel_create(rfcomm_multiplexer_t * multiplexer,
|
2011-05-03 21:14:41 +00:00
|
|
|
rfcomm_service_t * service, uint8_t server_channel){
|
2011-05-01 11:36:12 +00:00
|
|
|
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("rfcomm_channel_create for service %p, channel %u --- begin\n", service, server_channel);
|
2011-05-01 11:36:12 +00:00
|
|
|
rfcomm_dump_channels();
|
|
|
|
|
|
|
|
// alloc structure
|
|
|
|
rfcomm_channel_t * channel = malloc(sizeof(rfcomm_channel_t));
|
|
|
|
if (!channel) return NULL;
|
|
|
|
|
|
|
|
// fill in
|
|
|
|
rfcomm_channel_initialize(channel, multiplexer, service, server_channel);
|
|
|
|
|
|
|
|
// add to services list
|
|
|
|
linked_list_add(&rfcomm_channels, (linked_item_t *) channel);
|
|
|
|
|
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
|
|
|
|
static rfcomm_channel_t * rfcomm_channel_for_rfcomm_cid(uint16_t rfcomm_cid){
|
|
|
|
linked_item_t *it;
|
|
|
|
for (it = (linked_item_t *) rfcomm_channels; it ; it = it->next){
|
|
|
|
rfcomm_channel_t * channel = ((rfcomm_channel_t *) it);
|
|
|
|
if (channel->rfcomm_cid == rfcomm_cid) {
|
|
|
|
return channel;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static rfcomm_channel_t * rfcomm_channel_for_multiplexer_and_dlci(rfcomm_multiplexer_t * multiplexer, uint8_t dlci){
|
|
|
|
linked_item_t *it;
|
|
|
|
for (it = (linked_item_t *) rfcomm_channels; it ; it = it->next){
|
|
|
|
rfcomm_channel_t * channel = ((rfcomm_channel_t *) it);
|
|
|
|
if (channel->dlci == dlci && channel->multiplexer == multiplexer) {
|
|
|
|
return channel;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-05-03 21:14:41 +00:00
|
|
|
static rfcomm_service_t * rfcomm_service_for_channel(uint8_t server_channel){
|
2011-05-01 11:36:12 +00:00
|
|
|
linked_item_t *it;
|
|
|
|
for (it = (linked_item_t *) rfcomm_services; it ; it = it->next){
|
|
|
|
rfcomm_service_t * service = ((rfcomm_service_t *) it);
|
|
|
|
if ( service->server_channel == server_channel){
|
|
|
|
return service;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-07-04 20:54:35 +00:00
|
|
|
// MARK: RFCOMM SEND
|
|
|
|
|
2011-05-01 11:36:12 +00:00
|
|
|
/**
|
|
|
|
* @param credits - only used for RFCOMM flow control in UIH wiht P/F = 1
|
|
|
|
*/
|
2011-06-18 21:17:58 +00:00
|
|
|
static int rfcomm_send_packet_for_multiplexer(rfcomm_multiplexer_t *multiplexer, uint8_t address, uint8_t control, uint8_t credits, uint8_t *data, uint16_t len){
|
2011-05-01 11:36:12 +00:00
|
|
|
|
|
|
|
uint16_t pos = 0;
|
|
|
|
uint8_t crc_fields = 3;
|
|
|
|
|
|
|
|
rfcomm_out_buffer[pos++] = address;
|
|
|
|
rfcomm_out_buffer[pos++] = control;
|
|
|
|
|
|
|
|
// length field can be 1 or 2 octets
|
|
|
|
if (len < 128){
|
|
|
|
rfcomm_out_buffer[pos++] = (len << 1)| 1; // bits 0-6
|
|
|
|
} else {
|
|
|
|
rfcomm_out_buffer[pos++] = (len & 0x7f) << 1; // bits 0-6
|
|
|
|
rfcomm_out_buffer[pos++] = len >> 7; // bits 7-14
|
|
|
|
crc_fields++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add credits for UIH frames when PF bit is set
|
|
|
|
if (control == BT_RFCOMM_UIH_PF){
|
|
|
|
rfcomm_out_buffer[pos++] = credits;
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy actual data
|
|
|
|
if (len) {
|
|
|
|
memcpy(&rfcomm_out_buffer[pos], data, len);
|
|
|
|
pos += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
// UIH frames only calc FCS over address + control (5.1.1)
|
|
|
|
if ((control & 0xef) == BT_RFCOMM_UIH){
|
|
|
|
crc_fields = 2;
|
|
|
|
}
|
|
|
|
rfcomm_out_buffer[pos++] = crc8_calc(rfcomm_out_buffer, crc_fields); // calc fcs
|
2011-06-18 21:17:58 +00:00
|
|
|
|
|
|
|
int credits_taken = 0;
|
|
|
|
if (multiplexer->l2cap_credits){
|
|
|
|
credits_taken++;
|
|
|
|
multiplexer->l2cap_credits--;
|
|
|
|
} else {
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info( "rfcomm_send_packet addr %02x, ctrl %02x size %u without l2cap credits\n", address, control, pos);
|
2011-06-18 21:17:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int err = l2cap_send_internal(multiplexer->l2cap_cid, rfcomm_out_buffer, pos);
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
// undo credit counting
|
|
|
|
multiplexer->l2cap_credits += credits_taken;
|
|
|
|
}
|
|
|
|
return err;
|
2011-05-01 11:36:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// C/R Flag in Address
|
2011-07-20 15:20:06 +00:00
|
|
|
// - terms: initiator = station that creates multiplexer with SABM
|
|
|
|
// - terms: responder = station that responds to multiplexer setup with UA
|
2011-05-01 11:36:12 +00:00
|
|
|
// "For SABM, UA, DM and DISC frames C/R bit is set according to Table 1 in GSM 07.10, section 5.2.1.2"
|
2011-07-20 15:20:06 +00:00
|
|
|
// - command initiator = 1 /response responder = 1
|
|
|
|
// - command responder = 0 /response initiator = 0
|
2011-05-01 11:36:12 +00:00
|
|
|
// "For UIH frames, the C/R bit is always set according to section 5.4.3.1 in GSM 07.10.
|
|
|
|
// This applies independently of what is contained wthin the UIH frames, either data or control messages."
|
|
|
|
// - c/r = 1 for frames by initiating station, 0 = for frames by responding station
|
|
|
|
|
|
|
|
// C/R Flag in Message
|
|
|
|
// "In the message level, the C/R bit in the command type field is set as stated in section 5.4.6.2 in GSM 07.10."
|
|
|
|
// - If the C/R bit is set to 1 the message is a command
|
|
|
|
// - if it is set to 0 the message is a response.
|
|
|
|
|
|
|
|
// temp/old messge construction
|
|
|
|
|
|
|
|
// new object oriented version
|
2011-06-18 21:17:58 +00:00
|
|
|
static int rfcomm_send_sabm(rfcomm_multiplexer_t *multiplexer, uint8_t dlci){
|
2011-05-01 11:36:12 +00:00
|
|
|
uint8_t address = (1 << 0) | (multiplexer->outgoing << 1) | (dlci << 2); // command
|
2011-06-18 21:17:58 +00:00
|
|
|
return rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_SABM, 0, NULL, 0);
|
2011-05-01 11:36:12 +00:00
|
|
|
}
|
|
|
|
|
2011-06-18 21:17:58 +00:00
|
|
|
static int rfcomm_send_disc(rfcomm_multiplexer_t *multiplexer, uint8_t dlci){
|
2011-07-20 15:20:06 +00:00
|
|
|
uint8_t address = (1 << 0) | (multiplexer->outgoing << 1) | (dlci << 2); // command
|
2011-06-18 21:17:58 +00:00
|
|
|
return rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_DISC, 0, NULL, 0);
|
2011-05-01 11:36:12 +00:00
|
|
|
}
|
|
|
|
|
2011-06-18 21:17:58 +00:00
|
|
|
static int rfcomm_send_ua(rfcomm_multiplexer_t *multiplexer, uint8_t dlci){
|
2011-05-01 11:36:12 +00:00
|
|
|
uint8_t address = (1 << 0) | ((multiplexer->outgoing ^ 1) << 1) | (dlci << 2); // response
|
2011-06-18 21:17:58 +00:00
|
|
|
return rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_UA, 0, NULL, 0);
|
2011-05-01 11:36:12 +00:00
|
|
|
}
|
|
|
|
|
2011-06-18 21:17:58 +00:00
|
|
|
static int rfcomm_send_dm_pf(rfcomm_multiplexer_t *multiplexer, uint8_t dlci){
|
2011-05-01 11:36:12 +00:00
|
|
|
uint8_t address = (1 << 0) | ((multiplexer->outgoing ^ 1) << 1) | (dlci << 2); // response
|
2011-06-18 21:17:58 +00:00
|
|
|
return rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_DM_PF, 0, NULL, 0);
|
2011-05-01 11:36:12 +00:00
|
|
|
}
|
|
|
|
|
2011-06-18 21:17:58 +00:00
|
|
|
static int rfcomm_send_uih_msc_cmd(rfcomm_multiplexer_t *multiplexer, uint8_t dlci, uint8_t signals) {
|
2011-05-01 11:36:12 +00:00
|
|
|
uint8_t address = (1 << 0) | (multiplexer->outgoing << 1);
|
|
|
|
uint8_t payload[4];
|
|
|
|
uint8_t pos = 0;
|
|
|
|
payload[pos++] = BT_RFCOMM_MSC_CMD;
|
|
|
|
payload[pos++] = 2 << 1 | 1; // len
|
|
|
|
payload[pos++] = (1 << 0) | (1 << 1) | (dlci << 2); // CMD => C/R = 1
|
|
|
|
payload[pos++] = signals;
|
2011-06-18 21:17:58 +00:00
|
|
|
return rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_UIH, 0, (uint8_t *) payload, pos);
|
2011-05-01 11:36:12 +00:00
|
|
|
}
|
|
|
|
|
2011-06-24 19:44:56 +00:00
|
|
|
static int rfcomm_send_uih_msc_rsp(rfcomm_multiplexer_t *multiplexer, uint8_t dlci, uint8_t signals) {
|
2011-07-20 15:20:06 +00:00
|
|
|
uint8_t address = (1 << 0) | (multiplexer->outgoing<< 1);
|
2011-06-24 19:44:56 +00:00
|
|
|
uint8_t payload[4];
|
|
|
|
uint8_t pos = 0;
|
|
|
|
payload[pos++] = BT_RFCOMM_MSC_RSP;
|
|
|
|
payload[pos++] = 2 << 1 | 1; // len
|
|
|
|
payload[pos++] = (1 << 0) | (1 << 1) | (dlci << 2); // CMD => C/R = 1
|
|
|
|
payload[pos++] = signals;
|
|
|
|
return rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_UIH, 0, (uint8_t *) payload, pos);
|
|
|
|
}
|
|
|
|
|
2011-06-18 21:17:58 +00:00
|
|
|
static int rfcomm_send_uih_pn_command(rfcomm_multiplexer_t *multiplexer, uint8_t dlci, uint16_t max_frame_size){
|
2011-05-01 11:36:12 +00:00
|
|
|
uint8_t payload[10];
|
|
|
|
uint8_t address = (1 << 0) | (multiplexer->outgoing << 1);
|
|
|
|
uint8_t pos = 0;
|
|
|
|
payload[pos++] = BT_RFCOMM_PN_CMD;
|
|
|
|
payload[pos++] = 8 << 1 | 1; // len
|
|
|
|
payload[pos++] = dlci;
|
|
|
|
payload[pos++] = 0xf0; // pre-defined for Bluetooth, see 5.5.3 of TS 07.10 Adaption for RFCOMM
|
|
|
|
payload[pos++] = 0; // priority
|
|
|
|
payload[pos++] = 0; // max 60 seconds ack
|
|
|
|
payload[pos++] = max_frame_size & 0xff; // max framesize low
|
|
|
|
payload[pos++] = max_frame_size >> 8; // max framesize high
|
|
|
|
payload[pos++] = 0x00; // number of retransmissions
|
|
|
|
payload[pos++] = 0x00; // (unused error recovery window) initial number of credits
|
2011-06-18 21:17:58 +00:00
|
|
|
return rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_UIH, 0, (uint8_t *) payload, pos);
|
2011-05-01 11:36:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// "The response may not change the DLCI, the priority, the convergence layer, or the timer value." RFCOMM-tutorial.pdf
|
2011-06-18 21:17:58 +00:00
|
|
|
static int rfcomm_send_uih_pn_response(rfcomm_multiplexer_t *multiplexer, uint8_t dlci,
|
2011-07-20 15:20:06 +00:00
|
|
|
uint8_t priority, uint16_t max_frame_size){
|
2011-05-01 11:36:12 +00:00
|
|
|
uint8_t payload[10];
|
|
|
|
uint8_t address = (1 << 0) | (multiplexer->outgoing << 1);
|
|
|
|
uint8_t pos = 0;
|
|
|
|
payload[pos++] = BT_RFCOMM_PN_RSP;
|
|
|
|
payload[pos++] = 8 << 1 | 1; // len
|
|
|
|
payload[pos++] = dlci;
|
|
|
|
payload[pos++] = 0xe0; // pre defined for Bluetooth, see 5.5.3 of TS 07.10 Adaption for RFCOMM
|
|
|
|
payload[pos++] = priority; // priority
|
|
|
|
payload[pos++] = 0; // max 60 seconds ack
|
|
|
|
payload[pos++] = max_frame_size & 0xff; // max framesize low
|
|
|
|
payload[pos++] = max_frame_size >> 8; // max framesize high
|
|
|
|
payload[pos++] = 0x00; // number of retransmissions
|
|
|
|
payload[pos++] = 0x00; // (unused error recovery window) initial number of credits
|
2011-06-18 21:17:58 +00:00
|
|
|
return rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_UIH, 0, (uint8_t *) payload, pos);
|
2011-05-01 11:36:12 +00:00
|
|
|
}
|
|
|
|
|
2011-07-06 10:52:28 +00:00
|
|
|
static int rfcomm_send_uih_rpn_rsp(rfcomm_multiplexer_t *multiplexer, uint8_t dlci, rfcomm_rpn_data_t *rpn_data) {
|
|
|
|
uint8_t payload[10];
|
|
|
|
uint8_t address = (1 << 0) | (multiplexer->outgoing << 1);
|
|
|
|
uint8_t pos = 0;
|
|
|
|
payload[pos++] = BT_RFCOMM_RPN_RSP;
|
|
|
|
payload[pos++] = 8 << 1 | 1; // len
|
2011-07-20 20:30:57 +00:00
|
|
|
payload[pos++] = (1 << 0) | (1 << 1) | (dlci << 2); // CMD => C/R = 1
|
2011-07-06 10:52:28 +00:00
|
|
|
payload[pos++] = rpn_data->baud_rate;
|
|
|
|
payload[pos++] = rpn_data->flags;
|
|
|
|
payload[pos++] = rpn_data->flow_control;
|
|
|
|
payload[pos++] = rpn_data->xon;
|
|
|
|
payload[pos++] = rpn_data->xoff;
|
|
|
|
payload[pos++] = rpn_data->parameter_mask_0;
|
|
|
|
payload[pos++] = rpn_data->parameter_mask_1;
|
|
|
|
return rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_UIH, 0, (uint8_t *) payload, pos);
|
|
|
|
}
|
|
|
|
|
2011-07-20 15:20:06 +00:00
|
|
|
static int rfcomm_send_uih_data(rfcomm_multiplexer_t *multiplexer, uint8_t dlci, uint8_t *data, uint16_t len){
|
|
|
|
uint8_t address = (1 << 0) | (multiplexer->outgoing << 1) | (dlci << 2);
|
|
|
|
return rfcomm_send_packet_for_multiplexer(multiplexer, address, BT_RFCOMM_UIH, 0, data, len);
|
|
|
|
}
|
|
|
|
|
2011-07-06 13:45:50 +00:00
|
|
|
// MARK: RFCOMM MULTIPLEXER
|
2011-05-01 11:36:12 +00:00
|
|
|
|
|
|
|
static void rfcomm_multiplexer_finalize(rfcomm_multiplexer_t * multiplexer){
|
|
|
|
|
|
|
|
// remove (potential) timer
|
|
|
|
if (multiplexer->timer_active) {
|
|
|
|
run_loop_remove_timer(&multiplexer->timer);
|
|
|
|
multiplexer->timer_active = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// close and remove all channels
|
|
|
|
linked_item_t *it = (linked_item_t *) &rfcomm_channels;
|
|
|
|
while (it->next){
|
|
|
|
rfcomm_channel_t * channel = (rfcomm_channel_t *) it->next;
|
|
|
|
if (channel->multiplexer == multiplexer) {
|
|
|
|
// emit appropriate events
|
|
|
|
if (channel->state == RFCOMM_CHANNEL_OPEN) {
|
|
|
|
rfcomm_emit_channel_closed(channel);
|
|
|
|
} else {
|
|
|
|
rfcomm_emit_channel_opened(channel, RFCOMM_MULTIPLEXER_STOPPED);
|
|
|
|
}
|
|
|
|
// remove from list
|
|
|
|
it->next = it->next->next;
|
|
|
|
// free channel struct
|
|
|
|
free(channel);
|
|
|
|
} else {
|
|
|
|
it = it->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// keep reference to l2cap channel
|
|
|
|
uint16_t l2cap_cid = multiplexer->l2cap_cid;
|
|
|
|
|
|
|
|
// remove mutliplexer
|
|
|
|
linked_list_remove( &rfcomm_multiplexers, (linked_item_t *) multiplexer);
|
|
|
|
free(multiplexer);
|
|
|
|
|
|
|
|
// close l2cap multiplexer channel, too
|
|
|
|
l2cap_disconnect_internal(l2cap_cid, 0x13);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rfcomm_multiplexer_timer_handler(timer_source_t *timer){
|
|
|
|
rfcomm_multiplexer_t * multiplexer = (rfcomm_multiplexer_t *) linked_item_get_user( (linked_item_t *) timer);
|
|
|
|
if (!rfcomm_multiplexer_has_channels(multiplexer)){
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info( "rfcomm_multiplexer_timer_handler timeout: shutting down multiplexer!\n");
|
2011-05-01 11:36:12 +00:00
|
|
|
rfcomm_multiplexer_finalize(multiplexer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rfcomm_multiplexer_prepare_idle_timer(rfcomm_multiplexer_t * multiplexer){
|
|
|
|
if (multiplexer->timer_active) {
|
|
|
|
run_loop_remove_timer(&multiplexer->timer);
|
|
|
|
multiplexer->timer_active = 0;
|
|
|
|
}
|
|
|
|
if (!rfcomm_multiplexer_has_channels(multiplexer)){
|
|
|
|
// start timer for multiplexer timeout check
|
|
|
|
run_loop_set_timer(&multiplexer->timer, RFCOMM_MULIPLEXER_TIMEOUT_MS);
|
|
|
|
multiplexer->timer.process = rfcomm_multiplexer_timer_handler;
|
|
|
|
linked_item_set_user((linked_item_t*) &multiplexer->timer, multiplexer);
|
|
|
|
run_loop_add_timer(&multiplexer->timer);
|
|
|
|
multiplexer->timer_active = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-04 21:49:59 +00:00
|
|
|
static void rfcomm_multiplexer_opened(rfcomm_multiplexer_t *multiplexer){
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("Multiplexer up and running\n");
|
2011-07-04 21:49:59 +00:00
|
|
|
multiplexer->state = RFCOMM_MULTIPLEXER_OPEN;
|
|
|
|
|
2011-07-06 14:00:01 +00:00
|
|
|
rfcomm_channel_event_t event;
|
|
|
|
event.type = CH_EVT_MULTIPLEXER_READY;
|
|
|
|
|
2011-07-04 21:49:59 +00:00
|
|
|
// transition of channels that wait for multiplexer
|
|
|
|
linked_item_t *it;
|
|
|
|
for (it = (linked_item_t *) rfcomm_channels; it ; it = it->next){
|
|
|
|
rfcomm_channel_t * channel = ((rfcomm_channel_t *) it);
|
|
|
|
if (channel->multiplexer != multiplexer) continue;
|
2011-07-06 14:00:01 +00:00
|
|
|
rfcomm_channel_state_machine(channel, &event);
|
2011-07-04 21:49:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rfcomm_run();
|
|
|
|
rfcomm_multiplexer_prepare_idle_timer(multiplexer);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-01 11:36:12 +00:00
|
|
|
/**
|
|
|
|
* @return handled packet
|
|
|
|
*/
|
|
|
|
static int rfcomm_multiplexer_hci_event_handler(uint8_t *packet, uint16_t size){
|
|
|
|
bd_addr_t event_addr;
|
|
|
|
uint16_t psm;
|
|
|
|
uint16_t l2cap_cid;
|
|
|
|
hci_con_handle_t con_handle;
|
|
|
|
rfcomm_multiplexer_t *multiplexer = NULL;
|
|
|
|
switch (packet[0]) {
|
|
|
|
|
|
|
|
// accept incoming PSM_RFCOMM connection if no multiplexer exists yet
|
|
|
|
case L2CAP_EVENT_INCOMING_CONNECTION:
|
|
|
|
// data: event(8), len(8), address(48), handle (16), psm (16), source cid(16) dest cid(16)
|
|
|
|
bt_flip_addr(event_addr, &packet[2]);
|
|
|
|
psm = READ_BT_16(packet, 10);
|
|
|
|
if (psm != PSM_RFCOMM) break;
|
|
|
|
l2cap_cid = READ_BT_16(packet, 12);
|
|
|
|
multiplexer = rfcomm_multiplexer_for_addr(&event_addr);
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("L2CAP_EVENT_INCOMING_CONNECTION (l2cap_cid 0x%02x) for PSM_RFCOMM from ", l2cap_cid);
|
2011-05-01 11:36:12 +00:00
|
|
|
|
|
|
|
if (multiplexer) {
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info(" => decline\n");
|
2011-05-01 11:36:12 +00:00
|
|
|
// bt_send_cmd(&l2cap_decline_connection, l2cap_cid);
|
|
|
|
l2cap_decline_connection_internal(l2cap_cid, 0x13);
|
|
|
|
} else {
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info(" => accept\n");
|
2011-05-01 11:36:12 +00:00
|
|
|
// bt_send_cmd(&l2cap_accept_connection, l2cap_cid);
|
|
|
|
l2cap_accept_connection_internal(l2cap_cid);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// l2cap connection opened -> store l2cap_cid, remote_addr
|
|
|
|
case L2CAP_EVENT_CHANNEL_OPENED:
|
|
|
|
if (READ_BT_16(packet, 11) != PSM_RFCOMM) break;
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("L2CAP_EVENT_CHANNEL_OPENED for PSM_RFCOMM\n");
|
2011-05-01 11:36:12 +00:00
|
|
|
// get multiplexer for remote addr
|
|
|
|
con_handle = READ_BT_16(packet, 9);
|
|
|
|
l2cap_cid = READ_BT_16(packet, 13);
|
|
|
|
bt_flip_addr(event_addr, &packet[3]);
|
|
|
|
multiplexer = rfcomm_multiplexer_for_addr(&event_addr);
|
|
|
|
if (multiplexer) {
|
|
|
|
if (multiplexer->state == RFCOMM_MULTIPLEXER_W4_CONNECT) {
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("L2CAP_EVENT_CHANNEL_OPENED: outgoing connection A\n");
|
2011-05-01 11:36:12 +00:00
|
|
|
// wrong remote addr
|
|
|
|
if (BD_ADDR_CMP(event_addr, multiplexer->remote_addr)) break;
|
|
|
|
multiplexer->l2cap_cid = l2cap_cid;
|
|
|
|
multiplexer->con_handle = con_handle;
|
|
|
|
// send SABM #0
|
2011-06-24 21:11:56 +00:00
|
|
|
multiplexer->state = RFCOMM_MULTIPLEXER_SEND_SABM_0;
|
2011-05-01 11:36:12 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("L2CAP_EVENT_CHANNEL_OPENED: multiplexer already exists\n");
|
2011-05-01 11:36:12 +00:00
|
|
|
// single multiplexer per baseband connection
|
|
|
|
break;
|
|
|
|
}
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("L2CAP_EVENT_CHANNEL_OPENED: create incoming multiplexer for channel %02x\n", l2cap_cid);
|
2011-05-01 11:36:12 +00:00
|
|
|
// create and inititialize new multiplexer instance (incoming)
|
2011-06-12 12:33:31 +00:00
|
|
|
// - Max RFCOMM header has 6 bytes (P/F bit is set, payload length >= 128)
|
|
|
|
// - therefore, we set RFCOMM max frame size <= Local L2CAP MTU - 6
|
2011-05-01 11:36:12 +00:00
|
|
|
multiplexer = rfcomm_multiplexer_create_for_addr(&event_addr);
|
|
|
|
multiplexer->con_handle = con_handle;
|
|
|
|
multiplexer->l2cap_cid = l2cap_cid;
|
|
|
|
multiplexer->state = RFCOMM_MULTIPLEXER_W4_SABM_0;
|
2011-06-12 12:33:31 +00:00
|
|
|
multiplexer->max_frame_size = READ_BT_16(packet, 17) - 6;
|
2011-05-01 11:36:12 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
// l2cap disconnect -> state = RFCOMM_MULTIPLEXER_CLOSED;
|
|
|
|
|
|
|
|
case L2CAP_EVENT_CREDITS:
|
|
|
|
// data: event(8), len(8), local_cid(16), credits(8)
|
|
|
|
l2cap_cid = READ_BT_16(packet, 2);
|
|
|
|
multiplexer = rfcomm_multiplexer_for_l2cap_cid(l2cap_cid);
|
|
|
|
if (!multiplexer) break;
|
|
|
|
multiplexer->l2cap_credits += packet[4];
|
2011-06-23 20:32:12 +00:00
|
|
|
|
2011-07-22 18:31:02 +00:00
|
|
|
// log_info("L2CAP_EVENT_CREDITS: %u (now %u)\n", packet[4], multiplexer->l2cap_credits);
|
2011-07-16 15:19:37 +00:00
|
|
|
|
2011-06-23 20:32:12 +00:00
|
|
|
// new credits, continue with signaling
|
|
|
|
rfcomm_run();
|
|
|
|
|
2011-06-18 21:17:58 +00:00
|
|
|
if (multiplexer->state != RFCOMM_MULTIPLEXER_OPEN) break;
|
2011-05-01 11:36:12 +00:00
|
|
|
rfcomm_hand_out_credits();
|
|
|
|
break;
|
2011-07-16 15:19:37 +00:00
|
|
|
|
|
|
|
case DAEMON_EVENT_HCI_PACKET_SENT:
|
|
|
|
// testing DMA done code
|
|
|
|
rfcomm_run();
|
|
|
|
break;
|
2011-05-01 11:36:12 +00:00
|
|
|
|
|
|
|
case L2CAP_EVENT_CHANNEL_CLOSED:
|
|
|
|
// data: event (8), len(8), channel (16)
|
|
|
|
l2cap_cid = READ_BT_16(packet, 2);
|
|
|
|
multiplexer = rfcomm_multiplexer_for_l2cap_cid(l2cap_cid);
|
|
|
|
if (!multiplexer) break;
|
|
|
|
switch (multiplexer->state) {
|
|
|
|
case RFCOMM_MULTIPLEXER_W4_SABM_0:
|
|
|
|
case RFCOMM_MULTIPLEXER_W4_UA_0:
|
|
|
|
case RFCOMM_MULTIPLEXER_OPEN:
|
|
|
|
rfcomm_multiplexer_finalize(multiplexer);
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rfcomm_multiplexer_l2cap_packet_handler(uint16_t channel, uint8_t *packet, uint16_t size){
|
2011-07-06 13:45:50 +00:00
|
|
|
|
2011-07-05 09:33:05 +00:00
|
|
|
// get or create a multiplexer for a certain device
|
|
|
|
rfcomm_multiplexer_t *multiplexer = rfcomm_multiplexer_for_l2cap_cid(channel);
|
2011-05-01 11:36:12 +00:00
|
|
|
if (!multiplexer) return 0;
|
2011-07-06 13:45:50 +00:00
|
|
|
|
2011-05-01 11:36:12 +00:00
|
|
|
// but only care for multiplexer control channel
|
|
|
|
uint8_t frame_dlci = packet[0] >> 2;
|
|
|
|
if (frame_dlci) return 0;
|
|
|
|
const uint8_t length_offset = (packet[2] & 1) ^ 1; // to be used for pos >= 3
|
|
|
|
const uint8_t credit_offset = ((packet[1] & BT_RFCOMM_UIH_PF) == BT_RFCOMM_UIH_PF) ? 1 : 0; // credits for uih_pf frames
|
|
|
|
const uint8_t payload_offset = 3 + length_offset + credit_offset;
|
|
|
|
switch (packet[1]){
|
|
|
|
|
|
|
|
case BT_RFCOMM_SABM:
|
|
|
|
if (multiplexer->state == RFCOMM_MULTIPLEXER_W4_SABM_0){
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("Received SABM #0\n");
|
2011-05-01 11:36:12 +00:00
|
|
|
multiplexer->outgoing = 0;
|
2011-06-24 21:11:56 +00:00
|
|
|
multiplexer->state = RFCOMM_MULTIPLEXER_SEND_UA_0;
|
2011-05-01 11:36:12 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BT_RFCOMM_UA:
|
|
|
|
if (multiplexer->state == RFCOMM_MULTIPLEXER_W4_UA_0) {
|
|
|
|
// UA #0 -> send UA #0, state = RFCOMM_MULTIPLEXER_OPEN
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("Received UA #0 \n");
|
2011-07-04 21:49:59 +00:00
|
|
|
rfcomm_multiplexer_opened(multiplexer);
|
2011-05-01 11:36:12 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BT_RFCOMM_DISC:
|
|
|
|
// DISC #0 -> send UA #0, close multiplexer
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("Received DISC #0, (ougoing = %u)\n", multiplexer->outgoing);
|
2011-06-24 21:11:56 +00:00
|
|
|
multiplexer->state = RFCOMM_MULTIPLEXER_SEND_UA_0_AND_DISC;
|
2011-05-01 11:36:12 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
case BT_RFCOMM_DM:
|
|
|
|
// DM #0 - we shouldn't get this, just give up
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("Received DM #0\n");
|
|
|
|
log_info("-> Closing down multiplexer\n");
|
2011-05-01 11:36:12 +00:00
|
|
|
rfcomm_multiplexer_finalize(multiplexer);
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case BT_RFCOMM_UIH:
|
|
|
|
if (packet[payload_offset] == BT_RFCOMM_CLD_CMD){
|
|
|
|
// Multiplexer close down (CLD) -> close mutliplexer
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("Received Multiplexer close down command\n");
|
|
|
|
log_info("-> Closing down multiplexer\n");
|
2011-05-01 11:36:12 +00:00
|
|
|
rfcomm_multiplexer_finalize(multiplexer);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-07-07 22:50:58 +00:00
|
|
|
static void rfcomm_multiplexer_state_machine(rfcomm_multiplexer_t * multiplexer, RFCOMM_MULTIPLEXER_EVENT event){
|
2011-07-07 23:06:51 +00:00
|
|
|
|
|
|
|
// process stored DM responses
|
|
|
|
if (multiplexer->send_dm_for_dlci){
|
|
|
|
rfcomm_send_dm_pf(multiplexer, multiplexer->send_dm_for_dlci);
|
|
|
|
multiplexer->send_dm_for_dlci = 0;
|
|
|
|
}
|
|
|
|
|
2011-07-07 22:50:58 +00:00
|
|
|
switch (multiplexer->state) {
|
|
|
|
case RFCOMM_MULTIPLEXER_SEND_SABM_0:
|
|
|
|
switch (event) {
|
|
|
|
case MULT_EV_READY_TO_SEND:
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("Sending SABM #0 - (multi 0x%08x)\n", (int) multiplexer);
|
2011-07-07 22:50:58 +00:00
|
|
|
multiplexer->state = RFCOMM_MULTIPLEXER_W4_UA_0;
|
|
|
|
rfcomm_send_sabm(multiplexer, 0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case RFCOMM_MULTIPLEXER_SEND_UA_0:
|
|
|
|
switch (event) {
|
|
|
|
case MULT_EV_READY_TO_SEND:
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("Sending UA #0\n");
|
2011-07-07 22:50:58 +00:00
|
|
|
multiplexer->state = RFCOMM_MULTIPLEXER_OPEN;
|
|
|
|
rfcomm_send_ua(multiplexer, 0);
|
|
|
|
rfcomm_multiplexer_opened(multiplexer);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2011-07-07 23:24:07 +00:00
|
|
|
break;
|
2011-07-07 22:50:58 +00:00
|
|
|
case RFCOMM_MULTIPLEXER_SEND_UA_0_AND_DISC:
|
|
|
|
switch (event) {
|
|
|
|
case MULT_EV_READY_TO_SEND:
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("Sending UA #0\n");
|
|
|
|
log_info("Closing down multiplexer\n");
|
2011-07-07 22:50:58 +00:00
|
|
|
multiplexer->state = RFCOMM_MULTIPLEXER_CLOSED;
|
|
|
|
rfcomm_send_ua(multiplexer, 0);
|
|
|
|
rfcomm_multiplexer_finalize(multiplexer);
|
|
|
|
// try to detect authentication errors: drop link key if multiplexer closed before first channel got opened
|
|
|
|
if (!multiplexer->at_least_one_connection){
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("TODO: no connections established - delete link key prophylactically\n");
|
2011-07-15 19:48:07 +00:00
|
|
|
// hci_send_cmd(&hci_delete_stored_link_key, multiplexer->remote_addr);
|
2011-07-07 22:50:58 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2011-07-07 23:24:07 +00:00
|
|
|
break;
|
2011-07-07 22:50:58 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-06 13:45:50 +00:00
|
|
|
// MARK: RFCOMM CHANNEL
|
|
|
|
|
|
|
|
static void rfcomm_hand_out_credits(void){
|
|
|
|
linked_item_t * it;
|
|
|
|
for (it = (linked_item_t *) rfcomm_channels; it ; it = it->next){
|
|
|
|
rfcomm_channel_t * channel = (rfcomm_channel_t *) it;
|
|
|
|
if (channel->state != RFCOMM_CHANNEL_OPEN) {
|
2011-07-22 18:31:02 +00:00
|
|
|
// log_info("RFCOMM_EVENT_CREDITS: multiplexer not open\n");
|
2011-07-06 13:45:50 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (channel->packets_granted) {
|
2011-07-22 18:31:02 +00:00
|
|
|
// log_info("RFCOMM_EVENT_CREDITS: already packets granted\n");
|
2011-07-06 13:45:50 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!channel->credits_outgoing) {
|
2011-07-22 18:31:02 +00:00
|
|
|
// log_info("RFCOMM_EVENT_CREDITS: no outgoing credits\n");
|
2011-07-06 13:45:50 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!channel->multiplexer->l2cap_credits){
|
2011-07-22 18:31:02 +00:00
|
|
|
// log_info("RFCOMM_EVENT_CREDITS: no l2cap credits\n");
|
2011-07-06 13:45:50 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// channel open, multiplexer has l2cap credits and we didn't hand out credit before -> go!
|
2011-07-22 18:31:02 +00:00
|
|
|
// log_info("RFCOMM_EVENT_CREDITS: 1\n");
|
2011-07-06 13:45:50 +00:00
|
|
|
channel->packets_granted += 1;
|
|
|
|
rfcomm_emit_credits(channel, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void rfcomm_channel_provide_credits(rfcomm_channel_t *channel){
|
|
|
|
|
|
|
|
if (!l2cap_can_send_packet_now(channel->multiplexer->l2cap_cid)) return;
|
|
|
|
|
|
|
|
int credits = 0x30;
|
|
|
|
switch (channel->state) {
|
2011-07-07 15:07:27 +00:00
|
|
|
case RFCOMM_CHANNEL_DLC_SETUP:
|
2011-07-06 13:45:50 +00:00
|
|
|
case RFCOMM_CHANNEL_OPEN:
|
|
|
|
if (channel->credits_incoming < 5){
|
|
|
|
uint8_t address = (1 << 0) | (channel->multiplexer->outgoing << 1) | (channel->dlci << 2);
|
|
|
|
rfcomm_send_packet_for_multiplexer(channel->multiplexer, address, BT_RFCOMM_UIH_PF, credits, NULL, 0);
|
|
|
|
channel->credits_incoming += credits;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rfcomm_channel_opened(rfcomm_channel_t *rfChannel){
|
2011-07-15 20:50:14 +00:00
|
|
|
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("rfcomm_channel_opened!\n");
|
2011-07-15 20:50:14 +00:00
|
|
|
|
2011-07-06 13:45:50 +00:00
|
|
|
rfChannel->state = RFCOMM_CHANNEL_OPEN;
|
|
|
|
rfcomm_emit_channel_opened(rfChannel, 0);
|
|
|
|
rfcomm_hand_out_credits();
|
|
|
|
|
|
|
|
// remove (potential) timer
|
|
|
|
rfcomm_multiplexer_t *multiplexer = rfChannel->multiplexer;
|
|
|
|
if (multiplexer->timer_active) {
|
|
|
|
run_loop_remove_timer(&multiplexer->timer);
|
|
|
|
multiplexer->timer_active = 0;
|
|
|
|
}
|
|
|
|
// hack for problem detecting authentication failure
|
|
|
|
multiplexer->at_least_one_connection = 1;
|
|
|
|
|
|
|
|
// start next connection request if pending
|
|
|
|
rfcomm_run();
|
|
|
|
}
|
|
|
|
|
2011-05-01 11:36:12 +00:00
|
|
|
static void rfcomm_channel_packet_handler_uih(rfcomm_multiplexer_t *multiplexer, uint8_t * packet, uint16_t size){
|
|
|
|
const uint8_t frame_dlci = packet[0] >> 2;
|
|
|
|
const uint8_t length_offset = (packet[2] & 1) ^ 1; // to be used for pos >= 3
|
|
|
|
const uint8_t credit_offset = ((packet[1] & BT_RFCOMM_UIH_PF) == BT_RFCOMM_UIH_PF) ? 1 : 0; // credits for uih_pf frames
|
|
|
|
const uint8_t payload_offset = 3 + length_offset + credit_offset;
|
|
|
|
|
|
|
|
rfcomm_channel_t * rfChannel = rfcomm_channel_for_multiplexer_and_dlci(multiplexer, frame_dlci);
|
2011-06-17 20:42:18 +00:00
|
|
|
if (!rfChannel) return;
|
|
|
|
|
|
|
|
if (packet[1] == BT_RFCOMM_UIH_PF) {
|
|
|
|
// handle new credits
|
|
|
|
uint16_t new_credits = packet[3+length_offset];
|
|
|
|
rfChannel->credits_outgoing += new_credits;
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info( "RFCOMM data UIH_PF, new credits: %u, now %u\n", new_credits, rfChannel->credits_outgoing);
|
2011-06-18 21:17:58 +00:00
|
|
|
|
|
|
|
// notify daemon -> might trigger re-try of parked connections
|
|
|
|
uint8_t event[1];
|
|
|
|
event[0] = DAEMON_EVENT_NEW_RFCOMM_CREDITS;
|
|
|
|
(*app_packet_handler)(rfChannel->connection, DAEMON_EVENT_PACKET, rfChannel->rfcomm_cid, event, sizeof(event));
|
|
|
|
|
2011-07-07 15:07:27 +00:00
|
|
|
if (rfChannel->state == RFCOMM_CHANNEL_DLC_SETUP && rfcomm_channel_ready_for_open(rfChannel)) {
|
|
|
|
rfChannel->state = RFCOMM_CHANNEL_OPEN;
|
2011-06-17 20:42:18 +00:00
|
|
|
rfcomm_channel_opened(rfChannel);
|
2011-05-01 11:36:12 +00:00
|
|
|
}
|
|
|
|
}
|
2011-06-17 20:42:18 +00:00
|
|
|
if (rfChannel->credits_incoming > 0){
|
|
|
|
rfChannel->credits_incoming--;
|
|
|
|
}
|
|
|
|
rfcomm_channel_provide_credits(rfChannel);
|
|
|
|
|
|
|
|
if (size - 1 > payload_offset){ // don't send empty frames, -1 for header checksum at end
|
2011-07-22 18:31:02 +00:00
|
|
|
// log_info( "RFCOMM data UIH_PF, size %u, channel %x\n", size-payload_offset-1, (int) rfChannel->connection);
|
2011-06-17 20:42:18 +00:00
|
|
|
(*app_packet_handler)(rfChannel->connection, RFCOMM_DATA_PACKET, rfChannel->rfcomm_cid,
|
|
|
|
&packet[payload_offset], size-payload_offset-1);
|
|
|
|
}
|
|
|
|
// we received new RFCOMM credits, hand them out if possible
|
|
|
|
rfcomm_hand_out_credits();
|
2011-05-01 11:36:12 +00:00
|
|
|
}
|
|
|
|
|
2011-07-05 16:29:22 +00:00
|
|
|
static void rfcomm_channel_accept_pn(rfcomm_channel_t *channel, rfcomm_channel_event_pn_t *event){
|
2011-07-05 09:33:05 +00:00
|
|
|
// priority of client request
|
2011-07-05 16:29:22 +00:00
|
|
|
channel->pn_priority = event->priority;
|
|
|
|
|
2011-07-05 09:33:05 +00:00
|
|
|
// new credits
|
2011-07-05 16:29:22 +00:00
|
|
|
channel->credits_outgoing = event->credits_outgoing;
|
|
|
|
|
2011-07-05 09:33:05 +00:00
|
|
|
// negotiate max frame size
|
|
|
|
if (channel->max_frame_size > channel->multiplexer->max_frame_size) {
|
|
|
|
channel->max_frame_size = channel->multiplexer->max_frame_size;
|
|
|
|
}
|
2011-07-05 16:29:22 +00:00
|
|
|
if (channel->max_frame_size > event->max_frame_size) {
|
|
|
|
channel->max_frame_size = event->max_frame_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-07-07 16:42:40 +00:00
|
|
|
static void rfcomm_channel_finalize(rfcomm_channel_t *channel){
|
|
|
|
|
|
|
|
rfcomm_multiplexer_t *multiplexer = channel->multiplexer;
|
|
|
|
|
|
|
|
// remove from list
|
|
|
|
linked_list_remove( &rfcomm_channels, (linked_item_t *) channel);
|
|
|
|
|
|
|
|
// free channel
|
|
|
|
free(channel);
|
|
|
|
|
|
|
|
// update multiplexer timeout after channel was removed from list
|
|
|
|
rfcomm_multiplexer_prepare_idle_timer(multiplexer);
|
|
|
|
}
|
|
|
|
|
2011-07-06 11:25:51 +00:00
|
|
|
static void rfcomm_channel_state_machine_2(rfcomm_multiplexer_t * multiplexer, uint8_t dlci, rfcomm_channel_event_t *event){
|
|
|
|
|
2011-07-06 13:45:50 +00:00
|
|
|
// TODO: if client max frame size is smaller than RFCOMM_DEFAULT_SIZE, send PN
|
|
|
|
|
2011-07-07 19:31:11 +00:00
|
|
|
|
2011-07-06 13:45:50 +00:00
|
|
|
// lookup existing channel
|
|
|
|
rfcomm_channel_t * channel = rfcomm_channel_for_multiplexer_and_dlci(multiplexer, dlci);
|
2011-07-07 19:31:11 +00:00
|
|
|
|
2011-07-22 18:31:02 +00:00
|
|
|
// log_info("rfcomm_channel_state_machine_2 lookup dlci #%u = 0x%08x - event %u\n", dlci, (int) channel, event->type);
|
2011-07-07 19:31:11 +00:00
|
|
|
|
2011-07-06 13:45:50 +00:00
|
|
|
if (channel) {
|
|
|
|
rfcomm_channel_state_machine(channel, event);
|
|
|
|
return;
|
|
|
|
}
|
2011-07-06 11:25:51 +00:00
|
|
|
|
2011-07-06 13:45:50 +00:00
|
|
|
// service registered?
|
|
|
|
rfcomm_service_t * service = rfcomm_service_for_channel(dlci >> 1);
|
2011-07-22 18:31:02 +00:00
|
|
|
// log_info("rfcomm_channel_state_machine_2 service dlci #%u = 0x%08x\n", dlci, (int) service);
|
2011-07-06 13:45:50 +00:00
|
|
|
if (!service) {
|
|
|
|
// discard request by sending disconnected mode
|
2011-07-07 23:06:51 +00:00
|
|
|
multiplexer->send_dm_for_dlci = dlci;
|
2011-07-07 19:31:11 +00:00
|
|
|
return;
|
2011-07-06 13:45:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// create channel for some events
|
2011-07-06 11:25:51 +00:00
|
|
|
switch (event->type) {
|
2011-07-06 11:30:40 +00:00
|
|
|
case CH_EVT_RCVD_SABM:
|
2011-07-06 13:45:50 +00:00
|
|
|
case CH_EVT_RCVD_PN:
|
|
|
|
case CH_EVT_RCVD_RPN_REQ:
|
|
|
|
case CH_EVT_RCVD_RPN_CMD:
|
|
|
|
// setup incoming channel
|
|
|
|
channel = rfcomm_channel_create(multiplexer, service, dlci >> 1);
|
2011-07-06 11:30:40 +00:00
|
|
|
break;
|
2011-07-06 11:25:51 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2011-07-06 13:45:50 +00:00
|
|
|
|
|
|
|
if (!channel) {
|
|
|
|
// discard request by sending disconnected mode
|
2011-07-07 23:06:51 +00:00
|
|
|
multiplexer->send_dm_for_dlci = dlci;
|
2011-07-06 13:45:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
channel->connection = service->connection;
|
|
|
|
rfcomm_channel_state_machine(channel, event);
|
2011-07-06 11:25:51 +00:00
|
|
|
}
|
|
|
|
|
2011-07-06 11:09:43 +00:00
|
|
|
void rfcomm_channel_packet_handler(rfcomm_multiplexer_t * multiplexer, uint8_t *packet, uint16_t size){
|
2011-05-01 11:36:12 +00:00
|
|
|
|
|
|
|
// rfcomm: (0) addr [76543 server channel] [2 direction: initiator uses 1] [1 C/R: CMD by initiator = 1] [0 EA=1]
|
|
|
|
const uint8_t frame_dlci = packet[0] >> 2;
|
|
|
|
uint8_t message_dlci; // used by commands in UIH(_PF) packets
|
|
|
|
uint8_t message_len; // "
|
2011-07-06 10:52:28 +00:00
|
|
|
|
2011-05-01 11:36:12 +00:00
|
|
|
// rfcomm: (1) command/control
|
|
|
|
// -- credits_offset = 1 if command == BT_RFCOMM_UIH_PF
|
|
|
|
const uint8_t credit_offset = ((packet[1] & BT_RFCOMM_UIH_PF) == BT_RFCOMM_UIH_PF) ? 1 : 0; // credits for uih_pf frames
|
|
|
|
// rfcomm: (2) length. if bit 0 is cleared, 2 byte length is used. (little endian)
|
|
|
|
const uint8_t length_offset = (packet[2] & 1) ^ 1; // to be used for pos >= 3
|
|
|
|
// rfcomm: (3+length_offset) credits if credits_offset == 1
|
|
|
|
// rfcomm: (3+length_offest+credits_offset)
|
|
|
|
const uint8_t payload_offset = 3 + length_offset + credit_offset;
|
|
|
|
|
2011-07-05 16:58:18 +00:00
|
|
|
rfcomm_channel_event_t event;
|
2011-07-06 13:45:50 +00:00
|
|
|
rfcomm_channel_event_pn_t event_pn;
|
|
|
|
rfcomm_channel_event_rpn_t event_rpn;
|
|
|
|
|
2011-05-01 11:36:12 +00:00
|
|
|
// switch by rfcomm message type
|
|
|
|
switch(packet[1]) {
|
|
|
|
|
|
|
|
case BT_RFCOMM_SABM:
|
2011-07-06 11:25:51 +00:00
|
|
|
event.type = CH_EVT_RCVD_SABM;
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("Received SABM #%u\n", frame_dlci);
|
2011-07-06 11:25:51 +00:00
|
|
|
rfcomm_channel_state_machine_2(multiplexer, frame_dlci, &event);
|
2011-05-01 11:36:12 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BT_RFCOMM_UA:
|
2011-07-05 16:58:18 +00:00
|
|
|
event.type = CH_EVT_RCVD_UA;
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("Received UA #%u - channel opened\n",frame_dlci);
|
2011-07-06 11:30:40 +00:00
|
|
|
rfcomm_channel_state_machine_2(multiplexer, frame_dlci, &event);
|
2011-05-01 11:36:12 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BT_RFCOMM_DISC:
|
2011-07-05 16:58:18 +00:00
|
|
|
event.type = CH_EVT_RCVD_DISC;
|
2011-07-06 11:30:40 +00:00
|
|
|
rfcomm_channel_state_machine_2(multiplexer, frame_dlci, &event);
|
2011-05-01 11:36:12 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BT_RFCOMM_DM:
|
|
|
|
case BT_RFCOMM_DM_PF:
|
2011-07-06 11:30:40 +00:00
|
|
|
event.type = CH_EVT_RCVD_DM;
|
|
|
|
rfcomm_channel_state_machine_2(multiplexer, frame_dlci, &event);
|
2011-05-01 11:36:12 +00:00
|
|
|
break;
|
2011-07-05 16:58:18 +00:00
|
|
|
|
2011-05-01 11:36:12 +00:00
|
|
|
case BT_RFCOMM_UIH_PF:
|
|
|
|
case BT_RFCOMM_UIH:
|
2011-07-06 11:12:54 +00:00
|
|
|
|
|
|
|
message_len = packet[payload_offset+1] >> 1;
|
|
|
|
|
2011-05-01 11:36:12 +00:00
|
|
|
switch (packet[payload_offset]) {
|
|
|
|
case BT_RFCOMM_PN_CMD:
|
2011-07-07 19:31:11 +00:00
|
|
|
message_dlci = packet[payload_offset+2];
|
2011-07-06 13:45:50 +00:00
|
|
|
event_pn.super.type = CH_EVT_RCVD_PN;
|
|
|
|
event_pn.priority = packet[payload_offset+4];
|
|
|
|
event_pn.max_frame_size = READ_BT_16(packet, payload_offset+6);
|
|
|
|
event_pn.credits_outgoing = packet[payload_offset+9];
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("Received UIH Parameter Negotiation Command for #%u\n", message_dlci);
|
2011-07-07 19:31:11 +00:00
|
|
|
rfcomm_channel_state_machine_2(multiplexer, message_dlci, (rfcomm_channel_event_t*) &event_pn);
|
2011-05-01 11:36:12 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BT_RFCOMM_PN_RSP:
|
2011-07-07 19:31:11 +00:00
|
|
|
message_dlci = packet[payload_offset+2];
|
2011-07-06 10:52:28 +00:00
|
|
|
event_pn.super.type = CH_EVT_RCVD_PN_RSP;
|
|
|
|
event_pn.priority = packet[payload_offset+4];
|
|
|
|
event_pn.max_frame_size = READ_BT_16(packet, payload_offset+6);
|
|
|
|
event_pn.credits_outgoing = packet[payload_offset+9];
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("UIH Parameter Negotiation Response max frame %u, credits %u\n",
|
2011-07-06 10:52:28 +00:00
|
|
|
event_pn.max_frame_size, event_pn.credits_outgoing);
|
2011-07-06 13:45:50 +00:00
|
|
|
rfcomm_channel_state_machine_2(multiplexer, message_dlci, (rfcomm_channel_event_t*) &event_pn);
|
2011-05-01 11:36:12 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BT_RFCOMM_MSC_CMD:
|
2011-07-07 19:31:11 +00:00
|
|
|
message_dlci = packet[payload_offset+2] >> 2;
|
2011-07-06 10:52:28 +00:00
|
|
|
event.type = CH_EVT_RCVD_MSC_CMD;
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("Received MSC CMD for #%u, \n", message_dlci);
|
2011-07-06 13:45:50 +00:00
|
|
|
rfcomm_channel_state_machine_2(multiplexer, message_dlci, &event);
|
2011-05-01 11:36:12 +00:00
|
|
|
break;
|
2011-07-06 11:09:43 +00:00
|
|
|
|
2011-05-01 11:36:12 +00:00
|
|
|
case BT_RFCOMM_MSC_RSP:
|
2011-07-07 19:31:11 +00:00
|
|
|
message_dlci = packet[payload_offset+2] >> 2;
|
2011-07-06 10:52:28 +00:00
|
|
|
event.type = CH_EVT_RCVD_MSC_RSP;
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("Received MSC RSP for #%u\n", message_dlci);
|
2011-07-06 13:45:50 +00:00
|
|
|
rfcomm_channel_state_machine_2(multiplexer, message_dlci, &event);
|
2011-05-01 11:36:12 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BT_RFCOMM_RPN_CMD:
|
2011-07-07 19:31:11 +00:00
|
|
|
message_dlci = packet[payload_offset+2] >> 2;
|
2011-07-06 13:45:50 +00:00
|
|
|
switch (message_len){
|
|
|
|
case 1:
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("Received Remote Port Negotiation for #%u\n", message_dlci);
|
2011-07-06 13:45:50 +00:00
|
|
|
event.type = CH_EVT_RCVD_RPN_REQ;
|
|
|
|
rfcomm_channel_state_machine_2(multiplexer, message_dlci, &event);
|
|
|
|
break;
|
|
|
|
case 8:
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("Received Remote Port Negotiation (Info) for #%u\n", message_dlci);
|
2011-07-06 13:45:50 +00:00
|
|
|
event_rpn.super.type = CH_EVT_RCVD_RPN_CMD;
|
|
|
|
event_rpn.data.baud_rate = packet[payload_offset+3];
|
|
|
|
event_rpn.data.flags = packet[payload_offset+4];
|
|
|
|
event_rpn.data.flow_control = packet[payload_offset+5];
|
|
|
|
event_rpn.data.xon = packet[payload_offset+6];
|
|
|
|
event_rpn.data.xoff = packet[payload_offset+7];
|
|
|
|
event_rpn.data.parameter_mask_0 = packet[payload_offset+8];
|
|
|
|
event_rpn.data.parameter_mask_1 = packet[payload_offset+9];
|
2011-07-20 14:55:27 +00:00
|
|
|
rfcomm_channel_state_machine_2(multiplexer, message_dlci, (rfcomm_channel_event_t*) &event_rpn);
|
2011-07-06 13:45:50 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2011-07-06 10:52:28 +00:00
|
|
|
}
|
2011-05-01 11:36:12 +00:00
|
|
|
break;
|
2011-07-06 11:09:43 +00:00
|
|
|
|
2011-05-01 11:36:12 +00:00
|
|
|
default:
|
2011-07-22 18:34:18 +00:00
|
|
|
log_error("Received unknown UIH packet - 0x%02x\n", packet[payload_offset]);
|
2011-05-01 11:36:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2011-07-22 18:34:18 +00:00
|
|
|
log_error("Received unknown RFCOMM message type %x\n", packet[1]);
|
2011-05-01 11:36:12 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-07-07 19:31:11 +00:00
|
|
|
|
|
|
|
// trigger next action - example W4_PN_RSP: transition to SEND_SABM which only depends on "can send"
|
|
|
|
rfcomm_run();
|
2011-05-01 11:36:12 +00:00
|
|
|
}
|
|
|
|
|
2011-07-06 11:09:43 +00:00
|
|
|
void rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
|
|
|
|
|
|
|
|
// multiplexer handler
|
|
|
|
int handled = 0;
|
|
|
|
switch (packet_type) {
|
|
|
|
case HCI_EVENT_PACKET:
|
|
|
|
handled = rfcomm_multiplexer_hci_event_handler(packet, size);
|
|
|
|
break;
|
|
|
|
case L2CAP_DATA_PACKET:
|
|
|
|
handled = rfcomm_multiplexer_l2cap_packet_handler(channel, packet, size);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2011-07-07 19:31:11 +00:00
|
|
|
|
|
|
|
if (handled) {
|
|
|
|
rfcomm_run();
|
|
|
|
return;
|
|
|
|
}
|
2011-07-06 11:09:43 +00:00
|
|
|
|
|
|
|
// we only handle l2cap packet over open multiplexer channel now
|
|
|
|
if (packet_type != L2CAP_DATA_PACKET) {
|
|
|
|
(*app_packet_handler)(NULL, packet_type, channel, packet, size);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
rfcomm_multiplexer_t * multiplexer = rfcomm_multiplexer_for_l2cap_cid(channel);
|
|
|
|
if (!multiplexer || multiplexer->state != RFCOMM_MULTIPLEXER_OPEN) {
|
|
|
|
(*app_packet_handler)(NULL, packet_type, channel, packet, size);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// channel data ?
|
|
|
|
// rfcomm: (0) addr [76543 server channel] [2 direction: initiator uses 1] [1 C/R: CMD by initiator = 1] [0 EA=1]
|
|
|
|
const uint8_t frame_dlci = packet[0] >> 2;
|
|
|
|
|
|
|
|
if (frame_dlci && (packet[1] == BT_RFCOMM_UIH || packet[1] == BT_RFCOMM_UIH_PF)) {
|
|
|
|
rfcomm_channel_packet_handler_uih(multiplexer, packet, size);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rfcomm_channel_packet_handler(multiplexer, packet, size);
|
|
|
|
}
|
|
|
|
|
2011-07-07 22:50:58 +00:00
|
|
|
static int rfcomm_channel_ready_for_open(rfcomm_channel_t *channel){
|
2011-07-22 18:31:02 +00:00
|
|
|
// log_info("rfcomm_channel_ready_for_open state %u, flags needed %04x, current %04x, rf credits %u, l2cap credits %u \n", channel->state, STATE_VAR_RCVD_MSC_RSP|STATE_VAR_SENT_MSC_RSP|STATE_VAR_SENT_CREDITS, channel->state_var, channel->credits_outgoing, channel->multiplexer->l2cap_credits);
|
2011-07-07 22:50:58 +00:00
|
|
|
if ((channel->state_var & STATE_VAR_RCVD_MSC_RSP) == 0) return 0;
|
|
|
|
if ((channel->state_var & STATE_VAR_SENT_MSC_RSP) == 0) return 0;
|
|
|
|
if ((channel->state_var & STATE_VAR_SENT_CREDITS) == 0) return 0;
|
|
|
|
if (channel->credits_outgoing == 0) return 0;
|
|
|
|
|
|
|
|
return 1;
|
2011-06-12 12:37:38 +00:00
|
|
|
}
|
2011-05-01 11:36:12 +00:00
|
|
|
|
2011-07-06 14:00:01 +00:00
|
|
|
static void rfcomm_channel_state_machine(rfcomm_channel_t *channel, rfcomm_channel_event_t *event){
|
|
|
|
|
2011-07-22 18:31:02 +00:00
|
|
|
// log_info("rfcomm_channel_state_machine: state %u, state_var %04x, event %u\n", channel->state, channel->state_var ,event->type);
|
2011-07-07 19:31:11 +00:00
|
|
|
|
2011-07-06 14:00:01 +00:00
|
|
|
rfcomm_multiplexer_t *multiplexer = channel->multiplexer;
|
|
|
|
|
|
|
|
// TODO: integrate in common switch
|
|
|
|
if (event->type == CH_EVT_RCVD_DISC){
|
2011-07-20 20:54:22 +00:00
|
|
|
rfcomm_emit_channel_closed(channel);
|
2011-07-07 16:42:40 +00:00
|
|
|
channel->state = RFCOMM_CHANNEL_SEND_UA_AFTER_DISC;
|
2011-07-06 14:00:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-19 14:59:07 +00:00
|
|
|
// TODO: integrate in common switch
|
2011-07-06 14:00:01 +00:00
|
|
|
if (event->type == CH_EVT_RCVD_DM){
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("Received DM message for #%u\n", channel->dlci);
|
|
|
|
log_info("-> Closing channel locally for #%u\n", channel->dlci);
|
2011-07-06 14:00:01 +00:00
|
|
|
rfcomm_emit_channel_closed(channel);
|
2011-07-07 16:42:40 +00:00
|
|
|
rfcomm_channel_finalize(channel);
|
2011-07-06 14:00:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// remote port negotiation command - just accept everything for now
|
|
|
|
//
|
|
|
|
// "The RPN command can be used before a new DLC is opened and should be used whenever the port settings change."
|
|
|
|
// "The RPN command is specified as optional in TS 07.10, but it is mandatory to recognize and respond to it in RFCOMM.
|
|
|
|
// (Although the handling of individual settings are implementation-dependent.)"
|
|
|
|
//
|
|
|
|
|
2011-07-14 15:42:13 +00:00
|
|
|
// TODO: integrate in common switch
|
2011-07-06 14:00:01 +00:00
|
|
|
if (event->type == CH_EVT_RCVD_RPN_CMD){
|
|
|
|
// control port parameters
|
2011-07-14 15:42:13 +00:00
|
|
|
rfcomm_channel_event_rpn_t *event_rpn = (rfcomm_channel_event_rpn_t*) event;
|
2011-07-07 23:01:10 +00:00
|
|
|
memcpy(&channel->rpn_data, &event_rpn->data, sizeof(rfcomm_rpn_data_t));
|
|
|
|
channel->state_var |= STATE_VAR_SEND_RPN_RSP;
|
2011-07-06 14:00:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-14 15:42:13 +00:00
|
|
|
// TODO: integrate in common switch
|
2011-07-06 14:00:01 +00:00
|
|
|
if (event->type == CH_EVT_RCVD_RPN_REQ){
|
|
|
|
// default rpn rsp
|
|
|
|
rfcomm_rpn_data_t rpn_data;
|
|
|
|
rpn_data.baud_rate = 0xa0; /* 9600 bps */
|
|
|
|
rpn_data.flags = 0x03; /* 8-n-1 */
|
|
|
|
rpn_data.flow_control = 0; /* no flow control */
|
|
|
|
rpn_data.xon = 0xd1; /* XON */
|
|
|
|
rpn_data.xoff = 0xd3; /* XOFF */
|
|
|
|
rpn_data.parameter_mask_0 = 0x7f; /* parameter mask, all values set */
|
2011-07-12 15:39:46 +00:00
|
|
|
rpn_data.parameter_mask_1 = 0x3f; /* parameter mask, all values set */
|
2011-07-07 23:01:10 +00:00
|
|
|
memcpy(&channel->rpn_data, &rpn_data, sizeof(rfcomm_rpn_data_t));
|
|
|
|
channel->state_var |= STATE_VAR_SEND_RPN_RSP;
|
2011-07-06 14:00:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-07 23:01:10 +00:00
|
|
|
// TODO: integrate in common swich
|
|
|
|
if (event->type == CH_EVT_READY_TO_SEND){
|
|
|
|
if (channel->state_var & STATE_VAR_SEND_RPN_RSP){
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("Sending Remote Port Negotiation RSP for #%u\n", channel->dlci);
|
2011-07-07 23:01:10 +00:00
|
|
|
channel->state_var &= ~STATE_VAR_SEND_RPN_RSP;
|
|
|
|
rfcomm_send_uih_rpn_rsp(multiplexer, channel->dlci, &channel->rpn_data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-06 14:00:01 +00:00
|
|
|
rfcomm_channel_event_pn_t * event_pn = (rfcomm_channel_event_pn_t*) event;
|
|
|
|
|
|
|
|
switch (channel->state) {
|
|
|
|
case RFCOMM_CHANNEL_CLOSED:
|
|
|
|
switch (event->type){
|
|
|
|
case CH_EVT_RCVD_SABM:
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("-> Inform app\n");
|
2011-07-06 14:00:01 +00:00
|
|
|
channel->state_var |= STATE_VAR_RCVD_SABM;
|
|
|
|
channel->state = RFCOMM_CHANNEL_INCOMING_SETUP;
|
2011-07-14 15:42:13 +00:00
|
|
|
rfcomm_emit_connection_request(channel);
|
2011-07-06 14:00:01 +00:00
|
|
|
break;
|
|
|
|
case CH_EVT_RCVD_PN:
|
|
|
|
rfcomm_channel_accept_pn(channel, event_pn);
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("-> Inform app\n");
|
2011-07-06 14:00:01 +00:00
|
|
|
channel->state_var |= STATE_VAR_RCVD_PN;
|
|
|
|
channel->state = RFCOMM_CHANNEL_INCOMING_SETUP;
|
2011-07-14 15:42:13 +00:00
|
|
|
rfcomm_emit_connection_request(channel);
|
2011-07-06 14:00:01 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RFCOMM_CHANNEL_INCOMING_SETUP:
|
|
|
|
switch (event->type){
|
|
|
|
case CH_EVT_RCVD_SABM:
|
2011-07-07 19:31:11 +00:00
|
|
|
channel->state_var |= STATE_VAR_RCVD_SABM;
|
2011-07-06 14:00:01 +00:00
|
|
|
if (channel->state_var & STATE_VAR_CLIENT_ACCEPTED) {
|
|
|
|
channel->state_var |= STATE_VAR_SEND_UA;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CH_EVT_RCVD_PN:
|
|
|
|
rfcomm_channel_accept_pn(channel, event_pn);
|
|
|
|
channel->state_var |= STATE_VAR_RCVD_PN;
|
2011-07-07 19:31:11 +00:00
|
|
|
if (channel->state_var & STATE_VAR_CLIENT_ACCEPTED) {
|
|
|
|
channel->state_var |= STATE_VAR_SEND_PN_RSP;
|
|
|
|
}
|
2011-07-06 14:00:01 +00:00
|
|
|
break;
|
2011-07-07 16:24:30 +00:00
|
|
|
case CH_EVT_READY_TO_SEND:
|
|
|
|
if (channel->state_var & STATE_VAR_SEND_PN_RSP){
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("Sending UIH Parameter Negotiation Respond for #%u\n", channel->dlci);
|
2011-07-07 16:24:30 +00:00
|
|
|
channel->state_var &= ~STATE_VAR_SEND_PN_RSP;
|
2011-07-07 19:31:11 +00:00
|
|
|
rfcomm_send_uih_pn_response(multiplexer, channel->dlci, channel->pn_priority, channel->max_frame_size);
|
2011-07-07 16:24:30 +00:00
|
|
|
}
|
|
|
|
else if (channel->state_var & STATE_VAR_SEND_UA){
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("Sending UA #%u\n", channel->dlci);
|
2011-07-07 16:24:30 +00:00
|
|
|
channel->state_var &= ~STATE_VAR_SEND_UA;
|
2011-07-07 19:31:11 +00:00
|
|
|
rfcomm_send_ua(multiplexer, channel->dlci);
|
2011-07-07 16:24:30 +00:00
|
|
|
}
|
|
|
|
if ((channel->state_var & STATE_VAR_CLIENT_ACCEPTED) && (channel->state_var & STATE_VAR_RCVD_SABM)) {
|
|
|
|
channel->state_var |= STATE_VAR_SEND_MSC_CMD;
|
2011-07-07 19:31:11 +00:00
|
|
|
channel->state_var |= STATE_VAR_SEND_CREDITS;
|
2011-07-07 16:24:30 +00:00
|
|
|
channel->state = RFCOMM_CHANNEL_DLC_SETUP;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2011-07-06 14:00:01 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RFCOMM_CHANNEL_W4_MULTIPLEXER:
|
|
|
|
switch (event->type) {
|
|
|
|
case CH_EVT_MULTIPLEXER_READY:
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("Muliplexer opened, sending UIH PN next\n");
|
2011-07-06 14:00:01 +00:00
|
|
|
channel->state = RFCOMM_CHANNEL_SEND_UIH_PN;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2011-07-07 19:31:11 +00:00
|
|
|
break;
|
2011-07-06 14:00:01 +00:00
|
|
|
|
2011-07-07 16:24:30 +00:00
|
|
|
case RFCOMM_CHANNEL_SEND_UIH_PN:
|
|
|
|
switch (event->type) {
|
|
|
|
case CH_EVT_READY_TO_SEND:
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("Sending UIH Parameter Negotiation Command for #%u (channel 0x%08x\n", channel->dlci, (int) channel );
|
2011-07-07 16:24:30 +00:00
|
|
|
channel->state = RFCOMM_CHANNEL_W4_PN_RSP;
|
2011-07-07 19:31:11 +00:00
|
|
|
rfcomm_send_uih_pn_command(multiplexer, channel->dlci, channel->max_frame_size);
|
2011-07-07 16:24:30 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2011-07-06 14:00:01 +00:00
|
|
|
case RFCOMM_CHANNEL_W4_PN_RSP:
|
|
|
|
switch (event->type){
|
|
|
|
case CH_EVT_RCVD_PN_RSP:
|
|
|
|
// update max frame size
|
|
|
|
if (channel->max_frame_size > event_pn->max_frame_size) {
|
|
|
|
channel->max_frame_size = event_pn->max_frame_size;
|
|
|
|
}
|
|
|
|
// new credits
|
|
|
|
channel->credits_outgoing = event_pn->credits_outgoing;
|
|
|
|
channel->state = RFCOMM_CHANNEL_SEND_SABM_W4_UA;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2011-07-07 19:31:11 +00:00
|
|
|
|
|
|
|
case RFCOMM_CHANNEL_SEND_SABM_W4_UA:
|
|
|
|
switch (event->type) {
|
|
|
|
case CH_EVT_READY_TO_SEND:
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("Sending SABM #%u\n", channel->dlci);
|
2011-07-07 19:31:11 +00:00
|
|
|
channel->state = RFCOMM_CHANNEL_W4_UA;
|
|
|
|
rfcomm_send_sabm(multiplexer, channel->dlci);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2011-07-06 14:00:01 +00:00
|
|
|
|
2011-07-07 15:07:27 +00:00
|
|
|
case RFCOMM_CHANNEL_W4_UA:
|
2011-07-06 14:00:01 +00:00
|
|
|
switch (event->type){
|
2011-07-07 15:07:27 +00:00
|
|
|
case CH_EVT_RCVD_UA:
|
|
|
|
channel->state = RFCOMM_CHANNEL_DLC_SETUP;
|
|
|
|
channel->state_var |= STATE_VAR_SEND_MSC_CMD;
|
2011-07-07 19:31:11 +00:00
|
|
|
channel->state_var |= STATE_VAR_SEND_CREDITS;
|
2011-07-06 14:00:01 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2011-07-07 15:07:27 +00:00
|
|
|
case RFCOMM_CHANNEL_DLC_SETUP:
|
2011-07-06 14:00:01 +00:00
|
|
|
switch (event->type){
|
|
|
|
case CH_EVT_RCVD_MSC_CMD:
|
2011-07-07 15:07:27 +00:00
|
|
|
channel->state_var |= STATE_VAR_RCVD_MSC_CMD;
|
|
|
|
channel->state_var |= STATE_VAR_SEND_MSC_RSP;
|
2011-07-06 14:00:01 +00:00
|
|
|
break;
|
|
|
|
case CH_EVT_RCVD_MSC_RSP:
|
2011-07-07 15:07:27 +00:00
|
|
|
channel->state_var |= STATE_VAR_RCVD_MSC_RSP;
|
2011-07-06 14:00:01 +00:00
|
|
|
break;
|
2011-07-15 20:42:57 +00:00
|
|
|
|
2011-07-07 16:24:30 +00:00
|
|
|
case CH_EVT_READY_TO_SEND:
|
|
|
|
if (channel->state_var & STATE_VAR_SEND_MSC_CMD){
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("Sending MSC CMD for #%u\n", channel->dlci);
|
2011-07-07 16:24:30 +00:00
|
|
|
channel->state_var &= ~STATE_VAR_SEND_MSC_CMD;
|
|
|
|
channel->state_var |= STATE_VAR_SENT_MSC_CMD;
|
2011-07-07 19:31:11 +00:00
|
|
|
rfcomm_send_uih_msc_cmd(multiplexer, channel->dlci , 0x8d); // ea=1,fc=0,rtc=1,rtr=1,ic=0,dv=1
|
2011-07-15 20:42:57 +00:00
|
|
|
break;
|
2011-07-07 16:24:30 +00:00
|
|
|
}
|
2011-07-15 20:42:57 +00:00
|
|
|
if (channel->state_var & STATE_VAR_SEND_MSC_RSP){
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("Sending MSC RSP for #%u\n", channel->dlci);
|
2011-07-07 16:24:30 +00:00
|
|
|
channel->state_var &= ~STATE_VAR_SEND_MSC_RSP;
|
|
|
|
channel->state_var |= STATE_VAR_SENT_MSC_RSP;
|
2011-07-07 19:31:11 +00:00
|
|
|
rfcomm_send_uih_msc_rsp(multiplexer, channel->dlci, 0x8d); // ea=1,fc=0,rtc=1,rtr=1,ic=0,dv=1
|
2011-07-15 20:42:57 +00:00
|
|
|
break;
|
2011-07-07 16:24:30 +00:00
|
|
|
}
|
2011-07-15 20:42:57 +00:00
|
|
|
if (channel->state_var & STATE_VAR_SEND_CREDITS){
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("Providing credits for #%u\n", channel->dlci);
|
2011-07-07 16:24:30 +00:00
|
|
|
channel->state_var &= ~STATE_VAR_SEND_CREDITS;
|
|
|
|
channel->state_var |= STATE_VAR_SENT_CREDITS;
|
2011-07-07 19:31:11 +00:00
|
|
|
rfcomm_channel_provide_credits(channel);
|
2011-07-15 20:42:57 +00:00
|
|
|
break;
|
|
|
|
|
2011-07-07 16:24:30 +00:00
|
|
|
}
|
|
|
|
break;
|
2011-07-06 14:00:01 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2011-07-15 20:42:57 +00:00
|
|
|
// finally done?
|
|
|
|
if (rfcomm_channel_ready_for_open(channel)){
|
|
|
|
channel->state = RFCOMM_CHANNEL_OPEN;
|
|
|
|
rfcomm_channel_opened(channel);
|
|
|
|
}
|
2011-07-06 14:00:01 +00:00
|
|
|
break;
|
2011-07-19 14:59:07 +00:00
|
|
|
|
|
|
|
case RFCOMM_CHANNEL_OPEN:
|
|
|
|
switch (event->type){
|
|
|
|
case CH_EVT_RCVD_MSC_CMD:
|
|
|
|
channel->state_var |= STATE_VAR_SEND_MSC_RSP;
|
|
|
|
break;
|
|
|
|
case CH_EVT_READY_TO_SEND:
|
|
|
|
if (channel->state_var & STATE_VAR_SEND_MSC_RSP){
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("Sending MSC RSP for #%u\n", channel->dlci);
|
2011-07-19 14:59:07 +00:00
|
|
|
channel->state_var &= ~STATE_VAR_SEND_MSC_RSP;
|
|
|
|
rfcomm_send_uih_msc_rsp(multiplexer, channel->dlci, 0x8d); // ea=1,fc=0,rtc=1,rtr=1,ic=0,dv=1
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2011-07-06 14:00:01 +00:00
|
|
|
|
2011-07-07 16:24:30 +00:00
|
|
|
case RFCOMM_CHANNEL_SEND_DM:
|
|
|
|
switch (event->type) {
|
|
|
|
case CH_EVT_READY_TO_SEND:
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("Sending DM_PF for #%u\n", channel->dlci);
|
2011-07-07 16:42:40 +00:00
|
|
|
// don't emit channel closed - channel was never open
|
2011-07-07 19:31:11 +00:00
|
|
|
channel->state = RFCOMM_CHANNEL_CLOSED;
|
2011-07-07 16:24:30 +00:00
|
|
|
rfcomm_send_dm_pf(multiplexer, channel->dlci);
|
2011-07-07 16:42:40 +00:00
|
|
|
rfcomm_channel_finalize(channel);
|
2011-07-07 16:24:30 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2011-07-07 19:31:11 +00:00
|
|
|
break;
|
2011-07-07 16:24:30 +00:00
|
|
|
|
|
|
|
case RFCOMM_CHANNEL_SEND_DISC:
|
|
|
|
switch (event->type) {
|
|
|
|
case CH_EVT_READY_TO_SEND:
|
2011-07-07 19:31:11 +00:00
|
|
|
channel->state = RFCOMM_CHANNEL_CLOSED;
|
2011-07-07 16:24:30 +00:00
|
|
|
rfcomm_send_disc(multiplexer, channel->dlci);
|
2011-07-07 16:42:40 +00:00
|
|
|
rfcomm_emit_channel_closed(channel);
|
|
|
|
rfcomm_channel_finalize(channel);
|
2011-07-07 16:24:30 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2011-07-07 19:31:11 +00:00
|
|
|
break;
|
2011-07-07 16:24:30 +00:00
|
|
|
|
2011-07-07 16:42:40 +00:00
|
|
|
case RFCOMM_CHANNEL_SEND_UA_AFTER_DISC:
|
2011-07-07 16:24:30 +00:00
|
|
|
switch (event->type) {
|
|
|
|
case CH_EVT_READY_TO_SEND:
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("Sending UA after DISC for #%u\n", channel->dlci);
|
2011-07-07 19:31:11 +00:00
|
|
|
channel->state = RFCOMM_CHANNEL_CLOSED;
|
2011-07-07 16:24:30 +00:00
|
|
|
rfcomm_send_ua(multiplexer, channel->dlci);
|
2011-07-07 16:42:40 +00:00
|
|
|
rfcomm_channel_finalize(channel);
|
2011-07-07 16:24:30 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2011-07-07 19:31:11 +00:00
|
|
|
break;
|
2011-07-07 16:24:30 +00:00
|
|
|
|
2011-07-06 14:00:01 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-07 15:07:27 +00:00
|
|
|
|
2011-06-23 20:32:12 +00:00
|
|
|
// MARK: RFCOMM RUN
|
2011-07-06 14:00:01 +00:00
|
|
|
// process outstanding signaling tasks
|
2011-07-07 15:07:27 +00:00
|
|
|
static void rfcomm_run(void){
|
2011-06-23 20:32:12 +00:00
|
|
|
|
|
|
|
linked_item_t *it;
|
2011-07-07 22:37:17 +00:00
|
|
|
linked_item_t *next;
|
|
|
|
|
|
|
|
for (it = (linked_item_t *) rfcomm_multiplexers; it ; it = next){
|
|
|
|
|
|
|
|
next = it->next; // be prepared for removal of channel in state machine
|
|
|
|
|
2011-06-24 21:11:56 +00:00
|
|
|
rfcomm_multiplexer_t * multiplexer = ((rfcomm_multiplexer_t *) it);
|
|
|
|
|
2011-07-16 15:19:37 +00:00
|
|
|
if (!l2cap_can_send_packet_now(multiplexer->l2cap_cid)) {
|
2011-07-22 18:31:02 +00:00
|
|
|
// log_info("rfcomm_run cannot send l2cap packet for #%u, credits %u\n", multiplexer->l2cap_cid, multiplexer->l2cap_credits);
|
2011-07-16 15:19:37 +00:00
|
|
|
continue;
|
|
|
|
}
|
2011-07-22 18:31:02 +00:00
|
|
|
// log_info("rfcomm_run: multi 0x%08x, state %u\n", (int) multiplexer, multiplexer->state);
|
2011-06-24 21:11:56 +00:00
|
|
|
|
2011-07-07 22:37:17 +00:00
|
|
|
rfcomm_multiplexer_state_machine(multiplexer, MULT_EV_READY_TO_SEND);
|
2011-06-24 21:11:56 +00:00
|
|
|
}
|
2011-07-07 16:42:40 +00:00
|
|
|
|
|
|
|
for (it = (linked_item_t *) rfcomm_channels; it ; it = next){
|
|
|
|
|
|
|
|
next = it->next; // be prepared for removal of channel in state machine
|
|
|
|
|
2011-06-23 20:32:12 +00:00
|
|
|
rfcomm_channel_t * channel = ((rfcomm_channel_t *) it);
|
2011-06-24 20:06:47 +00:00
|
|
|
rfcomm_multiplexer_t * multiplexer = channel->multiplexer;
|
2011-06-23 20:32:12 +00:00
|
|
|
|
2011-07-16 15:19:37 +00:00
|
|
|
if (!l2cap_can_send_packet_now(multiplexer->l2cap_cid)) continue;
|
2011-06-24 18:37:10 +00:00
|
|
|
|
2011-07-07 15:10:29 +00:00
|
|
|
rfcomm_channel_event_t event;
|
|
|
|
event.type = CH_EVT_READY_TO_SEND;
|
|
|
|
rfcomm_channel_state_machine(channel, &event);
|
2011-06-23 20:32:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-23 18:28:19 +00:00
|
|
|
// MARK: RFCOMM BTstack API
|
2011-06-12 12:33:31 +00:00
|
|
|
|
2011-06-06 14:12:38 +00:00
|
|
|
void rfcomm_init(void){
|
2011-05-01 11:36:12 +00:00
|
|
|
rfcomm_client_cid_generator = 0;
|
|
|
|
rfcomm_multiplexers = NULL;
|
|
|
|
rfcomm_services = NULL;
|
|
|
|
rfcomm_channels = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// register packet handler
|
|
|
|
void rfcomm_register_packet_handler(void (*handler)(void * connection, uint8_t packet_type,
|
|
|
|
uint16_t channel, uint8_t *packet, uint16_t size)){
|
|
|
|
app_packet_handler = handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
// send packet over specific channel
|
|
|
|
int rfcomm_send_internal(uint8_t rfcomm_cid, uint8_t *data, uint16_t len){
|
2011-06-18 21:17:58 +00:00
|
|
|
|
2011-05-01 11:36:12 +00:00
|
|
|
rfcomm_channel_t * channel = rfcomm_channel_for_rfcomm_cid(rfcomm_cid);
|
2011-06-16 22:39:43 +00:00
|
|
|
if (!channel){
|
2011-07-22 18:34:18 +00:00
|
|
|
log_error("rfcomm_send_internal cid %u doesn't exist!\n", rfcomm_cid);
|
2011-06-16 22:39:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!channel->credits_outgoing){
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("rfcomm_send_internal cid %u, no rfcomm outgoing credits!\n", rfcomm_cid);
|
2011-06-18 21:47:40 +00:00
|
|
|
return RFCOMM_NO_OUTGOING_CREDITS;
|
2011-06-16 22:39:43 +00:00
|
|
|
}
|
2011-06-18 21:17:58 +00:00
|
|
|
|
|
|
|
if (!channel->packets_granted){
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("rfcomm_send_internal cid %u, no rfcomm credits granted!\n", rfcomm_cid);
|
2011-06-18 21:17:58 +00:00
|
|
|
// return RFCOMM_NO_OUTGOING_CREDITS;
|
|
|
|
}
|
|
|
|
|
2011-07-22 18:31:02 +00:00
|
|
|
// log_info("rfcomm_send_internal: len %u... outgoing credits %u, l2cap credit %us, granted %u\n",
|
2011-06-18 21:17:58 +00:00
|
|
|
// len, channel->credits_outgoing, channel->multiplexer->l2cap_credits, channel->packets_granted);
|
2011-06-16 22:39:43 +00:00
|
|
|
|
2011-06-18 21:17:58 +00:00
|
|
|
|
|
|
|
// send might cause l2cap to emit new credits, update counters first
|
|
|
|
channel->credits_outgoing--;
|
|
|
|
int packets_granted_decreased = 0;
|
|
|
|
if (channel->packets_granted) {
|
|
|
|
channel->packets_granted--;
|
|
|
|
packets_granted_decreased++;
|
|
|
|
}
|
2011-06-16 22:39:43 +00:00
|
|
|
|
2011-06-18 21:17:58 +00:00
|
|
|
int result = rfcomm_send_uih_data(channel->multiplexer, channel->dlci, data, len);
|
2011-06-16 22:39:43 +00:00
|
|
|
|
|
|
|
if (result != 0) {
|
2011-06-18 21:17:58 +00:00
|
|
|
channel->credits_outgoing++;
|
|
|
|
channel->packets_granted += packets_granted_decreased;
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("rfcomm_send_internal: error %d\n", result);
|
2011-06-16 22:39:43 +00:00
|
|
|
return result;
|
2011-05-01 11:36:12 +00:00
|
|
|
}
|
2011-06-16 22:39:43 +00:00
|
|
|
|
2011-07-22 18:31:02 +00:00
|
|
|
// log_info("rfcomm_send_internal: now outgoing credits %u, l2cap credit %us, granted %u\n",
|
2011-06-18 21:17:58 +00:00
|
|
|
// channel->credits_outgoing, channel->multiplexer->l2cap_credits, channel->packets_granted);
|
2011-06-16 22:39:43 +00:00
|
|
|
|
2011-05-01 11:36:12 +00:00
|
|
|
rfcomm_hand_out_credits();
|
2011-06-16 22:39:43 +00:00
|
|
|
|
2011-05-01 11:36:12 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rfcomm_create_channel_internal(void * connection, bd_addr_t *addr, uint8_t server_channel){
|
|
|
|
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("rfcomm_create_channel_internal to ");
|
2011-05-01 11:36:12 +00:00
|
|
|
print_bd_addr(*addr);
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info(" at channel #%02x\n", server_channel);
|
2011-05-01 11:36:12 +00:00
|
|
|
|
2011-07-04 21:49:59 +00:00
|
|
|
|
2011-05-01 11:36:12 +00:00
|
|
|
// create new multiplexer if necessary
|
|
|
|
rfcomm_multiplexer_t * multiplexer = rfcomm_multiplexer_for_addr(addr);
|
|
|
|
if (!multiplexer) {
|
|
|
|
multiplexer = rfcomm_multiplexer_create_for_addr(addr);
|
|
|
|
multiplexer->outgoing = 1;
|
|
|
|
multiplexer->state = RFCOMM_MULTIPLEXER_W4_CONNECT;
|
|
|
|
}
|
2011-07-04 21:49:59 +00:00
|
|
|
|
2011-05-01 11:36:12 +00:00
|
|
|
// prepare channel
|
|
|
|
rfcomm_channel_t * channel = rfcomm_channel_create(multiplexer, 0, server_channel);
|
|
|
|
channel->connection = connection;
|
|
|
|
|
2011-07-04 21:49:59 +00:00
|
|
|
// start multiplexer setup
|
|
|
|
if (multiplexer->state != RFCOMM_MULTIPLEXER_OPEN) {
|
|
|
|
|
|
|
|
channel->state = RFCOMM_CHANNEL_W4_MULTIPLEXER;
|
|
|
|
|
2011-07-07 22:50:58 +00:00
|
|
|
l2cap_create_channel_internal(connection, rfcomm_packet_handler, *addr, PSM_RFCOMM, RFCOMM_MAX_PAYLOAD);
|
2011-05-01 11:36:12 +00:00
|
|
|
|
2011-07-04 21:49:59 +00:00
|
|
|
return;
|
2011-05-01 11:36:12 +00:00
|
|
|
}
|
2011-07-04 21:49:59 +00:00
|
|
|
|
|
|
|
channel->state = RFCOMM_CHANNEL_SEND_UIH_PN;
|
2011-05-01 11:36:12 +00:00
|
|
|
|
2011-07-04 21:49:59 +00:00
|
|
|
// start connecting, if multiplexer is already up and running
|
|
|
|
rfcomm_run();
|
2011-05-01 11:36:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void rfcomm_disconnect_internal(uint16_t rfcomm_cid){
|
|
|
|
rfcomm_channel_t * channel = rfcomm_channel_for_rfcomm_cid(rfcomm_cid);
|
|
|
|
if (channel) {
|
2011-06-24 21:11:56 +00:00
|
|
|
channel->state = RFCOMM_CHANNEL_SEND_DISC;
|
2011-05-01 11:36:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-03 21:14:41 +00:00
|
|
|
void rfcomm_register_service_internal(void * connection, uint8_t channel, uint16_t max_frame_size){
|
|
|
|
|
|
|
|
// check if already registered
|
|
|
|
rfcomm_service_t * service = rfcomm_service_for_channel(channel);
|
|
|
|
if (service){
|
|
|
|
rfcomm_emit_service_registered(service->connection, RFCOMM_CHANNEL_ALREADY_REGISTERED, channel);
|
|
|
|
return;
|
|
|
|
}
|
2011-05-01 11:36:12 +00:00
|
|
|
|
|
|
|
// alloc structure
|
2011-05-03 21:14:41 +00:00
|
|
|
service = malloc(sizeof(rfcomm_service_t));
|
2011-05-01 11:36:12 +00:00
|
|
|
if (!service) {
|
2011-05-03 21:14:41 +00:00
|
|
|
rfcomm_emit_service_registered(service->connection, BTSTACK_MEMORY_ALLOC_FAILED, channel);
|
2011-05-01 11:36:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// register with l2cap if not registered before
|
|
|
|
if (linked_list_empty(&rfcomm_services)){
|
2011-07-07 22:50:58 +00:00
|
|
|
l2cap_register_service_internal(NULL, rfcomm_packet_handler, PSM_RFCOMM, RFCOMM_MAX_PAYLOAD);
|
2011-05-01 11:36:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// fill in
|
|
|
|
service->connection = connection;
|
2011-05-03 21:14:41 +00:00
|
|
|
service->server_channel = channel;
|
2011-05-01 11:36:12 +00:00
|
|
|
service->max_frame_size = max_frame_size;
|
|
|
|
|
|
|
|
// add to services list
|
|
|
|
linked_list_add(&rfcomm_services, (linked_item_t *) service);
|
|
|
|
|
|
|
|
// done
|
2011-05-03 21:14:41 +00:00
|
|
|
rfcomm_emit_service_registered(service->connection, 0, channel);
|
2011-05-01 11:36:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void rfcomm_unregister_service_internal(uint8_t service_channel){
|
|
|
|
rfcomm_service_t *service = rfcomm_service_for_channel(service_channel);
|
|
|
|
if (!service) return;
|
|
|
|
linked_list_remove(&rfcomm_services, (linked_item_t *) service);
|
|
|
|
free(service);
|
|
|
|
|
|
|
|
// unregister if no services active
|
|
|
|
if (linked_list_empty(&rfcomm_services)){
|
|
|
|
// bt_send_cmd(&l2cap_unregister_service, PSM_RFCOMM);
|
|
|
|
l2cap_unregister_service_internal(NULL, PSM_RFCOMM);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void rfcomm_accept_connection_internal(uint16_t rfcomm_cid){
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("Received Accept Connction\n");
|
2011-05-01 11:36:12 +00:00
|
|
|
rfcomm_channel_t * channel = rfcomm_channel_for_rfcomm_cid(rfcomm_cid);
|
|
|
|
if (!channel) return;
|
|
|
|
switch (channel->state) {
|
2011-07-05 14:59:00 +00:00
|
|
|
case RFCOMM_CHANNEL_INCOMING_SETUP:
|
|
|
|
channel->state_var |= STATE_VAR_CLIENT_ACCEPTED;
|
|
|
|
if (channel->state_var & STATE_VAR_RCVD_PN){
|
|
|
|
channel->state_var |= STATE_VAR_SEND_PN_RSP;
|
|
|
|
}
|
|
|
|
if (channel->state_var & STATE_VAR_RCVD_SABM){
|
|
|
|
channel->state_var |= STATE_VAR_SEND_UA;
|
|
|
|
}
|
2011-06-24 20:06:47 +00:00
|
|
|
break;
|
2011-05-01 11:36:12 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2011-07-07 19:31:11 +00:00
|
|
|
rfcomm_run();
|
2011-05-01 11:36:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void rfcomm_decline_connection_internal(uint16_t rfcomm_cid){
|
2011-07-22 18:31:02 +00:00
|
|
|
log_info("Received Decline Connction\n");
|
2011-05-01 11:36:12 +00:00
|
|
|
rfcomm_channel_t * channel = rfcomm_channel_for_rfcomm_cid(rfcomm_cid);
|
|
|
|
if (!channel) return;
|
|
|
|
switch (channel->state) {
|
2011-07-05 14:59:00 +00:00
|
|
|
case RFCOMM_CHANNEL_INCOMING_SETUP:
|
2011-06-24 20:06:47 +00:00
|
|
|
channel->state = RFCOMM_CHANNEL_SEND_DM;
|
|
|
|
break;
|
2011-05-01 11:36:12 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2011-07-07 19:31:11 +00:00
|
|
|
rfcomm_run();
|
2011-05-01 11:36:12 +00:00
|
|
|
}
|
|
|
|
|
2011-07-07 22:50:58 +00:00
|
|
|
//
|
|
|
|
void rfcomm_close_connection(void *connection){
|
|
|
|
linked_item_t *it;
|
|
|
|
|
|
|
|
// close open channels
|
|
|
|
for (it = (linked_item_t *) rfcomm_channels; it ; it = it->next){
|
|
|
|
((rfcomm_channel_t *)it)->state = RFCOMM_CHANNEL_SEND_DISC;
|
|
|
|
}
|
|
|
|
|
|
|
|
// unregister services
|
|
|
|
it = (linked_item_t *) &rfcomm_services;
|
|
|
|
while (it->next) {
|
|
|
|
rfcomm_service_t * service = (rfcomm_service_t *) it->next;
|
|
|
|
if (service->connection == connection){
|
|
|
|
it->next = it->next->next;
|
|
|
|
free(service);
|
|
|
|
} else {
|
|
|
|
it = it->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-01 11:36:12 +00:00
|
|
|
|
|
|
|
|