-
-
Notifications
You must be signed in to change notification settings - Fork 214
Characteristic Aggregate Format Descriptor (0x2905) support for Peripheral device #1054
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
srgg
wants to merge
1
commit into
h2zero:master
Choose a base branch
from
srgg:feat/aggregate-format-2905
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+171
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /* | ||
| * Copyright 2020-2025 Ryan Powell <ryan@nable-embedded.io> and | ||
| * esp-nimble-cpp, NimBLE-Arduino contributors. | ||
| * | ||
| * 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. | ||
| */ | ||
|
|
||
| #include "NimBLE2905.h" | ||
| #include "NimBLELog.h" | ||
|
|
||
| #include "NimBLECharacteristic.h" | ||
| #if CONFIG_BT_NIMBLE_ENABLED && MYNEWT_VAL(BLE_ROLE_PERIPHERAL) | ||
|
|
||
| // Define default if not already defined | ||
| #ifndef NIMBLE_MAX_AGGREGATE_FORMAT_DESCRIPTORS | ||
| #define NIMBLE_MAX_AGGREGATE_FORMAT_DESCRIPTORS 5 // Default value | ||
| #else | ||
| // Ensure the value is within a valid range | ||
| #if NIMBLE_MAX_AGGREGATE_FORMAT_DESCRIPTORS < 1 || NIMBLE_MAX_AGGREGATE_FORMAT_DESCRIPTORS > 255 | ||
| #error "NIMBLE_MAX_AGGREGATE_FORMAT_DESCRIPTORS must be between 1 and 128" | ||
| #endif | ||
| #endif | ||
|
|
||
| static const char* LOG_TAG = "NimBLE2905"; | ||
|
|
||
| NimBLE2905::NimBLE2905(NimBLECharacteristic* pChr) | ||
| : NimBLEDescriptor(NimBLEUUID(static_cast<uint16_t>(0x2905)), BLE_GATT_CHR_F_READ, NIMBLE_MAX_AGGREGATE_FORMAT_DESCRIPTORS * sizeof(uint16_t), pChr) { | ||
| } // NimBLE2905 | ||
|
|
||
| void NimBLE2905::initValue() { | ||
| const size_t count = m_vAggregatedDescriptors.size(); | ||
| uint16_t aggregatedHandles[count]; | ||
|
|
||
| for (size_t i = 0; i < count; ++i) { | ||
| auto* desc = m_vAggregatedDescriptors[i]; | ||
| uint16_t handle = desc->getHandle(); | ||
|
|
||
| if (handle == 0) { | ||
| NIMBLE_LOGE(LOG_TAG, "Failed to initialize value: presentation format descriptor handle is not initialized"); | ||
| return; | ||
| } | ||
|
|
||
| aggregatedHandles[i] = desc->getHandle(); | ||
| } // initValue | ||
|
|
||
| setValue(reinterpret_cast<const uint8_t*>(aggregatedHandles), sizeof(aggregatedHandles)); | ||
|
|
||
| // Presentation formats no longer needed, let's free some memory | ||
| m_vAggregatedDescriptors.clear(); | ||
| m_vAggregatedDescriptors.shrink_to_fit(); | ||
| } // initValue | ||
|
|
||
|
|
||
| /** | ||
| * @brief Add presentation format descriptor. | ||
| * @param [in] presentationFormat The 2904 descriptor to aggregate. | ||
| */ | ||
| void NimBLE2905::add2904Descriptor(const NimBLE2904* presentationFormat) { | ||
| if (presentationFormat == nullptr) { | ||
| NIMBLE_LOGE(LOG_TAG, "Failed to add presentation format descriptor: nullptr"); | ||
| return; | ||
| } | ||
|
|
||
| if (m_vAggregatedDescriptors.size() < NIMBLE_MAX_AGGREGATE_FORMAT_DESCRIPTORS) { | ||
| m_vAggregatedDescriptors.push_back(presentationFormat); | ||
| } else { | ||
| NIMBLE_LOGE(LOG_TAG, "Failed to add presentation format descriptor: maximum capacity reached"); | ||
| } | ||
| } // add2904Descriptor | ||
|
|
||
|
|
||
| #endif // CONFIG_BT_NIMBLE_ENABLED && MYNEWT_VAL(BLE_ROLE_PERIPHERAL) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /* | ||
| * Copyright 2020-2025 Ryan Powell <ryan@nable-embedded.io> and | ||
| * esp-nimble-cpp, NimBLE-Arduino contributors. | ||
| * | ||
| * 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. | ||
| */ | ||
|
|
||
| #ifndef NIMBLE_CPP_2905_H_ | ||
| #define NIMBLE_CPP_2905_H_ | ||
|
|
||
| #include "syscfg/syscfg.h" | ||
| #if CONFIG_BT_NIMBLE_ENABLED && MYNEWT_VAL(BLE_ROLE_PERIPHERAL) | ||
|
|
||
| # include "NimBLEDescriptor.h" | ||
|
|
||
| /** | ||
| * @brief Characteristic Aggregate Format descriptor (UUID: 0x2905). | ||
| * | ||
| * @details Contains an ordered list of handles referencing 0x2904 Presentation Format | ||
| * descriptors that define the parent characteristic’s value. | ||
| */ | ||
| class NimBLE2905 : public NimBLEDescriptor { | ||
| public: | ||
| NimBLE2905(NimBLECharacteristic* pChr = nullptr); | ||
|
|
||
| void add2904Descriptor(const NimBLE2904* presentationFormat); | ||
| private: | ||
|
|
||
| /** | ||
| * @brief Descriptor for the Characteristic Aggregate Format (UUID: 0x2905). | ||
| * | ||
| * @details Contains an ordered list of handles referencing the 0x2904 | ||
| * Presentation Format descriptors that define the parent characteristic's value. | ||
| * | ||
| * @see NimBLEServer::start() | ||
| */ | ||
| void initValue(); | ||
|
|
||
| friend class NimBLECharacteristic; | ||
| friend class NimBLEServer; | ||
|
|
||
| std::vector<const NimBLE2904*> m_vAggregatedDescriptors; | ||
| }; // NimBLE2904 | ||
|
|
||
| #endif // CONFIG_BT_NIMBLE_ENABLED && MYNEWT_VAL(BLE_ROLE_PERIPHERAL) | ||
| #endif // NIMBLE_CPP_2904_H_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
Refactor to remove VLA and prevent memory hoarding on early return.
Shiver me timbers! Ye be usin' a Variable-Length Array (VLA) for
aggregatedHandles, which be a compiler extension and not standard C++. Strict compilers will throw ye overboard! Worse yet, if ye hit a handle that's zero, yer earlyreturnskips the memory cleanup below, leavin' ye hoarding memory like a greedy pirate.Let's patch both leaks by using a standard array sized to the maximum capacity and skippin' invalid handles with a
continueinstead of abandonin' ship!🛠️ Proposed refactor for `initValue`
void NimBLE2905::initValue() { - const size_t count = m_vAggregatedDescriptors.size(); - uint16_t aggregatedHandles[count]; - - for (size_t i = 0; i < count; ++i) { - auto* desc = m_vAggregatedDescriptors[i]; - uint16_t handle = desc->getHandle(); - - if (handle == 0) { - NIMBLE_LOGE(LOG_TAG, "Failed to initialize value: presentation format descriptor handle is not initialized"); - return; - } - - aggregatedHandles[i] = desc->getHandle(); - } // initValue - - setValue(reinterpret_cast<const uint8_t*>(aggregatedHandles), sizeof(aggregatedHandles)); + uint16_t aggregatedHandles[NIMBLE_MAX_AGGREGATE_FORMAT_DESCRIPTORS]; + size_t validCount = 0; + + for (auto* desc : m_vAggregatedDescriptors) { + uint16_t handle = desc->getHandle(); + + if (handle == 0) { + NIMBLE_LOGE(LOG_TAG, "Failed to initialize value: presentation format descriptor handle is not initialized"); + continue; + } + + aggregatedHandles[validCount++] = handle; + } + + if (validCount > 0) { + setValue(reinterpret_cast<const uint8_t*>(aggregatedHandles), validCount * sizeof(uint16_t)); + } // Presentation formats no longer needed, let's free some memory m_vAggregatedDescriptors.clear();📝 Committable suggestion
🤖 Prompt for AI Agents