refactor(structure): move layered firmware modules from main to components
This commit is contained in:
@@ -1,15 +1,15 @@
|
||||
idf_component_register(
|
||||
SRCS
|
||||
"../../main/platform/src/platform_bootstrap.c"
|
||||
"../../main/platform/src/wifi_manager.c"
|
||||
"../../main/platform/src/ble_printer_client.c"
|
||||
"../../main/platform/src/voice_audio.c"
|
||||
"../../main/platform/src/runtime_policy.c"
|
||||
"../../main/platform/src/runtime_diagnostics.c"
|
||||
"src/platform_bootstrap.c"
|
||||
"src/wifi_manager.c"
|
||||
"src/ble_printer_client.c"
|
||||
"src/voice_audio.c"
|
||||
"src/runtime_policy.c"
|
||||
"src/runtime_diagnostics.c"
|
||||
INCLUDE_DIRS
|
||||
"../../main/platform/include"
|
||||
"include"
|
||||
PRIV_INCLUDE_DIRS
|
||||
"../../main/platform/internal"
|
||||
"internal"
|
||||
REQUIRES
|
||||
bt
|
||||
esp_coex
|
||||
|
||||
13
components/platform/README.md
Normal file
13
components/platform/README.md
Normal file
@@ -0,0 +1,13 @@
|
||||
# Platform Layer
|
||||
|
||||
Purpose: hardware and system resource abstraction only.
|
||||
|
||||
Allowed examples:
|
||||
- Wi-Fi/BLE driver wrappers
|
||||
- NVS/time/RTOS/system adapters
|
||||
- board-specific IO and transport bindings
|
||||
|
||||
Rules:
|
||||
- Expose stable APIs in `include/`.
|
||||
- Keep private headers in `internal/`.
|
||||
- Do not depend on `domain`, `control_plane`, or `app_composition`.
|
||||
0
components/platform/include/.gitkeep
Normal file
0
components/platform/include/.gitkeep
Normal file
25
components/platform/include/ble_printer_client.h
Normal file
25
components/platform/include/ble_printer_client.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "esp_err.h"
|
||||
|
||||
typedef void (*ble_frame_rx_cb_t)(const uint8_t *data, size_t len);
|
||||
|
||||
typedef struct {
|
||||
bool connected;
|
||||
bool notify_ready;
|
||||
uint16_t mtu;
|
||||
} ble_link_state_t;
|
||||
|
||||
esp_err_t ble_printer_client_init(ble_frame_rx_cb_t rx_cb);
|
||||
|
||||
esp_err_t ble_printer_client_connect(const char *target_name, uint32_t timeout_ms);
|
||||
void ble_printer_client_disconnect(void);
|
||||
|
||||
bool ble_printer_client_is_connected(void);
|
||||
void ble_printer_client_get_link_state(ble_link_state_t *out_state);
|
||||
|
||||
esp_err_t ble_printer_client_write(const uint8_t *data, size_t len);
|
||||
5
components/platform/include/platform_bootstrap.h
Normal file
5
components/platform/include/platform_bootstrap.h
Normal file
@@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "esp_err.h"
|
||||
|
||||
esp_err_t platform_bootstrap_init(void);
|
||||
61
components/platform/include/runtime_diagnostics.h
Normal file
61
components/platform/include/runtime_diagnostics.h
Normal file
@@ -0,0 +1,61 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "esp_err.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
RUNTIME_DIAG_COUNTER_LIFECYCLE_START_ATTEMPT = 0,
|
||||
RUNTIME_DIAG_COUNTER_LIFECYCLE_START_SUCCESS,
|
||||
RUNTIME_DIAG_COUNTER_LIFECYCLE_START_FAILED,
|
||||
RUNTIME_DIAG_COUNTER_LIFECYCLE_START_RETRY,
|
||||
RUNTIME_DIAG_COUNTER_LIFECYCLE_STOP_ATTEMPT,
|
||||
RUNTIME_DIAG_COUNTER_LIFECYCLE_STOP_SUCCESS,
|
||||
RUNTIME_DIAG_COUNTER_LIFECYCLE_STOP_FAILED,
|
||||
RUNTIME_DIAG_COUNTER_WIFI_CONNECT_SUCCESS,
|
||||
RUNTIME_DIAG_COUNTER_WIFI_CONNECT_FAILED,
|
||||
RUNTIME_DIAG_COUNTER_WIFI_CONNECT_TIMEOUT,
|
||||
RUNTIME_DIAG_COUNTER_PRINTER_JOB_SUBMITTED,
|
||||
RUNTIME_DIAG_COUNTER_PRINTER_JOB_SUCCESS,
|
||||
RUNTIME_DIAG_COUNTER_PRINTER_JOB_FAILED,
|
||||
RUNTIME_DIAG_COUNTER_PRINTER_JOB_CANCELED,
|
||||
RUNTIME_DIAG_COUNTER_IMAGE_GENERATE_ATTEMPT,
|
||||
RUNTIME_DIAG_COUNTER_IMAGE_GENERATE_SUCCESS,
|
||||
RUNTIME_DIAG_COUNTER_IMAGE_GENERATE_FAILED,
|
||||
RUNTIME_DIAG_COUNTER_IMAGE_GENERATE_TIMEOUT,
|
||||
RUNTIME_DIAG_COUNTER_REST_RESPONSES_TOTAL,
|
||||
RUNTIME_DIAG_COUNTER_REST_ERRORS_TOTAL,
|
||||
RUNTIME_DIAG_COUNTER_MAX,
|
||||
} runtime_diag_counter_t;
|
||||
|
||||
typedef enum {
|
||||
RUNTIME_DIAG_GAUGE_LIFECYCLE_STATE = 0,
|
||||
RUNTIME_DIAG_GAUGE_STATUS_POLL_PAUSE_DEPTH,
|
||||
RUNTIME_DIAG_GAUGE_PRINTER_QUEUE_DEPTH,
|
||||
RUNTIME_DIAG_GAUGE_MAX,
|
||||
} runtime_diag_gauge_t;
|
||||
|
||||
typedef struct {
|
||||
uint64_t counters[RUNTIME_DIAG_COUNTER_MAX];
|
||||
int32_t gauges[RUNTIME_DIAG_GAUGE_MAX];
|
||||
int64_t last_error_ms;
|
||||
esp_err_t last_error_code;
|
||||
char last_error_source[32];
|
||||
char last_error_message[96];
|
||||
} runtime_diag_snapshot_t;
|
||||
|
||||
void runtime_diag_counter_add(runtime_diag_counter_t counter, uint32_t delta);
|
||||
void runtime_diag_set_gauge(runtime_diag_gauge_t gauge, int32_t value);
|
||||
void runtime_diag_record_error(const char *source, esp_err_t code, const char *message);
|
||||
void runtime_diag_get_snapshot(runtime_diag_snapshot_t *out_snapshot);
|
||||
|
||||
const char *runtime_diag_counter_name(runtime_diag_counter_t counter);
|
||||
const char *runtime_diag_gauge_name(runtime_diag_gauge_t gauge);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
30
components/platform/include/runtime_policy.h
Normal file
30
components/platform/include/runtime_policy.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "esp_err.h"
|
||||
|
||||
uint32_t runtime_policy_wifi_connect_timeout_ms(void);
|
||||
|
||||
uint32_t runtime_policy_lifecycle_start_retry_count(void);
|
||||
uint32_t runtime_policy_lifecycle_retry_backoff_ms(uint32_t attempt);
|
||||
bool runtime_policy_is_retryable_error(esp_err_t err);
|
||||
|
||||
uint32_t runtime_policy_printer_control_lock_timeout_ms(void);
|
||||
uint32_t runtime_policy_printer_worker_queue_wait_ms(void);
|
||||
uint32_t runtime_policy_printer_queue_retry_delay_ms(void);
|
||||
uint32_t runtime_policy_printer_status_poll_interval_ms(void);
|
||||
uint32_t runtime_policy_printer_stop_timeout_ms(void);
|
||||
|
||||
uint32_t runtime_policy_rest_printer_connect_timeout_ms(void);
|
||||
uint32_t runtime_policy_rest_label_timeout_ms(void);
|
||||
uint32_t runtime_policy_rest_ota_timeout_ms(void);
|
||||
|
||||
uint32_t runtime_policy_image_generation_timeout_default_ms(void);
|
||||
uint32_t runtime_policy_image_generation_timeout_min_ms(void);
|
||||
uint32_t runtime_policy_image_generation_timeout_max_ms(void);
|
||||
|
||||
uint32_t runtime_policy_image_download_timeout_default_ms(void);
|
||||
uint32_t runtime_policy_image_download_timeout_min_ms(void);
|
||||
uint32_t runtime_policy_image_download_timeout_max_ms(void);
|
||||
31
components/platform/include/voice_audio.h
Normal file
31
components/platform/include/voice_audio.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "esp_err.h"
|
||||
|
||||
typedef struct {
|
||||
uint32_t sample_rate;
|
||||
uint8_t channels;
|
||||
uint8_t bits_per_sample;
|
||||
int output_volume;
|
||||
float input_gain_db;
|
||||
} voice_audio_open_config_t;
|
||||
|
||||
esp_err_t voice_audio_open(const voice_audio_open_config_t *cfg, char *err, size_t err_len);
|
||||
void voice_audio_close(void);
|
||||
|
||||
bool voice_audio_is_open(void);
|
||||
|
||||
esp_err_t voice_audio_read_pcm(int16_t *pcm,
|
||||
size_t samples,
|
||||
uint32_t timeout_ms,
|
||||
char *err,
|
||||
size_t err_len);
|
||||
esp_err_t voice_audio_write_pcm(const int16_t *pcm,
|
||||
size_t samples,
|
||||
uint32_t timeout_ms,
|
||||
char *err,
|
||||
size_t err_len);
|
||||
12
components/platform/include/wifi_manager.h
Normal file
12
components/platform/include/wifi_manager.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include "esp_err.h"
|
||||
|
||||
esp_err_t wifi_manager_start(void);
|
||||
esp_err_t wifi_manager_stop(void);
|
||||
|
||||
bool wifi_manager_is_ready(void);
|
||||
|
||||
void wifi_manager_get_ip(char *buf, size_t buf_len);
|
||||
0
components/platform/internal/.gitkeep
Normal file
0
components/platform/internal/.gitkeep
Normal file
0
components/platform/src/.gitkeep
Normal file
0
components/platform/src/.gitkeep
Normal file
697
components/platform/src/ble_printer_client.c
Normal file
697
components/platform/src/ble_printer_client.c
Normal file
@@ -0,0 +1,697 @@
|
||||
#include "ble_printer_client.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "esp_err.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_nimble_hci.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "host/ble_gatt.h"
|
||||
#include "host/ble_hs.h"
|
||||
#include "nimble/nimble_port.h"
|
||||
#include "nimble/nimble_port_freertos.h"
|
||||
#include "os/os_mbuf.h"
|
||||
#include "services/gap/ble_svc_gap.h"
|
||||
#include "services/gatt/ble_svc_gatt.h"
|
||||
|
||||
#define PRINTER_SERVICE_UUID16 0xFFF0
|
||||
#define PRINTER_NOTIFY_UUID16 0xFFF1
|
||||
#define PRINTER_WRITE_UUID16 0xFFF2
|
||||
#define CCCD_UUID16 0x2902
|
||||
#define PRINTER_NAME_PRIMARY "TQPrinter"
|
||||
#define PRINTER_NAME_LEGACY "lyfPrinter"
|
||||
|
||||
#define EVT_CONNECTED BIT0
|
||||
#define EVT_READY BIT1
|
||||
#define EVT_FAILED BIT2
|
||||
|
||||
static const char *TAG = "ble_client";
|
||||
|
||||
static EventGroupHandle_t s_evt_group;
|
||||
static SemaphoreHandle_t s_lock;
|
||||
|
||||
static ble_frame_rx_cb_t s_rx_cb;
|
||||
|
||||
static uint8_t s_addr_type;
|
||||
static ble_addr_t s_target_addr;
|
||||
static char s_target_name[32] = PRINTER_NAME_PRIMARY;
|
||||
static bool s_match_any_compatible;
|
||||
|
||||
static uint16_t s_conn_handle = BLE_HS_CONN_HANDLE_NONE;
|
||||
static uint16_t s_service_start;
|
||||
static uint16_t s_service_end;
|
||||
static uint16_t s_notify_val_handle;
|
||||
static uint16_t s_write_val_handle;
|
||||
static uint16_t s_cccd_handle;
|
||||
static uint16_t s_mtu = 23;
|
||||
|
||||
static bool s_scanning;
|
||||
static bool s_notify_ready;
|
||||
static bool s_host_synced;
|
||||
static bool s_initialized;
|
||||
|
||||
static uint16_t uuid16(const ble_uuid_t *uuid) {
|
||||
if (uuid == NULL || uuid->type != BLE_UUID_TYPE_16) {
|
||||
return 0;
|
||||
}
|
||||
return BLE_UUID16(uuid)->value;
|
||||
}
|
||||
|
||||
static bool adv_has_uuid16(const struct ble_hs_adv_fields *fields, uint16_t target_uuid) {
|
||||
if (fields == NULL || fields->uuids16 == NULL || fields->num_uuids16 == 0) {
|
||||
return false;
|
||||
}
|
||||
for (uint8_t i = 0; i < fields->num_uuids16; ++i) {
|
||||
if (fields->uuids16[i].value == target_uuid) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static size_t normalize_name(const char *in, char *out, size_t out_cap) {
|
||||
if (in == NULL || out == NULL || out_cap == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char *start = in;
|
||||
while (*start != '\0' && isspace((unsigned char)*start)) {
|
||||
++start;
|
||||
}
|
||||
if (*start == '"' || *start == '\'') {
|
||||
++start;
|
||||
}
|
||||
|
||||
size_t len = strlen(start);
|
||||
while (len > 0 && isspace((unsigned char)start[len - 1])) {
|
||||
--len;
|
||||
}
|
||||
if (len > 0 && (start[len - 1] == '"' || start[len - 1] == '\'')) {
|
||||
--len;
|
||||
}
|
||||
|
||||
size_t out_len = 0;
|
||||
for (size_t i = 0; i < len && out_len + 1 < out_cap; ++i) {
|
||||
unsigned char c = (unsigned char)start[i];
|
||||
if (isupper(c)) {
|
||||
c = (unsigned char)tolower(c);
|
||||
}
|
||||
out[out_len++] = (char)c;
|
||||
}
|
||||
out[out_len] = '\0';
|
||||
return out_len;
|
||||
}
|
||||
|
||||
static bool normalized_name_match(const char *adv_name, const char *target_name) {
|
||||
char adv[40] = {0};
|
||||
char target[40] = {0};
|
||||
size_t adv_len = normalize_name(adv_name, adv, sizeof(adv));
|
||||
size_t target_len = normalize_name(target_name, target, sizeof(target));
|
||||
if (adv_len == 0 || target_len == 0) {
|
||||
return false;
|
||||
}
|
||||
if (strcmp(adv, target) == 0) {
|
||||
return true;
|
||||
}
|
||||
// Be tolerant of shortened or prefixed names.
|
||||
return (strstr(adv, target) != NULL) || (strstr(target, adv) != NULL);
|
||||
}
|
||||
|
||||
static bool is_known_printer_name(const char *name) {
|
||||
return normalized_name_match(name, PRINTER_NAME_PRIMARY) ||
|
||||
normalized_name_match(name, PRINTER_NAME_LEGACY);
|
||||
}
|
||||
|
||||
static bool target_name_match_with_alias(const char *adv_name, const char *target_name) {
|
||||
if (normalized_name_match(adv_name, target_name)) {
|
||||
return true;
|
||||
}
|
||||
if (!is_known_printer_name(target_name)) {
|
||||
return false;
|
||||
}
|
||||
return is_known_printer_name(adv_name);
|
||||
}
|
||||
|
||||
static void reset_discovery_state(void) {
|
||||
s_service_start = 0;
|
||||
s_service_end = 0;
|
||||
s_notify_val_handle = 0;
|
||||
s_write_val_handle = 0;
|
||||
s_cccd_handle = 0;
|
||||
s_notify_ready = false;
|
||||
s_mtu = 23;
|
||||
}
|
||||
|
||||
static void signal_failure(void) {
|
||||
xEventGroupSetBits(s_evt_group, EVT_FAILED);
|
||||
}
|
||||
|
||||
static void signal_ready(void) {
|
||||
xEventGroupSetBits(s_evt_group, EVT_READY);
|
||||
}
|
||||
|
||||
static int dsc_disc_cb(uint16_t conn_handle,
|
||||
const struct ble_gatt_error *error,
|
||||
uint16_t chr_val_handle,
|
||||
const struct ble_gatt_dsc *dsc,
|
||||
void *arg);
|
||||
|
||||
static int chr_disc_cb(uint16_t conn_handle,
|
||||
const struct ble_gatt_error *error,
|
||||
const struct ble_gatt_chr *chr,
|
||||
void *arg);
|
||||
|
||||
static int svc_disc_cb(uint16_t conn_handle,
|
||||
const struct ble_gatt_error *error,
|
||||
const struct ble_gatt_svc *service,
|
||||
void *arg);
|
||||
|
||||
static int cccd_write_cb(uint16_t conn_handle,
|
||||
const struct ble_gatt_error *error,
|
||||
struct ble_gatt_attr *attr,
|
||||
void *arg) {
|
||||
(void)conn_handle;
|
||||
(void)attr;
|
||||
(void)arg;
|
||||
|
||||
if (error->status != 0) {
|
||||
ESP_LOGE(TAG, "CCCD write failed status=%d", error->status);
|
||||
signal_failure();
|
||||
return 0;
|
||||
}
|
||||
|
||||
s_notify_ready = true;
|
||||
ESP_LOGI(TAG, "Notify subscription enabled");
|
||||
signal_ready();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int dsc_disc_cb(uint16_t conn_handle,
|
||||
const struct ble_gatt_error *error,
|
||||
uint16_t chr_val_handle,
|
||||
const struct ble_gatt_dsc *dsc,
|
||||
void *arg) {
|
||||
(void)chr_val_handle;
|
||||
(void)arg;
|
||||
|
||||
if (error->status == 0 && dsc != NULL) {
|
||||
if (uuid16(&dsc->uuid.u) == CCCD_UUID16) {
|
||||
s_cccd_handle = dsc->handle;
|
||||
ESP_LOGI(TAG, "Found CCCD handle=%u", s_cccd_handle);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (error->status == BLE_HS_EDONE) {
|
||||
if (s_cccd_handle == 0) {
|
||||
ESP_LOGW(TAG, "CCCD not found, fallback to val_handle+1");
|
||||
s_cccd_handle = s_notify_val_handle + 1;
|
||||
}
|
||||
|
||||
uint8_t cccd[2] = {0x01, 0x00};
|
||||
int rc = ble_gattc_write_flat(conn_handle, s_cccd_handle, cccd, sizeof(cccd), cccd_write_cb, NULL);
|
||||
if (rc != 0) {
|
||||
ESP_LOGE(TAG, "ble_gattc_write_flat CCCD failed rc=%d", rc);
|
||||
signal_failure();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
ESP_LOGE(TAG, "Descriptor discovery failed status=%d", error->status);
|
||||
signal_failure();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int chr_disc_cb(uint16_t conn_handle,
|
||||
const struct ble_gatt_error *error,
|
||||
const struct ble_gatt_chr *chr,
|
||||
void *arg) {
|
||||
(void)arg;
|
||||
|
||||
if (error->status == 0 && chr != NULL) {
|
||||
uint16_t id = uuid16(&chr->uuid.u);
|
||||
if (id == PRINTER_NOTIFY_UUID16) {
|
||||
s_notify_val_handle = chr->val_handle;
|
||||
ESP_LOGI(TAG, "Found notify char handle=%u", s_notify_val_handle);
|
||||
} else if (id == PRINTER_WRITE_UUID16) {
|
||||
s_write_val_handle = chr->val_handle;
|
||||
ESP_LOGI(TAG, "Found write char handle=%u", s_write_val_handle);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (error->status == BLE_HS_EDONE) {
|
||||
if (s_notify_val_handle == 0 || s_write_val_handle == 0) {
|
||||
ESP_LOGE(TAG,
|
||||
"Characteristic discovery incomplete notify=%u write=%u",
|
||||
s_notify_val_handle,
|
||||
s_write_val_handle);
|
||||
signal_failure();
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint16_t end_handle = (s_write_val_handle > s_notify_val_handle)
|
||||
? (uint16_t)(s_write_val_handle - 1)
|
||||
: s_service_end;
|
||||
|
||||
if (end_handle <= s_notify_val_handle) {
|
||||
end_handle = s_service_end;
|
||||
}
|
||||
|
||||
int rc = ble_gattc_disc_all_dscs(conn_handle,
|
||||
s_notify_val_handle,
|
||||
end_handle,
|
||||
dsc_disc_cb,
|
||||
NULL);
|
||||
if (rc != 0) {
|
||||
ESP_LOGE(TAG, "Descriptor discovery start failed rc=%d", rc);
|
||||
signal_failure();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
ESP_LOGE(TAG, "Characteristic discovery failed status=%d", error->status);
|
||||
signal_failure();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int svc_disc_cb(uint16_t conn_handle,
|
||||
const struct ble_gatt_error *error,
|
||||
const struct ble_gatt_svc *service,
|
||||
void *arg) {
|
||||
(void)arg;
|
||||
|
||||
if (error->status == 0 && service != NULL) {
|
||||
if (uuid16(&service->uuid.u) == PRINTER_SERVICE_UUID16) {
|
||||
s_service_start = service->start_handle;
|
||||
s_service_end = service->end_handle;
|
||||
ESP_LOGI(TAG, "Found printer service [%u, %u]", s_service_start, s_service_end);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (error->status == BLE_HS_EDONE) {
|
||||
uint16_t start = s_service_start;
|
||||
uint16_t end = s_service_end;
|
||||
if (start == 0 || end == 0) {
|
||||
// Android app searches all services/chars for FFF1/FFF2.
|
||||
// Use a global fallback range for broader compatibility.
|
||||
start = 1;
|
||||
end = 0xFFFF;
|
||||
ESP_LOGW(TAG, "Printer service 0x%04x not found; fallback char discovery in full handle range", PRINTER_SERVICE_UUID16);
|
||||
}
|
||||
|
||||
int rc = ble_gattc_disc_all_chrs(conn_handle,
|
||||
start,
|
||||
end,
|
||||
chr_disc_cb,
|
||||
NULL);
|
||||
if (rc != 0) {
|
||||
ESP_LOGE(TAG, "Start characteristic discovery failed rc=%d", rc);
|
||||
signal_failure();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
ESP_LOGE(TAG, "Service discovery failed status=%d", error->status);
|
||||
signal_failure();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void start_service_discovery(void) {
|
||||
int rc = ble_gattc_disc_all_svcs(s_conn_handle, svc_disc_cb, NULL);
|
||||
if (rc != 0) {
|
||||
ESP_LOGE(TAG, "ble_gattc_disc_all_svcs failed rc=%d", rc);
|
||||
signal_failure();
|
||||
}
|
||||
}
|
||||
|
||||
static void start_scan(void);
|
||||
|
||||
static void stop_scan_if_running(void) {
|
||||
if (s_scanning) {
|
||||
ble_gap_disc_cancel();
|
||||
s_scanning = false;
|
||||
}
|
||||
}
|
||||
|
||||
static int gap_event_cb(struct ble_gap_event *event, void *arg) {
|
||||
(void)arg;
|
||||
|
||||
switch (event->type) {
|
||||
case BLE_GAP_EVENT_DISC: {
|
||||
if (!s_scanning) {
|
||||
return 0;
|
||||
}
|
||||
struct ble_hs_adv_fields fields;
|
||||
memset(&fields, 0, sizeof(fields));
|
||||
|
||||
int rc = ble_hs_adv_parse_fields(&fields,
|
||||
event->disc.data,
|
||||
event->disc.length_data);
|
||||
if (rc != 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
char name[32] = {0};
|
||||
bool has_name = (fields.name_len > 0 && fields.name != NULL);
|
||||
if (has_name) {
|
||||
size_t n = fields.name_len < sizeof(name) - 1 ? fields.name_len : sizeof(name) - 1;
|
||||
memcpy(name, fields.name, n);
|
||||
}
|
||||
|
||||
if (s_match_any_compatible) {
|
||||
bool uuid_match = adv_has_uuid16(&fields, PRINTER_SERVICE_UUID16);
|
||||
bool name_match = has_name && is_known_printer_name(name);
|
||||
if (!uuid_match && !name_match) {
|
||||
return 0;
|
||||
}
|
||||
ESP_LOGI(TAG,
|
||||
"Found compatible device '%s' RSSI=%d (uuid_match=%d)",
|
||||
has_name ? name : "<no-name>",
|
||||
event->disc.rssi,
|
||||
uuid_match ? 1 : 0);
|
||||
} else {
|
||||
if (!has_name || !target_name_match_with_alias(name, s_target_name)) {
|
||||
return 0;
|
||||
}
|
||||
ESP_LOGI(TAG, "Found target device '%s' RSSI=%d", name, event->disc.rssi);
|
||||
}
|
||||
|
||||
s_target_addr = event->disc.addr;
|
||||
stop_scan_if_running();
|
||||
|
||||
struct ble_gap_conn_params conn_params;
|
||||
memset(&conn_params, 0, sizeof(conn_params));
|
||||
conn_params.scan_itvl = 0x0010;
|
||||
conn_params.scan_window = 0x0010;
|
||||
conn_params.itvl_min = 0x0018;
|
||||
conn_params.itvl_max = 0x0028;
|
||||
conn_params.latency = 0;
|
||||
conn_params.supervision_timeout = 0x0100;
|
||||
conn_params.min_ce_len = 0x0010;
|
||||
conn_params.max_ce_len = 0x0300;
|
||||
|
||||
rc = ble_gap_connect(s_addr_type,
|
||||
&s_target_addr,
|
||||
30000,
|
||||
&conn_params,
|
||||
gap_event_cb,
|
||||
NULL);
|
||||
if (rc != 0) {
|
||||
ESP_LOGE(TAG, "ble_gap_connect failed rc=%d", rc);
|
||||
signal_failure();
|
||||
} else {
|
||||
ESP_LOGI(TAG, "Connecting...");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
case BLE_GAP_EVENT_DISC_COMPLETE:
|
||||
s_scanning = false;
|
||||
return 0;
|
||||
|
||||
case BLE_GAP_EVENT_CONNECT:
|
||||
if (event->connect.status == 0) {
|
||||
s_conn_handle = event->connect.conn_handle;
|
||||
xEventGroupSetBits(s_evt_group, EVT_CONNECTED);
|
||||
ESP_LOGI(TAG, "Connected handle=%u", s_conn_handle);
|
||||
|
||||
ble_gattc_exchange_mtu(s_conn_handle, NULL, NULL);
|
||||
start_service_discovery();
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Connect failed status=%d", event->connect.status);
|
||||
signal_failure();
|
||||
}
|
||||
return 0;
|
||||
|
||||
case BLE_GAP_EVENT_DISCONNECT:
|
||||
ESP_LOGW(TAG, "Disconnected reason=%d", event->disconnect.reason);
|
||||
s_conn_handle = BLE_HS_CONN_HANDLE_NONE;
|
||||
s_notify_ready = false;
|
||||
reset_discovery_state();
|
||||
return 0;
|
||||
|
||||
case BLE_GAP_EVENT_MTU:
|
||||
s_mtu = event->mtu.value;
|
||||
ESP_LOGI(TAG, "MTU updated to %u", s_mtu);
|
||||
return 0;
|
||||
|
||||
case BLE_GAP_EVENT_NOTIFY_RX: {
|
||||
if (event->notify_rx.om == NULL || s_rx_cb == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint16_t data_len = OS_MBUF_PKTLEN(event->notify_rx.om);
|
||||
if (data_len == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t buf[256];
|
||||
uint16_t out_len = 0;
|
||||
if (data_len > sizeof(buf)) {
|
||||
ESP_LOGW(TAG, "Notify packet too large len=%u", data_len);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int rc = ble_hs_mbuf_to_flat(event->notify_rx.om, buf, sizeof(buf), &out_len);
|
||||
if (rc != 0) {
|
||||
ESP_LOGW(TAG, "ble_hs_mbuf_to_flat notify failed rc=%d", rc);
|
||||
return 0;
|
||||
}
|
||||
|
||||
s_rx_cb(buf, out_len);
|
||||
return 0;
|
||||
}
|
||||
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static void start_scan(void) {
|
||||
struct ble_gap_disc_params params;
|
||||
memset(¶ms, 0, sizeof(params));
|
||||
// Keep duplicates so we can receive scan response updates carrying full local name.
|
||||
params.filter_duplicates = 0;
|
||||
params.passive = 0;
|
||||
params.itvl = 0x0010;
|
||||
params.window = 0x0010;
|
||||
|
||||
stop_scan_if_running();
|
||||
|
||||
int rc = ble_gap_disc(s_addr_type, BLE_HS_FOREVER, ¶ms, gap_event_cb, NULL);
|
||||
if (rc != 0) {
|
||||
ESP_LOGE(TAG, "ble_gap_disc failed rc=%d", rc);
|
||||
signal_failure();
|
||||
return;
|
||||
}
|
||||
|
||||
s_scanning = true;
|
||||
if (s_match_any_compatible) {
|
||||
ESP_LOGI(TAG, "Scanning for compatible printer (service 0x%04x)", PRINTER_SERVICE_UUID16);
|
||||
} else {
|
||||
ESP_LOGI(TAG, "Scanning for %s", s_target_name);
|
||||
}
|
||||
}
|
||||
|
||||
static void ble_on_reset(int reason) {
|
||||
ESP_LOGE(TAG, "BLE reset reason=%d", reason);
|
||||
}
|
||||
|
||||
static void ble_on_sync(void) {
|
||||
int rc = ble_hs_id_infer_auto(0, &s_addr_type);
|
||||
if (rc != 0) {
|
||||
ESP_LOGE(TAG, "ble_hs_id_infer_auto failed rc=%d", rc);
|
||||
}
|
||||
s_host_synced = true;
|
||||
}
|
||||
|
||||
static void nimble_host_task(void *param) {
|
||||
(void)param;
|
||||
nimble_port_run();
|
||||
nimble_port_freertos_deinit();
|
||||
}
|
||||
|
||||
esp_err_t ble_printer_client_init(ble_frame_rx_cb_t rx_cb) {
|
||||
s_rx_cb = rx_cb;
|
||||
|
||||
if (s_initialized) {
|
||||
reset_discovery_state();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
// NimBLE emits very chatty INFO logs during each chunk write.
|
||||
// Lowering these logs reduces serial I/O overhead and improves runtime smoothness.
|
||||
esp_log_level_set("NimBLE", ESP_LOG_WARN);
|
||||
esp_log_level_set("BLE_INIT", ESP_LOG_WARN);
|
||||
|
||||
s_evt_group = xEventGroupCreate();
|
||||
if (s_evt_group == NULL) {
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
|
||||
s_lock = xSemaphoreCreateMutex();
|
||||
if (s_lock == NULL) {
|
||||
vEventGroupDelete(s_evt_group);
|
||||
s_evt_group = NULL;
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
|
||||
esp_err_t err = nimble_port_init();
|
||||
if (err != ESP_OK && err != ESP_ERR_INVALID_STATE) {
|
||||
return err;
|
||||
}
|
||||
ble_hs_cfg.reset_cb = ble_on_reset;
|
||||
ble_hs_cfg.sync_cb = ble_on_sync;
|
||||
|
||||
ble_svc_gap_init();
|
||||
ble_svc_gatt_init();
|
||||
|
||||
nimble_port_freertos_init(nimble_host_task);
|
||||
|
||||
reset_discovery_state();
|
||||
s_initialized = true;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t ble_printer_client_connect(const char *target_name, uint32_t timeout_ms) {
|
||||
if (!s_initialized || s_lock == NULL || s_evt_group == NULL) {
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
s_match_any_compatible = false;
|
||||
if (target_name != NULL && target_name[0] != '\0') {
|
||||
if (strcmp(target_name, "*") == 0) {
|
||||
s_match_any_compatible = true;
|
||||
} else {
|
||||
strlcpy(s_target_name, target_name, sizeof(s_target_name));
|
||||
}
|
||||
}
|
||||
|
||||
if (xSemaphoreTake(s_lock, pdMS_TO_TICKS(3000)) != pdTRUE) {
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
|
||||
if (s_conn_handle != BLE_HS_CONN_HANDLE_NONE && s_notify_ready) {
|
||||
xSemaphoreGive(s_lock);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
xEventGroupClearBits(s_evt_group, EVT_CONNECTED | EVT_READY | EVT_FAILED);
|
||||
reset_discovery_state();
|
||||
|
||||
int wait_sync_ms = 3000;
|
||||
while (!s_host_synced && wait_sync_ms > 0) {
|
||||
vTaskDelay(pdMS_TO_TICKS(20));
|
||||
wait_sync_ms -= 20;
|
||||
}
|
||||
if (!s_host_synced) {
|
||||
xSemaphoreGive(s_lock);
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
start_scan();
|
||||
xSemaphoreGive(s_lock);
|
||||
|
||||
EventBits_t bits = xEventGroupWaitBits(s_evt_group,
|
||||
EVT_READY | EVT_FAILED,
|
||||
pdFALSE,
|
||||
pdFALSE,
|
||||
pdMS_TO_TICKS(timeout_ms));
|
||||
|
||||
if (bits & EVT_READY) {
|
||||
return ESP_OK;
|
||||
}
|
||||
if (xSemaphoreTake(s_lock, pdMS_TO_TICKS(500)) == pdTRUE) {
|
||||
stop_scan_if_running();
|
||||
xSemaphoreGive(s_lock);
|
||||
}
|
||||
|
||||
// Similar to Android app behavior: quick retry when initial attempt fails or times out.
|
||||
if (xSemaphoreTake(s_lock, pdMS_TO_TICKS(1000)) == pdTRUE) {
|
||||
xEventGroupClearBits(s_evt_group, EVT_CONNECTED | EVT_READY | EVT_FAILED);
|
||||
reset_discovery_state();
|
||||
start_scan();
|
||||
xSemaphoreGive(s_lock);
|
||||
}
|
||||
|
||||
bits = xEventGroupWaitBits(s_evt_group,
|
||||
EVT_READY | EVT_FAILED,
|
||||
pdFALSE,
|
||||
pdFALSE,
|
||||
pdMS_TO_TICKS(timeout_ms));
|
||||
if (bits & EVT_READY) {
|
||||
return ESP_OK;
|
||||
}
|
||||
if (xSemaphoreTake(s_lock, pdMS_TO_TICKS(500)) == pdTRUE) {
|
||||
stop_scan_if_running();
|
||||
xSemaphoreGive(s_lock);
|
||||
}
|
||||
if (bits & EVT_FAILED) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
|
||||
void ble_printer_client_disconnect(void) {
|
||||
if (!s_initialized || s_lock == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (xSemaphoreTake(s_lock, pdMS_TO_TICKS(2000)) != pdTRUE) {
|
||||
return;
|
||||
}
|
||||
|
||||
stop_scan_if_running();
|
||||
|
||||
if (s_conn_handle != BLE_HS_CONN_HANDLE_NONE) {
|
||||
ble_gap_terminate(s_conn_handle, BLE_ERR_REM_USER_CONN_TERM);
|
||||
}
|
||||
|
||||
s_conn_handle = BLE_HS_CONN_HANDLE_NONE;
|
||||
reset_discovery_state();
|
||||
xSemaphoreGive(s_lock);
|
||||
}
|
||||
|
||||
bool ble_printer_client_is_connected(void) {
|
||||
if (!s_initialized) {
|
||||
return false;
|
||||
}
|
||||
return (s_conn_handle != BLE_HS_CONN_HANDLE_NONE) && s_notify_ready;
|
||||
}
|
||||
|
||||
void ble_printer_client_get_link_state(ble_link_state_t *out_state) {
|
||||
if (out_state == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
out_state->connected = (s_conn_handle != BLE_HS_CONN_HANDLE_NONE);
|
||||
out_state->notify_ready = s_notify_ready;
|
||||
out_state->mtu = s_mtu;
|
||||
}
|
||||
|
||||
esp_err_t ble_printer_client_write(const uint8_t *data, size_t len) {
|
||||
if (data == NULL || len == 0) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
if (!ble_printer_client_is_connected() || s_write_val_handle == 0) {
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
int rc = ble_gattc_write_no_rsp_flat(s_conn_handle,
|
||||
s_write_val_handle,
|
||||
data,
|
||||
len);
|
||||
if (rc != 0) {
|
||||
ESP_LOGW(TAG, "write_no_rsp failed rc=%d", rc);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
54
components/platform/src/platform_bootstrap.c
Normal file
54
components/platform/src/platform_bootstrap.c
Normal file
@@ -0,0 +1,54 @@
|
||||
#include "platform_bootstrap.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "esp_heap_caps.h"
|
||||
#include "esp_log.h"
|
||||
#include "mbedtls/platform.h"
|
||||
#include "nvs_flash.h"
|
||||
|
||||
static bool s_initialized;
|
||||
static const char *TAG = "platform_bootstrap";
|
||||
|
||||
static void *tls_calloc_prefer_psram(size_t n, size_t size) {
|
||||
void *ptr = heap_caps_calloc(n, size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
||||
if (ptr == NULL) {
|
||||
ptr = heap_caps_calloc(n, size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
|
||||
static void tls_free(void *ptr) {
|
||||
heap_caps_free(ptr);
|
||||
}
|
||||
|
||||
esp_err_t platform_bootstrap_init(void) {
|
||||
if (s_initialized) {
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t err = nvs_flash_init();
|
||||
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||
ESP_ERROR_CHECK(nvs_flash_erase());
|
||||
err = nvs_flash_init();
|
||||
}
|
||||
ESP_ERROR_CHECK(err);
|
||||
|
||||
#if CONFIG_SPIRAM_USE_MALLOC
|
||||
// Keep very small allocations on internal RAM, push larger generic mallocs to PSRAM.
|
||||
heap_caps_malloc_extmem_enable(4096);
|
||||
ESP_LOGI(TAG, "extmem malloc threshold set to 4096 bytes");
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_MEMORY)
|
||||
int tls_rc = mbedtls_platform_set_calloc_free(tls_calloc_prefer_psram, tls_free);
|
||||
if (tls_rc == 0) {
|
||||
ESP_LOGI(TAG, "mbedtls calloc/free redirected to PSRAM-preferred allocator");
|
||||
} else {
|
||||
ESP_LOGW(TAG, "mbedtls calloc/free redirect failed: rc=%d", tls_rc);
|
||||
}
|
||||
#endif
|
||||
|
||||
s_initialized = true;
|
||||
return ESP_OK;
|
||||
}
|
||||
136
components/platform/src/runtime_diagnostics.c
Normal file
136
components/platform/src/runtime_diagnostics.c
Normal file
@@ -0,0 +1,136 @@
|
||||
#include "runtime_diagnostics.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "esp_timer.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/semphr.h"
|
||||
|
||||
static SemaphoreHandle_t s_lock;
|
||||
static runtime_diag_snapshot_t s_snapshot;
|
||||
|
||||
static const char *const s_counter_names[RUNTIME_DIAG_COUNTER_MAX] = {
|
||||
"lifecycle_start_attempt",
|
||||
"lifecycle_start_success",
|
||||
"lifecycle_start_failed",
|
||||
"lifecycle_start_retry",
|
||||
"lifecycle_stop_attempt",
|
||||
"lifecycle_stop_success",
|
||||
"lifecycle_stop_failed",
|
||||
"wifi_connect_success",
|
||||
"wifi_connect_failed",
|
||||
"wifi_connect_timeout",
|
||||
"printer_job_submitted",
|
||||
"printer_job_success",
|
||||
"printer_job_failed",
|
||||
"printer_job_canceled",
|
||||
"image_generate_attempt",
|
||||
"image_generate_success",
|
||||
"image_generate_failed",
|
||||
"image_generate_timeout",
|
||||
"rest_responses_total",
|
||||
"rest_errors_total",
|
||||
};
|
||||
|
||||
static const char *const s_gauge_names[RUNTIME_DIAG_GAUGE_MAX] = {
|
||||
"lifecycle_state",
|
||||
"status_poll_pause_depth",
|
||||
"printer_queue_depth",
|
||||
};
|
||||
|
||||
static void runtime_diag_ensure_lock(void) {
|
||||
if (s_lock == NULL) {
|
||||
s_lock = xSemaphoreCreateMutex();
|
||||
}
|
||||
}
|
||||
|
||||
void runtime_diag_counter_add(runtime_diag_counter_t counter, uint32_t delta) {
|
||||
if (counter < 0 || counter >= RUNTIME_DIAG_COUNTER_MAX || delta == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
runtime_diag_ensure_lock();
|
||||
if (s_lock == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (xSemaphoreTake(s_lock, pdMS_TO_TICKS(50)) != pdTRUE) {
|
||||
return;
|
||||
}
|
||||
s_snapshot.counters[counter] += delta;
|
||||
xSemaphoreGive(s_lock);
|
||||
}
|
||||
|
||||
void runtime_diag_set_gauge(runtime_diag_gauge_t gauge, int32_t value) {
|
||||
if (gauge < 0 || gauge >= RUNTIME_DIAG_GAUGE_MAX) {
|
||||
return;
|
||||
}
|
||||
|
||||
runtime_diag_ensure_lock();
|
||||
if (s_lock == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (xSemaphoreTake(s_lock, pdMS_TO_TICKS(50)) != pdTRUE) {
|
||||
return;
|
||||
}
|
||||
s_snapshot.gauges[gauge] = value;
|
||||
xSemaphoreGive(s_lock);
|
||||
}
|
||||
|
||||
void runtime_diag_record_error(const char *source, esp_err_t code, const char *message) {
|
||||
runtime_diag_ensure_lock();
|
||||
if (s_lock == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (xSemaphoreTake(s_lock, pdMS_TO_TICKS(100)) != pdTRUE) {
|
||||
return;
|
||||
}
|
||||
|
||||
s_snapshot.last_error_ms = esp_timer_get_time() / 1000;
|
||||
s_snapshot.last_error_code = code;
|
||||
strlcpy(s_snapshot.last_error_source,
|
||||
source != NULL ? source : "unknown",
|
||||
sizeof(s_snapshot.last_error_source));
|
||||
strlcpy(s_snapshot.last_error_message,
|
||||
message != NULL ? message : "unknown",
|
||||
sizeof(s_snapshot.last_error_message));
|
||||
|
||||
xSemaphoreGive(s_lock);
|
||||
}
|
||||
|
||||
void runtime_diag_get_snapshot(runtime_diag_snapshot_t *out_snapshot) {
|
||||
if (out_snapshot == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
memset(out_snapshot, 0, sizeof(*out_snapshot));
|
||||
|
||||
runtime_diag_ensure_lock();
|
||||
if (s_lock == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (xSemaphoreTake(s_lock, pdMS_TO_TICKS(100)) != pdTRUE) {
|
||||
return;
|
||||
}
|
||||
|
||||
*out_snapshot = s_snapshot;
|
||||
xSemaphoreGive(s_lock);
|
||||
}
|
||||
|
||||
const char *runtime_diag_counter_name(runtime_diag_counter_t counter) {
|
||||
if (counter < 0 || counter >= RUNTIME_DIAG_COUNTER_MAX) {
|
||||
return "unknown";
|
||||
}
|
||||
return s_counter_names[counter];
|
||||
}
|
||||
|
||||
const char *runtime_diag_gauge_name(runtime_diag_gauge_t gauge) {
|
||||
if (gauge < 0 || gauge >= RUNTIME_DIAG_GAUGE_MAX) {
|
||||
return "unknown";
|
||||
}
|
||||
return s_gauge_names[gauge];
|
||||
}
|
||||
142
components/platform/src/runtime_policy.c
Normal file
142
components/platform/src/runtime_policy.c
Normal file
@@ -0,0 +1,142 @@
|
||||
#include "runtime_policy.h"
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#ifndef CONFIG_TQ_WIFI_CONNECT_TIMEOUT_MS
|
||||
#define CONFIG_TQ_WIFI_CONNECT_TIMEOUT_MS 15000
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_TQ_LIFECYCLE_START_RETRY_COUNT
|
||||
#define CONFIG_TQ_LIFECYCLE_START_RETRY_COUNT 2
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_TQ_LIFECYCLE_RETRY_BACKOFF_MS
|
||||
#define CONFIG_TQ_LIFECYCLE_RETRY_BACKOFF_MS 800
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_TQ_PRINTER_CONTROL_LOCK_TIMEOUT_MS
|
||||
#define CONFIG_TQ_PRINTER_CONTROL_LOCK_TIMEOUT_MS 1000
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_TQ_PRINTER_WORKER_QUEUE_WAIT_MS
|
||||
#define CONFIG_TQ_PRINTER_WORKER_QUEUE_WAIT_MS 250
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_TQ_PRINTER_QUEUE_RETRY_DELAY_MS
|
||||
#define CONFIG_TQ_PRINTER_QUEUE_RETRY_DELAY_MS 20
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_TQ_PRINTER_STATUS_POLL_INTERVAL_MS
|
||||
#define CONFIG_TQ_PRINTER_STATUS_POLL_INTERVAL_MS 5000
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_TQ_PRINTER_STOP_TIMEOUT_MS
|
||||
#define CONFIG_TQ_PRINTER_STOP_TIMEOUT_MS 8000
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_TQ_REST_PRINTER_CONNECT_TIMEOUT_MS
|
||||
#define CONFIG_TQ_REST_PRINTER_CONNECT_TIMEOUT_MS 15000
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_TQ_REST_LABEL_TIMEOUT_MS
|
||||
#define CONFIG_TQ_REST_LABEL_TIMEOUT_MS 5000
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_TQ_REST_OTA_STEP_TIMEOUT_MS
|
||||
#define CONFIG_TQ_REST_OTA_STEP_TIMEOUT_MS 5000
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_TQ_Z_IMAGE_TIMEOUT_MS
|
||||
#define CONFIG_TQ_Z_IMAGE_TIMEOUT_MS 45000
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_TQ_Z_IMAGE_DOWNLOAD_TIMEOUT_MS
|
||||
#define CONFIG_TQ_Z_IMAGE_DOWNLOAD_TIMEOUT_MS 15000
|
||||
#endif
|
||||
|
||||
static uint32_t clamp_u32(uint32_t value, uint32_t min_value, uint32_t max_value) {
|
||||
if (value < min_value) {
|
||||
return min_value;
|
||||
}
|
||||
if (value > max_value) {
|
||||
return max_value;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
uint32_t runtime_policy_wifi_connect_timeout_ms(void) {
|
||||
return clamp_u32(CONFIG_TQ_WIFI_CONNECT_TIMEOUT_MS, 3000, 60000);
|
||||
}
|
||||
|
||||
uint32_t runtime_policy_lifecycle_start_retry_count(void) {
|
||||
return clamp_u32(CONFIG_TQ_LIFECYCLE_START_RETRY_COUNT, 0, 5);
|
||||
}
|
||||
|
||||
uint32_t runtime_policy_lifecycle_retry_backoff_ms(uint32_t attempt) {
|
||||
uint32_t base = clamp_u32(CONFIG_TQ_LIFECYCLE_RETRY_BACKOFF_MS, 100, 10000);
|
||||
if (attempt > 8) {
|
||||
attempt = 8;
|
||||
}
|
||||
uint32_t factor = (1u << attempt);
|
||||
return clamp_u32(base * factor, 100, 30000);
|
||||
}
|
||||
|
||||
bool runtime_policy_is_retryable_error(esp_err_t err) {
|
||||
return err == ESP_ERR_TIMEOUT || err == ESP_FAIL || err == ESP_ERR_NO_MEM;
|
||||
}
|
||||
|
||||
uint32_t runtime_policy_printer_control_lock_timeout_ms(void) {
|
||||
return clamp_u32(CONFIG_TQ_PRINTER_CONTROL_LOCK_TIMEOUT_MS, 100, 5000);
|
||||
}
|
||||
|
||||
uint32_t runtime_policy_printer_worker_queue_wait_ms(void) {
|
||||
return clamp_u32(CONFIG_TQ_PRINTER_WORKER_QUEUE_WAIT_MS, 50, 2000);
|
||||
}
|
||||
|
||||
uint32_t runtime_policy_printer_queue_retry_delay_ms(void) {
|
||||
return clamp_u32(CONFIG_TQ_PRINTER_QUEUE_RETRY_DELAY_MS, 5, 500);
|
||||
}
|
||||
|
||||
uint32_t runtime_policy_printer_status_poll_interval_ms(void) {
|
||||
return clamp_u32(CONFIG_TQ_PRINTER_STATUS_POLL_INTERVAL_MS, 1000, 30000);
|
||||
}
|
||||
|
||||
uint32_t runtime_policy_printer_stop_timeout_ms(void) {
|
||||
return clamp_u32(CONFIG_TQ_PRINTER_STOP_TIMEOUT_MS, 1000, 30000);
|
||||
}
|
||||
|
||||
uint32_t runtime_policy_rest_printer_connect_timeout_ms(void) {
|
||||
return clamp_u32(CONFIG_TQ_REST_PRINTER_CONNECT_TIMEOUT_MS, 1000, 60000);
|
||||
}
|
||||
|
||||
uint32_t runtime_policy_rest_label_timeout_ms(void) {
|
||||
return clamp_u32(CONFIG_TQ_REST_LABEL_TIMEOUT_MS, 500, 30000);
|
||||
}
|
||||
|
||||
uint32_t runtime_policy_rest_ota_timeout_ms(void) {
|
||||
return clamp_u32(CONFIG_TQ_REST_OTA_STEP_TIMEOUT_MS, 500, 60000);
|
||||
}
|
||||
|
||||
uint32_t runtime_policy_image_generation_timeout_default_ms(void) {
|
||||
return clamp_u32(CONFIG_TQ_Z_IMAGE_TIMEOUT_MS, 5000, 180000);
|
||||
}
|
||||
|
||||
uint32_t runtime_policy_image_generation_timeout_min_ms(void) {
|
||||
return 5000;
|
||||
}
|
||||
|
||||
uint32_t runtime_policy_image_generation_timeout_max_ms(void) {
|
||||
return 180000;
|
||||
}
|
||||
|
||||
uint32_t runtime_policy_image_download_timeout_default_ms(void) {
|
||||
return clamp_u32(CONFIG_TQ_Z_IMAGE_DOWNLOAD_TIMEOUT_MS, 2000, 120000);
|
||||
}
|
||||
|
||||
uint32_t runtime_policy_image_download_timeout_min_ms(void) {
|
||||
return 2000;
|
||||
}
|
||||
|
||||
uint32_t runtime_policy_image_download_timeout_max_ms(void) {
|
||||
return 120000;
|
||||
}
|
||||
464
components/platform/src/voice_audio.c
Normal file
464
components/platform/src/voice_audio.c
Normal file
@@ -0,0 +1,464 @@
|
||||
#include "voice_audio.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "audio_codec_if.h"
|
||||
#include "driver/i2s_std.h"
|
||||
#include "esp_check.h"
|
||||
#include "esp_codec_dev.h"
|
||||
#include "esp_codec_dev_defaults.h"
|
||||
#include "esp_idf_version.h"
|
||||
#include "esp_log.h"
|
||||
#include "es8311_codec.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/semphr.h"
|
||||
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0) && !defined(CONFIG_CODEC_I2C_BACKWARD_COMPATIBLE)
|
||||
#include "driver/i2c_master.h"
|
||||
#define VOICE_USE_IDF_I2C_MASTER
|
||||
#else
|
||||
#include "driver/i2c.h"
|
||||
#endif
|
||||
|
||||
static const char *TAG = "voice_audio";
|
||||
static const uint32_t VOICE_CODEC_I2C_PROBE_TIMEOUT_MS = 50;
|
||||
|
||||
#define ES8311_CODEC_ALT_ADDR ((uint8_t)(ES8311_CODEC_DEFAULT_ADDR + 0x02))
|
||||
|
||||
#ifdef CONFIG_TQ_VOICE_CODEC_PA_PIN_REVERSED
|
||||
#define VOICE_CODEC_PA_REVERSED true
|
||||
#else
|
||||
#define VOICE_CODEC_PA_REVERSED false
|
||||
#endif
|
||||
|
||||
static SemaphoreHandle_t s_lock;
|
||||
|
||||
#ifdef VOICE_USE_IDF_I2C_MASTER
|
||||
static i2c_master_bus_handle_t s_i2c_bus;
|
||||
#endif
|
||||
|
||||
static i2s_chan_handle_t s_i2s_tx;
|
||||
static i2s_chan_handle_t s_i2s_rx;
|
||||
|
||||
static const audio_codec_ctrl_if_t *s_codec_ctrl_if;
|
||||
static const audio_codec_gpio_if_t *s_codec_gpio_if;
|
||||
static const audio_codec_data_if_t *s_codec_data_if;
|
||||
static const audio_codec_if_t *s_codec_if;
|
||||
static esp_codec_dev_handle_t s_codec_dev;
|
||||
|
||||
static bool s_initialized;
|
||||
static bool s_opened;
|
||||
static uint32_t s_sample_rate;
|
||||
static uint8_t s_channels;
|
||||
static uint8_t s_bits_per_sample;
|
||||
|
||||
static void fill_err(char *err, size_t err_len, const char *msg) {
|
||||
if (err != NULL && err_len > 0) {
|
||||
snprintf(err, err_len, "%s", msg != NULL ? msg : "unknown error");
|
||||
}
|
||||
}
|
||||
|
||||
static bool voice_audio_i2c_probe_addr(uint8_t addr_8bit) {
|
||||
#ifdef VOICE_USE_IDF_I2C_MASTER
|
||||
if (s_i2c_bus == NULL || addr_8bit == 0) {
|
||||
return false;
|
||||
}
|
||||
uint16_t addr_7bit = (uint16_t)(addr_8bit >> 1);
|
||||
return i2c_master_probe(s_i2c_bus, addr_7bit, (int)VOICE_CODEC_I2C_PROBE_TIMEOUT_MS) == ESP_OK;
|
||||
#else
|
||||
if (addr_8bit == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
|
||||
if (cmd == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
i2c_master_start(cmd);
|
||||
i2c_master_write_byte(cmd, addr_8bit, true);
|
||||
i2c_master_stop(cmd);
|
||||
|
||||
esp_err_t err = i2c_master_cmd_begin(CONFIG_TQ_VOICE_I2C_PORT,
|
||||
cmd,
|
||||
pdMS_TO_TICKS(VOICE_CODEC_I2C_PROBE_TIMEOUT_MS));
|
||||
i2c_cmd_link_delete(cmd);
|
||||
return err == ESP_OK;
|
||||
#endif
|
||||
}
|
||||
|
||||
static uint8_t voice_audio_resolve_codec_addr(uint8_t preferred_addr) {
|
||||
uint8_t candidates[3] = {preferred_addr, ES8311_CODEC_DEFAULT_ADDR, ES8311_CODEC_ALT_ADDR};
|
||||
for (size_t i = 0; i < 3; ++i) {
|
||||
uint8_t addr = candidates[i];
|
||||
bool duplicate = false;
|
||||
for (size_t j = 0; j < i; ++j) {
|
||||
if (candidates[j] == addr) {
|
||||
duplicate = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (duplicate) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (voice_audio_i2c_probe_addr(addr)) {
|
||||
return addr;
|
||||
}
|
||||
}
|
||||
|
||||
ESP_LOGW(TAG,
|
||||
"ES8311 not found on I2C(port=%d,sda=%d,scl=%d), tried addrs:0x%02x/0x%02x/0x%02x",
|
||||
CONFIG_TQ_VOICE_I2C_PORT,
|
||||
CONFIG_TQ_VOICE_I2C_SDA_PIN,
|
||||
CONFIG_TQ_VOICE_I2C_SCL_PIN,
|
||||
(unsigned)preferred_addr,
|
||||
(unsigned)ES8311_CODEC_DEFAULT_ADDR,
|
||||
(unsigned)ES8311_CODEC_ALT_ADDR);
|
||||
return preferred_addr;
|
||||
}
|
||||
|
||||
static esp_err_t voice_audio_init_i2c(void) {
|
||||
#ifdef VOICE_USE_IDF_I2C_MASTER
|
||||
if (s_i2c_bus != NULL) {
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
i2c_master_bus_config_t bus_cfg = {
|
||||
.clk_source = I2C_CLK_SRC_DEFAULT,
|
||||
.i2c_port = CONFIG_TQ_VOICE_I2C_PORT,
|
||||
.sda_io_num = CONFIG_TQ_VOICE_I2C_SDA_PIN,
|
||||
.scl_io_num = CONFIG_TQ_VOICE_I2C_SCL_PIN,
|
||||
.glitch_ignore_cnt = 7,
|
||||
.flags.enable_internal_pullup = true,
|
||||
};
|
||||
|
||||
esp_err_t err = i2c_new_master_bus(&bus_cfg, &s_i2c_bus);
|
||||
if (err == ESP_OK) {
|
||||
ESP_LOGI(TAG,
|
||||
"voice i2c ready: port=%d sda=%d scl=%d",
|
||||
CONFIG_TQ_VOICE_I2C_PORT,
|
||||
CONFIG_TQ_VOICE_I2C_SDA_PIN,
|
||||
CONFIG_TQ_VOICE_I2C_SCL_PIN);
|
||||
}
|
||||
return err;
|
||||
#else
|
||||
i2c_config_t i2c_cfg = {
|
||||
.mode = I2C_MODE_MASTER,
|
||||
.sda_io_num = CONFIG_TQ_VOICE_I2C_SDA_PIN,
|
||||
.scl_io_num = CONFIG_TQ_VOICE_I2C_SCL_PIN,
|
||||
.sda_pullup_en = GPIO_PULLUP_ENABLE,
|
||||
.scl_pullup_en = GPIO_PULLUP_ENABLE,
|
||||
.master.clk_speed = 100000,
|
||||
};
|
||||
|
||||
ESP_RETURN_ON_ERROR(i2c_param_config(CONFIG_TQ_VOICE_I2C_PORT, &i2c_cfg), TAG, "i2c_param_config failed");
|
||||
esp_err_t err = i2c_driver_install(CONFIG_TQ_VOICE_I2C_PORT, i2c_cfg.mode, 0, 0, 0);
|
||||
if (err == ESP_OK) {
|
||||
ESP_LOGI(TAG,
|
||||
"voice i2c ready: port=%d sda=%d scl=%d",
|
||||
CONFIG_TQ_VOICE_I2C_PORT,
|
||||
CONFIG_TQ_VOICE_I2C_SDA_PIN,
|
||||
CONFIG_TQ_VOICE_I2C_SCL_PIN);
|
||||
}
|
||||
return err;
|
||||
#endif
|
||||
}
|
||||
|
||||
static esp_err_t voice_audio_init_i2s(uint32_t sample_rate) {
|
||||
if (s_i2s_tx != NULL && s_i2s_rx != NULL) {
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG((i2s_port_t)CONFIG_TQ_VOICE_I2S_PORT,
|
||||
I2S_ROLE_MASTER);
|
||||
chan_cfg.dma_desc_num = 6;
|
||||
chan_cfg.dma_frame_num = 240;
|
||||
|
||||
ESP_RETURN_ON_ERROR(i2s_new_channel(&chan_cfg, &s_i2s_tx, &s_i2s_rx), TAG, "i2s_new_channel failed");
|
||||
|
||||
i2s_std_config_t std_cfg = {
|
||||
.clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(sample_rate),
|
||||
.slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_MONO),
|
||||
.gpio_cfg = {
|
||||
.mclk = CONFIG_TQ_VOICE_I2S_MCLK_PIN,
|
||||
.bclk = CONFIG_TQ_VOICE_I2S_BCLK_PIN,
|
||||
.ws = CONFIG_TQ_VOICE_I2S_WS_PIN,
|
||||
.dout = CONFIG_TQ_VOICE_I2S_DOUT_PIN,
|
||||
.din = CONFIG_TQ_VOICE_I2S_DIN_PIN,
|
||||
.invert_flags = {
|
||||
.mclk_inv = false,
|
||||
.bclk_inv = false,
|
||||
.ws_inv = false,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
esp_err_t err = i2s_channel_init_std_mode(s_i2s_tx, &std_cfg);
|
||||
if (err != ESP_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
err = i2s_channel_init_std_mode(s_i2s_rx, &std_cfg);
|
||||
if (err != ESP_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
ESP_RETURN_ON_ERROR(i2s_channel_enable(s_i2s_tx), TAG, "i2s tx enable failed");
|
||||
ESP_RETURN_ON_ERROR(i2s_channel_enable(s_i2s_rx), TAG, "i2s rx enable failed");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t voice_audio_build_codec_dev(void) {
|
||||
if (s_codec_dev != NULL) {
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
uint8_t codec_addr = voice_audio_resolve_codec_addr((uint8_t)CONFIG_TQ_VOICE_CODEC_ADDR);
|
||||
if (codec_addr != (uint8_t)CONFIG_TQ_VOICE_CODEC_ADDR) {
|
||||
ESP_LOGW(TAG,
|
||||
"override ES8311 I2C addr 0x%02x -> 0x%02x",
|
||||
(unsigned)CONFIG_TQ_VOICE_CODEC_ADDR,
|
||||
(unsigned)codec_addr);
|
||||
}
|
||||
ESP_LOGI(TAG,
|
||||
"voice codec cfg: i2c_port=%d addr=0x%02x i2s_port=%d mclk=%d bclk=%d ws=%d dout=%d din=%d",
|
||||
CONFIG_TQ_VOICE_I2C_PORT,
|
||||
(unsigned)codec_addr,
|
||||
CONFIG_TQ_VOICE_I2S_PORT,
|
||||
CONFIG_TQ_VOICE_I2S_MCLK_PIN,
|
||||
CONFIG_TQ_VOICE_I2S_BCLK_PIN,
|
||||
CONFIG_TQ_VOICE_I2S_WS_PIN,
|
||||
CONFIG_TQ_VOICE_I2S_DOUT_PIN,
|
||||
CONFIG_TQ_VOICE_I2S_DIN_PIN);
|
||||
|
||||
audio_codec_i2c_cfg_t i2c_cfg = {
|
||||
.port = (uint8_t)CONFIG_TQ_VOICE_I2C_PORT,
|
||||
.addr = codec_addr,
|
||||
#ifdef VOICE_USE_IDF_I2C_MASTER
|
||||
.bus_handle = s_i2c_bus,
|
||||
#endif
|
||||
};
|
||||
|
||||
s_codec_ctrl_if = audio_codec_new_i2c_ctrl(&i2c_cfg);
|
||||
if (s_codec_ctrl_if == NULL) {
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
|
||||
audio_codec_i2s_cfg_t i2s_cfg = {
|
||||
.port = (uint8_t)CONFIG_TQ_VOICE_I2S_PORT,
|
||||
.rx_handle = s_i2s_rx,
|
||||
.tx_handle = s_i2s_tx,
|
||||
};
|
||||
|
||||
s_codec_data_if = audio_codec_new_i2s_data(&i2s_cfg);
|
||||
if (s_codec_data_if == NULL) {
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
|
||||
s_codec_gpio_if = audio_codec_new_gpio();
|
||||
if (s_codec_gpio_if == NULL) {
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
|
||||
es8311_codec_cfg_t codec_cfg = {
|
||||
.ctrl_if = s_codec_ctrl_if,
|
||||
.gpio_if = s_codec_gpio_if,
|
||||
.codec_mode = ESP_CODEC_DEV_WORK_MODE_BOTH,
|
||||
.pa_pin = CONFIG_TQ_VOICE_CODEC_PA_PIN,
|
||||
.pa_reverted = VOICE_CODEC_PA_REVERSED,
|
||||
.master_mode = false,
|
||||
.use_mclk = (CONFIG_TQ_VOICE_I2S_MCLK_PIN >= 0),
|
||||
.digital_mic = false,
|
||||
.invert_mclk = false,
|
||||
.invert_sclk = false,
|
||||
};
|
||||
|
||||
s_codec_if = es8311_codec_new(&codec_cfg);
|
||||
if (s_codec_if == NULL) {
|
||||
ESP_LOGE(TAG, "es8311_codec_new failed, check I2C wiring/pull-up/address");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
esp_codec_dev_cfg_t dev_cfg = {
|
||||
.dev_type = ESP_CODEC_DEV_TYPE_IN_OUT,
|
||||
.codec_if = s_codec_if,
|
||||
.data_if = s_codec_data_if,
|
||||
};
|
||||
|
||||
s_codec_dev = esp_codec_dev_new(&dev_cfg);
|
||||
if (s_codec_dev == NULL) {
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t voice_audio_prepare(const voice_audio_open_config_t *cfg) {
|
||||
ESP_RETURN_ON_ERROR(voice_audio_init_i2c(), TAG, "i2c init failed");
|
||||
ESP_RETURN_ON_ERROR(voice_audio_init_i2s(cfg->sample_rate), TAG, "i2s init failed");
|
||||
ESP_RETURN_ON_ERROR(voice_audio_build_codec_dev(), TAG, "codec dev init failed");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t voice_audio_open(const voice_audio_open_config_t *cfg, char *err, size_t err_len) {
|
||||
if (cfg == NULL || cfg->sample_rate == 0 || cfg->channels == 0 || cfg->bits_per_sample != 16) {
|
||||
fill_err(err, err_len, "invalid voice audio config");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
if (s_lock == NULL) {
|
||||
s_lock = xSemaphoreCreateMutex();
|
||||
if (s_lock == NULL) {
|
||||
fill_err(err, err_len, "create mutex failed");
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
}
|
||||
|
||||
if (xSemaphoreTake(s_lock, pdMS_TO_TICKS(1000)) != pdTRUE) {
|
||||
fill_err(err, err_len, "voice audio lock timeout");
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
|
||||
if (s_opened) {
|
||||
xSemaphoreGive(s_lock);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t init_err = voice_audio_prepare(cfg);
|
||||
if (init_err != ESP_OK) {
|
||||
if (init_err == ESP_FAIL) {
|
||||
fill_err(err, err_len, "es8311 init failed, check i2c wiring/address");
|
||||
} else {
|
||||
fill_err(err, err_len, "voice audio peripheral init failed");
|
||||
}
|
||||
xSemaphoreGive(s_lock);
|
||||
return init_err;
|
||||
}
|
||||
|
||||
if (s_initialized && (s_sample_rate != cfg->sample_rate ||
|
||||
s_channels != cfg->channels ||
|
||||
s_bits_per_sample != cfg->bits_per_sample)) {
|
||||
fill_err(err, err_len, "voice audio format changed after init");
|
||||
xSemaphoreGive(s_lock);
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
esp_codec_dev_sample_info_t fs = {
|
||||
.sample_rate = cfg->sample_rate,
|
||||
.channel = cfg->channels,
|
||||
.bits_per_sample = cfg->bits_per_sample,
|
||||
};
|
||||
|
||||
int rc = esp_codec_dev_open(s_codec_dev, &fs);
|
||||
if (rc != ESP_CODEC_DEV_OK) {
|
||||
fill_err(err, err_len, "esp_codec_dev_open failed");
|
||||
xSemaphoreGive(s_lock);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
(void)esp_codec_dev_set_out_vol(s_codec_dev, cfg->output_volume);
|
||||
(void)esp_codec_dev_set_in_gain(s_codec_dev, cfg->input_gain_db);
|
||||
(void)esp_codec_dev_set_out_mute(s_codec_dev, false);
|
||||
(void)esp_codec_dev_set_in_mute(s_codec_dev, false);
|
||||
|
||||
s_sample_rate = cfg->sample_rate;
|
||||
s_channels = cfg->channels;
|
||||
s_bits_per_sample = cfg->bits_per_sample;
|
||||
s_initialized = true;
|
||||
s_opened = true;
|
||||
|
||||
ESP_LOGI(TAG,
|
||||
"voice audio opened: sr=%u ch=%u bits=%u",
|
||||
(unsigned)s_sample_rate,
|
||||
(unsigned)s_channels,
|
||||
(unsigned)s_bits_per_sample);
|
||||
|
||||
xSemaphoreGive(s_lock);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void voice_audio_close(void) {
|
||||
if (s_lock == NULL) {
|
||||
return;
|
||||
}
|
||||
if (xSemaphoreTake(s_lock, pdMS_TO_TICKS(1000)) != pdTRUE) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (s_opened) {
|
||||
(void)esp_codec_dev_set_in_mute(s_codec_dev, true);
|
||||
(void)esp_codec_dev_set_out_mute(s_codec_dev, true);
|
||||
(void)esp_codec_dev_close(s_codec_dev);
|
||||
s_opened = false;
|
||||
}
|
||||
|
||||
xSemaphoreGive(s_lock);
|
||||
}
|
||||
|
||||
bool voice_audio_is_open(void) {
|
||||
return s_opened;
|
||||
}
|
||||
|
||||
esp_err_t voice_audio_read_pcm(int16_t *pcm,
|
||||
size_t samples,
|
||||
uint32_t timeout_ms,
|
||||
char *err,
|
||||
size_t err_len) {
|
||||
if (pcm == NULL || samples == 0) {
|
||||
fill_err(err, err_len, "invalid read args");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
if (s_lock == NULL || xSemaphoreTake(s_lock, pdMS_TO_TICKS(timeout_ms)) != pdTRUE) {
|
||||
fill_err(err, err_len, "voice read lock timeout");
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
|
||||
if (!s_opened) {
|
||||
fill_err(err, err_len, "voice audio not opened");
|
||||
xSemaphoreGive(s_lock);
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
int rc = esp_codec_dev_read(s_codec_dev, pcm, (int)(samples * sizeof(int16_t)));
|
||||
xSemaphoreGive(s_lock);
|
||||
if (rc != ESP_CODEC_DEV_OK) {
|
||||
fill_err(err, err_len, "codec read failed");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t voice_audio_write_pcm(const int16_t *pcm,
|
||||
size_t samples,
|
||||
uint32_t timeout_ms,
|
||||
char *err,
|
||||
size_t err_len) {
|
||||
if (pcm == NULL || samples == 0) {
|
||||
fill_err(err, err_len, "invalid write args");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
if (s_lock == NULL || xSemaphoreTake(s_lock, pdMS_TO_TICKS(timeout_ms)) != pdTRUE) {
|
||||
fill_err(err, err_len, "voice write lock timeout");
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
|
||||
if (!s_opened) {
|
||||
fill_err(err, err_len, "voice audio not opened");
|
||||
xSemaphoreGive(s_lock);
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
int rc = esp_codec_dev_write(s_codec_dev, (void *)pcm, (int)(samples * sizeof(int16_t)));
|
||||
xSemaphoreGive(s_lock);
|
||||
if (rc != ESP_CODEC_DEV_OK) {
|
||||
fill_err(err, err_len, "codec write failed");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
317
components/platform/src/wifi_manager.c
Normal file
317
components/platform/src/wifi_manager.c
Normal file
@@ -0,0 +1,317 @@
|
||||
#include "wifi_manager.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_netif.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_wifi_default.h"
|
||||
#include "runtime_diagnostics.h"
|
||||
#include "runtime_policy.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/event_groups.h"
|
||||
|
||||
#define WIFI_CONNECTED_BIT BIT0
|
||||
#define WIFI_FAIL_BIT BIT1
|
||||
|
||||
static const char *TAG = "wifi_manager";
|
||||
|
||||
static EventGroupHandle_t s_wifi_event_group;
|
||||
static int s_retry_num;
|
||||
static bool s_ready;
|
||||
static bool s_started;
|
||||
static esp_netif_t *s_sta_netif;
|
||||
static esp_event_handler_instance_t s_wifi_event_inst;
|
||||
static esp_event_handler_instance_t s_ip_event_inst;
|
||||
|
||||
static void wifi_manager_apply_sta_throughput_profile(void) {
|
||||
const uint8_t protocol = WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N;
|
||||
esp_err_t proto_rc = esp_wifi_set_protocol(WIFI_IF_STA, protocol);
|
||||
if (proto_rc != ESP_OK) {
|
||||
ESP_LOGW(TAG, "failed to set 11bgn protocol mask, rc=0x%x", (unsigned)proto_rc);
|
||||
} else {
|
||||
ESP_LOGI(TAG, "Wi-Fi protocol mask set to 11b/11g/11n");
|
||||
}
|
||||
|
||||
esp_err_t bw_rc = esp_wifi_set_bandwidth(WIFI_IF_STA, WIFI_BW_HT40);
|
||||
if (bw_rc == ESP_OK) {
|
||||
ESP_LOGI(TAG, "Wi-Fi bandwidth set to HT40");
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_LOGW(TAG, "failed to set HT40, fallback to HT20, rc=0x%x", (unsigned)bw_rc);
|
||||
bw_rc = esp_wifi_set_bandwidth(WIFI_IF_STA, WIFI_BW_HT20);
|
||||
if (bw_rc != ESP_OK) {
|
||||
ESP_LOGW(TAG, "failed to set HT20 fallback, rc=0x%x", (unsigned)bw_rc);
|
||||
} else {
|
||||
ESP_LOGI(TAG, "Wi-Fi bandwidth set to HT20");
|
||||
}
|
||||
}
|
||||
|
||||
static void wifi_event_handler(void *arg,
|
||||
esp_event_base_t event_base,
|
||||
int32_t event_id,
|
||||
void *event_data) {
|
||||
(void)arg;
|
||||
(void)event_data;
|
||||
|
||||
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
|
||||
(void)esp_wifi_connect();
|
||||
return;
|
||||
}
|
||||
|
||||
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
||||
s_ready = false;
|
||||
if (s_retry_num < CONFIG_TQ_WIFI_MAXIMUM_RETRY) {
|
||||
(void)esp_wifi_connect();
|
||||
s_retry_num++;
|
||||
ESP_LOGW(TAG, "retry to connect to the AP (%d/%d)", s_retry_num, CONFIG_TQ_WIFI_MAXIMUM_RETRY);
|
||||
} else if (s_wifi_event_group != NULL) {
|
||||
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
||||
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
|
||||
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
|
||||
s_retry_num = 0;
|
||||
s_ready = true;
|
||||
if (s_wifi_event_group != NULL) {
|
||||
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static esp_err_t start_sta_mode(void) {
|
||||
wifi_config_t wifi_config = {0};
|
||||
if (strlen(CONFIG_TQ_WIFI_SSID) == 0) {
|
||||
ESP_LOGE(TAG, "CONFIG_TQ_WIFI_SSID is empty; STA-only mode requires valid SSID");
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
strlcpy((char *)wifi_config.sta.ssid, CONFIG_TQ_WIFI_SSID, sizeof(wifi_config.sta.ssid));
|
||||
strlcpy((char *)wifi_config.sta.password, CONFIG_TQ_WIFI_PASSWORD, sizeof(wifi_config.sta.password));
|
||||
wifi_config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
|
||||
wifi_config.sta.pmf_cfg.capable = true;
|
||||
wifi_config.sta.pmf_cfg.required = false;
|
||||
|
||||
esp_err_t rc = esp_wifi_set_mode(WIFI_MODE_STA);
|
||||
if (rc != ESP_OK) {
|
||||
return rc;
|
||||
}
|
||||
rc = esp_wifi_set_config(WIFI_IF_STA, &wifi_config);
|
||||
if (rc != ESP_OK) {
|
||||
return rc;
|
||||
}
|
||||
rc = esp_wifi_start();
|
||||
if (rc != ESP_OK && rc != ESP_ERR_INVALID_STATE) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "wifi_init_sta finished");
|
||||
|
||||
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
|
||||
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
|
||||
pdFALSE,
|
||||
pdFALSE,
|
||||
pdMS_TO_TICKS(runtime_policy_wifi_connect_timeout_ms()));
|
||||
|
||||
if (bits & WIFI_CONNECTED_BIT) {
|
||||
ESP_LOGI(TAG, "connected to AP SSID:%s", CONFIG_TQ_WIFI_SSID);
|
||||
wifi_manager_apply_sta_throughput_profile();
|
||||
esp_err_t ps_rc = esp_wifi_set_ps(WIFI_PS_NONE);
|
||||
if (ps_rc == ESP_OK) {
|
||||
ESP_LOGI(TAG, "Wi-Fi power save disabled (WIFI_PS_NONE)");
|
||||
} else {
|
||||
ESP_LOGW(TAG, "failed to set WIFI_PS_NONE, rc=0x%x", (unsigned)ps_rc);
|
||||
}
|
||||
s_ready = true;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
if (bits & WIFI_FAIL_BIT) {
|
||||
ESP_LOGW(TAG, "Failed to connect to SSID:%s", CONFIG_TQ_WIFI_SSID);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
ESP_LOGW(TAG, "Wi-Fi connect timeout");
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
|
||||
esp_err_t wifi_manager_start(void) {
|
||||
if (s_started) {
|
||||
return s_ready ? ESP_OK : ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
s_retry_num = 0;
|
||||
s_ready = false;
|
||||
|
||||
if (s_wifi_event_group == NULL) {
|
||||
s_wifi_event_group = xEventGroupCreate();
|
||||
if (s_wifi_event_group == NULL) {
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
}
|
||||
xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_BIT | WIFI_FAIL_BIT);
|
||||
|
||||
esp_err_t err = esp_netif_init();
|
||||
if (err != ESP_OK && err != ESP_ERR_INVALID_STATE) {
|
||||
runtime_diag_record_error("wifi_start", err, "esp_netif_init failed");
|
||||
return err;
|
||||
}
|
||||
|
||||
err = esp_event_loop_create_default();
|
||||
if (err != ESP_OK && err != ESP_ERR_INVALID_STATE) {
|
||||
runtime_diag_record_error("wifi_start", err, "event loop init failed");
|
||||
return err;
|
||||
}
|
||||
|
||||
if (s_sta_netif == NULL) {
|
||||
s_sta_netif = esp_netif_create_default_wifi_sta();
|
||||
if (s_sta_netif == NULL) {
|
||||
runtime_diag_record_error("wifi_start", ESP_ERR_NO_MEM, "create sta netif failed");
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
}
|
||||
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
err = esp_wifi_init(&cfg);
|
||||
if (err != ESP_OK && err != ESP_ERR_INVALID_STATE) {
|
||||
runtime_diag_record_error("wifi_start", err, "esp_wifi_init failed");
|
||||
return err;
|
||||
}
|
||||
|
||||
if (s_wifi_event_inst == NULL) {
|
||||
err = esp_event_handler_instance_register(WIFI_EVENT,
|
||||
ESP_EVENT_ANY_ID,
|
||||
&wifi_event_handler,
|
||||
NULL,
|
||||
&s_wifi_event_inst);
|
||||
if (err != ESP_OK) {
|
||||
runtime_diag_record_error("wifi_start", err, "register WIFI event failed");
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
if (s_ip_event_inst == NULL) {
|
||||
err = esp_event_handler_instance_register(IP_EVENT,
|
||||
IP_EVENT_STA_GOT_IP,
|
||||
&wifi_event_handler,
|
||||
NULL,
|
||||
&s_ip_event_inst);
|
||||
if (err != ESP_OK) {
|
||||
runtime_diag_record_error("wifi_start", err, "register IP event failed");
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
s_started = true;
|
||||
err = start_sta_mode();
|
||||
if (err == ESP_OK) {
|
||||
runtime_diag_counter_add(RUNTIME_DIAG_COUNTER_WIFI_CONNECT_SUCCESS, 1);
|
||||
} else {
|
||||
if (err == ESP_ERR_TIMEOUT) {
|
||||
runtime_diag_counter_add(RUNTIME_DIAG_COUNTER_WIFI_CONNECT_TIMEOUT, 1);
|
||||
} else {
|
||||
runtime_diag_counter_add(RUNTIME_DIAG_COUNTER_WIFI_CONNECT_FAILED, 1);
|
||||
}
|
||||
runtime_diag_record_error("wifi_connect", err, "STA connect failed");
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
esp_err_t wifi_manager_stop(void) {
|
||||
if (!s_started) {
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t first_err = ESP_OK;
|
||||
|
||||
s_ready = false;
|
||||
s_retry_num = 0;
|
||||
|
||||
if (s_wifi_event_inst != NULL) {
|
||||
esp_err_t err = esp_event_handler_instance_unregister(WIFI_EVENT,
|
||||
ESP_EVENT_ANY_ID,
|
||||
s_wifi_event_inst);
|
||||
if (err != ESP_OK && first_err == ESP_OK) {
|
||||
first_err = err;
|
||||
}
|
||||
s_wifi_event_inst = NULL;
|
||||
}
|
||||
|
||||
if (s_ip_event_inst != NULL) {
|
||||
esp_err_t err = esp_event_handler_instance_unregister(IP_EVENT,
|
||||
IP_EVENT_STA_GOT_IP,
|
||||
s_ip_event_inst);
|
||||
if (err != ESP_OK && first_err == ESP_OK) {
|
||||
first_err = err;
|
||||
}
|
||||
s_ip_event_inst = NULL;
|
||||
}
|
||||
|
||||
esp_err_t err = esp_wifi_disconnect();
|
||||
if (err != ESP_OK && err != ESP_ERR_WIFI_NOT_STARTED && err != ESP_ERR_WIFI_CONN) {
|
||||
if (first_err == ESP_OK) {
|
||||
first_err = err;
|
||||
}
|
||||
}
|
||||
|
||||
err = esp_wifi_stop();
|
||||
if (err != ESP_OK && err != ESP_ERR_WIFI_NOT_INIT && err != ESP_ERR_WIFI_NOT_STARTED) {
|
||||
if (first_err == ESP_OK) {
|
||||
first_err = err;
|
||||
}
|
||||
}
|
||||
|
||||
err = esp_wifi_deinit();
|
||||
if (err != ESP_OK && err != ESP_ERR_WIFI_NOT_INIT) {
|
||||
if (first_err == ESP_OK) {
|
||||
first_err = err;
|
||||
}
|
||||
}
|
||||
|
||||
if (s_sta_netif != NULL) {
|
||||
esp_netif_destroy_default_wifi(s_sta_netif);
|
||||
s_sta_netif = NULL;
|
||||
}
|
||||
|
||||
if (s_wifi_event_group != NULL) {
|
||||
vEventGroupDelete(s_wifi_event_group);
|
||||
s_wifi_event_group = NULL;
|
||||
}
|
||||
|
||||
s_started = false;
|
||||
|
||||
if (first_err != ESP_OK) {
|
||||
runtime_diag_record_error("wifi_stop", first_err, "wifi stop failed");
|
||||
}
|
||||
|
||||
return first_err;
|
||||
}
|
||||
|
||||
bool wifi_manager_is_ready(void) {
|
||||
return s_ready;
|
||||
}
|
||||
|
||||
void wifi_manager_get_ip(char *buf, size_t buf_len) {
|
||||
if (buf == NULL || buf_len == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
buf[0] = '\0';
|
||||
|
||||
esp_netif_ip_info_t ip_info;
|
||||
memset(&ip_info, 0, sizeof(ip_info));
|
||||
|
||||
if (s_sta_netif != NULL && esp_netif_get_ip_info(s_sta_netif, &ip_info) == ESP_OK) {
|
||||
snprintf(buf, buf_len, IPSTR, IP2STR(&ip_info.ip));
|
||||
return;
|
||||
}
|
||||
|
||||
strlcpy(buf, "0.0.0.0", buf_len);
|
||||
}
|
||||
Reference in New Issue
Block a user