Skip to content

Commit a672ecd

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 3aace03 commit a672ecd

3 files changed

Lines changed: 93 additions & 1 deletion

File tree

src/NimBLECppVersion.h

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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 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+
/**
57+
* @brief Optional Semantic Versioning prerelease suffix.
58+
* @details Include the leading '-' when defined, for example: "-beta.1"
59+
*/
60+
#ifndef NIMBLE_CPP_VERSION_PRERELEASE
61+
# define NIMBLE_CPP_VERSION_PRERELEASE ""
62+
#endif
63+
64+
/**
65+
* @brief Optional Semantic Versioning build metadata suffix.
66+
* @details Include the leading '+' when defined, for example: "+sha.abcd1234"
67+
*/
68+
#ifndef NIMBLE_CPP_VERSION_BUILD_METADATA
69+
# define NIMBLE_CPP_VERSION_BUILD_METADATA ""
70+
#endif
71+
72+
/** @brief library version as a prefixed Semantic Versioning string. */
73+
#define NIMBLE_CPP_VERSION_STR \
74+
"NimBLE-CPP " \
75+
NIMBLE_CPP_VERSION_STRINGIFY(NIMBLE_CPP_VERSION_MAJOR) "." \
76+
NIMBLE_CPP_VERSION_STRINGIFY(NIMBLE_CPP_VERSION_MINOR) "." \
77+
NIMBLE_CPP_VERSION_STRINGIFY(NIMBLE_CPP_VERSION_PATCH) \
78+
NIMBLE_CPP_VERSION_PRERELEASE NIMBLE_CPP_VERSION_BUILD_METADATA
79+
80+
#endif // NIMBLE_CPP_VERSION_H_

src/NimBLEDevice.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,7 @@ void NimBLEDevice::onSync(void) {
864864
* @brief The main host task.
865865
*/
866866
void NimBLEDevice::host_task(void* param) {
867-
NIMBLE_LOGI(LOG_TAG, "BLE Host Task Started");
867+
NIMBLE_LOGI(LOG_TAG, "NimBLE Started!");
868868
nimble_port_run(); // This function will return only when nimble_port_stop() is executed
869869
nimble_port_freertos_deinit();
870870
} // host_task
@@ -875,6 +875,7 @@ void NimBLEDevice::host_task(void* param) {
875875
*/
876876
bool NimBLEDevice::init(const std::string& deviceName) {
877877
if (!m_initialized) {
878+
NIMBLE_LOGD(LOG_TAG, "Starting %s", getVersion());
878879
# ifdef ESP_PLATFORM
879880

880881
# if defined(CONFIG_ENABLE_ARDUINO_DEPENDS) && SOC_BT_SUPPORTED
@@ -996,6 +997,7 @@ bool NimBLEDevice::init(const std::string& deviceName) {
996997
}
997998

998999
m_initialized = true; // Set the initialization flag to ensure we are only initialized once.
1000+
NIMBLE_LOGD(LOG_TAG, "Initialized");
9991001
return true;
10001002
} // init
10011003

@@ -1326,6 +1328,14 @@ std::string NimBLEDevice::toString() {
13261328
return getAddress().toString();
13271329
} // toString
13281330

1331+
/**
1332+
* @brief Return the library version as a string.
1333+
* @return A const char* containing library version information.
1334+
*/
1335+
const char* NimBLEDevice::getVersion() {
1336+
return NIMBLE_CPP_VERSION_STR;
1337+
} // getVersion
1338+
13291339
# if MYNEWT_VAL(NIMBLE_CPP_DEBUG_ASSERT_ENABLED) || __DOXYGEN__
13301340
/**
13311341
* @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)