rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
Enumerations | Functions
bluetooth.h File Reference

Enumerations

enum  bluetooth_module_e {
  BLUETOOTH_HC_05 , BLUETOOTH_HC_06 , BLUETOOTH_BK3231 , BLUETOOTH_JDY_3x ,
  BLUETOOTH_JDY_31
}
 

Functions

void bluetoothStart (bluetooth_module_e moduleType, const char *baudRate, const char *name, const char *pinCode)
 
void bluetoothSoftwareDisconnectNotify (SerialTsChannelBase *tsChannel)
 
uint8_t findBaudIndex (SerialTsChannelBase *tsChannel)
 

Enumeration Type Documentation

◆ bluetooth_module_e

Enumerator
BLUETOOTH_HC_05 
BLUETOOTH_HC_06 
BLUETOOTH_BK3231 

See https://rusefi.com/forum/viewtopic.php?f=13&t=1999

BLUETOOTH_JDY_3x 
BLUETOOTH_JDY_31 

Definition at line 21 of file bluetooth.h.

21 {
24 /**
25 * See https://rusefi.com/forum/viewtopic.php?f=13&t=1999
26 */
28 // fun fact: those use BK3232 see above
bluetooth_module_e
Definition bluetooth.h:21
@ BLUETOOTH_HC_05
Definition bluetooth.h:22
@ BLUETOOTH_BK3231
Definition bluetooth.h:27
@ BLUETOOTH_JDY_3x
Definition bluetooth.h:29
@ BLUETOOTH_JDY_31
Definition bluetooth.h:30
@ BLUETOOTH_HC_06
Definition bluetooth.h:23

Function Documentation

◆ bluetoothSoftwareDisconnectNotify()

void bluetoothSoftwareDisconnectNotify ( SerialTsChannelBase *  tsChannel)

Called by runBinaryProtocolLoop() if a connection disconnect is detected. Bluetooth init code needs to make sure that there's no interference of the BT module and USB-UART (connected to PC)

Definition at line 338 of file bluetooth.cpp.

338 {
339 if (btSetupIsRequested) {
340 efiPrintf("*** Bluetooth module setup procedure ***");
341
342 /* JDY33 & JDY31 supports disconnect on request */
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!");
348 }
349
350 uint8_t tmp[1];
351 if (tsChannel->readTimeout(tmp, 1, BLUETOOTH_SILENT_TIMEOUT) != 0) {
352 efiPrintf("The Bluetooth module init procedure is cancelled (wait for silent timeout)!");
353 btSetupIsRequested = false;
354 return;
355 }
356
357 runCommands(tsChannel);
358 btSetupIsRequested = false;
359 }
360}
static volatile bool btSetupIsRequested
Definition bluetooth.cpp:29
static void runCommands(SerialTsChannelBase *tsChannel)
bluetooth_module_e btModuleType
Definition bluetooth.cpp:31
virtual size_t readTimeout(uint8_t *buffer, size_t size, int timeout)=0

Referenced by tsProcessOne().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ bluetoothStart()

void bluetoothStart ( bluetooth_module_e  moduleType,
const char *  baudRate,
const char *  name,
const char *  pinCode 
)

Start Bluetooth module initialization using UART connection:

  • wait for PC communication disconnect;
  • reconfigure the UART;
  • send AT-commands to the module;
  • restore connection to PC.

Definition at line 275 of file bluetooth.cpp.

