|
| 1 | +/* |
| 2 | + * Copyright 2020-2025 Ryan Powell <ryan@nable-embedded.io> and |
| 3 | + * esp-nimble-cpp, NimBLE-Arduino contributors. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include "NimBLE2905.h" |
| 19 | +#include "NimBLELog.h" |
| 20 | + |
| 21 | +#include "NimBLECharacteristic.h" |
| 22 | +#if CONFIG_BT_NIMBLE_ENABLED && MYNEWT_VAL(BLE_ROLE_PERIPHERAL) |
| 23 | + |
| 24 | +// Define default if not already defined |
| 25 | +#ifndef NIMBLE_MAX_AGGREGATE_FORMAT_DESCRIPTORS |
| 26 | + #define NIMBLE_MAX_AGGREGATE_FORMAT_DESCRIPTORS 5 // Default value |
| 27 | +#else |
| 28 | + // Ensure the value is within a valid range |
| 29 | + #if NIMBLE_MAX_AGGREGATE_FORMAT_DESCRIPTORS < 1 || NIMBLE_MAX_AGGREGATE_FORMAT_DESCRIPTORS > 255 |
| 30 | + #error "NIMBLE_MAX_AGGREGATE_FORMAT_DESCRIPTORS must be between 1 and 128" |
| 31 | + #endif |
| 32 | +#endif |
| 33 | + |
| 34 | +static const char* LOG_TAG = "NimBLE2905"; |
| 35 | + |
| 36 | +NimBLE2905::NimBLE2905(NimBLECharacteristic* pChr) |
| 37 | + : NimBLEDescriptor(NimBLEUUID(static_cast<uint16_t>(0x2905)), BLE_GATT_CHR_F_READ, NIMBLE_MAX_AGGREGATE_FORMAT_DESCRIPTORS * sizeof(uint16_t), pChr) { |
| 38 | +} // NimBLE2905 |
| 39 | + |
| 40 | +void NimBLE2905::initValue() { |
| 41 | + const size_t count = m_vAggregatedDescriptors.size(); |
| 42 | + uint16_t aggregatedHandles[count]; |
| 43 | + |
| 44 | + for (size_t i = 0; i < count; ++i) { |
| 45 | + auto* desc = m_vAggregatedDescriptors[i]; |
| 46 | + uint16_t handle = desc->getHandle(); |
| 47 | + |
| 48 | + if (handle == 0) { |
| 49 | + NIMBLE_LOGE(LOG_TAG, "Failed to initialize value: presentation format descriptor handle is not initialized"); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + aggregatedHandles[i] = desc->getHandle(); |
| 54 | + } // initValue |
| 55 | + |
| 56 | + setValue(reinterpret_cast<const uint8_t*>(aggregatedHandles), sizeof(aggregatedHandles)); |
| 57 | + |
| 58 | + // Presentation formats no longer needed, let's free some memory |
| 59 | + m_vAggregatedDescriptors.clear(); |
| 60 | + m_vAggregatedDescriptors.shrink_to_fit(); |
| 61 | +} // initValue |
| 62 | + |
| 63 | + |
| 64 | +/** |
| 65 | + * @brief Add presentation format descriptor. |
| 66 | + * @param [in] presentationFormat The 2904 descriptor to aggregate. |
| 67 | + */ |
| 68 | +void NimBLE2905::add2904Descriptor(const NimBLE2904* presentationFormat) { |
| 69 | + if (presentationFormat == nullptr) { |
| 70 | + NIMBLE_LOGE(LOG_TAG, "Failed to add presentation format descriptor: nullptr"); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + if (m_vAggregatedDescriptors.size() < NIMBLE_MAX_AGGREGATE_FORMAT_DESCRIPTORS) { |
| 75 | + m_vAggregatedDescriptors.push_back(presentationFormat); |
| 76 | + } else { |
| 77 | + NIMBLE_LOGE(LOG_TAG, "Failed to add presentation format descriptor: maximum capacity reached"); |
| 78 | + } |
| 79 | +} // add2904Descriptor |
| 80 | + |
| 81 | + |
| 82 | +#endif // CONFIG_BT_NIMBLE_ENABLED && MYNEWT_VAL(BLE_ROLE_PERIPHERAL) |
0 commit comments