1 /*
2 * Copyright (c) 2021 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_event_notifier_proxy.h"
17
18 #include "device_profile_log.h"
19 #include "profile_event.h"
20
21 namespace OHOS {
22 namespace DeviceProfile {
23 namespace {
24 const std::string TAG = "ProfileEventNotifierProxy";
25 }
26
ProfileEventNotifierProxy(const sptr<IRemoteObject> & impl)27 ProfileEventNotifierProxy::ProfileEventNotifierProxy(const sptr<IRemoteObject>& impl)
28 : IRemoteProxy<IProfileEventNotifier>(impl)
29 {}
30
OnSyncCompleted(const SyncResult & syncResults)31 void ProfileEventNotifierProxy::OnSyncCompleted(const SyncResult& syncResults)
32 {
33 HILOGI("called");
34 MessageParcel data;
35 MessageParcel reply;
36 if (!data.WriteInterfaceToken(ProfileEventNotifierProxy::GetDescriptor())) {
37 HILOGE("write descriptor failed");
38 return;
39 }
40 if (!data.WriteInt32(static_cast<int32_t>(syncResults.size()))) {
41 HILOGE("write syncResults size failed");
42 return;
43 }
44
45 for (const auto& [deviceId, syncResult] : syncResults) {
46 if (!data.WriteString(deviceId) ||
47 !data.WriteInt32(static_cast<int32_t>(syncResult))) {
48 HILOGE("write syncResults failed");
49 return;
50 }
51 }
52 MessageOption option { MessageOption::TF_ASYNC };
53 int32_t errCode = Remote()->SendRequest(EVENT_SYNC_COMPLETED, data, reply, option);
54 if (errCode != ERR_OK) {
55 HILOGE("send request failed, errCode = %{public}d", errCode);
56 }
57 }
58
OnProfileChanged(const ProfileChangeNotification & changeNotification)59 void ProfileEventNotifierProxy::OnProfileChanged(const ProfileChangeNotification& changeNotification)
60 {
61 HILOGI("called");
62 MessageParcel data;
63 MessageParcel reply;
64 if (!data.WriteInterfaceToken(ProfileEventNotifierProxy::GetDescriptor())) {
65 HILOGE("write descriptor failed");
66 return;
67 }
68 if (!changeNotification.Marshalling(data)) {
69 HILOGE("write profile change notification failed");
70 return;
71 }
72
73 MessageOption option { MessageOption::TF_ASYNC };
74 int32_t errCode = Remote()->SendRequest(EVENT_PROFILE_CHANGED, data, reply, option);
75 if (errCode != ERR_OK) {
76 HILOGE("send request failed, errCode = %{public}d", errCode);
77 }
78 }
79 } // namespace DeviceProfile
80 } // namespace OHOS
81