2016-09-26 13:37:39 +00:00
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Google, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
#if (defined(L2CAP_CLIENT_INCLUDED) && L2CAP_CLIENT_INCLUDED == TRUE)
|
|
|
|
#include <string.h>
|
2018-04-08 04:10:50 +00:00
|
|
|
#include "common/bt_trace.h"
|
|
|
|
#include "common/bt_defs.h"
|
|
|
|
#include "device/bdaddr.h"
|
|
|
|
#include "osi/allocator.h"
|
|
|
|
#include "osi/buffer.h"
|
|
|
|
#include "osi/list.h"
|
|
|
|
#include "osi/osi.h"
|
|
|
|
#include "stack/l2cap_client.h"
|
|
|
|
#include "stack/l2c_api.h"
|
2016-09-26 13:37:39 +00:00
|
|
|
|
|
|
|
struct l2cap_client_t {
|
2016-11-24 18:10:15 +00:00
|
|
|
l2cap_client_callbacks_t callbacks;
|
|
|
|
void *context;
|
|
|
|
|
|
|
|
uint16_t local_channel_id;
|
|
|
|
uint16_t remote_mtu;
|
|
|
|
bool configured_self;
|
|
|
|
bool configured_peer;
|
|
|
|
bool is_congested;
|
|
|
|
list_t *outbound_fragments;
|
2016-09-26 13:37:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void connect_completed_cb(uint16_t local_channel_id, uint16_t error_code);
|
|
|
|
static void config_request_cb(uint16_t local_channel_id, tL2CAP_CFG_INFO *requested_parameters);
|
|
|
|
static void config_completed_cb(uint16_t local_channel_id, tL2CAP_CFG_INFO *negotiated_parameters);
|
|
|
|
static void disconnect_request_cb(uint16_t local_channel_id, bool ack_required);
|
|
|
|
static void disconnect_completed_cb(uint16_t local_channel_id, uint16_t error_code);
|
|
|
|
static void congestion_cb(uint16_t local_channel_id, bool is_congested);
|
|
|
|
static void read_ready_cb(uint16_t local_channel_id, BT_HDR *packet);
|
|
|
|
static void write_completed_cb(uint16_t local_channel_id, uint16_t packets_completed);
|
|
|
|
|
|
|
|
static void fragment_packet(l2cap_client_t *client, buffer_t *packet);
|
|
|
|
static void dispatch_fragments(l2cap_client_t *client);
|
|
|
|
static l2cap_client_t *find(uint16_t local_channel_id);
|
|
|
|
|
|
|
|
// From the Bluetooth Core specification.
|
|
|
|
static const uint16_t L2CAP_MTU_DEFAULT = 672;
|
|
|
|
static const uint16_t L2CAP_MTU_MINIMUM = 48;
|
|
|
|
|
|
|
|
static const tL2CAP_APPL_INFO l2cap_callbacks = {
|
2016-11-24 18:10:15 +00:00
|
|
|
.pL2CA_ConnectCfm_Cb = connect_completed_cb,
|
|
|
|
.pL2CA_ConfigInd_Cb = config_request_cb,
|
|
|
|
.pL2CA_ConfigCfm_Cb = config_completed_cb,
|
|
|
|
.pL2CA_DisconnectInd_Cb = disconnect_request_cb,
|
|
|
|
.pL2CA_DisconnectCfm_Cb = disconnect_completed_cb,
|
|
|
|
.pL2CA_CongestionStatus_Cb = congestion_cb,
|
|
|
|
.pL2CA_DataInd_Cb = read_ready_cb,
|
|
|
|
.pL2CA_TxComplete_Cb = write_completed_cb,
|
2016-09-26 13:37:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static list_t *l2cap_clients; // A list of l2cap_client_t. Container does not own objects.
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
buffer_t *l2cap_buffer_new(size_t size)
|
|
|
|
{
|
|
|
|
buffer_t *buf = buffer_new(size + L2CAP_MIN_OFFSET);
|
|
|
|
buffer_t *slice = NULL;
|
|
|
|
if (buf) {
|
|
|
|
slice = buffer_new_slice(buf, size);
|
|
|
|
}
|
|
|
|
buffer_free(buf);
|
|
|
|
return slice;
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
l2cap_client_t *l2cap_client_new(const l2cap_client_callbacks_t *callbacks, void *context)
|
|
|
|
{
|
|
|
|
assert(callbacks != NULL);
|
|
|
|
assert(callbacks->connected != NULL);
|
|
|
|
assert(callbacks->disconnected != NULL);
|
|
|
|
assert(callbacks->read_ready != NULL);
|
|
|
|
assert(callbacks->write_ready != NULL);
|
2016-09-26 13:37:39 +00:00
|
|
|
|
|
|
|
if (!l2cap_clients) {
|
2016-11-24 18:10:15 +00:00
|
|
|
l2cap_clients = list_new(NULL);
|
|
|
|
if (!l2cap_clients) {
|
2018-04-25 02:11:06 +00:00
|
|
|
L2CAP_TRACE_ERROR("%s unable to allocate space for L2CAP client list.", __func__);
|
2016-11-24 18:10:15 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
l2cap_client_t *ret = (l2cap_client_t *)osi_calloc(sizeof(l2cap_client_t));
|
|
|
|
if (!ret) {
|
2018-04-25 02:11:06 +00:00
|
|
|
L2CAP_TRACE_ERROR("%s unable to allocate L2CAP client.", __func__);
|
2016-11-24 18:10:15 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
ret->callbacks = *callbacks;
|
|
|
|
ret->context = context;
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
ret->remote_mtu = L2CAP_MTU_DEFAULT;
|
|
|
|
ret->outbound_fragments = list_new(NULL);
|
|
|
|
if (!ret) {
|
2018-04-25 02:11:06 +00:00
|
|
|
L2CAP_TRACE_ERROR("%s unable to allocate outbound L2CAP fragment list.", __func__);
|
2016-11-24 18:10:15 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
list_append(l2cap_clients, ret);
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
return ret;
|
2016-09-26 13:37:39 +00:00
|
|
|
|
|
|
|
error:;
|
2016-11-24 18:10:15 +00:00
|
|
|
osi_free(ret);
|
|
|
|
return NULL;
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
void l2cap_client_free(l2cap_client_t *client)
|
|
|
|
{
|
|
|
|
if (!client) {
|
|
|
|
return;
|
|
|
|
}
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
list_remove(l2cap_clients, client);
|
|
|
|
l2cap_client_disconnect(client);
|
|
|
|
list_free(client->outbound_fragments);
|
|
|
|
osi_free(client);
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
bool l2cap_client_connect(l2cap_client_t *client, const bt_bdaddr_t *remote_bdaddr, uint16_t psm)
|
|
|
|
{
|
|
|
|
assert(client != NULL);
|
|
|
|
assert(remote_bdaddr != NULL);
|
|
|
|
assert(psm != 0);
|
|
|
|
assert(!bdaddr_is_empty(remote_bdaddr));
|
|
|
|
assert(client->local_channel_id == 0);
|
|
|
|
assert(!client->configured_self);
|
|
|
|
assert(!client->configured_peer);
|
|
|
|
assert(!L2C_INVALID_PSM(psm));
|
|
|
|
|
|
|
|
client->local_channel_id = L2CA_ConnectReq(psm, (uint8_t *)remote_bdaddr);
|
|
|
|
if (!client->local_channel_id) {
|
2018-04-25 02:11:06 +00:00
|
|
|
L2CAP_TRACE_ERROR("%s unable to create L2CAP connection.", __func__);
|
2016-11-24 18:10:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
L2CA_SetConnectionCallbacks(client->local_channel_id, &l2cap_callbacks);
|
|
|
|
return true;
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
void l2cap_client_disconnect(l2cap_client_t *client)
|
|
|
|
{
|
|
|
|
assert(client != NULL);
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
if (client->local_channel_id && !L2CA_DisconnectReq(client->local_channel_id)) {
|
2018-04-25 02:11:06 +00:00
|
|
|
L2CAP_TRACE_ERROR("%s unable to send disconnect message for LCID 0x%04x.", __func__, client->local_channel_id);
|
2016-11-24 18:10:15 +00:00
|
|
|
}
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
client->local_channel_id = 0;
|
|
|
|
client->remote_mtu = L2CAP_MTU_DEFAULT;
|
|
|
|
client->configured_self = false;
|
|
|
|
client->configured_peer = false;
|
|
|
|
client->is_congested = false;
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
for (const list_node_t *node = list_begin(client->outbound_fragments); node != list_end(client->outbound_fragments); node = list_next(node)) {
|
2017-08-17 13:13:45 +00:00
|
|
|
osi_free(list_node(node));
|
2016-11-24 18:10:15 +00:00
|
|
|
}
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
list_clear(client->outbound_fragments);
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
bool l2cap_client_is_connected(const l2cap_client_t *client)
|
|
|
|
{
|
|
|
|
assert(client != NULL);
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
return client->local_channel_id != 0 && client->configured_self && client->configured_peer;
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
bool l2cap_client_write(l2cap_client_t *client, buffer_t *packet)
|
|
|
|
{
|
|
|
|
assert(client != NULL);
|
|
|
|
assert(packet != NULL);
|
|
|
|
assert(l2cap_client_is_connected(client));
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
if (client->is_congested) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
fragment_packet(client, packet);
|
|
|
|
dispatch_fragments(client);
|
|
|
|
return true;
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
static void connect_completed_cb(uint16_t local_channel_id, uint16_t error_code)
|
|
|
|
{
|
|
|
|
assert(local_channel_id != 0);
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
l2cap_client_t *client = find(local_channel_id);
|
|
|
|
if (!client) {
|
2018-04-25 02:11:06 +00:00
|
|
|
L2CAP_TRACE_ERROR("%s unable to find L2CAP client for LCID 0x%04x.", __func__, local_channel_id);
|
2016-11-24 18:10:15 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
if (error_code != L2CAP_CONN_OK) {
|
2018-04-25 02:11:06 +00:00
|
|
|
L2CAP_TRACE_ERROR("%s error connecting L2CAP channel: %d.", __func__, error_code);
|
2016-11-24 18:10:15 +00:00
|
|
|
client->callbacks.disconnected(client, client->context);
|
|
|
|
return;
|
|
|
|
}
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
// Use default L2CAP parameters.
|
|
|
|
tL2CAP_CFG_INFO desired_parameters = { 0 };
|
|
|
|
if (!L2CA_ConfigReq(local_channel_id, &desired_parameters)) {
|
2018-04-25 02:11:06 +00:00
|
|
|
L2CAP_TRACE_ERROR("%s error sending L2CAP config parameters.", __func__);
|
2016-11-24 18:10:15 +00:00
|
|
|
client->callbacks.disconnected(client, client->context);
|
|
|
|
}
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
static void config_request_cb(uint16_t local_channel_id, tL2CAP_CFG_INFO *requested_parameters)
|
|
|
|
{
|
|
|
|
tL2CAP_CFG_INFO response = { 0 };
|
|
|
|
l2cap_client_t *client = find(local_channel_id);
|
|
|
|
|
|
|
|
if (!client) {
|
2018-04-25 02:11:06 +00:00
|
|
|
L2CAP_TRACE_ERROR("%s unable to find L2CAP client matching LCID 0x%04x.", __func__, local_channel_id);
|
2016-11-24 18:10:15 +00:00
|
|
|
return;
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
2016-11-24 18:10:15 +00:00
|
|
|
|
|
|
|
response.result = L2CAP_CFG_OK;
|
|
|
|
|
|
|
|
if (requested_parameters->mtu_present) {
|
|
|
|
// Make sure the peer chose an MTU at least as large as the minimum L2CAP MTU defined
|
|
|
|
// by the Bluetooth Core spec.
|
|
|
|
if (requested_parameters->mtu < L2CAP_MTU_MINIMUM) {
|
|
|
|
response.mtu = L2CAP_MTU_MINIMUM;
|
|
|
|
response.mtu_present = true;
|
|
|
|
response.result = L2CAP_CFG_UNACCEPTABLE_PARAMS;
|
|
|
|
} else {
|
|
|
|
client->remote_mtu = requested_parameters->mtu;
|
|
|
|
}
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
if (requested_parameters->fcr_present) {
|
|
|
|
if (requested_parameters->fcr.mode != L2CAP_FCR_BASIC_MODE) {
|
|
|
|
response.fcr_present = true;
|
|
|
|
response.fcr = requested_parameters->fcr;
|
|
|
|
response.fcr.mode = L2CAP_FCR_BASIC_MODE;
|
|
|
|
response.result = L2CAP_CFG_UNACCEPTABLE_PARAMS;
|
|
|
|
}
|
|
|
|
}
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
if (!L2CA_ConfigRsp(local_channel_id, &response)) {
|
2018-04-25 02:11:06 +00:00
|
|
|
L2CAP_TRACE_ERROR("%s unable to send config response for LCID 0x%04x.", __func__, local_channel_id);
|
2016-11-24 18:10:15 +00:00
|
|
|
l2cap_client_disconnect(client);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we've configured both endpoints, let the listener know we've connected.
|
|
|
|
client->configured_peer = true;
|
|
|
|
if (l2cap_client_is_connected(client)) {
|
|
|
|
client->callbacks.connected(client, client->context);
|
|
|
|
}
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
static void config_completed_cb(uint16_t local_channel_id, tL2CAP_CFG_INFO *negotiated_parameters)
|
|
|
|
{
|
|
|
|
l2cap_client_t *client = find(local_channel_id);
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
if (!client) {
|
2018-04-25 02:11:06 +00:00
|
|
|
L2CAP_TRACE_ERROR("%s unable to find L2CAP client matching LCID 0x%04x.", __func__, local_channel_id);
|
2016-11-24 18:10:15 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
switch (negotiated_parameters->result) {
|
2016-09-26 13:37:39 +00:00
|
|
|
// We'll get another configuration response later.
|
|
|
|
case L2CAP_CFG_PENDING:
|
2016-11-24 18:10:15 +00:00
|
|
|
break;
|
2016-09-26 13:37:39 +00:00
|
|
|
|
|
|
|
case L2CAP_CFG_UNACCEPTABLE_PARAMS:
|
2016-11-24 18:10:15 +00:00
|
|
|
// TODO: see if we can renegotiate parameters instead of dropping the connection.
|
2018-04-25 02:11:06 +00:00
|
|
|
L2CAP_TRACE_WARNING("%s dropping L2CAP connection due to unacceptable config parameters.\n", __func__);
|
2016-11-24 18:10:15 +00:00
|
|
|
l2cap_client_disconnect(client);
|
|
|
|
break;
|
2016-09-26 13:37:39 +00:00
|
|
|
|
|
|
|
case L2CAP_CFG_OK:
|
2016-11-24 18:10:15 +00:00
|
|
|
// If we've configured both endpoints, let the listener know we've connected.
|
|
|
|
client->configured_self = true;
|
|
|
|
if (l2cap_client_is_connected(client)) {
|
|
|
|
client->callbacks.connected(client, client->context);
|
|
|
|
}
|
|
|
|
break;
|
2016-09-26 13:37:39 +00:00
|
|
|
|
|
|
|
// Failure, no further parameter negotiation possible.
|
|
|
|
default:
|
2018-04-25 02:11:06 +00:00
|
|
|
L2CAP_TRACE_WARNING("%s L2CAP parameter negotiation failed with error code %d.\n", __func__, negotiated_parameters->result);
|
2016-11-24 18:10:15 +00:00
|
|
|
l2cap_client_disconnect(client);
|
|
|
|
break;
|
|
|
|
}
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
static void disconnect_request_cb(uint16_t local_channel_id, bool ack_required)
|
|
|
|
{
|
|
|
|
l2cap_client_t *client = find(local_channel_id);
|
|
|
|
if (!client) {
|
2018-04-25 02:11:06 +00:00
|
|
|
L2CAP_TRACE_ERROR("%s unable to find L2CAP client with LCID 0x%04x.\n", __func__, local_channel_id);
|
2016-11-24 18:10:15 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
if (ack_required) {
|
|
|
|
L2CA_DisconnectRsp(local_channel_id);
|
|
|
|
}
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
// We already sent a disconnect response so this LCID is now invalid.
|
|
|
|
client->local_channel_id = 0;
|
|
|
|
l2cap_client_disconnect(client);
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
client->callbacks.disconnected(client, client->context);
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
static void disconnect_completed_cb(uint16_t local_channel_id, UNUSED_ATTR uint16_t error_code)
|
|
|
|
{
|
|
|
|
assert(local_channel_id != 0);
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
l2cap_client_t *client = find(local_channel_id);
|
|
|
|
if (!client) {
|
2018-04-25 02:11:06 +00:00
|
|
|
L2CAP_TRACE_ERROR("%s unable to find L2CAP client with LCID 0x%04x.\n", __func__, local_channel_id);
|
2016-11-24 18:10:15 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
client->local_channel_id = 0;
|
|
|
|
l2cap_client_disconnect(client);
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
client->callbacks.disconnected(client, client->context);
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
static void congestion_cb(uint16_t local_channel_id, bool is_congested)
|
|
|
|
{
|
|
|
|
assert(local_channel_id != 0);
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
l2cap_client_t *client = find(local_channel_id);
|
|
|
|
if (!client) {
|
2018-04-25 02:11:06 +00:00
|
|
|
L2CAP_TRACE_ERROR("%s unable to find L2CAP client matching LCID 0x%04x.\n", __func__, local_channel_id);
|
2016-11-24 18:10:15 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
client->is_congested = is_congested;
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
if (!is_congested) {
|
|
|
|
// If we just decongested, dispatch whatever we have left over in our queue.
|
|
|
|
// Once that's done, if we're still decongested, notify the listener so it
|
|
|
|
// can start writing again.
|
|
|
|
dispatch_fragments(client);
|
|
|
|
if (!client->is_congested) {
|
|
|
|
client->callbacks.write_ready(client, client->context);
|
|
|
|
}
|
|
|
|
}
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
static void read_ready_cb(uint16_t local_channel_id, BT_HDR *packet)
|
|
|
|
{
|
|
|
|
assert(local_channel_id != 0);
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
l2cap_client_t *client = find(local_channel_id);
|
|
|
|
if (!client) {
|
2018-04-25 02:11:06 +00:00
|
|
|
L2CAP_TRACE_ERROR("%s unable to find L2CAP client matching LCID 0x%04x.\n", __func__, local_channel_id);
|
2016-11-24 18:10:15 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
// TODO(sharvil): eliminate copy from BT_HDR.
|
|
|
|
buffer_t *buffer = buffer_new(packet->len);
|
|
|
|
memcpy(buffer_ptr(buffer), packet->data + packet->offset, packet->len);
|
2017-08-17 13:13:45 +00:00
|
|
|
osi_free(packet);
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
client->callbacks.read_ready(client, buffer, client->context);
|
|
|
|
buffer_free(buffer);
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
static void write_completed_cb(UNUSED_ATTR uint16_t local_channel_id, UNUSED_ATTR uint16_t packets_completed)
|
|
|
|
{
|
|
|
|
// Do nothing. We update congestion state based on the congestion callback
|
|
|
|
// and we've already removed items from outbound_fragments list so we don't
|
|
|
|
// really care how many packets were successfully dispatched.
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
static void fragment_packet(l2cap_client_t *client, buffer_t *packet)
|
|
|
|
{
|
|
|
|
assert(client != NULL);
|
|
|
|
assert(packet != NULL);
|
|
|
|
|
|
|
|
// TODO(sharvil): eliminate copy into BT_HDR.
|
2017-08-17 13:13:45 +00:00
|
|
|
BT_HDR *bt_packet = osi_malloc(buffer_length(packet) + L2CAP_MIN_OFFSET);
|
2016-11-24 18:10:15 +00:00
|
|
|
bt_packet->offset = L2CAP_MIN_OFFSET;
|
|
|
|
bt_packet->len = buffer_length(packet);
|
|
|
|
memcpy(bt_packet->data + bt_packet->offset, buffer_ptr(packet), buffer_length(packet));
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (bt_packet->len <= client->remote_mtu) {
|
|
|
|
if (bt_packet->len > 0) {
|
|
|
|
list_append(client->outbound_fragments, bt_packet);
|
|
|
|
} else {
|
2017-08-17 13:13:45 +00:00
|
|
|
osi_free(bt_packet);
|
2016-11-24 18:10:15 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-08-17 13:13:45 +00:00
|
|
|
BT_HDR *fragment = osi_malloc(client->remote_mtu + L2CAP_MIN_OFFSET);
|
2016-11-24 18:10:15 +00:00
|
|
|
fragment->offset = L2CAP_MIN_OFFSET;
|
|
|
|
fragment->len = client->remote_mtu;
|
|
|
|
memcpy(fragment->data + fragment->offset, bt_packet->data + bt_packet->offset, client->remote_mtu);
|
|
|
|
|
|
|
|
list_append(client->outbound_fragments, fragment);
|
|
|
|
|
|
|
|
bt_packet->offset += client->remote_mtu;
|
|
|
|
bt_packet->len -= client->remote_mtu;
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
static void dispatch_fragments(l2cap_client_t *client)
|
|
|
|
{
|
|
|
|
assert(client != NULL);
|
|
|
|
assert(!client->is_congested);
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
while (!list_is_empty(client->outbound_fragments)) {
|
|
|
|
BT_HDR *packet = (BT_HDR *)list_front(client->outbound_fragments);
|
|
|
|
list_remove(client->outbound_fragments, packet);
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
switch (L2CA_DataWrite(client->local_channel_id, packet)) {
|
|
|
|
case L2CAP_DW_CONGESTED:
|
|
|
|
client->is_congested = true;
|
|
|
|
return;
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
case L2CAP_DW_FAILED:
|
2018-04-25 02:11:06 +00:00
|
|
|
L2CAP_TRACE_ERROR("%s error writing data to L2CAP connection LCID 0x%04x; disconnecting.", __func__, client->local_channel_id);
|
2016-11-24 18:10:15 +00:00
|
|
|
l2cap_client_disconnect(client);
|
|
|
|
return;
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
case L2CAP_DW_SUCCESS:
|
|
|
|
break;
|
|
|
|
}
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
static l2cap_client_t *find(uint16_t local_channel_id)
|
|
|
|
{
|
|
|
|
assert(local_channel_id != 0);
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
for (const list_node_t *node = list_begin(l2cap_clients); node != list_end(l2cap_clients); node = list_next(node)) {
|
|
|
|
l2cap_client_t *client = (l2cap_client_t *)list_node(node);
|
|
|
|
if (client->local_channel_id == local_channel_id) {
|
|
|
|
return client;
|
|
|
|
}
|
|
|
|
}
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
return NULL;
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /*L2CAP_CLIENT_INCLUDED*/
|