blob: 76246b55a6a9b756fd4b6ff8c121b2db2bfabbc6 [file] [log] [blame]
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/connectivity/bluetooth/core/bt-host/l2cap/low_energy_command_handler.h"
namespace bt::l2cap::internal {
bool LowEnergyCommandHandler::ConnectionParameterUpdateResponse::Decode(
const ByteBuffer& payload_buf) {
const auto result = letoh16(payload_buf.ReadMember<&PayloadT::result>());
result_ = ConnectionParameterUpdateResult{result};
return true;
}
LowEnergyCommandHandler::ConnectionParameterUpdateResponder::ConnectionParameterUpdateResponder(
SignalingChannel::Responder* sig_responder)
: Responder(sig_responder) {}
void LowEnergyCommandHandler::ConnectionParameterUpdateResponder::Send(
ConnectionParameterUpdateResult result) {
ConnectionParameterUpdateResponsePayload payload;
payload.result = ConnectionParameterUpdateResult{htole16(result)};
sig_responder_->Send(BufferView(&payload, sizeof(payload)));
}
LowEnergyCommandHandler::LowEnergyCommandHandler(SignalingChannelInterface* sig,
fit::closure request_fail_callback)
: CommandHandler(sig, std::move(request_fail_callback)) {}
bool LowEnergyCommandHandler::SendConnectionParameterUpdateRequest(
uint16_t interval_min, uint16_t interval_max, uint16_t peripheral_latency,
uint16_t timeout_multiplier, ConnectionParameterUpdateResponseCallback cb) {
auto on_param_update_rsp = BuildResponseHandler<ConnectionParameterUpdateResponse>(std::move(cb));
ConnectionParameterUpdateRequestPayload payload;
payload.interval_min = htole16(interval_min);
payload.interval_max = htole16(interval_max);
payload.peripheral_latency = htole16(peripheral_latency);
payload.timeout_multiplier = htole16(timeout_multiplier);
return sig()->SendRequest(kConnectionParameterUpdateRequest,
BufferView(&payload, sizeof(payload)), std::move(on_param_update_rsp));
}
void LowEnergyCommandHandler::ServeConnectionParameterUpdateRequest(
ConnectionParameterUpdateRequestCallback cb) {
auto on_param_update_req = [cb = std::move(cb)](const ByteBuffer& request_payload,
SignalingChannel::Responder* sig_responder) {
if (request_payload.size() != sizeof(ConnectionParameterUpdateRequestPayload)) {
bt_log(DEBUG, "l2cap-le",
"cmd: rejecting malformed Connection Parameter Update Request, size %zu",
request_payload.size());
sig_responder->RejectNotUnderstood();
return;
}
const auto& req = request_payload.To<ConnectionParameterUpdateRequestPayload>();
const auto interval_min = letoh16(req.interval_min);
const auto interval_max = letoh16(req.interval_max);
const auto peripheral_latency = letoh16(req.peripheral_latency);
const auto timeout_multiplier = letoh16(req.timeout_multiplier);
ConnectionParameterUpdateResponder responder(sig_responder);
cb(interval_min, interval_max, peripheral_latency, timeout_multiplier, &responder);
};
sig()->ServeRequest(kConnectionParameterUpdateRequest, std::move(on_param_update_req));
}
} // namespace bt::l2cap::internal