1 /*
2 * Copyright (c) 2020 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_client.h"
17
18 #include "ability_errors.h"
19 #include "ability_service_interface.h"
20 #include "ipc_skeleton.h"
21 #include "log.h"
22 #include "samgr_lite.h"
23 #include "want_utils.h"
24
25 namespace OHOS {
26 const unsigned int ERROR_SLEEP_TIMES = 300000;
27 const unsigned int RETRY_TIMES = 20;
28
Callback(void * owner,int code,IpcIo * reply)29 static int32_t Callback(void *owner, int code, IpcIo *reply)
30 {
31 return 0;
32 }
33
Initialize() const34 bool AbilityMsClient::Initialize() const
35 {
36 if (amsProxy_ != nullptr) {
37 return true;
38 }
39 int retry = RETRY_TIMES;
40 while (retry--) {
41 IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi(AMS_SERVICE, AMS_FEATURE);
42 if (iUnknown == nullptr) {
43 HILOG_ERROR(HILOG_MODULE_APP, "iUnknown is null");
44 usleep(ERROR_SLEEP_TIMES); // sleep 300ms
45 continue;
46 }
47
48 (void)iUnknown->QueryInterface(iUnknown, CLIENT_PROXY_VER, (void **)&amsProxy_);
49 if (amsProxy_ == nullptr) {
50 HILOG_ERROR(HILOG_MODULE_APP, "ams proxy is null");
51 usleep(ERROR_SLEEP_TIMES); // sleep 300ms
52 continue;
53 }
54
55 return true;
56 }
57
58 return false;
59 }
60
SchedulerLifecycleDone(uint64_t token,int state) const61 int AbilityMsClient::SchedulerLifecycleDone(uint64_t token, int state) const
62 {
63 if (amsProxy_ == nullptr) {
64 return PARAM_NULL_ERROR;
65 }
66 IpcIo req;
67 char data[MAX_IO_SIZE];
68 IpcIoInit(&req, data, MAX_IO_SIZE, 0);
69 WriteUint64(&req, token);
70 WriteInt32(&req, state);
71 return amsProxy_->Invoke(amsProxy_, ABILITY_TRANSACTION_DONE, &req, nullptr, Callback);
72 }
73
ScheduleAms(const Want * want,uint64_t token,const SvcIdentity * sid,int commandType) const74 int AbilityMsClient::ScheduleAms(const Want *want, uint64_t token, const SvcIdentity *sid, int commandType) const
75 {
76 if (amsProxy_ == nullptr) {
77 return PARAM_NULL_ERROR;
78 }
79 IpcIo req;
80 char data[MAX_IO_SIZE];
81 IpcIoInit(&req, data, MAX_IO_SIZE, 3);
82 if (token != 0) {
83 WriteUint64(&req, token);
84 }
85 if (sid != nullptr) {
86 bool ret = WriteRemoteObject(&req, sid);
87 if (!ret) {
88 return SERIALIZE_ERROR;
89 }
90 #ifdef __LINUX__
91 if (commandType == ATTACH_BUNDLE) {
92 pid_t pid = getpid();
93 WriteUint64(&req, pid);
94 }
95 #endif
96 }
97 if (want != nullptr && !SerializeWant(&req, want)) {
98 return SERIALIZE_ERROR;
99 }
100 return amsProxy_->Invoke(amsProxy_, commandType, &req, nullptr, Callback);
101 }
102 } // namespace OHOS
103