Skip to content

Commit fd55307

Browse files
Copiloth2zero
andcommitted
Add NimBLECppVersion.h with version macros and runtime version function
Co-authored-by: h2zero <32826625+h2zero@users.noreply.github.com>
1 parent 66522ce commit fd55307

3 files changed

Lines changed: 72 additions & 0 deletions

File tree

src/NimBLECppVersion.h

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
#ifndef NIMBLE_CPP_VERSION_H_
19+
#define NIMBLE_CPP_VERSION_H_
20+
21+
/** @brief NimBLE-Arduino library major version number. */
22+
#define NIMBLE_CPP_VERSION_MAJOR 2
23+
24+
/** @brief NimBLE-Arduino library minor version number. */
25+
#define NIMBLE_CPP_VERSION_MINOR 3
26+
27+
/** @brief NimBLE-Arduino library patch version number. */
28+
#define NIMBLE_CPP_VERSION_PATCH 9
29+
30+
/**
31+
* @brief Macro to create a version number for comparison.
32+
* @param major Major version number.
33+
* @param minor Minor version number.
34+
* @param patch Patch version number.
35+
* @details Example usage:
36+
* @code{.cpp}
37+
* #if NIMBLE_CPP_VERSION >= NIMBLE_CPP_VERSION_VAL(2, 0, 0)
38+
* // Using NimBLE-Arduino v2 or later
39+
* #endif
40+
* @endcode
41+
*/
42+
#define NIMBLE_CPP_VERSION_VAL(major, minor, patch) (((major) << 16) | ((minor) << 8) | (patch))
43+
44+
/**
45+
* @brief The NimBLE-Arduino library version as a single integer for compile-time comparison.
46+
* @details Format: (major << 16) | (minor << 8) | patch
47+
*/
48+
#define NIMBLE_CPP_VERSION \
49+
NIMBLE_CPP_VERSION_VAL(NIMBLE_CPP_VERSION_MAJOR, NIMBLE_CPP_VERSION_MINOR, NIMBLE_CPP_VERSION_PATCH)
50+
51+
/** @cond NIMBLE_CPP_INTERNAL */
52+
#define NIMBLE_CPP_VERSION_STRINGIFY_IMPL(x) #x
53+
#define NIMBLE_CPP_VERSION_STRINGIFY(x) NIMBLE_CPP_VERSION_STRINGIFY_IMPL(x)
54+
/** @endcond */
55+
56+
/** @brief NimBLE-Arduino library version as a string. */
57+
#define NIMBLE_CPP_VERSION_STR \
58+
NIMBLE_CPP_VERSION_STRINGIFY(NIMBLE_CPP_VERSION_MAJOR) "." \
59+
NIMBLE_CPP_VERSION_STRINGIFY(NIMBLE_CPP_VERSION_MINOR) "." \
60+
NIMBLE_CPP_VERSION_STRINGIFY(NIMBLE_CPP_VERSION_PATCH)
61+
62+
#endif // NIMBLE_CPP_VERSION_H_

src/NimBLEDevice.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,6 +1332,14 @@ std::string NimBLEDevice::toString() {
13321332
return getAddress().toString();
13331333
} // toString
13341334

1335+
/**
1336+
* @brief Return the library version as a string.
1337+
* @return A const char* containing the version string in the format "major.minor.patch".
1338+
*/
1339+
const char* NimBLEDevice::getVersion() {
1340+
return NIMBLE_CPP_VERSION_STR;
1341+
} // getVersion
1342+
13351343
# if MYNEWT_VAL(NIMBLE_CPP_DEBUG_ASSERT_ENABLED) || __DOXYGEN__
13361344
/**
13371345
* @brief Debug assert - weak function.

src/NimBLEDevice.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#ifndef NIMBLE_CPP_DEVICE_H_
1919
#define NIMBLE_CPP_DEVICE_H_
2020

21+
#include "NimBLECppVersion.h"
2122
#include "syscfg/syscfg.h"
2223
#if CONFIG_BT_NIMBLE_ENABLED
2324
# ifdef ESP_PLATFORM
@@ -123,6 +124,7 @@ class NimBLEDevice {
123124
static bool isInitialized();
124125
static NimBLEAddress getAddress();
125126
static std::string toString();
127+
static const char* getVersion();
126128
static bool whiteListAdd(const NimBLEAddress& address);
127129
static bool whiteListRemove(const NimBLEAddress& address);
128130
static bool onWhiteList(const NimBLEAddress& address);

0 commit comments

Comments
 (0)