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