Bluetooth hang (#4632)
* tunerstudio: send BT disconnect notifications only for BT channel * bluetooth: btProcessIsRunning duplicates btProcessIsStarted * bluetooth: some comments * bluetooth: no need to call chThdExit(), just exit thread function * bluetooth: avoid possible race conditions: set flag first * bluetooth: make "bluetooth_cancel" do something
This commit is contained in:
parent
738bfe5de3
commit
06638a5993
|
@ -20,7 +20,6 @@
|
||||||
#if EFI_BLUETOOTH_SETUP
|
#if EFI_BLUETOOTH_SETUP
|
||||||
|
|
||||||
static bool btProcessIsStarted = false;
|
static bool btProcessIsStarted = false;
|
||||||
static bool btProcessIsRunning = false;
|
|
||||||
static const char *commands[5];
|
static const char *commands[5];
|
||||||
static int numCommands = 0;
|
static int numCommands = 0;
|
||||||
static int setBaudIdx = -1;
|
static int setBaudIdx = -1;
|
||||||
|
@ -70,7 +69,7 @@ static void runCommands() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool restoreAndExit = (cmdIdx >= numCommands || baudIdx < 0);
|
bool restoreAndExit = (cmdIdx >= numCommands || baudIdx < 0) || chThdShouldTerminateX();
|
||||||
|
|
||||||
// if the baud rate is changed, reinit the UART
|
// if the baud rate is changed, reinit the UART
|
||||||
if (baudIdx != prevBaudIdx || restoreAndExit) {
|
if (baudIdx != prevBaudIdx || restoreAndExit) {
|
||||||
|
@ -105,10 +104,12 @@ static void runCommands() {
|
||||||
bool wasAnswer = false;
|
bool wasAnswer = false;
|
||||||
int received = tsChannel->readTimeout(buffer, 2, btModuleTimeout);
|
int received = tsChannel->readTimeout(buffer, 2, btModuleTimeout);
|
||||||
if (received == 2) {
|
if (received == 2) {
|
||||||
|
efiPrintf("received %d byte(s) %d [%c][%c]", received, wasAnswer, buffer[0], buffer[1]);
|
||||||
wasAnswer = (buffer[0] == 'O' && buffer[1] == 'K') ||
|
wasAnswer = (buffer[0] == 'O' && buffer[1] == 'K') ||
|
||||||
(buffer[0] == '+' && (buffer[1] >= 'A' && buffer[1] <= 'Z'));
|
(buffer[0] == '+' && (buffer[1] >= 'A' && buffer[1] <= 'Z'));
|
||||||
|
} else {
|
||||||
|
efiPrintf("timeout receiving BT reply");
|
||||||
}
|
}
|
||||||
efiPrintf("received %d byte(s) %d [%c][%c]", received, wasAnswer, buffer[0], buffer[1]);
|
|
||||||
|
|
||||||
// wait 1 second and skip all remaining response bytes from the bluetooth module
|
// wait 1 second and skip all remaining response bytes from the bluetooth module
|
||||||
while (true) {
|
while (true) {
|
||||||
|
@ -155,18 +156,16 @@ static THD_FUNCTION(btThreadEntryPoint, arg) {
|
||||||
chSysUnlock();
|
chSysUnlock();
|
||||||
|
|
||||||
if (msg == MSG_TIMEOUT) {
|
if (msg == MSG_TIMEOUT) {
|
||||||
|
// timeout waiting for silence on uart...
|
||||||
efiPrintf("The Bluetooth module init procedure is cancelled (timeout)!");
|
efiPrintf("The Bluetooth module init procedure is cancelled (timeout)!");
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
// call this when the thread is resumed (after the disconnect)
|
// call this when the thread is resumed (after the disconnect)
|
||||||
btProcessIsRunning = true;
|
|
||||||
runCommands();
|
runCommands();
|
||||||
btProcessIsRunning = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// release the command
|
// release the command
|
||||||
btProcessIsStarted = false;
|
btProcessIsStarted = false;
|
||||||
chThdExit(MSG_OK);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void bluetoothStart(bluetooth_module_e moduleType, const char *baudRate, const char *name, const char *pinCode) {
|
void bluetoothStart(bluetooth_module_e moduleType, const char *baudRate, const char *name, const char *pinCode) {
|
||||||
|
@ -270,13 +269,15 @@ void bluetoothStart(bluetooth_module_e moduleType, const char *baudRate, const c
|
||||||
commands[numCommands++] = cmdBaud;
|
commands[numCommands++] = cmdBaud;
|
||||||
commands[numCommands++] = cmdName;
|
commands[numCommands++] = cmdName;
|
||||||
commands[numCommands++] = cmdPin;
|
commands[numCommands++] = cmdPin;
|
||||||
|
|
||||||
// create a thread to execute these commands later
|
|
||||||
btThread = chThdCreateStatic(btThreadStack, sizeof(btThreadStack), PRIO_CONSOLE, (tfunc_t)btThreadEntryPoint, NULL);
|
|
||||||
|
|
||||||
btProcessIsStarted = true;
|
btProcessIsStarted = true;
|
||||||
|
|
||||||
|
// create a thread to execute these commands after TS disconnected
|
||||||
|
// See bluetoothSoftwareDisconnectNotify
|
||||||
|
btThread = chThdCreateStatic(btThreadStack, sizeof(btThreadStack), PRIO_CONSOLE, (tfunc_t)btThreadEntryPoint, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Called after 1S of silence on BT UART...
|
||||||
void bluetoothSoftwareDisconnectNotify() {
|
void bluetoothSoftwareDisconnectNotify() {
|
||||||
if (btProcessIsStarted) {
|
if (btProcessIsStarted) {
|
||||||
// start communication with the module
|
// start communication with the module
|
||||||
|
@ -291,9 +292,6 @@ void bluetoothCancel() {
|
||||||
efiPrintf("The Bluetooth module init procedure was not started! Nothing to cancel!");
|
efiPrintf("The Bluetooth module init procedure was not started! Nothing to cancel!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (btProcessIsRunning)
|
|
||||||
return;
|
|
||||||
|
|
||||||
// terminate thread
|
// terminate thread
|
||||||
chThdTerminate(btThread);
|
chThdTerminate(btThread);
|
||||||
|
|
|
@ -433,9 +433,11 @@ static int tsProcessOne(TsChannelBase* tsChannel) {
|
||||||
if (received != 1) {
|
if (received != 1) {
|
||||||
// tunerStudioError("ERROR: no command");
|
// tunerStudioError("ERROR: no command");
|
||||||
#if EFI_BLUETOOTH_SETUP
|
#if EFI_BLUETOOTH_SETUP
|
||||||
// no data in a whole second means time to disconnect BT
|
if (tsChannel == getBluetoothChannel()) {
|
||||||
// assume there's connection loss and notify the bluetooth init code
|
// no data in a whole second means time to disconnect BT
|
||||||
bluetoothSoftwareDisconnectNotify();
|
// assume there's connection loss and notify the bluetooth init code
|
||||||
|
bluetoothSoftwareDisconnectNotify();
|
||||||
|
}
|
||||||
#endif /* EFI_BLUETOOTH_SETUP */
|
#endif /* EFI_BLUETOOTH_SETUP */
|
||||||
tsChannel->in_sync = false;
|
tsChannel->in_sync = false;
|
||||||
return -1;
|
return -1;
|
||||||
|
|
Loading…
Reference in New Issue