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 "abilityms_slite_client.h"
17
18 #include "ability_errors.h"
19 #include "ability_mgr_service.h"
20 #include "ability_service.h"
21 #include "abilityms_log.h"
22 #include "adapter.h"
23 #include "cmsis_os2.h"
24 #include "los_task.h"
25 #include "samgr_lite.h"
26 #include "securec.h"
27 #include "want_utils.h"
28 #include "utils.h"
29
30 namespace OHOS {
Initialize() const31 bool AbilityMsClient::Initialize() const
32 {
33 if (amsProxy_ != nullptr) {
34 return true;
35 }
36 int retry = RETRY_TIMES;
37 while (retry--) {
38 IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi(AMS_SERVICE, AMS_SLITE_FEATURE);
39 if (iUnknown == nullptr) {
40 HILOG_ERROR(HILOG_MODULE_APP, "iUnknown is null");
41 osDelay(ERROR_SLEEP_TIMES); // sleep 300ms
42 continue;
43 }
44
45 (void)iUnknown->QueryInterface(iUnknown, DEFAULT_VERSION, (void **)&amsProxy_);
46 if (amsProxy_ == nullptr) {
47 HILOG_ERROR(HILOG_MODULE_APP, "ams proxy is null");
48 osDelay(ERROR_SLEEP_TIMES); // sleep 300ms
49 continue;
50 }
51 return true;
52 }
53 return false;
54 }
55
StartAbility(const Want * want) const56 int AbilityMsClient::StartAbility(const Want *want) const
57 {
58 if (want == nullptr || want->element == nullptr || !Initialize()) {
59 return PARAM_CHECK_ERROR;
60 }
61
62 AbilityMgrService *service = AbilityMgrService::GetInstance();
63 if (service == nullptr) {
64 return PARAM_CHECK_ERROR;
65 }
66
67 // 申请want内存,在服务端用完释放
68 Want *info = static_cast<Want *>(AdapterMalloc(sizeof(Want)));
69 if (info == nullptr) {
70 return MEMORY_MALLOC_ERROR;
71 }
72 info->element = nullptr;
73 info->data = nullptr;
74 info->dataLength = 0;
75 SetWantElement(info, *(want->element));
76 SetWantData(info, want->data, want->dataLength);
77 AbilityService::GetInstance().want_ = info;
78 AbilityService::GetInstance().curTask_ = LOS_CurTaskIDGet();
79 Request request = {
80 .msgId = START_ABILITY,
81 .len = 0,
82 .data = nullptr,
83 .msgValue = 0,
84 };
85 return SAMGR_SendRequest(service->GetIdentity(), &request, nullptr);
86 }
87
TerminateAbility(uint64_t token) const88 int AbilityMsClient::TerminateAbility(uint64_t token) const
89 {
90 AbilityMgrService *service = AbilityMgrService::GetInstance();
91 if (service == nullptr) {
92 return PARAM_CHECK_ERROR;
93 }
94 Request request = {
95 .msgId = TERMINATE_ABILITY,
96 .len = 0,
97 .data = nullptr,
98 .msgValue = static_cast<uint32_t>(token & 0xFFFF),
99 };
100 return SAMGR_SendRequest(service->GetIdentity(), &request, nullptr);
101 }
102
SchedulerLifecycleDone(uint64_t token,int state) const103 int AbilityMsClient::SchedulerLifecycleDone(uint64_t token, int state) const
104 {
105 AbilityMgrService *service = AbilityMgrService::GetInstance();
106 if (service == nullptr) {
107 return PARAM_CHECK_ERROR;
108 }
109 Request request = {
110 .msgId = ABILITY_TRANSACTION_DONE,
111 .len = 0,
112 .data = nullptr,
113 .msgValue = static_cast<uint32_t>((token & 0xFFFF) | (state << 16)),
114 };
115 return SAMGR_SendRequest(service->GetIdentity(), &request, nullptr);
116 }
117
ForceStopBundle(uint64_t token) const118 int AbilityMsClient::ForceStopBundle(uint64_t token) const
119 {
120 AbilityMgrService *service = AbilityMgrService::GetInstance();
121 if (service == nullptr) {
122 return PARAM_CHECK_ERROR;
123 }
124 Request request = {
125 .msgId = TERMINATE_APP,
126 .len = 0,
127 .data = nullptr,
128 .msgValue = static_cast<uint32_t>(token & 0xFFFF),
129 };
130 return SAMGR_SendRequest(service->GetIdentity(), &request, nullptr);
131 }
132
GetTopAbility() const133 ElementName *AbilityMsClient::GetTopAbility() const
134 {
135 if (!Initialize()) {
136 return nullptr;
137 }
138 return amsProxy_->GetTopAbility();
139 }
140
ForceStop(char * bundleName) const141 int AbilityMsClient::ForceStop(char *bundleName) const
142 {
143 AbilityMgrService *service = AbilityMgrService::GetInstance();
144 if (service == nullptr) {
145 return PARAM_CHECK_ERROR;
146 }
147 char* name = Utils::Strdup(bundleName);
148 Request request = {
149 .msgId = TERMINATE_APP_BY_BUNDLENAME,
150 .len = (int16)strlen(name),
151 .data = reinterpret_cast<void *>(name),
152 };
153
154 return SAMGR_SendRequest(service->GetIdentity(), &request, nullptr);
155 }
156 } // namespace OHOS
157