Skip to content

Commit 8db73d6

Browse files
committed
Add NimBLE source files
1 parent 940a896 commit 8db73d6

23 files changed

+10795
-0
lines changed

libraries/BLE/src/NimBLE2904.cpp

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2020-2024 Ryan Powell <[email protected]> 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 "sdkconfig.h"
19+
#if defined(CONFIG_NIMBLE_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
20+
21+
#include "BLE2904.h"
22+
23+
BLE2904::BLE2904(BLECharacteristic *pChr) : BLEDescriptor(BLEUUID((uint16_t)0x2904), BLE_GATT_CHR_F_READ, sizeof(BLE2904Data), pChr) {
24+
setValue(m_data);
25+
} // BLE2904
26+
27+
/**
28+
* @brief Set the description.
29+
* @param [in] description The description value to set.
30+
*/
31+
void BLE2904::setDescription(uint16_t description) {
32+
m_data.m_description = description;
33+
setValue(m_data);
34+
} // setDescription
35+
36+
/**
37+
* @brief Set the exponent.
38+
* @param [in] exponent The exponent value to set.
39+
*/
40+
void BLE2904::setExponent(int8_t exponent) {
41+
m_data.m_exponent = exponent;
42+
setValue(m_data);
43+
} // setExponent
44+
45+
/**
46+
* @brief Set the format.
47+
* @param [in] format The format value to set.
48+
*/
49+
void BLE2904::setFormat(uint8_t format) {
50+
m_data.m_format = format;
51+
setValue(m_data);
52+
} // setFormat
53+
54+
/**
55+
* @brief Set the namespace.
56+
* @param [in] namespace_value The namespace value toset.
57+
*/
58+
void BLE2904::setNamespace(uint8_t namespace_value) {
59+
m_data.m_namespace = namespace_value;
60+
setValue(m_data);
61+
} // setNamespace
62+
63+
/**
64+
* @brief Set the units for this value.
65+
* @param [in] unit The type of units of this characteristic as defined by assigned numbers.
66+
* @details See https://www.bluetooth.com/specifications/assigned-numbers/units
67+
*/
68+
void BLE2904::setUnit(uint16_t unit) {
69+
m_data.m_unit = unit;
70+
setValue(m_data);
71+
} // setUnit
72+
73+
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_PERIPHERAL */

libraries/BLE/src/NimBLEAddress.cpp

