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_stub.h"
17
18 #include "message_parcel.h"
19 #include "devicestatus_srv_proxy.h"
20 #include "devicestatus_common.h"
21 #include "idevicestatus_callback.h"
22 #include "devicestatus_data_utils.h"
23 #include "devicestatus_service.h"
24
25 namespace OHOS {
26 namespace Msdp {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)27 int32_t DevicestatusSrvStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, \
28 MessageOption &option)
29 {
30 DEV_HILOGD(SERVICE, "cmd = %{public}d, flags = %{public}d", code, option.GetFlags());
31 std::u16string descriptor = DevicestatusSrvStub::GetDescriptor();
32 std::u16string remoteDescriptor = data.ReadInterfaceToken();
33 if (descriptor != remoteDescriptor) {
34 DEV_HILOGE(SERVICE, "DevicestatusSrvStub::OnRemoteRequest failed, descriptor is not matched");
35 return E_DEVICESTATUS_GET_SERVICE_FAILED;
36 }
37
38 switch (code) {
39 case static_cast<int32_t>(Idevicestatus::DEVICESTATUS_SUBSCRIBE): {
40 return SubscribeStub(data);
41 }
42 case static_cast<int32_t>(Idevicestatus::DEVICESTATUS_UNSUBSCRIBE): {
43 return UnSubscribeStub(data);
44 }
45 case static_cast<int32_t>(Idevicestatus::DEVICESTATUS_GETCACHE): {
46 return GetLatestDevicestatusDataStub(data, reply);
47 }
48 default: {
49 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
50 }
51 }
52 return ERR_OK;
53 }
54
SubscribeStub(MessageParcel & data)55 int32_t DevicestatusSrvStub::SubscribeStub(MessageParcel& data)
56 {
57 DEV_HILOGD(SERVICE, "Enter");
58 int32_t type = -1;
59 DEVICESTATUS_READ_PARCEL_WITH_RET(data, Int32, type, E_DEVICESTATUS_READ_PARCEL_ERROR);
60 DEV_HILOGD(SERVICE, "Read type successfully");
61 sptr<IRemoteObject> obj = data.ReadRemoteObject();
62 DEVICESTATUS_RETURN_IF_WITH_RET((obj == nullptr), E_DEVICESTATUS_READ_PARCEL_ERROR);
63 DEV_HILOGD(SERVICE, "Read remote obj successfully");
64 sptr<IdevicestatusCallback> callback = iface_cast<IdevicestatusCallback>(obj);
65 DEVICESTATUS_RETURN_IF_WITH_RET((callback == nullptr), E_DEVICESTATUS_READ_PARCEL_ERROR);
66 DEV_HILOGD(SERVICE, "Read callback successfully");
67 Subscribe(DevicestatusDataUtils::DevicestatusType(type), callback);
68 return ERR_OK;
69 }
70
UnSubscribeStub(MessageParcel & data)71 int32_t DevicestatusSrvStub::UnSubscribeStub(MessageParcel& data)
72 {
73 DEV_HILOGD(SERVICE, "Enter");
74 int32_t type = -1;
75 DEVICESTATUS_READ_PARCEL_WITH_RET(data, Int32, type, E_DEVICESTATUS_READ_PARCEL_ERROR);
76 sptr<IRemoteObject> obj = data.ReadRemoteObject();
77 DEVICESTATUS_RETURN_IF_WITH_RET((obj == nullptr), E_DEVICESTATUS_READ_PARCEL_ERROR);
78 sptr<IdevicestatusCallback> callback = iface_cast<IdevicestatusCallback>(obj);
79 DEVICESTATUS_RETURN_IF_WITH_RET((callback == nullptr), E_DEVICESTATUS_READ_PARCEL_ERROR);
80 UnSubscribe(DevicestatusDataUtils::DevicestatusType(type), callback);
81 return ERR_OK;
82 }
83
GetLatestDevicestatusDataStub(MessageParcel & data,MessageParcel & reply)84 int32_t DevicestatusSrvStub::GetLatestDevicestatusDataStub(MessageParcel& data, MessageParcel& reply)
85 {
86 DEV_HILOGD(SERVICE, "Enter");
87 int32_t type = -1;
88 DEVICESTATUS_READ_PARCEL_WITH_RET(data, Int32, type, E_DEVICESTATUS_READ_PARCEL_ERROR);
89 DevicestatusDataUtils::DevicestatusData devicestatusData = GetCache(DevicestatusDataUtils::DevicestatusType(type));
90 DEV_HILOGD(SERVICE, "devicestatusData.type: %{public}d", devicestatusData.type);
91 DEV_HILOGD(SERVICE, "devicestatusData.value: %{public}d", devicestatusData.value);
92 DEVICESTATUS_WRITE_PARCEL_WITH_RET(reply, Int32, devicestatusData.type, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
93 DEVICESTATUS_WRITE_PARCEL_WITH_RET(reply, Int32, devicestatusData.value, E_DEVICESTATUS_WRITE_PARCEL_ERROR);
94 DEV_HILOGD(SERVICE, "Exit");
95 return ERR_OK;
96 }
97 } // Msdp
98 } // OHOS
99