Skip to content

Commit bc06787

Browse files
committed
Add back custom version of BluetoothSerial
This originally comes from RTK Firmware ~v2.0. This change exposes the TX/RX buffer sizes so that we can dynamically set them (from settings) at run time. The BluetoothSerial library defaults to 512 bytes in the RX buffer. Because RTCM is ~530 to 600 bytes, there are times when the btReadTask cannot keep up and an RTCM sentence is corrupted. This change reduces the number of RTCM bytes dropped during SPP from the phone. By default, this will increase the overall RAM usage by 512 bytes.
1 parent 12344eb commit bc06787

File tree

11 files changed

+1534
-10
lines changed

11 files changed

+1534
-10
lines changed

Firmware/RTK_Surveyor/Bluetooth.ino

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ void bluetoothStart()
180180
// while (bluetoothPinned == false) // Wait for task to run once
181181
// delay(1);
182182

183-
if (bluetoothSerial->begin(deviceName) == false)
183+
if (bluetoothSerial->begin(deviceName, false, settings.sppRxQueueSize, settings.sppTxQueueSize) ==
184+
false) // localName, isMaster, rxBufferSize, txBufferSize
184185
{
185186
systemPrintln("An error occurred initializing Bluetooth");
186187

@@ -235,7 +236,8 @@ void bluetoothStart()
235236
void pinBluetoothTask(void *pvParameters)
236237
{
237238
#ifdef COMPILE_BT
238-
if (bluetoothSerial->begin(deviceName) == false)
239+
if (bluetoothSerial->begin(deviceName, false, settings.sppRxQueueSize, settings.sppTxQueueSize) ==
240+
false) // localName, isMaster, rxBufferSize,
239241
{
240242
systemPrintln("An error occurred initializing Bluetooth");
241243

@@ -246,7 +248,7 @@ void pinBluetoothTask(void *pvParameters)
246248
bluetoothPinned = true;
247249

248250
vTaskDelete(nullptr); // Delete task once it has run once
249-
#endif // COMPILE_BT
251+
#endif // COMPILE_BT
250252
}
251253

252254
// This function stops BT so that it can be restarted later

Firmware/RTK_Surveyor/bluetoothSelect.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
#ifdef COMPILE_BT
22

3-
#include "BluetoothSerial.h"
3+
//We use a local copy of the BluetoothSerial library so that we can increase the RX buffer. See issues:
4+
//https://github.com/sparkfun/SparkFun_RTK_Firmware/issues/23
5+
//https://github.com/sparkfun/SparkFun_RTK_Firmware/issues/469
6+
#include "src/BluetoothSerial/BluetoothSerial.h"
7+
48
#include <BleSerial.h> //Click here to get the library: http://librarymanager/All#ESP32_BleSerial v1.0.4 by Avinab Malla
59

610
class BTSerialInterface
711
{
812
public:
9-
virtual bool begin(String deviceName) = 0;
13+
virtual bool begin(String deviceName, bool isMaster, uint16_t rxQueueSize, uint16_t txQueueSize) = 0;
1014
virtual void disconnect() = 0;
1115
virtual void end() = 0;
1216
virtual esp_err_t register_callback(esp_spp_cb_t *callback) = 0;
@@ -27,9 +31,9 @@ class BTClassicSerial : public virtual BTSerialInterface, public BluetoothSerial
2731
// Everything is already implemented in BluetoothSerial since the code was
2832
// originally written using that class
2933
public:
30-
bool begin(String deviceName)
34+
bool begin(String deviceName, bool isMaster, uint16_t rxQueueSize, uint16_t txQueueSize)
3135
{
32-
return BluetoothSerial::begin(deviceName);
36+
return BluetoothSerial::begin(deviceName, isMaster, rxQueueSize, txQueueSize);
3337
}
3438

3539
void disconnect()
@@ -87,7 +91,7 @@ class BTLESerial : public virtual BTSerialInterface, public BleSerial
8791
{
8892
public:
8993
// Missing from BleSerial
90-
bool begin(String deviceName)
94+
bool begin(String deviceName, bool isMaster, uint16_t rxQueueSize, uint16_t txQueueSize)
9195
{
9296
BleSerial::begin(deviceName.c_str());
9397
return true;

Firmware/RTK_Surveyor/settings.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -753,8 +753,8 @@ typedef struct
753753
bool enableLogging = true; // If an SD card is present, log default sentences
754754
bool enableARPLogging = false; // Log the Antenna Reference Position from RTCM 1005/1006 - if available
755755
uint16_t ARPLoggingInterval_s = 10; // Log the ARP every 10 seconds - if available
756-
uint16_t sppRxQueueSize = 2048;
757-
uint16_t sppTxQueueSize = 512;
756+
uint16_t sppRxQueueSize = 512 * 2;
757+
uint16_t sppTxQueueSize = 32;
758758
uint8_t dynamicModel = DYN_MODEL_PORTABLE;
759759
SystemState lastState = STATE_NOT_SET; // Start unit in last known state
760760
bool enableSensorFusion = false; // If IMU is available, avoid using it unless user specifically selects automotive
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
/**
25+
* @brief Create an address from the native ESP32 representation.
26+
* @param [in] address The native representation.
27+
*/
28+
BTAddress::BTAddress(esp_bd_addr_t address) {
29+
memcpy(m_address, address, ESP_BD_ADDR_LEN);
30+
} // BTAddress
31+
32+
33+
/**
34+
* @brief Create an address from a hex string
35+
*
36+
* A hex string is of the format:
37+
* ```
38+
* 00:00:00:00:00:00
39+
* ```
40+
* which is 17 characters in length.
41+
*
42+
* @param [in] stringAddress The hex representation of the address.
43+
*/
44+
BTAddress::BTAddress(std::string stringAddress) {
45+
if (stringAddress.length() != 17) return;
46+
47+
int data[6];
48+
sscanf(stringAddress.c_str(), "%x:%x:%x:%x:%x:%x", &data[0], &data[1], &data[2], &data[3], &data[4], &data[5]);
49+
m_address[0] = (uint8_t) data[0];
50+
m_address[1] = (uint8_t) data[1];
51+
m_address[2] = (uint8_t) data[2];
52+
m_address[3] = (uint8_t) data[3];
53+
m_address[4] = (uint8_t) data[4];
54+
m_address[5] = (uint8_t) data[5];
55+
} // BTAddress
56+
57+
58+
/**
59+
* @brief Determine if this address equals another.
60+
* @param [in] otherAddress The other address to compare against.
61+
* @return True if the addresses are equal.
62+
*/
63+
bool BTAddress::equals(BTAddress otherAddress) {
64+
return memcmp(otherAddress.getNative(), m_address, 6) == 0;
65+
} // equals
66+
67+
68+
/**
69+
* @brief Return the native representation of the address.
70+
* @return The native representation of the address.
71+
*/
72+
esp_bd_addr_t *BTAddress::getNative() {
73+
return &m_address;
74+
} // getNative
75+
76+
77+
/**
78+
* @brief Convert a BT address to a string.
79+
*
80+
* A string representation of an address is in the format:
81+
*
82+
* ```
83+
* xx:xx:xx:xx:xx:xx
84+
* ```
85+
*
86+
* @return The string representation of the address.
87+
*/
88+
std::string BTAddress::toString() {
89+
auto size = 18;
90+
char *res = (char*)malloc(size);
91+
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]);
92+
std::string ret(res);
93+
free(res);
94+
return ret;
95+
} // toString
96+
#endif
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 <string>
16+
17+
18+
/**
19+
* @brief A %BT device address.
20+
*
21+
* Every %BT device has a unique address which can be used to identify it and form connections.
22+
*/
23+
class BTAddress {
24+
public:
25+
BTAddress(esp_bd_addr_t address);
26+
BTAddress(std::string stringAddress);
27+
bool equals(BTAddress otherAddress);
28+
esp_bd_addr_t* getNative();
29+
std::string toString();
30+
31+
private:
32+
esp_bd_addr_t m_address;
33+
};
34+
35+
#endif /* CONFIG_BT_ENABLED */
36+
#endif /* COMPONENTS_CPP_UTILS_BTADDRESS_H_ */
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
13+
14+
class BTAdvertisedDevice {
15+
public:
16+
virtual ~BTAdvertisedDevice() = default;
17+
18+
virtual BTAddress getAddress();
19+
virtual uint32_t getCOD();
20+
virtual std::string getName();
21+
virtual int8_t getRSSI();
22+
23+
24+
virtual bool haveCOD();
25+
virtual bool haveName();
26+
virtual bool haveRSSI();
27+
28+
virtual std::string toString();
29+
};
30+
31+
class BTAdvertisedDeviceSet : public virtual BTAdvertisedDevice {
32+
public:
33+
BTAdvertisedDeviceSet();
34+
//~BTAdvertisedDeviceSet() = default;
35+
36+
37+
BTAddress getAddress();
38+
uint32_t getCOD();
39+
std::string getName();
40+
int8_t getRSSI();
41+
42+
43+
bool haveCOD();
44+
bool haveName();
45+
bool haveRSSI();
46+
47+
std::string toString();
48+
49+
void setAddress(BTAddress address);
50+
void setCOD(uint32_t cod);
51+
void setName(std::string name);
52+
void setRSSI(int8_t rssi);
53+
54+
bool m_haveCOD;
55+
bool m_haveName;
56+
bool m_haveRSSI;
57+
58+
59+
BTAddress m_address = BTAddress((uint8_t*)"\0\0\0\0\0\0");
60+
uint32_t m_cod;
61+
std::string m_name;
62+
int8_t m_rssi;
63+
};
64+
65+
#endif
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
17+
BTAdvertisedDeviceSet::BTAdvertisedDeviceSet() {
18+
m_cod = 0;
19+
m_name = "";
20+
m_rssi = 0;
21+
22+
m_haveCOD = false;
23+
m_haveName = false;
24+
m_haveRSSI = false;
25+
} // BTAdvertisedDeviceSet
26+
27+
BTAddress BTAdvertisedDeviceSet::getAddress() { return m_address; }
28+
uint32_t BTAdvertisedDeviceSet::getCOD() { return m_cod; }
29+
std::string BTAdvertisedDeviceSet::getName() { return m_name; }
30+
int8_t BTAdvertisedDeviceSet::getRSSI() { return m_rssi; }
31+
32+
33+
bool BTAdvertisedDeviceSet::haveCOD() { return m_haveCOD; }
34+
bool BTAdvertisedDeviceSet::haveName() { return m_haveName; }
35+
bool BTAdvertisedDeviceSet::haveRSSI() { return m_haveRSSI; }
36+
37+
/**
38+
* @brief Create a string representation of this device.
39+
* @return A string representation of this device.
40+
*/
41+
std::string BTAdvertisedDeviceSet::toString() {
42+
std::string res = "Name: " + getName() + ", Address: " + getAddress().toString();
43+
if (haveCOD()) {
44+
char val[6];
45+
snprintf(val, sizeof(val), "%d", getCOD());
46+
res += ", cod: ";
47+
res += val;
48+
}
49+
if (haveRSSI()) {
50+
char val[6];
51+
snprintf(val, sizeof(val), "%d", (int8_t)getRSSI());
52+
res += ", rssi: ";
53+
res += val;
54+
}
55+
return res;
56+
} // toString
57+
58+
59+
void BTAdvertisedDeviceSet::setAddress(BTAddress address) {
60+
m_address = address;
61+
}
62+
63+
void BTAdvertisedDeviceSet::setCOD(uint32_t cod) {
64+
m_cod = cod;
65+
m_haveCOD = true;
66+
}
67+
68+
void BTAdvertisedDeviceSet::setName(std::string name) {
69+
m_name = name;
70+
m_haveName = true;
71+
}
72+
73+
void BTAdvertisedDeviceSet::setRSSI(int8_t rssi) {
74+
m_rssi = rssi;
75+
m_haveRSSI = true;
76+
}
77+
78+
#endif /* CONFIG_BT_ENABLED */

0 commit comments

Comments
 (0)