bin/clang-format-fix

This commit is contained in:
Aleksejs Popovs 2026-01-21 21:44:31 -05:00
parent 25ef2e4e5d
commit 8e38e1bd49
3 changed files with 88 additions and 115 deletions

View File

@ -16,8 +16,7 @@
#define PROTOCOL_ASSERT(cond, fmt, ...) \ #define PROTOCOL_ASSERT(cond, fmt, ...) \
do { \ do { \
if (!(cond)) \ if (!(cond)) { \
{ \
snprintf(errorMessage, sizeof(errorMessage), fmt, ##__VA_ARGS__); \ snprintf(errorMessage, sizeof(errorMessage), fmt, ##__VA_ARGS__); \
intoState(STATE_ERROR); \ intoState(STATE_ERROR); \
return; \ return; \
@ -42,13 +41,9 @@ void BluetoothActivity::report() {
onFileReceived(OUTPUT_DIRECTORY "/" + filename); onFileReceived(OUTPUT_DIRECTORY "/" + filename);
} }
void BluetoothActivity::startAdvertising() { void BluetoothActivity::startAdvertising() { NimBLEDevice::startAdvertising(); }
NimBLEDevice::startAdvertising();
}
void BluetoothActivity::stopAdvertising() { void BluetoothActivity::stopAdvertising() { NimBLEDevice::stopAdvertising(); }
NimBLEDevice::stopAdvertising();
}
void BluetoothActivity::onEnter() { void BluetoothActivity::onEnter() {
Activity::onEnter(); Activity::onEnter();
@ -57,15 +52,10 @@ void BluetoothActivity::onEnter() {
NimBLEServer* pServer = NimBLEDevice::createServer(); NimBLEServer* pServer = NimBLEDevice::createServer();
pServer->setCallbacks(&serverCallbacks, false); pServer->setCallbacks(&serverCallbacks, false);
NimBLEService* pService = pServer->createService(SERVICE_UUID); NimBLEService* pService = pServer->createService(SERVICE_UUID);
NimBLECharacteristic *pRequestChar = pService->createCharacteristic( NimBLECharacteristic* pRequestChar =
REQUEST_CHARACTERISTIC_UUID, pService->createCharacteristic(REQUEST_CHARACTERISTIC_UUID, NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::WRITE_NR);
NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::WRITE_NR
);
pRequestChar->setCallbacks(&requestCallbacks); pRequestChar->setCallbacks(&requestCallbacks);
pResponseChar = pService->createCharacteristic( pResponseChar = pService->createCharacteristic(RESPONSE_CHARACTERISTIC_UUID, NIMBLE_PROPERTY::INDICATE);
RESPONSE_CHARACTERISTIC_UUID,
NIMBLE_PROPERTY::INDICATE
);
pService->start(); pService->start();
NimBLEAdvertising* pAdvertising = NimBLEDevice::getAdvertising(); NimBLEAdvertising* pAdvertising = NimBLEDevice::getAdvertising();
@ -109,11 +99,9 @@ void BluetoothActivity::intoState(State newState) {
2048, // Stack size 2048, // Stack size
this, // Parameters this, // Parameters
1, // Priority, 1, // Priority,
nullptr nullptr);
);
break; break;
case STATE_ERROR: case STATE_ERROR: {
{
// caller sets errorMessage // caller sets errorMessage
file.close(); file.close();
NimBLEServer* pServer = NimBLEDevice::getServer(); NimBLEServer* pServer = NimBLEDevice::getServer();
@ -229,12 +217,8 @@ void BluetoothActivity::render() const {
} }
// Draw help text at bottom // Draw help text at bottom
const auto labels = mappedInput.mapLabels( const auto labels =
"« Back", mappedInput.mapLabels("« Back", (state == STATE_ERROR || state == STATE_DONE) ? "Restart" : "", "", "");
(state == STATE_ERROR || state == STATE_DONE) ? "Restart" : "",
"",
""
);
renderer.drawButtonHints(UI_10_FONT_ID, labels.btn1, labels.btn2, labels.btn3, labels.btn4); renderer.drawButtonHints(UI_10_FONT_ID, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
renderer.displayBuffer(); renderer.displayBuffer();
@ -266,23 +250,24 @@ void BluetoothActivity::onRequest(const lfbt_message* msg, size_t msg_len) {
return; 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) { 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(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; totalBytes = msg->body.clientOffer.bodyLength;
size_t filenameLength = msg_len - 8 - sizeof(lfbt_msg_client_offer); size_t filenameLength = msg_len - 8 - sizeof(lfbt_msg_client_offer);
std::string originalFilename = StringUtils::sanitizeFilename( std::string originalFilename =
std::string(msg->body.clientOffer.name, filenameLength), StringUtils::sanitizeFilename(std::string(msg->body.clientOffer.name, filenameLength), MAX_FILENAME_LENGTH);
MAX_FILENAME_LENGTH
);
PROTOCOL_ASSERT(SdMan.ensureDirectoryExists(OUTPUT_DIRECTORY), "Couldn't create output directory %s", OUTPUT_DIRECTORY); PROTOCOL_ASSERT(SdMan.ensureDirectoryExists(OUTPUT_DIRECTORY), "Couldn't create output directory %s",
OUTPUT_DIRECTORY);
// generate unique filepath // generate unique filepath
auto splitName = StringUtils::splitFileName(originalFilename); auto splitName = StringUtils::splitFileName(originalFilename);
@ -300,18 +285,18 @@ void BluetoothActivity::onRequest(const lfbt_message* msg, size_t msg_len) {
filepath = OUTPUT_DIRECTORY "/" + filename; filepath = OUTPUT_DIRECTORY "/" + filename;
} }
PROTOCOL_ASSERT(SdMan.openFileForWrite("BT", filepath, file), "Couldn't open file %s for writing", filepath.c_str()); PROTOCOL_ASSERT(SdMan.openFileForWrite("BT", filepath, file), "Couldn't open file %s for writing",
// TODO: would be neat to check if we have enough space, but SDCardManager doesn't seem to expose that info currently 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; txnId = msg->txnId;
intoState(STATE_OFFERED); intoState(STATE_OFFERED);
lfbt_message response = { lfbt_message response = {.type = 1, // server_response
.type = 1, // server_response
.txnId = txnId, .txnId = txnId,
.body = {.serverResponse = {.status = 0}} .body = {.serverResponse = {.status = 0}}};
};
pResponseChar->setValue(reinterpret_cast<uint8_t*>(&response), 8 + sizeof(lfbt_msg_server_response)); pResponseChar->setValue(reinterpret_cast<uint8_t*>(&response), 8 + sizeof(lfbt_msg_server_response));
pResponseChar->indicate(); pResponseChar->indicate();
@ -320,23 +305,19 @@ void BluetoothActivity::onRequest(const lfbt_message* msg, size_t msg_len) {
} }
case 2: // client_chunk case 2: // client_chunk
{ {
Serial.printf( Serial.printf("[%lu] [BT] Received client_chunk, offset %u, length %zu\n", millis(), msg->body.clientChunk.offset,
"[%lu] [BT] Received client_chunk, offset %u, length %zu\n", msg_len - 8 - sizeof(lfbt_msg_client_chunk));
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(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( size_t written = file.write(reinterpret_cast<const uint8_t*>(msg->body.clientChunk.body),
reinterpret_cast<const uint8_t*>(msg->body.clientChunk.body), msg_len - 8 - sizeof(lfbt_msg_client_chunk));
msg_len - 8 - sizeof(lfbt_msg_client_chunk)
);
PROTOCOL_ASSERT(written > 0, "Couldn't write to file"); PROTOCOL_ASSERT(written > 0, "Couldn't write to file");
receivedBytes += msg_len - 8 - sizeof(lfbt_msg_client_chunk); receivedBytes += msg_len - 8 - sizeof(lfbt_msg_client_chunk);
if (receivedBytes >= totalBytes) { 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"); PROTOCOL_ASSERT(file.close(), "Couldn't finalize writing the file");
intoState(STATE_DONE); intoState(STATE_DONE);
} else { } else {
@ -350,11 +331,7 @@ void BluetoothActivity::onRequest(const lfbt_message* msg, size_t msg_len) {
void BluetoothActivity::RequestCallbacks::onWrite(NimBLECharacteristic* pCharacteristic, NimBLEConnInfo& connInfo) { void BluetoothActivity::RequestCallbacks::onWrite(NimBLECharacteristic* pCharacteristic, NimBLEConnInfo& connInfo) {
const lfbt_message* msg = reinterpret_cast<const lfbt_message*>(pCharacteristic->getValue().data()); const lfbt_message* msg = reinterpret_cast<const lfbt_message*>(pCharacteristic->getValue().data());
Serial.printf("[%lu] [BT] Received BLE message of type %u, txnId %x, length %d\n", Serial.printf("[%lu] [BT] Received BLE message of type %u, txnId %x, length %d\n", millis(), msg->type, msg->txnId,
millis(), pCharacteristic->getValue().length());
msg->type,
msg->txnId,
pCharacteristic->getValue().length()
);
activity->onRequest(msg, pCharacteristic->getValue().length()); activity->onRequest(msg, pCharacteristic->getValue().length());
} }

View File

@ -1,16 +1,14 @@
#pragma once #pragma once
#include <NimBLEDevice.h>
#include <NimBLEServer.h>
#include <NimBLEUtils.h>
#include <SDCardManager.h>
#include <freertos/FreeRTOS.h> #include <freertos/FreeRTOS.h>
#include <freertos/semphr.h> #include <freertos/semphr.h>
#include <freertos/task.h> #include <freertos/task.h>
#include <NimBLEDevice.h>
#include <NimBLEUtils.h>
#include <NimBLEServer.h>
#include <functional> #include <functional>
#include <SDCardManager.h>
#include "../Activity.h" #include "../Activity.h"
typedef struct __attribute__((packed)) { typedef struct __attribute__((packed)) {
@ -117,8 +115,11 @@ class BluetoothActivity final : public Activity {
explicit BluetoothActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, explicit BluetoothActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
const std::function<void()>& onCancel, const std::function<void()>& onCancel,
const std::function<void(const std::string&)>& onFileReceived) const std::function<void(const std::string&)>& onFileReceived)
: Activity("Bluetooth", renderer, mappedInput), onCancel(onCancel), onFileReceived(onFileReceived), : Activity("Bluetooth", renderer, mappedInput),
serverCallbacks(this), requestCallbacks(this) {} onCancel(onCancel),
onFileReceived(onFileReceived),
serverCallbacks(this),
requestCallbacks(this) {}
void onEnter() override; void onEnter() override;
void onExit() override; void onExit() override;
void loop() override; void loop() override;

View File

@ -230,17 +230,12 @@ void onGoToFileTransfer() {
void onGoToBluetooth() { void onGoToBluetooth() {
exitActivity(); exitActivity();
enterNewActivity(new BluetoothActivity( enterNewActivity(new BluetoothActivity(renderer, mappedInputManager, onGoHome, [](const std::string& filepath) {
renderer,
mappedInputManager,
onGoHome,
[](const std::string& filepath) {
Serial.printf("[%lu] [ ] File received over Bluetooth: %s\n", millis(), filepath.c_str()); Serial.printf("[%lu] [ ] File received over Bluetooth: %s\n", millis(), filepath.c_str());
if (StringUtils::readableFileExtension(filepath)) { if (StringUtils::readableFileExtension(filepath)) {
onGoToReader(filepath, MyLibraryActivity::Tab::Recent); onGoToReader(filepath, MyLibraryActivity::Tab::Recent);
} }
} }));
));
} }
void onGoToSettings() { void onGoToSettings() {