Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 3 additions & 70 deletions Firmware/RTK_Everywhere/Bluetooth.ino
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,14 @@

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

//----------------------------------------
// Constants
//----------------------------------------
#ifdef COMPILE_BT

//----------------------------------------
// Locals - compiled out
//----------------------------------------

static volatile BTState bluetoothState = BT_OFF;

BluetoothRadioType_e bluetoothRadioPreviousOnType = BLUETOOTH_RADIO_OFF;

#ifdef COMPILE_BT

#include <BleBatteryService.h>

BTSerialInterface *bluetoothSerialSpp = nullptr;
Expand All @@ -50,16 +44,13 @@ BleBatteryService bluetoothBatteryService;

TaskHandle_t bluetoothCommandTaskHandle = nullptr; // Task to monitor incoming CLI from BLE

#endif // COMPILE_BT

//----------------------------------------
// Global Bluetooth Routines
//----------------------------------------

// Check if Bluetooth is connected
void bluetoothUpdate()
{
#ifdef COMPILE_BT
static uint32_t lastCheck = millis(); // Check if connected every 100ms
if ((millis() - lastCheck) > 100)
{
Expand Down Expand Up @@ -95,14 +86,12 @@ void bluetoothUpdate()
bluetoothState = BT_NOTCONNECTED;
}
}
#endif // COMPILE_BT
}

// Test each interface to see if there is a connection
// Return true if one is
bool bluetoothIsConnected()
{
#ifdef COMPILE_BT
if (bluetoothGetState() == BT_OFF)
return (false);

Expand All @@ -127,15 +116,13 @@ bool bluetoothIsConnected()
if (bluetoothSerialSpp->connected() == true)
return (true);
}
#endif // COMPILE_BT

return (false);
}

// Return true if the BLE Command channel is connected
bool bluetoothCommandIsConnected()
{
#ifdef COMPILE_BT
if (bluetoothGetState() == BT_OFF)
return (false);

Expand All @@ -157,25 +144,19 @@ bool bluetoothCommandIsConnected()
{
return (false);
}
#endif // COMPILE_BT

return (false);
}

// Return the Bluetooth state
byte bluetoothGetState()
{
#ifdef COMPILE_BT
return bluetoothState;
#else // COMPILE_BT
return BT_OFF;
#endif // COMPILE_BT
}

// Read data from the Bluetooth device
int bluetoothRead(uint8_t *buffer, int length)
{
#ifdef COMPILE_BT
if (bluetoothGetState() == BT_OFF)
return 0;

Expand All @@ -201,16 +182,11 @@ int bluetoothRead(uint8_t *buffer, int length)
return 0; // Nothing to do here. SDP takes care of everything...

return 0;

#else // COMPILE_BT
return 0;
#endif // COMPILE_BT
}

// Read data from the Bluetooth command interface
int bluetoothCommandRead(uint8_t *buffer, int length)
{
#ifdef COMPILE_BT
if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP_AND_BLE ||
settings.bluetoothRadioType == BLUETOOTH_RADIO_BLE)
{
Expand All @@ -219,15 +195,11 @@ int bluetoothCommandRead(uint8_t *buffer, int length)
}

return 0;
#else // COMPILE_BT
return 0;
#endif // COMPILE_BT
}

// Read data from the Bluetooth device
uint8_t bluetoothRead()
{
#ifdef COMPILE_BT
if (bluetoothGetState() == BT_OFF)
return 0;

Expand All @@ -247,28 +219,20 @@ uint8_t bluetoothRead()
return 0; // Nothing to do here. SDP takes care of everything...

return 0;
#else // COMPILE_BT
return 0;
#endif // COMPILE_BT
}

// Read data from the BLE Command interface
uint8_t bluetoothCommandRead()
{
#ifdef COMPILE_BT
if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP_AND_BLE ||
settings.bluetoothRadioType == BLUETOOTH_RADIO_BLE)
return bluetoothSerialBleCommands->read();
return (0);
#else // COMPILE_BT
return 0;
#endif // COMPILE_BT
}

