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 "devicestatus_srv_proxy.h"
17
18 #include <message_option.h>
19 #include <message_parcel.h>
20
21 #include "devicestatus_common.h"
22 #include "devicestatus_data_utils.h"
23 #include "hitrace_meter.h"
24 #include "idevicestatus_callback.h"
25 #include "iremote_object.h"
26
27 namespace OHOS {
28 namespace Msdp {
Subscribe(const DevicestatusDataUtils::DevicestatusType & type,const sptr<IdevicestatusCallback> & callback)29 void DevicestatusSrvProxy::Subscribe(const DevicestatusDataUtils::DevicestatusType& type, \
30 const sptr<IdevicestatusCallback>& callback)
31 {
32 DEV_HILOGD(INNERKIT, "Enter");
33 StartTrace(HITRACE_TAG_MSDP, "clientSubcribeStart");
34 sptr<IRemoteObject> remote = Remote();
35 DEVICESTATUS_RETURN_IF((remote == nullptr) || (callback == nullptr));
36
37 MessageParcel data;
38 MessageParcel reply;
39 MessageOption option;
40
41 if (!data.WriteInterfaceToken(DevicestatusSrvProxy::GetDescriptor())) {
42 DEV_HILOGE(INNERKIT, "Write descriptor failed");
43 return;
44 }
45
46 DEVICESTATUS_WRITE_PARCEL_NO_RET(data, Int32, type);
47 DEVICESTATUS_WRITE_PARCEL_NO_RET(data, RemoteObject, callback->AsObject());
48
49 int32_t ret = remote->SendRequest(static_cast<int32_t>(Idevicestatus::DEVICESTATUS_SUBSCRIBE), data, reply, option);
50 if (ret != ERR_OK) {
51 DEV_HILOGE(INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
52 return;
53 }
54 FinishTrace(HITRACE_TAG_MSDP);
55 DEV_HILOGD(INNERKIT, "Exit");
56 }
57
UnSubscribe(const DevicestatusDataUtils::DevicestatusType & type,const sptr<IdevicestatusCallback> & callback)58 void DevicestatusSrvProxy::UnSubscribe(const DevicestatusDataUtils::DevicestatusType& type,
59 const sptr<IdevicestatusCallback>& callback)
60 {
61 DEV_HILOGD(INNERKIT, "Enter");
62 StartTrace(HITRACE_TAG_MSDP, "clientUnSubcribeStart");
63 sptr<IRemoteObject> remote = Remote();
64 DEVICESTATUS_RETURN_IF((remote == nullptr) || (callback == nullptr));
65
66 MessageParcel data;
67 MessageParcel reply;
68 MessageOption option;
69
70 if (!data.WriteInterfaceToken(DevicestatusSrvProxy::GetDescriptor())) {
71 DEV_HILOGE(INNERKIT, "Write descriptor failed!");
72 return;
73 }
74
75 DEVICESTATUS_WRITE_PARCEL_NO_RET(data, Int32, type);
76 DEVICESTATUS_WRITE_PARCEL_NO_RET(data, RemoteObject, callback->AsObject());
77
78 int32_t ret = remote->SendRequest(static_cast<int32_t>(Idevicestatus::DEVICESTATUS_UNSUBSCRIBE),
79 data, reply, option);
80 if (ret != ERR_OK) {
81 DEV_HILOGE(INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
82 return;
83 }
84 FinishTrace(HITRACE_TAG_MSDP);
85 DEV_HILOGD(INNERKIT, "Exit");
86 }
87
GetCache(const DevicestatusDataUtils::DevicestatusType & type)88 DevicestatusDataUtils::DevicestatusData DevicestatusSrvProxy::GetCache(const \
89 DevicestatusDataUtils::DevicestatusType& type)
90 {
91 DEV_HILOGD(INNERKIT, "Enter");
92 DevicestatusDataUtils::DevicestatusData devicestatusData;
93 devicestatusData.type = DevicestatusDataUtils::DevicestatusType::TYPE_INVALID;
94 devicestatusData.value = DevicestatusDataUtils::DevicestatusValue::VALUE_INVALID;
95
96 sptr<IRemoteObject> remote = Remote();
97 DEVICESTATUS_RETURN_IF_WITH_RET((remote == nullptr), devicestatusData);
98
99 MessageParcel data;
100 MessageParcel reply;
101 MessageOption option;
102
103 if (!data.WriteInterfaceToken(DevicestatusSrvProxy::GetDescriptor())) {
104 DEV_HILOGE(INNERKIT, "Write descriptor failed!");
105 return devicestatusData;
106 }
107
108 DEVICESTATUS_WRITE_PARCEL_WITH_RET(data, Int32, type, devicestatusData);
109
110 int32_t ret = remote->SendRequest(static_cast<int32_t>(Idevicestatus::DEVICESTATUS_GETCACHE), data, reply, option);
111 if (ret != ERR_OK) {
112 DEV_HILOGE(INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
113 return devicestatusData;
114 }
115
116 int32_t devicestatusType = -1;
117 int32_t devicestatusValue = -1;
118 DEVICESTATUS_READ_PARCEL_WITH_RET(reply, Int32, devicestatusType, devicestatusData);
119 DEVICESTATUS_READ_PARCEL_WITH_RET(reply, Int32, devicestatusValue, devicestatusData);
120 devicestatusData.type = DevicestatusDataUtils::DevicestatusType(devicestatusType);
121 devicestatusData.value = DevicestatusDataUtils::DevicestatusValue(devicestatusValue);
122 DEV_HILOGD(INNERKIT, "type: %{public}d, value: %{public}d", devicestatusData.type, devicestatusData.value);
123 DEV_HILOGD(INNERKIT, "Exit");
124 return devicestatusData;
125 }
126 } // Msdp
127 } // OHOS
128