Skip to content

Commit f812e29

Browse files
committed
Add BluetoothSerial Library from v3.0.1 core
1 parent 92f0692 commit f812e29

File tree

8 files changed

+1936
-0
lines changed

8 files changed

+1936
-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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* BTAddress.h
3+
*
4+
* Created on: Jul 2, 2017
5+
* Author: kolban
6+
* Ported on: Feb 5, 2021
7+
* Author: Thomas M. (ArcticSnowSky)
8+
*/
9+
10+
#ifndef COMPONENTS_CPP_UTILS_BTADDRESS_H_
11+
#define COMPONENTS_CPP_UTILS_BTADDRESS_H_
12+
#include "sdkconfig.h"
13+
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BLUEDROID_ENABLED)
14+
#include <esp_gap_bt_api.h> // ESP32 BT
15+
#include <Arduino.h>
16+
17+
/**
18+
* @brief A %BT device address.
19+
*
20+
* Every %BT device has a unique address which can be used to identify it and form connections.
21+
*/
22+
class BTAddress {
23+
public:
24+
BTAddress();
25+
BTAddress(esp_bd_addr_t address);
26+
BTAddress(String stringAddress);
27+
bool equals(BTAddress otherAddress);
28+
operator bool() const;
29+
30+
esp_bd_addr_t *getNative() const;
31+
String toString(bool capital = false) const;
32+
33+
private:
34+
esp_bd_addr_t m_address;
35+
};
36+
37+
#endif /* CONFIG_BT_ENABLED */
38+
#endif /* COMPONENTS_CPP_UTILS_BTADDRESS_H_ */
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* BTAdvertisedDevice.h
3+
*
4+
* Created on: Feb 5, 2021
5+
* Author: Thomas M. (ArcticSnowSky)
6+
*/
7+
8+
#ifndef __BTADVERTISEDDEVICE_H__
9+
#define __BTADVERTISEDDEVICE_H__
10+
11+
#include "BTAddress.h"
12+
#include <string>
13+
14+
class BTAdvertisedDevice {
15+
public:
16+
virtual ~BTAdvertisedDevice() = default;
17+
18+
virtual BTAddress getAddress() = 0;
19+
virtual uint32_t getCOD() const = 0;
20+
virtual std::string getName() const = 0;
21+
virtual int8_t getRSSI() const = 0;
22+
23+
virtual bool haveCOD() const = 0;
24+
virtual bool haveName() const = 0;
25+
virtual bool haveRSSI() const = 0;
26+
27+
virtual std::string toString() = 0;
28+
};
29+
30+
class BTAdvertisedDeviceSet : public virtual BTAdvertisedDevice {
31+
public:
32+
BTAdvertisedDeviceSet();
33+
//~BTAdvertisedDeviceSet() = default;
34+
35+
BTAddress getAddress();
36+
uint32_t getCOD() const;
37+
std::string getName() const;
38+
int8_t getRSSI() const;
39+
40+
bool haveCOD() const;
41+
bool haveName() const;
42+
bool haveRSSI() const;
43+
44+
std::string toString();
45+
46+
void setAddress(BTAddress address);
47+
void setCOD(uint32_t cod);
48+
void setName(std::string name);
49+
void setRSSI(int8_t rssi);
50+
51+
bool m_haveCOD;
52+
bool m_haveName;
53+
bool m_haveRSSI;
54+
55+
BTAddress m_address = BTAddress((uint8_t *)"\0\0\0\0\0\0");
56+
uint32_t m_cod;
57+
std::string m_name;
58+
int8_t m_rssi;
59+
};
60+
61+
#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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* BTScan.h
3+
*
4+
* Created on: Feb 5, 2021
5+
* Author: Thomas M. (ArcticSnowSky)
6+
*/
7+
8+
#ifndef __BTSCAN_H__
9+
#define __BTSCAN_H__
10+
11+
#include <map>
12+
#include <string>
13+
#include <Print.h>
14+
#include "BTAddress.h"
15+
#include "BTAdvertisedDevice.h"
16+
17+
class BTAdvertisedDevice;
18+
class BTAdvertisedDeviceSet;
19+
20+
class BTScanResults {
21+
public:
22+
virtual ~BTScanResults() = default;
23+
24+
virtual void dump(Print *print = nullptr) = 0;
25+
virtual int getCount() = 0;
26+
virtual BTAdvertisedDevice *getDevice(int i) = 0;
27+
};
28+
29+
class BTScanResultsSet : public BTScanResults {
30+
public:
31+
void dump(Print *print = nullptr);
32+
int getCount();
33+
BTAdvertisedDevice *getDevice(int i);
34+
35+
bool add(BTAdvertisedDeviceSet advertisedDevice, bool unique = true);
36+
void clear();
37+
38+
std::map<std::string, BTAdvertisedDeviceSet> m_vectorAdvertisedDevices;
39+
};
40+
41+
#endif

0 commit comments

Comments
 (0)