1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "msg_head.h" 17 #include "device_manager_log.h" 18 #include "constants.h" 19 20 namespace OHOS { 21 namespace DistributedHardware { Encode(nlohmann::json & json)22void MsgHead::Encode(nlohmann::json &json) 23 { 24 json[TAG_VER] = DM_ITF_VER; 25 json[TAG_TYPE] = mMsgType_; 26 } 27 Decode(nlohmann::json & json)28std::shared_ptr<MsgHead> MsgHead::Decode(nlohmann::json &json) 29 { 30 if (json.contains(TAG_TYPE) == false || json.contains(TAG_VER) == false) { 31 DMLOG(DM_LOG_ERROR, "err json string"); 32 return nullptr; 33 } 34 35 auto msgHeadPtr = std::make_shared<MsgHead>(); 36 msgHeadPtr->mItfVer_ = json[TAG_VER]; 37 38 if (DM_ITF_VER.compare(msgHeadPtr->mItfVer_) == 0) { 39 msgHeadPtr->mMsgType_ = json[TAG_TYPE]; 40 } else { 41 DMLOG(DM_LOG_ERROR, "msg head version mismatch"); 42 msgHeadPtr->mMsgType_ = DmMsgType::MSG_TYPE_UNKNOWN; 43 } 44 return msgHeadPtr; 45 } 46 GetMsgType()47int32_t MsgHead::GetMsgType() 48 { 49 return mMsgType_; 50 } 51 } 52 }