2009-10-29 20:25:42 +00:00
|
|
|
/*
|
2015-02-06 16:19:27 +00:00
|
|
|
* Copyright (C) 2014 BlueKitchen GmbH
|
2009-10-29 20:25:42 +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.
|
2012-05-07 21:54:09 +00:00
|
|
|
* 4. Any redistribution, use, or modification is done solely for
|
|
|
|
* personal benefit and not for any commercial purpose or for
|
|
|
|
* monetary gain.
|
2009-10-29 20:25:42 +00:00
|
|
|
*
|
2015-02-06 16:19:27 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
|
2009-10-29 20:25:42 +00:00
|
|
|
* ``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.
|
|
|
|
*
|
2015-02-06 16:19:27 +00:00
|
|
|
* Please inquire about commercial licensing options at
|
|
|
|
* contact@bluekitchen-gmbh.com
|
2012-05-07 21:54:09 +00:00
|
|
|
*
|
2009-10-29 20:25:42 +00:00
|
|
|
*/
|
|
|
|
|
2019-05-11 19:31:55 +02:00
|
|
|
#define BTSTACK_FILE__ "l2cap_signaling.c"
|
2017-03-24 23:39:20 +01:00
|
|
|
|
2009-07-23 21:55:36 +00:00
|
|
|
/*
|
|
|
|
* l2cap_signaling.h
|
|
|
|
*
|
|
|
|
* Created by Matthias Ringwald on 7/23/09.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "l2cap_signaling.h"
|
2016-01-21 15:41:16 +01:00
|
|
|
#include "btstack_config.h"
|
2016-07-01 14:48:49 +02:00
|
|
|
#include "btstack_debug.h"
|
2014-05-15 10:55:03 +00:00
|
|
|
#include "hci.h"
|
2009-07-23 21:55:36 +00:00
|
|
|
|
2009-09-30 19:25:56 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2020-01-31 12:05:24 +01:00
|
|
|
static uint16_t l2cap_create_signaling_internal(uint8_t * acl_buffer, hci_con_handle_t handle, bool is_classic, uint16_t cid, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, va_list argptr){
|
2020-01-17 11:30:10 +01:00
|
|
|
|
|
|
|
static const char *l2cap_signaling_commands_format[] = {
|
|
|
|
"2D", // 0x01 command reject: reason {cmd not understood (0), sig MTU exceeded (2:max sig MTU), invalid CID (4:req CID)}, data len, data
|
|
|
|
"22", // 0x02 connection request: PSM, Source CID
|
|
|
|
"2222", // 0x03 connection response: Dest CID, Source CID, Result, Status
|
|
|
|
"22D", // 0x04 config request: Dest CID, Flags, Configuration options
|
|
|
|
"222D", // 0x05 config response: Source CID, Flags, Result, Configuration options
|
|
|
|
"22", // 0x06 disconection request: Dest CID, Source CID
|
|
|
|
"22", // 0x07 disconection response: Dest CID, Source CID
|
|
|
|
"D", // 0x08 echo request: Data
|
|
|
|
"D", // 0x09 echo response: Data
|
|
|
|
"2", // 0x0a information request: InfoType {1=Connectionless MTU, 2=Extended features supported}
|
|
|
|
"22D", // 0x0b information response: InfoType, Result, Data
|
2016-01-21 15:33:36 +01:00
|
|
|
#ifdef ENABLE_BLE
|
2020-01-17 11:30:10 +01:00
|
|
|
NULL, // 0x0c non-supported AMP command
|
|
|
|
NULL, // 0x0d non-supported AMP command
|
|
|
|
NULL, // 0x0e non-supported AMP command
|
|
|
|
NULL, // 0x0f non-supported AMP command
|
|
|
|
NULL, // 0x10 non-supported AMP command
|
|
|
|
NULL, // 0x11 non-supported AMP command
|
|
|
|
"2222", // 0x12 connection parameter update request: interval min, interval max, slave latency, timeout multipler
|
|
|
|
"2", // 0x13 connection parameter update response: result
|
|
|
|
"22222", // 0X14 le credit based connection request: le psm, source cid, mtu, mps, initial credits
|
|
|
|
"22222", // 0x15 le credit based connection respone: dest cid, mtu, mps, initial credits, result
|
|
|
|
"22", // 0x16 le flow control credit: source cid, credits
|
2014-01-10 15:54:22 +00:00
|
|
|
#endif
|
2020-01-17 11:30:10 +01:00
|
|
|
};
|
|
|
|
static const unsigned int num_l2cap_commands = sizeof(l2cap_signaling_commands_format) / sizeof(const char *);
|
2009-07-23 21:55:36 +00:00
|
|
|
|
2016-07-01 14:48:49 +02:00
|
|
|
const char *format = NULL;
|
2019-11-19 14:31:48 +01:00
|
|
|
if ((cmd > 0) && (cmd <= num_l2cap_commands)) {
|
2016-07-01 14:48:49 +02:00
|
|
|
format = l2cap_signaling_commands_format[cmd-1];
|
|
|
|
}
|
|
|
|
if (!format){
|
|
|
|
log_error("l2cap_create_signaling_internal: invalid command id 0x%02x", cmd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-31 12:05:24 +01:00
|
|
|
int pb = 0x00; // First non-automatically-flushable packet of a higher layer message
|
|
|
|
#ifdef ENABLE_CLASSIC
|
|
|
|
if (is_classic){
|
|
|
|
pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
|
|
|
|
}
|
|
|
|
#endif
|
2014-05-09 13:29:58 +00:00
|
|
|
|
|
|
|
// 0 - Connection handle : PB=pb : BC=00
|
2016-01-31 00:07:32 +01:00
|
|
|
little_endian_store_16(acl_buffer, 0, handle | (pb << 12) | (0 << 14));
|
2009-07-23 21:55:36 +00:00
|
|
|
// 6 - L2CAP channel = 1
|
2016-01-31 00:07:32 +01:00
|
|
|
little_endian_store_16(acl_buffer, 6, cid);
|
2009-07-23 21:55:36 +00:00
|
|
|
// 8 - Code
|
|
|
|
acl_buffer[8] = cmd;
|
|
|
|
// 9 - id (!= 0 sequentially)
|
|
|
|
acl_buffer[9] = identifier;
|
|
|
|
|
|
|
|
// 12 - L2CAP signaling parameters
|
|
|
|
uint16_t pos = 12;
|
|
|
|
uint16_t word;
|
|
|
|
uint8_t * ptr;
|
|
|
|
while (*format) {
|
|
|
|
switch(*format) {
|
|
|
|
case '1': // 8 bit value
|
|
|
|
case '2': // 16 bit value
|
|
|
|
word = va_arg(argptr, int);
|
|
|
|
// minimal va_arg is int: 2 bytes on 8+16 bit CPUs
|
|
|
|
acl_buffer[pos++] = word & 0xff;
|
|
|
|
if (*format == '2') {
|
|
|
|
acl_buffer[pos++] = word >> 8;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'D': // variable data. passed: len, ptr
|
|
|
|
word = va_arg(argptr, int);
|
|
|
|
ptr = va_arg(argptr, uint8_t *);
|
2019-12-02 18:17:20 +01:00
|
|
|
(void)memcpy(&acl_buffer[pos], ptr, word);
|
2009-07-23 21:55:36 +00:00
|
|
|
pos += word;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
format++;
|
|
|
|
};
|
|
|
|
va_end(argptr);
|
|
|
|
|
|
|
|
// Fill in various length fields: it's the number of bytes following for ACL lenght and l2cap parameter length
|
|
|
|
// - the l2cap payload length is counted after the following channel id (only payload)
|
|
|
|
|
|
|
|
// 2 - ACL length
|
2016-01-31 00:07:32 +01:00
|
|
|
little_endian_store_16(acl_buffer, 2, pos - 4);
|
2009-07-23 21:55:36 +00:00
|
|
|
// 4 - L2CAP packet length
|
2016-01-31 00:07:32 +01:00
|
|
|
little_endian_store_16(acl_buffer, 4, pos - 6 - 2);
|
2009-07-23 21:55:36 +00:00
|
|
|
// 10 - L2CAP signaling parameter length
|
2016-01-31 00:07:32 +01:00
|
|
|
little_endian_store_16(acl_buffer, 10, pos - 12);
|
2009-07-23 21:55:36 +00:00
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|
2014-01-10 16:28:21 +00:00
|
|
|
|
2014-02-18 21:19:10 +00:00
|
|
|
uint16_t l2cap_create_signaling_classic(uint8_t * acl_buffer, hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, va_list argptr){
|
2020-01-31 12:05:24 +01:00
|
|
|
return l2cap_create_signaling_internal(acl_buffer, handle, true, 1, cmd, identifier, argptr);
|
2014-02-18 21:19:10 +00:00
|
|
|
}
|
|
|
|
|
2016-01-21 15:33:36 +01:00
|
|
|
#ifdef ENABLE_BLE
|
2014-02-18 21:19:10 +00:00
|
|
|
uint16_t l2cap_create_signaling_le(uint8_t * acl_buffer, hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, va_list argptr){
|
2020-01-31 12:05:24 +01:00
|
|
|
return l2cap_create_signaling_internal(acl_buffer, handle, false, 5, cmd, identifier, argptr);
|
2014-02-18 21:19:10 +00:00
|
|
|
}
|
2014-01-10 16:28:21 +00:00
|
|
|
#endif
|