Skip to content

Commit 9490b24

Browse files
committed
Add bluetoothRadioPreviousOnType and bluetoothStartSkipOnlineCheck
1 parent 60efba8 commit 9490b24

File tree

1 file changed

+79
-32
lines changed

1 file changed

+79
-32
lines changed

Firmware/RTK_Everywhere/Bluetooth.ino

Lines changed: 79 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ BleBatteryService bluetoothBatteryService;
4848

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

51+
BluetoothRadioType_e bluetoothRadioPreviousOnType = BLUETOOTH_RADIO_OFF;
52+
5153
#endif // COMPILE_BT
5254

5355
//----------------------------------------
@@ -309,7 +311,7 @@ int bluetoothWrite(const uint8_t *buffer, int length)
309311
{
310312
#ifdef COMPILE_BT
311313
if (bluetoothGetState() == BT_OFF)
312-
return 0;
314+
return length; // Avoid buffer full warnings
313315

314316
if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP_AND_BLE)
315317
{
@@ -378,7 +380,7 @@ int bluetoothWrite(uint8_t value)
378380
{
379381
#ifdef COMPILE_BT
380382
if (bluetoothGetState() == BT_OFF)
381-
return 0;
383+
return 1; // Avoid buffer full warnings
382384

383385
if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP_AND_BLE)
384386
{
@@ -446,36 +448,66 @@ void BTConfirmRequestCallback(uint32_t numVal) {
446448
// TODO: if the RTK device has an OLED, we should display the PIN so user can confirm
447449
}
448450

451+
void deviceNameSpacesToUnderscores()
452+
{
453+
for (size_t i = 0; i < strlen(deviceName); i++)
454+
{
455+
if (deviceName[i] == ' ')
456+
deviceName[i] = '_';
457+
}
458+
}
459+
460+
void deviceNameUnderscoresToSpaces()
461+
{
462+
for (size_t i = 0; i < strlen(deviceName); i++)
463+
{
464+
if (deviceName[i] == '_')
465+
deviceName[i] = ' ';
466+
}
467+
}
468+
449469
// Begin Bluetooth with a broadcast name of 'SparkFun Postcard-XXXX' or 'SparkPNT Facet mosaicX5-XXXX'
450470
// Add 4 characters of device's MAC address to end of the broadcast name
451471
// This allows users to discern between multiple devices in the local area
452472
void bluetoothStart()
473+
{
474+
bluetoothStart(false);
475+
}
476+
void bluetoothStartSkipOnlineCheck()
477+
{
478+
bluetoothStart(true);
479+
}
480+
void bluetoothStart(bool skipOnlineCheck)
453481
{
454482
if (settings.bluetoothRadioType == BLUETOOTH_RADIO_OFF)
455483
return;
456484

457-
if (online.bluetooth)
485+
if (!skipOnlineCheck)
458486
{
459-
return;
487+
if (online.bluetooth)
488+
{
489+
return;
490+
}
460491
}
461492

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

466497
char productName[50] = {0};
467498
strncpy(productName, platformPrefix, sizeof(productName));
468499

469-
// BLE is limited to ~28 characters in the device name. Shorten platformPrefix if needed.
470-
if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP_AND_BLE ||
471-
settings.bluetoothRadioType == BLUETOOTH_RADIO_BLE)
472-
{
473-
// Longest platform prefix is currently "Facet mosaicX5". We are just OK.
474-
if (strcmp(productName, "LONG PLATFORM PREFIX") == 0)
475-
{
476-
strncpy(productName, "SHORTER PREFIX", sizeof(productName));
477-
}
478-
}
500+
// Longest platform prefix is currently "Facet mosaicX5". We are just OK.
501+
// We currently don't need this:
502+
// // BLE is limited to ~28 characters in the device name. Shorten platformPrefix if needed.
503+
// if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP_AND_BLE ||
504+
// settings.bluetoothRadioType == BLUETOOTH_RADIO_BLE)
505+
// {
506+
// if (strcmp(productName, "LONG PLATFORM PREFIX") == 0)
507+
// {
508+
// strncpy(productName, "SHORTER PREFIX", sizeof(productName));
509+
// }
510+
// }
479511

480512
RTKBrandAttribute *brandAttributes = getBrandAttributeFromBrand(present.brand);
481513

@@ -492,9 +524,7 @@ void bluetoothStart()
492524
}
493525

494526
// Select Bluetooth setup
495-
if (settings.bluetoothRadioType == BLUETOOTH_RADIO_OFF)
496-
return;
497-
else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP_AND_BLE)
527+
if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP_AND_BLE)
498528
{
499529
if (bluetoothSerialSpp == nullptr)
500530
bluetoothSerialSpp = new BTClassicSerial();
@@ -722,6 +752,7 @@ void bluetoothStart()
722752
bluetoothState = BT_NOTCONNECTED;
723753
reportHeapNow(false);
724754
online.bluetooth = true;
755+
bluetoothRadioPreviousOnType = settings.bluetoothRadioType;
725756
} // if (1)
726757
#endif // COMPILE_BT
727758
}
@@ -756,59 +787,75 @@ void bluetoothStop()
756787

