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 "intention_service.h"
17 
18 #include "ipc_skeleton.h"
19 
20 #include "devicestatus_define.h"
21 #include "i_plugin.h"
22 
23 #undef LOG_TAG
24 #define LOG_TAG "IntentionService"
25 
26 namespace OHOS {
27 namespace Msdp {
28 namespace DeviceStatus {
29 
IntentionService(IContext * context)30 IntentionService::IntentionService(IContext *context)
31     : context_(context), socketServer_(context), cooperate_(context), drag_(context)
32 {}
33 
Enable(Intention intention,MessageParcel & data,MessageParcel & reply)34 int32_t IntentionService::Enable(Intention intention, MessageParcel &data, MessageParcel &reply)
35 {
36     CallingContext context {
37         .intention = intention,
38         .fullTokenId = IPCSkeleton::GetCallingFullTokenID(),
39         .tokenId = IPCSkeleton::GetCallingTokenID(),
40         .uid = IPCSkeleton::GetCallingUid(),
41         .pid = IPCSkeleton::GetCallingPid(),
42     };
43     CHKPR(context_, RET_ERR);
44     int32_t ret = context_->GetDelegateTasks().PostSyncTask([=] {
45         IPlugin *plugin = LoadPlugin(context.intention);
46         CHKPR(plugin, RET_ERR);
47         return plugin->Enable(context, data, reply);
48     });
49     if (ret != RET_OK) {
50         FI_HILOGE("Enable failed, ret:%{public}d", ret);
51     }
52     return ret;
53 }
54 
Disable(Intention intention,MessageParcel & data,MessageParcel & reply)55 int32_t IntentionService::Disable(Intention intention, MessageParcel &data, MessageParcel &reply)
56 {
57     CallingContext context {
58         .intention = intention,
59         .fullTokenId = IPCSkeleton::GetCallingFullTokenID(),
60         .tokenId = IPCSkeleton::GetCallingTokenID(),
61         .uid = IPCSkeleton::GetCallingUid(),
62         .pid = IPCSkeleton::GetCallingPid(),
63     };
64     CHKPR(context_, RET_ERR);
65     int32_t ret = context_->GetDelegateTasks().PostSyncTask([=] {
66         IPlugin *plugin = LoadPlugin(context.intention);
67         CHKPR(plugin, RET_ERR);
68         return plugin->Disable(context, data, reply);
69     });
70     if (ret != RET_OK) {
71         FI_HILOGE("Disable failed, ret:%{public}d", ret);
72     }
73     return ret;
74 }
75 
Start(Intention intention,MessageParcel & data,MessageParcel & reply)76 int32_t IntentionService::Start(Intention intention, MessageParcel &data, MessageParcel &reply)
77 {
78     CallingContext context {
79         .intention = intention,
80         .fullTokenId = IPCSkeleton::GetCallingFullTokenID(),
81         .tokenId = IPCSkeleton::GetCallingTokenID(),
82         .uid = IPCSkeleton::GetCallingUid(),
83         .pid = IPCSkeleton::GetCallingPid(),
84     };
85     CHKPR(context_, RET_ERR);
86     int32_t ret = context_->GetDelegateTasks().PostSyncTask([=] {
87         IPlugin *plugin = LoadPlugin(context.intention);
88         CHKPR(plugin, RET_ERR);
89         return plugin->Start(context, data, reply);
90     });
91     if (ret != RET_OK) {
92         FI_HILOGE("Start failed, ret:%{public}d", ret);
93     }
94     return ret;
95 }
96 
Stop(Intention intention,MessageParcel & data,MessageParcel & reply)97 int32_t IntentionService::Stop(Intention intention, MessageParcel &data, MessageParcel &reply)
98 {
99     CallingContext context {
100         .intention = intention,
101         .fullTokenId = IPCSkeleton::GetCallingFullTokenID(),
102         .tokenId = IPCSkeleton::GetCallingTokenID(),
103         .uid = IPCSkeleton::GetCallingUid(),
104         .pid = IPCSkeleton::GetCallingPid(),
105     };
106     CHKPR(context_, RET_ERR);
107     int32_t ret = context_->GetDelegateTasks().PostSyncTask([=] {
108         IPlugin *plugin = LoadPlugin(context.intention);
109         CHKPR(plugin, RET_ERR);
110         return plugin->Stop(context, data, reply);
111     });
112     if (ret != RET_OK) {
113         FI_HILOGE("Stop failed, ret:%{public}d", ret);
114     }
115     return ret;
116 }
117 
AddWatch(Intention intention,uint32_t id,MessageParcel & data,MessageParcel & reply)118 int32_t IntentionService::AddWatch(Intention intention, uint32_t id, MessageParcel &data, MessageParcel &reply)
119 {
120     CallingContext context {
121         .intention = intention,
122         .fullTokenId = IPCSkeleton::GetCallingFullTokenID(),
123         .tokenId = IPCSkeleton::GetCallingTokenID(),
124         .uid = IPCSkeleton::GetCallingUid(),
125         .pid = IPCSkeleton::GetCallingPid(),
126     };
127     CHKPR(context_, RET_ERR);
128     int32_t ret = context_->GetDelegateTasks().PostSyncTask([=] {
129         IPlugin *plugin = LoadPlugin(context.intention);
130         CHKPR(plugin, RET_ERR);
131         return plugin->AddWatch(context, id, data, reply);
132     });
133     if (ret != RET_OK) {
134         FI_HILOGE("AddWatch failed, ret:%{public}d", ret);
135     }
136     return ret;
137 }
138 
RemoveWatch(Intention intention,uint32_t id,MessageParcel & data,MessageParcel & reply)139 int32_t IntentionService::RemoveWatch(Intention intention, uint32_t id, MessageParcel &data, MessageParcel &reply)
140 {
141     CallingContext context {
142         .intention = intention,
143         .fullTokenId = IPCSkeleton::GetCallingFullTokenID(),
144         .tokenId = IPCSkeleton::GetCallingTokenID(),
145         .uid = IPCSkeleton::GetCallingUid(),
146         .pid = IPCSkeleton::GetCallingPid(),
147     };
148     CHKPR(context_, RET_ERR);
149     int32_t ret = context_->GetDelegateTasks().PostSyncTask([=] {
150         IPlugin *plugin = LoadPlugin(context.intention);
151         CHKPR(plugin, RET_ERR);
152         return plugin->RemoveWatch(context, id, data, reply);
153     });
154     if (ret != RET_OK) {
155         FI_HILOGE("RemoveWatch failed, ret:%{public}d", ret);
156     }
157     return ret;
158 }
159 
SetParam(Intention intention,uint32_t id,MessageParcel & data,MessageParcel & reply)160 int32_t IntentionService::SetParam(Intention intention, uint32_t id, MessageParcel &data, MessageParcel &reply)
161 {
162     CallingContext context {
163         .intention = intention,
164         .fullTokenId = IPCSkeleton::GetCallingFullTokenID(),
165         .tokenId = IPCSkeleton::GetCallingTokenID(),
166         .uid = IPCSkeleton::GetCallingUid(),
167         .pid = IPCSkeleton::GetCallingPid(),
168     };
169     CHKPR(context_, RET_ERR);
170     int32_t ret = context_->GetDelegateTasks().PostSyncTask([=] {
171         IPlugin *plugin = LoadPlugin(context.intention);
172         CHKPR(plugin, RET_ERR);
173         return plugin->SetParam(context, id, data, reply);
174     });
175     if (ret != RET_OK) {
176         FI_HILOGE("SetParam failed, ret:%{public}d", ret);
177     }
178     return ret;
179 }
180 
GetParam(Intention intention,uint32_t id,MessageParcel & data,MessageParcel & reply)181 int32_t IntentionService::GetParam(Intention intention, uint32_t id, MessageParcel &data, MessageParcel &reply)
182 {
183     CallingContext context {
184         .intention = intention,
185         .fullTokenId = IPCSkeleton::GetCallingFullTokenID(),
186         .tokenId = IPCSkeleton::GetCallingTokenID(),
187         .uid = IPCSkeleton::GetCallingUid(),
188         .pid = IPCSkeleton::GetCallingPid(),
189     };
190     CHKPR(context_, RET_ERR);
191     int32_t ret = context_->GetDelegateTasks().PostSyncTask([=] {
192         IPlugin *plugin = LoadPlugin(context.intention);
193         CHKPR(plugin, RET_ERR);
194         return plugin->GetParam(context, id, data, reply);
195     });
196     if (ret != RET_OK) {
197         FI_HILOGE("GetParam failed, ret:%{public}d", ret);
198     }
199     return ret;
200 }
201 
Control(Intention intention,uint32_t id,MessageParcel & data,MessageParcel & reply)202 int32_t IntentionService::Control(Intention intention, uint32_t id, MessageParcel &data, MessageParcel &reply)
203 {
204     CallingContext context {
205         .intention = intention,
206         .fullTokenId = IPCSkeleton::GetCallingFullTokenID(),
207         .tokenId = IPCSkeleton::GetCallingTokenID(),
208         .uid = IPCSkeleton::GetCallingUid(),
209         .pid = IPCSkeleton::GetCallingPid(),
210     };
211     CHKPR(context_, RET_ERR);
212     int32_t ret = context_->GetDelegateTasks().PostSyncTask([=] {
213         IPlugin *plugin = LoadPlugin(context.intention);
214         CHKPR(plugin, RET_ERR);
215         return plugin->Control(context, id, data, reply);
216     });
217     if (ret != RET_OK) {
218         FI_HILOGE("Control failed, ret:%{public}d", ret);
219     }
220     return ret;
221 }
222 
LoadPlugin(Intention intention)223 IPlugin* IntentionService::LoadPlugin(Intention intention)
224 {
225     CALL_DEBUG_ENTER;
226     switch (intention) {
227         case Intention::SOCKET: {
228             return &socketServer_;
229         }
230         case Intention::STATIONARY: {
231             return &stationary_;
232         }
233         case Intention::COOPERATE: {
234             return &cooperate_;
235         }
236         case Intention::DRAG: {
237             return &drag_;
238         }
239         default: {
240             return nullptr;
241         }
242     }
243 }
244 } // namespace DeviceStatus
245 } // namespace Msdp
246 } // namespace OHOS
247