+239
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
/*
2+
* Copyright 2020-2024 Ryan Powell <[email protected]> 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 "sdkconfig.h"
19+
#if defined(CONFIG_NIMBLE_ENABLED)
20+
21+
#include "BLEAddress.h"
22+
23+
#include <algorithm>
24+
25+
#ifdef CONFIG_NIMBLE_CPP_ADDR_FMT_EXCLUDE_DELIMITER
26+
#define NIMBLE_CPP_ADDR_DELIMITER ""
27+
#else
28+
#define NIMBLE_CPP_ADDR_DELIMITER ":"
29+
#endif
30+
31+
#ifdef CONFIG_NIMBLE_CPP_ADDR_FMT_UPPERCASE
32+
#define NIMBLE_CPP_ADDR_FMT "%02X%s%02X%s%02X%s%02X%s%02X%s%02X"
33+
#else
34+
#define NIMBLE_CPP_ADDR_FMT "%02x%s%02x%s%02x%s%02x%s%02x%s%02x"
35+
#endif
36+
37+
static const char *LOG_TAG = "BLEAddress";
38+
39+
/*************************************************
40+
* NOTE: BLE address bytes are in INVERSE ORDER!
41+
* We will accommodate that fact in these methods.
42+
*************************************************/
43+
44+
/**
45+
* @brief Create an address from the native BLE representation.
46+
* @param [in] address The native BLE address.
47+
*/
48+
BLEAddress::BLEAddress(ble_addr_t address) : ble_addr_t{address} {}
49+
50+
/**
51+
* @brief Create an address from a hex string.
52+
*
53+
* A hex string is of the format:
54+
* ```
55+
* 00:00:00:00:00:00
56+
* ```
57+
* which is 17 characters in length.
58+
* @param [in] addr The hex string representation of the address.
59+
* @param [in] type The type of the address, should be one of:
60+
* * BLE_ADDR_PUBLIC (0)
61+
* * BLE_ADDR_RANDOM (1)
62+
*/
63+
BLEAddress::BLEAddress(const std::string &addr, uint8_t type) {
64+
this->type = type;
65+
66+
if (addr.length() == BLE_DEV_ADDR_LEN) {
67+
std::reverse_copy(addr.data(), addr.data() + BLE_DEV_ADDR_LEN, this->val);
68+
return;
69+
}
70+
71+
if (addr.length() == 17) {
72+
std::string mac{addr};
73+
mac.erase(std::remove(mac.begin(), mac.end(), ':'), mac.end());
74+
uint64_t address = std::stoull(mac, nullptr, 16);
75+
memcpy(this->val, &address, sizeof(this->val));
76+
return;
77+
}
78+
79+
*this = BLEAddress{};
80+
log_e(LOG_TAG, "Invalid address '%s'", addr.c_str());
81+
} // BLEAddress
82+
83+
/**
84+
* @brief Constructor for compatibility with bluedroid esp library using native ESP representation.
85+
* @param [in] address A uint8_t[6] or esp_bd_addr_t containing the address.
86+
* @param [in] type The type of the address should be one of:
87+
* * BLE_ADDR_PUBLIC (0)
88+
* * BLE_ADDR_RANDOM (1)
89+
*/
90+
BLEAddress::BLEAddress(const uint8_t address[BLE_DEV_ADDR_LEN], uint8_t type) {
91+
std::reverse_copy(address, address + BLE_DEV_ADDR_LEN, this->val);
92+
this->type = type;
93+
} // BLEAddress
94+
95+
/**
96+
* @brief Constructor for address using a hex value.\n
97+
* Use the same byte order, so use 0xa4c1385def16 for "a4:c1:38:5d:ef:16"
98+
* @param [in] address uint64_t containing the address.
99+
* @param [in] type The type of the address should be one of:
100+
* * BLE_ADDR_PUBLIC (0)
101+
* * BLE_ADDR_RANDOM (1)
102+
*/
103+
BLEAddress::BLEAddress(const uint64_t &address, uint8_t type) {
104+
memcpy(this->val, &address, sizeof(this->val));
105+
this->type = type;
106+
} // BLEAddress
107+
108+
/**
109+
* @brief Determine if this address equals another.
110+
* @param [in] otherAddress The other address to compare against.
111+
* @return True if the addresses are equal.
112+
*/
113+
bool BLEAddress::equals(const BLEAddress &otherAddress) const {
114+
return *this == otherAddress;
115+
} // equals
116+
117+
/**
118+
* @brief Get the BLE base struct of the address.
119+
* @return A read only reference to the BLE base struct of the address.
120+
*/
121+
const ble_addr_t *BLEAddress::getBase() const {
122+
return reinterpret_cast<const ble_addr_t *>(this);
123+
} // getBase
124+
125+
/**
126+
* @brief Get the address type.
127+
* @return The address type.
128+
*/
129+
uint8_t BLEAddress::getType() const {
130+
return this->type;
131+
} // getType
132+
133+
/**
134+
* @brief Get the address value.
135+
* @return A read only reference to the address value.
136+
*/
137+
const uint8_t *BLEAddress::getVal() const {
138+
return this->val;
139+
} // getVal
140+
141+
/**
142+
* @brief Determine if this address is a Resolvable Private Address.
143+
* @return True if the address is a RPA.
144+
*/
145+
bool BLEAddress::isRpa() const {
146+
return BLE_ADDR_IS_RPA(this);
147+
} // isRpa
148+
149+
/**
150+
* @brief Determine if this address is a Non-Resolvable Private Address.
151+
* @return True if the address is a NRPA.
152+
*/
153+
bool BLEAddress::isNrpa() const {
154+
return BLE_ADDR_IS_NRPA(this);
155+
} // isNrpa
156+
157+
/**
158+
* @brief Determine if this address is a Static Address.
159+
* @return True if the address is a Static Address.
160+
*/
161+
bool BLEAddress::isStatic() const {
162+
return BLE_ADDR_IS_STATIC(this);
163+
} // isStatic
164+
165+
/**
166+
* @brief Determine if this address is a Public Address.
167+
* @return True if the address is a Public Address.
168+
*/
169+
bool BLEAddress::isPublic() const {
170+
return this->type == BLE_ADDR_PUBLIC;
171+
} // isPublic
172+
173+
/**
174+
* @brief Determine if this address is a NULL Address.
175+
* @return True if the address is a NULL Address.
176+
*/
177+
bool BLEAddress::isNull() const {
178+
return *this == BLEAddress{};
179+
} // isNull
180+
181+
/**
182+
* @brief Convert a BLE address to a string.
183+
* @return The string representation of the address.
184+
* @deprecated Use std::string() operator instead.
185+
*/
186+
std::string BLEAddress::toString() const {
187+
return std::string(*this);
188+
} // toString
189+
190+
/**
191+
* @brief Reverse the byte order of the address.
192+
* @return A reference to this address.
193+
*/
194+
const BLEAddress &BLEAddress::reverseByteOrder() {
195+
std::reverse(this->val, this->val + BLE_DEV_ADDR_LEN);
196+
return *this;
197+
} // reverseByteOrder
198+
199+
/**
200+
* @brief Convenience operator to check if this address is equal to another.
201+
*/
202+
bool BLEAddress::operator==(const BLEAddress &rhs) const {
203+
if (this->type != rhs.type) {
204+
return false;
205+
}
206+
207+
return memcmp(rhs.val, this->val, sizeof(this->val)) == 0;
208+
} // operator ==
209+
210+
/**
211+
* @brief Convenience operator to check if this address is not equal to another.
212+
*/
213+
bool BLEAddress::operator!=(const BLEAddress &rhs) const {
214+
return !this->operator==(rhs);
215+
} // operator !=
216+
217+
/**
218+
* @brief Convenience operator to convert this address to string representation.
219+
* @details This allows passing BLEAddress to functions that accept std::string and/or it's methods as a parameter.
220+
*/
221+
BLEAddress::operator std::string() const {
222+
char buffer[18];
223+
snprintf(
224+
buffer, sizeof(buffer), NIMBLE_CPP_ADDR_FMT, this->val[5], NIMBLE_CPP_ADDR_DELIMITER, this->val[4], NIMBLE_CPP_ADDR_DELIMITER, this->val[3],
225+
NIMBLE_CPP_ADDR_DELIMITER, this->val[2], NIMBLE_CPP_ADDR_DELIMITER, this->val[1], NIMBLE_CPP_ADDR_DELIMITER, this->val[0]
226+
);
227+
return std::string{buffer};
228+
} // operator std::string
229+
230+
/**
231+
* @brief Convenience operator to convert the native address representation to uint_64.
232+
*/
233+
BLEAddress::operator uint64_t() const {
234+
uint64_t address = 0;
235+
memcpy(&address, this->val, sizeof(this->val));
236+
return address;
237+
} // operator uint64_t
238+
239+
#endif

0 commit comments

Comments
 (0)