1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef AVCTP_INT_H
16 #define AVCTP_INT_H
17 
18 #include "avctp.h"
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 /*****************************************************************************
25  * Macro Define
26  ****************************************************************************/
27 #define AVCT_MAX_DEVICES 6   /* The max number of peer devices tha can connected. */
28 #define AVCT_MAX_CONNECTS 14 /* The max number of connect session for app. */
29 
30 /* State define for L2CAP channel */
31 typedef enum {
32     AVCT_CH_IDLE = 0, /* no connection */
33     AVCT_CH_CONN,     /* connection is requested */
34     AVCT_CH_CFG,      /* config state */
35     AVCT_CH_OPENED,   /* opened */
36     AVCT_CH_BUSY,     /* l2cap channel is busy.can't send message */
37 } AvctChSt;
38 
39 /* message packet header length */
40 #define AVCT_PKG_HDR_LEN_SINGLE 3
41 #define AVCT_PKG_HDR_LEN_START 4
42 #define AVCT_PKG_HDR_LEN_CONTINUE 1
43 #define AVCT_PKG_HDR_LEN_END 1
44 
45 /* message packet type */
46 #define AVCT_PKT_TYPE_SINGLE 0   /* single packet */
47 #define AVCT_PKT_TYPE_START 1    /* start packet */
48 #define AVCT_PKT_TYPE_CONTINUE 2 /* continue packet */
49 #define AVCT_PKT_TYPE_END 3      /* end packet */
50 
51 /* Command/Response/REJ indicator. */
52 #define AVCT_CMD 0 /* Command message */
53 #define AVCT_RSP 2 /* Response message */
54 #define AVCT_REJ 3 /* Message rejected */
55 
56 #define AVCT_CONN_UNALLOC 0 /* connect unallocated  */
57 #define AVCT_CONN_ALLOC 1   /* connect allocated  */
58 #define AVCT_CONN_BIND 2    /* connect Binded */
59 
60 #define AVCT_ALLOC_CTRL (1 << 0) /* allocated  ctrl */
61 #define AVCT_ALLOC_BR (1 << 1)   /* allocated br */
62 
63 #define AVCT_L2C_CFG_REQ (1 << 0) /* config requesed */
64 #define AVCT_L2C_CFG_RSP (1 << 1) /* config responsed */
65 
66 #define AVCT_CH_CTRL 0 /* Control channel */
67 #define AVCT_CH_BR 1   /* Browsing channel */
68 
69 #define WAIT_TIME (-1)
70 
71 #define AVCT_PARSE_SIGNLE_HDR(p, label, cr, ipid, pid)        \
72     do {                                                      \
73         (label) = *(p) >> 4;                                  \
74         (cr) = (*(p) >> 1) & 1;                               \
75         (ipid) = (*(p)) & 1;                                  \
76         (pid) = (uint16_t)((p)[1] << 8) + (uint16_t)((p)[2]); \
77     } while (0)
78 
79 #define BT_ADDR_FMT_ASC(addr) addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]
80 #define BT_ADDR_FMT_DSC(addr) addr[5], addr[4], addr[3], addr[2], addr[1], addr[0]
81 
82 #define AVCT_START_PKT_PID_OFFSET 2
83 #define AVCT_START_PKT_PID_SIZE 2
84 
85 #define AVCT_32BYTE 32
86 #define BT_ADDR_LENGTH 6
87 
88 /*****************************************************************************
89  * Data Types
90  ****************************************************************************/
91 /* channel control block type */
92 typedef struct {
93     uint16_t peerMtu;  /* Control l2c mtu from peer device */
94     uint16_t chResult; /* Connection result from L2CAP */
95     uint16_t chId;     /* Channel ID from L2CAP */
96     uint8_t chState;   /* Channel state */
97     uint8_t chCfgSt;   /* Channel config state.There are two state(ind/cfm) */
98     uint8_t stState;   /* The state table state */
99     uint8_t ia;        /* Int(AVCT_INIT) or Acp(AVCT_ACP) */
100     Packet *rxMsg;     /* The point to the message being reassembled */
101 } AvctCbCh;
102 
103 /* device control block type */
104 typedef struct {
105     BtAddr peerAddr;  /* BD address of peer device */
106     AvctCbCh *cbCtrl; /* Associated ctrl channel ControlBlock */
107     AvctCbCh *cbBr;   /* Associated browser channel ControlBlock */
108     uint8_t bindCnt;  /* The count of connection which is binded to the device */
109     uint8_t chLink;   /* 0:unlink, 1: ctrl linked 3:ctrl and br channe has been linked. */
110     bool alloced;     /* false:unalloc, true :alloced */
111 } AvctCbDev;
112 
113 /* connection session control block */
114 typedef struct {
115     AvctConnectParam connParam; /* Connection creation parameters from app  */
116     AvctCbDev *cbDev;           /* The point to the device ControlBlock */
117     uint8_t status;             /* 0:unalloc; 1:alloc;2 :binded */
118     uint8_t connId;             /* Connection Id */
119 } AvctCbConn;
120 
121 /* Channel Management service for AVCT */
122 typedef struct {
123     AvctCbConn cbConn[AVCT_MAX_CONNECTS]; /* Connection control blocks */
124     AvctCbDev cbDev[AVCT_MAX_DEVICES];    /* Device control blocks */
125     uint16_t rxMtu;                       /* Receive MTU for control channel */
126     uint16_t rxMtuBr;                     /* Receive MTU for browsing channel */
127     uint16_t role;
128     bool registered;
129 } AvctChMng;
130 
131 /* data type to send */
132 typedef struct {
133     Packet *buf;        /* Packet buffer point */
134     AvctCbConn *cbConn; /* The point to the connection control block */
135     uint8_t label;      /* Transaction lable from app */
136     uint8_t cr;         /* command / response */
137 } AvctTxMsg;
138 
139 /* state table event data */
140 typedef union {
141     AvctTxMsg txMsg;    /* Message to transfer */
142     Packet *buf;        /* Packet buffer point */
143     AvctCbConn *cbConn; /* The point to the connection control block */
144     uint16_t result;    /* Deal result */
145 } AvctEvtData;
146 
147 typedef struct {
148     BtAddr addr;        /* peer address */
149     uint16_t aclHandle; /* aclHandle */
150     uint16_t lcid;      /* L2CAP link channel ID */
151     uint8_t id;         /* id */
152     uint16_t psm;       /* psm */
153 } AvctRevConnParam;
154 
155 /*****************************************************************************
156  * Globle Variable
157  ****************************************************************************/
158 extern AvctChMng g_avctMng;
159 
160 /*****************************************************************************
161  * Extern Function Declare
162  ****************************************************************************/
163 extern uint8_t AvctAsyncProcess(void (*callback)(void *context), void *context);
164 
165 #ifdef __cplusplus
166 }
167 #endif
168 
169 #endif /* AVCTP_INT_H */