Skip to content

Commit 36e59d6

Browse files
committed
feat(L2CAP): Add disconnect as per #391
1 parent 181c725 commit 36e59d6

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

src/NimBLEL2CAPChannel.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,25 @@ bool NimBLEL2CAPChannel::write(const std::vector<uint8_t>& bytes) {
203203
return true;
204204
}
205205

206+
bool NimBLEL2CAPChannel::disconnect() {
207+
if (!this->channel) {
208+
NIMBLE_LOGW(LOG_TAG, "L2CAP Channel not open");
209+
return false;
210+
}
211+
212+
int rc = ble_l2cap_disconnect(this->channel);
213+
if (rc != 0 && rc != BLE_HS_ENOTCONN && rc != BLE_HS_EALREADY) {
214+
NIMBLE_LOGE(LOG_TAG, "ble_l2cap_disconnect failed: rc=%d %s", rc, NimBLEUtils::returnCodeToString(rc));
215+
return false;
216+
}
217+
218+
return true;
219+
}
220+
221+
uint16_t NimBLEL2CAPChannel::getConnHandle() const {
222+
return ble_l2cap_get_conn_handle(this->channel);
223+
}
224+
206225
// private
207226
int NimBLEL2CAPChannel::handleConnectionEvent(struct ble_l2cap_event* event) {
208227
channel = event->connect.chan;

src/NimBLEL2CAPChannel.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ class NimBLEL2CAPChannel {
5656
/// NOTE: This function will block until the data has been sent or an error occurred.
5757
bool write(const std::vector<uint8_t>& bytes);
5858

59+
/// @brief Disconnect this L2CAP channel.
60+
/// @return true on success, false on failure.
61+
bool disconnect();
62+
63+
/// @brief Get the connection handle associated with this channel.
64+
/// @return Connection handle, or BLE_HS_CONN_HANDLE_NONE if not connected.
65+
uint16_t getConnHandle() const;
66+
5967
/// @return True, if the channel is connected. False, otherwise.
6068
bool isConnected() const { return !!channel; }
6169

0 commit comments

Comments
 (0)