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