diff --git a/src/NimBLECppVersion.h b/src/NimBLECppVersion.h new file mode 100644 index 000000000..b07edd65b --- /dev/null +++ b/src/NimBLECppVersion.h @@ -0,0 +1,62 @@ +/* + * Copyright 2020-2025 Ryan Powell 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_VERSION_H_ +#define NIMBLE_CPP_VERSION_H_ + +/** @brief NimBLE-Arduino library major version number. */ +#define NIMBLE_CPP_VERSION_MAJOR 2 + +/** @brief NimBLE-Arduino library minor version number. */ +#define NIMBLE_CPP_VERSION_MINOR 3 + +/** @brief NimBLE-Arduino library patch version number. */ +#define NIMBLE_CPP_VERSION_PATCH 9 + +/** + * @brief Macro to create a version number for comparison. + * @param major Major version number. + * @param minor Minor version number. + * @param patch Patch version number. + * @details Example usage: + * @code{.cpp} + * #if NIMBLE_CPP_VERSION >= NIMBLE_CPP_VERSION_VAL(2, 0, 0) + * // Using NimBLE-Arduino v2 or later + * #endif + * @endcode + */ +#define NIMBLE_CPP_VERSION_VAL(major, minor, patch) (((major) << 16) | ((minor) << 8) | (patch)) + +/** + * @brief The NimBLE-Arduino library version as a single integer for compile-time comparison. + * @details Format: (major << 16) | (minor << 8) | patch + */ +#define NIMBLE_CPP_VERSION \ + NIMBLE_CPP_VERSION_VAL(NIMBLE_CPP_VERSION_MAJOR, NIMBLE_CPP_VERSION_MINOR, NIMBLE_CPP_VERSION_PATCH) + +/** @cond NIMBLE_CPP_INTERNAL */ +#define NIMBLE_CPP_VERSION_STRINGIFY_IMPL(x) #x +#define NIMBLE_CPP_VERSION_STRINGIFY(x) NIMBLE_CPP_VERSION_STRINGIFY_IMPL(x) +/** @endcond */ + +/** @brief NimBLE-Arduino library version as a string. */ +#define NIMBLE_CPP_VERSION_STR \ + NIMBLE_CPP_VERSION_STRINGIFY(NIMBLE_CPP_VERSION_MAJOR) "." \ + NIMBLE_CPP_VERSION_STRINGIFY(NIMBLE_CPP_VERSION_MINOR) "." \ + NIMBLE_CPP_VERSION_STRINGIFY(NIMBLE_CPP_VERSION_PATCH) + +#endif // NIMBLE_CPP_VERSION_H_ diff --git a/src/NimBLEDevice.cpp b/src/NimBLEDevice.cpp index 67d9373d9..ecbfcd359 100644 --- a/src/NimBLEDevice.cpp +++ b/src/NimBLEDevice.cpp @@ -1332,6 +1332,14 @@ std::string NimBLEDevice::toString() { return getAddress().toString(); } // toString +/** + * @brief Return the library version as a string. + * @return A const char* containing the version string in the format "major.minor.patch". + */ +const char* NimBLEDevice::getVersion() { + return NIMBLE_CPP_VERSION_STR; +} // getVersion + # if MYNEWT_VAL(NIMBLE_CPP_DEBUG_ASSERT_ENABLED) || __DOXYGEN__ /** * @brief Debug assert - weak function. diff --git a/src/NimBLEDevice.h b/src/NimBLEDevice.h index df63b3dc0..211c05c33 100644 --- a/src/NimBLEDevice.h +++ b/src/NimBLEDevice.h @@ -18,6 +18,7 @@ #ifndef NIMBLE_CPP_DEVICE_H_ #define NIMBLE_CPP_DEVICE_H_ +#include "NimBLECppVersion.h" #include "syscfg/syscfg.h" #if CONFIG_BT_NIMBLE_ENABLED # ifdef ESP_PLATFORM @@ -123,6 +124,7 @@ class NimBLEDevice { static bool isInitialized(); static NimBLEAddress getAddress(); static std::string toString(); + static const char* getVersion(); static bool whiteListAdd(const NimBLEAddress& address); static bool whiteListRemove(const NimBLEAddress& address); static bool onWhiteList(const NimBLEAddress& address);