1c29fa5a6Sopenharmony_ci/*
2c29fa5a6Sopenharmony_ci * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3c29fa5a6Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4c29fa5a6Sopenharmony_ci * you may not use this file except in compliance with the License.
5c29fa5a6Sopenharmony_ci * You may obtain a copy of the License at
6c29fa5a6Sopenharmony_ci *
7c29fa5a6Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8c29fa5a6Sopenharmony_ci *
9c29fa5a6Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10c29fa5a6Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11c29fa5a6Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12c29fa5a6Sopenharmony_ci * See the License for the specific language governing permissions and
13c29fa5a6Sopenharmony_ci * limitations under the License.
14c29fa5a6Sopenharmony_ci */
15c29fa5a6Sopenharmony_ci
16c29fa5a6Sopenharmony_ci#include "intention_service.h"
17c29fa5a6Sopenharmony_ci
18c29fa5a6Sopenharmony_ci#include "ipc_skeleton.h"
19c29fa5a6Sopenharmony_ci
20c29fa5a6Sopenharmony_ci#include "devicestatus_define.h"
21c29fa5a6Sopenharmony_ci#include "i_plugin.h"
22c29fa5a6Sopenharmony_ci
23c29fa5a6Sopenharmony_ci#undef LOG_TAG
24c29fa5a6Sopenharmony_ci#define LOG_TAG "IntentionService"
25c29fa5a6Sopenharmony_ci
26c29fa5a6Sopenharmony_cinamespace OHOS {
27c29fa5a6Sopenharmony_cinamespace Msdp {
28c29fa5a6Sopenharmony_cinamespace DeviceStatus {
29c29fa5a6Sopenharmony_ci
30c29fa5a6Sopenharmony_ciIntentionService::IntentionService(IContext *context)
31c29fa5a6Sopenharmony_ci    : context_(context), socketServer_(context), cooperate_(context), drag_(context)
32c29fa5a6Sopenharmony_ci{}
33c29fa5a6Sopenharmony_ci
34c29fa5a6Sopenharmony_ciint32_t IntentionService::Enable(Intention intention, MessageParcel &data, MessageParcel &reply)
35c29fa5a6Sopenharmony_ci{
36c29fa5a6Sopenharmony_ci    CallingContext context {
37c29fa5a6Sopenharmony_ci        .intention = intention,
38c29fa5a6Sopenharmony_ci        .fullTokenId = IPCSkeleton::GetCallingFullTokenID(),
39c29fa5a6Sopenharmony_ci        .tokenId = IPCSkeleton::GetCallingTokenID(),
40c29fa5a6Sopenharmony_ci        .uid = IPCSkeleton::GetCallingUid(),
41c29fa5a6Sopenharmony_ci        .pid = IPCSkeleton::GetCallingPid(),
42c29fa5a6Sopenharmony_ci    };
43c29fa5a6Sopenharmony_ci    CHKPR(context_, RET_ERR);
44c29fa5a6Sopenharmony_ci    int32_t ret = context_->GetDelegateTasks().PostSyncTask([=] {
45c29fa5a6Sopenharmony_ci        IPlugin *plugin = LoadPlugin(context.intention);
46c29fa5a6Sopenharmony_ci        CHKPR(plugin, RET_ERR);
47c29fa5a6Sopenharmony_ci        return plugin->Enable(context, data, reply);
48c29fa5a6Sopenharmony_ci    });
49c29fa5a6Sopenharmony_ci    if (ret != RET_OK) {
50c29fa5a6Sopenharmony_ci        FI_HILOGE("Enable failed, ret:%{public}d", ret);
51c29fa5a6Sopenharmony_ci    }
52c29fa5a6Sopenharmony_ci    return ret;
53c29fa5a6Sopenharmony_ci}
54c29fa5a6Sopenharmony_ci
55c29fa5a6Sopenharmony_ciint32_t IntentionService::Disable(Intention intention, MessageParcel &data, MessageParcel &reply)
56c29fa5a6Sopenharmony_ci{
57c29fa5a6Sopenharmony_ci    CallingContext context {
58c29fa5a6Sopenharmony_ci        .intention = intention,
59c29fa5a6Sopenharmony_ci        .fullTokenId = IPCSkeleton::GetCallingFullTokenID(),
60c29fa5a6Sopenharmony_ci        .tokenId = IPCSkeleton::GetCallingTokenID(),
61c29fa5a6Sopenharmony_ci        .uid = IPCSkeleton::GetCallingUid(),
62c29fa5a6Sopenharmony_ci        .pid = IPCSkeleton::GetCallingPid(),
63c29fa5a6Sopenharmony_ci    };
64c29fa5a6Sopenharmony_ci    CHKPR(context_, RET_ERR);
65c29fa5a6Sopenharmony_ci    int32_t ret = context_->GetDelegateTasks().PostSyncTask([=] {
66c29fa5a6Sopenharmony_ci        IPlugin *plugin = LoadPlugin(context.intention);
67c29fa5a6Sopenharmony_ci        CHKPR(plugin, RET_ERR);
68c29fa5a6Sopenharmony_ci        return plugin->Disable(context, data, reply);
69c29fa5a6Sopenharmony_ci    });
70c29fa5a6Sopenharmony_ci    if (ret != RET_OK) {
71c29fa5a6Sopenharmony_ci        FI_HILOGE("Disable failed, ret:%{public}d", ret);
72c29fa5a6Sopenharmony_ci    }
73c29fa5a6Sopenharmony_ci    return ret;
74c29fa5a6Sopenharmony_ci}
75c29fa5a6Sopenharmony_ci
76c29fa5a6Sopenharmony_ciint32_t IntentionService::Start(Intention intention, MessageParcel &data, MessageParcel &reply)
77c29fa5a6Sopenharmony_ci{
78c29fa5a6Sopenharmony_ci    CallingContext context {
79c29fa5a6Sopenharmony_ci        .intention = intention,
80c29fa5a6Sopenharmony_ci        .fullTokenId = IPCSkeleton::GetCallingFullTokenID(),
81c29fa5a6Sopenharmony_ci        .tokenId = IPCSkeleton::GetCallingTokenID(),
82c29fa5a6Sopenharmony_ci        .uid = IPCSkeleton::GetCallingUid(),
83c29fa5a6Sopenharmony_ci        .pid = IPCSkeleton::GetCallingPid(),
84c29fa5a6Sopenharmony_ci    };
85c29fa5a6Sopenharmony_ci    CHKPR(context_, RET_ERR);
86c29fa5a6Sopenharmony_ci    int32_t ret = context_->GetDelegateTasks().PostSyncTask([=] {
87c29fa5a6Sopenharmony_ci        IPlugin *plugin = LoadPlugin(context.intention);
88c29fa5a6Sopenharmony_ci        CHKPR(plugin, RET_ERR);
89c29fa5a6Sopenharmony_ci        return plugin->Start(context, data, reply);
90c29fa5a6Sopenharmony_ci    });
91c29fa5a6Sopenharmony_ci    if (ret != RET_OK) {
92c29fa5a6Sopenharmony_ci        FI_HILOGE("Start failed, ret:%{public}d", ret);
93c29fa5a6Sopenharmony_ci    }
94c29fa5a6Sopenharmony_ci    return ret;
95c29fa5a6Sopenharmony_ci}
96c29fa5a6Sopenharmony_ci
97c29fa5a6Sopenharmony_ciint32_t IntentionService::Stop(Intention intention, MessageParcel &data, MessageParcel &reply)
98c29fa5a6Sopenharmony_ci{
99c29fa5a6Sopenharmony_ci    CallingContext context {
100c29fa5a6Sopenharmony_ci        .intention = intention,
101c29fa5a6Sopenharmony_ci        .fullTokenId = IPCSkeleton::GetCallingFullTokenID(),
102c29fa5a6Sopenharmony_ci        .tokenId = IPCSkeleton::GetCallingTokenID(),
103c29fa5a6Sopenharmony_ci        .uid = IPCSkeleton::GetCallingUid(),
104c29fa5a6Sopenharmony_ci        .pid = IPCSkeleton::GetCallingPid(),
105c29fa5a6Sopenharmony_ci    };
106c29fa5a6Sopenharmony_ci    CHKPR(context_, RET_ERR);
107c29fa5a6Sopenharmony_ci    int32_t ret = context_->GetDelegateTasks().PostSyncTask([=] {
108c29fa5a6Sopenharmony_ci        IPlugin *plugin = LoadPlugin(context.intention);
109c29fa5a6Sopenharmony_ci        CHKPR(plugin, RET_ERR);
110c29fa5a6Sopenharmony_ci        return plugin->Stop(context, data, reply);
111c29fa5a6Sopenharmony_ci    });
112c29fa5a6Sopenharmony_ci    if (ret != RET_OK) {
113c29fa5a6Sopenharmony_ci        FI_HILOGE("Stop failed, ret:%{public}d", ret);
114c29fa5a6Sopenharmony_ci    }
115c29fa5a6Sopenharmony_ci    return ret;
116c29fa5a6Sopenharmony_ci}
117c29fa5a6Sopenharmony_ci
118c29fa5a6Sopenharmony_ciint32_t IntentionService::AddWatch(Intention intention, uint32_t id, MessageParcel &data, MessageParcel &reply)
119c29fa5a6Sopenharmony_ci{
120c29fa5a6Sopenharmony_ci    CallingContext context {
121c29fa5a6Sopenharmony_ci        .intention = intention,
122c29fa5a6Sopenharmony_ci        .fullTokenId = IPCSkeleton::GetCallingFullTokenID(),
123c29fa5a6Sopenharmony_ci        .tokenId = IPCSkeleton::GetCallingTokenID(),
124c29fa5a6Sopenharmony_ci        .uid = IPCSkeleton::GetCallingUid(),
125c29fa5a6Sopenharmony_ci        .pid = IPCSkeleton::GetCallingPid(),
126c29fa5a6Sopenharmony_ci    };
127c29fa5a6Sopenharmony_ci    CHKPR(context_, RET_ERR);
128c29fa5a6Sopenharmony_ci    int32_t ret = context_->GetDelegateTasks().PostSyncTask([=] {
129c29fa5a6Sopenharmony_ci        IPlugin *plugin = LoadPlugin(context.intention);
130c29fa5a6Sopenharmony_ci        CHKPR(plugin, RET_ERR);
131c29fa5a6Sopenharmony_ci        return plugin->AddWatch(context, id, data, reply);
132c29fa5a6Sopenharmony_ci    });
133c29fa5a6Sopenharmony_ci    if (ret != RET_OK) {
134c29fa5a6Sopenharmony_ci        FI_HILOGE("AddWatch failed, ret:%{public}d", ret);
135c29fa5a6Sopenharmony_ci    }
136c29fa5a6Sopenharmony_ci    return ret;
137c29fa5a6Sopenharmony_ci}
138c29fa5a6Sopenharmony_ci
139c29fa5a6Sopenharmony_ciint32_t IntentionService::RemoveWatch(Intention intention, uint32_t id, MessageParcel &data, MessageParcel &reply)
140c29fa5a6Sopenharmony_ci{
141c29fa5a6Sopenharmony_ci    CallingContext context {
142c29fa5a6Sopenharmony_ci        .intention = intention,
143c29fa5a6Sopenharmony_ci        .fullTokenId = IPCSkeleton::GetCallingFullTokenID(),
144c29fa5a6Sopenharmony_ci        .tokenId = IPCSkeleton::GetCallingTokenID(),
145c29fa5a6Sopenharmony_ci        .uid = IPCSkeleton::GetCallingUid(),
146c29fa5a6Sopenharmony_ci        .pid = IPCSkeleton::GetCallingPid(),
147c29fa5a6Sopenharmony_ci    };
148c29fa5a6Sopenharmony_ci    CHKPR(context_, RET_ERR);
149c29fa5a6Sopenharmony_ci    int32_t ret = context_->GetDelegateTasks().PostSyncTask([=] {
150c29fa5a6Sopenharmony_ci        IPlugin *plugin = LoadPlugin(context.intention);
151c29fa5a6Sopenharmony_ci        CHKPR(plugin, RET_ERR);
152c29fa5a6Sopenharmony_ci        return plugin->RemoveWatch(context, id, data, reply);
153c29fa5a6Sopenharmony_ci    });
154c29fa5a6Sopenharmony_ci    if (ret != RET_OK) {
155c29fa5a6Sopenharmony_ci        FI_HILOGE("RemoveWatch failed, ret:%{public}d", ret);
156c29fa5a6Sopenharmony_ci    }
157c29fa5a6Sopenharmony_ci    return ret;
158c29fa5a6Sopenharmony_ci}
159c29fa5a6Sopenharmony_ci
160c29fa5a6Sopenharmony_ciint32_t IntentionService::SetParam(Intention intention, uint32_t id, MessageParcel &data, MessageParcel &reply)
161c29fa5a6Sopenharmony_ci{
162c29fa5a6Sopenharmony_ci    CallingContext context {
163c29fa5a6Sopenharmony_ci        .intention = intention,
164c29fa5a6Sopenharmony_ci        .fullTokenId = IPCSkeleton::GetCallingFullTokenID(),
165c29fa5a6Sopenharmony_ci        .tokenId = IPCSkeleton::GetCallingTokenID(),
166c29fa5a6Sopenharmony_ci        .uid = IPCSkeleton::GetCallingUid(),
167c29fa5a6Sopenharmony_ci        .pid = IPCSkeleton::GetCallingPid(),
168c29fa5a6Sopenharmony_ci    };
169c29fa5a6Sopenharmony_ci    CHKPR(context_, RET_ERR);
170c29fa5a6Sopenharmony_ci    int32_t ret = context_->GetDelegateTasks().PostSyncTask([=] {
171c29fa5a6Sopenharmony_ci        IPlugin *plugin = LoadPlugin(context.intention);
172c29fa5a6Sopenharmony_ci        CHKPR(plugin, RET_ERR);
173c29fa5a6Sopenharmony_ci        return plugin->SetParam(context, id, data, reply);
174c29fa5a6Sopenharmony_ci    });
175c29fa5a6Sopenharmony_ci    if (ret != RET_OK) {
176c29fa5a6Sopenharmony_ci        FI_HILOGE("SetParam failed, ret:%{public}d", ret);
177c29fa5a6Sopenharmony_ci    }
178c29fa5a6Sopenharmony_ci    return ret;
179c29fa5a6Sopenharmony_ci}
180c29fa5a6Sopenharmony_ci
181c29fa5a6Sopenharmony_ciint32_t IntentionService::GetParam(Intention intention, uint32_t id, MessageParcel &data, MessageParcel &reply)
182c29fa5a6Sopenharmony_ci{
183c29fa5a6Sopenharmony_ci    CallingContext context {
184c29fa5a6Sopenharmony_ci        .intention = intention,
185c29fa5a6Sopenharmony_ci        .fullTokenId = IPCSkeleton::GetCallingFullTokenID(),
186c29fa5a6Sopenharmony_ci        .tokenId = IPCSkeleton::GetCallingTokenID(),
187c29fa5a6Sopenharmony_ci        .uid = IPCSkeleton::GetCallingUid(),
188c29fa5a6Sopenharmony_ci        .pid = IPCSkeleton::GetCallingPid(),
189c29fa5a6Sopenharmony_ci    };
190c29fa5a6Sopenharmony_ci    CHKPR(context_, RET_ERR);
191c29fa5a6Sopenharmony_ci    int32_t ret = context_->GetDelegateTasks().PostSyncTask([=] {
192c29fa5a6Sopenharmony_ci        IPlugin *plugin = LoadPlugin(context.intention);
193c29fa5a6Sopenharmony_ci        CHKPR(plugin, RET_ERR);
194c29fa5a6Sopenharmony_ci        return plugin->GetParam(context, id, data, reply);
195c29fa5a6Sopenharmony_ci    });
196c29fa5a6Sopenharmony_ci    if (ret != RET_OK) {
197c29fa5a6Sopenharmony_ci        FI_HILOGE("GetParam failed, ret:%{public}d", ret);
198c29fa5a6Sopenharmony_ci    }
199c29fa5a6Sopenharmony_ci    return ret;
200c29fa5a6Sopenharmony_ci}
201c29fa5a6Sopenharmony_ci
202c29fa5a6Sopenharmony_ciint32_t IntentionService::Control(Intention intention, uint32_t id, MessageParcel &data, MessageParcel &reply)
203c29fa5a6Sopenharmony_ci{
204c29fa5a6Sopenharmony_ci    CallingContext context {
205c29fa5a6Sopenharmony_ci        .intention = intention,
206c29fa5a6Sopenharmony_ci        .fullTokenId = IPCSkeleton::GetCallingFullTokenID(),
207c29fa5a6Sopenharmony_ci        .tokenId = IPCSkeleton::GetCallingTokenID(),
208c29fa5a6Sopenharmony_ci        .uid = IPCSkeleton::GetCallingUid(),
209c29fa5a6Sopenharmony_ci        .pid = IPCSkeleton::GetCallingPid(),
210c29fa5a6Sopenharmony_ci    };
211c29fa5a6Sopenharmony_ci    CHKPR(context_, RET_ERR);
212c29fa5a6Sopenharmony_ci    int32_t ret = context_->GetDelegateTasks().PostSyncTask([=] {
213c29fa5a6Sopenharmony_ci        IPlugin *plugin = LoadPlugin(context.intention);
214c29fa5a6Sopenharmony_ci        CHKPR(plugin, RET_ERR);
215c29fa5a6Sopenharmony_ci        return plugin->Control(context, id, data, reply);
216c29fa5a6Sopenharmony_ci    });
217c29fa5a6Sopenharmony_ci    if (ret != RET_OK) {
218c29fa5a6Sopenharmony_ci        FI_HILOGE("Control failed, ret:%{public}d", ret);
219c29fa5a6Sopenharmony_ci    }
220c29fa5a6Sopenharmony_ci    return ret;
221c29fa5a6Sopenharmony_ci}
222c29fa5a6Sopenharmony_ci
223c29fa5a6Sopenharmony_ciIPlugin* IntentionService::LoadPlugin(Intention intention)
224c29fa5a6Sopenharmony_ci{
225c29fa5a6Sopenharmony_ci    CALL_DEBUG_ENTER;
226c29fa5a6Sopenharmony_ci    switch (intention) {
227c29fa5a6Sopenharmony_ci        case Intention::SOCKET: {
228c29fa5a6Sopenharmony_ci            return &socketServer_;
229c29fa5a6Sopenharmony_ci        }
230c29fa5a6Sopenharmony_ci        case Intention::STATIONARY: {
231c29fa5a6Sopenharmony_ci            return &stationary_;
232c29fa5a6Sopenharmony_ci        }
233c29fa5a6Sopenharmony_ci        case Intention::COOPERATE: {
234c29fa5a6Sopenharmony_ci            return &cooperate_;
235c29fa5a6Sopenharmony_ci        }
236c29fa5a6Sopenharmony_ci        case Intention::DRAG: {
237c29fa5a6Sopenharmony_ci            return &drag_;
238c29fa5a6Sopenharmony_ci        }
239c29fa5a6Sopenharmony_ci        default: {
240c29fa5a6Sopenharmony_ci            return nullptr;
241c29fa5a6Sopenharmony_ci        }
242c29fa5a6Sopenharmony_ci    }
243c29fa5a6Sopenharmony_ci}
244c29fa5a6Sopenharmony_ci} // namespace DeviceStatus
245c29fa5a6Sopenharmony_ci} // namespace Msdp
246c29fa5a6Sopenharmony_ci} // namespace OHOS
247