// Determine if data is available
int bluetoothRxDataAvailable()
{
#ifdef COMPILE_BT
if (bluetoothGetState() == BT_OFF)
return 0;

Expand All @@ -288,28 +252,20 @@ int bluetoothRxDataAvailable()
return 0; // Nothing to do here. SDP takes care of everything...

return (0);
#else // COMPILE_BT
return 0;
#endif // COMPILE_BT
}

// Determine if data is available on the BLE Command interface
int bluetoothCommandAvailable()
{
#ifdef COMPILE_BT
if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP_AND_BLE ||
settings.bluetoothRadioType == BLUETOOTH_RADIO_BLE)
return bluetoothSerialBleCommands->available();
return (0);
#else // COMPILE_BT
return 0;
#endif // COMPILE_BT
}

// Write data to the Bluetooth device
int bluetoothWrite(const uint8_t *buffer, int length)
{
#ifdef COMPILE_BT
if (bluetoothGetState() == BT_OFF)
return length; // Avoid buffer full warnings

Expand Down Expand Up @@ -341,15 +297,11 @@ int bluetoothWrite(const uint8_t *buffer, int length)
return length; // Nothing to do here. SDP takes care of everything...

return (0);
#else // COMPILE_BT
return 0;
#endif // COMPILE_BT
}

// Write data to the BLE Command interface
int bluetoothCommandWrite(const uint8_t *buffer, int length)
{
#ifdef COMPILE_BT
if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP_AND_BLE)
{
return (bluetoothSerialBleCommands->write(buffer, length));
Expand All @@ -370,15 +322,11 @@ int bluetoothCommandWrite(const uint8_t *buffer, int length)
return length; // Nothing to do here. SDP takes care of everything...

return (0);
#else // COMPILE_BT
return 0;
#endif // COMPILE_BT
}

// Write data to the Bluetooth device
int bluetoothWrite(uint8_t value)
{
#ifdef COMPILE_BT
if (bluetoothGetState() == BT_OFF)
return 1; // Avoid buffer full warnings

Expand Down Expand Up @@ -407,15 +355,11 @@ int bluetoothWrite(uint8_t value)
return 1; // Nothing to do here. SDP takes care of everything...

return (0);
#else // COMPILE_BT
return 0;
#endif // COMPILE_BT
}

// Flush Bluetooth device
void bluetoothFlush()
{
#ifdef COMPILE_BT
if (bluetoothGetState() == BT_OFF)
return;

Expand All @@ -434,9 +378,6 @@ void bluetoothFlush()
}
else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP_ACCESSORY_MODE)
bluetoothSerialSpp->flush(); // Needed? Not sure... TODO
#else // COMPILE_BT
return;
#endif // COMPILE_BT
}

void BTConfirmRequestCallback(uint32_t numVal) {
Expand All @@ -445,9 +386,7 @@ void BTConfirmRequestCallback(uint32_t numVal) {

// TODO: if the RTK device has an OLED, we should display the PIN so user can confirm
systemPrintf("Device sent PIN: %06lu. Sending confirmation\r\n", numVal);
#ifdef COMPILE_BT
bluetoothSerialSpp->confirmReply(true); // AUTO_PAIR - equivalent to enableSSP(false, true);
#endif // COMPILE_BT
}

void deviceNameSpacesToUnderscores()
Expand Down Expand Up @@ -492,7 +431,6 @@ void bluetoothStart(bool skipOnlineCheck)
}
}

#ifdef COMPILE_BT
{ // Maintain the indentation for now. TODO: delete the braces and correct indentation
bluetoothState = BT_OFF; // Indicate to tasks that BT is unavailable

Expand Down Expand Up @@ -756,14 +694,12 @@ void bluetoothStart(bool skipOnlineCheck)
online.bluetooth = true;
bluetoothRadioPreviousOnType = settings.bluetoothRadioType;
} // if (1)
#endif // COMPILE_BT
}

