diff --git a/main/idf_component.yml b/main/idf_component.yml index 15aa30b3..c967007b 100644 --- a/main/idf_component.yml +++ b/main/idf_component.yml @@ -11,7 +11,7 @@ dependencies: 78/esp_lcd_nv3023: ~1.0.0 78/esp-wifi-connect: ~2.4.2 78/esp-opus-encoder: ~2.3.2 - 78/esp-ml307: ~2.0.0 + 78/esp-ml307: ~2.0.1 78/xiaozhi-fonts: ~1.3.2 espressif/led_strip: ^2.5.5 espressif/esp_codec_dev: ~1.3.2 diff --git a/main/ota.cc b/main/ota.cc index cca90711..f5005d7f 100644 --- a/main/ota.cc +++ b/main/ota.cc @@ -110,34 +110,34 @@ bool Ota::CheckVersion() { has_activation_code_ = false; has_activation_challenge_ = false; cJSON *activation = cJSON_GetObjectItem(root, "activation"); - if (activation != NULL) { + if (cJSON_IsObject(activation)) { cJSON* message = cJSON_GetObjectItem(activation, "message"); - if (message != NULL) { + if (cJSON_IsString(message)) { activation_message_ = message->valuestring; } cJSON* code = cJSON_GetObjectItem(activation, "code"); - if (code != NULL) { + if (cJSON_IsString(code)) { activation_code_ = code->valuestring; has_activation_code_ = true; } cJSON* challenge = cJSON_GetObjectItem(activation, "challenge"); - if (challenge != NULL) { + if (cJSON_IsString(challenge)) { activation_challenge_ = challenge->valuestring; has_activation_challenge_ = true; } cJSON* timeout_ms = cJSON_GetObjectItem(activation, "timeout_ms"); - if (timeout_ms != NULL) { + if (cJSON_IsNumber(timeout_ms)) { activation_timeout_ms_ = timeout_ms->valueint; } } has_mqtt_config_ = false; cJSON *mqtt = cJSON_GetObjectItem(root, "mqtt"); - if (mqtt != NULL) { + if (cJSON_IsObject(mqtt)) { Settings settings("mqtt", true); cJSON *item = NULL; cJSON_ArrayForEach(item, mqtt) { - if (item->type == cJSON_String) { + if (cJSON_IsString(item)) { if (settings.GetString(item->string) != item->valuestring) { settings.SetString(item->string, item->valuestring); } @@ -150,13 +150,13 @@ bool Ota::CheckVersion() { has_websocket_config_ = false; cJSON *websocket = cJSON_GetObjectItem(root, "websocket"); - if (websocket != NULL) { + if (cJSON_IsObject(websocket)) { Settings settings("websocket", true); cJSON *item = NULL; cJSON_ArrayForEach(item, websocket) { - if (item->type == cJSON_String) { + if (cJSON_IsString(item)) { settings.SetString(item->string, item->valuestring); - } else if (item->type == cJSON_Number) { + } else if (cJSON_IsNumber(item)) { settings.SetInt(item->string, item->valueint); } } @@ -167,17 +167,17 @@ bool Ota::CheckVersion() { has_server_time_ = false; cJSON *server_time = cJSON_GetObjectItem(root, "server_time"); - if (server_time != NULL) { + if (cJSON_IsObject(server_time)) { cJSON *timestamp = cJSON_GetObjectItem(server_time, "timestamp"); cJSON *timezone_offset = cJSON_GetObjectItem(server_time, "timezone_offset"); - if (timestamp != NULL) { + if (cJSON_IsNumber(timestamp)) { // 设置系统时间 struct timeval tv; double ts = timestamp->valuedouble; // 如果有时区偏移,计算本地时间 - if (timezone_offset != NULL) { + if (cJSON_IsNumber(timezone_offset)) { ts += (timezone_offset->valueint * 60 * 1000); // 转换分钟为毫秒 } @@ -192,17 +192,17 @@ bool Ota::CheckVersion() { has_new_version_ = false; cJSON *firmware = cJSON_GetObjectItem(root, "firmware"); - if (firmware != NULL) { + if (cJSON_IsObject(firmware)) { cJSON *version = cJSON_GetObjectItem(firmware, "version"); - if (version != NULL) { + if (cJSON_IsString(version)) { firmware_version_ = version->valuestring; } cJSON *url = cJSON_GetObjectItem(firmware, "url"); - if (url != NULL) { + if (cJSON_IsString(url)) { firmware_url_ = url->valuestring; } - if (version != NULL && url != NULL) { + if (cJSON_IsString(version) && cJSON_IsString(url)) { // Check if the version is newer, for example, 0.1.0 is newer than 0.0.1 has_new_version_ = IsNewVersionAvailable(current_version_, firmware_version_); if (has_new_version_) { @@ -212,7 +212,7 @@ bool Ota::CheckVersion() { } // If the force flag is set to 1, the given version is forced to be installed cJSON *force = cJSON_GetObjectItem(firmware, "force"); - if (force != NULL && force->valueint == 1) { + if (cJSON_IsNumber(force) && force->valueint == 1) { has_new_version_ = true; } } diff --git a/main/protocols/mqtt_protocol.cc b/main/protocols/mqtt_protocol.cc index fa786e76..67a40045 100644 --- a/main/protocols/mqtt_protocol.cc +++ b/main/protocols/mqtt_protocol.cc @@ -38,13 +38,13 @@ bool MqttProtocol::StartMqttClient(bool report_error) { } Settings settings("mqtt", false); - endpoint_ = settings.GetString("endpoint"); - client_id_ = settings.GetString("client_id"); - username_ = settings.GetString("username"); - password_ = settings.GetString("password"); + auto endpoint = settings.GetString("endpoint"); + auto client_id = settings.GetString("client_id"); + auto username = settings.GetString("username"); + auto password = settings.GetString("password"); publish_topic_ = settings.GetString("publish_topic"); - if (endpoint_.empty()) { + if (endpoint.empty()) { ESP_LOGW(TAG, "MQTT endpoint is not specified"); if (report_error) { SetError(Lang::Strings::SERVER_NOT_FOUND); @@ -66,8 +66,8 @@ bool MqttProtocol::StartMqttClient(bool report_error) { return; } cJSON* type = cJSON_GetObjectItem(root, "type"); - if (type == nullptr) { - ESP_LOGE(TAG, "Message type is not specified"); + if (!cJSON_IsString(type)) { + ESP_LOGE(TAG, "Message type is invalid"); cJSON_Delete(root); return; } @@ -89,17 +89,17 @@ bool MqttProtocol::StartMqttClient(bool report_error) { last_incoming_time_ = std::chrono::steady_clock::now(); }); - ESP_LOGI(TAG, "Connecting to endpoint %s", endpoint_.c_str()); + ESP_LOGI(TAG, "Connecting to endpoint %s", endpoint.c_str()); std::string broker_address; int broker_port = 8883; - size_t pos = endpoint_.find(':'); + size_t pos = endpoint.find(':'); if (pos != std::string::npos) { - broker_address = endpoint_.substr(0, pos); - broker_port = std::stoi(endpoint_.substr(pos + 1)); + broker_address = endpoint.substr(0, pos); + broker_port = std::stoi(endpoint.substr(pos + 1)); } else { - broker_address = endpoint_; + broker_address = endpoint; } - if (!mqtt_->Connect(broker_address, broker_port, client_id_, username_, password_)) { + if (!mqtt_->Connect(broker_address, broker_port, client_id, username, password)) { ESP_LOGE(TAG, "Failed to connect to endpoint"); SetError(Lang::Strings::SERVER_NOT_CONNECTED); return false; @@ -270,26 +270,26 @@ void MqttProtocol::ParseServerHello(const cJSON* root) { } auto session_id = cJSON_GetObjectItem(root, "session_id"); - if (session_id != nullptr) { + if (cJSON_IsString(session_id)) { session_id_ = session_id->valuestring; ESP_LOGI(TAG, "Session ID: %s", session_id_.c_str()); } // Get sample rate from hello message auto audio_params = cJSON_GetObjectItem(root, "audio_params"); - if (audio_params != NULL) { + if (cJSON_IsObject(audio_params)) { auto sample_rate = cJSON_GetObjectItem(audio_params, "sample_rate"); - if (sample_rate != NULL) { + if (cJSON_IsNumber(sample_rate)) { server_sample_rate_ = sample_rate->valueint; } auto frame_duration = cJSON_GetObjectItem(audio_params, "frame_duration"); - if (frame_duration != NULL) { + if (cJSON_IsNumber(frame_duration)) { server_frame_duration_ = frame_duration->valueint; } } auto udp = cJSON_GetObjectItem(root, "udp"); - if (udp == nullptr) { + if (!cJSON_IsObject(udp)) { ESP_LOGE(TAG, "UDP is not specified"); return; } diff --git a/main/protocols/mqtt_protocol.h b/main/protocols/mqtt_protocol.h index f8e9fc00..df3da3aa 100644 --- a/main/protocols/mqtt_protocol.h +++ b/main/protocols/mqtt_protocol.h @@ -34,10 +34,6 @@ public: private: EventGroupHandle_t event_group_handle_; - std::string endpoint_; - std::string client_id_; - std::string username_; - std::string password_; std::string publish_topic_; std::mutex channel_mutex_; diff --git a/main/protocols/websocket_protocol.cc b/main/protocols/websocket_protocol.cc index f9975a5f..83527296 100644 --- a/main/protocols/websocket_protocol.cc +++ b/main/protocols/websocket_protocol.cc @@ -154,7 +154,7 @@ bool WebsocketProtocol::OpenAudioChannel() { // Parse JSON data auto root = cJSON_Parse(data); auto type = cJSON_GetObjectItem(root, "type"); - if (type != NULL) { + if (cJSON_IsString(type)) { if (strcmp(type->valuestring, "hello") == 0) { ParseServerHello(root); } else { @@ -223,19 +223,19 @@ void WebsocketProtocol::ParseServerHello(const cJSON* root) { } auto session_id = cJSON_GetObjectItem(root, "session_id"); - if (session_id != nullptr) { + if (cJSON_IsString(session_id)) { session_id_ = session_id->valuestring; ESP_LOGI(TAG, "Session ID: %s", session_id_.c_str()); } auto audio_params = cJSON_GetObjectItem(root, "audio_params"); - if (audio_params != NULL) { + if (cJSON_IsObject(audio_params)) { auto sample_rate = cJSON_GetObjectItem(audio_params, "sample_rate"); - if (sample_rate != NULL) { + if (cJSON_IsNumber(sample_rate)) { server_sample_rate_ = sample_rate->valueint; } auto frame_duration = cJSON_GetObjectItem(audio_params, "frame_duration"); - if (frame_duration != NULL) { + if (cJSON_IsNumber(frame_duration)) { server_frame_duration_ = frame_duration->valueint; } }