757788
bluetoothState = BT_OFF; // Indicate to tasks that BT is unavailable
758789

790+
// Stop BLE Command Task if BLE is enabled
791+
if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP_AND_BLE ||
792+
settings.bluetoothRadioType == BLUETOOTH_RADIO_BLE)
793+
{
794+
task.bluetoothCommandTaskStopRequest = true;
795+
while (task.bluetoothCommandTaskRunning == true)
796+
delay(1);
797+
}
798+
799+
// end and delete BT instances
759800
if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP_AND_BLE)
760801
{
761802
bluetoothSerialBle->flush(); // Complete any transfers
762803
bluetoothSerialBle->disconnect(); // Drop any clients
763804
bluetoothSerialBle->end(); // Release resources
764-
delete bluetoothSerialBle;
765-
bluetoothSerialBle = nullptr;
805+
//delete bluetoothSerialBle;
806+
//bluetoothSerialBle = nullptr;
766807

767808
bluetoothSerialBleCommands->flush(); // Complete any transfers
768809
bluetoothSerialBleCommands->disconnect(); // Drop any clients
769810
bluetoothSerialBleCommands->end(); // Release resources
770-
delete bluetoothSerialBleCommands;
771-
bluetoothSerialBleCommands = nullptr;
811+
//delete bluetoothSerialBleCommands;
812+
//bluetoothSerialBleCommands = nullptr;
772813

773814
bluetoothSerialSpp->flush(); // Complete any transfers
774815
bluetoothSerialSpp->disconnect(); // Drop any clients
816+
bluetoothSerialSpp->register_callback(nullptr);
775817
bluetoothSerialSpp->end(); // Release resources
776-
delete bluetoothSerialSpp;
777-
bluetoothSerialSpp = nullptr;
818+
//bluetoothSerialSpp->memrelease(); // Release memory
819+
//delete bluetoothSerialSpp;
820+
//bluetoothSerialSpp = nullptr;
778821

779822
bluetoothBatteryService.end();
780823
}
781824
else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP)
782825
{
783826
bluetoothSerialSpp->flush(); // Complete any transfers
784827
bluetoothSerialSpp->disconnect(); // Drop any clients
828+
bluetoothSerialSpp->register_callback(nullptr);
785829
bluetoothSerialSpp->end(); // Release resources
786-
delete bluetoothSerialSpp;
787-
bluetoothSerialSpp = nullptr;
830+
//bluetoothSerialSpp->memrelease(); // Release memory
831+
//delete bluetoothSerialSpp;
832+
//bluetoothSerialSpp = nullptr;
788833
}
789834
else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_BLE)
790835
{
791836
bluetoothSerialBle->flush(); // Complete any transfers
792837
bluetoothSerialBle->disconnect(); // Drop any clients
793838
bluetoothSerialBle->end(); // Release resources
794-
delete bluetoothSerialBle;
795-
bluetoothSerialBle = nullptr;
839+
//delete bluetoothSerialBle;
840+
//bluetoothSerialBle = nullptr;
796841

797842
bluetoothSerialBleCommands->flush(); // Complete any transfers
798843
bluetoothSerialBleCommands->disconnect(); // Drop any clients
799844
bluetoothSerialBleCommands->end(); // Release resources
800-
delete bluetoothSerialBleCommands;
801-
bluetoothSerialBleCommands = nullptr;
845+
//delete bluetoothSerialBleCommands;
846+
//bluetoothSerialBleCommands = nullptr;
802847

803848
bluetoothBatteryService.end();
804849
}
805850
else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP_ACCESSORY_MODE)
806851
{
807852
bluetoothSerialSpp->flush(); // Complete any transfers
808853
bluetoothSerialSpp->disconnect(); // Drop any clients
854+
bluetoothSerialSpp->register_callback(nullptr);
809855
bluetoothSerialSpp->end(); // Release resources
810-
delete bluetoothSerialSpp;
811-
bluetoothSerialSpp = nullptr;
856+
//bluetoothSerialSpp->memrelease(); // Release memory
857+
//delete bluetoothSerialSpp;
858+
//bluetoothSerialSpp = nullptr;
812859
}
813860

814861
if (settings.debugNetworkLayer)

0 commit comments

Comments
 (0)