From 8e38e1bd49e38f66f828c6aa81031d60336a65ec Mon Sep 17 00:00:00 2001 From: Aleksejs Popovs Date: Wed, 21 Jan 2026 21:44:31 -0500 Subject: [PATCH] bin/clang-format-fix --- .../bluetooth/BluetoothActivity.cpp | 135 ++++++++---------- src/activities/bluetooth/BluetoothActivity.h | 53 +++---- src/main.cpp | 15 +- 3 files changed, 88 insertions(+), 115 deletions(-) diff --git a/src/activities/bluetooth/BluetoothActivity.cpp b/src/activities/bluetooth/BluetoothActivity.cpp index 8f387c84..5cf3b70d 100644 --- a/src/activities/bluetooth/BluetoothActivity.cpp +++ b/src/activities/bluetooth/BluetoothActivity.cpp @@ -6,23 +6,22 @@ #include "fontIds.h" #include "util/StringUtils.h" -#define DEVICE_NAME "EPaper" -#define SERVICE_UUID "4ae29d01-499a-480a-8c41-a82192105125" -#define REQUEST_CHARACTERISTIC_UUID "a00e530d-b48b-48c8-aadb-d062a1b91792" +#define DEVICE_NAME "EPaper" +#define SERVICE_UUID "4ae29d01-499a-480a-8c41-a82192105125" +#define REQUEST_CHARACTERISTIC_UUID "a00e530d-b48b-48c8-aadb-d062a1b91792" #define RESPONSE_CHARACTERISTIC_UUID "0c656023-dee6-47c5-9afb-e601dfbdaa1d" #define OUTPUT_DIRECTORY "/bt" #define MAX_FILENAME_LENGTH 200 -#define PROTOCOL_ASSERT(cond, fmt, ...) \ - do { \ - if (!(cond)) \ - { \ - snprintf(errorMessage, sizeof(errorMessage), fmt, ##__VA_ARGS__); \ - intoState(STATE_ERROR); \ - return; \ - } \ - } while (0) +#define PROTOCOL_ASSERT(cond, fmt, ...) \ + do { \ + if (!(cond)) { \ + snprintf(errorMessage, sizeof(errorMessage), fmt, ##__VA_ARGS__); \ + intoState(STATE_ERROR); \ + return; \ + } \ + } while (0) void BluetoothActivity::displayTaskTrampoline(void* param) { auto* self = static_cast(param); @@ -42,33 +41,24 @@ void BluetoothActivity::report() { onFileReceived(OUTPUT_DIRECTORY "/" + filename); } -void BluetoothActivity::startAdvertising() { - NimBLEDevice::startAdvertising(); -} +void BluetoothActivity::startAdvertising() { NimBLEDevice::startAdvertising(); } -void BluetoothActivity::stopAdvertising() { - NimBLEDevice::stopAdvertising(); -} +void BluetoothActivity::stopAdvertising() { NimBLEDevice::stopAdvertising(); } void BluetoothActivity::onEnter() { Activity::onEnter(); NimBLEDevice::init(DEVICE_NAME); - NimBLEServer *pServer = NimBLEDevice::createServer(); + NimBLEServer* pServer = NimBLEDevice::createServer(); pServer->setCallbacks(&serverCallbacks, false); - NimBLEService *pService = pServer->createService(SERVICE_UUID); - NimBLECharacteristic *pRequestChar = pService->createCharacteristic( - REQUEST_CHARACTERISTIC_UUID, - NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::WRITE_NR - ); + NimBLEService* pService = pServer->createService(SERVICE_UUID); + NimBLECharacteristic* pRequestChar = + pService->createCharacteristic(REQUEST_CHARACTERISTIC_UUID, NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::WRITE_NR); pRequestChar->setCallbacks(&requestCallbacks); - pResponseChar = pService->createCharacteristic( - RESPONSE_CHARACTERISTIC_UUID, - NIMBLE_PROPERTY::INDICATE - ); + pResponseChar = pService->createCharacteristic(RESPONSE_CHARACTERISTIC_UUID, NIMBLE_PROPERTY::INDICATE); pService->start(); - NimBLEAdvertising *pAdvertising = NimBLEDevice::getAdvertising(); + NimBLEAdvertising* pAdvertising = NimBLEDevice::getAdvertising(); pAdvertising->setName(DEVICE_NAME); pAdvertising->addServiceUUID(pService->getUUID()); pAdvertising->enableScanResponse(true); @@ -106,14 +96,12 @@ void BluetoothActivity::intoState(State newState) { // we cannot call onFileReceived here directly because it might cause onExit to be called, // which calls NimBLEDevice::deinit, which cannot be called from inside a NimBLE callback. xTaskCreate(&BluetoothActivity::reportTaskTrampoline, "BluetoothReportTask", - 2048, // Stack size - this, // Parameters - 1, // Priority, - nullptr - ); + 2048, // Stack size + this, // Parameters + 1, // Priority, + nullptr); break; - case STATE_ERROR: - { + case STATE_ERROR: { // caller sets errorMessage file.close(); NimBLEServer* pServer = NimBLEDevice::getServer(); @@ -229,12 +217,8 @@ void BluetoothActivity::render() const { } // Draw help text at bottom - const auto labels = mappedInput.mapLabels( - "« Back", - (state == STATE_ERROR || state == STATE_DONE) ? "Restart" : "", - "", - "" - ); + const auto labels = + mappedInput.mapLabels("« Back", (state == STATE_ERROR || state == STATE_DONE) ? "Restart" : "", "", ""); renderer.drawButtonHints(UI_10_FONT_ID, labels.btn1, labels.btn2, labels.btn3, labels.btn4); renderer.displayBuffer(); @@ -266,23 +250,24 @@ void BluetoothActivity::onRequest(const lfbt_message* msg, size_t msg_len) { return; } - PROTOCOL_ASSERT((txnId == 0) || (txnId == msg->txnId), "Multiple transfers happening at once (%x != %x)", txnId, msg->txnId); + PROTOCOL_ASSERT((txnId == 0) || (txnId == msg->txnId), "Multiple transfers happening at once (%x != %x)", txnId, + msg->txnId); switch (msg->type) { - case 0: // client_offer + case 0: // client_offer { PROTOCOL_ASSERT(state == STATE_CONNECTED, "Invalid state for client_offer: %d", state); - PROTOCOL_ASSERT(msg->body.clientOffer.version == 1, "Unsupported protocol version: %u", msg->body.clientOffer.version); + PROTOCOL_ASSERT(msg->body.clientOffer.version == 1, "Unsupported protocol version: %u", + msg->body.clientOffer.version); totalBytes = msg->body.clientOffer.bodyLength; - - size_t filenameLength = msg_len - 8 - sizeof(lfbt_msg_client_offer); - std::string originalFilename = StringUtils::sanitizeFilename( - std::string(msg->body.clientOffer.name, filenameLength), - MAX_FILENAME_LENGTH - ); - PROTOCOL_ASSERT(SdMan.ensureDirectoryExists(OUTPUT_DIRECTORY), "Couldn't create output directory %s", OUTPUT_DIRECTORY); + size_t filenameLength = msg_len - 8 - sizeof(lfbt_msg_client_offer); + std::string originalFilename = + StringUtils::sanitizeFilename(std::string(msg->body.clientOffer.name, filenameLength), MAX_FILENAME_LENGTH); + + PROTOCOL_ASSERT(SdMan.ensureDirectoryExists(OUTPUT_DIRECTORY), "Couldn't create output directory %s", + OUTPUT_DIRECTORY); // generate unique filepath auto splitName = StringUtils::splitFileName(originalFilename); @@ -300,43 +285,39 @@ void BluetoothActivity::onRequest(const lfbt_message* msg, size_t msg_len) { filepath = OUTPUT_DIRECTORY "/" + filename; } - PROTOCOL_ASSERT(SdMan.openFileForWrite("BT", filepath, file), "Couldn't open file %s for writing", filepath.c_str()); - // TODO: would be neat to check if we have enough space, but SDCardManager doesn't seem to expose that info currently + PROTOCOL_ASSERT(SdMan.openFileForWrite("BT", filepath, file), "Couldn't open file %s for writing", + filepath.c_str()); + // TODO: would be neat to check if we have enough space, but SDCardManager doesn't seem to expose that info + // currently txnId = msg->txnId; intoState(STATE_OFFERED); - lfbt_message response = { - .type = 1, // server_response - .txnId = txnId, - .body = {.serverResponse = {.status = 0}} - }; + lfbt_message response = {.type = 1, // server_response + .txnId = txnId, + .body = {.serverResponse = {.status = 0}}}; pResponseChar->setValue(reinterpret_cast(&response), 8 + sizeof(lfbt_msg_server_response)); pResponseChar->indicate(); updateRequired = true; break; } - case 2: // client_chunk + case 2: // client_chunk { - Serial.printf( - "[%lu] [BT] Received client_chunk, offset %u, length %zu\n", - millis(), - msg->body.clientChunk.offset, - msg_len - 8 - sizeof(lfbt_msg_client_chunk) - ); + Serial.printf("[%lu] [BT] Received client_chunk, offset %u, length %zu\n", millis(), msg->body.clientChunk.offset, + msg_len - 8 - sizeof(lfbt_msg_client_chunk)); PROTOCOL_ASSERT(state == STATE_OFFERED || state == STATE_RECEIVING, "Invalid state for client_chunk: %d", state); - PROTOCOL_ASSERT(msg->body.clientChunk.offset == receivedBytes, "Expected chunk %zu, got %u", receivedBytes, msg->body.clientChunk.offset); + PROTOCOL_ASSERT(msg->body.clientChunk.offset == receivedBytes, "Expected chunk %zu, got %u", receivedBytes, + msg->body.clientChunk.offset); - size_t written = file.write( - reinterpret_cast(msg->body.clientChunk.body), - msg_len - 8 - sizeof(lfbt_msg_client_chunk) - ); + size_t written = file.write(reinterpret_cast(msg->body.clientChunk.body), + msg_len - 8 - sizeof(lfbt_msg_client_chunk)); PROTOCOL_ASSERT(written > 0, "Couldn't write to file"); receivedBytes += msg_len - 8 - sizeof(lfbt_msg_client_chunk); if (receivedBytes >= totalBytes) { - PROTOCOL_ASSERT(receivedBytes == totalBytes, "Got more bytes than expected: %zu > %zu", receivedBytes, totalBytes); + PROTOCOL_ASSERT(receivedBytes == totalBytes, "Got more bytes than expected: %zu > %zu", receivedBytes, + totalBytes); PROTOCOL_ASSERT(file.close(), "Couldn't finalize writing the file"); intoState(STATE_DONE); } else { @@ -349,12 +330,8 @@ void BluetoothActivity::onRequest(const lfbt_message* msg, size_t msg_len) { } void BluetoothActivity::RequestCallbacks::onWrite(NimBLECharacteristic* pCharacteristic, NimBLEConnInfo& connInfo) { - const lfbt_message *msg = reinterpret_cast(pCharacteristic->getValue().data()); - Serial.printf("[%lu] [BT] Received BLE message of type %u, txnId %x, length %d\n", - millis(), - msg->type, - msg->txnId, - pCharacteristic->getValue().length() - ); + const lfbt_message* msg = reinterpret_cast(pCharacteristic->getValue().data()); + Serial.printf("[%lu] [BT] Received BLE message of type %u, txnId %x, length %d\n", millis(), msg->type, msg->txnId, + pCharacteristic->getValue().length()); activity->onRequest(msg, pCharacteristic->getValue().length()); } diff --git a/src/activities/bluetooth/BluetoothActivity.h b/src/activities/bluetooth/BluetoothActivity.h index 06e90653..42913e3b 100644 --- a/src/activities/bluetooth/BluetoothActivity.h +++ b/src/activities/bluetooth/BluetoothActivity.h @@ -1,33 +1,31 @@ #pragma once +#include +#include +#include +#include #include #include #include -#include -#include -#include - #include -#include - #include "../Activity.h" -typedef struct __attribute__((packed)) { +typedef struct __attribute__((packed)) { uint32_t version; uint32_t bodyLength; uint32_t nameLength; char name[]; -} lfbt_msg_client_offer; // msg type 0 +} lfbt_msg_client_offer; // msg type 0 -typedef struct __attribute__((packed)) { +typedef struct __attribute__((packed)) { uint32_t status; -} lfbt_msg_server_response; // msg type 1 +} lfbt_msg_server_response; // msg type 1 -typedef struct __attribute__((packed)) { +typedef struct __attribute__((packed)) { uint32_t offset; char body[]; -} lfbt_msg_client_chunk; // msg type 2 +} lfbt_msg_client_chunk; // msg type 2 typedef union { lfbt_msg_client_offer clientOffer; @@ -43,7 +41,7 @@ typedef struct __attribute__((packed)) { /** * BluetoothActivity receives files over a custom BLE protocol and stores them on the SD card. - * + * * The onCancel callback is called if the user presses back. * onFileReceived is called when a file is successfully received with the path to the file. */ @@ -62,34 +60,34 @@ class BluetoothActivity final : public Activity { void report(); void onConnected(bool isConnected); - void onRequest(const lfbt_message *msg, size_t msg_len); + void onRequest(const lfbt_message* msg, size_t msg_len); class ServerCallbacks : public NimBLEServerCallbacks { friend class BluetoothActivity; - BluetoothActivity *activity; + BluetoothActivity* activity; void onConnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo); void onDisconnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo, int reason); - protected: - explicit ServerCallbacks(BluetoothActivity *activity) : activity(activity) {} + protected: + explicit ServerCallbacks(BluetoothActivity* activity) : activity(activity) {} }; ServerCallbacks serverCallbacks; class RequestCallbacks : public NimBLECharacteristicCallbacks { friend class BluetoothActivity; - BluetoothActivity *activity; + BluetoothActivity* activity; void onWrite(NimBLECharacteristic* pCharacteristic, NimBLEConnInfo& connInfo); - protected: - explicit RequestCallbacks(BluetoothActivity *activity) : activity(activity) {} + protected: + explicit RequestCallbacks(BluetoothActivity* activity) : activity(activity) {} }; RequestCallbacks requestCallbacks; - NimBLECharacteristic *pResponseChar = nullptr; + NimBLECharacteristic* pResponseChar = nullptr; void startAdvertising(); void stopAdvertising(); @@ -107,7 +105,7 @@ class BluetoothActivity final : public Activity { std::string filename; FsFile file; size_t receivedBytes = 0; - size_t totalBytes = 0; + size_t totalBytes = 0; char errorMessage[256] = {}; uint32_t txnId = 0; @@ -115,10 +113,13 @@ class BluetoothActivity final : public Activity { public: explicit BluetoothActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, - const std::function& onCancel, - const std::function& onFileReceived) - : Activity("Bluetooth", renderer, mappedInput), onCancel(onCancel), onFileReceived(onFileReceived), - serverCallbacks(this), requestCallbacks(this) {} + const std::function& onCancel, + const std::function& onFileReceived) + : Activity("Bluetooth", renderer, mappedInput), + onCancel(onCancel), + onFileReceived(onFileReceived), + serverCallbacks(this), + requestCallbacks(this) {} void onEnter() override; void onExit() override; void loop() override; diff --git a/src/main.cpp b/src/main.cpp index d2b898d6..bdc0767f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -230,17 +230,12 @@ void onGoToFileTransfer() { void onGoToBluetooth() { exitActivity(); - enterNewActivity(new BluetoothActivity( - renderer, - mappedInputManager, - onGoHome, - [](const std::string& filepath) { - Serial.printf("[%lu] [ ] File received over Bluetooth: %s\n", millis(), filepath.c_str()); - if (StringUtils::readableFileExtension(filepath)) { - onGoToReader(filepath, MyLibraryActivity::Tab::Recent); - } + enterNewActivity(new BluetoothActivity(renderer, mappedInputManager, onGoHome, [](const std::string& filepath) { + Serial.printf("[%lu] [ ] File received over Bluetooth: %s\n", millis(), filepath.c_str()); + if (StringUtils::readableFileExtension(filepath)) { + onGoToReader(filepath, MyLibraryActivity::Tab::Recent); } - )); + })); } void onGoToSettings() {