feat(printer): add direct thermal backend and debug controls
This commit is contained in:
@@ -130,14 +130,44 @@ typedef struct {
|
||||
uint8_t patch;
|
||||
} printer_ota_version_t;
|
||||
|
||||
typedef enum {
|
||||
PRINTER_BACKEND_BLE = 0,
|
||||
PRINTER_BACKEND_DIRECT = 1,
|
||||
} printer_backend_t;
|
||||
|
||||
typedef struct {
|
||||
printer_backend_t backend;
|
||||
const char *name;
|
||||
uint32_t timeout_ms;
|
||||
} printer_connect_options_t;
|
||||
|
||||
typedef struct {
|
||||
// Direct backend only: bypass paper/temp/battery precheck for this print job.
|
||||
bool direct_ignore_precheck;
|
||||
} printer_print_options_t;
|
||||
|
||||
typedef struct {
|
||||
bool supports_connect;
|
||||
bool supports_gap_move;
|
||||
bool supports_label_offset;
|
||||
bool supports_ota;
|
||||
} printer_capabilities_t;
|
||||
|
||||
typedef struct {
|
||||
printer_backend_t backend;
|
||||
bool connected;
|
||||
bool transport_ready;
|
||||
bool notify_ready;
|
||||
bool busy;
|
||||
bool has_paper;
|
||||
int8_t paper_gpio_level;
|
||||
uint8_t paper_present_level;
|
||||
uint8_t battery_percent;
|
||||
float temperature;
|
||||
uint16_t mtu;
|
||||
bool supports_gap_move;
|
||||
bool supports_label_offset;
|
||||
bool supports_ota;
|
||||
uint32_t queue_depth;
|
||||
int64_t last_status_ms;
|
||||
} printer_runtime_status_t;
|
||||
@@ -147,8 +177,13 @@ typedef uint32_t printer_status_poll_pause_token_t;
|
||||
esp_err_t printer_protocol_init(void);
|
||||
esp_err_t printer_protocol_stop(uint32_t timeout_ms);
|
||||
|
||||
esp_err_t printer_protocol_set_backend(printer_backend_t backend, char *err, size_t err_len);
|
||||
printer_backend_t printer_protocol_get_backend(void);
|
||||
|
||||
esp_err_t printer_protocol_connect(const char *target_name, uint32_t timeout_ms);
|
||||
esp_err_t printer_protocol_connect_ex(const printer_connect_options_t *opt, char *err, size_t err_len);
|
||||
void printer_protocol_disconnect(void);
|
||||
void printer_protocol_get_capabilities(printer_capabilities_t *out_capabilities);
|
||||
|
||||
void printer_protocol_get_runtime_status(printer_runtime_status_t *out_status);
|
||||
printer_status_poll_pause_token_t printer_protocol_status_poll_pause_acquire(void);
|
||||
@@ -162,6 +197,15 @@ esp_err_t printer_protocol_submit_raster_job(const uint8_t *raster,
|
||||
uint32_t *out_job_id,
|
||||
char *err,
|
||||
size_t err_len);
|
||||
esp_err_t printer_protocol_submit_raster_job_ex(const uint8_t *raster,
|
||||
size_t raster_len,
|
||||
uint16_t width,
|
||||
uint16_t height,
|
||||
const char *density,
|
||||
const printer_print_options_t *options,
|
||||
uint32_t *out_job_id,
|
||||
char *err,
|
||||
size_t err_len);
|
||||
|
||||
bool printer_protocol_get_job(uint32_t job_id, print_job_info_t *out_info);
|
||||
esp_err_t printer_protocol_list_jobs(print_job_info_t *out_jobs, size_t max_jobs, size_t *out_count);
|
||||
|
||||
@@ -44,6 +44,7 @@
|
||||
typedef struct {
|
||||
bool used;
|
||||
bool cancel_requested;
|
||||
bool direct_ignore_precheck;
|
||||
uint32_t id;
|
||||
print_job_state_t state;
|
||||
uint8_t progress;
|
||||
@@ -60,6 +61,8 @@ typedef struct {
|
||||
|
||||
typedef struct {
|
||||
bool has_paper;
|
||||
int8_t paper_gpio_level;
|
||||
uint8_t paper_present_level;
|
||||
uint8_t battery;
|
||||
float temperature;
|
||||
int64_t updated_ms;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -4,8 +4,22 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
static bool is_direct_backend(void) {
|
||||
return printer_protocol_get_backend() == PRINTER_BACKEND_DIRECT;
|
||||
}
|
||||
|
||||
static esp_err_t unsupported_for_direct(char *err, size_t err_len, const char *op) {
|
||||
if (err != NULL && err_len > 0) {
|
||||
snprintf(err, err_len, "%s not supported on direct backend", op);
|
||||
}
|
||||
return ESP_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
esp_err_t printer_protocol_gap_move(uint32_t timeout_ms, char *err, size_t err_len) {
|
||||
if (is_direct_backend()) {
|
||||
return platform_direct_printer_gap_move(timeout_ms, err, err_len);
|
||||
}
|
||||
|
||||
if (!ble_printer_client_is_connected()) {
|
||||
if (err != NULL && err_len > 0) {
|
||||
snprintf(err, err_len, "printer not connected");
|
||||
@@ -34,6 +48,10 @@ esp_err_t printer_protocol_gap_move(uint32_t timeout_ms, char *err, size_t err_l
|
||||
}
|
||||
|
||||
esp_err_t printer_protocol_get_label_offset(uint8_t *out_offset, uint32_t timeout_ms, char *err, size_t err_len) {
|
||||
if (is_direct_backend()) {
|
||||
return unsupported_for_direct(err, err_len, "label offset");
|
||||
}
|
||||
|
||||
if (out_offset == NULL) {
|
||||
if (err != NULL && err_len > 0) {
|
||||
snprintf(err, err_len, "invalid args");
|
||||
@@ -88,6 +106,10 @@ esp_err_t printer_protocol_get_label_offset(uint8_t *out_offset, uint32_t timeou
|
||||
}
|
||||
|
||||
esp_err_t printer_protocol_set_label_offset(uint8_t offset, uint32_t timeout_ms, char *err, size_t err_len) {
|
||||
if (is_direct_backend()) {
|
||||
return unsupported_for_direct(err, err_len, "label offset");
|
||||
}
|
||||
|
||||
if (!ble_printer_client_is_connected()) {
|
||||
if (err != NULL && err_len > 0) {
|
||||
snprintf(err, err_len, "printer not connected");
|
||||
@@ -115,6 +137,10 @@ esp_err_t printer_protocol_set_label_offset(uint8_t offset, uint32_t timeout_ms,
|
||||
}
|
||||
|
||||
esp_err_t printer_protocol_ota_jump_boot(uint32_t timeout_ms, char *err, size_t err_len) {
|
||||
if (is_direct_backend()) {
|
||||
return unsupported_for_direct(err, err_len, "ota jump boot");
|
||||
}
|
||||
|
||||
if (!ble_printer_client_is_connected()) {
|
||||
if (err != NULL && err_len > 0) {
|
||||
snprintf(err, err_len, "printer not connected");
|
||||
@@ -142,6 +168,10 @@ esp_err_t printer_protocol_ota_jump_boot(uint32_t timeout_ms, char *err, size_t
|
||||
}
|
||||
|
||||
esp_err_t printer_protocol_ota_jump_app(uint32_t timeout_ms, char *err, size_t err_len) {
|
||||
if (is_direct_backend()) {
|
||||
return unsupported_for_direct(err, err_len, "ota jump app");
|
||||
}
|
||||
|
||||
if (!ble_printer_client_is_connected()) {
|
||||
if (err != NULL && err_len > 0) {
|
||||
snprintf(err, err_len, "printer not connected");
|
||||
@@ -169,6 +199,10 @@ esp_err_t printer_protocol_ota_jump_app(uint32_t timeout_ms, char *err, size_t e
|
||||
}
|
||||
|
||||
esp_err_t printer_protocol_ota_erase_page(uint16_t page_num, uint32_t timeout_ms, char *err, size_t err_len) {
|
||||
if (is_direct_backend()) {
|
||||
return unsupported_for_direct(err, err_len, "ota erase");
|
||||
}
|
||||
|
||||
if (!ble_printer_client_is_connected()) {
|
||||
if (err != NULL && err_len > 0) {
|
||||
snprintf(err, err_len, "printer not connected");
|
||||
@@ -207,6 +241,10 @@ esp_err_t printer_protocol_ota_write_frame(uint16_t packet_num,
|
||||
uint32_t timeout_ms,
|
||||
char *err,
|
||||
size_t err_len) {
|
||||
if (is_direct_backend()) {
|
||||
return unsupported_for_direct(err, err_len, "ota write");
|
||||
}
|
||||
|
||||
if ((data_len > 0 && data == NULL) || data_len > OTA_MAX_DATA_PER_FRAME) {
|
||||
if (err != NULL && err_len > 0) {
|
||||
snprintf(err, err_len, "invalid frame length");
|
||||
@@ -252,6 +290,10 @@ esp_err_t printer_protocol_ota_get_version(printer_ota_version_t *out_version,
|
||||
uint32_t timeout_ms,
|
||||
char *err,
|
||||
size_t err_len) {
|
||||
if (is_direct_backend()) {
|
||||
return unsupported_for_direct(err, err_len, "ota version");
|
||||
}
|
||||
|
||||
if (out_version == NULL) {
|
||||
if (err != NULL && err_len > 0) {
|
||||
snprintf(err, err_len, "invalid args");
|
||||
|
||||
@@ -48,14 +48,15 @@ bool printer_protocol_is_terminal_state(print_job_state_t state) {
|
||||
state == PRINT_JOB_STATE_CANCELED;
|
||||
}
|
||||
|
||||
esp_err_t printer_protocol_submit_raster_job(const uint8_t *raster,
|
||||
size_t raster_len,
|
||||
uint16_t width,
|
||||
uint16_t height,
|
||||
const char *density,
|
||||
uint32_t *out_job_id,
|
||||
char *err,
|
||||
size_t err_len) {
|
||||
esp_err_t printer_protocol_submit_raster_job_ex(const uint8_t *raster,
|
||||
size_t raster_len,
|
||||
uint16_t width,
|
||||
uint16_t height,
|
||||
const char *density,
|
||||
const printer_print_options_t *options,
|
||||
uint32_t *out_job_id,
|
||||
char *err,
|
||||
size_t err_len) {
|
||||
if (raster == NULL || raster_len == 0 || width == 0 || height == 0) {
|
||||
if (err != NULL && err_len > 0) {
|
||||
snprintf(err, err_len, "invalid args");
|
||||
@@ -134,9 +135,11 @@ esp_err_t printer_protocol_submit_raster_job(const uint8_t *raster,
|
||||
s_jobs[idx].width = width;
|
||||
s_jobs[idx].height = height;
|
||||
s_jobs[idx].data_len = raster_len;
|
||||
s_jobs[idx].direct_ignore_precheck = (options != NULL && options->direct_ignore_precheck);
|
||||
s_jobs[idx].created_ms = esp_timer_get_time() / 1000;
|
||||
s_jobs[idx].data = copy;
|
||||
strlcpy(s_jobs[idx].density, density != NULL ? density : "中等", sizeof(s_jobs[idx].density));
|
||||
bool ignore_precheck = s_jobs[idx].direct_ignore_precheck;
|
||||
|
||||
xSemaphoreGive(s_mutex);
|
||||
|
||||
@@ -166,12 +169,13 @@ esp_err_t printer_protocol_submit_raster_job(const uint8_t *raster,
|
||||
|
||||
UBaseType_t queue_depth = uxQueueMessagesWaiting(s_job_queue);
|
||||
ESP_LOGI(TAG,
|
||||
"job queued, job_id=%u, raster_bytes=%u, size=%ux%u, density=%s, queue_depth=%u",
|
||||
"job queued, job_id=%u, raster_bytes=%u, size=%ux%u, density=%s, ignore_precheck=%d, queue_depth=%u",
|
||||
(unsigned)id,
|
||||
(unsigned)raster_len,
|
||||
(unsigned)width,
|
||||
(unsigned)height,
|
||||
density != NULL ? density : "中等",
|
||||
ignore_precheck,
|
||||
(unsigned)queue_depth);
|
||||
runtime_diag_counter_add(RUNTIME_DIAG_COUNTER_PRINTER_JOB_SUBMITTED, 1);
|
||||
runtime_diag_set_gauge(RUNTIME_DIAG_GAUGE_PRINTER_QUEUE_DEPTH, (int32_t)queue_depth);
|
||||
@@ -179,6 +183,25 @@ esp_err_t printer_protocol_submit_raster_job(const uint8_t *raster,
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t printer_protocol_submit_raster_job(const uint8_t *raster,
|
||||
size_t raster_len,
|
||||
uint16_t width,
|
||||
uint16_t height,
|
||||
const char *density,
|
||||
uint32_t *out_job_id,
|
||||
char *err,
|
||||
size_t err_len) {
|
||||
return printer_protocol_submit_raster_job_ex(raster,
|
||||
raster_len,
|
||||
width,
|
||||
height,
|
||||
density,
|
||||
NULL,
|
||||
out_job_id,
|
||||
err,
|
||||
err_len);
|
||||
}
|
||||
|
||||
bool printer_protocol_get_job(uint32_t job_id, print_job_info_t *out_info) {
|
||||
if (out_info == NULL || job_id == 0) {
|
||||
return false;
|
||||
|
||||
@@ -15,6 +15,10 @@ static const char *TAG = "printer_protocol";
|
||||
static bool request_status_sync(uint32_t timeout_ms) {
|
||||
int64_t old_ms;
|
||||
|
||||
if (printer_protocol_get_backend() != PRINTER_BACKEND_BLE) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (printer_protocol_is_stopping()) {
|
||||
return false;
|
||||
}
|
||||
@@ -72,64 +76,82 @@ static uint16_t density_to_hot_time(const char *density) {
|
||||
return 2000;
|
||||
}
|
||||
|
||||
static bool precheck_printer_ready(char *err, size_t err_len) {
|
||||
if (!request_status_sync(1200)) {
|
||||
snprintf(err, err_len, "status timeout");
|
||||
return false;
|
||||
static bool precheck_printer_ready(bool direct_ignore_precheck, char *err, size_t err_len) {
|
||||
if (printer_protocol_get_backend() == PRINTER_BACKEND_BLE) {
|
||||
if (!request_status_sync(1200)) {
|
||||
snprintf(err, err_len, "status timeout");
|
||||
return false;
|
||||
}
|
||||
|
||||
parsed_status_t status;
|
||||
if (xSemaphoreTake(s_mutex, pdMS_TO_TICKS(200)) != pdTRUE) {
|
||||
snprintf(err, err_len, "status lock failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
status = s_status;
|
||||
xSemaphoreGive(s_mutex);
|
||||
|
||||
if (!status.has_paper) {
|
||||
snprintf(err, err_len, "printer out of paper");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (status.battery <= 40) {
|
||||
snprintf(err, err_len, "battery too low");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (status.temperature >= 60.0f) {
|
||||
snprintf(err, err_len, "temperature too high");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
parsed_status_t status;
|
||||
if (xSemaphoreTake(s_mutex, pdMS_TO_TICKS(200)) != pdTRUE) {
|
||||
snprintf(err, err_len, "status lock failed");
|
||||
return false;
|
||||
if (direct_ignore_precheck) {
|
||||
return true;
|
||||
}
|
||||
|
||||
status = s_status;
|
||||
xSemaphoreGive(s_mutex);
|
||||
|
||||
if (!status.has_paper) {
|
||||
platform_printer_sensors_t sensors = {0};
|
||||
if (platform_direct_printer_get_sensors(&sensors) != ESP_OK) {
|
||||
snprintf(err, err_len, "sensor read failed");
|
||||
return false;
|
||||
}
|
||||
if (!sensors.has_paper) {
|
||||
snprintf(err, err_len, "printer out of paper");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (status.battery <= 40) {
|
||||
float temp_min = runtime_policy_direct_printer_temp_min_c();
|
||||
float temp_max = runtime_policy_direct_printer_temp_max_c();
|
||||
if (temp_max <= temp_min) {
|
||||
temp_max = temp_min + 1.0f;
|
||||
}
|
||||
if (sensors.temperature_c < temp_min || sensors.temperature_c > temp_max) {
|
||||
snprintf(err, err_len, "temperature out of range");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (sensors.battery_percent < runtime_policy_direct_printer_battery_min_percent()) {
|
||||
snprintf(err, err_len, "battery too low");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (status.temperature >= 60.0f) {
|
||||
snprintf(err, err_len, "temperature too high");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool run_print_job(job_slot_t *job) {
|
||||
static bool run_print_job_ble(job_slot_t *job) {
|
||||
char err[96];
|
||||
err[0] = '\0';
|
||||
uint8_t next_progress_log = 25;
|
||||
|
||||
ESP_LOGI(TAG,
|
||||
"job %u print start, raster_bytes=%u, size=%ux%u, density=%s",
|
||||
(unsigned)job->id,
|
||||
(unsigned)job->data_len,
|
||||
(unsigned)job->width,
|
||||
(unsigned)job->height,
|
||||
job->density);
|
||||
|
||||
if (printer_protocol_is_stopping()) {
|
||||
snprintf(job->error, sizeof(job->error), "protocol stopping");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!ble_printer_client_is_connected()) {
|
||||
snprintf(job->error, sizeof(job->error), "printer not connected");
|
||||
ESP_LOGW(TAG, "job %u print aborted: %s", (unsigned)job->id, job->error);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!precheck_printer_ready(err, sizeof(err))) {
|
||||
if (!precheck_printer_ready(false, err, sizeof(err))) {
|
||||
snprintf(job->error, sizeof(job->error), "%s", err);
|
||||
ESP_LOGW(TAG, "job %u print aborted: %s", (unsigned)job->id, job->error);
|
||||
return false;
|
||||
@@ -232,6 +254,81 @@ static bool run_print_job(job_slot_t *job) {
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool run_print_job_direct(job_slot_t *job) {
|
||||
if (!platform_direct_printer_is_connected()) {
|
||||
snprintf(job->error, sizeof(job->error), "printer not connected");
|
||||
return false;
|
||||
}
|
||||
|
||||
char check_err[96];
|
||||
if (!precheck_printer_ready(job->direct_ignore_precheck, check_err, sizeof(check_err))) {
|
||||
snprintf(job->error, sizeof(job->error), "%s", check_err);
|
||||
return false;
|
||||
}
|
||||
|
||||
platform_direct_print_request_t req = {
|
||||
.width = job->width,
|
||||
.height = job->height,
|
||||
.raster = job->data,
|
||||
.raster_len = job->data_len,
|
||||
.strobe_on_time_us = density_to_hot_time(job->density),
|
||||
.strobe_interval_us = runtime_policy_direct_printer_strobe_interval_us(),
|
||||
.motor_step_delay_us = runtime_policy_direct_printer_motor_step_us(),
|
||||
.motor_steps_per_line = runtime_policy_direct_printer_steps_per_line(),
|
||||
.timeout_ms = runtime_policy_direct_printer_operation_timeout_ms(),
|
||||
.ignore_precheck = job->direct_ignore_precheck,
|
||||
.cancel_flag = &job->cancel_requested,
|
||||
};
|
||||
|
||||
if (xSemaphoreTake(s_mutex, pdMS_TO_TICKS(200)) == pdTRUE) {
|
||||
job->progress = 10;
|
||||
xSemaphoreGive(s_mutex);
|
||||
}
|
||||
|
||||
char direct_err[96] = {0};
|
||||
esp_err_t rc = platform_direct_printer_print(&req, direct_err, sizeof(direct_err));
|
||||
if (rc != ESP_OK) {
|
||||
if (direct_err[0] != '\0') {
|
||||
snprintf(job->error, sizeof(job->error), "%s", direct_err);
|
||||
} else if (rc == ESP_ERR_TIMEOUT) {
|
||||
snprintf(job->error, sizeof(job->error), "print timeout");
|
||||
} else if (rc == ESP_ERR_INVALID_STATE && job->cancel_requested) {
|
||||
snprintf(job->error, sizeof(job->error), "job canceled");
|
||||
} else {
|
||||
snprintf(job->error, sizeof(job->error), "direct print failed");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (xSemaphoreTake(s_mutex, pdMS_TO_TICKS(200)) == pdTRUE) {
|
||||
job->progress = 95;
|
||||
xSemaphoreGive(s_mutex);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool run_print_job(job_slot_t *job) {
|
||||
ESP_LOGI(TAG,
|
||||
"job %u print start, backend=%s, raster_bytes=%u, size=%ux%u, density=%s, ignore_precheck=%d",
|
||||
(unsigned)job->id,
|
||||
printer_protocol_get_backend() == PRINTER_BACKEND_BLE ? "ble" : "direct",
|
||||
(unsigned)job->data_len,
|
||||
(unsigned)job->width,
|
||||
(unsigned)job->height,
|
||||
job->density,
|
||||
job->direct_ignore_precheck);
|
||||
|
||||
if (printer_protocol_is_stopping()) {
|
||||
snprintf(job->error, sizeof(job->error), "protocol stopping");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (printer_protocol_get_backend() == PRINTER_BACKEND_BLE) {
|
||||
return run_print_job_ble(job);
|
||||
}
|
||||
return run_print_job_direct(job);
|
||||
}
|
||||
|
||||
void printer_protocol_worker_task(void *arg) {
|
||||
(void)arg;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user