// Assign Bluetooth interrupts to the core that started the task. See:
// https://github.com/espressif/arduino-esp32/issues/3386
// void pinBluetoothTask(void *pvParameters)
// {
// #ifdef COMPILE_BT
// if (bluetoothSerial->begin(deviceName, false, settings.sppRxQueueSize, settings.sppTxQueueSize) ==
// false) // localName, isMaster, rxBufferSize,
// {
Expand All @@ -775,13 +711,11 @@ void bluetoothStart(bool skipOnlineCheck)
// bluetoothPinned = true;

// vTaskDelete(nullptr); // Delete task once it has run once
// #endif // COMPILE_BT
// }

// This function stops BT so that it can be restarted later
void bluetoothStop()
{
#ifdef COMPILE_BT
if (online.bluetooth)
{
if (settings.debugNetworkLayer)
Expand Down Expand Up @@ -866,7 +800,6 @@ void bluetoothStop()
reportHeapNow(false);
online.bluetooth = false;
}
#endif // COMPILE_BT
bluetoothIncomingRTCM = false;
}

Expand Down Expand Up @@ -906,7 +839,6 @@ void bluetoothPrintStatus()
// Send over dedicated BLE service
void bluetoothSendBatteryPercent(int batteryLevelPercent)
{
#ifdef COMPILE_BT
if (bluetoothGetState() == BT_OFF)
return;

Expand All @@ -915,5 +847,6 @@ void bluetoothSendBatteryPercent(int batteryLevelPercent)
return;

bluetoothBatteryService.reportBatteryPercent(batteryLevelPercent);
}

#endif // COMPILE_BT
}
24 changes: 23 additions & 1 deletion Firmware/RTK_Everywhere/Developer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,28 @@ bool webServerIsRunning() {return false;}

#endif // COMPILE_AP

//----------------------------------------
// Global Bluetooth Routines
//----------------------------------------

#ifndef COMPILE_BT
int bluetoothCommandAvailable() {return 0;}
bool bluetoothCommandIsConnected() {return false;}
uint8_t bluetoothCommandRead() {return 0;}
int bluetoothCommandWrite(const uint8_t *buffer, int length) {return 0;}
void bluetoothFlush() {}
byte bluetoothGetState() {return BT_OFF;}
void bluetoothPrintStatus() {}
uint8_t bluetoothRead() {return 0;}
int bluetoothRxDataAvailable() {return 0;}
void bluetoothSendBatteryPercent(int batteryLevelPercent) {}
void bluetoothStart() {}
void bluetoothStartSkipOnlineCheck() {}
void bluetoothStop() {}
void bluetoothUpdate() {}
int bluetoothWrite(const uint8_t *buffer, int length) {return 0;}
#endif // COMPILE_BT

//----------------------------------------
// ESP-NOW
//----------------------------------------
Expand Down Expand Up @@ -322,4 +344,4 @@ void convertGnssTimeToEpoch(uint32_t *epochSecs, uint32_t *epochMicros) {
void beginAuthCoPro(TwoWire *i2cBus) {systemPrintln("**MFi Authentication Not Compiled**");}
void updateAuthCoPro() {}

#endif // COMPILE_AUTHENTICATION
#endif // COMPILE_AUTHENTICATION
2 changes: 2 additions & 0 deletions Firmware/RTK_Everywhere/RTK_Everywhere.ino
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,8 @@ float batteryChargingPercentPerHour;
#include "bluetoothSelect.h"
#endif // COMPILE_BT

BluetoothRadioType_e bluetoothRadioPreviousOnType = BLUETOOTH_RADIO_OFF;

// This value controls the data that is output from the USB serial port
// to the host PC. By default (false) status and debug messages are output
// to the USB serial port. When this value is set to true then the status
Expand Down