• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "dm_jsonstr_handle.h"
17 
18 #include "json_object.h"
19 #include "dm_anonymous.h"
20 #include "dm_error_type.h"
21 #include "dm_log.h"
22 
23 const char* TAG_PROXY = "proxy";
24 const char* TAG_PEER_BUNDLE_NAME = "peerBundleName";
25 const char* TAG_PEER_TOKENID = "peerTokenId";
26 
27 namespace OHOS {
28 namespace DistributedHardware {
29 DM_IMPLEMENT_SINGLE_INSTANCE(JsonStrHandle);
30 
GetPeerAppInfoParseExtra(const std::string & extra,uint64_t & peerTokenId,std::string & peerBundleName)31 DM_EXPORT void JsonStrHandle::GetPeerAppInfoParseExtra(const std::string &extra,
32     uint64_t &peerTokenId, std::string &peerBundleName)
33 {
34     JsonObject extraInfoJson(extra);
35     if (extraInfoJson.IsDiscarded()) {
36         LOGE("ParseExtra extraInfoJson error");
37         return;
38     }
39     if (IsString(extraInfoJson, TAG_PEER_BUNDLE_NAME)) {
40         peerBundleName = extraInfoJson[TAG_PEER_BUNDLE_NAME].Get<std::string>();
41     }
42     if (IsUint64(extraInfoJson, TAG_PEER_TOKENID)) {
43         peerTokenId = extraInfoJson[TAG_PEER_TOKENID].Get<uint64_t>();
44     }
45 }
46 
GetProxyTokenIdByExtra(const std::string & extraInfo)47 DM_EXPORT std::vector<int64_t> JsonStrHandle::GetProxyTokenIdByExtra(const std::string &extraInfo)
48 {
49     std::vector<int64_t> tokenIdVec;
50     JsonObject extraInfoJson(extraInfo);
51     if (extraInfoJson.IsDiscarded() || !IsString(extraInfoJson, TAG_PROXY)) {
52         LOGE("GetProxyTokenIdByExtra extraInfoJson error");
53         return tokenIdVec;
54     }
55     std::string proxyListStr = extraInfoJson[TAG_PROXY].Get<std::string>();
56     JsonObject proxyList;
57     if (!proxyList.Parse(proxyListStr)) {
58         LOGE("GetProxyTokenIdByExtra proxyListStr Parse error");
59         return tokenIdVec;
60     }
61     for (auto &item : proxyList.Items()) {
62         if (item.IsNumber()) {
63             tokenIdVec.push_back(item.Get<int64_t>());
64         }
65     }
66     return tokenIdVec;
67 }
68 } // namespace DistributedHardware
69 } // namespace OHOS
70