refactor(printer): remove BLE backend and dependencies
This commit is contained in:
@@ -219,8 +219,7 @@ typedef struct {
|
||||
} printer_ota_version_t;
|
||||
|
||||
typedef enum {
|
||||
PRINTER_BACKEND_BLE = 0,
|
||||
PRINTER_BACKEND_DIRECT = 1,
|
||||
PRINTER_BACKEND_DIRECT = 0,
|
||||
} printer_backend_t;
|
||||
|
||||
typedef struct {
|
||||
@@ -257,14 +256,12 @@ 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;
|
||||
|
||||
@@ -13,26 +13,9 @@
|
||||
#include "freertos/semphr.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
#define PROTO_ADDR 0x01
|
||||
|
||||
#define CMD_POWER 0x00
|
||||
#define CMD_GET_STATUS 0x01
|
||||
#define CMD_SET_DISTANCE 0x02
|
||||
#define CMD_SET_PARAM 0x03
|
||||
#define CMD_SEND_DATA 0x04
|
||||
#define CMD_GAP_MOVE 0x05
|
||||
#define CMD_GET_OFFSET 0x06
|
||||
#define CMD_SET_OFFSET 0x07
|
||||
#define CMD_BOOT_JUMP_BOOT 0xA0
|
||||
#define CMD_BOOT_ERASE_PAGE 0xA1
|
||||
#define CMD_BOOT_WRITE_DATA 0xA2
|
||||
#define CMD_BOOT_JUMP_APP 0xA3
|
||||
#define CMD_BOOT_GET_VERSION 0xA4
|
||||
|
||||
#define EVT_ACK BIT0
|
||||
#define EVT_SHUTDOWN BIT1
|
||||
#define EVT_WORKER_EXITED BIT2
|
||||
#define EVT_STATUS_POLL_EXITED BIT3
|
||||
#define EVT_SHUTDOWN BIT0
|
||||
#define EVT_WORKER_EXITED BIT1
|
||||
#define EVT_STATUS_POLL_EXITED BIT2
|
||||
|
||||
#define JOB_QUEUE_LEN 8
|
||||
#define JOB_SLOT_MAX 16
|
||||
@@ -40,8 +23,6 @@
|
||||
#define STATUS_POLL_PAUSE_SLOT_MAX 8
|
||||
#define PRINTER_JOB_SENTINEL_STOP 0u
|
||||
|
||||
#define OTA_MAX_DATA_PER_FRAME 236u
|
||||
|
||||
typedef struct {
|
||||
bool used;
|
||||
bool cancel_requested;
|
||||
@@ -74,9 +55,6 @@ extern QueueHandle_t s_job_queue;
|
||||
extern EventGroupHandle_t s_evt;
|
||||
extern uint32_t s_busy_refcnt;
|
||||
extern parsed_status_t s_status;
|
||||
extern uint8_t s_last_rsp_cmd;
|
||||
extern uint16_t s_last_rsp_payload_len;
|
||||
extern uint8_t s_last_rsp_payload[252];
|
||||
extern uint32_t s_status_poll_pause_next_token;
|
||||
extern uint32_t s_status_poll_pause_tokens[STATUS_POLL_PAUSE_SLOT_MAX];
|
||||
extern uint32_t s_next_job_id;
|
||||
@@ -88,33 +66,6 @@ extern bool s_protocol_stopping;
|
||||
|
||||
bool printer_protocol_acquire_control_lane(uint32_t timeout_ms, char *err, size_t err_len);
|
||||
void printer_protocol_release_control_lane(void);
|
||||
esp_err_t printer_protocol_send_cmd_wait_response(uint8_t cmd,
|
||||
const uint8_t *payload,
|
||||
uint16_t payload_len,
|
||||
bool with_checksum,
|
||||
uint32_t timeout_ms,
|
||||
bool expect_ack,
|
||||
bool reset_transport_on_timeout,
|
||||
uint8_t *out_payload,
|
||||
size_t out_payload_cap,
|
||||
uint16_t *out_payload_len,
|
||||
char *err,
|
||||
size_t err_len);
|
||||
esp_err_t printer_protocol_send_frame(uint8_t cmd,
|
||||
const uint8_t *payload,
|
||||
uint16_t payload_len,
|
||||
bool with_checksum);
|
||||
bool printer_protocol_wait_response(uint8_t cmd,
|
||||
uint8_t *out_payload,
|
||||
size_t out_payload_cap,
|
||||
uint16_t *out_payload_len,
|
||||
uint32_t timeout_ms);
|
||||
bool printer_protocol_send_cmd_with_ack(uint8_t cmd,
|
||||
const uint8_t *payload,
|
||||
uint16_t payload_len,
|
||||
bool with_checksum,
|
||||
uint32_t timeout_ms,
|
||||
bool reset_transport_on_timeout);
|
||||
|
||||
int printer_protocol_find_job_idx_locked(uint32_t id);
|
||||
int printer_protocol_alloc_job_slot_locked(void);
|
||||
|
||||
@@ -28,18 +28,13 @@ parsed_status_t s_status = {
|
||||
.updated_ms = 0,
|
||||
};
|
||||
|
||||
uint8_t s_last_rsp_cmd;
|
||||
uint16_t s_last_rsp_payload_len;
|
||||
uint8_t s_last_rsp_payload[252];
|
||||
|
||||
uint32_t s_next_job_id = 1;
|
||||
job_slot_t s_jobs[JOB_SLOT_MAX];
|
||||
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;
|
||||
printer_backend_t s_backend = PRINTER_BACKEND_DIRECT;
|
||||
|
||||
static const char *TAG = "printer_protocol";
|
||||
|
||||
@@ -49,14 +44,8 @@ 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 printer_backend_t default_backend_on_startup(void) {
|
||||
// This hardware has a first-class direct path; BLE remains opt-in via API.
|
||||
if (runtime_policy_direct_printer_enabled()) {
|
||||
return PRINTER_BACKEND_DIRECT;
|
||||
}
|
||||
return PRINTER_BACKEND_BLE;
|
||||
return PRINTER_BACKEND_DIRECT;
|
||||
}
|
||||
|
||||
static void backend_capabilities_for(printer_backend_t backend, printer_capabilities_t *out_caps) {
|
||||
@@ -64,43 +53,23 @@ static void backend_capabilities_for(printer_backend_t backend, printer_capabili
|
||||
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;
|
||||
out_caps->supports_connect = (backend == PRINTER_BACKEND_DIRECT);
|
||||
out_caps->supports_gap_move = (backend == PRINTER_BACKEND_DIRECT);
|
||||
out_caps->supports_label_offset = false;
|
||||
out_caps->supports_ota = false;
|
||||
}
|
||||
|
||||
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) {
|
||||
return ESP_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
if (backend == PRINTER_BACKEND_DIRECT) {
|
||||
if (!runtime_policy_direct_printer_enabled()) {
|
||||
return ESP_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
return platform_direct_printer_init();
|
||||
if (!runtime_policy_direct_printer_enabled()) {
|
||||
return ESP_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
return ESP_ERR_NOT_SUPPORTED;
|
||||
return platform_direct_printer_init();
|
||||
}
|
||||
|
||||
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;
|
||||
@@ -136,9 +105,6 @@ static void reset_jobs_locked(void) {
|
||||
|
||||
static void reset_runtime_state_locked(void) {
|
||||
s_busy_refcnt = 0;
|
||||
s_last_rsp_cmd = 0;
|
||||
s_last_rsp_payload_len = 0;
|
||||
memset(s_last_rsp_payload, 0, sizeof(s_last_rsp_payload));
|
||||
|
||||
status_poll_pause_slots_clear_locked();
|
||||
s_status_poll_pause_next_token = 1;
|
||||
@@ -314,232 +280,16 @@ void printer_protocol_release_control_lane(void) {
|
||||
xSemaphoreGive(s_mutex);
|
||||
}
|
||||
|
||||
static uint8_t checksum8(const uint8_t *data, size_t len) {
|
||||
uint32_t sum = 0;
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
sum += data[i];
|
||||
}
|
||||
return (uint8_t)(sum & 0xFF);
|
||||
}
|
||||
|
||||
esp_err_t printer_protocol_send_frame(uint8_t cmd,
|
||||
const uint8_t *payload,
|
||||
uint16_t payload_len,
|
||||
bool with_checksum) {
|
||||
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);
|
||||
if (total > sizeof(frame)) {
|
||||
return ESP_ERR_INVALID_SIZE;
|
||||
}
|
||||
|
||||
frame[0] = PROTO_ADDR;
|
||||
frame[1] = cmd;
|
||||
frame[2] = (uint8_t)((payload_len >> 8) & 0xFF);
|
||||
frame[3] = (uint8_t)(payload_len & 0xFF);
|
||||
|
||||
if (payload_len > 0 && payload != NULL) {
|
||||
memcpy(&frame[4], payload, payload_len);
|
||||
}
|
||||
|
||||
if (with_checksum) {
|
||||
frame[4 + payload_len] = checksum8(frame, 4 + payload_len);
|
||||
}
|
||||
|
||||
return ble_printer_client_write(frame, total);
|
||||
}
|
||||
|
||||
static void clear_ack_signal(void) {
|
||||
if (s_evt != NULL) {
|
||||
xEventGroupClearBits(s_evt, EVT_ACK);
|
||||
}
|
||||
}
|
||||
|
||||
bool printer_protocol_wait_response(uint8_t cmd,
|
||||
uint8_t *out_payload,
|
||||
size_t out_payload_cap,
|
||||
uint16_t *out_payload_len,
|
||||
uint32_t timeout_ms) {
|
||||
if (timeout_ms == 0 || s_evt == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int64_t deadline = esp_timer_get_time() / 1000 + timeout_ms;
|
||||
|
||||
while (true) {
|
||||
if (printer_protocol_is_stopping()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int64_t now = esp_timer_get_time() / 1000;
|
||||
if (now >= deadline) {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t wait_ms = (uint32_t)(deadline - now);
|
||||
if (wait_ms > 100) {
|
||||
wait_ms = 100;
|
||||
}
|
||||
|
||||
EventBits_t bits = xEventGroupWaitBits(s_evt,
|
||||
EVT_ACK | EVT_SHUTDOWN,
|
||||
pdFALSE,
|
||||
pdFALSE,
|
||||
pdMS_TO_TICKS(wait_ms));
|
||||
if (bits & EVT_SHUTDOWN) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!(bits & EVT_ACK)) {
|
||||
continue;
|
||||
}
|
||||
xEventGroupClearBits(s_evt, EVT_ACK);
|
||||
|
||||
if (xSemaphoreTake(s_mutex, pdMS_TO_TICKS(200)) != pdTRUE) {
|
||||
continue;
|
||||
}
|
||||
|
||||
uint8_t rsp_cmd = s_last_rsp_cmd;
|
||||
uint16_t rsp_len = s_last_rsp_payload_len;
|
||||
uint8_t rsp_copy[sizeof(s_last_rsp_payload)];
|
||||
if (rsp_len > sizeof(rsp_copy)) {
|
||||
rsp_len = sizeof(rsp_copy);
|
||||
}
|
||||
if (rsp_len > 0) {
|
||||
memcpy(rsp_copy, s_last_rsp_payload, rsp_len);
|
||||
}
|
||||
xSemaphoreGive(s_mutex);
|
||||
|
||||
if (rsp_cmd == cmd) {
|
||||
if (out_payload != NULL && out_payload_cap > 0 && rsp_len > 0) {
|
||||
size_t copy_len = rsp_len;
|
||||
if (copy_len > out_payload_cap) {
|
||||
copy_len = out_payload_cap;
|
||||
}
|
||||
memcpy(out_payload, rsp_copy, copy_len);
|
||||
}
|
||||
if (out_payload_len != NULL) {
|
||||
*out_payload_len = rsp_len;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static bool wait_ack(uint8_t cmd, uint32_t timeout_ms) {
|
||||
uint8_t payload[252];
|
||||
uint16_t payload_len = 0;
|
||||
if (!printer_protocol_wait_response(cmd, payload, sizeof(payload), &payload_len, timeout_ms)) {
|
||||
return false;
|
||||
}
|
||||
return payload_len >= 1 && payload[0] == 0x01;
|
||||
}
|
||||
|
||||
static void reset_transport_after_timeout(void) {
|
||||
if (printer_protocol_is_stopping()) {
|
||||
return;
|
||||
}
|
||||
if (printer_protocol_get_backend() == PRINTER_BACKEND_BLE) {
|
||||
ble_printer_client_disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
bool printer_protocol_send_cmd_with_ack(uint8_t cmd,
|
||||
const uint8_t *payload,
|
||||
uint16_t payload_len,
|
||||
bool with_checksum,
|
||||
uint32_t timeout_ms,
|
||||
bool reset_transport_on_timeout) {
|
||||
clear_ack_signal();
|
||||
if (printer_protocol_send_frame(cmd, payload, payload_len, with_checksum) != ESP_OK) {
|
||||
return false;
|
||||
}
|
||||
bool ok = wait_ack(cmd, timeout_ms);
|
||||
if (!ok && reset_transport_on_timeout) {
|
||||
reset_transport_after_timeout();
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
esp_err_t printer_protocol_send_cmd_wait_response(uint8_t cmd,
|
||||
const uint8_t *payload,
|
||||
uint16_t payload_len,
|
||||
bool with_checksum,
|
||||
uint32_t timeout_ms,
|
||||
bool expect_ack,
|
||||
bool reset_transport_on_timeout,
|
||||
uint8_t *out_payload,
|
||||
size_t out_payload_cap,
|
||||
uint16_t *out_payload_len,
|
||||
char *err,
|
||||
size_t err_len) {
|
||||
clear_ack_signal();
|
||||
if (printer_protocol_send_frame(cmd, payload, payload_len, with_checksum) != ESP_OK) {
|
||||
if (err != NULL && err_len > 0) {
|
||||
snprintf(err, err_len, "send frame failed");
|
||||
}
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
uint8_t rsp[252];
|
||||
uint16_t rsp_len = 0;
|
||||
if (!printer_protocol_wait_response(cmd, rsp, sizeof(rsp), &rsp_len, timeout_ms)) {
|
||||
bool stopping = printer_protocol_is_stopping();
|
||||
if (!stopping && reset_transport_on_timeout) {
|
||||
reset_transport_after_timeout();
|
||||
}
|
||||
if (err != NULL && err_len > 0) {
|
||||
if (stopping) {
|
||||
snprintf(err, err_len, "protocol stopping");
|
||||
} else {
|
||||
snprintf(err,
|
||||
err_len,
|
||||
reset_transport_on_timeout ? "response timeout, transport reset"
|
||||
: "response timeout");
|
||||
}
|
||||
}
|
||||
return stopping ? ESP_ERR_INVALID_STATE : ESP_ERR_TIMEOUT;
|
||||
}
|
||||
|
||||
if (expect_ack && (rsp_len < 1 || rsp[0] != 0x01)) {
|
||||
if (err != NULL && err_len > 0) {
|
||||
snprintf(err, err_len, "cmd 0x%02X rejected", cmd);
|
||||
}
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
if (out_payload != NULL && out_payload_cap > 0 && rsp_len > 0) {
|
||||
size_t copy_len = rsp_len;
|
||||
if (copy_len > out_payload_cap) {
|
||||
copy_len = out_payload_cap;
|
||||
}
|
||||
memcpy(out_payload, rsp, copy_len);
|
||||
}
|
||||
if (out_payload_len != NULL) {
|
||||
*out_payload_len = rsp_len;
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static void status_poll_task(void *arg) {
|
||||
(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);
|
||||
}
|
||||
|
||||
@@ -547,20 +297,12 @@ static void status_poll_task(void *arg) {
|
||||
break;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
if (can_poll && 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()));
|
||||
}
|
||||
|
||||
@@ -571,59 +313,6 @@ static void status_poll_task(void *arg) {
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
static void on_rx_frame(const uint8_t *data, size_t len) {
|
||||
if (data == NULL || len < 4 || s_mutex == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (data[0] != PROTO_ADDR) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t cmd = data[1];
|
||||
uint16_t payload_len = ((uint16_t)data[2] << 8) | data[3];
|
||||
size_t required = (size_t)payload_len + 4;
|
||||
if (len < required) {
|
||||
return;
|
||||
}
|
||||
|
||||
const uint8_t *payload = &data[4];
|
||||
|
||||
if (cmd == CMD_GET_STATUS && payload_len >= 5) {
|
||||
bool has_paper = payload[0] != 0;
|
||||
uint8_t battery = payload[1];
|
||||
int sign = (payload[2] == 0x2D) ? -1 : 1;
|
||||
int temp_x10 = ((int)payload[3] << 8) | payload[4];
|
||||
float temperature = (float)(sign * temp_x10) / 10.0f;
|
||||
|
||||
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;
|
||||
xSemaphoreGive(s_mutex);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (payload_len >= 1) {
|
||||
if (xSemaphoreTake(s_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
|
||||
s_last_rsp_cmd = cmd;
|
||||
s_last_rsp_payload_len = payload_len;
|
||||
if (s_last_rsp_payload_len > sizeof(s_last_rsp_payload)) {
|
||||
s_last_rsp_payload_len = sizeof(s_last_rsp_payload);
|
||||
}
|
||||
memcpy(s_last_rsp_payload, payload, s_last_rsp_payload_len);
|
||||
xSemaphoreGive(s_mutex);
|
||||
}
|
||||
if (s_evt != NULL) {
|
||||
xEventGroupSetBits(s_evt, EVT_ACK);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void destroy_runtime_objects(void) {
|
||||
if (s_job_queue != NULL) {
|
||||
vQueueDelete(s_job_queue);
|
||||
@@ -672,7 +361,6 @@ 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 = default_backend_on_startup();
|
||||
|
||||
esp_err_t err = ensure_backend_ready(s_backend);
|
||||
@@ -682,24 +370,22 @@ esp_err_t printer_protocol_init(void) {
|
||||
return err;
|
||||
}
|
||||
|
||||
if (s_backend == PRINTER_BACKEND_DIRECT) {
|
||||
uint32_t connect_timeout_ms = domain_policy_rest_printer_connect_timeout_ms();
|
||||
err = platform_direct_printer_connect(connect_timeout_ms);
|
||||
if (err != ESP_OK) {
|
||||
s_protocol_initialized = false;
|
||||
platform_direct_printer_disconnect();
|
||||
platform_direct_printer_deinit();
|
||||
destroy_runtime_objects();
|
||||
return err;
|
||||
}
|
||||
|
||||
if (xSemaphoreTake(s_mutex, pdMS_TO_TICKS(200)) == pdTRUE) {
|
||||
refresh_direct_status_locked();
|
||||
xSemaphoreGive(s_mutex);
|
||||
}
|
||||
ESP_LOGI(TAG, "direct printer auto connected at startup");
|
||||
uint32_t connect_timeout_ms = domain_policy_rest_printer_connect_timeout_ms();
|
||||
err = platform_direct_printer_connect(connect_timeout_ms);
|
||||
if (err != ESP_OK) {
|
||||
s_protocol_initialized = false;
|
||||
platform_direct_printer_disconnect();
|
||||
platform_direct_printer_deinit();
|
||||
destroy_runtime_objects();
|
||||
return err;
|
||||
}
|
||||
|
||||
if (xSemaphoreTake(s_mutex, pdMS_TO_TICKS(200)) == pdTRUE) {
|
||||
refresh_direct_status_locked();
|
||||
xSemaphoreGive(s_mutex);
|
||||
}
|
||||
ESP_LOGI(TAG, "direct printer auto connected at startup");
|
||||
|
||||
BaseType_t ok = printer_create_task_prefer_psram(printer_protocol_worker_task,
|
||||
"print_worker",
|
||||
6144,
|
||||
@@ -757,7 +443,6 @@ esp_err_t printer_protocol_stop(uint32_t timeout_ms) {
|
||||
(void)xQueueSend(s_job_queue, &sentinel, 0);
|
||||
}
|
||||
|
||||
ble_printer_client_disconnect();
|
||||
platform_direct_printer_disconnect();
|
||||
|
||||
bool worker_done = (s_worker_task == NULL);
|
||||
@@ -795,14 +480,10 @@ esp_err_t printer_protocol_stop(uint32_t timeout_ms) {
|
||||
s_busy_refcnt = 0;
|
||||
s_status_poll_pause_next_token = 1;
|
||||
memset(s_status_poll_pause_tokens, 0, sizeof(s_status_poll_pause_tokens));
|
||||
memset(s_last_rsp_payload, 0, sizeof(s_last_rsp_payload));
|
||||
s_last_rsp_cmd = 0;
|
||||
s_last_rsp_payload_len = 0;
|
||||
s_worker_task = NULL;
|
||||
s_status_poll_task_handle = NULL;
|
||||
s_protocol_initialized = false;
|
||||
s_protocol_stopping = false;
|
||||
s_ble_client_initialized = false;
|
||||
s_backend = default_backend_on_startup();
|
||||
|
||||
runtime_diag_set_gauge(RUNTIME_DIAG_GAUGE_STATUS_POLL_PAUSE_DEPTH, 0);
|
||||
@@ -815,18 +496,20 @@ 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) {
|
||||
if (backend != PRINTER_BACKEND_BLE && backend != PRINTER_BACKEND_DIRECT) {
|
||||
if (backend != PRINTER_BACKEND_DIRECT) {
|
||||
if (err != NULL && err_len > 0) {
|
||||
snprintf(err, err_len, "invalid backend");
|
||||
snprintf(err, err_len, "only direct backend is supported");
|
||||
}
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
return ESP_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
if (backend == PRINTER_BACKEND_DIRECT && !runtime_policy_direct_printer_enabled()) {
|
||||
|
||||
if (!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");
|
||||
@@ -841,12 +524,6 @@ esp_err_t printer_protocol_set_backend(printer_backend_t backend, char *err, siz
|
||||
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) {
|
||||
@@ -855,26 +532,18 @@ esp_err_t printer_protocol_set_backend(printer_backend_t backend, char *err, siz
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
s_backend = backend;
|
||||
s_backend = PRINTER_BACKEND_DIRECT;
|
||||
xSemaphoreGive(s_mutex);
|
||||
|
||||
ble_printer_client_disconnect();
|
||||
platform_direct_printer_disconnect();
|
||||
|
||||
esp_err_t rc = ensure_backend_ready(backend);
|
||||
esp_err_t rc = ensure_backend_ready(PRINTER_BACKEND_DIRECT);
|
||||
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");
|
||||
snprintf(err, err_len, "direct backend unavailable");
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
if (backend == PRINTER_BACKEND_DIRECT && xSemaphoreTake(s_mutex, pdMS_TO_TICKS(200)) == pdTRUE) {
|
||||
if (xSemaphoreTake(s_mutex, pdMS_TO_TICKS(200)) == pdTRUE) {
|
||||
refresh_direct_status_locked();
|
||||
xSemaphoreGive(s_mutex);
|
||||
}
|
||||
@@ -911,11 +580,9 @@ esp_err_t printer_protocol_connect_ex(const printer_connect_options_t *opt, char
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -924,36 +591,23 @@ esp_err_t printer_protocol_connect_ex(const printer_connect_options_t *opt, char
|
||||
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;
|
||||
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 ble_printer_client_connect(name, timeout_ms);
|
||||
return rc;
|
||||
}
|
||||
|
||||
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);
|
||||
rc = platform_direct_printer_connect(timeout_ms);
|
||||
if (rc == ESP_OK && s_mutex != NULL && xSemaphoreTake(s_mutex, pdMS_TO_TICKS(200)) == pdTRUE) {
|
||||
refresh_direct_status_locked();
|
||||
xSemaphoreGive(s_mutex);
|
||||
}
|
||||
|
||||
if (err != NULL && err_len > 0) {
|
||||
snprintf(err, err_len, "unsupported backend");
|
||||
}
|
||||
return ESP_ERR_NOT_SUPPORTED;
|
||||
return rc;
|
||||
}
|
||||
|
||||
void printer_protocol_disconnect(void) {
|
||||
ble_printer_client_disconnect();
|
||||
platform_direct_printer_disconnect();
|
||||
}
|
||||
|
||||
@@ -979,31 +633,20 @@ void printer_protocol_get_runtime_status(printer_runtime_status_t *out_status) {
|
||||
out_status->supports_label_offset = caps.supports_label_offset;
|
||||
out_status->supports_ota = caps.supports_ota;
|
||||
|
||||
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;
|
||||
out_status->connected = platform_direct_printer_is_connected();
|
||||
out_status->transport_ready = out_status->connected;
|
||||
|
||||
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);
|
||||
}
|
||||
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) {
|
||||
|
||||
@@ -2,18 +2,6 @@
|
||||
#include "printer_protocol_internal.h"
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
static void write_err(char *err, size_t err_len, const char *msg) {
|
||||
if (err != NULL && err_len > 0) {
|
||||
@@ -21,229 +9,47 @@ static void write_err(char *err, size_t err_len, const char *msg) {
|
||||
}
|
||||
}
|
||||
|
||||
static esp_err_t unsupported_operation(char *err, size_t err_len, const char *op) {
|
||||
if (err != NULL && err_len > 0) {
|
||||
snprintf(err, err_len, "%s is 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 (!runtime_policy_direct_printer_enabled()) {
|
||||
write_err(err, err_len, "direct backend disabled");
|
||||
return ESP_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
if (!ble_printer_client_is_connected()) {
|
||||
if (err != NULL && err_len > 0) {
|
||||
snprintf(err, err_len, "printer not connected");
|
||||
}
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
if (!printer_protocol_acquire_control_lane(runtime_policy_printer_control_lock_timeout_ms(), err, err_len)) {
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
uint8_t payload = 0x01;
|
||||
esp_err_t rc = printer_protocol_send_cmd_wait_response(CMD_GAP_MOVE,
|
||||
&payload,
|
||||
1,
|
||||
true,
|
||||
timeout_ms,
|
||||
true,
|
||||
true,
|
||||
NULL,
|
||||
0,
|
||||
NULL,
|
||||
err,
|
||||
err_len);
|
||||
printer_protocol_release_control_lane();
|
||||
return rc;
|
||||
return platform_direct_printer_gap_move(timeout_ms, err, err_len);
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
if (!ble_printer_client_is_connected()) {
|
||||
if (err != NULL && err_len > 0) {
|
||||
snprintf(err, err_len, "printer not connected");
|
||||
}
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
if (!printer_protocol_acquire_control_lane(runtime_policy_printer_control_lock_timeout_ms(), err, err_len)) {
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
uint8_t req = 0x01;
|
||||
uint8_t rsp[16];
|
||||
uint16_t rsp_len = 0;
|
||||
esp_err_t rc = printer_protocol_send_cmd_wait_response(CMD_GET_OFFSET,
|
||||
&req,
|
||||
1,
|
||||
true,
|
||||
timeout_ms,
|
||||
false,
|
||||
false,
|
||||
rsp,
|
||||
sizeof(rsp),
|
||||
&rsp_len,
|
||||
err,
|
||||
err_len);
|
||||
printer_protocol_release_control_lane();
|
||||
if (rc != ESP_OK) {
|
||||
return rc;
|
||||
}
|
||||
if (rsp_len < 1) {
|
||||
if (err != NULL && err_len > 0) {
|
||||
snprintf(err, err_len, "invalid offset response");
|
||||
}
|
||||
return ESP_FAIL;
|
||||
}
|
||||
if (rsp[0] == 0xFF) {
|
||||
if (err != NULL && err_len > 0) {
|
||||
snprintf(err, err_len, "offset unavailable");
|
||||
}
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
*out_offset = rsp[0];
|
||||
return ESP_OK;
|
||||
(void)out_offset;
|
||||
(void)timeout_ms;
|
||||
return unsupported_operation(err, err_len, "label offset");
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
if (!printer_protocol_acquire_control_lane(runtime_policy_printer_control_lock_timeout_ms(), err, err_len)) {
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
esp_err_t rc = printer_protocol_send_cmd_wait_response(CMD_SET_OFFSET,
|
||||
&offset,
|
||||
1,
|
||||
true,
|
||||
timeout_ms,
|
||||
true,
|
||||
true,
|
||||
NULL,
|
||||
0,
|
||||
NULL,
|
||||
err,
|
||||
err_len);
|
||||
printer_protocol_release_control_lane();
|
||||
return rc;
|
||||
(void)offset;
|
||||
(void)timeout_ms;
|
||||
return unsupported_operation(err, err_len, "label offset");
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
if (!printer_protocol_acquire_control_lane(runtime_policy_printer_control_lock_timeout_ms(), err, err_len)) {
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
esp_err_t rc = printer_protocol_send_cmd_wait_response(CMD_BOOT_JUMP_BOOT,
|
||||
NULL,
|
||||
0,
|
||||
true,
|
||||
timeout_ms,
|
||||
true,
|
||||
true,
|
||||
NULL,
|
||||
0,
|
||||
NULL,
|
||||
err,
|
||||
err_len);
|
||||
printer_protocol_release_control_lane();
|
||||
return rc;
|
||||
(void)timeout_ms;
|
||||
return unsupported_operation(err, err_len, "ota jump boot");
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
if (!printer_protocol_acquire_control_lane(runtime_policy_printer_control_lock_timeout_ms(), err, err_len)) {
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
esp_err_t rc = printer_protocol_send_cmd_wait_response(CMD_BOOT_JUMP_APP,
|
||||
NULL,
|
||||
0,
|
||||
true,
|
||||
timeout_ms,
|
||||
true,
|
||||
true,
|
||||
NULL,
|
||||
0,
|
||||
NULL,
|
||||
err,
|
||||
err_len);
|
||||
printer_protocol_release_control_lane();
|
||||
return rc;
|
||||
(void)timeout_ms;
|
||||
return unsupported_operation(err, err_len, "ota jump app");
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
if (!printer_protocol_acquire_control_lane(runtime_policy_printer_control_lock_timeout_ms(), err, err_len)) {
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
uint8_t payload[2] = {
|
||||
(uint8_t)((page_num >> 8) & 0xFF),
|
||||
(uint8_t)(page_num & 0xFF),
|
||||
};
|
||||
|
||||
esp_err_t rc = printer_protocol_send_cmd_wait_response(CMD_BOOT_ERASE_PAGE,
|
||||
payload,
|
||||
sizeof(payload),
|
||||
true,
|
||||
timeout_ms,
|
||||
true,
|
||||
true,
|
||||
NULL,
|
||||
0,
|
||||
NULL,
|
||||
err,
|
||||
err_len);
|
||||
printer_protocol_release_control_lane();
|
||||
return rc;
|
||||
(void)page_num;
|
||||
(void)timeout_ms;
|
||||
return unsupported_operation(err, err_len, "ota erase");
|
||||
}
|
||||
|
||||
esp_err_t printer_protocol_ota_write_frame(uint16_t packet_num,
|
||||
@@ -253,108 +59,21 @@ 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");
|
||||
}
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
if (!ble_printer_client_is_connected()) {
|
||||
if (err != NULL && err_len > 0) {
|
||||
snprintf(err, err_len, "printer not connected");
|
||||
}
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
if (!printer_protocol_acquire_control_lane(runtime_policy_printer_control_lock_timeout_ms(), err, err_len)) {
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
uint8_t payload[3 + OTA_MAX_DATA_PER_FRAME];
|
||||
payload[0] = (uint8_t)((packet_num >> 8) & 0xFF);
|
||||
payload[1] = (uint8_t)(packet_num & 0xFF);
|
||||
payload[2] = is_last_frame ? 0x01 : 0x00;
|
||||
if (data_len > 0) {
|
||||
memcpy(&payload[3], data, data_len);
|
||||
}
|
||||
|
||||
esp_err_t rc = printer_protocol_send_cmd_wait_response(CMD_BOOT_WRITE_DATA,
|
||||
payload,
|
||||
(uint16_t)(3 + data_len),
|
||||
true,
|
||||
timeout_ms,
|
||||
true,
|
||||
true,
|
||||
NULL,
|
||||
0,
|
||||
NULL,
|
||||
err,
|
||||
err_len);
|
||||
printer_protocol_release_control_lane();
|
||||
return rc;
|
||||
(void)packet_num;
|
||||
(void)is_last_frame;
|
||||
(void)data;
|
||||
(void)data_len;
|
||||
(void)timeout_ms;
|
||||
return unsupported_operation(err, err_len, "ota write");
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
if (!ble_printer_client_is_connected()) {
|
||||
if (err != NULL && err_len > 0) {
|
||||
snprintf(err, err_len, "printer not connected");
|
||||
}
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
if (!printer_protocol_acquire_control_lane(runtime_policy_printer_control_lock_timeout_ms(), err, err_len)) {
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
uint8_t rsp[16];
|
||||
uint16_t rsp_len = 0;
|
||||
esp_err_t rc = printer_protocol_send_cmd_wait_response(CMD_BOOT_GET_VERSION,
|
||||
NULL,
|
||||
0,
|
||||
true,
|
||||
timeout_ms,
|
||||
false,
|
||||
false,
|
||||
rsp,
|
||||
sizeof(rsp),
|
||||
&rsp_len,
|
||||
err,
|
||||
err_len);
|
||||
printer_protocol_release_control_lane();
|
||||
if (rc != ESP_OK) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
if (rsp_len < 3) {
|
||||
if (err != NULL && err_len > 0) {
|
||||
snprintf(err, err_len, "invalid version response");
|
||||
}
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
out_version->major = rsp[0];
|
||||
out_version->minor = rsp[1];
|
||||
out_version->patch = rsp[2];
|
||||
return ESP_OK;
|
||||
(void)out_version;
|
||||
(void)timeout_ms;
|
||||
return unsupported_operation(err, err_len, "ota version");
|
||||
}
|
||||
|
||||
esp_err_t printer_protocol_get_direct_debug_config(printer_direct_debug_config_t *out_config,
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#include "domain.h"
|
||||
#include "printer_protocol_internal.h"
|
||||
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "esp_log.h"
|
||||
@@ -12,51 +12,6 @@
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
if (xSemaphoreTake(s_mutex, pdMS_TO_TICKS(200)) != pdTRUE) {
|
||||
return false;
|
||||
}
|
||||
old_ms = s_status.updated_ms;
|
||||
xSemaphoreGive(s_mutex);
|
||||
|
||||
uint8_t payload = 0x00;
|
||||
if (printer_protocol_send_frame(CMD_GET_STATUS, &payload, 0, true) != ESP_OK) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int64_t deadline = esp_timer_get_time() / 1000 + timeout_ms;
|
||||
while ((esp_timer_get_time() / 1000) < deadline) {
|
||||
if (printer_protocol_is_stopping()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(20));
|
||||
|
||||
if (xSemaphoreTake(s_mutex, pdMS_TO_TICKS(100)) != pdTRUE) {
|
||||
continue;
|
||||
}
|
||||
|
||||
bool updated = s_status.updated_ms > old_ms;
|
||||
xSemaphoreGive(s_mutex);
|
||||
|
||||
if (updated) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static uint16_t density_to_hot_time(const char *density) {
|
||||
if (density == NULL) {
|
||||
return 2000;
|
||||
@@ -77,38 +32,6 @@ static uint16_t density_to_hot_time(const char *density) {
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
if (direct_ignore_precheck) {
|
||||
return true;
|
||||
}
|
||||
@@ -140,126 +63,6 @@ static bool precheck_printer_ready(bool direct_ignore_precheck, char *err, size_
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool run_print_job_ble(job_slot_t *job) {
|
||||
char err[96];
|
||||
err[0] = '\0';
|
||||
uint8_t next_progress_log = 25;
|
||||
|
||||
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(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;
|
||||
}
|
||||
|
||||
if (job->cancel_requested) {
|
||||
snprintf(job->error, sizeof(job->error), "job canceled");
|
||||
ESP_LOGW(TAG, "job %u print canceled before start", (unsigned)job->id);
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t power_on = 0x01;
|
||||
if (!printer_protocol_send_cmd_with_ack(CMD_POWER, &power_on, 1, true, 1500, true)) {
|
||||
snprintf(job->error, sizeof(job->error), "power on failed");
|
||||
ESP_LOGW(TAG, "job %u print aborted: %s", (unsigned)job->id, job->error);
|
||||
return false;
|
||||
}
|
||||
|
||||
uint16_t hot_time = density_to_hot_time(job->density);
|
||||
uint8_t param[4] = {
|
||||
0x01,
|
||||
0x02,
|
||||
(uint8_t)((hot_time >> 8) & 0xFF),
|
||||
(uint8_t)(hot_time & 0xFF),
|
||||
};
|
||||
|
||||
if (!printer_protocol_send_cmd_with_ack(CMD_SET_PARAM, param, sizeof(param), true, 1500, true)) {
|
||||
snprintf(job->error, sizeof(job->error), "set print param failed");
|
||||
ESP_LOGW(TAG, "job %u print aborted: %s", (unsigned)job->id, job->error);
|
||||
return false;
|
||||
}
|
||||
|
||||
const size_t chunk_max = 240;
|
||||
size_t total_chunks = (size_t)ceil((double)job->data_len / (double)chunk_max);
|
||||
ESP_LOGI(TAG,
|
||||
"job %u data transfer start, chunks=%u, chunk_bytes=%u, hot_time=%u",
|
||||
(unsigned)job->id,
|
||||
(unsigned)total_chunks,
|
||||
(unsigned)chunk_max,
|
||||
(unsigned)hot_time);
|
||||
|
||||
for (size_t i = 0; i < total_chunks; ++i) {
|
||||
if (printer_protocol_is_stopping()) {
|
||||
snprintf(job->error, sizeof(job->error), "protocol stopping");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (job->cancel_requested) {
|
||||
snprintf(job->error, sizeof(job->error), "job canceled");
|
||||
ESP_LOGW(TAG,
|
||||
"job %u print canceled at chunk %u/%u",
|
||||
(unsigned)job->id,
|
||||
(unsigned)(i + 1),
|
||||
(unsigned)total_chunks);
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t start = i * chunk_max;
|
||||
size_t remain = job->data_len - start;
|
||||
size_t chunk_len = remain > chunk_max ? chunk_max : remain;
|
||||
|
||||
if (!printer_protocol_send_cmd_with_ack(CMD_SEND_DATA,
|
||||
&job->data[start],
|
||||
(uint16_t)chunk_len,
|
||||
false,
|
||||
2500,
|
||||
true)) {
|
||||
snprintf(job->error, sizeof(job->error), "send chunk timeout at %u", (unsigned)i);
|
||||
ESP_LOGW(TAG, "job %u print aborted: %s", (unsigned)job->id, job->error);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (xSemaphoreTake(s_mutex, pdMS_TO_TICKS(200)) == pdTRUE) {
|
||||
job->progress = (uint8_t)(((i + 1) * 90) / total_chunks);
|
||||
if (job->progress < 5) {
|
||||
job->progress = 5;
|
||||
}
|
||||
xSemaphoreGive(s_mutex);
|
||||
}
|
||||
|
||||
while (job->progress >= next_progress_log && next_progress_log <= 90) {
|
||||
ESP_LOGI(TAG,
|
||||
"job %u print progress=%u%% (%u/%u chunks)",
|
||||
(unsigned)job->id,
|
||||
(unsigned)job->progress,
|
||||
(unsigned)(i + 1),
|
||||
(unsigned)total_chunks);
|
||||
next_progress_log = (uint8_t)(next_progress_log + 25);
|
||||
}
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "job %u data transfer done", (unsigned)job->id);
|
||||
|
||||
uint8_t power_off = 0x00;
|
||||
(void)printer_protocol_send_cmd_with_ack(CMD_POWER, &power_off, 1, true, 1200, false);
|
||||
|
||||
uint8_t feed_payload[3] = {0x2B, 0x00, 0x0C};
|
||||
(void)printer_protocol_send_cmd_with_ack(CMD_SET_DISTANCE,
|
||||
feed_payload,
|
||||
sizeof(feed_payload),
|
||||
true,
|
||||
1200,
|
||||
false);
|
||||
|
||||
ESP_LOGI(TAG, "job %u print command sequence done", (unsigned)job->id);
|
||||
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");
|
||||
@@ -294,7 +97,7 @@ static bool run_print_job_direct(job_slot_t *job) {
|
||||
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') {
|
||||
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");
|
||||
@@ -315,9 +118,8 @@ static bool run_print_job_direct(job_slot_t *job) {
|
||||
|
||||
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",
|
||||
"job %u print start, backend=direct, 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,
|
||||
@@ -329,9 +131,6 @@ static bool run_print_job(job_slot_t *job) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (printer_protocol_get_backend() == PRINTER_BACKEND_BLE) {
|
||||
return run_print_job_ble(job);
|
||||
}
|
||||
return run_print_job_direct(job);
|
||||
}
|
||||
|
||||
@@ -417,11 +216,11 @@ void printer_protocol_worker_task(void *arg) {
|
||||
|
||||
if (ok) {
|
||||
s_jobs[idx].state = PRINT_JOB_STATE_SUCCESS;
|
||||
s_jobs[idx].error[0] = '\0';
|
||||
s_jobs[idx].error[0] = 0;
|
||||
runtime_diag_counter_add(RUNTIME_DIAG_COUNTER_PRINTER_JOB_SUCCESS, 1);
|
||||
} else if (s_jobs[idx].cancel_requested || printer_protocol_is_stopping()) {
|
||||
s_jobs[idx].state = PRINT_JOB_STATE_CANCELED;
|
||||
if (s_jobs[idx].error[0] == '\0') {
|
||||
if (s_jobs[idx].error[0] == 0) {
|
||||
strlcpy(s_jobs[idx].error, "job canceled", sizeof(s_jobs[idx].error));
|
||||
}
|
||||
runtime_diag_counter_add(RUNTIME_DIAG_COUNTER_PRINTER_JOB_CANCELED, 1);
|
||||
@@ -446,7 +245,7 @@ void printer_protocol_worker_task(void *arg) {
|
||||
(unsigned)s_jobs[idx].id,
|
||||
printer_protocol_job_state_str(s_jobs[idx].state),
|
||||
(long long)duration_ms,
|
||||
s_jobs[idx].error[0] != '\0' ? s_jobs[idx].error : "-");
|
||||
s_jobs[idx].error[0] != 0 ? s_jobs[idx].error : "-");
|
||||
}
|
||||
|
||||
free(s_jobs[idx].data);
|
||||
|
||||
Reference in New Issue
Block a user