275 {
276 static const char *usage = "Usage: bluetooth_<hc05/hc06/bk/jdy> <baud> <name> <pincode>";
277
278 if ((baudRate == nullptr) || (name == nullptr) || (pinCode == nullptr)) {
279 efiPrintf("%s", usage);
280 return;
281 }
282
283 if (getBluetoothChannel() == nullptr) {
284 efiPrintf("This firmware does not support bluetooth [%s]", getTsSignature());
285 return;
286 }
287
288 if (btSetupIsRequested) {
289 efiPrintf("The Bluetooth module init procedure is already started!");
290 return;
291 }
292
293 // now check the arguments and add other commands:
294 // 1) baud rate
295 int baud = atoi(baudRate);
296 // find a known baud rate in our list
297 setBaudIdx = -1;
298 for (size_t i = 0; i < efi::size(baudRates); i++) {
299 if ((int)baudRates[i].rate == baud) {
300 setBaudIdx = i;
301 break;
302 }
303 }
304 // check the baud rate index
305 if (setBaudIdx < 0) {
306 // unknown baud rate
307 efiPrintf("Wrong <baud> parameter '%s'! %s", baudRate, usage);
308 return;
309 }
310
311 // 2) check name
312 if ((strlen(name) < 1) || (strlen(name) > 20)) {
313 efiPrintf("Wrong <name> parameter! Up to 20 characters expected! %s", usage);
314 return;
315 }
316
317 // 3) check pin code
318 if (strlen(pinCode) != 4) {
319 efiPrintf("Wrong <pincode> parameter! 4 digits expected! %s", usage);
320 return;
321 }
322 for (int i = 0; i < 4; i++) {
323 if (!isdigit(pinCode[i])) {
324 efiPrintf("<pincode> should contain digits only %s", usage);
325 return;
326 }
327 }
328
329 /* copy settings */
330 strncpy(btName, name, 20);
331 strncpy(btPinCode, pinCode, 4);
332
333 btModuleType = moduleType;
334 btSetupIsRequested = true;
335}
static const struct @5 baudRates[]
static int setBaudIdx
Definition bluetooth.cpp:32
static char btName[20+1]
Definition bluetooth.cpp:33
static char btPinCode[4+1]
Definition bluetooth.cpp:34
const char * getTsSignature()
Definition signature.cpp:31
SerialTsChannelBase * getBluetoothChannel()

Referenced by startTunerStudioConnectivity().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ findBaudIndex()

uint8_t findBaudIndex ( SerialTsChannelBase *  tsChannel)

Called during bluetooth initialization. Checks to see if module responds to common baud rates returns the index of the found baud

Definition at line 233 of file bluetooth.cpp.

233 {
234 // find current baudrate
235 for(uint8_t baudIdx=0; baudIdx < efi::size(baudRates); baudIdx++) {
236 tsChannel->stop();
237 chThdSleepMilliseconds(10); // safety
238
239 if (baudIdx == efi::size(baudRates)) {
240 efiPrintf("Failed to find current BT module baudrate");
242 return 255;//failed
243 }
244
245 efiPrintf("Restarting at %lu", baudRates[baudIdx].rate);
246 tsChannel->start(baudRates[baudIdx].rate);
247 chThdSleepMilliseconds(10); // safety
248
249 /* Ping BT module */
250 btWrite(tsChannel, "AT\r\n");
251 if (btWaitOk(tsChannel) == 0) {
252 return baudIdx;
253 } else if (btModuleType == BLUETOOTH_JDY_3x) {
254 /* try to disconnect in case device already configured and in silence mode */
255 btWrite(tsChannel, "AT+DISC\r\n");
256 if (btWaitOk(tsChannel) == 0) {
257 efiPrintf("JDY33 disconnected");
258 chThdSleepMilliseconds(10); // safety
259 return baudIdx;
260 }
261 } else if (btModuleType == BLUETOOTH_JDY_31) {
262 /* try to disconnect in case device already configured and in silence mode */
263 btWrite(tsChannel, "AT+BAUD\r\n");
264 if (btBaudOk(tsChannel) == 0) {
265 efiPrintf("JDY31 disconnected");
266 chThdSleepMilliseconds(10); // safety
267 return baudIdx;
268 }
269 }
270 /* try next baudrate */
271 }
272 return 255;//failed
273}
static int btWaitOk(SerialTsChannelBase *tsChannel)
Definition bluetooth.cpp:93
static void btWrite(TsChannelBase *tsChannel, const char *str)
Definition bluetooth.cpp:54
uint32_t rate
Definition bluetooth.cpp:39
static int btBaudOk(SerialTsChannelBase *tsChannel)
virtual void start(uint32_t baud)=0
virtual void stop()
static constexpr engine_configuration_s * engineConfiguration

Referenced by runCommands().

Here is the call graph for this function:
Here is the caller graph for this function:

Go to the source code of this file.