#include "SerialBLEInterface.h" void SerialBLEInterface::begin(const char* device_name, uint32_t pin_code) { char charpin[20]; sprintf(charpin, "%d", pin_code); Bluefruit.configPrphBandwidth(BANDWIDTH_MAX); Bluefruit.configPrphConn(250, BLE_GAP_EVENT_LENGTH_MIN, 16, 16); // increase MTU Bluefruit.begin(); Bluefruit.setTxPower(4); // Check bluefruit.h for supported values Bluefruit.setName(device_name); Bluefruit.Security.setMITM(true); Bluefruit.Security.setPIN(charpin); // To be consistent OTA DFU should be added first if it exists //bledfu.begin(); } void SerialBLEInterface::startAdv() { // Advertising packet Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); Bluefruit.Advertising.addTxPower(); // Include the BLE UART (AKA 'NUS') 128-bit UUID Bluefruit.Advertising.addService(bleuart); // Secondary Scan Response packet (optional) // Since there is no room for 'Name' in Advertising packet Bluefruit.ScanResponse.addName(); /* Start Advertising * - Enable auto advertising if disconnected * - Interval: fast mode = 20 ms, slow mode = 152.5 ms * - Timeout for fast mode is 30 seconds * - Start(timeout) with timeout = 0 will advertise forever (until connected) * * For recommended advertising interval * https://developer.apple.com/library/content/qa/qa1931/_index.html */ Bluefruit.Advertising.restartOnDisconnect(true); Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds } // ---------- public methods void SerialBLEInterface::enable() { if (_isEnabled) return; _isEnabled = true; clearBuffers(); // Configure and start the BLE Uart service bleuart.setPermission(SECMODE_ENC_WITH_MITM, SECMODE_ENC_WITH_MITM); bleuart.begin(); // Start advertising startAdv(); checkAdvRestart = false; } void SerialBLEInterface::disable() { _isEnabled = false; BLE_DEBUG_PRINTLN("SerialBLEInterface::disable"); Bluefruit.Advertising.stop(); oldDeviceConnected = deviceConnected = false; checkAdvRestart = false; } size_t SerialBLEInterface::writeFrame(const uint8_t src[], size_t len) { if (len > MAX_FRAME_SIZE) { BLE_DEBUG_PRINTLN("writeFrame(), frame too big, len=%d", len); return 0; } if (deviceConnected && len > 0) { if (send_queue_len >= FRAME_QUEUE_SIZE) { BLE_DEBUG_PRINTLN("writeFrame(), send_queue is full!"); return 0; } send_queue[send_queue_len].len = len; // add to send queue memcpy(send_queue[send_queue_len].buf, src, len); send_queue_len++; return len; } return 0; } #define BLE_WRITE_MIN_INTERVAL 60 bool SerialBLEInterface::isWriteBusy() const { return millis() < _last_write + BLE_WRITE_MIN_INTERVAL; // still too soon to start another write? } size_t SerialBLEInterface::checkRecvFrame(uint8_t dest[]) { if (send_queue_len > 0 // first, check send queue && millis() >= _last_write + BLE_WRITE_MIN_INTERVAL // space the writes apart ) { _last_write = millis(); bleuart.write(send_queue[0].buf, send_queue[0].len); BLE_DEBUG_PRINTLN("writeBytes: sz=%d, hdr=%d", (uint32_t)send_queue[0].len, (uint32_t) send_queue[0].buf[0]); send_queue_len--; for (int i = 0; i < send_queue_len; i++) { // delete top item from queue send_queue[i] = send_queue[i + 1]; } } else { int len = bleuart.available(); if (len > 0) { deviceConnected = true; // should probably use the callback to monitor cx bleuart.readBytes(dest, len); BLE_DEBUG_PRINTLN("readBytes: sz=%d, hdr=%d", len, (uint32_t) dest[0]); return len; } } if (Bluefruit.connected() == 0) deviceConnected = false; if (deviceConnected != oldDeviceConnected) { if (!deviceConnected) { // disconnecting clearBuffers(); BLE_DEBUG_PRINTLN("SerialBLEInterface -> disconnecting..."); delay(500); // give the bluetooth stack the chance to get things ready checkAdvRestart = true; } else { BLE_DEBUG_PRINTLN("SerialBLEInterface -> stopping advertising"); BLE_DEBUG_PRINTLN("SerialBLEInterface -> connecting..."); // connecting // do stuff here on connecting Bluefruit.Advertising.stop(); checkAdvRestart = false; } oldDeviceConnected = deviceConnected; } if (checkAdvRestart) { if (Bluefruit.connected() == 0) { BLE_DEBUG_PRINTLN("SerialBLEInterface -> re-starting advertising"); startAdv(); } checkAdvRestart = false; } return 0; } bool SerialBLEInterface::isConnected() const { return deviceConnected; //pServer != NULL && pServer->getConnectedCount() > 0; }