feat(printer): add direct thermal backend and debug controls
This commit is contained in:
@@ -21,6 +21,8 @@ uint32_t s_status_poll_pause_tokens[STATUS_POLL_PAUSE_SLOT_MAX];
|
||||
|
||||
parsed_status_t s_status = {
|
||||
.has_paper = true,
|
||||
.paper_gpio_level = -1,
|
||||
.paper_present_level = 0,
|
||||
.battery = 100,
|
||||
.temperature = 25.0f,
|
||||
.updated_ms = 0,
|
||||
@@ -36,6 +38,8 @@ TaskHandle_t s_worker_task;
|
||||
TaskHandle_t s_status_poll_task_handle;
|
||||
bool s_protocol_initialized;
|
||||
bool s_protocol_stopping;
|
||||
printer_backend_t s_backend = PRINTER_BACKEND_BLE;
|
||||
bool s_ble_client_initialized;
|
||||
|
||||
static const char *TAG = "printer_protocol";
|
||||
|
||||
@@ -45,6 +49,61 @@ static const char *TAG = "printer_protocol";
|
||||
#define PRINT_WORKER_CORE_ID 1
|
||||
#endif
|
||||
|
||||
static void on_rx_frame(const uint8_t *data, size_t len);
|
||||
|
||||
static void backend_capabilities_for(printer_backend_t backend, printer_capabilities_t *out_caps) {
|
||||
if (out_caps == NULL) {
|
||||
return;
|
||||
}
|
||||
memset(out_caps, 0, sizeof(*out_caps));
|
||||
out_caps->supports_connect = true;
|
||||
out_caps->supports_gap_move = true;
|
||||
if (backend == PRINTER_BACKEND_BLE) {
|
||||
out_caps->supports_label_offset = true;
|
||||
out_caps->supports_ota = true;
|
||||
}
|
||||
}
|
||||
|
||||
static esp_err_t ensure_ble_client_ready(void) {
|
||||
if (s_ble_client_initialized) {
|
||||
return ESP_OK;
|
||||
}
|
||||
esp_err_t err = ble_printer_client_init(on_rx_frame);
|
||||
if (err == ESP_OK) {
|
||||
s_ble_client_initialized = true;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
static esp_err_t ensure_backend_ready(printer_backend_t backend) {
|
||||
if (backend == PRINTER_BACKEND_BLE) {
|
||||
return ensure_ble_client_ready();
|
||||
}
|
||||
if (backend == PRINTER_BACKEND_DIRECT) {
|
||||
if (!runtime_policy_direct_printer_enabled()) {
|
||||
return ESP_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
return platform_direct_printer_init();
|
||||
}
|
||||
return ESP_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
static void refresh_direct_status_locked(void) {
|
||||
if (s_backend != PRINTER_BACKEND_DIRECT) {
|
||||
return;
|
||||
}
|
||||
|
||||
platform_printer_sensors_t sensors = {0};
|
||||
if (platform_direct_printer_get_sensors(&sensors) == ESP_OK) {
|
||||
s_status.has_paper = sensors.has_paper;
|
||||
s_status.paper_gpio_level = sensors.paper_gpio_level;
|
||||
s_status.paper_present_level = sensors.paper_present_level;
|
||||
s_status.battery = sensors.battery_percent;
|
||||
s_status.temperature = sensors.temperature_c;
|
||||
s_status.updated_ms = sensors.updated_ms;
|
||||
}
|
||||
}
|
||||
|
||||
static size_t status_poll_pause_depth_locked(void) {
|
||||
size_t depth = 0;
|
||||
for (size_t i = 0; i < STATUS_POLL_PAUSE_SLOT_MAX; ++i) {
|
||||
@@ -77,6 +136,8 @@ static void reset_runtime_state_locked(void) {
|
||||
s_status_poll_pause_next_token = 1;
|
||||
|
||||
s_status.has_paper = true;
|
||||
s_status.paper_gpio_level = -1;
|
||||
s_status.paper_present_level = 0;
|
||||
s_status.battery = 100;
|
||||
s_status.temperature = 25.0f;
|
||||
s_status.updated_ms = 0;
|
||||
@@ -260,6 +321,9 @@ esp_err_t printer_protocol_send_frame(uint8_t cmd,
|
||||
if (!s_protocol_initialized || printer_protocol_is_stopping()) {
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
if (s_backend != PRINTER_BACKEND_BLE) {
|
||||
return ESP_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
uint8_t frame[260];
|
||||
size_t total = 4 + payload_len + (with_checksum ? 1 : 0);
|
||||
@@ -440,10 +504,12 @@ static void status_poll_task(void *arg) {
|
||||
while (true) {
|
||||
bool should_stop = false;
|
||||
bool can_poll = false;
|
||||
printer_backend_t backend = PRINTER_BACKEND_BLE;
|
||||
|
||||
if (xSemaphoreTake(s_mutex, pdMS_TO_TICKS(50)) == pdTRUE) {
|
||||
should_stop = s_protocol_stopping;
|
||||
can_poll = (s_busy_refcnt == 0) && (status_poll_pause_depth_locked() == 0);
|
||||
backend = s_backend;
|
||||
xSemaphoreGive(s_mutex);
|
||||
}
|
||||
|
||||
@@ -451,9 +517,19 @@ static void status_poll_task(void *arg) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (ble_printer_client_is_connected() && can_poll) {
|
||||
uint8_t dummy = 0x00;
|
||||
(void)printer_protocol_send_frame(CMD_GET_STATUS, &dummy, 0, true);
|
||||
if (can_poll) {
|
||||
if (backend == PRINTER_BACKEND_BLE) {
|
||||
if (ble_printer_client_is_connected()) {
|
||||
uint8_t dummy = 0x00;
|
||||
(void)printer_protocol_send_frame(CMD_GET_STATUS, &dummy, 0, true);
|
||||
}
|
||||
} else if (backend == PRINTER_BACKEND_DIRECT) {
|
||||
if (platform_direct_printer_is_connected() &&
|
||||
xSemaphoreTake(s_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
|
||||
refresh_direct_status_locked();
|
||||
xSemaphoreGive(s_mutex);
|
||||
}
|
||||
}
|
||||
}
|
||||
vTaskDelay(pdMS_TO_TICKS(runtime_policy_printer_status_poll_interval_ms()));
|
||||
}
|
||||
@@ -492,6 +568,8 @@ static void on_rx_frame(const uint8_t *data, size_t len) {
|
||||
|
||||
if (xSemaphoreTake(s_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
|
||||
s_status.has_paper = has_paper;
|
||||
s_status.paper_gpio_level = -1;
|
||||
s_status.paper_present_level = 0;
|
||||
s_status.battery = battery;
|
||||
s_status.temperature = temperature;
|
||||
s_status.updated_ms = esp_timer_get_time() / 1000;
|
||||
@@ -564,8 +642,12 @@ esp_err_t printer_protocol_init(void) {
|
||||
runtime_diag_set_gauge(RUNTIME_DIAG_GAUGE_PRINTER_QUEUE_DEPTH, 0);
|
||||
s_protocol_initialized = true;
|
||||
s_protocol_stopping = false;
|
||||
s_ble_client_initialized = false;
|
||||
s_backend = (runtime_policy_printer_default_backend() == 1 && runtime_policy_direct_printer_enabled())
|
||||
? PRINTER_BACKEND_DIRECT
|
||||
: PRINTER_BACKEND_BLE;
|
||||
|
||||
esp_err_t err = ble_printer_client_init(on_rx_frame);
|
||||
esp_err_t err = ensure_backend_ready(s_backend);
|
||||
if (err != ESP_OK) {
|
||||
s_protocol_initialized = false;
|
||||
destroy_runtime_objects();
|
||||
@@ -630,6 +712,7 @@ esp_err_t printer_protocol_stop(uint32_t timeout_ms) {
|
||||
}
|
||||
|
||||
ble_printer_client_disconnect();
|
||||
platform_direct_printer_disconnect();
|
||||
|
||||
bool worker_done = (s_worker_task == NULL);
|
||||
bool poll_done = (s_status_poll_task_handle == NULL);
|
||||
@@ -673,23 +756,166 @@ esp_err_t printer_protocol_stop(uint32_t timeout_ms) {
|
||||
s_status_poll_task_handle = NULL;
|
||||
s_protocol_initialized = false;
|
||||
s_protocol_stopping = false;
|
||||
s_ble_client_initialized = false;
|
||||
s_backend = PRINTER_BACKEND_BLE;
|
||||
|
||||
runtime_diag_set_gauge(RUNTIME_DIAG_GAUGE_STATUS_POLL_PAUSE_DEPTH, 0);
|
||||
runtime_diag_set_gauge(RUNTIME_DIAG_GAUGE_PRINTER_QUEUE_DEPTH, 0);
|
||||
|
||||
platform_direct_printer_deinit();
|
||||
|
||||
ESP_LOGI(TAG, "printer protocol stopped");
|
||||
return (worker_done && poll_done) ? ESP_OK : ESP_ERR_TIMEOUT;
|
||||
}
|
||||
|
||||
esp_err_t printer_protocol_connect(const char *target_name, uint32_t timeout_ms) {
|
||||
if (!s_protocol_initialized || s_protocol_stopping) {
|
||||
esp_err_t printer_protocol_set_backend(printer_backend_t backend, char *err, size_t err_len) {
|
||||
if (backend != PRINTER_BACKEND_BLE && backend != PRINTER_BACKEND_DIRECT) {
|
||||
if (err != NULL && err_len > 0) {
|
||||
snprintf(err, err_len, "invalid backend");
|
||||
}
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
if (backend == PRINTER_BACKEND_DIRECT && !runtime_policy_direct_printer_enabled()) {
|
||||
if (err != NULL && err_len > 0) {
|
||||
snprintf(err, err_len, "direct backend disabled");
|
||||
}
|
||||
return ESP_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
if (!s_protocol_initialized || s_protocol_stopping || s_mutex == NULL) {
|
||||
if (err != NULL && err_len > 0) {
|
||||
snprintf(err, err_len, "printer protocol unavailable");
|
||||
}
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
return ble_printer_client_connect(target_name, timeout_ms);
|
||||
|
||||
if (xSemaphoreTake(s_mutex, pdMS_TO_TICKS(runtime_policy_printer_control_lock_timeout_ms())) != pdTRUE) {
|
||||
if (err != NULL && err_len > 0) {
|
||||
snprintf(err, err_len, "lock timeout");
|
||||
}
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
|
||||
printer_backend_t old_backend = s_backend;
|
||||
if (old_backend == backend) {
|
||||
xSemaphoreGive(s_mutex);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
if (s_busy_refcnt != 0 || (s_job_queue != NULL && uxQueueMessagesWaiting(s_job_queue) > 0)) {
|
||||
xSemaphoreGive(s_mutex);
|
||||
if (err != NULL && err_len > 0) {
|
||||
snprintf(err, err_len, "printer busy");
|
||||
}
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
s_backend = backend;
|
||||
xSemaphoreGive(s_mutex);
|
||||
|
||||
ble_printer_client_disconnect();
|
||||
platform_direct_printer_disconnect();
|
||||
|
||||
esp_err_t rc = ensure_backend_ready(backend);
|
||||
if (rc != ESP_OK) {
|
||||
if (xSemaphoreTake(s_mutex, pdMS_TO_TICKS(200)) == pdTRUE) {
|
||||
s_backend = old_backend;
|
||||
xSemaphoreGive(s_mutex);
|
||||
}
|
||||
(void)ensure_backend_ready(old_backend);
|
||||
if (err != NULL && err_len > 0) {
|
||||
snprintf(err, err_len, "backend init failed");
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
if (backend == PRINTER_BACKEND_DIRECT && xSemaphoreTake(s_mutex, pdMS_TO_TICKS(200)) == pdTRUE) {
|
||||
refresh_direct_status_locked();
|
||||
xSemaphoreGive(s_mutex);
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
printer_backend_t printer_protocol_get_backend(void) {
|
||||
if (!s_protocol_initialized || s_mutex == NULL) {
|
||||
return s_backend;
|
||||
}
|
||||
if (xSemaphoreTake(s_mutex, pdMS_TO_TICKS(100)) != pdTRUE) {
|
||||
return s_backend;
|
||||
}
|
||||
printer_backend_t backend = s_backend;
|
||||
xSemaphoreGive(s_mutex);
|
||||
return backend;
|
||||
}
|
||||
|
||||
esp_err_t printer_protocol_connect(const char *target_name, uint32_t timeout_ms) {
|
||||
printer_connect_options_t opt = {
|
||||
.backend = printer_protocol_get_backend(),
|
||||
.name = target_name,
|
||||
.timeout_ms = timeout_ms,
|
||||
};
|
||||
return printer_protocol_connect_ex(&opt, NULL, 0);
|
||||
}
|
||||
|
||||
esp_err_t printer_protocol_connect_ex(const printer_connect_options_t *opt, char *err, size_t err_len) {
|
||||
if (!s_protocol_initialized || s_protocol_stopping) {
|
||||
if (err != NULL && err_len > 0) {
|
||||
snprintf(err, err_len, "printer protocol unavailable");
|
||||
}
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
printer_backend_t backend = printer_protocol_get_backend();
|
||||
const char *name = NULL;
|
||||
uint32_t timeout_ms = 0;
|
||||
if (opt != NULL) {
|
||||
backend = opt->backend;
|
||||
name = opt->name;
|
||||
timeout_ms = opt->timeout_ms;
|
||||
}
|
||||
|
||||
esp_err_t rc = printer_protocol_set_backend(backend, err, err_len);
|
||||
if (rc != ESP_OK) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
if (backend == PRINTER_BACKEND_BLE) {
|
||||
rc = ensure_backend_ready(PRINTER_BACKEND_BLE);
|
||||
if (rc != ESP_OK) {
|
||||
if (err != NULL && err_len > 0) {
|
||||
snprintf(err, err_len, "ble backend unavailable");
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
return ble_printer_client_connect(name, timeout_ms);
|
||||
}
|
||||
|
||||
if (backend == PRINTER_BACKEND_DIRECT) {
|
||||
rc = ensure_backend_ready(PRINTER_BACKEND_DIRECT);
|
||||
if (rc != ESP_OK) {
|
||||
if (err != NULL && err_len > 0) {
|
||||
snprintf(err, err_len, "direct backend unavailable");
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
return platform_direct_printer_connect(timeout_ms);
|
||||
}
|
||||
|
||||
if (err != NULL && err_len > 0) {
|
||||
snprintf(err, err_len, "unsupported backend");
|
||||
}
|
||||
return ESP_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
void printer_protocol_disconnect(void) {
|
||||
ble_printer_client_disconnect();
|
||||
platform_direct_printer_disconnect();
|
||||
}
|
||||
|
||||
void printer_protocol_get_capabilities(printer_capabilities_t *out_capabilities) {
|
||||
if (out_capabilities == NULL) {
|
||||
return;
|
||||
}
|
||||
backend_capabilities_for(printer_protocol_get_backend(), out_capabilities);
|
||||
}
|
||||
|
||||
void printer_protocol_get_runtime_status(printer_runtime_status_t *out_status) {
|
||||
@@ -698,17 +924,47 @@ void printer_protocol_get_runtime_status(printer_runtime_status_t *out_status) {
|
||||
}
|
||||
|
||||
memset(out_status, 0, sizeof(*out_status));
|
||||
out_status->paper_gpio_level = -1;
|
||||
out_status->backend = printer_protocol_get_backend();
|
||||
|
||||
ble_link_state_t link = {0};
|
||||
ble_printer_client_get_link_state(&link);
|
||||
printer_capabilities_t caps = {0};
|
||||
backend_capabilities_for(out_status->backend, &caps);
|
||||
out_status->supports_gap_move = caps.supports_gap_move;
|
||||
out_status->supports_label_offset = caps.supports_label_offset;
|
||||
out_status->supports_ota = caps.supports_ota;
|
||||
|
||||
out_status->connected = link.connected;
|
||||
out_status->notify_ready = link.notify_ready;
|
||||
out_status->mtu = link.mtu;
|
||||
if (out_status->backend == PRINTER_BACKEND_BLE) {
|
||||
ble_link_state_t link = {0};
|
||||
ble_printer_client_get_link_state(&link);
|
||||
out_status->connected = link.connected;
|
||||
out_status->notify_ready = link.notify_ready;
|
||||
out_status->transport_ready = link.connected && link.notify_ready;
|
||||
out_status->mtu = link.mtu;
|
||||
} else if (out_status->backend == PRINTER_BACKEND_DIRECT) {
|
||||
out_status->connected = platform_direct_printer_is_connected();
|
||||
out_status->transport_ready = out_status->connected;
|
||||
out_status->notify_ready = false;
|
||||
out_status->mtu = 0;
|
||||
|
||||
platform_printer_sensors_t sensors = {0};
|
||||
if (platform_direct_printer_get_sensors(&sensors) == ESP_OK &&
|
||||
s_mutex != NULL &&
|
||||
xSemaphoreTake(s_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
|
||||
s_status.has_paper = sensors.has_paper;
|
||||
s_status.paper_gpio_level = sensors.paper_gpio_level;
|
||||
s_status.paper_present_level = sensors.paper_present_level;
|
||||
s_status.battery = sensors.battery_percent;
|
||||
s_status.temperature = sensors.temperature_c;
|
||||
s_status.updated_ms = sensors.updated_ms;
|
||||
xSemaphoreGive(s_mutex);
|
||||
}
|
||||
}
|
||||
|
||||
if (s_mutex != NULL && xSemaphoreTake(s_mutex, pdMS_TO_TICKS(200)) == pdTRUE) {
|
||||
out_status->busy = s_busy_refcnt > 0;
|
||||
out_status->has_paper = s_status.has_paper;
|
||||
out_status->paper_gpio_level = s_status.paper_gpio_level;
|
||||
out_status->paper_present_level = s_status.paper_present_level;
|
||||
out_status->battery_percent = s_status.battery;
|
||||
out_status->temperature = s_status.temperature;
|
||||
out_status->last_status_ms = s_status.updated_ms;
|
||||
|
||||
Reference in New Issue
Block a user