1 /*
2  * Copyright (c) 2023 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 "av_trans_message.h"
17 
18 #include "av_trans_constants.h"
19 
20 namespace OHOS {
21 namespace DistributedHardware {
22 const std::string KEY_TYPE = "type";
23 const std::string KEY_CONTENT = "content";
24 const std::string KEY_DST_DEVID = "dstDevId";
25 
AVTransMessage()26 AVTransMessage::AVTransMessage()
27 {
28     type_ = 0;
29 }
30 
AVTransMessage(uint32_t type,std::string content,std::string dstDevId)31 AVTransMessage::AVTransMessage(uint32_t type, std::string content, std::string dstDevId)
32     : type_(type), content_(content), dstDevId_(dstDevId)
33 {
34 }
35 
~AVTransMessage()36 AVTransMessage::~AVTransMessage()
37 {
38 }
39 
MarshalMessage()40 std::string AVTransMessage::MarshalMessage()
41 {
42     cJSON *msgJson = cJSON_CreateObject();
43     if (msgJson == nullptr) {
44         return "";
45     }
46     cJSON_AddNumberToObject(msgJson, KEY_TYPE.c_str(), type_);
47     cJSON_AddStringToObject(msgJson, KEY_CONTENT.c_str(), content_.c_str());
48     cJSON_AddStringToObject(msgJson, KEY_DST_DEVID.c_str(), dstDevId_.c_str());
49     char *data = cJSON_PrintUnformatted(msgJson);
50     if (data == nullptr) {
51         cJSON_Delete(msgJson);
52         return "";
53     }
54     std::string jsonstr(data);
55     cJSON_free(data);
56     cJSON_Delete(msgJson);
57     return jsonstr;
58 }
59 
UnmarshalMessage(const std::string & jsonStr,const std::string & peerDevId)60 bool AVTransMessage::UnmarshalMessage(const std::string& jsonStr, const std::string &peerDevId)
61 {
62     cJSON *metaJson = cJSON_Parse(jsonStr.c_str());
63     if (metaJson == nullptr) {
64         return false;
65     }
66     if (!IsUInt32(metaJson, KEY_TYPE) || !IsString(metaJson, KEY_CONTENT)) {
67         cJSON_Delete(metaJson);
68         return false;
69     }
70     cJSON *typeObj = cJSON_GetObjectItemCaseSensitive(metaJson, KEY_TYPE.c_str());
71     if (typeObj == nullptr || !cJSON_IsNumber(typeObj)) {
72         cJSON_Delete(metaJson);
73         return false;
74     }
75 
76     type_ = static_cast<uint32_t>(typeObj->valueint);
77     cJSON *contentObj = cJSON_GetObjectItemCaseSensitive(metaJson, KEY_CONTENT.c_str());
78     if (contentObj == nullptr || !cJSON_IsString(contentObj)) {
79         cJSON_Delete(metaJson);
80         return false;
81     }
82     content_ = contentObj->valuestring;
83     dstDevId_ = peerDevId;
84     cJSON_Delete(metaJson);
85     return true;
86 }
87 
IsUInt32(const cJSON * msgJson,const std::string & key)88 bool AVTransMessage::IsUInt32(const cJSON *msgJson, const std::string &key)
89 {
90     cJSON *keyObj = cJSON_GetObjectItemCaseSensitive(msgJson, key.c_str());
91     return (keyObj != nullptr) && cJSON_IsNumber(keyObj) &&
92         static_cast<uint32_t>(keyObj->valueint) <= UINT32_MAX;
93 }
94 
IsString(const cJSON * msgJson,const std::string & key)95 bool AVTransMessage::IsString(const cJSON *msgJson, const std::string &key)
96 {
97     cJSON *keyObj = cJSON_GetObjectItemCaseSensitive(msgJson, key.c_str());
98     return (keyObj != nullptr) && cJSON_IsString(keyObj) &&
99         strlen(cJSON_GetStringValue(keyObj)) <= MAX_MESSAGES_LEN;
100 }
101 } // namespace DistributedHardware
102 } // namespace OHOS