1 /*
2  * Copyright (c) 2022-2024 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 "device_info_proxy.h"
17 
18 #include "edm_constants.h"
19 #include "edm_log.h"
20 #include "func_code.h"
21 
22 namespace OHOS {
23 namespace EDM {
24 std::shared_ptr<DeviceInfoProxy> DeviceInfoProxy::instance_ = nullptr;
25 std::mutex DeviceInfoProxy::mutexLock_;
26 const std::u16string DESCRIPTOR = u"ohos.edm.IEnterpriseDeviceMgr";
27 
DeviceInfoProxy()28 DeviceInfoProxy::DeviceInfoProxy() {}
29 
~DeviceInfoProxy()30 DeviceInfoProxy::~DeviceInfoProxy() {}
31 
GetDeviceInfoProxy()32 std::shared_ptr<DeviceInfoProxy> DeviceInfoProxy::GetDeviceInfoProxy()
33 {
34     if (instance_ == nullptr) {
35         std::lock_guard<std::mutex> lock(mutexLock_);
36         if (instance_ == nullptr) {
37             std::shared_ptr<DeviceInfoProxy> temp = std::make_shared<DeviceInfoProxy>();
38             instance_ = temp;
39         }
40     }
41     return instance_;
42 }
43 
GetDeviceSerial(AppExecFwk::ElementName & admin,std::string & info)44 int32_t DeviceInfoProxy::GetDeviceSerial(AppExecFwk::ElementName &admin, std::string &info)
45 {
46     return GetDeviceInfoSync(admin, EdmConstants::DeviceInfo::DEVICE_SERIAL, info);
47 }
48 
GetDisplayVersion(AppExecFwk::ElementName & admin,std::string & info)49 int32_t DeviceInfoProxy::GetDisplayVersion(AppExecFwk::ElementName &admin, std::string &info)
50 {
51     return GetDeviceInfo(admin, info, EdmInterfaceCode::GET_DISPLAY_VERSION);
52 }
53 
GetDeviceName(AppExecFwk::ElementName & admin,std::string & info)54 int32_t DeviceInfoProxy::GetDeviceName(AppExecFwk::ElementName &admin, std::string &info)
55 {
56     return GetDeviceInfoSync(admin, EdmConstants::DeviceInfo::DEVICE_NAME, info);
57 }
58 
GetDeviceInfo(AppExecFwk::ElementName & admin,std::string & info,int policyCode)59 int32_t DeviceInfoProxy::GetDeviceInfo(AppExecFwk::ElementName &admin, std::string &info, int policyCode)
60 {
61     EDMLOGD("DeviceInfoProxy::GetDeviceInfo %{public}d", policyCode);
62     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
63     MessageParcel data;
64     MessageParcel reply;
65     data.WriteInterfaceToken(DESCRIPTOR);
66     data.WriteInt32(WITHOUT_USERID);
67     data.WriteString(WITHOUT_PERMISSION_TAG);
68     data.WriteInt32(HAS_ADMIN);
69     data.WriteParcelable(&admin);
70     proxy->GetPolicy(policyCode, data, reply);
71     int32_t ret = ERR_INVALID_VALUE;
72     bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
73     if (!blRes) {
74         EDMLOGW("EnterpriseDeviceMgrProxy:GetPolicy fail. %{public}d", ret);
75         return ret;
76     }
77     reply.ReadString(info);
78     return ERR_OK;
79 }
80 
GetDeviceInfoSync(AppExecFwk::ElementName & admin,const std::string & label,std::string & info)81 int32_t DeviceInfoProxy::GetDeviceInfoSync(AppExecFwk::ElementName &admin, const std::string &label, std::string &info)
82 {
83     EDMLOGI("DeviceInfoProxy::GetDeviceInfoSync %{public}s", label.c_str());
84     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
85     MessageParcel data;
86     MessageParcel reply;
87     data.WriteInterfaceToken(DESCRIPTOR);
88     data.WriteInt32(WITHOUT_USERID);
89     data.WriteString(WITHOUT_PERMISSION_TAG);
90     data.WriteInt32(HAS_ADMIN);
91     data.WriteParcelable(&admin);
92     data.WriteString(label);
93     proxy->GetPolicy(EdmInterfaceCode::GET_DEVICE_INFO, data, reply);
94     int32_t ret = ERR_INVALID_VALUE;
95     bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
96     if (!blRes) {
97         EDMLOGW("EnterpriseDeviceMgrProxy:GetDeviceInfoSync fail. %{public}d", ret);
98         return ret;
99     }
100     reply.ReadString(info);
101     return ERR_OK;
102 }
103 }  // namespace EDM
104 }  // namespace OHOS
105