• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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_device_profile_proxy.h"
17 
18 #include <string>
19 
20 #include "device_profile_log.h"
21 #include "device_profile_utils.h"
22 #include "errors.h"
23 #include "ipc_types.h"
24 #include "iremote_object.h"
25 #include "message_option.h"
26 #include "message_parcel.h"
27 #include "parcel_helper.h"
28 #include "service_characteristic_profile.h"
29 #include "subscribe_info.h"
30 #include "sync_options.h"
31 
32 namespace OHOS {
33 namespace DeviceProfile {
34 namespace {
35 const std::string TAG = "DistributedDeviceProfileProxy";
36 }
37 
PutDeviceProfile(const ServiceCharacteristicProfile & profile)38 int32_t DistributedDeviceProfileProxy::PutDeviceProfile(const ServiceCharacteristicProfile& profile)
39 {
40     sptr<IRemoteObject> remote = Remote();
41     MessageParcel data;
42     if (!data.WriteInterfaceToken(IDistributedDeviceProfile::GetDescriptor())) {
43         HILOGE("write interface token failed");
44         return ERR_FLATTEN_OBJECT;
45     }
46     if (!profile.Marshalling(data)) {
47         HILOGE("marshall profile failed");
48         return ERR_FLATTEN_OBJECT;
49     }
50     MessageParcel reply;
51     PARCEL_TRANSACT_SYNC_RET_INT(remote, PUT_DEVICE_PROFILE, data, reply);
52 }
53 
GetDeviceProfile(const std::string & udid,const std::string & serviceId,ServiceCharacteristicProfile & profile)54 int32_t DistributedDeviceProfileProxy::GetDeviceProfile(const std::string& udid, const std::string& serviceId,
55     ServiceCharacteristicProfile& profile)
56 {
57     sptr<IRemoteObject> remote = Remote();
58     MessageParcel data;
59     if (!data.WriteInterfaceToken(IDistributedDeviceProfile::GetDescriptor())) {
60         HILOGE("write interface token failed");
61         return ERR_FLATTEN_OBJECT;
62     }
63     PARCEL_WRITE_HELPER(data, String, udid);
64     PARCEL_WRITE_HELPER(data, String, serviceId);
65     MessageParcel reply;
66     MessageOption option;
67     int32_t errCode = remote->SendRequest(GET_DEVICE_PROFILE, data, reply, option);
68     if (errCode != ERR_NONE) {
69         HILOGE("transact failed, errCode = %{public}d", errCode);
70         return errCode;
71     }
72     int32_t ret = reply.ReadInt32();
73     if (!profile.Unmarshalling(reply)) {
74         HILOGE("profile unmarshall failed");
75         return ERR_FLATTEN_OBJECT;
76     }
77     HILOGI("ret = %{public}d", ret);
78     return ret;
79 }
80 
DeleteDeviceProfile(const std::string & serviceId)81 int32_t DistributedDeviceProfileProxy::DeleteDeviceProfile(const std::string& serviceId)
82 {
83     sptr<IRemoteObject> remote = Remote();
84     MessageParcel data;
85     if (!data.WriteInterfaceToken(IDistributedDeviceProfile::GetDescriptor())) {
86         HILOGE("write interface token failed");
87         return ERR_FLATTEN_OBJECT;
88     }
89     PARCEL_WRITE_HELPER(data, String, serviceId);
90     MessageParcel reply;
91     PARCEL_TRANSACT_SYNC_RET_INT(remote, DELETE_DEVICE_PROFILE, data, reply);
92 }
93 
SubscribeProfileEvents(const std::list<SubscribeInfo> & subscribeInfos,const sptr<IRemoteObject> & profileEventNotifier,std::list<ProfileEvent> & failedEvents)94 int32_t DistributedDeviceProfileProxy::SubscribeProfileEvents(
95     const std::list<SubscribeInfo>& subscribeInfos,
96     const sptr<IRemoteObject>& profileEventNotifier,
97     std::list<ProfileEvent>& failedEvents)
98 {
99     sptr<IRemoteObject> remote = Remote();
100     MessageParcel data;
101     if (!data.WriteInterfaceToken(IDistributedDeviceProfile::GetDescriptor())) {
102         HILOGE("write interface token failed");
103         return ERR_FLATTEN_OBJECT;
104     }
105     size_t size = subscribeInfos.size();
106     PARCEL_WRITE_HELPER(data, Uint32, static_cast<uint32_t>(size));
107     for (const auto& subscribeInfo : subscribeInfos) {
108         if (!subscribeInfo.Marshalling(data)) {
109             HILOGE("marshall subscribe info failed");
110             return ERR_FLATTEN_OBJECT;
111         }
112     }
113     PARCEL_WRITE_HELPER(data, RemoteObject, profileEventNotifier);
114     MessageParcel reply;
115     MessageOption option;
116     int32_t errCode = remote->SendRequest(SUBSCRIBE_PROFILE_EVENT, data, reply, option);
117     if (errCode != ERR_NONE) {
118         HILOGE("transact failed, errCode = %{public}d", errCode);
119         return errCode;
120     }
121     int32_t ret = reply.ReadInt32();
122     if ((ret != ERR_OK) && !DeviceProfileUtils::ReadProfileEvents(reply, failedEvents)) {
123         HILOGE("read profile events failed");
124         return ERR_FLATTEN_OBJECT;
125     }
126     HILOGI("ret = %{public}d", ret);
127     return ret;
128 }
129 
UnsubscribeProfileEvents(const std::list<ProfileEvent> & profileEvents,const sptr<IRemoteObject> & profileEventNotifier,std::list<ProfileEvent> & failedEvents)130 int32_t DistributedDeviceProfileProxy::UnsubscribeProfileEvents(
131     const std::list<ProfileEvent>& profileEvents,
132     const sptr<IRemoteObject>& profileEventNotifier,
133     std::list<ProfileEvent>& failedEvents)
134 {
135     sptr<IRemoteObject> remote = Remote();
136     MessageParcel data;
137     if (!data.WriteInterfaceToken(IDistributedDeviceProfile::GetDescriptor())) {
138         HILOGE("write interface token failed");
139         return ERR_FLATTEN_OBJECT;
140     }
141     if (!DeviceProfileUtils::WriteProfileEvents(profileEvents, data)) {
142         return ERR_FLATTEN_OBJECT;
143     }
144 
145     PARCEL_WRITE_HELPER(data, RemoteObject, profileEventNotifier);
146     MessageParcel reply;
147     MessageOption option;
148     int32_t errCode = remote->SendRequest(UNSUBSCRIBE_PROFILE_EVENT, data, reply, option);
149     if (errCode != ERR_NONE) {
150         HILOGE("transact failed, errCode = %{public}d", errCode);
151         return errCode;
152     }
153     int32_t ret = reply.ReadInt32();
154     if ((ret != ERR_OK) && !DeviceProfileUtils::ReadProfileEvents(reply, failedEvents)) {
155         HILOGW("read profile events failed");
156         return ERR_FLATTEN_OBJECT;
157     }
158     HILOGI("ret = %{public}d", ret);
159     return ret;
160 }
161 
SyncDeviceProfile(const SyncOptions & syncOptions,const sptr<IRemoteObject> & profileEventNotifier)162 int32_t DistributedDeviceProfileProxy::SyncDeviceProfile(const SyncOptions& syncOptions,
163     const sptr<IRemoteObject>& profileEventNotifier)
164 {
165     sptr<IRemoteObject> remote = Remote();
166     MessageParcel data;
167     if (!data.WriteInterfaceToken(IDistributedDeviceProfile::GetDescriptor())) {
168         HILOGE("write interface token failed");
169         return ERR_FLATTEN_OBJECT;
170     }
171 
172     if (!syncOptions.Marshalling(data)) {
173         HILOGE("marshall option failed");
174         return ERR_FLATTEN_OBJECT;
175     }
176 
177     PARCEL_WRITE_HELPER(data, RemoteObject, profileEventNotifier);
178     MessageParcel reply;
179     PARCEL_TRANSACT_SYNC_RET_INT(remote, SYNC_DEVICE_PROFILE, data, reply);
180 }
181 } // namespace DeviceProfile
182 } // namespace OHOS