• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "distributed_hardware_proxy.h"
17 
18 #include <unordered_set>
19 
20 #include "anonymous_string.h"
21 #include "constants.h"
22 #include "distributed_hardware_errno.h"
23 #include "distributed_hardware_log.h"
24 #include "nlohmann/json.hpp"
25 #include "parcel.h"
26 
27 namespace OHOS {
28 namespace DistributedHardware {
29 #undef DH_LOG_TAG
30 #define DH_LOG_TAG "DistributedHardwareProxy"
31 const std::unordered_set<DHType> DH_TYPE_SET {
32     DHType::UNKNOWN, DHType::CAMERA, DHType::MIC, DHType::SPEAKER,      DHType::DISPLAY,          DHType::GPS,
33     DHType::BUTTON,  DHType::HFP,    DHType::A2D, DHType::VIRMODEM_MIC, DHType::VIRMODEM_SPEAKER, DHType::MAX_DH,
34 };
35 
QuerySinkVersion(std::unordered_map<DHType,std::string> & versionMap)36 int32_t DistributedHardwareProxy::QuerySinkVersion(std::unordered_map<DHType, std::string> &versionMap)
37 {
38     MessageParcel data;
39     MessageParcel reply;
40     MessageOption option;
41 
42     if (!data.WriteInterfaceToken(GetDescriptor())) {
43         DHLOGE("WriteInterfaceToken fail!");
44         return ERR_DH_FWK_SERVICE_WRITE_TOKEN_FAIL;
45     }
46     int32_t error = Remote()->SendRequest(QUERY_SINK_VERSION, data, reply, option);
47     if (error != NO_ERROR) {
48         DHLOGE("SendRequest failed, errCode =  %d", error);
49         return ERR_DH_FWK_SERVICE_IPC_SEND_REQUEST_FAIL;
50     }
51     auto sinkVersion = reply.ReadString();
52     if (sinkVersion.empty()) {
53         DHLOGE("sinkVersion is empty");
54         return ERR_DH_FWK_SERVICE_STRING_IS_EMPTY;
55     }
56     versionMap = FromJson(sinkVersion);
57     DHLOGI("success, sinkVersion = %s", sinkVersion.c_str());
58     return DH_FWK_SUCCESS;
59 }
60 
from_json(const nlohmann::json & jsonObj,std::unordered_map<DHType,std::string> & versionMap)61 void from_json(const nlohmann::json &jsonObj, std::unordered_map<DHType, std::string> &versionMap)
62 {
63     for (const auto &item : jsonObj.value(DH_COMPONENT_VERSIONS, nlohmann::json {})) {
64         DHType dhType = (DH_TYPE_SET.find(item.value(DH_COMPONENT_TYPE, DHType::UNKNOWN)) != DH_TYPE_SET.end()) ?
65             item.value(DH_COMPONENT_TYPE, DHType::UNKNOWN) :
66             DHType::UNKNOWN;
67         std::string sinkVersion = item.value(DH_COMPONENT_SINK_VER, DH_COMPONENT_DEFAULT_VERSION);
68         versionMap.emplace(std::pair<DHType, std::string>(dhType, sinkVersion));
69     }
70 }
71 
FromJson(const std::string & json) const72 std::unordered_map<DHType, std::string> DistributedHardwareProxy::FromJson(const std::string &json) const
73 {
74     return nlohmann::json::parse(json).get<std::unordered_map<DHType, std::string>>();
75 }
76 }
77 }
78