refactor(printer): remove BLE backend and dependencies

This commit is contained in:
admin
2026-02-28 22:35:08 +08:00
parent d6fae38d46
commit 9deeab738e
14 changed files with 115 additions and 1783 deletions

View File

@@ -2,7 +2,6 @@ idf_component_register(
SRCS
"src/platform_bootstrap.c"
"src/wifi_manager.c"
"src/ble_printer_client.c"
"src/direct_thermal_printer.c"
"src/display_st7789.c"
"src/display_lcd_module.c"
@@ -16,8 +15,6 @@ idf_component_register(
PRIV_INCLUDE_DIRS
"internal"
REQUIRES
bt
esp_coex
esp_wifi
esp_netif
esp_event

View File

@@ -3,7 +3,7 @@
Purpose: hardware and system resource abstraction only.
Allowed examples:
- Wi-Fi/BLE driver wrappers
- Wi-Fi and board-level driver wrappers
- NVS/time/RTOS/system adapters
- board-specific IO and transport bindings

View File

@@ -20,23 +20,6 @@ esp_err_t wifi_manager_stop(void);
bool wifi_manager_is_ready(void);
void wifi_manager_get_ip(char *buf, size_t buf_len);
// ---------- ble_printer_client ----------
typedef void (*ble_frame_rx_cb_t)(const uint8_t *data, size_t len);
typedef struct {
bool connected;
bool notify_ready;
uint16_t mtu;
} ble_link_state_t;
esp_err_t ble_printer_client_init(ble_frame_rx_cb_t rx_cb);
esp_err_t ble_printer_client_connect(const char *target_name, uint32_t timeout_ms);
void ble_printer_client_disconnect(void);
bool ble_printer_client_is_connected(void);
void ble_printer_client_get_link_state(ble_link_state_t *out_state);
esp_err_t ble_printer_client_write(const uint8_t *data, size_t len);
// ---------- direct_thermal_printer ----------
typedef struct {
bool has_paper;
@@ -153,7 +136,6 @@ uint32_t runtime_policy_printer_worker_queue_wait_ms(void);
uint32_t runtime_policy_printer_queue_retry_delay_ms(void);
uint32_t runtime_policy_printer_status_poll_interval_ms(void);
uint32_t runtime_policy_printer_stop_timeout_ms(void);
uint8_t runtime_policy_printer_default_backend(void);
bool runtime_policy_direct_printer_enabled(void);
uint32_t runtime_policy_direct_printer_operation_timeout_ms(void);

View File

@@ -1,706 +0,0 @@
#include "platform.h"
#include <ctype.h>
#include <string.h>
#include "esp_err.h"
#include "esp_log.h"
#include "esp_nimble_hci.h"
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "freertos/semphr.h"
#include "host/ble_gatt.h"
#include "host/ble_hs.h"
#include "nimble/nimble_port.h"
#include "nimble/nimble_port_freertos.h"
#include "os/os_mbuf.h"
#include "services/gap/ble_svc_gap.h"
#include "services/gatt/ble_svc_gatt.h"
#define PRINTER_SERVICE_UUID16 0xFFF0
#define PRINTER_NOTIFY_UUID16 0xFFF1
#define PRINTER_WRITE_UUID16 0xFFF2
#define CCCD_UUID16 0x2902
#define PRINTER_NAME_PRIMARY "TQPrinter"
#define PRINTER_NAME_LEGACY "lyfPrinter"
#define EVT_CONNECTED BIT0
#define EVT_READY BIT1
#define EVT_FAILED BIT2
static const char *TAG = "ble_client";
static EventGroupHandle_t s_evt_group;
static SemaphoreHandle_t s_lock;
static ble_frame_rx_cb_t s_rx_cb;
static uint8_t s_addr_type;
static ble_addr_t s_target_addr;
static char s_target_name[32] = PRINTER_NAME_PRIMARY;
static bool s_match_any_compatible;
static uint16_t s_conn_handle = BLE_HS_CONN_HANDLE_NONE;
static uint16_t s_service_start;
static uint16_t s_service_end;
static uint16_t s_notify_val_handle;
static uint16_t s_write_val_handle;
static uint16_t s_cccd_handle;
static uint16_t s_mtu = 23;
static bool s_scanning;
static bool s_notify_ready;
static bool s_host_synced;
static bool s_initialized;
static bool s_connecting;
static uint32_t s_connect_timeout_ms = 15000;
static uint16_t uuid16(const ble_uuid_t *uuid) {
if (uuid == NULL || uuid->type != BLE_UUID_TYPE_16) {
return 0;
}
return BLE_UUID16(uuid)->value;
}
static bool adv_has_uuid16(const struct ble_hs_adv_fields *fields, uint16_t target_uuid) {
if (fields == NULL || fields->uuids16 == NULL || fields->num_uuids16 == 0) {
return false;
}
for (uint8_t i = 0; i < fields->num_uuids16; ++i) {
if (fields->uuids16[i].value == target_uuid) {
return true;
}
}
return false;
}
static size_t normalize_name(const char *in, char *out, size_t out_cap) {
if (in == NULL || out == NULL || out_cap == 0) {
return 0;
}
const char *start = in;
while (*start != '\0' && isspace((unsigned char)*start)) {
++start;
}
if (*start == '"' || *start == '\'') {
++start;
}
size_t len = strlen(start);
while (len > 0 && isspace((unsigned char)start[len - 1])) {
--len;
}
if (len > 0 && (start[len - 1] == '"' || start[len - 1] == '\'')) {
--len;
}
size_t out_len = 0;
for (size_t i = 0; i < len && out_len + 1 < out_cap; ++i) {
unsigned char c = (unsigned char)start[i];
if (isupper(c)) {
c = (unsigned char)tolower(c);
}
out[out_len++] = (char)c;
}
out[out_len] = '\0';
return out_len;
}
static bool normalized_name_match(const char *adv_name, const char *target_name) {
char adv[40] = {0};
char target[40] = {0};
size_t adv_len = normalize_name(adv_name, adv, sizeof(adv));
size_t target_len = normalize_name(target_name, target, sizeof(target));
if (adv_len == 0 || target_len == 0) {
return false;
}
if (strcmp(adv, target) == 0) {
return true;
}
// Be tolerant of shortened or prefixed names.
return (strstr(adv, target) != NULL) || (strstr(target, adv) != NULL);
}
static bool is_known_printer_name(const char *name) {
return normalized_name_match(name, PRINTER_NAME_PRIMARY) ||
normalized_name_match(name, PRINTER_NAME_LEGACY);
}
static bool target_name_match_with_alias(const char *adv_name, const char *target_name) {
if (normalized_name_match(adv_name, target_name)) {
return true;
}
if (!is_known_printer_name(target_name)) {
return false;
}
return is_known_printer_name(adv_name);
}
static void reset_discovery_state(void) {
s_service_start = 0;
s_service_end = 0;
s_notify_val_handle = 0;
s_write_val_handle = 0;
s_cccd_handle = 0;
s_notify_ready = false;
s_mtu = 23;
}
static void signal_failure(void) {
xEventGroupSetBits(s_evt_group, EVT_FAILED);
}
static void signal_ready(void) {
xEventGroupSetBits(s_evt_group, EVT_READY);
}
static int dsc_disc_cb(uint16_t conn_handle,
const struct ble_gatt_error *error,
uint16_t chr_val_handle,
const struct ble_gatt_dsc *dsc,
void *arg);
static int chr_disc_cb(uint16_t conn_handle,
const struct ble_gatt_error *error,
const struct ble_gatt_chr *chr,
void *arg);
static int svc_disc_cb(uint16_t conn_handle,
const struct ble_gatt_error *error,
const struct ble_gatt_svc *service,
void *arg);
static int cccd_write_cb(uint16_t conn_handle,
const struct ble_gatt_error *error,
struct ble_gatt_attr *attr,
void *arg) {
(void)conn_handle;
(void)attr;
(void)arg;
if (error->status != 0) {
ESP_LOGE(TAG, "CCCD write failed status=%d", error->status);
signal_failure();
return 0;
}
s_notify_ready = true;
ESP_LOGI(TAG, "Notify subscription enabled");
signal_ready();
return 0;
}
static int dsc_disc_cb(uint16_t conn_handle,
const struct ble_gatt_error *error,
uint16_t chr_val_handle,
const struct ble_gatt_dsc *dsc,
void *arg) {
(void)chr_val_handle;
(void)arg;
if (error->status == 0 && dsc != NULL) {
if (uuid16(&dsc->uuid.u) == CCCD_UUID16) {
s_cccd_handle = dsc->handle;
ESP_LOGI(TAG, "Found CCCD handle=%u", s_cccd_handle);
}
return 0;
}
if (error->status == BLE_HS_EDONE) {
if (s_cccd_handle == 0) {
ESP_LOGW(TAG, "CCCD not found, fallback to val_handle+1");
s_cccd_handle = s_notify_val_handle + 1;
}
uint8_t cccd[2] = {0x01, 0x00};
int rc = ble_gattc_write_flat(conn_handle, s_cccd_handle, cccd, sizeof(cccd), cccd_write_cb, NULL);
if (rc != 0) {
ESP_LOGE(TAG, "ble_gattc_write_flat CCCD failed rc=%d", rc);
signal_failure();
}
return 0;
}
ESP_LOGE(TAG, "Descriptor discovery failed status=%d", error->status);
signal_failure();
return 0;
}
static int chr_disc_cb(uint16_t conn_handle,
const struct ble_gatt_error *error,
const struct ble_gatt_chr *chr,
void *arg) {
(void)arg;
if (error->status == 0 && chr != NULL) {
uint16_t id = uuid16(&chr->uuid.u);
if (id == PRINTER_NOTIFY_UUID16) {
s_notify_val_handle = chr->val_handle;
ESP_LOGI(TAG, "Found notify char handle=%u", s_notify_val_handle);
} else if (id == PRINTER_WRITE_UUID16) {
s_write_val_handle = chr->val_handle;
ESP_LOGI(TAG, "Found write char handle=%u", s_write_val_handle);
}
return 0;
}
if (error->status == BLE_HS_EDONE) {
if (s_notify_val_handle == 0 || s_write_val_handle == 0) {
ESP_LOGE(TAG,
"Characteristic discovery incomplete notify=%u write=%u",
s_notify_val_handle,
s_write_val_handle);
signal_failure();
return 0;
}
uint16_t end_handle = (s_write_val_handle > s_notify_val_handle)
? (uint16_t)(s_write_val_handle - 1)
: s_service_end;
if (end_handle <= s_notify_val_handle) {
end_handle = s_service_end;
}
int rc = ble_gattc_disc_all_dscs(conn_handle,
s_notify_val_handle,
end_handle,
dsc_disc_cb,
NULL);
if (rc != 0) {
ESP_LOGE(TAG, "Descriptor discovery start failed rc=%d", rc);
signal_failure();
}
return 0;
}
ESP_LOGE(TAG, "Characteristic discovery failed status=%d", error->status);
signal_failure();
return 0;
}
static int svc_disc_cb(uint16_t conn_handle,
const struct ble_gatt_error *error,
const struct ble_gatt_svc *service,
void *arg) {
(void)arg;
if (error->status == 0 && service != NULL) {
if (uuid16(&service->uuid.u) == PRINTER_SERVICE_UUID16) {
s_service_start = service->start_handle;
s_service_end = service->end_handle;
ESP_LOGI(TAG, "Found printer service [%u, %u]", s_service_start, s_service_end);
}
return 0;
}
if (error->status == BLE_HS_EDONE) {
uint16_t start = s_service_start;
uint16_t end = s_service_end;
if (start == 0 || end == 0) {
// Android app searches all services/chars for FFF1/FFF2.
// Use a global fallback range for broader compatibility.
start = 1;
end = 0xFFFF;
ESP_LOGW(TAG, "Printer service 0x%04x not found; fallback char discovery in full handle range", PRINTER_SERVICE_UUID16);
}
int rc = ble_gattc_disc_all_chrs(conn_handle,
start,
end,
chr_disc_cb,
NULL);
if (rc != 0) {
ESP_LOGE(TAG, "Start characteristic discovery failed rc=%d", rc);
signal_failure();
}
return 0;
}
ESP_LOGE(TAG, "Service discovery failed status=%d", error->status);
signal_failure();
return 0;
}
static void start_service_discovery(void) {
int rc = ble_gattc_disc_all_svcs(s_conn_handle, svc_disc_cb, NULL);
if (rc != 0) {
ESP_LOGE(TAG, "ble_gattc_disc_all_svcs failed rc=%d", rc);
signal_failure();
}
}
static void start_scan(void);
static void stop_scan_if_running(void) {
if (s_scanning) {
ble_gap_disc_cancel();
s_scanning = false;
}
}
static int gap_event_cb(struct ble_gap_event *event, void *arg) {
(void)arg;
switch (event->type) {
case BLE_GAP_EVENT_DISC: {
if (!s_scanning) {
return 0;
}
struct ble_hs_adv_fields fields;
memset(&fields, 0, sizeof(fields));
int rc = ble_hs_adv_parse_fields(&fields,
event->disc.data,
event->disc.length_data);
if (rc != 0) {
return 0;
}
char name[32] = {0};
bool has_name = (fields.name_len > 0 && fields.name != NULL);
if (has_name) {
size_t n = fields.name_len < sizeof(name) - 1 ? fields.name_len : sizeof(name) - 1;
memcpy(name, fields.name, n);
}
if (s_match_any_compatible) {
bool uuid_match = adv_has_uuid16(&fields, PRINTER_SERVICE_UUID16);
bool name_match = has_name && is_known_printer_name(name);
if (!uuid_match && !name_match) {
return 0;
}
ESP_LOGI(TAG,
"Found compatible device '%s' RSSI=%d (uuid_match=%d)",
has_name ? name : "<no-name>",
event->disc.rssi,
uuid_match ? 1 : 0);
} else {
if (!has_name || !target_name_match_with_alias(name, s_target_name)) {
return 0;
}
ESP_LOGI(TAG, "Found target device '%s' RSSI=%d", name, event->disc.rssi);
}
s_target_addr = event->disc.addr;
stop_scan_if_running();
struct ble_gap_conn_params conn_params;
memset(&conn_params, 0, sizeof(conn_params));
conn_params.scan_itvl = 0x0010;
conn_params.scan_window = 0x0010;
conn_params.itvl_min = 0x0018;
conn_params.itvl_max = 0x0028;
conn_params.latency = 0;
conn_params.supervision_timeout = 0x0100;
conn_params.min_ce_len = 0x0010;
conn_params.max_ce_len = 0x0300;
rc = ble_gap_connect(s_addr_type,
&s_target_addr,
s_connect_timeout_ms > 30000 ? 30000 : s_connect_timeout_ms,
&conn_params,
gap_event_cb,
NULL);
if (rc != 0) {
ESP_LOGE(TAG, "ble_gap_connect failed rc=%d", rc);
s_connecting = false;
signal_failure();
} else {
ESP_LOGI(TAG, "Connecting...");
}
return 0;
}
case BLE_GAP_EVENT_DISC_COMPLETE:
s_scanning = false;
return 0;
case BLE_GAP_EVENT_CONNECT:
if (event->connect.status == 0) {
if (!s_connecting) {
(void)ble_gap_terminate(event->connect.conn_handle, BLE_ERR_REM_USER_CONN_TERM);
return 0;
}
s_connecting = false;
s_conn_handle = event->connect.conn_handle;
xEventGroupSetBits(s_evt_group, EVT_CONNECTED);
ESP_LOGI(TAG, "Connected handle=%u", s_conn_handle);
ble_gattc_exchange_mtu(s_conn_handle, NULL, NULL);
start_service_discovery();
} else {
s_connecting = false;
ESP_LOGE(TAG, "Connect failed status=%d", event->connect.status);
signal_failure();
}
return 0;
case BLE_GAP_EVENT_DISCONNECT:
ESP_LOGW(TAG, "Disconnected reason=%d", event->disconnect.reason);
s_connecting = false;
s_conn_handle = BLE_HS_CONN_HANDLE_NONE;
s_notify_ready = false;
reset_discovery_state();
return 0;
case BLE_GAP_EVENT_MTU:
s_mtu = event->mtu.value;
ESP_LOGI(TAG, "MTU updated to %u", s_mtu);
return 0;
case BLE_GAP_EVENT_NOTIFY_RX: {
if (event->notify_rx.om == NULL || s_rx_cb == NULL) {
return 0;
}
uint16_t data_len = OS_MBUF_PKTLEN(event->notify_rx.om);
if (data_len == 0) {
return 0;
}
uint8_t buf[256];
uint16_t out_len = 0;
if (data_len > sizeof(buf)) {
ESP_LOGW(TAG, "Notify packet too large len=%u", data_len);
return 0;
}
int rc = ble_hs_mbuf_to_flat(event->notify_rx.om, buf, sizeof(buf), &out_len);
if (rc != 0) {
ESP_LOGW(TAG, "ble_hs_mbuf_to_flat notify failed rc=%d", rc);
return 0;
}
s_rx_cb(buf, out_len);
return 0;
}
default:
return 0;
}
}
static void start_scan(void) {
struct ble_gap_disc_params params;
memset(&params, 0, sizeof(params));
// Keep duplicates so we can receive scan response updates carrying full local name.
params.filter_duplicates = 0;
params.passive = 0;
params.itvl = 0x0010;
params.window = 0x0010;
stop_scan_if_running();
int rc = ble_gap_disc(s_addr_type, BLE_HS_FOREVER, &params, gap_event_cb, NULL);
if (rc != 0) {
ESP_LOGE(TAG, "ble_gap_disc failed rc=%d", rc);
signal_failure();
return;
}
s_scanning = true;
if (s_match_any_compatible) {
ESP_LOGI(TAG, "Scanning for compatible printer (service 0x%04x)", PRINTER_SERVICE_UUID16);
} else {
ESP_LOGI(TAG, "Scanning for %s", s_target_name);
}
}
static void ble_on_reset(int reason) {
ESP_LOGE(TAG, "BLE reset reason=%d", reason);
}
static void ble_on_sync(void) {
int rc = ble_hs_id_infer_auto(0, &s_addr_type);
if (rc != 0) {
ESP_LOGE(TAG, "ble_hs_id_infer_auto failed rc=%d", rc);
}
s_host_synced = true;
}
static void nimble_host_task(void *param) {
(void)param;
nimble_port_run();
nimble_port_freertos_deinit();
}
esp_err_t ble_printer_client_init(ble_frame_rx_cb_t rx_cb) {
s_rx_cb = rx_cb;
if (s_initialized) {
reset_discovery_state();
return ESP_OK;
}
// NimBLE emits very chatty INFO logs during each chunk write.
// Lowering these logs reduces serial I/O overhead and improves runtime smoothness.
esp_log_level_set("NimBLE", ESP_LOG_WARN);
esp_log_level_set("BLE_INIT", ESP_LOG_WARN);
s_evt_group = xEventGroupCreate();
if (s_evt_group == NULL) {
return ESP_ERR_NO_MEM;
}
s_lock = xSemaphoreCreateMutex();
if (s_lock == NULL) {
vEventGroupDelete(s_evt_group);
s_evt_group = NULL;
return ESP_ERR_NO_MEM;
}
esp_err_t err = nimble_port_init();
if (err != ESP_OK && err != ESP_ERR_INVALID_STATE) {
return err;
}
ble_hs_cfg.reset_cb = ble_on_reset;
ble_hs_cfg.sync_cb = ble_on_sync;
ble_svc_gap_init();
ble_svc_gatt_init();
nimble_port_freertos_init(nimble_host_task);
reset_discovery_state();
s_initialized = true;
return ESP_OK;
}
esp_err_t ble_printer_client_connect(const char *target_name, uint32_t timeout_ms) {
if (!s_initialized || s_lock == NULL || s_evt_group == NULL) {
return ESP_ERR_INVALID_STATE;
}
uint32_t effective_timeout_ms = timeout_ms;
if (effective_timeout_ms < 1000) {
effective_timeout_ms = 1000;
} else if (effective_timeout_ms > 60000) {
effective_timeout_ms = 60000;
}
s_match_any_compatible = false;
if (target_name != NULL && target_name[0] != '\0') {
if (strcmp(target_name, "*") == 0) {
s_match_any_compatible = true;
} else {
strlcpy(s_target_name, target_name, sizeof(s_target_name));
}
}
if (xSemaphoreTake(s_lock, pdMS_TO_TICKS(3000)) != pdTRUE) {
return ESP_ERR_TIMEOUT;
}
if (s_conn_handle != BLE_HS_CONN_HANDLE_NONE && s_notify_ready) {
xSemaphoreGive(s_lock);
return ESP_OK;
}
if (s_connecting) {
xSemaphoreGive(s_lock);
return ESP_ERR_INVALID_STATE;
}
xEventGroupClearBits(s_evt_group, EVT_CONNECTED | EVT_READY | EVT_FAILED);
reset_discovery_state();
s_connect_timeout_ms = effective_timeout_ms;
s_connecting = true;
int wait_sync_ms = 3000;
while (!s_host_synced && wait_sync_ms > 0) {
vTaskDelay(pdMS_TO_TICKS(20));
wait_sync_ms -= 20;
}
if (!s_host_synced) {
xSemaphoreGive(s_lock);
return ESP_ERR_INVALID_STATE;
}
start_scan();
xSemaphoreGive(s_lock);
EventBits_t bits = xEventGroupWaitBits(s_evt_group,
EVT_READY | EVT_FAILED,
pdFALSE,
pdFALSE,
pdMS_TO_TICKS(effective_timeout_ms));
if (bits & EVT_READY) {
return ESP_OK;
}
if (xSemaphoreTake(s_lock, pdMS_TO_TICKS(1000)) == pdTRUE) {
stop_scan_if_running();
if (s_conn_handle != BLE_HS_CONN_HANDLE_NONE) {
(void)ble_gap_terminate(s_conn_handle, BLE_ERR_REM_USER_CONN_TERM);
}
s_connecting = false;
reset_discovery_state();
xSemaphoreGive(s_lock);
}
if (bits & EVT_FAILED) {
return ESP_FAIL;
}
return ESP_ERR_TIMEOUT;
}
void ble_printer_client_disconnect(void) {
if (!s_initialized || s_lock == NULL) {
return;
}
if (xSemaphoreTake(s_lock, pdMS_TO_TICKS(2000)) != pdTRUE) {
return;
}
stop_scan_if_running();
if (s_conn_handle != BLE_HS_CONN_HANDLE_NONE) {
ble_gap_terminate(s_conn_handle, BLE_ERR_REM_USER_CONN_TERM);
}
s_connecting = false;
s_conn_handle = BLE_HS_CONN_HANDLE_NONE;
reset_discovery_state();
xSemaphoreGive(s_lock);
}
bool ble_printer_client_is_connected(void) {
if (!s_initialized) {
return false;
}
return (s_conn_handle != BLE_HS_CONN_HANDLE_NONE) && s_notify_ready;
}
void ble_printer_client_get_link_state(ble_link_state_t *out_state) {
if (out_state == NULL) {
return;
}
out_state->connected = (s_conn_handle != BLE_HS_CONN_HANDLE_NONE);
out_state->notify_ready = s_notify_ready;
out_state->mtu = s_mtu;
}
esp_err_t ble_printer_client_write(const uint8_t *data, size_t len) {
if (data == NULL || len == 0) {
return ESP_ERR_INVALID_ARG;
}
if (!ble_printer_client_is_connected() || s_write_val_handle == 0) {
return ESP_ERR_INVALID_STATE;
}
int rc = ble_gattc_write_no_rsp_flat(s_conn_handle,
s_write_val_handle,
data,
len);
if (rc != 0) {
ESP_LOGW(TAG, "write_no_rsp failed rc=%d", rc);
return ESP_FAIL;
}
return ESP_OK;
}

View File

@@ -34,10 +34,6 @@
#define CONFIG_TQ_PRINTER_STOP_TIMEOUT_MS 8000
#endif
#ifndef CONFIG_TQ_PRINTER_DEFAULT_BACKEND
#define CONFIG_TQ_PRINTER_DEFAULT_BACKEND 0
#endif
#ifndef CONFIG_TQ_DIRECT_PRINTER_ENABLE
#define CONFIG_TQ_DIRECT_PRINTER_ENABLE 1
#endif
@@ -201,10 +197,6 @@ uint32_t runtime_policy_printer_stop_timeout_ms(void) {
return clamp_u32(CONFIG_TQ_PRINTER_STOP_TIMEOUT_MS, 1000, 30000);
}
uint8_t runtime_policy_printer_default_backend(void) {
return (uint8_t)clamp_u32(CONFIG_TQ_PRINTER_DEFAULT_BACKEND, 0, 1);
}
bool runtime_policy_direct_printer_enabled(void) {
return CONFIG_TQ_DIRECT_PRINTER_ENABLE != 0;
}