1 /*
2  * Copyright (c) 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 "dms_listener_stub.h"
17 
18 #include <cstring>
19 #include <memory>
20 #include <string>
21 
22 #include "ipc_object_stub.h"
23 #include "ipc_types.h"
24 #include "message_option.h"
25 #include "message_parcel.h"
26 #include "securec.h"
27 
28 namespace OHOS {
29 namespace DistributedSchedule {
30 namespace {
31 const std::string TAG = "DmsListenerStub";
32 }
33 
DSchedEventListenerStub()34 DSchedEventListenerStub::DSchedEventListenerStub()
35 {
36     memberFuncMap_[static_cast<uint32_t>(DSchedEventListenerStub::Message::DSCHED_EVENT_CALLBACK)] =
37         &DSchedEventListenerStub::DSchedEventNotifyInner;
38 }
39 
~DSchedEventListenerStub()40 DSchedEventListenerStub::~DSchedEventListenerStub()
41 {}
42 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)43 int32_t DSchedEventListenerStub::OnRemoteRequest(uint32_t code,
44     MessageParcel &data, MessageParcel &reply, MessageOption &option)
45 {
46     HILOGI("DSchedEventListenerStub OnRemoteRequest code: %u", code);
47     std::u16string desc = DSchedEventListenerStub::GetDescriptor();
48     std::u16string remoteDesc = data.ReadInterfaceToken();
49     if (desc != remoteDesc) {
50         HILOGE("remoteDesc is invalid!");
51         return GET_REMOTE_DMS_FAIL;
52     }
53     auto itFunc = memberFuncMap_.find(code);
54     if (itFunc == memberFuncMap_.end()) {
55         HILOGE("memberFuncMap_ not found");
56         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
57     }
58 
59     auto memberFunc = itFunc->second;
60     if (!memberFunc) {
61         HILOGE("memberFunc is null for code %u", code);
62         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
63     }
64     (this->*memberFunc)(data, reply);
65     return 0;
66 }
67 
DSchedEventNotifyInner(MessageParcel & data,MessageParcel & reply)68 void DSchedEventListenerStub::DSchedEventNotifyInner(MessageParcel &data, MessageParcel &reply)
69 {
70     HILOGI("DSchedEventListenerStub DSchedEventNotifyInner");
71     int32_t ret = 0;
72     do {
73         EventNotify eventNotify;
74         eventNotify.eventResult_ = data.ReadInt32();
75         eventNotify.srcNetworkId_ = data.ReadString();
76         eventNotify.dstNetworkId_ = data.ReadString();
77         eventNotify.srcBundleName_ = data.ReadString();
78         eventNotify.srcModuleName_ = data.ReadString();
79         eventNotify.srcAbilityName_ = data.ReadString();
80         eventNotify.destBundleName_ = data.ReadString();
81         eventNotify.destModuleName_ = data.ReadString();
82         eventNotify.destAbilityName_ = data.ReadString();
83         eventNotify.developerId_ = data.ReadString();
84         eventNotify.dSchedEventType_ = static_cast<DSchedEventType>(data.ReadInt32());
85         eventNotify.state_ = static_cast<DSchedEventState>(data.ReadInt32());
86         DSchedEventNotify(eventNotify);
87     } while (0);
88     if (!reply.WriteInt32(ret)) {
89         HILOGE("DSchedEventNotifyInner write ret failed, ret = %{public}d", ret);
90     }
91 }
92 }  // namespace DistributedSchedule
93 }  // namespace OHOS