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_codec.h"
17 #include "device_manager_log.h"
18 #include "msg_head.h"
19
20 #include "constants.h"
21 #include "device_manager_log.h"
22 #include "device_manager_errno.h"
23
24 namespace OHOS {
25 namespace DistributedHardware {
DecodeMsgType(std::string & jsonStr)26 int32_t MsgCodec::DecodeMsgType(std::string &jsonStr)
27 {
28 nlohmann::json jsonObject = nlohmann::json::parse(jsonStr, nullptr, false);
29 if (jsonObject.is_discarded()) {
30 DMLOG(DM_LOG_ERROR, "DecodeMsgType jsonStr error");
31 return DmMsgType::MSG_TYPE_UNKNOWN;
32 }
33 MsgHead mMsgHead;
34 auto msgHeadPtr = mMsgHead.Decode(jsonObject);
35 if (msgHeadPtr == nullptr) {
36 DMLOG(DM_LOG_ERROR, "mMsgHead decode error");
37 return DEVICEMANAGER_NULLPTR;
38 }
39 return msgHeadPtr->GetMsgType();
40 }
41
EncodeSyncGroup(std::vector<std::string> & groupIdList,std::string & deviceId)42 std::string MsgCodec::EncodeSyncGroup(std::vector<std::string> &groupIdList, std::string &deviceId)
43 {
44 nlohmann::json jsonObject;
45 MsgSyncGroup mMsgSyncGroup(groupIdList, deviceId);
46 mMsgSyncGroup.Encode(jsonObject);
47 return jsonObject.dump();
48 }
49
EncodeReqAppAuth(std::string & token,std::string hostPkg,std::string targetPkg,const DmDeviceInfo & devReqInfo,const DmAppImageInfo & imageInfo,std::string & extras)50 std::vector<std::string> MsgCodec::EncodeReqAppAuth(std::string &token, std::string hostPkg, std::string targetPkg,
51 const DmDeviceInfo &devReqInfo, const DmAppImageInfo &imageInfo, std::string &extras)
52 {
53 MsgRequestAuth mMsgRequestAuth(token, hostPkg, targetPkg, devReqInfo, imageInfo, extras);
54 return mMsgRequestAuth.Encode();
55 }
56
EncodeAcceptRespAuth(int32_t reply,int64_t requestId,std::string & groupId,std::string & groupName,std::string & reqDeviceId)57 std::string MsgCodec::EncodeAcceptRespAuth(int32_t reply, int64_t requestId, std::string &groupId,
58 std::string &groupName, std::string &reqDeviceId)
59 {
60 nlohmann::json jsonObject;
61 MsgResponseAuth mMsgResponseAuth(reply, requestId, groupId, groupName, reqDeviceId);
62 mMsgResponseAuth.Encode(jsonObject);
63 return jsonObject.dump();
64 }
65
EncodeRefuseRespAuth(int32_t reply,std::string & reqDeviceId)66 std::string MsgCodec::EncodeRefuseRespAuth(int32_t reply, std::string &reqDeviceId)
67 {
68 nlohmann::json jsonObject;
69 MsgResponseAuth mMsgResponseAuth(reply, reqDeviceId);
70 mMsgResponseAuth.Encode(jsonObject);
71 return jsonObject.dump();
72 }
73
DecodeRequestAuth(std::string & jsonStr,std::shared_ptr<MsgRequestAuth> msgRequestAuth)74 std::shared_ptr<MsgRequestAuth> MsgCodec::DecodeRequestAuth(std::string &jsonStr,
75 std::shared_ptr<MsgRequestAuth> msgRequestAuth)
76 {
77 nlohmann::json jsonObject = nlohmann::json::parse(jsonStr, nullptr, false);
78 if (jsonObject.is_discarded()) {
79 DMLOG(DM_LOG_ERROR, "DecodeRequestAuth jsonStr error");
80 return nullptr;
81 }
82 return MsgRequestAuth::Decode(jsonObject, msgRequestAuth);
83 }
84
DecodeResponseAuth(std::string & jsonStr)85 std::shared_ptr<MsgResponseAuth> MsgCodec::DecodeResponseAuth(std::string &jsonStr)
86 {
87 nlohmann::json jsonObject = nlohmann::json::parse(jsonStr, nullptr, false);
88 if (jsonObject.is_discarded()) {
89 DMLOG(DM_LOG_ERROR, "DecodeResponseAuth jsonStr error");
90 return nullptr;
91 }
92 std::shared_ptr<MsgResponseAuth> msgResponseAuthPtr = std::make_shared<MsgResponseAuth>();
93 if (msgResponseAuthPtr->Decode(jsonObject) == 0) {
94 return msgResponseAuthPtr;
95 }
96 return nullptr;
97 }
98 }
99 }
100