Skip to content

Commit cd0b1d1

Browse files
authored
Merge pull request #417 from sparkfun/BringBackBluetoothSerialLibrary
Bring back bluetooth serial library
2 parents 92f0692 + f5b4c64 commit cd0b1d1

File tree

10 files changed

+1950
-11
lines changed

10 files changed

+1950
-11
lines changed

Firmware/RTK_Everywhere/Bluetooth.ino

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -292,17 +292,17 @@ void bluetoothStart()
292292
if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP_AND_BLE)
293293
{
294294
beginSuccess &=
295-
bluetoothSerialBle->begin(deviceName, false, settings.sppRxQueueSize,
296-
settings.sppTxQueueSize); // localName, isMaster, rxBufferSize, txBufferSize
295+
bluetoothSerialBle->begin(deviceName, false, false, settings.sppRxQueueSize,
296+
settings.sppTxQueueSize); // localName, isMaster, disableBLE, rxBufferSize, txBufferSize
297297
beginSuccess &=
298-
bluetoothSerialSpp->begin(deviceName, false, settings.sppRxQueueSize,
299-
settings.sppTxQueueSize); // localName, isMaster, rxBufferSize, txBufferSize
298+
bluetoothSerialSpp->begin(deviceName, false, false, settings.sppRxQueueSize,
299+
settings.sppTxQueueSize); // localName, isMaster, disableBLE, rxBufferSize, txBufferSize
300300
}
301301
else
302302
{
303303
beginSuccess &=
304-
bluetoothSerial->begin(deviceName, false, settings.sppRxQueueSize,
305-
settings.sppTxQueueSize); // localName, isMaster, rxBufferSize, txBufferSize
304+
bluetoothSerial->begin(deviceName, false, true, settings.sppRxQueueSize,
305+
settings.sppTxQueueSize); // localName, isMaster, disableBLE, rxBufferSize, txBufferSize
306306
}
307307

308308
if (beginSuccess == false)

Firmware/RTK_Everywhere/bluetoothSelect.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
// We use a local copy of the BluetoothSerial library so that we can increase the RX buffer. See issues:
44
// https://github.com/sparkfun/SparkFun_RTK_Firmware/issues/23
55
// https://github.com/sparkfun/SparkFun_RTK_Firmware/issues/469
6-
#include <BluetoothSerial.h>
6+
#include "src/BluetoothSerial/BluetoothSerial.h"
77

88
#include <BleSerial.h> //Click here to get the library: http://librarymanager/All#ESP32_BleSerial v1.0.4 by Avinab Malla
99

1010
class BTSerialInterface
1111
{
1212
public:
13-
virtual bool begin(String deviceName, bool isMaster, uint16_t rxQueueSize, uint16_t txQueueSize) = 0;
13+
virtual bool begin(String deviceName, bool isMaster, bool disableBLE, uint16_t rxQueueSize, uint16_t txQueueSize) = 0;
1414
virtual void disconnect() = 0;
1515
virtual void end() = 0;
1616
virtual esp_err_t register_callback(esp_spp_cb_t callback) = 0;
@@ -31,9 +31,9 @@ class BTClassicSerial : public virtual BTSerialInterface, public BluetoothSerial
3131
// Everything is already implemented in BluetoothSerial since the code was
3232
// originally written using that class
3333
public:
34-
bool begin(String deviceName, bool isMaster, uint16_t rxQueueSize, uint16_t txQueueSize)
34+
bool begin(String deviceName, bool isMaster, bool disableBLE, uint16_t rxQueueSize, uint16_t txQueueSize)
3535
{
36-
return BluetoothSerial::begin(deviceName, isMaster); //, rxQueueSize, txQueueSize); v3.0.0 has no QueueSize parameters
36+
return BluetoothSerial::begin(deviceName, isMaster, disableBLE, rxQueueSize, txQueueSize);
3737
}
3838

3939
void disconnect()
@@ -91,7 +91,7 @@ class BTLESerial : public virtual BTSerialInterface, public BleSerial
9191
{
9292
public:
9393
// Missing from BleSerial
94-
bool begin(String deviceName, bool isMaster, uint16_t rxQueueSize, uint16_t txQueueSize)
94+
bool begin(String deviceName, bool isMaster, bool disableBLE, uint16_t rxQueueSize, uint16_t txQueueSize)
9595
{
9696
BleSerial::begin(deviceName.c_str());
9797
return true;
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 */

0 commit comments

Comments
 (0)