1 /*
2  * Copyright (c) 2023-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 "profile_change_listener_stub.h"
17 #include "distributed_device_profile_errors.h"
18 #include "trust_device_profile.h"
19 #include "access_control_profile.h"
20 #include "device_profile.h"
21 #include "service_profile.h"
22 #include "characteristic_profile.h"
23 #include "distributed_device_profile_log.h"
24 #include "ipc_utils.h"
25 #include "message_parcel.h"
26 
27 namespace OHOS {
28 namespace DistributedDeviceProfile {
29 namespace {
30     const std::string TAG = "ProfileChangeListenerStub";
31 }
ProfileChangeListenerStub()32 ProfileChangeListenerStub::ProfileChangeListenerStub()
33 {
34     HILOGI("construct!");
35     funcsMap_[static_cast<uint32_t>(DPInterfaceCode::ON_TRUST_DEVICE_PROFILE_ADD)] =
36         &ProfileChangeListenerStub::OnTrustDeviceProfileAddInner;
37     funcsMap_[static_cast<uint32_t>(DPInterfaceCode::ON_TRUST_DEVICE_PROFILE_DELETE)] =
38         &ProfileChangeListenerStub::OnTrustDeviceProfileDeleteInner;
39     funcsMap_[static_cast<uint32_t>(DPInterfaceCode::ON_TRUST_DEVICE_PROFILE_UPDATE)] =
40         &ProfileChangeListenerStub::OnTrustDeviceProfileUpdateInner;
41     funcsMap_[static_cast<uint32_t>(DPInterfaceCode::ON_DEVICE_PROFILE_ADD)] =
42         &ProfileChangeListenerStub::OnDeviceProfileAddInner;
43     funcsMap_[static_cast<uint32_t>(DPInterfaceCode::ON_DEVICE_PROFILE_DELETE)] =
44         &ProfileChangeListenerStub::OnDeviceProfileDeleteInner;
45     funcsMap_[static_cast<uint32_t>(DPInterfaceCode::ON_DEVICE_PROFILE_UPDATE)] =
46         &ProfileChangeListenerStub::OnDeviceProfileUpdateInner;
47     funcsMap_[static_cast<uint32_t>(DPInterfaceCode::ON_SERVICE_PROFILE_ADD)] =
48         &ProfileChangeListenerStub::OnServiceProfileAddInner;
49     funcsMap_[static_cast<uint32_t>(DPInterfaceCode::ON_SERVICE_PROFILE_DELETE)] =
50         &ProfileChangeListenerStub::OnServiceProfileDeleteInner;
51     funcsMap_[static_cast<uint32_t>(DPInterfaceCode::ON_SERVICE_PROFILE_UPDATE)] =
52         &ProfileChangeListenerStub::OnServiceProfileUpdateInner;
53     funcsMap_[static_cast<uint32_t>(DPInterfaceCode::ON_CHAR_PROFILE_ADD)] =
54         &ProfileChangeListenerStub::OnCharacteristicProfileAddInner;
55     funcsMap_[static_cast<uint32_t>(DPInterfaceCode::ON_CHAR_PROFILE_DELETE)] =
56         &ProfileChangeListenerStub::OnCharacteristicProfileDeleteInner;
57     funcsMap_[static_cast<uint32_t>(DPInterfaceCode::ON_CHAR_PROFILE_UPDATE)] =
58         &ProfileChangeListenerStub::OnCharacteristicProfileUpdateInner;
59 }
60 
~ProfileChangeListenerStub()61 ProfileChangeListenerStub::~ProfileChangeListenerStub()
62 {
63     HILOGI("destruct!");
64 }
65 
IsInterfaceToken(MessageParcel & data)66 bool ProfileChangeListenerStub::IsInterfaceToken(MessageParcel& data)
67 {
68     return data.ReadInterfaceToken() == IProfileChangeListener::GetDescriptor();
69 }
70 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)71 int32_t ProfileChangeListenerStub::OnRemoteRequest(uint32_t code, MessageParcel& data,
72     MessageParcel& reply, MessageOption& option)
73 {
74     HILOGI("code = %{public}u, flags = %{public}d", code, option.GetFlags());
75 
76     if (!IsInterfaceToken(data)) {
77         HILOGE("check interface token failed");
78         return DP_INTERFACE_CHECK_FAILED;
79     }
80     switch (code) {
81         case static_cast<uint32_t>(DPInterfaceCode::ON_TRUST_DEVICE_PROFILE_ADD):
82             return ProfileChangeListenerStub::OnTrustDeviceProfileAddInner(data, reply);
83         case static_cast<uint32_t>(DPInterfaceCode::ON_TRUST_DEVICE_PROFILE_DELETE):
84             return ProfileChangeListenerStub::OnTrustDeviceProfileDeleteInner(data, reply);
85         case static_cast<uint32_t>(DPInterfaceCode::ON_TRUST_DEVICE_PROFILE_UPDATE):
86             return ProfileChangeListenerStub::OnTrustDeviceProfileUpdateInner(data, reply);
87         case static_cast<uint32_t>(DPInterfaceCode::ON_DEVICE_PROFILE_ADD):
88             return ProfileChangeListenerStub::OnDeviceProfileAddInner(data, reply);
89         case static_cast<uint32_t>(DPInterfaceCode::ON_DEVICE_PROFILE_DELETE):
90             return ProfileChangeListenerStub::OnDeviceProfileDeleteInner(data, reply);
91         case static_cast<uint32_t>(DPInterfaceCode::ON_DEVICE_PROFILE_UPDATE):
92             return ProfileChangeListenerStub::OnDeviceProfileUpdateInner(data, reply);
93         case static_cast<uint32_t>(DPInterfaceCode::ON_SERVICE_PROFILE_ADD):
94             return ProfileChangeListenerStub::OnServiceProfileAddInner(data, reply);
95         case static_cast<uint32_t>(DPInterfaceCode::ON_SERVICE_PROFILE_DELETE):
96             return ProfileChangeListenerStub::OnServiceProfileDeleteInner(data, reply);
97         case static_cast<uint32_t>(DPInterfaceCode::ON_SERVICE_PROFILE_UPDATE):
98             return ProfileChangeListenerStub::OnServiceProfileUpdateInner(data, reply);
99         case static_cast<uint32_t>(DPInterfaceCode::ON_CHAR_PROFILE_ADD):
100             return ProfileChangeListenerStub::OnCharacteristicProfileAddInner(data, reply);
101         case static_cast<uint32_t>(DPInterfaceCode::ON_CHAR_PROFILE_DELETE):
102             return ProfileChangeListenerStub::OnCharacteristicProfileDeleteInner(data, reply);
103         case static_cast<uint32_t>(DPInterfaceCode::ON_CHAR_PROFILE_UPDATE):
104             return ProfileChangeListenerStub::OnCharacteristicProfileUpdateInner(data, reply);
105         default:
106             HILOGW("unknown request code, please check, code = %{public}u", code);
107             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
108     }
109 }
110 
OnTrustDeviceProfileAddInner(MessageParcel & data,MessageParcel & reply)111 int32_t ProfileChangeListenerStub::OnTrustDeviceProfileAddInner(MessageParcel& data, MessageParcel& reply)
112 {
113     HILOGI("called");
114     TrustDeviceProfile trustDeviceProfile;
115     if (!trustDeviceProfile.UnMarshalling(data)) {
116         HILOGE("Read reply failed");
117         return ERR_FLATTEN_OBJECT;
118     }
119     OnTrustDeviceProfileAdd(trustDeviceProfile);
120     if (!reply.WriteInt32(DP_SUCCESS)) {
121         HILOGE("Read reply failed");
122         return ERR_FLATTEN_OBJECT;
123     }
124     return DP_SUCCESS;
125 }
126 
OnTrustDeviceProfileDeleteInner(MessageParcel & data,MessageParcel & reply)127 int32_t ProfileChangeListenerStub::OnTrustDeviceProfileDeleteInner(MessageParcel& data, MessageParcel& reply)
128 {
129     HILOGI("called");
130     TrustDeviceProfile trustDeviceProfile;
131     if (!trustDeviceProfile.UnMarshalling(data)) {
132         HILOGE("Read reply failed");
133         return ERR_FLATTEN_OBJECT;
134     }
135     OnTrustDeviceProfileDelete(trustDeviceProfile);
136     if (!reply.WriteInt32(DP_SUCCESS)) {
137         HILOGE("Read reply failed");
138         return ERR_FLATTEN_OBJECT;
139     }
140     return DP_SUCCESS;
141 }
142 
OnTrustDeviceProfileUpdateInner(MessageParcel & data,MessageParcel & reply)143 int32_t ProfileChangeListenerStub::OnTrustDeviceProfileUpdateInner(MessageParcel& data, MessageParcel& reply)
144 {
145     HILOGI("called");
146     TrustDeviceProfile oldTrustDeviceProfile;
147     TrustDeviceProfile newTrustDeviceProfile;
148     if (!oldTrustDeviceProfile.UnMarshalling(data)) {
149         HILOGE("Read reply failed");
150         return ERR_FLATTEN_OBJECT;
151     }
152     if (!newTrustDeviceProfile.UnMarshalling(data)) {
153         HILOGE("Read reply failed");
154         return ERR_FLATTEN_OBJECT;
155     }
156     OnTrustDeviceProfileUpdate(oldTrustDeviceProfile, newTrustDeviceProfile);
157     if (!reply.WriteInt32(DP_SUCCESS)) {
158         HILOGE("Write reply failed");
159         return ERR_FLATTEN_OBJECT;
160     }
161     return DP_SUCCESS;
162 }
163 
OnDeviceProfileAddInner(MessageParcel & data,MessageParcel & reply)164 int32_t ProfileChangeListenerStub::OnDeviceProfileAddInner(MessageParcel& data, MessageParcel& reply)
165 {
166     HILOGI("called");
167     DeviceProfile deviceProfile;
168     if (!deviceProfile.UnMarshalling(data)) {
169         HILOGE("Read reply failed");
170         return ERR_FLATTEN_OBJECT;
171     }
172     OnDeviceProfileAdd(deviceProfile);
173     if (!reply.WriteInt32(DP_SUCCESS)) {
174         HILOGE("Write reply failed");
175         return ERR_FLATTEN_OBJECT;
176     }
177     return DP_SUCCESS;
178 }
179 
OnDeviceProfileDeleteInner(MessageParcel & data,MessageParcel & reply)180 int32_t ProfileChangeListenerStub::OnDeviceProfileDeleteInner(MessageParcel& data, MessageParcel& reply)
181 {
182     HILOGI("called");
183     DeviceProfile deviceProfile;
184     if (!deviceProfile.UnMarshalling(data)) {
185         HILOGE("Read reply failed");
186         return ERR_FLATTEN_OBJECT;
187     }
188     OnDeviceProfileDelete(deviceProfile);
189     if (!reply.WriteInt32(DP_SUCCESS)) {
190         HILOGE("Write reply failed");
191         return ERR_FLATTEN_OBJECT;
192     }
193     return DP_SUCCESS;
194 }
195 
OnDeviceProfileUpdateInner(MessageParcel & data,MessageParcel & reply)196 int32_t ProfileChangeListenerStub::OnDeviceProfileUpdateInner(MessageParcel& data, MessageParcel& reply)
197 {
198     HILOGI("called");
199     DeviceProfile oldDeviceProfile;
200     DeviceProfile newDeviceProfile;
201     if (!oldDeviceProfile.UnMarshalling(data)) {
202         HILOGE("Read reply failed");
203         return ERR_FLATTEN_OBJECT;
204     }
205     if (!newDeviceProfile.UnMarshalling(data)) {
206         HILOGE("Read reply failed");
207         return ERR_FLATTEN_OBJECT;
208     }
209     OnDeviceProfileUpdate(oldDeviceProfile, newDeviceProfile);
210     if (!reply.WriteInt32(DP_SUCCESS)) {
211         HILOGE("Write reply failed");
212         return ERR_FLATTEN_OBJECT;
213     }
214     return DP_SUCCESS;
215 }
216 
OnServiceProfileAddInner(MessageParcel & data,MessageParcel & reply)217 int32_t ProfileChangeListenerStub::OnServiceProfileAddInner(MessageParcel& data, MessageParcel& reply)
218 {
219     HILOGI("called");
220     ServiceProfile serviceProfile;
221     if (!serviceProfile.UnMarshalling(data)) {
222         HILOGE("Read reply failed");
223         return ERR_FLATTEN_OBJECT;
224     }
225     OnServiceProfileAdd(serviceProfile);
226     if (!reply.WriteInt32(DP_SUCCESS)) {
227         HILOGE("Write reply failed");
228         return ERR_FLATTEN_OBJECT;
229     }
230     return DP_SUCCESS;
231 }
232 
OnServiceProfileDeleteInner(MessageParcel & data,MessageParcel & reply)233 int32_t ProfileChangeListenerStub::OnServiceProfileDeleteInner(MessageParcel& data, MessageParcel& reply)
234 {
235     HILOGI("called");
236     ServiceProfile serviceProfile;
237     if (!serviceProfile.UnMarshalling(data)) {
238         HILOGE("Read reply failed");
239         return ERR_FLATTEN_OBJECT;
240     }
241     OnServiceProfileDelete(serviceProfile);
242     if (!reply.WriteInt32(DP_SUCCESS)) {
243         HILOGE("Write reply failed");
244         return ERR_FLATTEN_OBJECT;
245     }
246     return DP_SUCCESS;
247 }
248 
OnServiceProfileUpdateInner(MessageParcel & data,MessageParcel & reply)249 int32_t ProfileChangeListenerStub::OnServiceProfileUpdateInner(MessageParcel& data, MessageParcel& reply)
250 {
251     HILOGI("called");
252     ServiceProfile oldServiceProfile;
253     ServiceProfile newServiceProfile;
254     if (!oldServiceProfile.UnMarshalling(data)) {
255         HILOGE("Read reply failed");
256         return ERR_FLATTEN_OBJECT;
257     }
258     if (!newServiceProfile.UnMarshalling(data)) {
259         HILOGE("Read reply failed");
260         return ERR_FLATTEN_OBJECT;
261     }
262     OnServiceProfileUpdate(oldServiceProfile, newServiceProfile);
263     if (!reply.WriteInt32(DP_SUCCESS)) {
264         HILOGE("Write reply failed");
265         return ERR_FLATTEN_OBJECT;
266     }
267     return DP_SUCCESS;
268 }
269 
OnCharacteristicProfileAddInner(MessageParcel & data,MessageParcel & reply)270 int32_t ProfileChangeListenerStub::OnCharacteristicProfileAddInner(MessageParcel& data, MessageParcel& reply)
271 {
272     HILOGI("called");
273     CharacteristicProfile characteristicProfile;
274     if (!characteristicProfile.UnMarshalling(data)) {
275         HILOGE("Read reply failed");
276         return ERR_FLATTEN_OBJECT;
277     }
278     OnCharacteristicProfileAdd(characteristicProfile);
279     if (!reply.WriteInt32(DP_SUCCESS)) {
280         HILOGE("Write reply failed");
281         return ERR_FLATTEN_OBJECT;
282     }
283     return DP_SUCCESS;
284 }
285 
OnCharacteristicProfileDeleteInner(MessageParcel & data,MessageParcel & reply)286 int32_t ProfileChangeListenerStub::OnCharacteristicProfileDeleteInner(MessageParcel& data, MessageParcel& reply)
287 {
288     HILOGI("called");
289     CharacteristicProfile characteristicProfile;
290     if (!characteristicProfile.UnMarshalling(data)) {
291         HILOGE("Read reply failed");
292         return ERR_FLATTEN_OBJECT;
293     }
294     OnCharacteristicProfileDelete(characteristicProfile);
295     if (!reply.WriteInt32(DP_SUCCESS)) {
296         HILOGE("Write reply failed");
297         return ERR_FLATTEN_OBJECT;
298     }
299     return DP_SUCCESS;
300 }
301 
OnCharacteristicProfileUpdateInner(MessageParcel & data,MessageParcel & reply)302 int32_t ProfileChangeListenerStub::OnCharacteristicProfileUpdateInner(MessageParcel& data, MessageParcel& reply)
303 {
304     HILOGI("called");
305     CharacteristicProfile oldCharacteristicProfile;
306     CharacteristicProfile newCharacteristicProfile;
307     if (!oldCharacteristicProfile.UnMarshalling(data)) {
308         HILOGE("Read reply failed");
309         return ERR_FLATTEN_OBJECT;
310     }
311     if (!newCharacteristicProfile.UnMarshalling(data)) {
312         HILOGE("Read reply failed");
313         return ERR_FLATTEN_OBJECT;
314     }
315     OnCharacteristicProfileUpdate(oldCharacteristicProfile, newCharacteristicProfile);
316     if (!reply.WriteInt32(DP_SUCCESS)) {
317         HILOGE("Write reply failed");
318         return ERR_FLATTEN_OBJECT;
319     }
320     return DP_SUCCESS;
321 }
322 } // namespace DistributedDeviceProfile
323 } // namespace OHOS
324