23#if EFI_BLUETOOTH_SETUP
25#ifndef EFI_BLUETOOTH_SETUP_DEBUG
26#define EFI_BLUETOOTH_SETUP_DEBUG TRUE
57#if EFI_BLUETOOTH_SETUP_DEBUG
58 efiPrintf(
"sending %s", str);
60 tsChannel->
write((uint8_t *)str, strlen(str));
69 efiPrintf(
"BT reply is unexpectedly long");
73 efiPrintf(
"Timeout waiting for BT reply after %d byte(s)", len);
76 }
while (str[len++] !=
'\n');
84#if EFI_BLUETOOTH_SETUP_DEBUG
86 efiPrintf(
"Received %d %s", len, str);
101 if (strncmp(tmp,
"+OK", 3) == 0)
115 len =
btReadLine(tsChannel, tmp,
sizeof(tmp));
134#if EFI_BLUETOOTH_SETUP_DEBUG
136 btWrite(tsChannel,
"AT+VERSION\r\n");
139 btWrite(tsChannel,
"AT+BAUD\r\n");
143 btWrite(tsChannel,
"AT+TYPE\r\n");
147 btWrite(tsChannel,
"AT+PIN\r\n");
150 btWrite(tsChannel,
"AT+LADDR\r\n");
154 btWrite(tsChannel,
"AT+STAT\r\n");
161 btWrite(tsChannel,
"AT+DEFAULT\r\n");
165 btWrite(tsChannel,
"AT+ENLOG0\r\n");
170 btWrite(tsChannel,
"AT+TYPE0\r\n");
177 chThdSleepMilliseconds(10);
182 chsnprintf(tmp,
sizeof(tmp),
"AT+NAME=%s\r\n",
btName);
184 chsnprintf(tmp,
sizeof(tmp),
"AT+NAME%s\r\n",
btName);
191 chsnprintf(tmp,
sizeof(tmp),
"AT+NAMB%s-BLE\r\n",
btName);
199 chsnprintf(tmp,
sizeof(tmp),
"AT+PSWD=%s\r\n",
btPinCode);
201 chsnprintf(tmp,
sizeof(tmp),
"AT+PIN%s\r\n",
btPinCode);
220 btWrite(tsChannel,
"AT+RESET\r\n");
222 efiPrintf(
"JDY3x failed to reset");
226 efiPrintf(
"SUCCESS! All commands passed to the Bluetooth module!");
230 efiPrintf(
"FAIL! Command %s failed", tmp);
235 for(uint8_t baudIdx=0; baudIdx < efi::size(
baudRates); baudIdx++) {
237 chThdSleepMilliseconds(10);
240 efiPrintf(
"Failed to find current BT module baudrate");
247 chThdSleepMilliseconds(10);
255 btWrite(tsChannel,
"AT+DISC\r\n");
257 efiPrintf(
"JDY33 disconnected");
258 chThdSleepMilliseconds(10);
263 btWrite(tsChannel,
"AT+BAUD\r\n");
265 efiPrintf(
"JDY31 disconnected");
266 chThdSleepMilliseconds(10);
276 static const char *usage =
"Usage: bluetooth_<hc05/hc06/bk/jdy> <baud> <name> <pincode>";
278 if ((baudRate ==
nullptr) || (name ==
nullptr) || (pinCode ==
nullptr)) {
279 efiPrintf(
"%s", usage);
284 efiPrintf(
"This firmware does not support bluetooth [%s]",
getTsSignature());
289 efiPrintf(
"The Bluetooth module init procedure is already started!");
295 int baud = atoi(baudRate);
298 for (
size_t i = 0; i < efi::size(
baudRates); i++) {
307 efiPrintf(
"Wrong <baud> parameter '%s'! %s", baudRate, usage);
312 if ((strlen(name) < 1) || (strlen(name) > 20)) {
313 efiPrintf(
"Wrong <name> parameter! Up to 20 characters expected! %s", usage);
318 if (strlen(pinCode) != 4) {
319 efiPrintf(
"Wrong <pincode> parameter! 4 digits expected! %s", usage);
322 for (
int i = 0; i < 4; i++) {
323 if (!isdigit(pinCode[i])) {
324 efiPrintf(
"<pincode> should contain digits only %s", usage);
330 strncpy(
btName, name, 20);
340 efiPrintf(
"*** Bluetooth module setup procedure ***");
345 efiPrintf(
"!Warning! Please make sure you're not currently using the BT module for communication (not paired)!");
346 efiPrintf(
"TO START THE PROCEDURE: PLEASE DISCONNECT YOUR PC COM-PORT FROM THE BOARD NOW!");
347 efiPrintf(
"After that please don't turn off the board power and wait for ~15 seconds to complete. Then reconnect to the board!");
351 if (tsChannel->
readTimeout(tmp, 1, BLUETOOTH_SILENT_TIMEOUT) != 0) {
352 efiPrintf(
"The Bluetooth module init procedure is cancelled (wait for silent timeout)!");
static volatile bool btSetupIsRequested
static int btWaitOk(SerialTsChannelBase *tsChannel)
static const int btModuleTimeout
void bluetoothSoftwareDisconnectNotify(SerialTsChannelBase *tsChannel)
uint8_t findBaudIndex(SerialTsChannelBase *tsChannel)
static void btWrite(TsChannelBase *tsChannel, const char *str)
static const struct @5 baudRates[]
static int btBaudOk(SerialTsChannelBase *tsChannel)
static uint8_t workingBaudIndex
void bluetoothStart(bluetooth_module_e moduleType, const char *baudRate, const char *name, const char *pinCode)
static int btReadLine(TsChannelBase *tsChannel, char *str, size_t max_len)
static void runCommands(SerialTsChannelBase *tsChannel)
static char btPinCode[4+1]
bluetooth_module_e btModuleType
virtual void start(uint32_t baud)=0
virtual void write(const uint8_t *buffer, size_t size, bool isEndOfPacket=false)=0
virtual size_t readTimeout(uint8_t *buffer, size_t size, int timeout)=0
static constexpr engine_configuration_s * engineConfiguration
const char * getTsSignature()
uint32_t tunerStudioSerialSpeed
SerialTsChannelBase * getBluetoothChannel()