1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
/*
* Copyright 2020 HIMSA II K/S - www.himsa.com. Represented by EHIMA -
* www.ehima.com
*
* 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.
*/
#pragma once
#include <queue>
#include "stack/gatt/gatt_int.h"
#include "types/raw_address.h"
#define EATT_MIN_MTU_MPS (64)
#define EATT_DEFAULT_MTU (256)
namespace bluetooth {
namespace eatt {
/* Enums */
enum class EattChannelState : uint8_t {
EATT_CHANNEL_PENDING = 0x00,
EATT_CHANNEL_OPENED,
EATT_CHANNEL_RECONFIGURING,
};
class EattChannel {
public:
/* Pointer to EattDevice */
RawAddress bda_;
uint16_t cid_;
uint16_t tx_mtu_;
uint16_t rx_mtu_;
EattChannelState state_;
/* Used to keep server commands */
tGATT_SR_CMD server_outstanding_cmd_;
/* Used to veryfy indication confirmation*/
uint16_t indicate_handle_;
/* local app confirm to indication timer */
alarm_t* ind_ack_timer_;
/* indication confirmation timer */
alarm_t* ind_confirmation_timer_;
/* GATT client command queue */
std::queue<tGATT_CMD_Q> cl_cmd_q_;
EattChannel(RawAddress& bda, uint16_t cid, uint16_t tx_mtu, uint16_t rx_mtu)
: bda_(bda),
cid_(cid),
tx_mtu_(tx_mtu),
rx_mtu_(rx_mtu),
state_(EattChannelState::EATT_CHANNEL_PENDING),
indicate_handle_(0),
ind_ack_timer_(NULL),
ind_confirmation_timer_(NULL) {}
~EattChannel() {
if (ind_ack_timer_ != NULL) {
alarm_free(ind_ack_timer_);
}
if (ind_confirmation_timer_ != NULL) {
alarm_free(ind_confirmation_timer_);
}
}
void EattChannelSetState(EattChannelState state) {
if (state_ == EattChannelState::EATT_CHANNEL_PENDING) {
if (state == EattChannelState::EATT_CHANNEL_OPENED) {
cl_cmd_q_ = std::queue<tGATT_CMD_Q>();
memset(&server_outstanding_cmd_, 0, sizeof(tGATT_SR_CMD));
char name[64];
sprintf(name, "eatt_ind_ack_timer_%s_cid_0x%04x",
bda_.ToString().c_str(), cid_);
ind_ack_timer_ = alarm_new(name);
sprintf(name, "eatt_ind_conf_timer_%s_cid_0x%04x",
bda_.ToString().c_str(), cid_);
ind_confirmation_timer_ = alarm_new(name);
}
}
state_ = state;
}
void EattChannelSetTxMTU(uint16_t tx_mtu) { this->tx_mtu_ = tx_mtu; }
};
/* Interface class */
class EattExtension {
public:
EattExtension();
EattExtension(const EattExtension&) = delete;
EattExtension& operator=(const EattExtension&) = delete;
virtual ~EattExtension();
static EattExtension* GetInstance() {
static EattExtension* instance = new EattExtension();
return instance;
}
static void AddFromStorage(const RawAddress& bd_addr);
/**
* Checks if EATT is supported on peer device.
*
* @param bd_addr peer device address
*/
virtual bool IsEattSupportedByPeer(const RawAddress& bd_addr);
/**
* Connect at maximum 5 EATT channels to peer device.
*
* @param bd_addr peer device address
*/
virtual void Connect(const RawAddress& bd_addr);
/**
* Disconnect all EATT channels to peer device.
*
* @param bd_addr peer device address
*/
virtual void Disconnect(const RawAddress& bd_addr);
/**
* Reconfigure EATT channel for give CID
*
* @param bd_addr peer device address
* @param cid channel id
* @param mtu new maximum transmit unit available of local device
*/
virtual void Reconfigure(const RawAddress& bd_addr, uint16_t cid,
uint16_t mtu);
/**
* Reconfigure all EATT channels to peer device.
*
* @param bd_addr peer device address
* @param mtu new maximum transmit unit available of local device
*/
virtual void ReconfigureAll(const RawAddress& bd_addr, uint16_t mtu);
/* Below methods required by GATT implementation */
/**
* Find EATT channel by cid.
*
* @param bd_addr peer device address
* @param cid channel id
*
* @return Eatt Channel instance.
*/
virtual EattChannel* FindEattChannelByCid(const RawAddress& bd_addr,
uint16_t cid);
/**
* Find EATT channel by transaction id.
*
* @param bd_addr peer device address
* @param trans_id transaction id
*
* @return pointer to EATT channel.
*/
virtual EattChannel* FindEattChannelByTransId(const RawAddress& bd_addr,
uint32_t trans_id);
/**
* Check if EATT channel on given handle is waiting for a indication
* confirmation
*
* @param bd_addr peer device address
* @param indication_handle handle of the pending indication
*
* @return true if confirmation is pending false otherwise
*/
virtual bool IsIndicationPending(const RawAddress& bd_addr,
uint16_t indication_handle);
/**
* Get EATT channel available for indication.
*
* @param bd_addr peer device address
*
* @return pointer to EATT channel.
*/
virtual EattChannel* GetChannelAvailableForIndication(
const RawAddress& bd_addr);
/**
* Free Resources.
*
* (Maybe not needed)
* @param bd_addr peer device address
*
*/
virtual void FreeGattResources(const RawAddress& bd_addr);
/**
* Check if there is any EATT channels having some msg in its send queue
*
* @param bd_addr peer device address
*
* @return true when there is at least one EATT channel ready to send
*/
virtual bool IsOutstandingMsgInSendQueue(const RawAddress& bd_addr);
/**
* Get EATT channel ready to send.
*
* @param bd_addr peer device address
*
* @return pointer to EATT channel.
*/
virtual EattChannel* GetChannelWithQueuedData(const RawAddress& bd_addr);
/**
* Get EATT channel available to send GATT request.
*
* @param bd_addr peer device address
*
* @return pointer to EATT channel.
*/
virtual EattChannel* GetChannelAvailableForClientRequest(
const RawAddress& bd_addr);
/**
* Start GATT indication timer per CID.
*
* @param bd_addr peer device address
* @param cid channel id
*/
virtual void StartIndicationConfirmationTimer(const RawAddress& bd_addr,
uint16_t cid);
/**
* Stop GATT indication timer per CID.
*
* @param bd_addr peer device address
* @param cid channel id
*/
virtual void StopIndicationConfirmationTimer(const RawAddress& bd_addr,
uint16_t cid);
/**
* Start application time for incoming indication on given CID
*
* @param bd_addr peer device address
* @param cid channel id
*/
virtual void StartAppIndicationTimer(const RawAddress& bd_addr, uint16_t cid);
/**
* Stop application time for incoming indication on given CID
*
* @param bd_addr peer device address
* @param cid channel id
*/
virtual void StopAppIndicationTimer(const RawAddress& bd_addr, uint16_t cid);
/**
* Starts the EattExtension module
*/
void Start();
/**
* Stops the EattExtension module
*/
void Stop();
private:
struct impl;
std::unique_ptr<impl> pimpl_;
};
} // namespace eatt
} // namespace bluetooth
|