ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
bluedroid_gattc_order.h
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <cstddef>
5#include <cstdint>
6
8
9namespace esphome::esp32_ble {
10
11static constexpr uint8_t ATT_FIND_INFO_RSP = 0x05;
12static constexpr uint8_t ATT_FIND_BY_TYPE_VALUE_RSP = 0x07;
13static constexpr uint8_t ATT_READ_BY_TYPE_RSP = 0x09;
14static constexpr uint8_t ATT_READ_BY_GRP_TYPE_RSP = 0x11;
15static constexpr size_t ATT_MAX_MTU = 517;
16
18inline void sort_records_by_handle(uint8_t *records, size_t len, size_t size) {
19 const size_t count = len / size;
20 auto handle = [records, size](size_t i) { return encode_uint16(records[i * size + 1], records[i * size]); };
21 for (size_t i = 1; i < count; i++) {
22 const uint16_t current = handle(i);
23 size_t j = i;
24 while (j > 0 && handle(j - 1) > current)
25 j--;
26 if (j != i)
27 std::rotate(records + j * size, records + i * size, records + (i + 1) * size);
28 }
29}
30
33inline void sort_att_response_by_handle(uint8_t op_code, uint8_t *data, size_t len) {
34 if (len < 1 || len >= ATT_MAX_MTU)
35 return;
36 switch (op_code) {
37 case ATT_READ_BY_TYPE_RSP:
38 case ATT_READ_BY_GRP_TYPE_RSP:
39 // Length byte, then records of that length
40 if (data[0] >= 2)
41 sort_records_by_handle(data + 1, len - 1, data[0]);
42 break;
43 case ATT_FIND_INFO_RSP:
44 // Format byte: handle and 16-bit UUID, or handle and 128-bit UUID
45 if (data[0] == 1 || data[0] == 2)
46 sort_records_by_handle(data + 1, len - 1, data[0] == 1 ? 4 : 18);
47 break;
48 case ATT_FIND_BY_TYPE_VALUE_RSP:
49 // Found handle and group end handle, no header
51 break;
52 default:
53 break;
54 }
55}
56
57} // namespace esphome::esp32_ble
unsigned count
Definition gif.cpp:51
void sort_att_response_by_handle(uint8_t op_code, uint8_t *data, size_t len)
Sort the records of an ATT discovery response by handle, in place; data is the PDU after the opcode.
void sort_records_by_handle(uint8_t *records, size_t len, size_t size)
Sort records of size bytes that each start with a little-endian handle, in place; a partial tail is l...
const void size_t len
Definition hal.h:68
uint16_t size
Definition helpers.cpp:34
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:1043
spi_device_handle_t handle