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_stub.h"
17
18 #include <iosfwd>
19 #include <string>
20 #include <utility>
21
22 #include "device_profile_errors.h"
23 #include "device_profile_log.h"
24 #include "errors.h"
25 #include "ipc_object_stub.h"
26 #include "iprofile_event_callback.h"
27 #include "message_parcel.h"
28 #include "profile_change_notification.h"
29 #include "profile_event.h"
30
31 namespace OHOS {
32 namespace DeviceProfile {
33 namespace {
34 const std::string TAG = "ProfileEventNotifierStub";
35 }
36
37 const ProfileEventNotifierStub::HandlersMap ProfileEventNotifierStub::handlersMap_ =
38 ProfileEventNotifierStub::InitHandlersMap();
39
InitHandlersMap()40 ProfileEventNotifierStub::HandlersMap ProfileEventNotifierStub::InitHandlersMap()
41 {
42 ProfileEventNotifierStub::HandlersMap m;
43 m[EVENT_SYNC_COMPLETED] = &ProfileEventNotifierStub::OnSyncCompletedInner;
44 m[EVENT_PROFILE_CHANGED] = &ProfileEventNotifierStub::OnProfileChangedInner;
45 return m;
46 }
47
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)48 int32_t ProfileEventNotifierStub::OnRemoteRequest(uint32_t code, MessageParcel& data,
49 MessageParcel& reply, MessageOption& option)
50 {
51 HILOGI("code = %{public}u", code);
52 std::u16string descriptor = ProfileEventNotifierStub::GetDescriptor();
53 std::u16string remoteDescriptor = data.ReadInterfaceToken();
54 if (descriptor != remoteDescriptor) {
55 HILOGE("check descriptor failed");
56 return ERR_DP_INTERFACE_CHECK_FAILED;
57 }
58
59 auto iter = handlersMap_.find(code);
60 if (iter != handlersMap_.end()) {
61 auto handler = iter->second;
62 if (handler != nullptr) {
63 return (this->*handler)(data, reply);
64 }
65 }
66 HILOGW("unknown request code, please check");
67 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
68 }
69
OnSyncCompletedInner(MessageParcel & data,MessageParcel & reply)70 int32_t ProfileEventNotifierStub::OnSyncCompletedInner(MessageParcel& data,
71 [[maybe_unused]] MessageParcel& reply)
72 {
73 int32_t size = data.ReadInt32();
74 if (size <= 0) {
75 HILOGW("size %{public}d", size);
76 return ERR_DP_INVALID_PARAMS;
77 }
78
79 SyncResult syncResults;
80 for (int32_t i = 0; i < size; i++) {
81 syncResults.emplace(data.ReadString(), static_cast<SyncStatus>(data.ReadInt32()));
82 }
83 if (profileEventCb_ != nullptr) {
84 profileEventCb_->OnSyncCompleted(syncResults);
85 }
86 return ERR_OK;
87 }
88
OnProfileChangedInner(MessageParcel & data,MessageParcel & reply)89 int32_t ProfileEventNotifierStub::OnProfileChangedInner(MessageParcel& data,
90 [[maybe_unused]] MessageParcel& reply)
91 {
92 ProfileChangeNotification changeNotification;
93 if (!changeNotification.Unmarshalling(data)) {
94 HILOGW("marshalling failed");
95 return ERR_DP_INVALID_PARAMS;
96 }
97 profileEventCb_->OnProfileChanged(changeNotification);
98 return ERR_OK;
99 }
100 } // namespace DeviceProfile
101 } // namespace OHOS