Skip to content

Commit 4864710

Browse files
committed
Add missing v3.2.0 .cpp files
1 parent a1660dc commit 4864710

File tree

4 files changed

+1996
-0
lines changed

4 files changed

+1996
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* BTAddress.cpp
3+
*
4+
* Created on: Jul 2, 2017
5+
* Author: kolban
6+
* Ported on: Feb 5, 2021
7+
* Author: Thomas M. (ArcticSnowSky)
8+
*/
9+
#include "sdkconfig.h"
10+
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BLUEDROID_ENABLED)
11+
12+
#include "BTAddress.h"
13+
#include <string>
14+
#include <sstream>
15+
#include <iomanip>
16+
#include <string.h>
17+
#include <stdio.h>
18+
#include <malloc.h>
19+
#ifdef ARDUINO_ARCH_ESP32
20+
#include "esp32-hal-log.h"
21+
#endif
22+
23+
/**
24+
* @brief Create an address from the native ESP32 representation.
25+
* @param [in] address The native representation.
26+
*/
27+
BTAddress::BTAddress(esp_bd_addr_t address) {
28+
memcpy(m_address, address, ESP_BD_ADDR_LEN);
29+
} // BTAddress
30+
31+
BTAddress::BTAddress() {
32+
bzero(m_address, ESP_BD_ADDR_LEN);
33+
} // BTAddress
34+
35+
/**
36+
* @brief Create an address from a hex string
37+
*
38+
* A hex string is of the format:
39+
* ```
40+
* 00:00:00:00:00:00
41+
* ```
42+
* which is 17 characters in length.
43+
*
44+
* @param [in] stringAddress The hex representation of the address.
45+
*/
46+
BTAddress::BTAddress(String stringAddress) {
47+
if (stringAddress.length() != 17) {
48+
return;
49+
}
50+
51+
int data[6];
52+
sscanf(stringAddress.c_str(), "%x:%x:%x:%x:%x:%x", &data[0], &data[1], &data[2], &data[3], &data[4], &data[5]);
53+
m_address[0] = (uint8_t)data[0];
54+
m_address[1] = (uint8_t)data[1];
55+
m_address[2] = (uint8_t)data[2];
56+
m_address[3] = (uint8_t)data[3];
57+
m_address[4] = (uint8_t)data[4];
58+
m_address[5] = (uint8_t)data[5];
59+
} // BTAddress
60+
61+
/**
62+
* @brief Determine if this address equals another.
63+
* @param [in] otherAddress The other address to compare against.
64+
* @return True if the addresses are equal.
65+
*/
66+
bool BTAddress::equals(BTAddress otherAddress) {
67+
return memcmp(otherAddress.getNative(), m_address, 6) == 0;
68+
} // equals
69+
70+
BTAddress::operator bool() const {
71+
for (int i = 0; i < ESP_BD_ADDR_LEN; i++) {
72+
if (this->m_address[i]) {
73+
return true;
74+
}
75+
}
76+
return false;
77+
} // operator ()
78+
79+
/**
80+
* @brief Return the native representation of the address.
81+
* @return The native representation of the address.
82+
*/
83+
esp_bd_addr_t *BTAddress::getNative() const {
84+
return const_cast<esp_bd_addr_t *>(&m_address);
85+
} // getNative
86+
87+
/**
88+
* @brief Convert a BT address to a string.
89+
* @param [in] capital changes the letter size
90+
* By default the parameter `capital` == false and the string representation of an address is in the format:
91+
* ```
92+
* xx:xx:xx:xx:xx:xx
93+
* ```
94+
* When the parameter `capital` == true the format uses capital letters:
95+
* ```
96+
* XX:XX:XX:XX:XX:XX
97+
* ```
98+
* @return The string representation of the address.
99+
*/
100+
String BTAddress::toString(bool capital) const {
101+
auto size = 18;
102+
char *res = (char *)malloc(size);
103+
if (capital) {
104+
snprintf(res, size, "%02X:%02X:%02X:%02X:%02X:%02X", m_address[0], m_address[1], m_address[2], m_address[3], m_address[4], m_address[5]);
105+
} else {
106+
snprintf(res, size, "%02x:%02x:%02x:%02x:%02x:%02x", m_address[0], m_address[1], m_address[2], m_address[3], m_address[4], m_address[5]);
107+
}
108+
String ret(res);
109+
free(res);
110+
return ret;
111+
} // toString
112+
#endif
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* BTAdvertisedDeviceSet.cpp
3+
*
4+
* Created on: Feb 5, 2021
5+
* Author: Thomas M. (ArcticSnowSky)
6+
*/
7+
8+
#include "sdkconfig.h"
9+
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BLUEDROID_ENABLED)
10+
11+
//#include <map>
12+
13+
#include "BTAdvertisedDevice.h"
14+
//#include "BTScan.h"
15+
16+
BTAdvertisedDeviceSet::BTAdvertisedDeviceSet() {
17+
m_cod = 0;
18+
m_name = "";
19+
m_rssi = 0;
20+
21+
m_haveCOD = false;
22+
m_haveName = false;
23+
m_haveRSSI = false;
24+
} // BTAdvertisedDeviceSet
25+
26+
BTAddress BTAdvertisedDeviceSet::getAddress() {
27+
return m_address;
28+
}
29+
uint32_t BTAdvertisedDeviceSet::getCOD() const {
30+
return m_cod;
31+
}
32+
std::string BTAdvertisedDeviceSet::getName() const {
33+
return m_name;
34+
}
35+
int8_t BTAdvertisedDeviceSet::getRSSI() const {
36+
return m_rssi;
37+
}
38+
39+
bool BTAdvertisedDeviceSet::haveCOD() const {
40+
return m_haveCOD;
41+
}
42+
bool BTAdvertisedDeviceSet::haveName() const {
43+
return m_haveName;
44+
}
45+
bool BTAdvertisedDeviceSet::haveRSSI() const {
46+
return m_haveRSSI;
47+
}
48+
49+
/**
50+
* @brief Create a string representation of this device.
51+
* @return A string representation of this device.
52+
*/
53+
std::string BTAdvertisedDeviceSet::toString() {
54+
std::string res = "Name: " + getName() + ", Address: " + std::string(getAddress().toString().c_str(), getAddress().toString().length());
55+
if (haveCOD()) {
56+
char val[7]; //6 hex digits + null
57+
snprintf(val, sizeof(val), "%06lx", getCOD() & 0xFFFFFF);
58+
res += ", cod: 0x";
59+
res += val;
60+
}
61+
if (haveRSSI()) {
62+
char val[6];
63+
snprintf(val, sizeof(val), "%d", (int8_t)getRSSI());
64+
res += ", rssi: ";
65+
res += val;
66+
}
67+
return res;
68+
} // toString
69+
70+
void BTAdvertisedDeviceSet::setAddress(BTAddress address) {
71+
m_address = address;
72+
}
73+
74+
void BTAdvertisedDeviceSet::setCOD(uint32_t cod) {
75+
m_cod = cod;
76+
m_haveCOD = true;
77+
}
78+
79+
void BTAdvertisedDeviceSet::setName(std::string name) {
80+
m_name = name;
81+
m_haveName = true;
82+
}
83+
84+
void BTAdvertisedDeviceSet::setRSSI(int8_t rssi) {
85+
m_rssi = rssi;
86+
m_haveRSSI = true;
87+
}
88+
89+
#endif /* CONFIG_BT_ENABLED */
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* BTScanResultsSet.cpp
3+
*
4+
* Created on: Feb 5, 2021
5+
* Author: Thomas M. (ArcticSnowSky)
6+
*/
7+
8+
#include "sdkconfig.h"
9+
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BLUEDROID_ENABLED)
10+
11+
#include <esp_err.h>
12+
13+
#include "BTAdvertisedDevice.h"
14+
#include "BTScan.h"
15+
//#include "GeneralUtils.h"
16+
#include "esp32-hal-log.h"
17+
18+
class BTAdvertisedDevice;
19+
20+
/**
21+
* @brief Dump the scan results to the log.
22+
*/
23+
void BTScanResultsSet::dump(Print *print) {
24+
int cnt = getCount();
25+
if (print == nullptr) {
26+
log_v(">> Dump scan results : %d", cnt);
27+
for (int i = 0; i < cnt; i++) {
28+
BTAdvertisedDevice *dev = getDevice(i);
29+
if (dev) {
30+
log_d("- %d: %s\n", i + 1, dev->toString().c_str());
31+
} else {
32+
log_d("- %d is null\n", i + 1);
33+
}
34+
}
35+
log_v("-- dump finished --");
36+
} else {
37+
print->printf(">> Dump scan results: %d\n", cnt);
38+
for (int i = 0; i < cnt; i++) {
39+
BTAdvertisedDevice *dev = getDevice(i);
40+
if (dev) {
41+
print->printf("- %d: %s\n", i + 1, dev->toString().c_str());
42+
} else {
43+
print->printf("- %d is null\n", i + 1);
44+
}
45+
}
46+
print->println("-- Dump finished --");
47+
}
48+
} // dump
49+
50+
/**
51+
* @brief Return the count of devices found in the last scan.
52+
* @return The number of devices found in the last scan.
53+
*/
54+
int BTScanResultsSet::getCount() {
55+
return m_vectorAdvertisedDevices.size();
56+
} // getCount
57+
58+
/**
59+
* @brief Return the specified device at the given index.
60+
* The index should be between 0 and getCount()-1.
61+
* @param [in] i The index of the device.
62+
* @return The device at the specified index.
63+
*/
64+
BTAdvertisedDevice *BTScanResultsSet::getDevice(int i) {
65+
if (i < 0) {
66+
return nullptr;
67+
}
68+
69+
int x = 0;
70+
BTAdvertisedDeviceSet *pDev = &m_vectorAdvertisedDevices.begin()->second;
71+
for (auto it = m_vectorAdvertisedDevices.begin(); it != m_vectorAdvertisedDevices.end(); it++) {
72+
pDev = &it->second;
73+
if (x == i) {
74+
break;
75+
}
76+
x++;
77+
}
78+
return x == i ? pDev : nullptr;
79+
}
80+
81+
void BTScanResultsSet::clear() {
82+
//for(auto _dev : m_vectorAdvertisedDevices)
83+
// delete _dev.second;
84+
m_vectorAdvertisedDevices.clear();
85+
}
86+
87+
bool BTScanResultsSet::add(BTAdvertisedDeviceSet advertisedDevice, bool unique) {
88+
std::string key = std::string(advertisedDevice.getAddress().toString().c_str(), advertisedDevice.getAddress().toString().length());
89+
if (!unique || m_vectorAdvertisedDevices.count(key) == 0) {
90+
m_vectorAdvertisedDevices.insert(std::pair<std::string, BTAdvertisedDeviceSet>(key, advertisedDevice));
91+
return true;
92+
} else {
93+
return false;
94+
}
95+
}
96+
97+
#endif

0 commit comments

Comments
 (0)