From 5c47677f34cabee2372f2ccbd506ebd025e8b793 Mon Sep 17 00:00:00 2001 From: m30073484 Date: Tue, 15 Apr 2025 09:31:42 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=91=8A=E8=AD=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: m30073484 --- .../src/relationshipsyncmgr/relationship_sync_mgr.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/services/service/src/relationshipsyncmgr/relationship_sync_mgr.cpp b/services/service/src/relationshipsyncmgr/relationship_sync_mgr.cpp index a676b056d..dacfda288 100644 --- a/services/service/src/relationshipsyncmgr/relationship_sync_mgr.cpp +++ b/services/service/src/relationshipsyncmgr/relationship_sync_mgr.cpp @@ -568,7 +568,12 @@ std::string RelationShipChangeMsg::ToJson() const cJSON *udidStringObj = nullptr; for (uint32_t index = 0; index < peerUdids.size(); index++) { udidStringObj = cJSON_CreateString(peerUdids[index].c_str()); - if (udidStringObj == nullptr || !cJSON_AddItemToArray(udidArrayObj, udidStringObj)) { + if (udidStringObj == nullptr) { + cJSON_Delete(udidArrayObj); + cJSON_Delete(msg); + return ""; + } + if (!cJSON_AddItemToArray(udidArrayObj, udidStringObj)) { cJSON_Delete(udidStringObj); cJSON_Delete(udidArrayObj); cJSON_Delete(msg); @@ -581,15 +586,11 @@ std::string RelationShipChangeMsg::ToJson() const if (retStr == nullptr) { LOGE("to json is nullptr."); cJSON_Delete(msg); - cJSON_Delete(udidArrayObj); - cJSON_Delete(udidStringObj); return ""; } std::string ret = std::string(retStr); cJSON_Delete(msg); cJSON_free(retStr); - cJSON_Delete(udidArrayObj); - cJSON_Delete(udidStringObj); return ret; } -- Gitee From 0b916d9d3e1dd4e9aef2c05ddc5b70ab2da95bdd Mon Sep 17 00:00:00 2001 From: m30073484 Date: Tue, 15 Apr 2025 09:31:42 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=91=8A=E8=AD=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: m30073484 --- .../relationshipsyncmgr/relationship_sync_mgr.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/services/service/src/relationshipsyncmgr/relationship_sync_mgr.cpp b/services/service/src/relationshipsyncmgr/relationship_sync_mgr.cpp index a676b056d..e5087af03 100644 --- a/services/service/src/relationshipsyncmgr/relationship_sync_mgr.cpp +++ b/services/service/src/relationshipsyncmgr/relationship_sync_mgr.cpp @@ -562,13 +562,17 @@ std::string RelationShipChangeMsg::ToJson() const if (udidArrayObj == nullptr) { LOGE("cJSON_CreateArray failed"); cJSON_Delete(msg); - cJSON_Delete(udidArrayObj); return ""; } cJSON *udidStringObj = nullptr; for (uint32_t index = 0; index < peerUdids.size(); index++) { udidStringObj = cJSON_CreateString(peerUdids[index].c_str()); - if (udidStringObj == nullptr || !cJSON_AddItemToArray(udidArrayObj, udidStringObj)) { + if (udidStringObj == nullptr) { + cJSON_Delete(udidArrayObj); + cJSON_Delete(msg); + return ""; + } + if (!cJSON_AddItemToArray(udidArrayObj, udidStringObj)) { cJSON_Delete(udidStringObj); cJSON_Delete(udidArrayObj); cJSON_Delete(msg); @@ -581,15 +585,11 @@ std::string RelationShipChangeMsg::ToJson() const if (retStr == nullptr) { LOGE("to json is nullptr."); cJSON_Delete(msg); - cJSON_Delete(udidArrayObj); - cJSON_Delete(udidStringObj); return ""; } std::string ret = std::string(retStr); cJSON_Delete(msg); cJSON_free(retStr); - cJSON_Delete(udidArrayObj); - cJSON_Delete(udidStringObj); return ret; } -- Gitee From af696147189554387e1bf75ac65d9366d6b76fc4 Mon Sep 17 00:00:00 2001 From: m30073484 Date: Tue, 15 Apr 2025 23:15:20 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=91=8A=E8=AD=A6=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: m30073484 --- .../relationship_sync_mgr.h | 2 + .../relationship_sync_mgr.cpp | 101 ++++++++++++------ 2 files changed, 69 insertions(+), 34 deletions(-) diff --git a/services/service/include/relationshipsyncmgr/relationship_sync_mgr.h b/services/service/include/relationshipsyncmgr/relationship_sync_mgr.h index d0787d446..57900827e 100644 --- a/services/service/include/relationshipsyncmgr/relationship_sync_mgr.h +++ b/services/service/include/relationshipsyncmgr/relationship_sync_mgr.h @@ -84,6 +84,8 @@ struct RelationShipChangeMsg { bool FromJson(const std::string &msgJson); const std::string ToString() const; + bool BuildCoreFields(cJSON *msg) const; + cJSON *BuildUdidArray() const; }; class ReleationShipSyncMgr { diff --git a/services/service/src/relationshipsyncmgr/relationship_sync_mgr.cpp b/services/service/src/relationshipsyncmgr/relationship_sync_mgr.cpp index e5087af03..ec13afb68 100644 --- a/services/service/src/relationshipsyncmgr/relationship_sync_mgr.cpp +++ b/services/service/src/relationshipsyncmgr/relationship_sync_mgr.cpp @@ -542,52 +542,84 @@ cJSON *RelationShipChangeMsg::ToPayLoadJson() const return arrayObj; } -std::string RelationShipChangeMsg::ToJson() const +cJSON *RelationShipChangeMsg::BuildUdidArray() const { - cJSON *msg = cJSON_CreateObject(); - if (msg == NULL) { - LOGE("failed to create cjson object"); - return ""; + cJson *udidArrayObj = cJSON_CreateArray(); + if (!udidArrayObj) { + LOGE("cJSON_CreatArray failed"); + return nullptr; } - cJSON_AddNumberToObject(msg, MSG_TYPE, (uint32_t)type); + + for (uint32_t index = 0; index < peerUdids.size(); index++) { + cJSON *udidStringObj = cJSON_CreateString(peerUdids[index].c_str()); + if (!udidStringObj) { + LOGE("Create UDID string failed"); + return nullptr; + } + + if (!cJSON_AddItemToArray(udidArrayObj, udidStringObj)) { + LOGE("Add UDID to array failed"); + cJSON_Delete(udidArrayObj); + cJSON_Delete(udidStringObj); + return nullptr; + } + } + return udidArrayObj; +} + +bool RelationShipChangeMsg::BuildCoreFields(cJSON *msg) const +{ + if (!cJSON_AddNumberToObject(msg, MSG_TYPE, (unit32_t)type)) { + LOGE("Add type failed"); + return false; + } + cJSON *arrayObj = ToPayLoadJson(); - if (arrayObj == nullptr) { - LOGE("ArrayObj is nullptr."); - cJSON_Delete(msg); + if (!arrayObj) { + LOGE("Payload is nullptr"); + return false; + } + if (!cJSON_AddItemToObject(msg, MSG_VALUE, arrayObj)) { + LOGE("Add Payload failed"); + cJSON_Delete(arrayObj); + return false; + } + cJSON *udidArrayObj = BuildUdidArray(); + if (!udidArrayObj) { + return false; + } + if (!cJSON_AddItemToObject(msg, MSG_PEER_UDID, udidArrayObj)) { + LOGE("Add UDID array failed"); + cJSON_Delete(udidArrayObj); + return false; + } + if (!cJSON_AddStringToObject(msg, MSG_ACCOUNTID, accountName.c_str())) { + LOGE("Add account ID failed"); + return false; + } + return true; +} + +std::string RelationShipChangeMsg::ToJson() const +{ + cJSON *msg = cJSON_CreateObject(); + if (!msg) { + LOGE("Creat root object failed"); return ""; } - cJSON_AddItemToObject(msg, MSG_VALUE, arrayObj); - cJSON *udidArrayObj = cJSON_CreateArray(); - if (udidArrayObj == nullptr) { - LOGE("cJSON_CreateArray failed"); + if (!BuildCoreFields(msg)) { cJSON_Delete(msg); return ""; } - cJSON *udidStringObj = nullptr; - for (uint32_t index = 0; index < peerUdids.size(); index++) { - udidStringObj = cJSON_CreateString(peerUdids[index].c_str()); - if (udidStringObj == nullptr) { - cJSON_Delete(udidArrayObj); - cJSON_Delete(msg); - return ""; - } - if (!cJSON_AddItemToArray(udidArrayObj, udidStringObj)) { - cJSON_Delete(udidStringObj); - cJSON_Delete(udidArrayObj); - cJSON_Delete(msg); - return ""; - } - } - cJSON_AddItemToObject(msg, MSG_PEER_UDID, udidArrayObj); - cJSON_AddStringToObject(msg, MSG_ACCOUNTID, accountName.c_str()); + char *retStr = cJSON_PrintUnformatted(msg); - if (retStr == nullptr) { - LOGE("to json is nullptr."); - cJSON_Delete(msg); + if (!retStr) { + LOGE("Generate JSON string failed"); return ""; } - std::string ret = std::string(retStr); + + std::string ret(retStr); cJSON_Delete(msg); cJSON_free(retStr); return ret; @@ -697,5 +729,6 @@ void GetFrontAndBackUserIdInfos(const std::vector &remoteUserIdInfos } } } + } // DistributedHardware } // OHOS \ No newline at end of file -- Gitee