126 lines
5.3 KiB
C
126 lines
5.3 KiB
C
#include "rest_server_internal.h"
|
|
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "esp_log.h"
|
|
#include "printer_protocol.h"
|
|
|
|
static const char *TAG = "rest_print";
|
|
|
|
static const char *trace_id_or_default(const char *trace_id) {
|
|
return (trace_id != NULL && trace_id[0] != '\0') ? trace_id : "-";
|
|
}
|
|
|
|
esp_err_t rest_server_print_submit_raster_job_and_reply(httpd_req_t *req,
|
|
const uint8_t *raw,
|
|
size_t raw_len,
|
|
uint16_t width,
|
|
uint16_t height,
|
|
const char *density,
|
|
const char *warning,
|
|
const char *trace_id) {
|
|
const char *trace = trace_id_or_default(trace_id);
|
|
ESP_LOGI(TAG,
|
|
"print submit start, trace=%s, raster_bytes=%u, size=%ux%u, density=%s",
|
|
trace,
|
|
(unsigned)raw_len,
|
|
(unsigned)width,
|
|
(unsigned)height,
|
|
density != NULL ? density : "中等");
|
|
|
|
char submit_err[128] = {0};
|
|
uint32_t job_id = 0;
|
|
esp_err_t submit_rc = printer_protocol_submit_raster_job(raw,
|
|
raw_len,
|
|
width,
|
|
height,
|
|
density,
|
|
&job_id,
|
|
submit_err,
|
|
sizeof(submit_err));
|
|
if (submit_rc != ESP_OK) {
|
|
ESP_LOGW(TAG,
|
|
"print submit failed, trace=%s, rc=0x%x, msg=%s",
|
|
trace,
|
|
(unsigned)submit_rc,
|
|
submit_err[0] != '\0' ? submit_err : "submit failed");
|
|
return rest_server_send_error(req,
|
|
"400 Bad Request",
|
|
submit_err[0] != '\0' ? submit_err : "submit failed");
|
|
}
|
|
|
|
ESP_LOGI(TAG, "print submit done, trace=%s, job_id=%u", trace, (unsigned)job_id);
|
|
|
|
cJSON *root = cJSON_CreateObject();
|
|
cJSON_AddBoolToObject(root, "ok", true);
|
|
cJSON_AddNumberToObject(root, "job_id", job_id);
|
|
cJSON_AddStringToObject(root, "state", "queued");
|
|
if (warning != NULL && warning[0] != '\0') {
|
|
cJSON_AddStringToObject(root, "warning", warning);
|
|
}
|
|
|
|
esp_err_t err = rest_server_send_json(req, "202 Accepted", root);
|
|
cJSON_Delete(root);
|
|
return err;
|
|
}
|
|
|
|
esp_err_t rest_server_print_raster_post(httpd_req_t *req) {
|
|
if (!rest_server_auth_ok(req)) {
|
|
return rest_server_send_error(req, "401 Unauthorized", "unauthorized");
|
|
}
|
|
|
|
char *body = NULL;
|
|
esp_err_t body_err = rest_server_read_body(req, &body);
|
|
if (body_err != ESP_OK) {
|
|
return rest_server_send_error(req, "400 Bad Request", "invalid request body");
|
|
}
|
|
|
|
cJSON *json = cJSON_Parse(body);
|
|
free(body);
|
|
if (json == NULL) {
|
|
return rest_server_send_error(req, "400 Bad Request", "invalid json");
|
|
}
|
|
|
|
cJSON *jwidth = cJSON_GetObjectItemCaseSensitive(json, "width");
|
|
cJSON *jheight = cJSON_GetObjectItemCaseSensitive(json, "height");
|
|
cJSON *jencoding = cJSON_GetObjectItemCaseSensitive(json, "encoding");
|
|
cJSON *jdata = cJSON_GetObjectItemCaseSensitive(json, "data");
|
|
cJSON *jdensity = cJSON_GetObjectItemCaseSensitive(json, "density");
|
|
|
|
if (!cJSON_IsNumber(jwidth) || !cJSON_IsNumber(jheight) ||
|
|
!cJSON_IsString(jencoding) || !cJSON_IsString(jdata)) {
|
|
cJSON_Delete(json);
|
|
return rest_server_send_error(req, "400 Bad Request", "missing required fields");
|
|
}
|
|
|
|
if (strcmp(jencoding->valuestring, "base64_msb_1bpp") != 0) {
|
|
cJSON_Delete(json);
|
|
return rest_server_send_error(req, "400 Bad Request", "unsupported encoding");
|
|
}
|
|
|
|
const char *density = (cJSON_IsString(jdensity) && jdensity->valuestring != NULL)
|
|
? jdensity->valuestring
|
|
: "中等";
|
|
|
|
uint8_t *raw = NULL;
|
|
size_t raw_len = 0;
|
|
esp_err_t b64_rc = rest_server_base64_decode_alloc(jdata->valuestring, &raw, &raw_len);
|
|
if (b64_rc != ESP_OK) {
|
|
cJSON_Delete(json);
|
|
return rest_server_send_error(req, "400 Bad Request", "invalid base64");
|
|
}
|
|
|
|
esp_err_t submit_rc = rest_server_print_submit_raster_job_and_reply(req,
|
|
raw,
|
|
raw_len,
|
|
(uint16_t)jwidth->valuedouble,
|
|
(uint16_t)jheight->valuedouble,
|
|
density,
|
|
NULL,
|
|
NULL);
|
|
free(raw);
|
|
cJSON_Delete(json);
|
|
return submit_rc;
|
|
}
|