119e95205Sopenharmony_ci/*
219e95205Sopenharmony_ci * Copyright (C) 2021 Huawei Device Co., Ltd.
319e95205Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
419e95205Sopenharmony_ci * you may not use this file except in compliance with the License.
519e95205Sopenharmony_ci * You may obtain a copy of the License at
619e95205Sopenharmony_ci *
719e95205Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
819e95205Sopenharmony_ci *
919e95205Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1019e95205Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1119e95205Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1219e95205Sopenharmony_ci * See the License for the specific language governing permissions and
1319e95205Sopenharmony_ci * limitations under the License.
1419e95205Sopenharmony_ci */
1519e95205Sopenharmony_ci
1619e95205Sopenharmony_ci/**
1719e95205Sopenharmony_ci * @file l2cap_def.h
1819e95205Sopenharmony_ci *
1919e95205Sopenharmony_ci * @brief Interface of bluetooth l2cap protocol BR/EDR part
2019e95205Sopenharmony_ci *
2119e95205Sopenharmony_ci */
2219e95205Sopenharmony_ci
2319e95205Sopenharmony_ci#ifndef L2CAP_DEF_H
2419e95205Sopenharmony_ci#define L2CAP_DEF_H
2519e95205Sopenharmony_ci
2619e95205Sopenharmony_ci#include <stdint.h>
2719e95205Sopenharmony_ci#include "packet.h"
2819e95205Sopenharmony_ci#include "btstack.h"
2919e95205Sopenharmony_ci
3019e95205Sopenharmony_ci#ifdef __cplusplus
3119e95205Sopenharmony_ciextern "C" {
3219e95205Sopenharmony_ci#endif  // __cplusplus
3319e95205Sopenharmony_ci
3419e95205Sopenharmony_ci// -------------------- Below is for BR/EDR ------------------------------
3519e95205Sopenharmony_ci
3619e95205Sopenharmony_ci#define L2CAP_INFORMATION_TYPE_CONNECTIONLESS_MTU 0x0001  // not supported in this implementation
3719e95205Sopenharmony_ci#define L2CAP_INFORMATION_TYPE_EXTENDED_FEATURE 0x0002
3819e95205Sopenharmony_ci#define L2CAP_INFORMATION_TYPE_FIXED_CHANNEL 0x0003
3919e95205Sopenharmony_ci
4019e95205Sopenharmony_ci// the first octet for extended feature mask
4119e95205Sopenharmony_ci#define L2CAP_FEATURE_FLOW_CONTROL_MODE 0x01
4219e95205Sopenharmony_ci#define L2CAP_FEATURE_RETRANSMISSION_MODE 0x02
4319e95205Sopenharmony_ci#define L2CAP_FEATURE_BIDIRECTIONAL_QOS 0x04
4419e95205Sopenharmony_ci#define L2CAP_FEATURE_ENHANCED_RETRANSMISSION_MODE 0x08
4519e95205Sopenharmony_ci#define L2CAP_FEATURE_STREAMING_MODE 0x10
4619e95205Sopenharmony_ci#define L2CAP_FEATURE_FCS_OPTION 0x20
4719e95205Sopenharmony_ci#define L2CAP_FEATURE_EXTENDED_FLOW_SPECIFICATION 0x40
4819e95205Sopenharmony_ci#define L2CAP_FEATURE_FIXED_CHANNELS 0x80
4919e95205Sopenharmony_ci
5019e95205Sopenharmony_ci// the second octet for extended feature mask
5119e95205Sopenharmony_ci#define L2CAP_FEATURE_EXTENDED_WINDOW_SIZE 0x01
5219e95205Sopenharmony_ci#define L2CAP_FEATURE_UNICAST_CONNECTIONLESS_DATA 0x02
5319e95205Sopenharmony_ci
5419e95205Sopenharmony_ci// the first octet for fixed channels supported
5519e95205Sopenharmony_ci#define L2CAP_FEATURE_SIGNALING_CHANNEl 0x02
5619e95205Sopenharmony_ci#define L2CAP_FEATURE_CONNECTIONLESS_RECEPTION 0x04
5719e95205Sopenharmony_ci#define L2CAP_FEATURE_AMP_MANAGER_PROTOCOL 0x08
5819e95205Sopenharmony_ci#define L2CAP_FEATURE_BREDR_SECURITY_MANAGER 0x80
5919e95205Sopenharmony_ci
6019e95205Sopenharmony_ci// the eighth octet for fixed channels supported
6119e95205Sopenharmony_ci#define L2CAP_FEATURE_AMP_TEST_MANAGER 0x80
6219e95205Sopenharmony_ci
6319e95205Sopenharmony_ci// l2cap mode
6419e95205Sopenharmony_ci#define L2CAP_BASIC_MODE 0x00
6519e95205Sopenharmony_ci#define L2CAP_ENHANCED_RETRANSMISSION_MODE 0x03
6619e95205Sopenharmony_ci#define L2CAP_STREAM_MODE 0x04
6719e95205Sopenharmony_ci
6819e95205Sopenharmony_ci#define L2CAP_DEFAULT_MTU 672
6919e95205Sopenharmony_ci
7019e95205Sopenharmony_ci// L2cap connection response result
7119e95205Sopenharmony_ci#define L2CAP_CONNECTION_SUCCESSFUL 0x0000
7219e95205Sopenharmony_ci#define L2CAP_CONNECTION_PENDING 0x0001
7319e95205Sopenharmony_ci#define L2CAP_PSM_NOT_SUPPORTED 0x0002
7419e95205Sopenharmony_ci#define L2CAP_SECURITY_BLOCK 0x0003
7519e95205Sopenharmony_ci#define L2CAP_NO_RESOURCES_AVAILABLE 0x0004
7619e95205Sopenharmony_ci#define L2CAP_INVALID_SOURCE_CID 0x0006
7719e95205Sopenharmony_ci#define L2CAP_SOURCE_CID_ALREADY_ALLOCATED 0x0007
7819e95205Sopenharmony_ci
7919e95205Sopenharmony_ci// L2cap connection response status
8019e95205Sopenharmony_ci#define L2CAP_NO_FURTHER_INFORMATION_AVAILABLE 0x0000
8119e95205Sopenharmony_ci#define L2CAP_AUTHENTICATION_PENDING 0x0001
8219e95205Sopenharmony_ci#define L2CAP_AUTHORIZATION_PENDING 0x0002
8319e95205Sopenharmony_ci
8419e95205Sopenharmony_ci// L2cap config response result
8519e95205Sopenharmony_ci#define L2CAP_SUCCESS 0x0000
8619e95205Sopenharmony_ci#define L2CAP_UNACCEPTABLE_PARAMETERS 0x0001
8719e95205Sopenharmony_ci#define L2CAP_REJECTED 0x0002
8819e95205Sopenharmony_ci#define L2CAP_UNKNOWN_OPTIONS 0x0003
8919e95205Sopenharmony_ci#define L2CAP_PENDING 0x0004
9019e95205Sopenharmony_ci#define L2CAP_FLOW_SPEC_REJECTED 0x0005
9119e95205Sopenharmony_ci
9219e95205Sopenharmony_ci#define L2CAP_STATE_COLLISION 0xFF
9319e95205Sopenharmony_ci
9419e95205Sopenharmony_citypedef struct {
9519e95205Sopenharmony_ci    // The requested mode of the link.
9619e95205Sopenharmony_ci    // Possible values are,
9719e95205Sopenharmony_ci    // 0x00(Default) - Basic Mode
9819e95205Sopenharmony_ci    // 0x03 - Enhanced Retransmission mode
9919e95205Sopenharmony_ci    // 0x04 - Streaming mode
10019e95205Sopenharmony_ci    uint8_t mode;
10119e95205Sopenharmony_ci
10219e95205Sopenharmony_ci    // Valid in Enhanced Retransmission mode, the value should be set to 0.
10319e95205Sopenharmony_ci    uint8_t maxTransmit;
10419e95205Sopenharmony_ci
10519e95205Sopenharmony_ci    // Valid in Enhanced Retransmission mode, the value should be set to 0.
10619e95205Sopenharmony_ci    uint8_t txWindowSize;
10719e95205Sopenharmony_ci
10819e95205Sopenharmony_ci    // Valid in Enhanced Retransmission mode, refer to the size of transmission window.
10919e95205Sopenharmony_ci    // The value range is 1 to 63
11019e95205Sopenharmony_ci    // If the value is set to 0, then l2cap will determine the real value.
11119e95205Sopenharmony_ci    uint8_t rxWindowSize;
11219e95205Sopenharmony_ci
11319e95205Sopenharmony_ci    // Valid in Enhanced Retransmission mode, the value should be set to 0.
11419e95205Sopenharmony_ci    uint16_t retransmissionTimeout;
11519e95205Sopenharmony_ci
11619e95205Sopenharmony_ci    // Valid in Enhanced Retransmission mode, the value should be set to 0.
11719e95205Sopenharmony_ci    uint16_t monitorTimeout;
11819e95205Sopenharmony_ci
11919e95205Sopenharmony_ci    // Valid in Enhanced Retransmission mode or Streaming mode, the value should be set to 0.
12019e95205Sopenharmony_ci    uint16_t mps;
12119e95205Sopenharmony_ci} L2capOptionRfc;
12219e95205Sopenharmony_ci
12319e95205Sopenharmony_citypedef struct {
12419e95205Sopenharmony_ci    // The maximum SDU size the sender of this option is capable of accepting for a channel.
12519e95205Sopenharmony_ci    // The minimum value is 48.
12619e95205Sopenharmony_ci    // The default value is 672.
12719e95205Sopenharmony_ci    uint16_t mtu;
12819e95205Sopenharmony_ci
12919e95205Sopenharmony_ci    // The Flush Timeout the sender is going to use.
13019e95205Sopenharmony_ci    // It should be set to default value if profile/protocol does not specially refer.
13119e95205Sopenharmony_ci    // Possible values are,
13219e95205Sopenharmony_ci    // 0x0001 - no retransmissions at the baseband level should be performed.
13319e95205Sopenharmony_ci    // 0x0002 to 0xFFFE - Flush Timeout in milliseconds used by the baseband.
13419e95205Sopenharmony_ci    // 0xFFFF(Default) - an infinite amount of retransmissions.
13519e95205Sopenharmony_ci    uint16_t flushTimeout;
13619e95205Sopenharmony_ci
13719e95205Sopenharmony_ci    // Retransmission and flow control option
13819e95205Sopenharmony_ci    L2capOptionRfc rfc;
13919e95205Sopenharmony_ci
14019e95205Sopenharmony_ci    // Valid in Enhanced Retransmission mode or Streaming mode, specify the type of Frame Check Sequence (FCS).
14119e95205Sopenharmony_ci    // It should be set to default value if profile/protocol does not specially refer.
14219e95205Sopenharmony_ci    // Possible values are,
14319e95205Sopenharmony_ci    // 0x00 - No FCS
14419e95205Sopenharmony_ci    // 0x01(Default) - 16-bit FCS
14519e95205Sopenharmony_ci    uint8_t fcs;
14619e95205Sopenharmony_ci} L2capConfigInfo;
14719e95205Sopenharmony_ci
14819e95205Sopenharmony_citypedef struct {
14919e95205Sopenharmony_ci    BtAddr addr;
15019e95205Sopenharmony_ci    uint16_t handle;
15119e95205Sopenharmony_ci} L2capConnectionInfo;
15219e95205Sopenharmony_ci
15319e95205Sopenharmony_citypedef struct {
15419e95205Sopenharmony_ci    // Connection Request packets received
15519e95205Sopenharmony_ci    void (*recvConnectionReq)(uint16_t lcid, uint8_t id, const L2capConnectionInfo *info, uint16_t lpsm, void *ctx);
15619e95205Sopenharmony_ci
15719e95205Sopenharmony_ci    // Connection Response packet received
15819e95205Sopenharmony_ci    void (*recvConnectionRsp)(
15919e95205Sopenharmony_ci        uint16_t lcid, const L2capConnectionInfo *info, uint16_t result, uint16_t status, void *ctx);
16019e95205Sopenharmony_ci
16119e95205Sopenharmony_ci    // Configuration Request packet received
16219e95205Sopenharmony_ci    void (*recvConfigReq)(uint16_t lcid, uint8_t id, const L2capConfigInfo *cfg, void *ctx);
16319e95205Sopenharmony_ci
16419e95205Sopenharmony_ci    // Configuration Response packet received
16519e95205Sopenharmony_ci    void (*recvConfigRsp)(uint16_t lcid, const L2capConfigInfo *cfg, uint16_t result, void *ctx);
16619e95205Sopenharmony_ci
16719e95205Sopenharmony_ci    // Disconnection Request packet received
16819e95205Sopenharmony_ci    void (*recvDisconnectionReq)(uint16_t lcid, uint8_t id, void *ctx);
16919e95205Sopenharmony_ci
17019e95205Sopenharmony_ci    // Disconnection Response packet received
17119e95205Sopenharmony_ci    void (*recvDisconnectionRsp)(uint16_t lcid, void *ctx);
17219e95205Sopenharmony_ci
17319e95205Sopenharmony_ci    // Disconnected abnormal, such as acl disconnected or link loss
17419e95205Sopenharmony_ci    void (*disconnectAbnormal)(uint16_t lcid, uint8_t reason, void *ctx);
17519e95205Sopenharmony_ci
17619e95205Sopenharmony_ci    // L2cap data packet received
17719e95205Sopenharmony_ci    void (*recvData)(uint16_t lcid, Packet *pkt, void *ctx);
17819e95205Sopenharmony_ci
17919e95205Sopenharmony_ci    // In Enhanced Retransmission mode, when RNR received or tx_window overflow, this callback is generated
18019e95205Sopenharmony_ci    void (*remoteBusy)(uint16_t lcid, uint8_t isBusy, void *ctx);
18119e95205Sopenharmony_ci} L2capService;
18219e95205Sopenharmony_ci
18319e95205Sopenharmony_citypedef struct {
18419e95205Sopenharmony_ci    // Echo Request packet received
18519e95205Sopenharmony_ci    void (*recvEchoReq)(uint16_t aclHandle, uint8_t id, const uint8_t *data, uint16_t dataLen, void *ctx);
18619e95205Sopenharmony_ci    // Echo Response packet received
18719e95205Sopenharmony_ci    void (*recvEchoRsp)(uint16_t aclHandle, const uint8_t *data, uint16_t dataLen, void *ctx);
18819e95205Sopenharmony_ci} L2capEcho;
18919e95205Sopenharmony_ci
19019e95205Sopenharmony_ci// -------------------- Below is for LE ------------------------------
19119e95205Sopenharmony_ci
19219e95205Sopenharmony_ci#define L2CAP_LE_ROLE_MASTER 0x00
19319e95205Sopenharmony_ci#define L2CAP_LE_ROLE_SLAVE 0x01
19419e95205Sopenharmony_ci
19519e95205Sopenharmony_ci#define L2CAP_LE_ATT_CHANNEL 0x0004
19619e95205Sopenharmony_ci#define L2CAP_LE_SMP_CHANNEL 0x0006
19719e95205Sopenharmony_ci
19819e95205Sopenharmony_ci// L2CAP CONNECTION PARAMETER UPDATE RESPONSE result
19919e95205Sopenharmony_ci#define L2CAP_LE_CONNECTION_PARAMETERS_ACCEPTED 0x0000
20019e95205Sopenharmony_ci#define L2CAP_LE_CONNECTION_PARAMETERS_REJECTED 0x0001
20119e95205Sopenharmony_ci
20219e95205Sopenharmony_ci// L2cap Credit Based Connection response result
20319e95205Sopenharmony_ci#define L2CAP_LE_CONNECTION_SUCCESSFUL 0x0000
20419e95205Sopenharmony_ci#define L2CAP_LE_PSM_NOT_SUPPORTED 0x0002
20519e95205Sopenharmony_ci#define L2CAP_LE_NO_RESOURCES_AVAILABLE 0x0004
20619e95205Sopenharmony_ci#define L2CAP_LE_INSUFFICIENT_AUTHENTICATION 0x0005
20719e95205Sopenharmony_ci#define L2CAP_LE_INSUFFICIENT_AUTHORIZATION 0x0006
20819e95205Sopenharmony_ci#define L2CAP_LE_INSUFFICIENT_ENCRYPTION_KEY_SIZE 0x0007
20919e95205Sopenharmony_ci#define L2CAP_LE_INSUFFICIENT_ENCRYPTION 0x0008
21019e95205Sopenharmony_ci#define L2CAP_LE_INVALID_SOURCE_CID 0x0009
21119e95205Sopenharmony_ci#define L2CAP_LE_SOURCE_CID_ALREADY_ALLOCATED 0x000A
21219e95205Sopenharmony_ci#define L2CAP_LE_UNACCEPTABLE_PARAMETERS 0x000B
21319e95205Sopenharmony_ci
21419e95205Sopenharmony_citypedef struct {
21519e95205Sopenharmony_ci    uint16_t mtu;
21619e95205Sopenharmony_ci    uint16_t mps;
21719e95205Sopenharmony_ci    uint16_t credit;
21819e95205Sopenharmony_ci} L2capLeConfigInfo;
21919e95205Sopenharmony_ci
22019e95205Sopenharmony_citypedef struct {
22119e95205Sopenharmony_ci    // LE Credit Based Connection Request packet received
22219e95205Sopenharmony_ci    // Refer to charter 4.22 of Core 5.0
22319e95205Sopenharmony_ci    void (*recvLeCreditBasedConnectionReq)(
22419e95205Sopenharmony_ci        uint16_t lcid, uint8_t id, const L2capConnectionInfo *info, const L2capLeConfigInfo *cfg, void *ctx);
22519e95205Sopenharmony_ci    // LE Credit Based Connection Response packet received
22619e95205Sopenharmony_ci    // Refer to charter 4.23 of Core 5.0
22719e95205Sopenharmony_ci    void (*recvLeCreditBasedConnectionRsp)(
22819e95205Sopenharmony_ci        uint16_t lcid, const L2capConnectionInfo *info, const L2capLeConfigInfo *cfg, uint16_t result, void *ctx);
22919e95205Sopenharmony_ci
23019e95205Sopenharmony_ci    // Disconnection Request packet received
23119e95205Sopenharmony_ci    // Refer to charter 4.6 of Core 5.0
23219e95205Sopenharmony_ci    void (*recvLeDisconnectionReq)(uint16_t lcid, uint8_t id, void *ctx);
23319e95205Sopenharmony_ci    // Disconnection Response packet received
23419e95205Sopenharmony_ci    // Refer to charter 4.7 of Core 5.0
23519e95205Sopenharmony_ci    void (*recvLeDisconnectionRsp)(uint16_t lcid, void *ctx);
23619e95205Sopenharmony_ci
23719e95205Sopenharmony_ci    // Disconnected abnormal, such as le acl disconnected or link loss
23819e95205Sopenharmony_ci    // Refer to HCI specification charter 7.7.5 of Core 5.0
23919e95205Sopenharmony_ci    void (*leDisconnectAbnormal)(uint16_t lcid, uint8_t reason, void *ctx);
24019e95205Sopenharmony_ci
24119e95205Sopenharmony_ci    // LE data packet received
24219e95205Sopenharmony_ci    void (*recvLeData)(uint16_t lcid, Packet *pkt, void *ctx);
24319e95205Sopenharmony_ci
24419e95205Sopenharmony_ci    void (*leRemoteBusy)(uint16_t lcid, uint8_t busy, void *ctx);
24519e95205Sopenharmony_ci} L2capLeService;
24619e95205Sopenharmony_ci
24719e95205Sopenharmony_citypedef struct {
24819e95205Sopenharmony_ci    uint16_t cid;
24919e95205Sopenharmony_ci
25019e95205Sopenharmony_ci    // LE ACL connected
25119e95205Sopenharmony_ci    void (*leConnected)(const BtAddr *addr, uint16_t aclHandle, uint8_t role, uint8_t status);
25219e95205Sopenharmony_ci
25319e95205Sopenharmony_ci    // LE ACL disconnected
25419e95205Sopenharmony_ci    void (*leDisconnected)(uint16_t aclHandle, uint8_t status, uint8_t reason);
25519e95205Sopenharmony_ci
25619e95205Sopenharmony_ci    // LE Fix Channel data received
25719e95205Sopenharmony_ci    void (*recvLeData)(uint16_t aclHandle, const Packet *pkt);
25819e95205Sopenharmony_ci} L2capLeFixChannel;
25919e95205Sopenharmony_ci
26019e95205Sopenharmony_citypedef struct {
26119e95205Sopenharmony_ci    uint16_t connIntervalMin;     // Range: 0x0006 to 0x0C80, Time = N * 1.25 ms, Time Range: 7.5 ms to 4 s.
26219e95205Sopenharmony_ci    uint16_t connIntervalMax;     // Range: 0x0006 to 0x0C80, Time = N * 1.25 ms, Time Range: 7.5 ms to 4 s.
26319e95205Sopenharmony_ci    uint16_t connLatency;         // the range 0 to ((supervisionTimeout / (connIntervalMax * 2)) - 1) less than 500.
26419e95205Sopenharmony_ci    uint16_t supervisionTimeout;  // Range: 0x000A to 0x0C80, Time = N * 10 ms, Time Range: 100 ms to 32 s
26519e95205Sopenharmony_ci} L2capLeConnectionParameter;
26619e95205Sopenharmony_ci
26719e95205Sopenharmony_citypedef struct {
26819e95205Sopenharmony_ci    // Connection Parameter Update Request packet received
26919e95205Sopenharmony_ci    void (*recvLeConnectionParameterUpdateReq)(
27019e95205Sopenharmony_ci        uint16_t aclHandle, uint8_t id, const L2capLeConnectionParameter *param, void *ctx);
27119e95205Sopenharmony_ci
27219e95205Sopenharmony_ci    // Connection Parameter Update Response packet received
27319e95205Sopenharmony_ci    void (*recvLeConnectionParameterUpdateRsp)(uint16_t aclHandle, uint16_t result, void *ctx);
27419e95205Sopenharmony_ci} L2capLeConnectionParameterUpdate;
27519e95205Sopenharmony_ci
27619e95205Sopenharmony_ci#ifdef __cplusplus
27719e95205Sopenharmony_ci}
27819e95205Sopenharmony_ci#endif  // __cplusplus
27919e95205Sopenharmony_ci
28019e95205Sopenharmony_ci#endif  // L2CAP_DEF_H