#include "mqtt_probe.h" #include "mqtt_packet/MQTTPacket.h" #include "xz_tls.h" #include #include #include #include #include #include #define TAG "xz_mqtt" #define PACKET_CAP 8192 static const char client_hello[] = "{\"type\":\"hello\",\"version\":3,\"transport\":\"udp\"," "\"audio_params\":{\"format\":\"opus\",\"sample_rate\":16000," "\"channels\":1,\"frame_duration\":60}}"; static char *string_field(const cJSON *obj, const char *key) { const cJSON *field = cJSON_GetObjectItemCaseSensitive(obj, key); return cJSON_IsString(field) ? field->valuestring : NULL; } static int endpoint_parts(const char *endpoint, char *host, size_t cap, unsigned *port) { if (!endpoint || !*endpoint) return -1; const char *end = NULL; const char *port_text = NULL; if (*endpoint == '[') { end = strchr(++endpoint, ']'); if (!end || (end[1] && end[1] != ':')) return -1; if (end[1]) port_text = end + 2; } else { end = strchr(endpoint, ':'); if (end) port_text = end + 1; else end = endpoint + strlen(endpoint); } size_t length = (size_t)(end - endpoint); if (length == 0 || length >= cap) return -1; for (size_t i = 0; i < length; ++i) { unsigned char c = (unsigned char)endpoint[i]; if (!(isalnum(c) || c == '.' || c == '-' || c == ':')) return -1; } memcpy(host, endpoint, length); host[length] = '\0'; *port = 8883; if (port_text) { if (!*port_text) return -1; unsigned value = 0; for (; *port_text; ++port_text) { if (*port_text < '0' || *port_text > '9') return -1; value = value * 10 + (*port_text - '0'); if (value > 65535) return -1; } if (!value) return -1; *port = value; } return 0; } /* The extra buffer byte provides a terminator for JSON without advancing the TLS stream. */ static int read_packet(xz_tls_t *tls, uint8_t *header, uint8_t *body) { if (xz_tls_read_exact(tls, header, 1) < 0) return -1; unsigned length = 0; for (int i = 0; i < 4; ++i) { uint8_t digit; if (xz_tls_read_exact(tls, &digit, 1) < 0) return -1; length |= ((unsigned)digit & 127u) << (7 * i); if (length > PACKET_CAP) return -1; if (!(digit & 128u)) { if (i && !digit) return -1; if (length && xz_tls_read_exact(tls, body, length) < 0) return -1; body[length] = 0; return (int)length; } } return -1; } static int publish(xz_tls_t *tls, uint8_t *packet, char *topic, const char *json) { MQTTString dest = MQTTString_initializer; dest.cstring = topic; int size = MQTTSerialize_publish(packet, PACKET_CAP, 0, 0, 0, 0, dest, (unsigned char *)json, (int)strlen(json)); return size > 0 ? xz_tls_write_all(tls, packet, (size_t)size) : -1; } static int valid_key(const char *text) { if (!text || strlen(text) != 32) return 0; for (size_t i = 0; i < 32; ++i) { if (!isxdigit((unsigned char)text[i])) return 0; } return 1; } /* 1 means the server returned a usable hello, 0 means an unrelated message. */ static int finish_hello(xz_tls_t *tls, uint8_t *packet, char *topic, const cJSON *reply) { const char *type = string_field(reply, "type"); if (!type || strcmp(type, "hello") != 0) return 0; const char *transport = string_field(reply, "transport"); const char *session_id = string_field(reply, "session_id"); const cJSON *udp = cJSON_GetObjectItemCaseSensitive(reply, "udp"); const char *server = string_field(udp, "server"); const cJSON *port = cJSON_GetObjectItemCaseSensitive(udp, "port"); if (!transport || strcmp(transport, "udp") || !session_id || !*session_id || !server || !*server || !cJSON_IsNumber(port) || port->valuedouble != port->valueint || port->valueint < 1 || port->valueint > 65535 || !valid_key(string_field(udp, "key")) || !valid_key(string_field(udp, "nonce"))) { BK_LOGE(TAG, "server hello has invalid session or UDP parameters\r\n"); return -1; } /* MQTT carries signaling. End this probe without sending audio to the UDP endpoint. */ cJSON *goodbye = cJSON_CreateObject(); char *json = NULL; if (goodbye && cJSON_AddStringToObject(goodbye, "type", "goodbye") && cJSON_AddStringToObject(goodbye, "session_id", session_id)) { json = cJSON_PrintUnformatted(goodbye); } int rc = json ? publish(tls, packet, topic, json) : -1; cJSON_free(json); cJSON_Delete(goodbye); return rc == 0 ? 1 : -1; } static int wait_hello(xz_tls_t *tls, uint8_t *packet, char *topic) { xz_tls_start_deadline(tls, 10000); for (;;) { uint8_t header; int size = read_packet(tls, &header, packet); if (size < 0) return -1; if (header == 0xd0 && size == 0) continue; /* PINGRESP */ if ((header >> 4) != PUBLISH || size < 2) return -1; unsigned qos = (header >> 1) & 3; size_t topic_len = ((size_t)packet[0] << 8) | packet[1]; size_t offset = 2 + topic_len; if (qos > 1 || !topic_len || offset > (size_t)size) return -1; if (qos == 1) { if (offset + 2 > (size_t)size) return -1; uint8_t ack[] = {0x40, 2, packet[offset], packet[offset + 1]}; if (!(ack[2] || ack[3]) || xz_tls_write_all(tls, ack, sizeof(ack)) < 0) return -1; offset += 2; } if (header & 1) continue; /* A retained reply cannot confirm this new hello. */ size_t payload_len = (size_t)size - offset; if (!payload_len || memchr(packet + offset, 0, payload_len)) continue; cJSON *reply = cJSON_ParseWithLengthOpts((const char *)packet + offset, payload_len + 1, NULL, 1); if (!reply) continue; int rc = finish_hello(tls, packet, topic, reply); cJSON_Delete(reply); if (rc) return rc == 1 ? 0 : -1; } } static int exchange(xz_tls_t *tls, uint8_t *packet, const cJSON *mqtt, char *topic) { MQTTPacket_connectData options = MQTTPacket_connectData_initializer; options.clientID.cstring = string_field(mqtt, "client_id"); options.username.cstring = string_field(mqtt, "username"); options.password.cstring = string_field(mqtt, "password"); if (!options.clientID.cstring || !*options.clientID.cstring || !options.username.cstring || !options.password.cstring) return -1; if (strlen(options.clientID.cstring) + strlen(options.username.cstring) + strlen(options.password.cstring) >= PACKET_CAP) return -1; const cJSON *keepalive = cJSON_GetObjectItemCaseSensitive(mqtt, "keepalive"); options.keepAliveInterval = 240; if (cJSON_IsNumber(keepalive) && keepalive->valueint >= 0 && keepalive->valueint <= 65535) { options.keepAliveInterval = (unsigned short)keepalive->valueint; } int size = MQTTSerialize_connect(packet, PACKET_CAP, &options); if (size <= 0 || xz_tls_write_all(tls, packet, (size_t)size) < 0) return -1; xz_tls_start_deadline(tls, 10000); uint8_t header; if (read_packet(tls, &header, packet) != 2 || header != 0x20 || packet[0] != 0) return -1; if (packet[1]) { BK_LOGE(TAG, "broker refused MQTT CONNECT (code %u)\r\n", (unsigned)packet[1]); return -1; } BK_LOGI(TAG, "MQTT CONNACK accepted\r\n"); /* The XiaoZhi broker routes downlink messages to the authenticated client. */ int rc = publish(tls, packet, topic, client_hello); if (rc == 0) rc = wait_hello(tls, packet, topic); size = MQTTSerialize_disconnect(packet, PACKET_CAP); if (size <= 0 || xz_tls_write_all(tls, packet, (size_t)size) < 0) return -1; return rc; } bk_err_t xiaozhi_mqtt_probe(const cJSON *mqtt) { char host[254]; unsigned port; char *topic = string_field(mqtt, "publish_topic"); if (!topic || !*topic || strlen(topic) >= PACKET_CAP || strpbrk(topic, "+#") || endpoint_parts(string_field(mqtt, "endpoint"), host, sizeof(host), &port) != 0) { BK_LOGE(TAG, "invalid MQTT endpoint or publish topic in OTA reply\r\n"); return BK_FAIL; } uint8_t *packet = os_malloc(PACKET_CAP + 1); if (!packet) return BK_ERR_NO_MEM; xz_tls_t *tls = NULL; int rc = xz_tls_open(host, port, &tls); if (rc == 0) { rc = exchange(tls, packet, mqtt, topic); xz_tls_close(tls); } os_free(packet); if (rc != 0) { BK_LOGE(TAG, "MQTT handshake failed\r\n"); return BK_FAIL; } BK_LOGI(TAG, "MQTT server hello verified, signaling session closed\r\n"); return BK_OK; }