diff --git a/services/distributedhardwarefwkservice/src/componentloader/component_loader.cpp b/services/distributedhardwarefwkservice/src/componentloader/component_loader.cpp index 145f2e27a4bd80ff4e5d8058d9f6c9373cbf5c33..6f99cb37aa071c82bf277362721b83764dee8143 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 (!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 (!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 (!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 (!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 (!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 (!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 (!IsInt32(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 (!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 (!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 (!IsInt32(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 (!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 (!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 (!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 (!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 (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 (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 (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 (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 (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 (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 (IsInt32(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 (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 (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 (IsInt32(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 (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 (!IsBool(sensitive)) { + DHLOGE("COMP_SUBTYPE is invalid!"); + return; + } if (cJSON_IsTrue(sensitive)) { sensitiveValue = true; } else { sensitiveValue = false; } ResourceDesc resource; - if (!IsString(resourceDesc, COMP_SUBTYPE)) { + cJSON *subtypeJson = cJSON_GetObjectItem(resourceDesc, COMP_SUBTYPE.c_str()); + if (!IsString(subtypeJson)) { DHLOGE("COMP_SUBTYPE is invalid!"); return; } - resource.subtype = cJSON_GetObjectItem(resourceDesc, COMP_SUBTYPE.c_str())->valuestring; + resource.subtype = subtypeJson->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 8a370b3b4857bf704198c282a6a906b14467b1c1..98c2c460492c135fdeb83387becb57f5a870c633 100644 --- a/services/distributedhardwarefwkservice/src/componentmanager/component_privacy.cpp +++ b/services/distributedhardwarefwkservice/src/componentmanager/component_privacy.cpp @@ -295,47 +295,41 @@ 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)) { + cJSON *subtypeJson = cJSON_GetObjectItem(innerMsg, PRIVACY_SUBTYPE.c_str()); + if (!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 *networkIdJson = cJSON_GetObjectItem(innerMsg, PRIVACY_NETWORKID.c_str()); + if (!IsString(networkIdJson)) { DHLOGE("PRIVACY_NETWORKID is invalid!"); return; } + std::string networkId = networkIdJson->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)) { DHLOGE("PRIVACY_SUBTYPE is invalid!"); return; } + std::string subtype = cJSON_GetObjectItem(innerMsg, PRIVACY_SUBTYPE.c_str())->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 7c6c246697813cca09bd675a5c561c721f388a6f..ff88dc9a3193af2f10e80558fa544e604e4976ce 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp +++ b/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp @@ -237,12 +237,13 @@ std::string DistributedHardwareService::QueryDhSysSpec(const std::string &target DHLOGE("attrs json is invalid, attrs: %{public}s", attrs.c_str()); return ""; } - if (!IsString(attrJson, targetKey)) { + cJSON *targetKeyJson = cJSON_GetObjectItem(attrJson, targetKey.c_str()); + if (!IsString(targetKeyJson)) { DHLOGE("Attrs Json not contains key: %{public}s", targetKey.c_str()); 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 d49b50e83ace610edabdc25c68219d1f9198299c..b6ba4ac3576ae6a6b6b2463de845790913a6b9ae 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 (!IsUInt32(dhTypeJson)) { DHLOGE("The DH_TYPE key is invalid!"); cJSON_Delete(jsonObj); return; } - if (!IsBool(jsonObj, LOW_LATENCY_ENABLE)) { + + cJSON *enableJson = cJSON_GetObjectItem(jsonObj, LOW_LATENCY_ENABLE.c_str()); + if (!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)) { + DHType dhType = (DHType)dhTypeJson->valuedouble; + 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 3f1994cacf0d939f950ad570eb8155a22be0e213..c29a7b5fa168811d9842951d8d7a57c9a337d0ed 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/capability_info.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/capability_info.cpp @@ -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 (!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 (!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 (!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 (!IsUInt16(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 (!IsUInt32(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 (!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 (!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 fc08bc6207c82f93211c2ada116694afe6668037..b5bce33233400c640cd23c6a8f69f26ac31a563d 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 (!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 (!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 (!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 (!IsUInt16(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 (!IsUInt32(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 (!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 (!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 (!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 (!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 8f8b2a74d87a8b7275d544522ebf22e7c762f7a4..dbfbd65a94888c7a342b68eb7512182eb72d1e56 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/version_info.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/version_info.cpp @@ -101,21 +101,25 @@ 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 (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 (IsUInt32(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 (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 (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 (IsString(sinkVerJson)) { + compVer.sinkVersion = sinkVerJson->valuestring; } } @@ -125,12 +129,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 (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 (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 3f40ca9add6a05ff881670a4c1efbcbea0745774..4535079b7800572bc3f2dd4da730514057b0766c 100644 --- a/services/distributedhardwarefwkservice/src/transport/dh_transport_obj.cpp +++ b/services/distributedhardwarefwkservice/src/transport/dh_transport_obj.cpp @@ -51,16 +51,15 @@ 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 (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 (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); std::shared_ptr capPtr = std::make_shared(); FromJson(cap, *capPtr); capsRsp.caps.push_back(capPtr); @@ -85,13 +84,13 @@ 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 (IsInt32(commMsgCodeJson)) { + commMsg.code = commMsgCodeJson->valueint; } - std::string keyMsg(COMM_MSG_MSG_KEY); + cJSON *commMsgeJson = cJSON_GetObjectItem(jsonObject, COMM_MSG_MSG_KEY); if (IsString(jsonObject, keyMsg)) { - commMsg.msg = cJSON_GetObjectItem(jsonObject, COMM_MSG_MSG_KEY)->valuestring; + commMsg.msg = commMsgeJson->valuestring; } } diff --git a/services/distributedhardwarefwkservice/src/utils/dh_context.cpp b/services/distributedhardwarefwkservice/src/utils/dh_context.cpp index 4ff2d633a890d85cce143b251bd56fe0db9f3745..b02b2f9b541ae7008e0b073ebdbcc93b422ec33b 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 (!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 (!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 9fd96f0632d0fcbe3907d1f43abb64f49fd003d8..333cceda11d1e3bde190ff57d2c142dce7a4bfdb 100644 --- a/utils/include/dh_utils_tool.h +++ b/utils/include/dh_utils_tool.h @@ -47,19 +47,19 @@ std::string GetDeviceIdByUUID(const std::string &uuid); std::string Sha256(const std::string& string); -bool IsUInt8(const cJSON* jsonObj, const std::string& key); +bool IsUInt8(const cJSON* jsonObj); -bool IsUInt16(const cJSON* jsonObj, const std::string& key); +bool IsUInt16(const cJSON* jsonObj); -bool IsInt32(const cJSON* jsonObj, const std::string& key); +bool IsInt32(const cJSON* jsonObj); -bool IsUInt32(const cJSON* jsonObj, const std::string& key); +bool IsUInt32(const cJSON* jsonObj); -bool IsBool(const cJSON* jsonObj, const std::string& key); +bool IsBool(const cJSON* jsonObj); -bool IsString(const cJSON* jsonObj, const std::string& key); +bool IsString(const cJSON* jsonObj); -bool IsArray(const cJSON* jsonObj, const std::string& key); +bool IsArray(const cJSON* jsonObj); 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 8d7b19aa2aa71285a773bd8a990d1871a76f4766..1f9ad4847a00e7dfa6e01eb57d0c495ac9b062b3 100644 --- a/utils/src/dh_utils_tool.cpp +++ b/utils/src/dh_utils_tool.cpp @@ -163,64 +163,57 @@ std::string GetLocalNetworkId() return info.networkId; } -bool IsUInt8(const cJSON* jsonObj, const std::string& key) +bool IsUInt8(const cJSON* jsonObj) { - const cJSON* value = cJSON_GetObjectItem(jsonObj, key.c_str()); - if (value == NULL || !cJSON_IsNumber(value)) { + if (jsonObj == NULL || !cJSON_IsNumber(jsonObj)) { return false; } - return (value->valuedouble >= 0 && value->valuedouble <= UINT8_MAX); + return (jsonObj->valuedouble >= 0 && jsonObj->valuedouble <= UINT8_MAX); } -bool IsUInt16(const cJSON* jsonObj, const std::string& key) +bool IsUInt16(const cJSON* jsonObj) { - const cJSON* value = cJSON_GetObjectItem(jsonObj, key.c_str()); - if (value == NULL || !cJSON_IsNumber(value)) { + if (jsonObj == NULL || !cJSON_IsNumber(jsonObj)) { return false; } - return (value->valuedouble >= 0 && value->valuedouble <= UINT16_MAX); + return (jsonObj->valuedouble >= 0 && jsonObj->valuedouble <= UINT16_MAX); } -bool IsInt32(const cJSON* jsonObj, const std::string& key) +bool IsInt32(const cJSON* jsonObj) { - const cJSON* value = cJSON_GetObjectItem(jsonObj, key.c_str()); - if (value == NULL || !cJSON_IsNumber(value)) { + if (jsonObj == NULL || !cJSON_IsNumber(jsonObj)) { return false; } - return (value->valuedouble >= INT32_MIN && value->valuedouble <= INT32_MAX); + return (jsonObj->valuedouble >= INT32_MIN && jsonObj->valuedouble <= INT32_MAX); } -bool IsUInt32(const cJSON* jsonObj, const std::string& key) +bool IsUInt32(const cJSON* jsonObj) { - const cJSON* value = cJSON_GetObjectItem(jsonObj, key.c_str()); - if (value == NULL || !cJSON_IsNumber(value)) { + if (jsonObj == NULL || !cJSON_IsNumber(jsonObj)) { return false; } - return (value->valuedouble >= 0 && value->valuedouble <= UINT32_MAX); + return (jsonObj->valuedouble >= 0 && jsonObj->valuedouble <= UINT32_MAX); } -bool IsBool(const cJSON* jsonObj, const std::string& key) +bool IsBool(const cJSON* jsonObj) { - const cJSON* value = cJSON_GetObjectItem(jsonObj, key.c_str()); - return (value != NULL && cJSON_IsBool(value)); + return (jsonObj != NULL && cJSON_IsBool(jsonObj)); } -bool IsString(const cJSON* jsonObj, const std::string& key) +bool IsString(const cJSON* jsonObj) { - const cJSON* value = cJSON_GetObjectItem(jsonObj, key.c_str()); - if (value == NULL || !cJSON_IsString(value)) { + if (jsonObj == NULL || !cJSON_IsString(jsonObj)) { return false; } - return (strlen(value->valuestring) > MIN_MESSAGE_LEN && strlen(value->valuestring) <= MAX_MESSAGE_LEN); + return (strlen(jsonObj->valuestring) > MIN_MESSAGE_LEN && strlen(jsonObj->valuestring) <= MAX_MESSAGE_LEN); } -bool IsArray(const cJSON* jsonObj, const std::string& key) +bool IsArray(const cJSON* jsonObj) { - const cJSON* value = cJSON_GetObjectItem(jsonObj, key.c_str()); - if (value == NULL || !cJSON_IsArray(value)) { + if (jsonObj == NULL || !cJSON_IsArray(jsonObj)) { return false; } - return ((uint32_t)cJSON_GetArraySize(value) >= 0 && (uint32_t)cJSON_GetArraySize(value) <= MAX_ARR_SIZE); + return ((uint32_t)cJSON_GetArraySize(jsonObj) >= 0 && (uint32_t)cJSON_GetArraySize(jsonObj) <= MAX_ARR_SIZE); } std::string Compress(const std::string& data) diff --git a/utils/src/histreamer_ability_parser.cpp b/utils/src/histreamer_ability_parser.cpp index e70920d0b7919f8c39d26c01f1a8486513eec107..732a83a71504b5b37c4672671be8d7d46f3d1970 100644 --- a/utils/src/histreamer_ability_parser.cpp +++ b/utils/src/histreamer_ability_parser.cpp @@ -41,18 +41,16 @@ 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 (!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 (!IsArray(sampleRate)) { + DHLOGE("AudioEncoderIn SAMPLE_RATE is invalid!"); return; } cJSON *sampleRateItem = nullptr; @@ -69,30 +67,33 @@ void FromJson(const cJSON *jsonObject, AudioEncoderOut &audioEncoderOut) DHLOGE("Json pointer is nullptr!"); return; } - if (!IsString(jsonObject, MIME)) { + cJSON *mimeJsonObj = cJSON_GetObjectItem(jsonObject, MIME.c_str()); + if (!IsString(mimeJsonObj)) { DHLOGE("AudioEncoderOut 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 (!IsUInt32(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 (!IsUInt8(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 (!IsUInt8(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 (!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 (!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 (!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 (!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 (!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 (!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 (!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 (!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 (!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 (!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 (!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 (!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 (!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 (!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 (!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 (!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 (!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 (!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 (!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 (!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 (!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 (!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 (!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 7b589c4c721b6a7b9a27946c19a0c80c1ecce66b..bc0865d23d6ef72c5fcf3a54e4b6493d3340c511 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;