refactor(printer): remove BLE backend and dependencies
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user