From 0049bf0211c2dd650aa7951d8415218bd01c6c53 Mon Sep 17 00:00:00 2001 From: li-tiangang4 Date: Wed, 6 Nov 2024 17:51:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=91=8A=E8=AD=A6=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: li-tiangang4 --- .../src/componentloader/component_loader.cpp | 142 ++++++++++------ .../componentmanager/component_privacy.cpp | 31 ++-- .../src/distributed_hardware_service.cpp | 6 +- .../src/lowlatency/low_latency_listener.cpp | 12 +- .../src/resourcemanager/capability_info.cpp | 37 +++-- .../resourcemanager/meta_capability_info.cpp | 45 +++--- .../src/resourcemanager/version_info.cpp | 40 +++-- .../src/transport/dh_transport_obj.cpp | 32 ++-- .../src/utils/dh_context.cpp | 4 +- utils/include/dh_utils_tool.h | 14 -- utils/src/dh_utils_tool.cpp | 60 ------- utils/src/histreamer_ability_parser.cpp | 151 ++++++++++-------- .../common/utilstool/utils_tool_test.cpp | 96 ----------- 13 files changed, 292 insertions(+), 378 deletions(-) diff --git a/services/distributedhardwarefwkservice/src/componentloader/component_loader.cpp b/services/distributedhardwarefwkservice/src/componentloader/component_loader.cpp index 145f2e27..5c19018e 100644 --- a/services/distributedhardwarefwkservice/src/componentloader/component_loader.cpp +++ b/services/distributedhardwarefwkservice/src/componentloader/component_loader.cpp @@ -109,85 +109,108 @@ std::vector ComponentLoader::GetAllCompTypes() int32_t ParseComponent(const cJSON *json, CompConfig &cfg) { - if (!IsString(json, COMP_NAME)) { + cJSON *nameJson = cJSON_GetObjectItem(json, COMP_NAME.c_str()); + if (nameJson == NULL || !cJSON_IsString(nameJson)) { DHLOGE("COMP_NAME is invalid!"); return ERR_DH_FWK_JSON_PARSE_FAILED; } - cfg.name = cJSON_GetObjectItem(json, COMP_NAME.c_str())->valuestring; - if (!IsString(json, COMP_TYPE)) { + cfg.name = nameJson->valuestring; + + cJSON *typeJson = cJSON_GetObjectItem(json, COMP_TYPE.c_str()); + if (typeJson == NULL || !cJSON_IsString(typeJson)) { DHLOGE("COMP_TYPE is invalid!"); return ERR_DH_FWK_JSON_PARSE_FAILED; } - cfg.type = g_mapDhTypeName[cJSON_GetObjectItem(json, COMP_TYPE.c_str())->valuestring]; - if (!IsString(json, COMP_HANDLER_LOC)) { + cfg.type = g_mapDhTypeName[typeJson->valuestring]; + + cJSON *handlerLocJson = cJSON_GetObjectItem(json, COMP_HANDLER_LOC.c_str()); + if (handlerLocJson == NULL || !cJSON_IsString(handlerLocJson)) { DHLOGE("COMP_HANDLER_LOC is invalid!"); return ERR_DH_FWK_JSON_PARSE_FAILED; } - cfg.compHandlerLoc = cJSON_GetObjectItem(json, COMP_HANDLER_LOC.c_str())->valuestring; - if (!IsString(json, COMP_HANDLER_VERSION)) { + cfg.compHandlerLoc = handlerLocJson->valuestring; + + cJSON *handlerVerJson = cJSON_GetObjectItem(json, COMP_HANDLER_VERSION.c_str()); + if (handlerVerJson == NULL || !cJSON_IsString(handlerVerJson)) { DHLOGE("COMP_HANDLER_VERSION is invalid!"); return ERR_DH_FWK_JSON_PARSE_FAILED; } - cfg.compHandlerVersion = cJSON_GetObjectItem(json, COMP_HANDLER_VERSION.c_str())->valuestring; + cfg.compHandlerVersion = handlerVerJson->valuestring; return DH_FWK_SUCCESS; } int32_t ParseSource(const cJSON *json, CompConfig &cfg) { - if (!IsString(json, COMP_SOURCE_LOC)) { + cJSON *sourceLocJson = cJSON_GetObjectItem(json, COMP_SOURCE_LOC.c_str()); + if (sourceLocJson == NULL || !cJSON_IsString(sourceLocJson)) { DHLOGE("COMP_SOURCE_LOC is invalid!"); return ERR_DH_FWK_JSON_PARSE_FAILED; } - cfg.compSourceLoc = cJSON_GetObjectItem(json, COMP_SOURCE_LOC.c_str())->valuestring; - if (!IsString(json, COMP_SOURCE_VERSION)) { + cfg.compSourceLoc = sourceLocJson->valuestring; + + cJSON *sourceVerJson = cJSON_GetObjectItem(json, COMP_SOURCE_VERSION.c_str()); + if (sourceVerJson == NULL || !cJSON_IsString(sourceVerJson)) { DHLOGE("COMP_SOURCE_VERSION is invalid!"); return ERR_DH_FWK_JSON_PARSE_FAILED; } - cfg.compSourceVersion = cJSON_GetObjectItem(json, COMP_SOURCE_VERSION.c_str())->valuestring; - if (!IsInt32(json, COMP_SOURCE_SA_ID)) { + cfg.compSourceVersion = sourceVerJson->valuestring; + + cJSON *sourceSaIdJson = cJSON_GetObjectItem(json, COMP_SOURCE_SA_ID.c_str()); + if (sourceSaIdJson == NULL || !cJSON_IsNumber(sourceSaIdJson)) { DHLOGE("COMP_SOURCE_SA_ID is invalid!"); return ERR_DH_FWK_JSON_PARSE_FAILED; } - cfg.compSourceSaId = static_cast(cJSON_GetObjectItem(json, COMP_SOURCE_SA_ID.c_str())->valuedouble); + cfg.compSourceSaId = static_cast(sourceSaIdJson->valuedouble); return DH_FWK_SUCCESS; } int32_t ParseSink(const cJSON *json, CompConfig &cfg) { - if (!IsString(json, COMP_SINK_LOC)) { + cJSON *sinkLocJson = cJSON_GetObjectItem(json, COMP_SINK_LOC.c_str()); + if (sinkLocJson == NULL || !cJSON_IsString(sinkLocJson)) { DHLOGE("COMP_SINK_LOC is invalid!"); return ERR_DH_FWK_JSON_PARSE_FAILED; } - cfg.compSinkLoc = cJSON_GetObjectItem(json, COMP_SINK_LOC.c_str())->valuestring; - if (!IsString(json, COMP_SINK_VERSION)) { + cfg.compSinkLoc = sinkLocJson->valuestring; + + cJSON *sinkVerJson = cJSON_GetObjectItem(json, COMP_SINK_VERSION.c_str()); + if (sinkVerJson == NULL || !cJSON_IsString(sinkVerJson)) { DHLOGE("COMP_SINK_VERSION is invalid!"); return ERR_DH_FWK_JSON_PARSE_FAILED; } - cfg.compSinkVersion = cJSON_GetObjectItem(json, COMP_SINK_VERSION.c_str())->valuestring; - if (!IsInt32(json, COMP_SINK_SA_ID)) { + cfg.compSinkVersion = sinkVerJson->valuestring; + + cJSON *sinkSaIdJson = cJSON_GetObjectItem(json, COMP_SINK_SA_ID.c_str()); + if (sinkSaIdJson == NULL || !cJSON_IsNumber(sinkSaIdJson)) { DHLOGE("COMP_SINK_SA_ID is invalid!"); return ERR_DH_FWK_JSON_PARSE_FAILED; } - cfg.compSinkSaId = static_cast(cJSON_GetObjectItem(json, COMP_SINK_SA_ID.c_str())->valuedouble); + cfg.compSinkSaId = static_cast(sinkSaIdJson->valuedouble); return DH_FWK_SUCCESS; } int32_t ParseResourceDesc(const cJSON *json, CompConfig &cfg) { - if (!IsArray(json, COMP_RESOURCE_DESC)) { + cJSON *resouceDescJson = cJSON_GetObjectItem(json, COMP_RESOURCE_DESC.c_str()); + if (resouceDescJson == NULL || !cJSON_IsArray(resouceDescJson)) { DHLOGE("COMP_RESOURCE_DESC is invalid!"); return ERR_DH_FWK_JSON_PARSE_FAILED; } - cJSON *resourceDescArray = cJSON_GetObjectItem(json, COMP_RESOURCE_DESC.c_str()); cJSON *element = nullptr; - cJSON_ArrayForEach(element, resourceDescArray) { + cJSON_ArrayForEach(element, resouceDescJson) { ResourceDesc desc; - if (!IsString(element, COMP_SUBTYPE)) { + cJSON *subtypeJson = cJSON_GetObjectItem(element, COMP_SUBTYPE.c_str()); + if (subtypeJson == NULL || !cJSON_IsString(subtypeJson)) { DHLOGE("COMP_SUBTYPE is invalid!"); return ERR_DH_FWK_JSON_PARSE_FAILED; } - desc.subtype = cJSON_GetObjectItem(element, COMP_SUBTYPE.c_str())->valuestring; + desc.subtype = subtypeJson->valuestring; + cJSON *sensitive = cJSON_GetObjectItem(element, COMP_SENSITIVE.c_str()); + if (sensitive == NULL || !cJSON_IsBool(sensitive)) { + DHLOGE("COMP_SENSITIVE is invalid!"); + return ERR_DH_FWK_JSON_PARSE_FAILED; + } if (cJSON_IsTrue(sensitive)) { desc.sensitiveValue = true; } else { @@ -255,12 +278,12 @@ int32_t ComponentLoader::GetCompPathAndVersion(const std::string &jsonStr, std:: DHLOGE("jsonStr parse failed"); return ERR_DH_FWK_JSON_PARSE_FAILED; } - if (!IsArray(root, COMPONENTSLOAD_DISTRIBUTED_COMPONENTS)) { + cJSON *components = cJSON_GetObjectItem(root, COMPONENTSLOAD_DISTRIBUTED_COMPONENTS.c_str()); + if (components == NULL || !cJSON_IsArray(components)) { DHLOGE("distributed_components is not an array"); cJSON_Delete(root); return ERR_DH_FWK_PARA_INVALID; } - cJSON *components = cJSON_GetObjectItem(root, COMPONENTSLOAD_DISTRIBUTED_COMPONENTS.c_str()); size_t compSize = static_cast(cJSON_GetArraySize(components)); if (compSize == 0 || compSize > MAX_COMP_SIZE) { @@ -283,40 +306,48 @@ int32_t ComponentLoader::GetCompPathAndVersion(const std::string &jsonStr, std:: void ComponentLoader::ParseCompConfigFromJson(cJSON *component, CompConfig &config) { - if (IsString(component, COMP_NAME.c_str())) { - config.name = cJSON_GetObjectItem(component, COMP_NAME.c_str())->valuestring; + cJSON *nameJson = cJSON_GetObjectItem(component, COMP_NAME.c_str()); + if (nameJson && cJSON_IsString(nameJson)) { + config.name = nameJson->valuestring; } - if (IsString(component, COMP_TYPE.c_str())) { - config.type = g_mapDhTypeName[cJSON_GetObjectItem(component, COMP_TYPE.c_str())->valuestring]; + cJSON *typeJson = cJSON_GetObjectItem(component, COMP_TYPE.c_str()); + if (typeJson && cJSON_IsString(typeJson)) { + config.type = g_mapDhTypeName[typeJson->valuestring]; } - if (IsString(component, COMP_HANDLER_LOC.c_str())) { - config.compHandlerLoc = cJSON_GetObjectItem(component, COMP_HANDLER_LOC.c_str())->valuestring; + cJSON *handlerLocJson = cJSON_GetObjectItem(component, COMP_HANDLER_LOC.c_str()); + if (handlerLocJson && cJSON_IsString(handlerLocJson)) { + config.compHandlerLoc = handlerLocJson->valuestring; } - if (IsString(component, COMP_HANDLER_VERSION.c_str())) { - config.compHandlerVersion = cJSON_GetObjectItem(component, COMP_HANDLER_VERSION.c_str())->valuestring; + cJSON *handlerVerJson = cJSON_GetObjectItem(component, COMP_HANDLER_VERSION.c_str()); + if (handlerVerJson && cJSON_IsString(handlerVerJson)) { + config.compHandlerVersion = handlerVerJson->valuestring; } - if (IsString(component, COMP_SOURCE_LOC.c_str())) { - config.compSourceLoc = cJSON_GetObjectItem(component, COMP_SOURCE_LOC.c_str())->valuestring; + cJSON *sourceLocJson = cJSON_GetObjectItem(component, COMP_SOURCE_LOC.c_str()); + if (sourceLocJson && cJSON_IsString(sourceLocJson)) { + config.compSourceLoc = sourceLocJson->valuestring; } - if (IsString(component, COMP_SOURCE_VERSION.c_str())) { - config.compSourceVersion = cJSON_GetObjectItem(component, COMP_SOURCE_VERSION.c_str())->valuestring; + cJSON *sourceVerJson = cJSON_GetObjectItem(component, COMP_SOURCE_VERSION.c_str()); + if (sourceVerJson && cJSON_IsString(sourceVerJson)) { + config.compSourceVersion = sourceVerJson->valuestring; } - if (IsInt32(component, COMP_SOURCE_SA_ID.c_str())) { - config.compSourceSaId = - static_cast(cJSON_GetObjectItem(component, COMP_SOURCE_SA_ID.c_str())->valuedouble); + cJSON *sourceSaIdJson = cJSON_GetObjectItem(component, COMP_SOURCE_SA_ID.c_str()); + if (sourceSaIdJson && cJSON_IsNumber(sourceSaIdJson)) { + config.compSourceSaId = static_cast(sourceSaIdJson->valuedouble); } - if (IsString(component, COMP_SINK_LOC.c_str())) { - config.compSinkLoc = cJSON_GetObjectItem(component, COMP_SINK_LOC.c_str())->valuestring; + cJSON *sinkLocJson = cJSON_GetObjectItem(component, COMP_SINK_LOC.c_str()); + if (sinkLocJson && cJSON_IsString(sinkLocJson)) { + config.compSinkLoc = sinkLocJson->valuestring; } - if (IsString(component, COMP_SINK_VERSION.c_str())) { - config.compSinkVersion = cJSON_GetObjectItem(component, COMP_SINK_VERSION.c_str())->valuestring; + cJSON *sinkVerJson = cJSON_GetObjectItem(component, COMP_SINK_VERSION.c_str()); + if (sinkVerJson && cJSON_IsString(sinkVerJson)) { + config.compSinkVersion = sinkVerJson->valuestring; } - if (IsInt32(component, COMP_SINK_SA_ID.c_str())) { - config.compSinkSaId = - static_cast(cJSON_GetObjectItem(component, COMP_SINK_SA_ID.c_str())->valuedouble); + cJSON *sinkSaIdJson = cJSON_GetObjectItem(component, COMP_SINK_SA_ID.c_str()); + if (sinkSaIdJson && cJSON_IsNumber(sinkSaIdJson)) { + config.compSinkSaId = static_cast(sinkSaIdJson->valuedouble); } - if (IsArray(component, COMP_RESOURCE_DESC.c_str())) { - cJSON *resourceDescs = cJSON_GetObjectItem(component, COMP_RESOURCE_DESC.c_str()); + cJSON *resourceDescs = cJSON_GetObjectItem(component, COMP_RESOURCE_DESC.c_str()); + if (resourceDescs && cJSON_IsArray(resourceDescs)) { ParseResourceDescFromJson(resourceDescs, config); } } @@ -327,17 +358,22 @@ void ComponentLoader::ParseResourceDescFromJson(cJSON *resourceDescs, CompConfig cJSON_ArrayForEach(resourceDesc, resourceDescs) { bool sensitiveValue; cJSON *sensitive = cJSON_GetObjectItem(resourceDesc, COMP_SENSITIVE.c_str()); + if (sensitive == NULL || !cJSON_IsBool(sensitive)) { + DHLOGE("COMP_SENSITIVE is invalid!"); + return; + } if (cJSON_IsTrue(sensitive)) { sensitiveValue = true; } else { sensitiveValue = false; } ResourceDesc resource; - if (!IsString(resourceDesc, COMP_SUBTYPE)) { + cJSON *resourceDescObj = cJSON_GetObjectItem(resourceDesc, COMP_SUBTYPE.c_str()); + if (resourceDescObj == NULL || !cJSON_IsString(resourceDescObj)) { DHLOGE("COMP_SUBTYPE is invalid!"); return; } - resource.subtype = cJSON_GetObjectItem(resourceDesc, COMP_SUBTYPE.c_str())->valuestring; + resource.subtype = resourceDescObj->valuestring; resource.sensitiveValue = sensitiveValue; config.compResourceDesc.push_back(resource); } diff --git a/services/distributedhardwarefwkservice/src/componentmanager/component_privacy.cpp b/services/distributedhardwarefwkservice/src/componentmanager/component_privacy.cpp index 8a370b3b..1656ff63 100644 --- a/services/distributedhardwarefwkservice/src/componentmanager/component_privacy.cpp +++ b/services/distributedhardwarefwkservice/src/componentmanager/component_privacy.cpp @@ -295,47 +295,50 @@ ComponentPrivacy::ComponentEventHandler::~ComponentEventHandler() void ComponentPrivacy::ComponentEventHandler::ProcessStartPage(const AppExecFwk::InnerEvent::Pointer &event) { DHLOGI("ProcessStartPage enter."); - if (event == nullptr) { - DHLOGE("event is nullptr"); - return; - } std::shared_ptr dataMsg = event->GetSharedObject(); cJSON *innerMsg = cJSON_GetArrayItem(dataMsg.get(), 0); - if (!IsString(innerMsg, PRIVACY_SUBTYPE)) { + if (innerMsg == NULL) { + DHLOGE("innerMsg is nullptr"); + return; + } + cJSON *subtypeJson = cJSON_GetObjectItem(innerMsg, PRIVACY_SUBTYPE.c_str()); + if (subtypeJson == NULL || !cJSON_IsString(subtypeJson)) { DHLOGE("PRIVACY_SUBTYPE is invalid!"); return; } - std::string subtype = cJSON_GetObjectItem(innerMsg, PRIVACY_SUBTYPE.c_str())->valuestring; - if (!IsString(innerMsg, PRIVACY_NETWORKID)) { + std::string subtype = subtypeJson->valuestring; + cJSON *networkIdObj = cJSON_GetObjectItem(innerMsg, PRIVACY_NETWORKID.c_str()); + if (networkIdObj == NULL || !cJSON_IsString(networkIdObj)) { DHLOGE("PRIVACY_NETWORKID is invalid!"); return; } + std::string networkId = networkIdObj->valuestring; if (comPrivacyObj_ == nullptr) { DHLOGE("comPrivacyObj_ is nullptr"); return; } - std::string networkId = cJSON_GetObjectItem(innerMsg, PRIVACY_NETWORKID.c_str())->valuestring; comPrivacyObj_->StartPrivacePage(subtype, networkId); } void ComponentPrivacy::ComponentEventHandler::ProcessStopPage(const AppExecFwk::InnerEvent::Pointer &event) { DHLOGI("ProcessStopPage enter."); - if (event == nullptr) { - DHLOGE("event is nullptr"); - return; - } std::shared_ptr dataMsg = event->GetSharedObject(); cJSON *innerMsg = cJSON_GetArrayItem(dataMsg.get(), 0); - if (!IsString(innerMsg, PRIVACY_SUBTYPE)) { + if (innerMsg == NULL) { + DHLOGE("innerMsg is nullptr"); + return; + } + cJSON *subtypeJson = cJSON_GetObjectItem(innerMsg, PRIVACY_SUBTYPE.c_str()); + if (subtypeJson == NULL || !cJSON_IsString(subtypeJson)) { DHLOGE("PRIVACY_SUBTYPE is invalid!"); return; } + std::string subtype = subtypeJson->valuestring; if (comPrivacyObj_ == nullptr) { DHLOGE("comPrivacyObj_ is nullptr"); return; } - std::string subtype = cJSON_GetObjectItem(innerMsg, PRIVACY_SUBTYPE.c_str())->valuestring; comPrivacyObj_->StopPrivacePage(subtype); } } // namespace DistributedHardware diff --git a/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp b/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp index 7c6c2466..3ccaa1a7 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp +++ b/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp @@ -237,12 +237,12 @@ std::string DistributedHardwareService::QueryDhSysSpec(const std::string &target DHLOGE("attrs json is invalid, attrs: %{public}s", attrs.c_str()); return ""; } - if (!IsString(attrJson, targetKey)) { - DHLOGE("Attrs Json not contains key: %{public}s", targetKey.c_str()); + cJSON *targetKeyJson = cJSON_GetObjectItem(attrJson, targetKey.c_str()); + if (targetKeyJson == NULL || !cJSON_IsString(targetKeyJson)) { cJSON_Delete(attrJson); return ""; } - std::string result = cJSON_GetObjectItem(attrJson, targetKey.c_str())->valuestring; + std::string result = targetKeyJson->valuestring; cJSON_Delete(attrJson); return result; } diff --git a/services/distributedhardwarefwkservice/src/lowlatency/low_latency_listener.cpp b/services/distributedhardwarefwkservice/src/lowlatency/low_latency_listener.cpp index d49b50e8..9382bda1 100644 --- a/services/distributedhardwarefwkservice/src/lowlatency/low_latency_listener.cpp +++ b/services/distributedhardwarefwkservice/src/lowlatency/low_latency_listener.cpp @@ -53,19 +53,21 @@ void LowLatencyListener::OnMessage(const DHTopic topic, const std::string& messa DHLOGE("jsonStr parse failed"); return; } - if (!IsUInt32(jsonObj, DH_TYPE)) { + cJSON *dhTypeJson = cJSON_GetObjectItem(jsonObj, DH_TYPE.c_str()); + if (dhTypeJson == NULL || !cJSON_IsNumber(dhTypeJson)) { DHLOGE("The DH_TYPE key is invalid!"); cJSON_Delete(jsonObj); return; } - if (!IsBool(jsonObj, LOW_LATENCY_ENABLE)) { + DHType dhType = (DHType)dhTypeJson->valuedouble; + + cJSON *enableJson = cJSON_GetObjectItem(jsonObj, LOW_LATENCY_ENABLE.c_str()); + if (enableJson == NULL || !cJSON_IsBool(enableJson)) { DHLOGE("The LOW_LATENCY_ENABLE key is invalid!"); cJSON_Delete(jsonObj); return; } - DHType dhType = (DHType)cJSON_GetObjectItem(jsonObj, DH_TYPE.c_str())->valuedouble; - cJSON *isEnable = cJSON_GetObjectItem(jsonObj, LOW_LATENCY_ENABLE.c_str()); - if (cJSON_IsTrue(isEnable)) { + if (cJSON_IsTrue(enableJson)) { LowLatency::GetInstance().EnableLowLatency(dhType); } else { LowLatency::GetInstance().DisableLowLatency(dhType); diff --git a/services/distributedhardwarefwkservice/src/resourcemanager/capability_info.cpp b/services/distributedhardwarefwkservice/src/resourcemanager/capability_info.cpp index 3f1994ca..99d7e45b 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/capability_info.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/capability_info.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -207,47 +207,54 @@ void FromJson(const cJSON *jsonObject, CapabilityInfo &capability) DHLOGE("Json pointer is nullptr!"); return; } - if (!IsString(jsonObject, DH_ID)) { + cJSON *dhIdJson = cJSON_GetObjectItem(jsonObject, DH_ID.c_str()); + if (dhIdJson == NULL || !cJSON_IsString(dhIdJson)) { DHLOGE("DH_ID is invalid!"); return; } - capability.SetDHId(cJSON_GetObjectItem(jsonObject, DH_ID.c_str())->valuestring); + capability.SetDHId(dhIdJson->valuestring); - if (!IsString(jsonObject, DEV_ID)) { + cJSON *devIdJson = cJSON_GetObjectItem(jsonObject, DEV_ID.c_str()); + if (devIdJson == NULL || !cJSON_IsString(devIdJson)) { DHLOGE("DEV_ID is invalid!"); return; } - capability.SetDeviceId(cJSON_GetObjectItem(jsonObject, DEV_ID.c_str())->valuestring); + capability.SetDeviceId(devIdJson->valuestring); - if (!IsString(jsonObject, DEV_NAME)) { + cJSON *devNameJson = cJSON_GetObjectItem(jsonObject, DEV_NAME.c_str()); + if (devNameJson == NULL || !cJSON_IsString(devNameJson)) { DHLOGE("DEV_NAME is invalid!"); return; } - capability.SetDeviceName(cJSON_GetObjectItem(jsonObject, DEV_NAME.c_str())->valuestring); + capability.SetDeviceName(devNameJson->valuestring); - if (!IsUInt16(jsonObject, DEV_TYPE)) { + cJSON *devTypeJson = cJSON_GetObjectItem(jsonObject, DEV_TYPE.c_str()); + if (devTypeJson == NULL || !cJSON_IsNumber(devTypeJson)) { DHLOGE("DEV_TYPE is invalid!"); return; } - capability.SetDeviceType((uint16_t)cJSON_GetObjectItem(jsonObject, DEV_TYPE.c_str())->valuedouble); + capability.SetDeviceType(static_cast(devTypeJson->valuedouble)); - if (!IsUInt32(jsonObject, DH_TYPE)) { + cJSON *dhTypeJson = cJSON_GetObjectItem(jsonObject, DH_TYPE.c_str()); + if (dhTypeJson == NULL || !cJSON_IsNumber(dhTypeJson)) { DHLOGE("DH_TYPE is invalid!"); return; } - capability.SetDHType((DHType)cJSON_GetObjectItem(jsonObject, DH_TYPE.c_str())->valuedouble); + capability.SetDHType((DHType)dhTypeJson->valuedouble); - if (!IsString(jsonObject, DH_ATTRS)) { + cJSON *dhAttrsJson = cJSON_GetObjectItem(jsonObject, DH_ATTRS.c_str()); + if (dhAttrsJson == NULL || !cJSON_IsString(dhAttrsJson)) { DHLOGE("DH_ATTRS is invalid!"); return; } - capability.SetDHAttrs(cJSON_GetObjectItem(jsonObject, DH_ATTRS.c_str())->valuestring); + capability.SetDHAttrs(dhAttrsJson->valuestring); - if (!IsString(jsonObject, DH_SUBTYPE)) { + cJSON *dhSubtypeJson = cJSON_GetObjectItem(jsonObject, DH_SUBTYPE.c_str()); + if (dhSubtypeJson == NULL || !cJSON_IsString(dhSubtypeJson)) { DHLOGE("DH_SUBTYPE is invalid!"); return; } - capability.SetDHSubtype(cJSON_GetObjectItem(jsonObject, DH_SUBTYPE.c_str())->valuestring); + capability.SetDHSubtype(dhSubtypeJson->valuestring); } } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/src/resourcemanager/meta_capability_info.cpp b/services/distributedhardwarefwkservice/src/resourcemanager/meta_capability_info.cpp index fc08bc62..df0257a2 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/meta_capability_info.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/meta_capability_info.cpp @@ -163,59 +163,68 @@ void ToJson(cJSON *jsonObject, const MetaCapabilityInfo &metaCapInfo) void FromJson(const cJSON *jsonObject, MetaCapabilityInfo &metaCapInfo) { - if (!IsString(jsonObject, DH_ID)) { + cJSON *dhIdJson = cJSON_GetObjectItem(jsonObject, DH_ID.c_str()); + if (dhIdJson == NULL || !cJSON_IsString(dhIdJson)) { DHLOGE("DH_ID is invalid!"); return; } - metaCapInfo.SetDHId(cJSON_GetObjectItem(jsonObject, DH_ID.c_str())->valuestring); + metaCapInfo.SetDHId(dhIdJson->valuestring); - if (!IsString(jsonObject, DEV_ID)) { + cJSON *devIdJson = cJSON_GetObjectItem(jsonObject, DEV_ID.c_str()); + if (devIdJson == NULL || !cJSON_IsString(devIdJson)) { DHLOGE("DEV_ID is invalid!"); return; } - metaCapInfo.SetDeviceId(cJSON_GetObjectItem(jsonObject, DEV_ID.c_str())->valuestring); + metaCapInfo.SetDeviceId(devIdJson->valuestring); - if (!IsString(jsonObject, DEV_NAME)) { + cJSON *devNameJson = cJSON_GetObjectItem(jsonObject, DEV_NAME.c_str()); + if (devNameJson == NULL || !cJSON_IsString(devNameJson)) { DHLOGE("DEV_NAME is invalid!"); return; } - metaCapInfo.SetDeviceName(cJSON_GetObjectItem(jsonObject, DEV_NAME.c_str())->valuestring); + metaCapInfo.SetDeviceName(devNameJson->valuestring); - if (!IsUInt16(jsonObject, DEV_TYPE)) { + cJSON *devTypeJson = cJSON_GetObjectItem(jsonObject, DEV_TYPE.c_str()); + if (devTypeJson == NULL || !cJSON_IsNumber(devTypeJson)) { DHLOGE("DEV_TYPE is invalid!"); return; } - metaCapInfo.SetDeviceType((uint16_t)cJSON_GetObjectItem(jsonObject, DEV_TYPE.c_str())->valuedouble); + metaCapInfo.SetDeviceType(static_cast(devTypeJson->valuedouble)); - if (!IsUInt32(jsonObject, DH_TYPE)) { + cJSON *dhTypeJson = cJSON_GetObjectItem(jsonObject, DH_TYPE.c_str()); + if (dhTypeJson == NULL || !cJSON_IsNumber(dhTypeJson)) { DHLOGE("DH_TYPE is invalid!"); return; } - metaCapInfo.SetDHType((DHType)cJSON_GetObjectItem(jsonObject, DH_TYPE.c_str())->valuedouble); + metaCapInfo.SetDHType((DHType)dhTypeJson->valuedouble); - if (!IsString(jsonObject, DH_ATTRS)) { + cJSON *dhAttrsObj = cJSON_GetObjectItem(jsonObject, DH_ATTRS.c_str()); + if (dhAttrsObj == NULL || !cJSON_IsString(dhAttrsObj)) { DHLOGE("DH_ATTRS is invalid!"); return; } - metaCapInfo.SetDHAttrs(cJSON_GetObjectItem(jsonObject, DH_ATTRS.c_str())->valuestring); + metaCapInfo.SetDHAttrs(dhAttrsObj->valuestring); - if (!IsString(jsonObject, DH_SUBTYPE)) { + cJSON *dhSubtypeJson = cJSON_GetObjectItem(jsonObject, DH_SUBTYPE.c_str()); + if (dhSubtypeJson == NULL || !cJSON_IsString(dhSubtypeJson)) { DHLOGE("DH_SUBTYPE is invalid!"); return; } - metaCapInfo.SetDHSubtype(cJSON_GetObjectItem(jsonObject, DH_SUBTYPE.c_str())->valuestring); + metaCapInfo.SetDHSubtype(dhSubtypeJson->valuestring); - if (!IsString(jsonObject, DEV_UDID_HASH)) { + cJSON *udidHashJson = cJSON_GetObjectItem(jsonObject, DEV_UDID_HASH.c_str()); + if (udidHashJson == NULL || !cJSON_IsString(udidHashJson)) { DHLOGE("DEV_UDID_HASH is invalid!"); return; } - metaCapInfo.SetUdidHash(cJSON_GetObjectItem(jsonObject, DEV_UDID_HASH.c_str())->valuestring); + metaCapInfo.SetUdidHash(udidHashJson->valuestring); - if (!IsString(jsonObject, SINK_VER)) { + cJSON *sinkVerJson = cJSON_GetObjectItem(jsonObject, SINK_VER.c_str()); + if (sinkVerJson == NULL || !cJSON_IsString(sinkVerJson)) { DHLOGE("SINK_VER is invalid!"); return; } - metaCapInfo.SetSinkVersion(cJSON_GetObjectItem(jsonObject, SINK_VER.c_str())->valuestring); + metaCapInfo.SetSinkVersion(sinkVerJson->valuestring); } } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/src/resourcemanager/version_info.cpp b/services/distributedhardwarefwkservice/src/resourcemanager/version_info.cpp index 8f8b2a74..25ff1716 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/version_info.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/version_info.cpp @@ -101,21 +101,29 @@ void FromJson(const cJSON *jsonObject, CompVersion &compVer) DHLOGE("Json pointer is nullptr!"); return; } - if (IsString(jsonObject, NAME)) { - compVer.name = cJSON_GetObjectItem(jsonObject, NAME.c_str())->valuestring; + cJSON *nameJson = cJSON_GetObjectItem(jsonObject, NAME.c_str()); + if (nameJson && cJSON_IsString(nameJson)) { + compVer.name = nameJson->valuestring; } - if (IsUInt32(jsonObject, TYPE) && - (DHType)cJSON_GetObjectItem(jsonObject, TYPE.c_str())->valuedouble <= DHType::MAX_DH) { - compVer.dhType = (DHType)(cJSON_GetObjectItem(jsonObject, TYPE.c_str())->valuedouble); + + cJSON *typeJson = cJSON_GetObjectItem(jsonObject, TYPE.c_str()); + if (typeJson && cJSON_IsNumber(typeJson) && (DHType)typeJson->valuedouble <= DHType::MAX_DH) { + compVer.dhType = (DHType)typeJson->valuedouble; } - if (IsString(jsonObject, HANDLER)) { - compVer.handlerVersion = cJSON_GetObjectItem(jsonObject, HANDLER.c_str())->valuestring; + + cJSON *handlerJson = cJSON_GetObjectItem(jsonObject, HANDLER.c_str()); + if (handlerJson && cJSON_IsString(handlerJson)) { + compVer.handlerVersion = handlerJson->valuestring; } - if (IsString(jsonObject, SOURCE_VER)) { - compVer.sourceVersion = cJSON_GetObjectItem(jsonObject, SOURCE_VER.c_str())->valuestring; + + cJSON *sourceVerJson = cJSON_GetObjectItem(jsonObject, SOURCE_VER.c_str()); + if (sourceVerJson && cJSON_IsString(sourceVerJson)) { + compVer.sourceVersion = sourceVerJson->valuestring; } - if (IsString(jsonObject, SINK_VER)) { - compVer.sinkVersion = cJSON_GetObjectItem(jsonObject, SINK_VER.c_str())->valuestring; + + cJSON *sinkVerJson = cJSON_GetObjectItem(jsonObject, SINK_VER.c_str()); + if (sinkVerJson && cJSON_IsString(sinkVerJson)) { + compVer.sinkVersion = sinkVerJson->valuestring; } } @@ -125,12 +133,14 @@ void FromJson(const cJSON *jsonObject, VersionInfo &versionInfo) DHLOGE("Json pointer is nullptr!"); return; } - if (IsString(jsonObject, DEV_ID)) { - versionInfo.deviceId = cJSON_GetObjectItem(jsonObject, DEV_ID.c_str())->valuestring; + cJSON *devIdJson = cJSON_GetObjectItem(jsonObject, DEV_ID.c_str()); + if (devIdJson && cJSON_IsString(devIdJson)) { + versionInfo.deviceId = devIdJson->valuestring; } - if (IsString(jsonObject, DH_VER)) { - versionInfo.dhVersion = cJSON_GetObjectItem(jsonObject, DH_VER.c_str())->valuestring; + cJSON *dhVerJson = cJSON_GetObjectItem(jsonObject, DH_VER.c_str()); + if (dhVerJson && cJSON_IsString(dhVerJson)) { + versionInfo.dhVersion = dhVerJson->valuestring; } const cJSON *compVer = cJSON_GetObjectItem(jsonObject, COMP_VER.c_str()); diff --git a/services/distributedhardwarefwkservice/src/transport/dh_transport_obj.cpp b/services/distributedhardwarefwkservice/src/transport/dh_transport_obj.cpp index 3f40ca9a..ff1f683e 100644 --- a/services/distributedhardwarefwkservice/src/transport/dh_transport_obj.cpp +++ b/services/distributedhardwarefwkservice/src/transport/dh_transport_obj.cpp @@ -51,16 +51,19 @@ void FromJson(const cJSON *jsonObject, FullCapsRsp &capsRsp) DHLOGE("Json pointer is nullptr!"); return; } - std::string keyNetworkId(CAPS_RSP_NETWORKID_KEY); - if (IsString(jsonObject, keyNetworkId)) { - capsRsp.networkId = cJSON_GetObjectItem(jsonObject, CAPS_RSP_NETWORKID_KEY)->valuestring; + cJSON *capsRspJson = cJSON_GetObjectItem(jsonObject, CAPS_RSP_NETWORKID_KEY); + if (capsRspJson && cJSON_IsString(capsRspJson)) { + capsRsp.networkId = capsRspJson->valuestring; } - std::string keyCaps(CAPS_RSP_CAPS_KEY); - if (IsArray(jsonObject, keyCaps)) { - cJSON *capsArr = cJSON_GetObjectItem(jsonObject, CAPS_RSP_CAPS_KEY); - int32_t arrSize = cJSON_GetArraySize(capsArr); + + cJSON *capsRspKeyJson = cJSON_GetObjectItem(jsonObject, CAPS_RSP_CAPS_KEY); + if (capsRspKeyJson && cJSON_IsArray(capsRspKeyJson)) { + int32_t arrSize = cJSON_GetArraySize(capsRspKeyJson); for (int32_t i = 0; i < arrSize; i++) { - cJSON *cap = cJSON_GetArrayItem(capsArr, i); + cJSON *cap = cJSON_GetArrayItem(capsRspKeyJson, i); + if (cap == NULL) { + continue; + } std::shared_ptr capPtr = std::make_shared(); FromJson(cap, *capPtr); capsRsp.caps.push_back(capPtr); @@ -85,13 +88,14 @@ void FromJson(const cJSON *jsonObject, CommMsg &commMsg) DHLOGE("Json pointer is nullptr!"); return; } - std::string keyCode(COMM_MSG_CODE_KEY); - if (IsInt32(jsonObject, keyCode)) { - commMsg.code = cJSON_GetObjectItem(jsonObject, COMM_MSG_CODE_KEY)->valueint; + cJSON *commMsgCodeJson = cJSON_GetObjectItem(jsonObject, COMM_MSG_CODE_KEY); + if (commMsgCodeJson && cJSON_IsNumber(commMsgCodeJson)) { + commMsg.code = commMsgCodeJson->valueint; } - std::string keyMsg(COMM_MSG_MSG_KEY); - if (IsString(jsonObject, keyMsg)) { - commMsg.msg = cJSON_GetObjectItem(jsonObject, COMM_MSG_MSG_KEY)->valuestring; + + cJSON *commMsgeJson = cJSON_GetObjectItem(jsonObject, COMM_MSG_MSG_KEY); + if (commMsgeJson && cJSON_IsString(commMsgeJson)) { + commMsg.msg = commMsgeJson->valuestring; } } diff --git a/services/distributedhardwarefwkservice/src/utils/dh_context.cpp b/services/distributedhardwarefwkservice/src/utils/dh_context.cpp index 4ff2d633..71556359 100644 --- a/services/distributedhardwarefwkservice/src/utils/dh_context.cpp +++ b/services/distributedhardwarefwkservice/src/utils/dh_context.cpp @@ -324,13 +324,13 @@ void DHContext::DHFWKIsomerismListener::OnMessage(const DHTopic topic, const std return; } cJSON *eventObj = cJSON_GetObjectItemCaseSensitive(messageJson, ISOMERISM_EVENT_KEY.c_str()); - if (eventObj == nullptr || !IsString(messageJson, ISOMERISM_EVENT_KEY)) { + if (eventObj == nullptr || !cJSON_IsString(eventObj)) { cJSON_Delete(messageJson); DHLOGE("OnMessage event invaild"); return; } cJSON *devObj = cJSON_GetObjectItemCaseSensitive(messageJson, DEV_ID.c_str()); - if (devObj == nullptr || !IsString(messageJson, DEV_ID)) { + if (devObj == nullptr || !cJSON_IsString(devObj)) { cJSON_Delete(messageJson); DHLOGE("OnMessage deviceId invaild"); return; diff --git a/utils/include/dh_utils_tool.h b/utils/include/dh_utils_tool.h index 9fd96f06..d5b9ace9 100644 --- a/utils/include/dh_utils_tool.h +++ b/utils/include/dh_utils_tool.h @@ -47,20 +47,6 @@ std::string GetDeviceIdByUUID(const std::string &uuid); std::string Sha256(const std::string& string); -bool IsUInt8(const cJSON* jsonObj, const std::string& key); - -bool IsUInt16(const cJSON* jsonObj, const std::string& key); - -bool IsInt32(const cJSON* jsonObj, const std::string& key); - -bool IsUInt32(const cJSON* jsonObj, const std::string& key); - -bool IsBool(const cJSON* jsonObj, const std::string& key); - -bool IsString(const cJSON* jsonObj, const std::string& key); - -bool IsArray(const cJSON* jsonObj, const std::string& key); - std::string Compress(const std::string& data); std::string Decompress(const std::string& data); diff --git a/utils/src/dh_utils_tool.cpp b/utils/src/dh_utils_tool.cpp index 8d7b19aa..12aa546c 100644 --- a/utils/src/dh_utils_tool.cpp +++ b/utils/src/dh_utils_tool.cpp @@ -163,66 +163,6 @@ std::string GetLocalNetworkId() return info.networkId; } -bool IsUInt8(const cJSON* jsonObj, const std::string& key) -{ - const cJSON* value = cJSON_GetObjectItem(jsonObj, key.c_str()); - if (value == NULL || !cJSON_IsNumber(value)) { - return false; - } - return (value->valuedouble >= 0 && value->valuedouble <= UINT8_MAX); -} - -bool IsUInt16(const cJSON* jsonObj, const std::string& key) -{ - const cJSON* value = cJSON_GetObjectItem(jsonObj, key.c_str()); - if (value == NULL || !cJSON_IsNumber(value)) { - return false; - } - return (value->valuedouble >= 0 && value->valuedouble <= UINT16_MAX); -} - -bool IsInt32(const cJSON* jsonObj, const std::string& key) -{ - const cJSON* value = cJSON_GetObjectItem(jsonObj, key.c_str()); - if (value == NULL || !cJSON_IsNumber(value)) { - return false; - } - return (value->valuedouble >= INT32_MIN && value->valuedouble <= INT32_MAX); -} - -bool IsUInt32(const cJSON* jsonObj, const std::string& key) -{ - const cJSON* value = cJSON_GetObjectItem(jsonObj, key.c_str()); - if (value == NULL || !cJSON_IsNumber(value)) { - return false; - } - return (value->valuedouble >= 0 && value->valuedouble <= UINT32_MAX); -} - -bool IsBool(const cJSON* jsonObj, const std::string& key) -{ - const cJSON* value = cJSON_GetObjectItem(jsonObj, key.c_str()); - return (value != NULL && cJSON_IsBool(value)); -} - -bool IsString(const cJSON* jsonObj, const std::string& key) -{ - const cJSON* value = cJSON_GetObjectItem(jsonObj, key.c_str()); - if (value == NULL || !cJSON_IsString(value)) { - return false; - } - return (strlen(value->valuestring) > MIN_MESSAGE_LEN && strlen(value->valuestring) <= MAX_MESSAGE_LEN); -} - -bool IsArray(const cJSON* jsonObj, const std::string& key) -{ - const cJSON* value = cJSON_GetObjectItem(jsonObj, key.c_str()); - if (value == NULL || !cJSON_IsArray(value)) { - return false; - } - return ((uint32_t)cJSON_GetArraySize(value) >= 0 && (uint32_t)cJSON_GetArraySize(value) <= MAX_ARR_SIZE); -} - std::string Compress(const std::string& data) { z_stream strm; diff --git a/utils/src/histreamer_ability_parser.cpp b/utils/src/histreamer_ability_parser.cpp index e70920d0..48302e74 100644 --- a/utils/src/histreamer_ability_parser.cpp +++ b/utils/src/histreamer_ability_parser.cpp @@ -41,24 +41,22 @@ void FromJson(const cJSON *jsonObject, AudioEncoderIn &audioEncoderIn) DHLOGE("Json pointer is nullptr!"); return; } - if (!IsString(jsonObject, MIME)) { - DHLOGE("AudioEncoderIn MIME is invalid!\n"); - return; - } - audioEncoderIn.mime = cJSON_GetObjectItem(jsonObject, MIME.c_str())->valuestring; - if (!IsArray(jsonObject, SAMPLE_RATE)) { - DHLOGE("AudioEncoderIn SAMPLE_RATE is invalid\n"); + cJSON *mimeJsonObj = cJSON_GetObjectItem(jsonObject, MIME.c_str()); + if (mimeJsonObj == NULL || !cJSON_IsString(mimeJsonObj)) { + DHLOGE("AudioEncoderIn MIME is invalid!"); return; } + audioEncoderIn.mime = mimeJsonObj->valuestring; + cJSON *sampleRate = cJSON_GetObjectItem(jsonObject, SAMPLE_RATE.c_str()); - if (sampleRate == NULL) { - DHLOGE("AudioEncoderIn SAMPLE_RATE is invalid\n"); + if (sampleRate == NULL || !cJSON_IsArray(sampleRate)) { + DHLOGE("AudioEncoderIn SAMPLE_RATE is invalid!"); return; } cJSON *sampleRateItem = nullptr; cJSON_ArrayForEach(sampleRateItem, sampleRate) { if (sampleRateItem && sampleRateItem->type == cJSON_Number) { - audioEncoderIn.sample_rate.push_back((uint32_t)sampleRateItem->valuedouble); + audioEncoderIn.sample_rate.push_back(static_cast(sampleRateItem->valuedouble)); } } } @@ -69,30 +67,33 @@ void FromJson(const cJSON *jsonObject, AudioEncoderOut &audioEncoderOut) DHLOGE("Json pointer is nullptr!"); return; } - if (!IsString(jsonObject, MIME)) { - DHLOGE("AudioEncoderOut MIME is invalid!"); + cJSON *mimeJsonObj = cJSON_GetObjectItem(jsonObject, MIME.c_str()); + if (mimeJsonObj == NULL || !cJSON_IsString(mimeJsonObj)) { + DHLOGE("AudioEncoderIn MIME is invalid!"); return; } - audioEncoderOut.mime = cJSON_GetObjectItem(jsonObject, MIME.c_str())->valuestring; - if (!IsUInt32(jsonObject, AD_MPEG_VER)) { + audioEncoderOut.mime = mimeJsonObj->valuestring; + + cJSON *mpegVerJsonObj = cJSON_GetObjectItem(jsonObject, AD_MPEG_VER.c_str()); + if (mpegVerJsonObj == NULL || !cJSON_IsNumber(mpegVerJsonObj)) { DHLOGE("AudioEncoderOut AD_MPEG_VER is invalid!"); return; } - audioEncoderOut.ad_mpeg_ver = (uint32_t)cJSON_GetObjectItem(jsonObject, AD_MPEG_VER.c_str())->valuedouble; + audioEncoderOut.ad_mpeg_ver = static_cast(mpegVerJsonObj->valuedouble); - if (!IsUInt8(jsonObject, AUDIO_AAC_PROFILE)) { + cJSON *aacProfileJsonObj = cJSON_GetObjectItem(jsonObject, AUDIO_AAC_PROFILE.c_str()); + if (aacProfileJsonObj == NULL || !cJSON_IsNumber(aacProfileJsonObj)) { DHLOGE("AudioEncoderOut AUDIO_AAC_PROFILE is invalid!"); return; } - audioEncoderOut.aac_profile = - (AudioAacProfile)cJSON_GetObjectItem(jsonObject, AUDIO_AAC_PROFILE.c_str())->valuedouble; + audioEncoderOut.aac_profile = (AudioAacProfile)aacProfileJsonObj->valuedouble; - if (!IsUInt8(jsonObject, AUDIO_AAC_STREAM_FORMAT)) { + cJSON *aacStreamFmtJsonObj = cJSON_GetObjectItem(jsonObject, AUDIO_AAC_STREAM_FORMAT.c_str()); + if (aacStreamFmtJsonObj == NULL || !cJSON_IsNumber(aacStreamFmtJsonObj)) { DHLOGE("AudioEncoderOut AUDIO_AAC_STREAM_FORMAT is invalid!"); return; } - audioEncoderOut.aac_stm_fmt = - (AudioAacStreamFormat)cJSON_GetObjectItem(jsonObject, AUDIO_AAC_STREAM_FORMAT.c_str())->valuedouble; + audioEncoderOut.aac_stm_fmt = (AudioAacStreamFormat)aacStreamFmtJsonObj->valuedouble; } void FromJson(const cJSON *jsonObject, AudioEncoder &audioEncoder) @@ -101,17 +102,18 @@ void FromJson(const cJSON *jsonObject, AudioEncoder &audioEncoder) DHLOGE("Json pointer is nullptr!"); return; } - if (!IsString(jsonObject, NAME)) { + cJSON *nameJsonObj = cJSON_GetObjectItem(jsonObject, NAME.c_str()); + if (nameJsonObj == NULL || !cJSON_IsString(nameJsonObj)) { DHLOGE("AudioEncoder NAME is invalid!"); return; } - audioEncoder.name = cJSON_GetObjectItem(jsonObject, NAME.c_str())->valuestring; + audioEncoder.name = nameJsonObj->valuestring; - if (!IsArray(jsonObject, INS)) { + cJSON *insJson = cJSON_GetObjectItem(jsonObject, INS.c_str()); + if (insJson == NULL || !cJSON_IsArray(insJson)) { DHLOGE("AudioEncoder INS is invalid!"); return; } - cJSON *insJson = cJSON_GetObjectItem(jsonObject, INS.c_str()); cJSON *inJson = nullptr; cJSON_ArrayForEach(inJson, insJson) { AudioEncoderIn in; @@ -119,11 +121,11 @@ void FromJson(const cJSON *jsonObject, AudioEncoder &audioEncoder) audioEncoder.ins.push_back(in); } - if (!IsArray(jsonObject, OUTS)) { + cJSON *outsJson = cJSON_GetObjectItem(jsonObject, OUTS.c_str()); + if (outsJson == NULL || !cJSON_IsArray(outsJson)) { DHLOGE("AudioEncoder OUTS is invalid!"); return; } - cJSON *outsJson = cJSON_GetObjectItem(jsonObject, OUTS.c_str()); cJSON *outJson = nullptr; cJSON_ArrayForEach(outJson, outsJson) { AudioEncoderOut out; @@ -138,17 +140,18 @@ void FromJson(const cJSON *jsonObject, AudioDecoderIn &audioDecoderIn) DHLOGE("Json pointer is nullptr!"); return; } - if (!IsString(jsonObject, MIME)) { + cJSON *mimeJsonObj = cJSON_GetObjectItem(jsonObject, MIME.c_str()); + if (mimeJsonObj == NULL || !cJSON_IsString(mimeJsonObj)) { DHLOGE("AudioDecoderIn MIME is invalid!"); return; } - audioDecoderIn.mime = cJSON_GetObjectItem(jsonObject, MIME.c_str())->valuestring; + audioDecoderIn.mime = mimeJsonObj->valuestring; - if (!IsArray(jsonObject, AUDIO_CHANNEL_LAYOUT)) { + cJSON *channelLayoutJson = cJSON_GetObjectItem(jsonObject, AUDIO_CHANNEL_LAYOUT.c_str()); + if (channelLayoutJson == NULL || !cJSON_IsArray(channelLayoutJson)) { DHLOGE("AudioDecoder AUDIO_CHANNEL_LAYOUT is invalid!"); return; } - const cJSON *channelLayoutJson = cJSON_GetObjectItem(jsonObject, AUDIO_CHANNEL_LAYOUT.c_str()); const cJSON *layout = nullptr; cJSON_ArrayForEach(layout, channelLayoutJson) { if (layout && layout->type == cJSON_Number) { @@ -163,16 +166,18 @@ void FromJson(const cJSON *jsonObject, AudioDecoderOut &audioDecoderOut) DHLOGE("Json pointer is nullptr!"); return; } - if (!IsString(jsonObject, MIME)) { + cJSON *mimeJsonObj = cJSON_GetObjectItem(jsonObject, MIME.c_str()); + if (mimeJsonObj == NULL || !cJSON_IsString(mimeJsonObj)) { DHLOGE("AudioDecoderOut MIME is invalid!"); return; } - audioDecoderOut.mime = cJSON_GetObjectItem(jsonObject, MIME.c_str())->valuestring; - if (!IsArray(jsonObject, AUDIO_SAMPLE_FORMAT)) { + audioDecoderOut.mime = mimeJsonObj->valuestring; + + cJSON *sampleFormatJson = cJSON_GetObjectItem(jsonObject, AUDIO_SAMPLE_FORMAT.c_str()); + if (sampleFormatJson == NULL || !cJSON_IsArray(sampleFormatJson)) { DHLOGE("AudioDecoderOut AUDIO_SAMPLE_FORMAT is invalid!"); return; } - cJSON *sampleFormatJson = cJSON_GetObjectItem(jsonObject, AUDIO_SAMPLE_FORMAT.c_str()); cJSON *format = nullptr; cJSON_ArrayForEach(format, sampleFormatJson) { if (format && format->type == cJSON_Number) { @@ -187,28 +192,30 @@ void FromJson(const cJSON *jsonObject, AudioDecoder &audioDecoder) DHLOGE("Json pointer is nullptr!"); return; } - if (!IsString(jsonObject, NAME)) { + cJSON *nameJsonObj = cJSON_GetObjectItem(jsonObject, NAME.c_str()); + if (nameJsonObj == NULL || !cJSON_IsString(nameJsonObj)) { DHLOGE("AudioDecoderOut MIME is invalid!"); return; } - audioDecoder.name = cJSON_GetObjectItem(jsonObject, NAME.c_str())->valuestring; + audioDecoder.name = nameJsonObj->valuestring; - if (!IsArray(jsonObject, INS)) { - DHLOGE("AudioDecoder OUTS is invalid!"); + cJSON *insJson = cJSON_GetObjectItem(jsonObject, INS.c_str()); + if (insJson == NULL || !cJSON_IsArray(insJson)) { + DHLOGE("AudioDecoder INS is invalid!"); return; } - const cJSON *insJson = cJSON_GetObjectItem(jsonObject, INS.c_str()); cJSON *inJson = nullptr; cJSON_ArrayForEach(inJson, insJson) { AudioDecoderIn in; FromJson(inJson, in); audioDecoder.ins.push_back(in); } - if (!IsArray(jsonObject, OUTS)) { + + cJSON *outsJson = cJSON_GetObjectItem(jsonObject, OUTS.c_str()); + if (outsJson == NULL || !cJSON_IsArray(outsJson)) { DHLOGE("AudioDecoder OUTS is invalid!"); return; } - cJSON *outsJson = cJSON_GetObjectItem(jsonObject, OUTS.c_str()); cJSON *outJson = nullptr; cJSON_ArrayForEach(outJson, outsJson) { AudioDecoderOut out; @@ -223,17 +230,18 @@ void FromJson(const cJSON *jsonObject, VideoEncoderIn &videoEncoderIn) DHLOGE("Json pointer is nullptr!"); return; } - if (!IsString(jsonObject, MIME)) { + cJSON *mimeJsonObj = cJSON_GetObjectItem(jsonObject, MIME.c_str()); + if (mimeJsonObj == NULL || !cJSON_IsString(mimeJsonObj)) { DHLOGE("VideoEncoderIn MIME is invalid!"); return; } - videoEncoderIn.mime = cJSON_GetObjectItem(jsonObject, MIME.c_str())->valuestring; + videoEncoderIn.mime = mimeJsonObj->valuestring; - if (!IsArray(jsonObject, VIDEO_PIXEL_FMT)) { + cJSON *videoPixelFmt = cJSON_GetObjectItem(jsonObject, VIDEO_PIXEL_FMT.c_str()); + if (videoPixelFmt == NULL || !cJSON_IsArray(videoPixelFmt)) { DHLOGE("VideoEncoderIn VIDEO_PIXEL_FMT is invalid!"); return; } - cJSON *videoPixelFmt = cJSON_GetObjectItem(jsonObject, VIDEO_PIXEL_FMT.c_str()); cJSON *pixelFmt = nullptr; cJSON_ArrayForEach(pixelFmt, videoPixelFmt) { if (pixelFmt && pixelFmt->type == cJSON_Number) { @@ -248,11 +256,12 @@ void FromJson(const cJSON *jsonObject, VideoEncoderOut &videoEncoderOut) DHLOGE("Json pointer is nullptr!"); return; } - if (!IsString(jsonObject, MIME)) { - DHLOGE("VideoEncoderIn MIME is invalid!"); + cJSON *mimeJsonObj = cJSON_GetObjectItem(jsonObject, MIME.c_str()); + if (mimeJsonObj == NULL || !cJSON_IsString(mimeJsonObj)) { + DHLOGE("VideoEncoderOut MIME is invalid!"); return; } - videoEncoderOut.mime = cJSON_GetObjectItem(jsonObject, MIME.c_str())->valuestring; + videoEncoderOut.mime = mimeJsonObj->valuestring; } void FromJson(const cJSON *jsonObject, VideoEncoder &videoEncoder) @@ -261,17 +270,18 @@ void FromJson(const cJSON *jsonObject, VideoEncoder &videoEncoder) DHLOGE("Json pointer is nullptr!"); return; } - if (!IsString(jsonObject, NAME)) { + cJSON *nameJsonObj = cJSON_GetObjectItem(jsonObject, NAME.c_str()); + if (nameJsonObj == NULL || !cJSON_IsString(nameJsonObj)) { DHLOGE("VideoEncoder NAME is invalid!"); return; } - videoEncoder.name = cJSON_GetObjectItem(jsonObject, NAME.c_str())->valuestring; + videoEncoder.name = nameJsonObj->valuestring; - if (!IsArray(jsonObject, INS)) { + cJSON *videoEncoderInsJson = cJSON_GetObjectItem(jsonObject, INS.c_str()); + if (videoEncoderInsJson == NULL || !cJSON_IsArray(videoEncoderInsJson)) { DHLOGE("VideoEncoder INS is invalid!"); return; } - cJSON *videoEncoderInsJson = cJSON_GetObjectItem(jsonObject, INS.c_str()); cJSON *inJson = nullptr; cJSON_ArrayForEach(inJson, videoEncoderInsJson) { VideoEncoderIn in; @@ -279,11 +289,11 @@ void FromJson(const cJSON *jsonObject, VideoEncoder &videoEncoder) videoEncoder.ins.push_back(in); } - if (!IsArray(jsonObject, OUTS)) { + cJSON *videoEncoderOutsJson = cJSON_GetObjectItem(jsonObject, OUTS.c_str()); + if (videoEncoderOutsJson == NULL || !cJSON_IsArray(videoEncoderOutsJson)) { DHLOGE("VideoEncoder OUTS is invalid!"); return; } - cJSON *videoEncoderOutsJson = cJSON_GetObjectItem(jsonObject, OUTS.c_str()); cJSON *outJson = nullptr; cJSON_ArrayForEach(outJson, videoEncoderOutsJson) { VideoEncoderOut out; @@ -298,17 +308,18 @@ void FromJson(const cJSON *jsonObject, VideoDecoderIn &videoDecoderIn) DHLOGE("Json pointer is nullptr!"); return; } - if (!IsString(jsonObject, MIME)) { + cJSON *mimeJsonObj = cJSON_GetObjectItem(jsonObject, MIME.c_str()); + if (mimeJsonObj == NULL || !cJSON_IsString(mimeJsonObj)) { DHLOGE("VideoDecoderIn MIME is invalid!"); return; } - videoDecoderIn.mime = cJSON_GetObjectItem(jsonObject, MIME.c_str())->valuestring; + videoDecoderIn.mime = mimeJsonObj->valuestring; - if (!IsArray(jsonObject, VIDEO_BIT_STREAM_FMT)) { + cJSON *videoBitStreamFmtJson = cJSON_GetObjectItem(jsonObject, VIDEO_BIT_STREAM_FMT.c_str()); + if (videoBitStreamFmtJson == NULL || !cJSON_IsArray(videoBitStreamFmtJson)) { DHLOGE("VideoDecoderIn VIDEO_BIT_STREAM_FMT is invalid!"); return; } - cJSON *videoBitStreamFmtJson = cJSON_GetObjectItem(jsonObject, VIDEO_BIT_STREAM_FMT.c_str()); cJSON *fmt = nullptr; cJSON_ArrayForEach(fmt, videoBitStreamFmtJson) { if (fmt && fmt->type == cJSON_Number) { @@ -323,17 +334,18 @@ void FromJson(const cJSON *jsonObject, VideoDecoderOut &videoDecoderOut) DHLOGE("Json pointer is nullptr!"); return; } - if (!IsString(jsonObject, MIME)) { + cJSON *mimeJsonObj = cJSON_GetObjectItem(jsonObject, MIME.c_str()); + if (mimeJsonObj == NULL || !cJSON_IsString(mimeJsonObj)) { DHLOGE("VideoDecoderOut MIME is invalid!"); return; } - videoDecoderOut.mime = cJSON_GetObjectItem(jsonObject, MIME.c_str())->valuestring; + videoDecoderOut.mime = mimeJsonObj->valuestring; - if (!IsArray(jsonObject, VIDEO_PIXEL_FMT)) { + cJSON *videoPixelFmtJson = cJSON_GetObjectItem(jsonObject, VIDEO_PIXEL_FMT.c_str()); + if (videoPixelFmtJson == NULL || !cJSON_IsArray(videoPixelFmtJson)) { DHLOGE("videoDecoderOut VIDEO_PIXEL_FMT is invalid!"); return; } - cJSON *videoPixelFmtJson = cJSON_GetObjectItem(jsonObject, VIDEO_PIXEL_FMT.c_str()); cJSON *fmt = nullptr; cJSON_ArrayForEach(fmt, videoPixelFmtJson) { if (fmt && fmt->type == cJSON_Number) { @@ -348,17 +360,18 @@ void FromJson(const cJSON *jsonObject, VideoDecoder &videoDecoder) DHLOGE("Json pointer is nullptr!"); return; } - if (!IsString(jsonObject, NAME)) { + cJSON *nameJsonObj = cJSON_GetObjectItem(jsonObject, NAME.c_str()); + if (nameJsonObj == NULL || !cJSON_IsString(nameJsonObj)) { DHLOGE("VideoDecoder NAME is invalid!"); return; } - videoDecoder.name = cJSON_GetObjectItem(jsonObject, NAME.c_str())->valuestring; + videoDecoder.name = nameJsonObj->valuestring; - if (!IsArray(jsonObject, INS)) { + cJSON *videoDecoderInsJson = cJSON_GetObjectItem(jsonObject, INS.c_str()); + if (videoDecoderInsJson == NULL || !cJSON_IsArray(videoDecoderInsJson)) { DHLOGE("VideoDecoder INS is invalid!"); return; } - cJSON *videoDecoderInsJson = cJSON_GetObjectItem(jsonObject, INS.c_str()); cJSON *inJson = nullptr; cJSON_ArrayForEach(inJson, videoDecoderInsJson) { VideoDecoderIn in; @@ -366,11 +379,11 @@ void FromJson(const cJSON *jsonObject, VideoDecoder &videoDecoder) videoDecoder.ins.push_back(in); } - if (!IsArray(jsonObject, OUTS)) { + cJSON *videoDecoderOutsJson = cJSON_GetObjectItem(jsonObject, OUTS.c_str()); + if (videoDecoderOutsJson == NULL || !cJSON_IsArray(videoDecoderOutsJson)) { DHLOGE("VideoDecoder OUTS is invalid!"); return; } - cJSON *videoDecoderOutsJson = cJSON_GetObjectItem(jsonObject, OUTS.c_str()); cJSON *outJson = nullptr; cJSON_ArrayForEach(outJson, videoDecoderOutsJson) { VideoDecoderOut out; diff --git a/utils/test/unittest/common/utilstool/utils_tool_test.cpp b/utils/test/unittest/common/utilstool/utils_tool_test.cpp index 7b589c4c..bc0865d2 100644 --- a/utils/test/unittest/common/utilstool/utils_tool_test.cpp +++ b/utils/test/unittest/common/utilstool/utils_tool_test.cpp @@ -147,102 +147,6 @@ HWTEST_F(UtilsToolTest, GetDeviceIdByUUID_002, TestSize.Level0) EXPECT_NE(0, ret.size()); } -HWTEST_F(UtilsToolTest, IsUInt8_001, TestSize.Level0) -{ - cJSON* jsonObj = cJSON_CreateObject(); - ASSERT_TRUE(jsonObj != nullptr); - const std::string key = "int8_key"; - cJSON_AddStringToObject(jsonObj, key.c_str(), "int8_key_test"); - - std::string inputKey = "key"; - auto ret = IsUInt8(jsonObj, key); - cJSON_Delete(jsonObj); - EXPECT_EQ(false, ret); - - cJSON* jsonObj1 = cJSON_CreateObject(); - ASSERT_TRUE(jsonObj1 != nullptr); - cJSON_AddStringToObject(jsonObj1, key.c_str(), "int8_key_test"); - ret = IsUInt8(jsonObj1, key); - cJSON_Delete(jsonObj1); - EXPECT_EQ(false, ret); -} - -HWTEST_F(UtilsToolTest, IsUInt8_002, TestSize.Level0) -{ - cJSON* jsonObj = cJSON_CreateObject(); - ASSERT_TRUE(jsonObj != nullptr); - const std::string key = "int8_key"; - const uint8_t keyVaule = 1; - cJSON_AddNumberToObject(jsonObj, key.c_str(), keyVaule); - auto ret = IsUInt8(jsonObj, key); - cJSON_Delete(jsonObj); - EXPECT_EQ(true, ret); -} - -HWTEST_F(UtilsToolTest, IsUInt16_001, TestSize.Level0) -{ - cJSON* jsonObj = cJSON_CreateObject(); - ASSERT_TRUE(jsonObj != nullptr); - const std::string key = "uint16_key"; - cJSON_AddStringToObject(jsonObj, key.c_str(), "uint16_key_test"); - - std::string inputKey = "key"; - auto ret = IsUInt16(jsonObj, key); - cJSON_Delete(jsonObj); - EXPECT_EQ(false, ret); - - cJSON* jsonObj1 = cJSON_CreateObject(); - ASSERT_TRUE(jsonObj1 != nullptr); - cJSON_AddStringToObject(jsonObj1, key.c_str(), "uint16_key_test"); - ret = IsUInt16(jsonObj1, key); - cJSON_Delete(jsonObj1); - EXPECT_EQ(false, ret); -} - -HWTEST_F(UtilsToolTest, IsUInt16_002, TestSize.Level0) -{ - cJSON* jsonObj = cJSON_CreateObject(); - ASSERT_TRUE(jsonObj != nullptr); - const std::string key = "uint16_key"; - const uint16_t keyVaule = 1; - cJSON_AddNumberToObject(jsonObj, key.c_str(), keyVaule); - auto ret = IsUInt16(jsonObj, key); - cJSON_Delete(jsonObj); - EXPECT_EQ(true, ret); -} - -HWTEST_F(UtilsToolTest, IsUInt32_001, TestSize.Level0) -{ - cJSON* jsonObj = cJSON_CreateObject(); - ASSERT_TRUE(jsonObj != nullptr); - const std::string key = "uint32_key"; - cJSON_AddStringToObject(jsonObj, key.c_str(), "uint32_key_test"); - - std::string inputKey = "key"; - auto ret = IsUInt32(jsonObj, key); - cJSON_Delete(jsonObj); - EXPECT_EQ(false, ret); - - cJSON* jsonObj1 = cJSON_CreateObject(); - ASSERT_TRUE(jsonObj1 != nullptr); - cJSON_AddStringToObject(jsonObj1, key.c_str(), "uint32_key_test"); - ret = IsUInt32(jsonObj1, key); - cJSON_Delete(jsonObj1); - EXPECT_EQ(false, ret); -} - -HWTEST_F(UtilsToolTest, IsUInt32_002, TestSize.Level0) -{ - cJSON* jsonObj = cJSON_CreateObject(); - ASSERT_TRUE(jsonObj != nullptr); - const std::string key = "uint32_key"; - const uint32_t keyVaule = 1; - cJSON_AddNumberToObject(jsonObj, key.c_str(), keyVaule); - auto ret = IsUInt32(jsonObj, key); - cJSON_Delete(jsonObj); - EXPECT_EQ(true, ret); -} - HWTEST_F(UtilsToolTest, GetSysPara_001, TestSize.Level0) { char *key = nullptr; -- Gitee