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/queue.h"
#include "freertos/task.h"
#include "host/ble_att.h"
#include "host/ble_gap.h"
#include "host/ble_gatt.h"
#include "host/ble_hs.h"
#include "host/ble_att.h"
#include "host/ble_hs_mbuf.h"
#include "host/util/util.h"
#include "nimble/nimble_port.h"
@@ -44,18 +44,14 @@ namespace {
constexpr char kLogTag[] = "TimeSyncBLE";
constexpr char kDeviceName[] = "Cardboy";
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 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 kPreferredConnLatency = 2;
constexpr std::uint16_t kPreferredSupervisionTimeout = BLE_GAP_SUPERVISION_TIMEOUT_MS(5000); // 5 s
constexpr float connIntervalUnitsToMs(std::uint16_t units) {
return static_cast<float>(units) * 1.25f;
}
constexpr float connIntervalUnitsToMs(std::uint16_t units) { return static_cast<float>(units) * 1.25f; }
constexpr float supervisionUnitsToMs(std::uint16_t units) {
return static_cast<float>(units) * 10.0f;
}
constexpr float supervisionUnitsToMs(std::uint16_t units) { return static_cast<float>(units) * 10.0f; }
// 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,
@@ -107,9 +103,9 @@ struct FileUploadContext {
};
struct FileDownloadContext {
FILE* file = nullptr;
std::size_t remaining = 0;
bool active = false;
FILE* file = nullptr;
std::size_t remaining = 0;
bool active = false;
bool chunkScheduled = false;
};
@@ -371,11 +367,11 @@ bool scheduleDownloadChunk() {
return true;
ResponseMessage msg{};
msg.opcode = static_cast<uint8_t>(FileCommandCode::DownloadRequest);
msg.status = 0;
msg.length = 0;
msg.data = nullptr;
msg.streamDownload = true;
msg.opcode = static_cast<uint8_t>(FileCommandCode::DownloadRequest);
msg.status = 0;
msg.length = 0;
msg.data = nullptr;
msg.streamDownload = true;
if (xQueueSend(g_responseQueue, &msg, pdMS_TO_TICKS(20)) != pdPASS) {
ESP_LOGW(kLogTag, "Failed to schedule download chunk; response queue full");
@@ -410,8 +406,8 @@ void resetDownloadContext() {
std::fclose(g_downloadCtx.file);
g_downloadCtx.file = nullptr;
}
g_downloadCtx.remaining = 0;
g_downloadCtx.active = false;
g_downloadCtx.remaining = 0;
g_downloadCtx.active = false;
g_downloadCtx.chunkScheduled = false;
}
@@ -425,7 +421,7 @@ void processDownloadChunk() {
return;
}
constexpr std::size_t kMaxChunkBuffer = 244;
constexpr std::size_t kMaxChunkBuffer = 244;
std::array<uint8_t, kMaxChunkBuffer> buffer{};
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 timeoutMs = supervisionUnitsToMs(desc.supervision_timeout);
ESP_LOGI(kLogTag,
"%s params: interval=%.1f ms latency=%u supervision=%.0f ms",
context,
intervalMs,
static_cast<unsigned>(desc.conn_latency),
timeoutMs);
ESP_LOGI(kLogTag, "%s params: interval=%.1f ms latency=%u supervision=%.0f ms", context, intervalMs,
static_cast<unsigned>(desc.conn_latency), timeoutMs);
}
void applyPreferredConnectionParams(uint16_t connHandle) {
ble_gap_upd_params params{
.itvl_min = kPreferredConnIntervalMin,
.itvl_max = kPreferredConnIntervalMax,
.latency = kPreferredConnLatency,
.itvl_min = kPreferredConnIntervalMin,
.itvl_max = kPreferredConnIntervalMax,
.latency = kPreferredConnLatency,
.supervision_timeout = kPreferredSupervisionTimeout,
.min_ce_len = 0,
.max_ce_len = 0,
.min_ce_len = 0,
.max_ce_len = 0,
};
const int rc = ble_gap_update_params(connHandle, &params);
@@ -993,12 +985,9 @@ void applyPreferredConnectionParams(uint16_t connHandle) {
return;
}
ESP_LOGI(kLogTag,
"Requested conn params: interval=%.0f-%.0f ms latency=%u supervision=%.0f ms",
connIntervalUnitsToMs(kPreferredConnIntervalMin),
connIntervalUnitsToMs(kPreferredConnIntervalMax),
kPreferredConnLatency,
supervisionUnitsToMs(kPreferredSupervisionTimeout));
ESP_LOGI(kLogTag, "Requested conn params: interval=%.0f-%.0f ms latency=%u supervision=%.0f ms",
connIntervalUnitsToMs(kPreferredConnIntervalMin), connIntervalUnitsToMs(kPreferredConnIntervalMax),
kPreferredConnLatency, supervisionUnitsToMs(kPreferredSupervisionTimeout));
}
void startAdvertising() {
@@ -1066,13 +1055,13 @@ void onSync() {
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");
}
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");
}
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");
}
@@ -1112,9 +1101,7 @@ int gapEventHandler(struct ble_gap_event* event, void* /*arg*/) {
if (event->conn_update.status == 0) {
logConnectionParams(event->conn_update.conn_handle, "Updated");
} else {
ESP_LOGW(kLogTag,
"Connection update failed; status=%d handle=%u",
event->conn_update.status,
ESP_LOGW(kLogTag, "Connection update failed; status=%d handle=%u", event->conn_update.status,
static_cast<unsigned>(event->conn_update.conn_handle));
}
break;
@@ -1133,11 +1120,8 @@ int gapEventHandler(struct ble_gap_event* event, void* /*arg*/) {
params.min_ce_len = 0;
params.max_ce_len = 0;
ESP_LOGI(kLogTag,
"Peer update request -> interval %.1f-%.1f ms latency %u timeout %.0f ms",
connIntervalUnitsToMs(params.itvl_min),
connIntervalUnitsToMs(params.itvl_max),
params.latency,
ESP_LOGI(kLogTag, "Peer update request -> interval %.1f-%.1f ms latency %u timeout %.0f ms",
connIntervalUnitsToMs(params.itvl_min), connIntervalUnitsToMs(params.itvl_max), params.latency,
supervisionUnitsToMs(params.supervision_timeout));
}
break;