a bit higher tx power

This commit is contained in:
2025-10-20 08:48:59 +02:00
parent e8ae1cbec4
commit fc633d7c90

View File

@@ -10,10 +10,10 @@
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/queue.h" #include "freertos/queue.h"
#include "freertos/task.h" #include "freertos/task.h"
#include "host/ble_att.h"
#include "host/ble_gap.h" #include "host/ble_gap.h"
#include "host/ble_gatt.h" #include "host/ble_gatt.h"
#include "host/ble_hs.h" #include "host/ble_hs.h"
#include "host/ble_att.h"
#include "host/ble_hs_mbuf.h" #include "host/ble_hs_mbuf.h"
#include "host/util/util.h" #include "host/util/util.h"
#include "nimble/nimble_port.h" #include "nimble/nimble_port.h"
@@ -44,18 +44,14 @@ namespace {
constexpr char kLogTag[] = "TimeSyncBLE"; constexpr char kLogTag[] = "TimeSyncBLE";
constexpr char kDeviceName[] = "Cardboy"; constexpr char kDeviceName[] = "Cardboy";
constexpr std::uint16_t kPreferredConnIntervalMin = BLE_GAP_CONN_ITVL_MS(80); // 80 ms constexpr std::uint16_t kPreferredConnIntervalMin = BLE_GAP_CONN_ITVL_MS(80); // 80 ms
constexpr std::uint16_t kPreferredConnIntervalMax = BLE_GAP_CONN_ITVL_MS(150); // 150 ms constexpr std::uint16_t kPreferredConnIntervalMax = BLE_GAP_CONN_ITVL_MS(150); // 150 ms
constexpr std::uint16_t kPreferredConnLatency = 2; constexpr std::uint16_t kPreferredConnLatency = 2;
constexpr std::uint16_t kPreferredSupervisionTimeout = BLE_GAP_SUPERVISION_TIMEOUT_MS(5000); // 5 s constexpr std::uint16_t kPreferredSupervisionTimeout = BLE_GAP_SUPERVISION_TIMEOUT_MS(5000); // 5 s
constexpr float connIntervalUnitsToMs(std::uint16_t units) { constexpr float connIntervalUnitsToMs(std::uint16_t units) { return static_cast<float>(units) * 1.25f; }
return static_cast<float>(units) * 1.25f;
}
constexpr float supervisionUnitsToMs(std::uint16_t units) { constexpr float supervisionUnitsToMs(std::uint16_t units) { return static_cast<float>(units) * 10.0f; }
return static_cast<float>(units) * 10.0f;
}
// 128-bit UUIDs (little-endian order for NimBLE macros) // 128-bit UUIDs (little-endian order for NimBLE macros)
static const ble_uuid128_t kTimeServiceUuid = BLE_UUID128_INIT(0x30, 0xF2, 0xD3, 0xF4, 0xC3, 0x10, 0xA6, 0xB5, 0xFD, static const ble_uuid128_t kTimeServiceUuid = BLE_UUID128_INIT(0x30, 0xF2, 0xD3, 0xF4, 0xC3, 0x10, 0xA6, 0xB5, 0xFD,
@@ -107,9 +103,9 @@ struct FileUploadContext {
}; };
struct FileDownloadContext { struct FileDownloadContext {
FILE* file = nullptr; FILE* file = nullptr;
std::size_t remaining = 0; std::size_t remaining = 0;
bool active = false; bool active = false;
bool chunkScheduled = false; bool chunkScheduled = false;
}; };
@@ -371,11 +367,11 @@ bool scheduleDownloadChunk() {
return true; return true;
ResponseMessage msg{}; ResponseMessage msg{};
msg.opcode = static_cast<uint8_t>(FileCommandCode::DownloadRequest); msg.opcode = static_cast<uint8_t>(FileCommandCode::DownloadRequest);
msg.status = 0; msg.status = 0;
msg.length = 0; msg.length = 0;
msg.data = nullptr; msg.data = nullptr;
msg.streamDownload = true; msg.streamDownload = true;
if (xQueueSend(g_responseQueue, &msg, pdMS_TO_TICKS(20)) != pdPASS) { if (xQueueSend(g_responseQueue, &msg, pdMS_TO_TICKS(20)) != pdPASS) {
ESP_LOGW(kLogTag, "Failed to schedule download chunk; response queue full"); ESP_LOGW(kLogTag, "Failed to schedule download chunk; response queue full");
@@ -410,8 +406,8 @@ void resetDownloadContext() {
std::fclose(g_downloadCtx.file); std::fclose(g_downloadCtx.file);
g_downloadCtx.file = nullptr; g_downloadCtx.file = nullptr;
} }
g_downloadCtx.remaining = 0; g_downloadCtx.remaining = 0;
g_downloadCtx.active = false; g_downloadCtx.active = false;
g_downloadCtx.chunkScheduled = false; g_downloadCtx.chunkScheduled = false;
} }
@@ -425,7 +421,7 @@ void processDownloadChunk() {
return; return;
} }
constexpr std::size_t kMaxChunkBuffer = 244; constexpr std::size_t kMaxChunkBuffer = 244;
std::array<uint8_t, kMaxChunkBuffer> buffer{}; std::array<uint8_t, kMaxChunkBuffer> buffer{};
std::size_t maxPayload = buffer.size(); std::size_t maxPayload = buffer.size();
@@ -969,22 +965,18 @@ void logConnectionParams(uint16_t connHandle, const char* context) {
const float intervalMs = connIntervalUnitsToMs(desc.conn_itvl); const float intervalMs = connIntervalUnitsToMs(desc.conn_itvl);
const float timeoutMs = supervisionUnitsToMs(desc.supervision_timeout); const float timeoutMs = supervisionUnitsToMs(desc.supervision_timeout);
ESP_LOGI(kLogTag, ESP_LOGI(kLogTag, "%s params: interval=%.1f ms latency=%u supervision=%.0f ms", context, intervalMs,
"%s params: interval=%.1f ms latency=%u supervision=%.0f ms", static_cast<unsigned>(desc.conn_latency), timeoutMs);
context,
intervalMs,
static_cast<unsigned>(desc.conn_latency),
timeoutMs);
} }
void applyPreferredConnectionParams(uint16_t connHandle) { void applyPreferredConnectionParams(uint16_t connHandle) {
ble_gap_upd_params params{ ble_gap_upd_params params{
.itvl_min = kPreferredConnIntervalMin, .itvl_min = kPreferredConnIntervalMin,
.itvl_max = kPreferredConnIntervalMax, .itvl_max = kPreferredConnIntervalMax,
.latency = kPreferredConnLatency, .latency = kPreferredConnLatency,
.supervision_timeout = kPreferredSupervisionTimeout, .supervision_timeout = kPreferredSupervisionTimeout,
.min_ce_len = 0, .min_ce_len = 0,
.max_ce_len = 0, .max_ce_len = 0,
}; };
const int rc = ble_gap_update_params(connHandle, &params); const int rc = ble_gap_update_params(connHandle, &params);
@@ -993,12 +985,9 @@ void applyPreferredConnectionParams(uint16_t connHandle) {
return; return;
} }
ESP_LOGI(kLogTag, ESP_LOGI(kLogTag, "Requested conn params: interval=%.0f-%.0f ms latency=%u supervision=%.0f ms",
"Requested conn params: interval=%.0f-%.0f ms latency=%u supervision=%.0f ms", connIntervalUnitsToMs(kPreferredConnIntervalMin), connIntervalUnitsToMs(kPreferredConnIntervalMax),
connIntervalUnitsToMs(kPreferredConnIntervalMin), kPreferredConnLatency, supervisionUnitsToMs(kPreferredSupervisionTimeout));
connIntervalUnitsToMs(kPreferredConnIntervalMax),
kPreferredConnLatency,
supervisionUnitsToMs(kPreferredSupervisionTimeout));
} }
void startAdvertising() { void startAdvertising() {
@@ -1066,13 +1055,13 @@ void onSync() {
ESP_LOGW(kLogTag, "Failed to set preferred PHY (rc=%d)", rc); ESP_LOGW(kLogTag, "Failed to set preferred PHY (rc=%d)", rc);
} }
if (esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_DEFAULT, ESP_PWR_LVL_N24) != ESP_OK) { if (esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_DEFAULT, ESP_PWR_LVL_N12) != ESP_OK) {
ESP_LOGW(kLogTag, "Failed to set default TX power level"); ESP_LOGW(kLogTag, "Failed to set default TX power level");
} }
if (esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_ADV, ESP_PWR_LVL_N24) != ESP_OK) { if (esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_ADV, ESP_PWR_LVL_N12) != ESP_OK) {
ESP_LOGW(kLogTag, "Failed to set advertising TX power level"); ESP_LOGW(kLogTag, "Failed to set advertising TX power level");
} }
if (esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_SCAN, ESP_PWR_LVL_N24) != ESP_OK) { if (esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_SCAN, ESP_PWR_LVL_N12) != ESP_OK) {
ESP_LOGW(kLogTag, "Failed to set scan TX power level"); ESP_LOGW(kLogTag, "Failed to set scan TX power level");
} }
@@ -1112,9 +1101,7 @@ int gapEventHandler(struct ble_gap_event* event, void* /*arg*/) {
if (event->conn_update.status == 0) { if (event->conn_update.status == 0) {
logConnectionParams(event->conn_update.conn_handle, "Updated"); logConnectionParams(event->conn_update.conn_handle, "Updated");
} else { } else {
ESP_LOGW(kLogTag, ESP_LOGW(kLogTag, "Connection update failed; status=%d handle=%u", event->conn_update.status,
"Connection update failed; status=%d handle=%u",
event->conn_update.status,
static_cast<unsigned>(event->conn_update.conn_handle)); static_cast<unsigned>(event->conn_update.conn_handle));
} }
break; break;
@@ -1133,11 +1120,8 @@ int gapEventHandler(struct ble_gap_event* event, void* /*arg*/) {
params.min_ce_len = 0; params.min_ce_len = 0;
params.max_ce_len = 0; params.max_ce_len = 0;
ESP_LOGI(kLogTag, ESP_LOGI(kLogTag, "Peer update request -> interval %.1f-%.1f ms latency %u timeout %.0f ms",
"Peer update request -> interval %.1f-%.1f ms latency %u timeout %.0f ms", connIntervalUnitsToMs(params.itvl_min), connIntervalUnitsToMs(params.itvl_max), params.latency,
connIntervalUnitsToMs(params.itvl_min),
connIntervalUnitsToMs(params.itvl_max),
params.latency,
supervisionUnitsToMs(params.supervision_timeout)); supervisionUnitsToMs(params.supervision_timeout));
} }
break; break;