|
ELEC-C7222
Libraries for ELEC C7222 Course Work
|
This library wraps BTstack with C++ classes to provide a more structured, object‑oriented BLE API while keeping BTstack’s behavior and constraints visible. It is intentionally thin: it does not hide the underlying ATT/GATT and HCI concepts, and it expects the application to manage event dispatch and object lifetimes carefully.
BTstack configuration for this library lives under ble/platform/rpi_pico/config/. The main configuration file is btstack_config.h.
HAVE_FREERTOS_TASK_NOTIFICATIONSENABLE_LE_PERIPHERALENABLE_LE_CENTRAL (when RUNNING_AS_CLIENT is set)ENABLE_LOG_INFOENABLE_LOG_ERRORENABLE_PRINTF_HEXDUMPENABLE_SOFTWARE_AES128ENABLE_MICRO_ECC_FOR_LE_SECURE_CONNECTIONSENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROLHAVE_EMBEDDED_TIME_MSHAVE_ASSERTMAX_ATT_DB_SIZE (fixed-size ATT DB)MAX_NR_HCI_CONNECTIONSMAX_NR_GATT_CLIENTS (depends on RUNNING_AS_CLIENT)MAX_NR_SM_LOOKUP_ENTRIESMAX_NR_WHITELIST_ENTRIESMAX_NR_LE_DEVICE_DB_ENTRIESMAX_NR_CONTROLLER_ACL_BUFFERSMAX_NR_CONTROLLER_SCO_PACKETSHCI_OUTGOING_PRE_BUFFER_SIZEHCI_ACL_PAYLOAD_SIZEHCI_ACL_CHUNK_SIZE_ALIGNMENTHCI_HOST_ACL_PACKET_LEN, HCI_HOST_ACL_PACKET_NUMHCI_HOST_SCO_PACKET_LEN, HCI_HOST_SCO_PACKET_NUMNVM_NUM_DEVICE_DB_ENTRIESNVM_NUM_LINK_KEYSHCI_RESET_RESEND_TIMEOUT_MSplatform/ while the core logic stays platform‑agnostic.c7222::Ble is the top‑level singleton facade and owns access to the other singletons.c7222::Gap handles advertising, connection state, and GAP events. See gap.md.c7222::AttributeServer parses the ATT database and routes attribute reads/writes. See gatt.md.c7222::SecurityManager configures pairing/encryption and dispatches security events. See security-manager.md.BleAddress, BleError, Uuid) provide shared protocol data types and error mapping.Ble, Gap, AttributeServer, and SecurityManager are singletons.att_db.h/att_db.c) generated from .gatt files. See creating-profiles.md for the project’s GATT‑file guidance.c7222::AttributeServer::Init(context) expects a platform‑specific context pointer. On Pico W, this is the ATT database blob.AttributeServer parses the ATT database into Service, Characteristic, and Attribute objects.Attribute represents a single ATT database entry (UUID, properties, handle, value).Characteristic wraps a Declaration, Value, and optional descriptors (CCCD/SCCD/User Description, etc.).Service groups characteristics and service‑level attributes.Characteristic uses internal read/write handlers and forwards higher‑level events to registered EventHandler instances.SecurityManager configures IO capability, authentication requirements, bonding, and key size ranges.AttributeServer caches security state and uses it to validate descriptor writes (e.g., CCCD/SCCD).c7222::Ble::DispatchBleHciPacket().Ble fans out events to GAP, AttributeServer, and SecurityManager as appropriate.libs/elec_c7222/ble/platform/rpi_pico/libs/elec_c7222/ble/gap/platform/rpi_pico/libs/elec_c7222/ble/gatt/platform/rpi_pico/libs/elec_c7222/ble/security_manager/platform/rpi_pico/AttributeServer. Multiple connections are not supported reliably.SecurityManager.c7222::Ble::TurnOn().c7222::Ble::DispatchBleHciPacket().platform/rpi_pico provides the real BTstack integration for Pico W.platform/grader offers a lightweight stub for grading/testing..gatt changes regenerate the ATT database header and that clients clear their GATT cache..gatt files.The project uses a custom C++ layer to wrap the raw BTstack ATT callbacks into an object-oriented structure. See module‑specific documentation for full details:
libs/elec_c7222/ble/doc/markdown/gap.mdlibs/elec_c7222/ble/doc/markdown/gatt.mdlibs/elec_c7222/ble/doc/markdown/security-manager.mdcompile_gatt.py produces a binary array profile_db. c7222::AttributeServer::Init (libs/elec_c7222/ble/gatt/platform/rpi_pico/attribute_server.cpp) receives this pointer and registers it with the BTstack core via att_server_init(...).db + 1 (BTstack reserves the first byte).entry_size (uint16, little-endian). A size of 0 terminates the list.flags (uint16), handle (uint16), then UUID (2 or 16 bytes).ParseEntry() constructs an Attribute from (uuid, flags, value_ptr, value_len, handle).InitServices() calls c7222::Service::ParseFromAttributes(), which walks the ordered list.c7222::Characteristic::ParseFromAttributes() groups:att_read_callback / att_write_callback.AttributeServer finds the matching handle:Characteristic, it calls c7222::Characteristic::HandleAttributeRead/Write().c7222::Attribute::InvokeRead/WriteCallback().BleError back to BTstack ATT error codes.There are two main handler families: low-level attribute callbacks and high-level characteristic events.
1) Attribute callbacks (data access + validation)
Registered on Attribute instances:
c7222::Attribute::SetReadCallback(ReadCallback) c7222::Attribute::SetWriteCallback(WriteCallback) Where used:
Notes:
InvokeReadCallback() returns stored bytes for static or dynamic attributes.BleError.2) Characteristic EventHandlers (semantic events)
Registered via c7222::Characteristic::AddEventHandler(EventHandlers&):
OnUpdatesEnabled(bool is_indication) / OnUpdatesDisabled() OnBroadcastEnabled() / OnBroadcastDisabled() OnRead() SetValue().OnWrite(const std::vector<uint8_t>& data) OnIndicationComplete(uint8_t status) / OnConfirmationReceived(bool status) DispatchBleHciPacket() being fed HCI packets.Notes:
SetReadCallback()/SetWriteCallback(), the default handlers (and therefore OnRead/OnWrite) are bypassed.