/* * YM310 MQTT Driver - Header File - DEBUG VERSION * * Changes: * - Added memory alignment for buffer structures * - Added buffer size validation macros */ #ifndef __YM310_MQTT_H__ #define __YM310_MQTT_H__ #include #include #ifdef __cplusplus extern "C" { #endif /*============================================================================= * Configuration *=============================================================================*/ /* UART Configuration */ #define YM310_UART_NAME "uart2" #define YM310_UART_BAUDRATE 115200 #define RX_BUF_SIZE (2048+256) /* Thread Configuration */ #define INIT_THREAD_STACK_SIZE 2048 #define INIT_THREAD_PRIORITY 10 #define MQTT_THREAD_STACK_SIZE 4096 #define MQTT_THREAD_PRIORITY 18 #define MAINTAIN_THREAD_STACK_SIZE 2048 #define MAINTAIN_THREAD_PRIORITY 20 /* Timing Configuration */ #define YM310_AT_CMD_TIMEOUT_MS 10000 #define YM310_AT_SHORT_TIMEOUT_MS 3000 #define YM310_AT_LONG_TIMEOUT_MS 30000 #define MAINTAIN_INTERVAL_MS 120000 #define NTP_SYNC_INTERVAL_MS 600000 #define GPS_QUERY_INTERVAL_MS 12000 /* MQTT Configuration */ #define MQTT_INSTANCE_MAX 2 #define MQTT_MAX_HOST_LEN 64 #define MQTT_MAX_CLIENT_ID_LEN 32 #define MQTT_MAX_USERNAME_LEN 32 #define MQTT_MAX_PASSWORD_LEN 32 #define MQTT_MAX_TOPIC_LEN 64 #define MQTT_MAX_PAYLOAD_SIZE 1600 /* Reduced from 1400 for safety */ #define MAINTAIN_CHECK_INTERVAL_MS 120000 // 2 minutes /* Buffer size validation */ #define MQTT_RECV_BUF_TOTAL_SIZE (sizeof(rt_bool_t) + MQTT_MAX_TOPIC_LEN + MQTT_MAX_PAYLOAD_SIZE + sizeof(rt_uint16_t)) /*============================================================================= * Primary MQTT Configuration *=============================================================================*/ #define EF_PLATFORM_FLAG "platform" /* 0 = main platform, 1 = backup platform */ #define PLATFORM_VICE_MAGIC 0x9D /* Backup platform flag */ #define MQTT_PRIMARY_ENABLE 1 #define MQTT0CFG_MAIN_HOST "mqtt.hnyfwlw.com" #define MQTT0CFG_MAIN_PORT 61883 #define MQTT0CFG_MAIN_QOS 0 #define MQTT0CFG_MAIN_USR "user" #define MQTT0CFG_MAIN_PWD "f335bf402c655ee5fd2b5300905124e" #define MQTT0CFG_MAIN_TSUB "/yfkj/scd/sub/" #define MQTT0CFG_MAIN_TPUB "/yfkj/scd/pub/" #define MQTT0CFG_VICE_HOST "120.27.222.26" #define MQTT0CFG_VICE_PORT 1883 #define MQTT0CFG_VICE_QOS 0 #define MQTT0CFG_VICE_USR "device_001" #define MQTT0CFG_VICE_PWD "device_001" #define MQTT0CFG_VICE_TSUB "/yfkj/scd/sub/" #define MQTT0CFG_VICE_TPUB "/yfkj/scd/pub/" /*============================================================================= * Secondary MQTT Configuration *=============================================================================*/ #define MQTT_SECONDARY_ENABLE 1 #define MQTT_SECONDARY_HOST "120.27.222.26" #define MQTT_SECONDARY_PORT 1883 #define MQTT_SECONDARY_CLIENT_ID "device_002" #define MQTT_SECONDARY_USERNAME "device_002" #define MQTT_SECONDARY_PASSWORD "device_002" #define MQTT_SECONDARY_SUB_TOPIC "/yfkj/scd/sub/" #define MQTT_SECONDARY_PUB_TOPIC "/yfkj/scd/pub/" #define MQTT_SECONDARY_QOS 0 /*============================================================================= * AT Response Types *=============================================================================*/ typedef enum { AT_RESP_UNKNOWN = 0, AT_RESP_OK, AT_RESP_ERROR, AT_RESP_CME_ERROR, AT_RESP_CMS_ERROR, AT_RESP_ABORTED, AT_RESP_BUSY, AT_RESP_TIMEOUT, } at_resp_type_t; /*============================================================================= * AT Command Types *=============================================================================*/ typedef enum { AT_CMD_SYNC = 0, AT_CMD_ASYNC_URC, AT_CMD_ASYNC_DATA, } at_cmd_type_t; /*============================================================================= * MQTT State *=============================================================================*/ typedef enum { MQTT_STATE_DISCONNECTED = 0, MQTT_STATE_CONNECTING, MQTT_STATE_CONNECTED, MQTT_STATE_DISCONNECTING, MQTT_STATE_RECONNECTING, } mqtt_state_t; /*============================================================================= * MQTT Configuration Structure *=============================================================================*/ typedef struct { rt_uint8_t enabled; rt_uint8_t client_idx; char host[MQTT_MAX_HOST_LEN]; rt_uint16_t port; char client_id[MQTT_MAX_CLIENT_ID_LEN]; char username[MQTT_MAX_USERNAME_LEN]; char password[MQTT_MAX_PASSWORD_LEN]; char sub_topic[MQTT_MAX_TOPIC_LEN]; char pub_topic[MQTT_MAX_TOPIC_LEN]; rt_uint8_t qos; } mqtt_config_t; /*============================================================================= * MQTT Instance Structure *=============================================================================*/ typedef struct { mqtt_config_t config; mqtt_state_t state; rt_tick_t last_activity; rt_uint32_t tx_count; rt_uint32_t rx_count; rt_uint32_t err_count; void (*recv_callback)(rt_uint8_t client_idx, const char *topic, const char *payload, rt_uint16_t len); } mqtt_instance_t; /*============================================================================= * MQTT Receive Buffer Structure - ALIGNED *=============================================================================*/ typedef struct __attribute__((aligned(8))) { rt_bool_t data_ready; /* Flag: data waiting to be processed */ char topic[MQTT_MAX_TOPIC_LEN]; /* Received topic */ char payload[MQTT_MAX_PAYLOAD_SIZE]; /* Received payload */ rt_uint16_t payload_len; /* Payload length */ } mqtt_recv_buf_t; /*============================================================================= * MQTT Callback Function Type *=============================================================================*/ typedef void (*mqtt_recv_handler_t)(rt_uint8_t client_idx, const char *topic, const char *payload, rt_uint16_t len); /*============================================================================= * URC Handler Type *=============================================================================*/ typedef void (*urc_handler_t)(const char *urc); /*============================================================================= * Global Variables *=============================================================================*/ extern rt_device_t g_ym310_uart; extern rt_mutex_t g_mqtt_mutex; extern mqtt_instance_t g_mqtt_instances[MQTT_INSTANCE_MAX]; extern mqtt_recv_buf_t g_mqtt_recv_bufs[MQTT_INSTANCE_MAX]; extern rt_thread_t g_init_thread; extern rt_thread_t g_mqtt_thread; extern rt_thread_t g_maintain_thread; extern volatile rt_tick_t g_last_mqtt_success_tick; /*============================================================================= * AT Core API *=============================================================================*/ int ym310_at_core_init(void); void ym310_at_core_deinit(void); at_resp_type_t ym310_at_exec_cmd(const char *cmd, char *resp, rt_uint16_t resp_size, rt_int32_t timeout_ms); at_resp_type_t ym310_at_exec_cmd_with_retry(const char *cmd, char *resp, rt_uint16_t resp_size, rt_int32_t timeout_ms, rt_uint8_t retry_max); at_resp_type_t ym310_at_exec_async(const char *cmd, const char *urc_key, char *urc_buf, rt_uint16_t urc_size, rt_int32_t timeout_ms); at_resp_type_t ym310_at_exec_async_with_data(const char *cmd, const char *data, rt_uint16_t data_len, const char *urc_key, char *urc_buf, rt_uint16_t urc_size, rt_int32_t timeout_ms); int ym310_at_wait_for(const char *expected, char *buf, rt_uint16_t buf_size, rt_int32_t timeout_ms); int ym310_at_register_urc(const char *urc_prefix, urc_handler_t handler); void ym310_at_unregister_urc(const char *urc_prefix); int ym310_at_send_raw(const char *data, rt_uint16_t len); int ym310_at_send_line(const char *line); rt_err_t ym310_at_mutex_lock(rt_int32_t timeout_ms); void ym310_at_mutex_unlock(void); /*============================================================================= * Utility Functions *=============================================================================*/ rt_bool_t ym310_str_starts_with(const char *str, const char *prefix); int ym310_str_extract_int(const char *str, const char *prefix); int ym310_str_extract_nth_int(const char *str, int n); int ym310_parse_creg_response(const char *resp_buf, int *n, int *stat); int ym310_parse_cgreg_response(const char *resp_buf, int *n, int *stat); int ym310_parse_cgatt_response(const char *resp_buf, int *state); int ym310_parse_csq_response(const char *resp_buf, int *rssi, int *ber); int ym310_parse_qipact_response(const char *resp_buf, int *context_id, int *state, char *ip_addr, int ip_addr_size); /*============================================================================= * Module Init API *=============================================================================*/ rt_thread_t ym310_start_init_thread(void); void ym310_init_thread_stop(void); /*============================================================================= * MQTT API *=============================================================================*/ int ym310_mqtt_init(void); void ym310_mqtt_deinit(void); rt_thread_t ym310_start_mqtt_thread(void); mqtt_state_t ym310_mqtt_get_state(rt_uint8_t client_idx); rt_bool_t ym310_mqtt_is_connected(rt_uint8_t client_idx); int ym310_mqtt_set_recv_callback(rt_uint8_t client_idx, void (*callback)(rt_uint8_t, const char*, const char*, rt_uint16_t)); int ym310_mqtt_open_network(rt_uint8_t client_idx); int ym310_mqtt_close_network(rt_uint8_t client_idx); int ym310_mqtt_connect(rt_uint8_t client_idx); int ym310_mqtt_disconnect(rt_uint8_t client_idx); int ym310_mqtt_subscribe(rt_uint8_t client_idx, const char *topic, rt_uint8_t qos); int ym310_mqtt_unsubscribe(rt_uint8_t client_idx, const char *topic); void ym310_mqtt_force_reconnect(rt_uint8_t client_idx); void ym310_mqtt_force_reconnect_all(void); /*============================================================================= * MQTT Send API *=============================================================================*/ int ym310_mqtt_send_primary(const char *payload, rt_uint16_t len); int ym310_mqtt_send_secondary(const char *payload, rt_uint16_t len); int ym310_mqtt_send_to(rt_uint8_t client_idx, const char *topic, const char *payload, rt_uint16_t len, rt_uint8_t qos); /*============================================================================= * MQTT Stats API *=============================================================================*/ int ym310_mqtt_get_stats(rt_uint8_t client_idx, rt_uint32_t *tx_count, rt_uint32_t *rx_count, rt_uint32_t *err_count); int ym310_mqtt_reset_stats(rt_uint8_t client_idx); /*============================================================================= * MQTT Config API *=============================================================================*/ int ym310_mqtt_update_config(rt_uint8_t client_idx, mqtt_config_t *config); int ym310_mqtt_get_config(rt_uint8_t client_idx, mqtt_config_t *config); /*============================================================================= * Maintenance API *=============================================================================*/ rt_thread_t ym310_start_maintain_thread(void); void ym310_maintain_thread_stop(void); rt_bool_t ym310_check_network_connected(void); rt_bool_t ym310_check_mqtt_connected(rt_uint8_t client_idx); int ym310_module_reinit(void); int ym310_ntp_sync_time(void); int ym310_gps_init(void); int ym310_gps_get_info(void); void ym310_gps_deinit(void); #ifdef __cplusplus } #endif #endif /* __YM310_MQTT_H__ */